GDB (API)
/home/stan/gdb/src/gdb/tilegx-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for the Tilera TILE-Gx processor.
00002 
00003    Copyright (C) 2012-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "frame.h"
00022 #include "frame-base.h"
00023 #include "frame-unwind.h"
00024 #include "dwarf2-frame.h"
00025 #include "trad-frame.h"
00026 #include "symtab.h"
00027 #include "gdbtypes.h"
00028 #include "gdbcmd.h"
00029 #include "gdbcore.h"
00030 #include "value.h"
00031 #include "dis-asm.h"
00032 #include "inferior.h"
00033 #include "gdb_string.h"
00034 #include "gdb_assert.h"
00035 #include "arch-utils.h"
00036 #include "floatformat.h"
00037 #include "regcache.h"
00038 #include "regset.h"
00039 #include "doublest.h"
00040 #include "osabi.h"
00041 #include "linux-tdep.h"
00042 #include "objfiles.h"
00043 #include "solib-svr4.h"
00044 #include "symtab.h"
00045 #include "tilegx-tdep.h"
00046 #include "opcode/tilegx.h"
00047 
00048 struct tilegx_frame_cache
00049 {
00050   /* Base address.  */
00051   CORE_ADDR base;
00052   /* Function start.  */
00053   CORE_ADDR start_pc;
00054 
00055   /* Table of saved registers.  */
00056   struct trad_frame_saved_reg *saved_regs;
00057 };
00058 
00059 /* Register state values used by analyze_prologue.  */
00060 enum reverse_state
00061   {
00062     REVERSE_STATE_REGISTER,
00063     REVERSE_STATE_VALUE,
00064     REVERSE_STATE_UNKNOWN
00065   };
00066 
00067 /* Register state used by analyze_prologue().  */
00068 struct tilegx_reverse_regs
00069 {
00070   LONGEST value;
00071   enum reverse_state state;
00072 };
00073 
00074 static const struct tilegx_reverse_regs
00075 template_reverse_regs[TILEGX_NUM_PHYS_REGS] =
00076   {
00077     { TILEGX_R0_REGNUM,  REVERSE_STATE_REGISTER },
00078     { TILEGX_R1_REGNUM,  REVERSE_STATE_REGISTER },
00079     { TILEGX_R2_REGNUM,  REVERSE_STATE_REGISTER },
00080     { TILEGX_R3_REGNUM,  REVERSE_STATE_REGISTER },
00081     { TILEGX_R4_REGNUM,  REVERSE_STATE_REGISTER },
00082     { TILEGX_R5_REGNUM,  REVERSE_STATE_REGISTER },
00083     { TILEGX_R6_REGNUM,  REVERSE_STATE_REGISTER },
00084     { TILEGX_R7_REGNUM,  REVERSE_STATE_REGISTER },
00085     { TILEGX_R8_REGNUM,  REVERSE_STATE_REGISTER },
00086     { TILEGX_R9_REGNUM,  REVERSE_STATE_REGISTER },
00087     { TILEGX_R10_REGNUM, REVERSE_STATE_REGISTER },
00088     { TILEGX_R11_REGNUM, REVERSE_STATE_REGISTER },
00089     { TILEGX_R12_REGNUM, REVERSE_STATE_REGISTER },
00090     { TILEGX_R13_REGNUM, REVERSE_STATE_REGISTER },
00091     { TILEGX_R14_REGNUM, REVERSE_STATE_REGISTER },
00092     { TILEGX_R15_REGNUM, REVERSE_STATE_REGISTER },
00093     { TILEGX_R16_REGNUM, REVERSE_STATE_REGISTER },
00094     { TILEGX_R17_REGNUM, REVERSE_STATE_REGISTER },
00095     { TILEGX_R18_REGNUM, REVERSE_STATE_REGISTER },
00096     { TILEGX_R19_REGNUM, REVERSE_STATE_REGISTER },
00097     { TILEGX_R20_REGNUM, REVERSE_STATE_REGISTER },
00098     { TILEGX_R21_REGNUM, REVERSE_STATE_REGISTER },
00099     { TILEGX_R22_REGNUM, REVERSE_STATE_REGISTER },
00100     { TILEGX_R23_REGNUM, REVERSE_STATE_REGISTER },
00101     { TILEGX_R24_REGNUM, REVERSE_STATE_REGISTER },
00102     { TILEGX_R25_REGNUM, REVERSE_STATE_REGISTER },
00103     { TILEGX_R26_REGNUM, REVERSE_STATE_REGISTER },
00104     { TILEGX_R27_REGNUM, REVERSE_STATE_REGISTER },
00105     { TILEGX_R28_REGNUM, REVERSE_STATE_REGISTER },
00106     { TILEGX_R29_REGNUM, REVERSE_STATE_REGISTER },
00107     { TILEGX_R30_REGNUM, REVERSE_STATE_REGISTER },
00108     { TILEGX_R31_REGNUM, REVERSE_STATE_REGISTER },
00109     { TILEGX_R32_REGNUM, REVERSE_STATE_REGISTER },
00110     { TILEGX_R33_REGNUM, REVERSE_STATE_REGISTER },
00111     { TILEGX_R34_REGNUM, REVERSE_STATE_REGISTER },
00112     { TILEGX_R35_REGNUM, REVERSE_STATE_REGISTER },
00113     { TILEGX_R36_REGNUM, REVERSE_STATE_REGISTER },
00114     { TILEGX_R37_REGNUM, REVERSE_STATE_REGISTER },
00115     { TILEGX_R38_REGNUM, REVERSE_STATE_REGISTER },
00116     { TILEGX_R39_REGNUM, REVERSE_STATE_REGISTER },
00117     { TILEGX_R40_REGNUM, REVERSE_STATE_REGISTER },
00118     { TILEGX_R41_REGNUM, REVERSE_STATE_REGISTER },
00119     { TILEGX_R42_REGNUM, REVERSE_STATE_REGISTER },
00120     { TILEGX_R43_REGNUM, REVERSE_STATE_REGISTER },
00121     { TILEGX_R44_REGNUM, REVERSE_STATE_REGISTER },
00122     { TILEGX_R45_REGNUM, REVERSE_STATE_REGISTER },
00123     { TILEGX_R46_REGNUM, REVERSE_STATE_REGISTER },
00124     { TILEGX_R47_REGNUM, REVERSE_STATE_REGISTER },
00125     { TILEGX_R48_REGNUM, REVERSE_STATE_REGISTER },
00126     { TILEGX_R49_REGNUM, REVERSE_STATE_REGISTER },
00127     { TILEGX_R50_REGNUM, REVERSE_STATE_REGISTER },
00128     { TILEGX_R51_REGNUM, REVERSE_STATE_REGISTER },
00129     { TILEGX_R52_REGNUM, REVERSE_STATE_REGISTER },
00130     { TILEGX_TP_REGNUM,  REVERSE_STATE_REGISTER },
00131     { TILEGX_SP_REGNUM,  REVERSE_STATE_REGISTER },
00132     { TILEGX_LR_REGNUM,  REVERSE_STATE_REGISTER },
00133     { 0, REVERSE_STATE_UNKNOWN },
00134     { 0, REVERSE_STATE_UNKNOWN },
00135     { 0, REVERSE_STATE_UNKNOWN },
00136     { 0, REVERSE_STATE_UNKNOWN },
00137     { 0, REVERSE_STATE_UNKNOWN },
00138     { 0, REVERSE_STATE_UNKNOWN },
00139     { 0, REVERSE_STATE_UNKNOWN },
00140     { TILEGX_ZERO_REGNUM, REVERSE_STATE_VALUE }
00141   };
00142 
00143 /* Implement the "register_name" gdbarch method.  */
00144 
00145 static const char *
00146 tilegx_register_name (struct gdbarch *gdbarch, int regnum)
00147 {
00148   static const char *const register_names[TILEGX_NUM_REGS] =
00149     {
00150       "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
00151       "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
00152       "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
00153       "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
00154       "r32",  "r33",  "r34",  "r35",  "r36",  "r37",  "r38",  "r39",
00155       "r40",  "r41",  "r42",  "r43",  "r44",  "r45",  "r46",  "r47",
00156       "r48",  "r49",  "r50",  "r51",  "r52",  "tp",   "sp",   "lr",
00157       "sn",   "idn0", "idn1", "udn0", "udn1", "udn2", "udn3", "zero",
00158       "pc",   "faultnum",
00159     };
00160 
00161   if (regnum < 0 || regnum >= TILEGX_NUM_REGS)
00162     internal_error (__FILE__, __LINE__,
00163                     "tilegx_register_name: invalid register number %d",
00164                     regnum);
00165 
00166   return register_names[regnum];
00167 }
00168 
00169 /* This is the implementation of gdbarch method register_type.  */
00170 
00171 static struct type *
00172 tilegx_register_type (struct gdbarch *gdbarch, int regnum)
00173 {
00174   if (regnum == TILEGX_PC_REGNUM)
00175     return builtin_type (gdbarch)->builtin_func_ptr;
00176   else
00177     return builtin_type (gdbarch)->builtin_uint64;
00178 }
00179 
00180 /* This is the implementation of gdbarch method dwarf2_reg_to_regnum.  */
00181 
00182 static int
00183 tilegx_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
00184 {
00185   return num;
00186 }
00187 
00188 /* Makes the decision of whether a given type is a scalar type.
00189    Scalar types are returned in the registers r2-r11 as they fit.  */
00190 
00191 static int
00192 tilegx_type_is_scalar (struct type *t)
00193 {
00194   return (TYPE_CODE(t) != TYPE_CODE_STRUCT
00195           && TYPE_CODE(t) != TYPE_CODE_UNION
00196           && TYPE_CODE(t) != TYPE_CODE_ARRAY);
00197 }
00198 
00199 /* Returns non-zero if the given struct type will be returned using
00200    a special convention, rather than the normal function return method.
00201    Used in the context of the "return" command, and target function
00202    calls from the debugger.  */
00203 
00204 static int
00205 tilegx_use_struct_convention (struct type *type)
00206 {
00207   /* Only scalars which fit in R0 - R9 can be returned in registers.
00208      Otherwise, they are returned via a pointer passed in R0.  */
00209   return (!tilegx_type_is_scalar (type)
00210           && (TYPE_LENGTH (type) > (1 + TILEGX_R9_REGNUM - TILEGX_R0_REGNUM)
00211               * tilegx_reg_size));
00212 }
00213 
00214 /* Find a function's return value in the appropriate registers (in
00215    REGCACHE), and copy it into VALBUF.  */
00216 
00217 static void
00218 tilegx_extract_return_value (struct type *type, struct regcache *regcache,
00219                              gdb_byte *valbuf)
00220 {
00221   int len = TYPE_LENGTH (type);
00222   int i, regnum = TILEGX_R0_REGNUM;
00223 
00224   for (i = 0; i < len; i += tilegx_reg_size)
00225     regcache_raw_read (regcache, regnum++, valbuf + i);
00226 }
00227 
00228 /* Copy the function return value from VALBUF into the proper
00229    location for a function return.
00230    Called only in the context of the "return" command.  */
00231 
00232 static void
00233 tilegx_store_return_value (struct type *type, struct regcache *regcache,
00234                            const void *valbuf)
00235 {
00236   if (TYPE_LENGTH (type) < tilegx_reg_size)
00237     {
00238       /* Add leading zeros to the (little-endian) value.  */
00239       gdb_byte buf[tilegx_reg_size] = { 0 };
00240 
00241       memcpy (buf, valbuf, TYPE_LENGTH (type));
00242       regcache_raw_write (regcache, TILEGX_R0_REGNUM, buf);
00243     }
00244   else
00245     {
00246       int len = TYPE_LENGTH (type);
00247       int i, regnum = TILEGX_R0_REGNUM;
00248 
00249       for (i = 0; i < len; i += tilegx_reg_size)
00250         regcache_raw_write (regcache, regnum++, (gdb_byte *) valbuf + i);
00251     }
00252 }
00253 
00254 /* This is the implementation of gdbarch method return_value.  */
00255 
00256 static enum return_value_convention
00257 tilegx_return_value (struct gdbarch *gdbarch, struct value *function,
00258                      struct type *type, struct regcache *regcache,
00259                      gdb_byte *readbuf, const gdb_byte *writebuf)
00260 {
00261   if (tilegx_use_struct_convention (type))
00262     return RETURN_VALUE_STRUCT_CONVENTION;
00263   if (writebuf)
00264     tilegx_store_return_value (type, regcache, writebuf);
00265   else if (readbuf)
00266     tilegx_extract_return_value (type, regcache, readbuf);
00267   return RETURN_VALUE_REGISTER_CONVENTION;
00268 }
00269 
00270 /* This is the implementation of gdbarch method frame_align.  */
00271 
00272 static CORE_ADDR
00273 tilegx_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
00274 {
00275   return addr & -8;
00276 }
00277 
00278 
00279 /* Implement the "push_dummy_call" gdbarch method.  */
00280 
00281 static CORE_ADDR
00282 tilegx_push_dummy_call (struct gdbarch *gdbarch,
00283                         struct value *function,
00284                         struct regcache *regcache,
00285                         CORE_ADDR bp_addr, int nargs,
00286                         struct value **args,
00287                         CORE_ADDR sp, int struct_return,
00288                         CORE_ADDR struct_addr)
00289 {
00290   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00291   CORE_ADDR stack_dest = sp;
00292   int argreg = TILEGX_R0_REGNUM;
00293   int i, j;
00294   int typelen, slacklen, alignlen;
00295   static const gdb_byte four_zero_words[16] = { 0 };
00296 
00297   /* If struct_return is 1, then the struct return address will
00298      consume one argument-passing register.  */
00299   if (struct_return)
00300     regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
00301 
00302   /* Arguments are passed in R0 - R9, and as soon as an argument
00303      will not fit completely in the remaining registers, then it,
00304      and all remaining arguments, are put on the stack.  */
00305   for (i = 0; i < nargs && argreg <= TILEGX_R9_REGNUM; i++)
00306     {
00307       const gdb_byte *val;
00308       typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
00309 
00310       if (typelen > (TILEGX_R9_REGNUM - argreg + 1) * tilegx_reg_size)
00311         break;
00312 
00313       /* Put argument into registers wordwise.  */
00314       val = value_contents (args[i]);
00315       for (j = 0; j < typelen; j += tilegx_reg_size)
00316         {
00317           /* ISSUE: Why special handling for "typelen = 4x + 1"?
00318              I don't ever see "typelen" values except 4 and 8.  */
00319           int n = (typelen - j == 1) ? 1 : tilegx_reg_size;
00320           ULONGEST w = extract_unsigned_integer (val + j, n, byte_order);
00321 
00322           regcache_cooked_write_unsigned (regcache, argreg++, w);
00323         }
00324     }
00325 
00326   /* Align SP.  */
00327   stack_dest = tilegx_frame_align (gdbarch, stack_dest);
00328 
00329   /* Loop backwards through remaining arguments and push them on
00330      the stack, word aligned.  */
00331   for (j = nargs - 1; j >= i; j--)
00332     {
00333       gdb_byte *val;
00334       struct cleanup *back_to;
00335       const gdb_byte *contents = value_contents (args[j]);
00336 
00337       typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
00338       slacklen = align_up (typelen, 8) - typelen;
00339       val = xmalloc (typelen + slacklen);
00340       back_to = make_cleanup (xfree, val);
00341       memcpy (val, contents, typelen);
00342       memset (val + typelen, 0, slacklen);
00343 
00344       /* Now write data to the stack.  The stack grows downwards.  */
00345       stack_dest -= typelen + slacklen;
00346       write_memory (stack_dest, val, typelen + slacklen);
00347       do_cleanups (back_to);
00348     }
00349 
00350   /* Add 16 bytes for linkage space to the stack.  */
00351   stack_dest = stack_dest - 16;
00352   write_memory (stack_dest, four_zero_words, 16);
00353 
00354   /* Update stack pointer.  */
00355   regcache_cooked_write_unsigned (regcache, TILEGX_SP_REGNUM, stack_dest);
00356 
00357   /* Set the return address register to point to the entry point of
00358      the program, where a breakpoint lies in wait.  */
00359   regcache_cooked_write_unsigned (regcache, TILEGX_LR_REGNUM, bp_addr);
00360 
00361   return stack_dest;
00362 }
00363 
00364 
00365 /* Decode the instructions within the given address range.
00366    Decide when we must have reached the end of the function prologue.
00367    If a frame_info pointer is provided, fill in its saved_regs etc.
00368    Returns the address of the first instruction after the prologue.
00369    NOTE: This is often called with start_addr being the start of some
00370    function, and end_addr being the current PC.  */
00371 
00372 static CORE_ADDR
00373 tilegx_analyze_prologue (struct gdbarch* gdbarch,
00374                          CORE_ADDR start_addr, CORE_ADDR end_addr,
00375                          struct tilegx_frame_cache *cache,
00376                          struct frame_info *next_frame)
00377 {
00378   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00379   CORE_ADDR next_addr;
00380   CORE_ADDR prolog_end = end_addr;
00381   ULONGEST inst, inst2;
00382   LONGEST offset;
00383   int regnum;
00384   gdb_byte instbuf[32 * TILEGX_BUNDLE_SIZE_IN_BYTES];
00385   CORE_ADDR instbuf_start;
00386   unsigned int instbuf_size;
00387   int status;
00388   bfd_uint64_t bundle;
00389   struct tilegx_decoded_instruction
00390     decoded[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
00391   int num_insns;
00392   struct tilegx_reverse_regs reverse_frame[TILEGX_NUM_PHYS_REGS];
00393   struct tilegx_reverse_regs
00394     new_reverse_frame[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
00395   int dest_regs[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
00396   int reverse_frame_valid, prolog_done, branch_seen, lr_saved_on_stack_p;
00397   LONGEST prev_sp_value;
00398   int i, j;
00399 
00400   if (start_addr >= end_addr
00401       || (start_addr % TILEGX_BUNDLE_ALIGNMENT_IN_BYTES) != 0)
00402     return end_addr;
00403 
00404   /* Initialize the reverse frame.  This maps the CURRENT frame's
00405      registers to the outer frame's registers (the frame on the
00406      stack goes the other way).  */
00407   memcpy (&reverse_frame, &template_reverse_regs, sizeof (reverse_frame));
00408 
00409   prolog_done = 0;
00410   branch_seen = 0;
00411   prev_sp_value = 0;
00412   lr_saved_on_stack_p = 0;
00413 
00414   /* To cut down on round-trip overhead, we fetch multiple bundles
00415      at once.  These variables describe the range of memory we have
00416      prefetched.  */
00417   instbuf_start = 0;
00418   instbuf_size = 0;
00419 
00420   for (next_addr = start_addr;
00421        next_addr < end_addr;
00422        next_addr += TILEGX_BUNDLE_SIZE_IN_BYTES)
00423     {
00424       /* Retrieve the next instruction.  */
00425       if (next_addr - instbuf_start >= instbuf_size)
00426         {
00427           /* Figure out how many bytes to fetch.  Don't span a page
00428              boundary since that might cause an unnecessary memory
00429              error.  */
00430           unsigned int size_on_same_page = 4096 - (next_addr & 4095);
00431 
00432           instbuf_size = sizeof instbuf;
00433 
00434           if (instbuf_size > size_on_same_page)
00435             instbuf_size = size_on_same_page;
00436 
00437           instbuf_size = min (instbuf_size, (end_addr - next_addr));
00438           instbuf_start = next_addr;
00439 
00440           status = safe_frame_unwind_memory (next_frame, instbuf_start,
00441                                              instbuf, instbuf_size);
00442           if (status == 0)
00443             memory_error (status, next_addr);
00444         }
00445 
00446       reverse_frame_valid = 0;
00447 
00448       bundle = extract_unsigned_integer (&instbuf[next_addr - instbuf_start],
00449                                          8, byte_order);
00450 
00451       num_insns = parse_insn_tilegx (bundle, next_addr, decoded);
00452 
00453       for (i = 0; i < num_insns; i++)
00454         {
00455           struct tilegx_decoded_instruction *this_insn = &decoded[i];
00456           int64_t *operands = (int64_t *) this_insn->operand_values;
00457           const struct tilegx_opcode *opcode = this_insn->opcode;
00458 
00459           switch (opcode->mnemonic)
00460             {
00461             case TILEGX_OPC_ST:
00462               if (cache
00463                   && reverse_frame[operands[0]].state == REVERSE_STATE_VALUE
00464                   && reverse_frame[operands[1]].state
00465                   == REVERSE_STATE_REGISTER)
00466                 {
00467                   LONGEST saved_address = reverse_frame[operands[0]].value;
00468                   unsigned saved_register
00469                     = (unsigned) reverse_frame[operands[1]].value;
00470 
00471                   /* realreg >= 0 and addr != -1 indicates that the
00472                      value of saved_register is in memory location
00473                      saved_address.  The value of realreg is not
00474                      meaningful in this case but it must be >= 0.
00475                      See trad-frame.h.  */
00476                   cache->saved_regs[saved_register].realreg = saved_register;
00477                   cache->saved_regs[saved_register].addr = saved_address;
00478                 } 
00479               else if (cache
00480                        && (operands[0] == TILEGX_SP_REGNUM) 
00481                        && (operands[1] == TILEGX_LR_REGNUM))
00482                 lr_saved_on_stack_p = 1;
00483               break;
00484             case TILEGX_OPC_ADDI:
00485             case TILEGX_OPC_ADDLI:
00486               if (cache
00487                   && operands[0] == TILEGX_SP_REGNUM
00488                   && operands[1] == TILEGX_SP_REGNUM
00489                   && reverse_frame[operands[1]].state == REVERSE_STATE_REGISTER)
00490                 {
00491                   /* Special case.  We're fixing up the stack frame.  */
00492                   uint64_t hopefully_sp
00493                     = (unsigned) reverse_frame[operands[1]].value;
00494                   short op2_as_short = (short) operands[2];
00495                   signed char op2_as_char = (signed char) operands[2];
00496 
00497                   /* Fix up the sign-extension.  */
00498                   if (opcode->mnemonic == TILEGX_OPC_ADDI)
00499                     op2_as_short = op2_as_char;
00500                   prev_sp_value = (cache->saved_regs[hopefully_sp].addr
00501                                    - op2_as_short);
00502 
00503                   new_reverse_frame[i].state = REVERSE_STATE_VALUE;
00504                   new_reverse_frame[i].value
00505                     = cache->saved_regs[hopefully_sp].addr;
00506                   trad_frame_set_value (cache->saved_regs,
00507                                         hopefully_sp, prev_sp_value);
00508                 }
00509               else
00510                 {
00511                   short op2_as_short = (short) operands[2];
00512                   signed char op2_as_char = (signed char) operands[2];
00513 
00514                   /* Fix up the sign-extension.  */
00515                   if (opcode->mnemonic == TILEGX_OPC_ADDI)
00516                     op2_as_short = op2_as_char;
00517 
00518                   new_reverse_frame[i] = reverse_frame[operands[1]];
00519                   if (new_reverse_frame[i].state == REVERSE_STATE_VALUE)
00520                     new_reverse_frame[i].value += op2_as_short;
00521                   else
00522                     new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
00523                 }
00524               reverse_frame_valid |= 1 << i;
00525               dest_regs[i] = operands[0];
00526               break;
00527             case TILEGX_OPC_ADD:
00528               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
00529                   && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
00530                 {
00531                   /* We have values -- we can do this.  */
00532                   new_reverse_frame[i] = reverse_frame[operands[2]];
00533                   new_reverse_frame[i].value
00534                     += reverse_frame[operands[i]].value;
00535                 }
00536               else
00537                 {
00538                   /* We don't know anything about the values.  Punt.  */
00539                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
00540                 }
00541               reverse_frame_valid |= 1 << i;
00542               dest_regs[i] = operands[0];
00543               break;
00544             case TILEGX_OPC_MOVE:
00545               new_reverse_frame[i] = reverse_frame[operands[1]];
00546               reverse_frame_valid |= 1 << i;
00547               dest_regs[i] = operands[0];
00548               break;
00549             case TILEGX_OPC_MOVEI:
00550             case TILEGX_OPC_MOVELI:
00551               new_reverse_frame[i].state = REVERSE_STATE_VALUE;
00552               new_reverse_frame[i].value = operands[1];
00553               reverse_frame_valid |= 1 << i;
00554               dest_regs[i] = operands[0];
00555               break;
00556             case TILEGX_OPC_ORI:
00557               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE)
00558                 {
00559                   /* We have a value in A -- we can do this.  */
00560                   new_reverse_frame[i] = reverse_frame[operands[1]];
00561                   new_reverse_frame[i].value
00562                     = reverse_frame[operands[1]].value | operands[2];
00563                 }
00564               else if (operands[2] == 0)
00565                 {
00566                   /* This is a move.  */
00567                   new_reverse_frame[i] = reverse_frame[operands[1]];
00568                 }
00569               else
00570                 {
00571                   /* We don't know anything about the values.  Punt.  */
00572                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
00573                 }
00574               reverse_frame_valid |= 1 << i;
00575               dest_regs[i] = operands[0];
00576               break;
00577             case TILEGX_OPC_OR:
00578               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
00579                   && reverse_frame[operands[1]].value == 0)
00580                 {
00581                   /* This is a move.  */
00582                   new_reverse_frame[i] = reverse_frame[operands[2]];
00583                 }
00584               else if (reverse_frame[operands[2]].state == REVERSE_STATE_VALUE
00585                        && reverse_frame[operands[2]].value == 0)
00586                 {
00587                   /* This is a move.  */
00588                   new_reverse_frame[i] = reverse_frame[operands[1]];
00589                 }
00590               else
00591                 {
00592                   /* We don't know anything about the values.  Punt.  */
00593                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
00594                 }
00595               reverse_frame_valid |= 1 << i;
00596               dest_regs[i] = operands[0];
00597               break;
00598             case TILEGX_OPC_SUB:
00599               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
00600                   && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
00601                 {
00602                   /* We have values -- we can do this.  */
00603                   new_reverse_frame[i] = reverse_frame[operands[1]];
00604                   new_reverse_frame[i].value
00605                     -= reverse_frame[operands[2]].value;
00606                 }
00607               else
00608                 {
00609                   /* We don't know anything about the values.  Punt.  */
00610                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
00611                 }
00612               reverse_frame_valid |= 1 << i;
00613               dest_regs[i] = operands[0];
00614               break;
00615 
00616             case TILEGX_OPC_FNOP:
00617             case TILEGX_OPC_INFO:
00618             case TILEGX_OPC_INFOL:
00619               /* Nothing to see here, move on.
00620                  Note that real NOP is treated as a 'real' instruction
00621                  because someone must have intended that it be there.
00622                  It therefore terminates the prolog.  */
00623               break;
00624 
00625             case TILEGX_OPC_J:
00626             case TILEGX_OPC_JAL:
00627 
00628             case TILEGX_OPC_BEQZ:
00629             case TILEGX_OPC_BEQZT:
00630             case TILEGX_OPC_BGEZ:
00631             case TILEGX_OPC_BGEZT:
00632             case TILEGX_OPC_BGTZ:
00633             case TILEGX_OPC_BGTZT:
00634             case TILEGX_OPC_BLBC:
00635             case TILEGX_OPC_BLBCT:
00636             case TILEGX_OPC_BLBS:
00637             case TILEGX_OPC_BLBST:
00638             case TILEGX_OPC_BLEZ:
00639             case TILEGX_OPC_BLEZT:
00640             case TILEGX_OPC_BLTZ:
00641             case TILEGX_OPC_BLTZT:
00642             case TILEGX_OPC_BNEZ:
00643             case TILEGX_OPC_BNEZT:
00644 
00645             case TILEGX_OPC_IRET:
00646             case TILEGX_OPC_JALR:
00647             case TILEGX_OPC_JALRP:
00648             case TILEGX_OPC_JR:
00649             case TILEGX_OPC_JRP:
00650             case TILEGX_OPC_SWINT0:
00651             case TILEGX_OPC_SWINT1:
00652             case TILEGX_OPC_SWINT2:
00653             case TILEGX_OPC_SWINT3:
00654               /* We're really done -- this is a branch.  */
00655               branch_seen = 1;
00656               prolog_done = 1;
00657               break;
00658             default:
00659               /* We don't know or care what this instruction is.
00660                  All we know is that it isn't part of a prolog, and if
00661                  there's a destination register, we're trashing it.  */
00662               prolog_done = 1;
00663               for (j = 0; j < opcode->num_operands; j++)
00664                 {
00665                   if (this_insn->operands[j]->is_dest_reg)
00666                     {
00667                       dest_regs[i] = operands[j];
00668                       new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
00669                       reverse_frame_valid |= 1 << i;
00670                       break;
00671                     }
00672                 }
00673               break;
00674             }
00675         }
00676 
00677       /* Now update the reverse frames.  */
00678       for (i = 0; i < num_insns; i++)
00679         {
00680           /* ISSUE: Does this properly handle "network" registers?  */
00681           if ((reverse_frame_valid & (1 << i))
00682               && dest_regs[i] != TILEGX_ZERO_REGNUM)
00683             reverse_frame[dest_regs[i]] = new_reverse_frame[i];
00684         }
00685 
00686       if (prev_sp_value != 0)
00687         {
00688           /* GCC uses R52 as a frame pointer.  Have we seen "move r52, sp"?  */
00689           if (reverse_frame[TILEGX_R52_REGNUM].state == REVERSE_STATE_REGISTER
00690               && reverse_frame[TILEGX_R52_REGNUM].value == TILEGX_SP_REGNUM)
00691           {
00692             reverse_frame[TILEGX_R52_REGNUM].state = REVERSE_STATE_VALUE;
00693             reverse_frame[TILEGX_R52_REGNUM].value = prev_sp_value;
00694           }
00695 
00696           prev_sp_value = 0;
00697         }
00698 
00699       if (prolog_done && prolog_end == end_addr)
00700         {
00701           /* We found non-prolog code.  As such, _this_ instruction
00702              is the one after the prolog.  We keep processing, because
00703              there may be more prolog code in there, but this is what
00704              we'll return.  */
00705           /* ISSUE: There may not have actually been a prologue, and
00706              we may have simply skipped some random instructions.  */
00707           prolog_end = next_addr;
00708         }
00709       if (branch_seen)
00710         {
00711           /* We saw a branch.  The prolog absolutely must be over.  */
00712           break;
00713         }
00714     }
00715 
00716   if (prolog_end == end_addr && cache)
00717     {
00718       /* We may have terminated the prolog early, and we're certainly
00719          at THIS point right now.  It's possible that the values of
00720          registers we need are currently actually in other registers
00721          (and haven't been written to memory yet).  Go find them.  */
00722       for (i = 0; i < TILEGX_NUM_PHYS_REGS; i++)
00723         {
00724           if (reverse_frame[i].state == REVERSE_STATE_REGISTER
00725               && reverse_frame[i].value != i)
00726             {
00727               unsigned saved_register = (unsigned) reverse_frame[i].value;
00728 
00729               cache->saved_regs[saved_register].realreg = i;
00730               cache->saved_regs[saved_register].addr = (LONGEST) -1;
00731             }
00732         }
00733     }
00734 
00735   if (lr_saved_on_stack_p)
00736     {
00737       cache->saved_regs[TILEGX_LR_REGNUM].realreg = TILEGX_LR_REGNUM;
00738       cache->saved_regs[TILEGX_LR_REGNUM].addr =
00739         cache->saved_regs[TILEGX_SP_REGNUM].addr;
00740     }
00741 
00742   return prolog_end;
00743 }
00744 
00745 /* This is the implementation of gdbarch method skip_prologue.  */
00746 
00747 static CORE_ADDR
00748 tilegx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
00749 {
00750   CORE_ADDR func_start, end_pc;
00751   struct obj_section *s;
00752 
00753   /* This is the preferred method, find the end of the prologue by
00754      using the debugging information.  */
00755   if (find_pc_partial_function (start_pc, NULL, &func_start, NULL))
00756     {
00757       CORE_ADDR post_prologue_pc
00758         = skip_prologue_using_sal (gdbarch, func_start);
00759 
00760       if (post_prologue_pc != 0)
00761         return max (start_pc, post_prologue_pc);
00762     }
00763 
00764   /* Don't straddle a section boundary.  */
00765   s = find_pc_section (start_pc);
00766   end_pc = start_pc + 8 * TILEGX_BUNDLE_SIZE_IN_BYTES;
00767   if (s != NULL)
00768     end_pc = min (end_pc, obj_section_endaddr (s));
00769 
00770   /* Otherwise, try to skip prologue the hard way.  */
00771   return tilegx_analyze_prologue (gdbarch,
00772                                   start_pc,
00773                                   end_pc,
00774                                   NULL, NULL);
00775 }
00776 
00777 /* This is the implementation of gdbarch method in_function_epilogue_p.  */
00778 
00779 static int
00780 tilegx_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
00781 {
00782   CORE_ADDR func_addr = 0, func_end = 0;
00783 
00784   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
00785     {
00786       ULONGEST inst, inst2;
00787       CORE_ADDR addr = func_end - TILEGX_BUNDLE_SIZE_IN_BYTES;
00788 
00789       /* FIXME: Find the actual epilogue.  */
00790       /* HACK: Just assume the final bundle is the "ret" instruction".  */
00791       if (pc > addr)
00792         return 1;
00793     }
00794   return 0;
00795 }
00796 
00797 /* This is the implementation of gdbarch method get_longjmp_target.  */
00798 
00799 static int
00800 tilegx_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
00801 {
00802   struct gdbarch *gdbarch = get_frame_arch (frame);
00803   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00804   CORE_ADDR jb_addr;
00805   gdb_byte buf[8];
00806 
00807   jb_addr = get_frame_register_unsigned (frame, TILEGX_R0_REGNUM);
00808 
00809   /* TileGX jmp_buf contains 32 elements of type __uint_reg_t which
00810      has a size of 8 bytes.  The return address is stored in the 25th
00811      slot.  */
00812   if (target_read_memory (jb_addr + 25 * 8, buf, 8))
00813     return 0;
00814 
00815   *pc = extract_unsigned_integer (buf, 8, byte_order);
00816 
00817   return 1;
00818 }
00819 
00820 /* by assigning the 'faultnum' reg in kernel pt_regs with this value,
00821    kernel do_signal will not check r0. see tilegx kernel/signal.c
00822    for details.  */
00823 #define INT_SWINT_1_SIGRETURN (~0)
00824 
00825 /* Implement the "write_pc" gdbarch method.  */
00826 
00827 static void
00828 tilegx_write_pc (struct regcache *regcache, CORE_ADDR pc)
00829 {
00830   regcache_cooked_write_unsigned (regcache, TILEGX_PC_REGNUM, pc);
00831 
00832   /* We must be careful with modifying the program counter.  If we
00833      just interrupted a system call, the kernel might try to restart
00834      it when we resume the inferior.  On restarting the system call,
00835      the kernel will try backing up the program counter even though it
00836      no longer points at the system call.  This typically results in a
00837      SIGSEGV or SIGILL.  We can prevent this by writing INT_SWINT_1_SIGRETURN
00838      in the "faultnum" pseudo-register.
00839 
00840      Note that "faultnum" is saved when setting up a dummy call frame.
00841      This means that it is properly restored when that frame is
00842      popped, and that the interrupted system call will be restarted
00843      when we resume the inferior on return from a function call from
00844      within GDB.  In all other cases the system call will not be
00845      restarted.  */
00846   regcache_cooked_write_unsigned (regcache, TILEGX_FAULTNUM_REGNUM,
00847                                   INT_SWINT_1_SIGRETURN);
00848 }
00849 
00850 /* This is the implementation of gdbarch method breakpoint_from_pc.  */
00851 
00852 static const unsigned char *
00853 tilegx_breakpoint_from_pc (struct gdbarch *gdbarch,
00854                            CORE_ADDR *pcptr, int *lenptr)
00855 {
00856   /* 64-bit pattern for a { bpt ; nop } bundle.  */
00857   static const unsigned char breakpoint[] =
00858     { 0x00, 0x50, 0x48, 0x51, 0xae, 0x44, 0x6a, 0x28 };
00859 
00860   *lenptr = sizeof (breakpoint);
00861   return breakpoint;
00862 }
00863 
00864 /* Normal frames.  */
00865 
00866 static struct tilegx_frame_cache *
00867 tilegx_frame_cache (struct frame_info *this_frame, void **this_cache)
00868 {
00869   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00870   struct tilegx_frame_cache *cache;
00871   CORE_ADDR current_pc;
00872   int i;
00873 
00874   if (*this_cache)
00875     return *this_cache;
00876 
00877   cache = FRAME_OBSTACK_ZALLOC (struct tilegx_frame_cache);
00878   *this_cache = cache;
00879   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00880   cache->base = 0;
00881   cache->start_pc = get_frame_func (this_frame);
00882   current_pc = get_frame_pc (this_frame);
00883 
00884   cache->base = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
00885   trad_frame_set_value (cache->saved_regs, TILEGX_SP_REGNUM, cache->base);
00886 
00887   if (cache->start_pc)
00888     tilegx_analyze_prologue (gdbarch, cache->start_pc, current_pc,
00889                              cache, this_frame);
00890 
00891   cache->saved_regs[TILEGX_PC_REGNUM] = cache->saved_regs[TILEGX_LR_REGNUM];
00892 
00893   return cache;
00894 }
00895 
00896 /* Retrieve the value of REGNUM in FRAME.  */
00897 
00898 static struct value*
00899 tilegx_frame_prev_register (struct frame_info *this_frame,
00900                             void **this_cache,
00901                             int regnum)
00902 {
00903   struct tilegx_frame_cache *info =
00904     tilegx_frame_cache (this_frame, this_cache);
00905 
00906   return trad_frame_get_prev_register (this_frame, info->saved_regs,
00907                                        regnum);
00908 }
00909 
00910 /* Build frame id.  */
00911 
00912 static void
00913 tilegx_frame_this_id (struct frame_info *this_frame, void **this_cache,
00914                       struct frame_id *this_id)
00915 {
00916   struct tilegx_frame_cache *info =
00917     tilegx_frame_cache (this_frame, this_cache);
00918 
00919   /* This marks the outermost frame.  */
00920   if (info->base == 0)
00921     return;
00922 
00923   (*this_id) = frame_id_build (info->base, info->start_pc);
00924 }
00925 
00926 static CORE_ADDR
00927 tilegx_frame_base_address (struct frame_info *this_frame, void **this_cache)
00928 {
00929   struct tilegx_frame_cache *cache =
00930     tilegx_frame_cache (this_frame, this_cache);
00931 
00932   return cache->base;
00933 }
00934 
00935 static const struct frame_unwind tilegx_frame_unwind = {
00936   NORMAL_FRAME,
00937   default_frame_unwind_stop_reason,
00938   tilegx_frame_this_id,
00939   tilegx_frame_prev_register,
00940   NULL,                        /* const struct frame_data *unwind_data  */
00941   default_frame_sniffer,       /* frame_sniffer_ftype *sniffer  */
00942   NULL                         /* frame_prev_pc_ftype *prev_pc  */
00943 };
00944 
00945 static const struct frame_base tilegx_frame_base = {
00946   &tilegx_frame_unwind,
00947   tilegx_frame_base_address,
00948   tilegx_frame_base_address,
00949   tilegx_frame_base_address
00950 };
00951 
00952 static CORE_ADDR
00953 tilegx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
00954 {
00955   return frame_unwind_register_unsigned (next_frame, TILEGX_SP_REGNUM);
00956 }
00957 
00958 static CORE_ADDR
00959 tilegx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
00960 {
00961   return frame_unwind_register_unsigned (next_frame, TILEGX_PC_REGNUM);
00962 }
00963 
00964 static struct frame_id
00965 tilegx_unwind_dummy_id (struct gdbarch *gdbarch,
00966                         struct frame_info *this_frame)
00967 {
00968   CORE_ADDR sp;
00969 
00970   sp = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
00971   return frame_id_build (sp, get_frame_pc (this_frame));
00972 }
00973 
00974 
00975 /* We cannot read/write the "special" registers.  */
00976 
00977 static int
00978 tilegx_cannot_reference_register (struct gdbarch *gdbarch, int regno)
00979 {
00980   if (regno >= 0 && regno < TILEGX_NUM_EASY_REGS)
00981     return 0;
00982   else if (regno == TILEGX_PC_REGNUM
00983            || regno == TILEGX_FAULTNUM_REGNUM)
00984     return 0;
00985   else
00986     return 1;
00987 }
00988 
00989 static struct gdbarch *
00990 tilegx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
00991 {
00992   struct gdbarch *gdbarch;
00993   int arch_size = 64;
00994 
00995   /* Handle arch_size == 32 or 64.  Default to 64.  */
00996   if (info.abfd)
00997     arch_size = bfd_get_arch_size (info.abfd);
00998 
00999   /* Try to find a pre-existing architecture.  */
01000   for (arches = gdbarch_list_lookup_by_info (arches, &info);
01001        arches != NULL;
01002        arches = gdbarch_list_lookup_by_info (arches->next, &info))
01003     {
01004       /* We only have two flavors -- just make sure arch_size matches.  */
01005       if (gdbarch_ptr_bit (arches->gdbarch) == arch_size)
01006         return (arches->gdbarch);
01007     }
01008 
01009   gdbarch = gdbarch_alloc (&info, NULL);
01010 
01011   /* Basic register fields and methods, datatype sizes and stuff.  */
01012 
01013   /* There are 64 physical registers which can be referenced by
01014      instructions (although only 56 of them can actually be
01015      debugged) and 1 magic register (the PC).  The other three
01016      magic registers (ex1, syscall, orig_r0) which are known to
01017      "ptrace" are ignored by "gdb".  Note that we simply pretend
01018      that there are 65 registers, and no "pseudo registers".  */
01019   set_gdbarch_num_regs (gdbarch, TILEGX_NUM_REGS);
01020   set_gdbarch_num_pseudo_regs (gdbarch, 0);
01021 
01022   set_gdbarch_sp_regnum (gdbarch, TILEGX_SP_REGNUM);
01023   set_gdbarch_pc_regnum (gdbarch, TILEGX_PC_REGNUM);
01024 
01025   set_gdbarch_register_name (gdbarch, tilegx_register_name);
01026   set_gdbarch_register_type (gdbarch, tilegx_register_type);
01027 
01028   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
01029   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
01030   set_gdbarch_long_bit (gdbarch, arch_size);
01031   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
01032 
01033   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
01034   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
01035   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
01036 
01037   set_gdbarch_ptr_bit (gdbarch, arch_size);
01038   set_gdbarch_addr_bit (gdbarch, arch_size);
01039 
01040   set_gdbarch_cannot_fetch_register (gdbarch,
01041                                      tilegx_cannot_reference_register);
01042   set_gdbarch_cannot_store_register (gdbarch,
01043                                      tilegx_cannot_reference_register);
01044 
01045   /* Stack grows down.  */
01046   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
01047 
01048   /* Frame Info.  */
01049   set_gdbarch_unwind_sp (gdbarch, tilegx_unwind_sp);
01050   set_gdbarch_unwind_pc (gdbarch, tilegx_unwind_pc);
01051   set_gdbarch_dummy_id (gdbarch, tilegx_unwind_dummy_id);
01052   set_gdbarch_frame_align (gdbarch, tilegx_frame_align);
01053   frame_base_set_default (gdbarch, &tilegx_frame_base);
01054 
01055   set_gdbarch_skip_prologue (gdbarch, tilegx_skip_prologue);
01056 
01057   set_gdbarch_in_function_epilogue_p (gdbarch,
01058                                       tilegx_in_function_epilogue_p);
01059 
01060   /* Map debug registers into internal register numbers.  */
01061   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, tilegx_dwarf2_reg_to_regnum);
01062 
01063   /* These values and methods are used when gdb calls a target function.  */
01064   set_gdbarch_push_dummy_call (gdbarch, tilegx_push_dummy_call);
01065   set_gdbarch_get_longjmp_target (gdbarch, tilegx_get_longjmp_target);
01066   set_gdbarch_write_pc (gdbarch, tilegx_write_pc);
01067   set_gdbarch_breakpoint_from_pc (gdbarch, tilegx_breakpoint_from_pc);
01068   set_gdbarch_return_value (gdbarch, tilegx_return_value);
01069 
01070   set_gdbarch_print_insn (gdbarch, print_insn_tilegx);
01071 
01072   gdbarch_init_osabi (info, gdbarch);
01073 
01074   dwarf2_append_unwinders (gdbarch);
01075   frame_unwind_append_unwinder (gdbarch, &tilegx_frame_unwind);
01076 
01077   return gdbarch;
01078 }
01079 
01080 /* Provide a prototype to silence -Wmissing-prototypes.  */
01081 extern initialize_file_ftype _initialize_tilegx_tdep;
01082 
01083 void
01084 _initialize_tilegx_tdep (void)
01085 {
01086   register_gdbarch_init (bfd_arch_tilegx, tilegx_gdbarch_init);
01087 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines