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. */
27 #include "gdb_string.h"
33 /* Should call_function allocate stack space for a struct return? */
35 mn10200_use_struct_convention (int gcc_p, struct type *type)
37 return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8);
40 /* The main purpose of this file is dealing with prologues to extract
41 information about stack frames and saved registers.
43 For reference here's how prologues look on the mn10200:
49 Register saves for d2, d3, a1, a2 as needed. Saves start
50 at fp - <size> + <outgoing_args_size> and work towards higher
51 addresses. Note that the saves are actually done off the stack
52 pointer in the prologue! This makes for smaller code and easier
53 prologue scanning as the displacement fields will unlikely
56 Without frame pointer:
58 Register saves for d2, d3, a1, a2 as needed. Saves start
59 at sp + <outgoing_args_size> and work towards higher addresses.
62 add <local size>,sp -- optional
64 add <outgoing_size>,sp -- optional
66 The stack pointer remains constant throughout the life of most
67 functions. As a result the compiler will usually omit the
68 frame pointer, so we must handle frame pointerless functions. */
70 /* Analyze the prologue to determine where registers are saved,
71 the end of the prologue, etc etc. Return the end of the prologue
74 We store into FI (if non-null) several tidbits of information:
76 * stack_size -- size of this stack frame. Note that if we stop in
77 certain parts of the prologue/epilogue we may claim the size of the
78 current frame is zero. This happens when the current frame has
79 not been allocated yet or has already been deallocated.
81 * fsr -- Addresses of registers saved in the stack by this frame.
83 * status -- A (relatively) generic status indicator. It's a bitmask
84 with the following bits:
86 MY_FRAME_IN_SP: The base of the current frame is actually in
87 the stack pointer. This can happen for frame pointerless
88 functions, or cases where we're stopped in the prologue/epilogue
89 itself. For these cases mn10200_analyze_prologue will need up
90 update fi->frame before returning or analyzing the register
93 MY_FRAME_IN_FP: The base of the current frame is in the
94 frame pointer register ($a2).
96 CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily
97 in $a0. This can happen if we're stopped in the prologue.
99 NO_MORE_FRAMES: Set this if the current frame is "start" or
100 if the first instruction looks like mov <imm>,sp. This tells
101 frame chain to not bother trying to unwind past this frame. */
107 #define MY_FRAME_IN_SP 0x1
108 #define MY_FRAME_IN_FP 0x2
109 #define CALLER_A2_IN_A0 0x4
110 #define NO_MORE_FRAMES 0x8
113 mn10200_analyze_prologue (struct frame_info *fi, CORE_ADDR pc)
115 CORE_ADDR func_addr, func_end, addr, stop;
116 CORE_ADDR stack_size = 0;
117 unsigned char buf[4];
120 int out_of_line_prologue = 0;
122 /* Use the PC in the frame if it's provided to look up the
123 start of this function. */
124 pc = (fi ? get_frame_pc (fi) : pc);
126 /* Find the start of this function. */
127 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
129 /* Do nothing if we couldn't find the start of this function or if we're
130 stopped at the first instruction in the prologue. */
134 /* If we're in start, then give up. */
135 if (strcmp (name, "start") == 0)
138 fi->status = NO_MORE_FRAMES;
142 /* At the start of a function our frame is in the stack pointer. */
144 fi->status = MY_FRAME_IN_SP;
146 /* If we're physically on an RTS instruction, then our frame has already
149 fi->frame is bogus, we need to fix it. */
150 if (fi && get_frame_pc (fi) + 1 == func_end)
152 status = target_read_memory (get_frame_pc (fi), buf, 1);
155 if (fi->next == NULL)
156 deprecated_update_frame_base_hack (fi, read_sp ());
157 return get_frame_pc (fi);
162 if (fi->next == NULL)
163 deprecated_update_frame_base_hack (fi, read_sp ());
164 return get_frame_pc (fi);
168 /* Similarly if we're stopped on the first insn of a prologue as our
169 frame hasn't been allocated yet. */
170 if (fi && get_frame_pc (fi) == func_addr)
172 if (fi->next == NULL)
173 deprecated_update_frame_base_hack (fi, read_sp ());
174 return get_frame_pc (fi);
177 /* Figure out where to stop scanning. */
178 stop = fi ? get_frame_pc (fi) : func_end;
180 /* Don't walk off the end of the function. */
181 stop = stop > func_end ? func_end : stop;
183 /* Start scanning on the first instruction of this function. */
186 status = target_read_memory (addr, buf, 2);
189 if (fi && fi->next == NULL && fi->status & MY_FRAME_IN_SP)
190 deprecated_update_frame_base_hack (fi, read_sp ());
194 /* First see if this insn sets the stack pointer; if so, it's something
195 we won't understand, so quit now. */
197 || (buf[0] == 0xf4 && buf[1] == 0x77))
200 fi->status = NO_MORE_FRAMES;
204 /* Now see if we have a frame pointer.
206 Search for mov a2,a0 (0xf278)
207 then mov a3,a2 (0xf27e). */
209 if (buf[0] == 0xf2 && buf[1] == 0x78)
211 /* Our caller's $a2 will be found in $a0 now. Note it for
214 fi->status |= CALLER_A2_IN_A0;
218 /* We still haven't allocated our local stack. Handle this
219 as if we stopped on the first or last insn of a function. */
220 if (fi && fi->next == NULL)
221 deprecated_update_frame_base_hack (fi, read_sp ());
225 status = target_read_memory (addr, buf, 2);
228 if (fi && fi->next == NULL)
229 deprecated_update_frame_base_hack (fi, read_sp ());
232 if (buf[0] == 0xf2 && buf[1] == 0x7e)
236 /* Our frame pointer is valid now. */
239 fi->status |= MY_FRAME_IN_FP;
240 fi->status &= ~MY_FRAME_IN_SP;
247 if (fi && fi->next == NULL)
248 deprecated_update_frame_base_hack (fi, read_sp ());
253 /* Next we should allocate the local frame.
255 Search for add imm8,a3 (0xd3XX)
256 or add imm16,a3 (0xf70bXXXX)
257 or add imm24,a3 (0xf467XXXXXX).
259 If none of the above was found, then this prologue has
260 no stack, and therefore can't have any register saves,
262 status = target_read_memory (addr, buf, 2);
265 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
266 deprecated_update_frame_base_hack (fi, read_sp ());
271 stack_size = extract_signed_integer (&buf[1], 1);
273 fi->stack_size = stack_size;
277 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
278 deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
282 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
284 status = target_read_memory (addr + 2, buf, 2);
287 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
288 deprecated_update_frame_base_hack (fi, read_sp ());
291 stack_size = extract_signed_integer (buf, 2);
293 fi->stack_size = stack_size;
297 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
298 deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
302 else if (buf[0] == 0xf4 && buf[1] == 0x67)
304 status = target_read_memory (addr + 2, buf, 3);
307 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
308 deprecated_update_frame_base_hack (fi, read_sp ());
311 stack_size = extract_signed_integer (buf, 3);
313 fi->stack_size = stack_size;
317 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
318 deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
323 /* Now see if we have a call to __prologue for an out of line
325 status = target_read_memory (addr, buf, 2);
329 /* First check for 16bit pc-relative call to __prologue. */
333 status = target_read_memory (addr + 1, buf, 2);
336 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
337 deprecated_update_frame_base_hack (fi, read_sp ());
341 /* Get the PC this instruction will branch to. */
342 temp = (extract_signed_integer (buf, 2) + addr + 3) & 0xffffff;
344 /* Get the name of the function at the target address. */
345 status = find_pc_partial_function (temp, &name, NULL, NULL);
348 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
349 deprecated_update_frame_base_hack (fi, read_sp ());
353 /* Note if it is an out of line prologue. */
354 out_of_line_prologue = (strcmp (name, "__prologue") == 0);
356 /* This sucks up 3 bytes of instruction space. */
357 if (out_of_line_prologue)
362 if (fi && fi->next == NULL)
364 fi->stack_size -= 16;
365 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
370 /* Now check for the 24bit pc-relative call to __prologue. */
371 else if (buf[0] == 0xf4 && buf[1] == 0xe1)
374 status = target_read_memory (addr + 2, buf, 3);
377 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
378 deprecated_update_frame_base_hack (fi, read_sp ());
382 /* Get the PC this instruction will branch to. */
383 temp = (extract_signed_integer (buf, 3) + addr + 5) & 0xffffff;
385 /* Get the name of the function at the target address. */
386 status = find_pc_partial_function (temp, &name, NULL, NULL);
389 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
390 deprecated_update_frame_base_hack (fi, read_sp ());
394 /* Note if it is an out of line prologue. */
395 out_of_line_prologue = (strcmp (name, "__prologue") == 0);
397 /* This sucks up 5 bytes of instruction space. */
398 if (out_of_line_prologue)
403 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
405 fi->stack_size -= 16;
406 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
412 /* Now actually handle the out of line prologue. */
413 if (out_of_line_prologue)
415 int outgoing_args_size = 0;
417 /* First adjust the stack size for this function. The out of
418 line prologue saves 4 registers (16bytes of data). */
420 fi->stack_size -= 16;
422 /* Update fi->frame if necessary. */
423 if (fi && fi->next == NULL)
424 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
426 /* After the out of line prologue, there may be another
427 stack adjustment for the outgoing arguments.
429 Search for add imm8,a3 (0xd3XX)
430 or add imm16,a3 (0xf70bXXXX)
431 or add imm24,a3 (0xf467XXXXXX). */
433 status = target_read_memory (addr, buf, 2);
438 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
439 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
440 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
441 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
448 outgoing_args_size = extract_signed_integer (&buf[1], 1);
451 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
453 status = target_read_memory (addr + 2, buf, 2);
458 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
459 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
460 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
461 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
465 outgoing_args_size = extract_signed_integer (buf, 2);
468 else if (buf[0] == 0xf4 && buf[1] == 0x67)
470 status = target_read_memory (addr + 2, buf, 3);
473 if (fi && fi->next == NULL)
475 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
476 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
477 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
478 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
482 outgoing_args_size = extract_signed_integer (buf, 3);
486 outgoing_args_size = 0;
488 /* Now that we know the size of the outgoing arguments, fix
489 fi->frame again if this is the innermost frame. */
490 if (fi && fi->next == NULL)
491 deprecated_update_frame_base_hack (fi, fi->frame - outgoing_args_size);
493 /* Note the register save information and update the stack
494 size for this frame too. */
497 fi->fsr.regs[2] = fi->frame + fi->stack_size + 4;
498 fi->fsr.regs[3] = fi->frame + fi->stack_size + 8;
499 fi->fsr.regs[5] = fi->frame + fi->stack_size + 12;
500 fi->fsr.regs[6] = fi->frame + fi->stack_size + 16;
501 fi->stack_size += outgoing_args_size;
503 /* There can be no more prologue insns, so return now. */
507 /* At this point fi->frame needs to be correct.
509 If MY_FRAME_IN_SP is set and we're the innermost frame, then we
510 need to fix fi->frame so that backtracing, find_frame_saved_regs,
511 etc work correctly. */
512 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP) != 0)
513 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
515 /* And last we have the register saves. These are relatively
516 simple because they're physically done off the stack pointer,
517 and thus the number of different instructions we need to
518 check is greatly reduced because we know the displacements
521 Search for movx d2,(X,a3) (0xf55eXX)
522 then movx d3,(X,a3) (0xf55fXX)
523 then mov a1,(X,a3) (0x5dXX) No frame pointer case
524 then mov a2,(X,a3) (0x5eXX) No frame pointer case
525 or mov a0,(X,a3) (0x5cXX) Frame pointer case. */
527 status = target_read_memory (addr, buf, 2);
530 if (buf[0] == 0xf5 && buf[1] == 0x5e)
534 status = target_read_memory (addr + 2, buf, 1);
537 fi->fsr.regs[2] = (fi->frame + stack_size
538 + extract_signed_integer (buf, 1));
543 status = target_read_memory (addr, buf, 2);
547 if (buf[0] == 0xf5 && buf[1] == 0x5f)
551 status = target_read_memory (addr + 2, buf, 1);
554 fi->fsr.regs[3] = (fi->frame + stack_size
555 + extract_signed_integer (buf, 1));
560 status = target_read_memory (addr, buf, 2);
568 status = target_read_memory (addr + 1, buf, 1);
571 fi->fsr.regs[5] = (fi->frame + stack_size
572 + extract_signed_integer (buf, 1));
577 status = target_read_memory (addr, buf, 2);
581 if (buf[0] == 0x5e || buf[0] == 0x5c)
585 status = target_read_memory (addr + 1, buf, 1);
588 fi->fsr.regs[6] = (fi->frame + stack_size
589 + extract_signed_integer (buf, 1));
590 fi->status &= ~CALLER_A2_IN_A0;
600 /* Function: frame_chain
601 Figure out and return the caller's frame pointer given current
604 We don't handle dummy frames yet but we would probably just return the
605 stack pointer that was in use at the time the function call was made? */
608 mn10200_frame_chain (struct frame_info *fi)
610 struct frame_info dummy_frame;
612 /* Walk through the prologue to determine the stack size,
613 location of saved registers, end of the prologue, etc. */
615 mn10200_analyze_prologue (fi, (CORE_ADDR) 0);
617 /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES. */
618 if (fi->status & NO_MORE_FRAMES)
621 /* Now that we've analyzed our prologue, determine the frame
622 pointer for our caller.
624 If our caller has a frame pointer, then we need to
625 find the entry value of $a2 to our function.
627 If CALLER_A2_IN_A0, then the chain is in $a0.
629 If fsr.regs[6] is nonzero, then it's at the memory
630 location pointed to by fsr.regs[6].
632 Else it's still in $a2.
634 If our caller does not have a frame pointer, then his
635 frame base is fi->frame + -caller's stack size + 4. */
637 /* The easiest way to get that info is to analyze our caller's frame.
639 So we set up a dummy frame and call mn10200_analyze_prologue to
640 find stuff for us. */
641 deprecated_update_frame_pc_hack (&dummy_frame, FRAME_SAVED_PC (fi));
642 deprecated_update_frame_base_hack (&dummy_frame, fi->frame);
643 memset (dummy_frame.fsr.regs, '\000', sizeof dummy_frame.fsr.regs);
644 dummy_frame.status = 0;
645 dummy_frame.stack_size = 0;
646 mn10200_analyze_prologue (&dummy_frame, 0);
648 if (dummy_frame.status & MY_FRAME_IN_FP)
650 /* Our caller has a frame pointer. So find the frame in $a2, $a0,
653 return (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE)
655 else if (fi->status & CALLER_A2_IN_A0)
656 return read_register (4);
658 return read_register (FP_REGNUM);
662 /* Our caller does not have a frame pointer. So his frame starts
663 at the base of our frame (fi->frame) + <his size> + 4 (saved pc). */
664 return fi->frame + -dummy_frame.stack_size + 4;
668 /* Function: skip_prologue
669 Return the address of the first inst past the prologue of the function. */
672 mn10200_skip_prologue (CORE_ADDR pc)
674 /* We used to check the debug symbols, but that can lose if
675 we have a null prologue. */
676 return mn10200_analyze_prologue (NULL, pc);
679 /* Function: pop_frame
680 This routine gets called when either the user uses the `return'
681 command, or the call dummy breakpoint gets hit. */
684 mn10200_pop_frame (struct frame_info *frame)
688 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame), frame->frame, frame->frame))
689 generic_pop_dummy_frame ();
692 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
694 /* Restore any saved registers. */
695 for (regnum = 0; regnum < NUM_REGS; regnum++)
696 if (frame->fsr.regs[regnum] != 0)
700 value = read_memory_unsigned_integer (frame->fsr.regs[regnum],
701 REGISTER_RAW_SIZE (regnum));
702 write_register (regnum, value);
705 /* Actually cut back the stack. */
706 write_register (SP_REGNUM, get_frame_base (frame));
708 /* Don't we need to set the PC?!? XXX FIXME. */
711 /* Throw away any cached frame information. */
712 flush_cached_frames ();
715 /* Function: push_arguments
716 Setup arguments for a call to the target. Arguments go in
717 order on the stack. */
720 mn10200_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
721 unsigned char struct_return, CORE_ADDR struct_addr)
725 int stack_offset = 0;
726 int regsused = struct_return ? 1 : 0;
728 /* This should be a nop, but align the stack just in case something
729 went wrong. Stacks are two byte aligned on the mn10200. */
732 /* Now make space on the stack for the args.
734 XXX This doesn't appear to handle pass-by-invisible reference
736 for (argnum = 0; argnum < nargs; argnum++)
738 int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 1) & ~1;
740 /* If we've used all argument registers, then this argument is
742 if (regsused >= 2 || arg_length > 4)
747 /* We know we've got some arg register space left. If this argument
748 will fit entirely in regs, then put it there. */
749 else if (arg_length <= 2
750 || TYPE_CODE (VALUE_TYPE (args[argnum])) == TYPE_CODE_PTR)
754 else if (regsused == 0)
765 /* Allocate stack space. */
768 regsused = struct_return ? 1 : 0;
769 /* Push all arguments onto the stack. */
770 for (argnum = 0; argnum < nargs; argnum++)
775 /* XXX Check this. What about UNIONS? */
776 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
777 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
779 /* XXX Wrong, we want a pointer to this argument. */
780 len = TYPE_LENGTH (VALUE_TYPE (*args));
781 val = (char *) VALUE_CONTENTS (*args);
785 len = TYPE_LENGTH (VALUE_TYPE (*args));
786 val = (char *) VALUE_CONTENTS (*args);
791 || TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_PTR))
793 write_register (regsused, extract_unsigned_integer (val, 4));
796 else if (regsused == 0 && len == 4)
798 write_register (regsused, extract_unsigned_integer (val, 2));
799 write_register (regsused + 1, extract_unsigned_integer (val + 2, 2));
807 write_memory (sp + stack_offset, val, 2);
820 /* Function: push_return_address (pc)
821 Set up the return address for the inferior function call.
822 Needed for targets where we don't actually execute a JSR/BSR instruction */
825 mn10200_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
827 unsigned char buf[4];
829 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
830 write_memory (sp - 4, buf, 4);
834 /* Function: store_struct_return (addr,sp)
835 Store the structure value return address for an inferior function
839 mn10200_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
841 /* The structure return address is passed as the first argument. */
842 write_register (0, addr);
846 /* Function: frame_saved_pc
847 Find the caller of this frame. We do this by seeing if RP_REGNUM
848 is saved in the stack anywhere, otherwise we get it from the
849 registers. If the inner frame is a dummy frame, return its PC
850 instead of RP, because that's where "caller" of the dummy-frame
854 mn10200_frame_saved_pc (struct frame_info *fi)
856 /* The saved PC will always be at the base of the current frame. */
857 return (read_memory_integer (fi->frame, REGISTER_SIZE) & 0xffffff);
860 /* Function: init_extra_frame_info
861 Setup the frame's frame pointer, pc, and frame addresses for saved
862 registers. Most of the work is done in mn10200_analyze_prologue().
864 Note that when we are called for the last frame (currently active frame),
865 that get_frame_pc (fi) and fi->frame will already be setup. However, fi->frame will
866 be valid only if this routine uses FP. For previous frames, fi-frame will
867 always be correct. mn10200_analyze_prologue will fix fi->frame if
870 We can be called with the PC in the call dummy under two circumstances.
871 First, during normal backtracing, second, while figuring out the frame
872 pointer just prior to calling the target function (see run_stack_dummy). */
875 mn10200_init_extra_frame_info (struct frame_info *fi)
878 deprecated_update_frame_pc_hack (fi, FRAME_SAVED_PC (fi->next));
880 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
884 mn10200_analyze_prologue (fi, 0);
888 _initialize_mn10200_tdep (void)
890 tm_print_insn = print_insn_mn10200;