1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2 Copyright 1996, 1997, 1998 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"
32 static CORE_ADDR mn10300_analyze_prologue
PARAMS ((struct frame_info
* fi
,
35 /* Additional info used by the frame */
37 struct frame_extra_info
44 static char *mn10300_generic_register_names
[] =
45 {"d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
46 "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
47 "", "", "", "", "", "", "", "",
48 "", "", "", "", "", "", "", "fp"};
50 static char **mn10300_register_names
= mn10300_generic_register_names
;
53 mn10300_register_name (i
)
56 return mn10300_register_names
[i
];
60 mn10300_saved_pc_after_call (fi
)
61 struct frame_info
*fi
;
63 return read_memory_integer (read_register (SP_REGNUM
), 4);
67 mn10300_extract_return_value (type
, regbuf
, valbuf
)
72 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
73 memcpy (valbuf
, regbuf
+ REGISTER_BYTE (4), TYPE_LENGTH (type
));
75 memcpy (valbuf
, regbuf
+ REGISTER_BYTE (0), TYPE_LENGTH (type
));
79 mn10300_extract_struct_value_address (regbuf
)
82 return extract_address (regbuf
+ REGISTER_BYTE (4),
83 REGISTER_RAW_SIZE (4));
87 mn10300_store_return_value (type
, valbuf
)
91 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
92 write_register_bytes (REGISTER_BYTE (4), valbuf
, TYPE_LENGTH (type
));
94 write_register_bytes (REGISTER_BYTE (0), valbuf
, TYPE_LENGTH (type
));
97 static struct frame_info
*analyze_dummy_frame
PARAMS ((CORE_ADDR
, CORE_ADDR
));
98 static struct frame_info
*
99 analyze_dummy_frame (pc
, frame
)
103 static struct frame_info
*dummy
= NULL
;
106 dummy
= xmalloc (sizeof (struct frame_info
));
107 dummy
->saved_regs
= xmalloc (SIZEOF_FRAME_SAVED_REGS
);
108 dummy
->extra_info
= xmalloc (sizeof (struct frame_extra_info
));
113 dummy
->frame
= frame
;
114 dummy
->extra_info
->status
= 0;
115 dummy
->extra_info
->stack_size
= 0;
116 memset (dummy
->saved_regs
, '\000', SIZEOF_FRAME_SAVED_REGS
);
117 mn10300_analyze_prologue (dummy
, 0);
121 /* Values for frame_info.status */
123 #define MY_FRAME_IN_SP 0x1
124 #define MY_FRAME_IN_FP 0x2
125 #define NO_MORE_FRAMES 0x4
128 /* Should call_function allocate stack space for a struct return? */
130 mn10300_use_struct_convention (gcc_p
, type
)
134 return (TYPE_NFIELDS (type
) > 1 || TYPE_LENGTH (type
) > 8);
137 /* The breakpoint instruction must be the same size as the smallest
138 instruction in the instruction set.
140 The Matsushita mn10x00 processors have single byte instructions
141 so we need a single byte breakpoint. Matsushita hasn't defined
142 one, so we defined it ourselves. */
145 mn10300_breakpoint_from_pc (bp_addr
, bp_size
)
149 static char breakpoint
[] =
156 /* Fix fi->frame if it's bogus at this point. This is a helper
157 function for mn10300_analyze_prologue. */
160 fix_frame_pointer (fi
, stack_size
)
161 struct frame_info
*fi
;
164 if (fi
&& fi
->next
== NULL
)
166 if (fi
->extra_info
->status
& MY_FRAME_IN_SP
)
167 fi
->frame
= read_sp () - stack_size
;
168 else if (fi
->extra_info
->status
& MY_FRAME_IN_FP
)
169 fi
->frame
= read_register (A3_REGNUM
);
174 /* Set offsets of registers saved by movm instruction.
175 This is a helper function for mn10300_analyze_prologue. */
178 set_movm_offsets (fi
, movm_args
)
179 struct frame_info
*fi
;
184 if (fi
== NULL
|| movm_args
== 0)
187 if (movm_args
& 0x10)
189 fi
->saved_regs
[A3_REGNUM
] = fi
->frame
+ offset
;
192 if (movm_args
& 0x20)
194 fi
->saved_regs
[A2_REGNUM
] = fi
->frame
+ offset
;
197 if (movm_args
& 0x40)
199 fi
->saved_regs
[D3_REGNUM
] = fi
->frame
+ offset
;
202 if (movm_args
& 0x80)
204 fi
->saved_regs
[D2_REGNUM
] = fi
->frame
+ offset
;
210 /* The main purpose of this file is dealing with prologues to extract
211 information about stack frames and saved registers.
213 For reference here's how prologues look on the mn10300:
216 movm [d2,d3,a2,a3],sp
220 Without frame pointer:
221 movm [d2,d3,a2,a3],sp (if needed)
224 One day we might keep the stack pointer constant, that won't
225 change the code for prologues, but it will make the frame
226 pointerless case much more common. */
228 /* Analyze the prologue to determine where registers are saved,
229 the end of the prologue, etc etc. Return the end of the prologue
232 We store into FI (if non-null) several tidbits of information:
234 * stack_size -- size of this stack frame. Note that if we stop in
235 certain parts of the prologue/epilogue we may claim the size of the
236 current frame is zero. This happens when the current frame has
237 not been allocated yet or has already been deallocated.
239 * fsr -- Addresses of registers saved in the stack by this frame.
241 * status -- A (relatively) generic status indicator. It's a bitmask
242 with the following bits:
244 MY_FRAME_IN_SP: The base of the current frame is actually in
245 the stack pointer. This can happen for frame pointerless
246 functions, or cases where we're stopped in the prologue/epilogue
247 itself. For these cases mn10300_analyze_prologue will need up
248 update fi->frame before returning or analyzing the register
251 MY_FRAME_IN_FP: The base of the current frame is in the
252 frame pointer register ($a2).
254 NO_MORE_FRAMES: Set this if the current frame is "start" or
255 if the first instruction looks like mov <imm>,sp. This tells
256 frame chain to not bother trying to unwind past this frame. */
259 mn10300_analyze_prologue (fi
, pc
)
260 struct frame_info
*fi
;
263 CORE_ADDR func_addr
, func_end
, addr
, stop
;
264 CORE_ADDR stack_size
;
266 unsigned char buf
[4];
267 int status
, movm_args
= 0;
270 /* Use the PC in the frame if it's provided to look up the
271 start of this function. */
272 pc
= (fi
? fi
->pc
: pc
);
274 /* Find the start of this function. */
275 status
= find_pc_partial_function (pc
, &name
, &func_addr
, &func_end
);
277 /* Do nothing if we couldn't find the start of this function or if we're
278 stopped at the first instruction in the prologue. */
284 /* If we're in start, then give up. */
285 if (strcmp (name
, "start") == 0)
288 fi
->extra_info
->status
= NO_MORE_FRAMES
;
292 /* At the start of a function our frame is in the stack pointer. */
294 fi
->extra_info
->status
= MY_FRAME_IN_SP
;
296 /* Get the next two bytes into buf, we need two because rets is a two
297 byte insn and the first isn't enough to uniquely identify it. */
298 status
= read_memory_nobpt (pc
, buf
, 2);
302 /* If we're physically on an "rets" instruction, then our frame has
303 already been deallocated. Note this can also be true for retf
304 and ret if they specify a size of zero.
306 In this case fi->frame is bogus, we need to fix it. */
307 if (fi
&& buf
[0] == 0xf0 && buf
[1] == 0xfc)
309 if (fi
->next
== NULL
)
310 fi
->frame
= read_sp ();
314 /* Similarly if we're stopped on the first insn of a prologue as our
315 frame hasn't been allocated yet. */
316 if (fi
&& fi
->pc
== func_addr
)
318 if (fi
->next
== NULL
)
319 fi
->frame
= read_sp ();
323 /* Figure out where to stop scanning. */
324 stop
= fi
? fi
->pc
: func_end
;
326 /* Don't walk off the end of the function. */
327 stop
= stop
> func_end
? func_end
: stop
;
329 /* Start scanning on the first instruction of this function. */
332 /* Suck in two bytes. */
333 status
= read_memory_nobpt (addr
, buf
, 2);
336 fix_frame_pointer (fi
, 0);
340 /* First see if this insn sets the stack pointer; if so, it's something
341 we won't understand, so quit now. */
342 if (buf
[0] == 0xf2 && (buf
[1] & 0xf3) == 0xf0)
345 fi
->extra_info
->status
= NO_MORE_FRAMES
;
349 /* Now look for movm [regs],sp, which saves the callee saved registers.
351 At this time we don't know if fi->frame is valid, so we only note
352 that we encountered a movm instruction. Later, we'll set the entries
353 in fsr.regs as needed. */
356 /* Extract the register list for the movm instruction. */
357 status
= read_memory_nobpt (addr
+ 1, buf
, 1);
362 /* Quit now if we're beyond the stop point. */
365 /* Fix fi->frame since it's bogus at this point. */
366 if (fi
&& fi
->next
== NULL
)
367 fi
->frame
= read_sp ();
369 /* Note if/where callee saved registers were saved. */
370 set_movm_offsets (fi
, movm_args
);
374 /* Get the next two bytes so the prologue scan can continue. */
375 status
= read_memory_nobpt (addr
, buf
, 2);
378 /* Fix fi->frame since it's bogus at this point. */
379 if (fi
&& fi
->next
== NULL
)
380 fi
->frame
= read_sp ();
382 /* Note if/where callee saved registers were saved. */
383 set_movm_offsets (fi
, movm_args
);
388 /* Now see if we set up a frame pointer via "mov sp,a3" */
393 /* The frame pointer is now valid. */
396 fi
->extra_info
->status
|= MY_FRAME_IN_FP
;
397 fi
->extra_info
->status
&= ~MY_FRAME_IN_SP
;
400 /* Quit now if we're beyond the stop point. */
403 /* Fix fi->frame if it's bogus at this point. */
404 fix_frame_pointer (fi
, 0);
406 /* Note if/where callee saved registers were saved. */
407 set_movm_offsets (fi
, movm_args
);
411 /* Get two more bytes so scanning can continue. */
412 status
= read_memory_nobpt (addr
, buf
, 2);
415 /* Fix fi->frame if it's bogus at this point. */
416 fix_frame_pointer (fi
, 0);
418 /* Note if/where callee saved registers were saved. */
419 set_movm_offsets (fi
, movm_args
);
424 /* Next we should allocate the local frame. No more prologue insns
425 are found after allocating the local frame.
427 Search for add imm8,sp (0xf8feXX)
428 or add imm16,sp (0xfafeXXXX)
429 or add imm32,sp (0xfcfeXXXXXXXX).
431 If none of the above was found, then this prologue has no
434 status
= read_memory_nobpt (addr
, buf
, 2);
437 /* Fix fi->frame if it's bogus at this point. */
438 fix_frame_pointer (fi
, 0);
440 /* Note if/where callee saved registers were saved. */
441 set_movm_offsets (fi
, movm_args
);
446 if (buf
[0] == 0xf8 && buf
[1] == 0xfe)
448 else if (buf
[0] == 0xfa && buf
[1] == 0xfe)
450 else if (buf
[0] == 0xfc && buf
[1] == 0xfe)
455 /* Suck in imm_size more bytes, they'll hold the size of the
457 status
= read_memory_nobpt (addr
+ 2, buf
, imm_size
);
460 /* Fix fi->frame if it's bogus at this point. */
461 fix_frame_pointer (fi
, 0);
463 /* Note if/where callee saved registers were saved. */
464 set_movm_offsets (fi
, movm_args
);
468 /* Note the size of the stack in the frame info structure. */
469 stack_size
= extract_signed_integer (buf
, imm_size
);
471 fi
->extra_info
->stack_size
= stack_size
;
473 /* We just consumed 2 + imm_size bytes. */
474 addr
+= 2 + imm_size
;
476 /* No more prologue insns follow, so begin preparation to return. */
477 /* Fix fi->frame if it's bogus at this point. */
478 fix_frame_pointer (fi
, stack_size
);
480 /* Note if/where callee saved registers were saved. */
481 set_movm_offsets (fi
, movm_args
);
485 /* We never found an insn which allocates local stack space, regardless
486 this is the end of the prologue. */
487 /* Fix fi->frame if it's bogus at this point. */
488 fix_frame_pointer (fi
, 0);
490 /* Note if/where callee saved registers were saved. */
491 set_movm_offsets (fi
, movm_args
);
495 /* Function: frame_chain
496 Figure out and return the caller's frame pointer given current
499 We don't handle dummy frames yet but we would probably just return the
500 stack pointer that was in use at the time the function call was made? */
503 mn10300_frame_chain (fi
)
504 struct frame_info
*fi
;
506 struct frame_info
*dummy
;
507 /* Walk through the prologue to determine the stack size,
508 location of saved registers, end of the prologue, etc. */
509 if (fi
->extra_info
->status
== 0)
510 mn10300_analyze_prologue (fi
, (CORE_ADDR
) 0);
512 /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */
513 if (fi
->extra_info
->status
& NO_MORE_FRAMES
)
516 /* Now that we've analyzed our prologue, determine the frame
517 pointer for our caller.
519 If our caller has a frame pointer, then we need to
520 find the entry value of $a3 to our function.
522 If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
523 location pointed to by fsr.regs[A3_REGNUM].
525 Else it's still in $a3.
527 If our caller does not have a frame pointer, then his
528 frame base is fi->frame + -caller's stack size. */
530 /* The easiest way to get that info is to analyze our caller's frame.
531 So we set up a dummy frame and call mn10300_analyze_prologue to
532 find stuff for us. */
533 dummy
= analyze_dummy_frame (FRAME_SAVED_PC (fi
), fi
->frame
);
535 if (dummy
->extra_info
->status
& MY_FRAME_IN_FP
)
537 /* Our caller has a frame pointer. So find the frame in $a3 or
539 if (fi
->saved_regs
[A3_REGNUM
])
540 return (read_memory_integer (fi
->saved_regs
[A3_REGNUM
], REGISTER_SIZE
));
542 return read_register (A3_REGNUM
);
548 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
549 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
550 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
551 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 4 : 0);
553 /* Our caller does not have a frame pointer. So his frame starts
554 at the base of our frame (fi->frame) + register save space
556 return fi
->frame
+ adjust
+ -dummy
->extra_info
->stack_size
;
560 /* Function: skip_prologue
561 Return the address of the first inst past the prologue of the function. */
564 mn10300_skip_prologue (pc
)
567 /* We used to check the debug symbols, but that can lose if
568 we have a null prologue. */
569 return mn10300_analyze_prologue (NULL
, pc
);
573 /* Function: pop_frame
574 This routine gets called when either the user uses the `return'
575 command, or the call dummy breakpoint gets hit. */
578 mn10300_pop_frame (frame
)
579 struct frame_info
*frame
;
583 if (PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
584 generic_pop_dummy_frame ();
587 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
589 /* Restore any saved registers. */
590 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
591 if (frame
->saved_regs
[regnum
] != 0)
595 value
= read_memory_unsigned_integer (frame
->saved_regs
[regnum
],
596 REGISTER_RAW_SIZE (regnum
));
597 write_register (regnum
, value
);
600 /* Actually cut back the stack. */
601 write_register (SP_REGNUM
, FRAME_FP (frame
));
603 /* Don't we need to set the PC?!? XXX FIXME. */
606 /* Throw away any cached frame information. */
607 flush_cached_frames ();
610 /* Function: push_arguments
611 Setup arguments for a call to the target. Arguments go in
612 order on the stack. */
615 mn10300_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
619 unsigned char struct_return
;
620 CORE_ADDR struct_addr
;
624 int stack_offset
= 0;
625 int regsused
= struct_return
? 1 : 0;
627 /* This should be a nop, but align the stack just in case something
628 went wrong. Stacks are four byte aligned on the mn10300. */
631 /* Now make space on the stack for the args.
633 XXX This doesn't appear to handle pass-by-invisible reference
635 for (argnum
= 0; argnum
< nargs
; argnum
++)
637 int arg_length
= (TYPE_LENGTH (VALUE_TYPE (args
[argnum
])) + 3) & ~3;
639 while (regsused
< 2 && arg_length
> 0)
647 /* Allocate stack space. */
650 regsused
= struct_return
? 1 : 0;
651 /* Push all arguments onto the stack. */
652 for (argnum
= 0; argnum
< nargs
; argnum
++)
657 /* XXX Check this. What about UNIONS? */
658 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
659 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
661 /* XXX Wrong, we want a pointer to this argument. */
662 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
663 val
= (char *) VALUE_CONTENTS (*args
);
667 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
668 val
= (char *) VALUE_CONTENTS (*args
);
671 while (regsused
< 2 && len
> 0)
673 write_register (regsused
, extract_unsigned_integer (val
, 4));
681 write_memory (sp
+ stack_offset
, val
, 4);
690 /* Make space for the flushback area. */
695 /* Function: push_return_address (pc)
696 Set up the return address for the inferior function call.
697 Needed for targets where we don't actually execute a JSR/BSR instruction */
700 mn10300_push_return_address (pc
, sp
)
704 unsigned char buf
[4];
706 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
707 write_memory (sp
- 4, buf
, 4);
711 /* Function: store_struct_return (addr,sp)
712 Store the structure value return address for an inferior function
716 mn10300_store_struct_return (addr
, sp
)
720 /* The structure return address is passed as the first argument. */
721 write_register (0, addr
);
725 /* Function: frame_saved_pc
726 Find the caller of this frame. We do this by seeing if RP_REGNUM
727 is saved in the stack anywhere, otherwise we get it from the
728 registers. If the inner frame is a dummy frame, return its PC
729 instead of RP, because that's where "caller" of the dummy-frame
733 mn10300_frame_saved_pc (fi
)
734 struct frame_info
*fi
;
738 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
739 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
740 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
741 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 4 : 0);
743 return (read_memory_integer (fi
->frame
+ adjust
, REGISTER_SIZE
));
746 /* Function: mn10300_init_extra_frame_info
747 Setup the frame's frame pointer, pc, and frame addresses for saved
748 registers. Most of the work is done in mn10300_analyze_prologue().
750 Note that when we are called for the last frame (currently active frame),
751 that fi->pc and fi->frame will already be setup. However, fi->frame will
752 be valid only if this routine uses FP. For previous frames, fi-frame will
753 always be correct. mn10300_analyze_prologue will fix fi->frame if
756 We can be called with the PC in the call dummy under two circumstances.
757 First, during normal backtracing, second, while figuring out the frame
758 pointer just prior to calling the target function (see run_stack_dummy). */
761 mn10300_init_extra_frame_info (fi
)
762 struct frame_info
*fi
;
765 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
767 frame_saved_regs_zalloc (fi
);
768 fi
->extra_info
= (struct frame_extra_info
*)
769 frame_obstack_alloc (sizeof (struct frame_extra_info
));
771 fi
->extra_info
->status
= 0;
772 fi
->extra_info
->stack_size
= 0;
774 mn10300_analyze_prologue (fi
, 0);
777 /* Function: mn10300_virtual_frame_pointer
778 Return the register that the function uses for a frame pointer,
779 plus any necessary offset to be applied to the register before
780 any frame pointer offsets. */
783 mn10300_virtual_frame_pointer (pc
, reg
, offset
)
788 struct frame_info
*dummy
= analyze_dummy_frame (pc
, 0);
789 /* Set up a dummy frame_info, Analyze the prolog and fill in the
791 /* Results will tell us which type of frame it uses. */
792 if (dummy
->extra_info
->status
& MY_FRAME_IN_SP
)
795 *offset
= -(dummy
->extra_info
->stack_size
);
804 /* This can be made more generic later. */
806 set_machine_hook (filename
)
811 if (bfd_get_mach (exec_bfd
) == bfd_mach_mn10300
812 || bfd_get_mach (exec_bfd
) == 0)
814 mn10300_register_names
= mn10300_generic_register_names
;
820 _initialize_mn10300_tdep ()
822 /* printf("_initialize_mn10300_tdep\n"); */
824 tm_print_insn
= print_insn_mn10300
;
826 specify_exec_file_hook (set_machine_hook
);