1 /* Target-dependent code for the Tilera TILE-Gx processor.
3 Copyright (C) 2012-2014 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/>. */
22 #include "frame-base.h"
23 #include "frame-unwind.h"
24 #include "dwarf2-frame.h"
25 #include "trad-frame.h"
34 #include "gdb_assert.h"
35 #include "arch-utils.h"
36 #include "floatformat.h"
41 #include "linux-tdep.h"
43 #include "solib-svr4.h"
44 #include "tilegx-tdep.h"
45 #include "opcode/tilegx.h"
47 struct tilegx_frame_cache
54 /* Table of saved registers. */
55 struct trad_frame_saved_reg *saved_regs;
58 /* Register state values used by analyze_prologue. */
61 REVERSE_STATE_REGISTER,
66 /* Register state used by analyze_prologue(). */
67 struct tilegx_reverse_regs
70 enum reverse_state state;
73 static const struct tilegx_reverse_regs
74 template_reverse_regs[TILEGX_NUM_PHYS_REGS] =
76 { TILEGX_R0_REGNUM, REVERSE_STATE_REGISTER },
77 { TILEGX_R1_REGNUM, REVERSE_STATE_REGISTER },
78 { TILEGX_R2_REGNUM, REVERSE_STATE_REGISTER },
79 { TILEGX_R3_REGNUM, REVERSE_STATE_REGISTER },
80 { TILEGX_R4_REGNUM, REVERSE_STATE_REGISTER },
81 { TILEGX_R5_REGNUM, REVERSE_STATE_REGISTER },
82 { TILEGX_R6_REGNUM, REVERSE_STATE_REGISTER },
83 { TILEGX_R7_REGNUM, REVERSE_STATE_REGISTER },
84 { TILEGX_R8_REGNUM, REVERSE_STATE_REGISTER },
85 { TILEGX_R9_REGNUM, REVERSE_STATE_REGISTER },
86 { TILEGX_R10_REGNUM, REVERSE_STATE_REGISTER },
87 { TILEGX_R11_REGNUM, REVERSE_STATE_REGISTER },
88 { TILEGX_R12_REGNUM, REVERSE_STATE_REGISTER },
89 { TILEGX_R13_REGNUM, REVERSE_STATE_REGISTER },
90 { TILEGX_R14_REGNUM, REVERSE_STATE_REGISTER },
91 { TILEGX_R15_REGNUM, REVERSE_STATE_REGISTER },
92 { TILEGX_R16_REGNUM, REVERSE_STATE_REGISTER },
93 { TILEGX_R17_REGNUM, REVERSE_STATE_REGISTER },
94 { TILEGX_R18_REGNUM, REVERSE_STATE_REGISTER },
95 { TILEGX_R19_REGNUM, REVERSE_STATE_REGISTER },
96 { TILEGX_R20_REGNUM, REVERSE_STATE_REGISTER },
97 { TILEGX_R21_REGNUM, REVERSE_STATE_REGISTER },
98 { TILEGX_R22_REGNUM, REVERSE_STATE_REGISTER },
99 { TILEGX_R23_REGNUM, REVERSE_STATE_REGISTER },
100 { TILEGX_R24_REGNUM, REVERSE_STATE_REGISTER },
101 { TILEGX_R25_REGNUM, REVERSE_STATE_REGISTER },
102 { TILEGX_R26_REGNUM, REVERSE_STATE_REGISTER },
103 { TILEGX_R27_REGNUM, REVERSE_STATE_REGISTER },
104 { TILEGX_R28_REGNUM, REVERSE_STATE_REGISTER },
105 { TILEGX_R29_REGNUM, REVERSE_STATE_REGISTER },
106 { TILEGX_R30_REGNUM, REVERSE_STATE_REGISTER },
107 { TILEGX_R31_REGNUM, REVERSE_STATE_REGISTER },
108 { TILEGX_R32_REGNUM, REVERSE_STATE_REGISTER },
109 { TILEGX_R33_REGNUM, REVERSE_STATE_REGISTER },
110 { TILEGX_R34_REGNUM, REVERSE_STATE_REGISTER },
111 { TILEGX_R35_REGNUM, REVERSE_STATE_REGISTER },
112 { TILEGX_R36_REGNUM, REVERSE_STATE_REGISTER },
113 { TILEGX_R37_REGNUM, REVERSE_STATE_REGISTER },
114 { TILEGX_R38_REGNUM, REVERSE_STATE_REGISTER },
115 { TILEGX_R39_REGNUM, REVERSE_STATE_REGISTER },
116 { TILEGX_R40_REGNUM, REVERSE_STATE_REGISTER },
117 { TILEGX_R41_REGNUM, REVERSE_STATE_REGISTER },
118 { TILEGX_R42_REGNUM, REVERSE_STATE_REGISTER },
119 { TILEGX_R43_REGNUM, REVERSE_STATE_REGISTER },
120 { TILEGX_R44_REGNUM, REVERSE_STATE_REGISTER },
121 { TILEGX_R45_REGNUM, REVERSE_STATE_REGISTER },
122 { TILEGX_R46_REGNUM, REVERSE_STATE_REGISTER },
123 { TILEGX_R47_REGNUM, REVERSE_STATE_REGISTER },
124 { TILEGX_R48_REGNUM, REVERSE_STATE_REGISTER },
125 { TILEGX_R49_REGNUM, REVERSE_STATE_REGISTER },
126 { TILEGX_R50_REGNUM, REVERSE_STATE_REGISTER },
127 { TILEGX_R51_REGNUM, REVERSE_STATE_REGISTER },
128 { TILEGX_R52_REGNUM, REVERSE_STATE_REGISTER },
129 { TILEGX_TP_REGNUM, REVERSE_STATE_REGISTER },
130 { TILEGX_SP_REGNUM, REVERSE_STATE_REGISTER },
131 { TILEGX_LR_REGNUM, REVERSE_STATE_REGISTER },
132 { 0, REVERSE_STATE_UNKNOWN },
133 { 0, REVERSE_STATE_UNKNOWN },
134 { 0, REVERSE_STATE_UNKNOWN },
135 { 0, REVERSE_STATE_UNKNOWN },
136 { 0, REVERSE_STATE_UNKNOWN },
137 { 0, REVERSE_STATE_UNKNOWN },
138 { 0, REVERSE_STATE_UNKNOWN },
139 { TILEGX_ZERO_REGNUM, REVERSE_STATE_VALUE }
142 /* Implement the "register_name" gdbarch method. */
145 tilegx_register_name (struct gdbarch *gdbarch, int regnum)
147 static const char *const register_names[TILEGX_NUM_REGS] =
149 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
150 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
151 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
152 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
153 "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
154 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
155 "r48", "r49", "r50", "r51", "r52", "tp", "sp", "lr",
156 "sn", "idn0", "idn1", "udn0", "udn1", "udn2", "udn3", "zero",
160 if (regnum < 0 || regnum >= TILEGX_NUM_REGS)
161 internal_error (__FILE__, __LINE__,
162 "tilegx_register_name: invalid register number %d",
165 return register_names[regnum];
168 /* This is the implementation of gdbarch method register_type. */
171 tilegx_register_type (struct gdbarch *gdbarch, int regnum)
173 if (regnum == TILEGX_PC_REGNUM)
174 return builtin_type (gdbarch)->builtin_func_ptr;
176 return builtin_type (gdbarch)->builtin_uint64;
179 /* This is the implementation of gdbarch method dwarf2_reg_to_regnum. */
182 tilegx_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
187 /* Makes the decision of whether a given type is a scalar type.
188 Scalar types are returned in the registers r2-r11 as they fit. */
191 tilegx_type_is_scalar (struct type *t)
193 return (TYPE_CODE(t) != TYPE_CODE_STRUCT
194 && TYPE_CODE(t) != TYPE_CODE_UNION
195 && TYPE_CODE(t) != TYPE_CODE_ARRAY);
198 /* Returns non-zero if the given struct type will be returned using
199 a special convention, rather than the normal function return method.
200 Used in the context of the "return" command, and target function
201 calls from the debugger. */
204 tilegx_use_struct_convention (struct type *type)
206 /* Only scalars which fit in R0 - R9 can be returned in registers.
207 Otherwise, they are returned via a pointer passed in R0. */
208 return (!tilegx_type_is_scalar (type)
209 && (TYPE_LENGTH (type) > (1 + TILEGX_R9_REGNUM - TILEGX_R0_REGNUM)
213 /* Find a function's return value in the appropriate registers (in
214 REGCACHE), and copy it into VALBUF. */
217 tilegx_extract_return_value (struct type *type, struct regcache *regcache,
220 int len = TYPE_LENGTH (type);
221 int i, regnum = TILEGX_R0_REGNUM;
223 for (i = 0; i < len; i += tilegx_reg_size)
224 regcache_raw_read (regcache, regnum++, valbuf + i);
227 /* Copy the function return value from VALBUF into the proper
228 location for a function return.
229 Called only in the context of the "return" command. */
232 tilegx_store_return_value (struct type *type, struct regcache *regcache,
235 if (TYPE_LENGTH (type) < tilegx_reg_size)
237 /* Add leading zeros to the (little-endian) value. */
238 gdb_byte buf[tilegx_reg_size] = { 0 };
240 memcpy (buf, valbuf, TYPE_LENGTH (type));
241 regcache_raw_write (regcache, TILEGX_R0_REGNUM, buf);
245 int len = TYPE_LENGTH (type);
246 int i, regnum = TILEGX_R0_REGNUM;
248 for (i = 0; i < len; i += tilegx_reg_size)
249 regcache_raw_write (regcache, regnum++, (gdb_byte *) valbuf + i);
253 /* This is the implementation of gdbarch method return_value. */
255 static enum return_value_convention
256 tilegx_return_value (struct gdbarch *gdbarch, struct value *function,
257 struct type *type, struct regcache *regcache,
258 gdb_byte *readbuf, const gdb_byte *writebuf)
260 if (tilegx_use_struct_convention (type))
261 return RETURN_VALUE_STRUCT_CONVENTION;
263 tilegx_store_return_value (type, regcache, writebuf);
265 tilegx_extract_return_value (type, regcache, readbuf);
266 return RETURN_VALUE_REGISTER_CONVENTION;
269 /* This is the implementation of gdbarch method frame_align. */
272 tilegx_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
278 /* Implement the "push_dummy_call" gdbarch method. */
281 tilegx_push_dummy_call (struct gdbarch *gdbarch,
282 struct value *function,
283 struct regcache *regcache,
284 CORE_ADDR bp_addr, int nargs,
286 CORE_ADDR sp, int struct_return,
287 CORE_ADDR struct_addr)
289 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
290 CORE_ADDR stack_dest = sp;
291 int argreg = TILEGX_R0_REGNUM;
293 int typelen, slacklen, alignlen;
294 static const gdb_byte four_zero_words[16] = { 0 };
296 /* If struct_return is 1, then the struct return address will
297 consume one argument-passing register. */
299 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
301 /* Arguments are passed in R0 - R9, and as soon as an argument
302 will not fit completely in the remaining registers, then it,
303 and all remaining arguments, are put on the stack. */
304 for (i = 0; i < nargs && argreg <= TILEGX_R9_REGNUM; i++)
307 typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
309 if (typelen > (TILEGX_R9_REGNUM - argreg + 1) * tilegx_reg_size)
312 /* Put argument into registers wordwise. */
313 val = value_contents (args[i]);
314 for (j = 0; j < typelen; j += tilegx_reg_size)
316 /* ISSUE: Why special handling for "typelen = 4x + 1"?
317 I don't ever see "typelen" values except 4 and 8. */
318 int n = (typelen - j == 1) ? 1 : tilegx_reg_size;
319 ULONGEST w = extract_unsigned_integer (val + j, n, byte_order);
321 regcache_cooked_write_unsigned (regcache, argreg++, w);
326 stack_dest = tilegx_frame_align (gdbarch, stack_dest);
328 /* Loop backwards through remaining arguments and push them on
329 the stack, word aligned. */
330 for (j = nargs - 1; j >= i; j--)
333 struct cleanup *back_to;
334 const gdb_byte *contents = value_contents (args[j]);
336 typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
337 slacklen = align_up (typelen, 8) - typelen;
338 val = xmalloc (typelen + slacklen);
339 back_to = make_cleanup (xfree, val);
340 memcpy (val, contents, typelen);
341 memset (val + typelen, 0, slacklen);
343 /* Now write data to the stack. The stack grows downwards. */
344 stack_dest -= typelen + slacklen;
345 write_memory (stack_dest, val, typelen + slacklen);
346 do_cleanups (back_to);
349 /* Add 16 bytes for linkage space to the stack. */
350 stack_dest = stack_dest - 16;
351 write_memory (stack_dest, four_zero_words, 16);
353 /* Update stack pointer. */
354 regcache_cooked_write_unsigned (regcache, TILEGX_SP_REGNUM, stack_dest);
356 /* Set the return address register to point to the entry point of
357 the program, where a breakpoint lies in wait. */
358 regcache_cooked_write_unsigned (regcache, TILEGX_LR_REGNUM, bp_addr);
364 /* Decode the instructions within the given address range.
365 Decide when we must have reached the end of the function prologue.
366 If a frame_info pointer is provided, fill in its saved_regs etc.
367 Returns the address of the first instruction after the prologue.
368 NOTE: This is often called with start_addr being the start of some
369 function, and end_addr being the current PC. */
372 tilegx_analyze_prologue (struct gdbarch* gdbarch,
373 CORE_ADDR start_addr, CORE_ADDR end_addr,
374 struct tilegx_frame_cache *cache,
375 struct frame_info *next_frame)
377 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
379 CORE_ADDR prolog_end = end_addr;
380 ULONGEST inst, inst2;
383 gdb_byte instbuf[32 * TILEGX_BUNDLE_SIZE_IN_BYTES];
384 CORE_ADDR instbuf_start;
385 unsigned int instbuf_size;
388 struct tilegx_decoded_instruction
389 decoded[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
391 struct tilegx_reverse_regs reverse_frame[TILEGX_NUM_PHYS_REGS];
392 struct tilegx_reverse_regs
393 new_reverse_frame[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
394 int dest_regs[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
395 int reverse_frame_valid, prolog_done, branch_seen, lr_saved_on_stack_p;
396 LONGEST prev_sp_value;
399 if (start_addr >= end_addr
400 || (start_addr % TILEGX_BUNDLE_ALIGNMENT_IN_BYTES) != 0)
403 /* Initialize the reverse frame. This maps the CURRENT frame's
404 registers to the outer frame's registers (the frame on the
405 stack goes the other way). */
406 memcpy (&reverse_frame, &template_reverse_regs, sizeof (reverse_frame));
411 lr_saved_on_stack_p = 0;
413 /* To cut down on round-trip overhead, we fetch multiple bundles
414 at once. These variables describe the range of memory we have
419 for (next_addr = start_addr;
420 next_addr < end_addr;
421 next_addr += TILEGX_BUNDLE_SIZE_IN_BYTES)
423 /* Retrieve the next instruction. */
424 if (next_addr - instbuf_start >= instbuf_size)
426 /* Figure out how many bytes to fetch. Don't span a page
427 boundary since that might cause an unnecessary memory
429 unsigned int size_on_same_page = 4096 - (next_addr & 4095);
431 instbuf_size = sizeof instbuf;
433 if (instbuf_size > size_on_same_page)
434 instbuf_size = size_on_same_page;
436 instbuf_size = min (instbuf_size, (end_addr - next_addr));
437 instbuf_start = next_addr;
439 status = safe_frame_unwind_memory (next_frame, instbuf_start,
440 instbuf, instbuf_size);
442 memory_error (status, next_addr);
445 reverse_frame_valid = 0;
447 bundle = extract_unsigned_integer (&instbuf[next_addr - instbuf_start],
450 num_insns = parse_insn_tilegx (bundle, next_addr, decoded);
452 for (i = 0; i < num_insns; i++)
454 struct tilegx_decoded_instruction *this_insn = &decoded[i];
455 int64_t *operands = (int64_t *) this_insn->operand_values;
456 const struct tilegx_opcode *opcode = this_insn->opcode;
458 switch (opcode->mnemonic)
462 && reverse_frame[operands[0]].state == REVERSE_STATE_VALUE
463 && reverse_frame[operands[1]].state
464 == REVERSE_STATE_REGISTER)
466 LONGEST saved_address = reverse_frame[operands[0]].value;
467 unsigned saved_register
468 = (unsigned) reverse_frame[operands[1]].value;
470 /* realreg >= 0 and addr != -1 indicates that the
471 value of saved_register is in memory location
472 saved_address. The value of realreg is not
473 meaningful in this case but it must be >= 0.
475 cache->saved_regs[saved_register].realreg = saved_register;
476 cache->saved_regs[saved_register].addr = saved_address;
479 && (operands[0] == TILEGX_SP_REGNUM)
480 && (operands[1] == TILEGX_LR_REGNUM))
481 lr_saved_on_stack_p = 1;
483 case TILEGX_OPC_ADDI:
484 case TILEGX_OPC_ADDLI:
486 && operands[0] == TILEGX_SP_REGNUM
487 && operands[1] == TILEGX_SP_REGNUM
488 && reverse_frame[operands[1]].state == REVERSE_STATE_REGISTER)
490 /* Special case. We're fixing up the stack frame. */
491 uint64_t hopefully_sp
492 = (unsigned) reverse_frame[operands[1]].value;
493 short op2_as_short = (short) operands[2];
494 signed char op2_as_char = (signed char) operands[2];
496 /* Fix up the sign-extension. */
497 if (opcode->mnemonic == TILEGX_OPC_ADDI)
498 op2_as_short = op2_as_char;
499 prev_sp_value = (cache->saved_regs[hopefully_sp].addr
502 new_reverse_frame[i].state = REVERSE_STATE_VALUE;
503 new_reverse_frame[i].value
504 = cache->saved_regs[hopefully_sp].addr;
505 trad_frame_set_value (cache->saved_regs,
506 hopefully_sp, prev_sp_value);
510 short op2_as_short = (short) operands[2];
511 signed char op2_as_char = (signed char) operands[2];
513 /* Fix up the sign-extension. */
514 if (opcode->mnemonic == TILEGX_OPC_ADDI)
515 op2_as_short = op2_as_char;
517 new_reverse_frame[i] = reverse_frame[operands[1]];
518 if (new_reverse_frame[i].state == REVERSE_STATE_VALUE)
519 new_reverse_frame[i].value += op2_as_short;
521 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
523 reverse_frame_valid |= 1 << i;
524 dest_regs[i] = operands[0];
527 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
528 && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
530 /* We have values -- we can do this. */
531 new_reverse_frame[i] = reverse_frame[operands[2]];
532 new_reverse_frame[i].value
533 += reverse_frame[operands[i]].value;
537 /* We don't know anything about the values. Punt. */
538 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
540 reverse_frame_valid |= 1 << i;
541 dest_regs[i] = operands[0];
543 case TILEGX_OPC_MOVE:
544 new_reverse_frame[i] = reverse_frame[operands[1]];
545 reverse_frame_valid |= 1 << i;
546 dest_regs[i] = operands[0];
548 case TILEGX_OPC_MOVEI:
549 case TILEGX_OPC_MOVELI:
550 new_reverse_frame[i].state = REVERSE_STATE_VALUE;
551 new_reverse_frame[i].value = operands[1];
552 reverse_frame_valid |= 1 << i;
553 dest_regs[i] = operands[0];
556 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE)
558 /* We have a value in A -- we can do this. */
559 new_reverse_frame[i] = reverse_frame[operands[1]];
560 new_reverse_frame[i].value
561 = reverse_frame[operands[1]].value | operands[2];
563 else if (operands[2] == 0)
565 /* This is a move. */
566 new_reverse_frame[i] = reverse_frame[operands[1]];
570 /* We don't know anything about the values. Punt. */
571 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
573 reverse_frame_valid |= 1 << i;
574 dest_regs[i] = operands[0];
577 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
578 && reverse_frame[operands[1]].value == 0)
580 /* This is a move. */
581 new_reverse_frame[i] = reverse_frame[operands[2]];
583 else if (reverse_frame[operands[2]].state == REVERSE_STATE_VALUE
584 && reverse_frame[operands[2]].value == 0)
586 /* This is a move. */
587 new_reverse_frame[i] = reverse_frame[operands[1]];
591 /* We don't know anything about the values. Punt. */
592 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
594 reverse_frame_valid |= 1 << i;
595 dest_regs[i] = operands[0];
598 if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
599 && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
601 /* We have values -- we can do this. */
602 new_reverse_frame[i] = reverse_frame[operands[1]];
603 new_reverse_frame[i].value
604 -= reverse_frame[operands[2]].value;
608 /* We don't know anything about the values. Punt. */
609 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
611 reverse_frame_valid |= 1 << i;
612 dest_regs[i] = operands[0];
615 case TILEGX_OPC_FNOP:
616 case TILEGX_OPC_INFO:
617 case TILEGX_OPC_INFOL:
618 /* Nothing to see here, move on.
619 Note that real NOP is treated as a 'real' instruction
620 because someone must have intended that it be there.
621 It therefore terminates the prolog. */
627 case TILEGX_OPC_BEQZ:
628 case TILEGX_OPC_BEQZT:
629 case TILEGX_OPC_BGEZ:
630 case TILEGX_OPC_BGEZT:
631 case TILEGX_OPC_BGTZ:
632 case TILEGX_OPC_BGTZT:
633 case TILEGX_OPC_BLBC:
634 case TILEGX_OPC_BLBCT:
635 case TILEGX_OPC_BLBS:
636 case TILEGX_OPC_BLBST:
637 case TILEGX_OPC_BLEZ:
638 case TILEGX_OPC_BLEZT:
639 case TILEGX_OPC_BLTZ:
640 case TILEGX_OPC_BLTZT:
641 case TILEGX_OPC_BNEZ:
642 case TILEGX_OPC_BNEZT:
644 case TILEGX_OPC_IRET:
645 case TILEGX_OPC_JALR:
646 case TILEGX_OPC_JALRP:
649 case TILEGX_OPC_SWINT0:
650 case TILEGX_OPC_SWINT1:
651 case TILEGX_OPC_SWINT2:
652 case TILEGX_OPC_SWINT3:
653 /* We're really done -- this is a branch. */
658 /* We don't know or care what this instruction is.
659 All we know is that it isn't part of a prolog, and if
660 there's a destination register, we're trashing it. */
662 for (j = 0; j < opcode->num_operands; j++)
664 if (this_insn->operands[j]->is_dest_reg)
666 dest_regs[i] = operands[j];
667 new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
668 reverse_frame_valid |= 1 << i;
676 /* Now update the reverse frames. */
677 for (i = 0; i < num_insns; i++)
679 /* ISSUE: Does this properly handle "network" registers? */
680 if ((reverse_frame_valid & (1 << i))
681 && dest_regs[i] != TILEGX_ZERO_REGNUM)
682 reverse_frame[dest_regs[i]] = new_reverse_frame[i];
685 if (prev_sp_value != 0)
687 /* GCC uses R52 as a frame pointer. Have we seen "move r52, sp"? */
688 if (reverse_frame[TILEGX_R52_REGNUM].state == REVERSE_STATE_REGISTER
689 && reverse_frame[TILEGX_R52_REGNUM].value == TILEGX_SP_REGNUM)
691 reverse_frame[TILEGX_R52_REGNUM].state = REVERSE_STATE_VALUE;
692 reverse_frame[TILEGX_R52_REGNUM].value = prev_sp_value;
698 if (prolog_done && prolog_end == end_addr)
700 /* We found non-prolog code. As such, _this_ instruction
701 is the one after the prolog. We keep processing, because
702 there may be more prolog code in there, but this is what
704 /* ISSUE: There may not have actually been a prologue, and
705 we may have simply skipped some random instructions. */
706 prolog_end = next_addr;
710 /* We saw a branch. The prolog absolutely must be over. */
715 if (prolog_end == end_addr && cache)
717 /* We may have terminated the prolog early, and we're certainly
718 at THIS point right now. It's possible that the values of
719 registers we need are currently actually in other registers
720 (and haven't been written to memory yet). Go find them. */
721 for (i = 0; i < TILEGX_NUM_PHYS_REGS; i++)
723 if (reverse_frame[i].state == REVERSE_STATE_REGISTER
724 && reverse_frame[i].value != i)
726 unsigned saved_register = (unsigned) reverse_frame[i].value;
728 cache->saved_regs[saved_register].realreg = i;
729 cache->saved_regs[saved_register].addr = (LONGEST) -1;
734 if (lr_saved_on_stack_p)
736 cache->saved_regs[TILEGX_LR_REGNUM].realreg = TILEGX_LR_REGNUM;
737 cache->saved_regs[TILEGX_LR_REGNUM].addr =
738 cache->saved_regs[TILEGX_SP_REGNUM].addr;
744 /* This is the implementation of gdbarch method skip_prologue. */
747 tilegx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
749 CORE_ADDR func_start, end_pc;
750 struct obj_section *s;
752 /* This is the preferred method, find the end of the prologue by
753 using the debugging information. */
754 if (find_pc_partial_function (start_pc, NULL, &func_start, NULL))
756 CORE_ADDR post_prologue_pc
757 = skip_prologue_using_sal (gdbarch, func_start);
759 if (post_prologue_pc != 0)
760 return max (start_pc, post_prologue_pc);
763 /* Don't straddle a section boundary. */
764 s = find_pc_section (start_pc);
765 end_pc = start_pc + 8 * TILEGX_BUNDLE_SIZE_IN_BYTES;
767 end_pc = min (end_pc, obj_section_endaddr (s));
769 /* Otherwise, try to skip prologue the hard way. */
770 return tilegx_analyze_prologue (gdbarch,
776 /* This is the implementation of gdbarch method in_function_epilogue_p. */
779 tilegx_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
781 CORE_ADDR func_addr = 0, func_end = 0;
783 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
785 ULONGEST inst, inst2;
786 CORE_ADDR addr = func_end - TILEGX_BUNDLE_SIZE_IN_BYTES;
788 /* FIXME: Find the actual epilogue. */
789 /* HACK: Just assume the final bundle is the "ret" instruction". */
796 /* This is the implementation of gdbarch method get_longjmp_target. */
799 tilegx_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
801 struct gdbarch *gdbarch = get_frame_arch (frame);
802 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
806 jb_addr = get_frame_register_unsigned (frame, TILEGX_R0_REGNUM);
808 /* TileGX jmp_buf contains 32 elements of type __uint_reg_t which
809 has a size of 8 bytes. The return address is stored in the 25th
811 if (target_read_memory (jb_addr + 25 * 8, buf, 8))
814 *pc = extract_unsigned_integer (buf, 8, byte_order);
819 /* by assigning the 'faultnum' reg in kernel pt_regs with this value,
820 kernel do_signal will not check r0. see tilegx kernel/signal.c
822 #define INT_SWINT_1_SIGRETURN (~0)
824 /* Implement the "write_pc" gdbarch method. */
827 tilegx_write_pc (struct regcache *regcache, CORE_ADDR pc)
829 regcache_cooked_write_unsigned (regcache, TILEGX_PC_REGNUM, pc);
831 /* We must be careful with modifying the program counter. If we
832 just interrupted a system call, the kernel might try to restart
833 it when we resume the inferior. On restarting the system call,
834 the kernel will try backing up the program counter even though it
835 no longer points at the system call. This typically results in a
836 SIGSEGV or SIGILL. We can prevent this by writing INT_SWINT_1_SIGRETURN
837 in the "faultnum" pseudo-register.
839 Note that "faultnum" is saved when setting up a dummy call frame.
840 This means that it is properly restored when that frame is
841 popped, and that the interrupted system call will be restarted
842 when we resume the inferior on return from a function call from
843 within GDB. In all other cases the system call will not be
845 regcache_cooked_write_unsigned (regcache, TILEGX_FAULTNUM_REGNUM,
846 INT_SWINT_1_SIGRETURN);
849 /* This is the implementation of gdbarch method breakpoint_from_pc. */
851 static const unsigned char *
852 tilegx_breakpoint_from_pc (struct gdbarch *gdbarch,
853 CORE_ADDR *pcptr, int *lenptr)
855 /* 64-bit pattern for a { bpt ; nop } bundle. */
856 static const unsigned char breakpoint[] =
857 { 0x00, 0x50, 0x48, 0x51, 0xae, 0x44, 0x6a, 0x28 };
859 *lenptr = sizeof (breakpoint);
865 static struct tilegx_frame_cache *
866 tilegx_frame_cache (struct frame_info *this_frame, void **this_cache)
868 struct gdbarch *gdbarch = get_frame_arch (this_frame);
869 struct tilegx_frame_cache *cache;
870 CORE_ADDR current_pc;
876 cache = FRAME_OBSTACK_ZALLOC (struct tilegx_frame_cache);
878 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
880 cache->start_pc = get_frame_func (this_frame);
881 current_pc = get_frame_pc (this_frame);
883 cache->base = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
884 trad_frame_set_value (cache->saved_regs, TILEGX_SP_REGNUM, cache->base);
887 tilegx_analyze_prologue (gdbarch, cache->start_pc, current_pc,
890 cache->saved_regs[TILEGX_PC_REGNUM] = cache->saved_regs[TILEGX_LR_REGNUM];
895 /* Retrieve the value of REGNUM in FRAME. */
898 tilegx_frame_prev_register (struct frame_info *this_frame,
902 struct tilegx_frame_cache *info =
903 tilegx_frame_cache (this_frame, this_cache);
905 return trad_frame_get_prev_register (this_frame, info->saved_regs,
909 /* Build frame id. */
912 tilegx_frame_this_id (struct frame_info *this_frame, void **this_cache,
913 struct frame_id *this_id)
915 struct tilegx_frame_cache *info =
916 tilegx_frame_cache (this_frame, this_cache);
918 /* This marks the outermost frame. */
922 (*this_id) = frame_id_build (info->base, info->start_pc);
926 tilegx_frame_base_address (struct frame_info *this_frame, void **this_cache)
928 struct tilegx_frame_cache *cache =
929 tilegx_frame_cache (this_frame, this_cache);
934 static const struct frame_unwind tilegx_frame_unwind = {
936 default_frame_unwind_stop_reason,
937 tilegx_frame_this_id,
938 tilegx_frame_prev_register,
939 NULL, /* const struct frame_data *unwind_data */
940 default_frame_sniffer, /* frame_sniffer_ftype *sniffer */
941 NULL /* frame_prev_pc_ftype *prev_pc */
944 static const struct frame_base tilegx_frame_base = {
945 &tilegx_frame_unwind,
946 tilegx_frame_base_address,
947 tilegx_frame_base_address,
948 tilegx_frame_base_address
952 tilegx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
954 return frame_unwind_register_unsigned (next_frame, TILEGX_SP_REGNUM);
958 tilegx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
960 return frame_unwind_register_unsigned (next_frame, TILEGX_PC_REGNUM);
963 static struct frame_id
964 tilegx_unwind_dummy_id (struct gdbarch *gdbarch,
965 struct frame_info *this_frame)
969 sp = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
970 return frame_id_build (sp, get_frame_pc (this_frame));
974 /* We cannot read/write the "special" registers. */
977 tilegx_cannot_reference_register (struct gdbarch *gdbarch, int regno)
979 if (regno >= 0 && regno < TILEGX_NUM_EASY_REGS)
981 else if (regno == TILEGX_PC_REGNUM
982 || regno == TILEGX_FAULTNUM_REGNUM)
988 static struct gdbarch *
989 tilegx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
991 struct gdbarch *gdbarch;
994 /* Handle arch_size == 32 or 64. Default to 64. */
996 arch_size = bfd_get_arch_size (info.abfd);
998 /* Try to find a pre-existing architecture. */
999 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1001 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1003 /* We only have two flavors -- just make sure arch_size matches. */
1004 if (gdbarch_ptr_bit (arches->gdbarch) == arch_size)
1005 return (arches->gdbarch);
1008 gdbarch = gdbarch_alloc (&info, NULL);
1010 /* Basic register fields and methods, datatype sizes and stuff. */
1012 /* There are 64 physical registers which can be referenced by
1013 instructions (although only 56 of them can actually be
1014 debugged) and 1 magic register (the PC). The other three
1015 magic registers (ex1, syscall, orig_r0) which are known to
1016 "ptrace" are ignored by "gdb". Note that we simply pretend
1017 that there are 65 registers, and no "pseudo registers". */
1018 set_gdbarch_num_regs (gdbarch, TILEGX_NUM_REGS);
1019 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1021 set_gdbarch_sp_regnum (gdbarch, TILEGX_SP_REGNUM);
1022 set_gdbarch_pc_regnum (gdbarch, TILEGX_PC_REGNUM);
1024 set_gdbarch_register_name (gdbarch, tilegx_register_name);
1025 set_gdbarch_register_type (gdbarch, tilegx_register_type);
1027 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1028 set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1029 set_gdbarch_long_bit (gdbarch, arch_size);
1030 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1032 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1033 set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1034 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1036 set_gdbarch_ptr_bit (gdbarch, arch_size);
1037 set_gdbarch_addr_bit (gdbarch, arch_size);
1039 set_gdbarch_cannot_fetch_register (gdbarch,
1040 tilegx_cannot_reference_register);
1041 set_gdbarch_cannot_store_register (gdbarch,
1042 tilegx_cannot_reference_register);
1044 /* Stack grows down. */
1045 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1048 set_gdbarch_unwind_sp (gdbarch, tilegx_unwind_sp);
1049 set_gdbarch_unwind_pc (gdbarch, tilegx_unwind_pc);
1050 set_gdbarch_dummy_id (gdbarch, tilegx_unwind_dummy_id);
1051 set_gdbarch_frame_align (gdbarch, tilegx_frame_align);
1052 frame_base_set_default (gdbarch, &tilegx_frame_base);
1054 set_gdbarch_skip_prologue (gdbarch, tilegx_skip_prologue);
1056 set_gdbarch_in_function_epilogue_p (gdbarch,
1057 tilegx_in_function_epilogue_p);
1059 /* Map debug registers into internal register numbers. */
1060 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, tilegx_dwarf2_reg_to_regnum);
1062 /* These values and methods are used when gdb calls a target function. */
1063 set_gdbarch_push_dummy_call (gdbarch, tilegx_push_dummy_call);
1064 set_gdbarch_get_longjmp_target (gdbarch, tilegx_get_longjmp_target);
1065 set_gdbarch_write_pc (gdbarch, tilegx_write_pc);
1066 set_gdbarch_breakpoint_from_pc (gdbarch, tilegx_breakpoint_from_pc);
1067 set_gdbarch_return_value (gdbarch, tilegx_return_value);
1069 set_gdbarch_print_insn (gdbarch, print_insn_tilegx);
1071 gdbarch_init_osabi (info, gdbarch);
1073 dwarf2_append_unwinders (gdbarch);
1074 frame_unwind_append_unwinder (gdbarch, &tilegx_frame_unwind);
1079 /* Provide a prototype to silence -Wmissing-prototypes. */
1080 extern initialize_file_ftype _initialize_tilegx_tdep;
1083 _initialize_tilegx_tdep (void)
1085 register_gdbarch_init (bfd_arch_tilegx, tilegx_gdbarch_init);