gdb/m88k-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the Motorola 88000 series.

  2.    Copyright (C) 2004-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "dis-asm.h"
  17. #include "frame.h"
  18. #include "frame-base.h"
  19. #include "frame-unwind.h"
  20. #include "gdbcore.h"
  21. #include "gdbtypes.h"
  22. #include "regcache.h"
  23. #include "regset.h"
  24. #include "symtab.h"
  25. #include "trad-frame.h"
  26. #include "value.h"

  27. #include "m88k-tdep.h"

  28. /* Fetch the instruction at PC.  */

  29. static unsigned long
  30. m88k_fetch_instruction (CORE_ADDR pc, enum bfd_endian byte_order)
  31. {
  32.   return read_memory_unsigned_integer (pc, 4, byte_order);
  33. }

  34. /* Register information.  */

  35. /* Return the name of register REGNUM.  */

  36. static const char *
  37. m88k_register_name (struct gdbarch *gdbarch, int regnum)
  38. {
  39.   static char *register_names[] =
  40.   {
  41.     "r0""r1""r2""r3""r4""r5""r6""r7",
  42.     "r8""r9""r10", "r11", "r12", "r13", "r14", "r15",
  43.     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  44.     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  45.     "epsr", "fpsr", "fpcr", "sxip", "snip", "sfip"
  46.   };

  47.   if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
  48.     return register_names[regnum];

  49.   return NULL;
  50. }

  51. /* Return the GDB type object for the "standard" data type of data in
  52.    register REGNUM.  */

  53. static struct type *
  54. m88k_register_type (struct gdbarch *gdbarch, int regnum)
  55. {
  56.   /* SXIP, SNIP, SFIP and R1 contain code addresses.  */
  57.   if ((regnum >= M88K_SXIP_REGNUM && regnum <= M88K_SFIP_REGNUM)
  58.       || regnum == M88K_R1_REGNUM)
  59.     return builtin_type (gdbarch)->builtin_func_ptr;

  60.   /* R30 and R31 typically contains data addresses.  */
  61.   if (regnum == M88K_R30_REGNUM || regnum == M88K_R31_REGNUM)
  62.     return builtin_type (gdbarch)->builtin_data_ptr;

  63.   return builtin_type (gdbarch)->builtin_int32;
  64. }


  65. static CORE_ADDR
  66. m88k_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
  67. {
  68.   /* All instructures are 4-byte aligned.  The lower 2 bits of SXIP,
  69.      SNIP and SFIP are used for special purposes: bit 0 is the
  70.      exception bit and bit 1 is the valid bit.  */
  71.   return addr & ~0x3;
  72. }

  73. /* Use the program counter to determine the contents and size of a
  74.    breakpoint instruction.  Return a pointer to a string of bytes that
  75.    encode a breakpoint instruction, store the length of the string in
  76.    *LEN and optionally adjust *PC to point to the correct memory
  77.    location for inserting the breakpoint.  */

  78. static const gdb_byte *
  79. m88k_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
  80. {
  81.   /* tb 0,r0,511 */
  82.   static gdb_byte break_insn[] = { 0xf0, 0x00, 0xd1, 0xff };

  83.   *len = sizeof (break_insn);
  84.   return break_insn;
  85. }

  86. static CORE_ADDR
  87. m88k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  88. {
  89.   CORE_ADDR pc;

  90.   pc = frame_unwind_register_unsigned (next_frame, M88K_SXIP_REGNUM);
  91.   return m88k_addr_bits_remove (gdbarch, pc);
  92. }

  93. static void
  94. m88k_write_pc (struct regcache *regcache, CORE_ADDR pc)
  95. {
  96.   /* According to the MC88100 RISC Microprocessor User's Manual,
  97.      section 6.4.3.1.2:

  98.      "... can be made to return to a particular instruction by placing
  99.      a valid instruction address in the SNIP and the next sequential
  100.      instruction address in the SFIP (with V bits set and E bits
  101.      clear).  The rte resumes execution at the instruction pointed to
  102.      by the SNIP, then the SFIP."

  103.      The E bit is the least significant bit (bit 0).  The V (valid)
  104.      bit is bit 1.  This is why we logical or 2 into the values we are
  105.      writing below.  It turns out that SXIP plays no role when
  106.      returning from an exception so nothing special has to be done
  107.      with it.  We could even (presumably) give it a totally bogus
  108.      value.  */

  109.   regcache_cooked_write_unsigned (regcache, M88K_SXIP_REGNUM, pc);
  110.   regcache_cooked_write_unsigned (regcache, M88K_SNIP_REGNUM, pc | 2);
  111.   regcache_cooked_write_unsigned (regcache, M88K_SFIP_REGNUM, (pc + 4) | 2);
  112. }


  113. /* The functions on this page are intended to be used to classify
  114.    function arguments.  */

  115. /* Check whether TYPE is "Integral or Pointer".  */

  116. static int
  117. m88k_integral_or_pointer_p (const struct type *type)
  118. {
  119.   switch (TYPE_CODE (type))
  120.     {
  121.     case TYPE_CODE_INT:
  122.     case TYPE_CODE_BOOL:
  123.     case TYPE_CODE_CHAR:
  124.     case TYPE_CODE_ENUM:
  125.     case TYPE_CODE_RANGE:
  126.       {
  127.         /* We have byte, half-word, word and extended-word/doubleword
  128.            integral types.  */
  129.         int len = TYPE_LENGTH (type);
  130.         return (len == 1 || len == 2 || len == 4 || len == 8);
  131.       }
  132.       return 1;
  133.     case TYPE_CODE_PTR:
  134.     case TYPE_CODE_REF:
  135.       {
  136.         /* Allow only 32-bit pointers.  */
  137.         return (TYPE_LENGTH (type) == 4);
  138.       }
  139.       return 1;
  140.     default:
  141.       break;
  142.     }

  143.   return 0;
  144. }

  145. /* Check whether TYPE is "Floating".  */

  146. static int
  147. m88k_floating_p (const struct type *type)
  148. {
  149.   switch (TYPE_CODE (type))
  150.     {
  151.     case TYPE_CODE_FLT:
  152.       {
  153.         int len = TYPE_LENGTH (type);
  154.         return (len == 4 || len == 8);
  155.       }
  156.     default:
  157.       break;
  158.     }

  159.   return 0;
  160. }

  161. /* Check whether TYPE is "Structure or Union".  */

  162. static int
  163. m88k_structure_or_union_p (const struct type *type)
  164. {
  165.   switch (TYPE_CODE (type))
  166.     {
  167.     case TYPE_CODE_STRUCT:
  168.     case TYPE_CODE_UNION:
  169.       return 1;
  170.     default:
  171.       break;
  172.     }

  173.   return 0;
  174. }

  175. /* Check whether TYPE has 8-byte alignment.  */

  176. static int
  177. m88k_8_byte_align_p (struct type *type)
  178. {
  179.   if (m88k_structure_or_union_p (type))
  180.     {
  181.       int i;

  182.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  183.         {
  184.           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));

  185.           if (m88k_8_byte_align_p (subtype))
  186.             return 1;
  187.         }
  188.     }

  189.   if (m88k_integral_or_pointer_p (type) || m88k_floating_p (type))
  190.     return (TYPE_LENGTH (type) == 8);

  191.   return 0;
  192. }

  193. /* Check whether TYPE can be passed in a register.  */

  194. static int
  195. m88k_in_register_p (struct type *type)
  196. {
  197.   if (m88k_integral_or_pointer_p (type) || m88k_floating_p (type))
  198.     return 1;

  199.   if (m88k_structure_or_union_p (type) && TYPE_LENGTH (type) == 4)
  200.     return 1;

  201.   return 0;
  202. }

  203. static CORE_ADDR
  204. m88k_store_arguments (struct regcache *regcache, int nargs,
  205.                       struct value **args, CORE_ADDR sp)
  206. {
  207.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  208.   int num_register_words = 0;
  209.   int num_stack_words = 0;
  210.   int i;

  211.   for (i = 0; i < nargs; i++)
  212.     {
  213.       struct type *type = value_type (args[i]);
  214.       int len = TYPE_LENGTH (type);

  215.       if (m88k_integral_or_pointer_p (type) && len < 4)
  216.         {
  217.           args[i] = value_cast (builtin_type (gdbarch)->builtin_int32,
  218.                                 args[i]);
  219.           type = value_type (args[i]);
  220.           len = TYPE_LENGTH (type);
  221.         }

  222.       if (m88k_in_register_p (type))
  223.         {
  224.           int num_words = 0;

  225.           if (num_register_words % 2 == 1 && m88k_8_byte_align_p (type))
  226.             num_words++;

  227.           num_words += ((len + 3) / 4);
  228.           if (num_register_words + num_words <= 8)
  229.             {
  230.               num_register_words += num_words;
  231.               continue;
  232.             }

  233.           /* We've run out of available registers.  Pass the argument
  234.              on the stack.  */
  235.         }

  236.       if (num_stack_words % 2 == 1 && m88k_8_byte_align_p (type))
  237.         num_stack_words++;

  238.       num_stack_words += ((len + 3) / 4);
  239.     }

  240.   /* Allocate stack space.  */
  241.   sp = align_down (sp - 32 - num_stack_words * 4, 16);
  242.   num_stack_words = num_register_words = 0;

  243.   for (i = 0; i < nargs; i++)
  244.     {
  245.       const bfd_byte *valbuf = value_contents (args[i]);
  246.       struct type *type = value_type (args[i]);
  247.       int len = TYPE_LENGTH (type);
  248.       int stack_word = num_stack_words;

  249.       if (m88k_in_register_p (type))
  250.         {
  251.           int register_word = num_register_words;

  252.           if (register_word % 2 == 1 && m88k_8_byte_align_p (type))
  253.             register_word++;

  254.           gdb_assert (len == 4 || len == 8);

  255.           if (register_word + len / 8 < 8)
  256.             {
  257.               int regnum = M88K_R2_REGNUM + register_word;

  258.               regcache_raw_write (regcache, regnum, valbuf);
  259.               if (len > 4)
  260.                 regcache_raw_write (regcache, regnum + 1, valbuf + 4);

  261.               num_register_words = (register_word + len / 4);
  262.               continue;
  263.             }
  264.         }

  265.       if (stack_word % 2 == -1 && m88k_8_byte_align_p (type))
  266.         stack_word++;

  267.       write_memory (sp + stack_word * 4, valbuf, len);
  268.       num_stack_words = (stack_word + (len + 3) / 4);
  269.     }

  270.   return sp;
  271. }

  272. static CORE_ADDR
  273. m88k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  274.                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  275.                       struct value **args, CORE_ADDR sp, int struct_return,
  276.                       CORE_ADDR struct_addr)
  277. {
  278.   /* Set up the function arguments.  */
  279.   sp = m88k_store_arguments (regcache, nargs, args, sp);
  280.   gdb_assert (sp % 16 == 0);

  281.   /* Store return value address.  */
  282.   if (struct_return)
  283.     regcache_raw_write_unsigned (regcache, M88K_R12_REGNUM, struct_addr);

  284.   /* Store the stack pointer and return address in the appropriate
  285.      registers.  */
  286.   regcache_raw_write_unsigned (regcache, M88K_R31_REGNUM, sp);
  287.   regcache_raw_write_unsigned (regcache, M88K_R1_REGNUM, bp_addr);

  288.   /* Return the stack pointer.  */
  289.   return sp;
  290. }

  291. static struct frame_id
  292. m88k_dummy_id (struct gdbarch *arch, struct frame_info *this_frame)
  293. {
  294.   CORE_ADDR sp;

  295.   sp = get_frame_register_unsigned (this_frame, M88K_R31_REGNUM);
  296.   return frame_id_build (sp, get_frame_pc (this_frame));
  297. }


  298. /* Determine, for architecture GDBARCH, how a return value of TYPE
  299.    should be returned.  If it is supposed to be returned in registers,
  300.    and READBUF is non-zero, read the appropriate value from REGCACHE,
  301.    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
  302.    from WRITEBUF into REGCACHE.  */

  303. static enum return_value_convention
  304. m88k_return_value (struct gdbarch *gdbarch, struct value *function,
  305.                    struct type *type, struct regcache *regcache,
  306.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  307. {
  308.   int len = TYPE_LENGTH (type);
  309.   gdb_byte buf[8];

  310.   if (!m88k_integral_or_pointer_p (type) && !m88k_floating_p (type))
  311.     return RETURN_VALUE_STRUCT_CONVENTION;

  312.   if (readbuf)
  313.     {
  314.       /* Read the contents of R2 and (if necessary) R3.  */
  315.       regcache_cooked_read (regcache, M88K_R2_REGNUM, buf);
  316.       if (len > 4)
  317.         {
  318.           regcache_cooked_read (regcache, M88K_R3_REGNUM, buf + 4);
  319.           gdb_assert (len == 8);
  320.           memcpy (readbuf, buf, len);
  321.         }
  322.       else
  323.         {
  324.           /* Just stripping off any unused bytes should preserve the
  325.              signed-ness just fine.  */
  326.           memcpy (readbuf, buf + 4 - len, len);
  327.         }
  328.     }

  329.   if (writebuf)
  330.     {
  331.       /* Read the contents to R2 and (if necessary) R3.  */
  332.       if (len > 4)
  333.         {
  334.           gdb_assert (len == 8);
  335.           memcpy (buf, writebuf, 8);
  336.           regcache_cooked_write (regcache, M88K_R3_REGNUM, buf + 4);
  337.         }
  338.       else
  339.         {
  340.           /* ??? Do we need to do any sign-extension here?  */
  341.           memcpy (buf + 4 - len, writebuf, len);
  342.         }
  343.       regcache_cooked_write (regcache, M88K_R2_REGNUM, buf);
  344.     }

  345.   return RETURN_VALUE_REGISTER_CONVENTION;
  346. }

  347. /* Default frame unwinder.  */

  348. struct m88k_frame_cache
  349. {
  350.   /* Base address.  */
  351.   CORE_ADDR base;
  352.   CORE_ADDR pc;

  353.   int sp_offset;
  354.   int fp_offset;

  355.   /* Table of saved registers.  */
  356.   struct trad_frame_saved_reg *saved_regs;
  357. };

  358. /* Prologue analysis.  */

  359. /* Macros for extracting fields from instructions.  */

  360. #define BITMASK(pos, width) (((0x1 << (width)) - 1) << (pos))
  361. #define EXTRACT_FIELD(val, pos, width) ((val) >> (pos) & BITMASK (0, width))
  362. #define        SUBU_OFFSET(x)        ((unsigned)(x & 0xFFFF))
  363. #define        ST_OFFSET(x)        ((unsigned)((x) & 0xFFFF))
  364. #define        ST_SRC(x)        EXTRACT_FIELD ((x), 21, 5)
  365. #define        ADDU_OFFSET(x)        ((unsigned)(x & 0xFFFF))

  366. /* Possible actions to be taken by the prologue analyzer for the
  367.    instructions it encounters.  */

  368. enum m88k_prologue_insn_action
  369. {
  370.   M88K_PIA_SKIP,                /* Ignore.  */
  371.   M88K_PIA_NOTE_ST,                /* Note register store.  */
  372.   M88K_PIA_NOTE_STD,                /* Note register pair store.  */
  373.   M88K_PIA_NOTE_SP_ADJUSTMENT,        /* Note stack pointer adjustment.  */
  374.   M88K_PIA_NOTE_FP_ASSIGNMENT,        /* Note frame pointer assignment.  */
  375.   M88K_PIA_NOTE_BRANCH,                /* Note branch.  */
  376.   M88K_PIA_NOTE_PROLOGUE_END        /* Note end of prologue.  */
  377. };

  378. /* Table of instructions that may comprise a function prologue.  */

  379. struct m88k_prologue_insn
  380. {
  381.   unsigned long insn;
  382.   unsigned long mask;
  383.   enum m88k_prologue_insn_action action;
  384. };

  385. struct m88k_prologue_insn m88k_prologue_insn_table[] =
  386. {
  387.   /* Various register move instructions.  */
  388.   { 0x58000000, 0xf800ffff, M88K_PIA_SKIP },     /* or/or.u with immed of 0 */
  389.   { 0xf4005800, 0xfc1fffe0, M88K_PIA_SKIP },     /* or rd,r0,rs */
  390.   { 0xf4005800, 0xfc00ffff, M88K_PIA_SKIP },     /* or rd,rs,r0 */

  391.   /* Various other instructions.  */
  392.   { 0x58000000, 0xf8000000, M88K_PIA_SKIP },     /* or/or.u */

  393.   /* Stack pointer setup: "subu sp,sp,n" where n is a multiple of 8.  */
  394.   { 0x67ff0000, 0xffff0007, M88K_PIA_NOTE_SP_ADJUSTMENT },

  395.   /* Frame pointer assignment: "addu r30,r31,n".  */
  396.   { 0x63df0000, 0xffff0000, M88K_PIA_NOTE_FP_ASSIGNMENT },

  397.   /* Store to stack instructions; either "st rx,sp,n" or "st.d rx,sp,n".  */
  398.   { 0x241f0000, 0xfc1f0000, M88K_PIA_NOTE_ST },  /* st rx,sp,n */
  399.   { 0x201f0000, 0xfc1f0000, M88K_PIA_NOTE_STD }, /* st.d rs,sp,n */

  400.   /* Instructions needed for setting up r25 for pic code.  */
  401.   { 0x5f200000, 0xffff0000, M88K_PIA_SKIP },     /* or.u r25,r0,offset_high */
  402.   { 0xcc000002, 0xffffffff, M88K_PIA_SKIP },     /* bsr.n Lab */
  403.   { 0x5b390000, 0xffff0000, M88K_PIA_SKIP },     /* or r25,r25,offset_low */
  404.   { 0xf7396001, 0xffffffff, M88K_PIA_SKIP },     /* Lab: addu r25,r25,r1 */

  405.   /* Various branch or jump instructions which have a delay slot --
  406.      these do not form part of the prologue, but the instruction in
  407.      the delay slot might be a store instruction which should be
  408.      noted.  */
  409.   { 0xc4000000, 0xe4000000, M88K_PIA_NOTE_BRANCH },
  410.                                       /* br.n, bsr.n, bb0.n, or bb1.n */
  411.   { 0xec000000, 0xfc000000, M88K_PIA_NOTE_BRANCH }, /* bcnd.n */
  412.   { 0xf400c400, 0xfffff7e0, M88K_PIA_NOTE_BRANCH }, /* jmp.n or jsr.n */

  413.   /* Catch all.  Ends prologue analysis.  */
  414.   { 0x00000000, 0x00000000, M88K_PIA_NOTE_PROLOGUE_END }
  415. };

  416. /* Do a full analysis of the function prologue at PC and update CACHE
  417.    accordingly.  Bail out early if LIMIT is reached.  Return the
  418.    address where the analysis stopped.  If LIMIT points beyond the
  419.    function prologue, the return address should be the end of the
  420.    prologue.  */

  421. static CORE_ADDR
  422. m88k_analyze_prologue (struct gdbarch *gdbarch,
  423.                        CORE_ADDR pc, CORE_ADDR limit,
  424.                        struct m88k_frame_cache *cache)
  425. {
  426.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  427.   CORE_ADDR end = limit;

  428.   /* Provide a dummy cache if necessary.  */
  429.   if (cache == NULL)
  430.     {
  431.       size_t sizeof_saved_regs =
  432.         (M88K_R31_REGNUM + 1) * sizeof (struct trad_frame_saved_reg);

  433.       cache = alloca (sizeof (struct m88k_frame_cache));
  434.       cache->saved_regs = alloca (sizeof_saved_regs);

  435.       /* We only initialize the members we care about.  */
  436.       cache->saved_regs[M88K_R1_REGNUM].addr = -1;
  437.       cache->fp_offset = -1;
  438.     }

  439.   while (pc < limit)
  440.     {
  441.       struct m88k_prologue_insn *pi = m88k_prologue_insn_table;
  442.       unsigned long insn = m88k_fetch_instruction (pc, byte_order);

  443.       while ((insn & pi->mask) != pi->insn)
  444.         pi++;

  445.       switch (pi->action)
  446.         {
  447.         case M88K_PIA_SKIP:
  448.           /* If we have a frame pointer, and R1 has been saved,
  449.              consider this instruction as not being part of the
  450.              prologue.  */
  451.           if (cache->fp_offset != -1
  452.               && cache->saved_regs[M88K_R1_REGNUM].addr != -1)
  453.             return min (pc, end);
  454.           break;

  455.         case M88K_PIA_NOTE_ST:
  456.         case M88K_PIA_NOTE_STD:
  457.           /* If no frame has been allocated, the stores aren't part of
  458.              the prologue.  */
  459.           if (cache->sp_offset == 0)
  460.             return min (pc, end);

  461.           /* Record location of saved registers.  */
  462.           {
  463.             int regnum = ST_SRC (insn) + M88K_R0_REGNUM;
  464.             ULONGEST offset = ST_OFFSET (insn);

  465.             cache->saved_regs[regnum].addr = offset;
  466.             if (pi->action == M88K_PIA_NOTE_STD && regnum < M88K_R31_REGNUM)
  467.               cache->saved_regs[regnum + 1].addr = offset + 4;
  468.           }
  469.           break;

  470.         case M88K_PIA_NOTE_SP_ADJUSTMENT:
  471.           /* A second stack pointer adjustment isn't part of the
  472.              prologue.  */
  473.           if (cache->sp_offset != 0)
  474.             return min (pc, end);

  475.           /* Store stack pointer adjustment.  */
  476.           cache->sp_offset = -SUBU_OFFSET (insn);
  477.           break;

  478.         case M88K_PIA_NOTE_FP_ASSIGNMENT:
  479.           /* A second frame pointer assignment isn't part of the
  480.              prologue.  */
  481.           if (cache->fp_offset != -1)
  482.             return min (pc, end);

  483.           /* Record frame pointer assignment.  */
  484.           cache->fp_offset = ADDU_OFFSET (insn);
  485.           break;

  486.         case M88K_PIA_NOTE_BRANCH:
  487.           /* The branch instruction isn't part of the prologue, but
  488.              the instruction in the delay slot might be.  Limit the
  489.              prologue analysis to the delay slot and record the branch
  490.              instruction as the end of the prologue.  */
  491.           limit = min (limit, pc + 2 * M88K_INSN_SIZE);
  492.           end = pc;
  493.           break;

  494.         case M88K_PIA_NOTE_PROLOGUE_END:
  495.           return min (pc, end);
  496.         }

  497.       pc += M88K_INSN_SIZE;
  498.     }

  499.   return end;
  500. }

  501. /* An upper limit to the size of the prologue.  */
  502. const int m88k_max_prologue_size = 128 * M88K_INSN_SIZE;

  503. /* Return the address of first real instruction of the function
  504.    starting at PC.  */

  505. static CORE_ADDR
  506. m88k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  507. {
  508.   struct symtab_and_line sal;
  509.   CORE_ADDR func_start, func_end;

  510.   /* This is the preferred method, find the end of the prologue by
  511.      using the debugging information.  */
  512.   if (find_pc_partial_function (pc, NULL, &func_start, &func_end))
  513.     {
  514.       sal = find_pc_line (func_start, 0);

  515.       if (sal.end < func_end && pc <= sal.end)
  516.         return sal.end;
  517.     }

  518.   return m88k_analyze_prologue (gdbarch, pc, pc + m88k_max_prologue_size,
  519.                                 NULL);
  520. }

  521. static struct m88k_frame_cache *
  522. m88k_frame_cache (struct frame_info *this_frame, void **this_cache)
  523. {
  524.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  525.   struct m88k_frame_cache *cache;
  526.   CORE_ADDR frame_sp;

  527.   if (*this_cache)
  528.     return *this_cache;

  529.   cache = FRAME_OBSTACK_ZALLOC (struct m88k_frame_cache);
  530.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  531.   cache->fp_offset = -1;

  532.   cache->pc = get_frame_func (this_frame);
  533.   if (cache->pc != 0)
  534.     m88k_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
  535.                            cache);

  536.   /* Calculate the stack pointer used in the prologue.  */
  537.   if (cache->fp_offset != -1)
  538.     {
  539.       CORE_ADDR fp;

  540.       fp = get_frame_register_unsigned (this_frame, M88K_R30_REGNUM);
  541.       frame_sp = fp - cache->fp_offset;
  542.     }
  543.   else
  544.     {
  545.       /* If we know where the return address is saved, we can take a
  546.          solid guess at what the frame pointer should be.  */
  547.       if (cache->saved_regs[M88K_R1_REGNUM].addr != -1)
  548.         cache->fp_offset = cache->saved_regs[M88K_R1_REGNUM].addr - 4;
  549.       frame_sp = get_frame_register_unsigned (this_frame, M88K_R31_REGNUM);
  550.     }

  551.   /* Now that we know the stack pointer, adjust the location of the
  552.      saved registers.  */
  553.   {
  554.     int regnum;

  555.     for (regnum = M88K_R0_REGNUM; regnum < M88K_R31_REGNUM; regnum ++)
  556.       if (cache->saved_regs[regnum].addr != -1)
  557.         cache->saved_regs[regnum].addr += frame_sp;
  558.   }

  559.   /* Calculate the frame's base.  */
  560.   cache->base = frame_sp - cache->sp_offset;
  561.   trad_frame_set_value (cache->saved_regs, M88K_R31_REGNUM, cache->base);

  562.   /* Identify SXIP with the return address in R1.  */
  563.   cache->saved_regs[M88K_SXIP_REGNUM] = cache->saved_regs[M88K_R1_REGNUM];

  564.   *this_cache = cache;
  565.   return cache;
  566. }

  567. static void
  568. m88k_frame_this_id (struct frame_info *this_frame, void **this_cache,
  569.                     struct frame_id *this_id)
  570. {
  571.   struct m88k_frame_cache *cache = m88k_frame_cache (this_frame, this_cache);

  572.   /* This marks the outermost frame.  */
  573.   if (cache->base == 0)
  574.     return;

  575.   (*this_id) = frame_id_build (cache->base, cache->pc);
  576. }

  577. static struct value *
  578. m88k_frame_prev_register (struct frame_info *this_frame,
  579.                           void **this_cache, int regnum)
  580. {
  581.   struct m88k_frame_cache *cache = m88k_frame_cache (this_frame, this_cache);

  582.   if (regnum == M88K_SNIP_REGNUM || regnum == M88K_SFIP_REGNUM)
  583.     {
  584.       struct value *value;
  585.       CORE_ADDR pc;

  586.       value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
  587.                                             M88K_SXIP_REGNUM);
  588.       pc = value_as_long (value);
  589.       release_value (value);
  590.       value_free (value);

  591.       if (regnum == M88K_SFIP_REGNUM)
  592.         pc += 4;

  593.       return frame_unwind_got_constant (this_frame, regnum, pc + 4);
  594.     }

  595.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  596. }

  597. static const struct frame_unwind m88k_frame_unwind =
  598. {
  599.   NORMAL_FRAME,
  600.   default_frame_unwind_stop_reason,
  601.   m88k_frame_this_id,
  602.   m88k_frame_prev_register,
  603.   NULL,
  604.   default_frame_sniffer
  605. };


  606. static CORE_ADDR
  607. m88k_frame_base_address (struct frame_info *this_frame, void **this_cache)
  608. {
  609.   struct m88k_frame_cache *cache = m88k_frame_cache (this_frame, this_cache);

  610.   if (cache->fp_offset != -1)
  611.     return cache->base + cache->sp_offset + cache->fp_offset;

  612.   return 0;
  613. }

  614. static const struct frame_base m88k_frame_base =
  615. {
  616.   &m88k_frame_unwind,
  617.   m88k_frame_base_address,
  618.   m88k_frame_base_address,
  619.   m88k_frame_base_address
  620. };


  621. /* Core file support.  */

  622. /* Supply register REGNUM from the buffer specified by GREGS and LEN
  623.    in the general-purpose register set REGSET to register cache
  624.    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */

  625. static void
  626. m88k_supply_gregset (const struct regset *regset,
  627.                      struct regcache *regcache,
  628.                      int regnum, const void *gregs, size_t len)
  629. {
  630.   const gdb_byte *regs = gregs;
  631.   int i;

  632.   for (i = 0; i < M88K_NUM_REGS; i++)
  633.     {
  634.       if (regnum == i || regnum == -1)
  635.         regcache_raw_supply (regcache, i, regs + i * 4);
  636.     }
  637. }

  638. /* Motorola 88000 register set.  */

  639. static const struct regset m88k_gregset =
  640. {
  641.   NULL,
  642.   m88k_supply_gregset
  643. };

  644. /* Iterate over supported core file register note sections. */

  645. static void
  646. m88k_iterate_over_regset_sections (struct gdbarch *gdbarch,
  647.                                    iterate_over_regset_sections_cb *cb,
  648.                                    void *cb_data,
  649.                                    const struct regcache *regcache)
  650. {
  651.   cb (".reg", M88K_NUM_REGS * 4, &m88k_gregset, NULL, cb_data);
  652. }


  653. static struct gdbarch *
  654. m88k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  655. {
  656.   struct gdbarch *gdbarch;

  657.   /* If there is already a candidate, use it.  */
  658.   arches = gdbarch_list_lookup_by_info (arches, &info);
  659.   if (arches != NULL)
  660.     return arches->gdbarch;

  661.   /* Allocate space for the new architecture.  */
  662.   gdbarch = gdbarch_alloc (&info, NULL);

  663.   /* There is no real `long double'.  */
  664.   set_gdbarch_long_double_bit (gdbarch, 64);
  665.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);

  666.   set_gdbarch_num_regs (gdbarch, M88K_NUM_REGS);
  667.   set_gdbarch_register_name (gdbarch, m88k_register_name);
  668.   set_gdbarch_register_type (gdbarch, m88k_register_type);

  669.   /* Register numbers of various important registers.  */
  670.   set_gdbarch_sp_regnum (gdbarch, M88K_R31_REGNUM);
  671.   set_gdbarch_pc_regnum (gdbarch, M88K_SXIP_REGNUM);

  672.   /* Core file support.  */
  673.   set_gdbarch_iterate_over_regset_sections
  674.     (gdbarch, m88k_iterate_over_regset_sections);

  675.   set_gdbarch_print_insn (gdbarch, print_insn_m88k);

  676.   set_gdbarch_skip_prologue (gdbarch, m88k_skip_prologue);

  677.   /* Stack grows downward.  */
  678.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  679.   /* Call dummy code.  */
  680.   set_gdbarch_push_dummy_call (gdbarch, m88k_push_dummy_call);
  681.   set_gdbarch_dummy_id (gdbarch, m88k_dummy_id);

  682.   /* Return value info.  */
  683.   set_gdbarch_return_value (gdbarch, m88k_return_value);

  684.   set_gdbarch_addr_bits_remove (gdbarch, m88k_addr_bits_remove);
  685.   set_gdbarch_breakpoint_from_pc (gdbarch, m88k_breakpoint_from_pc);
  686.   set_gdbarch_unwind_pc (gdbarch, m88k_unwind_pc);
  687.   set_gdbarch_write_pc (gdbarch, m88k_write_pc);

  688.   frame_base_set_default (gdbarch, &m88k_frame_base);
  689.   frame_unwind_append_unwinder (gdbarch, &m88k_frame_unwind);

  690.   return gdbarch;
  691. }


  692. /* Provide a prototype to silence -Wmissing-prototypes.  */
  693. void _initialize_m88k_tdep (void);

  694. void
  695. _initialize_m88k_tdep (void)
  696. {
  697.   gdbarch_register (bfd_arch_m88k, m88k_gdbarch_init, NULL);
  698. }