1 /* Target-dependent code for the Mitsubishi m32r for GDB, the GNU debugger.
2 Copyright 1996, Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
27 #include "gdb_string.h"
33 struct dummy_frame *next;
42 m32r_frame_find_saved_regs PARAMS ((struct frame_info *fi,
43 struct frame_saved_regs *regaddr))
48 static struct dummy_frame *dummy_frame_stack = NULL;
50 /* Find end of function prologue */
53 m32r_skip_prologue (pc)
56 CORE_ADDR func_addr, func_end;
57 struct symtab_and_line sal;
59 /* See what the symbol table says */
61 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
63 sal = find_pc_line (func_addr, 0);
65 if (sal.line != 0 && sal.end < func_end)
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
74 /* We can't find the start of this function, so there's nothing we can do. */
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.
85 m32r_scan_prologue (fi, fsr)
86 struct frame_info *fi;
87 struct frame_saved_regs *fsr;
89 struct symtab_and_line sal;
90 CORE_ADDR prologue_start, prologue_end, current_pc;
91 unsigned long framesize;
93 /* this code essentially duplicates skip_prologue,
94 but we need the start address below. */
97 memset (fsr->regs, '\000', sizeof fsr->regs);
99 if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
101 sal = find_pc_line (prologue_start, 0);
103 if (sal.line == 0) /* no line info, use current PC */
104 if (prologue_start != entry_point_address ())
105 prologue_end = fi->pc;
107 return 0; /* _start has no frame or prologue */
108 else if (sal.end < prologue_end) /* next line begins after fn end */
109 prologue_end = sal.end; /* (probably means no prologue) */
112 prologue_end = prologue_start + 48; /* We're in the boondocks: allow for */
113 /* 16 pushes, an add, and "mv fp,sp" */
115 prologue_end = min (prologue_end, fi->pc);
117 /* Now, search the prologue looking for instructions that setup fp, save
118 rp (and other regs), adjust sp and such. */
121 for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
126 insn = read_memory_unsigned_integer (current_pc, 2);
127 if (insn & 0x8000) /* Four byte instruction? */
130 if ((insn & 0xf0ff) == 0x207f) { /* st reg, @-sp */
132 regno = ((insn >> 8) & 0xf);
133 if (fsr) /* save_regs offset */
134 fsr->regs[regno] = framesize;
136 else if ((insn >> 8) == 0x4f) /* addi sp, xx */
137 /* add 8 bit sign-extended offset */
138 framesize += -((char) (insn & 0xff));
139 else if (insn == 0x8faf) /* add3 sp, sp, xxxx */
140 /* add 16 bit sign-extended offset */
141 framesize += -((short) read_memory_unsigned_integer (current_pc, 2));
142 else if (((insn >> 8) == 0xe4) && /* ld24 r4, xxxxxx ; sub sp, r4 */
143 read_memory_unsigned_integer (current_pc + 2, 2) == 0x0f24)
144 { /* subtract 24 bit sign-extended negative-offset */
145 insn = read_memory_unsigned_integer (current_pc - 2, 4);
146 if (insn & 0x00800000) /* sign extend */
147 insn |= 0xff000000; /* negative */
149 insn &= 0x00ffffff; /* positive */
152 else if (insn == 0x1d8f) /* mv fp, sp */
153 break; /* end of stack adjustments */
158 /* This function actually figures out the frame address for a given pc and
159 sp. This is tricky on the v850 because we only use an explicit frame
160 pointer when using alloca(). The only reliable way to get this info is to
161 examine the prologue.
165 m32r_init_extra_frame_info (fi)
166 struct frame_info *fi;
172 fi->pc = FRAME_SAVED_PC (fi->next);
174 framesize = m32r_scan_prologue (fi, &fi->fsr);
176 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
177 fi->frame = dummy_frame_stack->sp;
181 fi->frame = read_register (SP_REGNUM);
183 for (reg = 0; reg < NUM_REGS; reg++)
184 if (fi->fsr.regs[reg] != 0)
185 fi->fsr.regs[reg] = fi->frame + framesize - fi->fsr.regs[reg];
188 /* Find the caller of this frame. We do this by seeing if RP_REGNUM is saved
189 in the stack anywhere, otherwise we get it from the registers. */
192 m32r_find_callers_reg (fi, regnum)
193 struct frame_info *fi;
197 /* XXX - Won't work if multiple dummy frames are active */
198 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
202 return dummy_frame_stack->sp;
205 return dummy_frame_stack->fp;
208 return dummy_frame_stack->pc;
211 return dummy_frame_stack->pc;
216 for (; fi; fi = fi->next)
217 if (fi->fsr.regs[regnum] != 0)
218 return read_memory_integer (fi->fsr.regs[regnum], 4);
219 return read_register (regnum);
222 /* Given a GDB frame, determine the address of the calling function's frame.
223 This will be used to create a new GDB frame struct, and then
224 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
225 For m32r, simply get the saved FP off the stack.
229 m32r_frame_chain (fi)
230 struct frame_info *fi;
232 CORE_ADDR saved_fp = fi->fsr.regs[FP_REGNUM];
233 CORE_ADDR fn_start, fn_end;
236 return read_memory_integer (saved_fp, 4);
238 if (find_pc_partial_function (fi->pc, 0, &fn_start, &fn_end))
239 if (fn_start == entry_point_address ())
240 return 0; /* in _start fn, don't chain further */
242 return read_register (FP_REGNUM);
243 else /* in the woods, what to do? */
244 return 0; /* for now, play it safe and give up... */
248 /* All we do here is record SP and FP on the call dummy stack */
251 m32r_push_dummy_frame ()
253 struct dummy_frame *dummy_frame;
255 dummy_frame = xmalloc (sizeof (struct dummy_frame));
257 dummy_frame->fp = read_register (FP_REGNUM);
258 dummy_frame->sp = read_register (SP_REGNUM);
259 dummy_frame->rp = read_register (RP_REGNUM);
260 dummy_frame->pc = read_register (PC_REGNUM);
261 dummy_frame->next = dummy_frame_stack;
262 dummy_frame_stack = dummy_frame;
266 * MISSING FUNCTION HEADER COMMENT
270 m32r_pc_in_call_dummy (pc)
274 return dummy_frame_stack
275 && pc >= CALL_DUMMY_ADDRESS ()
276 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
282 /* Discard from the stack the innermost frame,
283 restoring all saved registers. */
286 m32r_pop_frame (frame)
287 struct frame_info *frame;
292 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
294 struct dummy_frame *dummy_frame;
296 dummy_frame = dummy_frame_stack;
298 error ("Can't pop dummy frame!");
300 dummy_frame_stack = dummy_frame->next;
302 write_register (FP_REGNUM, dummy_frame->fp);
303 write_register (SP_REGNUM, dummy_frame->sp);
304 write_register (RP_REGNUM, dummy_frame->rp);
305 write_register (PC_REGNUM, dummy_frame->pc);
309 flush_cached_frames ();
315 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
317 for (regnum = 0; regnum < NUM_REGS; regnum++)
318 if (frame->fsr.regs[regnum] != 0)
319 write_register (regnum,
320 read_memory_integer (frame->fsr.regs[regnum], 4));
322 write_register (SP_REGNUM, read_register (FP_REGNUM));
323 if (read_register (PSW_REGNUM) & 0x80)
324 write_register (SPU_REGNUM, read_register (SP_REGNUM));
326 write_register (SPI_REGNUM, read_register (SP_REGNUM));
327 /* registers_changed (); */
328 flush_cached_frames ();
333 /* Put arguments in the right places, and setup return address register (RP) to
334 point at a convenient place to put a breakpoint. First four args go in
335 R6->R9, subsequent args go into sp + 16 -> sp + ... Structs are passed by
336 reference. 64 bit quantities (doubles and long longs) may be split between
337 the regs and the stack. When calling a function that returns a struct, a
338 pointer to the struct is passed in as a secret first argument (always in R6).
340 By the time we get here, stack space has been allocated for the args, but
341 not for the struct return pointer. */
344 m32r_push_arguments (nargs, args, sp, struct_return, struct_addr)
348 unsigned char struct_return;
349 CORE_ADDR struct_addr;
354 argreg = ARG0_REGNUM;
359 write_register (argreg++, struct_addr);
363 for (argnum = 0; argnum < nargs; argnum++)
369 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
370 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
372 store_address (valbuf, 4, VALUE_ADDRESS (*args));
378 len = TYPE_LENGTH (VALUE_TYPE (*args));
379 val = (char *)VALUE_CONTENTS (*args);
383 if (argreg <= ARGLAST_REGNUM)
387 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
388 write_register (argreg, regval);
390 len -= REGISTER_RAW_SIZE (argreg);
391 val += REGISTER_RAW_SIZE (argreg);
396 write_memory (sp + argnum * 4, val, 4);
404 write_register (RP_REGNUM, entry_point_address ());
411 _initialize_m32r_tdep ()
413 tm_print_insn = print_insn_m32r;