1 /* Target-machine dependent code for Motorola MCore for GDB, the GNU debugger
2 Copyright 1999, 2000, 2001 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. */
29 #include "arch-utils.h"
30 #include "gdb_string.h"
32 /* Functions declared and used only in this file */
34 static CORE_ADDR mcore_analyze_prologue (struct frame_info *fi, CORE_ADDR pc, int skip_prologue);
36 static struct frame_info *analyze_dummy_frame (CORE_ADDR pc, CORE_ADDR frame);
38 static int get_insn (CORE_ADDR pc);
40 /* Functions exported from this file */
42 int mcore_use_struct_convention (int gcc_p, struct type *type);
44 void _initialize_mcore (void);
46 void mcore_init_extra_frame_info (int fromleaf, struct frame_info *fi);
48 CORE_ADDR mcore_frame_saved_pc (struct frame_info *fi);
50 CORE_ADDR mcore_find_callers_reg (struct frame_info *fi, int regnum);
52 CORE_ADDR mcore_frame_args_address (struct frame_info *fi);
54 CORE_ADDR mcore_frame_locals_address (struct frame_info *fi);
56 CORE_ADDR mcore_push_return_address (CORE_ADDR pc, CORE_ADDR sp);
58 CORE_ADDR mcore_push_arguments (int nargs, struct value ** args, CORE_ADDR sp,
59 int struct_return, CORE_ADDR struct_addr);
61 void mcore_pop_frame ();
63 CORE_ADDR mcore_skip_prologue (CORE_ADDR pc);
65 CORE_ADDR mcore_frame_chain (struct frame_info *fi);
67 const unsigned char *mcore_breakpoint_from_pc (CORE_ADDR * bp_addr, int *bp_size);
69 int mcore_use_struct_convention (int gcc_p, struct type *type);
71 void mcore_store_return_value (struct type *type, char *valbuf);
73 CORE_ADDR mcore_extract_struct_value_address (char *regbuf);
75 void mcore_extract_return_value (struct type *type, char *regbuf, char *valbuf);
82 /* All registers are 4 bytes long. */
83 #define MCORE_REG_SIZE 4
84 #define MCORE_NUM_REGS 65
86 /* Some useful register numbers. */
88 #define FIRST_ARGREG 2
90 #define RETVAL_REGNUM 2
93 /* Additional info that we use for managing frames */
94 struct frame_extra_info
96 /* A generic status word */
99 /* Size of this frame */
102 /* The register that is acting as a frame pointer, if
103 it is being used. This is undefined if status
104 does not contain the flag MY_FRAME_IN_FP. */
108 /* frame_extra_info status flags */
110 /* The base of the current frame is actually in the stack pointer.
111 This happens when there is no frame pointer (MCore ABI does not
112 require a frame pointer) or when we're stopped in the prologue or
113 epilogue itself. In these cases, mcore_analyze_prologue will need
114 to update fi->frame before returning or analyzing the register
115 save instructions. */
116 #define MY_FRAME_IN_SP 0x1
118 /* The base of the current frame is in a frame pointer register.
119 This register is noted in frame_extra_info->fp_regnum.
121 Note that the existence of an FP might also indicate that the
122 function has called alloca. */
123 #define MY_FRAME_IN_FP 0x2
125 /* This flag is set to indicate that this frame is the top-most
126 frame. This tells frame chain not to bother trying to unwind
127 beyond this frame. */
128 #define NO_MORE_FRAMES 0x4
130 /* Instruction macros used for analyzing the prologue */
131 #define IS_SUBI0(x) (((x) & 0xfe0f) == 0x2400) /* subi r0,oimm5 */
132 #define IS_STM(x) (((x) & 0xfff0) == 0x0070) /* stm rf-r15,r0 */
133 #define IS_STWx0(x) (((x) & 0xf00f) == 0x9000) /* stw rz,(r0,disp) */
134 #define IS_STWxy(x) (((x) & 0xf000) == 0x9000) /* stw rx,(ry,disp) */
135 #define IS_MOVx0(x) (((x) & 0xfff0) == 0x1200) /* mov rn,r0 */
136 #define IS_LRW1(x) (((x) & 0xff00) == 0x7100) /* lrw r1,literal */
137 #define IS_MOVI1(x) (((x) & 0xf80f) == 0x6001) /* movi r1,imm7 */
138 #define IS_BGENI1(x) (((x) & 0xfe0f) == 0x3201) /* bgeni r1,imm5 */
139 #define IS_BMASKI1(x) (((x) & 0xfe0f) == 0x2C01) /* bmaski r1,imm5 */
140 #define IS_ADDI1(x) (((x) & 0xfe0f) == 0x2001) /* addi r1,oimm5 */
141 #define IS_SUBI1(x) (((x) & 0xfe0f) == 0x2401) /* subi r1,oimm5 */
142 #define IS_RSUBI1(x) (((x) & 0xfe0f) == 0x2801) /* rsubi r1,imm5 */
143 #define IS_NOT1(x) (((x) & 0xffff) == 0x01f1) /* not r1 */
144 #define IS_ROTLI1(x) (((x) & 0xfe0f) == 0x3801) /* rotli r1,imm5 */
145 #define IS_BSETI1(x) (((x) & 0xfe0f) == 0x3401) /* bseti r1,imm5 */
146 #define IS_BCLRI1(x) (((x) & 0xfe0f) == 0x3001) /* bclri r1,imm5 */
147 #define IS_IXH1(x) (((x) & 0xffff) == 0x1d11) /* ixh r1,r1 */
148 #define IS_IXW1(x) (((x) & 0xffff) == 0x1511) /* ixw r1,r1 */
149 #define IS_SUB01(x) (((x) & 0xffff) == 0x0510) /* subu r0,r1 */
150 #define IS_RTS(x) (((x) & 0xffff) == 0x00cf) /* jmp r15 */
152 #define IS_R1_ADJUSTER(x) \
153 (IS_ADDI1(x) || IS_SUBI1(x) || IS_ROTLI1(x) || IS_BSETI1(x) \
154 || IS_BCLRI1(x) || IS_RSUBI1(x) || IS_NOT1(x) \
155 || IS_IXH1(x) || IS_IXW1(x))
160 mcore_dump_insn (char *commnt, CORE_ADDR pc, int insn)
164 printf_filtered ("MCORE: %s %08x %08x ",
165 commnt, (unsigned int) pc, (unsigned int) insn);
166 TARGET_PRINT_INSN (pc, &tm_print_insn_info);
167 printf_filtered ("\n");
170 #define mcore_insn_debug(args) { if (mcore_debug) printf_filtered args; }
171 #else /* !MCORE_DEBUG */
172 #define mcore_dump_insn(a,b,c) {}
173 #define mcore_insn_debug(args) {}
178 mcore_register_virtual_type (int regnum)
180 if (regnum < 0 || regnum >= MCORE_NUM_REGS)
181 internal_error (__FILE__, __LINE__,
182 "mcore_register_virtual_type: illegal register number %d",
185 return builtin_type_int;
189 mcore_register_byte (int regnum)
191 if (regnum < 0 || regnum >= MCORE_NUM_REGS)
192 internal_error (__FILE__, __LINE__,
193 "mcore_register_byte: illegal register number %d",
196 return (regnum * MCORE_REG_SIZE);
200 mcore_register_size (int regnum)
203 if (regnum < 0 || regnum >= MCORE_NUM_REGS)
204 internal_error (__FILE__, __LINE__,
205 "mcore_register_size: illegal register number %d",
208 return MCORE_REG_SIZE;
211 /* The registers of the Motorola MCore processors */
214 mcore_register_name (int regnum)
217 static char *register_names[] = {
218 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
219 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
220 "ar0", "ar1", "ar2", "ar3", "ar4", "ar5", "ar6", "ar7",
221 "ar8", "ar9", "ar10", "ar11", "ar12", "ar13", "ar14", "ar15",
222 "psr", "vbr", "epsr", "fpsr", "epc", "fpc", "ss0", "ss1",
223 "ss2", "ss3", "ss4", "gcr", "gsr", "cr13", "cr14", "cr15",
224 "cr16", "cr17", "cr18", "cr19", "cr20", "cr21", "cr22", "cr23",
225 "cr24", "cr25", "cr26", "cr27", "cr28", "cr29", "cr30", "cr31",
230 regnum >= sizeof (register_names) / sizeof (register_names[0]))
231 internal_error (__FILE__, __LINE__,
232 "mcore_register_name: illegal register number %d",
235 return register_names[regnum];
238 /* Given the address at which to insert a breakpoint (BP_ADDR),
239 what will that breakpoint be?
241 For MCore, we have a breakpoint instruction. Since all MCore
242 instructions are 16 bits, this is all we need, regardless of
243 address. bpkt = 0x0000 */
245 const unsigned char *
246 mcore_breakpoint_from_pc (CORE_ADDR * bp_addr, int *bp_size)
248 static char breakpoint[] =
255 mcore_saved_pc_after_call (struct frame_info *frame)
257 return read_register (PR_REGNUM);
260 /* This is currently handled by init_extra_frame_info. */
262 mcore_frame_init_saved_regs (struct frame_info *frame)
267 /* This is currently handled by mcore_push_arguments */
269 mcore_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
275 mcore_reg_struct_has_addr (int gcc_p, struct type *type)
281 /* Helper function for several routines below. This funtion simply
282 sets up a fake, aka dummy, frame (not a _call_ dummy frame) that
283 we can analyze with mcore_analyze_prologue. */
285 static struct frame_info *
286 analyze_dummy_frame (CORE_ADDR pc, CORE_ADDR frame)
288 static struct frame_info *dummy = NULL;
292 dummy = (struct frame_info *) xmalloc (sizeof (struct frame_info));
293 dummy->saved_regs = (CORE_ADDR *) xmalloc (SIZEOF_FRAME_SAVED_REGS);
295 (struct frame_extra_info *) xmalloc (sizeof (struct frame_extra_info));
301 dummy->frame = frame;
302 dummy->extra_info->status = 0;
303 dummy->extra_info->framesize = 0;
304 memset (dummy->saved_regs, '\000', SIZEOF_FRAME_SAVED_REGS);
305 mcore_analyze_prologue (dummy, 0, 0);
309 /* Function prologues on the Motorola MCore processors consist of:
311 - adjustments to the stack pointer (r1 used as scratch register)
312 - store word/multiples that use r0 as the base address
313 - making a copy of r0 into another register (a "frame" pointer)
315 Note that the MCore really doesn't have a real frame pointer.
316 Instead, the compiler may copy the SP into a register (usually
317 r8) to act as an arg pointer. For our target-dependent purposes,
318 the frame info's "frame" member will be the beginning of the
319 frame. The SP could, in fact, point below this.
321 The prologue ends when an instruction fails to meet either of
322 the first two criteria or when an FP is made. We make a special
323 exception for gcc. When compiling unoptimized code, gcc will
324 setup stack slots. We need to make sure that we skip the filling
325 of these stack slots as much as possible. This is only done
326 when SKIP_PROLOGUE is set, so that it does not mess up
329 /* Analyze the prologue of frame FI to determine where registers are saved,
330 the end of the prologue, etc. Return the address of the first line
331 of "real" code (i.e., the end of the prologue). */
334 mcore_analyze_prologue (struct frame_info *fi, CORE_ADDR pc, int skip_prologue)
336 CORE_ADDR func_addr, func_end, addr, stop;
337 CORE_ADDR stack_size;
340 int fp_regnum = 0; /* dummy, valid when (flags & MY_FRAME_IN_FP) */
343 int register_offsets[NUM_REGS];
346 /* If provided, use the PC in the frame to look up the
347 start of this function. */
348 pc = (fi == NULL ? pc : fi->pc);
350 /* Find the start of this function. */
351 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
353 /* If the start of this function could not be found or if the debbuger
354 is stopped at the first instruction of the prologue, do nothing. */
358 /* If the debugger is entry function, give up. */
359 if (func_addr == entry_point_address ())
362 fi->extra_info->status |= NO_MORE_FRAMES;
366 /* At the start of a function, our frame is in the stack pointer. */
367 flags = MY_FRAME_IN_SP;
369 /* Start decoding the prologue. We start by checking two special cases:
371 1. We're about to return
372 2. We're at the first insn of the prologue.
374 If we're about to return, our frame has already been deallocated.
375 If we are stopped at the first instruction of a prologue,
376 then our frame has not yet been set up. */
378 /* Get the first insn from memory (all MCore instructions are 16 bits) */
379 mcore_insn_debug (("MCORE: starting prologue decoding\n"));
380 insn = get_insn (pc);
381 mcore_dump_insn ("got 1: ", pc, insn);
383 /* Check for return. */
384 if (fi != NULL && IS_RTS (insn))
386 mcore_insn_debug (("MCORE: got jmp r15"));
387 if (fi->next == NULL)
388 fi->frame = read_sp ();
392 /* Check for first insn of prologue */
393 if (fi != NULL && fi->pc == func_addr)
395 if (fi->next == NULL)
396 fi->frame = read_sp ();
400 /* Figure out where to stop scanning */
401 stop = (fi ? fi->pc : func_end);
403 /* Don't walk off the end of the function */
404 stop = (stop > func_end ? func_end : stop);
406 /* REGISTER_OFFSETS will contain offsets, from the top of the frame
407 (NOT the frame pointer), for the various saved registers or -1
408 if the register is not saved. */
409 for (rn = 0; rn < NUM_REGS; rn++)
410 register_offsets[rn] = -1;
412 /* Analyze the prologue. Things we determine from analyzing the
414 * the size of the frame
415 * where saved registers are located (and which are saved)
417 mcore_insn_debug (("MCORE: Scanning prologue: func_addr=0x%x, stop=0x%x\n",
418 (unsigned int) func_addr, (unsigned int) stop));
421 for (addr = func_addr; addr < stop; addr += 2)
424 insn = get_insn (addr);
425 mcore_dump_insn ("got 2: ", addr, insn);
429 int offset = 1 + ((insn >> 4) & 0x1f);
430 mcore_insn_debug (("MCORE: got subi r0,%d; continuing\n", offset));
434 else if (IS_STM (insn))
436 /* Spill register(s) */
440 /* BIG WARNING! The MCore ABI does not restrict functions
441 to taking only one stack allocation. Therefore, when
442 we save a register, we record the offset of where it was
443 saved relative to the current framesize. This will
444 then give an offset from the SP upon entry to our
445 function. Remember, framesize is NOT constant until
446 we're done scanning the prologue. */
447 start_register = (insn & 0xf);
448 mcore_insn_debug (("MCORE: got stm r%d-r15,(r0)\n", start_register));
450 for (rn = start_register, offset = 0; rn <= 15; rn++, offset += 4)
452 register_offsets[rn] = framesize - offset;
453 mcore_insn_debug (("MCORE: r%d saved at 0x%x (offset %d)\n", rn,
454 register_offsets[rn], offset));
456 mcore_insn_debug (("MCORE: continuing\n"));
459 else if (IS_STWx0 (insn))
461 /* Spill register: see note for IS_STM above. */
464 rn = (insn >> 8) & 0xf;
465 imm = (insn >> 4) & 0xf;
466 register_offsets[rn] = framesize - (imm << 2);
467 mcore_insn_debug (("MCORE: r%d saved at offset 0x%x\n", rn, register_offsets[rn]));
468 mcore_insn_debug (("MCORE: continuing\n"));
471 else if (IS_MOVx0 (insn))
473 /* We have a frame pointer, so this prologue is over. Note
474 the register which is acting as the frame pointer. */
475 flags |= MY_FRAME_IN_FP;
476 flags &= ~MY_FRAME_IN_SP;
477 fp_regnum = insn & 0xf;
478 mcore_insn_debug (("MCORE: Found a frame pointer: r%d\n", fp_regnum));
480 /* If we found an FP, we're at the end of the prologue. */
481 mcore_insn_debug (("MCORE: end of prologue\n"));
485 /* If we're decoding prologue, stop here. */
489 else if (IS_STWxy (insn) && (flags & MY_FRAME_IN_FP) && ((insn & 0xf) == fp_regnum))
491 /* Special case. Skip over stack slot allocs, too. */
492 mcore_insn_debug (("MCORE: push arg onto stack.\n"));
495 else if (IS_LRW1 (insn) || IS_MOVI1 (insn)
496 || IS_BGENI1 (insn) || IS_BMASKI1 (insn))
502 mcore_insn_debug (("MCORE: looking at large frame\n"));
506 read_memory_integer ((addr + 2 + ((insn & 0xff) << 2)) & 0xfffffffc, 4);
508 else if (IS_MOVI1 (insn))
509 adjust = (insn >> 4) & 0x7f;
510 else if (IS_BGENI1 (insn))
511 adjust = 1 << ((insn >> 4) & 0x1f);
512 else /* IS_BMASKI (insn) */
513 adjust = (1 << (adjust >> 4) & 0x1f) - 1;
515 mcore_insn_debug (("MCORE: base framesize=0x%x\n", adjust));
517 /* May have zero or more insns which modify r1 */
518 mcore_insn_debug (("MCORE: looking for r1 adjusters...\n"));
520 insn2 = get_insn (addr + offset);
521 while (IS_R1_ADJUSTER (insn2))
525 imm = (insn2 >> 4) & 0x1f;
526 mcore_dump_insn ("got 3: ", addr + offset, insn);
527 if (IS_ADDI1 (insn2))
530 mcore_insn_debug (("MCORE: addi r1,%d\n", imm + 1));
532 else if (IS_SUBI1 (insn2))
535 mcore_insn_debug (("MCORE: subi r1,%d\n", imm + 1));
537 else if (IS_RSUBI1 (insn2))
539 adjust = imm - adjust;
540 mcore_insn_debug (("MCORE: rsubi r1,%d\n", imm + 1));
542 else if (IS_NOT1 (insn2))
545 mcore_insn_debug (("MCORE: not r1\n"));
547 else if (IS_ROTLI1 (insn2))
550 mcore_insn_debug (("MCORE: rotli r1,%d\n", imm + 1));
552 else if (IS_BSETI1 (insn2))
554 adjust |= (1 << imm);
555 mcore_insn_debug (("MCORE: bseti r1,%d\n", imm));
557 else if (IS_BCLRI1 (insn2))
559 adjust &= ~(1 << imm);
560 mcore_insn_debug (("MCORE: bclri r1,%d\n", imm));
562 else if (IS_IXH1 (insn2))
565 mcore_insn_debug (("MCORE: ix.h r1,r1\n"));
567 else if (IS_IXW1 (insn2))
570 mcore_insn_debug (("MCORE: ix.w r1,r1\n"));
574 insn2 = get_insn (addr + offset);
577 mcore_insn_debug (("MCORE: done looking for r1 adjusters\n"));
579 /* If the next insn adjusts the stack pointer, we keep everything;
580 if not, we scrap it and we've found the end of the prologue. */
581 if (IS_SUB01 (insn2))
585 mcore_insn_debug (("MCORE: found stack adjustment of 0x%x bytes.\n", adjust));
586 mcore_insn_debug (("MCORE: skipping to new address 0x%x\n", addr));
587 mcore_insn_debug (("MCORE: continuing\n"));
591 /* None of these instructions are prologue, so don't touch
593 mcore_insn_debug (("MCORE: no subu r1,r0, NOT altering framesize.\n"));
597 /* This is not a prologue insn, so stop here. */
598 mcore_insn_debug (("MCORE: insn is not a prologue insn -- ending scan\n"));
602 mcore_insn_debug (("MCORE: done analyzing prologue\n"));
603 mcore_insn_debug (("MCORE: prologue end = 0x%x\n", addr));
605 /* Save everything we have learned about this frame into FI. */
608 fi->extra_info->framesize = framesize;
609 fi->extra_info->fp_regnum = fp_regnum;
610 fi->extra_info->status = flags;
612 /* Fix the frame pointer. When gcc uses r8 as a frame pointer,
613 it is really an arg ptr. We adjust fi->frame to be a "real"
615 if (fi->next == NULL)
617 if (fi->extra_info->status & MY_FRAME_IN_SP)
618 fi->frame = read_sp () + framesize;
620 fi->frame = read_register (fp_regnum) + framesize;
623 /* Note where saved registers are stored. The offsets in REGISTER_OFFSETS
624 are computed relative to the top of the frame. */
625 for (rn = 0; rn < NUM_REGS; rn++)
627 if (register_offsets[rn] >= 0)
629 fi->saved_regs[rn] = fi->frame - register_offsets[rn];
630 mcore_insn_debug (("Saved register %s stored at 0x%08x, value=0x%08x\n",
631 mcore_register_names[rn], fi->saved_regs[rn],
632 read_memory_integer (fi->saved_regs[rn], 4)));
637 /* Return addr of first non-prologue insn. */
641 /* Given a GDB frame, determine the address of the calling function's frame.
642 This will be used to create a new GDB frame struct, and then
643 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame. */
646 mcore_frame_chain (struct frame_info * fi)
648 struct frame_info *dummy;
649 CORE_ADDR callers_addr;
651 /* Analyze the prologue of this function. */
652 if (fi->extra_info->status == 0)
653 mcore_analyze_prologue (fi, 0, 0);
655 /* If mcore_analyze_prologue set NO_MORE_FRAMES, quit now. */
656 if (fi->extra_info->status & NO_MORE_FRAMES)
659 /* Now that we've analyzed our prologue, we can start to ask
660 for information about our caller. The easiest way to do
661 this is to analyze our caller's prologue.
663 If our caller has a frame pointer, then we need to find
664 the value of that register upon entry to our frame.
665 This value is either in fi->saved_regs[rn] if it's saved,
666 or it's still in a register.
668 If our caller does not have a frame pointer, then his frame base
669 is <our base> + -<caller's frame size>. */
670 dummy = analyze_dummy_frame (FRAME_SAVED_PC (fi), fi->frame);
672 if (dummy->extra_info->status & MY_FRAME_IN_FP)
674 int fp = dummy->extra_info->fp_regnum;
676 /* Our caller has a frame pointer. */
677 if (fi->saved_regs[fp] != 0)
679 /* The "FP" was saved on the stack. Don't forget to adjust
680 the "FP" with the framesize to get a real FP. */
681 callers_addr = read_memory_integer (fi->saved_regs[fp], REGISTER_SIZE)
682 + dummy->extra_info->framesize;
686 /* It's still in the register. Don't forget to adjust
687 the "FP" with the framesize to get a real FP. */
688 callers_addr = read_register (fp) + dummy->extra_info->framesize;
693 /* Our caller does not have a frame pointer. */
694 callers_addr = fi->frame + dummy->extra_info->framesize;
700 /* Skip the prologue of the function at PC. */
703 mcore_skip_prologue (CORE_ADDR pc)
705 CORE_ADDR func_addr, func_end;
706 struct symtab_and_line sal;
708 /* If we have line debugging information, then the end of the
709 prologue should be the first assembly instruction of the first
711 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
713 sal = find_pc_line (func_addr, 0);
714 if (sal.end && sal.end < func_end)
718 return mcore_analyze_prologue (NULL, pc, 1);
721 /* Return the address at which function arguments are offset. */
723 mcore_frame_args_address (struct frame_info * fi)
725 return fi->frame - fi->extra_info->framesize;
729 mcore_frame_locals_address (struct frame_info * fi)
731 return fi->frame - fi->extra_info->framesize;
734 /* Return the frame pointer in use at address PC. */
737 mcore_virtual_frame_pointer (CORE_ADDR pc, int *reg, LONGEST *offset)
739 struct frame_info *dummy = analyze_dummy_frame (pc, 0);
740 if (dummy->extra_info->status & MY_FRAME_IN_SP)
747 *reg = dummy->extra_info->fp_regnum;
752 /* Find the value of register REGNUM in frame FI. */
755 mcore_find_callers_reg (struct frame_info *fi, int regnum)
757 for (; fi != NULL; fi = fi->next)
759 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
760 return deprecated_read_register_dummy (fi->pc, fi->frame, regnum);
761 else if (fi->saved_regs[regnum] != 0)
762 return read_memory_integer (fi->saved_regs[regnum],
766 return read_register (regnum);
769 /* Find the saved pc in frame FI. */
772 mcore_frame_saved_pc (struct frame_info * fi)
775 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
776 return deprecated_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
778 return mcore_find_callers_reg (fi, PR_REGNUM);
781 /* INFERIOR FUNCTION CALLS */
783 /* This routine gets called when either the user uses the "return"
784 command, or the call dummy breakpoint gets hit. */
787 mcore_pop_frame (void)
790 struct frame_info *fi = get_current_frame ();
792 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
793 generic_pop_dummy_frame ();
796 /* Write out the PC we saved. */
797 write_register (PC_REGNUM, FRAME_SAVED_PC (fi));
799 /* Restore any saved registers. */
800 for (rn = 0; rn < NUM_REGS; rn++)
802 if (fi->saved_regs[rn] != 0)
806 value = read_memory_unsigned_integer (fi->saved_regs[rn],
808 write_register (rn, value);
812 /* Actually cut back the stack. */
813 write_register (SP_REGNUM, get_frame_base (fi));
816 /* Finally, throw away any cached frame information. */
817 flush_cached_frames ();
820 /* Setup arguments and PR for a call to the target. First six arguments
821 go in FIRST_ARGREG -> LAST_ARGREG, subsequent args go on to the stack.
823 * Types with lengths greater than REGISTER_SIZE may not be split
824 between registers and the stack, and they must start in an even-numbered
825 register. Subsequent args will go onto the stack.
827 * Structs may be split between registers and stack, left-aligned.
829 * If the function returns a struct which will not fit into registers (it's
830 more than eight bytes), we must allocate for that, too. Gdb will tell
831 us where this buffer is (STRUCT_ADDR), and we simply place it into
832 FIRST_ARGREG, since the MCORE treats struct returns (of less than eight
833 bytes) as hidden first arguments. */
836 mcore_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
837 int struct_return, CORE_ADDR struct_addr)
849 stack_args = (struct stack_arg *) alloca (nargs * sizeof (struct stack_arg));
851 argreg = FIRST_ARGREG;
853 /* Align the stack. This is mostly a nop, but not always. It will be needed
854 if we call a function which has argument overflow. */
857 /* If this function returns a struct which does not fit in the
858 return registers, we must pass a buffer to the function
859 which it can use to save the return value. */
861 write_register (argreg++, struct_addr);
863 /* FIXME: what about unions? */
864 for (argnum = 0; argnum < nargs; argnum++)
866 char *val = (char *) VALUE_CONTENTS (args[argnum]);
867 int len = TYPE_LENGTH (VALUE_TYPE (args[argnum]));
868 struct type *type = VALUE_TYPE (args[argnum]);
871 mcore_insn_debug (("MCORE PUSH: argreg=%d; len=%d; %s\n",
872 argreg, len, TYPE_CODE (type) == TYPE_CODE_STRUCT ? "struct" : "not struct"));
873 /* Arguments larger than a register must start in an even
874 numbered register. */
877 if (TYPE_CODE (type) != TYPE_CODE_STRUCT && len > REGISTER_SIZE && argreg % 2)
879 mcore_insn_debug (("MCORE PUSH: %d > REGISTER_SIZE: and %s is not even\n",
880 len, mcore_register_names[argreg]));
884 if ((argreg <= LAST_ARGREG && len <= (LAST_ARGREG - argreg + 1) * REGISTER_SIZE)
885 || (TYPE_CODE (type) == TYPE_CODE_STRUCT))
887 /* Something that will fit entirely into registers (or a struct
888 which may be split between registers and stack). */
889 mcore_insn_debug (("MCORE PUSH: arg %d going into regs\n", argnum));
891 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && olen < REGISTER_SIZE)
893 /* Small structs must be right aligned within the register,
894 the most significant bits are undefined. */
895 write_register (argreg, extract_unsigned_integer (val, len));
900 while (len > 0 && argreg <= LAST_ARGREG)
902 write_register (argreg, extract_unsigned_integer (val, REGISTER_SIZE));
904 val += REGISTER_SIZE;
905 len -= REGISTER_SIZE;
908 /* Any remainder for the stack is noted below... */
910 else if (TYPE_CODE (VALUE_TYPE (args[argnum])) != TYPE_CODE_STRUCT
911 && len > REGISTER_SIZE)
913 /* All subsequent args go onto the stack. */
914 mcore_insn_debug (("MCORE PUSH: does not fit into regs, going onto stack\n"));
915 argnum = LAST_ARGREG + 1;
920 /* Note that this must be saved onto the stack */
921 mcore_insn_debug (("MCORE PUSH: adding arg %d to stack\n", argnum));
922 stack_args[nstack_args].val = val;
923 stack_args[nstack_args].len = len;
929 /* We're done with registers and stack allocation. Now do the actual
931 while (nstack_args--)
933 sp -= stack_args[nstack_args].len;
934 write_memory (sp, stack_args[nstack_args].val, stack_args[nstack_args].len);
937 /* Return adjusted stack pointer. */
941 /* Store the return address for the call dummy. For MCore, we've
942 opted to use generic call dummies, so we simply store the
943 CALL_DUMMY_ADDRESS into the PR register (r15). */
946 mcore_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
948 write_register (PR_REGNUM, CALL_DUMMY_ADDRESS ());
952 /* Setting/getting return values from functions.
954 The Motorola MCore processors use r2/r3 to return anything
955 not larger than 32 bits. Everything else goes into a caller-
956 supplied buffer, which is passed in via a hidden first
959 For gdb, this leaves us two routes, based on what
960 USE_STRUCT_CONVENTION (mcore_use_struct_convention) returns.
961 If this macro returns 1, gdb will call STORE_STRUCT_RETURN and
962 EXTRACT_STRUCT_VALUE_ADDRESS.
964 If USE_STRUCT_CONVENTION retruns 0, then gdb uses STORE_RETURN_VALUE
965 and EXTRACT_RETURN_VALUE to store/fetch the functions return value. */
967 /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
968 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
969 and TYPE is the type (which is known to be struct, union or array). */
972 mcore_use_struct_convention (int gcc_p, struct type *type)
974 return (TYPE_LENGTH (type) > 8);
977 /* Where is the return value saved? For MCore, a pointer to
978 this buffer was passed as a hidden first argument, so
979 just return that address. */
982 mcore_extract_struct_value_address (char *regbuf)
984 return extract_address (regbuf + REGISTER_BYTE (FIRST_ARGREG), REGISTER_SIZE);
987 /* Given a function which returns a value of type TYPE, extract the
988 the function's return value and place the result into VALBUF.
989 REGBUF is the register contents of the target. */
992 mcore_extract_return_value (struct type *type, char *regbuf, char *valbuf)
994 /* Copy the return value (starting) in RETVAL_REGNUM to VALBUF. */
995 /* Only getting the first byte! if len = 1, we need the last byte of
996 the register, not the first. */
997 memcpy (valbuf, regbuf + REGISTER_BYTE (RETVAL_REGNUM) +
998 (TYPE_LENGTH (type) < 4 ? 4 - TYPE_LENGTH (type) : 0), TYPE_LENGTH (type));
1001 /* Store the return value in VALBUF (of type TYPE) where the caller
1004 Values less than 32 bits are stored in r2, right justified and
1005 sign or zero extended.
1007 Values between 32 and 64 bits are stored in r2 (most
1008 significant word) and r3 (least significant word, left justified).
1009 Note that this includes structures of less than eight bytes, too. */
1012 mcore_store_return_value (struct type *type, char *valbuf)
1019 value_size = TYPE_LENGTH (type);
1021 /* Return value fits into registers. */
1022 return_size = (value_size + REGISTER_SIZE - 1) & ~(REGISTER_SIZE - 1);
1023 offset = REGISTER_BYTE (RETVAL_REGNUM) + (return_size - value_size);
1024 zeros = alloca (return_size);
1025 memset (zeros, 0, return_size);
1027 deprecated_write_register_bytes (REGISTER_BYTE (RETVAL_REGNUM), zeros,
1029 deprecated_write_register_bytes (offset, valbuf, value_size);
1032 /* Initialize our target-dependent "stuff" for this newly created frame.
1034 This includes allocating space for saved registers and analyzing
1035 the prologue of this frame. */
1038 mcore_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1041 fi->pc = FRAME_SAVED_PC (fi->next);
1043 frame_saved_regs_zalloc (fi);
1045 fi->extra_info = (struct frame_extra_info *)
1046 frame_obstack_alloc (sizeof (struct frame_extra_info));
1047 fi->extra_info->status = 0;
1048 fi->extra_info->framesize = 0;
1050 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1052 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
1053 by assuming it's always FP. */
1054 fi->frame = deprecated_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
1057 mcore_analyze_prologue (fi, 0, 0);
1060 /* Get an insturction from memory. */
1063 get_insn (CORE_ADDR pc)
1066 int status = read_memory_nobpt (pc, buf, 2);
1070 return extract_unsigned_integer (buf, 2);
1073 static struct gdbarch *
1074 mcore_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1076 static LONGEST call_dummy_words[7] = { };
1077 struct gdbarch_tdep *tdep = NULL;
1078 struct gdbarch *gdbarch;
1080 /* find a candidate among the list of pre-declared architectures. */
1081 arches = gdbarch_list_lookup_by_info (arches, &info);
1083 return (arches->gdbarch);
1085 gdbarch = gdbarch_alloc (&info, 0);
1089 /* All registers are 32 bits */
1090 set_gdbarch_register_size (gdbarch, MCORE_REG_SIZE);
1091 set_gdbarch_max_register_raw_size (gdbarch, MCORE_REG_SIZE);
1092 set_gdbarch_max_register_virtual_size (gdbarch, MCORE_REG_SIZE);
1093 set_gdbarch_register_name (gdbarch, mcore_register_name);
1094 set_gdbarch_register_virtual_type (gdbarch, mcore_register_virtual_type);
1095 set_gdbarch_register_virtual_size (gdbarch, mcore_register_size);
1096 set_gdbarch_register_raw_size (gdbarch, mcore_register_size);
1097 set_gdbarch_register_byte (gdbarch, mcore_register_byte);
1098 set_gdbarch_register_bytes (gdbarch, MCORE_REG_SIZE * MCORE_NUM_REGS);
1099 set_gdbarch_num_regs (gdbarch, MCORE_NUM_REGS);
1100 set_gdbarch_pc_regnum (gdbarch, 64);
1101 set_gdbarch_sp_regnum (gdbarch, 0);
1102 set_gdbarch_fp_regnum (gdbarch, 0);
1106 set_gdbarch_call_dummy_p (gdbarch, 1);
1107 set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1108 set_gdbarch_call_dummy_words (gdbarch, call_dummy_words);
1109 set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
1110 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1111 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1112 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1113 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1114 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1115 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1116 set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1117 set_gdbarch_pc_in_call_dummy (gdbarch, generic_pc_in_call_dummy);
1118 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1119 set_gdbarch_saved_pc_after_call (gdbarch, mcore_saved_pc_after_call);
1120 set_gdbarch_function_start_offset (gdbarch, 0);
1121 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1122 set_gdbarch_breakpoint_from_pc (gdbarch, mcore_breakpoint_from_pc);
1123 set_gdbarch_push_return_address (gdbarch, mcore_push_return_address);
1124 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1125 set_gdbarch_push_arguments (gdbarch, mcore_push_arguments);
1126 set_gdbarch_call_dummy_length (gdbarch, 0);
1130 set_gdbarch_init_extra_frame_info (gdbarch, mcore_init_extra_frame_info);
1131 set_gdbarch_frame_chain (gdbarch, mcore_frame_chain);
1132 set_gdbarch_frame_chain_valid (gdbarch, generic_func_frame_chain_valid);
1133 set_gdbarch_frame_init_saved_regs (gdbarch, mcore_frame_init_saved_regs);
1134 set_gdbarch_frame_saved_pc (gdbarch, mcore_frame_saved_pc);
1135 set_gdbarch_deprecated_store_return_value (gdbarch, mcore_store_return_value);
1136 set_gdbarch_deprecated_extract_return_value (gdbarch,
1137 mcore_extract_return_value);
1138 set_gdbarch_store_struct_return (gdbarch, mcore_store_struct_return);
1139 set_gdbarch_deprecated_extract_struct_value_address (gdbarch,
1140 mcore_extract_struct_value_address);
1141 set_gdbarch_skip_prologue (gdbarch, mcore_skip_prologue);
1142 set_gdbarch_frame_args_skip (gdbarch, 0);
1143 set_gdbarch_frame_args_address (gdbarch, mcore_frame_args_address);
1144 set_gdbarch_frame_locals_address (gdbarch, mcore_frame_locals_address);
1145 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1146 set_gdbarch_pop_frame (gdbarch, mcore_pop_frame);
1147 set_gdbarch_virtual_frame_pointer (gdbarch, mcore_virtual_frame_pointer);
1151 /* Stack grows down. */
1152 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1153 set_gdbarch_use_struct_convention (gdbarch, mcore_use_struct_convention);
1154 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1155 /* MCore will never pass a sturcture by reference. It will always be split
1156 between registers and stack. */
1157 set_gdbarch_reg_struct_has_addr (gdbarch, mcore_reg_struct_has_addr);
1163 mcore_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1169 _initialize_mcore_tdep (void)
1171 extern int print_insn_mcore (bfd_vma, disassemble_info *);
1172 gdbarch_register (bfd_arch_mcore, mcore_gdbarch_init, mcore_dump_tdep);
1173 tm_print_insn = print_insn_mcore;
1176 add_show_from_set (add_set_cmd ("mcoredebug", no_class,
1177 var_boolean, (char *) &mcore_debug,
1178 "Set mcore debugging.\n", &setlist),