1 /* Target-machine dependent code for Motorola 88000 series, for GDB.
3 Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4 2000, 2001, 2002 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
33 /* Size of an instruction */
34 #define BYTES_PER_88K_INSN 4
36 void frame_find_saved_regs ();
38 /* Is this target an m88110? Otherwise assume m88100. This has
39 relevance for the ways in which we screw with instruction pointers. */
41 int target_is_m88110 = 0;
44 m88k_target_write_pc (CORE_ADDR pc, ptid_t ptid)
46 /* According to the MC88100 RISC Microprocessor User's Manual,
49 ... can be made to return to a particular instruction by placing
50 a valid instruction address in the SNIP and the next sequential
51 instruction address in the SFIP (with V bits set and E bits
52 clear). The rte resumes execution at the instruction pointed to
53 by the SNIP, then the SFIP.
55 The E bit is the least significant bit (bit 0). The V (valid)
56 bit is bit 1. This is why we logical or 2 into the values we are
57 writing below. It turns out that SXIP plays no role when
58 returning from an exception so nothing special has to be done
59 with it. We could even (presumably) give it a totally bogus
64 write_register_pid (SXIP_REGNUM, pc, ptid);
65 write_register_pid (SNIP_REGNUM, (pc | 2), ptid);
66 write_register_pid (SFIP_REGNUM, (pc | 2) + 4, ptid);
69 /* The type of a register. */
71 m88k_register_type (int regnum)
73 if (regnum >= XFP_REGNUM)
74 return builtin_type_m88110_ext;
75 else if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
76 return builtin_type_void_func_ptr;
78 return builtin_type_int32;
82 /* The m88k kernel aligns all instructions on 4-byte boundaries. The
83 kernel also uses the least significant two bits for its own hocus
84 pocus. When gdb receives an address from the kernel, it needs to
85 preserve those right-most two bits, but gdb also needs to be careful
86 to realize that those two bits are not really a part of the address
87 of an instruction. Shrug. */
90 m88k_addr_bits_remove (CORE_ADDR addr)
96 /* Given a GDB frame, determine the address of the calling function's frame.
97 This will be used to create a new GDB frame struct, and then
98 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
100 For us, the frame address is its stack pointer value, so we look up
101 the function prologue to determine the caller's sp value, and return it. */
104 frame_chain (struct frame_info *thisframe)
107 frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
108 /* NOTE: this depends on frame_find_saved_regs returning the VALUE, not
109 the ADDRESS, of SP_REGNUM. It also depends on the cache of
110 frame_find_saved_regs results. */
111 if (thisframe->fsr->regs[SP_REGNUM])
112 return thisframe->fsr->regs[SP_REGNUM];
114 return thisframe->frame; /* Leaf fn -- next frame up has same SP. */
118 frameless_function_invocation (struct frame_info *frame)
121 frame_find_saved_regs (frame, (struct frame_saved_regs *) 0);
122 /* NOTE: this depends on frame_find_saved_regs returning the VALUE, not
123 the ADDRESS, of SP_REGNUM. It also depends on the cache of
124 frame_find_saved_regs results. */
125 if (frame->fsr->regs[SP_REGNUM])
126 return 0; /* Frameful -- return addr saved somewhere */
128 return 1; /* Frameless -- no saved return address */
132 init_extra_frame_info (int fromleaf, struct frame_info *frame)
134 frame->fsr = 0; /* Not yet allocated */
135 frame->args_pointer = 0; /* Unknown */
136 frame->locals_pointer = 0; /* Unknown */
139 /* Examine an m88k function prologue, recording the addresses at which
140 registers are saved explicitly by the prologue code, and returning
141 the address of the first instruction after the prologue (but not
142 after the instruction at address LIMIT, as explained below).
144 LIMIT places an upper bound on addresses of the instructions to be
145 examined. If the prologue code scan reaches LIMIT, the scan is
146 aborted and LIMIT is returned. This is used, when examining the
147 prologue for the current frame, to keep examine_prologue () from
148 claiming that a given register has been saved when in fact the
149 instruction that saves it has not yet been executed. LIMIT is used
150 at other times to stop the scan when we hit code after the true
151 function prologue (e.g. for the first source line) which might
152 otherwise be mistaken for function prologue.
154 The format of the function prologue matched by this routine is
155 derived from examination of the source to gcc 1.95, particularly
156 the routine output_prologue () in config/out-m88k.c.
158 subu r31,r31,n # stack pointer update
160 (st rn,r31,offset)? # save incoming regs
161 (st.d rn,r31,offset)?
163 (addu r30,r31,n)? # frame pointer update
165 (pic sequence)? # PIC code prologue
167 (or rn,rm,0)? # Move parameters to other regs
170 /* Macros for extracting fields from instructions. */
172 #define BITMASK(pos, width) (((0x1 << (width)) - 1) << (pos))
173 #define EXTRACT_FIELD(val, pos, width) ((val) >> (pos) & BITMASK (0, width))
174 #define SUBU_OFFSET(x) ((unsigned)(x & 0xFFFF))
175 #define ST_OFFSET(x) ((unsigned)((x) & 0xFFFF))
176 #define ST_SRC(x) EXTRACT_FIELD ((x), 21, 5)
177 #define ADDU_OFFSET(x) ((unsigned)(x & 0xFFFF))
180 * prologue_insn_tbl is a table of instructions which may comprise a
181 * function prologue. Associated with each table entry (corresponding
182 * to a single instruction or group of instructions), is an action.
183 * This action is used by examine_prologue (below) to determine
184 * the state of certain machine registers and where the stack frame lives.
187 enum prologue_insn_action
189 PIA_SKIP, /* don't care what the instruction does */
190 PIA_NOTE_ST, /* note register stored and where */
191 PIA_NOTE_STD, /* note pair of registers stored and where */
192 PIA_NOTE_SP_ADJUSTMENT, /* note stack pointer adjustment */
193 PIA_NOTE_FP_ASSIGNMENT, /* note frame pointer assignment */
194 PIA_NOTE_PROLOGUE_END, /* no more prologue */
197 struct prologue_insns
201 enum prologue_insn_action action;
204 struct prologue_insns prologue_insn_tbl[] =
206 /* Various register move instructions */
207 {0x58000000, 0xf800ffff, PIA_SKIP}, /* or/or.u with immed of 0 */
208 {0xf4005800, 0xfc1fffe0, PIA_SKIP}, /* or rd, r0, rs */
209 {0xf4005800, 0xfc00ffff, PIA_SKIP}, /* or rd, rs, r0 */
211 /* Stack pointer setup: "subu sp, sp, n" where n is a multiple of 8 */
212 {0x67ff0000, 0xffff0007, PIA_NOTE_SP_ADJUSTMENT},
214 /* Frame pointer assignment: "addu r30, r31, n" */
215 {0x63df0000, 0xffff0000, PIA_NOTE_FP_ASSIGNMENT},
217 /* Store to stack instructions; either "st rx, sp, n" or "st.d rx, sp, n" */
218 {0x241f0000, 0xfc1f0000, PIA_NOTE_ST}, /* st rx, sp, n */
219 {0x201f0000, 0xfc1f0000, PIA_NOTE_STD}, /* st.d rs, sp, n */
221 /* Instructions needed for setting up r25 for pic code. */
222 {0x5f200000, 0xffff0000, PIA_SKIP}, /* or.u r25, r0, offset_high */
223 {0xcc000002, 0xffffffff, PIA_SKIP}, /* bsr.n Lab */
224 {0x5b390000, 0xffff0000, PIA_SKIP}, /* or r25, r25, offset_low */
225 {0xf7396001, 0xffffffff, PIA_SKIP}, /* Lab: addu r25, r25, r1 */
227 /* Various branch or jump instructions which have a delay slot -- these
228 do not form part of the prologue, but the instruction in the delay
229 slot might be a store instruction which should be noted. */
230 {0xc4000000, 0xe4000000, PIA_NOTE_PROLOGUE_END},
231 /* br.n, bsr.n, bb0.n, or bb1.n */
232 {0xec000000, 0xfc000000, PIA_NOTE_PROLOGUE_END}, /* bcnd.n */
233 {0xf400c400, 0xfffff7e0, PIA_NOTE_PROLOGUE_END} /* jmp.n or jsr.n */
238 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
239 is not the address of a valid instruction, the address of the next
240 instruction beyond ADDR otherwise. *PWORD1 receives the first word
241 of the instruction. */
243 #define NEXT_PROLOGUE_INSN(addr, lim, pword1) \
244 (((addr) < (lim)) ? next_insn (addr, pword1) : 0)
246 /* Read the m88k instruction at 'memaddr' and return the address of
247 the next instruction after that, or 0 if 'memaddr' is not the
248 address of a valid instruction. The instruction
249 is stored at 'pword1'. */
252 next_insn (CORE_ADDR memaddr, unsigned long *pword1)
254 *pword1 = read_memory_integer (memaddr, BYTES_PER_88K_INSN);
255 return memaddr + BYTES_PER_88K_INSN;
258 /* Read a register from frames called by us (or from the hardware regs). */
261 read_next_frame_reg (struct frame_info *frame, int regno)
263 for (; frame; frame = frame->next)
265 if (regno == SP_REGNUM)
266 return FRAME_FP (frame);
267 else if (frame->fsr->regs[regno])
268 return read_memory_integer (frame->fsr->regs[regno], 4);
270 return read_register (regno);
273 /* Examine the prologue of a function. `ip' points to the first instruction.
274 `limit' is the limit of the prologue (e.g. the addr of the first
275 linenumber, or perhaps the program counter if we're stepping through).
276 `frame_sp' is the stack pointer value in use in this frame.
277 `fsr' is a pointer to a frame_saved_regs structure into which we put
278 info about the registers saved by this frame.
279 `fi' is a struct frame_info pointer; we fill in various fields in it
280 to reflect the offsets of the arg pointer and the locals pointer. */
283 examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit,
284 CORE_ADDR frame_sp, struct frame_saved_regs *fsr,
285 struct frame_info *fi)
287 register CORE_ADDR next_ip;
291 char must_adjust[32]; /* If set, must adjust offsets in fsr */
292 int sp_offset = -1; /* -1 means not set (valid must be mult of 8) */
293 int fp_offset = -1; /* -1 means not set */
295 CORE_ADDR prologue_end = 0;
297 memset (must_adjust, '\0', sizeof (must_adjust));
298 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
302 struct prologue_insns *pip;
304 for (pip = prologue_insn_tbl; (insn & pip->mask) != pip->insn;)
305 if (++pip >= prologue_insn_tbl + sizeof prologue_insn_tbl)
306 goto end_of_prologue_found; /* not a prologue insn */
315 offset = ST_OFFSET (insn);
316 must_adjust[src] = 1;
317 fsr->regs[src++] = offset; /* Will be adjusted later */
318 if (pip->action == PIA_NOTE_STD && src < 32)
321 must_adjust[src] = 1;
322 fsr->regs[src++] = offset;
326 goto end_of_prologue_found;
328 case PIA_NOTE_SP_ADJUSTMENT:
330 sp_offset = -SUBU_OFFSET (insn);
332 goto end_of_prologue_found;
334 case PIA_NOTE_FP_ASSIGNMENT:
336 fp_offset = ADDU_OFFSET (insn);
338 goto end_of_prologue_found;
340 case PIA_NOTE_PROLOGUE_END:
351 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
354 end_of_prologue_found:
359 /* We're done with the prologue. If we don't care about the stack
360 frame itself, just return. (Note that fsr->regs has been trashed,
361 but the one caller who calls with fi==0 passes a dummy there.) */
369 sp_offset original (before any alloca calls) displacement of SP
372 fp_offset displacement from original SP to the FP for this frame
375 fsr->regs[0..31] displacement from original SP to the stack
376 location where reg[0..31] is stored.
378 must_adjust[0..31] set if corresponding offset was set.
380 If alloca has been called between the function prologue and the current
381 IP, then the current SP (frame_sp) will not be the original SP as set by
382 the function prologue. If the current SP is not the original SP, then the
383 compiler will have allocated an FP for this frame, fp_offset will be set,
384 and we can use it to calculate the original SP.
386 Then, we figure out where the arguments and locals are, and relocate the
387 offsets in fsr->regs to absolute addresses. */
391 /* We have a frame pointer, so get it, and base our calc's on it. */
392 frame_fp = (CORE_ADDR) read_next_frame_reg (fi->next, ACTUAL_FP_REGNUM);
393 frame_sp = frame_fp - fp_offset;
397 /* We have no frame pointer, therefore frame_sp is still the same value
398 as set by prologue. But where is the frame itself? */
399 if (must_adjust[SRP_REGNUM])
401 /* Function header saved SRP (r1), the return address. Frame starts
402 4 bytes down from where it was saved. */
403 frame_fp = frame_sp + fsr->regs[SRP_REGNUM] - 4;
404 fi->locals_pointer = frame_fp;
408 /* Function header didn't save SRP (r1), so we are in a leaf fn or
409 are otherwise confused. */
414 /* The locals are relative to the FP (whether it exists as an allocated
415 register, or just as an assumed offset from the SP) */
416 fi->locals_pointer = frame_fp;
418 /* The arguments are just above the SP as it was before we adjusted it
420 fi->args_pointer = frame_sp - sp_offset;
422 /* Now that we know the SP value used by the prologue, we know where
423 it saved all the registers. */
424 for (src = 0; src < 32; src++)
425 if (must_adjust[src])
426 fsr->regs[src] += frame_sp;
428 /* The saved value of the SP is always known. */
430 if (fsr->regs[SP_REGNUM] != 0
431 && fsr->regs[SP_REGNUM] != frame_sp - sp_offset)
432 fprintf_unfiltered (gdb_stderr, "Bad saved SP value %lx != %lx, offset %x!\n",
433 fsr->regs[SP_REGNUM],
434 frame_sp - sp_offset, sp_offset);
436 fsr->regs[SP_REGNUM] = frame_sp - sp_offset;
441 /* Given an ip value corresponding to the start of a function,
442 return the ip of the first instruction after the function
446 m88k_skip_prologue (CORE_ADDR ip)
448 struct frame_saved_regs saved_regs_dummy;
449 struct symtab_and_line sal;
452 sal = find_pc_line (ip, 0);
453 limit = (sal.end) ? sal.end : 0xffffffff;
455 return (examine_prologue (ip, limit, (CORE_ADDR) 0, &saved_regs_dummy,
456 (struct frame_info *) 0));
459 /* Put here the code to store, into a struct frame_saved_regs,
460 the addresses of the saved registers of frame described by FRAME_INFO.
461 This includes special registers such as pc and fp saved in special
462 ways in the stack frame. sp is even more special:
463 the address we return for it IS the sp for the next frame.
465 We cache the result of doing this in the frame_obstack, since it is
469 frame_find_saved_regs (struct frame_info *fi, struct frame_saved_regs *fsr)
471 register struct frame_saved_regs *cache_fsr;
473 struct symtab_and_line sal;
478 cache_fsr = (struct frame_saved_regs *)
479 frame_obstack_alloc (sizeof (struct frame_saved_regs));
480 memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
483 /* Find the start and end of the function prologue. If the PC
484 is in the function prologue, we only consider the part that
485 has executed already. In the case where the PC is not in
486 the function prologue, we set limit to two instructions beyond
487 where the prologue ends in case if any of the prologue instructions
488 were moved into a delay slot of a branch instruction. */
490 ip = get_pc_function_start (fi->pc);
491 sal = find_pc_line (ip, 0);
492 limit = (sal.end && sal.end < fi->pc) ? sal.end + 2 * BYTES_PER_88K_INSN
495 /* This will fill in fields in *fi as well as in cache_fsr. */
496 #ifdef SIGTRAMP_FRAME_FIXUP
497 if (fi->signal_handler_caller)
498 SIGTRAMP_FRAME_FIXUP (fi->frame);
500 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
501 #ifdef SIGTRAMP_SP_FIXUP
502 if (fi->signal_handler_caller && fi->fsr->regs[SP_REGNUM])
503 SIGTRAMP_SP_FIXUP (fi->fsr->regs[SP_REGNUM]);
511 /* Return the address of the locals block for the frame
512 described by FI. Returns 0 if the address is unknown.
513 NOTE! Frame locals are referred to by negative offsets from the
514 argument pointer, so this is the same as frame_args_address(). */
517 frame_locals_address (struct frame_info *fi)
519 struct frame_saved_regs fsr;
521 if (fi->args_pointer) /* Cached value is likely there. */
522 return fi->args_pointer;
524 /* Nope, generate it. */
526 get_frame_saved_regs (fi, &fsr);
528 return fi->args_pointer;
531 /* Return the address of the argument block for the frame
532 described by FI. Returns 0 if the address is unknown. */
535 frame_args_address (struct frame_info *fi)
537 struct frame_saved_regs fsr;
539 if (fi->args_pointer) /* Cached value is likely there. */
540 return fi->args_pointer;
542 /* Nope, generate it. */
544 get_frame_saved_regs (fi, &fsr);
546 return fi->args_pointer;
549 /* Return the saved PC from this frame.
551 If the frame has a memory copy of SRP_REGNUM, use that. If not,
552 just use the register SRP_REGNUM itself. */
555 frame_saved_pc (struct frame_info *frame)
557 return read_next_frame_reg (frame, SRP_REGNUM);
561 #define DUMMY_FRAME_SIZE 192
564 write_word (CORE_ADDR sp, ULONGEST word)
566 register int len = REGISTER_SIZE;
567 char buffer[MAX_REGISTER_RAW_SIZE];
569 store_unsigned_integer (buffer, len, word);
570 write_memory (sp, buffer, len);
574 m88k_push_dummy_frame (void)
576 register CORE_ADDR sp = read_register (SP_REGNUM);
580 sp -= DUMMY_FRAME_SIZE; /* allocate a bunch of space */
582 for (rn = 0, offset = 0; rn <= SP_REGNUM; rn++, offset += 4)
583 write_word (sp + offset, read_register (rn));
585 write_word (sp + offset, read_register (SXIP_REGNUM));
588 write_word (sp + offset, read_register (SNIP_REGNUM));
591 write_word (sp + offset, read_register (SFIP_REGNUM));
594 write_word (sp + offset, read_register (PSR_REGNUM));
597 write_word (sp + offset, read_register (FPSR_REGNUM));
600 write_word (sp + offset, read_register (FPCR_REGNUM));
603 write_register (SP_REGNUM, sp);
604 write_register (ACTUAL_FP_REGNUM, sp);
610 register struct frame_info *frame = get_current_frame ();
612 struct frame_saved_regs fsr;
614 get_frame_saved_regs (frame, &fsr);
616 if (PC_IN_CALL_DUMMY (read_pc (), read_register (SP_REGNUM), frame->frame))
618 /* FIXME: I think get_frame_saved_regs should be handling this so
619 that we can deal with the saved registers properly (e.g. frame
620 1 is a call dummy, the user types "frame 2" and then "print $ps"). */
621 register CORE_ADDR sp = read_register (ACTUAL_FP_REGNUM);
624 for (regnum = 0, offset = 0; regnum <= SP_REGNUM; regnum++, offset += 4)
625 (void) write_register (regnum, read_memory_integer (sp + offset, 4));
627 write_register (SXIP_REGNUM, read_memory_integer (sp + offset, 4));
630 write_register (SNIP_REGNUM, read_memory_integer (sp + offset, 4));
633 write_register (SFIP_REGNUM, read_memory_integer (sp + offset, 4));
636 write_register (PSR_REGNUM, read_memory_integer (sp + offset, 4));
639 write_register (FPSR_REGNUM, read_memory_integer (sp + offset, 4));
642 write_register (FPCR_REGNUM, read_memory_integer (sp + offset, 4));
648 for (regnum = FP_REGNUM; regnum > 0; regnum--)
649 if (fsr.regs[regnum])
650 write_register (regnum,
651 read_memory_integer (fsr.regs[regnum], 4));
652 write_pc (frame_saved_pc (frame));
654 reinit_frame_cache ();
658 _initialize_m88k_tdep (void)
660 tm_print_insn = print_insn_m88k;