gdb/tilegx-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the Tilera TILE-Gx processor.

  2.    Copyright (C) 2012-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 "trad-frame.h"
  20. #include "symtab.h"
  21. #include "gdbtypes.h"
  22. #include "gdbcmd.h"
  23. #include "gdbcore.h"
  24. #include "value.h"
  25. #include "dis-asm.h"
  26. #include "inferior.h"
  27. #include "arch-utils.h"
  28. #include "floatformat.h"
  29. #include "regcache.h"
  30. #include "regset.h"
  31. #include "doublest.h"
  32. #include "osabi.h"
  33. #include "linux-tdep.h"
  34. #include "objfiles.h"
  35. #include "solib-svr4.h"
  36. #include "tilegx-tdep.h"
  37. #include "opcode/tilegx.h"

  38. struct tilegx_frame_cache
  39. {
  40.   /* Base address.  */
  41.   CORE_ADDR base;
  42.   /* Function start.  */
  43.   CORE_ADDR start_pc;

  44.   /* Table of saved registers.  */
  45.   struct trad_frame_saved_reg *saved_regs;
  46. };

  47. /* Register state values used by analyze_prologue.  */
  48. enum reverse_state
  49.   {
  50.     REVERSE_STATE_REGISTER,
  51.     REVERSE_STATE_VALUE,
  52.     REVERSE_STATE_UNKNOWN
  53.   };

  54. /* Register state used by analyze_prologue().  */
  55. struct tilegx_reverse_regs
  56. {
  57.   LONGEST value;
  58.   enum reverse_state state;
  59. };

  60. static const struct tilegx_reverse_regs
  61. template_reverse_regs[TILEGX_NUM_PHYS_REGS] =
  62.   {
  63.     { TILEGX_R0_REGNUM,  REVERSE_STATE_REGISTER },
  64.     { TILEGX_R1_REGNUM,  REVERSE_STATE_REGISTER },
  65.     { TILEGX_R2_REGNUM,  REVERSE_STATE_REGISTER },
  66.     { TILEGX_R3_REGNUM,  REVERSE_STATE_REGISTER },
  67.     { TILEGX_R4_REGNUM,  REVERSE_STATE_REGISTER },
  68.     { TILEGX_R5_REGNUM,  REVERSE_STATE_REGISTER },
  69.     { TILEGX_R6_REGNUM,  REVERSE_STATE_REGISTER },
  70.     { TILEGX_R7_REGNUM,  REVERSE_STATE_REGISTER },
  71.     { TILEGX_R8_REGNUM,  REVERSE_STATE_REGISTER },
  72.     { TILEGX_R9_REGNUM,  REVERSE_STATE_REGISTER },
  73.     { TILEGX_R10_REGNUM, REVERSE_STATE_REGISTER },
  74.     { TILEGX_R11_REGNUM, REVERSE_STATE_REGISTER },
  75.     { TILEGX_R12_REGNUM, REVERSE_STATE_REGISTER },
  76.     { TILEGX_R13_REGNUM, REVERSE_STATE_REGISTER },
  77.     { TILEGX_R14_REGNUM, REVERSE_STATE_REGISTER },
  78.     { TILEGX_R15_REGNUM, REVERSE_STATE_REGISTER },
  79.     { TILEGX_R16_REGNUM, REVERSE_STATE_REGISTER },
  80.     { TILEGX_R17_REGNUM, REVERSE_STATE_REGISTER },
  81.     { TILEGX_R18_REGNUM, REVERSE_STATE_REGISTER },
  82.     { TILEGX_R19_REGNUM, REVERSE_STATE_REGISTER },
  83.     { TILEGX_R20_REGNUM, REVERSE_STATE_REGISTER },
  84.     { TILEGX_R21_REGNUM, REVERSE_STATE_REGISTER },
  85.     { TILEGX_R22_REGNUM, REVERSE_STATE_REGISTER },
  86.     { TILEGX_R23_REGNUM, REVERSE_STATE_REGISTER },
  87.     { TILEGX_R24_REGNUM, REVERSE_STATE_REGISTER },
  88.     { TILEGX_R25_REGNUM, REVERSE_STATE_REGISTER },
  89.     { TILEGX_R26_REGNUM, REVERSE_STATE_REGISTER },
  90.     { TILEGX_R27_REGNUM, REVERSE_STATE_REGISTER },
  91.     { TILEGX_R28_REGNUM, REVERSE_STATE_REGISTER },
  92.     { TILEGX_R29_REGNUM, REVERSE_STATE_REGISTER },
  93.     { TILEGX_R30_REGNUM, REVERSE_STATE_REGISTER },
  94.     { TILEGX_R31_REGNUM, REVERSE_STATE_REGISTER },
  95.     { TILEGX_R32_REGNUM, REVERSE_STATE_REGISTER },
  96.     { TILEGX_R33_REGNUM, REVERSE_STATE_REGISTER },
  97.     { TILEGX_R34_REGNUM, REVERSE_STATE_REGISTER },
  98.     { TILEGX_R35_REGNUM, REVERSE_STATE_REGISTER },
  99.     { TILEGX_R36_REGNUM, REVERSE_STATE_REGISTER },
  100.     { TILEGX_R37_REGNUM, REVERSE_STATE_REGISTER },
  101.     { TILEGX_R38_REGNUM, REVERSE_STATE_REGISTER },
  102.     { TILEGX_R39_REGNUM, REVERSE_STATE_REGISTER },
  103.     { TILEGX_R40_REGNUM, REVERSE_STATE_REGISTER },
  104.     { TILEGX_R41_REGNUM, REVERSE_STATE_REGISTER },
  105.     { TILEGX_R42_REGNUM, REVERSE_STATE_REGISTER },
  106.     { TILEGX_R43_REGNUM, REVERSE_STATE_REGISTER },
  107.     { TILEGX_R44_REGNUM, REVERSE_STATE_REGISTER },
  108.     { TILEGX_R45_REGNUM, REVERSE_STATE_REGISTER },
  109.     { TILEGX_R46_REGNUM, REVERSE_STATE_REGISTER },
  110.     { TILEGX_R47_REGNUM, REVERSE_STATE_REGISTER },
  111.     { TILEGX_R48_REGNUM, REVERSE_STATE_REGISTER },
  112.     { TILEGX_R49_REGNUM, REVERSE_STATE_REGISTER },
  113.     { TILEGX_R50_REGNUM, REVERSE_STATE_REGISTER },
  114.     { TILEGX_R51_REGNUM, REVERSE_STATE_REGISTER },
  115.     { TILEGX_R52_REGNUM, REVERSE_STATE_REGISTER },
  116.     { TILEGX_TP_REGNUM,  REVERSE_STATE_REGISTER },
  117.     { TILEGX_SP_REGNUM,  REVERSE_STATE_REGISTER },
  118.     { TILEGX_LR_REGNUM,  REVERSE_STATE_REGISTER },
  119.     { 0, REVERSE_STATE_UNKNOWN },
  120.     { 0, REVERSE_STATE_UNKNOWN },
  121.     { 0, REVERSE_STATE_UNKNOWN },
  122.     { 0, REVERSE_STATE_UNKNOWN },
  123.     { 0, REVERSE_STATE_UNKNOWN },
  124.     { 0, REVERSE_STATE_UNKNOWN },
  125.     { 0, REVERSE_STATE_UNKNOWN },
  126.     { TILEGX_ZERO_REGNUM, REVERSE_STATE_VALUE }
  127.   };

  128. /* Implement the "register_name" gdbarch method.  */

  129. static const char *
  130. tilegx_register_name (struct gdbarch *gdbarch, int regnum)
  131. {
  132.   static const char *const register_names[TILEGX_NUM_REGS] =
  133.     {
  134.       "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
  135.       "r8",   "r9",   "r10""r11""r12""r13""r14""r15",
  136.       "r16""r17""r18""r19""r20""r21""r22""r23",
  137.       "r24""r25""r26""r27""r28""r29""r30""r31",
  138.       "r32""r33""r34""r35""r36""r37""r38""r39",
  139.       "r40""r41""r42""r43""r44""r45""r46""r47",
  140.       "r48""r49""r50""r51""r52""tp",   "sp",   "lr",
  141.       "sn",   "idn0", "idn1", "udn0", "udn1", "udn2", "udn3", "zero",
  142.       "pc",   "faultnum",
  143.     };

  144.   if (regnum < 0 || regnum >= TILEGX_NUM_REGS)
  145.     internal_error (__FILE__, __LINE__,
  146.                     "tilegx_register_name: invalid register number %d",
  147.                     regnum);

  148.   return register_names[regnum];
  149. }

  150. /* This is the implementation of gdbarch method register_type.  */

  151. static struct type *
  152. tilegx_register_type (struct gdbarch *gdbarch, int regnum)
  153. {
  154.   if (regnum == TILEGX_PC_REGNUM)
  155.     return builtin_type (gdbarch)->builtin_func_ptr;
  156.   else
  157.     return builtin_type (gdbarch)->builtin_uint64;
  158. }

  159. /* This is the implementation of gdbarch method dwarf2_reg_to_regnum.  */

  160. static int
  161. tilegx_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
  162. {
  163.   return num;
  164. }

  165. /* Makes the decision of whether a given type is a scalar type.
  166.    Scalar types are returned in the registers r2-r11 as they fit.  */

  167. static int
  168. tilegx_type_is_scalar (struct type *t)
  169. {
  170.   return (TYPE_CODE(t) != TYPE_CODE_STRUCT
  171.           && TYPE_CODE(t) != TYPE_CODE_UNION
  172.           && TYPE_CODE(t) != TYPE_CODE_ARRAY);
  173. }

  174. /* Returns non-zero if the given struct type will be returned using
  175.    a special convention, rather than the normal function return method.
  176.    Used in the context of the "return" command, and target function
  177.    calls from the debugger.  */

  178. static int
  179. tilegx_use_struct_convention (struct type *type)
  180. {
  181.   /* Only scalars which fit in R0 - R9 can be returned in registers.
  182.      Otherwise, they are returned via a pointer passed in R0.  */
  183.   return (!tilegx_type_is_scalar (type)
  184.           && (TYPE_LENGTH (type) > (1 + TILEGX_R9_REGNUM - TILEGX_R0_REGNUM)
  185.               * tilegx_reg_size));
  186. }

  187. /* Find a function's return value in the appropriate registers (in
  188.    REGCACHE), and copy it into VALBUF.  */

  189. static void
  190. tilegx_extract_return_value (struct type *type, struct regcache *regcache,
  191.                              gdb_byte *valbuf)
  192. {
  193.   int len = TYPE_LENGTH (type);
  194.   int i, regnum = TILEGX_R0_REGNUM;

  195.   for (i = 0; i < len; i += tilegx_reg_size)
  196.     regcache_raw_read (regcache, regnum++, valbuf + i);
  197. }

  198. /* Copy the function return value from VALBUF into the proper
  199.    location for a function return.
  200.    Called only in the context of the "return" command.  */

  201. static void
  202. tilegx_store_return_value (struct type *type, struct regcache *regcache,
  203.                            const void *valbuf)
  204. {
  205.   if (TYPE_LENGTH (type) < tilegx_reg_size)
  206.     {
  207.       /* Add leading zeros to the (little-endian) value.  */
  208.       gdb_byte buf[tilegx_reg_size] = { 0 };

  209.       memcpy (buf, valbuf, TYPE_LENGTH (type));
  210.       regcache_raw_write (regcache, TILEGX_R0_REGNUM, buf);
  211.     }
  212.   else
  213.     {
  214.       int len = TYPE_LENGTH (type);
  215.       int i, regnum = TILEGX_R0_REGNUM;

  216.       for (i = 0; i < len; i += tilegx_reg_size)
  217.         regcache_raw_write (regcache, regnum++, (gdb_byte *) valbuf + i);
  218.     }
  219. }

  220. /* This is the implementation of gdbarch method return_value.  */

  221. static enum return_value_convention
  222. tilegx_return_value (struct gdbarch *gdbarch, struct value *function,
  223.                      struct type *type, struct regcache *regcache,
  224.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  225. {
  226.   if (tilegx_use_struct_convention (type))
  227.     return RETURN_VALUE_STRUCT_CONVENTION;
  228.   if (writebuf)
  229.     tilegx_store_return_value (type, regcache, writebuf);
  230.   else if (readbuf)
  231.     tilegx_extract_return_value (type, regcache, readbuf);
  232.   return RETURN_VALUE_REGISTER_CONVENTION;
  233. }

  234. /* This is the implementation of gdbarch method frame_align.  */

  235. static CORE_ADDR
  236. tilegx_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  237. {
  238.   return addr & -8;
  239. }


  240. /* Implement the "push_dummy_call" gdbarch method.  */

  241. static CORE_ADDR
  242. tilegx_push_dummy_call (struct gdbarch *gdbarch,
  243.                         struct value *function,
  244.                         struct regcache *regcache,
  245.                         CORE_ADDR bp_addr, int nargs,
  246.                         struct value **args,
  247.                         CORE_ADDR sp, int struct_return,
  248.                         CORE_ADDR struct_addr)
  249. {
  250.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  251.   CORE_ADDR stack_dest = sp;
  252.   int argreg = TILEGX_R0_REGNUM;
  253.   int i, j;
  254.   int typelen, slacklen, alignlen;
  255.   static const gdb_byte four_zero_words[16] = { 0 };

  256.   /* If struct_return is 1, then the struct return address will
  257.      consume one argument-passing register.  */
  258.   if (struct_return)
  259.     regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);

  260.   /* Arguments are passed in R0 - R9, and as soon as an argument
  261.      will not fit completely in the remaining registers, then it,
  262.      and all remaining arguments, are put on the stack.  */
  263.   for (i = 0; i < nargs && argreg <= TILEGX_R9_REGNUM; i++)
  264.     {
  265.       const gdb_byte *val;
  266.       typelen = TYPE_LENGTH (value_enclosing_type (args[i]));

  267.       if (typelen > (TILEGX_R9_REGNUM - argreg + 1) * tilegx_reg_size)
  268.         break;

  269.       /* Put argument into registers wordwise.        */
  270.       val = value_contents (args[i]);
  271.       for (j = 0; j < typelen; j += tilegx_reg_size)
  272.         {
  273.           /* ISSUE: Why special handling for "typelen = 4x + 1"?
  274.              I don't ever see "typelen" values except 4 and 8.        */
  275.           int n = (typelen - j == 1) ? 1 : tilegx_reg_size;
  276.           ULONGEST w = extract_unsigned_integer (val + j, n, byte_order);

  277.           regcache_cooked_write_unsigned (regcache, argreg++, w);
  278.         }
  279.     }

  280.   /* Align SP.  */
  281.   stack_dest = tilegx_frame_align (gdbarch, stack_dest);

  282.   /* Loop backwards through remaining arguments and push them on
  283.      the stack, word aligned.  */
  284.   for (j = nargs - 1; j >= i; j--)
  285.     {
  286.       gdb_byte *val;
  287.       struct cleanup *back_to;
  288.       const gdb_byte *contents = value_contents (args[j]);

  289.       typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
  290.       slacklen = align_up (typelen, 8) - typelen;
  291.       val = xmalloc (typelen + slacklen);
  292.       back_to = make_cleanup (xfree, val);
  293.       memcpy (val, contents, typelen);
  294.       memset (val + typelen, 0, slacklen);

  295.       /* Now write data to the stack.  The stack grows downwards.  */
  296.       stack_dest -= typelen + slacklen;
  297.       write_memory (stack_dest, val, typelen + slacklen);
  298.       do_cleanups (back_to);
  299.     }

  300.   /* Add 16 bytes for linkage space to the stack.  */
  301.   stack_dest = stack_dest - 16;
  302.   write_memory (stack_dest, four_zero_words, 16);

  303.   /* Update stack pointer.  */
  304.   regcache_cooked_write_unsigned (regcache, TILEGX_SP_REGNUM, stack_dest);

  305.   /* Set the return address register to point to the entry point of
  306.      the program, where a breakpoint lies in wait.  */
  307.   regcache_cooked_write_unsigned (regcache, TILEGX_LR_REGNUM, bp_addr);

  308.   return stack_dest;
  309. }


  310. /* Decode the instructions within the given address range.
  311.    Decide when we must have reached the end of the function prologue.
  312.    If a frame_info pointer is provided, fill in its saved_regs etc.
  313.    Returns the address of the first instruction after the prologue.
  314.    NOTE: This is often called with start_addr being the start of some
  315.    function, and end_addr being the current PC.  */

  316. static CORE_ADDR
  317. tilegx_analyze_prologue (struct gdbarch* gdbarch,
  318.                          CORE_ADDR start_addr, CORE_ADDR end_addr,
  319.                          struct tilegx_frame_cache *cache,
  320.                          struct frame_info *next_frame)
  321. {
  322.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  323.   CORE_ADDR next_addr;
  324.   CORE_ADDR prolog_end = end_addr;
  325.   ULONGEST inst, inst2;
  326.   LONGEST offset;
  327.   int regnum;
  328.   gdb_byte instbuf[32 * TILEGX_BUNDLE_SIZE_IN_BYTES];
  329.   CORE_ADDR instbuf_start;
  330.   unsigned int instbuf_size;
  331.   int status;
  332.   bfd_uint64_t bundle;
  333.   struct tilegx_decoded_instruction
  334.     decoded[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
  335.   int num_insns;
  336.   struct tilegx_reverse_regs reverse_frame[TILEGX_NUM_PHYS_REGS];
  337.   struct tilegx_reverse_regs
  338.     new_reverse_frame[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
  339.   int dest_regs[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
  340.   int reverse_frame_valid, prolog_done, branch_seen, lr_saved_on_stack_p;
  341.   LONGEST prev_sp_value;
  342.   int i, j;

  343.   if (start_addr >= end_addr
  344.       || (start_addr % TILEGX_BUNDLE_ALIGNMENT_IN_BYTES) != 0)
  345.     return end_addr;

  346.   /* Initialize the reverse frame.  This maps the CURRENT frame's
  347.      registers to the outer frame's registers (the frame on the
  348.      stack goes the other way).  */
  349.   memcpy (&reverse_frame, &template_reverse_regs, sizeof (reverse_frame));

  350.   prolog_done = 0;
  351.   branch_seen = 0;
  352.   prev_sp_value = 0;
  353.   lr_saved_on_stack_p = 0;

  354.   /* To cut down on round-trip overhead, we fetch multiple bundles
  355.      at once.  These variables describe the range of memory we have
  356.      prefetched.  */
  357.   instbuf_start = 0;
  358.   instbuf_size = 0;

  359.   for (next_addr = start_addr;
  360.        next_addr < end_addr;
  361.        next_addr += TILEGX_BUNDLE_SIZE_IN_BYTES)
  362.     {
  363.       /* Retrieve the next instruction.  */
  364.       if (next_addr - instbuf_start >= instbuf_size)
  365.         {
  366.           /* Figure out how many bytes to fetch.  Don't span a page
  367.              boundary since that might cause an unnecessary memory
  368.              error.  */
  369.           unsigned int size_on_same_page = 4096 - (next_addr & 4095);

  370.           instbuf_size = sizeof instbuf;

  371.           if (instbuf_size > size_on_same_page)
  372.             instbuf_size = size_on_same_page;

  373.           instbuf_size = min (instbuf_size, (end_addr - next_addr));
  374.           instbuf_start = next_addr;

  375.           status = safe_frame_unwind_memory (next_frame, instbuf_start,
  376.                                              instbuf, instbuf_size);
  377.           if (status == 0)
  378.             memory_error (status, next_addr);
  379.         }

  380.       reverse_frame_valid = 0;

  381.       bundle = extract_unsigned_integer (&instbuf[next_addr - instbuf_start],
  382.                                          8, byte_order);

  383.       num_insns = parse_insn_tilegx (bundle, next_addr, decoded);

  384.       for (i = 0; i < num_insns; i++)
  385.         {
  386.           struct tilegx_decoded_instruction *this_insn = &decoded[i];
  387.           int64_t *operands = (int64_t *) this_insn->operand_values;
  388.           const struct tilegx_opcode *opcode = this_insn->opcode;

  389.           switch (opcode->mnemonic)
  390.             {
  391.             case TILEGX_OPC_ST:
  392.               if (cache
  393.                   && reverse_frame[operands[0]].state == REVERSE_STATE_VALUE
  394.                   && reverse_frame[operands[1]].state
  395.                   == REVERSE_STATE_REGISTER)
  396.                 {
  397.                   LONGEST saved_address = reverse_frame[operands[0]].value;
  398.                   unsigned saved_register
  399.                     = (unsigned) reverse_frame[operands[1]].value;

  400.                   /* realreg >= 0 and addr != -1 indicates that the
  401.                      value of saved_register is in memory location
  402.                      saved_address.  The value of realreg is not
  403.                      meaningful in this case but it must be >= 0.
  404.                      See trad-frame.h.  */
  405.                   cache->saved_regs[saved_register].realreg = saved_register;
  406.                   cache->saved_regs[saved_register].addr = saved_address;
  407.                 }
  408.               else if (cache
  409.                        && (operands[0] == TILEGX_SP_REGNUM)
  410.                        && (operands[1] == TILEGX_LR_REGNUM))
  411.                 lr_saved_on_stack_p = 1;
  412.               break;
  413.             case TILEGX_OPC_ADDI:
  414.             case TILEGX_OPC_ADDLI:
  415.               if (cache
  416.                   && operands[0] == TILEGX_SP_REGNUM
  417.                   && operands[1] == TILEGX_SP_REGNUM
  418.                   && reverse_frame[operands[1]].state == REVERSE_STATE_REGISTER)
  419.                 {
  420.                   /* Special case.  We're fixing up the stack frame.  */
  421.                   uint64_t hopefully_sp
  422.                     = (unsigned) reverse_frame[operands[1]].value;
  423.                   short op2_as_short = (short) operands[2];
  424.                   signed char op2_as_char = (signed char) operands[2];

  425.                   /* Fix up the sign-extension.  */
  426.                   if (opcode->mnemonic == TILEGX_OPC_ADDI)
  427.                     op2_as_short = op2_as_char;
  428.                   prev_sp_value = (cache->saved_regs[hopefully_sp].addr
  429.                                    - op2_as_short);

  430.                   new_reverse_frame[i].state = REVERSE_STATE_VALUE;
  431.                   new_reverse_frame[i].value
  432.                     = cache->saved_regs[hopefully_sp].addr;
  433.                   trad_frame_set_value (cache->saved_regs,
  434.                                         hopefully_sp, prev_sp_value);
  435.                 }
  436.               else
  437.                 {
  438.                   short op2_as_short = (short) operands[2];
  439.                   signed char op2_as_char = (signed char) operands[2];

  440.                   /* Fix up the sign-extension.  */
  441.                   if (opcode->mnemonic == TILEGX_OPC_ADDI)
  442.                     op2_as_short = op2_as_char;

  443.                   new_reverse_frame[i] = reverse_frame[operands[1]];
  444.                   if (new_reverse_frame[i].state == REVERSE_STATE_VALUE)
  445.                     new_reverse_frame[i].value += op2_as_short;
  446.                   else
  447.                     new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  448.                 }
  449.               reverse_frame_valid |= 1 << i;
  450.               dest_regs[i] = operands[0];
  451.               break;
  452.             case TILEGX_OPC_ADD:
  453.               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
  454.                   && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
  455.                 {
  456.                   /* We have values -- we can do this.  */
  457.                   new_reverse_frame[i] = reverse_frame[operands[2]];
  458.                   new_reverse_frame[i].value
  459.                     += reverse_frame[operands[i]].value;
  460.                 }
  461.               else
  462.                 {
  463.                   /* We don't know anything about the values.  Punt.  */
  464.                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  465.                 }
  466.               reverse_frame_valid |= 1 << i;
  467.               dest_regs[i] = operands[0];
  468.               break;
  469.             case TILEGX_OPC_MOVE:
  470.               new_reverse_frame[i] = reverse_frame[operands[1]];
  471.               reverse_frame_valid |= 1 << i;
  472.               dest_regs[i] = operands[0];
  473.               break;
  474.             case TILEGX_OPC_MOVEI:
  475.             case TILEGX_OPC_MOVELI:
  476.               new_reverse_frame[i].state = REVERSE_STATE_VALUE;
  477.               new_reverse_frame[i].value = operands[1];
  478.               reverse_frame_valid |= 1 << i;
  479.               dest_regs[i] = operands[0];
  480.               break;
  481.             case TILEGX_OPC_ORI:
  482.               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE)
  483.                 {
  484.                   /* We have a value in A -- we can do this.  */
  485.                   new_reverse_frame[i] = reverse_frame[operands[1]];
  486.                   new_reverse_frame[i].value
  487.                     = reverse_frame[operands[1]].value | operands[2];
  488.                 }
  489.               else if (operands[2] == 0)
  490.                 {
  491.                   /* This is a move.  */
  492.                   new_reverse_frame[i] = reverse_frame[operands[1]];
  493.                 }
  494.               else
  495.                 {
  496.                   /* We don't know anything about the values.  Punt.  */
  497.                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  498.                 }
  499.               reverse_frame_valid |= 1 << i;
  500.               dest_regs[i] = operands[0];
  501.               break;
  502.             case TILEGX_OPC_OR:
  503.               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
  504.                   && reverse_frame[operands[1]].value == 0)
  505.                 {
  506.                   /* This is a move.  */
  507.                   new_reverse_frame[i] = reverse_frame[operands[2]];
  508.                 }
  509.               else if (reverse_frame[operands[2]].state == REVERSE_STATE_VALUE
  510.                        && reverse_frame[operands[2]].value == 0)
  511.                 {
  512.                   /* This is a move.  */
  513.                   new_reverse_frame[i] = reverse_frame[operands[1]];
  514.                 }
  515.               else
  516.                 {
  517.                   /* We don't know anything about the values.  Punt.  */
  518.                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  519.                 }
  520.               reverse_frame_valid |= 1 << i;
  521.               dest_regs[i] = operands[0];
  522.               break;
  523.             case TILEGX_OPC_SUB:
  524.               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
  525.                   && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
  526.                 {
  527.                   /* We have values -- we can do this.  */
  528.                   new_reverse_frame[i] = reverse_frame[operands[1]];
  529.                   new_reverse_frame[i].value
  530.                     -= reverse_frame[operands[2]].value;
  531.                 }
  532.               else
  533.                 {
  534.                   /* We don't know anything about the values.  Punt.  */
  535.                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  536.                 }
  537.               reverse_frame_valid |= 1 << i;
  538.               dest_regs[i] = operands[0];
  539.               break;

  540.             case TILEGX_OPC_FNOP:
  541.             case TILEGX_OPC_INFO:
  542.             case TILEGX_OPC_INFOL:
  543.               /* Nothing to see here, move on.
  544.                  Note that real NOP is treated as a 'real' instruction
  545.                  because someone must have intended that it be there.
  546.                  It therefore terminates the prolog.  */
  547.               break;

  548.             case TILEGX_OPC_J:
  549.             case TILEGX_OPC_JAL:

  550.             case TILEGX_OPC_BEQZ:
  551.             case TILEGX_OPC_BEQZT:
  552.             case TILEGX_OPC_BGEZ:
  553.             case TILEGX_OPC_BGEZT:
  554.             case TILEGX_OPC_BGTZ:
  555.             case TILEGX_OPC_BGTZT:
  556.             case TILEGX_OPC_BLBC:
  557.             case TILEGX_OPC_BLBCT:
  558.             case TILEGX_OPC_BLBS:
  559.             case TILEGX_OPC_BLBST:
  560.             case TILEGX_OPC_BLEZ:
  561.             case TILEGX_OPC_BLEZT:
  562.             case TILEGX_OPC_BLTZ:
  563.             case TILEGX_OPC_BLTZT:
  564.             case TILEGX_OPC_BNEZ:
  565.             case TILEGX_OPC_BNEZT:

  566.             case TILEGX_OPC_IRET:
  567.             case TILEGX_OPC_JALR:
  568.             case TILEGX_OPC_JALRP:
  569.             case TILEGX_OPC_JR:
  570.             case TILEGX_OPC_JRP:
  571.             case TILEGX_OPC_SWINT0:
  572.             case TILEGX_OPC_SWINT1:
  573.             case TILEGX_OPC_SWINT2:
  574.             case TILEGX_OPC_SWINT3:
  575.               /* We're really done -- this is a branch.  */
  576.               branch_seen = 1;
  577.               prolog_done = 1;
  578.               break;
  579.             default:
  580.               /* We don't know or care what this instruction is.
  581.                  All we know is that it isn't part of a prolog, and if
  582.                  there's a destination register, we're trashing it.  */
  583.               prolog_done = 1;
  584.               for (j = 0; j < opcode->num_operands; j++)
  585.                 {
  586.                   if (this_insn->operands[j]->is_dest_reg)
  587.                     {
  588.                       dest_regs[i] = operands[j];
  589.                       new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  590.                       reverse_frame_valid |= 1 << i;
  591.                       break;
  592.                     }
  593.                 }
  594.               break;
  595.             }
  596.         }

  597.       /* Now update the reverse frames.  */
  598.       for (i = 0; i < num_insns; i++)
  599.         {
  600.           /* ISSUE: Does this properly handle "network" registers?  */
  601.           if ((reverse_frame_valid & (1 << i))
  602.               && dest_regs[i] != TILEGX_ZERO_REGNUM)
  603.             reverse_frame[dest_regs[i]] = new_reverse_frame[i];
  604.         }

  605.       if (prev_sp_value != 0)
  606.         {
  607.           /* GCC uses R52 as a frame pointer.  Have we seen "move r52, sp"?  */
  608.           if (reverse_frame[TILEGX_R52_REGNUM].state == REVERSE_STATE_REGISTER
  609.               && reverse_frame[TILEGX_R52_REGNUM].value == TILEGX_SP_REGNUM)
  610.           {
  611.             reverse_frame[TILEGX_R52_REGNUM].state = REVERSE_STATE_VALUE;
  612.             reverse_frame[TILEGX_R52_REGNUM].value = prev_sp_value;
  613.           }

  614.           prev_sp_value = 0;
  615.         }

  616.       if (prolog_done && prolog_end == end_addr)
  617.         {
  618.           /* We found non-prolog code.        As such, _this_ instruction
  619.              is the one after the prolog.  We keep processing, because
  620.              there may be more prolog code in there, but this is what
  621.              we'll return.  */
  622.           /* ISSUE: There may not have actually been a prologue, and
  623.              we may have simply skipped some random instructions.  */
  624.           prolog_end = next_addr;
  625.         }
  626.       if (branch_seen)
  627.         {
  628.           /* We saw a branch.  The prolog absolutely must be over.  */
  629.           break;
  630.         }
  631.     }

  632.   if (prolog_end == end_addr && cache)
  633.     {
  634.       /* We may have terminated the prolog early, and we're certainly
  635.          at THIS point right now.  It's possible that the values of
  636.          registers we need are currently actually in other registers
  637.          (and haven't been written to memory yet).  Go find them.  */
  638.       for (i = 0; i < TILEGX_NUM_PHYS_REGS; i++)
  639.         {
  640.           if (reverse_frame[i].state == REVERSE_STATE_REGISTER
  641.               && reverse_frame[i].value != i)
  642.             {
  643.               unsigned saved_register = (unsigned) reverse_frame[i].value;

  644.               cache->saved_regs[saved_register].realreg = i;
  645.               cache->saved_regs[saved_register].addr = (LONGEST) -1;
  646.             }
  647.         }
  648.     }

  649.   if (lr_saved_on_stack_p)
  650.     {
  651.       cache->saved_regs[TILEGX_LR_REGNUM].realreg = TILEGX_LR_REGNUM;
  652.       cache->saved_regs[TILEGX_LR_REGNUM].addr =
  653.         cache->saved_regs[TILEGX_SP_REGNUM].addr;
  654.     }

  655.   return prolog_end;
  656. }

  657. /* This is the implementation of gdbarch method skip_prologue.  */

  658. static CORE_ADDR
  659. tilegx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  660. {
  661.   CORE_ADDR func_start, end_pc;
  662.   struct obj_section *s;

  663.   /* This is the preferred method, find the end of the prologue by
  664.      using the debugging information.  */
  665.   if (find_pc_partial_function (start_pc, NULL, &func_start, NULL))
  666.     {
  667.       CORE_ADDR post_prologue_pc
  668.         = skip_prologue_using_sal (gdbarch, func_start);

  669.       if (post_prologue_pc != 0)
  670.         return max (start_pc, post_prologue_pc);
  671.     }

  672.   /* Don't straddle a section boundary.  */
  673.   s = find_pc_section (start_pc);
  674.   end_pc = start_pc + 8 * TILEGX_BUNDLE_SIZE_IN_BYTES;
  675.   if (s != NULL)
  676.     end_pc = min (end_pc, obj_section_endaddr (s));

  677.   /* Otherwise, try to skip prologue the hard way.  */
  678.   return tilegx_analyze_prologue (gdbarch,
  679.                                   start_pc,
  680.                                   end_pc,
  681.                                   NULL, NULL);
  682. }

  683. /* This is the implementation of gdbarch method in_function_epilogue_p.  */

  684. static int
  685. tilegx_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  686. {
  687.   CORE_ADDR func_addr = 0, func_end = 0;

  688.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  689.     {
  690.       ULONGEST inst, inst2;
  691.       CORE_ADDR addr = func_end - TILEGX_BUNDLE_SIZE_IN_BYTES;

  692.       /* FIXME: Find the actual epilogue.  */
  693.       /* HACK: Just assume the final bundle is the "ret" instruction".  */
  694.       if (pc > addr)
  695.         return 1;
  696.     }
  697.   return 0;
  698. }

  699. /* This is the implementation of gdbarch method get_longjmp_target.  */

  700. static int
  701. tilegx_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  702. {
  703.   struct gdbarch *gdbarch = get_frame_arch (frame);
  704.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  705.   CORE_ADDR jb_addr;
  706.   gdb_byte buf[8];

  707.   jb_addr = get_frame_register_unsigned (frame, TILEGX_R0_REGNUM);

  708.   /* TileGX jmp_buf contains 32 elements of type __uint_reg_t which
  709.      has a size of 8 bytes.  The return address is stored in the 25th
  710.      slot.  */
  711.   if (target_read_memory (jb_addr + 25 * 8, buf, 8))
  712.     return 0;

  713.   *pc = extract_unsigned_integer (buf, 8, byte_order);

  714.   return 1;
  715. }

  716. /* by assigning the 'faultnum' reg in kernel pt_regs with this value,
  717.    kernel do_signal will not check r0. see tilegx kernel/signal.c
  718.    for details.  */
  719. #define INT_SWINT_1_SIGRETURN (~0)

  720. /* Implement the "write_pc" gdbarch method.  */

  721. static void
  722. tilegx_write_pc (struct regcache *regcache, CORE_ADDR pc)
  723. {
  724.   regcache_cooked_write_unsigned (regcache, TILEGX_PC_REGNUM, pc);

  725.   /* We must be careful with modifying the program counter.  If we
  726.      just interrupted a system call, the kernel might try to restart
  727.      it when we resume the inferior.  On restarting the system call,
  728.      the kernel will try backing up the program counter even though it
  729.      no longer points at the system call.  This typically results in a
  730.      SIGSEGV or SIGILL.  We can prevent this by writing INT_SWINT_1_SIGRETURN
  731.      in the "faultnum" pseudo-register.

  732.      Note that "faultnum" is saved when setting up a dummy call frame.
  733.      This means that it is properly restored when that frame is
  734.      popped, and that the interrupted system call will be restarted
  735.      when we resume the inferior on return from a function call from
  736.      within GDB.  In all other cases the system call will not be
  737.      restarted.  */
  738.   regcache_cooked_write_unsigned (regcache, TILEGX_FAULTNUM_REGNUM,
  739.                                   INT_SWINT_1_SIGRETURN);
  740. }

  741. /* This is the implementation of gdbarch method breakpoint_from_pc.  */

  742. static const unsigned char *
  743. tilegx_breakpoint_from_pc (struct gdbarch *gdbarch,
  744.                            CORE_ADDR *pcptr, int *lenptr)
  745. {
  746.   /* 64-bit pattern for a { bpt ; nop } bundle.  */
  747.   static const unsigned char breakpoint[] =
  748.     { 0x00, 0x50, 0x48, 0x51, 0xae, 0x44, 0x6a, 0x28 };

  749.   *lenptr = sizeof (breakpoint);
  750.   return breakpoint;
  751. }

  752. /* Normal frames.  */

  753. static struct tilegx_frame_cache *
  754. tilegx_frame_cache (struct frame_info *this_frame, void **this_cache)
  755. {
  756.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  757.   struct tilegx_frame_cache *cache;
  758.   CORE_ADDR current_pc;
  759.   int i;

  760.   if (*this_cache)
  761.     return *this_cache;

  762.   cache = FRAME_OBSTACK_ZALLOC (struct tilegx_frame_cache);
  763.   *this_cache = cache;
  764.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  765.   cache->base = 0;
  766.   cache->start_pc = get_frame_func (this_frame);
  767.   current_pc = get_frame_pc (this_frame);

  768.   cache->base = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
  769.   trad_frame_set_value (cache->saved_regs, TILEGX_SP_REGNUM, cache->base);

  770.   if (cache->start_pc)
  771.     tilegx_analyze_prologue (gdbarch, cache->start_pc, current_pc,
  772.                              cache, this_frame);

  773.   cache->saved_regs[TILEGX_PC_REGNUM] = cache->saved_regs[TILEGX_LR_REGNUM];

  774.   return cache;
  775. }

  776. /* Retrieve the value of REGNUM in FRAME.  */

  777. static struct value*
  778. tilegx_frame_prev_register (struct frame_info *this_frame,
  779.                             void **this_cache,
  780.                             int regnum)
  781. {
  782.   struct tilegx_frame_cache *info =
  783.     tilegx_frame_cache (this_frame, this_cache);

  784.   return trad_frame_get_prev_register (this_frame, info->saved_regs,
  785.                                        regnum);
  786. }

  787. /* Build frame id.  */

  788. static void
  789. tilegx_frame_this_id (struct frame_info *this_frame, void **this_cache,
  790.                       struct frame_id *this_id)
  791. {
  792.   struct tilegx_frame_cache *info =
  793.     tilegx_frame_cache (this_frame, this_cache);

  794.   /* This marks the outermost frame.  */
  795.   if (info->base == 0)
  796.     return;

  797.   (*this_id) = frame_id_build (info->base, info->start_pc);
  798. }

  799. static CORE_ADDR
  800. tilegx_frame_base_address (struct frame_info *this_frame, void **this_cache)
  801. {
  802.   struct tilegx_frame_cache *cache =
  803.     tilegx_frame_cache (this_frame, this_cache);

  804.   return cache->base;
  805. }

  806. static const struct frame_unwind tilegx_frame_unwind = {
  807.   NORMAL_FRAME,
  808.   default_frame_unwind_stop_reason,
  809.   tilegx_frame_this_id,
  810.   tilegx_frame_prev_register,
  811.   NULL,                        /* const struct frame_data *unwind_data  */
  812.   default_frame_sniffer,       /* frame_sniffer_ftype *sniffer  */
  813.   NULL                         /* frame_prev_pc_ftype *prev_pc  */
  814. };

  815. static const struct frame_base tilegx_frame_base = {
  816.   &tilegx_frame_unwind,
  817.   tilegx_frame_base_address,
  818.   tilegx_frame_base_address,
  819.   tilegx_frame_base_address
  820. };

  821. static CORE_ADDR
  822. tilegx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  823. {
  824.   return frame_unwind_register_unsigned (next_frame, TILEGX_SP_REGNUM);
  825. }

  826. static CORE_ADDR
  827. tilegx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  828. {
  829.   return frame_unwind_register_unsigned (next_frame, TILEGX_PC_REGNUM);
  830. }

  831. static struct frame_id
  832. tilegx_unwind_dummy_id (struct gdbarch *gdbarch,
  833.                         struct frame_info *this_frame)
  834. {
  835.   CORE_ADDR sp;

  836.   sp = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
  837.   return frame_id_build (sp, get_frame_pc (this_frame));
  838. }


  839. /* We cannot read/write the "special" registers.  */

  840. static int
  841. tilegx_cannot_reference_register (struct gdbarch *gdbarch, int regno)
  842. {
  843.   if (regno >= 0 && regno < TILEGX_NUM_EASY_REGS)
  844.     return 0;
  845.   else if (regno == TILEGX_PC_REGNUM
  846.            || regno == TILEGX_FAULTNUM_REGNUM)
  847.     return 0;
  848.   else
  849.     return 1;
  850. }

  851. static struct gdbarch *
  852. tilegx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  853. {
  854.   struct gdbarch *gdbarch;
  855.   int arch_size = 64;

  856.   /* Handle arch_size == 32 or 64.  Default to 64.  */
  857.   if (info.abfd)
  858.     arch_size = bfd_get_arch_size (info.abfd);

  859.   /* Try to find a pre-existing architecture.  */
  860.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  861.        arches != NULL;
  862.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  863.     {
  864.       /* We only have two flavors -- just make sure arch_size matches.  */
  865.       if (gdbarch_ptr_bit (arches->gdbarch) == arch_size)
  866.         return (arches->gdbarch);
  867.     }

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

  869.   /* Basic register fields and methods, datatype sizes and stuff.  */

  870.   /* There are 64 physical registers which can be referenced by
  871.      instructions (although only 56 of them can actually be
  872.      debugged) and 1 magic register (the PC).  The other three
  873.      magic registers (ex1, syscall, orig_r0) which are known to
  874.      "ptrace" are ignored by "gdb".  Note that we simply pretend
  875.      that there are 65 registers, and no "pseudo registers".  */
  876.   set_gdbarch_num_regs (gdbarch, TILEGX_NUM_REGS);
  877.   set_gdbarch_num_pseudo_regs (gdbarch, 0);

  878.   set_gdbarch_sp_regnum (gdbarch, TILEGX_SP_REGNUM);
  879.   set_gdbarch_pc_regnum (gdbarch, TILEGX_PC_REGNUM);

  880.   set_gdbarch_register_name (gdbarch, tilegx_register_name);
  881.   set_gdbarch_register_type (gdbarch, tilegx_register_type);

  882.   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  883.   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  884.   set_gdbarch_long_bit (gdbarch, arch_size);
  885.   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  886.   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  887.   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  888.   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  889.   set_gdbarch_ptr_bit (gdbarch, arch_size);
  890.   set_gdbarch_addr_bit (gdbarch, arch_size);

  891.   set_gdbarch_cannot_fetch_register (gdbarch,
  892.                                      tilegx_cannot_reference_register);
  893.   set_gdbarch_cannot_store_register (gdbarch,
  894.                                      tilegx_cannot_reference_register);

  895.   /* Stack grows down.  */
  896.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  897.   /* Frame Info.  */
  898.   set_gdbarch_unwind_sp (gdbarch, tilegx_unwind_sp);
  899.   set_gdbarch_unwind_pc (gdbarch, tilegx_unwind_pc);
  900.   set_gdbarch_dummy_id (gdbarch, tilegx_unwind_dummy_id);
  901.   set_gdbarch_frame_align (gdbarch, tilegx_frame_align);
  902.   frame_base_set_default (gdbarch, &tilegx_frame_base);

  903.   set_gdbarch_skip_prologue (gdbarch, tilegx_skip_prologue);

  904.   set_gdbarch_in_function_epilogue_p (gdbarch,
  905.                                       tilegx_in_function_epilogue_p);

  906.   /* Map debug registers into internal register numbers.  */
  907.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, tilegx_dwarf2_reg_to_regnum);

  908.   /* These values and methods are used when gdb calls a target function.  */
  909.   set_gdbarch_push_dummy_call (gdbarch, tilegx_push_dummy_call);
  910.   set_gdbarch_get_longjmp_target (gdbarch, tilegx_get_longjmp_target);
  911.   set_gdbarch_write_pc (gdbarch, tilegx_write_pc);
  912.   set_gdbarch_breakpoint_from_pc (gdbarch, tilegx_breakpoint_from_pc);
  913.   set_gdbarch_return_value (gdbarch, tilegx_return_value);

  914.   set_gdbarch_print_insn (gdbarch, print_insn_tilegx);

  915.   gdbarch_init_osabi (info, gdbarch);

  916.   dwarf2_append_unwinders (gdbarch);
  917.   frame_unwind_append_unwinder (gdbarch, &tilegx_frame_unwind);

  918.   return gdbarch;
  919. }

  920. /* Provide a prototype to silence -Wmissing-prototypes.  */
  921. extern initialize_file_ftype _initialize_tilegx_tdep;

  922. void
  923. _initialize_tilegx_tdep (void)
  924. {
  925.   register_gdbarch_init (bfd_arch_tilegx, tilegx_gdbarch_init);
  926. }