Upload Tizen:Base source
[external/gdb.git] / gdb / m88k-tdep.c
1 /* Target-dependent code for the Motorola 88000 series.
2
3    Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include "dis-asm.h"
24 #include "frame.h"
25 #include "frame-base.h"
26 #include "frame-unwind.h"
27 #include "gdbcore.h"
28 #include "gdbtypes.h"
29 #include "regcache.h"
30 #include "regset.h"
31 #include "symtab.h"
32 #include "trad-frame.h"
33 #include "value.h"
34
35 #include "gdb_assert.h"
36 #include "gdb_string.h"
37
38 #include "m88k-tdep.h"
39
40 /* Fetch the instruction at PC.  */
41
42 static unsigned long
43 m88k_fetch_instruction (CORE_ADDR pc, enum bfd_endian byte_order)
44 {
45   return read_memory_unsigned_integer (pc, 4, byte_order);
46 }
47
48 /* Register information.  */
49
50 /* Return the name of register REGNUM.  */
51
52 static const char *
53 m88k_register_name (struct gdbarch *gdbarch, int regnum)
54 {
55   static char *register_names[] =
56   {
57     "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
58     "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
59     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
60     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
61     "epsr", "fpsr", "fpcr", "sxip", "snip", "sfip"
62   };
63
64   if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
65     return register_names[regnum];
66
67   return NULL;
68 }
69
70 /* Return the GDB type object for the "standard" data type of data in
71    register REGNUM. */
72
73 static struct type *
74 m88k_register_type (struct gdbarch *gdbarch, int regnum)
75 {
76   /* SXIP, SNIP, SFIP and R1 contain code addresses.  */
77   if ((regnum >= M88K_SXIP_REGNUM && regnum <= M88K_SFIP_REGNUM)
78       || regnum == M88K_R1_REGNUM)
79     return builtin_type (gdbarch)->builtin_func_ptr;
80
81   /* R30 and R31 typically contains data addresses.  */
82   if (regnum == M88K_R30_REGNUM || regnum == M88K_R31_REGNUM)
83     return builtin_type (gdbarch)->builtin_data_ptr;
84
85   return builtin_type (gdbarch)->builtin_int32;
86 }
87 \f
88
89 static CORE_ADDR
90 m88k_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
91 {
92   /* All instructures are 4-byte aligned.  The lower 2 bits of SXIP,
93      SNIP and SFIP are used for special purposes: bit 0 is the
94      exception bit and bit 1 is the valid bit.  */
95   return addr & ~0x3;
96 }
97
98 /* Use the program counter to determine the contents and size of a
99    breakpoint instruction.  Return a pointer to a string of bytes that
100    encode a breakpoint instruction, store the length of the string in
101    *LEN and optionally adjust *PC to point to the correct memory
102    location for inserting the breakpoint.  */
103    
104 static const gdb_byte *
105 m88k_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
106 {
107   /* tb 0,r0,511 */
108   static gdb_byte break_insn[] = { 0xf0, 0x00, 0xd1, 0xff };
109
110   *len = sizeof (break_insn);
111   return break_insn;
112 }
113
114 static CORE_ADDR
115 m88k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
116 {
117   CORE_ADDR pc;
118
119   pc = frame_unwind_register_unsigned (next_frame, M88K_SXIP_REGNUM);
120   return m88k_addr_bits_remove (gdbarch, pc);
121 }
122
123 static void
124 m88k_write_pc (struct regcache *regcache, CORE_ADDR pc)
125 {
126   /* According to the MC88100 RISC Microprocessor User's Manual,
127      section 6.4.3.1.2:
128
129      "... can be made to return to a particular instruction by placing
130      a valid instruction address in the SNIP and the next sequential
131      instruction address in the SFIP (with V bits set and E bits
132      clear).  The rte resumes execution at the instruction pointed to
133      by the SNIP, then the SFIP."
134
135      The E bit is the least significant bit (bit 0).  The V (valid)
136      bit is bit 1.  This is why we logical or 2 into the values we are
137      writing below.  It turns out that SXIP plays no role when
138      returning from an exception so nothing special has to be done
139      with it.  We could even (presumably) give it a totally bogus
140      value.  */
141
142   regcache_cooked_write_unsigned (regcache, M88K_SXIP_REGNUM, pc);
143   regcache_cooked_write_unsigned (regcache, M88K_SNIP_REGNUM, pc | 2);
144   regcache_cooked_write_unsigned (regcache, M88K_SFIP_REGNUM, (pc + 4) | 2);
145 }
146 \f
147
148 /* The functions on this page are intended to be used to classify
149    function arguments.  */
150
151 /* Check whether TYPE is "Integral or Pointer".  */
152
153 static int
154 m88k_integral_or_pointer_p (const struct type *type)
155 {
156   switch (TYPE_CODE (type))
157     {
158     case TYPE_CODE_INT:
159     case TYPE_CODE_BOOL:
160     case TYPE_CODE_CHAR:
161     case TYPE_CODE_ENUM:
162     case TYPE_CODE_RANGE:
163       {
164         /* We have byte, half-word, word and extended-word/doubleword
165            integral types.  */
166         int len = TYPE_LENGTH (type);
167         return (len == 1 || len == 2 || len == 4 || len == 8);
168       }
169       return 1;
170     case TYPE_CODE_PTR:
171     case TYPE_CODE_REF:
172       {
173         /* Allow only 32-bit pointers.  */
174         return (TYPE_LENGTH (type) == 4);
175       }
176       return 1;
177     default:
178       break;
179     }
180
181   return 0;
182 }
183
184 /* Check whether TYPE is "Floating".  */
185
186 static int
187 m88k_floating_p (const struct type *type)
188 {
189   switch (TYPE_CODE (type))
190     {
191     case TYPE_CODE_FLT:
192       {
193         int len = TYPE_LENGTH (type);
194         return (len == 4 || len == 8);
195       }
196     default:
197       break;
198     }
199
200   return 0;
201 }
202
203 /* Check whether TYPE is "Structure or Union".  */
204
205 static int
206 m88k_structure_or_union_p (const struct type *type)
207 {
208   switch (TYPE_CODE (type))
209     {
210     case TYPE_CODE_STRUCT:
211     case TYPE_CODE_UNION:
212       return 1;
213     default:
214       break;
215     }
216
217   return 0;
218 }
219
220 /* Check whether TYPE has 8-byte alignment.  */
221
222 static int
223 m88k_8_byte_align_p (struct type *type)
224 {
225   if (m88k_structure_or_union_p (type))
226     {
227       int i;
228
229       for (i = 0; i < TYPE_NFIELDS (type); i++)
230         {
231           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
232
233           if (m88k_8_byte_align_p (subtype))
234             return 1;
235         }
236     }
237
238   if (m88k_integral_or_pointer_p (type) || m88k_floating_p (type))
239     return (TYPE_LENGTH (type) == 8);
240
241   return 0;
242 }
243
244 /* Check whether TYPE can be passed in a register.  */
245
246 static int
247 m88k_in_register_p (struct type *type)
248 {
249   if (m88k_integral_or_pointer_p (type) || m88k_floating_p (type))
250     return 1;
251
252   if (m88k_structure_or_union_p (type) && TYPE_LENGTH (type) == 4)
253     return 1;
254
255   return 0;
256 }
257
258 static CORE_ADDR
259 m88k_store_arguments (struct regcache *regcache, int nargs,
260                       struct value **args, CORE_ADDR sp)
261 {
262   struct gdbarch *gdbarch = get_regcache_arch (regcache);
263   int num_register_words = 0;
264   int num_stack_words = 0;
265   int i;
266
267   for (i = 0; i < nargs; i++)
268     {
269       struct type *type = value_type (args[i]);
270       int len = TYPE_LENGTH (type);
271
272       if (m88k_integral_or_pointer_p (type) && len < 4)
273         {
274           args[i] = value_cast (builtin_type (gdbarch)->builtin_int32,
275                                 args[i]);
276           type = value_type (args[i]);
277           len = TYPE_LENGTH (type);
278         }
279
280       if (m88k_in_register_p (type))
281         {
282           int num_words = 0;
283
284           if (num_register_words % 2 == 1 && m88k_8_byte_align_p (type))
285             num_words++;
286
287           num_words += ((len + 3) / 4);
288           if (num_register_words + num_words <= 8)
289             {
290               num_register_words += num_words;
291               continue;
292             }
293
294           /* We've run out of available registers.  Pass the argument
295              on the stack.  */
296         }
297
298       if (num_stack_words % 2 == 1 && m88k_8_byte_align_p (type))
299         num_stack_words++;
300
301       num_stack_words += ((len + 3) / 4);
302     }
303
304   /* Allocate stack space.  */
305   sp = align_down (sp - 32 - num_stack_words * 4, 16);
306   num_stack_words = num_register_words = 0;
307
308   for (i = 0; i < nargs; i++)
309     {
310       const bfd_byte *valbuf = value_contents (args[i]);
311       struct type *type = value_type (args[i]);
312       int len = TYPE_LENGTH (type);
313       int stack_word = num_stack_words;
314
315       if (m88k_in_register_p (type))
316         {
317           int register_word = num_register_words;
318
319           if (register_word % 2 == 1 && m88k_8_byte_align_p (type))
320             register_word++;
321
322           gdb_assert (len == 4 || len == 8);
323
324           if (register_word + len / 8 < 8)
325             {
326               int regnum = M88K_R2_REGNUM + register_word;
327
328               regcache_raw_write (regcache, regnum, valbuf);
329               if (len > 4)
330                 regcache_raw_write (regcache, regnum + 1, valbuf + 4);
331
332               num_register_words = (register_word + len / 4);
333               continue;
334             }
335         }
336
337       if (stack_word % 2 == -1 && m88k_8_byte_align_p (type))
338         stack_word++;
339
340       write_memory (sp + stack_word * 4, valbuf, len);
341       num_stack_words = (stack_word + (len + 3) / 4);
342     }
343
344   return sp;
345 }
346
347 static CORE_ADDR
348 m88k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
349                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
350                       struct value **args, CORE_ADDR sp, int struct_return,
351                       CORE_ADDR struct_addr)
352 {
353   /* Set up the function arguments.  */
354   sp = m88k_store_arguments (regcache, nargs, args, sp);
355   gdb_assert (sp % 16 == 0);
356
357   /* Store return value address.  */
358   if (struct_return)
359     regcache_raw_write_unsigned (regcache, M88K_R12_REGNUM, struct_addr);
360
361   /* Store the stack pointer and return address in the appropriate
362      registers.  */
363   regcache_raw_write_unsigned (regcache, M88K_R31_REGNUM, sp);
364   regcache_raw_write_unsigned (regcache, M88K_R1_REGNUM, bp_addr);
365
366   /* Return the stack pointer.  */
367   return sp;
368 }
369
370 static struct frame_id
371 m88k_dummy_id (struct gdbarch *arch, struct frame_info *this_frame)
372 {
373   CORE_ADDR sp;
374
375   sp = get_frame_register_unsigned (this_frame, M88K_R31_REGNUM);
376   return frame_id_build (sp, get_frame_pc (this_frame));
377 }
378 \f
379
380 /* Determine, for architecture GDBARCH, how a return value of TYPE
381    should be returned.  If it is supposed to be returned in registers,
382    and READBUF is non-zero, read the appropriate value from REGCACHE,
383    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
384    from WRITEBUF into REGCACHE.  */
385
386 static enum return_value_convention
387 m88k_return_value (struct gdbarch *gdbarch, struct type *func_type,
388                    struct type *type, struct regcache *regcache,
389                    gdb_byte *readbuf, const gdb_byte *writebuf)
390 {
391   int len = TYPE_LENGTH (type);
392   gdb_byte buf[8];
393
394   if (!m88k_integral_or_pointer_p (type) && !m88k_floating_p (type))
395     return RETURN_VALUE_STRUCT_CONVENTION;
396
397   if (readbuf)
398     {
399       /* Read the contents of R2 and (if necessary) R3.  */
400       regcache_cooked_read (regcache, M88K_R2_REGNUM, buf);
401       if (len > 4)
402         {
403           regcache_cooked_read (regcache, M88K_R3_REGNUM, buf + 4);
404           gdb_assert (len == 8);
405           memcpy (readbuf, buf, len);
406         }
407       else
408         {
409           /* Just stripping off any unused bytes should preserve the
410              signed-ness just fine.  */
411           memcpy (readbuf, buf + 4 - len, len);
412         }
413     }
414
415   if (writebuf)
416     {
417       /* Read the contents to R2 and (if necessary) R3.  */
418       if (len > 4)
419         {
420           gdb_assert (len == 8);
421           memcpy (buf, writebuf, 8);
422           regcache_cooked_write (regcache, M88K_R3_REGNUM, buf + 4);
423         }
424       else
425         {
426           /* ??? Do we need to do any sign-extension here?  */
427           memcpy (buf + 4 - len, writebuf, len);
428         }
429       regcache_cooked_write (regcache, M88K_R2_REGNUM, buf);
430     }
431
432   return RETURN_VALUE_REGISTER_CONVENTION;
433 }
434 \f
435 /* Default frame unwinder.  */
436
437 struct m88k_frame_cache
438 {
439   /* Base address.  */
440   CORE_ADDR base;
441   CORE_ADDR pc;
442
443   int sp_offset;
444   int fp_offset;
445
446   /* Table of saved registers.  */
447   struct trad_frame_saved_reg *saved_regs;
448 };
449
450 /* Prologue analysis.  */
451
452 /* Macros for extracting fields from instructions.  */
453
454 #define BITMASK(pos, width) (((0x1 << (width)) - 1) << (pos))
455 #define EXTRACT_FIELD(val, pos, width) ((val) >> (pos) & BITMASK (0, width))
456 #define SUBU_OFFSET(x)  ((unsigned)(x & 0xFFFF))
457 #define ST_OFFSET(x)    ((unsigned)((x) & 0xFFFF))
458 #define ST_SRC(x)       EXTRACT_FIELD ((x), 21, 5)
459 #define ADDU_OFFSET(x)  ((unsigned)(x & 0xFFFF))
460
461 /* Possible actions to be taken by the prologue analyzer for the
462    instructions it encounters.  */
463
464 enum m88k_prologue_insn_action
465 {
466   M88K_PIA_SKIP,                /* Ignore.  */
467   M88K_PIA_NOTE_ST,             /* Note register store.  */
468   M88K_PIA_NOTE_STD,            /* Note register pair store.  */
469   M88K_PIA_NOTE_SP_ADJUSTMENT,  /* Note stack pointer adjustment.  */
470   M88K_PIA_NOTE_FP_ASSIGNMENT,  /* Note frame pointer assignment.  */
471   M88K_PIA_NOTE_BRANCH,         /* Note branch.  */
472   M88K_PIA_NOTE_PROLOGUE_END    /* Note end of prologue.  */
473 };
474
475 /* Table of instructions that may comprise a function prologue.  */
476
477 struct m88k_prologue_insn
478 {
479   unsigned long insn;
480   unsigned long mask;
481   enum m88k_prologue_insn_action action;
482 };
483
484 struct m88k_prologue_insn m88k_prologue_insn_table[] =
485 {
486   /* Various register move instructions.  */
487   { 0x58000000, 0xf800ffff, M88K_PIA_SKIP },     /* or/or.u with immed of 0 */
488   { 0xf4005800, 0xfc1fffe0, M88K_PIA_SKIP },     /* or rd,r0,rs */
489   { 0xf4005800, 0xfc00ffff, M88K_PIA_SKIP },     /* or rd,rs,r0 */
490
491   /* Various other instructions.  */
492   { 0x58000000, 0xf8000000, M88K_PIA_SKIP },     /* or/or.u */
493
494   /* Stack pointer setup: "subu sp,sp,n" where n is a multiple of 8.  */
495   { 0x67ff0000, 0xffff0007, M88K_PIA_NOTE_SP_ADJUSTMENT },
496
497   /* Frame pointer assignment: "addu r30,r31,n".  */
498   { 0x63df0000, 0xffff0000, M88K_PIA_NOTE_FP_ASSIGNMENT },
499
500   /* Store to stack instructions; either "st rx,sp,n" or "st.d rx,sp,n".  */
501   { 0x241f0000, 0xfc1f0000, M88K_PIA_NOTE_ST },  /* st rx,sp,n */
502   { 0x201f0000, 0xfc1f0000, M88K_PIA_NOTE_STD }, /* st.d rs,sp,n */
503
504   /* Instructions needed for setting up r25 for pic code.  */
505   { 0x5f200000, 0xffff0000, M88K_PIA_SKIP },     /* or.u r25,r0,offset_high */
506   { 0xcc000002, 0xffffffff, M88K_PIA_SKIP },     /* bsr.n Lab */
507   { 0x5b390000, 0xffff0000, M88K_PIA_SKIP },     /* or r25,r25,offset_low */
508   { 0xf7396001, 0xffffffff, M88K_PIA_SKIP },     /* Lab: addu r25,r25,r1 */
509
510   /* Various branch or jump instructions which have a delay slot --
511      these do not form part of the prologue, but the instruction in
512      the delay slot might be a store instruction which should be
513      noted.  */
514   { 0xc4000000, 0xe4000000, M88K_PIA_NOTE_BRANCH },
515                                       /* br.n, bsr.n, bb0.n, or bb1.n */
516   { 0xec000000, 0xfc000000, M88K_PIA_NOTE_BRANCH }, /* bcnd.n */
517   { 0xf400c400, 0xfffff7e0, M88K_PIA_NOTE_BRANCH }, /* jmp.n or jsr.n */
518
519   /* Catch all.  Ends prologue analysis.  */
520   { 0x00000000, 0x00000000, M88K_PIA_NOTE_PROLOGUE_END }
521 };
522
523 /* Do a full analysis of the function prologue at PC and update CACHE
524    accordingly.  Bail out early if LIMIT is reached.  Return the
525    address where the analysis stopped.  If LIMIT points beyond the
526    function prologue, the return address should be the end of the
527    prologue.  */
528
529 static CORE_ADDR
530 m88k_analyze_prologue (struct gdbarch *gdbarch,
531                        CORE_ADDR pc, CORE_ADDR limit,
532                        struct m88k_frame_cache *cache)
533 {
534   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
535   CORE_ADDR end = limit;
536
537   /* Provide a dummy cache if necessary.  */
538   if (cache == NULL)
539     {
540       size_t sizeof_saved_regs =
541         (M88K_R31_REGNUM + 1) * sizeof (struct trad_frame_saved_reg);
542
543       cache = alloca (sizeof (struct m88k_frame_cache));
544       cache->saved_regs = alloca (sizeof_saved_regs);
545
546       /* We only initialize the members we care about.  */
547       cache->saved_regs[M88K_R1_REGNUM].addr = -1;
548       cache->fp_offset = -1;
549     }
550
551   while (pc < limit)
552     {
553       struct m88k_prologue_insn *pi = m88k_prologue_insn_table;
554       unsigned long insn = m88k_fetch_instruction (pc, byte_order);
555
556       while ((insn & pi->mask) != pi->insn)
557         pi++;
558
559       switch (pi->action)
560         {
561         case M88K_PIA_SKIP:
562           /* If we have a frame pointer, and R1 has been saved,
563              consider this instruction as not being part of the
564              prologue.  */
565           if (cache->fp_offset != -1
566               && cache->saved_regs[M88K_R1_REGNUM].addr != -1)
567             return min (pc, end);
568           break;
569
570         case M88K_PIA_NOTE_ST:
571         case M88K_PIA_NOTE_STD:
572           /* If no frame has been allocated, the stores aren't part of
573              the prologue.  */
574           if (cache->sp_offset == 0)
575             return min (pc, end);
576
577           /* Record location of saved registers.  */
578           {
579             int regnum = ST_SRC (insn) + M88K_R0_REGNUM;
580             ULONGEST offset = ST_OFFSET (insn);
581
582             cache->saved_regs[regnum].addr = offset;
583             if (pi->action == M88K_PIA_NOTE_STD && regnum < M88K_R31_REGNUM)
584               cache->saved_regs[regnum + 1].addr = offset + 4;
585           }
586           break;
587
588         case M88K_PIA_NOTE_SP_ADJUSTMENT:
589           /* A second stack pointer adjustment isn't part of the
590              prologue.  */
591           if (cache->sp_offset != 0)
592             return min (pc, end);
593
594           /* Store stack pointer adjustment.  */
595           cache->sp_offset = -SUBU_OFFSET (insn);
596           break;
597
598         case M88K_PIA_NOTE_FP_ASSIGNMENT:
599           /* A second frame pointer assignment isn't part of the
600              prologue.  */
601           if (cache->fp_offset != -1)
602             return min (pc, end);
603
604           /* Record frame pointer assignment.  */
605           cache->fp_offset = ADDU_OFFSET (insn);
606           break;
607
608         case M88K_PIA_NOTE_BRANCH:
609           /* The branch instruction isn't part of the prologue, but
610              the instruction in the delay slot might be.  Limit the
611              prologue analysis to the delay slot and record the branch
612              instruction as the end of the prologue.  */
613           limit = min (limit, pc + 2 * M88K_INSN_SIZE);
614           end = pc;
615           break;
616
617         case M88K_PIA_NOTE_PROLOGUE_END:
618           return min (pc, end);
619         }
620
621       pc += M88K_INSN_SIZE;
622     }
623
624   return end;
625 }
626
627 /* An upper limit to the size of the prologue.  */
628 const int m88k_max_prologue_size = 128 * M88K_INSN_SIZE;
629
630 /* Return the address of first real instruction of the function
631    starting at PC.  */
632
633 static CORE_ADDR
634 m88k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
635 {
636   struct symtab_and_line sal;
637   CORE_ADDR func_start, func_end;
638
639   /* This is the preferred method, find the end of the prologue by
640      using the debugging information.  */
641   if (find_pc_partial_function (pc, NULL, &func_start, &func_end))
642     {
643       sal = find_pc_line (func_start, 0);
644
645       if (sal.end < func_end && pc <= sal.end)
646         return sal.end;
647     }
648
649   return m88k_analyze_prologue (gdbarch, pc, pc + m88k_max_prologue_size,
650                                 NULL);
651 }
652
653 static struct m88k_frame_cache *
654 m88k_frame_cache (struct frame_info *this_frame, void **this_cache)
655 {
656   struct gdbarch *gdbarch = get_frame_arch (this_frame);
657   struct m88k_frame_cache *cache;
658   CORE_ADDR frame_sp;
659
660   if (*this_cache)
661     return *this_cache;
662
663   cache = FRAME_OBSTACK_ZALLOC (struct m88k_frame_cache);
664   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
665   cache->fp_offset = -1;
666
667   cache->pc = get_frame_func (this_frame);
668   if (cache->pc != 0)
669     m88k_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
670                            cache);
671
672   /* Calculate the stack pointer used in the prologue.  */
673   if (cache->fp_offset != -1)
674     {
675       CORE_ADDR fp;
676
677       fp = get_frame_register_unsigned (this_frame, M88K_R30_REGNUM);
678       frame_sp = fp - cache->fp_offset;
679     }
680   else
681     {
682       /* If we know where the return address is saved, we can take a
683          solid guess at what the frame pointer should be.  */
684       if (cache->saved_regs[M88K_R1_REGNUM].addr != -1)
685         cache->fp_offset = cache->saved_regs[M88K_R1_REGNUM].addr - 4;
686       frame_sp = get_frame_register_unsigned (this_frame, M88K_R31_REGNUM);
687     }
688
689   /* Now that we know the stack pointer, adjust the location of the
690      saved registers.  */
691   {
692     int regnum;
693
694     for (regnum = M88K_R0_REGNUM; regnum < M88K_R31_REGNUM; regnum ++)
695       if (cache->saved_regs[regnum].addr != -1)
696         cache->saved_regs[regnum].addr += frame_sp;
697   }
698
699   /* Calculate the frame's base.  */
700   cache->base = frame_sp - cache->sp_offset;
701   trad_frame_set_value (cache->saved_regs, M88K_R31_REGNUM, cache->base);
702
703   /* Identify SXIP with the return address in R1.  */
704   cache->saved_regs[M88K_SXIP_REGNUM] = cache->saved_regs[M88K_R1_REGNUM];
705
706   *this_cache = cache;
707   return cache;
708 }
709
710 static void
711 m88k_frame_this_id (struct frame_info *this_frame, void **this_cache,
712                     struct frame_id *this_id)
713 {
714   struct m88k_frame_cache *cache = m88k_frame_cache (this_frame, this_cache);
715
716   /* This marks the outermost frame.  */
717   if (cache->base == 0)
718     return;
719
720   (*this_id) = frame_id_build (cache->base, cache->pc);
721 }
722
723 static struct value *
724 m88k_frame_prev_register (struct frame_info *this_frame,
725                           void **this_cache, int regnum)
726 {
727   struct m88k_frame_cache *cache = m88k_frame_cache (this_frame, this_cache);
728
729   if (regnum == M88K_SNIP_REGNUM || regnum == M88K_SFIP_REGNUM)
730     {
731       struct value *value;
732       CORE_ADDR pc;
733
734       value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
735                                             M88K_SXIP_REGNUM);
736       pc = value_as_long (value);
737       release_value (value);
738       value_free (value);
739
740       if (regnum == M88K_SFIP_REGNUM)
741         pc += 4;
742
743       return frame_unwind_got_constant (this_frame, regnum, pc + 4);
744     }
745
746   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
747 }
748
749 static const struct frame_unwind m88k_frame_unwind =
750 {
751   NORMAL_FRAME,
752   m88k_frame_this_id,
753   m88k_frame_prev_register,
754   NULL,
755   default_frame_sniffer
756 };
757 \f
758
759 static CORE_ADDR
760 m88k_frame_base_address (struct frame_info *this_frame, void **this_cache)
761 {
762   struct m88k_frame_cache *cache = m88k_frame_cache (this_frame, this_cache);
763
764   if (cache->fp_offset != -1)
765     return cache->base + cache->sp_offset + cache->fp_offset;
766
767   return 0;
768 }
769
770 static const struct frame_base m88k_frame_base =
771 {
772   &m88k_frame_unwind,
773   m88k_frame_base_address,
774   m88k_frame_base_address,
775   m88k_frame_base_address
776 };
777 \f
778
779 /* Core file support.  */
780
781 /* Supply register REGNUM from the buffer specified by GREGS and LEN
782    in the general-purpose register set REGSET to register cache
783    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
784
785 static void
786 m88k_supply_gregset (const struct regset *regset,
787                      struct regcache *regcache,
788                      int regnum, const void *gregs, size_t len)
789 {
790   const gdb_byte *regs = gregs;
791   int i;
792
793   for (i = 0; i < M88K_NUM_REGS; i++)
794     {
795       if (regnum == i || regnum == -1)
796         regcache_raw_supply (regcache, i, regs + i * 4);
797     }
798 }
799
800 /* Motorola 88000 register set.  */
801
802 static struct regset m88k_gregset =
803 {
804   NULL,
805   m88k_supply_gregset
806 };
807
808 /* Return the appropriate register set for the core section identified
809    by SECT_NAME and SECT_SIZE.  */
810
811 static const struct regset *
812 m88k_regset_from_core_section (struct gdbarch *gdbarch,
813                                const char *sect_name, size_t sect_size)
814 {
815   if (strcmp (sect_name, ".reg") == 0 && sect_size >= M88K_NUM_REGS * 4)
816     return &m88k_gregset;
817
818   return NULL;
819 }
820 \f
821
822 static struct gdbarch *
823 m88k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
824 {
825   struct gdbarch *gdbarch;
826
827   /* If there is already a candidate, use it.  */
828   arches = gdbarch_list_lookup_by_info (arches, &info);
829   if (arches != NULL)
830     return arches->gdbarch;
831
832   /* Allocate space for the new architecture.  */
833   gdbarch = gdbarch_alloc (&info, NULL);
834
835   /* There is no real `long double'.  */
836   set_gdbarch_long_double_bit (gdbarch, 64);
837   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
838
839   set_gdbarch_num_regs (gdbarch, M88K_NUM_REGS);
840   set_gdbarch_register_name (gdbarch, m88k_register_name);
841   set_gdbarch_register_type (gdbarch, m88k_register_type);
842
843   /* Register numbers of various important registers.  */
844   set_gdbarch_sp_regnum (gdbarch, M88K_R31_REGNUM);
845   set_gdbarch_pc_regnum (gdbarch, M88K_SXIP_REGNUM);
846
847   /* Core file support.  */
848   set_gdbarch_regset_from_core_section
849     (gdbarch, m88k_regset_from_core_section);
850
851   set_gdbarch_print_insn (gdbarch, print_insn_m88k);
852
853   set_gdbarch_skip_prologue (gdbarch, m88k_skip_prologue);
854
855   /* Stack grows downward.  */
856   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
857
858   /* Call dummy code.  */
859   set_gdbarch_push_dummy_call (gdbarch, m88k_push_dummy_call);
860   set_gdbarch_dummy_id (gdbarch, m88k_dummy_id);
861
862   /* Return value info */
863   set_gdbarch_return_value (gdbarch, m88k_return_value);
864
865   set_gdbarch_addr_bits_remove (gdbarch, m88k_addr_bits_remove);
866   set_gdbarch_breakpoint_from_pc (gdbarch, m88k_breakpoint_from_pc);
867   set_gdbarch_unwind_pc (gdbarch, m88k_unwind_pc);
868   set_gdbarch_write_pc (gdbarch, m88k_write_pc);
869
870   frame_base_set_default (gdbarch, &m88k_frame_base);
871   frame_unwind_append_unwinder (gdbarch, &m88k_frame_unwind);
872
873   return gdbarch;
874 }
875 \f
876
877 /* Provide a prototype to silence -Wmissing-prototypes.  */
878 void _initialize_m88k_tdep (void);
879
880 void
881 _initialize_m88k_tdep (void)
882 {
883   gdbarch_register (bfd_arch_m88k, m88k_gdbarch_init, NULL);
884 }