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. */
96 if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
98 sal = find_pc_line (prologue_start, 0);
100 if (sal.line == 0) /* no line info, use current PC */
101 if (prologue_start != entry_point_address ())
102 prologue_end = fi->pc;
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) */
109 prologue_end = prologue_start + 40; /* We're in the boondocks: allow for */
110 /* 16 pushes, an add, and "mv fp,sp" */
112 prologue_end = min (prologue_end, fi->pc);
114 /* Now, search the prologue looking for instructions that setup fp, save
115 rp (and other regs), adjust sp and such. */
118 for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
123 insn = read_memory_unsigned_integer (current_pc, 2);
124 if (insn & 0x8000) /* Four byte instruction? */
127 if ((insn & 0xf0ff) == 0x207f) { /* st reg, @-sp */
129 regno = ((insn >> 8) & 0xf);
130 if (fsr) /* save_regs offset */
131 fsr->regs[regno] = framesize;
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 */
146 insn &= 0x00ffffff; /* positive */
149 else if (insn == 0x1d8f) { /* mv fp, sp */
150 fi->using_frame_pointer = 1; /* fp is now valid */
151 break; /* end of stack adjustments */
154 break; /* anything else isn't prologue */
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.
167 m32r_init_extra_frame_info (fi)
168 struct frame_info *fi;
173 fi->pc = FRAME_SAVED_PC (fi->next);
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);
179 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
180 fi->frame = dummy_frame_stack->sp;
184 if (fi->using_frame_pointer)
185 fi->frame = read_register (FP_REGNUM);
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);
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];
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. */
202 m32r_find_callers_reg (fi, regnum)
203 struct frame_info *fi;
207 /* XXX - Won't work if multiple dummy frames are active */
208 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
212 return dummy_frame_stack->sp;
215 return dummy_frame_stack->fp;
218 return dummy_frame_stack->pc;
221 return dummy_frame_stack->pc;
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);
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.
239 m32r_frame_chain (fi)
240 struct frame_info *fi;
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;
250 /* All we do here is record SP and FP on the call dummy stack */
253 m32r_push_dummy_frame ()
255 struct dummy_frame *dummy_frame;
257 dummy_frame = xmalloc (sizeof (struct dummy_frame));
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;
268 * MISSING FUNCTION HEADER COMMENT
272 m32r_pc_in_call_dummy (pc)
276 return dummy_frame_stack
277 && pc >= CALL_DUMMY_ADDRESS ()
278 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
284 /* Discard from the stack the innermost frame,
285 restoring all saved registers. */
288 m32r_pop_frame (frame)
289 struct frame_info *frame;
294 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
296 struct dummy_frame *dummy_frame;
298 dummy_frame = dummy_frame_stack;
300 error ("Can't pop dummy frame!");
302 dummy_frame_stack = dummy_frame->next;
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);
311 flush_cached_frames ();
317 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
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));
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));
328 write_register (SPI_REGNUM, read_register (SP_REGNUM));
329 /* registers_changed (); */
330 flush_cached_frames ();
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).
342 By the time we get here, stack space has been allocated for the args, but
343 not for the struct return pointer. */
346 m32r_push_arguments (nargs, args, sp, struct_return, struct_addr)
350 unsigned char struct_return;
351 CORE_ADDR struct_addr;
356 argreg = ARG0_REGNUM;
361 write_register (argreg++, struct_addr);
365 for (argnum = 0; argnum < nargs; argnum++)
371 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
372 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
374 store_address (valbuf, 4, VALUE_ADDRESS (*args));
380 len = TYPE_LENGTH (VALUE_TYPE (*args));
381 val = (char *)VALUE_CONTENTS (*args);
385 if (argreg <= ARGLAST_REGNUM)
389 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
390 write_register (argreg, regval);
392 len -= REGISTER_RAW_SIZE (argreg);
393 val += REGISTER_RAW_SIZE (argreg);
398 write_memory (sp + argnum * 4, val, 4);
406 write_register (RP_REGNUM, entry_point_address ());
413 _initialize_m32r_tdep ()
415 tm_print_insn = print_insn_m32r;