Recognize -mep mode when used to store the stack frame
[external/binutils.git] / gdb / v850-tdep.c
1 /* Target-dependent code for the NEC V850 for GDB, the GNU debugger.
2    Copyright 1996, Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "obstack.h"
24 #include "target.h"
25 #include "value.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29 #include "symfile.h"
30
31 /* Info gleaned from scanning a function's prologue.  */
32
33 struct pifsr                    /* Info about one saved reg */
34 {
35   int framereg;                 /* Frame reg (SP or FP) */
36   int offset;                   /* Offset from framereg */
37   int reg;                      /* Saved register number */
38 };
39
40 struct prologue_info
41 {
42   int framereg;
43   int frameoffset;
44   int start_function;
45   struct pifsr *pifsrs;
46 };
47
48 static CORE_ADDR v850_scan_prologue PARAMS ((CORE_ADDR pc, 
49                                              struct prologue_info *fs));
50 \f
51 /* Function: scan_prologue
52    Scan the prologue of the function that contains PC, and record what
53    we find in PI.  PI->fsr must be zeroed by the called.  Returns the
54    pc after the prologue.  Note that the addresses saved in pi->fsr
55    are actually just frame relative (negative offsets from the frame
56    pointer).  This is because we don't know the actual value of the
57    frame pointer yet.  In some circumstances, the frame pointer can't
58    be determined till after we have scanned the prologue.  */
59
60 static CORE_ADDR
61 v850_scan_prologue (pc, pi)
62      CORE_ADDR pc;
63      struct prologue_info *pi;
64 {
65   CORE_ADDR func_addr, prologue_end, current_pc;
66   struct pifsr *pifsr;
67   int fp_used;
68   int ep_used;
69
70   /* First, figure out the bounds of the prologue so that we can limit the
71      search to something reasonable.  */
72
73   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
74     {
75       struct symtab_and_line sal;
76
77       sal = find_pc_line (func_addr, 0);
78
79       if (func_addr == entry_point_address ())
80         pi->start_function = 1;
81       else
82         pi->start_function = 0;
83
84 #if 0
85       if (sal.line == 0)
86         prologue_end = pc;
87       else
88         prologue_end = sal.end;
89 #else
90       prologue_end = pc;
91 #endif
92     }
93   else
94     {                           /* We're in the boondocks */
95       func_addr = pc - 100;
96       prologue_end = pc;
97     }
98
99   prologue_end = min (prologue_end, pc);
100
101   /* Now, search the prologue looking for instructions that setup fp, save
102      rp, adjust sp and such.  We also record the frame offset of any saved
103      registers. */ 
104
105   pi->frameoffset = 0;
106   pi->framereg = SP_REGNUM;
107   fp_used = 0;
108   ep_used = 0;
109   pifsr = pi->pifsrs;
110
111   for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
112     {
113       int insn;
114
115       insn = read_memory_unsigned_integer (current_pc, 2);
116
117       if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
118           || (insn & 0xffe0) == 0x0060 /* jmp */
119           || (insn & 0x0780) == 0x0580) /* branch */
120         break;                  /* Ran into end of prologue */
121       if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
122         pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
123       else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
124         pi->frameoffset = read_memory_integer (current_pc + 2, 2);
125       else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
126         {
127           fp_used = 1;
128           pi->framereg = FP_REGNUM;
129         }
130       else if (insn == 0xf003)  /* mov sp,ep */
131         ep_used = 1;
132       else if (insn == 0xf001)  /* mov r1,ep */
133         ep_used = 0;
134       else if (((insn & 0x07ff) == (0x0760 | SP_REGNUM)          /* st.w <reg>,<offset>[sp] */
135                 || (fp_used
136                     && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
137                && pifsr)
138         {
139           pifsr->framereg = insn & 0x1f;
140           pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
141           pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
142           pifsr++;
143         }
144
145       else if (ep_used                  /* sst.w <reg>,<offset>[ep] */
146                && ((insn & 0x0781) == 0x0501)
147                && pifsr)
148         {
149           pifsr->framereg = 3;
150           pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
151           pifsr->offset = (insn & 0x007e) << 2;
152           pifsr++;
153         }
154
155       if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
156         current_pc += 2;
157     }
158
159   if (pifsr)
160     pifsr->framereg = 0;        /* Tie off last entry */
161
162   return current_pc;
163 }
164
165 /* Function: init_extra_frame_info
166    Setup the frame's frame pointer, pc, and frame addresses for saved
167    registers.  Most of the work is done in scan_prologue().
168
169    Note that when we are called for the last frame (currently active frame),
170    that fi->pc and fi->frame will already be setup.  However, fi->frame will
171    be valid only if this routine uses FP.  For previous frames, fi-frame will
172    always be correct (since that is derived from v850_frame_chain ()).
173
174    We can be called with the PC in the call dummy under two circumstances.
175    First, during normal backtracing, second, while figuring out the frame
176    pointer just prior to calling the target function (see run_stack_dummy).  */
177
178 void
179 v850_init_extra_frame_info (fi)
180      struct frame_info *fi;
181 {
182   struct prologue_info pi;
183   struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
184   int reg;
185
186   if (fi->next)
187     fi->pc = FRAME_SAVED_PC (fi->next);
188
189   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
190
191   /* The call dummy doesn't save any registers on the stack, so we can return
192      now.  */
193   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
194       return;
195
196   pi.pifsrs = pifsrs;
197
198   v850_scan_prologue (fi->pc, &pi);
199
200   if (!fi->next && pi.framereg == SP_REGNUM)
201     fi->frame = read_register (pi.framereg) - pi.frameoffset;
202
203   for (pifsr = pifsrs; pifsr->framereg; pifsr++)
204     {
205       fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
206
207       if (pifsr->framereg == SP_REGNUM)
208         fi->fsr.regs[pifsr->reg] += pi.frameoffset;
209     }
210 }
211
212 /* Function: frame_chain
213    Figure out the frame prior to FI.  Unfortunately, this involves
214    scanning the prologue of the caller, which will also be done
215    shortly by v850_init_extra_frame_info.  For the dummy frame, we
216    just return the stack pointer that was in use at the time the
217    function call was made.  */
218
219 CORE_ADDR
220 v850_frame_chain (fi)
221      struct frame_info *fi;
222 {
223   struct prologue_info pi;
224   CORE_ADDR callers_pc, fp;
225
226   /* First, find out who called us */
227   callers_pc = FRAME_SAVED_PC (fi);
228   /* If caller is a call-dummy, then our FP bears no relation to his FP! */
229   fp = v850_find_callers_reg (fi, FP_REGNUM);
230   if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))
231     return fp;  /* caller is call-dummy: return oldest value of FP */
232
233   /* Caller is NOT a call-dummy, so everything else should just work.
234      Even if THIS frame is a call-dummy! */
235   pi.pifsrs = NULL;
236
237   v850_scan_prologue (callers_pc, &pi);
238
239   if (pi.start_function)
240     return 0;                   /* Don't chain beyond the start function */
241
242   if (pi.framereg == FP_REGNUM)
243     return v850_find_callers_reg (fi, pi.framereg);
244
245   return fi->frame - pi.frameoffset;
246 }
247
248 /* Function: find_callers_reg
249    Find REGNUM on the stack.  Otherwise, it's in an active register.
250    One thing we might want to do here is to check REGNUM against the
251    clobber mask, and somehow flag it as invalid if it isn't saved on
252    the stack somewhere.  This would provide a graceful failure mode
253    when trying to get the value of caller-saves registers for an inner
254    frame.  */
255
256 CORE_ADDR
257 v850_find_callers_reg (fi, regnum)
258      struct frame_info *fi;
259      int regnum;
260 {
261   for (; fi; fi = fi->next)
262     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
263       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
264     else if (fi->fsr.regs[regnum] != 0)
265       return read_memory_unsigned_integer (fi->fsr.regs[regnum], 
266                                            REGISTER_RAW_SIZE(regnum));
267
268   return read_register (regnum);
269 }
270
271 /* Function: skip_prologue
272    Return the address of the first code past the prologue of the function.  */
273
274 CORE_ADDR
275 v850_skip_prologue (pc)
276      CORE_ADDR pc;
277 {
278   CORE_ADDR func_addr, func_end;
279
280   /* See what the symbol table says */
281
282   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
283     {
284       struct symtab_and_line sal;
285
286       sal = find_pc_line (func_addr, 0);
287
288       if (sal.line != 0 && sal.end < func_end)
289         return sal.end;
290       else
291         /* Either there's no line info, or the line after the prologue is after
292            the end of the function.  In this case, there probably isn't a
293            prologue.  */
294         return pc;
295     }
296
297 /* We can't find the start of this function, so there's nothing we can do. */
298   return pc;
299 }
300
301 /* Function: pop_frame
302    This routine gets called when either the user uses the `return'
303    command, or the call dummy breakpoint gets hit.  */
304
305 void
306 v850_pop_frame (frame)
307      struct frame_info *frame;
308 {
309   int regnum;
310
311   if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
312     generic_pop_dummy_frame ();
313   else
314     {
315       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
316
317       for (regnum = 0; regnum < NUM_REGS; regnum++)
318         if (frame->fsr.regs[regnum] != 0)
319           write_register (regnum,
320                           read_memory_unsigned_integer (frame->fsr.regs[regnum],
321                                                         REGISTER_RAW_SIZE(regnum)));
322
323       write_register (SP_REGNUM, FRAME_FP (frame));
324     }
325
326   flush_cached_frames ();
327 }
328
329 /* Function: push_arguments
330    Setup arguments and RP for a call to the target.  First four args
331    go in R6->R9, subsequent args go into sp + 16 -> sp + ...  Structs
332    are passed by reference.  64 bit quantities (doubles and long
333    longs) may be split between the regs and the stack.  When calling a
334    function that returns a struct, a pointer to the struct is passed
335    in as a secret first argument (always in R6).
336
337    Stack space for the args has NOT been allocated: that job is up to us.
338    */
339
340 CORE_ADDR
341 v850_push_arguments (nargs, args, sp, struct_return, struct_addr)
342      int nargs;
343      value_ptr *args;
344      CORE_ADDR sp;
345      unsigned char struct_return;
346      CORE_ADDR struct_addr;
347 {
348   int argreg;
349   int argnum;
350   int len = 0;
351   int stack_offset;
352
353   /* First, just for safety, make sure stack is aligned */
354   sp &= ~3;
355
356   /* Now make space on the stack for the args. */
357   for (argnum = 0; argnum < nargs; argnum++)
358     len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
359   sp -= len;    /* possibly over-allocating, but it works... */
360                 /* (you might think we could allocate 16 bytes */
361                 /* less, but the ABI seems to use it all! )  */
362   argreg = ARG0_REGNUM;
363
364   /* the struct_return pointer occupies the first parameter-passing reg */
365   if (struct_return)
366       write_register (argreg++, struct_addr);
367
368   stack_offset = 16;
369   /* The offset onto the stack at which we will start copying parameters
370      (after the registers are used up) begins at 16 rather than at zero.
371      I don't really know why, that's just the way it seems to work.  */
372
373   /* Now load as many as possible of the first arguments into
374      registers, and push the rest onto the stack.  There are 16 bytes
375      in four registers available.  Loop thru args from first to last.  */
376   for (argnum = 0; argnum < nargs; argnum++)
377     {
378       int len;
379       char *val;
380       char valbuf[REGISTER_RAW_SIZE(ARG0_REGNUM)];
381
382       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
383           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
384         {
385           store_address (valbuf, 4, VALUE_ADDRESS (*args));
386           len = 4;
387           val = valbuf;
388         }
389       else
390         {
391           len = TYPE_LENGTH (VALUE_TYPE (*args));
392           val = (char *)VALUE_CONTENTS (*args);
393         }
394
395       while (len > 0)
396         if  (argreg <= ARGLAST_REGNUM)
397           {
398             CORE_ADDR regval;
399
400             regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
401             write_register (argreg, regval);
402
403             len -= REGISTER_RAW_SIZE (argreg);
404             val += REGISTER_RAW_SIZE (argreg);
405             argreg++;
406           }
407         else
408           {
409             write_memory (sp + stack_offset, val, 4);
410
411             len -= 4;
412             val += 4;
413             stack_offset += 4;
414           }
415       args++;
416     }
417   return sp;
418 }
419
420 /* Function: push_return_address (pc)
421    Set up the return address for the inferior function call.
422    Needed for targets where we don't actually execute a JSR/BSR instruction */
423  
424 CORE_ADDR
425 v850_push_return_address (pc, sp)
426      CORE_ADDR pc;
427      CORE_ADDR sp;
428 {
429   write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ());
430   return sp;
431 }
432  
433 /* Function: frame_saved_pc 
434    Find the caller of this frame.  We do this by seeing if RP_REGNUM
435    is saved in the stack anywhere, otherwise we get it from the
436    registers.  If the inner frame is a dummy frame, return its PC
437    instead of RP, because that's where "caller" of the dummy-frame
438    will be found.  */
439
440 CORE_ADDR
441 v850_frame_saved_pc (fi)
442      struct frame_info *fi;
443 {
444   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
445     return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
446   else
447     return v850_find_callers_reg (fi, RP_REGNUM);
448 }
449
450 void
451 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
452      char *raw_buffer;
453      int *optimized;
454      CORE_ADDR *addrp;
455      struct frame_info *frame;
456      int regnum;
457      enum lval_type *lval;
458 {
459   generic_get_saved_register (raw_buffer, optimized, addrp, 
460                               frame, regnum, lval);
461 }
462
463
464 /* Function: fix_call_dummy
465    Pokes the callee function's address into the CALL_DUMMY assembly stub.
466    Assumes that the CALL_DUMMY looks like this:
467         jarl <offset24>, r31
468         trap
469    */
470
471 int
472 v850_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
473      char *dummy;
474      CORE_ADDR sp;
475      CORE_ADDR fun;
476      int nargs;
477      value_ptr *args;
478      struct type *type;
479      int gcc_p;
480 {
481   long offset24;
482
483   offset24 = (long) fun - (long) entry_point_address ();
484   offset24 &= 0x3fffff;
485   offset24 |= 0xff800000;       /* jarl <offset24>, r31 */
486
487   store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
488   store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
489   return 0;
490 }
491
492 void
493 _initialize_v850_tdep ()
494 {
495   tm_print_insn = print_insn_v850;
496 }