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 char *mn10300_generic_register_names
[] = REGISTER_NAMES
;
33 /* start-sanitize-am33 */
34 char *am33_register_names
[] =
35 { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
36 "sp", "pc", "mdr", "psw", "lir", "lar", "",
37 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
38 "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""};
40 /* end-sanitize-am33 */
42 static CORE_ADDR mn10300_analyze_prologue
PARAMS ((struct frame_info
*fi
,
45 /* Values for frame_info.status */
47 #define MY_FRAME_IN_SP 0x1
48 #define MY_FRAME_IN_FP 0x2
49 #define NO_MORE_FRAMES 0x4
52 /* Should call_function allocate stack space for a struct return? */
54 mn10300_use_struct_convention (gcc_p
, type
)
58 return (TYPE_NFIELDS (type
) > 1 || TYPE_LENGTH (type
) > 8);
61 /* The breakpoint instruction must be the same size as the smallest
62 instruction in the instruction set.
64 The Matsushita mn10x00 processors have single byte instructions
65 so we need a single byte breakpoint. Matsushita hasn't defined
66 one, so we defined it ourselves. */
69 mn10300_breakpoint_from_pc (bp_addr
, bp_size
)
73 static char breakpoint
[] = {0xff};
79 /* Fix fi->frame if it's bogus at this point. This is a helper
80 function for mn10300_analyze_prologue. */
83 fix_frame_pointer (fi
, stack_size
)
84 struct frame_info
*fi
;
87 if (fi
&& fi
->next
== NULL
)
89 if (fi
->status
& MY_FRAME_IN_SP
)
90 fi
->frame
= read_sp () - stack_size
;
91 else if (fi
->status
& MY_FRAME_IN_FP
)
92 fi
->frame
= read_register (A3_REGNUM
);
97 /* Set offsets of registers saved by movm instruction.
98 This is a helper function for mn10300_analyze_prologue. */
101 set_movm_offsets (fi
, movm_args
)
102 struct frame_info
*fi
;
107 if (fi
== NULL
|| movm_args
== 0)
110 if (movm_args
& 0x10)
112 fi
->fsr
.regs
[A3_REGNUM
] = fi
->frame
+ offset
;
115 if (movm_args
& 0x20)
117 fi
->fsr
.regs
[A2_REGNUM
] = fi
->frame
+ offset
;
120 if (movm_args
& 0x40)
122 fi
->fsr
.regs
[D3_REGNUM
] = fi
->frame
+ offset
;
125 if (movm_args
& 0x80)
127 fi
->fsr
.regs
[D2_REGNUM
] = fi
->frame
+ offset
;
130 /* start-sanitize-am33 */
131 if (am33_mode
&& movm_args
& 0x02)
133 fi
->fsr
.regs
[E0_REGNUM
+5] = fi
->frame
+ offset
;
134 fi
->fsr
.regs
[E0_REGNUM
+4] = fi
->frame
+ offset
+ 4;
135 fi
->fsr
.regs
[E0_REGNUM
+3] = fi
->frame
+ offset
+ 8;
136 fi
->fsr
.regs
[E0_REGNUM
+2] = fi
->frame
+ offset
+ 12;
138 /* end-sanitize-am33 */
142 /* The main purpose of this file is dealing with prologues to extract
143 information about stack frames and saved registers.
145 For reference here's how prologues look on the mn10300:
148 movm [d2,d3,a2,a3],sp
152 Without frame pointer:
153 movm [d2,d3,a2,a3],sp (if needed)
156 One day we might keep the stack pointer constant, that won't
157 change the code for prologues, but it will make the frame
158 pointerless case much more common. */
160 /* Analyze the prologue to determine where registers are saved,
161 the end of the prologue, etc etc. Return the end of the prologue
164 We store into FI (if non-null) several tidbits of information:
166 * stack_size -- size of this stack frame. Note that if we stop in
167 certain parts of the prologue/epilogue we may claim the size of the
168 current frame is zero. This happens when the current frame has
169 not been allocated yet or has already been deallocated.
171 * fsr -- Addresses of registers saved in the stack by this frame.
173 * status -- A (relatively) generic status indicator. It's a bitmask
174 with the following bits:
176 MY_FRAME_IN_SP: The base of the current frame is actually in
177 the stack pointer. This can happen for frame pointerless
178 functions, or cases where we're stopped in the prologue/epilogue
179 itself. For these cases mn10300_analyze_prologue will need up
180 update fi->frame before returning or analyzing the register
183 MY_FRAME_IN_FP: The base of the current frame is in the
184 frame pointer register ($a2).
186 NO_MORE_FRAMES: Set this if the current frame is "start" or
187 if the first instruction looks like mov <imm>,sp. This tells
188 frame chain to not bother trying to unwind past this frame. */
191 mn10300_analyze_prologue (fi
, pc
)
192 struct frame_info
*fi
;
195 CORE_ADDR func_addr
, func_end
, addr
, stop
;
196 CORE_ADDR stack_size
;
198 unsigned char buf
[4];
199 int status
, movm_args
= 0;
202 /* Use the PC in the frame if it's provided to look up the
203 start of this function. */
204 pc
= (fi
? fi
->pc
: pc
);
206 /* Find the start of this function. */
207 status
= find_pc_partial_function (pc
, &name
, &func_addr
, &func_end
);
209 /* Do nothing if we couldn't find the start of this function or if we're
210 stopped at the first instruction in the prologue. */
214 /* If we're in start, then give up. */
215 if (strcmp (name
, "start") == 0)
218 fi
->status
= NO_MORE_FRAMES
;
222 /* At the start of a function our frame is in the stack pointer. */
224 fi
->status
= MY_FRAME_IN_SP
;
226 /* Get the next two bytes into buf, we need two because rets is a two
227 byte insn and the first isn't enough to uniquely identify it. */
228 status
= read_memory_nobpt (pc
, buf
, 2);
232 /* If we're physically on an "rets" instruction, then our frame has
233 already been deallocated. Note this can also be true for retf
234 and ret if they specify a size of zero.
236 In this case fi->frame is bogus, we need to fix it. */
237 if (fi
&& buf
[0] == 0xf0 && buf
[1] == 0xfc)
239 if (fi
->next
== NULL
)
240 fi
->frame
= read_sp ();
244 /* Similarly if we're stopped on the first insn of a prologue as our
245 frame hasn't been allocated yet. */
246 if (fi
&& fi
->pc
== func_addr
)
248 if (fi
->next
== NULL
)
249 fi
->frame
= read_sp ();
253 /* Figure out where to stop scanning. */
254 stop
= fi
? fi
->pc
: func_end
;
256 /* Don't walk off the end of the function. */
257 stop
= stop
> func_end
? func_end
: stop
;
259 /* Start scanning on the first instruction of this function. */
262 /* Suck in two bytes. */
263 status
= read_memory_nobpt (addr
, buf
, 2);
266 fix_frame_pointer (fi
, 0);
270 /* First see if this insn sets the stack pointer; if so, it's something
271 we won't understand, so quit now. */
272 if (buf
[0] == 0xf2 && (buf
[1] & 0xf3) == 0xf0)
275 fi
->status
= NO_MORE_FRAMES
;
279 /* Now look for movm [regs],sp, which saves the callee saved registers.
281 At this time we don't know if fi->frame is valid, so we only note
282 that we encountered a movm instruction. Later, we'll set the entries
283 in fsr.regs as needed. */
286 /* Extract the register list for the movm instruction. */
287 status
= read_memory_nobpt (addr
+ 1, buf
, 1);
292 /* Quit now if we're beyond the stop point. */
295 /* Fix fi->frame since it's bogus at this point. */
296 if (fi
&& fi
->next
== NULL
)
297 fi
->frame
= read_sp ();
299 /* Note if/where callee saved registers were saved. */
300 set_movm_offsets (fi
, movm_args
);
304 /* Get the next two bytes so the prologue scan can continue. */
305 status
= read_memory_nobpt (addr
, buf
, 2);
308 /* Fix fi->frame since it's bogus at this point. */
309 if (fi
&& fi
->next
== NULL
)
310 fi
->frame
= read_sp ();
312 /* Note if/where callee saved registers were saved. */
313 set_movm_offsets (fi
, movm_args
);
318 /* Now see if we set up a frame pointer via "mov sp,a3" */
323 /* The frame pointer is now valid. */
326 fi
->status
|= MY_FRAME_IN_FP
;
327 fi
->status
&= ~MY_FRAME_IN_SP
;
330 /* Quit now if we're beyond the stop point. */
333 /* Fix fi->frame if it's bogus at this point. */
334 fix_frame_pointer (fi
, 0);
336 /* Note if/where callee saved registers were saved. */
337 set_movm_offsets (fi
, movm_args
);
341 /* Get two more bytes so scanning can continue. */
342 status
= read_memory_nobpt (addr
, buf
, 2);
345 /* Fix fi->frame if it's bogus at this point. */
346 fix_frame_pointer (fi
, 0);
348 /* Note if/where callee saved registers were saved. */
349 set_movm_offsets (fi
, movm_args
);
354 /* Next we should allocate the local frame. No more prologue insns
355 are found after allocating the local frame.
357 Search for add imm8,sp (0xf8feXX)
358 or add imm16,sp (0xfafeXXXX)
359 or add imm32,sp (0xfcfeXXXXXXXX).
361 If none of the above was found, then this prologue has no
364 status
= read_memory_nobpt (addr
, buf
, 2);
367 /* Fix fi->frame if it's bogus at this point. */
368 fix_frame_pointer (fi
, 0);
370 /* Note if/where callee saved registers were saved. */
371 set_movm_offsets (fi
, movm_args
);
376 if (buf
[0] == 0xf8 && buf
[1] == 0xfe)
378 else if (buf
[0] == 0xfa && buf
[1] == 0xfe)
380 else if (buf
[0] == 0xfc && buf
[1] == 0xfe)
385 /* Suck in imm_size more bytes, they'll hold the size of the
387 status
= read_memory_nobpt (addr
+ 2, buf
, imm_size
);
390 /* Fix fi->frame if it's bogus at this point. */
391 fix_frame_pointer (fi
, 0);
393 /* Note if/where callee saved registers were saved. */
394 set_movm_offsets (fi
, movm_args
);
398 /* Note the size of the stack in the frame info structure. */
399 stack_size
= extract_signed_integer (buf
, imm_size
);
401 fi
->stack_size
= stack_size
;
403 /* We just consumed 2 + imm_size bytes. */
404 addr
+= 2 + imm_size
;
406 /* No more prologue insns follow, so begin preparation to return. */
407 /* Fix fi->frame if it's bogus at this point. */
408 fix_frame_pointer (fi
, stack_size
);
410 /* Note if/where callee saved registers were saved. */
411 set_movm_offsets (fi
, movm_args
);
415 /* We never found an insn which allocates local stack space, regardless
416 this is the end of the prologue. */
417 /* Fix fi->frame if it's bogus at this point. */
418 fix_frame_pointer (fi
, 0);
420 /* Note if/where callee saved registers were saved. */
421 set_movm_offsets (fi
, movm_args
);
425 /* Function: frame_chain
426 Figure out and return the caller's frame pointer given current
429 We don't handle dummy frames yet but we would probably just return the
430 stack pointer that was in use at the time the function call was made? */
433 mn10300_frame_chain (fi
)
434 struct frame_info
*fi
;
436 struct frame_info dummy_frame
;
438 /* Walk through the prologue to determine the stack size,
439 location of saved registers, end of the prologue, etc. */
441 mn10300_analyze_prologue (fi
, (CORE_ADDR
)0);
443 /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */
444 if (fi
->status
& NO_MORE_FRAMES
)
447 /* Now that we've analyzed our prologue, determine the frame
448 pointer for our caller.
450 If our caller has a frame pointer, then we need to
451 find the entry value of $a3 to our function.
453 If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
454 location pointed to by fsr.regs[A3_REGNUM].
456 Else it's still in $a3.
458 If our caller does not have a frame pointer, then his
459 frame base is fi->frame + -caller's stack size. */
461 /* The easiest way to get that info is to analyze our caller's frame.
463 So we set up a dummy frame and call mn10300_analyze_prologue to
464 find stuff for us. */
465 dummy_frame
.pc
= FRAME_SAVED_PC (fi
);
466 dummy_frame
.frame
= fi
->frame
;
467 memset (dummy_frame
.fsr
.regs
, '\000', sizeof dummy_frame
.fsr
.regs
);
468 dummy_frame
.status
= 0;
469 dummy_frame
.stack_size
= 0;
470 mn10300_analyze_prologue (&dummy_frame
, 0);
472 if (dummy_frame
.status
& MY_FRAME_IN_FP
)
474 /* Our caller has a frame pointer. So find the frame in $a3 or
476 if (fi
->fsr
.regs
[A3_REGNUM
])
477 return (read_memory_integer (fi
->fsr
.regs
[A3_REGNUM
], REGISTER_SIZE
));
479 return read_register (A3_REGNUM
);
485 adjust
+= (fi
->fsr
.regs
[D2_REGNUM
] ? 4 : 0);
486 adjust
+= (fi
->fsr
.regs
[D3_REGNUM
] ? 4 : 0);
487 adjust
+= (fi
->fsr
.regs
[A2_REGNUM
] ? 4 : 0);
488 adjust
+= (fi
->fsr
.regs
[A3_REGNUM
] ? 4 : 0);
489 /* start-sanitize-am33 */
492 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+5] ? 4 : 0);
493 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+4] ? 4 : 0);
494 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+3] ? 4 : 0);
495 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+2] ? 4 : 0);
497 /* end-sanitize-am33 */
499 /* Our caller does not have a frame pointer. So his frame starts
500 at the base of our frame (fi->frame) + register save space
502 return fi
->frame
+ adjust
+ -dummy_frame
.stack_size
;
506 /* Function: skip_prologue
507 Return the address of the first inst past the prologue of the function. */
510 mn10300_skip_prologue (pc
)
513 /* We used to check the debug symbols, but that can lose if
514 we have a null prologue. */
515 return mn10300_analyze_prologue (NULL
, pc
);
519 /* Function: pop_frame
520 This routine gets called when either the user uses the `return'
521 command, or the call dummy breakpoint gets hit. */
524 mn10300_pop_frame (frame
)
525 struct frame_info
*frame
;
529 if (PC_IN_CALL_DUMMY(frame
->pc
, frame
->frame
, frame
->frame
))
530 generic_pop_dummy_frame ();
533 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
535 /* Restore any saved registers. */
536 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
537 if (frame
->fsr
.regs
[regnum
] != 0)
541 value
= read_memory_unsigned_integer (frame
->fsr
.regs
[regnum
],
542 REGISTER_RAW_SIZE (regnum
));
543 write_register (regnum
, value
);
546 /* Actually cut back the stack. */
547 write_register (SP_REGNUM
, FRAME_FP (frame
));
549 /* Don't we need to set the PC?!? XXX FIXME. */
552 /* Throw away any cached frame information. */
553 flush_cached_frames ();
556 /* Function: push_arguments
557 Setup arguments for a call to the target. Arguments go in
558 order on the stack. */
561 mn10300_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
565 unsigned char struct_return
;
566 CORE_ADDR struct_addr
;
570 int stack_offset
= 0;
571 int regsused
= struct_return
? 1 : 0;
573 /* This should be a nop, but align the stack just in case something
574 went wrong. Stacks are four byte aligned on the mn10300. */
577 /* Now make space on the stack for the args.
579 XXX This doesn't appear to handle pass-by-invisible reference
581 for (argnum
= 0; argnum
< nargs
; argnum
++)
583 int arg_length
= (TYPE_LENGTH (VALUE_TYPE (args
[argnum
])) + 3) & ~3;
585 while (regsused
< 2 && arg_length
> 0)
593 /* Allocate stack space. */
596 regsused
= struct_return
? 1 : 0;
597 /* Push all arguments onto the stack. */
598 for (argnum
= 0; argnum
< nargs
; argnum
++)
603 /* XXX Check this. What about UNIONS? */
604 if (TYPE_CODE (VALUE_TYPE (*args
)) == TYPE_CODE_STRUCT
605 && TYPE_LENGTH (VALUE_TYPE (*args
)) > 8)
607 /* XXX Wrong, we want a pointer to this argument. */
608 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
609 val
= (char *)VALUE_CONTENTS (*args
);
613 len
= TYPE_LENGTH (VALUE_TYPE (*args
));
614 val
= (char *)VALUE_CONTENTS (*args
);
617 while (regsused
< 2 && len
> 0)
619 write_register (regsused
, extract_unsigned_integer (val
, 4));
627 write_memory (sp
+ stack_offset
, val
, 4);
636 /* Make space for the flushback area. */
641 /* Function: push_return_address (pc)
642 Set up the return address for the inferior function call.
643 Needed for targets where we don't actually execute a JSR/BSR instruction */
646 mn10300_push_return_address (pc
, sp
)
650 unsigned char buf
[4];
652 store_unsigned_integer (buf
, 4, CALL_DUMMY_ADDRESS ());
653 write_memory (sp
- 4, buf
, 4);
657 /* Function: store_struct_return (addr,sp)
658 Store the structure value return address for an inferior function
662 mn10300_store_struct_return (addr
, sp
)
666 /* The structure return address is passed as the first argument. */
667 write_register (0, addr
);
671 /* Function: frame_saved_pc
672 Find the caller of this frame. We do this by seeing if RP_REGNUM
673 is saved in the stack anywhere, otherwise we get it from the
674 registers. If the inner frame is a dummy frame, return its PC
675 instead of RP, because that's where "caller" of the dummy-frame
679 mn10300_frame_saved_pc (fi
)
680 struct frame_info
*fi
;
684 adjust
+= (fi
->fsr
.regs
[D2_REGNUM
] ? 4 : 0);
685 adjust
+= (fi
->fsr
.regs
[D3_REGNUM
] ? 4 : 0);
686 adjust
+= (fi
->fsr
.regs
[A2_REGNUM
] ? 4 : 0);
687 adjust
+= (fi
->fsr
.regs
[A3_REGNUM
] ? 4 : 0);
688 /* start-sanitize-am33 */
691 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+5] ? 4 : 0);
692 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+4] ? 4 : 0);
693 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+3] ? 4 : 0);
694 adjust
+= (fi
->fsr
.regs
[E0_REGNUM
+2] ? 4 : 0);
696 /* end-sanitize-am33 */
698 return (read_memory_integer (fi
->frame
+ adjust
, REGISTER_SIZE
));
702 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
706 struct frame_info
*frame
;
708 enum lval_type
*lval
;
710 generic_get_saved_register (raw_buffer
, optimized
, addrp
,
711 frame
, regnum
, lval
);
714 /* Function: init_extra_frame_info
715 Setup the frame's frame pointer, pc, and frame addresses for saved
716 registers. Most of the work is done in mn10300_analyze_prologue().
718 Note that when we are called for the last frame (currently active frame),
719 that fi->pc and fi->frame will already be setup. However, fi->frame will
720 be valid only if this routine uses FP. For previous frames, fi-frame will
721 always be correct. mn10300_analyze_prologue will fix fi->frame if
724 We can be called with the PC in the call dummy under two circumstances.
725 First, during normal backtracing, second, while figuring out the frame
726 pointer just prior to calling the target function (see run_stack_dummy). */
729 mn10300_init_extra_frame_info (fi
)
730 struct frame_info
*fi
;
733 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
735 memset (fi
->fsr
.regs
, '\000', sizeof fi
->fsr
.regs
);
739 mn10300_analyze_prologue (fi
, 0);
742 /* This can be made more generic later. */
744 set_machine_hook (filename
)
749 if (bfd_get_mach (exec_bfd
) == bfd_mach_mn10300
750 || bfd_get_mach (exec_bfd
) == 0)
752 for (i
= 0; i
< NUM_REGS
; i
++)
753 reg_names
[i
] = mn10300_generic_register_names
[i
];
756 /* start-sanitize-am33 */
758 if (bfd_get_mach (exec_bfd
) == bfd_mach_am33
)
760 for (i
= 0; i
< NUM_REGS
; i
++)
761 reg_names
[i
] = am33_register_names
[i
];
764 /* end-sanitize-am33 */
768 _initialize_mn10300_tdep ()
770 /* printf("_initialize_mn10300_tdep\n"); */
772 tm_print_insn
= print_insn_mn10300
;
774 specify_exec_file_hook (set_machine_hook
);