Remove cleanup from tilegx-tdep.c
[external/binutils.git] / gdb / tilegx-tdep.c
1 /* Target-dependent code for the Tilera TILE-Gx processor.
2
3    Copyright (C) 2012-2017 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "frame-base.h"
23 #include "frame-unwind.h"
24 #include "dwarf2-frame.h"
25 #include "trad-frame.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "gdbcmd.h"
29 #include "gdbcore.h"
30 #include "value.h"
31 #include "dis-asm.h"
32 #include "inferior.h"
33 #include "arch-utils.h"
34 #include "floatformat.h"
35 #include "regcache.h"
36 #include "regset.h"
37 #include "doublest.h"
38 #include "osabi.h"
39 #include "linux-tdep.h"
40 #include "objfiles.h"
41 #include "solib-svr4.h"
42 #include "tilegx-tdep.h"
43 #include "opcode/tilegx.h"
44 #include <algorithm>
45 #include "common/byte-vector.h"
46
47 struct tilegx_frame_cache
48 {
49   /* Base address.  */
50   CORE_ADDR base;
51   /* Function start.  */
52   CORE_ADDR start_pc;
53
54   /* Table of saved registers.  */
55   struct trad_frame_saved_reg *saved_regs;
56 };
57
58 /* Register state values used by analyze_prologue.  */
59 enum reverse_state
60   {
61     REVERSE_STATE_REGISTER,
62     REVERSE_STATE_VALUE,
63     REVERSE_STATE_UNKNOWN
64   };
65
66 /* Register state used by analyze_prologue().  */
67 struct tilegx_reverse_regs
68 {
69   LONGEST value;
70   enum reverse_state state;
71 };
72
73 static const struct tilegx_reverse_regs
74 template_reverse_regs[TILEGX_NUM_PHYS_REGS] =
75   {
76     { TILEGX_R0_REGNUM,  REVERSE_STATE_REGISTER },
77     { TILEGX_R1_REGNUM,  REVERSE_STATE_REGISTER },
78     { TILEGX_R2_REGNUM,  REVERSE_STATE_REGISTER },
79     { TILEGX_R3_REGNUM,  REVERSE_STATE_REGISTER },
80     { TILEGX_R4_REGNUM,  REVERSE_STATE_REGISTER },
81     { TILEGX_R5_REGNUM,  REVERSE_STATE_REGISTER },
82     { TILEGX_R6_REGNUM,  REVERSE_STATE_REGISTER },
83     { TILEGX_R7_REGNUM,  REVERSE_STATE_REGISTER },
84     { TILEGX_R8_REGNUM,  REVERSE_STATE_REGISTER },
85     { TILEGX_R9_REGNUM,  REVERSE_STATE_REGISTER },
86     { TILEGX_R10_REGNUM, REVERSE_STATE_REGISTER },
87     { TILEGX_R11_REGNUM, REVERSE_STATE_REGISTER },
88     { TILEGX_R12_REGNUM, REVERSE_STATE_REGISTER },
89     { TILEGX_R13_REGNUM, REVERSE_STATE_REGISTER },
90     { TILEGX_R14_REGNUM, REVERSE_STATE_REGISTER },
91     { TILEGX_R15_REGNUM, REVERSE_STATE_REGISTER },
92     { TILEGX_R16_REGNUM, REVERSE_STATE_REGISTER },
93     { TILEGX_R17_REGNUM, REVERSE_STATE_REGISTER },
94     { TILEGX_R18_REGNUM, REVERSE_STATE_REGISTER },
95     { TILEGX_R19_REGNUM, REVERSE_STATE_REGISTER },
96     { TILEGX_R20_REGNUM, REVERSE_STATE_REGISTER },
97     { TILEGX_R21_REGNUM, REVERSE_STATE_REGISTER },
98     { TILEGX_R22_REGNUM, REVERSE_STATE_REGISTER },
99     { TILEGX_R23_REGNUM, REVERSE_STATE_REGISTER },
100     { TILEGX_R24_REGNUM, REVERSE_STATE_REGISTER },
101     { TILEGX_R25_REGNUM, REVERSE_STATE_REGISTER },
102     { TILEGX_R26_REGNUM, REVERSE_STATE_REGISTER },
103     { TILEGX_R27_REGNUM, REVERSE_STATE_REGISTER },
104     { TILEGX_R28_REGNUM, REVERSE_STATE_REGISTER },
105     { TILEGX_R29_REGNUM, REVERSE_STATE_REGISTER },
106     { TILEGX_R30_REGNUM, REVERSE_STATE_REGISTER },
107     { TILEGX_R31_REGNUM, REVERSE_STATE_REGISTER },
108     { TILEGX_R32_REGNUM, REVERSE_STATE_REGISTER },
109     { TILEGX_R33_REGNUM, REVERSE_STATE_REGISTER },
110     { TILEGX_R34_REGNUM, REVERSE_STATE_REGISTER },
111     { TILEGX_R35_REGNUM, REVERSE_STATE_REGISTER },
112     { TILEGX_R36_REGNUM, REVERSE_STATE_REGISTER },
113     { TILEGX_R37_REGNUM, REVERSE_STATE_REGISTER },
114     { TILEGX_R38_REGNUM, REVERSE_STATE_REGISTER },
115     { TILEGX_R39_REGNUM, REVERSE_STATE_REGISTER },
116     { TILEGX_R40_REGNUM, REVERSE_STATE_REGISTER },
117     { TILEGX_R41_REGNUM, REVERSE_STATE_REGISTER },
118     { TILEGX_R42_REGNUM, REVERSE_STATE_REGISTER },
119     { TILEGX_R43_REGNUM, REVERSE_STATE_REGISTER },
120     { TILEGX_R44_REGNUM, REVERSE_STATE_REGISTER },
121     { TILEGX_R45_REGNUM, REVERSE_STATE_REGISTER },
122     { TILEGX_R46_REGNUM, REVERSE_STATE_REGISTER },
123     { TILEGX_R47_REGNUM, REVERSE_STATE_REGISTER },
124     { TILEGX_R48_REGNUM, REVERSE_STATE_REGISTER },
125     { TILEGX_R49_REGNUM, REVERSE_STATE_REGISTER },
126     { TILEGX_R50_REGNUM, REVERSE_STATE_REGISTER },
127     { TILEGX_R51_REGNUM, REVERSE_STATE_REGISTER },
128     { TILEGX_R52_REGNUM, REVERSE_STATE_REGISTER },
129     { TILEGX_TP_REGNUM,  REVERSE_STATE_REGISTER },
130     { TILEGX_SP_REGNUM,  REVERSE_STATE_REGISTER },
131     { TILEGX_LR_REGNUM,  REVERSE_STATE_REGISTER },
132     { 0, REVERSE_STATE_UNKNOWN },
133     { 0, REVERSE_STATE_UNKNOWN },
134     { 0, REVERSE_STATE_UNKNOWN },
135     { 0, REVERSE_STATE_UNKNOWN },
136     { 0, REVERSE_STATE_UNKNOWN },
137     { 0, REVERSE_STATE_UNKNOWN },
138     { 0, REVERSE_STATE_UNKNOWN },
139     { TILEGX_ZERO_REGNUM, REVERSE_STATE_VALUE }
140   };
141
142 /* Implement the "register_name" gdbarch method.  */
143
144 static const char *
145 tilegx_register_name (struct gdbarch *gdbarch, int regnum)
146 {
147   static const char *const register_names[TILEGX_NUM_REGS] =
148     {
149       "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
150       "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
151       "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
152       "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
153       "r32",  "r33",  "r34",  "r35",  "r36",  "r37",  "r38",  "r39",
154       "r40",  "r41",  "r42",  "r43",  "r44",  "r45",  "r46",  "r47",
155       "r48",  "r49",  "r50",  "r51",  "r52",  "tp",   "sp",   "lr",
156       "sn",   "idn0", "idn1", "udn0", "udn1", "udn2", "udn3", "zero",
157       "pc",   "faultnum",
158     };
159
160   if (regnum < 0 || regnum >= TILEGX_NUM_REGS)
161     internal_error (__FILE__, __LINE__,
162                     "tilegx_register_name: invalid register number %d",
163                     regnum);
164
165   return register_names[regnum];
166 }
167
168 /* This is the implementation of gdbarch method register_type.  */
169
170 static struct type *
171 tilegx_register_type (struct gdbarch *gdbarch, int regnum)
172 {
173   if (regnum == TILEGX_PC_REGNUM)
174     return builtin_type (gdbarch)->builtin_func_ptr;
175   else
176     return builtin_type (gdbarch)->builtin_uint64;
177 }
178
179 /* This is the implementation of gdbarch method dwarf2_reg_to_regnum.  */
180
181 static int
182 tilegx_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
183 {
184   return num;
185 }
186
187 /* Makes the decision of whether a given type is a scalar type.
188    Scalar types are returned in the registers r2-r11 as they fit.  */
189
190 static int
191 tilegx_type_is_scalar (struct type *t)
192 {
193   return (TYPE_CODE(t) != TYPE_CODE_STRUCT
194           && TYPE_CODE(t) != TYPE_CODE_UNION
195           && TYPE_CODE(t) != TYPE_CODE_ARRAY);
196 }
197
198 /* Returns non-zero if the given struct type will be returned using
199    a special convention, rather than the normal function return method.
200    Used in the context of the "return" command, and target function
201    calls from the debugger.  */
202
203 static int
204 tilegx_use_struct_convention (struct type *type)
205 {
206   /* Only scalars which fit in R0 - R9 can be returned in registers.
207      Otherwise, they are returned via a pointer passed in R0.  */
208   return (!tilegx_type_is_scalar (type)
209           && (TYPE_LENGTH (type) > (1 + TILEGX_R9_REGNUM - TILEGX_R0_REGNUM)
210               * tilegx_reg_size));
211 }
212
213 /* Find a function's return value in the appropriate registers (in
214    REGCACHE), and copy it into VALBUF.  */
215
216 static void
217 tilegx_extract_return_value (struct type *type, struct regcache *regcache,
218                              gdb_byte *valbuf)
219 {
220   int len = TYPE_LENGTH (type);
221   int i, regnum = TILEGX_R0_REGNUM;
222
223   for (i = 0; i < len; i += tilegx_reg_size)
224     regcache_raw_read (regcache, regnum++, valbuf + i);
225 }
226
227 /* Copy the function return value from VALBUF into the proper
228    location for a function return.
229    Called only in the context of the "return" command.  */
230
231 static void
232 tilegx_store_return_value (struct type *type, struct regcache *regcache,
233                            const void *valbuf)
234 {
235   if (TYPE_LENGTH (type) < tilegx_reg_size)
236     {
237       /* Add leading zeros to the (little-endian) value.  */
238       gdb_byte buf[tilegx_reg_size] = { 0 };
239
240       memcpy (buf, valbuf, TYPE_LENGTH (type));
241       regcache_raw_write (regcache, TILEGX_R0_REGNUM, buf);
242     }
243   else
244     {
245       int len = TYPE_LENGTH (type);
246       int i, regnum = TILEGX_R0_REGNUM;
247
248       for (i = 0; i < len; i += tilegx_reg_size)
249         regcache_raw_write (regcache, regnum++, (gdb_byte *) valbuf + i);
250     }
251 }
252
253 /* This is the implementation of gdbarch method return_value.  */
254
255 static enum return_value_convention
256 tilegx_return_value (struct gdbarch *gdbarch, struct value *function,
257                      struct type *type, struct regcache *regcache,
258                      gdb_byte *readbuf, const gdb_byte *writebuf)
259 {
260   if (tilegx_use_struct_convention (type))
261     return RETURN_VALUE_STRUCT_CONVENTION;
262   if (writebuf)
263     tilegx_store_return_value (type, regcache, writebuf);
264   else if (readbuf)
265     tilegx_extract_return_value (type, regcache, readbuf);
266   return RETURN_VALUE_REGISTER_CONVENTION;
267 }
268
269 /* This is the implementation of gdbarch method frame_align.  */
270
271 static CORE_ADDR
272 tilegx_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
273 {
274   return addr & -8;
275 }
276
277
278 /* Implement the "push_dummy_call" gdbarch method.  */
279
280 static CORE_ADDR
281 tilegx_push_dummy_call (struct gdbarch *gdbarch,
282                         struct value *function,
283                         struct regcache *regcache,
284                         CORE_ADDR bp_addr, int nargs,
285                         struct value **args,
286                         CORE_ADDR sp, int struct_return,
287                         CORE_ADDR struct_addr)
288 {
289   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
290   CORE_ADDR stack_dest = sp;
291   int argreg = TILEGX_R0_REGNUM;
292   int i, j;
293   int typelen, slacklen;
294   static const gdb_byte four_zero_words[16] = { 0 };
295
296   /* If struct_return is 1, then the struct return address will
297      consume one argument-passing register.  */
298   if (struct_return)
299     regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
300
301   /* Arguments are passed in R0 - R9, and as soon as an argument
302      will not fit completely in the remaining registers, then it,
303      and all remaining arguments, are put on the stack.  */
304   for (i = 0; i < nargs && argreg <= TILEGX_R9_REGNUM; i++)
305     {
306       const gdb_byte *val;
307       typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
308
309       if (typelen > (TILEGX_R9_REGNUM - argreg + 1) * tilegx_reg_size)
310         break;
311
312       /* Put argument into registers wordwise.  */
313       val = value_contents (args[i]);
314       for (j = 0; j < typelen; j += tilegx_reg_size)
315         {
316           /* ISSUE: Why special handling for "typelen = 4x + 1"?
317              I don't ever see "typelen" values except 4 and 8.  */
318           int n = (typelen - j == 1) ? 1 : tilegx_reg_size;
319           ULONGEST w = extract_unsigned_integer (val + j, n, byte_order);
320
321           regcache_cooked_write_unsigned (regcache, argreg++, w);
322         }
323     }
324
325   /* Align SP.  */
326   stack_dest = tilegx_frame_align (gdbarch, stack_dest);
327
328   /* Loop backwards through remaining arguments and push them on
329      the stack, word aligned.  */
330   for (j = nargs - 1; j >= i; j--)
331     {
332       const gdb_byte *contents = value_contents (args[j]);
333
334       typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
335       slacklen = align_up (typelen, 8) - typelen;
336       gdb::byte_vector val (typelen + slacklen);
337       memcpy (val.data (), contents, typelen);
338       memset (val.data () + typelen, 0, slacklen);
339
340       /* Now write data to the stack.  The stack grows downwards.  */
341       stack_dest -= typelen + slacklen;
342       write_memory (stack_dest, val.data (), typelen + slacklen);
343     }
344
345   /* Add 16 bytes for linkage space to the stack.  */
346   stack_dest = stack_dest - 16;
347   write_memory (stack_dest, four_zero_words, 16);
348
349   /* Update stack pointer.  */
350   regcache_cooked_write_unsigned (regcache, TILEGX_SP_REGNUM, stack_dest);
351
352   /* Set the return address register to point to the entry point of
353      the program, where a breakpoint lies in wait.  */
354   regcache_cooked_write_unsigned (regcache, TILEGX_LR_REGNUM, bp_addr);
355
356   return stack_dest;
357 }
358
359
360 /* Decode the instructions within the given address range.
361    Decide when we must have reached the end of the function prologue.
362    If a frame_info pointer is provided, fill in its saved_regs etc.
363    Returns the address of the first instruction after the prologue.
364    NOTE: This is often called with start_addr being the start of some
365    function, and end_addr being the current PC.  */
366
367 static CORE_ADDR
368 tilegx_analyze_prologue (struct gdbarch* gdbarch,
369                          CORE_ADDR start_addr, CORE_ADDR end_addr,
370                          struct tilegx_frame_cache *cache,
371                          struct frame_info *next_frame)
372 {
373   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
374   CORE_ADDR next_addr;
375   CORE_ADDR prolog_end = end_addr;
376   gdb_byte instbuf[32 * TILEGX_BUNDLE_SIZE_IN_BYTES];
377   CORE_ADDR instbuf_start;
378   unsigned int instbuf_size;
379   int status;
380   bfd_uint64_t bundle;
381   struct tilegx_decoded_instruction
382     decoded[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
383   int num_insns;
384   struct tilegx_reverse_regs reverse_frame[TILEGX_NUM_PHYS_REGS];
385   struct tilegx_reverse_regs
386     new_reverse_frame[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
387   int dest_regs[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
388   int reverse_frame_valid, prolog_done, branch_seen, lr_saved_on_stack_p;
389   LONGEST prev_sp_value;
390   int i, j;
391
392   if (start_addr >= end_addr
393       || (start_addr % TILEGX_BUNDLE_ALIGNMENT_IN_BYTES) != 0)
394     return end_addr;
395
396   /* Initialize the reverse frame.  This maps the CURRENT frame's
397      registers to the outer frame's registers (the frame on the
398      stack goes the other way).  */
399   memcpy (&reverse_frame, &template_reverse_regs, sizeof (reverse_frame));
400
401   prolog_done = 0;
402   branch_seen = 0;
403   prev_sp_value = 0;
404   lr_saved_on_stack_p = 0;
405
406   /* To cut down on round-trip overhead, we fetch multiple bundles
407      at once.  These variables describe the range of memory we have
408      prefetched.  */
409   instbuf_start = 0;
410   instbuf_size = 0;
411
412   for (next_addr = start_addr;
413        next_addr < end_addr;
414        next_addr += TILEGX_BUNDLE_SIZE_IN_BYTES)
415     {
416       /* Retrieve the next instruction.  */
417       if (next_addr - instbuf_start >= instbuf_size)
418         {
419           /* Figure out how many bytes to fetch.  Don't span a page
420              boundary since that might cause an unnecessary memory
421              error.  */
422           unsigned int size_on_same_page = 4096 - (next_addr & 4095);
423
424           instbuf_size = sizeof instbuf;
425
426           if (instbuf_size > size_on_same_page)
427             instbuf_size = size_on_same_page;
428
429           instbuf_size = std::min ((CORE_ADDR) instbuf_size,
430                                    (end_addr - next_addr));
431           instbuf_start = next_addr;
432
433           status = safe_frame_unwind_memory (next_frame, instbuf_start,
434                                              instbuf, instbuf_size);
435           if (status == 0)
436             memory_error (TARGET_XFER_E_IO, next_addr);
437         }
438
439       reverse_frame_valid = 0;
440
441       bundle = extract_unsigned_integer (&instbuf[next_addr - instbuf_start],
442                                          8, byte_order);
443
444       num_insns = parse_insn_tilegx (bundle, next_addr, decoded);
445
446       for (i = 0; i < num_insns; i++)
447         {
448           struct tilegx_decoded_instruction *this_insn = &decoded[i];
449           long long *operands = this_insn->operand_values;
450           const struct tilegx_opcode *opcode = this_insn->opcode;
451
452           switch (opcode->mnemonic)
453             {
454             case TILEGX_OPC_ST:
455               if (cache
456                   && reverse_frame[operands[0]].state == REVERSE_STATE_VALUE
457                   && reverse_frame[operands[1]].state
458                   == REVERSE_STATE_REGISTER)
459                 {
460                   LONGEST saved_address = reverse_frame[operands[0]].value;
461                   unsigned saved_register
462                     = (unsigned) reverse_frame[operands[1]].value;
463
464                   /* realreg >= 0 and addr != -1 indicates that the
465                      value of saved_register is in memory location
466                      saved_address.  The value of realreg is not
467                      meaningful in this case but it must be >= 0.
468                      See trad-frame.h.  */
469                   cache->saved_regs[saved_register].realreg = saved_register;
470                   cache->saved_regs[saved_register].addr = saved_address;
471                 } 
472               else if (cache
473                        && (operands[0] == TILEGX_SP_REGNUM) 
474                        && (operands[1] == TILEGX_LR_REGNUM))
475                 lr_saved_on_stack_p = 1;
476               break;
477             case TILEGX_OPC_ADDI:
478             case TILEGX_OPC_ADDLI:
479               if (cache
480                   && operands[0] == TILEGX_SP_REGNUM
481                   && operands[1] == TILEGX_SP_REGNUM
482                   && reverse_frame[operands[1]].state == REVERSE_STATE_REGISTER)
483                 {
484                   /* Special case.  We're fixing up the stack frame.  */
485                   uint64_t hopefully_sp
486                     = (unsigned) reverse_frame[operands[1]].value;
487                   short op2_as_short = (short) operands[2];
488                   signed char op2_as_char = (signed char) operands[2];
489
490                   /* Fix up the sign-extension.  */
491                   if (opcode->mnemonic == TILEGX_OPC_ADDI)
492                     op2_as_short = op2_as_char;
493                   prev_sp_value = (cache->saved_regs[hopefully_sp].addr
494                                    - op2_as_short);
495
496                   new_reverse_frame[i].state = REVERSE_STATE_VALUE;
497                   new_reverse_frame[i].value
498                     = cache->saved_regs[hopefully_sp].addr;
499                   trad_frame_set_value (cache->saved_regs,
500                                         hopefully_sp, prev_sp_value);
501                 }
502               else
503                 {
504                   short op2_as_short = (short) operands[2];
505                   signed char op2_as_char = (signed char) operands[2];
506
507                   /* Fix up the sign-extension.  */
508                   if (opcode->mnemonic == TILEGX_OPC_ADDI)
509                     op2_as_short = op2_as_char;
510
511                   new_reverse_frame[i] = reverse_frame[operands[1]];
512                   if (new_reverse_frame[i].state == REVERSE_STATE_VALUE)
513                     new_reverse_frame[i].value += op2_as_short;
514                   else
515                     new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
516                 }
517               reverse_frame_valid |= 1 << i;
518               dest_regs[i] = operands[0];
519               break;
520             case TILEGX_OPC_ADD:
521               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
522                   && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
523                 {
524                   /* We have values -- we can do this.  */
525                   new_reverse_frame[i] = reverse_frame[operands[2]];
526                   new_reverse_frame[i].value
527                     += reverse_frame[operands[i]].value;
528                 }
529               else
530                 {
531                   /* We don't know anything about the values.  Punt.  */
532                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
533                 }
534               reverse_frame_valid |= 1 << i;
535               dest_regs[i] = operands[0];
536               break;
537             case TILEGX_OPC_MOVE:
538               new_reverse_frame[i] = reverse_frame[operands[1]];
539               reverse_frame_valid |= 1 << i;
540               dest_regs[i] = operands[0];
541               break;
542             case TILEGX_OPC_MOVEI:
543             case TILEGX_OPC_MOVELI:
544               new_reverse_frame[i].state = REVERSE_STATE_VALUE;
545               new_reverse_frame[i].value = operands[1];
546               reverse_frame_valid |= 1 << i;
547               dest_regs[i] = operands[0];
548               break;
549             case TILEGX_OPC_ORI:
550               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE)
551                 {
552                   /* We have a value in A -- we can do this.  */
553                   new_reverse_frame[i] = reverse_frame[operands[1]];
554                   new_reverse_frame[i].value
555                     = reverse_frame[operands[1]].value | operands[2];
556                 }
557               else if (operands[2] == 0)
558                 {
559                   /* This is a move.  */
560                   new_reverse_frame[i] = reverse_frame[operands[1]];
561                 }
562               else
563                 {
564                   /* We don't know anything about the values.  Punt.  */
565                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
566                 }
567               reverse_frame_valid |= 1 << i;
568               dest_regs[i] = operands[0];
569               break;
570             case TILEGX_OPC_OR:
571               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
572                   && reverse_frame[operands[1]].value == 0)
573                 {
574                   /* This is a move.  */
575                   new_reverse_frame[i] = reverse_frame[operands[2]];
576                 }
577               else if (reverse_frame[operands[2]].state == REVERSE_STATE_VALUE
578                        && reverse_frame[operands[2]].value == 0)
579                 {
580                   /* This is a move.  */
581                   new_reverse_frame[i] = reverse_frame[operands[1]];
582                 }
583               else
584                 {
585                   /* We don't know anything about the values.  Punt.  */
586                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
587                 }
588               reverse_frame_valid |= 1 << i;
589               dest_regs[i] = operands[0];
590               break;
591             case TILEGX_OPC_SUB:
592               if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
593                   && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
594                 {
595                   /* We have values -- we can do this.  */
596                   new_reverse_frame[i] = reverse_frame[operands[1]];
597                   new_reverse_frame[i].value
598                     -= reverse_frame[operands[2]].value;
599                 }
600               else
601                 {
602                   /* We don't know anything about the values.  Punt.  */
603                   new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
604                 }
605               reverse_frame_valid |= 1 << i;
606               dest_regs[i] = operands[0];
607               break;
608
609             case TILEGX_OPC_FNOP:
610             case TILEGX_OPC_INFO:
611             case TILEGX_OPC_INFOL:
612               /* Nothing to see here, move on.
613                  Note that real NOP is treated as a 'real' instruction
614                  because someone must have intended that it be there.
615                  It therefore terminates the prolog.  */
616               break;
617
618             case TILEGX_OPC_J:
619             case TILEGX_OPC_JAL:
620
621             case TILEGX_OPC_BEQZ:
622             case TILEGX_OPC_BEQZT:
623             case TILEGX_OPC_BGEZ:
624             case TILEGX_OPC_BGEZT:
625             case TILEGX_OPC_BGTZ:
626             case TILEGX_OPC_BGTZT:
627             case TILEGX_OPC_BLBC:
628             case TILEGX_OPC_BLBCT:
629             case TILEGX_OPC_BLBS:
630             case TILEGX_OPC_BLBST:
631             case TILEGX_OPC_BLEZ:
632             case TILEGX_OPC_BLEZT:
633             case TILEGX_OPC_BLTZ:
634             case TILEGX_OPC_BLTZT:
635             case TILEGX_OPC_BNEZ:
636             case TILEGX_OPC_BNEZT:
637
638             case TILEGX_OPC_IRET:
639             case TILEGX_OPC_JALR:
640             case TILEGX_OPC_JALRP:
641             case TILEGX_OPC_JR:
642             case TILEGX_OPC_JRP:
643             case TILEGX_OPC_SWINT0:
644             case TILEGX_OPC_SWINT1:
645             case TILEGX_OPC_SWINT2:
646             case TILEGX_OPC_SWINT3:
647               /* We're really done -- this is a branch.  */
648               branch_seen = 1;
649               prolog_done = 1;
650               break;
651             default:
652               /* We don't know or care what this instruction is.
653                  All we know is that it isn't part of a prolog, and if
654                  there's a destination register, we're trashing it.  */
655               prolog_done = 1;
656               for (j = 0; j < opcode->num_operands; j++)
657                 {
658                   if (this_insn->operands[j]->is_dest_reg)
659                     {
660                       dest_regs[i] = operands[j];
661                       new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
662                       reverse_frame_valid |= 1 << i;
663                       break;
664                     }
665                 }
666               break;
667             }
668         }
669
670       /* Now update the reverse frames.  */
671       for (i = 0; i < num_insns; i++)
672         {
673           /* ISSUE: Does this properly handle "network" registers?  */
674           if ((reverse_frame_valid & (1 << i))
675               && dest_regs[i] != TILEGX_ZERO_REGNUM)
676             reverse_frame[dest_regs[i]] = new_reverse_frame[i];
677         }
678
679       if (prev_sp_value != 0)
680         {
681           /* GCC uses R52 as a frame pointer.  Have we seen "move r52, sp"?  */
682           if (reverse_frame[TILEGX_R52_REGNUM].state == REVERSE_STATE_REGISTER
683               && reverse_frame[TILEGX_R52_REGNUM].value == TILEGX_SP_REGNUM)
684           {
685             reverse_frame[TILEGX_R52_REGNUM].state = REVERSE_STATE_VALUE;
686             reverse_frame[TILEGX_R52_REGNUM].value = prev_sp_value;
687           }
688
689           prev_sp_value = 0;
690         }
691
692       if (prolog_done && prolog_end == end_addr)
693         {
694           /* We found non-prolog code.  As such, _this_ instruction
695              is the one after the prolog.  We keep processing, because
696              there may be more prolog code in there, but this is what
697              we'll return.  */
698           /* ISSUE: There may not have actually been a prologue, and
699              we may have simply skipped some random instructions.  */
700           prolog_end = next_addr;
701         }
702       if (branch_seen)
703         {
704           /* We saw a branch.  The prolog absolutely must be over.  */
705           break;
706         }
707     }
708
709   if (prolog_end == end_addr && cache)
710     {
711       /* We may have terminated the prolog early, and we're certainly
712          at THIS point right now.  It's possible that the values of
713          registers we need are currently actually in other registers
714          (and haven't been written to memory yet).  Go find them.  */
715       for (i = 0; i < TILEGX_NUM_PHYS_REGS; i++)
716         {
717           if (reverse_frame[i].state == REVERSE_STATE_REGISTER
718               && reverse_frame[i].value != i)
719             {
720               unsigned saved_register = (unsigned) reverse_frame[i].value;
721
722               cache->saved_regs[saved_register].realreg = i;
723               cache->saved_regs[saved_register].addr = (LONGEST) -1;
724             }
725         }
726     }
727
728   if (lr_saved_on_stack_p)
729     {
730       cache->saved_regs[TILEGX_LR_REGNUM].realreg = TILEGX_LR_REGNUM;
731       cache->saved_regs[TILEGX_LR_REGNUM].addr =
732         cache->saved_regs[TILEGX_SP_REGNUM].addr;
733     }
734
735   return prolog_end;
736 }
737
738 /* This is the implementation of gdbarch method skip_prologue.  */
739
740 static CORE_ADDR
741 tilegx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
742 {
743   CORE_ADDR func_start, end_pc;
744   struct obj_section *s;
745
746   /* This is the preferred method, find the end of the prologue by
747      using the debugging information.  */
748   if (find_pc_partial_function (start_pc, NULL, &func_start, NULL))
749     {
750       CORE_ADDR post_prologue_pc
751         = skip_prologue_using_sal (gdbarch, func_start);
752
753       if (post_prologue_pc != 0)
754         return std::max (start_pc, post_prologue_pc);
755     }
756
757   /* Don't straddle a section boundary.  */
758   s = find_pc_section (start_pc);
759   end_pc = start_pc + 8 * TILEGX_BUNDLE_SIZE_IN_BYTES;
760   if (s != NULL)
761     end_pc = std::min (end_pc, obj_section_endaddr (s));
762
763   /* Otherwise, try to skip prologue the hard way.  */
764   return tilegx_analyze_prologue (gdbarch,
765                                   start_pc,
766                                   end_pc,
767                                   NULL, NULL);
768 }
769
770 /* This is the implementation of gdbarch method stack_frame_destroyed_p.  */
771
772 static int
773 tilegx_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
774 {
775   CORE_ADDR func_addr = 0, func_end = 0;
776
777   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
778     {
779       CORE_ADDR addr = func_end - TILEGX_BUNDLE_SIZE_IN_BYTES;
780
781       /* FIXME: Find the actual epilogue.  */
782       /* HACK: Just assume the final bundle is the "ret" instruction".  */
783       if (pc > addr)
784         return 1;
785     }
786   return 0;
787 }
788
789 /* This is the implementation of gdbarch method get_longjmp_target.  */
790
791 static int
792 tilegx_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
793 {
794   struct gdbarch *gdbarch = get_frame_arch (frame);
795   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
796   CORE_ADDR jb_addr;
797   gdb_byte buf[8];
798
799   jb_addr = get_frame_register_unsigned (frame, TILEGX_R0_REGNUM);
800
801   /* TileGX jmp_buf contains 32 elements of type __uint_reg_t which
802      has a size of 8 bytes.  The return address is stored in the 25th
803      slot.  */
804   if (target_read_memory (jb_addr + 25 * 8, buf, 8))
805     return 0;
806
807   *pc = extract_unsigned_integer (buf, 8, byte_order);
808
809   return 1;
810 }
811
812 /* by assigning the 'faultnum' reg in kernel pt_regs with this value,
813    kernel do_signal will not check r0. see tilegx kernel/signal.c
814    for details.  */
815 #define INT_SWINT_1_SIGRETURN (~0)
816
817 /* Implement the "write_pc" gdbarch method.  */
818
819 static void
820 tilegx_write_pc (struct regcache *regcache, CORE_ADDR pc)
821 {
822   regcache_cooked_write_unsigned (regcache, TILEGX_PC_REGNUM, pc);
823
824   /* We must be careful with modifying the program counter.  If we
825      just interrupted a system call, the kernel might try to restart
826      it when we resume the inferior.  On restarting the system call,
827      the kernel will try backing up the program counter even though it
828      no longer points at the system call.  This typically results in a
829      SIGSEGV or SIGILL.  We can prevent this by writing INT_SWINT_1_SIGRETURN
830      in the "faultnum" pseudo-register.
831
832      Note that "faultnum" is saved when setting up a dummy call frame.
833      This means that it is properly restored when that frame is
834      popped, and that the interrupted system call will be restarted
835      when we resume the inferior on return from a function call from
836      within GDB.  In all other cases the system call will not be
837      restarted.  */
838   regcache_cooked_write_unsigned (regcache, TILEGX_FAULTNUM_REGNUM,
839                                   INT_SWINT_1_SIGRETURN);
840 }
841
842 /* 64-bit pattern for a { bpt ; nop } bundle.  */
843 constexpr gdb_byte tilegx_break_insn[] =
844   { 0x00, 0x50, 0x48, 0x51, 0xae, 0x44, 0x6a, 0x28 };
845
846 typedef BP_MANIPULATION (tilegx_break_insn) tilegx_breakpoint;
847
848 /* Normal frames.  */
849
850 static struct tilegx_frame_cache *
851 tilegx_frame_cache (struct frame_info *this_frame, void **this_cache)
852 {
853   struct gdbarch *gdbarch = get_frame_arch (this_frame);
854   struct tilegx_frame_cache *cache;
855   CORE_ADDR current_pc;
856
857   if (*this_cache)
858     return (struct tilegx_frame_cache *) *this_cache;
859
860   cache = FRAME_OBSTACK_ZALLOC (struct tilegx_frame_cache);
861   *this_cache = cache;
862   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
863   cache->base = 0;
864   cache->start_pc = get_frame_func (this_frame);
865   current_pc = get_frame_pc (this_frame);
866
867   cache->base = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
868   trad_frame_set_value (cache->saved_regs, TILEGX_SP_REGNUM, cache->base);
869
870   if (cache->start_pc)
871     tilegx_analyze_prologue (gdbarch, cache->start_pc, current_pc,
872                              cache, this_frame);
873
874   cache->saved_regs[TILEGX_PC_REGNUM] = cache->saved_regs[TILEGX_LR_REGNUM];
875
876   return cache;
877 }
878
879 /* Retrieve the value of REGNUM in FRAME.  */
880
881 static struct value*
882 tilegx_frame_prev_register (struct frame_info *this_frame,
883                             void **this_cache,
884                             int regnum)
885 {
886   struct tilegx_frame_cache *info =
887     tilegx_frame_cache (this_frame, this_cache);
888
889   return trad_frame_get_prev_register (this_frame, info->saved_regs,
890                                        regnum);
891 }
892
893 /* Build frame id.  */
894
895 static void
896 tilegx_frame_this_id (struct frame_info *this_frame, void **this_cache,
897                       struct frame_id *this_id)
898 {
899   struct tilegx_frame_cache *info =
900     tilegx_frame_cache (this_frame, this_cache);
901
902   /* This marks the outermost frame.  */
903   if (info->base == 0)
904     return;
905
906   (*this_id) = frame_id_build (info->base, info->start_pc);
907 }
908
909 static CORE_ADDR
910 tilegx_frame_base_address (struct frame_info *this_frame, void **this_cache)
911 {
912   struct tilegx_frame_cache *cache =
913     tilegx_frame_cache (this_frame, this_cache);
914
915   return cache->base;
916 }
917
918 static const struct frame_unwind tilegx_frame_unwind = {
919   NORMAL_FRAME,
920   default_frame_unwind_stop_reason,
921   tilegx_frame_this_id,
922   tilegx_frame_prev_register,
923   NULL,                        /* const struct frame_data *unwind_data  */
924   default_frame_sniffer,       /* frame_sniffer_ftype *sniffer  */
925   NULL                         /* frame_prev_pc_ftype *prev_pc  */
926 };
927
928 static const struct frame_base tilegx_frame_base = {
929   &tilegx_frame_unwind,
930   tilegx_frame_base_address,
931   tilegx_frame_base_address,
932   tilegx_frame_base_address
933 };
934
935 static CORE_ADDR
936 tilegx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
937 {
938   return frame_unwind_register_unsigned (next_frame, TILEGX_SP_REGNUM);
939 }
940
941 static CORE_ADDR
942 tilegx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
943 {
944   return frame_unwind_register_unsigned (next_frame, TILEGX_PC_REGNUM);
945 }
946
947 static struct frame_id
948 tilegx_unwind_dummy_id (struct gdbarch *gdbarch,
949                         struct frame_info *this_frame)
950 {
951   CORE_ADDR sp;
952
953   sp = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
954   return frame_id_build (sp, get_frame_pc (this_frame));
955 }
956
957
958 /* We cannot read/write the "special" registers.  */
959
960 static int
961 tilegx_cannot_reference_register (struct gdbarch *gdbarch, int regno)
962 {
963   if (regno >= 0 && regno < TILEGX_NUM_EASY_REGS)
964     return 0;
965   else if (regno == TILEGX_PC_REGNUM
966            || regno == TILEGX_FAULTNUM_REGNUM)
967     return 0;
968   else
969     return 1;
970 }
971
972 static struct gdbarch *
973 tilegx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
974 {
975   struct gdbarch *gdbarch;
976   int arch_size = 64;
977
978   /* Handle arch_size == 32 or 64.  Default to 64.  */
979   if (info.abfd)
980     arch_size = bfd_get_arch_size (info.abfd);
981
982   /* Try to find a pre-existing architecture.  */
983   for (arches = gdbarch_list_lookup_by_info (arches, &info);
984        arches != NULL;
985        arches = gdbarch_list_lookup_by_info (arches->next, &info))
986     {
987       /* We only have two flavors -- just make sure arch_size matches.  */
988       if (gdbarch_ptr_bit (arches->gdbarch) == arch_size)
989         return (arches->gdbarch);
990     }
991
992   gdbarch = gdbarch_alloc (&info, NULL);
993
994   /* Basic register fields and methods, datatype sizes and stuff.  */
995
996   /* There are 64 physical registers which can be referenced by
997      instructions (although only 56 of them can actually be
998      debugged) and 1 magic register (the PC).  The other three
999      magic registers (ex1, syscall, orig_r0) which are known to
1000      "ptrace" are ignored by "gdb".  Note that we simply pretend
1001      that there are 65 registers, and no "pseudo registers".  */
1002   set_gdbarch_num_regs (gdbarch, TILEGX_NUM_REGS);
1003   set_gdbarch_num_pseudo_regs (gdbarch, 0);
1004
1005   set_gdbarch_sp_regnum (gdbarch, TILEGX_SP_REGNUM);
1006   set_gdbarch_pc_regnum (gdbarch, TILEGX_PC_REGNUM);
1007
1008   set_gdbarch_register_name (gdbarch, tilegx_register_name);
1009   set_gdbarch_register_type (gdbarch, tilegx_register_type);
1010
1011   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1012   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1013   set_gdbarch_long_bit (gdbarch, arch_size);
1014   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1015
1016   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1017   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1018   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1019
1020   set_gdbarch_ptr_bit (gdbarch, arch_size);
1021   set_gdbarch_addr_bit (gdbarch, arch_size);
1022
1023   set_gdbarch_cannot_fetch_register (gdbarch,
1024                                      tilegx_cannot_reference_register);
1025   set_gdbarch_cannot_store_register (gdbarch,
1026                                      tilegx_cannot_reference_register);
1027
1028   /* Stack grows down.  */
1029   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1030
1031   /* Frame Info.  */
1032   set_gdbarch_unwind_sp (gdbarch, tilegx_unwind_sp);
1033   set_gdbarch_unwind_pc (gdbarch, tilegx_unwind_pc);
1034   set_gdbarch_dummy_id (gdbarch, tilegx_unwind_dummy_id);
1035   set_gdbarch_frame_align (gdbarch, tilegx_frame_align);
1036   frame_base_set_default (gdbarch, &tilegx_frame_base);
1037
1038   set_gdbarch_skip_prologue (gdbarch, tilegx_skip_prologue);
1039
1040   set_gdbarch_stack_frame_destroyed_p (gdbarch, tilegx_stack_frame_destroyed_p);
1041
1042   /* Map debug registers into internal register numbers.  */
1043   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, tilegx_dwarf2_reg_to_regnum);
1044
1045   /* These values and methods are used when gdb calls a target function.  */
1046   set_gdbarch_push_dummy_call (gdbarch, tilegx_push_dummy_call);
1047   set_gdbarch_get_longjmp_target (gdbarch, tilegx_get_longjmp_target);
1048   set_gdbarch_write_pc (gdbarch, tilegx_write_pc);
1049   set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1050                                        tilegx_breakpoint::kind_from_pc);
1051   set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1052                                        tilegx_breakpoint::bp_from_kind);
1053   set_gdbarch_return_value (gdbarch, tilegx_return_value);
1054
1055   gdbarch_init_osabi (info, gdbarch);
1056
1057   dwarf2_append_unwinders (gdbarch);
1058   frame_unwind_append_unwinder (gdbarch, &tilegx_frame_unwind);
1059
1060   return gdbarch;
1061 }
1062
1063 void
1064 _initialize_tilegx_tdep (void)
1065 {
1066   register_gdbarch_init (bfd_arch_tilegx, tilegx_gdbarch_init);
1067 }