Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / tilegx-tdep.c
blobd5ea93cf50386f7f4ac1fdbd4c707d54e4dfa7e0
1 /* Target-dependent code for the Tilera TILE-Gx processor.
3 Copyright (C) 2012-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "frame.h"
21 #include "frame-base.h"
22 #include "frame-unwind.h"
23 #include "dwarf2/frame.h"
24 #include "trad-frame.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "value.h"
30 #include "dis-asm.h"
31 #include "inferior.h"
32 #include "arch-utils.h"
33 #include "regcache.h"
34 #include "regset.h"
35 #include "osabi.h"
36 #include "linux-tdep.h"
37 #include "objfiles.h"
38 #include "solib-svr4.h"
39 #include "tilegx-tdep.h"
40 #include "opcode/tilegx.h"
41 #include <algorithm>
42 #include "gdbsupport/byte-vector.h"
44 struct tilegx_frame_cache
46 /* Base address. */
47 CORE_ADDR base;
48 /* Function start. */
49 CORE_ADDR start_pc;
51 /* Table of saved registers. */
52 trad_frame_saved_reg *saved_regs;
55 /* Register state values used by analyze_prologue. */
56 enum reverse_state
58 REVERSE_STATE_REGISTER,
59 REVERSE_STATE_VALUE,
60 REVERSE_STATE_UNKNOWN
63 /* Register state used by analyze_prologue(). */
64 struct tilegx_reverse_regs
66 LONGEST value;
67 enum reverse_state state;
70 static const struct tilegx_reverse_regs
71 template_reverse_regs[TILEGX_NUM_PHYS_REGS] =
73 { TILEGX_R0_REGNUM, REVERSE_STATE_REGISTER },
74 { TILEGX_R1_REGNUM, REVERSE_STATE_REGISTER },
75 { TILEGX_R2_REGNUM, REVERSE_STATE_REGISTER },
76 { TILEGX_R3_REGNUM, REVERSE_STATE_REGISTER },
77 { TILEGX_R4_REGNUM, REVERSE_STATE_REGISTER },
78 { TILEGX_R5_REGNUM, REVERSE_STATE_REGISTER },
79 { TILEGX_R6_REGNUM, REVERSE_STATE_REGISTER },
80 { TILEGX_R7_REGNUM, REVERSE_STATE_REGISTER },
81 { TILEGX_R8_REGNUM, REVERSE_STATE_REGISTER },
82 { TILEGX_R9_REGNUM, REVERSE_STATE_REGISTER },
83 { TILEGX_R10_REGNUM, REVERSE_STATE_REGISTER },
84 { TILEGX_R11_REGNUM, REVERSE_STATE_REGISTER },
85 { TILEGX_R12_REGNUM, REVERSE_STATE_REGISTER },
86 { TILEGX_R13_REGNUM, REVERSE_STATE_REGISTER },
87 { TILEGX_R14_REGNUM, REVERSE_STATE_REGISTER },
88 { TILEGX_R15_REGNUM, REVERSE_STATE_REGISTER },
89 { TILEGX_R16_REGNUM, REVERSE_STATE_REGISTER },
90 { TILEGX_R17_REGNUM, REVERSE_STATE_REGISTER },
91 { TILEGX_R18_REGNUM, REVERSE_STATE_REGISTER },
92 { TILEGX_R19_REGNUM, REVERSE_STATE_REGISTER },
93 { TILEGX_R20_REGNUM, REVERSE_STATE_REGISTER },
94 { TILEGX_R21_REGNUM, REVERSE_STATE_REGISTER },
95 { TILEGX_R22_REGNUM, REVERSE_STATE_REGISTER },
96 { TILEGX_R23_REGNUM, REVERSE_STATE_REGISTER },
97 { TILEGX_R24_REGNUM, REVERSE_STATE_REGISTER },
98 { TILEGX_R25_REGNUM, REVERSE_STATE_REGISTER },
99 { TILEGX_R26_REGNUM, REVERSE_STATE_REGISTER },
100 { TILEGX_R27_REGNUM, REVERSE_STATE_REGISTER },
101 { TILEGX_R28_REGNUM, REVERSE_STATE_REGISTER },
102 { TILEGX_R29_REGNUM, REVERSE_STATE_REGISTER },
103 { TILEGX_R30_REGNUM, REVERSE_STATE_REGISTER },
104 { TILEGX_R31_REGNUM, REVERSE_STATE_REGISTER },
105 { TILEGX_R32_REGNUM, REVERSE_STATE_REGISTER },
106 { TILEGX_R33_REGNUM, REVERSE_STATE_REGISTER },
107 { TILEGX_R34_REGNUM, REVERSE_STATE_REGISTER },
108 { TILEGX_R35_REGNUM, REVERSE_STATE_REGISTER },
109 { TILEGX_R36_REGNUM, REVERSE_STATE_REGISTER },
110 { TILEGX_R37_REGNUM, REVERSE_STATE_REGISTER },
111 { TILEGX_R38_REGNUM, REVERSE_STATE_REGISTER },
112 { TILEGX_R39_REGNUM, REVERSE_STATE_REGISTER },
113 { TILEGX_R40_REGNUM, REVERSE_STATE_REGISTER },
114 { TILEGX_R41_REGNUM, REVERSE_STATE_REGISTER },
115 { TILEGX_R42_REGNUM, REVERSE_STATE_REGISTER },
116 { TILEGX_R43_REGNUM, REVERSE_STATE_REGISTER },
117 { TILEGX_R44_REGNUM, REVERSE_STATE_REGISTER },
118 { TILEGX_R45_REGNUM, REVERSE_STATE_REGISTER },
119 { TILEGX_R46_REGNUM, REVERSE_STATE_REGISTER },
120 { TILEGX_R47_REGNUM, REVERSE_STATE_REGISTER },
121 { TILEGX_R48_REGNUM, REVERSE_STATE_REGISTER },
122 { TILEGX_R49_REGNUM, REVERSE_STATE_REGISTER },
123 { TILEGX_R50_REGNUM, REVERSE_STATE_REGISTER },
124 { TILEGX_R51_REGNUM, REVERSE_STATE_REGISTER },
125 { TILEGX_R52_REGNUM, REVERSE_STATE_REGISTER },
126 { TILEGX_TP_REGNUM, REVERSE_STATE_REGISTER },
127 { TILEGX_SP_REGNUM, REVERSE_STATE_REGISTER },
128 { TILEGX_LR_REGNUM, REVERSE_STATE_REGISTER },
129 { 0, REVERSE_STATE_UNKNOWN },
130 { 0, REVERSE_STATE_UNKNOWN },
131 { 0, REVERSE_STATE_UNKNOWN },
132 { 0, REVERSE_STATE_UNKNOWN },
133 { 0, REVERSE_STATE_UNKNOWN },
134 { 0, REVERSE_STATE_UNKNOWN },
135 { 0, REVERSE_STATE_UNKNOWN },
136 { TILEGX_ZERO_REGNUM, REVERSE_STATE_VALUE }
139 /* Implement the "register_name" gdbarch method. */
141 static const char *
142 tilegx_register_name (struct gdbarch *gdbarch, int regnum)
144 static const char *const register_names[] =
146 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
147 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
148 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
149 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
150 "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
151 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
152 "r48", "r49", "r50", "r51", "r52", "tp", "sp", "lr",
153 "sn", "idn0", "idn1", "udn0", "udn1", "udn2", "udn3", "zero",
154 "pc", "faultnum",
157 static_assert (TILEGX_NUM_REGS == ARRAY_SIZE (register_names));
158 return register_names[regnum];
161 /* This is the implementation of gdbarch method register_type. */
163 static struct type *
164 tilegx_register_type (struct gdbarch *gdbarch, int regnum)
166 if (regnum == TILEGX_PC_REGNUM)
167 return builtin_type (gdbarch)->builtin_func_ptr;
168 else
169 return builtin_type (gdbarch)->builtin_uint64;
172 /* This is the implementation of gdbarch method dwarf2_reg_to_regnum. */
174 static int
175 tilegx_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
177 return num;
180 /* Makes the decision of whether a given type is a scalar type.
181 Scalar types are returned in the registers r2-r11 as they fit. */
183 static int
184 tilegx_type_is_scalar (struct type *t)
186 return (t->code () != TYPE_CODE_STRUCT
187 && t->code () != TYPE_CODE_UNION
188 && t->code () != TYPE_CODE_ARRAY);
191 /* Returns non-zero if the given struct type will be returned using
192 a special convention, rather than the normal function return method.
193 Used in the context of the "return" command, and target function
194 calls from the debugger. */
196 static int
197 tilegx_use_struct_convention (struct type *type)
199 /* Only scalars which fit in R0 - R9 can be returned in registers.
200 Otherwise, they are returned via a pointer passed in R0. */
201 return (!tilegx_type_is_scalar (type)
202 && (type->length () > (1 + TILEGX_R9_REGNUM - TILEGX_R0_REGNUM)
203 * tilegx_reg_size));
206 /* Find a function's return value in the appropriate registers (in
207 REGCACHE), and copy it into VALBUF. */
209 static void
210 tilegx_extract_return_value (struct type *type, struct regcache *regcache,
211 gdb_byte *valbuf)
213 int len = type->length ();
214 int i, regnum = TILEGX_R0_REGNUM;
216 for (i = 0; i < len; i += tilegx_reg_size)
217 regcache->raw_read (regnum++, valbuf + i);
220 /* Copy the function return value from VALBUF into the proper
221 location for a function return.
222 Called only in the context of the "return" command. */
224 static void
225 tilegx_store_return_value (struct type *type, struct regcache *regcache,
226 const void *valbuf)
228 if (type->length () < tilegx_reg_size)
230 /* Add leading zeros to the (little-endian) value. */
231 gdb_byte buf[tilegx_reg_size] = { 0 };
233 memcpy (buf, valbuf, type->length ());
234 regcache->raw_write (TILEGX_R0_REGNUM, buf);
236 else
238 int len = type->length ();
239 int i, regnum = TILEGX_R0_REGNUM;
241 for (i = 0; i < len; i += tilegx_reg_size)
242 regcache->raw_write (regnum++, (gdb_byte *) valbuf + i);
246 /* This is the implementation of gdbarch method return_value. */
248 static enum return_value_convention
249 tilegx_return_value (struct gdbarch *gdbarch, struct value *function,
250 struct type *type, struct regcache *regcache,
251 gdb_byte *readbuf, const gdb_byte *writebuf)
253 if (tilegx_use_struct_convention (type))
254 return RETURN_VALUE_STRUCT_CONVENTION;
255 if (writebuf)
256 tilegx_store_return_value (type, regcache, writebuf);
257 else if (readbuf)
258 tilegx_extract_return_value (type, regcache, readbuf);
259 return RETURN_VALUE_REGISTER_CONVENTION;
262 /* This is the implementation of gdbarch method frame_align. */
264 static CORE_ADDR
265 tilegx_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
267 return addr & -8;
271 /* Implement the "push_dummy_call" gdbarch method. */
273 static CORE_ADDR
274 tilegx_push_dummy_call (struct gdbarch *gdbarch,
275 struct value *function,
276 struct regcache *regcache,
277 CORE_ADDR bp_addr, int nargs,
278 struct value **args,
279 CORE_ADDR sp, function_call_return_method return_method,
280 CORE_ADDR struct_addr)
282 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
283 CORE_ADDR stack_dest = sp;
284 int argreg = TILEGX_R0_REGNUM;
285 int i, j;
286 int typelen, slacklen;
287 static const gdb_byte four_zero_words[16] = { 0 };
289 /* If struct_return is 1, then the struct return address will
290 consume one argument-passing register. */
291 if (return_method == return_method_struct)
292 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
294 /* Arguments are passed in R0 - R9, and as soon as an argument
295 will not fit completely in the remaining registers, then it,
296 and all remaining arguments, are put on the stack. */
297 for (i = 0; i < nargs && argreg <= TILEGX_R9_REGNUM; i++)
299 const gdb_byte *val;
300 typelen = args[i]->enclosing_type ()->length ();
302 if (typelen > (TILEGX_R9_REGNUM - argreg + 1) * tilegx_reg_size)
303 break;
305 /* Put argument into registers wordwise. */
306 val = args[i]->contents ().data ();
307 for (j = 0; j < typelen; j += tilegx_reg_size)
309 /* ISSUE: Why special handling for "typelen = 4x + 1"?
310 I don't ever see "typelen" values except 4 and 8. */
311 int n = (typelen - j == 1) ? 1 : tilegx_reg_size;
312 ULONGEST w = extract_unsigned_integer (val + j, n, byte_order);
314 regcache_cooked_write_unsigned (regcache, argreg++, w);
318 /* Align SP. */
319 stack_dest = tilegx_frame_align (gdbarch, stack_dest);
321 /* Loop backwards through remaining arguments and push them on
322 the stack, word aligned. */
323 for (j = nargs - 1; j >= i; j--)
325 const gdb_byte *contents = args[j]->contents ().data ();
327 typelen = args[j]->enclosing_type ()->length ();
328 slacklen = align_up (typelen, 8) - typelen;
329 gdb::byte_vector val (typelen + slacklen);
330 memcpy (val.data (), contents, typelen);
331 memset (val.data () + typelen, 0, slacklen);
333 /* Now write data to the stack. The stack grows downwards. */
334 stack_dest -= typelen + slacklen;
335 write_memory (stack_dest, val.data (), typelen + slacklen);
338 /* Add 16 bytes for linkage space to the stack. */
339 stack_dest = stack_dest - 16;
340 write_memory (stack_dest, four_zero_words, 16);
342 /* Update stack pointer. */
343 regcache_cooked_write_unsigned (regcache, TILEGX_SP_REGNUM, stack_dest);
345 /* Set the return address register to point to the entry point of
346 the program, where a breakpoint lies in wait. */
347 regcache_cooked_write_unsigned (regcache, TILEGX_LR_REGNUM, bp_addr);
349 return stack_dest;
353 /* Decode the instructions within the given address range.
354 Decide when we must have reached the end of the function prologue.
355 If a frame_info pointer is provided, fill in its saved_regs etc.
356 Returns the address of the first instruction after the prologue.
357 NOTE: This is often called with start_addr being the start of some
358 function, and end_addr being the current PC. */
360 static CORE_ADDR
361 tilegx_analyze_prologue (struct gdbarch* gdbarch,
362 CORE_ADDR start_addr, CORE_ADDR end_addr,
363 struct tilegx_frame_cache *cache,
364 const frame_info_ptr &next_frame)
366 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
367 CORE_ADDR next_addr;
368 CORE_ADDR prolog_end = end_addr;
369 gdb_byte instbuf[32 * TILEGX_BUNDLE_SIZE_IN_BYTES];
370 CORE_ADDR instbuf_start;
371 unsigned int instbuf_size;
372 int status;
373 uint64_t bundle;
374 struct tilegx_decoded_instruction
375 decoded[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
376 int num_insns;
377 struct tilegx_reverse_regs reverse_frame[TILEGX_NUM_PHYS_REGS];
378 struct tilegx_reverse_regs
379 new_reverse_frame[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
380 int dest_regs[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
381 int reverse_frame_valid, prolog_done, branch_seen, lr_saved_on_stack_p;
382 LONGEST prev_sp_value;
383 int i, j;
385 if (start_addr >= end_addr
386 || (start_addr % TILEGX_BUNDLE_ALIGNMENT_IN_BYTES) != 0)
387 return end_addr;
389 /* Initialize the reverse frame. This maps the CURRENT frame's
390 registers to the outer frame's registers (the frame on the
391 stack goes the other way). */
392 memcpy (&reverse_frame, &template_reverse_regs, sizeof (reverse_frame));
394 prolog_done = 0;
395 branch_seen = 0;
396 prev_sp_value = 0;
397 lr_saved_on_stack_p = 0;
399 /* To cut down on round-trip overhead, we fetch multiple bundles
400 at once. These variables describe the range of memory we have
401 prefetched. */
402 instbuf_start = 0;
403 instbuf_size = 0;
405 for (next_addr = start_addr;
406 next_addr < end_addr;
407 next_addr += TILEGX_BUNDLE_SIZE_IN_BYTES)
409 /* Retrieve the next instruction. */
410 if (next_addr - instbuf_start >= instbuf_size)
412 /* Figure out how many bytes to fetch. Don't span a page
413 boundary since that might cause an unnecessary memory
414 error. */
415 unsigned int size_on_same_page = 4096 - (next_addr & 4095);
417 instbuf_size = sizeof instbuf;
419 if (instbuf_size > size_on_same_page)
420 instbuf_size = size_on_same_page;
422 instbuf_size = std::min ((CORE_ADDR) instbuf_size,
423 (end_addr - next_addr));
424 instbuf_start = next_addr;
426 status = safe_frame_unwind_memory (next_frame, instbuf_start,
427 {instbuf, instbuf_size});
428 if (status == 0)
429 memory_error (TARGET_XFER_E_IO, next_addr);
432 reverse_frame_valid = 0;
434 bundle = extract_unsigned_integer (&instbuf[next_addr - instbuf_start],
435 8, byte_order);
437 num_insns = parse_insn_tilegx (bundle, next_addr, decoded);
439 for (i = 0; i < num_insns; i++)
441 struct tilegx_decoded_instruction *this_insn = &decoded[i];
442 long long *operands = this_insn->operand_values;
443 const struct tilegx_opcode *opcode = this_insn->opcode;
445 switch (opcode->mnemonic)
447 case TILEGX_OPC_ST:
448 if (cache
449 && reverse_frame[operands[0]].state == REVERSE_STATE_VALUE
450 && reverse_frame[operands[1]].state
451 == REVERSE_STATE_REGISTER)
453 LONGEST saved_address = reverse_frame[operands[0]].value;
454 unsigned saved_register
455 = (unsigned) reverse_frame[operands[1]].value;
457 cache->saved_regs[saved_register].set_addr (saved_address);
459 else if (cache
460 && (operands[0] == TILEGX_SP_REGNUM)
461 && (operands[1] == TILEGX_LR_REGNUM))
462 lr_saved_on_stack_p = 1;
463 break;
464 case TILEGX_OPC_ADDI:
465 case TILEGX_OPC_ADDLI:
466 if (cache
467 && operands[0] == TILEGX_SP_REGNUM
468 && operands[1] == TILEGX_SP_REGNUM
469 && reverse_frame[operands[1]].state == REVERSE_STATE_REGISTER)
471 /* Special case. We're fixing up the stack frame. */
472 uint64_t hopefully_sp
473 = (unsigned) reverse_frame[operands[1]].value;
474 short op2_as_short = (short) operands[2];
475 signed char op2_as_char = (signed char) operands[2];
477 /* Fix up the sign-extension. */
478 if (opcode->mnemonic == TILEGX_OPC_ADDI)
479 op2_as_short = op2_as_char;
480 prev_sp_value = (cache->saved_regs[hopefully_sp].addr ()
481 - op2_as_short);
483 new_reverse_frame[i].state = REVERSE_STATE_VALUE;
484 new_reverse_frame[i].value
485 = cache->saved_regs[hopefully_sp].addr ();
486 cache->saved_regs[hopefully_sp].set_value (prev_sp_value);
488 else
490 short op2_as_short = (short) operands[2];
491 signed char op2_as_char = (signed char) operands[2];
493 /* Fix up the sign-extension. */
494 if (opcode->mnemonic == TILEGX_OPC_ADDI)
495 op2_as_short = op2_as_char;
497 new_reverse_frame[i] = reverse_frame[operands[1]];
498 if (new_reverse_frame[i].state == REVERSE_STATE_VALUE)
499 new_reverse_frame[i].value += op2_as_short;
500 else
501 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
503 reverse_frame_valid |= 1 << i;
504 dest_regs[i] = operands[0];
505 break;
506 case TILEGX_OPC_ADD:
507 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
508 && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
510 /* We have values -- we can do this. */
511 new_reverse_frame[i] = reverse_frame[operands[2]];
512 new_reverse_frame[i].value
513 += reverse_frame[operands[i]].value;
515 else
517 /* We don't know anything about the values. Punt. */
518 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
520 reverse_frame_valid |= 1 << i;
521 dest_regs[i] = operands[0];
522 break;
523 case TILEGX_OPC_MOVE:
524 new_reverse_frame[i] = reverse_frame[operands[1]];
525 reverse_frame_valid |= 1 << i;
526 dest_regs[i] = operands[0];
527 break;
528 case TILEGX_OPC_MOVEI:
529 case TILEGX_OPC_MOVELI:
530 new_reverse_frame[i].state = REVERSE_STATE_VALUE;
531 new_reverse_frame[i].value = operands[1];
532 reverse_frame_valid |= 1 << i;
533 dest_regs[i] = operands[0];
534 break;
535 case TILEGX_OPC_ORI:
536 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE)
538 /* We have a value in A -- we can do this. */
539 new_reverse_frame[i] = reverse_frame[operands[1]];
540 new_reverse_frame[i].value
541 = reverse_frame[operands[1]].value | operands[2];
543 else if (operands[2] == 0)
545 /* This is a move. */
546 new_reverse_frame[i] = reverse_frame[operands[1]];
548 else
550 /* We don't know anything about the values. Punt. */
551 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
553 reverse_frame_valid |= 1 << i;
554 dest_regs[i] = operands[0];
555 break;
556 case TILEGX_OPC_OR:
557 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
558 && reverse_frame[operands[1]].value == 0)
560 /* This is a move. */
561 new_reverse_frame[i] = reverse_frame[operands[2]];
563 else if (reverse_frame[operands[2]].state == REVERSE_STATE_VALUE
564 && reverse_frame[operands[2]].value == 0)
566 /* This is a move. */
567 new_reverse_frame[i] = reverse_frame[operands[1]];
569 else
571 /* We don't know anything about the values. Punt. */
572 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
574 reverse_frame_valid |= 1 << i;
575 dest_regs[i] = operands[0];
576 break;
577 case TILEGX_OPC_SUB:
578 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
579 && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
581 /* We have values -- we can do this. */
582 new_reverse_frame[i] = reverse_frame[operands[1]];
583 new_reverse_frame[i].value
584 -= reverse_frame[operands[2]].value;
586 else
588 /* We don't know anything about the values. Punt. */
589 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
591 reverse_frame_valid |= 1 << i;
592 dest_regs[i] = operands[0];
593 break;
595 case TILEGX_OPC_FNOP:
596 case TILEGX_OPC_INFO:
597 case TILEGX_OPC_INFOL:
598 /* Nothing to see here, move on.
599 Note that real NOP is treated as a 'real' instruction
600 because someone must have intended that it be there.
601 It therefore terminates the prolog. */
602 break;
604 case TILEGX_OPC_J:
605 case TILEGX_OPC_JAL:
607 case TILEGX_OPC_BEQZ:
608 case TILEGX_OPC_BEQZT:
609 case TILEGX_OPC_BGEZ:
610 case TILEGX_OPC_BGEZT:
611 case TILEGX_OPC_BGTZ:
612 case TILEGX_OPC_BGTZT:
613 case TILEGX_OPC_BLBC:
614 case TILEGX_OPC_BLBCT:
615 case TILEGX_OPC_BLBS:
616 case TILEGX_OPC_BLBST:
617 case TILEGX_OPC_BLEZ:
618 case TILEGX_OPC_BLEZT:
619 case TILEGX_OPC_BLTZ:
620 case TILEGX_OPC_BLTZT:
621 case TILEGX_OPC_BNEZ:
622 case TILEGX_OPC_BNEZT:
624 case TILEGX_OPC_IRET:
625 case TILEGX_OPC_JALR:
626 case TILEGX_OPC_JALRP:
627 case TILEGX_OPC_JR:
628 case TILEGX_OPC_JRP:
629 case TILEGX_OPC_SWINT0:
630 case TILEGX_OPC_SWINT1:
631 case TILEGX_OPC_SWINT2:
632 case TILEGX_OPC_SWINT3:
633 /* We're really done -- this is a branch. */
634 branch_seen = 1;
635 prolog_done = 1;
636 break;
637 default:
638 /* We don't know or care what this instruction is.
639 All we know is that it isn't part of a prolog, and if
640 there's a destination register, we're trashing it. */
641 prolog_done = 1;
642 for (j = 0; j < opcode->num_operands; j++)
644 if (this_insn->operands[j]->is_dest_reg)
646 dest_regs[i] = operands[j];
647 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
648 reverse_frame_valid |= 1 << i;
649 break;
652 break;
656 /* Now update the reverse frames. */
657 for (i = 0; i < num_insns; i++)
659 /* ISSUE: Does this properly handle "network" registers? */
660 if ((reverse_frame_valid & (1 << i))
661 && dest_regs[i] != TILEGX_ZERO_REGNUM)
662 reverse_frame[dest_regs[i]] = new_reverse_frame[i];
665 if (prev_sp_value != 0)
667 /* GCC uses R52 as a frame pointer. Have we seen "move r52, sp"? */
668 if (reverse_frame[TILEGX_R52_REGNUM].state == REVERSE_STATE_REGISTER
669 && reverse_frame[TILEGX_R52_REGNUM].value == TILEGX_SP_REGNUM)
671 reverse_frame[TILEGX_R52_REGNUM].state = REVERSE_STATE_VALUE;
672 reverse_frame[TILEGX_R52_REGNUM].value = prev_sp_value;
675 prev_sp_value = 0;
678 if (prolog_done && prolog_end == end_addr)
680 /* We found non-prolog code. As such, _this_ instruction
681 is the one after the prolog. We keep processing, because
682 there may be more prolog code in there, but this is what
683 we'll return. */
684 /* ISSUE: There may not have actually been a prologue, and
685 we may have simply skipped some random instructions. */
686 prolog_end = next_addr;
688 if (branch_seen)
690 /* We saw a branch. The prolog absolutely must be over. */
691 break;
695 if (prolog_end == end_addr && cache)
697 /* We may have terminated the prolog early, and we're certainly
698 at THIS point right now. It's possible that the values of
699 registers we need are currently actually in other registers
700 (and haven't been written to memory yet). Go find them. */
701 for (i = 0; i < TILEGX_NUM_PHYS_REGS; i++)
703 if (reverse_frame[i].state == REVERSE_STATE_REGISTER
704 && reverse_frame[i].value != i)
706 unsigned saved_register = (unsigned) reverse_frame[i].value;
708 cache->saved_regs[saved_register].set_realreg (i);
713 if (lr_saved_on_stack_p)
715 CORE_ADDR addr = cache->saved_regs[TILEGX_SP_REGNUM].addr ();
716 cache->saved_regs[TILEGX_LR_REGNUM].set_addr (addr);
719 return prolog_end;
722 /* This is the implementation of gdbarch method skip_prologue. */
724 static CORE_ADDR
725 tilegx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
727 CORE_ADDR func_start, end_pc;
728 struct obj_section *s;
730 /* This is the preferred method, find the end of the prologue by
731 using the debugging information. */
732 if (find_pc_partial_function (start_pc, NULL, &func_start, NULL))
734 CORE_ADDR post_prologue_pc
735 = skip_prologue_using_sal (gdbarch, func_start);
737 if (post_prologue_pc != 0)
738 return std::max (start_pc, post_prologue_pc);
741 /* Don't straddle a section boundary. */
742 s = find_pc_section (start_pc);
743 end_pc = start_pc + 8 * TILEGX_BUNDLE_SIZE_IN_BYTES;
744 if (s != NULL)
745 end_pc = std::min (end_pc, s->endaddr ());
747 /* Otherwise, try to skip prologue the hard way. */
748 return tilegx_analyze_prologue (gdbarch,
749 start_pc,
750 end_pc,
751 NULL, NULL);
754 /* This is the implementation of gdbarch method stack_frame_destroyed_p. */
756 static int
757 tilegx_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
759 CORE_ADDR func_addr = 0, func_end = 0;
761 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
763 CORE_ADDR addr = func_end - TILEGX_BUNDLE_SIZE_IN_BYTES;
765 /* FIXME: Find the actual epilogue. */
766 /* HACK: Just assume the final bundle is the "ret" instruction". */
767 if (pc > addr)
768 return 1;
770 return 0;
773 /* This is the implementation of gdbarch method get_longjmp_target. */
775 static int
776 tilegx_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
778 struct gdbarch *gdbarch = get_frame_arch (frame);
779 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
780 CORE_ADDR jb_addr;
781 gdb_byte buf[8];
783 jb_addr = get_frame_register_unsigned (frame, TILEGX_R0_REGNUM);
785 /* TileGX jmp_buf contains 32 elements of type __uint_reg_t which
786 has a size of 8 bytes. The return address is stored in the 25th
787 slot. */
788 if (target_read_memory (jb_addr + 25 * 8, buf, 8))
789 return 0;
791 *pc = extract_unsigned_integer (buf, 8, byte_order);
793 return 1;
796 /* by assigning the 'faultnum' reg in kernel pt_regs with this value,
797 kernel do_signal will not check r0. see tilegx kernel/signal.c
798 for details. */
799 #define INT_SWINT_1_SIGRETURN (~0)
801 /* Implement the "write_pc" gdbarch method. */
803 static void
804 tilegx_write_pc (struct regcache *regcache, CORE_ADDR pc)
806 regcache_cooked_write_unsigned (regcache, TILEGX_PC_REGNUM, pc);
808 /* We must be careful with modifying the program counter. If we
809 just interrupted a system call, the kernel might try to restart
810 it when we resume the inferior. On restarting the system call,
811 the kernel will try backing up the program counter even though it
812 no longer points at the system call. This typically results in a
813 SIGSEGV or SIGILL. We can prevent this by writing INT_SWINT_1_SIGRETURN
814 in the "faultnum" pseudo-register.
816 Note that "faultnum" is saved when setting up a dummy call frame.
817 This means that it is properly restored when that frame is
818 popped, and that the interrupted system call will be restarted
819 when we resume the inferior on return from a function call from
820 within GDB. In all other cases the system call will not be
821 restarted. */
822 regcache_cooked_write_unsigned (regcache, TILEGX_FAULTNUM_REGNUM,
823 INT_SWINT_1_SIGRETURN);
826 /* 64-bit pattern for a { bpt ; nop } bundle. */
827 constexpr gdb_byte tilegx_break_insn[] =
828 { 0x00, 0x50, 0x48, 0x51, 0xae, 0x44, 0x6a, 0x28 };
830 typedef BP_MANIPULATION (tilegx_break_insn) tilegx_breakpoint;
832 /* Normal frames. */
834 static struct tilegx_frame_cache *
835 tilegx_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
837 struct gdbarch *gdbarch = get_frame_arch (this_frame);
838 struct tilegx_frame_cache *cache;
839 CORE_ADDR current_pc;
841 if (*this_cache)
842 return (struct tilegx_frame_cache *) *this_cache;
844 cache = FRAME_OBSTACK_ZALLOC (struct tilegx_frame_cache);
845 *this_cache = cache;
846 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
847 cache->base = 0;
848 cache->start_pc = get_frame_func (this_frame);
849 current_pc = get_frame_pc (this_frame);
851 cache->base = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
852 cache->saved_regs[TILEGX_SP_REGNUM].set_value (cache->base);
854 if (cache->start_pc)
855 tilegx_analyze_prologue (gdbarch, cache->start_pc, current_pc,
856 cache, this_frame);
858 cache->saved_regs[TILEGX_PC_REGNUM] = cache->saved_regs[TILEGX_LR_REGNUM];
860 return cache;
863 /* Retrieve the value of REGNUM in FRAME. */
865 static struct value*
866 tilegx_frame_prev_register (const frame_info_ptr &this_frame,
867 void **this_cache,
868 int regnum)
870 struct tilegx_frame_cache *info =
871 tilegx_frame_cache (this_frame, this_cache);
873 return trad_frame_get_prev_register (this_frame, info->saved_regs,
874 regnum);
877 /* Build frame id. */
879 static void
880 tilegx_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
881 struct frame_id *this_id)
883 struct tilegx_frame_cache *info =
884 tilegx_frame_cache (this_frame, this_cache);
886 /* This marks the outermost frame. */
887 if (info->base == 0)
888 return;
890 (*this_id) = frame_id_build (info->base, info->start_pc);
893 static CORE_ADDR
894 tilegx_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
896 struct tilegx_frame_cache *cache =
897 tilegx_frame_cache (this_frame, this_cache);
899 return cache->base;
902 static const struct frame_unwind tilegx_frame_unwind = {
903 "tilegx prologue",
904 NORMAL_FRAME,
905 default_frame_unwind_stop_reason,
906 tilegx_frame_this_id,
907 tilegx_frame_prev_register,
908 NULL, /* const struct frame_data *unwind_data */
909 default_frame_sniffer, /* frame_sniffer_ftype *sniffer */
910 NULL /* frame_prev_pc_ftype *prev_pc */
913 static const struct frame_base tilegx_frame_base = {
914 &tilegx_frame_unwind,
915 tilegx_frame_base_address,
916 tilegx_frame_base_address,
917 tilegx_frame_base_address
920 /* We cannot read/write the "special" registers. */
922 static int
923 tilegx_cannot_reference_register (struct gdbarch *gdbarch, int regno)
925 if (regno >= 0 && regno < TILEGX_NUM_EASY_REGS)
926 return 0;
927 else if (regno == TILEGX_PC_REGNUM
928 || regno == TILEGX_FAULTNUM_REGNUM)
929 return 0;
930 else
931 return 1;
934 static struct gdbarch *
935 tilegx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
937 struct gdbarch *gdbarch;
938 int arch_size = 64;
940 /* Handle arch_size == 32 or 64. Default to 64. */
941 if (info.abfd)
942 arch_size = bfd_get_arch_size (info.abfd);
944 /* Try to find a pre-existing architecture. */
945 for (arches = gdbarch_list_lookup_by_info (arches, &info);
946 arches != NULL;
947 arches = gdbarch_list_lookup_by_info (arches->next, &info))
949 /* We only have two flavors -- just make sure arch_size matches. */
950 if (gdbarch_ptr_bit (arches->gdbarch) == arch_size)
951 return (arches->gdbarch);
954 gdbarch = gdbarch_alloc (&info, NULL);
956 /* Basic register fields and methods, datatype sizes and stuff. */
958 /* There are 64 physical registers which can be referenced by
959 instructions (although only 56 of them can actually be
960 debugged) and 1 magic register (the PC). The other three
961 magic registers (ex1, syscall, orig_r0) which are known to
962 "ptrace" are ignored by "gdb". Note that we simply pretend
963 that there are 65 registers, and no "pseudo registers". */
964 set_gdbarch_num_regs (gdbarch, TILEGX_NUM_REGS);
965 set_gdbarch_num_pseudo_regs (gdbarch, 0);
967 set_gdbarch_sp_regnum (gdbarch, TILEGX_SP_REGNUM);
968 set_gdbarch_pc_regnum (gdbarch, TILEGX_PC_REGNUM);
970 set_gdbarch_register_name (gdbarch, tilegx_register_name);
971 set_gdbarch_register_type (gdbarch, tilegx_register_type);
973 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
974 set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
975 set_gdbarch_long_bit (gdbarch, arch_size);
976 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
978 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
979 set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
980 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
982 set_gdbarch_ptr_bit (gdbarch, arch_size);
983 set_gdbarch_addr_bit (gdbarch, arch_size);
985 set_gdbarch_cannot_fetch_register (gdbarch,
986 tilegx_cannot_reference_register);
987 set_gdbarch_cannot_store_register (gdbarch,
988 tilegx_cannot_reference_register);
990 /* Stack grows down. */
991 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
993 /* Frame Info. */
994 set_gdbarch_frame_align (gdbarch, tilegx_frame_align);
995 frame_base_set_default (gdbarch, &tilegx_frame_base);
997 set_gdbarch_skip_prologue (gdbarch, tilegx_skip_prologue);
999 set_gdbarch_stack_frame_destroyed_p (gdbarch, tilegx_stack_frame_destroyed_p);
1001 /* Map debug registers into internal register numbers. */
1002 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, tilegx_dwarf2_reg_to_regnum);
1004 /* These values and methods are used when gdb calls a target function. */
1005 set_gdbarch_push_dummy_call (gdbarch, tilegx_push_dummy_call);
1006 set_gdbarch_get_longjmp_target (gdbarch, tilegx_get_longjmp_target);
1007 set_gdbarch_write_pc (gdbarch, tilegx_write_pc);
1008 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1009 tilegx_breakpoint::kind_from_pc);
1010 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1011 tilegx_breakpoint::bp_from_kind);
1012 set_gdbarch_return_value (gdbarch, tilegx_return_value);
1014 gdbarch_init_osabi (info, gdbarch);
1016 dwarf2_append_unwinders (gdbarch);
1017 frame_unwind_append_unwinder (gdbarch, &tilegx_frame_unwind);
1019 return gdbarch;
1022 void _initialize_tilegx_tdep ();
1023 void
1024 _initialize_tilegx_tdep ()
1026 gdbarch_register (bfd_arch_tilegx, tilegx_gdbarch_init);