gdb/xstormy16-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the Sanyo Xstormy16a (LC590000) processor.

  2.    Copyright (C) 2001-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 "frame.h"
  16. #include "frame-base.h"
  17. #include "frame-unwind.h"
  18. #include "dwarf2-frame.h"
  19. #include "symtab.h"
  20. #include "gdbtypes.h"
  21. #include "gdbcmd.h"
  22. #include "gdbcore.h"
  23. #include "value.h"
  24. #include "dis-asm.h"
  25. #include "inferior.h"
  26. #include "arch-utils.h"
  27. #include "floatformat.h"
  28. #include "regcache.h"
  29. #include "doublest.h"
  30. #include "osabi.h"
  31. #include "objfiles.h"

  32. enum gdb_regnum
  33. {
  34.   /* Xstormy16 has 16 general purpose registers (R0-R15) plus PC.
  35.      Functions will return their values in register R2-R7 as they fit.
  36.      Otherwise a hidden pointer to an big enough area is given as argument
  37.      to the function in r2.  Further arguments are beginning in r3 then.
  38.      R13 is used as frame pointer when GCC compiles w/o optimization
  39.      R14 is used as "PSW", displaying the CPU status.
  40.      R15 is used implicitely as stack pointer.  */
  41.   E_R0_REGNUM,
  42.   E_R1_REGNUM,
  43.   E_R2_REGNUM, E_1ST_ARG_REGNUM = E_R2_REGNUM, E_PTR_RET_REGNUM = E_R2_REGNUM,
  44.   E_R3_REGNUM,
  45.   E_R4_REGNUM,
  46.   E_R5_REGNUM,
  47.   E_R6_REGNUM,
  48.   E_R7_REGNUM, E_LST_ARG_REGNUM = E_R7_REGNUM,
  49.   E_R8_REGNUM,
  50.   E_R9_REGNUM,
  51.   E_R10_REGNUM,
  52.   E_R11_REGNUM,
  53.   E_R12_REGNUM,
  54.   E_R13_REGNUM, E_FP_REGNUM = E_R13_REGNUM,
  55.   E_R14_REGNUM, E_PSW_REGNUM = E_R14_REGNUM,
  56.   E_R15_REGNUM, E_SP_REGNUM = E_R15_REGNUM,
  57.   E_PC_REGNUM,
  58.   E_NUM_REGS
  59. };

  60. /* Use an invalid address value as 'not available' marker.  */
  61. enum { REG_UNAVAIL = (CORE_ADDR) -1 };

  62. struct xstormy16_frame_cache
  63. {
  64.   /* Base address.  */
  65.   CORE_ADDR base;
  66.   CORE_ADDR pc;
  67.   LONGEST framesize;
  68.   int uses_fp;
  69.   CORE_ADDR saved_regs[E_NUM_REGS];
  70.   CORE_ADDR saved_sp;
  71. };

  72. /* Size of instructions, registers, etc.  */
  73. enum
  74. {
  75.   xstormy16_inst_size = 2,
  76.   xstormy16_reg_size = 2,
  77.   xstormy16_pc_size = 4
  78. };

  79. /* Size of return datatype which fits into the remaining return registers.  */
  80. #define E_MAX_RETTYPE_SIZE(regnum)        ((E_LST_ARG_REGNUM - (regnum) + 1) \
  81.                                         * xstormy16_reg_size)

  82. /* Size of return datatype which fits into all return registers.  */
  83. enum
  84. {
  85.   E_MAX_RETTYPE_SIZE_IN_REGS = E_MAX_RETTYPE_SIZE (E_R2_REGNUM)
  86. };

  87. /* Function: xstormy16_register_name
  88.    Returns the name of the standard Xstormy16 register N.  */

  89. static const char *
  90. xstormy16_register_name (struct gdbarch *gdbarch, int regnum)
  91. {
  92.   static char *register_names[] = {
  93.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  94.     "r8", "r9", "r10", "r11", "r12", "r13",
  95.     "psw", "sp", "pc"
  96.   };

  97.   if (regnum < 0 || regnum >= E_NUM_REGS)
  98.     internal_error (__FILE__, __LINE__,
  99.                     _("xstormy16_register_name: illegal register number %d"),
  100.                     regnum);
  101.   else
  102.     return register_names[regnum];

  103. }

  104. static struct type *
  105. xstormy16_register_type (struct gdbarch *gdbarch, int regnum)
  106. {
  107.   if (regnum == E_PC_REGNUM)
  108.     return builtin_type (gdbarch)->builtin_uint32;
  109.   else
  110.     return builtin_type (gdbarch)->builtin_uint16;
  111. }

  112. /* Function: xstormy16_type_is_scalar
  113.    Makes the decision if a given type is a scalar types.  Scalar
  114.    types are returned in the registers r2-r7 as they fit.  */

  115. static int
  116. xstormy16_type_is_scalar (struct type *t)
  117. {
  118.   return (TYPE_CODE(t) != TYPE_CODE_STRUCT
  119.           && TYPE_CODE(t) != TYPE_CODE_UNION
  120.           && TYPE_CODE(t) != TYPE_CODE_ARRAY);
  121. }

  122. /* Function: xstormy16_use_struct_convention
  123.    Returns non-zero if the given struct type will be returned using
  124.    a special convention, rather than the normal function return method.
  125.    7sed in the contexts of the "return" command, and of
  126.    target function calls from the debugger.  */

  127. static int
  128. xstormy16_use_struct_convention (struct type *type)
  129. {
  130.   return !xstormy16_type_is_scalar (type)
  131.          || TYPE_LENGTH (type) > E_MAX_RETTYPE_SIZE_IN_REGS;
  132. }

  133. /* Function: xstormy16_extract_return_value
  134.    Find a function's return value in the appropriate registers (in
  135.    regbuf), and copy it into valbuf.  */

  136. static void
  137. xstormy16_extract_return_value (struct type *type, struct regcache *regcache,
  138.                                 gdb_byte *valbuf)
  139. {
  140.   int len = TYPE_LENGTH (type);
  141.   int i, regnum = E_1ST_ARG_REGNUM;

  142.   for (i = 0; i < len; i += xstormy16_reg_size)
  143.     regcache_raw_read (regcache, regnum++, valbuf + i);
  144. }

  145. /* Function: xstormy16_store_return_value
  146.    Copy the function return value from VALBUF into the
  147.    proper location for a function return.
  148.    Called only in the context of the "return" command.  */

  149. static void
  150. xstormy16_store_return_value (struct type *type, struct regcache *regcache,
  151.                               const gdb_byte *valbuf)
  152. {
  153.   if (TYPE_LENGTH (type) == 1)
  154.     {
  155.       /* Add leading zeros to the value.  */
  156.       gdb_byte buf[xstormy16_reg_size];
  157.       memset (buf, 0, xstormy16_reg_size);
  158.       memcpy (buf, valbuf, 1);
  159.       regcache_raw_write (regcache, E_1ST_ARG_REGNUM, buf);
  160.     }
  161.   else
  162.     {
  163.       int len = TYPE_LENGTH (type);
  164.       int i, regnum = E_1ST_ARG_REGNUM;

  165.       for (i = 0; i < len; i += xstormy16_reg_size)
  166.         regcache_raw_write (regcache, regnum++, valbuf + i);
  167.     }
  168. }

  169. static enum return_value_convention
  170. xstormy16_return_value (struct gdbarch *gdbarch, struct value *function,
  171.                         struct type *type, struct regcache *regcache,
  172.                         gdb_byte *readbuf, const gdb_byte *writebuf)
  173. {
  174.   if (xstormy16_use_struct_convention (type))
  175.     return RETURN_VALUE_STRUCT_CONVENTION;
  176.   if (writebuf)
  177.     xstormy16_store_return_value (type, regcache, writebuf);
  178.   else if (readbuf)
  179.     xstormy16_extract_return_value (type, regcache, readbuf);
  180.   return RETURN_VALUE_REGISTER_CONVENTION;
  181. }

  182. static CORE_ADDR
  183. xstormy16_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  184. {
  185.   if (addr & 1)
  186.     ++addr;
  187.   return addr;
  188. }

  189. /* Function: xstormy16_push_dummy_call
  190.    Setup the function arguments for GDB to call a function in the inferior.
  191.    Called only in the context of a target function call from the debugger.
  192.    Returns the value of the SP register after the args are pushed.  */

  193. static CORE_ADDR
  194. xstormy16_push_dummy_call (struct gdbarch *gdbarch,
  195.                            struct value *function,
  196.                            struct regcache *regcache,
  197.                            CORE_ADDR bp_addr, int nargs,
  198.                            struct value **args,
  199.                            CORE_ADDR sp, int struct_return,
  200.                            CORE_ADDR struct_addr)
  201. {
  202.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  203.   CORE_ADDR stack_dest = sp;
  204.   int argreg = E_1ST_ARG_REGNUM;
  205.   int i, j;
  206.   int typelen, slacklen;
  207.   const gdb_byte *val;
  208.   gdb_byte buf[xstormy16_pc_size];

  209.   /* If struct_return is true, then the struct return address will
  210.      consume one argument-passing register.  */
  211.   if (struct_return)
  212.     {
  213.       regcache_cooked_write_unsigned (regcache, E_PTR_RET_REGNUM, struct_addr);
  214.       argreg++;
  215.     }

  216.   /* Arguments are passed in R2-R7 as they fit.  If an argument doesn't
  217.      fit in the remaining registers we're switching over to the stack.
  218.      No argument is put on stack partially and as soon as we switched
  219.      over to stack no further argument is put in a register even if it
  220.      would fit in the remaining unused registers.  */
  221.   for (i = 0; i < nargs && argreg <= E_LST_ARG_REGNUM; i++)
  222.     {
  223.       typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
  224.       if (typelen > E_MAX_RETTYPE_SIZE (argreg))
  225.         break;

  226.       /* Put argument into registers wordwise.  */
  227.       val = value_contents (args[i]);
  228.       for (j = 0; j < typelen; j += xstormy16_reg_size)
  229.         {
  230.           ULONGEST regval;
  231.           int size = (typelen - j == 1) ? 1 : xstormy16_reg_size;

  232.           regval = extract_unsigned_integer (val + j, size, byte_order);
  233.           regcache_cooked_write_unsigned (regcache, argreg++, regval);
  234.         }
  235.     }

  236.   /* Align SP */
  237.   stack_dest = xstormy16_frame_align (gdbarch, stack_dest);

  238.   /* Loop backwards through remaining arguments and push them on the stack,
  239.      wordaligned.  */
  240.   for (j = nargs - 1; j >= i; j--)
  241.     {
  242.       gdb_byte *val;
  243.       struct cleanup *back_to;
  244.       const gdb_byte *bytes = value_contents (args[j]);

  245.       typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
  246.       slacklen = typelen & 1;
  247.       val = xmalloc (typelen + slacklen);
  248.       back_to = make_cleanup (xfree, val);
  249.       memcpy (val, bytes, typelen);
  250.       memset (val + typelen, 0, slacklen);

  251.       /* Now write this data to the stack.  The stack grows upwards.  */
  252.       write_memory (stack_dest, val, typelen + slacklen);
  253.       stack_dest += typelen + slacklen;
  254.       do_cleanups (back_to);
  255.     }

  256.   store_unsigned_integer (buf, xstormy16_pc_size, byte_order, bp_addr);
  257.   write_memory (stack_dest, buf, xstormy16_pc_size);
  258.   stack_dest += xstormy16_pc_size;

  259.   /* Update stack pointer.  */
  260.   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, stack_dest);

  261.   /* Return the new stack pointer minus the return address slot since
  262.      that's what DWARF2/GCC uses as the frame's CFA.  */
  263.   return stack_dest - xstormy16_pc_size;
  264. }

  265. /* Function: xstormy16_scan_prologue
  266.    Decode the instructions within the given address range.
  267.    Decide when we must have reached the end of the function prologue.
  268.    If a frame_info pointer is provided, fill in its saved_regs etc.

  269.    Returns the address of the first instruction after the prologue.  */

  270. static CORE_ADDR
  271. xstormy16_analyze_prologue (struct gdbarch *gdbarch,
  272.                             CORE_ADDR start_addr, CORE_ADDR end_addr,
  273.                             struct xstormy16_frame_cache *cache,
  274.                             struct frame_info *this_frame)
  275. {
  276.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  277.   CORE_ADDR next_addr;
  278.   ULONGEST inst, inst2;
  279.   LONGEST offset;
  280.   int regnum;

  281.   /* Initialize framesize with size of PC put on stack by CALLF inst.  */
  282.   cache->saved_regs[E_PC_REGNUM] = 0;
  283.   cache->framesize = xstormy16_pc_size;

  284.   if (start_addr >= end_addr)
  285.     return end_addr;

  286.   for (next_addr = start_addr;
  287.        next_addr < end_addr; next_addr += xstormy16_inst_size)
  288.     {
  289.       inst = read_memory_unsigned_integer (next_addr,
  290.                                            xstormy16_inst_size, byte_order);
  291.       inst2 = read_memory_unsigned_integer (next_addr + xstormy16_inst_size,
  292.                                             xstormy16_inst_size, byte_order);

  293.       if (inst >= 0x0082 && inst <= 0x008d)        /* push r2 .. push r13 */
  294.         {
  295.           regnum = inst & 0x000f;
  296.           cache->saved_regs[regnum] = cache->framesize;
  297.           cache->framesize += xstormy16_reg_size;
  298.         }

  299.       /* Optional stack allocation for args and local vars <= 4 byte.  */
  300.       else if (inst == 0x301f || inst == 0x303f)       /* inc r15, #0x1/#0x3 */
  301.         {
  302.           cache->framesize += ((inst & 0x0030) >> 4) + 1;
  303.         }

  304.       /* optional stack allocation for args and local vars > 4 && < 16 byte */
  305.       else if ((inst & 0xff0f) == 0x510f)        /* 51Hf   add r15, #0xH */
  306.         {
  307.           cache->framesize += (inst & 0x00f0) >> 4;
  308.         }

  309.       /* Optional stack allocation for args and local vars >= 16 byte.  */
  310.       else if (inst == 0x314f && inst2 >= 0x0010) /* 314f HHHH add r15, #0xH */
  311.         {
  312.           cache->framesize += inst2;
  313.           next_addr += xstormy16_inst_size;
  314.         }

  315.       else if (inst == 0x46fd)        /* mov r13, r15 */
  316.         {
  317.           cache->uses_fp = 1;
  318.         }

  319.       /* optional copying of args in r2-r7 to r10-r13.  */
  320.       /* Probably only in optimized case but legal action for prologue.  */
  321.       else if ((inst & 0xff00) == 0x4600        /* 46SD   mov rD, rS */
  322.                && (inst & 0x00f0) >= 0x0020 && (inst & 0x00f0) <= 0x0070
  323.                && (inst & 0x000f) >= 0x000a && (inst & 0x000f) <= 0x000d)
  324.         ;

  325.       /* Optional copying of args in r2-r7 to stack.  */
  326.       /* 72DS HHHH   mov.b (rD, 0xHHHH), r(S-8)
  327.          (bit3 always 1, bit2-0 = reg) */
  328.       /* 73DS HHHH   mov.w (rD, 0xHHHH), r(S-8) */
  329.       else if ((inst & 0xfed8) == 0x72d8 && (inst & 0x0007) >= 2)
  330.         {
  331.           regnum = inst & 0x0007;
  332.           /* Only 12 of 16 bits of the argument are used for the
  333.              signed offset.  */
  334.           offset = (LONGEST) (inst2 & 0x0fff);
  335.           if (offset & 0x0800)
  336.             offset -= 0x1000;

  337.           cache->saved_regs[regnum] = cache->framesize + offset;
  338.           next_addr += xstormy16_inst_size;
  339.         }

  340.       else                        /* Not a prologue instruction.  */
  341.         break;
  342.     }

  343.   return next_addr;
  344. }

  345. /* Function: xstormy16_skip_prologue
  346.    If the input address is in a function prologue,
  347.    returns the address of the end of the prologue;
  348.    else returns the input address.

  349.    Note: the input address is likely to be the function start,
  350.    since this function is mainly used for advancing a breakpoint
  351.    to the first line, or stepping to the first line when we have
  352.    stepped into a function call.  */

  353. static CORE_ADDR
  354. xstormy16_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  355. {
  356.   CORE_ADDR func_addr = 0, func_end = 0;
  357.   const char *func_name;

  358.   if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
  359.     {
  360.       struct symtab_and_line sal;
  361.       struct symbol *sym;
  362.       struct xstormy16_frame_cache cache;
  363.       CORE_ADDR plg_end;

  364.       memset (&cache, 0, sizeof cache);

  365.       /* Don't trust line number debug info in frameless functions.  */
  366.       plg_end = xstormy16_analyze_prologue (gdbarch, func_addr, func_end,
  367.                                             &cache, NULL);
  368.       if (!cache.uses_fp)
  369.         return plg_end;

  370.       /* Found a function.  */
  371.       sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL);
  372.       /* Don't use line number debug info for assembly source files.  */
  373.       if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
  374.         {
  375.           sal = find_pc_line (func_addr, 0);
  376.           if (sal.end && sal.end < func_end)
  377.             {
  378.               /* Found a line number, use it as end of prologue.  */
  379.               return sal.end;
  380.             }
  381.         }
  382.       /* No useable line symbol.  Use result of prologue parsing method.  */
  383.       return plg_end;
  384.     }

  385.   /* No function symbol -- just return the PC.  */

  386.   return (CORE_ADDR) pc;
  387. }

  388. /* The epilogue is defined here as the area at the end of a function,
  389.    either on the `ret' instruction itself or after an instruction which
  390.    destroys the function's stack frame.  */
  391. static int
  392. xstormy16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  393. {
  394.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  395.   CORE_ADDR func_addr = 0, func_end = 0;

  396.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  397.     {
  398.       ULONGEST inst, inst2;
  399.       CORE_ADDR addr = func_end - xstormy16_inst_size;

  400.       /* The Xstormy16 epilogue is max. 14 bytes long.  */
  401.       if (pc < func_end - 7 * xstormy16_inst_size)
  402.         return 0;

  403.       /* Check if we're on a `ret' instruction.  Otherwise it's
  404.          too dangerous to proceed.  */
  405.       inst = read_memory_unsigned_integer (addr,
  406.                                            xstormy16_inst_size, byte_order);
  407.       if (inst != 0x0003)
  408.         return 0;

  409.       while ((addr -= xstormy16_inst_size) >= func_addr)
  410.         {
  411.           inst = read_memory_unsigned_integer (addr,
  412.                                                xstormy16_inst_size,
  413.                                                byte_order);
  414.           if (inst >= 0x009a && inst <= 0x009d)        /* pop r10...r13 */
  415.             continue;
  416.           if (inst == 0x305f || inst == 0x307f)        /* dec r15, #0x1/#0x3 */
  417.             break;
  418.           inst2 = read_memory_unsigned_integer (addr - xstormy16_inst_size,
  419.                                                 xstormy16_inst_size,
  420.                                                 byte_order);
  421.           if (inst2 == 0x314f && inst >= 0x8000)      /* add r15, neg. value */
  422.             {
  423.               addr -= xstormy16_inst_size;
  424.               break;
  425.             }
  426.           return 0;
  427.         }
  428.       if (pc > addr)
  429.         return 1;
  430.     }
  431.   return 0;
  432. }

  433. static const unsigned char *
  434. xstormy16_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  435.                               int *lenptr)
  436. {
  437.   static unsigned char breakpoint[] = { 0x06, 0x0 };
  438.   *lenptr = sizeof (breakpoint);
  439.   return breakpoint;
  440. }

  441. /* Given a pointer to a jump table entry, return the address
  442.    of the function it jumps to.  Return 0 if not found.  */
  443. static CORE_ADDR
  444. xstormy16_resolve_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
  445. {
  446.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  447.   struct obj_section *faddr_sect = find_pc_section (faddr);

  448.   if (faddr_sect)
  449.     {
  450.       LONGEST inst, inst2, addr;
  451.       gdb_byte buf[2 * xstormy16_inst_size];

  452.       /* Return faddr if it's not pointing into the jump table.  */
  453.       if (strcmp (faddr_sect->the_bfd_section->name, ".plt"))
  454.         return faddr;

  455.       if (!target_read_memory (faddr, buf, sizeof buf))
  456.         {
  457.           inst = extract_unsigned_integer (buf,
  458.                                            xstormy16_inst_size, byte_order);
  459.           inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
  460.                                             xstormy16_inst_size, byte_order);
  461.           addr = inst2 << 8 | (inst & 0xff);
  462.           return addr;
  463.         }
  464.     }
  465.   return 0;
  466. }

  467. /* Given a function's address, attempt to find (and return) the
  468.    address of the corresponding jump table entry.  Return 0 if
  469.    not found.  */
  470. static CORE_ADDR
  471. xstormy16_find_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
  472. {
  473.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  474.   struct obj_section *faddr_sect = find_pc_section (faddr);

  475.   if (faddr_sect)
  476.     {
  477.       struct obj_section *osect;

  478.       /* Return faddr if it's already a pointer to a jump table entry.  */
  479.       if (!strcmp (faddr_sect->the_bfd_section->name, ".plt"))
  480.         return faddr;

  481.       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
  482.       {
  483.         if (!strcmp (osect->the_bfd_section->name, ".plt"))
  484.           break;
  485.       }

  486.       if (osect < faddr_sect->objfile->sections_end)
  487.         {
  488.           CORE_ADDR addr, endaddr;

  489.           addr = obj_section_addr (osect);
  490.           endaddr = obj_section_endaddr (osect);

  491.           for (; addr < endaddr; addr += 2 * xstormy16_inst_size)
  492.             {
  493.               LONGEST inst, inst2, faddr2;
  494.               gdb_byte buf[2 * xstormy16_inst_size];

  495.               if (target_read_memory (addr, buf, sizeof buf))
  496.                 return 0;
  497.               inst = extract_unsigned_integer (buf,
  498.                                                xstormy16_inst_size,
  499.                                                byte_order);
  500.               inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
  501.                                                 xstormy16_inst_size,
  502.                                                 byte_order);
  503.               faddr2 = inst2 << 8 | (inst & 0xff);
  504.               if (faddr == faddr2)
  505.                 return addr;
  506.             }
  507.         }
  508.     }
  509.   return 0;
  510. }

  511. static CORE_ADDR
  512. xstormy16_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  513. {
  514.   struct gdbarch *gdbarch = get_frame_arch (frame);
  515.   CORE_ADDR tmp = xstormy16_resolve_jmp_table_entry (gdbarch, pc);

  516.   if (tmp && tmp != pc)
  517.     return tmp;
  518.   return 0;
  519. }

  520. /* Function pointers are 16 bit.  The address space is 24 bit, using
  521.    32 bit addresses.  Pointers to functions on the XStormy16 are implemented
  522.    by using 16 bit pointers, which are either direct pointers in case the
  523.    function begins below 0x10000, or indirect pointers into a jump table.
  524.    The next two functions convert 16 bit pointers into 24 (32) bit addresses
  525.    and vice versa.  */

  526. static CORE_ADDR
  527. xstormy16_pointer_to_address (struct gdbarch *gdbarch,
  528.                               struct type *type, const gdb_byte *buf)
  529. {
  530.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  531.   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
  532.   CORE_ADDR addr
  533.     = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);

  534.   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
  535.     {
  536.       CORE_ADDR addr2 = xstormy16_resolve_jmp_table_entry (gdbarch, addr);
  537.       if (addr2)
  538.         addr = addr2;
  539.     }

  540.   return addr;
  541. }

  542. static void
  543. xstormy16_address_to_pointer (struct gdbarch *gdbarch,
  544.                               struct type *type, gdb_byte *buf, CORE_ADDR addr)
  545. {
  546.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  547.   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));

  548.   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
  549.     {
  550.       CORE_ADDR addr2 = xstormy16_find_jmp_table_entry (gdbarch, addr);
  551.       if (addr2)
  552.         addr = addr2;
  553.     }
  554.   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  555. }

  556. static struct xstormy16_frame_cache *
  557. xstormy16_alloc_frame_cache (void)
  558. {
  559.   struct xstormy16_frame_cache *cache;
  560.   int i;

  561.   cache = FRAME_OBSTACK_ZALLOC (struct xstormy16_frame_cache);

  562.   cache->base = 0;
  563.   cache->saved_sp = 0;
  564.   cache->pc = 0;
  565.   cache->uses_fp = 0;
  566.   cache->framesize = 0;
  567.   for (i = 0; i < E_NUM_REGS; ++i)
  568.     cache->saved_regs[i] = REG_UNAVAIL;

  569.   return cache;
  570. }

  571. static struct xstormy16_frame_cache *
  572. xstormy16_frame_cache (struct frame_info *this_frame, void **this_cache)
  573. {
  574.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  575.   struct xstormy16_frame_cache *cache;
  576.   CORE_ADDR current_pc;
  577.   int i;

  578.   if (*this_cache)
  579.     return *this_cache;

  580.   cache = xstormy16_alloc_frame_cache ();
  581.   *this_cache = cache;

  582.   cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
  583.   if (cache->base == 0)
  584.     return cache;

  585.   cache->pc = get_frame_func (this_frame);
  586.   current_pc = get_frame_pc (this_frame);
  587.   if (cache->pc)
  588.     xstormy16_analyze_prologue (gdbarch, cache->pc, current_pc,
  589.                                 cache, this_frame);

  590.   if (!cache->uses_fp)
  591.     cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);

  592.   cache->saved_sp = cache->base - cache->framesize;

  593.   for (i = 0; i < E_NUM_REGS; ++i)
  594.     if (cache->saved_regs[i] != REG_UNAVAIL)
  595.       cache->saved_regs[i] += cache->saved_sp;

  596.   return cache;
  597. }

  598. static struct value *
  599. xstormy16_frame_prev_register (struct frame_info *this_frame,
  600.                                void **this_cache, int regnum)
  601. {
  602.   struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
  603.                                                                this_cache);
  604.   gdb_assert (regnum >= 0);

  605.   if (regnum == E_SP_REGNUM && cache->saved_sp)
  606.     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);

  607.   if (regnum < E_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
  608.     return frame_unwind_got_memory (this_frame, regnum,
  609.                                     cache->saved_regs[regnum]);

  610.   return frame_unwind_got_register (this_frame, regnum, regnum);
  611. }

  612. static void
  613. xstormy16_frame_this_id (struct frame_info *this_frame, void **this_cache,
  614.                          struct frame_id *this_id)
  615. {
  616.   struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
  617.                                                                this_cache);

  618.   /* This marks the outermost frame.  */
  619.   if (cache->base == 0)
  620.     return;

  621.   *this_id = frame_id_build (cache->saved_sp, cache->pc);
  622. }

  623. static CORE_ADDR
  624. xstormy16_frame_base_address (struct frame_info *this_frame, void **this_cache)
  625. {
  626.   struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
  627.                                                                this_cache);
  628.   return cache->base;
  629. }

  630. static const struct frame_unwind xstormy16_frame_unwind = {
  631.   NORMAL_FRAME,
  632.   default_frame_unwind_stop_reason,
  633.   xstormy16_frame_this_id,
  634.   xstormy16_frame_prev_register,
  635.   NULL,
  636.   default_frame_sniffer
  637. };

  638. static const struct frame_base xstormy16_frame_base = {
  639.   &xstormy16_frame_unwind,
  640.   xstormy16_frame_base_address,
  641.   xstormy16_frame_base_address,
  642.   xstormy16_frame_base_address
  643. };

  644. static CORE_ADDR
  645. xstormy16_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  646. {
  647.   return frame_unwind_register_unsigned (next_frame, E_SP_REGNUM);
  648. }

  649. static CORE_ADDR
  650. xstormy16_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  651. {
  652.   return frame_unwind_register_unsigned (next_frame, E_PC_REGNUM);
  653. }

  654. static struct frame_id
  655. xstormy16_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  656. {
  657.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  658.   return frame_id_build (sp, get_frame_pc (this_frame));
  659. }


  660. /* Function: xstormy16_gdbarch_init
  661.    Initializer function for the xstormy16 gdbarch vector.
  662.    Called by gdbarch.  Sets up the gdbarch vector(s) for this target.  */

  663. static struct gdbarch *
  664. xstormy16_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  665. {
  666.   struct gdbarch *gdbarch;

  667.   /* find a candidate among the list of pre-declared architectures.  */
  668.   arches = gdbarch_list_lookup_by_info (arches, &info);
  669.   if (arches != NULL)
  670.     return (arches->gdbarch);

  671.   gdbarch = gdbarch_alloc (&info, NULL);

  672.   /*
  673.    * Basic register fields and methods, datatype sizes and stuff.
  674.    */

  675.   set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
  676.   set_gdbarch_num_pseudo_regs (gdbarch, 0);
  677.   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
  678.   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
  679.   set_gdbarch_register_name (gdbarch, xstormy16_register_name);
  680.   set_gdbarch_register_type (gdbarch, xstormy16_register_type);

  681.   set_gdbarch_char_signed (gdbarch, 0);
  682.   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  683.   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  684.   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  685.   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  686.   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  687.   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  688.   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  689.   set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  690.   set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  691.   set_gdbarch_dwarf2_addr_size (gdbarch, 4);

  692.   set_gdbarch_address_to_pointer (gdbarch, xstormy16_address_to_pointer);
  693.   set_gdbarch_pointer_to_address (gdbarch, xstormy16_pointer_to_address);

  694.   /* Stack grows up.  */
  695.   set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);

  696.   /*
  697.    * Frame Info
  698.    */
  699.   set_gdbarch_unwind_sp (gdbarch, xstormy16_unwind_sp);
  700.   set_gdbarch_unwind_pc (gdbarch, xstormy16_unwind_pc);
  701.   set_gdbarch_dummy_id (gdbarch, xstormy16_dummy_id);
  702.   set_gdbarch_frame_align (gdbarch, xstormy16_frame_align);
  703.   frame_base_set_default (gdbarch, &xstormy16_frame_base);

  704.   set_gdbarch_skip_prologue (gdbarch, xstormy16_skip_prologue);
  705.   set_gdbarch_in_function_epilogue_p (gdbarch,
  706.                                       xstormy16_in_function_epilogue_p);

  707.   /* These values and methods are used when gdb calls a target function.  */
  708.   set_gdbarch_push_dummy_call (gdbarch, xstormy16_push_dummy_call);
  709.   set_gdbarch_breakpoint_from_pc (gdbarch, xstormy16_breakpoint_from_pc);
  710.   set_gdbarch_return_value (gdbarch, xstormy16_return_value);

  711.   set_gdbarch_skip_trampoline_code (gdbarch, xstormy16_skip_trampoline_code);

  712.   set_gdbarch_print_insn (gdbarch, print_insn_xstormy16);

  713.   gdbarch_init_osabi (info, gdbarch);

  714.   dwarf2_append_unwinders (gdbarch);
  715.   frame_unwind_append_unwinder (gdbarch, &xstormy16_frame_unwind);

  716.   return gdbarch;
  717. }

  718. /* Function: _initialize_xstormy16_tdep
  719.    Initializer function for the Sanyo Xstormy16a module.
  720.    Called by gdb at start-up.  */

  721. /* -Wmissing-prototypes */
  722. extern initialize_file_ftype _initialize_xstormy16_tdep;

  723. void
  724. _initialize_xstormy16_tdep (void)
  725. {
  726.   register_gdbarch_init (bfd_arch_xstormy16, xstormy16_gdbarch_init);
  727. }