Fri Jan 3 14:20:05 1997 Geoffrey Noer <noer@cygnus.com>
[binutils-gdb.git] / gdb / mn10300-tdep.c
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2 Copyright 1996, 1997 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 /* Info gleaned from scanning a function's prologue. */
32
33 struct pifsr /* Info about one saved reg */
34 {
35 int framereg; /* Frame reg (SP or FP) */
36 int offset; /* Offset from framereg */
37 int reg; /* Saved register number */
38 };
39
40 struct prologue_info
41 {
42 int framereg;
43 int frameoffset;
44 int start_function;
45 struct pifsr *pifsrs;
46 };
47
48 static CORE_ADDR mn10300_scan_prologue PARAMS ((CORE_ADDR pc,
49 struct prologue_info *fs));
50 \f
51 /* Function: scan_prologue
52 Scan the prologue of the function that contains PC, and record what
53 we find in PI. PI->fsr must be zeroed by the called. Returns the
54 pc after the prologue. Note that the addresses saved in pi->fsr
55 are actually just frame relative (negative offsets from the frame
56 pointer). This is because we don't know the actual value of the
57 frame pointer yet. In some circumstances, the frame pointer can't
58 be determined till after we have scanned the prologue. */
59
60 static CORE_ADDR
61 mn10300_scan_prologue (pc, pi)
62 CORE_ADDR pc;
63 struct prologue_info *pi;
64 {
65 CORE_ADDR func_addr, prologue_end, current_pc;
66 struct pifsr *pifsr;
67 int fp_used;
68
69 printf("mn10300_scan_prologue start\n");
70
71 /* First, figure out the bounds of the prologue so that we can limit the
72 search to something reasonable. */
73
74 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
75 {
76 struct symtab_and_line sal;
77
78 sal = find_pc_line (func_addr, 0);
79
80 if (func_addr == entry_point_address ())
81 pi->start_function = 1;
82 else
83 pi->start_function = 0;
84
85 #if 0
86 if (sal.line == 0)
87 prologue_end = pc;
88 else
89 prologue_end = sal.end;
90 #else
91 prologue_end = pc;
92 #endif
93 }
94 else
95 { /* We're in the boondocks */
96 func_addr = pc - 100;
97 prologue_end = pc;
98 }
99
100 prologue_end = min (prologue_end, pc);
101
102 /* Now, search the prologue looking for instructions that setup fp, save
103 rp, adjust sp and such. We also record the frame offset of any saved
104 registers. */
105
106 pi->frameoffset = 0;
107 pi->framereg = SP_REGNUM;
108 fp_used = 0;
109 pifsr = pi->pifsrs;
110
111 for (current_pc = func_addr; current_pc < prologue_end; current_pc += 2)
112 {
113 int insn;
114
115 insn = read_memory_unsigned_integer (current_pc, 2);
116
117 if ((insn & 0x07c0) == 0x0780 /* jarl or jr */
118 || (insn & 0xffe0) == 0x0060 /* jmp */
119 || (insn & 0x0780) == 0x0580) /* branch */
120 break; /* Ran into end of prologue */
121 if ((insn & 0xffe0) == ((SP_REGNUM << 11) | 0x0240)) /* add <imm>,sp */
122 pi->frameoffset = ((insn & 0x1f) ^ 0x10) - 0x10;
123 else if (insn == ((SP_REGNUM << 11) | 0x0600 | SP_REGNUM)) /* addi <imm>,sp,sp */
124 pi->frameoffset = read_memory_integer (current_pc + 2, 2);
125 else if (insn == ((FP_REGNUM << 11) | 0x0000 | 12)) /* mov r12,fp */
126 {
127 fp_used = 1;
128 pi->framereg = FP_REGNUM;
129 }
130 else if ((insn & 0x07ff) == (0x0760 | SP_REGNUM) /* st.w <reg>,<offset>[sp] */
131 || (fp_used
132 && (insn & 0x07ff) == (0x0760 | FP_REGNUM))) /* st.w <reg>,<offset>[fp] */
133 if (pifsr)
134 {
135 pifsr->framereg = insn & 0x1f;
136 pifsr->reg = (insn >> 11) & 0x1f; /* Extract <reg> */
137
138 pifsr->offset = read_memory_integer (current_pc + 2, 2) & ~1;
139
140 pifsr++;
141 }
142
143 if ((insn & 0x0780) >= 0x0600) /* Four byte instruction? */
144 current_pc += 2;
145 }
146
147 if (pifsr)
148 pifsr->framereg = 0; /* Tie off last entry */
149
150 printf("mn10300_scan_prologue end \n");
151
152 return current_pc;
153 }
154
155 /* Function: init_extra_frame_info
156 Setup the frame's frame pointer, pc, and frame addresses for saved
157 registers. Most of the work is done in scan_prologue().
158
159 Note that when we are called for the last frame (currently active frame),
160 that fi->pc and fi->frame will already be setup. However, fi->frame will
161 be valid only if this routine uses FP. For previous frames, fi-frame will
162 always be correct (since that is derived from mn10300_frame_chain ()).
163
164 We can be called with the PC in the call dummy under two circumstances.
165 First, during normal backtracing, second, while figuring out the frame
166 pointer just prior to calling the target function (see run_stack_dummy). */
167
168 void
169 mn10300_init_extra_frame_info (fi)
170 struct frame_info *fi;
171 {
172 struct prologue_info pi;
173 struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
174 int reg;
175
176 printf("mn10300_init_extra_frame_info start\n");
177
178 if (fi->next)
179 fi->pc = FRAME_SAVED_PC (fi->next);
180
181 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
182
183 /* The call dummy doesn't save any registers on the stack, so we can return
184 now. */
185 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
186 return;
187
188 pi.pifsrs = pifsrs;
189
190 mn10300_scan_prologue (fi->pc, &pi);
191
192 if (!fi->next && pi.framereg == SP_REGNUM)
193 fi->frame = read_register (pi.framereg) - pi.frameoffset;
194
195 for (pifsr = pifsrs; pifsr->framereg; pifsr++)
196 {
197 fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
198
199 if (pifsr->framereg == SP_REGNUM)
200 fi->fsr.regs[pifsr->reg] += pi.frameoffset;
201 }
202
203 printf("mn10300_init_extra_frame_info end\n");
204 }
205
206 /* Function: frame_chain
207 Figure out the frame prior to FI. Unfortunately, this involves
208 scanning the prologue of the caller, which will also be done
209 shortly by mn10300_init_extra_frame_info. For the dummy frame, we
210 just return the stack pointer that was in use at the time the
211 function call was made. */
212
213 CORE_ADDR
214 mn10300_frame_chain (fi)
215 struct frame_info *fi;
216 {
217 struct prologue_info pi;
218 CORE_ADDR callers_pc, fp;
219
220 printf("mn10300_frame_chain start\n");
221
222 /* First, find out who called us */
223 callers_pc = FRAME_SAVED_PC (fi);
224 /* If caller is a call-dummy, then our FP bears no relation to his FP! */
225 fp = mn10300_find_callers_reg (fi, FP_REGNUM);
226 if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))
227 return fp; /* caller is call-dummy: return oldest value of FP */
228
229 /* Caller is NOT a call-dummy, so everything else should just work.
230 Even if THIS frame is a call-dummy! */
231 pi.pifsrs = NULL;
232
233 mn10300_scan_prologue (callers_pc, &pi);
234
235 printf("mn10300_frame_chain end\n");
236
237 if (pi.start_function)
238 return 0; /* Don't chain beyond the start function */
239
240 if (pi.framereg == FP_REGNUM)
241 return mn10300_find_callers_reg (fi, pi.framereg);
242
243 return fi->frame - pi.frameoffset;
244 }
245
246 /* Function: find_callers_reg
247 Find REGNUM on the stack. Otherwise, it's in an active register.
248 One thing we might want to do here is to check REGNUM against the
249 clobber mask, and somehow flag it as invalid if it isn't saved on
250 the stack somewhere. This would provide a graceful failure mode
251 when trying to get the value of caller-saves registers for an inner
252 frame. */
253
254 CORE_ADDR
255 mn10300_find_callers_reg (fi, regnum)
256 struct frame_info *fi;
257 int regnum;
258 {
259 printf("mn10300_find_callers_reg\n");
260
261 for (; fi; fi = fi->next)
262 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
263 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
264 else if (fi->fsr.regs[regnum] != 0)
265 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
266 REGISTER_RAW_SIZE(regnum));
267
268 return read_register (regnum);
269 }
270
271 /* Function: skip_prologue
272 Return the address of the first code past the prologue of the function. */
273
274 CORE_ADDR
275 mn10300_skip_prologue (pc)
276 CORE_ADDR pc;
277 {
278 CORE_ADDR func_addr, func_end;
279
280 printf("mn10300_skip_prologue\n");
281
282 /* See what the symbol table says */
283
284 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
285 {
286 struct symtab_and_line sal;
287
288 sal = find_pc_line (func_addr, 0);
289
290 if (sal.line != 0 && sal.end < func_end)
291 return sal.end;
292 else
293 /* Either there's no line info, or the line after the prologue is after
294 the end of the function. In this case, there probably isn't a
295 prologue. */
296 return pc;
297 }
298
299 /* We can't find the start of this function, so there's nothing we can do. */
300 return pc;
301 }
302
303 /* Function: pop_frame
304 This routine gets called when either the user uses the `return'
305 command, or the call dummy breakpoint gets hit. */
306
307 void
308 mn10300_pop_frame (frame)
309 struct frame_info *frame;
310 {
311 int regnum;
312
313 printf("mn10300_pop_frame start\n");
314
315 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
316 generic_pop_dummy_frame ();
317 else
318 {
319 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
320
321 for (regnum = 0; regnum < NUM_REGS; regnum++)
322 if (frame->fsr.regs[regnum] != 0)
323 write_register (regnum,
324 read_memory_unsigned_integer (frame->fsr.regs[regnum],
325 REGISTER_RAW_SIZE(regnum)));
326
327 write_register (SP_REGNUM, FRAME_FP (frame));
328 }
329
330 flush_cached_frames ();
331
332 printf("mn10300_pop_frame end\n");
333 }
334
335 /* Function: push_arguments
336 Setup arguments for a call to the target. Arguments go in
337 order on the stack.
338 */
339
340 CORE_ADDR
341 mn10300_push_arguments (nargs, args, sp, struct_return, struct_addr)
342 int nargs;
343 value_ptr *args;
344 CORE_ADDR sp;
345 unsigned char struct_return;
346 CORE_ADDR struct_addr;
347 {
348 int argnum = 0;
349 int len = 0;
350 int stack_offset = 0; /* copy args to this offset onto stack */
351
352 printf("mn10300_push_arguments start\n");
353
354 /* First, just for safety, make sure stack is aligned */
355 sp &= ~3;
356
357 /* Now make space on the stack for the args. */
358 for (argnum = 0; argnum < nargs; argnum++)
359 len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
360
361 sp -= len;
362
363 /* Push all arguments onto the stack. */
364 for (argnum = 0; argnum < nargs; argnum++)
365 {
366 int len;
367 char *val;
368
369 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
370 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
371 {
372 /* for now, pretend structs aren't special */
373 len = TYPE_LENGTH (VALUE_TYPE (*args));
374 val = (char *)VALUE_CONTENTS (*args);
375 }
376 else
377 {
378 len = TYPE_LENGTH (VALUE_TYPE (*args));
379 val = (char *)VALUE_CONTENTS (*args);
380 }
381
382 while (len > 0)
383 {
384 write_memory (sp + stack_offset, val, 4);
385
386 len -= 4;
387 val += 4;
388 stack_offset += 4;
389 }
390 args++;
391 }
392
393 printf("mn10300_push_arguments end\n");
394
395 return sp;
396 }
397
398 /* Function: push_return_address (pc)
399 Set up the return address for the inferior function call.
400 Needed for targets where we don't actually execute a JSR/BSR instruction */
401
402 CORE_ADDR
403 mn10300_push_return_address (pc, sp)
404 CORE_ADDR pc;
405 CORE_ADDR sp;
406 {
407 printf("mn10300_push_return_address\n");
408
409 /* write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ()); */
410 return sp;
411 }
412
413 /* Function: frame_saved_pc
414 Find the caller of this frame. We do this by seeing if RP_REGNUM
415 is saved in the stack anywhere, otherwise we get it from the
416 registers. If the inner frame is a dummy frame, return its PC
417 instead of RP, because that's where "caller" of the dummy-frame
418 will be found. */
419
420 CORE_ADDR
421 mn10300_frame_saved_pc (fi)
422 struct frame_info *fi;
423 {
424 printf("mn10300_frame_saved_pc\n");
425
426 /* if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame)) */
427 return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
428 /* else
429 return mn10300_find_callers_reg (fi, RP_REGNUM);
430 */
431 }
432
433 void
434 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
435 char *raw_buffer;
436 int *optimized;
437 CORE_ADDR *addrp;
438 struct frame_info *frame;
439 int regnum;
440 enum lval_type *lval;
441 {
442 printf("get_saved_register\n");
443
444 generic_get_saved_register (raw_buffer, optimized, addrp,
445 frame, regnum, lval);
446 }
447
448 /* Function: fix_call_dummy
449 Pokes the callee function's address into the CALL_DUMMY assembly stub.
450 Assumes that the CALL_DUMMY looks like this:
451 jarl <offset24>, r31
452 trap
453 */
454
455 int
456 mn10300_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
457 char *dummy;
458 CORE_ADDR sp;
459 CORE_ADDR fun;
460 int nargs;
461 value_ptr *args;
462 struct type *type;
463 int gcc_p;
464 {
465 long offset24;
466
467 printf("mn10300_fix_call_dummy start\n");
468
469 offset24 = (long) fun - (long) entry_point_address ();
470 offset24 &= 0x3fffff;
471 offset24 |= 0xff800000; /* jarl <offset24>, r31 */
472
473 store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
474 store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
475
476 printf("mn10300_fix_call_dummy end\n");
477
478 return 0;
479 }
480
481 void
482 _initialize_mn10300_tdep ()
483 {
484 printf("_initialize_mn10300_tdep\n");
485
486 tm_print_insn = print_insn_mn10300;
487 }