1 /* Target-machine dependent code for the Intel 960
2 Copyright (C) 1991 Free Software Foundation, Inc.
3 Contributed by Intel Corporation.
4 examine_prologue and other parts contributed by Wind River Systems.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* Miscellaneous i80960-dependent routines.
23 Most are called from macros defined in "tm-i960.h". */
32 #include "ieee-float.h"
34 /* Structure of i960 extended floating point format. */
36 const struct ext_format ext_format_i960 = {
37 /* tot sbyte smask expbyte manbyte */
38 12, 9, 0x80, 9,8, 4,0, /* i960 */
41 /* gdb960 is always running on a non-960 host. Check its characteristics.
42 This routine must be called as part of gdb initialization. */
49 static struct typestruct {
50 int hostsize; /* Size of type on host */
51 int i960size; /* Size of type on i960 */
52 char *typename; /* Name of type, for error msg */
54 { sizeof(short), 2, "short" },
55 { sizeof(int), 4, "int" },
56 { sizeof(long), 4, "long" },
57 { sizeof(float), 4, "float" },
58 { sizeof(double), 8, "double" },
59 { sizeof(char *), 4, "pointer" },
61 #define TYPELEN (sizeof(types) / sizeof(struct typestruct))
63 /* Make sure that host type sizes are same as i960
65 for ( i = 0; i < TYPELEN; i++ ){
66 if ( types[i].hostsize != types[i].i960size ){
67 printf("sizeof(%s) != %d: PROCEED AT YOUR OWN RISK!\n",
68 types[i].typename, types[i].i960size );
74 /* Examine an i960 function prologue, recording the addresses at which
75 registers are saved explicitly by the prologue code, and returning
76 the address of the first instruction after the prologue (but not
77 after the instruction at address LIMIT, as explained below).
79 LIMIT places an upper bound on addresses of the instructions to be
80 examined. If the prologue code scan reaches LIMIT, the scan is
81 aborted and LIMIT is returned. This is used, when examining the
82 prologue for the current frame, to keep examine_prologue () from
83 claiming that a given register has been saved when in fact the
84 instruction that saves it has not yet been executed. LIMIT is used
85 at other times to stop the scan when we hit code after the true
86 function prologue (e.g. for the first source line) which might
87 otherwise be mistaken for function prologue.
89 The format of the function prologue matched by this routine is
90 derived from examination of the source to gcc960 1.21, particularly
91 the routine i960_function_prologue (). A "regular expression" for
92 the function prologue is given below:
96 (mov 0, g14) | (lda 0, g14))?
98 (mov[qtl]? g[0-15], r[4-15])*
99 ((addo [1-31], sp, sp) | (lda n(sp), sp))?
100 (st[qtl]? g[0-15], n(fp))*
113 /* Macros for extracting fields from i960 instructions. */
115 #define BITMASK(pos, width) (((0x1 << (width)) - 1) << (pos))
116 #define EXTRACT_FIELD(val, pos, width) ((val) >> (pos) & BITMASK (0, width))
118 #define REG_SRC1(insn) EXTRACT_FIELD (insn, 0, 5)
119 #define REG_SRC2(insn) EXTRACT_FIELD (insn, 14, 5)
120 #define REG_SRCDST(insn) EXTRACT_FIELD (insn, 19, 5)
121 #define MEM_SRCDST(insn) EXTRACT_FIELD (insn, 19, 5)
122 #define MEMA_OFFSET(insn) EXTRACT_FIELD (insn, 0, 12)
124 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
125 is not the address of a valid instruction, the address of the next
126 instruction beyond ADDR otherwise. *PWORD1 receives the first word
127 of the instruction, and (for two-word instructions), *PWORD2 receives
130 #define NEXT_PROLOGUE_INSN(addr, lim, pword1, pword2) \
131 (((addr) < (lim)) ? next_insn (addr, pword1, pword2) : 0)
134 examine_prologue (ip, limit, frame_addr, fsr)
135 register CORE_ADDR ip;
136 register CORE_ADDR limit;
137 FRAME_ADDR frame_addr;
138 struct frame_saved_regs *fsr;
140 register CORE_ADDR next_ip;
141 register int src, dst;
142 register unsigned int *pcode;
143 unsigned int insn1, insn2;
145 int within_leaf_prologue;
147 static unsigned int varargs_prologue_code [] =
149 0x3507a00c, /* cmpobne 0x0, g14, LFn */
150 0x5cf01601, /* mov sp, g14 */
151 0x8c086030, /* lda 0x30(sp), sp */
152 0xb2879000, /* LFn: stq g0, (g14) */
153 0xb2a7a010, /* stq g4, 0x10(g14) */
154 0xb2c7a020 /* stq g8, 0x20(g14) */
157 /* Accept a leaf procedure prologue code fragment if present.
158 Note that ip might point to either the leaf or non-leaf
159 entry point; we look for the non-leaf entry point first: */
161 within_leaf_prologue = 0;
162 if ((next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2))
163 && ((insn1 & 0xfffff000) == 0x8cf00000 /* lda LRx, g14 (MEMA) */
164 || (insn1 & 0xfffffc60) == 0x8cf03000)) /* lda LRx, g14 (MEMB) */
166 within_leaf_prologue = 1;
167 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn1, &insn2);
170 /* Now look for the prologue code at a leaf entry point: */
173 && (insn1 & 0xff87ffff) == 0x5c80161e /* mov g14, gx */
174 && REG_SRCDST (insn1) <= G0_REGNUM + 7)
176 within_leaf_prologue = 1;
177 if ((next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn1, &insn2))
178 && (insn1 == 0x8cf00000 /* lda 0, g14 */
179 || insn1 == 0x5cf01e00)) /* mov 0, g14 */
182 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
183 within_leaf_prologue = 0;
187 /* If something that looks like the beginning of a leaf prologue
188 has been seen, but the remainder of the prologue is missing, bail.
189 We don't know what we've got. */
191 if (within_leaf_prologue)
194 /* Accept zero or more instances of "mov[qtl]? gx, ry", where y >= 4.
195 This may cause us to mistake the moving of a register
196 parameter to a local register for the saving of a callee-saved
197 register, but that can't be helped, since with the
198 "-fcall-saved" flag, any register can be made callee-saved. */
201 && (insn1 & 0xfc802fb0) == 0x5c000610
202 && (dst = REG_SRCDST (insn1)) >= (R0_REGNUM + 4))
204 src = REG_SRC1 (insn1);
205 size = EXTRACT_FIELD (insn1, 24, 2) + 1;
206 save_addr = frame_addr + ((dst - R0_REGNUM) * 4);
209 fsr->regs[src++] = save_addr;
213 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
216 /* Accept an optional "addo n, sp, sp" or "lda n(sp), sp". */
219 ((insn1 & 0xffffffe0) == 0x59084800 /* addo n, sp, sp */
220 || (insn1 & 0xfffff000) == 0x8c086000 /* lda n(sp), sp (MEMA) */
221 || (insn1 & 0xfffffc60) == 0x8c087400)) /* lda n(sp), sp (MEMB) */
224 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
227 /* Accept zero or more instances of "st[qtl]? gx, n(fp)".
228 This may cause us to mistake the copying of a register
229 parameter to the frame for the saving of a callee-saved
230 register, but that can't be helped, since with the
231 "-fcall-saved" flag, any register can be made callee-saved.
232 We can, however, refuse to accept a save of register g14,
233 since that is matched explicitly below. */
236 ((insn1 & 0xf787f000) == 0x9287e000 /* stl? gx, n(fp) (MEMA) */
237 || (insn1 & 0xf787fc60) == 0x9287f400 /* stl? gx, n(fp) (MEMB) */
238 || (insn1 & 0xef87f000) == 0xa287e000 /* st[tq] gx, n(fp) (MEMA) */
239 || (insn1 & 0xef87fc60) == 0xa287f400) /* st[tq] gx, n(fp) (MEMB) */
240 && ((src = MEM_SRCDST (insn1)) != G14_REGNUM))
242 save_addr = frame_addr + ((insn1 & BITMASK (12, 1))
243 ? insn2 : MEMA_OFFSET (insn1));
244 size = (insn1 & BITMASK (29, 1)) ? ((insn1 & BITMASK (28, 1)) ? 4 : 3)
245 : ((insn1 & BITMASK (27, 1)) ? 2 : 1);
248 fsr->regs[src++] = save_addr;
252 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
255 /* Accept the varargs prologue code if present. */
257 size = sizeof (varargs_prologue_code) / sizeof (int);
258 pcode = varargs_prologue_code;
259 while (size-- && next_ip && *pcode++ == insn1)
262 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
265 /* Accept an optional "st g14, n(fp)". */
268 ((insn1 & 0xfffff000) == 0x92f7e000 /* st g14, n(fp) (MEMA) */
269 || (insn1 & 0xfffffc60) == 0x92f7f400)) /* st g14, n(fp) (MEMB) */
271 fsr->regs[G14_REGNUM] = frame_addr + ((insn1 & BITMASK (12, 1))
272 ? insn2 : MEMA_OFFSET (insn1));
274 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
277 /* Accept zero or one instance of "mov g13, ry", where y >= 4.
278 This is saving the address where a struct should be returned. */
281 && (insn1 & 0xff802fbf) == 0x5c00061d
282 && (dst = REG_SRCDST (insn1)) >= (R0_REGNUM + 4))
284 save_addr = frame_addr + ((dst - R0_REGNUM) * 4);
285 fsr->regs[G0_REGNUM+13] = save_addr;
287 #if 0 /* We'll need this once there is a subsequent instruction examined. */
288 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
295 /* Given an ip value corresponding to the start of a function,
296 return the ip of the first instruction after the function
303 struct frame_saved_regs saved_regs_dummy;
304 struct symtab_and_line sal;
307 sal = find_pc_line (ip, 0);
308 limit = (sal.end) ? sal.end : 0xffffffff;
310 return (examine_prologue (ip, limit, (FRAME_ADDR) 0, &saved_regs_dummy));
313 /* Put here the code to store, into a struct frame_saved_regs,
314 the addresses of the saved registers of frame described by FRAME_INFO.
315 This includes special registers such as pc and fp saved in special
316 ways in the stack frame. sp is even more special:
317 the address we return for it IS the sp for the next frame.
319 We cache the result of doing this in the frame_cache_obstack, since
320 it is fairly expensive. */
323 frame_find_saved_regs (fi, fsr)
324 struct frame_info *fi;
325 struct frame_saved_regs *fsr;
327 register CORE_ADDR next_addr;
328 register CORE_ADDR *saved_regs;
330 register struct frame_saved_regs *cache_fsr;
331 extern struct obstack frame_cache_obstack;
333 struct symtab_and_line sal;
338 cache_fsr = (struct frame_saved_regs *)
339 obstack_alloc (&frame_cache_obstack,
340 sizeof (struct frame_saved_regs));
341 bzero (cache_fsr, sizeof (struct frame_saved_regs));
344 /* Find the start and end of the function prologue. If the PC
345 is in the function prologue, we only consider the part that
346 has executed already. */
348 ip = get_pc_function_start (fi->pc);
349 sal = find_pc_line (ip, 0);
350 limit = (sal.end && sal.end < fi->pc) ? sal.end: fi->pc;
352 examine_prologue (ip, limit, fi->frame, cache_fsr);
354 /* Record the addresses at which the local registers are saved.
355 Strictly speaking, we should only do this for non-leaf procedures,
356 but no one will ever look at these values if it is a leaf procedure,
357 since local registers are always caller-saved. */
359 next_addr = (CORE_ADDR) fi->frame;
360 saved_regs = cache_fsr->regs;
361 for (regnum = R0_REGNUM; regnum <= R15_REGNUM; regnum++)
363 *saved_regs++ = next_addr;
367 cache_fsr->regs[FP_REGNUM] = cache_fsr->regs[PFP_REGNUM];
372 /* Fetch the value of the sp from memory every time, since it
373 is conceivable that it has changed since the cache was flushed.
374 This unfortunately undoes much of the savings from caching the
375 saved register values. I suggest adding an argument to
376 get_frame_saved_regs () specifying the register number we're
377 interested in (or -1 for all registers). This would be passed
378 through to FRAME_FIND_SAVED_REGS (), permitting more efficient
379 computation of saved register addresses (e.g., on the i960,
380 we don't have to examine the prologue to find local registers).
382 FIXME, we don't need to refetch this, since the cache is cleared
383 every time the child process is restarted. If GDB itself
384 modifies SP, it has to clear the cache by hand (does it?). -gnu */
386 fsr->regs[SP_REGNUM] = read_memory_integer (fsr->regs[SP_REGNUM], 4);
389 /* Return the address of the argument block for the frame
390 described by FI. Returns 0 if the address is unknown. */
393 frame_args_address (fi, must_be_correct)
394 struct frame_info *fi;
396 register FRAME frame;
397 struct frame_saved_regs fsr;
400 /* If g14 was saved in the frame by the function prologue code, return
401 the saved value. If the frame is current and we are being sloppy,
402 return the value of g14. Otherwise, return zero. */
404 frame = FRAME_INFO_ID (fi);
405 get_frame_saved_regs (fi, &fsr);
406 if (fsr.regs[G14_REGNUM])
407 ap = read_memory_integer (fsr.regs[G14_REGNUM],4);
410 return 0; /* Don't cache this result */
411 if (get_next_frame (frame))
414 ap = read_register (G14_REGNUM);
416 fi->arg_pointer = ap; /* Cache it for next time */
420 /* Return the address of the return struct for the frame
421 described by FI. Returns 0 if the address is unknown. */
424 frame_struct_result_address (fi)
425 struct frame_info *fi;
427 register FRAME frame;
428 struct frame_saved_regs fsr;
431 /* If the frame is non-current, check to see if g14 was saved in the
432 frame by the function prologue code; return the saved value if so,
433 zero otherwise. If the frame is current, return the value of g14.
435 FIXME, shouldn't this use the saved value as long as we are past
436 the function prologue, and only use the current value if we have
439 frame = FRAME_INFO_ID (fi);
440 if (get_next_frame (frame)) {
441 get_frame_saved_regs (fi, &fsr);
442 if (fsr.regs[G13_REGNUM])
443 ap = read_memory_integer (fsr.regs[G13_REGNUM],4);
447 ap = read_register (G13_REGNUM);
452 /* Return address to which the currently executing leafproc will return,
453 or 0 if ip is not in a leafproc (or if we can't tell if it is).
455 Do this by finding the starting address of the routine in which ip lies.
456 If the instruction there is "mov g14, gx" (where x is in [0,7]), this
457 is a leafproc and the return address is in register gx. Well, this is
458 true unless the return address points at a RET instruction in the current
459 procedure, which indicates that we have a 'dual entry' routine that
460 has been entered through the CALL entry point. */
464 CORE_ADDR ip; /* ip from currently executing function */
467 register struct misc_function *mf;
470 unsigned int insn1, insn2;
471 CORE_ADDR return_addr;
474 if ((i = find_pc_misc_function (ip)) >= 0)
476 mf = &misc_function_vector[i];
477 if ((p = index (mf->name, '.')) && !strcmp (p, ".lf"))
479 if (next_insn (mf->address, &insn1, &insn2)
480 && (insn1 & 0xff87ffff) == 0x5c80161e /* mov g14, gx */
481 && (dst = REG_SRCDST (insn1)) <= G0_REGNUM + 7)
483 /* Get the return address. If the "mov g14, gx"
484 instruction hasn't been executed yet, read
485 the return address from g14; otherwise, read it
486 from the register into which g14 was moved. */
488 return_addr = read_register ((ip == mf->address)
491 /* We know we are in a leaf procedure, but we don't know
492 whether the caller actually did a "bal" to the ".lf"
493 entry point, or a normal "call" to the non-leaf entry
494 point one instruction before. In the latter case, the
495 return address will be the address of a "ret"
496 instruction within the procedure itself. We test for
499 if (!next_insn (return_addr, &insn1, &insn2)
500 || (insn1 & 0xff000000) != 0xa000000 /* ret */
501 || find_pc_misc_function (return_addr) != i)
502 return (return_addr);
510 /* Immediately after a function call, return the saved pc.
511 Can't go through the frames for this because on some machines
512 the new frame is not set up until the new function executes
514 On the i960, the frame *is* set up immediately after the call,
515 unless the function is a leaf procedure. */
518 saved_pc_after_call (frame)
522 CORE_ADDR get_frame_pc ();
524 saved_pc = leafproc_return (get_frame_pc (frame));
526 saved_pc = FRAME_SAVED_PC (frame);
531 /* Discard from the stack the innermost frame,
532 restoring all saved registers. */
536 register struct frame_info *current_fi, *prev_fi;
539 CORE_ADDR leaf_return_addr;
540 struct frame_saved_regs fsr;
541 char local_regs_buf[16 * 4];
543 current_fi = get_frame_info (get_current_frame ());
545 /* First, undo what the hardware does when we return.
546 If this is a non-leaf procedure, restore local registers from
547 the save area in the calling frame. Otherwise, load the return
548 address obtained from leafproc_return () into the rip. */
550 leaf_return_addr = leafproc_return (current_fi->pc);
551 if (!leaf_return_addr)
553 /* Non-leaf procedure. Restore local registers, incl IP. */
554 prev_fi = get_frame_info (get_prev_frame (FRAME_INFO_ID (current_fi)));
555 read_memory (prev_fi->frame, local_regs_buf, sizeof (local_regs_buf));
556 write_register_bytes (REGISTER_BYTE (R0_REGNUM), local_regs_buf,
557 sizeof (local_regs_buf));
559 /* Restore frame pointer. */
560 write_register (FP_REGNUM, prev_fi->frame);
564 /* Leaf procedure. Just restore the return address into the IP. */
565 write_register (RIP_REGNUM, leaf_return_addr);
568 /* Now restore any global regs that the current function had saved. */
569 get_frame_saved_regs (current_fi, &fsr);
570 for (i = G0_REGNUM; i < G14_REGNUM; i++)
572 if (save_addr = fsr.regs[i])
573 write_register (i, read_memory_integer (save_addr, 4));
576 /* Flush the frame cache, create a frame for the new innermost frame,
577 and make it the current frame. */
579 flush_cached_frames ();
580 set_current_frame (create_new_frame (read_register (FP_REGNUM), read_pc ()));
583 /* Print out text describing a "signal number" with which the i80960 halted.
585 See the file "fault.c" in the nindy monitor source code for a list
589 print_fault( siggnal )
590 int siggnal; /* Signal number, as returned by target_wait() */
592 static char unknown[] = "Unknown fault or trace";
593 static char *sigmsgs[] = {
595 "parallel fault", /* 0x00 */
597 "operation fault", /* 0x02 */
598 "arithmetic fault", /* 0x03 */
599 "floating point fault", /* 0x04 */
600 "constraint fault", /* 0x05 */
601 "virtual memory fault", /* 0x06 */
602 "protection fault", /* 0x07 */
603 "machine fault", /* 0x08 */
604 "structural fault", /* 0x09 */
605 "type fault", /* 0x0a */
606 "reserved (0xb) fault", /* 0x0b */
607 "process fault", /* 0x0c */
608 "descriptor fault", /* 0x0d */
609 "event fault", /* 0x0e */
610 "reserved (0xf) fault", /* 0x0f */
613 "single-step trace", /* 0x10 */
614 "branch trace", /* 0x11 */
615 "call trace", /* 0x12 */
616 "return trace", /* 0x13 */
617 "pre-return trace", /* 0x14 */
618 "supervisor call trace",/* 0x15 */
619 "breakpoint trace", /* 0x16 */
621 # define NUMMSGS ((int)( sizeof(sigmsgs) / sizeof(sigmsgs[0]) ))
623 if (siggnal < NSIG) {
624 printf ("\nProgram received signal %d, %s\n",
626 sys_siglist[siggnal]);
628 /* The various target_wait()s bias the 80960 "signal number"
629 by adding NSIG to it, so it won't get confused with any
630 of the Unix signals elsewhere in GDB. We need to
631 "unbias" it before using it. */
634 printf("Program stopped for reason #%d: %s.\n", siggnal,
635 (siggnal < NUMMSGS && siggnal >= 0)?
636 sigmsgs[siggnal] : unknown );
640 /* Initialization stub */
642 _initialize_i960_tdep ()