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, Boston, MA 02111-1307, USA. */
27 #include "gdb_string.h"
31 static char *mn10300_generic_register_names
[] =
32 { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
33 "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
34 "", "", "", "", "", "", "", "",
35 "", "", "", "", "", "", "", "fp" };
37 char **mn10300_register_names
= mn10300_generic_register_names
;
38 /* start-sanitize-am33 */
39 static char *am33_register_names
[] =
40 { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
41 "sp", "pc", "mdr", "psw", "lir", "lar", "",
42 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
43 "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""};
45 /* end-sanitize-am33 */
47 static CORE_ADDR mn10300_analyze_prologue
PARAMS ((struct frame_info
*fi
,
50 /* Values for frame_info.status */
52 #define MY_FRAME_IN_SP 0x1
53 #define MY_FRAME_IN_FP 0x2
54 #define NO_MORE_FRAMES 0x4
57 /* Should call_function allocate stack space for a struct return? */
59 mn10300_use_struct_convention (gcc_p
, type
)
63 return (TYPE_NFIELDS (type
) > 1 || TYPE_LENGTH (type
) > 8);
66 /* The breakpoint instruction must be the same size as the smallest
67 instruction in the instruction set.
69 The Matsushita mn10x00 processors have single byte instructions
70 so we need a single byte breakpoint. Matsushita hasn't defined
71 one, so we defined it ourselves. */
74 mn10300_breakpoint_from_pc (bp_addr
, bp_size
)
78 static char breakpoint
[] = {0xff};
84 /* Fix fi->frame if it's bogus at this point. This is a helper
85 function for mn10300_analyze_prologue. */
88 fix_frame_pointer (fi
, stack_size
)
89 struct frame_info
*fi
;
92 if (fi
&& fi
->next
== NULL
)
94 if (fi
->status
& MY_FRAME_IN_SP
)
95 fi
->frame
= read_sp () - stack_size
;
96 else if (fi
->status
& MY_FRAME_IN_FP
)
97 fi
->frame
= read_register (A3_REGNUM
);
102 /* Set offsets of registers saved by movm instruction.
103 This is a helper function for mn10300_analyze_prologue. */
106 set_movm_offsets (fi
, movm_args
)
107 struct frame_info
*fi
;
112 if (fi
== NULL
|| movm_args
== 0)
115 if (movm_args
& 0x10)
117 fi
->fsr
.regs
[A3_REGNUM
] = fi
->frame
+ offset
;
120 if (movm_args
& 0x20)
122 fi
->fsr
.regs
[A2_REGNUM
] = fi
->frame
+ offset
;
125 if (movm_args
& 0x40)
127 fi
->fsr
.regs
[D3_REGNUM
] = fi
->frame
+ offset
;
130 if (movm_args
& 0x80)
132 fi
->fsr
.regs
[D2_REGNUM
] = fi
->frame
+ offset
;
135 /* start-sanitize-am33 */
136 if (am33_mode
&& movm_args
& 0x02)
138 fi
->fsr
.regs
[E0_REGNUM
+5] = fi
->frame
+ offset
;
139 fi
->fsr
.regs
[E0_REGNUM
+4] = fi
->frame
+ offset
+ 4;
140 fi
->fsr
.regs
[E0_REGNUM
+3] = fi
->frame
+ offset
+ 8;
141 fi
->fsr
.regs
[E0_REGNUM
+2] = fi
->frame
+ offset
+ 12;
143 /* end-sanitize-am33 */
147 /* The main purpose of this file is dealing with prologues to extract
148 information about stack frames and saved registers.
150 For reference here's how prologues look on the mn10300:
153 movm [d2,d3,a2,a3],sp
157 Without frame pointer:
158 movm [d2,d3,a2,a3],sp (if needed)
161 One day we might keep the stack pointer constant, that won't
162 change the code for prologues, but it will make the frame
163 pointerless case much more common. */
165 /* Analyze the prologue to determine where registers are saved,
166 the end of the prologue, etc etc. Return the end of the prologue
169 We store into FI (if non-null) several tidbits of information:
171 * stack_size -- size of this stack frame. Note that if we stop in
172 certain parts of the prologue/epilogue we may claim the size of the
173 current frame is zero. This happens when the current frame has
174 not been allocated yet or has already been deallocated.
176 * fsr -- Addresses of registers saved in the stack by this frame.
178 * status -- A (relatively) generic status indicator. It's a bitmask
179 with the following bits:
181 MY_FRAME_IN_SP: The base of the current frame is actually in
182 the stack pointer. This can happen for frame pointerless
183 functions, or cases where we're stopped in the prologue/epilogue
184 itself. For these cases mn10300_analyze_prologue will need up
185 update fi->frame before returning or analyzing the register
188 MY_FRAME_IN_FP: The base of the current frame is in the
189 frame pointer register ($a2).
191 NO_MORE_FRAMES: Set this if the current frame is "start" or
192 if the first instruction looks like mov <imm>,sp. This tells
193 frame chain to not bother trying to unwind past this frame. */
196 mn10300_analyze_prologue (fi
, pc
)
197 struct frame_info
*fi
;
200 CORE_ADDR func_addr
, func_end
, addr
, stop
;
201 CORE_ADDR stack_size
;
203 unsigned char buf
[4];
204 int status
, movm_args
= 0;
207 /* Use the PC in the frame if it's provided to look up the
208 start of this function. */
209 pc
= (fi
? fi
->pc
: pc
);
211 /* Find the start of this function. */
212 status
= find_pc_partial_function (pc
, &name
, &func_addr
, &func_end
);
214 /* Do nothing if we couldn't find the start of this function or if we're
215 stopped at the first instruction in the prologue. */
219 /* If we're in start, then give up. */
220 if (strcmp (name
, "start") == 0)
223 fi
->status
= NO_MORE_FRAMES
;
227 /* At the start of a function our frame is in the stack pointer. */
229 fi
->status
= MY_FRAME_IN_SP
;
231 /* Get the next two bytes into buf, we need two because rets is a two
232 byte insn and the first isn't enough to uniquely identify it. */
233 status
= read_memory_nobpt (pc
, buf
, 2);
237 /* If we're physically on an "rets" instruction, then our frame has
238 already been deallocated. Note this can also be true for retf
239 and ret if they specify a size of zero.
241 In this case fi->frame is bogus, we need to fix it. */
242 if (fi
&& buf
[0] == 0xf0 && buf
[1] == 0xfc)
244 if (fi
->next
== NULL
)
245 fi
->frame
= read_sp ();
249 /* Similarly if we're stopped on the first insn of a prologue as our
250 frame hasn't been allocated yet. */
251 if (fi
&& fi
->pc
== func_addr
)
253 if (fi
->next
== NULL
)
254 fi
->frame
= read_sp ();
258 /* Figure out where to stop scanning. */
259 stop
= fi
? fi
->pc
: func_end
;
261 /* Don't walk off the end of the function. */
262 stop
= stop
> func_end
? func_end
: stop
;
264 /* Start scanning on the first instruction of this function. */
267 /* Suck in two bytes. */
268 status
= read_memory_nobpt (addr
, buf
, 2);
271 fix_frame_pointer (fi
, 0);
275 /* First see if this insn sets the stack pointer; if so, it's something
276 we won't understand, so quit now. */
277 if (buf
[0] == 0xf2 && (buf
[1] & 0xf3) == 0xf0)
280 fi
->status
= NO_MORE_FRAMES
;
284 /* Now look for movm [regs],sp, which saves the callee saved registers.
286 At this time we don't know if fi->frame is valid, so we only note
287 that we encountered a movm instruction. Later, we'll set the entries
288 in fsr.regs as needed. */
291 /* Extract the register list for the movm instruction. */
292 status
= read_memory_nobpt (addr
+ 1, buf
, 1);
297 /* Quit now if we're beyond the stop point. */
300 /* Fix fi->frame since it's bogus at this point. */
301 if (fi
&& fi
->next
== NULL
)
302 fi
->frame
= read_sp ();
304 /* Note if/where callee saved registers were saved. */
305 set_movm_offsets (fi
, movm_args
);
309 /* Get the next two bytes so the prologue scan can continue. */
310 status
= read_memory_nobpt (addr
, buf
, 2);
313 /* Fix fi->frame since it's bogus at this point. */
314 if (fi
&& fi
->next
== NULL
)
315 fi
->frame
= read_sp ();
317 /* Note if/where callee saved registers were saved. */
318 set_movm_offsets (fi
, movm_args
);
323 /* Now see if we set up a frame pointer via "mov sp,a3" */
328 /* The frame pointer is now valid. */
331 fi
->status
|= MY_FRAME_IN_FP
;
332 fi
->status
&= ~MY_FRAME_IN_SP
;
335 /* Quit now if we're beyond the stop point. */
338 /* Fix fi->frame if it's bogus at this point. */
339 fix_frame_pointer (fi
, 0);
341 /* Note if/where callee saved registers were saved. */
342 set_movm_offsets (fi
, movm_args
);
346 /* Get two more bytes so scanning can continue. */
347 status
= read_memory_nobpt (addr
, buf
, 2);
350 /* Fix fi->frame if it's bogus at this point. */
351 fix_frame_pointer (fi
, 0);
353 /* Note if/where callee saved registers were saved. */
354 set_movm_offsets (fi
, movm_args
);
359 /* Next we should allocate the local frame. No more prologue insns
360 are found after allocating the local frame.
362 Search for add imm8,sp (0xf8feXX)
363 or add imm16,sp (0xfafeXXXX)
364 or add imm32,sp (0xfcfeXXXXXXXX).
366 If none of the above was found, then this prologue has no
369 status
= read_memory_nobpt (addr
, buf
, 2);
372 /* Fix fi->frame if it's bogus at this point. */
373 fix_frame_pointer (fi
, 0);
375 /* Note if/where callee saved registers were saved. */
376 set_movm_offsets (fi
, movm_args
);
381 if (buf
[0] == 0xf8 && buf
[1] == 0xfe)
383 else if (buf
[0] == 0xfa && buf
[1] == 0xfe)
385 else if (buf
[0] == 0xfc && buf
[1] == 0xfe)
390 /* Suck in imm_size more bytes, they'll hold the size of the
392 status
= read_memory_nobpt (addr
+ 2, buf
, imm_size
);
395 /* Fix fi->frame if it's bogus at this point. */
396 fix_frame_pointer (fi
, 0);
398 /* Note if/where callee saved registers were saved. */
399 set_movm_offsets (fi
, movm_args
);
403 /* Note the size of the stack in the frame info structure. */
404 stack_size
= extract_signed_integer (buf
, imm_size
);
406 fi
->stack_size
= stack_size
;
408 /* We just consumed 2 + imm_size bytes. */
409 addr
+= 2 + imm_size
;
411 /* No more prologue insns follow, so begin preparation to return. */
412 /* Fix fi->frame if it's bogus at this point. */
413 fix_frame_pointer (fi
, stack_size
);
415 /* Note if/where callee saved registers were saved. */
416 set_movm_offsets (fi
, movm_args
);
420 /* We never found an insn which allocates local stack space, regardless
421 this is the end of the prologue. */
422 /* Fix fi->frame if it's bogus at this point. */
423 fix_frame_pointer (fi
, 0);
425 /* Note if/where callee saved registers were saved. */
426 set_movm_offsets (fi
, movm_args
);
430 /* Function: frame_chain
431 Figure out and return the caller's frame pointer given current
434 We don't handle dummy frames yet but we would probably just return the
435 stack pointer that was in use at the time the function call was made? */
438 mn10300_frame_chain (fi
)
439 struct frame_info
*fi
;
441 struct frame_info dummy_frame
;
443 /* Walk through the prologue to determine the stack size,
444 location of saved registers, end of the prologue, etc. */
446 mn10300_analyze_prologue (fi
, (CORE_ADDR
)0);
448 /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */
449 if (fi
->status
& NO_MORE_FRAMES
)
452 /* Now that we've analyzed our prologue, determine the frame
453 pointer for our caller.
455 If our caller has a frame pointer, then we need to
456 find the entry value of $a3 to our function.
458 If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
459 location pointed to by fsr.regs[A3_REGNUM].
461 Else it's still in $a3.
463 If our caller does not have a frame pointer, then his
464 frame base is fi->frame + -caller's stack size. */
466 /* The easiest way to get that info is to analyze our caller's frame.
468 So we set up a dummy frame and call mn10300_analyze_prologue to
469 find stuff for us. */
470 dummy_frame
.pc
= FRAME_SAVED_PC (fi
);
471 dummy_frame
.frame
= fi
->frame
;
472 memset (dummy_frame
.fsr
.regs
, '\000', sizeof dummy_frame
.fsr
.regs
);
473 dummy_frame
.status
= 0;
474 dummy_frame
.stack_size
= 0;
475 mn10300_analyze_prologue (&dummy_frame
, 0);
477 if (dummy_frame
.status
& MY_FRAME_IN_FP
)
479 /* Our caller has a frame pointer. So find the frame in $a3 or
481 if (fi
->fsr
.regs
[A3_REGNUM
])
482 return (read_memory_integer (fi
->fsr
.regs
[A3_REGNUM
], REGISTER_SIZE
));
484 return read_register (A3_REGNUM
);
490 adjust
+= (fi
->fsr
.regs
[D2_REGNUM
] ? 4 : 0);
491 adjust
+= (fi
->fsr
.regs
[D3_REGNUM
] ? 4 : 0);
492 adjust
+= (fi
->fsr
.regs
[A2_REGNUM
] ? 4 : 0);
493 adjust
+= (fi
->fsr
.regs
[A3_REGNUM
] ? 4 : 0);
494 /* start-sanitize-am33 */
497 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+5] ? 4 : 0);
498 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+4] ? 4 : 0);
499 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+3] ? 4 : 0);
500 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+2] ? 4 : 0);
502 /* end-sanitize-am33 */
504 /* Our caller does not have a frame pointer. So his frame starts
505 at the base of our frame (fi->frame) + register save space
507 return fi
->frame
+ adjust
+ -dummy_frame
.stack_size
;
511 /* Function: skip_prologue
512 Return the address of the first inst past the prologue of the function. */
515 mn10300_skip_prologue (pc
)
518 /* We used to check the debug symbols, but that can lose if
519 we have a null prologue. */
520 return mn10300_analyze_prologue (NULL
, pc
);
524 /* Function: pop_frame
525 This routine gets called when either the user uses the `return'
526 command, or the call dummy breakpoint gets hit. */
529 mn10300_pop_frame (frame
)
530 struct frame_info
*frame
;
534 if (PC_IN_CALL_DUMMY(frame
->pc
, frame
->frame
, frame
->frame
))
535 generic_pop_dummy_frame ();
538 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
540 /* Restore any saved registers. */
541 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
542 if (frame
->fsr
.regs
[regnum
] != 0)
546 value
= read_memory_unsigned_integer (frame
->fsr
.regs
[regnum
],
547 REGISTER_RAW_SIZE (regnum
));
548 write_register (regnum
, value
);
551 /* Actually cut back the stack. */
552 write_register (SP_REGNUM
, FRAME_FP (frame
));
554 /* Don't we need to set the PC?!? XXX FIXME. */
557 /* Throw away any cached frame information. */
558 flush_cached_frames ();
561 /* Function: push_arguments
562 Setup arguments for a call to the target. Arguments go in
563 order on the stack. */
566 mn10300_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
570 unsigned char struct_return
;
571 CORE_ADDR struct_addr
;
575 int stack_offset
= 0;
576 int regsused
= struct_return
? 1 : 0;
578 /* This should be a nop, but align the stack just in case something
579 went wrong. Stacks are four byte aligned on the mn10300. */
582 /* Now make space on the stack for the args.
584 XXX This doesn't appear to handle pass-by-invisible reference
586 for (argnum
= 0; argnum
< nargs
; argnum
++)
588 int arg_length
= (TYPE_LENGTH (VALUE_TYPE (args
[argnum
])) + 3) & ~3;
590 while (regsused
< 2 && arg_length
> 0)
598 /* Allocate stack space. */
601 regsused
= struct_return
? 1 : 0;
602 /* Push all arguments onto the stack. */
603 for (argnum
= 0; argnum
< nargs
; argnum
++)
608 /* XXX Check this. What about UNIONS? */
609 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
610 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
612 /* XXX Wrong, we want a pointer to this argument. */
613 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
614 val
= (char *)VALUE_CONTENTS (*args
);
618 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
619 val
= (char *)VALUE_CONTENTS (*args
);
622 while (regsused
< 2 && len
> 0)
624 write_register (regsused
, extract_unsigned_integer (val
, 4));
632 write_memory (sp
+ stack_offset
, val
, 4);
641 /* Make space for the flushback area. */
646 /* Function: push_return_address (pc)
647 Set up the return address for the inferior function call.
648 Needed for targets where we don't actually execute a JSR/BSR instruction */
651 mn10300_push_return_address (pc
, sp
)
655 unsigned char buf
[4];
657 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
658 write_memory (sp
- 4, buf
, 4);
662 /* Function: store_struct_return (addr,sp)
663 Store the structure value return address for an inferior function
667 mn10300_store_struct_return (addr
, sp
)
671 /* The structure return address is passed as the first argument. */
672 write_register (0, addr
);
676 /* Function: frame_saved_pc
677 Find the caller of this frame. We do this by seeing if RP_REGNUM
678 is saved in the stack anywhere, otherwise we get it from the
679 registers. If the inner frame is a dummy frame, return its PC
680 instead of RP, because that's where "caller" of the dummy-frame
684 mn10300_frame_saved_pc (fi
)
685 struct frame_info
*fi
;
689 adjust
+= (fi
->fsr
.regs
[D2_REGNUM
] ? 4 : 0);
690 adjust
+= (fi
->fsr
.regs
[D3_REGNUM
] ? 4 : 0);
691 adjust
+= (fi
->fsr
.regs
[A2_REGNUM
] ? 4 : 0);
692 adjust
+= (fi
->fsr
.regs
[A3_REGNUM
] ? 4 : 0);
693 /* start-sanitize-am33 */
696 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+5] ? 4 : 0);
697 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+4] ? 4 : 0);
698 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+3] ? 4 : 0);
699 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+2] ? 4 : 0);
701 /* end-sanitize-am33 */
703 return (read_memory_integer (fi
->frame
+ adjust
, REGISTER_SIZE
));
707 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
711 struct frame_info
*frame
;
713 enum lval_type
*lval
;
715 generic_get_saved_register (raw_buffer
, optimized
, addrp
,
716 frame
, regnum
, lval
);
719 /* Function: init_extra_frame_info
720 Setup the frame's frame pointer, pc, and frame addresses for saved
721 registers. Most of the work is done in mn10300_analyze_prologue().
723 Note that when we are called for the last frame (currently active frame),
724 that fi->pc and fi->frame will already be setup. However, fi->frame will
725 be valid only if this routine uses FP. For previous frames, fi-frame will
726 always be correct. mn10300_analyze_prologue will fix fi->frame if
729 We can be called with the PC in the call dummy under two circumstances.
730 First, during normal backtracing, second, while figuring out the frame
731 pointer just prior to calling the target function (see run_stack_dummy). */
734 mn10300_init_extra_frame_info (fi
)
735 struct frame_info
*fi
;
738 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
740 memset (fi
->fsr
.regs
, '\000', sizeof fi
->fsr
.regs
);
744 mn10300_analyze_prologue (fi
, 0);
747 /* Function: mn10300_virtual_frame_pointer
748 Return the register that the function uses for a frame pointer,
749 plus any necessary offset to be applied to the register before
750 any frame pointer offsets. */
753 mn10300_virtual_frame_pointer (pc
, reg
, offset
)
758 struct frame_info fi
;
760 /* Set up a dummy frame_info. */
766 /* Analyze the prolog and fill in the extra info. */
767 mn10300_init_extra_frame_info (&fi
);
769 /* Results will tell us which type of frame it uses. */
770 if (fi
.status
& MY_FRAME_IN_SP
)
773 *offset
= -(fi
.stack_size
);
782 /* This can be made more generic later. */
784 set_machine_hook (filename
)
789 if (bfd_get_mach (exec_bfd
) == bfd_mach_mn10300
790 || bfd_get_mach (exec_bfd
) == 0)
792 mn10300_register_names
= mn10300_generic_register_names
;
795 /* start-sanitize-am33 */
797 if (bfd_get_mach (exec_bfd
) == bfd_mach_am33
)
800 mn10300_register_names
= am33_register_names
;
803 /* end-sanitize-am33 */
807 _initialize_mn10300_tdep ()
809 /* printf("_initialize_mn10300_tdep\n"); */
811 tm_print_insn
= print_insn_mn10300
;
813 specify_exec_file_hook (set_machine_hook
);