Wed Oct 30 18:14:14 1996 Michael Snyder <msnyder@cleaver.cygnus.com>
[binutils-gdb.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 prologue_end = fi->pc;
102 else if (sal.end < prologue_end) /* next line begins after fn end */
103 prologue_end = sal.end; /* (probably means no prologue) */
104 }
105 else
106 prologue_end = prologue_start + 100; /* We're in the boondocks */
107
108 prologue_end = min (prologue_end, fi->pc);
109
110 /* Now, search the prologue looking for instructions that setup fp, save
111 rp (and other regs), adjust sp and such. */
112
113 framesize = 0;
114 memset (fsr->regs, '\000', sizeof fsr->regs);
115
116 for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
117 {
118 int insn;
119 int regno;
120
121 insn = read_memory_unsigned_integer (current_pc, 2);
122 if (insn & 0x80) /* Four byte instruction? */
123 current_pc += 2;
124
125 if ((insn & 0xf0ff) == 0x207f) { /* st reg, @-sp */
126 framesize += 4;
127 regno = ((insn >> 8) & 0xf);
128 fsr->regs[regno] = framesize;
129 }
130 else if ((insn >> 8) == 0x4f) { /* addi sp */
131 framesize += -((char) (insn & 0xff)); /* offset */
132 break; /* end of stack adjustments */
133 }
134 }
135 return framesize;
136 }
137
138 /* This function actually figures out the frame address for a given pc and
139 sp. This is tricky on the v850 because we only use an explicit frame
140 pointer when using alloca(). The only reliable way to get this info is to
141 examine the prologue.
142 */
143
144 void
145 m32r_init_extra_frame_info (fi)
146 struct frame_info *fi;
147 {
148 int reg;
149 int framesize;
150
151 if (fi->next)
152 fi->pc = FRAME_SAVED_PC (fi->next);
153
154 framesize = m32r_scan_prologue (fi, &fi->fsr);
155
156 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
157 fi->frame = dummy_frame_stack->sp;
158 else if (!fi->next)
159 fi->frame = read_register (SP_REGNUM);
160
161 for (reg = 0; reg < NUM_REGS; reg++)
162 if (fi->fsr.regs[reg] != 0)
163 fi->fsr.regs[reg] = fi->frame + framesize - fi->fsr.regs[reg];
164 }
165
166 /* Find the caller of this frame. We do this by seeing if RP_REGNUM is saved
167 in the stack anywhere, otherwise we get it from the registers. */
168
169 CORE_ADDR
170 m32r_find_callers_reg (fi, regnum)
171 struct frame_info *fi;
172 int regnum;
173 {
174 #if 0
175 /* XXX - Won't work if multiple dummy frames are active */
176 if (PC_IN_CALL_DUMMY (fi->pc, NULL, NULL))
177 switch (regnum)
178 {
179 case SP_REGNUM:
180 return dummy_frame_stack->sp;
181 break;
182 case FP_REGNUM:
183 return dummy_frame_stack->fp;
184 break;
185 case RP_REGNUM:
186 return dummy_frame_stack->pc;
187 break;
188 case PC_REGNUM:
189 return dummy_frame_stack->pc;
190 break;
191 }
192
193 #endif
194 for (; fi; fi = fi->next)
195 if (fi->fsr.regs[regnum] != 0)
196 return read_memory_integer (fi->fsr.regs[regnum], 4);
197 return read_register (regnum);
198 }
199
200 /* Given a GDB frame, determine the address of the calling function's frame.
201 This will be used to create a new GDB frame struct, and then
202 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
203 For m32r, simply get the saved FP off the stack.
204 */
205
206 CORE_ADDR
207 m32r_frame_chain (fi)
208 struct frame_info *fi;
209 {
210 CORE_ADDR saved_fp = fi->fsr.regs[FP_REGNUM];
211
212 if (saved_fp == 0)
213 return 0; /* frameless assembly language fn (such as _start) */
214
215 return read_memory_integer (saved_fp, 4);
216 }
217
218 /* All we do here is record SP and FP on the call dummy stack */
219
220 void
221 m32r_push_dummy_frame ()
222 {
223 struct dummy_frame *dummy_frame;
224
225 dummy_frame = xmalloc (sizeof (struct dummy_frame));
226
227 dummy_frame->fp = read_register (FP_REGNUM);
228 dummy_frame->sp = read_register (SP_REGNUM);
229 dummy_frame->rp = read_register (RP_REGNUM);
230 dummy_frame->pc = read_register (PC_REGNUM);
231 dummy_frame->next = dummy_frame_stack;
232 dummy_frame_stack = dummy_frame;
233 }
234
235 /*
236 * MISSING FUNCTION HEADER COMMENT
237 */
238
239 int
240 m32r_pc_in_call_dummy (pc)
241 CORE_ADDR pc;
242 {
243 return dummy_frame_stack
244 && pc >= CALL_DUMMY_ADDRESS ()
245 && pc <= CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK;
246 }
247
248 /* Discard from the stack the innermost frame,
249 restoring all saved registers. */
250
251 struct frame_info *
252 m32r_pop_frame (frame)
253 struct frame_info *frame;
254 {
255 int regnum;
256
257 #if 0
258 if (PC_IN_CALL_DUMMY (frame->pc, NULL, NULL))
259 {
260 struct dummy_frame *dummy_frame;
261
262 dummy_frame = dummy_frame_stack;
263 if (!dummy_frame)
264 error ("Can't pop dummy frame!");
265
266 dummy_frame_stack = dummy_frame->next;
267
268 write_register (FP_REGNUM, dummy_frame->fp);
269 write_register (SP_REGNUM, dummy_frame->sp);
270 write_register (RP_REGNUM, dummy_frame->rp);
271 write_register (PC_REGNUM, dummy_frame->pc);
272
273 free (dummy_frame);
274
275 flush_cached_frames ();
276
277 return NULL;
278 }
279
280 #endif
281 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
282
283 for (regnum = 0; regnum < NUM_REGS; regnum++)
284 if (frame->fsr.regs[regnum] != 0)
285 write_register (regnum,
286 read_memory_integer (frame->fsr.regs[regnum], 4));
287
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 /* registers_changed (); */
294 flush_cached_frames ();
295
296 return NULL;
297 }
298
299 /* Put arguments in the right places, and setup return address register (RP) to
300 point at a convenient place to put a breakpoint. First four args go in
301 R6->R9, subsequent args go into sp + 16 -> sp + ... Structs are passed by
302 reference. 64 bit quantities (doubles and long longs) may be split between
303 the regs and the stack. When calling a function that returns a struct, a
304 pointer to the struct is passed in as a secret first argument (always in R6).
305
306 By the time we get here, stack space has been allocated for the args, but
307 not for the struct return pointer. */
308
309 CORE_ADDR
310 m32r_push_arguments (nargs, args, sp, struct_return, struct_addr)
311 int nargs;
312 value_ptr *args;
313 CORE_ADDR sp;
314 unsigned char struct_return;
315 CORE_ADDR struct_addr;
316 {
317 int argreg;
318 int argnum;
319
320 argreg = ARG0_REGNUM;
321
322 #if 0
323 if (struct_return)
324 {
325 write_register (argreg++, struct_addr);
326 sp -= 4;
327 }
328
329 for (argnum = 0; argnum < nargs; argnum++)
330 {
331 int len;
332 char *val;
333 char valbuf[4];
334
335 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
336 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
337 {
338 store_address (valbuf, 4, VALUE_ADDRESS (*args));
339 len = 4;
340 val = valbuf;
341 }
342 else
343 {
344 len = TYPE_LENGTH (VALUE_TYPE (*args));
345 val = (char *)VALUE_CONTENTS (*args);
346 }
347
348 while (len > 0)
349 if (argreg <= ARGLAST_REGNUM)
350 {
351 CORE_ADDR regval;
352
353 regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
354 write_register (argreg, regval);
355
356 len -= REGISTER_RAW_SIZE (argreg);
357 val += REGISTER_RAW_SIZE (argreg);
358 argreg++;
359 }
360 else
361 {
362 write_memory (sp + argnum * 4, val, 4);
363
364 len -= 4;
365 val += 4;
366 }
367 args++;
368 }
369
370 write_register (RP_REGNUM, entry_point_address ());
371
372 #endif
373 return sp;
374 }
375 \f
376 void
377 _initialize_m32r_tdep ()
378 {
379 tm_print_insn = print_insn_m32r;
380 }