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 extern void _initialize_mn10300_tdep (void);
33 static CORE_ADDR
mn10300_analyze_prologue (struct frame_info
*fi
,
36 /* Additional info used by the frame */
38 struct frame_extra_info
45 static char *mn10300_generic_register_names
[] =
46 {"d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
47 "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
48 "", "", "", "", "", "", "", "",
49 "", "", "", "", "", "", "", "fp"};
51 static char **mn10300_register_names
= mn10300_generic_register_names
;
52 static char *am33_register_names
[] =
54 "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
55 "sp", "pc", "mdr", "psw", "lir", "lar", "",
56 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
57 "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""};
61 mn10300_register_name (int i
)
63 return mn10300_register_names
[i
];
67 mn10300_saved_pc_after_call (struct frame_info
*fi
)
69 return read_memory_integer (read_register (SP_REGNUM
), 4);
73 mn10300_extract_return_value (struct type
*type
, char *regbuf
, char *valbuf
)
75 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
76 memcpy (valbuf
, regbuf
+ REGISTER_BYTE (4), TYPE_LENGTH (type
));
78 memcpy (valbuf
, regbuf
+ REGISTER_BYTE (0), TYPE_LENGTH (type
));
82 mn10300_extract_struct_value_address (char *regbuf
)
84 return extract_address (regbuf
+ REGISTER_BYTE (4),
85 REGISTER_RAW_SIZE (4));
89 mn10300_store_return_value (struct type
*type
, char *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 (CORE_ADDR
, CORE_ADDR
);
98 static struct frame_info
*
99 analyze_dummy_frame (CORE_ADDR pc
, CORE_ADDR frame
)
101 static struct frame_info
*dummy
= NULL
;
104 dummy
= xmalloc (sizeof (struct frame_info
));
105 dummy
->saved_regs
= xmalloc (SIZEOF_FRAME_SAVED_REGS
);
106 dummy
->extra_info
= xmalloc (sizeof (struct frame_extra_info
));
111 dummy
->frame
= frame
;
112 dummy
->extra_info
->status
= 0;
113 dummy
->extra_info
->stack_size
= 0;
114 memset (dummy
->saved_regs
, '\000', SIZEOF_FRAME_SAVED_REGS
);
115 mn10300_analyze_prologue (dummy
, 0);
119 /* Values for frame_info.status */
121 #define MY_FRAME_IN_SP 0x1
122 #define MY_FRAME_IN_FP 0x2
123 #define NO_MORE_FRAMES 0x4
126 /* Should call_function allocate stack space for a struct return? */
128 mn10300_use_struct_convention (int gcc_p
, struct type
*type
)
130 return (TYPE_NFIELDS (type
) > 1 || TYPE_LENGTH (type
) > 8);
133 /* The breakpoint instruction must be the same size as the smallest
134 instruction in the instruction set.
136 The Matsushita mn10x00 processors have single byte instructions
137 so we need a single byte breakpoint. Matsushita hasn't defined
138 one, so we defined it ourselves. */
141 mn10300_breakpoint_from_pc (CORE_ADDR
*bp_addr
, int *bp_size
)
143 static char breakpoint
[] =
150 /* Fix fi->frame if it's bogus at this point. This is a helper
151 function for mn10300_analyze_prologue. */
154 fix_frame_pointer (struct frame_info
*fi
, int stack_size
)
156 if (fi
&& fi
->next
== NULL
)
158 if (fi
->extra_info
->status
& MY_FRAME_IN_SP
)
159 fi
->frame
= read_sp () - stack_size
;
160 else if (fi
->extra_info
->status
& MY_FRAME_IN_FP
)
161 fi
->frame
= read_register (A3_REGNUM
);
166 /* Set offsets of registers saved by movm instruction.
167 This is a helper function for mn10300_analyze_prologue. */
170 set_movm_offsets (struct frame_info
*fi
, int movm_args
)
174 if (fi
== NULL
|| movm_args
== 0)
177 if (movm_args
& 0x10)
179 fi
->saved_regs
[A3_REGNUM
] = fi
->frame
+ offset
;
182 if (movm_args
& 0x20)
184 fi
->saved_regs
[A2_REGNUM
] = fi
->frame
+ offset
;
187 if (movm_args
& 0x40)
189 fi
->saved_regs
[D3_REGNUM
] = fi
->frame
+ offset
;
192 if (movm_args
& 0x80)
194 fi
->saved_regs
[D2_REGNUM
] = fi
->frame
+ offset
;
197 if (am33_mode
&& movm_args
& 0x02)
199 fi
->saved_regs
[E0_REGNUM
+ 5] = fi
->frame
+ offset
;
200 fi
->saved_regs
[E0_REGNUM
+ 4] = fi
->frame
+ offset
+ 4;
201 fi
->saved_regs
[E0_REGNUM
+ 3] = fi
->frame
+ offset
+ 8;
202 fi
->saved_regs
[E0_REGNUM
+ 2] = fi
->frame
+ offset
+ 12;
207 /* The main purpose of this file is dealing with prologues to extract
208 information about stack frames and saved registers.
210 For reference here's how prologues look on the mn10300:
213 movm [d2,d3,a2,a3],sp
217 Without frame pointer:
218 movm [d2,d3,a2,a3],sp (if needed)
221 One day we might keep the stack pointer constant, that won't
222 change the code for prologues, but it will make the frame
223 pointerless case much more common. */
225 /* Analyze the prologue to determine where registers are saved,
226 the end of the prologue, etc etc. Return the end of the prologue
229 We store into FI (if non-null) several tidbits of information:
231 * stack_size -- size of this stack frame. Note that if we stop in
232 certain parts of the prologue/epilogue we may claim the size of the
233 current frame is zero. This happens when the current frame has
234 not been allocated yet or has already been deallocated.
236 * fsr -- Addresses of registers saved in the stack by this frame.
238 * status -- A (relatively) generic status indicator. It's a bitmask
239 with the following bits:
241 MY_FRAME_IN_SP: The base of the current frame is actually in
242 the stack pointer. This can happen for frame pointerless
243 functions, or cases where we're stopped in the prologue/epilogue
244 itself. For these cases mn10300_analyze_prologue will need up
245 update fi->frame before returning or analyzing the register
248 MY_FRAME_IN_FP: The base of the current frame is in the
249 frame pointer register ($a2).
251 NO_MORE_FRAMES: Set this if the current frame is "start" or
252 if the first instruction looks like mov <imm>,sp. This tells
253 frame chain to not bother trying to unwind past this frame. */
256 mn10300_analyze_prologue (struct frame_info
*fi
, CORE_ADDR pc
)
258 CORE_ADDR func_addr
, func_end
, addr
, stop
;
259 CORE_ADDR stack_size
;
261 unsigned char buf
[4];
262 int status
, movm_args
= 0;
265 /* Use the PC in the frame if it's provided to look up the
266 start of this function. */
267 pc
= (fi
? fi
->pc
: pc
);
269 /* Find the start of this function. */
270 status
= find_pc_partial_function (pc
, &name
, &func_addr
, &func_end
);
272 /* Do nothing if we couldn't find the start of this function or if we're
273 stopped at the first instruction in the prologue. */
279 /* If we're in start, then give up. */
280 if (strcmp (name
, "start") == 0)
283 fi
->extra_info
->status
= NO_MORE_FRAMES
;
287 /* At the start of a function our frame is in the stack pointer. */
289 fi
->extra_info
->status
= MY_FRAME_IN_SP
;
291 /* Get the next two bytes into buf, we need two because rets is a two
292 byte insn and the first isn't enough to uniquely identify it. */
293 status
= read_memory_nobpt (pc
, buf
, 2);
297 /* If we're physically on an "rets" instruction, then our frame has
298 already been deallocated. Note this can also be true for retf
299 and ret if they specify a size of zero.
301 In this case fi->frame is bogus, we need to fix it. */
302 if (fi
&& buf
[0] == 0xf0 && buf
[1] == 0xfc)
304 if (fi
->next
== NULL
)
305 fi
->frame
= read_sp ();
309 /* Similarly if we're stopped on the first insn of a prologue as our
310 frame hasn't been allocated yet. */
311 if (fi
&& fi
->pc
== func_addr
)
313 if (fi
->next
== NULL
)
314 fi
->frame
= read_sp ();
318 /* Figure out where to stop scanning. */
319 stop
= fi
? fi
->pc
: func_end
;
321 /* Don't walk off the end of the function. */
322 stop
= stop
> func_end
? func_end
: stop
;
324 /* Start scanning on the first instruction of this function. */
327 /* Suck in two bytes. */
328 status
= read_memory_nobpt (addr
, buf
, 2);
331 fix_frame_pointer (fi
, 0);
335 /* First see if this insn sets the stack pointer; if so, it's something
336 we won't understand, so quit now. */
337 if (buf
[0] == 0xf2 && (buf
[1] & 0xf3) == 0xf0)
340 fi
->extra_info
->status
= NO_MORE_FRAMES
;
344 /* Now look for movm [regs],sp, which saves the callee saved registers.
346 At this time we don't know if fi->frame is valid, so we only note
347 that we encountered a movm instruction. Later, we'll set the entries
348 in fsr.regs as needed. */
351 /* Extract the register list for the movm instruction. */
352 status
= read_memory_nobpt (addr
+ 1, buf
, 1);
357 /* Quit now if we're beyond the stop point. */
360 /* Fix fi->frame since it's bogus at this point. */
361 if (fi
&& fi
->next
== NULL
)
362 fi
->frame
= read_sp ();
364 /* Note if/where callee saved registers were saved. */
365 set_movm_offsets (fi
, movm_args
);
369 /* Get the next two bytes so the prologue scan can continue. */
370 status
= read_memory_nobpt (addr
, buf
, 2);
373 /* Fix fi->frame since it's bogus at this point. */
374 if (fi
&& fi
->next
== NULL
)
375 fi
->frame
= read_sp ();
377 /* Note if/where callee saved registers were saved. */
378 set_movm_offsets (fi
, movm_args
);
383 /* Now see if we set up a frame pointer via "mov sp,a3" */
388 /* The frame pointer is now valid. */
391 fi
->extra_info
->status
|= MY_FRAME_IN_FP
;
392 fi
->extra_info
->status
&= ~MY_FRAME_IN_SP
;
395 /* Quit now if we're beyond the stop point. */
398 /* Fix fi->frame if it's bogus at this point. */
399 fix_frame_pointer (fi
, 0);
401 /* Note if/where callee saved registers were saved. */
402 set_movm_offsets (fi
, movm_args
);
406 /* Get two more bytes so scanning can continue. */
407 status
= read_memory_nobpt (addr
, buf
, 2);
410 /* Fix fi->frame if it's bogus at this point. */
411 fix_frame_pointer (fi
, 0);
413 /* Note if/where callee saved registers were saved. */
414 set_movm_offsets (fi
, movm_args
);
419 /* Next we should allocate the local frame. No more prologue insns
420 are found after allocating the local frame.
422 Search for add imm8,sp (0xf8feXX)
423 or add imm16,sp (0xfafeXXXX)
424 or add imm32,sp (0xfcfeXXXXXXXX).
426 If none of the above was found, then this prologue has no
429 status
= read_memory_nobpt (addr
, buf
, 2);
432 /* Fix fi->frame if it's bogus at this point. */
433 fix_frame_pointer (fi
, 0);
435 /* Note if/where callee saved registers were saved. */
436 set_movm_offsets (fi
, movm_args
);
441 if (buf
[0] == 0xf8 && buf
[1] == 0xfe)
443 else if (buf
[0] == 0xfa && buf
[1] == 0xfe)
445 else if (buf
[0] == 0xfc && buf
[1] == 0xfe)
450 /* Suck in imm_size more bytes, they'll hold the size of the
452 status
= read_memory_nobpt (addr
+ 2, buf
, imm_size
);
455 /* Fix fi->frame if it's bogus at this point. */
456 fix_frame_pointer (fi
, 0);
458 /* Note if/where callee saved registers were saved. */
459 set_movm_offsets (fi
, movm_args
);
463 /* Note the size of the stack in the frame info structure. */
464 stack_size
= extract_signed_integer (buf
, imm_size
);
466 fi
->extra_info
->stack_size
= stack_size
;
468 /* We just consumed 2 + imm_size bytes. */
469 addr
+= 2 + imm_size
;
471 /* No more prologue insns follow, so begin preparation to return. */
472 /* Fix fi->frame if it's bogus at this point. */
473 fix_frame_pointer (fi
, stack_size
);
475 /* Note if/where callee saved registers were saved. */
476 set_movm_offsets (fi
, movm_args
);
480 /* We never found an insn which allocates local stack space, regardless
481 this is the end of the prologue. */
482 /* Fix fi->frame if it's bogus at this point. */
483 fix_frame_pointer (fi
, 0);
485 /* Note if/where callee saved registers were saved. */
486 set_movm_offsets (fi
, movm_args
);
490 /* Function: frame_chain
491 Figure out and return the caller's frame pointer given current
494 We don't handle dummy frames yet but we would probably just return the
495 stack pointer that was in use at the time the function call was made? */
498 mn10300_frame_chain (struct frame_info
*fi
)
500 struct frame_info
*dummy
;
501 /* Walk through the prologue to determine the stack size,
502 location of saved registers, end of the prologue, etc. */
503 if (fi
->extra_info
->status
== 0)
504 mn10300_analyze_prologue (fi
, (CORE_ADDR
) 0);
506 /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */
507 if (fi
->extra_info
->status
& NO_MORE_FRAMES
)
510 /* Now that we've analyzed our prologue, determine the frame
511 pointer for our caller.
513 If our caller has a frame pointer, then we need to
514 find the entry value of $a3 to our function.
516 If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
517 location pointed to by fsr.regs[A3_REGNUM].
519 Else it's still in $a3.
521 If our caller does not have a frame pointer, then his
522 frame base is fi->frame + -caller's stack size. */
524 /* The easiest way to get that info is to analyze our caller's frame.
525 So we set up a dummy frame and call mn10300_analyze_prologue to
526 find stuff for us. */
527 dummy
= analyze_dummy_frame (FRAME_SAVED_PC (fi
), fi
->frame
);
529 if (dummy
->extra_info
->status
& MY_FRAME_IN_FP
)
531 /* Our caller has a frame pointer. So find the frame in $a3 or
533 if (fi
->saved_regs
[A3_REGNUM
])
534 return (read_memory_integer (fi
->saved_regs
[A3_REGNUM
], REGISTER_SIZE
));
536 return read_register (A3_REGNUM
);
542 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
543 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
544 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
545 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 4 : 0);
548 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 5] ? 4 : 0);
549 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 4] ? 4 : 0);
550 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 3] ? 4 : 0);
551 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 2] ? 4 : 0);
554 /* Our caller does not have a frame pointer. So his frame starts
555 at the base of our frame (fi->frame) + register save space
557 return fi
->frame
+ adjust
+ -dummy
->extra_info
->stack_size
;
561 /* Function: skip_prologue
562 Return the address of the first inst past the prologue of the function. */
565 mn10300_skip_prologue (CORE_ADDR 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 (struct frame_info
*frame
)
582 if (PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
583 generic_pop_dummy_frame ();
586 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
588 /* Restore any saved registers. */
589 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
590 if (frame
->saved_regs
[regnum
] != 0)
594 value
= read_memory_unsigned_integer (frame
->saved_regs
[regnum
],
595 REGISTER_RAW_SIZE (regnum
));
596 write_register (regnum
, value
);
599 /* Actually cut back the stack. */
600 write_register (SP_REGNUM
, FRAME_FP (frame
));
602 /* Don't we need to set the PC?!? XXX FIXME. */
605 /* Throw away any cached frame information. */
606 flush_cached_frames ();
609 /* Function: push_arguments
610 Setup arguments for a call to the target. Arguments go in
611 order on the stack. */
614 mn10300_push_arguments (int nargs
, value_ptr
*args
, CORE_ADDR sp
,
615 unsigned char struct_return
, CORE_ADDR struct_addr
)
619 int stack_offset
= 0;
620 int regsused
= struct_return
? 1 : 0;
622 /* This should be a nop, but align the stack just in case something
623 went wrong. Stacks are four byte aligned on the mn10300. */
626 /* Now make space on the stack for the args.
628 XXX This doesn't appear to handle pass-by-invisible reference
630 for (argnum
= 0; argnum
< nargs
; argnum
++)
632 int arg_length
= (TYPE_LENGTH (VALUE_TYPE (args
[argnum
])) + 3) & ~3;
634 while (regsused
< 2 && arg_length
> 0)
642 /* Allocate stack space. */
645 regsused
= struct_return
? 1 : 0;
646 /* Push all arguments onto the stack. */
647 for (argnum
= 0; argnum
< nargs
; argnum
++)
652 /* XXX Check this. What about UNIONS? */
653 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
654 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
656 /* XXX Wrong, we want a pointer to this argument. */
657 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
658 val
= (char *) VALUE_CONTENTS (*args
);
662 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
663 val
= (char *) VALUE_CONTENTS (*args
);
666 while (regsused
< 2 && len
> 0)
668 write_register (regsused
, extract_unsigned_integer (val
, 4));
676 write_memory (sp
+ stack_offset
, val
, 4);
685 /* Make space for the flushback area. */
690 /* Function: push_return_address (pc)
691 Set up the return address for the inferior function call.
692 Needed for targets where we don't actually execute a JSR/BSR instruction */
695 mn10300_push_return_address (CORE_ADDR pc
, CORE_ADDR sp
)
697 unsigned char buf
[4];
699 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
700 write_memory (sp
- 4, buf
, 4);
704 /* Function: store_struct_return (addr,sp)
705 Store the structure value return address for an inferior function
709 mn10300_store_struct_return (CORE_ADDR addr
, CORE_ADDR sp
)
711 /* The structure return address is passed as the first argument. */
712 write_register (0, addr
);
716 /* Function: frame_saved_pc
717 Find the caller of this frame. We do this by seeing if RP_REGNUM
718 is saved in the stack anywhere, otherwise we get it from the
719 registers. If the inner frame is a dummy frame, return its PC
720 instead of RP, because that's where "caller" of the dummy-frame
724 mn10300_frame_saved_pc (struct frame_info
*fi
)
728 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
729 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
730 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
731 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 4 : 0);
734 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 5] ? 4 : 0);
735 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 4] ? 4 : 0);
736 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 3] ? 4 : 0);
737 adjust
+= (fi
->saved_regs
[E0_REGNUM
+ 2] ? 4 : 0);
740 return (read_memory_integer (fi
->frame
+ adjust
, REGISTER_SIZE
));
743 /* Function: mn10300_init_extra_frame_info
744 Setup the frame's frame pointer, pc, and frame addresses for saved
745 registers. Most of the work is done in mn10300_analyze_prologue().
747 Note that when we are called for the last frame (currently active frame),
748 that fi->pc and fi->frame will already be setup. However, fi->frame will
749 be valid only if this routine uses FP. For previous frames, fi-frame will
750 always be correct. mn10300_analyze_prologue will fix fi->frame if
753 We can be called with the PC in the call dummy under two circumstances.
754 First, during normal backtracing, second, while figuring out the frame
755 pointer just prior to calling the target function (see run_stack_dummy). */
758 mn10300_init_extra_frame_info (struct frame_info
*fi
)
761 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
763 frame_saved_regs_zalloc (fi
);
764 fi
->extra_info
= (struct frame_extra_info
*)
765 frame_obstack_alloc (sizeof (struct frame_extra_info
));
767 fi
->extra_info
->status
= 0;
768 fi
->extra_info
->stack_size
= 0;
770 mn10300_analyze_prologue (fi
, 0);
773 /* Function: mn10300_virtual_frame_pointer
774 Return the register that the function uses for a frame pointer,
775 plus any necessary offset to be applied to the register before
776 any frame pointer offsets. */
779 mn10300_virtual_frame_pointer (CORE_ADDR pc
, long *reg
, long *offset
)
781 struct frame_info
*dummy
= analyze_dummy_frame (pc
, 0);
782 /* Set up a dummy frame_info, Analyze the prolog and fill in the
784 /* Results will tell us which type of frame it uses. */
785 if (dummy
->extra_info
->status
& MY_FRAME_IN_SP
)
788 *offset
= -(dummy
->extra_info
->stack_size
);
797 /* This can be made more generic later. */
799 set_machine_hook (char *filename
)
803 if (bfd_get_mach (exec_bfd
) == bfd_mach_mn10300
804 || bfd_get_mach (exec_bfd
) == 0)
806 mn10300_register_names
= mn10300_generic_register_names
;
810 if (bfd_get_mach (exec_bfd
) == bfd_mach_am33
)
813 mn10300_register_names
= am33_register_names
;
819 _initialize_mn10300_tdep (void)
821 /* printf("_initialize_mn10300_tdep\n"); */
823 tm_print_insn
= print_insn_mn10300
;
825 specify_exec_file_hook (set_machine_hook
);