* mn10300.igen (OP_F0F4): Need to load contents of register AN0
[platform/upstream/binutils.git] / gdb / i960-tdep.c
1 /* Target-machine dependent code for the Intel 960
2    Copyright 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3    Contributed by Intel Corporation.
4    examine_prologue and other parts contributed by Wind River Systems.
5
6 This file is part of GDB.
7
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.
12
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.
17
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, Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "floatformat.h"
27 #include "target.h"
28 #include "gdbcore.h"
29
30 static CORE_ADDR next_insn PARAMS ((CORE_ADDR memaddr,
31                                     unsigned int *pword1,
32                                     unsigned int *pword2));
33
34 /* gdb960 is always running on a non-960 host.  Check its characteristics.
35    This routine must be called as part of gdb initialization.  */
36
37 static void
38 check_host()
39 {
40         int i;
41
42         static struct typestruct {
43                 int hostsize;           /* Size of type on host         */
44                 int i960size;           /* Size of type on i960         */
45                 char *typename;         /* Name of type, for error msg  */
46         } types[] = {
47                 { sizeof(short),  2, "short" },
48                 { sizeof(int),    4, "int" },
49                 { sizeof(long),   4, "long" },
50                 { sizeof(float),  4, "float" },
51                 { sizeof(double), 8, "double" },
52                 { sizeof(char *), 4, "pointer" },
53         };
54 #define TYPELEN (sizeof(types) / sizeof(struct typestruct))
55
56         /* Make sure that host type sizes are same as i960
57          */
58         for ( i = 0; i < TYPELEN; i++ ){
59                 if ( types[i].hostsize != types[i].i960size ){
60                         printf_unfiltered("sizeof(%s) != %d:  PROCEED AT YOUR OWN RISK!\n",
61                                         types[i].typename, types[i].i960size );
62                 }
63
64         }
65 }
66 \f
67 /* Examine an i960 function prologue, recording the addresses at which
68    registers are saved explicitly by the prologue code, and returning
69    the address of the first instruction after the prologue (but not
70    after the instruction at address LIMIT, as explained below).
71
72    LIMIT places an upper bound on addresses of the instructions to be
73    examined.  If the prologue code scan reaches LIMIT, the scan is
74    aborted and LIMIT is returned.  This is used, when examining the
75    prologue for the current frame, to keep examine_prologue () from
76    claiming that a given register has been saved when in fact the
77    instruction that saves it has not yet been executed.  LIMIT is used
78    at other times to stop the scan when we hit code after the true
79    function prologue (e.g. for the first source line) which might
80    otherwise be mistaken for function prologue.
81
82    The format of the function prologue matched by this routine is
83    derived from examination of the source to gcc960 1.21, particularly
84    the routine i960_function_prologue ().  A "regular expression" for
85    the function prologue is given below:
86
87    (lda LRn, g14
88     mov g14, g[0-7]
89     (mov 0, g14) | (lda 0, g14))?
90
91    (mov[qtl]? g[0-15], r[4-15])*
92    ((addo [1-31], sp, sp) | (lda n(sp), sp))?
93    (st[qtl]? g[0-15], n(fp))*
94
95    (cmpobne 0, g14, LFn
96     mov sp, g14
97     lda 0x30(sp), sp
98     LFn: stq g0, (g14)
99     stq g4, 0x10(g14)
100     stq g8, 0x20(g14))?
101
102    (st g14, n(fp))?
103    (mov g13,r[4-15])?
104 */
105
106 /* Macros for extracting fields from i960 instructions.  */
107
108 #define BITMASK(pos, width) (((0x1 << (width)) - 1) << (pos))
109 #define EXTRACT_FIELD(val, pos, width) ((val) >> (pos) & BITMASK (0, width))
110
111 #define REG_SRC1(insn)    EXTRACT_FIELD (insn, 0, 5)
112 #define REG_SRC2(insn)    EXTRACT_FIELD (insn, 14, 5)
113 #define REG_SRCDST(insn)  EXTRACT_FIELD (insn, 19, 5)
114 #define MEM_SRCDST(insn)  EXTRACT_FIELD (insn, 19, 5)
115 #define MEMA_OFFSET(insn) EXTRACT_FIELD (insn, 0, 12)
116
117 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
118    is not the address of a valid instruction, the address of the next
119    instruction beyond ADDR otherwise.  *PWORD1 receives the first word
120    of the instruction, and (for two-word instructions), *PWORD2 receives
121    the second.  */
122
123 #define NEXT_PROLOGUE_INSN(addr, lim, pword1, pword2) \
124   (((addr) < (lim)) ? next_insn (addr, pword1, pword2) : 0)
125
126 static CORE_ADDR
127 examine_prologue (ip, limit, frame_addr, fsr)
128      register CORE_ADDR ip;
129      register CORE_ADDR limit;
130      CORE_ADDR frame_addr;
131      struct frame_saved_regs *fsr;
132 {
133   register CORE_ADDR next_ip;
134   register int src, dst;
135   register unsigned int *pcode;
136   unsigned int insn1, insn2;
137   int size;
138   int within_leaf_prologue;
139   CORE_ADDR save_addr;
140   static unsigned int varargs_prologue_code [] =
141     {
142        0x3507a00c,      /* cmpobne 0x0, g14, LFn */
143        0x5cf01601,      /* mov sp, g14           */
144        0x8c086030,      /* lda 0x30(sp), sp      */
145        0xb2879000,      /* LFn: stq  g0, (g14)   */
146        0xb2a7a010,      /* stq g4, 0x10(g14)     */
147        0xb2c7a020       /* stq g8, 0x20(g14)     */
148     };
149
150   /* Accept a leaf procedure prologue code fragment if present.
151      Note that ip might point to either the leaf or non-leaf
152      entry point; we look for the non-leaf entry point first:  */
153
154   within_leaf_prologue = 0;
155   if ((next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2))
156       && ((insn1 & 0xfffff000) == 0x8cf00000         /* lda LRx, g14 (MEMA) */
157           || (insn1 & 0xfffffc60) == 0x8cf03000))    /* lda LRx, g14 (MEMB) */
158     {
159       within_leaf_prologue = 1;
160       next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn1, &insn2);
161     }
162
163   /* Now look for the prologue code at a leaf entry point:  */
164
165   if (next_ip
166       && (insn1 & 0xff87ffff) == 0x5c80161e         /* mov g14, gx */
167       && REG_SRCDST (insn1) <= G0_REGNUM + 7)
168     {
169       within_leaf_prologue = 1;
170       if ((next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn1, &insn2))
171           && (insn1 == 0x8cf00000                   /* lda 0, g14 */
172               || insn1 == 0x5cf01e00))              /* mov 0, g14 */
173         {
174           ip = next_ip;
175           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
176           within_leaf_prologue = 0;
177         }
178     }
179
180   /* If something that looks like the beginning of a leaf prologue
181      has been seen, but the remainder of the prologue is missing, bail.
182      We don't know what we've got.  */
183
184   if (within_leaf_prologue)
185     return (ip);
186           
187   /* Accept zero or more instances of "mov[qtl]? gx, ry", where y >= 4.
188      This may cause us to mistake the moving of a register
189      parameter to a local register for the saving of a callee-saved
190      register, but that can't be helped, since with the
191      "-fcall-saved" flag, any register can be made callee-saved.  */
192
193   while (next_ip
194          && (insn1 & 0xfc802fb0) == 0x5c000610
195          && (dst = REG_SRCDST (insn1)) >= (R0_REGNUM + 4))
196     {
197       src = REG_SRC1 (insn1);
198       size = EXTRACT_FIELD (insn1, 24, 2) + 1;
199       save_addr = frame_addr + ((dst - R0_REGNUM) * 4);
200       while (size--)
201         {
202           fsr->regs[src++] = save_addr;
203           save_addr += 4;
204         }
205       ip = next_ip;
206       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
207     }
208
209   /* Accept an optional "addo n, sp, sp" or "lda n(sp), sp".  */
210
211   if (next_ip &&
212       ((insn1 & 0xffffffe0) == 0x59084800       /* addo n, sp, sp */
213        || (insn1 & 0xfffff000) == 0x8c086000    /* lda n(sp), sp (MEMA) */
214        || (insn1 & 0xfffffc60) == 0x8c087400))  /* lda n(sp), sp (MEMB) */
215     {
216       ip = next_ip;
217       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
218     }
219
220   /* Accept zero or more instances of "st[qtl]? gx, n(fp)".  
221      This may cause us to mistake the copying of a register
222      parameter to the frame for the saving of a callee-saved
223      register, but that can't be helped, since with the
224      "-fcall-saved" flag, any register can be made callee-saved.
225      We can, however, refuse to accept a save of register g14,
226      since that is matched explicitly below.  */
227
228   while (next_ip &&
229          ((insn1 & 0xf787f000) == 0x9287e000      /* stl? gx, n(fp) (MEMA) */
230           || (insn1 & 0xf787fc60) == 0x9287f400   /* stl? gx, n(fp) (MEMB) */
231           || (insn1 & 0xef87f000) == 0xa287e000   /* st[tq] gx, n(fp) (MEMA) */
232           || (insn1 & 0xef87fc60) == 0xa287f400)  /* st[tq] gx, n(fp) (MEMB) */
233          && ((src = MEM_SRCDST (insn1)) != G14_REGNUM))
234     {
235       save_addr = frame_addr + ((insn1 & BITMASK (12, 1))
236                                 ? insn2 : MEMA_OFFSET (insn1));
237       size = (insn1 & BITMASK (29, 1)) ? ((insn1 & BITMASK (28, 1)) ? 4 : 3)
238                                        : ((insn1 & BITMASK (27, 1)) ? 2 : 1);
239       while (size--)
240         {
241           fsr->regs[src++] = save_addr;
242           save_addr += 4;
243         }
244       ip = next_ip;
245       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
246     }
247
248   /* Accept the varargs prologue code if present.  */
249
250   size = sizeof (varargs_prologue_code) / sizeof (int);
251   pcode = varargs_prologue_code;
252   while (size-- && next_ip && *pcode++ == insn1)
253     {
254       ip = next_ip;
255       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
256     }
257
258   /* Accept an optional "st g14, n(fp)".  */
259
260   if (next_ip &&
261       ((insn1 & 0xfffff000) == 0x92f7e000        /* st g14, n(fp) (MEMA) */
262        || (insn1 & 0xfffffc60) == 0x92f7f400))   /* st g14, n(fp) (MEMB) */
263     {
264       fsr->regs[G14_REGNUM] = frame_addr + ((insn1 & BITMASK (12, 1))
265                                             ? insn2 : MEMA_OFFSET (insn1));
266       ip = next_ip;
267       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
268     }
269
270   /* Accept zero or one instance of "mov g13, ry", where y >= 4.
271      This is saving the address where a struct should be returned.  */
272
273   if (next_ip
274       && (insn1 & 0xff802fbf) == 0x5c00061d
275       && (dst = REG_SRCDST (insn1)) >= (R0_REGNUM + 4))
276     {
277       save_addr = frame_addr + ((dst - R0_REGNUM) * 4);
278       fsr->regs[G0_REGNUM+13] = save_addr;
279       ip = next_ip;
280 #if 0  /* We'll need this once there is a subsequent instruction examined. */
281       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn1, &insn2);
282 #endif
283     }
284
285   return (ip);
286 }
287
288 /* Given an ip value corresponding to the start of a function,
289    return the ip of the first instruction after the function 
290    prologue.  */
291
292 CORE_ADDR
293 skip_prologue (ip)
294      CORE_ADDR (ip);
295 {
296   struct frame_saved_regs saved_regs_dummy;
297   struct symtab_and_line sal;
298   CORE_ADDR limit;
299
300   sal = find_pc_line (ip, 0);
301   limit = (sal.end) ? sal.end : 0xffffffff;
302
303   return (examine_prologue (ip, limit, (CORE_ADDR) 0, &saved_regs_dummy));
304 }
305
306 /* Put here the code to store, into a struct frame_saved_regs,
307    the addresses of the saved registers of frame described by FRAME_INFO.
308    This includes special registers such as pc and fp saved in special
309    ways in the stack frame.  sp is even more special:
310    the address we return for it IS the sp for the next frame.
311
312    We cache the result of doing this in the frame_cache_obstack, since
313    it is fairly expensive.  */
314
315 void
316 frame_find_saved_regs (fi, fsr)
317      struct frame_info *fi;
318      struct frame_saved_regs *fsr;
319 {
320   register CORE_ADDR next_addr;
321   register CORE_ADDR *saved_regs;
322   register int regnum;
323   register struct frame_saved_regs *cache_fsr;
324   extern struct obstack frame_cache_obstack;
325   CORE_ADDR ip;
326   struct symtab_and_line sal;
327   CORE_ADDR limit;
328
329   if (!fi->fsr)
330     {
331       cache_fsr = (struct frame_saved_regs *)
332                   obstack_alloc (&frame_cache_obstack,
333                                  sizeof (struct frame_saved_regs));
334       memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
335       fi->fsr = cache_fsr;
336
337       /* Find the start and end of the function prologue.  If the PC
338          is in the function prologue, we only consider the part that
339          has executed already.  */
340          
341       ip = get_pc_function_start (fi->pc);
342       sal = find_pc_line (ip, 0);
343       limit = (sal.end && sal.end < fi->pc) ? sal.end: fi->pc;
344
345       examine_prologue (ip, limit, fi->frame, cache_fsr);
346
347       /* Record the addresses at which the local registers are saved.
348          Strictly speaking, we should only do this for non-leaf procedures,
349          but no one will ever look at these values if it is a leaf procedure,
350          since local registers are always caller-saved.  */
351
352       next_addr = (CORE_ADDR) fi->frame;
353       saved_regs = cache_fsr->regs;
354       for (regnum = R0_REGNUM; regnum <= R15_REGNUM; regnum++)
355         {
356           *saved_regs++ = next_addr;
357           next_addr += 4;
358         }
359
360       cache_fsr->regs[FP_REGNUM] = cache_fsr->regs[PFP_REGNUM];
361     }
362
363   *fsr = *fi->fsr;
364
365   /* Fetch the value of the sp from memory every time, since it
366      is conceivable that it has changed since the cache was flushed.  
367      This unfortunately undoes much of the savings from caching the 
368      saved register values.  I suggest adding an argument to 
369      get_frame_saved_regs () specifying the register number we're
370      interested in (or -1 for all registers).  This would be passed
371      through to FRAME_FIND_SAVED_REGS (), permitting more efficient
372      computation of saved register addresses (e.g., on the i960,
373      we don't have to examine the prologue to find local registers). 
374         -- markf@wrs.com 
375      FIXME, we don't need to refetch this, since the cache is cleared
376      every time the child process is restarted.  If GDB itself
377      modifies SP, it has to clear the cache by hand (does it?).  -gnu */
378
379   fsr->regs[SP_REGNUM] = read_memory_integer (fsr->regs[SP_REGNUM], 4);
380 }
381
382 /* Return the address of the argument block for the frame
383    described by FI.  Returns 0 if the address is unknown.  */
384
385 CORE_ADDR
386 frame_args_address (fi, must_be_correct)
387      struct frame_info *fi;
388 {
389   struct frame_saved_regs fsr;
390   CORE_ADDR ap;
391
392   /* If g14 was saved in the frame by the function prologue code, return
393      the saved value.  If the frame is current and we are being sloppy,
394      return the value of g14.  Otherwise, return zero.  */
395
396   get_frame_saved_regs (fi, &fsr);
397   if (fsr.regs[G14_REGNUM])
398     ap = read_memory_integer (fsr.regs[G14_REGNUM],4);
399   else
400     {
401       if (must_be_correct)
402         return 0;                       /* Don't cache this result */
403       if (get_next_frame (fi))
404         ap = 0;
405       else
406         ap = read_register (G14_REGNUM);
407       if (ap == 0)
408         ap = fi->frame;
409     }
410   fi->arg_pointer = ap;         /* Cache it for next time */
411   return ap;
412 }
413
414 /* Return the address of the return struct for the frame
415    described by FI.  Returns 0 if the address is unknown.  */
416
417 CORE_ADDR
418 frame_struct_result_address (fi)
419      struct frame_info *fi;
420 {
421   struct frame_saved_regs fsr;
422   CORE_ADDR ap;
423
424   /* If the frame is non-current, check to see if g14 was saved in the
425      frame by the function prologue code; return the saved value if so,
426      zero otherwise.  If the frame is current, return the value of g14.
427
428      FIXME, shouldn't this use the saved value as long as we are past
429      the function prologue, and only use the current value if we have
430      no saved value and are at TOS?   -- gnu@cygnus.com */
431
432   if (get_next_frame (fi))
433     {
434       get_frame_saved_regs (fi, &fsr);
435       if (fsr.regs[G13_REGNUM])
436         ap = read_memory_integer (fsr.regs[G13_REGNUM],4);
437       else
438         ap = 0;
439     }
440   else
441     ap = read_register (G13_REGNUM);
442
443   return ap;
444 }
445
446 /* Return address to which the currently executing leafproc will return,
447    or 0 if ip is not in a leafproc (or if we can't tell if it is).
448   
449    Do this by finding the starting address of the routine in which ip lies.
450    If the instruction there is "mov g14, gx" (where x is in [0,7]), this
451    is a leafproc and the return address is in register gx.  Well, this is
452    true unless the return address points at a RET instruction in the current
453    procedure, which indicates that we have a 'dual entry' routine that
454    has been entered through the CALL entry point.  */
455
456 CORE_ADDR
457 leafproc_return (ip)
458      CORE_ADDR ip;      /* ip from currently executing function */
459 {
460   register struct minimal_symbol *msymbol;
461   char *p;
462   int dst;
463   unsigned int insn1, insn2;
464   CORE_ADDR return_addr;
465
466   if ((msymbol = lookup_minimal_symbol_by_pc (ip)) != NULL)
467     {
468       if ((p = strchr(SYMBOL_NAME (msymbol), '.')) && STREQ (p, ".lf"))
469         {
470           if (next_insn (SYMBOL_VALUE_ADDRESS (msymbol), &insn1, &insn2)
471               && (insn1 & 0xff87ffff) == 0x5c80161e       /* mov g14, gx */
472               && (dst = REG_SRCDST (insn1)) <= G0_REGNUM + 7)
473             {
474               /* Get the return address.  If the "mov g14, gx" 
475                  instruction hasn't been executed yet, read
476                  the return address from g14; otherwise, read it
477                  from the register into which g14 was moved.  */
478
479               return_addr =
480                   read_register ((ip == SYMBOL_VALUE_ADDRESS (msymbol))
481                                            ? G14_REGNUM : dst);
482
483               /* We know we are in a leaf procedure, but we don't know
484                  whether the caller actually did a "bal" to the ".lf"
485                  entry point, or a normal "call" to the non-leaf entry
486                  point one instruction before.  In the latter case, the
487                  return address will be the address of a "ret"
488                  instruction within the procedure itself.  We test for
489                  this below.  */
490
491               if (!next_insn (return_addr, &insn1, &insn2)
492                   || (insn1 & 0xff000000) != 0xa000000   /* ret */
493                   || lookup_minimal_symbol_by_pc (return_addr) != msymbol)
494                 return (return_addr);
495             }
496         }
497     }
498   
499   return (0);
500 }
501
502 /* Immediately after a function call, return the saved pc.
503    Can't go through the frames for this because on some machines
504    the new frame is not set up until the new function executes
505    some instructions. 
506    On the i960, the frame *is* set up immediately after the call,
507    unless the function is a leaf procedure.  */
508
509 CORE_ADDR
510 saved_pc_after_call (frame)
511      struct frame_info *frame;
512 {
513   CORE_ADDR saved_pc;
514
515   saved_pc = leafproc_return (get_frame_pc (frame));
516   if (!saved_pc)
517     saved_pc = FRAME_SAVED_PC (frame);
518
519   return saved_pc;
520 }
521
522 /* Discard from the stack the innermost frame,
523    restoring all saved registers.  */
524
525 pop_frame ()
526 {
527   register struct frame_info *current_fi, *prev_fi;
528   register int i;
529   CORE_ADDR save_addr;
530   CORE_ADDR leaf_return_addr;
531   struct frame_saved_regs fsr;
532   char local_regs_buf[16 * 4];
533
534   current_fi = get_current_frame ();
535
536   /* First, undo what the hardware does when we return.
537      If this is a non-leaf procedure, restore local registers from
538      the save area in the calling frame.  Otherwise, load the return
539      address obtained from leafproc_return () into the rip.  */
540
541   leaf_return_addr = leafproc_return (current_fi->pc);
542   if (!leaf_return_addr)
543     {
544       /* Non-leaf procedure.  Restore local registers, incl IP.  */
545       prev_fi = get_prev_frame (current_fi);
546       read_memory (prev_fi->frame, local_regs_buf, sizeof (local_regs_buf));
547       write_register_bytes (REGISTER_BYTE (R0_REGNUM), local_regs_buf, 
548                             sizeof (local_regs_buf));
549
550       /* Restore frame pointer.  */
551       write_register (FP_REGNUM, prev_fi->frame);
552     }
553   else
554     {
555       /* Leaf procedure.  Just restore the return address into the IP.  */
556       write_register (RIP_REGNUM, leaf_return_addr);
557     }
558
559   /* Now restore any global regs that the current function had saved. */
560   get_frame_saved_regs (current_fi, &fsr);
561   for (i = G0_REGNUM; i < G14_REGNUM; i++)
562     {
563       if (save_addr = fsr.regs[i])
564         write_register (i, read_memory_integer (save_addr, 4));
565     }
566
567   /* Flush the frame cache, create a frame for the new innermost frame,
568      and make it the current frame.  */
569
570   flush_cached_frames ();
571 }
572
573 /* Given a 960 stop code (fault or trace), return the signal which
574    corresponds.  */
575
576 enum target_signal
577 i960_fault_to_signal (fault)
578     int fault;
579 {
580   switch (fault)
581     {
582     case 0: return TARGET_SIGNAL_BUS; /* parallel fault */
583     case 1: return TARGET_SIGNAL_UNKNOWN;
584     case 2: return TARGET_SIGNAL_ILL; /* operation fault */
585     case 3: return TARGET_SIGNAL_FPE; /* arithmetic fault */
586     case 4: return TARGET_SIGNAL_FPE; /* floating point fault */
587
588       /* constraint fault.  This appears not to distinguish between
589          a range constraint fault (which should be SIGFPE) and a privileged
590          fault (which should be SIGILL).  */
591     case 5: return TARGET_SIGNAL_ILL;
592
593     case 6: return TARGET_SIGNAL_SEGV; /* virtual memory fault */
594
595       /* protection fault.  This is for an out-of-range argument to
596          "calls".  I guess it also could be SIGILL. */
597     case 7: return TARGET_SIGNAL_SEGV;
598
599     case 8: return TARGET_SIGNAL_BUS; /* machine fault */
600     case 9: return TARGET_SIGNAL_BUS; /* structural fault */
601     case 0xa: return TARGET_SIGNAL_ILL; /* type fault */
602     case 0xb: return TARGET_SIGNAL_UNKNOWN; /* reserved fault */
603     case 0xc: return TARGET_SIGNAL_BUS; /* process fault */
604     case 0xd: return TARGET_SIGNAL_SEGV; /* descriptor fault */
605     case 0xe: return TARGET_SIGNAL_BUS; /* event fault */
606     case 0xf: return TARGET_SIGNAL_UNKNOWN; /* reserved fault */
607     case 0x10: return TARGET_SIGNAL_TRAP; /* single-step trace */
608     case 0x11: return TARGET_SIGNAL_TRAP; /* branch trace */
609     case 0x12: return TARGET_SIGNAL_TRAP; /* call trace */
610     case 0x13: return TARGET_SIGNAL_TRAP; /* return trace */
611     case 0x14: return TARGET_SIGNAL_TRAP; /* pre-return trace */
612     case 0x15: return TARGET_SIGNAL_TRAP; /* supervisor call trace */
613     case 0x16: return TARGET_SIGNAL_TRAP; /* breakpoint trace */
614     default: return TARGET_SIGNAL_UNKNOWN;
615     }
616 }
617
618 /****************************************/
619 /* MEM format                           */
620 /****************************************/
621
622 struct tabent {
623         char    *name;
624         char    numops;
625 };
626
627 static int                              /* returns instruction length: 4 or 8 */
628 mem( memaddr, word1, word2, noprint )
629     unsigned long memaddr;
630     unsigned long word1, word2;
631     int noprint;                /* If TRUE, return instruction length, but
632                                    don't output any text.  */
633 {
634         int i, j;
635         int len;
636         int mode;
637         int offset;
638         const char *reg1, *reg2, *reg3;
639
640         /* This lookup table is too sparse to make it worth typing in, but not
641          * so large as to make a sparse array necessary.  We allocate the
642          * table at runtime, initialize all entries to empty, and copy the
643          * real ones in from an initialization table.
644          *
645          * NOTE: In this table, the meaning of 'numops' is:
646          *       1: single operand
647          *       2: 2 operands, load instruction
648          *      -2: 2 operands, store instruction
649          */
650         static struct tabent *mem_tab = NULL;
651 /* Opcodes of 0x8X, 9X, aX, bX, and cX must be in the table.  */
652 #define MEM_MIN 0x80
653 #define MEM_MAX 0xcf
654 #define MEM_SIZ ((MEM_MAX-MEM_MIN+1) * sizeof(struct tabent))
655
656         static struct { int opcode; char *name; char numops; } mem_init[] = {
657                 0x80,   "ldob",  2,
658                 0x82,   "stob", -2,
659                 0x84,   "bx",    1,
660                 0x85,   "balx",  2,
661                 0x86,   "callx", 1,
662                 0x88,   "ldos",  2,
663                 0x8a,   "stos", -2,
664                 0x8c,   "lda",   2,
665                 0x90,   "ld",    2,
666                 0x92,   "st",   -2,
667                 0x98,   "ldl",   2,
668                 0x9a,   "stl",  -2,
669                 0xa0,   "ldt",   2,
670                 0xa2,   "stt",  -2,
671                 0xb0,   "ldq",   2,
672                 0xb2,   "stq",  -2,
673                 0xc0,   "ldib",  2,
674                 0xc2,   "stib", -2,
675                 0xc8,   "ldis",  2,
676                 0xca,   "stis", -2,
677                 0,      NULL,   0
678         };
679
680         if ( mem_tab == NULL ){
681                 mem_tab = (struct tabent *) xmalloc( MEM_SIZ );
682                 memset( mem_tab, '\0', MEM_SIZ );
683                 for ( i = 0; mem_init[i].opcode != 0; i++ ){
684                         j = mem_init[i].opcode - MEM_MIN;
685                         mem_tab[j].name = mem_init[i].name;
686                         mem_tab[j].numops = mem_init[i].numops;
687                 }
688         }
689
690         i = ((word1 >> 24) & 0xff) - MEM_MIN;
691         mode = (word1 >> 10) & 0xf;
692
693         if ( (mem_tab[i].name != NULL)          /* Valid instruction */
694         &&   ((mode == 5) || (mode >=12)) ){    /* With 32-bit displacement */
695                 len = 8;
696         } else {
697                 len = 4;
698         }
699
700         if ( noprint ){
701                 return len;
702         }
703         abort ();
704 }
705
706 /* Read the i960 instruction at 'memaddr' and return the address of 
707    the next instruction after that, or 0 if 'memaddr' is not the
708    address of a valid instruction.  The first word of the instruction
709    is stored at 'pword1', and the second word, if any, is stored at
710    'pword2'.  */
711
712 static CORE_ADDR
713 next_insn (memaddr, pword1, pword2)
714      unsigned int *pword1, *pword2;
715      CORE_ADDR memaddr;
716 {
717   int len;
718   char buf[8];
719
720   /* Read the two (potential) words of the instruction at once,
721      to eliminate the overhead of two calls to read_memory ().
722      FIXME: Loses if the first one is readable but the second is not
723      (e.g. last word of the segment).  */
724
725   read_memory (memaddr, buf, 8);
726   *pword1 = extract_unsigned_integer (buf, 4);
727   *pword2 = extract_unsigned_integer (buf + 4, 4);
728
729   /* Divide instruction set into classes based on high 4 bits of opcode*/
730
731   switch ((*pword1 >> 28) & 0xf)
732     {
733     case 0x0:
734     case 0x1:   /* ctrl */
735
736     case 0x2:
737     case 0x3:   /* cobr */
738
739     case 0x5:
740     case 0x6:
741     case 0x7:   /* reg */
742       len = 4;
743       break;
744
745     case 0x8:
746     case 0x9:
747     case 0xa:
748     case 0xb:
749     case 0xc:
750       len = mem (memaddr, *pword1, *pword2, 1);
751       break;
752
753     default:    /* invalid instruction */
754       len = 0;
755       break;
756     }
757
758   if (len)
759     return memaddr + len;
760   else
761     return 0;
762 }
763
764 /* 'start_frame' is a variable in the MON960 runtime startup routine
765    that contains the frame pointer of the 'start' routine (the routine
766    that calls 'main').  By reading its contents out of remote memory,
767    we can tell where the frame chain ends:  backtraces should halt before
768    they display this frame.  */
769
770 int
771 mon960_frame_chain_valid (chain, curframe)
772     unsigned int chain;
773     struct frame_info *curframe;
774 {
775         struct symbol *sym;
776         struct minimal_symbol *msymbol;
777
778         /* crtmon960.o is an assembler module that is assumed to be linked
779          * first in an i80960 executable.  It contains the true entry point;
780          * it performs startup up initialization and then calls 'main'.
781          *
782          * 'sf' is the name of a variable in crtmon960.o that is set
783          *      during startup to the address of the first frame.
784          *
785          * 'a' is the address of that variable in 80960 memory.
786          */
787         static char sf[] = "start_frame";
788         CORE_ADDR a;
789
790
791         chain &= ~0x3f; /* Zero low 6 bits because previous frame pointers
792                            contain return status info in them.  */
793         if ( chain == 0 ){
794                 return 0;
795         }
796
797         sym = lookup_symbol(sf, 0, VAR_NAMESPACE, (int *)NULL, 
798                                   (struct symtab **)NULL);
799         if ( sym != 0 ){
800                 a = SYMBOL_VALUE (sym);
801         } else {
802                 msymbol = lookup_minimal_symbol (sf, NULL, NULL);
803                 if (msymbol == NULL)
804                         return 0;
805                 a = SYMBOL_VALUE_ADDRESS (msymbol);
806         }
807
808         return ( chain != read_memory_integer(a,4) );
809 }
810
811 void
812 _initialize_i960_tdep ()
813 {
814   check_host ();
815
816   tm_print_insn = print_insn_i960;
817 }