1 /* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger.
2 Copyright 1997 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"
31 /* The main purpose of this file is dealing with prologues to extract
32 information about stack frames and saved registers.
34 For reference here's how prologues look on the mn10200:
40 Register saves for d2, d3, a3 as needed. Saves start
41 at fp - <size> and work towards higher addresses. Note
42 that the saves are actually done off the stack pointer
43 in the prologue! This makes for smaller code and easier
44 prologue scanning as the displacement fields will never
47 Without frame pointer:
49 Register saves for d2, d3, a3 as needed. Saves start
50 at sp and work towards higher addresses.
53 One day we might keep the stack pointer constant, that won't
54 change the code for prologues, but it will make the frame
55 pointerless case much more common. */
57 /* Analyze the prologue to determine where registers are saved,
58 the end of the prologue, etc etc. Return the end of the prologue
61 We store into FI (if non-null) several tidbits of information:
63 * stack_size -- size of this stack frame. Note that if we stop in
64 certain parts of the prologue/epilogue we may claim the size of the
65 current frame is zero. This happens when the current frame has
66 not been allocated yet or has already been deallocated.
68 * fsr -- Addresses of registers saved in the stack by this frame.
70 * status -- A (relatively) generic status indicator. It's a bitmask
71 with the following bits:
73 MY_FRAME_IN_SP: The base of the current frame is actually in
74 the stack pointer. This can happen for frame pointerless
75 functions, or cases where we're stopped in the prologue/epilogue
76 itself. For these cases mn10200_analyze_prologue will need up
77 update fi->frame before returning or analyzing the register
80 MY_FRAME_IN_FP: The base of the current frame is in the
81 frame pointer register ($a2).
83 CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily
84 in $a0. This can happen if we're stopped in the prologue.
86 NO_MORE_FRAMES: Set this if the current frame is "start" or
87 if the first instruction looks like mov <imm>,sp. This tells
88 frame chain to not bother trying to unwind past this frame. */
90 #define MY_FRAME_IN_SP 0x1
91 #define MY_FRAME_IN_FP 0x2
92 #define CALLER_A2_IN_A0 0x4
93 #define NO_MORE_FRAMES 0x8
96 mn10200_analyze_prologue (fi
, pc
)
97 struct frame_info
*fi
;
100 CORE_ADDR func_addr
, func_end
, addr
, stop
;
101 CORE_ADDR stack_size
;
102 unsigned char buf
[4];
106 /* Use the PC in the frame if it's provided to look up the
107 start of this function. */
108 pc
= (fi
? fi
->pc
: pc
);
110 /* Find the start of this function. */
111 status
= find_pc_partial_function (pc
, &name
, &func_addr
, &func_end
);
113 /* Do nothing if we couldn't find the start of this function or if we're
114 stopped at the first instruction in the prologue. */
118 /* If we're in start, then give up. */
119 if (strcmp (name
, "start") == 0)
121 fi
->status
= NO_MORE_FRAMES
;
125 /* At the start of a function our frame is in the stack pointer. */
127 fi
->status
= MY_FRAME_IN_SP
;
129 /* If we're physically on an RTS instruction, then our frame has already
132 fi->frame is bogus, we need to fix it. */
133 if (fi
&& fi
->pc
+ 1 == func_end
)
135 status
= target_read_memory (fi
->pc
, buf
, 1);
138 if (fi
->next
== NULL
)
139 fi
->frame
= read_sp ();
145 if (fi
->next
== NULL
)
146 fi
->frame
= read_sp ();
151 /* Similarly if we're stopped on the first insn of a prologue as our
152 frame hasn't been allocated yet. */
153 if (fi
&& fi
->pc
== func_addr
)
155 if (fi
->next
== NULL
)
156 fi
->frame
= read_sp ();
160 /* Figure out where to stop scanning. */
161 stop
= fi
? fi
->pc
: func_end
;
163 /* Don't walk off the end of the function. */
164 stop
= stop
> func_end
? func_end
: stop
;
166 /* Start scanning on the first instruction of this function. */
169 status
= target_read_memory (addr
, buf
, 2);
172 if (fi
&& fi
->next
== NULL
&& fi
->status
& MY_FRAME_IN_SP
)
173 fi
->frame
= read_sp ();
177 /* First see if this insn sets the stack pointer; if so, it's something
178 we won't understand, so quit now. */
180 || (buf
[0] == 0xf4 && buf
[1] == 0x77))
183 fi
->status
= NO_MORE_FRAMES
;
187 /* Now see if we have a frame pointer.
189 Search for mov a2,a0 (0xf278)
190 then mov a3,a2 (0xf27e). */
192 if (buf
[0] == 0xf2 && buf
[1] == 0x78)
194 /* Our caller's $a2 will be found in $a0 now. Note it for
197 fi
->status
|= CALLER_A2_IN_A0
;
201 /* We still haven't allocated our local stack. Handle this
202 as if we stopped on the first or last insn of a function. */
203 if (fi
&& fi
->next
== NULL
)
204 fi
->frame
= read_sp ();
208 status
= target_read_memory (addr
, buf
, 2);
211 if (fi
&& fi
->next
== NULL
)
212 fi
->frame
= read_sp ();
215 if (buf
[0] == 0xf2 && buf
[1] == 0x7e)
219 /* Our frame pointer is valid now. */
222 fi
->status
|= MY_FRAME_IN_FP
;
223 fi
->status
&= ~MY_FRAME_IN_SP
;
230 if (fi
&& fi
->next
== NULL
)
231 fi
->frame
= read_sp ();
236 /* Next we should allocate the local frame.
238 Search for add imm8,a3 (0xd3XX)
239 or add imm16,a3 (0xf70bXXXX)
240 or add imm24,a3 (0xf467XXXXXX).
242 If none of the above was found, then this prologue has
243 no stack, and therefore can't have any register saves,
245 status
= target_read_memory (addr
, buf
, 2);
248 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
))
249 fi
->frame
= read_sp ();
254 stack_size
= extract_signed_integer (&buf
[1], 1);
256 fi
->stack_size
= stack_size
;
260 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
))
261 fi
->frame
= read_sp () - stack_size
;
265 else if (buf
[0] == 0xf7 && buf
[1] == 0x0b)
267 status
= target_read_memory (addr
+ 2, buf
, 2);
270 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
))
271 fi
->frame
= read_sp ();
274 stack_size
= extract_signed_integer (buf
, 2);
276 fi
->stack_size
= stack_size
;
280 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
))
281 fi
->frame
= read_sp () - stack_size
;
285 else if (buf
[0] == 0xf4 && buf
[1] == 0x67)
287 status
= target_read_memory (addr
+ 2, buf
, 3);
290 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
))
291 fi
->frame
= read_sp ();
294 stack_size
= extract_signed_integer (buf
, 3);
296 fi
->stack_size
= stack_size
;
300 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
))
301 fi
->frame
= read_sp () - stack_size
;
307 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
))
308 fi
->frame
= read_sp ();
312 /* At this point fi->frame needs to be correct.
314 If MY_FRAME_IN_SP is set and we're the innermost frame, then we
315 need to fix fi->frame so that backtracing, find_frame_saved_regs,
316 etc work correctly. */
317 if (fi
&& fi
->next
== NULL
&& (fi
->status
& MY_FRAME_IN_SP
) != 0)
318 fi
->frame
= read_sp () - fi
->stack_size
;
320 /* And last we have the register saves. These are relatively
321 simple because they're physically done off the stack pointer,
322 and thus the number of different instructions we need to
323 check is greatly reduced because we know the displacements
326 Search for movx d2,(X,a3) (0xf55eXX)
327 then movx d3,(X,a3) (0xf55fXX)
328 then mov a2,(X,a3) (0x5eXX) No frame pointer case
329 or mov a0,(X,a3) (0x5cXX) Frame pointer case. */
331 status
= target_read_memory (addr
, buf
, 2);
334 if (buf
[0] == 0xf5 && buf
[1] == 0x5e)
338 status
= target_read_memory (addr
+ 2, buf
, 1);
341 fi
->fsr
.regs
[2] = (fi
->frame
+ stack_size
342 + extract_signed_integer (buf
, 1));
347 status
= target_read_memory (addr
, buf
, 2);
351 if (buf
[0] == 0xf5 && buf
[1] == 0x5f)
355 status
= target_read_memory (addr
+ 2, buf
, 1);
358 fi
->fsr
.regs
[3] = (fi
->frame
+ stack_size
359 + extract_signed_integer (buf
, 1));
364 status
= target_read_memory (addr
, buf
, 2);
368 if (buf
[0] == 0x5e || buf
[0] == 0x5c)
372 status
= target_read_memory (addr
+ 1, buf
, 1);
375 fi
->fsr
.regs
[6] = (fi
->frame
+ stack_size
376 + extract_signed_integer (buf
, 1));
377 fi
->status
&= ~CALLER_A2_IN_A0
;
387 /* Function: frame_chain
388 Figure out and return the caller's frame pointer given current
391 We don't handle dummy frames yet but we would probably just return the
392 stack pointer that was in use at the time the function call was made? */
395 mn10200_frame_chain (fi
)
396 struct frame_info
*fi
;
398 struct frame_info dummy_frame
;
400 /* Walk through the prologue to determine the stack size,
401 location of saved registers, end of the prologue, etc. */
403 mn10200_analyze_prologue (fi
, (CORE_ADDR
)0);
405 /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES. */
406 if (fi
->status
& NO_MORE_FRAMES
)
409 /* Now that we've analyzed our prologue, determine the frame
410 pointer for our caller.
412 If our caller has a frame pointer, then we need to
413 find the entry value of $a2 to our function.
415 If CALLER_A2_IN_A0, then the chain is in $a0.
417 If fsr.regs[6] is nonzero, then it's at the memory
418 location pointed to by fsr.regs[6].
420 Else it's still in $a2.
422 If our caller does not have a frame pointer, then his
423 frame base is fi->frame + -caller's stack size + 4. */
425 /* The easiest way to get that info is to analyze our caller's frame.
427 So we set up a dummy frame and call mn10200_analyze_prologue to
428 find stuff for us. */
429 dummy_frame
.pc
= FRAME_SAVED_PC (fi
);
430 dummy_frame
.frame
= fi
->frame
;
431 memset (dummy_frame
.fsr
.regs
, '\000', sizeof dummy_frame
.fsr
.regs
);
432 dummy_frame
.status
= 0;
433 dummy_frame
.stack_size
= 0;
434 mn10200_analyze_prologue (&dummy_frame
);
436 if (dummy_frame
.status
& MY_FRAME_IN_FP
)
438 /* Our caller has a frame pointer. So find the frame in $a2, $a0,
441 return (read_memory_integer (fi
->fsr
.regs
[FP_REGNUM
], REGISTER_SIZE
)
443 else if (fi
->status
& CALLER_A2_IN_A0
)
444 return read_register (4);
446 return read_register (FP_REGNUM
);
450 /* Our caller does not have a frame pointer. So his frame starts
451 at the base of our frame (fi->frame) + <his size> + 4 (saved pc). */
452 return fi
->frame
+ -dummy_frame
.stack_size
+ 4;
456 /* Function: skip_prologue
457 Return the address of the first inst past the prologue of the function. */
460 mn10200_skip_prologue (pc
)
463 CORE_ADDR func_addr
, func_end
;
465 /* First check the symbol table. That'll be faster than scanning
466 the prologue instructions if we have debug sybmols. */
467 if (find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
469 struct symtab_and_line sal
;
471 sal
= find_pc_line (func_addr
, 0);
473 if (sal
.line
!= 0 && sal
.end
< func_end
)
476 return mn10200_analyze_prologue (NULL
, pc
);
479 /* We couldn't find the start of this function, do nothing. */
483 /* Function: pop_frame
484 This routine gets called when either the user uses the `return'
485 command, or the call dummy breakpoint gets hit. */
488 mn10200_pop_frame (frame
)
489 struct frame_info
*frame
;
493 if (PC_IN_CALL_DUMMY(frame
->pc
, frame
->frame
, frame
->frame
))
494 generic_pop_dummy_frame ();
497 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
499 /* Restore any saved registers. */
500 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
501 if (frame
->fsr
.regs
[regnum
] != 0)
505 value
= read_memory_unsigned_integer (frame
->fsr
.regs
[regnum
],
506 REGISTER_RAW_SIZE (regnum
));
507 write_register (regnum
, value
);
510 /* Actually cut back the stack. */
511 write_register (SP_REGNUM
, FRAME_FP (frame
));
513 /* Don't we need to set the PC?!? XXX FIXME. */
516 /* Throw away any cached frame information. */
517 flush_cached_frames ();
520 /* Function: push_arguments
521 Setup arguments for a call to the target. Arguments go in
522 order on the stack. */
525 mn10200_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
529 unsigned char struct_return
;
530 CORE_ADDR struct_addr
;
534 int stack_offset
= 0;
536 /* This should be a nop, but align the stack just in case something
537 went wrong. Stacks are two byte aligned on the mn10200. */
540 /* Now make space on the stack for the args.
542 XXX This doesn't appear to handle pass-by-invisible reference
544 for (argnum
= 0; argnum
< nargs
; argnum
++)
545 len
+= ((TYPE_LENGTH (VALUE_TYPE (args
[argnum
])) + 1) & ~1);
547 /* Allocate stack space. */
550 /* Push all arguments onto the stack. */
551 for (argnum
= 0; argnum
< nargs
; argnum
++)
556 /* XXX Check this. What about UNIONS? Size check looks
558 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
559 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
561 /* XXX Wrong, we want a pointer to this argument. */
562 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
563 val
= (char *)VALUE_CONTENTS (*args
);
567 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
568 val
= (char *)VALUE_CONTENTS (*args
);
573 /* XXX This looks wrong; we can have one and two byte args. */
574 write_memory (sp
+ stack_offset
, val
, 2);
586 /* Function: push_return_address (pc)
587 Set up the return address for the inferior function call.
588 Needed for targets where we don't actually execute a JSR/BSR instruction */
591 mn10200_push_return_address (pc
, sp
)
595 unsigned char buf
[4];
597 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
598 write_memory (sp
- 4, buf
, 4);
602 /* Function: store_struct_return (addr,sp)
603 Store the structure value return address for an inferior function
607 mn10200_store_struct_return (addr
, sp
)
611 unsigned char buf1
[4];
612 unsigned char buf2
[4];
614 /* Get the saved PC and hold onto it. */
615 target_read_memory (sp
, buf1
, 4);
617 /* Now push the structure value address. */
618 store_unsigned_integer (buf2
, 4, addr
);
619 write_memory (sp
, buf2
, 4);
621 /* Now push the saved PC back onto the stack. */
622 target_write_memory (sp
- 4, buf1
, 4);
626 /* Function: frame_saved_pc
627 Find the caller of this frame. We do this by seeing if RP_REGNUM
628 is saved in the stack anywhere, otherwise we get it from the
629 registers. If the inner frame is a dummy frame, return its PC
630 instead of RP, because that's where "caller" of the dummy-frame
634 mn10200_frame_saved_pc (fi
)
635 struct frame_info
*fi
;
637 /* The saved PC will always be at the base of the current frame. */
638 return (read_memory_integer (fi
->frame
, REGISTER_SIZE
) & 0xffffff);
642 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
646 struct frame_info
*frame
;
648 enum lval_type
*lval
;
650 generic_get_saved_register (raw_buffer
, optimized
, addrp
,
651 frame
, regnum
, lval
);
654 /* Function: init_extra_frame_info
655 Setup the frame's frame pointer, pc, and frame addresses for saved
656 registers. Most of the work is done in mn10200_analyze_prologue().
658 Note that when we are called for the last frame (currently active frame),
659 that fi->pc and fi->frame will already be setup. However, fi->frame will
660 be valid only if this routine uses FP. For previous frames, fi-frame will
661 always be correct. mn10200_analyze_prologue will fix fi->frame if
664 We can be called with the PC in the call dummy under two circumstances.
665 First, during normal backtracing, second, while figuring out the frame
666 pointer just prior to calling the target function (see run_stack_dummy). */
669 mn10200_init_extra_frame_info (fi
)
670 struct frame_info
*fi
;
673 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
675 memset (fi
->fsr
.regs
, '\000', sizeof fi
->fsr
.regs
);
679 mn10200_analyze_prologue (fi
, 0);
683 _initialize_mn10200_tdep ()
685 tm_print_insn
= print_insn_mn10200
;