Update
[gdb.git] / gdb / mn10300-tdep.c
blob8cf9bb3cabb48116d3404fbc2f75eb32c9f53847
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include "dis-asm.h"
24 #include "gdbtypes.h"
25 #include "regcache.h"
26 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #include "gdbcore.h" /* for write_memory_unsigned_integer */
29 #include "value.h"
30 #include "gdbtypes.h"
31 #include "frame.h"
32 #include "frame-unwind.h"
33 #include "frame-base.h"
34 #include "trad-frame.h"
35 #include "symtab.h"
36 #include "dwarf2-frame.h"
37 #include "osabi.h"
39 #include "mn10300-tdep.h"
41 /* Forward decl. */
42 extern struct trad_frame_cache *mn10300_frame_unwind_cache (struct frame_info*,
43 void **);
45 /* Compute the alignment required by a type. */
47 static int
48 mn10300_type_align (struct type *type)
50 int i, align = 1;
52 switch (TYPE_CODE (type))
54 case TYPE_CODE_INT:
55 case TYPE_CODE_ENUM:
56 case TYPE_CODE_SET:
57 case TYPE_CODE_RANGE:
58 case TYPE_CODE_CHAR:
59 case TYPE_CODE_BOOL:
60 case TYPE_CODE_FLT:
61 case TYPE_CODE_PTR:
62 case TYPE_CODE_REF:
63 return TYPE_LENGTH (type);
65 case TYPE_CODE_COMPLEX:
66 return TYPE_LENGTH (type) / 2;
68 case TYPE_CODE_STRUCT:
69 case TYPE_CODE_UNION:
70 for (i = 0; i < TYPE_NFIELDS (type); i++)
72 int falign = mn10300_type_align (TYPE_FIELD_TYPE (type, i));
73 while (align < falign)
74 align <<= 1;
76 return align;
78 case TYPE_CODE_ARRAY:
79 /* HACK! Structures containing arrays, even small ones, are not
80 elligible for returning in registers. */
81 return 256;
83 case TYPE_CODE_TYPEDEF:
84 return mn10300_type_align (check_typedef (type));
86 default:
87 internal_error (__FILE__, __LINE__, _("bad switch"));
91 /* Should call_function allocate stack space for a struct return? */
92 static int
93 mn10300_use_struct_convention (struct type *type)
95 /* Structures bigger than a pair of words can't be returned in
96 registers. */
97 if (TYPE_LENGTH (type) > 8)
98 return 1;
100 switch (TYPE_CODE (type))
102 case TYPE_CODE_STRUCT:
103 case TYPE_CODE_UNION:
104 /* Structures with a single field are handled as the field
105 itself. */
106 if (TYPE_NFIELDS (type) == 1)
107 return mn10300_use_struct_convention (TYPE_FIELD_TYPE (type, 0));
109 /* Structures with word or double-word size are passed in memory, as
110 long as they require at least word alignment. */
111 if (mn10300_type_align (type) >= 4)
112 return 0;
114 return 1;
116 /* Arrays are addressable, so they're never returned in
117 registers. This condition can only hold when the array is
118 the only field of a struct or union. */
119 case TYPE_CODE_ARRAY:
120 return 1;
122 case TYPE_CODE_TYPEDEF:
123 return mn10300_use_struct_convention (check_typedef (type));
125 default:
126 return 0;
130 static void
131 mn10300_store_return_value (struct gdbarch *gdbarch, struct type *type,
132 struct regcache *regcache, const void *valbuf)
134 int len = TYPE_LENGTH (type);
135 int reg, regsz;
137 if (TYPE_CODE (type) == TYPE_CODE_PTR)
138 reg = 4;
139 else
140 reg = 0;
142 regsz = register_size (gdbarch, reg);
144 if (len <= regsz)
145 regcache_raw_write_part (regcache, reg, 0, len, valbuf);
146 else if (len <= 2 * regsz)
148 regcache_raw_write (regcache, reg, valbuf);
149 gdb_assert (regsz == register_size (gdbarch, reg + 1));
150 regcache_raw_write_part (regcache, reg+1, 0,
151 len - regsz, (char *) valbuf + regsz);
153 else
154 internal_error (__FILE__, __LINE__,
155 _("Cannot store return value %d bytes long."), len);
158 static void
159 mn10300_extract_return_value (struct gdbarch *gdbarch, struct type *type,
160 struct regcache *regcache, void *valbuf)
162 char buf[MAX_REGISTER_SIZE];
163 int len = TYPE_LENGTH (type);
164 int reg, regsz;
166 if (TYPE_CODE (type) == TYPE_CODE_PTR)
167 reg = 4;
168 else
169 reg = 0;
171 regsz = register_size (gdbarch, reg);
172 if (len <= regsz)
174 regcache_raw_read (regcache, reg, buf);
175 memcpy (valbuf, buf, len);
177 else if (len <= 2 * regsz)
179 regcache_raw_read (regcache, reg, buf);
180 memcpy (valbuf, buf, regsz);
181 gdb_assert (regsz == register_size (gdbarch, reg + 1));
182 regcache_raw_read (regcache, reg + 1, buf);
183 memcpy ((char *) valbuf + regsz, buf, len - regsz);
185 else
186 internal_error (__FILE__, __LINE__,
187 _("Cannot extract return value %d bytes long."), len);
190 /* Determine, for architecture GDBARCH, how a return value of TYPE
191 should be returned. If it is supposed to be returned in registers,
192 and READBUF is non-zero, read the appropriate value from REGCACHE,
193 and copy it into READBUF. If WRITEBUF is non-zero, write the value
194 from WRITEBUF into REGCACHE. */
196 static enum return_value_convention
197 mn10300_return_value (struct gdbarch *gdbarch, struct type *type,
198 struct regcache *regcache, gdb_byte *readbuf,
199 const gdb_byte *writebuf)
201 if (mn10300_use_struct_convention (type))
202 return RETURN_VALUE_STRUCT_CONVENTION;
204 if (readbuf)
205 mn10300_extract_return_value (gdbarch, type, regcache, readbuf);
206 if (writebuf)
207 mn10300_store_return_value (gdbarch, type, regcache, writebuf);
209 return RETURN_VALUE_REGISTER_CONVENTION;
212 static char *
213 register_name (int reg, char **regs, long sizeof_regs)
215 if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))
216 return NULL;
217 else
218 return regs[reg];
221 static const char *
222 mn10300_generic_register_name (struct gdbarch *gdbarch, int reg)
224 static char *regs[] =
225 { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
226 "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
227 "", "", "", "", "", "", "", "",
228 "", "", "", "", "", "", "", "fp"
230 return register_name (reg, regs, sizeof regs);
234 static const char *
235 am33_register_name (struct gdbarch *gdbarch, int reg)
237 static char *regs[] =
238 { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
239 "sp", "pc", "mdr", "psw", "lir", "lar", "",
240 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
241 "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""
243 return register_name (reg, regs, sizeof regs);
246 static const char *
247 am33_2_register_name (struct gdbarch *gdbarch, int reg)
249 static char *regs[] =
251 "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
252 "sp", "pc", "mdr", "psw", "lir", "lar", "mdrq", "r0",
253 "r1", "r2", "r3", "r4", "r5", "r6", "r7", "ssp",
254 "msp", "usp", "mcrh", "mcrl", "mcvf", "fpcr", "", "",
255 "fs0", "fs1", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",
256 "fs8", "fs9", "fs10", "fs11", "fs12", "fs13", "fs14", "fs15",
257 "fs16", "fs17", "fs18", "fs19", "fs20", "fs21", "fs22", "fs23",
258 "fs24", "fs25", "fs26", "fs27", "fs28", "fs29", "fs30", "fs31"
260 return register_name (reg, regs, sizeof regs);
263 static struct type *
264 mn10300_register_type (struct gdbarch *gdbarch, int reg)
266 return builtin_type_int;
269 static CORE_ADDR
270 mn10300_read_pc (struct regcache *regcache)
272 ULONGEST val;
273 regcache_cooked_read_unsigned (regcache, E_PC_REGNUM, &val);
274 return val;
277 static void
278 mn10300_write_pc (struct regcache *regcache, CORE_ADDR val)
280 regcache_cooked_write_unsigned (regcache, E_PC_REGNUM, val);
283 /* The breakpoint instruction must be the same size as the smallest
284 instruction in the instruction set.
286 The Matsushita mn10x00 processors have single byte instructions
287 so we need a single byte breakpoint. Matsushita hasn't defined
288 one, so we defined it ourselves. */
290 const static unsigned char *
291 mn10300_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *bp_addr,
292 int *bp_size)
294 static char breakpoint[] = {0xff};
295 *bp_size = 1;
296 return breakpoint;
299 /* Set offsets of saved registers.
300 This is a helper function for mn10300_analyze_prologue. */
302 static void
303 set_reg_offsets (struct frame_info *fi,
304 void **this_cache,
305 int movm_args,
306 int fpregmask,
307 int stack_extra_size,
308 int frame_in_fp)
310 struct gdbarch *gdbarch;
311 struct trad_frame_cache *cache;
312 int offset = 0;
313 CORE_ADDR base;
315 if (fi == NULL || this_cache == NULL)
316 return;
318 cache = mn10300_frame_unwind_cache (fi, this_cache);
319 if (cache == NULL)
320 return;
321 gdbarch = get_frame_arch (fi);
323 if (frame_in_fp)
325 base = frame_unwind_register_unsigned (fi, E_A3_REGNUM);
327 else
329 base = frame_unwind_register_unsigned (fi, E_SP_REGNUM)
330 + stack_extra_size;
333 trad_frame_set_this_base (cache, base);
335 if (AM33_MODE (gdbarch) == 2)
337 /* If bit N is set in fpregmask, fsN is saved on the stack.
338 The floating point registers are saved in ascending order.
339 For example: fs16 <- Frame Pointer
340 fs17 Frame Pointer + 4 */
341 if (fpregmask != 0)
343 int i;
344 for (i = 0; i < 32; i++)
346 if (fpregmask & (1 << i))
348 trad_frame_set_reg_addr (cache, E_FS0_REGNUM + i,
349 base + offset);
350 offset += 4;
357 if (movm_args & movm_other_bit)
359 /* The `other' bit leaves a blank area of four bytes at the
360 beginning of its block of saved registers, making it 32 bytes
361 long in total. */
362 trad_frame_set_reg_addr (cache, E_LAR_REGNUM, base + offset + 4);
363 trad_frame_set_reg_addr (cache, E_LIR_REGNUM, base + offset + 8);
364 trad_frame_set_reg_addr (cache, E_MDR_REGNUM, base + offset + 12);
365 trad_frame_set_reg_addr (cache, E_A0_REGNUM + 1, base + offset + 16);
366 trad_frame_set_reg_addr (cache, E_A0_REGNUM, base + offset + 20);
367 trad_frame_set_reg_addr (cache, E_D0_REGNUM + 1, base + offset + 24);
368 trad_frame_set_reg_addr (cache, E_D0_REGNUM, base + offset + 28);
369 offset += 32;
372 if (movm_args & movm_a3_bit)
374 trad_frame_set_reg_addr (cache, E_A3_REGNUM, base + offset);
375 offset += 4;
377 if (movm_args & movm_a2_bit)
379 trad_frame_set_reg_addr (cache, E_A2_REGNUM, base + offset);
380 offset += 4;
382 if (movm_args & movm_d3_bit)
384 trad_frame_set_reg_addr (cache, E_D3_REGNUM, base + offset);
385 offset += 4;
387 if (movm_args & movm_d2_bit)
389 trad_frame_set_reg_addr (cache, E_D2_REGNUM, base + offset);
390 offset += 4;
392 if (AM33_MODE (gdbarch))
394 if (movm_args & movm_exother_bit)
396 trad_frame_set_reg_addr (cache, E_MCVF_REGNUM, base + offset);
397 trad_frame_set_reg_addr (cache, E_MCRL_REGNUM, base + offset + 4);
398 trad_frame_set_reg_addr (cache, E_MCRH_REGNUM, base + offset + 8);
399 trad_frame_set_reg_addr (cache, E_MDRQ_REGNUM, base + offset + 12);
400 trad_frame_set_reg_addr (cache, E_E1_REGNUM, base + offset + 16);
401 trad_frame_set_reg_addr (cache, E_E0_REGNUM, base + offset + 20);
402 offset += 24;
404 if (movm_args & movm_exreg1_bit)
406 trad_frame_set_reg_addr (cache, E_E7_REGNUM, base + offset);
407 trad_frame_set_reg_addr (cache, E_E6_REGNUM, base + offset + 4);
408 trad_frame_set_reg_addr (cache, E_E5_REGNUM, base + offset + 8);
409 trad_frame_set_reg_addr (cache, E_E4_REGNUM, base + offset + 12);
410 offset += 16;
412 if (movm_args & movm_exreg0_bit)
414 trad_frame_set_reg_addr (cache, E_E3_REGNUM, base + offset);
415 trad_frame_set_reg_addr (cache, E_E2_REGNUM, base + offset + 4);
416 offset += 8;
419 /* The last (or first) thing on the stack will be the PC. */
420 trad_frame_set_reg_addr (cache, E_PC_REGNUM, base + offset);
421 /* Save the SP in the 'traditional' way.
422 This will be the same location where the PC is saved. */
423 trad_frame_set_reg_value (cache, E_SP_REGNUM, base + offset);
426 /* The main purpose of this file is dealing with prologues to extract
427 information about stack frames and saved registers.
429 In gcc/config/mn13000/mn10300.c, the expand_prologue prologue
430 function is pretty readable, and has a nice explanation of how the
431 prologue is generated. The prologues generated by that code will
432 have the following form (NOTE: the current code doesn't handle all
433 this!):
435 + If this is an old-style varargs function, then its arguments
436 need to be flushed back to the stack:
438 mov d0,(4,sp)
439 mov d1,(4,sp)
441 + If we use any of the callee-saved registers, save them now.
443 movm [some callee-saved registers],(sp)
445 + If we have any floating-point registers to save:
447 - Decrement the stack pointer to reserve space for the registers.
448 If the function doesn't need a frame pointer, we may combine
449 this with the adjustment that reserves space for the frame.
451 add -SIZE, sp
453 - Save the floating-point registers. We have two possible
454 strategies:
456 . Save them at fixed offset from the SP:
458 fmov fsN,(OFFSETN,sp)
459 fmov fsM,(OFFSETM,sp)
462 Note that, if OFFSETN happens to be zero, you'll get the
463 different opcode: fmov fsN,(sp)
465 . Or, set a0 to the start of the save area, and then use
466 post-increment addressing to save the FP registers.
468 mov sp, a0
469 add SIZE, a0
470 fmov fsN,(a0+)
471 fmov fsM,(a0+)
474 + If the function needs a frame pointer, we set it here.
476 mov sp, a3
478 + Now we reserve space for the stack frame proper. This could be
479 merged into the `add -SIZE, sp' instruction for FP saves up
480 above, unless we needed to set the frame pointer in the previous
481 step, or the frame is so large that allocating the whole thing at
482 once would put the FP register save slots out of reach of the
483 addressing mode (128 bytes).
485 add -SIZE, sp
487 One day we might keep the stack pointer constant, that won't
488 change the code for prologues, but it will make the frame
489 pointerless case much more common. */
491 /* Analyze the prologue to determine where registers are saved,
492 the end of the prologue, etc etc. Return the end of the prologue
493 scanned.
495 We store into FI (if non-null) several tidbits of information:
497 * stack_size -- size of this stack frame. Note that if we stop in
498 certain parts of the prologue/epilogue we may claim the size of the
499 current frame is zero. This happens when the current frame has
500 not been allocated yet or has already been deallocated.
502 * fsr -- Addresses of registers saved in the stack by this frame.
504 * status -- A (relatively) generic status indicator. It's a bitmask
505 with the following bits:
507 MY_FRAME_IN_SP: The base of the current frame is actually in
508 the stack pointer. This can happen for frame pointerless
509 functions, or cases where we're stopped in the prologue/epilogue
510 itself. For these cases mn10300_analyze_prologue will need up
511 update fi->frame before returning or analyzing the register
512 save instructions.
514 MY_FRAME_IN_FP: The base of the current frame is in the
515 frame pointer register ($a3).
517 NO_MORE_FRAMES: Set this if the current frame is "start" or
518 if the first instruction looks like mov <imm>,sp. This tells
519 frame chain to not bother trying to unwind past this frame. */
521 static CORE_ADDR
522 mn10300_analyze_prologue (struct gdbarch *gdbarch, struct frame_info *fi,
523 void **this_cache,
524 CORE_ADDR pc)
526 CORE_ADDR func_addr, func_end, addr, stop;
527 long stack_extra_size = 0;
528 int imm_size;
529 unsigned char buf[4];
530 int status;
531 int movm_args = 0;
532 int fpregmask = 0;
533 char *name;
534 int frame_in_fp = 0;
536 /* Use the PC in the frame if it's provided to look up the
537 start of this function.
539 Note: kevinb/2003-07-16: We used to do the following here:
540 pc = (fi ? get_frame_pc (fi) : pc);
541 But this is (now) badly broken when called from analyze_dummy_frame().
543 if (fi)
545 pc = (pc ? pc : get_frame_pc (fi));
548 /* Find the start of this function. */
549 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
551 /* Do nothing if we couldn't find the start of this function
553 MVS: comment went on to say "or if we're stopped at the first
554 instruction in the prologue" -- but code doesn't reflect that,
555 and I don't want to do that anyway. */
556 if (status == 0)
558 addr = pc;
559 goto finish_prologue;
562 /* If we're in start, then give up. */
563 if (strcmp (name, "start") == 0)
565 addr = pc;
566 goto finish_prologue;
569 /* Figure out where to stop scanning. */
570 stop = fi ? pc : func_end;
572 /* Don't walk off the end of the function. */
573 stop = stop > func_end ? func_end : stop;
575 /* Start scanning on the first instruction of this function. */
576 addr = func_addr;
578 /* Suck in two bytes. */
579 if (addr + 2 > stop || !safe_frame_unwind_memory (fi, addr, buf, 2))
580 goto finish_prologue;
582 /* First see if this insn sets the stack pointer from a register; if
583 so, it's probably the initialization of the stack pointer in _start,
584 so mark this as the bottom-most frame. */
585 if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0)
587 goto finish_prologue;
590 /* Now look for movm [regs],sp, which saves the callee saved registers.
592 At this time we don't know if fi->frame is valid, so we only note
593 that we encountered a movm instruction. Later, we'll set the entries
594 in fsr.regs as needed. */
595 if (buf[0] == 0xcf)
597 /* Extract the register list for the movm instruction. */
598 movm_args = buf[1];
600 addr += 2;
602 /* Quit now if we're beyond the stop point. */
603 if (addr >= stop)
604 goto finish_prologue;
606 /* Get the next two bytes so the prologue scan can continue. */
607 if (!safe_frame_unwind_memory (fi, addr, buf, 2))
608 goto finish_prologue;
611 /* Check for "mov pc, a2", an instruction found in optimized, position
612 independent code. Skip it if found. */
613 if (buf[0] == 0xf0 && buf[1] == 0x2e)
615 addr += 2;
617 /* Quit now if we're beyond the stop point. */
618 if (addr >= stop)
619 goto finish_prologue;
621 /* Get the next two bytes so the prologue scan can continue. */
622 status = read_memory_nobpt (addr, buf, 2);
623 if (status != 0)
624 goto finish_prologue;
627 if (AM33_MODE (gdbarch) == 2)
629 /* Determine if any floating point registers are to be saved.
630 Look for one of the following three prologue formats:
632 [movm [regs],(sp)] [movm [regs],(sp)] [movm [regs],(sp)]
634 add -SIZE,sp add -SIZE,sp add -SIZE,sp
635 fmov fs#,(sp) mov sp,a0/a1 mov sp,a0/a1
636 fmov fs#,(#,sp) fmov fs#,(a0/a1+) add SIZE2,a0/a1
637 ... ... fmov fs#,(a0/a1+)
638 ... ... ...
639 fmov fs#,(#,sp) fmov fs#,(a0/a1+) fmov fs#,(a0/a1+)
641 [mov sp,a3] [mov sp,a3]
642 [add -SIZE2,sp] [add -SIZE2,sp] */
644 /* Remember the address at which we started in the event that we
645 don't ultimately find an fmov instruction. Once we're certain
646 that we matched one of the above patterns, we'll set
647 ``restore_addr'' to the appropriate value. Note: At one time
648 in the past, this code attempted to not adjust ``addr'' until
649 there was a fair degree of certainty that the pattern would be
650 matched. However, that code did not wait until an fmov instruction
651 was actually encountered. As a consequence, ``addr'' would
652 sometimes be advanced even when no fmov instructions were found. */
653 CORE_ADDR restore_addr = addr;
654 int fmov_found = 0;
656 /* First, look for add -SIZE,sp (i.e. add imm8,sp (0xf8feXX)
657 or add imm16,sp (0xfafeXXXX)
658 or add imm32,sp (0xfcfeXXXXXXXX)) */
659 imm_size = 0;
660 if (buf[0] == 0xf8 && buf[1] == 0xfe)
661 imm_size = 1;
662 else if (buf[0] == 0xfa && buf[1] == 0xfe)
663 imm_size = 2;
664 else if (buf[0] == 0xfc && buf[1] == 0xfe)
665 imm_size = 4;
666 if (imm_size != 0)
668 /* An "add -#,sp" instruction has been found. "addr + 2 + imm_size"
669 is the address of the next instruction. Don't modify "addr" until
670 the next "floating point prologue" instruction is found. If this
671 is not a prologue that saves floating point registers we need to
672 be able to back out of this bit of code and continue with the
673 prologue analysis. */
674 if (addr + 2 + imm_size < stop)
676 if (!safe_frame_unwind_memory (fi, addr + 2 + imm_size, buf, 3))
677 goto finish_prologue;
678 if ((buf[0] & 0xfc) == 0x3c)
680 /* Occasionally, especially with C++ code, the "fmov"
681 instructions will be preceded by "mov sp,aN"
682 (aN => a0, a1, a2, or a3).
684 This is a one byte instruction: mov sp,aN = 0011 11XX
685 where XX is the register number.
687 Skip this instruction by incrementing addr. The "fmov"
688 instructions will have the form "fmov fs#,(aN+)" in this
689 case, but that will not necessitate a change in the
690 "fmov" parsing logic below. */
692 addr++;
694 if ((buf[1] & 0xfc) == 0x20)
696 /* Occasionally, especially with C++ code compiled with
697 the -fomit-frame-pointer or -O3 options, the
698 "mov sp,aN" instruction will be followed by an
699 "add #,aN" instruction. This indicates the
700 "stack_size", the size of the portion of the stack
701 containing the arguments. This instruction format is:
702 add #,aN = 0010 00XX YYYY YYYY
703 where XX is the register number
704 YYYY YYYY is the constant.
705 Note the size of the stack (as a negative number) in
706 the frame info structure. */
707 if (fi)
708 stack_extra_size += -buf[2];
710 addr += 2;
714 if ((buf[0] & 0xfc) == 0x3c ||
715 buf[0] == 0xf9 || buf[0] == 0xfb)
717 /* An "fmov" instruction has been found indicating that this
718 prologue saves floating point registers (or, as described
719 above, a "mov sp,aN" and possible "add #,aN" have been
720 found and we will assume an "fmov" follows). Process the
721 consecutive "fmov" instructions. */
722 for (addr += 2 + imm_size;;addr += imm_size)
724 int regnum;
726 /* Read the "fmov" instruction. */
727 if (addr >= stop ||
728 !safe_frame_unwind_memory (fi, addr, buf, 4))
729 goto finish_prologue;
731 if (buf[0] != 0xf9 && buf[0] != 0xfb)
732 break;
734 /* An fmov instruction has just been seen. We can
735 now really commit to the pattern match. */
737 fmov_found = 1;
739 /* Get the floating point register number from the
740 2nd and 3rd bytes of the "fmov" instruction:
741 Machine Code: 0000 00X0 YYYY 0000 =>
742 Regnum: 000X YYYY */
743 regnum = (buf[1] & 0x02) << 3;
744 regnum |= ((buf[2] & 0xf0) >> 4) & 0x0f;
746 /* Add this register number to the bit mask of floating
747 point registers that have been saved. */
748 fpregmask |= 1 << regnum;
750 /* Determine the length of this "fmov" instruction.
751 fmov fs#,(sp) => 3 byte instruction
752 fmov fs#,(#,sp) => 4 byte instruction */
753 imm_size = (buf[0] == 0xf9) ? 3 : 4;
758 /* If no fmov instructions were found by the above sequence, reset
759 the state and pretend that the above bit of code never happened. */
760 if (!fmov_found)
762 addr = restore_addr;
763 status = read_memory_nobpt (addr, buf, 2);
764 if (status != 0)
765 goto finish_prologue;
766 stack_extra_size = 0;
770 /* Now see if we set up a frame pointer via "mov sp,a3" */
771 if (buf[0] == 0x3f)
773 addr += 1;
775 /* The frame pointer is now valid. */
776 if (fi)
778 frame_in_fp = 1;
781 /* Quit now if we're beyond the stop point. */
782 if (addr >= stop)
783 goto finish_prologue;
785 /* Get two more bytes so scanning can continue. */
786 if (!safe_frame_unwind_memory (fi, addr, buf, 2))
787 goto finish_prologue;
790 /* Next we should allocate the local frame. No more prologue insns
791 are found after allocating the local frame.
793 Search for add imm8,sp (0xf8feXX)
794 or add imm16,sp (0xfafeXXXX)
795 or add imm32,sp (0xfcfeXXXXXXXX).
797 If none of the above was found, then this prologue has no
798 additional stack. */
800 imm_size = 0;
801 if (buf[0] == 0xf8 && buf[1] == 0xfe)
802 imm_size = 1;
803 else if (buf[0] == 0xfa && buf[1] == 0xfe)
804 imm_size = 2;
805 else if (buf[0] == 0xfc && buf[1] == 0xfe)
806 imm_size = 4;
808 if (imm_size != 0)
810 /* Suck in imm_size more bytes, they'll hold the size of the
811 current frame. */
812 if (!safe_frame_unwind_memory (fi, addr + 2, buf, imm_size))
813 goto finish_prologue;
815 /* Note the size of the stack. */
816 stack_extra_size -= extract_signed_integer (buf, imm_size);
818 /* We just consumed 2 + imm_size bytes. */
819 addr += 2 + imm_size;
821 /* No more prologue insns follow, so begin preparation to return. */
822 goto finish_prologue;
824 /* Do the essentials and get out of here. */
825 finish_prologue:
826 /* Note if/where callee saved registers were saved. */
827 if (fi)
828 set_reg_offsets (fi, this_cache, movm_args, fpregmask, stack_extra_size,
829 frame_in_fp);
830 return addr;
833 /* Function: skip_prologue
834 Return the address of the first inst past the prologue of the function. */
836 static CORE_ADDR
837 mn10300_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
839 return mn10300_analyze_prologue (gdbarch, NULL, NULL, pc);
842 /* Simple frame_unwind_cache.
843 This finds the "extra info" for the frame. */
844 struct trad_frame_cache *
845 mn10300_frame_unwind_cache (struct frame_info *next_frame,
846 void **this_prologue_cache)
848 struct gdbarch *gdbarch;
849 struct trad_frame_cache *cache;
850 CORE_ADDR pc, start, end;
851 void *cache_p;
853 if (*this_prologue_cache)
854 return (*this_prologue_cache);
856 gdbarch = get_frame_arch (next_frame);
857 cache_p = trad_frame_cache_zalloc (next_frame);
858 pc = gdbarch_unwind_pc (gdbarch, next_frame);
859 mn10300_analyze_prologue (gdbarch, next_frame, &cache_p, pc);
860 cache = cache_p;
862 if (find_pc_partial_function (pc, NULL, &start, &end))
863 trad_frame_set_id (cache,
864 frame_id_build (trad_frame_get_this_base (cache),
865 start));
866 else
868 start = frame_func_unwind (next_frame, NORMAL_FRAME);
869 trad_frame_set_id (cache,
870 frame_id_build (trad_frame_get_this_base (cache),
871 start));
874 (*this_prologue_cache) = cache;
875 return cache;
878 /* Here is a dummy implementation. */
879 static struct frame_id
880 mn10300_unwind_dummy_id (struct gdbarch *gdbarch,
881 struct frame_info *next_frame)
883 return frame_id_build (frame_sp_unwind (next_frame),
884 frame_pc_unwind (next_frame));
887 /* Trad frame implementation. */
888 static void
889 mn10300_frame_this_id (struct frame_info *next_frame,
890 void **this_prologue_cache,
891 struct frame_id *this_id)
893 struct trad_frame_cache *cache =
894 mn10300_frame_unwind_cache (next_frame, this_prologue_cache);
896 trad_frame_get_id (cache, this_id);
899 static void
900 mn10300_frame_prev_register (struct frame_info *next_frame,
901 void **this_prologue_cache,
902 int regnum, int *optimizedp,
903 enum lval_type *lvalp, CORE_ADDR *addrp,
904 int *realnump, gdb_byte *bufferp)
906 struct trad_frame_cache *cache =
907 mn10300_frame_unwind_cache (next_frame, this_prologue_cache);
909 trad_frame_get_register (cache, next_frame, regnum, optimizedp,
910 lvalp, addrp, realnump, bufferp);
911 /* Or...
912 trad_frame_get_prev_register (next_frame, cache->prev_regs, regnum,
913 optimizedp, lvalp, addrp, realnump, bufferp);
917 static const struct frame_unwind mn10300_frame_unwind = {
918 NORMAL_FRAME,
919 mn10300_frame_this_id,
920 mn10300_frame_prev_register
923 static CORE_ADDR
924 mn10300_frame_base_address (struct frame_info *next_frame,
925 void **this_prologue_cache)
927 struct trad_frame_cache *cache =
928 mn10300_frame_unwind_cache (next_frame, this_prologue_cache);
930 return trad_frame_get_this_base (cache);
933 static const struct frame_unwind *
934 mn10300_frame_sniffer (struct frame_info *next_frame)
936 return &mn10300_frame_unwind;
939 static const struct frame_base mn10300_frame_base = {
940 &mn10300_frame_unwind,
941 mn10300_frame_base_address,
942 mn10300_frame_base_address,
943 mn10300_frame_base_address
946 static CORE_ADDR
947 mn10300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
949 ULONGEST pc;
951 pc = frame_unwind_register_unsigned (next_frame, E_PC_REGNUM);
952 return pc;
955 static CORE_ADDR
956 mn10300_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
958 ULONGEST sp;
960 sp = frame_unwind_register_unsigned (next_frame, E_SP_REGNUM);
961 return sp;
964 static void
965 mn10300_frame_unwind_init (struct gdbarch *gdbarch)
967 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
968 frame_unwind_append_sniffer (gdbarch, mn10300_frame_sniffer);
969 frame_base_set_default (gdbarch, &mn10300_frame_base);
970 set_gdbarch_unwind_dummy_id (gdbarch, mn10300_unwind_dummy_id);
971 set_gdbarch_unwind_pc (gdbarch, mn10300_unwind_pc);
972 set_gdbarch_unwind_sp (gdbarch, mn10300_unwind_sp);
975 /* Function: push_dummy_call
977 * Set up machine state for a target call, including
978 * function arguments, stack, return address, etc.
982 static CORE_ADDR
983 mn10300_push_dummy_call (struct gdbarch *gdbarch,
984 struct value *target_func,
985 struct regcache *regcache,
986 CORE_ADDR bp_addr,
987 int nargs, struct value **args,
988 CORE_ADDR sp,
989 int struct_return,
990 CORE_ADDR struct_addr)
992 const int push_size = register_size (gdbarch, E_PC_REGNUM);
993 int regs_used;
994 int len, arg_len;
995 int stack_offset = 0;
996 int argnum;
997 char *val, valbuf[MAX_REGISTER_SIZE];
999 /* This should be a nop, but align the stack just in case something
1000 went wrong. Stacks are four byte aligned on the mn10300. */
1001 sp &= ~3;
1003 /* Now make space on the stack for the args.
1005 XXX This doesn't appear to handle pass-by-invisible reference
1006 arguments. */
1007 regs_used = struct_return ? 1 : 0;
1008 for (len = 0, argnum = 0; argnum < nargs; argnum++)
1010 arg_len = (TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3;
1011 while (regs_used < 2 && arg_len > 0)
1013 regs_used++;
1014 arg_len -= push_size;
1016 len += arg_len;
1019 /* Allocate stack space. */
1020 sp -= len;
1022 if (struct_return)
1024 regs_used = 1;
1025 regcache_cooked_write_unsigned (regcache, E_D0_REGNUM, struct_addr);
1027 else
1028 regs_used = 0;
1030 /* Push all arguments onto the stack. */
1031 for (argnum = 0; argnum < nargs; argnum++)
1033 /* FIXME what about structs? Unions? */
1034 if (TYPE_CODE (value_type (*args)) == TYPE_CODE_STRUCT
1035 && TYPE_LENGTH (value_type (*args)) > 8)
1037 /* Change to pointer-to-type. */
1038 arg_len = push_size;
1039 store_unsigned_integer (valbuf, push_size,
1040 VALUE_ADDRESS (*args));
1041 val = &valbuf[0];
1043 else
1045 arg_len = TYPE_LENGTH (value_type (*args));
1046 val = (char *) value_contents (*args);
1049 while (regs_used < 2 && arg_len > 0)
1051 regcache_cooked_write_unsigned (regcache, regs_used,
1052 extract_unsigned_integer (val, push_size));
1053 val += push_size;
1054 arg_len -= push_size;
1055 regs_used++;
1058 while (arg_len > 0)
1060 write_memory (sp + stack_offset, val, push_size);
1061 arg_len -= push_size;
1062 val += push_size;
1063 stack_offset += push_size;
1066 args++;
1069 /* Make space for the flushback area. */
1070 sp -= 8;
1072 /* Push the return address that contains the magic breakpoint. */
1073 sp -= 4;
1074 write_memory_unsigned_integer (sp, push_size, bp_addr);
1076 /* The CPU also writes the return address always into the
1077 MDR register on "call". */
1078 regcache_cooked_write_unsigned (regcache, E_MDR_REGNUM, bp_addr);
1080 /* Update $sp. */
1081 regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);
1082 return sp;
1085 /* If DWARF2 is a register number appearing in Dwarf2 debug info, then
1086 mn10300_dwarf2_reg_to_regnum (DWARF2) is the corresponding GDB
1087 register number. Why don't Dwarf2 and GDB use the same numbering?
1088 Who knows? But since people have object files lying around with
1089 the existing Dwarf2 numbering, and other people have written stubs
1090 to work with the existing GDB, neither of them can change. So we
1091 just have to cope. */
1092 static int
1093 mn10300_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int dwarf2)
1095 /* This table is supposed to be shaped like the gdbarch_register_name
1096 initializer in gcc/config/mn10300/mn10300.h. Registers which
1097 appear in GCC's numbering, but have no counterpart in GDB's
1098 world, are marked with a -1. */
1099 static int dwarf2_to_gdb[] = {
1100 0, 1, 2, 3, 4, 5, 6, 7, -1, 8,
1101 15, 16, 17, 18, 19, 20, 21, 22,
1102 32, 33, 34, 35, 36, 37, 38, 39,
1103 40, 41, 42, 43, 44, 45, 46, 47,
1104 48, 49, 50, 51, 52, 53, 54, 55,
1105 56, 57, 58, 59, 60, 61, 62, 63,
1109 if (dwarf2 < 0
1110 || dwarf2 >= ARRAY_SIZE (dwarf2_to_gdb))
1112 warning (_("Bogus register number in debug info: %d"), dwarf2);
1113 return -1;
1116 return dwarf2_to_gdb[dwarf2];
1119 static struct gdbarch *
1120 mn10300_gdbarch_init (struct gdbarch_info info,
1121 struct gdbarch_list *arches)
1123 struct gdbarch *gdbarch;
1124 struct gdbarch_tdep *tdep;
1125 int num_regs;
1127 arches = gdbarch_list_lookup_by_info (arches, &info);
1128 if (arches != NULL)
1129 return arches->gdbarch;
1131 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1132 gdbarch = gdbarch_alloc (&info, tdep);
1134 switch (info.bfd_arch_info->mach)
1136 case 0:
1137 case bfd_mach_mn10300:
1138 set_gdbarch_register_name (gdbarch, mn10300_generic_register_name);
1139 tdep->am33_mode = 0;
1140 num_regs = 32;
1141 break;
1142 case bfd_mach_am33:
1143 set_gdbarch_register_name (gdbarch, am33_register_name);
1144 tdep->am33_mode = 1;
1145 num_regs = 32;
1146 break;
1147 case bfd_mach_am33_2:
1148 set_gdbarch_register_name (gdbarch, am33_2_register_name);
1149 tdep->am33_mode = 2;
1150 num_regs = 64;
1151 set_gdbarch_fp0_regnum (gdbarch, 32);
1152 break;
1153 default:
1154 internal_error (__FILE__, __LINE__,
1155 _("mn10300_gdbarch_init: Unknown mn10300 variant"));
1156 break;
1159 /* Registers. */
1160 set_gdbarch_num_regs (gdbarch, num_regs);
1161 set_gdbarch_register_type (gdbarch, mn10300_register_type);
1162 set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);
1163 set_gdbarch_read_pc (gdbarch, mn10300_read_pc);
1164 set_gdbarch_write_pc (gdbarch, mn10300_write_pc);
1165 set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1166 set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1167 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mn10300_dwarf2_reg_to_regnum);
1169 /* Stack unwinding. */
1170 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1171 /* Breakpoints. */
1172 set_gdbarch_breakpoint_from_pc (gdbarch, mn10300_breakpoint_from_pc);
1173 /* decr_pc_after_break? */
1174 /* Disassembly. */
1175 set_gdbarch_print_insn (gdbarch, print_insn_mn10300);
1177 /* Stage 2 */
1178 set_gdbarch_return_value (gdbarch, mn10300_return_value);
1180 /* Stage 3 -- get target calls working. */
1181 set_gdbarch_push_dummy_call (gdbarch, mn10300_push_dummy_call);
1182 /* set_gdbarch_return_value (store, extract) */
1185 mn10300_frame_unwind_init (gdbarch);
1187 /* Hook in ABI-specific overrides, if they have been registered. */
1188 gdbarch_init_osabi (info, gdbarch);
1190 return gdbarch;
1193 /* Dump out the mn10300 specific architecture information. */
1195 static void
1196 mn10300_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
1198 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1199 fprintf_unfiltered (file, "mn10300_dump_tdep: am33_mode = %d\n",
1200 tdep->am33_mode);
1203 void
1204 _initialize_mn10300_tdep (void)
1206 gdbarch_register (bfd_arch_mn10300, mn10300_gdbarch_init, mn10300_dump_tdep);