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