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
PARAMS ((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
;
54 mn10300_register_name (i
)
57 return mn10300_register_names
[i
];
61 mn10300_saved_pc_after_call (fi
)
62 struct frame_info
*fi
;
64 return read_memory_integer (read_register (SP_REGNUM
), 4);
68 mn10300_extract_return_value (type
, regbuf
, valbuf
)
73 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
74 memcpy (valbuf
, regbuf
+ REGISTER_BYTE (4), TYPE_LENGTH (type
));
76 memcpy (valbuf
, regbuf
+ REGISTER_BYTE (0), TYPE_LENGTH (type
));
80 mn10300_extract_struct_value_address (regbuf
)
83 return extract_address (regbuf
+ REGISTER_BYTE (4),
84 REGISTER_RAW_SIZE (4));
88 mn10300_store_return_value (type
, valbuf
)
92 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
93 write_register_bytes (REGISTER_BYTE (4), valbuf
, TYPE_LENGTH (type
));
95 write_register_bytes (REGISTER_BYTE (0), valbuf
, TYPE_LENGTH (type
));
98 static struct frame_info
*analyze_dummy_frame
PARAMS ((CORE_ADDR
, CORE_ADDR
));
99 static struct frame_info
*
100 analyze_dummy_frame (pc
, frame
)
104 static struct frame_info
*dummy
= NULL
;
107 dummy
= xmalloc (sizeof (struct frame_info
));
108 dummy
->saved_regs
= xmalloc (SIZEOF_FRAME_SAVED_REGS
);
109 dummy
->extra_info
= xmalloc (sizeof (struct frame_extra_info
));
114 dummy
->frame
= frame
;
115 dummy
->extra_info
->status
= 0;
116 dummy
->extra_info
->stack_size
= 0;
117 memset (dummy
->saved_regs
, '\000', SIZEOF_FRAME_SAVED_REGS
);
118 mn10300_analyze_prologue (dummy
, 0);
122 /* Values for frame_info.status */
124 #define MY_FRAME_IN_SP 0x1
125 #define MY_FRAME_IN_FP 0x2
126 #define NO_MORE_FRAMES 0x4
129 /* Should call_function allocate stack space for a struct return? */
131 mn10300_use_struct_convention (gcc_p
, type
)
135 return (TYPE_NFIELDS (type
) > 1 || TYPE_LENGTH (type
) > 8);
138 /* The breakpoint instruction must be the same size as the smallest
139 instruction in the instruction set.
141 The Matsushita mn10x00 processors have single byte instructions
142 so we need a single byte breakpoint. Matsushita hasn't defined
143 one, so we defined it ourselves. */
146 mn10300_breakpoint_from_pc (bp_addr
, bp_size
)
150 static char breakpoint
[] =
157 /* Fix fi->frame if it's bogus at this point. This is a helper
158 function for mn10300_analyze_prologue. */
161 fix_frame_pointer (fi
, stack_size
)
162 struct frame_info
*fi
;
165 if (fi
&& fi
->next
== NULL
)
167 if (fi
->extra_info
->status
& MY_FRAME_IN_SP
)
168 fi
->frame
= read_sp () - stack_size
;
169 else if (fi
->extra_info
->status
& MY_FRAME_IN_FP
)
170 fi
->frame
= read_register (A3_REGNUM
);
175 /* Set offsets of registers saved by movm instruction.
176 This is a helper function for mn10300_analyze_prologue. */
179 set_movm_offsets (fi
, movm_args
)
180 struct frame_info
*fi
;
185 if (fi
== NULL
|| movm_args
== 0)
188 if (movm_args
& 0x10)
190 fi
->saved_regs
[A3_REGNUM
] = fi
->frame
+ offset
;
193 if (movm_args
& 0x20)
195 fi
->saved_regs
[A2_REGNUM
] = fi
->frame
+ offset
;
198 if (movm_args
& 0x40)
200 fi
->saved_regs
[D3_REGNUM
] = fi
->frame
+ offset
;
203 if (movm_args
& 0x80)
205 fi
->saved_regs
[D2_REGNUM
] = fi
->frame
+ offset
;
211 /* The main purpose of this file is dealing with prologues to extract
212 information about stack frames and saved registers.
214 For reference here's how prologues look on the mn10300:
217 movm [d2,d3,a2,a3],sp
221 Without frame pointer:
222 movm [d2,d3,a2,a3],sp (if needed)
225 One day we might keep the stack pointer constant, that won't
226 change the code for prologues, but it will make the frame
227 pointerless case much more common. */
229 /* Analyze the prologue to determine where registers are saved,
230 the end of the prologue, etc etc. Return the end of the prologue
233 We store into FI (if non-null) several tidbits of information:
235 * stack_size -- size of this stack frame. Note that if we stop in
236 certain parts of the prologue/epilogue we may claim the size of the
237 current frame is zero. This happens when the current frame has
238 not been allocated yet or has already been deallocated.
240 * fsr -- Addresses of registers saved in the stack by this frame.
242 * status -- A (relatively) generic status indicator. It's a bitmask
243 with the following bits:
245 MY_FRAME_IN_SP: The base of the current frame is actually in
246 the stack pointer. This can happen for frame pointerless
247 functions, or cases where we're stopped in the prologue/epilogue
248 itself. For these cases mn10300_analyze_prologue will need up
249 update fi->frame before returning or analyzing the register
252 MY_FRAME_IN_FP: The base of the current frame is in the
253 frame pointer register ($a2).
255 NO_MORE_FRAMES: Set this if the current frame is "start" or
256 if the first instruction looks like mov <imm>,sp. This tells
257 frame chain to not bother trying to unwind past this frame. */
260 mn10300_analyze_prologue (fi
, pc
)
261 struct frame_info
*fi
;
264 CORE_ADDR func_addr
, func_end
, addr
, stop
;
265 CORE_ADDR stack_size
;
267 unsigned char buf
[4];
268 int status
, movm_args
= 0;
271 /* Use the PC in the frame if it's provided to look up the
272 start of this function. */
273 pc
= (fi
? fi
->pc
: pc
);
275 /* Find the start of this function. */
276 status
= find_pc_partial_function (pc
, &name
, &func_addr
, &func_end
);
278 /* Do nothing if we couldn't find the start of this function or if we're
279 stopped at the first instruction in the prologue. */
285 /* If we're in start, then give up. */
286 if (strcmp (name
, "start") == 0)
289 fi
->extra_info
->status
= NO_MORE_FRAMES
;
293 /* At the start of a function our frame is in the stack pointer. */
295 fi
->extra_info
->status
= MY_FRAME_IN_SP
;
297 /* Get the next two bytes into buf, we need two because rets is a two
298 byte insn and the first isn't enough to uniquely identify it. */
299 status
= read_memory_nobpt (pc
, buf
, 2);
303 /* If we're physically on an "rets" instruction, then our frame has
304 already been deallocated. Note this can also be true for retf
305 and ret if they specify a size of zero.
307 In this case fi->frame is bogus, we need to fix it. */
308 if (fi
&& buf
[0] == 0xf0 && buf
[1] == 0xfc)
310 if (fi
->next
== NULL
)
311 fi
->frame
= read_sp ();
315 /* Similarly if we're stopped on the first insn of a prologue as our
316 frame hasn't been allocated yet. */
317 if (fi
&& fi
->pc
== func_addr
)
319 if (fi
->next
== NULL
)
320 fi
->frame
= read_sp ();
324 /* Figure out where to stop scanning. */
325 stop
= fi
? fi
->pc
: func_end
;
327 /* Don't walk off the end of the function. */
328 stop
= stop
> func_end
? func_end
: stop
;
330 /* Start scanning on the first instruction of this function. */
333 /* Suck in two bytes. */
334 status
= read_memory_nobpt (addr
, buf
, 2);
337 fix_frame_pointer (fi
, 0);
341 /* First see if this insn sets the stack pointer; if so, it's something
342 we won't understand, so quit now. */
343 if (buf
[0] == 0xf2 && (buf
[1] & 0xf3) == 0xf0)
346 fi
->extra_info
->status
= NO_MORE_FRAMES
;
350 /* Now look for movm [regs],sp, which saves the callee saved registers.
352 At this time we don't know if fi->frame is valid, so we only note
353 that we encountered a movm instruction. Later, we'll set the entries
354 in fsr.regs as needed. */
357 /* Extract the register list for the movm instruction. */
358 status
= read_memory_nobpt (addr
+ 1, buf
, 1);
363 /* Quit now if we're beyond the stop point. */
366 /* Fix fi->frame since it's bogus at this point. */
367 if (fi
&& fi
->next
== NULL
)
368 fi
->frame
= read_sp ();
370 /* Note if/where callee saved registers were saved. */
371 set_movm_offsets (fi
, movm_args
);
375 /* Get the next two bytes so the prologue scan can continue. */
376 status
= read_memory_nobpt (addr
, buf
, 2);
379 /* Fix fi->frame since it's bogus at this point. */
380 if (fi
&& fi
->next
== NULL
)
381 fi
->frame
= read_sp ();
383 /* Note if/where callee saved registers were saved. */
384 set_movm_offsets (fi
, movm_args
);
389 /* Now see if we set up a frame pointer via "mov sp,a3" */
394 /* The frame pointer is now valid. */
397 fi
->extra_info
->status
|= MY_FRAME_IN_FP
;
398 fi
->extra_info
->status
&= ~MY_FRAME_IN_SP
;
401 /* Quit now if we're beyond the stop point. */
404 /* Fix fi->frame if it's bogus at this point. */
405 fix_frame_pointer (fi
, 0);
407 /* Note if/where callee saved registers were saved. */
408 set_movm_offsets (fi
, movm_args
);
412 /* Get two more bytes so scanning can continue. */
413 status
= read_memory_nobpt (addr
, buf
, 2);
416 /* Fix fi->frame if it's bogus at this point. */
417 fix_frame_pointer (fi
, 0);
419 /* Note if/where callee saved registers were saved. */
420 set_movm_offsets (fi
, movm_args
);
425 /* Next we should allocate the local frame. No more prologue insns
426 are found after allocating the local frame.
428 Search for add imm8,sp (0xf8feXX)
429 or add imm16,sp (0xfafeXXXX)
430 or add imm32,sp (0xfcfeXXXXXXXX).
432 If none of the above was found, then this prologue has no
435 status
= read_memory_nobpt (addr
, buf
, 2);
438 /* Fix fi->frame if it's bogus at this point. */
439 fix_frame_pointer (fi
, 0);
441 /* Note if/where callee saved registers were saved. */
442 set_movm_offsets (fi
, movm_args
);
447 if (buf
[0] == 0xf8 && buf
[1] == 0xfe)
449 else if (buf
[0] == 0xfa && buf
[1] == 0xfe)
451 else if (buf
[0] == 0xfc && buf
[1] == 0xfe)
456 /* Suck in imm_size more bytes, they'll hold the size of the
458 status
= read_memory_nobpt (addr
+ 2, buf
, imm_size
);
461 /* Fix fi->frame if it's bogus at this point. */
462 fix_frame_pointer (fi
, 0);
464 /* Note if/where callee saved registers were saved. */
465 set_movm_offsets (fi
, movm_args
);
469 /* Note the size of the stack in the frame info structure. */
470 stack_size
= extract_signed_integer (buf
, imm_size
);
472 fi
->extra_info
->stack_size
= stack_size
;
474 /* We just consumed 2 + imm_size bytes. */
475 addr
+= 2 + imm_size
;
477 /* No more prologue insns follow, so begin preparation to return. */
478 /* Fix fi->frame if it's bogus at this point. */
479 fix_frame_pointer (fi
, stack_size
);
481 /* Note if/where callee saved registers were saved. */
482 set_movm_offsets (fi
, movm_args
);
486 /* We never found an insn which allocates local stack space, regardless
487 this is the end of the prologue. */
488 /* Fix fi->frame if it's bogus at this point. */
489 fix_frame_pointer (fi
, 0);
491 /* Note if/where callee saved registers were saved. */
492 set_movm_offsets (fi
, movm_args
);
496 /* Function: frame_chain
497 Figure out and return the caller's frame pointer given current
500 We don't handle dummy frames yet but we would probably just return the
501 stack pointer that was in use at the time the function call was made? */
504 mn10300_frame_chain (fi
)
505 struct frame_info
*fi
;
507 struct frame_info
*dummy
;
508 /* Walk through the prologue to determine the stack size,
509 location of saved registers, end of the prologue, etc. */
510 if (fi
->extra_info
->status
== 0)
511 mn10300_analyze_prologue (fi
, (CORE_ADDR
) 0);
513 /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */
514 if (fi
->extra_info
->status
& NO_MORE_FRAMES
)
517 /* Now that we've analyzed our prologue, determine the frame
518 pointer for our caller.
520 If our caller has a frame pointer, then we need to
521 find the entry value of $a3 to our function.
523 If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
524 location pointed to by fsr.regs[A3_REGNUM].
526 Else it's still in $a3.
528 If our caller does not have a frame pointer, then his
529 frame base is fi->frame + -caller's stack size. */
531 /* The easiest way to get that info is to analyze our caller's frame.
532 So we set up a dummy frame and call mn10300_analyze_prologue to
533 find stuff for us. */
534 dummy
= analyze_dummy_frame (FRAME_SAVED_PC (fi
), fi
->frame
);
536 if (dummy
->extra_info
->status
& MY_FRAME_IN_FP
)
538 /* Our caller has a frame pointer. So find the frame in $a3 or
540 if (fi
->saved_regs
[A3_REGNUM
])
541 return (read_memory_integer (fi
->saved_regs
[A3_REGNUM
], REGISTER_SIZE
));
543 return read_register (A3_REGNUM
);
549 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
550 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
551 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
552 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 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 (pc
)
568 /* We used to check the debug symbols, but that can lose if
569 we have a null prologue. */
570 return mn10300_analyze_prologue (NULL
, pc
);
574 /* Function: pop_frame
575 This routine gets called when either the user uses the `return'
576 command, or the call dummy breakpoint gets hit. */
579 mn10300_pop_frame (frame
)
580 struct frame_info
*frame
;
584 if (PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
585 generic_pop_dummy_frame ();
588 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
590 /* Restore any saved registers. */
591 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
592 if (frame
->saved_regs
[regnum
] != 0)
596 value
= read_memory_unsigned_integer (frame
->saved_regs
[regnum
],
597 REGISTER_RAW_SIZE (regnum
));
598 write_register (regnum
, value
);
601 /* Actually cut back the stack. */
602 write_register (SP_REGNUM
, FRAME_FP (frame
));
604 /* Don't we need to set the PC?!? XXX FIXME. */
607 /* Throw away any cached frame information. */
608 flush_cached_frames ();
611 /* Function: push_arguments
612 Setup arguments for a call to the target. Arguments go in
613 order on the stack. */
616 mn10300_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
620 unsigned char struct_return
;
621 CORE_ADDR struct_addr
;
625 int stack_offset
= 0;
626 int regsused
= struct_return
? 1 : 0;
628 /* This should be a nop, but align the stack just in case something
629 went wrong. Stacks are four byte aligned on the mn10300. */
632 /* Now make space on the stack for the args.
634 XXX This doesn't appear to handle pass-by-invisible reference
636 for (argnum
= 0; argnum
< nargs
; argnum
++)
638 int arg_length
= (TYPE_LENGTH (VALUE_TYPE (args
[argnum
])) + 3) & ~3;
640 while (regsused
< 2 && arg_length
> 0)
648 /* Allocate stack space. */
651 regsused
= struct_return
? 1 : 0;
652 /* Push all arguments onto the stack. */
653 for (argnum
= 0; argnum
< nargs
; argnum
++)
658 /* XXX Check this. What about UNIONS? */
659 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
660 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
662 /* XXX Wrong, we want a pointer to this argument. */
663 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
664 val
= (char *) VALUE_CONTENTS (*args
);
668 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
669 val
= (char *) VALUE_CONTENTS (*args
);
672 while (regsused
< 2 && len
> 0)
674 write_register (regsused
, extract_unsigned_integer (val
, 4));
682 write_memory (sp
+ stack_offset
, val
, 4);
691 /* Make space for the flushback area. */
696 /* Function: push_return_address (pc)
697 Set up the return address for the inferior function call.
698 Needed for targets where we don't actually execute a JSR/BSR instruction */
701 mn10300_push_return_address (pc
, sp
)
705 unsigned char buf
[4];
707 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
708 write_memory (sp
- 4, buf
, 4);
712 /* Function: store_struct_return (addr,sp)
713 Store the structure value return address for an inferior function
717 mn10300_store_struct_return (addr
, sp
)
721 /* The structure return address is passed as the first argument. */
722 write_register (0, addr
);
726 /* Function: frame_saved_pc
727 Find the caller of this frame. We do this by seeing if RP_REGNUM
728 is saved in the stack anywhere, otherwise we get it from the
729 registers. If the inner frame is a dummy frame, return its PC
730 instead of RP, because that's where "caller" of the dummy-frame
734 mn10300_frame_saved_pc (fi
)
735 struct frame_info
*fi
;
739 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
740 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
741 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
742 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 4 : 0);
744 return (read_memory_integer (fi
->frame
+ adjust
, REGISTER_SIZE
));
747 /* Function: mn10300_init_extra_frame_info
748 Setup the frame's frame pointer, pc, and frame addresses for saved
749 registers. Most of the work is done in mn10300_analyze_prologue().
751 Note that when we are called for the last frame (currently active frame),
752 that fi->pc and fi->frame will already be setup. However, fi->frame will
753 be valid only if this routine uses FP. For previous frames, fi-frame will
754 always be correct. mn10300_analyze_prologue will fix fi->frame if
757 We can be called with the PC in the call dummy under two circumstances.
758 First, during normal backtracing, second, while figuring out the frame
759 pointer just prior to calling the target function (see run_stack_dummy). */
762 mn10300_init_extra_frame_info (fi
)
763 struct frame_info
*fi
;
766 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
768 frame_saved_regs_zalloc (fi
);
769 fi
->extra_info
= (struct frame_extra_info
*)
770 frame_obstack_alloc (sizeof (struct frame_extra_info
));
772 fi
->extra_info
->status
= 0;
773 fi
->extra_info
->stack_size
= 0;
775 mn10300_analyze_prologue (fi
, 0);
778 /* Function: mn10300_virtual_frame_pointer
779 Return the register that the function uses for a frame pointer,
780 plus any necessary offset to be applied to the register before
781 any frame pointer offsets. */
784 mn10300_virtual_frame_pointer (pc
, reg
, offset
)
789 struct frame_info
*dummy
= analyze_dummy_frame (pc
, 0);
790 /* Set up a dummy frame_info, Analyze the prolog and fill in the
792 /* Results will tell us which type of frame it uses. */
793 if (dummy
->extra_info
->status
& MY_FRAME_IN_SP
)
796 *offset
= -(dummy
->extra_info
->stack_size
);
805 /* This can be made more generic later. */
807 set_machine_hook (filename
)
812 if (bfd_get_mach (exec_bfd
) == bfd_mach_mn10300
813 || bfd_get_mach (exec_bfd
) == 0)
815 mn10300_register_names
= mn10300_generic_register_names
;
821 _initialize_mn10300_tdep ()
823 /* printf("_initialize_mn10300_tdep\n"); */
825 tm_print_insn
= print_insn_mn10300
;
827 specify_exec_file_hook (set_machine_hook
);