1 /* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2001 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,
19 Boston, MA 02111-1307, USA. */
28 #include "gdb_string.h"
34 /* Should call_function allocate stack space for a struct return? */
36 mn10200_use_struct_convention (int gcc_p, struct type *type)
38 return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8);
41 /* The main purpose of this file is dealing with prologues to extract
42 information about stack frames and saved registers.
44 For reference here's how prologues look on the mn10200:
50 Register saves for d2, d3, a1, a2 as needed. Saves start
51 at fp - <size> + <outgoing_args_size> and work towards higher
52 addresses. Note that the saves are actually done off the stack
53 pointer in the prologue! This makes for smaller code and easier
54 prologue scanning as the displacement fields will unlikely
57 Without frame pointer:
59 Register saves for d2, d3, a1, a2 as needed. Saves start
60 at sp + <outgoing_args_size> and work towards higher addresses.
63 add <local size>,sp -- optional
65 add <outgoing_size>,sp -- optional
67 The stack pointer remains constant throughout the life of most
68 functions. As a result the compiler will usually omit the
69 frame pointer, so we must handle frame pointerless functions. */
71 /* Analyze the prologue to determine where registers are saved,
72 the end of the prologue, etc etc. Return the end of the prologue
75 We store into FI (if non-null) several tidbits of information:
77 * stack_size -- size of this stack frame. Note that if we stop in
78 certain parts of the prologue/epilogue we may claim the size of the
79 current frame is zero. This happens when the current frame has
80 not been allocated yet or has already been deallocated.
82 * fsr -- Addresses of registers saved in the stack by this frame.
84 * status -- A (relatively) generic status indicator. It's a bitmask
85 with the following bits:
87 MY_FRAME_IN_SP: The base of the current frame is actually in
88 the stack pointer. This can happen for frame pointerless
89 functions, or cases where we're stopped in the prologue/epilogue
90 itself. For these cases mn10200_analyze_prologue will need up
91 update fi->frame before returning or analyzing the register
94 MY_FRAME_IN_FP: The base of the current frame is in the
95 frame pointer register ($a2).
97 CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily
98 in $a0. This can happen if we're stopped in the prologue.
100 NO_MORE_FRAMES: Set this if the current frame is "start" or
101 if the first instruction looks like mov <imm>,sp. This tells
102 frame chain to not bother trying to unwind past this frame. */
108 #define MY_FRAME_IN_SP 0x1
109 #define MY_FRAME_IN_FP 0x2
110 #define CALLER_A2_IN_A0 0x4
111 #define NO_MORE_FRAMES 0x8
114 mn10200_analyze_prologue (struct frame_info *fi, CORE_ADDR pc)
116 CORE_ADDR func_addr, func_end, addr, stop;
117 CORE_ADDR stack_size = 0;
118 unsigned char buf[4];
121 int out_of_line_prologue = 0;
123 /* Use the PC in the frame if it's provided to look up the
124 start of this function. */
125 pc = (fi ? fi->pc : pc);
127 /* Find the start of this function. */
128 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
130 /* Do nothing if we couldn't find the start of this function or if we're
131 stopped at the first instruction in the prologue. */
135 /* If we're in start, then give up. */
136 if (strcmp (name, "start") == 0)
139 fi->status = NO_MORE_FRAMES;
143 /* At the start of a function our frame is in the stack pointer. */
145 fi->status = MY_FRAME_IN_SP;
147 /* If we're physically on an RTS instruction, then our frame has already
150 fi->frame is bogus, we need to fix it. */
151 if (fi && fi->pc + 1 == func_end)
153 status = target_read_memory (fi->pc, buf, 1);
156 if (fi->next == NULL)
157 fi->frame = read_sp ();
163 if (fi->next == NULL)
164 fi->frame = read_sp ();
169 /* Similarly if we're stopped on the first insn of a prologue as our
170 frame hasn't been allocated yet. */
171 if (fi && fi->pc == func_addr)
173 if (fi->next == NULL)
174 fi->frame = read_sp ();
178 /* Figure out where to stop scanning. */
179 stop = fi ? fi->pc : func_end;
181 /* Don't walk off the end of the function. */
182 stop = stop > func_end ? func_end : stop;
184 /* Start scanning on the first instruction of this function. */
187 status = target_read_memory (addr, buf, 2);
190 if (fi && fi->next == NULL && fi->status & MY_FRAME_IN_SP)
191 fi->frame = read_sp ();
195 /* First see if this insn sets the stack pointer; if so, it's something
196 we won't understand, so quit now. */
198 || (buf[0] == 0xf4 && buf[1] == 0x77))
201 fi->status = NO_MORE_FRAMES;
205 /* Now see if we have a frame pointer.
207 Search for mov a2,a0 (0xf278)
208 then mov a3,a2 (0xf27e). */
210 if (buf[0] == 0xf2 && buf[1] == 0x78)
212 /* Our caller's $a2 will be found in $a0 now. Note it for
215 fi->status |= CALLER_A2_IN_A0;
219 /* We still haven't allocated our local stack. Handle this
220 as if we stopped on the first or last insn of a function. */
221 if (fi && fi->next == NULL)
222 fi->frame = read_sp ();
226 status = target_read_memory (addr, buf, 2);
229 if (fi && fi->next == NULL)
230 fi->frame = read_sp ();
233 if (buf[0] == 0xf2 && buf[1] == 0x7e)
237 /* Our frame pointer is valid now. */
240 fi->status |= MY_FRAME_IN_FP;
241 fi->status &= ~MY_FRAME_IN_SP;
248 if (fi && fi->next == NULL)
249 fi->frame = read_sp ();
254 /* Next we should allocate the local frame.
256 Search for add imm8,a3 (0xd3XX)
257 or add imm16,a3 (0xf70bXXXX)
258 or add imm24,a3 (0xf467XXXXXX).
260 If none of the above was found, then this prologue has
261 no stack, and therefore can't have any register saves,
263 status = target_read_memory (addr, buf, 2);
266 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
267 fi->frame = read_sp ();
272 stack_size = extract_signed_integer (&buf[1], 1);
274 fi->stack_size = stack_size;
278 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
279 fi->frame = read_sp () - stack_size;
283 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
285 status = target_read_memory (addr + 2, buf, 2);
288 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
289 fi->frame = read_sp ();
292 stack_size = extract_signed_integer (buf, 2);
294 fi->stack_size = stack_size;
298 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
299 fi->frame = read_sp () - stack_size;
303 else if (buf[0] == 0xf4 && buf[1] == 0x67)
305 status = target_read_memory (addr + 2, buf, 3);
308 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
309 fi->frame = read_sp ();
312 stack_size = extract_signed_integer (buf, 3);
314 fi->stack_size = stack_size;
318 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
319 fi->frame = read_sp () - stack_size;
324 /* Now see if we have a call to __prologue for an out of line
326 status = target_read_memory (addr, buf, 2);
330 /* First check for 16bit pc-relative call to __prologue. */
334 status = target_read_memory (addr + 1, buf, 2);
337 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
338 fi->frame = read_sp ();
342 /* Get the PC this instruction will branch to. */
343 temp = (extract_signed_integer (buf, 2) + addr + 3) & 0xffffff;
345 /* Get the name of the function at the target address. */
346 status = find_pc_partial_function (temp, &name, NULL, NULL);
349 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
350 fi->frame = read_sp ();
354 /* Note if it is an out of line prologue. */
355 out_of_line_prologue = (strcmp (name, "__prologue") == 0);
357 /* This sucks up 3 bytes of instruction space. */
358 if (out_of_line_prologue)
363 if (fi && fi->next == NULL)
365 fi->stack_size -= 16;
366 fi->frame = read_sp () - fi->stack_size;
371 /* Now check for the 24bit pc-relative call to __prologue. */
372 else if (buf[0] == 0xf4 && buf[1] == 0xe1)
375 status = target_read_memory (addr + 2, buf, 3);
378 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
379 fi->frame = read_sp ();
383 /* Get the PC this instruction will branch to. */
384 temp = (extract_signed_integer (buf, 3) + addr + 5) & 0xffffff;
386 /* Get the name of the function at the target address. */
387 status = find_pc_partial_function (temp, &name, NULL, NULL);
390 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
391 fi->frame = read_sp ();
395 /* Note if it is an out of line prologue. */
396 out_of_line_prologue = (strcmp (name, "__prologue") == 0);
398 /* This sucks up 5 bytes of instruction space. */
399 if (out_of_line_prologue)
404 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
406 fi->stack_size -= 16;
407 fi->frame = read_sp () - fi->stack_size;
413 /* Now actually handle the out of line prologue. */
414 if (out_of_line_prologue)
416 int outgoing_args_size = 0;
418 /* First adjust the stack size for this function. The out of
419 line prologue saves 4 registers (16bytes of data). */
421 fi->stack_size -= 16;
423 /* Update fi->frame if necessary. */
424 if (fi && fi->next == NULL)
425 fi->frame = read_sp () - fi->stack_size;
427 /* After the out of line prologue, there may be another
428 stack adjustment for the outgoing arguments.
430 Search for add imm8,a3 (0xd3XX)
431 or add imm16,a3 (0xf70bXXXX)
432 or add imm24,a3 (0xf467XXXXXX). */
434 status = target_read_memory (addr, buf, 2);
439 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
440 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
441 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
442 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
449 outgoing_args_size = extract_signed_integer (&buf[1], 1);
452 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
454 status = target_read_memory (addr + 2, buf, 2);
459 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
460 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
461 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
462 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
466 outgoing_args_size = extract_signed_integer (buf, 2);
469 else if (buf[0] == 0xf4 && buf[1] == 0x67)
471 status = target_read_memory (addr + 2, buf, 3);
474 if (fi && fi->next == NULL)
476 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
477 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
478 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
479 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
483 outgoing_args_size = extract_signed_integer (buf, 3);
487 outgoing_args_size = 0;
489 /* Now that we know the size of the outgoing arguments, fix
490 fi->frame again if this is the innermost frame. */
491 if (fi && fi->next == NULL)
492 fi->frame -= outgoing_args_size;
494 /* Note the register save information and update the stack
495 size for this frame too. */
498 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
499 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
500 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
501 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
502 fi->stack_size += outgoing_args_size;
504 /* There can be no more prologue insns, so return now. */
508 /* At this point fi->frame needs to be correct.
510 If MY_FRAME_IN_SP is set and we're the innermost frame, then we
511 need to fix fi->frame so that backtracing, find_frame_saved_regs,
512 etc work correctly. */
513 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP) != 0)
514 fi->frame = read_sp () - fi->stack_size;
516 /* And last we have the register saves. These are relatively
517 simple because they're physically done off the stack pointer,
518 and thus the number of different instructions we need to
519 check is greatly reduced because we know the displacements
522 Search for movx d2,(X,a3) (0xf55eXX)
523 then movx d3,(X,a3) (0xf55fXX)
524 then mov a1,(X,a3) (0x5dXX) No frame pointer case
525 then mov a2,(X,a3) (0x5eXX) No frame pointer case
526 or mov a0,(X,a3) (0x5cXX) Frame pointer case. */
528 status = target_read_memory (addr, buf, 2);
531 if (buf[0] == 0xf5 && buf[1] == 0x5e)
535 status = target_read_memory (addr + 2, buf, 1);
538 fi->fsr.regs[2] = (fi->frame + stack_size
539 + extract_signed_integer (buf, 1));
544 status = target_read_memory (addr, buf, 2);
548 if (buf[0] == 0xf5 && buf[1] == 0x5f)
552 status = target_read_memory (addr + 2, buf, 1);
555 fi->fsr.regs[3] = (fi->frame + stack_size
556 + extract_signed_integer (buf, 1));
561 status = target_read_memory (addr, buf, 2);
569 status = target_read_memory (addr + 1, buf, 1);
572 fi->fsr.regs[5] = (fi->frame + stack_size
573 + extract_signed_integer (buf, 1));
578 status = target_read_memory (addr, buf, 2);
582 if (buf[0] == 0x5e || buf[0] == 0x5c)
586 status = target_read_memory (addr + 1, buf, 1);
589 fi->fsr.regs[6] = (fi->frame + stack_size
590 + extract_signed_integer (buf, 1));
591 fi->status &= ~CALLER_A2_IN_A0;
601 /* Function: frame_chain
602 Figure out and return the caller's frame pointer given current
605 We don't handle dummy frames yet but we would probably just return the
606 stack pointer that was in use at the time the function call was made? */
609 mn10200_frame_chain (struct frame_info *fi)
611 struct frame_info dummy_frame;
613 /* Walk through the prologue to determine the stack size,
614 location of saved registers, end of the prologue, etc. */
616 mn10200_analyze_prologue (fi, (CORE_ADDR) 0);
618 /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES. */
619 if (fi->status & NO_MORE_FRAMES)
622 /* Now that we've analyzed our prologue, determine the frame
623 pointer for our caller.
625 If our caller has a frame pointer, then we need to
626 find the entry value of $a2 to our function.
628 If CALLER_A2_IN_A0, then the chain is in $a0.
630 If fsr.regs[6] is nonzero, then it's at the memory
631 location pointed to by fsr.regs[6].
633 Else it's still in $a2.
635 If our caller does not have a frame pointer, then his
636 frame base is fi->frame + -caller's stack size + 4. */
638 /* The easiest way to get that info is to analyze our caller's frame.
640 So we set up a dummy frame and call mn10200_analyze_prologue to
641 find stuff for us. */
642 dummy_frame.pc = FRAME_SAVED_PC (fi);
643 dummy_frame.frame = fi->frame;
644 memset (dummy_frame.fsr.regs, '\000', sizeof dummy_frame.fsr.regs);
645 dummy_frame.status = 0;
646 dummy_frame.stack_size = 0;
647 mn10200_analyze_prologue (&dummy_frame, 0);
649 if (dummy_frame.status & MY_FRAME_IN_FP)
651 /* Our caller has a frame pointer. So find the frame in $a2, $a0,
654 return (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE)
656 else if (fi->status & CALLER_A2_IN_A0)
657 return read_register (4);
659 return read_register (FP_REGNUM);
663 /* Our caller does not have a frame pointer. So his frame starts
664 at the base of our frame (fi->frame) + <his size> + 4 (saved pc). */
665 return fi->frame + -dummy_frame.stack_size + 4;
669 /* Function: skip_prologue
670 Return the address of the first inst past the prologue of the function. */
673 mn10200_skip_prologue (CORE_ADDR pc)
675 /* We used to check the debug symbols, but that can lose if
676 we have a null prologue. */
677 return mn10200_analyze_prologue (NULL, pc);
680 /* Function: pop_frame
681 This routine gets called when either the user uses the `return'
682 command, or the call dummy breakpoint gets hit. */
685 mn10200_pop_frame (struct frame_info *frame)
689 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
690 generic_pop_dummy_frame ();
693 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
695 /* Restore any saved registers. */
696 for (regnum = 0; regnum < NUM_REGS; regnum++)
697 if (frame->fsr.regs[regnum] != 0)
701 value = read_memory_unsigned_integer (frame->fsr.regs[regnum],
702 REGISTER_RAW_SIZE (regnum));
703 write_register (regnum, value);
706 /* Actually cut back the stack. */
707 write_register (SP_REGNUM, FRAME_FP (frame));
709 /* Don't we need to set the PC?!? XXX FIXME. */
712 /* Throw away any cached frame information. */
713 flush_cached_frames ();
716 /* Function: push_arguments
717 Setup arguments for a call to the target. Arguments go in
718 order on the stack. */
721 mn10200_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
722 unsigned char struct_return, CORE_ADDR struct_addr)
726 int stack_offset = 0;
727 int regsused = struct_return ? 1 : 0;
729 /* This should be a nop, but align the stack just in case something
730 went wrong. Stacks are two byte aligned on the mn10200. */
733 /* Now make space on the stack for the args.
735 XXX This doesn't appear to handle pass-by-invisible reference
737 for (argnum = 0; argnum < nargs; argnum++)
739 int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 1) & ~1;
741 /* If we've used all argument registers, then this argument is
743 if (regsused >= 2 || arg_length > 4)
748 /* We know we've got some arg register space left. If this argument
749 will fit entirely in regs, then put it there. */
750 else if (arg_length <= 2
751 || TYPE_CODE (VALUE_TYPE (args[argnum])) == TYPE_CODE_PTR)
755 else if (regsused == 0)
766 /* Allocate stack space. */
769 regsused = struct_return ? 1 : 0;
770 /* Push all arguments onto the stack. */
771 for (argnum = 0; argnum < nargs; argnum++)
776 /* XXX Check this. What about UNIONS? */
777 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
778 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
780 /* XXX Wrong, we want a pointer to this argument. */
781 len = TYPE_LENGTH (VALUE_TYPE (*args));
782 val = (char *) VALUE_CONTENTS (*args);
786 len = TYPE_LENGTH (VALUE_TYPE (*args));
787 val = (char *) VALUE_CONTENTS (*args);
792 || TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_PTR))
794 write_register (regsused, extract_unsigned_integer (val, 4));
797 else if (regsused == 0 && len == 4)
799 write_register (regsused, extract_unsigned_integer (val, 2));
800 write_register (regsused + 1, extract_unsigned_integer (val + 2, 2));
808 write_memory (sp + stack_offset, val, 2);
821 /* Function: push_return_address (pc)
822 Set up the return address for the inferior function call.
823 Needed for targets where we don't actually execute a JSR/BSR instruction */
826 mn10200_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
828 unsigned char buf[4];
830 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
831 write_memory (sp - 4, buf, 4);
835 /* Function: store_struct_return (addr,sp)
836 Store the structure value return address for an inferior function
840 mn10200_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
842 /* The structure return address is passed as the first argument. */
843 write_register (0, addr);
847 /* Function: frame_saved_pc
848 Find the caller of this frame. We do this by seeing if RP_REGNUM
849 is saved in the stack anywhere, otherwise we get it from the
850 registers. If the inner frame is a dummy frame, return its PC
851 instead of RP, because that's where "caller" of the dummy-frame
855 mn10200_frame_saved_pc (struct frame_info *fi)
857 /* The saved PC will always be at the base of the current frame. */
858 return (read_memory_integer (fi->frame, REGISTER_SIZE) & 0xffffff);
861 /* Function: init_extra_frame_info
862 Setup the frame's frame pointer, pc, and frame addresses for saved
863 registers. Most of the work is done in mn10200_analyze_prologue().
865 Note that when we are called for the last frame (currently active frame),
866 that fi->pc and fi->frame will already be setup. However, fi->frame will
867 be valid only if this routine uses FP. For previous frames, fi-frame will
868 always be correct. mn10200_analyze_prologue will fix fi->frame if
871 We can be called with the PC in the call dummy under two circumstances.
872 First, during normal backtracing, second, while figuring out the frame
873 pointer just prior to calling the target function (see run_stack_dummy). */
876 mn10200_init_extra_frame_info (struct frame_info *fi)
879 fi->pc = FRAME_SAVED_PC (fi->next);
881 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
885 mn10200_analyze_prologue (fi, 0);
889 _initialize_mn10200_tdep (void)
891 tm_print_insn = print_insn_mn10200;