Tue Nov 5 10:21:02 1996 Michael Snyder <msnyder@cleaver.cygnus.com>
[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 struct dummy_frame
32 {
33   struct dummy_frame *next;
34
35   CORE_ADDR fp;
36   CORE_ADDR sp;
37   CORE_ADDR rp;
38   CORE_ADDR pc;
39 };
40
41 void 
42 m32r_frame_find_saved_regs PARAMS ((struct frame_info *fi, 
43                                     struct frame_saved_regs *regaddr))
44 {
45   *regaddr = fi->fsr;
46 }
47
48 static struct dummy_frame *dummy_frame_stack = NULL;
49
50 /* Find end of function prologue */
51
52 CORE_ADDR
53 m32r_skip_prologue (pc)
54      CORE_ADDR pc;
55 {
56   CORE_ADDR func_addr, func_end;
57   struct symtab_and_line sal;
58
59   /* See what the symbol table says */
60
61   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
62     {
63       sal = find_pc_line (func_addr, 0);
64
65       if (sal.line != 0 && sal.end < func_end)
66         return sal.end;
67       else
68         /* Either there's no line info, or the line after the prologue is after
69            the end of the function.  In this case, there probably isn't a
70            prologue.  */
71         return pc;
72     }
73
74   /* We can't find the start of this function, so there's nothing we can do. */
75   return pc;
76 }
77
78 /* This function decodes the target function prologue to determine
79    1) the size of the stack frame, and 2) which registers are saved on it.
80    It saves the offsets of saved regs in the frame_saved_regs argument,
81    and returns the frame size.
82 */
83
84 static unsigned long
85 m32r_scan_prologue (fi, fsr)
86      struct frame_info *fi;
87      struct frame_saved_regs *fsr;
88 {
89   struct symtab_and_line sal;
90   CORE_ADDR prologue_start, prologue_end, current_pc;
91   unsigned long framesize;
92
93   /* this code essentially duplicates skip_prologue, 
94      but we need the start address below.  */
95
96   if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
97     {
98       sal = find_pc_line (prologue_start, 0);
99
100       if (sal.line == 0)                /* no line info, use current PC */
101         if (prologue_start != entry_point_address ())
102           prologue_end = fi->pc;
103         else
104           return 0;                     /* _start has no frame or prologue */
105       else if (sal.end < prologue_end)  /* next line begins after fn end */
106         prologue_end = sal.end;         /* (probably means no prologue)  */
107     }
108   else
109     prologue_end = prologue_start + 40; /* We're in the boondocks: allow for */
110                                         /* 16 pushes, an add, and "mv fp,sp" */
111
112   prologue_end = min (prologue_end, fi->pc);
113
114   /* Now, search the prologue looking for instructions that setup fp, save
115      rp (and other regs), adjust sp and such. */ 
116
117   framesize = 0;
118   for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
119     {
120       int insn;
121       int regno;
122
123       insn = read_memory_unsigned_integer (current_pc, 2);
124       if (insn & 0x8000)                        /* Four byte instruction? */
125         current_pc += 2;
126
127       if ((insn & 0xf0ff) == 0x207f) {          /* st reg, @-sp */
128         framesize += 4;
129         regno = ((insn >> 8) & 0xf);
130         if (fsr)                                /* save_regs offset */
131           fsr->regs[regno] = framesize;
132       }
133       else if ((insn >> 8) == 0x4f)             /* addi sp, xx */
134         /* add 8 bit sign-extended offset */
135         framesize += -((char) (insn & 0xff));
136       else if (insn == 0x8faf)                  /* add3 sp, sp, xxxx */
137         /* add 16 bit sign-extended offset */
138         framesize += -((short) read_memory_unsigned_integer (current_pc, 2));
139       else if (((insn >> 8) == 0xe4) &&     /* ld24 r4, xxxxxx ;  sub sp, r4 */
140                read_memory_unsigned_integer (current_pc + 2, 2) == 0x0f24)
141         { /* subtract 24 bit sign-extended negative-offset */
142           insn = read_memory_unsigned_integer (current_pc - 2, 4);
143           if (insn & 0x00800000)        /* sign extend */
144             insn  |= 0xff000000;        /* negative */
145           else
146             insn  &= 0x00ffffff;        /* positive */
147           framesize += insn;
148         }
149       else if (insn == 0x1d8f) {        /* mv fp, sp */
150         fi->using_frame_pointer = 1;    /* fp is now valid */
151         break;                          /* end of stack adjustments */
152       }
153       else
154         break;                          /* anything else isn't prologue */
155     }
156   return framesize;
157 }
158
159 /* This function actually figures out the frame address for a given pc and
160    sp.  This is tricky on the m32r because we sometimes don't use an explicit
161    frame pointer, and the previous stack pointer isn't necessarily recorded
162    on the stack.  The only reliable way to get this info is to
163    examine the prologue.
164 */
165
166 void
167 m32r_init_extra_frame_info (fi)
168      struct frame_info *fi;
169 {
170   int reg;
171
172   if (fi->next)
173     fi->pc = FRAME_SAVED_PC (fi->next);
174
175   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
176   fi->using_frame_pointer = 0;
177   fi->framesize = m32r_scan_prologue (fi, &fi->fsr);
178 #if 0
179   if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
180     fi->frame = dummy_frame_stack->sp;
181   else 
182 #endif
183     if (!fi->next)
184       if (fi->using_frame_pointer)
185         fi->frame = read_register (FP_REGNUM);
186       else
187         fi->frame = read_register (SP_REGNUM);
188     else        /* fi->next means this is not the innermost frame */
189       if (fi->using_frame_pointer)              /* we have an FP */
190         if (fi->next->fsr.regs[FP_REGNUM] != 0) /* caller saved our FP */
191           fi->frame = read_memory_integer (fi->next->fsr.regs[FP_REGNUM], 4);
192
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 /* Find the caller of this frame.  We do this by seeing if RP_REGNUM is saved
199    in the stack anywhere, otherwise we get it from the registers. */
200
201 CORE_ADDR
202 m32r_find_callers_reg (fi, regnum)
203      struct frame_info *fi;
204      int regnum;
205 {
206 #if 0
207   /* XXX - Won't work if multiple dummy frames are active */
208   if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
209     switch (regnum)
210       {
211       case SP_REGNUM:
212         return dummy_frame_stack->sp;
213         break;
214       case FP_REGNUM:
215         return dummy_frame_stack->fp;
216         break;
217       case RP_REGNUM:
218         return dummy_frame_stack->pc;
219         break;
220       case PC_REGNUM:
221         return dummy_frame_stack->pc;
222         break;
223       }
224
225 #endif
226   for (; fi; fi = fi->next)
227     if (fi->fsr.regs[regnum] != 0)
228       return read_memory_integer (fi->fsr.regs[regnum], 4);
229   return read_register (regnum);
230 }
231
232 /* Given a GDB frame, determine the address of the calling function's frame.
233    This will be used to create a new GDB frame struct, and then
234    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
235    For m32r, we save the frame size when we initialize the frame_info.
236  */
237
238 CORE_ADDR
239 m32r_frame_chain (fi)
240      struct frame_info *fi;
241 {
242   CORE_ADDR fn_start;
243
244   if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
245     if (fn_start == entry_point_address ())
246       return 0;         /* in _start fn, don't chain further */
247   return fi->frame + fi->framesize;
248 }
249
250 /* All we do here is record SP and FP on the call dummy stack */
251
252 void
253 m32r_push_dummy_frame ()
254 {
255   struct dummy_frame *dummy_frame;
256
257   dummy_frame = xmalloc (sizeof (struct dummy_frame));
258
259   dummy_frame->fp = read_register (FP_REGNUM);
260   dummy_frame->sp = read_register (SP_REGNUM);
261   dummy_frame->rp = read_register (RP_REGNUM);
262   dummy_frame->pc = read_register (PC_REGNUM);
263   dummy_frame->next = dummy_frame_stack;
264   dummy_frame_stack = dummy_frame;
265 }
266
267 /*
268  * MISSING FUNCTION HEADER COMMENT
269  */
270
271 int
272 m32r_pc_in_call_dummy (pc)
273      CORE_ADDR pc;
274 {
275 #if 0
276   return dummy_frame_stack
277          && pc >= CALL_DUMMY_ADDRESS ()
278          && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
279 #else
280   return 0;
281 #endif
282 }
283
284 /* Discard from the stack the innermost frame,
285    restoring all saved registers.  */
286
287 struct frame_info *
288 m32r_pop_frame (frame)
289      struct frame_info *frame;
290 {
291   int regnum;
292
293 #if 0
294   if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
295     {
296       struct dummy_frame *dummy_frame;
297       
298       dummy_frame = dummy_frame_stack;
299       if (!dummy_frame)
300         error ("Can't pop dummy frame!");
301
302       dummy_frame_stack = dummy_frame->next;
303
304       write_register (FP_REGNUM, dummy_frame->fp);
305       write_register (SP_REGNUM, dummy_frame->sp);
306       write_register (RP_REGNUM, dummy_frame->rp);
307       write_register (PC_REGNUM, dummy_frame->pc);
308
309       free (dummy_frame);
310
311       flush_cached_frames ();
312
313       return NULL;
314     }
315
316 #endif
317   write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
318
319   for (regnum = 0; regnum < NUM_REGS; regnum++)
320     if (frame->fsr.regs[regnum] != 0)
321       write_register (regnum, 
322                       read_memory_integer (frame->fsr.regs[regnum], 4));
323
324   write_register (SP_REGNUM, read_register (FP_REGNUM));
325   if (read_register (PSW_REGNUM) & 0x80)
326     write_register (SPU_REGNUM, read_register (SP_REGNUM));
327   else
328     write_register (SPI_REGNUM, read_register (SP_REGNUM));
329   /*  registers_changed (); */
330   flush_cached_frames ();
331
332   return NULL;
333 }
334
335 /* Put arguments in the right places, and setup return address register (RP) to
336    point at a convenient place to put a breakpoint.  First four args go in
337    R6->R9, subsequent args go into sp + 16 -> sp + ...  Structs are passed by
338    reference.  64 bit quantities (doubles and long longs) may be split between
339    the regs and the stack.  When calling a function that returns a struct, a
340    pointer to the struct is passed in as a secret first argument (always in R6).
341
342    By the time we get here, stack space has been allocated for the args, but
343    not for the struct return pointer.  */
344
345 CORE_ADDR
346 m32r_push_arguments (nargs, args, sp, struct_return, struct_addr)
347      int nargs;
348      value_ptr *args;
349      CORE_ADDR sp;
350      unsigned char struct_return;
351      CORE_ADDR struct_addr;
352 {
353   int argreg;
354   int argnum;
355
356   argreg = ARG0_REGNUM;
357
358 #if 0
359   if (struct_return)
360     {
361       write_register (argreg++, struct_addr);
362       sp -= 4;
363     }
364
365   for (argnum = 0; argnum < nargs; argnum++)
366     {
367       int len;
368       char *val;
369       char valbuf[4];
370
371       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
372           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
373         {
374           store_address (valbuf, 4, VALUE_ADDRESS (*args));
375           len = 4;
376           val = valbuf;
377         }
378       else
379         {
380           len = TYPE_LENGTH (VALUE_TYPE (*args));
381           val = (char *)VALUE_CONTENTS (*args);
382         }
383
384       while (len > 0)
385         if  (argreg <= ARGLAST_REGNUM)
386           {
387             CORE_ADDR regval;
388
389             regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
390             write_register (argreg, regval);
391
392             len -= REGISTER_RAW_SIZE (argreg);
393             val += REGISTER_RAW_SIZE (argreg);
394             argreg++;
395           }
396         else
397           {
398             write_memory (sp + argnum * 4, val, 4);
399
400             len -= 4;
401             val += 4;
402           }
403       args++;
404     }
405
406   write_register (RP_REGNUM, entry_point_address ());
407
408 #endif
409   return sp;
410 }
411 \f
412 void
413 _initialize_m32r_tdep ()
414 {
415   tm_print_insn = print_insn_m32r;
416 }