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
;
39 static CORE_ADDR mn10300_analyze_prologue
PARAMS ((struct frame_info
*fi
,
42 /* Additional info used by the frame */
44 struct frame_extra_info
50 static struct frame_info
*analyze_dummy_frame
PARAMS ((CORE_ADDR
, CORE_ADDR
));
51 static struct frame_info
*
52 analyze_dummy_frame (pc
, frame
)
56 static struct frame_info
*dummy
= NULL
;
59 dummy
= xmalloc (sizeof (struct frame_info
));
60 dummy
->saved_regs
= xmalloc (SIZEOF_FRAME_SAVED_REGS
);
61 dummy
->extra_info
= xmalloc (sizeof (struct frame_extra_info
));
67 dummy
->extra_info
->status
= 0;
68 dummy
->extra_info
->stack_size
= 0;
69 memset (dummy
->saved_regs
, '\000', SIZEOF_FRAME_SAVED_REGS
);
70 mn10300_analyze_prologue (dummy
, 0);
74 /* Values for frame_info.status */
76 #define MY_FRAME_IN_SP 0x1
77 #define MY_FRAME_IN_FP 0x2
78 #define NO_MORE_FRAMES 0x4
81 /* Should call_function allocate stack space for a struct return? */
83 mn10300_use_struct_convention (gcc_p
, type
)
87 return (TYPE_NFIELDS (type
) > 1 || TYPE_LENGTH (type
) > 8);
90 /* The breakpoint instruction must be the same size as the smallest
91 instruction in the instruction set.
93 The Matsushita mn10x00 processors have single byte instructions
94 so we need a single byte breakpoint. Matsushita hasn't defined
95 one, so we defined it ourselves. */
98 mn10300_breakpoint_from_pc (bp_addr
, bp_size
)
102 static char breakpoint
[] = {0xff};
108 /* Fix fi->frame if it's bogus at this point. This is a helper
109 function for mn10300_analyze_prologue. */
112 fix_frame_pointer (fi
, stack_size
)
113 struct frame_info
*fi
;
116 if (fi
&& fi
->next
== NULL
)
118 if (fi
->extra_info
->status
& MY_FRAME_IN_SP
)
119 fi
->frame
= read_sp () - stack_size
;
120 else if (fi
->extra_info
->status
& MY_FRAME_IN_FP
)
121 fi
->frame
= read_register (A3_REGNUM
);
126 /* Set offsets of registers saved by movm instruction.
127 This is a helper function for mn10300_analyze_prologue. */
130 set_movm_offsets (fi
, movm_args
)
131 struct frame_info
*fi
;
136 if (fi
== NULL
|| movm_args
== 0)
139 if (movm_args
& 0x10)
141 fi
->saved_regs
[A3_REGNUM
] = fi
->frame
+ offset
;
144 if (movm_args
& 0x20)
146 fi
->saved_regs
[A2_REGNUM
] = fi
->frame
+ offset
;
149 if (movm_args
& 0x40)
151 fi
->saved_regs
[D3_REGNUM
] = fi
->frame
+ offset
;
154 if (movm_args
& 0x80)
156 fi
->saved_regs
[D2_REGNUM
] = fi
->frame
+ offset
;
162 /* The main purpose of this file is dealing with prologues to extract
163 information about stack frames and saved registers.
165 For reference here's how prologues look on the mn10300:
168 movm [d2,d3,a2,a3],sp
172 Without frame pointer:
173 movm [d2,d3,a2,a3],sp (if needed)
176 One day we might keep the stack pointer constant, that won't
177 change the code for prologues, but it will make the frame
178 pointerless case much more common. */
180 /* Analyze the prologue to determine where registers are saved,
181 the end of the prologue, etc etc. Return the end of the prologue
184 We store into FI (if non-null) several tidbits of information:
186 * stack_size -- size of this stack frame. Note that if we stop in
187 certain parts of the prologue/epilogue we may claim the size of the
188 current frame is zero. This happens when the current frame has
189 not been allocated yet or has already been deallocated.
191 * fsr -- Addresses of registers saved in the stack by this frame.
193 * status -- A (relatively) generic status indicator. It's a bitmask
194 with the following bits:
196 MY_FRAME_IN_SP: The base of the current frame is actually in
197 the stack pointer. This can happen for frame pointerless
198 functions, or cases where we're stopped in the prologue/epilogue
199 itself. For these cases mn10300_analyze_prologue will need up
200 update fi->frame before returning or analyzing the register
203 MY_FRAME_IN_FP: The base of the current frame is in the
204 frame pointer register ($a2).
206 NO_MORE_FRAMES: Set this if the current frame is "start" or
207 if the first instruction looks like mov <imm>,sp. This tells
208 frame chain to not bother trying to unwind past this frame. */
211 mn10300_analyze_prologue (fi
, pc
)
212 struct frame_info
*fi
;
215 CORE_ADDR func_addr
, func_end
, addr
, stop
;
216 CORE_ADDR stack_size
;
218 unsigned char buf
[4];
219 int status
, movm_args
= 0;
222 /* Use the PC in the frame if it's provided to look up the
223 start of this function. */
224 pc
= (fi
? fi
->pc
: pc
);
226 /* Find the start of this function. */
227 status
= find_pc_partial_function (pc
, &name
, &func_addr
, &func_end
);
229 /* Do nothing if we couldn't find the start of this function or if we're
230 stopped at the first instruction in the prologue. */
234 /* If we're in start, then give up. */
235 if (strcmp (name
, "start") == 0)
238 fi
->extra_info
->status
= NO_MORE_FRAMES
;
242 /* At the start of a function our frame is in the stack pointer. */
244 fi
->extra_info
->status
= MY_FRAME_IN_SP
;
246 /* Get the next two bytes into buf, we need two because rets is a two
247 byte insn and the first isn't enough to uniquely identify it. */
248 status
= read_memory_nobpt (pc
, buf
, 2);
252 /* If we're physically on an "rets" instruction, then our frame has
253 already been deallocated. Note this can also be true for retf
254 and ret if they specify a size of zero.
256 In this case fi->frame is bogus, we need to fix it. */
257 if (fi
&& buf
[0] == 0xf0 && buf
[1] == 0xfc)
259 if (fi
->next
== NULL
)
260 fi
->frame
= read_sp ();
264 /* Similarly if we're stopped on the first insn of a prologue as our
265 frame hasn't been allocated yet. */
266 if (fi
&& fi
->pc
== func_addr
)
268 if (fi
->next
== NULL
)
269 fi
->frame
= read_sp ();
273 /* Figure out where to stop scanning. */
274 stop
= fi
? fi
->pc
: func_end
;
276 /* Don't walk off the end of the function. */
277 stop
= stop
> func_end
? func_end
: stop
;
279 /* Start scanning on the first instruction of this function. */
282 /* Suck in two bytes. */
283 status
= read_memory_nobpt (addr
, buf
, 2);
286 fix_frame_pointer (fi
, 0);
290 /* First see if this insn sets the stack pointer; if so, it's something
291 we won't understand, so quit now. */
292 if (buf
[0] == 0xf2 && (buf
[1] & 0xf3) == 0xf0)
295 fi
->extra_info
->status
= NO_MORE_FRAMES
;
299 /* Now look for movm [regs],sp, which saves the callee saved registers.
301 At this time we don't know if fi->frame is valid, so we only note
302 that we encountered a movm instruction. Later, we'll set the entries
303 in fsr.regs as needed. */
306 /* Extract the register list for the movm instruction. */
307 status
= read_memory_nobpt (addr
+ 1, buf
, 1);
312 /* Quit now if we're beyond the stop point. */
315 /* Fix fi->frame since it's bogus at this point. */
316 if (fi
&& fi
->next
== NULL
)
317 fi
->frame
= read_sp ();
319 /* Note if/where callee saved registers were saved. */
320 set_movm_offsets (fi
, movm_args
);
324 /* Get the next two bytes so the prologue scan can continue. */
325 status
= read_memory_nobpt (addr
, buf
, 2);
328 /* Fix fi->frame since it's bogus at this point. */
329 if (fi
&& fi
->next
== NULL
)
330 fi
->frame
= read_sp ();
332 /* Note if/where callee saved registers were saved. */
333 set_movm_offsets (fi
, movm_args
);
338 /* Now see if we set up a frame pointer via "mov sp,a3" */
343 /* The frame pointer is now valid. */
346 fi
->extra_info
->status
|= MY_FRAME_IN_FP
;
347 fi
->extra_info
->status
&= ~MY_FRAME_IN_SP
;
350 /* Quit now if we're beyond the stop point. */
353 /* Fix fi->frame if it's bogus at this point. */
354 fix_frame_pointer (fi
, 0);
356 /* Note if/where callee saved registers were saved. */
357 set_movm_offsets (fi
, movm_args
);
361 /* Get two more bytes so scanning can continue. */
362 status
= read_memory_nobpt (addr
, buf
, 2);
365 /* Fix fi->frame if it's bogus at this point. */
366 fix_frame_pointer (fi
, 0);
368 /* Note if/where callee saved registers were saved. */
369 set_movm_offsets (fi
, movm_args
);
374 /* Next we should allocate the local frame. No more prologue insns
375 are found after allocating the local frame.
377 Search for add imm8,sp (0xf8feXX)
378 or add imm16,sp (0xfafeXXXX)
379 or add imm32,sp (0xfcfeXXXXXXXX).
381 If none of the above was found, then this prologue has no
384 status
= read_memory_nobpt (addr
, buf
, 2);
387 /* Fix fi->frame if it's bogus at this point. */
388 fix_frame_pointer (fi
, 0);
390 /* Note if/where callee saved registers were saved. */
391 set_movm_offsets (fi
, movm_args
);
396 if (buf
[0] == 0xf8 && buf
[1] == 0xfe)
398 else if (buf
[0] == 0xfa && buf
[1] == 0xfe)
400 else if (buf
[0] == 0xfc && buf
[1] == 0xfe)
405 /* Suck in imm_size more bytes, they'll hold the size of the
407 status
= read_memory_nobpt (addr
+ 2, buf
, imm_size
);
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
);
418 /* Note the size of the stack in the frame info structure. */
419 stack_size
= extract_signed_integer (buf
, imm_size
);
421 fi
->extra_info
->stack_size
= stack_size
;
423 /* We just consumed 2 + imm_size bytes. */
424 addr
+= 2 + imm_size
;
426 /* No more prologue insns follow, so begin preparation to return. */
427 /* Fix fi->frame if it's bogus at this point. */
428 fix_frame_pointer (fi
, stack_size
);
430 /* Note if/where callee saved registers were saved. */
431 set_movm_offsets (fi
, movm_args
);
435 /* We never found an insn which allocates local stack space, regardless
436 this is the end of the prologue. */
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
);
445 /* Function: frame_chain
446 Figure out and return the caller's frame pointer given current
449 We don't handle dummy frames yet but we would probably just return the
450 stack pointer that was in use at the time the function call was made? */
453 mn10300_frame_chain (fi
)
454 struct frame_info
*fi
;
456 struct frame_info
*dummy
;
457 /* Walk through the prologue to determine the stack size,
458 location of saved registers, end of the prologue, etc. */
459 if (fi
->extra_info
->status
== 0)
460 mn10300_analyze_prologue (fi
, (CORE_ADDR
)0);
462 /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */
463 if (fi
->extra_info
->status
& NO_MORE_FRAMES
)
466 /* Now that we've analyzed our prologue, determine the frame
467 pointer for our caller.
469 If our caller has a frame pointer, then we need to
470 find the entry value of $a3 to our function.
472 If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
473 location pointed to by fsr.regs[A3_REGNUM].
475 Else it's still in $a3.
477 If our caller does not have a frame pointer, then his
478 frame base is fi->frame + -caller's stack size. */
480 /* The easiest way to get that info is to analyze our caller's frame.
481 So we set up a dummy frame and call mn10300_analyze_prologue to
482 find stuff for us. */
483 dummy
= analyze_dummy_frame (FRAME_SAVED_PC (fi
), fi
->frame
);
485 if (dummy
->extra_info
->status
& MY_FRAME_IN_FP
)
487 /* Our caller has a frame pointer. So find the frame in $a3 or
489 if (fi
->saved_regs
[A3_REGNUM
])
490 return (read_memory_integer (fi
->saved_regs
[A3_REGNUM
], REGISTER_SIZE
));
492 return read_register (A3_REGNUM
);
498 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
499 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
500 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
501 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 4 : 0);
503 /* Our caller does not have a frame pointer. So his frame starts
504 at the base of our frame (fi->frame) + register save space
506 return fi
->frame
+ adjust
+ -dummy
->extra_info
->stack_size
;
510 /* Function: skip_prologue
511 Return the address of the first inst past the prologue of the function. */
514 mn10300_skip_prologue (pc
)
517 /* We used to check the debug symbols, but that can lose if
518 we have a null prologue. */
519 return mn10300_analyze_prologue (NULL
, pc
);
523 /* Function: pop_frame
524 This routine gets called when either the user uses the `return'
525 command, or the call dummy breakpoint gets hit. */
528 mn10300_pop_frame (frame
)
529 struct frame_info
*frame
;
533 if (PC_IN_CALL_DUMMY(frame
->pc
, frame
->frame
, frame
->frame
))
534 generic_pop_dummy_frame ();
537 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
539 /* Restore any saved registers. */
540 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
541 if (frame
->saved_regs
[regnum
] != 0)
545 value
= read_memory_unsigned_integer (frame
->saved_regs
[regnum
],
546 REGISTER_RAW_SIZE (regnum
));
547 write_register (regnum
, value
);
550 /* Actually cut back the stack. */
551 write_register (SP_REGNUM
, FRAME_FP (frame
));
553 /* Don't we need to set the PC?!? XXX FIXME. */
556 /* Throw away any cached frame information. */
557 flush_cached_frames ();
560 /* Function: push_arguments
561 Setup arguments for a call to the target. Arguments go in
562 order on the stack. */
565 mn10300_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
569 unsigned char struct_return
;
570 CORE_ADDR struct_addr
;
574 int stack_offset
= 0;
575 int regsused
= struct_return
? 1 : 0;
577 /* This should be a nop, but align the stack just in case something
578 went wrong. Stacks are four byte aligned on the mn10300. */
581 /* Now make space on the stack for the args.
583 XXX This doesn't appear to handle pass-by-invisible reference
585 for (argnum
= 0; argnum
< nargs
; argnum
++)
587 int arg_length
= (TYPE_LENGTH (VALUE_TYPE (args
[argnum
])) + 3) & ~3;
589 while (regsused
< 2 && arg_length
> 0)
597 /* Allocate stack space. */
600 regsused
= struct_return
? 1 : 0;
601 /* Push all arguments onto the stack. */
602 for (argnum
= 0; argnum
< nargs
; argnum
++)
607 /* XXX Check this. What about UNIONS? */
608 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
609 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
611 /* XXX Wrong, we want a pointer to this argument. */
612 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
613 val
= (char *)VALUE_CONTENTS (*args
);
617 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
618 val
= (char *)VALUE_CONTENTS (*args
);
621 while (regsused
< 2 && len
> 0)
623 write_register (regsused
, extract_unsigned_integer (val
, 4));
631 write_memory (sp
+ stack_offset
, val
, 4);
640 /* Make space for the flushback area. */
645 /* Function: push_return_address (pc)
646 Set up the return address for the inferior function call.
647 Needed for targets where we don't actually execute a JSR/BSR instruction */
650 mn10300_push_return_address (pc
, sp
)
654 unsigned char buf
[4];
656 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
657 write_memory (sp
- 4, buf
, 4);
661 /* Function: store_struct_return (addr,sp)
662 Store the structure value return address for an inferior function
666 mn10300_store_struct_return (addr
, sp
)
670 /* The structure return address is passed as the first argument. */
671 write_register (0, addr
);
675 /* Function: frame_saved_pc
676 Find the caller of this frame. We do this by seeing if RP_REGNUM
677 is saved in the stack anywhere, otherwise we get it from the
678 registers. If the inner frame is a dummy frame, return its PC
679 instead of RP, because that's where "caller" of the dummy-frame
683 mn10300_frame_saved_pc (fi
)
684 struct frame_info
*fi
;
688 adjust
+= (fi
->saved_regs
[D2_REGNUM
] ? 4 : 0);
689 adjust
+= (fi
->saved_regs
[D3_REGNUM
] ? 4 : 0);
690 adjust
+= (fi
->saved_regs
[A2_REGNUM
] ? 4 : 0);
691 adjust
+= (fi
->saved_regs
[A3_REGNUM
] ? 4 : 0);
693 return (read_memory_integer (fi
->frame
+ adjust
, REGISTER_SIZE
));
697 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
701 struct frame_info
*frame
;
703 enum lval_type
*lval
;
705 generic_get_saved_register (raw_buffer
, optimized
, addrp
,
706 frame
, regnum
, lval
);
709 /* Function: mn10300_init_extra_frame_info
710 Setup the frame's frame pointer, pc, and frame addresses for saved
711 registers. Most of the work is done in mn10300_analyze_prologue().
713 Note that when we are called for the last frame (currently active frame),
714 that fi->pc and fi->frame will already be setup. However, fi->frame will
715 be valid only if this routine uses FP. For previous frames, fi-frame will
716 always be correct. mn10300_analyze_prologue will fix fi->frame if
719 We can be called with the PC in the call dummy under two circumstances.
720 First, during normal backtracing, second, while figuring out the frame
721 pointer just prior to calling the target function (see run_stack_dummy). */
724 mn10300_init_extra_frame_info (fi
)
725 struct frame_info
*fi
;
728 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
730 frame_saved_regs_zalloc (fi
);
731 fi
->extra_info
= (struct frame_extra_info
*)
732 frame_obstack_alloc (sizeof (struct frame_extra_info
));
734 fi
->extra_info
->status
= 0;
735 fi
->extra_info
->stack_size
= 0;
737 mn10300_analyze_prologue (fi
, 0);
740 /* Function: mn10300_virtual_frame_pointer
741 Return the register that the function uses for a frame pointer,
742 plus any necessary offset to be applied to the register before
743 any frame pointer offsets. */
746 mn10300_virtual_frame_pointer (pc
, reg
, offset
)
751 struct frame_info
*dummy
= analyze_dummy_frame (pc
, 0);
752 /* Set up a dummy frame_info, Analyze the prolog and fill in the
754 /* Results will tell us which type of frame it uses. */
755 if (dummy
->extra_info
->status
& MY_FRAME_IN_SP
)
758 *offset
= -(dummy
->extra_info
->stack_size
);
767 /* This can be made more generic later. */
769 set_machine_hook (filename
)
774 if (bfd_get_mach (exec_bfd
) == bfd_mach_mn10300
775 || bfd_get_mach (exec_bfd
) == 0)
777 mn10300_register_names
= mn10300_generic_register_names
;
783 _initialize_mn10300_tdep ()
785 /* printf("_initialize_mn10300_tdep\n"); */
787 tm_print_insn
= print_insn_mn10300
;
789 specify_exec_file_hook (set_machine_hook
);