Added target function calls for SH, M32R and H8300.
[external/binutils.git] / gdb / m32r-tdep.c
1 /* Target-dependent code for the Mitsubishi m32r 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 /* Function: frame_find_saved_regs
32    Return the frame_saved_regs structure for the frame.
33    Doesn't really work for dummy frames, but it does pass back
34    an empty frame_saved_regs, so I guess that's better than total failure */
35
36 void 
37 m32r_frame_find_saved_regs PARAMS ((struct frame_info *fi, 
38                                     struct frame_saved_regs *regaddr))
39 {
40   memcpy(regaddr, &fi->fsr, sizeof(struct frame_saved_regs));
41 }
42
43 /* Function: skip_prologue
44    Find end of function prologue */
45
46 CORE_ADDR
47 m32r_skip_prologue (pc)
48      CORE_ADDR pc;
49 {
50   CORE_ADDR func_addr, func_end;
51   struct symtab_and_line sal;
52
53   /* See what the symbol table says */
54
55   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
56     {
57       sal = find_pc_line (func_addr, 0);
58
59       if (sal.line != 0 && sal.end < func_end)
60         return sal.end;
61       else
62         /* Either there's no line info, or the line after the prologue is after
63            the end of the function.  In this case, there probably isn't a
64            prologue.  */
65         return pc;
66     }
67
68   /* We can't find the start of this function, so there's nothing we can do. */
69   return pc;
70 }
71
72 /* Function: scan_prologue
73    This function decodes the target function prologue to determine
74    1) the size of the stack frame, and 2) which registers are saved on it.
75    It saves the offsets of saved regs in the frame_saved_regs argument,
76    and returns the frame size.  */
77
78 static unsigned long
79 m32r_scan_prologue (fi, fsr)
80      struct frame_info *fi;
81      struct frame_saved_regs *fsr;
82 {
83   struct symtab_and_line sal;
84   CORE_ADDR prologue_start, prologue_end, current_pc;
85   unsigned long framesize;
86
87   /* this code essentially duplicates skip_prologue, 
88      but we need the start address below.  */
89
90   if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
91     {
92       sal = find_pc_line (prologue_start, 0);
93
94       if (sal.line == 0)                /* no line info, use current PC */
95         if (prologue_start != entry_point_address ())
96           prologue_end = fi->pc;
97         else
98           return 0;                     /* _start has no frame or prologue */
99       else if (sal.end < prologue_end)  /* next line begins after fn end */
100         prologue_end = sal.end;         /* (probably means no prologue)  */
101     }
102   else
103     prologue_end = prologue_start + 40; /* We're in the boondocks: allow for */
104                                         /* 16 pushes, an add, and "mv fp,sp" */
105
106   prologue_end = min (prologue_end, fi->pc);
107
108   /* Now, search the prologue looking for instructions that setup fp, save
109      rp (and other regs), adjust sp and such. */ 
110
111   framesize = 0;
112   for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
113     {
114       int insn;
115       int regno;
116
117       insn = read_memory_unsigned_integer (current_pc, 2);
118       if (insn & 0x8000)                        /* Four byte instruction? */
119         current_pc += 2;
120
121       if ((insn & 0xf0ff) == 0x207f) {          /* st reg, @-sp */
122         framesize += 4;
123         regno = ((insn >> 8) & 0xf);
124         if (fsr)                                /* save_regs offset */
125           fsr->regs[regno] = framesize;
126       }
127       else if ((insn >> 8) == 0x4f)             /* addi sp, xx */
128         /* add 8 bit sign-extended offset */
129         framesize += -((char) (insn & 0xff));
130       else if (insn == 0x8faf)                  /* add3 sp, sp, xxxx */
131         /* add 16 bit sign-extended offset */
132         framesize += -((short) read_memory_unsigned_integer (current_pc, 2));
133       else if (((insn >> 8) == 0xe4) &&     /* ld24 r4, xxxxxx ;  sub sp, r4 */
134                read_memory_unsigned_integer (current_pc + 2, 2) == 0x0f24)
135         { /* subtract 24 bit sign-extended negative-offset */
136           insn = read_memory_unsigned_integer (current_pc - 2, 4);
137           if (insn & 0x00800000)        /* sign extend */
138             insn  |= 0xff000000;        /* negative */
139           else
140             insn  &= 0x00ffffff;        /* positive */
141           framesize += insn;
142         }
143       else if (insn == 0x1d8f) {        /* mv fp, sp */
144         fi->using_frame_pointer = 1;    /* fp is now valid */
145         break;                          /* end of stack adjustments */
146       }
147       else
148         break;                          /* anything else isn't prologue */
149     }
150   return framesize;
151 }
152
153 /* Function: init_extra_frame_info
154    This function actually figures out the frame address for a given pc and
155    sp.  This is tricky on the m32r because we sometimes don't use an explicit
156    frame pointer, and the previous stack pointer isn't necessarily recorded
157    on the stack.  The only reliable way to get this info is to
158    examine the prologue.  */
159
160 void
161 m32r_init_extra_frame_info (fi)
162      struct frame_info *fi;
163 {
164   int reg;
165
166   if (fi->next)
167     fi->pc = FRAME_SAVED_PC (fi->next);
168
169   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
170
171   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
172     {
173       /* We need to setup fi->frame here because run_stack_dummy gets it wrong
174          by assuming it's always FP.  */
175       fi->frame = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
176       fi->framesize = 0;
177       return;
178     }
179   else 
180     {
181       fi->using_frame_pointer = 0;
182       fi->framesize = m32r_scan_prologue (fi, &fi->fsr);
183
184       if (!fi->next)
185         if (fi->using_frame_pointer)
186           fi->frame = read_register (FP_REGNUM);
187         else
188           fi->frame = read_register (SP_REGNUM);
189       else      /* fi->next means this is not the innermost frame */
190         if (fi->using_frame_pointer)                /* we have an FP */
191           if (fi->next->fsr.regs[FP_REGNUM] != 0)   /* caller saved our FP */
192             fi->frame = read_memory_integer (fi->next->fsr.regs[FP_REGNUM], 4);
193       for (reg = 0; reg < NUM_REGS; reg++)
194         if (fi->fsr.regs[reg] != 0)
195           fi->fsr.regs[reg] = fi->frame + fi->framesize - fi->fsr.regs[reg];
196     }
197 }
198
199 /* Function: find_callers_reg
200    Find REGNUM on the stack.  Otherwise, it's in an active register.  One thing
201    we might want to do here is to check REGNUM against the clobber mask, and
202    somehow flag it as invalid if it isn't saved on the stack somewhere.  This
203    would provide a graceful failure mode when trying to get the value of
204    caller-saves registers for an inner frame.  */
205
206 CORE_ADDR
207 m32r_find_callers_reg (fi, regnum)
208      struct frame_info *fi;
209      int regnum;
210 {
211   for (; fi; fi = fi->next)
212     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
213       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
214     else if (fi->fsr.regs[regnum] != 0)
215       return read_memory_integer (fi->fsr.regs[regnum], 
216                                   REGISTER_RAW_SIZE(regnum));
217   return read_register (regnum);
218 }
219
220 /* Function: frame_chain
221    Given a GDB frame, determine the address of the calling function's frame.
222    This will be used to create a new GDB frame struct, and then
223    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
224    For m32r, we save the frame size when we initialize the frame_info.  */
225
226 CORE_ADDR
227 m32r_frame_chain (fi)
228      struct frame_info *fi;
229 {
230   CORE_ADDR fn_start, callers_pc, fp;
231
232   /* is this a dummy frame? */
233   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
234     return fi->frame;   /* dummy frame same as caller's frame */
235
236   /* is caller-of-this a dummy frame? */
237   callers_pc = FRAME_SAVED_PC(fi);  /* find out who called us: */
238   fp = m32r_find_callers_reg (fi, FP_REGNUM);
239   if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))     
240     return fp;          /* dummy frame's frame may bear no relation to ours */
241
242   if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
243     if (fn_start == entry_point_address ())
244       return 0;         /* in _start fn, don't chain further */
245   return fi->frame + fi->framesize;
246 }
247
248 /* Function: push_return_address (pc)
249    Set up the return address for the inferior function call.
250    Necessary for targets that don't actually execute a JSR/BSR instruction 
251    (ie. when using an empty CALL_DUMMY) */
252
253 CORE_ADDR
254 m32r_push_return_address (pc, sp)
255      CORE_ADDR pc;
256      CORE_ADDR sp;
257 {
258 #if CALL_DUMMY_LOCATION != AT_ENTRY_POINT
259   pc = pc - CALL_DUMMY_START_OFFSET + CALL_DUMMY_BREAKPOINT_OFFSET;
260 #else
261   pc = CALL_DUMMY_ADDRESS ();
262 #endif
263   write_register (RP_REGNUM, pc);
264   return sp;
265 }
266
267
268 /* Function: pop_frame
269    Discard from the stack the innermost frame,
270    restoring all saved registers.  */
271
272 struct frame_info *
273 m32r_pop_frame (frame)
274      struct frame_info *frame;
275 {
276   int regnum;
277
278   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
279     generic_pop_dummy_frame ();
280   else
281     {
282       for (regnum = 0; regnum < NUM_REGS; regnum++)
283         if (frame->fsr.regs[regnum] != 0)
284           write_register (regnum, 
285                           read_memory_integer (frame->fsr.regs[regnum], 4));
286
287       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
288       write_register (SP_REGNUM, read_register (FP_REGNUM));
289       if (read_register (PSW_REGNUM) & 0x80)
290         write_register (SPU_REGNUM, read_register (SP_REGNUM));
291       else
292         write_register (SPI_REGNUM, read_register (SP_REGNUM));
293     }
294   flush_cached_frames ();
295   return NULL;
296 }
297
298 /* Function: frame_saved_pc
299    Find the caller of this frame.  We do this by seeing if RP_REGNUM is saved
300    in the stack anywhere, otherwise we get it from the registers. */
301
302 CORE_ADDR
303 m32r_frame_saved_pc (fi)
304      struct frame_info *fi;
305 {
306   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
307     return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
308   else
309     return m32r_find_callers_reg (fi, RP_REGNUM);
310 }
311
312 /* Function: push_arguments
313    Setup the function arguments for calling a function in the inferior.
314
315    On the Mitsubishi M32R architecture, there are four registers (R0 to R3)
316    which are dedicated for passing function arguments.  Up to the first 
317    four arguments (depending on size) may go into these registers.
318    The rest go on the stack.
319
320    Arguments that are smaller than 4 bytes will still take up a whole
321    register or a whole 32-bit word on the stack, and will be
322    right-justified in the register or the stack word.  This includes
323    chars, shorts, and small aggregate types.
324  
325    Arguments of 8 bytes size are split between two registers, if 
326    available.  If only one register is available, the argument will 
327    be split between the register and the stack.  Otherwise it is
328    passed entirely on the stack.  Aggregate types with sizes between
329    4 and 8 bytes are passed entirely on the stack, and are left-justified
330    within the double-word (as opposed to aggregates smaller than 4 bytes
331    which are right-justified).
332
333    Aggregates of greater than 8 bytes are first copied onto the stack, 
334    and then a pointer to the copy is passed in the place of the normal
335    argument (either in a register if available, or on the stack).
336
337    Functions that must return an aggregate type can return it in the 
338    normal return value registers (R0 and R1) if its size is 8 bytes or
339    less.  For larger return values, the caller must allocate space for 
340    the callee to copy the return value to.  A pointer to this space is
341    passed as an implicit first argument, always in R0. */
342
343 CORE_ADDR
344 m32r_push_arguments (nargs, args, sp, struct_return, struct_addr)
345      int nargs;
346      value_ptr *args;
347      CORE_ADDR sp;
348      unsigned char struct_return;
349      CORE_ADDR struct_addr;
350 {
351   int stack_offset, stack_alloc;
352   int argreg;
353   int argnum;
354   struct type *type;
355   CORE_ADDR regval;
356   char *val;
357   char valbuf[4];
358   int len;
359   int odd_sized_struct;
360
361   /* first force sp to a 4-byte alignment */
362   sp = sp & ~3;
363
364   argreg = ARG0_REGNUM;  
365   /* The "struct return pointer" pseudo-argument goes in R0 */
366   if (struct_return)
367       write_register (argreg++, struct_addr);
368  
369   /* Now make sure there's space on the stack */
370   for (argnum = 0, stack_alloc = 0;
371        argnum < nargs; argnum++)
372     stack_alloc += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
373   sp -= stack_alloc;    /* make room on stack for args */
374  
375  
376   /* Now load as many as possible of the first arguments into
377      registers, and push the rest onto the stack.  There are 16 bytes
378      in four registers available.  Loop thru args from first to last.  */
379  
380   argreg = ARG0_REGNUM;
381   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
382     {
383       type = VALUE_TYPE (args[argnum]);
384       len  = TYPE_LENGTH (type);
385       memset(valbuf, 0, sizeof(valbuf));
386       if (len < 4)
387         { /* value gets right-justified in the register or stack word */
388           memcpy(valbuf + (4 - len),
389                  (char *) VALUE_CONTENTS (args[argnum]), len);
390           val = valbuf;
391         }
392       else
393         val = (char *) VALUE_CONTENTS (args[argnum]);
394  
395       if (len > 4 && (len & 3) != 0)
396         odd_sized_struct = 1;           /* such structs go entirely on stack */
397       else
398         odd_sized_struct = 0;
399       while (len > 0)
400         {
401           if (argreg > ARGLAST_REGNUM || odd_sized_struct)
402             {                           /* must go on the stack */
403               write_memory (sp + stack_offset, val, 4);
404               stack_offset += 4;
405             }
406           /* NOTE WELL!!!!!  This is not an "else if" clause!!!
407              That's because some *&^%$ things get passed on the stack
408              AND in the registers!   */
409           if (argreg <= ARGLAST_REGNUM)
410             {                           /* there's room in a register */
411               regval = extract_address (val, REGISTER_RAW_SIZE(argreg));
412               write_register (argreg++, regval);
413             }
414           /* Store the value 4 bytes at a time.  This means that things
415              larger than 4 bytes may go partly in registers and partly
416              on the stack.  */
417           len -= REGISTER_RAW_SIZE(argreg);
418           val += REGISTER_RAW_SIZE(argreg);
419         }
420     }
421   return sp;
422 }
423
424 /* Function: fix_call_dummy 
425    If there is real CALL_DUMMY code (eg. on the stack), this function
426    has the responsability to insert the address of the actual code that
427    is the target of the target function call.  */
428
429 int
430 m32r_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
431      char *dummy;
432      CORE_ADDR pc;
433      CORE_ADDR fun;
434      int nargs;
435      value_ptr *args;
436      struct type *type;
437      int gcc_p;
438 {
439   /* ld24 r8, <(imm24) fun> */
440   *(unsigned long *) (dummy) = (fun & 0x00ffffff) | 0xe8000000;
441 }
442
443 /* Function: get_saved_register
444    Just call the generic_get_saved_register function.  */
445
446 void
447 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
448      char *raw_buffer;
449      int *optimized;
450      CORE_ADDR *addrp;
451      struct frame_info *frame;
452      int regnum;
453      enum lval_type *lval;
454 {
455   generic_get_saved_register (raw_buffer, optimized, addrp, 
456                               frame, regnum, lval);
457 }
458
459
460 /* Function: m32r_write_sp
461    Because SP is really a read-only register that mirrors either SPU or SPI,
462    we must actually write one of those two as well, depending on PSW. */
463
464 void
465 m32r_write_sp (val)
466      CORE_ADDR val;
467 {
468   unsigned long psw = read_register (PSW_REGNUM);
469
470   if (psw & 0x80)       /* stack mode: user or interrupt */
471     write_register (SPU_REGNUM, val);
472   else
473     write_register (SPI_REGNUM, val);
474   write_register (SP_REGNUM, val);
475 }
476
477 void
478 _initialize_m32r_tdep ()
479 {
480   tm_print_insn = print_insn_m32r;
481 }
482