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