Carry the gnulib getcwd backport as a patch
[deliverable/binutils-gdb.git] / gdb / nios2-tdep.c
CommitLineData
a1217d97 1/* Target-machine dependent code for Nios II, for GDB.
e2882c85 2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
a1217d97
SL
3 Contributed by Peter Brookes (pbrookes@altera.com)
4 and Andrew Draper (adraper@altera.com).
5 Contributed by Mentor Graphics, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "defs.h"
23#include "frame.h"
24#include "frame-unwind.h"
25#include "frame-base.h"
26#include "trad-frame.h"
27#include "dwarf2-frame.h"
28#include "symtab.h"
29#include "inferior.h"
30#include "gdbtypes.h"
31#include "gdbcore.h"
32#include "gdbcmd.h"
33#include "osabi.h"
34#include "target.h"
35#include "dis-asm.h"
36#include "regcache.h"
37#include "value.h"
38#include "symfile.h"
39#include "arch-utils.h"
a1217d97
SL
40#include "infcall.h"
41#include "regset.h"
42#include "target-descriptions.h"
43
44/* To get entry_point_address. */
45#include "objfiles.h"
325fac50 46#include <algorithm>
a1217d97 47
a1217d97
SL
48/* Nios II specific header. */
49#include "nios2-tdep.h"
50
51#include "features/nios2.c"
52
53/* Control debugging information emitted in this file. */
54
55static int nios2_debug = 0;
56
57/* The following structures are used in the cache for prologue
58 analysis; see the reg_value and reg_saved tables in
59 struct nios2_unwind_cache, respectively. */
60
61/* struct reg_value is used to record that a register has the same value
62 as reg at the given offset from the start of a function. */
63
64struct reg_value
65{
66 int reg;
67 unsigned int offset;
68};
69
70/* struct reg_saved is used to record that a register value has been saved at
71 basereg + addr, for basereg >= 0. If basereg < 0, that indicates
72 that the register is not known to have been saved. Note that when
73 basereg == NIOS2_Z_REGNUM (that is, r0, which holds value 0),
74 addr is an absolute address. */
75
76struct reg_saved
77{
78 int basereg;
79 CORE_ADDR addr;
80};
81
82struct nios2_unwind_cache
83{
84 /* The frame's base, optionally used by the high-level debug info. */
85 CORE_ADDR base;
86
87 /* The previous frame's inner most stack address. Used as this
88 frame ID's stack_addr. */
89 CORE_ADDR cfa;
90
91 /* The address of the first instruction in this function. */
92 CORE_ADDR pc;
93
94 /* Which register holds the return address for the frame. */
95 int return_regnum;
96
97 /* Table indicating what changes have been made to each register. */
98 struct reg_value reg_value[NIOS2_NUM_REGS];
99
100 /* Table indicating where each register has been saved. */
101 struct reg_saved reg_saved[NIOS2_NUM_REGS];
102};
103
104
105/* This array is a mapping from Dwarf-2 register numbering to GDB's. */
106
107static int nios2_dwarf2gdb_regno_map[] =
108{
109 0, 1, 2, 3,
110 4, 5, 6, 7,
111 8, 9, 10, 11,
112 12, 13, 14, 15,
113 16, 17, 18, 19,
114 20, 21, 22, 23,
115 24, 25,
116 NIOS2_GP_REGNUM, /* 26 */
117 NIOS2_SP_REGNUM, /* 27 */
118 NIOS2_FP_REGNUM, /* 28 */
119 NIOS2_EA_REGNUM, /* 29 */
120 NIOS2_BA_REGNUM, /* 30 */
121 NIOS2_RA_REGNUM, /* 31 */
122 NIOS2_PC_REGNUM, /* 32 */
123 NIOS2_STATUS_REGNUM, /* 33 */
124 NIOS2_ESTATUS_REGNUM, /* 34 */
125 NIOS2_BSTATUS_REGNUM, /* 35 */
126 NIOS2_IENABLE_REGNUM, /* 36 */
127 NIOS2_IPENDING_REGNUM, /* 37 */
128 NIOS2_CPUID_REGNUM, /* 38 */
129 39, /* CTL6 */ /* 39 */
130 NIOS2_EXCEPTION_REGNUM, /* 40 */
131 NIOS2_PTEADDR_REGNUM, /* 41 */
132 NIOS2_TLBACC_REGNUM, /* 42 */
133 NIOS2_TLBMISC_REGNUM, /* 43 */
134 NIOS2_ECCINJ_REGNUM, /* 44 */
135 NIOS2_BADADDR_REGNUM, /* 45 */
136 NIOS2_CONFIG_REGNUM, /* 46 */
137 NIOS2_MPUBASE_REGNUM, /* 47 */
138 NIOS2_MPUACC_REGNUM /* 48 */
139};
140
0fde2c53 141gdb_static_assert (ARRAY_SIZE (nios2_dwarf2gdb_regno_map) == NIOS2_NUM_REGS);
a1217d97
SL
142
143/* Implement the dwarf2_reg_to_regnum gdbarch method. */
144
145static int
146nios2_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
147{
0fde2c53
DE
148 if (dw_reg < 0 || dw_reg >= NIOS2_NUM_REGS)
149 return -1;
a1217d97
SL
150
151 return nios2_dwarf2gdb_regno_map[dw_reg];
152}
153
154/* Canonical names for the 49 registers. */
155
156static const char *const nios2_reg_names[NIOS2_NUM_REGS] =
157{
158 "zero", "at", "r2", "r3", "r4", "r5", "r6", "r7",
159 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
160 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
0b76b0ce 161 "et", "bt", "gp", "sp", "fp", "ea", "sstatus", "ra",
a1217d97
SL
162 "pc",
163 "status", "estatus", "bstatus", "ienable",
164 "ipending", "cpuid", "ctl6", "exception",
165 "pteaddr", "tlbacc", "tlbmisc", "eccinj",
166 "badaddr", "config", "mpubase", "mpuacc"
167};
168
169/* Implement the register_name gdbarch method. */
170
171static const char *
172nios2_register_name (struct gdbarch *gdbarch, int regno)
173{
174 /* Use mnemonic aliases for GPRs. */
175 if (regno >= 0 && regno < NIOS2_NUM_REGS)
176 return nios2_reg_names[regno];
177 else
178 return tdesc_register_name (gdbarch, regno);
179}
180
181/* Implement the register_type gdbarch method. */
182
183static struct type *
184nios2_register_type (struct gdbarch *gdbarch, int regno)
185{
186 /* If the XML description has register information, use that to
187 determine the register type. */
188 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
189 return tdesc_register_type (gdbarch, regno);
190
191 if (regno == NIOS2_PC_REGNUM)
192 return builtin_type (gdbarch)->builtin_func_ptr;
193 else if (regno == NIOS2_SP_REGNUM)
194 return builtin_type (gdbarch)->builtin_data_ptr;
195 else
196 return builtin_type (gdbarch)->builtin_uint32;
197}
198
199/* Given a return value in REGCACHE with a type VALTYPE,
200 extract and copy its value into VALBUF. */
201
202static void
203nios2_extract_return_value (struct gdbarch *gdbarch, struct type *valtype,
204 struct regcache *regcache, gdb_byte *valbuf)
205{
206 int len = TYPE_LENGTH (valtype);
207
208 /* Return values of up to 8 bytes are returned in $r2 $r3. */
209 if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
dca08e1f 210 regcache->cooked_read (NIOS2_R2_REGNUM, valbuf);
a1217d97
SL
211 else
212 {
213 gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
214 + register_size (gdbarch, NIOS2_R3_REGNUM)));
dca08e1f
SM
215 regcache->cooked_read (NIOS2_R2_REGNUM, valbuf);
216 regcache->cooked_read (NIOS2_R3_REGNUM, valbuf + 4);
a1217d97
SL
217 }
218}
219
220/* Write into appropriate registers a function return value
221 of type TYPE, given in virtual format. */
222
223static void
224nios2_store_return_value (struct gdbarch *gdbarch, struct type *valtype,
225 struct regcache *regcache, const gdb_byte *valbuf)
226{
227 int len = TYPE_LENGTH (valtype);
228
229 /* Return values of up to 8 bytes are returned in $r2 $r3. */
230 if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
b66f5587 231 regcache->cooked_write (NIOS2_R2_REGNUM, valbuf);
a1217d97
SL
232 else
233 {
234 gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
235 + register_size (gdbarch, NIOS2_R3_REGNUM)));
b66f5587
SM
236 regcache->cooked_write (NIOS2_R2_REGNUM, valbuf);
237 regcache->cooked_write (NIOS2_R3_REGNUM, valbuf + 4);
a1217d97
SL
238 }
239}
240
241
242/* Set up the default values of the registers. */
243
244static void
245nios2_setup_default (struct nios2_unwind_cache *cache)
246{
247 int i;
248
249 for (i = 0; i < NIOS2_NUM_REGS; i++)
250 {
251 /* All registers start off holding their previous values. */
252 cache->reg_value[i].reg = i;
253 cache->reg_value[i].offset = 0;
254
255 /* All registers start off not saved. */
256 cache->reg_saved[i].basereg = -1;
257 cache->reg_saved[i].addr = 0;
258 }
259}
260
261/* Initialize the unwind cache. */
262
263static void
264nios2_init_cache (struct nios2_unwind_cache *cache, CORE_ADDR pc)
265{
266 cache->base = 0;
267 cache->cfa = 0;
268 cache->pc = pc;
269 cache->return_regnum = NIOS2_RA_REGNUM;
270 nios2_setup_default (cache);
271}
272
d53c26c7
SL
273/* Read and identify an instruction at PC. If INSNP is non-null,
274 store the instruction word into that location. Return the opcode
275 pointer or NULL if the memory couldn't be read or disassembled. */
276
277static const struct nios2_opcode *
278nios2_fetch_insn (struct gdbarch *gdbarch, CORE_ADDR pc,
279 unsigned int *insnp)
280{
281 LONGEST memword;
282 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
283 unsigned int insn;
284
af60a1ef
SL
285 if (mach == bfd_mach_nios2r2)
286 {
287 if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
288 BFD_ENDIAN_LITTLE, &memword)
289 && !safe_read_memory_integer (pc, NIOS2_CDX_OPCODE_SIZE,
290 BFD_ENDIAN_LITTLE, &memword))
291 return NULL;
292 }
293 else if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
294 gdbarch_byte_order (gdbarch), &memword))
d53c26c7
SL
295 return NULL;
296
297 insn = (unsigned int) memword;
298 if (insnp)
299 *insnp = insn;
300 return nios2_find_opcode_hash (insn, mach);
301}
302
303
304/* Match and disassemble an ADD-type instruction, with 3 register operands.
305 Returns true on success, and fills in the operand pointers. */
306
307static int
308nios2_match_add (uint32_t insn, const struct nios2_opcode *op,
309 unsigned long mach, int *ra, int *rb, int *rc)
310{
af60a1ef
SL
311 int is_r2 = (mach == bfd_mach_nios2r2);
312
313 if (!is_r2 && (op->match == MATCH_R1_ADD || op->match == MATCH_R1_MOV))
d53c26c7
SL
314 {
315 *ra = GET_IW_R_A (insn);
316 *rb = GET_IW_R_B (insn);
317 *rc = GET_IW_R_C (insn);
318 return 1;
319 }
af60a1ef
SL
320 else if (!is_r2)
321 return 0;
322 else if (op->match == MATCH_R2_ADD || op->match == MATCH_R2_MOV)
323 {
324 *ra = GET_IW_F3X6L5_A (insn);
325 *rb = GET_IW_F3X6L5_B (insn);
326 *rc = GET_IW_F3X6L5_C (insn);
327 return 1;
328 }
329 else if (op->match == MATCH_R2_ADD_N)
330 {
331 *ra = nios2_r2_reg3_mappings[GET_IW_T3X1_A3 (insn)];
332 *rb = nios2_r2_reg3_mappings[GET_IW_T3X1_B3 (insn)];
333 *rc = nios2_r2_reg3_mappings[GET_IW_T3X1_C3 (insn)];
334 return 1;
335 }
336 else if (op->match == MATCH_R2_MOV_N)
337 {
338 *ra = GET_IW_F2_A (insn);
339 *rb = 0;
340 *rc = GET_IW_F2_B (insn);
341 return 1;
342 }
d53c26c7
SL
343 return 0;
344}
345
346/* Match and disassemble a SUB-type instruction, with 3 register operands.
347 Returns true on success, and fills in the operand pointers. */
348
349static int
350nios2_match_sub (uint32_t insn, const struct nios2_opcode *op,
351 unsigned long mach, int *ra, int *rb, int *rc)
352{
af60a1ef
SL
353 int is_r2 = (mach == bfd_mach_nios2r2);
354
355 if (!is_r2 && op->match == MATCH_R1_SUB)
d53c26c7
SL
356 {
357 *ra = GET_IW_R_A (insn);
358 *rb = GET_IW_R_B (insn);
359 *rc = GET_IW_R_C (insn);
360 return 1;
361 }
af60a1ef
SL
362 else if (!is_r2)
363 return 0;
364 else if (op->match == MATCH_R2_SUB)
365 {
366 *ra = GET_IW_F3X6L5_A (insn);
367 *rb = GET_IW_F3X6L5_B (insn);
368 *rc = GET_IW_F3X6L5_C (insn);
369 return 1;
370 }
371 else if (op->match == MATCH_R2_SUB_N)
372 {
373 *ra = nios2_r2_reg3_mappings[GET_IW_T3X1_A3 (insn)];
374 *rb = nios2_r2_reg3_mappings[GET_IW_T3X1_B3 (insn)];
375 *rc = nios2_r2_reg3_mappings[GET_IW_T3X1_C3 (insn)];
376 return 1;
377 }
d53c26c7
SL
378 return 0;
379}
380
381/* Match and disassemble an ADDI-type instruction, with 2 register operands
382 and one immediate operand.
383 Returns true on success, and fills in the operand pointers. */
384
385static int
386nios2_match_addi (uint32_t insn, const struct nios2_opcode *op,
387 unsigned long mach, int *ra, int *rb, int *imm)
388{
af60a1ef
SL
389 int is_r2 = (mach == bfd_mach_nios2r2);
390
391 if (!is_r2 && op->match == MATCH_R1_ADDI)
d53c26c7
SL
392 {
393 *ra = GET_IW_I_A (insn);
394 *rb = GET_IW_I_B (insn);
395 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
396 return 1;
397 }
af60a1ef
SL
398 else if (!is_r2)
399 return 0;
400 else if (op->match == MATCH_R2_ADDI)
401 {
402 *ra = GET_IW_F2I16_A (insn);
403 *rb = GET_IW_F2I16_B (insn);
404 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
405 return 1;
406 }
407 else if (op->match == MATCH_R2_ADDI_N || op->match == MATCH_R2_SUBI_N)
408 {
409 *ra = nios2_r2_reg3_mappings[GET_IW_T2X1I3_A3 (insn)];
410 *rb = nios2_r2_reg3_mappings[GET_IW_T2X1I3_B3 (insn)];
411 *imm = nios2_r2_asi_n_mappings[GET_IW_T2X1I3_IMM3 (insn)];
412 if (op->match == MATCH_R2_SUBI_N)
413 *imm = - (*imm);
414 return 1;
415 }
416 else if (op->match == MATCH_R2_SPADDI_N)
417 {
418 *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
419 *rb = NIOS2_SP_REGNUM;
420 *imm = GET_IW_T1I7_IMM7 (insn) << 2;
421 return 1;
422 }
423 else if (op->match == MATCH_R2_SPINCI_N || op->match == MATCH_R2_SPDECI_N)
424 {
425 *ra = NIOS2_SP_REGNUM;
426 *rb = NIOS2_SP_REGNUM;
427 *imm = GET_IW_X1I7_IMM7 (insn) << 2;
428 if (op->match == MATCH_R2_SPDECI_N)
429 *imm = - (*imm);
430 return 1;
431 }
d53c26c7
SL
432 return 0;
433}
434
435/* Match and disassemble an ORHI-type instruction, with 2 register operands
436 and one unsigned immediate operand.
437 Returns true on success, and fills in the operand pointers. */
438
439static int
440nios2_match_orhi (uint32_t insn, const struct nios2_opcode *op,
441 unsigned long mach, int *ra, int *rb, unsigned int *uimm)
442{
af60a1ef
SL
443 int is_r2 = (mach == bfd_mach_nios2r2);
444
445 if (!is_r2 && op->match == MATCH_R1_ORHI)
d53c26c7
SL
446 {
447 *ra = GET_IW_I_A (insn);
448 *rb = GET_IW_I_B (insn);
449 *uimm = GET_IW_I_IMM16 (insn);
450 return 1;
451 }
af60a1ef
SL
452 else if (!is_r2)
453 return 0;
454 else if (op->match == MATCH_R2_ORHI)
455 {
456 *ra = GET_IW_F2I16_A (insn);
457 *rb = GET_IW_F2I16_B (insn);
458 *uimm = GET_IW_F2I16_IMM16 (insn);
459 return 1;
460 }
d53c26c7
SL
461 return 0;
462}
463
464/* Match and disassemble a STW-type instruction, with 2 register operands
465 and one immediate operand.
466 Returns true on success, and fills in the operand pointers. */
467
468static int
469nios2_match_stw (uint32_t insn, const struct nios2_opcode *op,
470 unsigned long mach, int *ra, int *rb, int *imm)
471{
af60a1ef
SL
472 int is_r2 = (mach == bfd_mach_nios2r2);
473
474 if (!is_r2 && (op->match == MATCH_R1_STW || op->match == MATCH_R1_STWIO))
d53c26c7
SL
475 {
476 *ra = GET_IW_I_A (insn);
477 *rb = GET_IW_I_B (insn);
478 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
479 return 1;
480 }
af60a1ef
SL
481 else if (!is_r2)
482 return 0;
483 else if (op->match == MATCH_R2_STW)
484 {
485 *ra = GET_IW_F2I16_A (insn);
486 *rb = GET_IW_F2I16_B (insn);
487 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
488 return 1;
489 }
490 else if (op->match == MATCH_R2_STWIO)
491 {
492 *ra = GET_IW_F2X4I12_A (insn);
493 *rb = GET_IW_F2X4I12_B (insn);
494 *imm = (signed) (GET_IW_F2X4I12_IMM12 (insn) << 20) >> 20;
495 return 1;
496 }
497 else if (op->match == MATCH_R2_STW_N)
498 {
499 *ra = nios2_r2_reg3_mappings[GET_IW_T2I4_A3 (insn)];
500 *rb = nios2_r2_reg3_mappings[GET_IW_T2I4_B3 (insn)];
501 *imm = GET_IW_T2I4_IMM4 (insn) << 2;
502 return 1;
503 }
504 else if (op->match == MATCH_R2_STWSP_N)
505 {
506 *ra = NIOS2_SP_REGNUM;
507 *rb = GET_IW_F1I5_B (insn);
508 *imm = GET_IW_F1I5_IMM5 (insn) << 2;
509 return 1;
510 }
511 else if (op->match == MATCH_R2_STWZ_N)
512 {
513 *ra = nios2_r2_reg3_mappings[GET_IW_T1X1I6_A3 (insn)];
514 *rb = 0;
515 *imm = GET_IW_T1X1I6_IMM6 (insn) << 2;
516 return 1;
517 }
d53c26c7
SL
518 return 0;
519}
520
521/* Match and disassemble a LDW-type instruction, with 2 register operands
522 and one immediate operand.
523 Returns true on success, and fills in the operand pointers. */
524
525static int
526nios2_match_ldw (uint32_t insn, const struct nios2_opcode *op,
527 unsigned long mach, int *ra, int *rb, int *imm)
528{
af60a1ef
SL
529 int is_r2 = (mach == bfd_mach_nios2r2);
530
531 if (!is_r2 && (op->match == MATCH_R1_LDW || op->match == MATCH_R1_LDWIO))
d53c26c7
SL
532 {
533 *ra = GET_IW_I_A (insn);
534 *rb = GET_IW_I_B (insn);
535 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
536 return 1;
537 }
af60a1ef
SL
538 else if (!is_r2)
539 return 0;
540 else if (op->match == MATCH_R2_LDW)
541 {
542 *ra = GET_IW_F2I16_A (insn);
543 *rb = GET_IW_F2I16_B (insn);
544 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
545 return 1;
546 }
547 else if (op->match == MATCH_R2_LDWIO)
548 {
549 *ra = GET_IW_F2X4I12_A (insn);
550 *rb = GET_IW_F2X4I12_B (insn);
551 *imm = (signed) (GET_IW_F2X4I12_IMM12 (insn) << 20) >> 20;
552 return 1;
553 }
554 else if (op->match == MATCH_R2_LDW_N)
555 {
556 *ra = nios2_r2_reg3_mappings[GET_IW_T2I4_A3 (insn)];
557 *rb = nios2_r2_reg3_mappings[GET_IW_T2I4_B3 (insn)];
558 *imm = GET_IW_T2I4_IMM4 (insn) << 2;
559 return 1;
560 }
561 else if (op->match == MATCH_R2_LDWSP_N)
562 {
563 *ra = NIOS2_SP_REGNUM;
564 *rb = GET_IW_F1I5_B (insn);
565 *imm = GET_IW_F1I5_IMM5 (insn) << 2;
566 return 1;
567 }
d53c26c7
SL
568 return 0;
569}
570
571/* Match and disassemble a RDCTL instruction, with 2 register operands.
572 Returns true on success, and fills in the operand pointers. */
573
574static int
575nios2_match_rdctl (uint32_t insn, const struct nios2_opcode *op,
576 unsigned long mach, int *ra, int *rc)
577{
af60a1ef
SL
578 int is_r2 = (mach == bfd_mach_nios2r2);
579
580 if (!is_r2 && (op->match == MATCH_R1_RDCTL))
d53c26c7
SL
581 {
582 *ra = GET_IW_R_IMM5 (insn);
583 *rc = GET_IW_R_C (insn);
584 return 1;
585 }
af60a1ef
SL
586 else if (!is_r2)
587 return 0;
588 else if (op->match == MATCH_R2_RDCTL)
589 {
590 *ra = GET_IW_F3X6L5_IMM5 (insn);
591 *rc = GET_IW_F3X6L5_C (insn);
592 return 1;
593 }
d53c26c7
SL
594 return 0;
595}
596
af60a1ef
SL
597/* Match and disassemble a PUSH.N or STWM instruction.
598 Returns true on success, and fills in the operand pointers. */
599
600static int
601nios2_match_stwm (uint32_t insn, const struct nios2_opcode *op,
602 unsigned long mach, unsigned int *reglist,
603 int *ra, int *imm, int *wb, int *id)
604{
605 int is_r2 = (mach == bfd_mach_nios2r2);
606
607 if (!is_r2)
608 return 0;
609 else if (op->match == MATCH_R2_PUSH_N)
610 {
611 *reglist = 1 << 31;
612 if (GET_IW_L5I4X1_FP (insn))
613 *reglist |= (1 << 28);
614 if (GET_IW_L5I4X1_CS (insn))
615 {
616 int val = GET_IW_L5I4X1_REGRANGE (insn);
617 *reglist |= nios2_r2_reg_range_mappings[val];
618 }
619 *ra = NIOS2_SP_REGNUM;
620 *imm = GET_IW_L5I4X1_IMM4 (insn) << 2;
621 *wb = 1;
622 *id = 0;
623 return 1;
624 }
625 else if (op->match == MATCH_R2_STWM)
626 {
627 unsigned int rawmask = GET_IW_F1X4L17_REGMASK (insn);
628 if (GET_IW_F1X4L17_RS (insn))
629 {
630 *reglist = ((rawmask << 14) & 0x00ffc000);
631 if (rawmask & (1 << 10))
632 *reglist |= (1 << 28);
633 if (rawmask & (1 << 11))
634 *reglist |= (1 << 31);
635 }
636 else
637 *reglist = rawmask << 2;
638 *ra = GET_IW_F1X4L17_A (insn);
639 *imm = 0;
640 *wb = GET_IW_F1X4L17_WB (insn);
641 *id = GET_IW_F1X4L17_ID (insn);
642 return 1;
643 }
644 return 0;
645}
646
647/* Match and disassemble a POP.N or LDWM instruction.
648 Returns true on success, and fills in the operand pointers. */
649
650static int
651nios2_match_ldwm (uint32_t insn, const struct nios2_opcode *op,
652 unsigned long mach, unsigned int *reglist,
653 int *ra, int *imm, int *wb, int *id, int *ret)
654{
655 int is_r2 = (mach == bfd_mach_nios2r2);
656
657 if (!is_r2)
658 return 0;
659 else if (op->match == MATCH_R2_POP_N)
660 {
661 *reglist = 1 << 31;
662 if (GET_IW_L5I4X1_FP (insn))
663 *reglist |= (1 << 28);
664 if (GET_IW_L5I4X1_CS (insn))
665 {
666 int val = GET_IW_L5I4X1_REGRANGE (insn);
667 *reglist |= nios2_r2_reg_range_mappings[val];
668 }
669 *ra = NIOS2_SP_REGNUM;
670 *imm = GET_IW_L5I4X1_IMM4 (insn) << 2;
671 *wb = 1;
672 *id = 1;
673 *ret = 1;
674 return 1;
675 }
676 else if (op->match == MATCH_R2_LDWM)
677 {
678 unsigned int rawmask = GET_IW_F1X4L17_REGMASK (insn);
679 if (GET_IW_F1X4L17_RS (insn))
680 {
681 *reglist = ((rawmask << 14) & 0x00ffc000);
682 if (rawmask & (1 << 10))
683 *reglist |= (1 << 28);
684 if (rawmask & (1 << 11))
685 *reglist |= (1 << 31);
686 }
687 else
688 *reglist = rawmask << 2;
689 *ra = GET_IW_F1X4L17_A (insn);
690 *imm = 0;
691 *wb = GET_IW_F1X4L17_WB (insn);
692 *id = GET_IW_F1X4L17_ID (insn);
693 *ret = GET_IW_F1X4L17_PC (insn);
694 return 1;
695 }
696 return 0;
697}
d53c26c7
SL
698
699/* Match and disassemble a branch instruction, with (potentially)
700 2 register operands and one immediate operand.
701 Returns true on success, and fills in the operand pointers. */
702
703enum branch_condition {
704 branch_none,
705 branch_eq,
706 branch_ne,
707 branch_ge,
708 branch_geu,
709 branch_lt,
710 branch_ltu
711};
712
713static int
714nios2_match_branch (uint32_t insn, const struct nios2_opcode *op,
715 unsigned long mach, int *ra, int *rb, int *imm,
716 enum branch_condition *cond)
717{
af60a1ef
SL
718 int is_r2 = (mach == bfd_mach_nios2r2);
719
720 if (!is_r2)
d53c26c7 721 {
af60a1ef
SL
722 switch (op->match)
723 {
724 case MATCH_R1_BR:
725 *cond = branch_none;
726 break;
727 case MATCH_R1_BEQ:
728 *cond = branch_eq;
729 break;
730 case MATCH_R1_BNE:
731 *cond = branch_ne;
732 break;
733 case MATCH_R1_BGE:
734 *cond = branch_ge;
735 break;
736 case MATCH_R1_BGEU:
737 *cond = branch_geu;
738 break;
739 case MATCH_R1_BLT:
740 *cond = branch_lt;
741 break;
742 case MATCH_R1_BLTU:
743 *cond = branch_ltu;
744 break;
745 default:
746 return 0;
747 }
748 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
749 *ra = GET_IW_I_A (insn);
750 *rb = GET_IW_I_B (insn);
751 return 1;
d53c26c7 752 }
af60a1ef
SL
753 else
754 {
755 switch (op->match)
756 {
757 case MATCH_R2_BR_N:
758 *cond = branch_none;
759 *ra = NIOS2_Z_REGNUM;
760 *rb = NIOS2_Z_REGNUM;
761 *imm = (signed) ((GET_IW_I10_IMM10 (insn) << 1) << 21) >> 21;
762 return 1;
763 case MATCH_R2_BEQZ_N:
764 *cond = branch_eq;
765 *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
766 *rb = NIOS2_Z_REGNUM;
767 *imm = (signed) ((GET_IW_T1I7_IMM7 (insn) << 1) << 24) >> 24;
768 return 1;
769 case MATCH_R2_BNEZ_N:
770 *cond = branch_ne;
771 *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
772 *rb = NIOS2_Z_REGNUM;
773 *imm = (signed) ((GET_IW_T1I7_IMM7 (insn) << 1) << 24) >> 24;
774 return 1;
775 case MATCH_R2_BR:
776 *cond = branch_none;
777 break;
778 case MATCH_R2_BEQ:
779 *cond = branch_eq;
780 break;
781 case MATCH_R2_BNE:
782 *cond = branch_ne;
783 break;
784 case MATCH_R2_BGE:
785 *cond = branch_ge;
786 break;
787 case MATCH_R2_BGEU:
788 *cond = branch_geu;
789 break;
790 case MATCH_R2_BLT:
791 *cond = branch_lt;
792 break;
793 case MATCH_R2_BLTU:
794 *cond = branch_ltu;
795 break;
796 default:
797 return 0;
798 }
799 *ra = GET_IW_F2I16_A (insn);
800 *rb = GET_IW_F2I16_B (insn);
801 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
802 return 1;
803 }
804 return 0;
d53c26c7
SL
805}
806
807/* Match and disassemble a direct jump instruction, with an
808 unsigned operand. Returns true on success, and fills in the operand
809 pointer. */
810
811static int
812nios2_match_jmpi (uint32_t insn, const struct nios2_opcode *op,
813 unsigned long mach, unsigned int *uimm)
814{
af60a1ef
SL
815 int is_r2 = (mach == bfd_mach_nios2r2);
816
817 if (!is_r2 && op->match == MATCH_R1_JMPI)
d53c26c7
SL
818 {
819 *uimm = GET_IW_J_IMM26 (insn) << 2;
820 return 1;
821 }
af60a1ef
SL
822 else if (!is_r2)
823 return 0;
824 else if (op->match == MATCH_R2_JMPI)
825 {
826 *uimm = GET_IW_L26_IMM26 (insn) << 2;
827 return 1;
828 }
d53c26c7
SL
829 return 0;
830}
831
832/* Match and disassemble a direct call instruction, with an
833 unsigned operand. Returns true on success, and fills in the operand
834 pointer. */
835
836static int
837nios2_match_calli (uint32_t insn, const struct nios2_opcode *op,
838 unsigned long mach, unsigned int *uimm)
839{
af60a1ef
SL
840 int is_r2 = (mach == bfd_mach_nios2r2);
841
842 if (!is_r2 && op->match == MATCH_R1_CALL)
d53c26c7
SL
843 {
844 *uimm = GET_IW_J_IMM26 (insn) << 2;
845 return 1;
846 }
af60a1ef
SL
847 else if (!is_r2)
848 return 0;
849 else if (op->match == MATCH_R2_CALL)
850 {
851 *uimm = GET_IW_L26_IMM26 (insn) << 2;
852 return 1;
853 }
d53c26c7
SL
854 return 0;
855}
856
857/* Match and disassemble an indirect jump instruction, with a
858 (possibly implicit) register operand. Returns true on success, and fills
859 in the operand pointer. */
860
861static int
862nios2_match_jmpr (uint32_t insn, const struct nios2_opcode *op,
863 unsigned long mach, int *ra)
864{
af60a1ef
SL
865 int is_r2 = (mach == bfd_mach_nios2r2);
866
867 if (!is_r2)
868 switch (op->match)
869 {
870 case MATCH_R1_JMP:
871 *ra = GET_IW_I_A (insn);
872 return 1;
873 case MATCH_R1_RET:
874 *ra = NIOS2_RA_REGNUM;
875 return 1;
876 case MATCH_R1_ERET:
877 *ra = NIOS2_EA_REGNUM;
878 return 1;
879 case MATCH_R1_BRET:
880 *ra = NIOS2_BA_REGNUM;
881 return 1;
882 default:
883 return 0;
884 }
885 else
886 switch (op->match)
887 {
888 case MATCH_R2_JMP:
889 *ra = GET_IW_F2I16_A (insn);
890 return 1;
891 case MATCH_R2_JMPR_N:
892 *ra = GET_IW_F1X1_A (insn);
893 return 1;
894 case MATCH_R2_RET:
895 case MATCH_R2_RET_N:
896 *ra = NIOS2_RA_REGNUM;
897 return 1;
898 case MATCH_R2_ERET:
899 *ra = NIOS2_EA_REGNUM;
900 return 1;
901 case MATCH_R2_BRET:
902 *ra = NIOS2_BA_REGNUM;
903 return 1;
904 default:
905 return 0;
906 }
907 return 0;
d53c26c7
SL
908}
909
910/* Match and disassemble an indirect call instruction, with a register
911 operand. Returns true on success, and fills in the operand pointer. */
912
913static int
914nios2_match_callr (uint32_t insn, const struct nios2_opcode *op,
915 unsigned long mach, int *ra)
916{
af60a1ef
SL
917 int is_r2 = (mach == bfd_mach_nios2r2);
918
919 if (!is_r2 && op->match == MATCH_R1_CALLR)
d53c26c7
SL
920 {
921 *ra = GET_IW_I_A (insn);
922 return 1;
923 }
af60a1ef
SL
924 else if (!is_r2)
925 return 0;
926 else if (op->match == MATCH_R2_CALLR)
927 {
928 *ra = GET_IW_F2I16_A (insn);
929 return 1;
930 }
931 else if (op->match == MATCH_R2_CALLR_N)
932 {
933 *ra = GET_IW_F1X1_A (insn);
934 return 1;
935 }
d53c26c7
SL
936 return 0;
937}
938
939/* Match and disassemble a break instruction, with an unsigned operand.
940 Returns true on success, and fills in the operand pointer. */
941
942static int
943nios2_match_break (uint32_t insn, const struct nios2_opcode *op,
944 unsigned long mach, unsigned int *uimm)
945{
af60a1ef
SL
946 int is_r2 = (mach == bfd_mach_nios2r2);
947
948 if (!is_r2 && op->match == MATCH_R1_BREAK)
d53c26c7
SL
949 {
950 *uimm = GET_IW_R_IMM5 (insn);
951 return 1;
952 }
af60a1ef
SL
953 else if (!is_r2)
954 return 0;
955 else if (op->match == MATCH_R2_BREAK)
956 {
957 *uimm = GET_IW_F3X6L5_IMM5 (insn);
958 return 1;
959 }
960 else if (op->match == MATCH_R2_BREAK_N)
961 {
962 *uimm = GET_IW_X2L5_IMM5 (insn);
963 return 1;
964 }
d53c26c7
SL
965 return 0;
966}
967
968/* Match and disassemble a trap instruction, with an unsigned operand.
969 Returns true on success, and fills in the operand pointer. */
970
971static int
972nios2_match_trap (uint32_t insn, const struct nios2_opcode *op,
973 unsigned long mach, unsigned int *uimm)
974{
af60a1ef
SL
975 int is_r2 = (mach == bfd_mach_nios2r2);
976
977 if (!is_r2 && op->match == MATCH_R1_TRAP)
d53c26c7
SL
978 {
979 *uimm = GET_IW_R_IMM5 (insn);
980 return 1;
981 }
af60a1ef
SL
982 else if (!is_r2)
983 return 0;
984 else if (op->match == MATCH_R2_TRAP)
985 {
986 *uimm = GET_IW_F3X6L5_IMM5 (insn);
987 return 1;
988 }
989 else if (op->match == MATCH_R2_TRAP_N)
990 {
991 *uimm = GET_IW_X2L5_IMM5 (insn);
992 return 1;
993 }
d53c26c7
SL
994 return 0;
995}
996
a1217d97
SL
997/* Helper function to identify when we're in a function epilogue;
998 that is, the part of the function from the point at which the
d53c26c7
SL
999 stack adjustments are made, to the return or sibcall.
1000 Note that we may have several stack adjustment instructions, and
1001 this function needs to test whether the stack teardown has already
1002 started before current_pc, not whether it has completed. */
a1217d97
SL
1003
1004static int
1005nios2_in_epilogue_p (struct gdbarch *gdbarch,
1006 CORE_ADDR current_pc,
1007 CORE_ADDR start_pc)
1008{
d53c26c7 1009 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
af60a1ef 1010 int is_r2 = (mach == bfd_mach_nios2r2);
7f1659b5
SL
1011 /* Maximum number of possibly-epilogue instructions to check.
1012 Note that this number should not be too large, else we can
1013 potentially end up iterating through unmapped memory. */
1014 int ninsns, max_insns = 5;
d53c26c7
SL
1015 unsigned int insn;
1016 const struct nios2_opcode *op = NULL;
1017 unsigned int uimm;
1018 int imm;
af60a1ef 1019 int wb, id, ret;
d53c26c7
SL
1020 int ra, rb, rc;
1021 enum branch_condition cond;
7f1659b5 1022 CORE_ADDR pc;
a1217d97
SL
1023
1024 /* There has to be a previous instruction in the function. */
7f1659b5
SL
1025 if (current_pc <= start_pc)
1026 return 0;
1027
af60a1ef
SL
1028 /* Find the previous instruction before current_pc. For R2, it might
1029 be either a 16-bit or 32-bit instruction; the only way to know for
1030 sure is to scan through from the beginning of the function,
1031 disassembling as we go. */
1032 if (is_r2)
1033 for (pc = start_pc; ; )
1034 {
1035 op = nios2_fetch_insn (gdbarch, pc, &insn);
1036 if (op == NULL)
1037 return 0;
1038 if (pc + op->size < current_pc)
1039 pc += op->size;
1040 else
1041 break;
1042 /* We can skip over insns to a forward branch target. Since
1043 the branch offset is relative to the next instruction,
1044 it's correct to do this after incrementing the pc above. */
1045 if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
1046 && imm > 0
1047 && pc + imm < current_pc)
1048 pc += imm;
1049 }
1050 /* Otherwise just go back to the previous 32-bit insn. */
1051 else
1052 pc = current_pc - NIOS2_OPCODE_SIZE;
d53c26c7 1053
7f1659b5
SL
1054 /* Beginning with the previous instruction we just located, check whether
1055 we are in a sequence of at least one stack adjustment instruction.
1056 Possible instructions here include:
d53c26c7
SL
1057 ADDI sp, sp, n
1058 ADD sp, sp, rn
af60a1ef
SL
1059 LDW sp, n(sp)
1060 SPINCI.N n
1061 LDWSP.N sp, n(sp)
1062 LDWM {reglist}, (sp)++, wb */
7f1659b5
SL
1063 for (ninsns = 0; ninsns < max_insns; ninsns++)
1064 {
1065 int ok = 0;
1066
1067 /* Fetch the insn at pc. */
1068 op = nios2_fetch_insn (gdbarch, pc, &insn);
d53c26c7
SL
1069 if (op == NULL)
1070 return 0;
7f1659b5 1071 pc += op->size;
d53c26c7
SL
1072
1073 /* Was it a stack adjustment? */
1074 if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
1075 ok = (rb == NIOS2_SP_REGNUM);
1076 else if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
1077 ok = (rc == NIOS2_SP_REGNUM);
1078 else if (nios2_match_ldw (insn, op, mach, &ra, &rb, &imm))
1079 ok = (rb == NIOS2_SP_REGNUM);
af60a1ef
SL
1080 else if (nios2_match_ldwm (insn, op, mach, &uimm, &ra,
1081 &imm, &wb, &ret, &id))
1082 ok = (ra == NIOS2_SP_REGNUM && wb && id);
d53c26c7 1083 if (!ok)
7f1659b5 1084 break;
a1217d97 1085 }
7f1659b5
SL
1086
1087 /* No stack adjustments found. */
1088 if (ninsns == 0)
1089 return 0;
1090
1091 /* We found more stack adjustments than we expect GCC to be generating.
1092 Since it looks like a stack unwind might be in progress tell GDB to
1093 treat it as such. */
1094 if (ninsns == max_insns)
1095 return 1;
1096
1097 /* The next instruction following the stack adjustments must be a
af60a1ef
SL
1098 return, jump, or unconditional branch, or a CDX pop.n or ldwm
1099 that does an implicit return. */
7f1659b5
SL
1100 if (nios2_match_jmpr (insn, op, mach, &ra)
1101 || nios2_match_jmpi (insn, op, mach, &uimm)
af60a1ef
SL
1102 || (nios2_match_ldwm (insn, op, mach, &uimm, &ra, &imm, &wb, &id, &ret)
1103 && ret)
7f1659b5
SL
1104 || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
1105 && cond == branch_none))
1106 return 1;
1107
a1217d97
SL
1108 return 0;
1109}
1110
c9cf6e20 1111/* Implement the stack_frame_destroyed_p gdbarch method. */
a1217d97
SL
1112
1113static int
c9cf6e20 1114nios2_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
a1217d97
SL
1115{
1116 CORE_ADDR func_addr;
1117
1118 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1119 return nios2_in_epilogue_p (gdbarch, pc, func_addr);
1120
1121 return 0;
1122}
1123
a1217d97
SL
1124/* Do prologue analysis, returning the PC of the first instruction
1125 after the function prologue. Assumes CACHE has already been
1126 initialized. THIS_FRAME can be null, in which case we are only
1127 interested in skipping the prologue. Otherwise CACHE is filled in
1128 from the frame information.
1129
d53c26c7
SL
1130 The prologue may consist of the following parts:
1131 1) Profiling instrumentation. For non-PIC code it looks like:
a1217d97
SL
1132 mov r8, ra
1133 call mcount
1134 mov ra, r8
1135
d53c26c7 1136 2) A stack adjustment and save of R4-R7 for varargs functions.
af60a1ef
SL
1137 For R2 CDX this is typically handled with a STWM, otherwise
1138 this is typically merged with item 3.
d53c26c7 1139
af60a1ef
SL
1140 3) A stack adjustment and save of the callee-saved registers.
1141 For R2 CDX these are typically handled with a PUSH.N or STWM,
1142 otherwise as an explicit SP decrement and individual register
d53c26c7
SL
1143 saves.
1144
1145 There may also be a stack switch here in an exception handler
1146 in place of a stack adjustment. It looks like:
a1217d97
SL
1147 movhi rx, %hiadj(newstack)
1148 addhi rx, rx, %lo(newstack)
1149 stw sp, constant(rx)
1150 mov sp, rx
1151
9aaf8e3a 1152 4) A frame pointer save, which can be either a MOV or ADDI.
d53c26c7 1153
9aaf8e3a
SL
1154 5) A further stack pointer adjustment. This is normally included
1155 adjustment in step 3 unless the total adjustment is too large
d53c26c7
SL
1156 to be done in one step.
1157
1158 7) A stack overflow check, which can take either of these forms:
a1217d97 1159 bgeu sp, rx, +8
9aaf8e3a 1160 trap 3
a1217d97
SL
1161 or
1162 bltu sp, rx, .Lstack_overflow
1163 ...
1164 .Lstack_overflow:
9aaf8e3a
SL
1165 trap 3
1166
1167 Older versions of GCC emitted "break 3" instead of "trap 3" here,
1168 so we check for both cases.
1169
1170 Older GCC versions emitted stack overflow checks after the SP
1171 adjustments in both steps 3 and 4. Starting with GCC 6, there is
1172 at most one overflow check, which is placed before the first
1173 stack adjustment for R2 CDX and after the first stack adjustment
1174 otherwise.
a1217d97 1175
d53c26c7
SL
1176 The prologue instructions may be combined or interleaved with other
1177 instructions.
a1217d97
SL
1178
1179 To cope with all this variability we decode all the instructions
d53c26c7
SL
1180 from the start of the prologue until we hit an instruction that
1181 cannot possibly be a prologue instruction, such as a branch, call,
1182 return, or epilogue instruction. The prologue is considered to end
1183 at the last instruction that can definitely be considered a
1184 prologue instruction. */
a1217d97
SL
1185
1186static CORE_ADDR
1187nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc,
1188 const CORE_ADDR current_pc,
1189 struct nios2_unwind_cache *cache,
1190 struct frame_info *this_frame)
1191{
d53c26c7 1192 /* Maximum number of possibly-prologue instructions to check.
a1217d97
SL
1193 Note that this number should not be too large, else we can
1194 potentially end up iterating through unmapped memory. */
d53c26c7 1195 int ninsns, max_insns = 50;
a1217d97 1196 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
d53c26c7 1197 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
a1217d97
SL
1198
1199 /* Does the frame set up the FP register? */
1200 int base_reg = 0;
1201
1202 struct reg_value *value = cache->reg_value;
1203 struct reg_value temp_value[NIOS2_NUM_REGS];
1204
1205 int i;
1206
1207 /* Save the starting PC so we can correct the pc after running
1208 through the prolog, using symbol info. */
1209 CORE_ADDR pc = start_pc;
1210
1211 /* Is this an exception handler? */
1212 int exception_handler = 0;
1213
1214 /* What was the original value of SP (or fake original value for
1215 functions which switch stacks? */
1216 CORE_ADDR frame_high;
1217
d53c26c7 1218 /* The last definitely-prologue instruction seen. */
a1217d97
SL
1219 CORE_ADDR prologue_end;
1220
1221 /* Is this the innermost function? */
1222 int innermost = (this_frame ? (frame_relative_level (this_frame) == 0) : 1);
1223
1224 if (nios2_debug)
1225 fprintf_unfiltered (gdb_stdlog,
1226 "{ nios2_analyze_prologue start=%s, current=%s ",
1227 paddress (gdbarch, start_pc),
1228 paddress (gdbarch, current_pc));
1229
1230 /* Set up the default values of the registers. */
1231 nios2_setup_default (cache);
a1217d97
SL
1232
1233 /* Find the prologue instructions. */
d53c26c7
SL
1234 prologue_end = start_pc;
1235 for (ninsns = 0; ninsns < max_insns; ninsns++)
a1217d97
SL
1236 {
1237 /* Present instruction. */
1238 uint32_t insn;
d53c26c7
SL
1239 const struct nios2_opcode *op;
1240 int ra, rb, rc, imm;
1241 unsigned int uimm;
1242 unsigned int reglist;
af60a1ef 1243 int wb, id, ret;
d53c26c7 1244 enum branch_condition cond;
a1217d97
SL
1245
1246 if (pc == current_pc)
1247 {
1248 /* When we reach the current PC we must save the current
1249 register state (for the backtrace) but keep analysing
1250 because there might be more to find out (eg. is this an
1251 exception handler). */
1252 memcpy (temp_value, value, sizeof (temp_value));
1253 value = temp_value;
1254 if (nios2_debug)
1255 fprintf_unfiltered (gdb_stdlog, "*");
1256 }
1257
d53c26c7
SL
1258 op = nios2_fetch_insn (gdbarch, pc, &insn);
1259
1260 /* Unknown opcode? Stop scanning. */
1261 if (op == NULL)
1262 break;
1263 pc += op->size;
a1217d97
SL
1264
1265 if (nios2_debug)
af60a1ef
SL
1266 {
1267 if (op->size == 2)
1268 fprintf_unfiltered (gdb_stdlog, "[%04X]", insn & 0xffff);
1269 else
1270 fprintf_unfiltered (gdb_stdlog, "[%08X]", insn);
1271 }
a1217d97
SL
1272
1273 /* The following instructions can appear in the prologue. */
1274
d53c26c7 1275 if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
a1217d97
SL
1276 {
1277 /* ADD rc, ra, rb (also used for MOV) */
a1217d97
SL
1278 if (rc == NIOS2_SP_REGNUM
1279 && rb == 0
1280 && value[ra].reg == cache->reg_saved[NIOS2_SP_REGNUM].basereg)
1281 {
1282 /* If the previous value of SP is available somewhere
1283 near the new stack pointer value then this is a
1284 stack switch. */
1285
1286 /* If any registers were saved on the stack before then
1287 we can't backtrace into them now. */
1288 for (i = 0 ; i < NIOS2_NUM_REGS ; i++)
1289 {
1290 if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
1291 cache->reg_saved[i].basereg = -1;
1292 if (value[i].reg == NIOS2_SP_REGNUM)
1293 value[i].reg = -1;
1294 }
1295
1296 /* Create a fake "high water mark" 4 bytes above where SP
1297 was stored and fake up the registers to be consistent
1298 with that. */
1299 value[NIOS2_SP_REGNUM].reg = NIOS2_SP_REGNUM;
1300 value[NIOS2_SP_REGNUM].offset
1301 = (value[ra].offset
1302 - cache->reg_saved[NIOS2_SP_REGNUM].addr
1303 - 4);
1304 cache->reg_saved[NIOS2_SP_REGNUM].basereg = NIOS2_SP_REGNUM;
1305 cache->reg_saved[NIOS2_SP_REGNUM].addr = -4;
1306 }
1307
aa489395
SL
1308 else if (rc == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
1309 /* This is setting SP from FP. This only happens in the
1310 function epilogue. */
1311 break;
1312
a1217d97
SL
1313 else if (rc != 0)
1314 {
1315 if (value[rb].reg == 0)
1316 value[rc].reg = value[ra].reg;
1317 else if (value[ra].reg == 0)
1318 value[rc].reg = value[rb].reg;
1319 else
1320 value[rc].reg = -1;
1321 value[rc].offset = value[ra].offset + value[rb].offset;
1322 }
a1217d97 1323
aa489395
SL
1324 /* The add/move is only considered a prologue instruction
1325 if the destination is SP or FP. */
1326 if (rc == NIOS2_SP_REGNUM || rc == NIOS2_FP_REGNUM)
1327 prologue_end = pc;
d53c26c7
SL
1328 }
1329
1330 else if (nios2_match_sub (insn, op, mach, &ra, &rb, &rc))
a1217d97
SL
1331 {
1332 /* SUB rc, ra, rb */
aa489395
SL
1333 if (rc == NIOS2_SP_REGNUM && rb == NIOS2_SP_REGNUM
1334 && value[rc].reg != 0)
1335 /* If we are decrementing the SP by a non-constant amount,
1336 this is alloca, not part of the prologue. */
1337 break;
1338 else if (rc != 0)
a1217d97
SL
1339 {
1340 if (value[rb].reg == 0)
1341 value[rc].reg = value[ra].reg;
1342 else
1343 value[rc].reg = -1;
1344 value[rc].offset = value[ra].offset - value[rb].offset;
1345 }
1346 }
1347
d53c26c7 1348 else if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
a1217d97 1349 {
d53c26c7 1350 /* ADDI rb, ra, imm */
a1217d97 1351
aa489395 1352 /* A positive stack adjustment has to be part of the epilogue. */
a1217d97 1353 if (rb == NIOS2_SP_REGNUM
aa489395
SL
1354 && (imm > 0 || value[ra].reg != NIOS2_SP_REGNUM))
1355 break;
1356
1357 /* Likewise restoring SP from FP. */
1358 else if (rb == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
a1217d97
SL
1359 break;
1360
1361 if (rb != 0)
1362 {
1363 value[rb].reg = value[ra].reg;
d53c26c7 1364 value[rb].offset = value[ra].offset + imm;
a1217d97
SL
1365 }
1366
aa489395
SL
1367 /* The add is only considered a prologue instruction
1368 if the destination is SP or FP. */
1369 if (rb == NIOS2_SP_REGNUM || rb == NIOS2_FP_REGNUM)
1370 prologue_end = pc;
a1217d97
SL
1371 }
1372
d53c26c7 1373 else if (nios2_match_orhi (insn, op, mach, &ra, &rb, &uimm))
a1217d97 1374 {
d53c26c7 1375 /* ORHI rb, ra, uimm (also used for MOVHI) */
a1217d97
SL
1376 if (rb != 0)
1377 {
1378 value[rb].reg = (value[ra].reg == 0) ? 0 : -1;
d53c26c7 1379 value[rb].offset = value[ra].offset | (uimm << 16);
a1217d97
SL
1380 }
1381 }
1382
d53c26c7 1383 else if (nios2_match_stw (insn, op, mach, &ra, &rb, &imm))
a1217d97 1384 {
d53c26c7 1385 /* STW rb, imm(ra) */
a1217d97 1386
d53c26c7 1387 /* Are we storing the original value of a register to the stack?
a1217d97
SL
1388 For exception handlers the value of EA-4 (return
1389 address from interrupts etc) is sometimes stored. */
1390 int orig = value[rb].reg;
1391 if (orig > 0
1392 && (value[rb].offset == 0
d53c26c7 1393 || (orig == NIOS2_EA_REGNUM && value[rb].offset == -4))
e1b5381f 1394 && value[ra].reg == NIOS2_SP_REGNUM)
d53c26c7
SL
1395 {
1396 if (pc < current_pc)
a1217d97 1397 {
d53c26c7
SL
1398 /* Save off callee saved registers. */
1399 cache->reg_saved[orig].basereg = value[ra].reg;
1400 cache->reg_saved[orig].addr = value[ra].offset + imm;
a1217d97 1401 }
d53c26c7
SL
1402
1403 prologue_end = pc;
1404
1405 if (orig == NIOS2_EA_REGNUM || orig == NIOS2_ESTATUS_REGNUM)
1406 exception_handler = 1;
a1217d97
SL
1407 }
1408 else
d53c26c7
SL
1409 /* Non-stack memory writes cannot appear in the prologue. */
1410 break;
a1217d97
SL
1411 }
1412
af60a1ef
SL
1413 else if (nios2_match_stwm (insn, op, mach,
1414 &reglist, &ra, &imm, &wb, &id))
1415 {
1416 /* PUSH.N {reglist}, adjust
1417 or
1418 STWM {reglist}, --(SP)[, writeback] */
1419 int i;
1420 int off = 0;
1421
1422 if (ra != NIOS2_SP_REGNUM || id != 0)
1423 /* This is a non-stack-push memory write and cannot be
1424 part of the prologue. */
1425 break;
1426
1427 for (i = 31; i >= 0; i--)
1428 if (reglist & (1 << i))
1429 {
1430 int orig = value[i].reg;
1431
1432 off += 4;
1433 if (orig > 0 && value[i].offset == 0 && pc < current_pc)
1434 {
1435 cache->reg_saved[orig].basereg
1436 = value[NIOS2_SP_REGNUM].reg;
1437 cache->reg_saved[orig].addr
1438 = value[NIOS2_SP_REGNUM].offset - off;
1439 }
1440 }
1441
1442 if (wb)
1443 value[NIOS2_SP_REGNUM].offset -= off;
1444 value[NIOS2_SP_REGNUM].offset -= imm;
1445
1446 prologue_end = pc;
1447 }
1448
d53c26c7 1449 else if (nios2_match_rdctl (insn, op, mach, &ra, &rc))
a1217d97 1450 {
d53c26c7
SL
1451 /* RDCTL rC, ctlN
1452 This can appear in exception handlers in combination with
1453 a subsequent save to the stack frame. */
a1217d97
SL
1454 if (rc != 0)
1455 {
d53c26c7 1456 value[rc].reg = NIOS2_STATUS_REGNUM + ra;
a1217d97
SL
1457 value[rc].offset = 0;
1458 }
a1217d97
SL
1459 }
1460
d53c26c7 1461 else if (nios2_match_calli (insn, op, mach, &uimm))
a1217d97 1462 {
d53c26c7
SL
1463 if (value[8].reg == NIOS2_RA_REGNUM
1464 && value[8].offset == 0
1465 && value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM
1466 && value[NIOS2_SP_REGNUM].offset == 0)
1467 {
1468 /* A CALL instruction. This is treated as a call to mcount
1469 if ra has been stored into r8 beforehand and if it's
1470 before the stack adjust.
1471 Note mcount corrupts r2-r3, r9-r15 & ra. */
1472 for (i = 2 ; i <= 3 ; i++)
1473 value[i].reg = -1;
1474 for (i = 9 ; i <= 15 ; i++)
1475 value[i].reg = -1;
1476 value[NIOS2_RA_REGNUM].reg = -1;
1477
1478 prologue_end = pc;
1479 }
a1217d97 1480
d53c26c7 1481 /* Other calls are not part of the prologue. */
a1217d97 1482 else
d53c26c7 1483 break;
a1217d97
SL
1484 }
1485
d53c26c7 1486 else if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
a1217d97 1487 {
d53c26c7
SL
1488 /* Branches not involving a stack overflow check aren't part of
1489 the prologue. */
1490 if (ra != NIOS2_SP_REGNUM)
1491 break;
1492 else if (cond == branch_geu)
a1217d97 1493 {
d53c26c7 1494 /* BGEU sp, rx, +8
9aaf8e3a 1495 TRAP 3 (or BREAK 3)
d53c26c7
SL
1496 This instruction sequence is used in stack checking;
1497 we can ignore it. */
1498 unsigned int next_insn;
1499 const struct nios2_opcode *next_op
1500 = nios2_fetch_insn (gdbarch, pc, &next_insn);
1501 if (next_op != NULL
9aaf8e3a
SL
1502 && (nios2_match_trap (next_insn, op, mach, &uimm)
1503 || nios2_match_break (next_insn, op, mach, &uimm)))
d53c26c7
SL
1504 pc += next_op->size;
1505 else
1506 break;
a1217d97 1507 }
d53c26c7
SL
1508 else if (cond == branch_ltu)
1509 {
1510 /* BLTU sp, rx, .Lstackoverflow
9aaf8e3a
SL
1511 If the location branched to holds a TRAP or BREAK
1512 instruction then this is also stack overflow detection. */
d53c26c7
SL
1513 unsigned int next_insn;
1514 const struct nios2_opcode *next_op
1515 = nios2_fetch_insn (gdbarch, pc + imm, &next_insn);
1516 if (next_op != NULL
9aaf8e3a
SL
1517 && (nios2_match_trap (next_insn, op, mach, &uimm)
1518 || nios2_match_break (next_insn, op, mach, &uimm)))
d53c26c7
SL
1519 ;
1520 else
1521 break;
1522 }
1523 else
1524 break;
a1217d97
SL
1525 }
1526
9aaf8e3a 1527 /* All other calls, jumps, returns, TRAPs, or BREAKs terminate
d53c26c7
SL
1528 the prologue. */
1529 else if (nios2_match_callr (insn, op, mach, &ra)
1530 || nios2_match_jmpr (insn, op, mach, &ra)
9aaf8e3a 1531 || nios2_match_jmpi (insn, op, mach, &uimm)
af60a1ef
SL
1532 || (nios2_match_ldwm (insn, op, mach, &reglist, &ra,
1533 &imm, &wb, &id, &ret)
1534 && ret)
9aaf8e3a
SL
1535 || nios2_match_trap (insn, op, mach, &uimm)
1536 || nios2_match_break (insn, op, mach, &uimm))
d53c26c7 1537 break;
a1217d97
SL
1538 }
1539
1540 /* If THIS_FRAME is NULL, we are being called from skip_prologue
1541 and are only interested in the PROLOGUE_END value, so just
1542 return that now and skip over the cache updates, which depend
1543 on having frame information. */
1544 if (this_frame == NULL)
1545 return prologue_end;
1546
1547 /* If we are in the function epilogue and have already popped
1548 registers off the stack in preparation for returning, then we
1549 want to go back to the original register values. */
1550 if (innermost && nios2_in_epilogue_p (gdbarch, current_pc, start_pc))
1551 nios2_setup_default (cache);
1552
1553 /* Exception handlers use a different return address register. */
1554 if (exception_handler)
1555 cache->return_regnum = NIOS2_EA_REGNUM;
1556
1557 if (nios2_debug)
1558 fprintf_unfiltered (gdb_stdlog, "\n-> retreg=%d, ", cache->return_regnum);
1559
1560 if (cache->reg_value[NIOS2_FP_REGNUM].reg == NIOS2_SP_REGNUM)
1561 /* If the FP now holds an offset from the CFA then this is a
1562 normal frame which uses the frame pointer. */
1563 base_reg = NIOS2_FP_REGNUM;
1564 else if (cache->reg_value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM)
1565 /* FP doesn't hold an offset from the CFA. If SP still holds an
1566 offset from the CFA then we might be in a function which omits
1567 the frame pointer, or we might be partway through the prologue.
1568 In both cases we can find the CFA using SP. */
1569 base_reg = NIOS2_SP_REGNUM;
1570 else
1571 {
1572 /* Somehow the stack pointer has been corrupted.
1573 We can't return. */
1574 if (nios2_debug)
1575 fprintf_unfiltered (gdb_stdlog, "<can't reach cfa> }\n");
1576 return 0;
1577 }
1578
1579 if (cache->reg_value[base_reg].offset == 0
1580 || cache->reg_saved[NIOS2_RA_REGNUM].basereg != NIOS2_SP_REGNUM
1581 || cache->reg_saved[cache->return_regnum].basereg != NIOS2_SP_REGNUM)
1582 {
1583 /* If the frame didn't adjust the stack, didn't save RA or
1584 didn't save EA in an exception handler then it must either
1585 be a leaf function (doesn't call any other functions) or it
1586 can't return. If it has called another function then it
1587 can't be a leaf, so set base == 0 to indicate that we can't
1588 backtrace past it. */
1589
1590 if (!innermost)
1591 {
1592 /* If it isn't the innermost function then it can't be a
1593 leaf, unless it was interrupted. Check whether RA for
1594 this frame is the same as PC. If so then it probably
1595 wasn't interrupted. */
1596 CORE_ADDR ra
1597 = get_frame_register_unsigned (this_frame, NIOS2_RA_REGNUM);
1598
1599 if (ra == current_pc)
1600 {
1601 if (nios2_debug)
1602 fprintf_unfiltered
1603 (gdb_stdlog,
1604 "<noreturn ADJUST %s, r31@r%d+?>, r%d@r%d+?> }\n",
1605 paddress (gdbarch, cache->reg_value[base_reg].offset),
1606 cache->reg_saved[NIOS2_RA_REGNUM].basereg,
1607 cache->return_regnum,
1608 cache->reg_saved[cache->return_regnum].basereg);
1609 return 0;
1610 }
1611 }
1612 }
1613
1614 /* Get the value of whichever register we are using for the
1615 base. */
1616 cache->base = get_frame_register_unsigned (this_frame, base_reg);
1617
1618 /* What was the value of SP at the start of this function (or just
1619 after the stack switch). */
1620 frame_high = cache->base - cache->reg_value[base_reg].offset;
1621
1622 /* Adjust all the saved registers such that they contain addresses
1623 instead of offsets. */
1624 for (i = 0; i < NIOS2_NUM_REGS; i++)
1625 if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
1626 {
1627 cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
1628 cache->reg_saved[i].addr += frame_high;
1629 }
1630
1631 for (i = 0; i < NIOS2_NUM_REGS; i++)
1632 if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
1633 {
1634 CORE_ADDR gp = get_frame_register_unsigned (this_frame,
1635 NIOS2_GP_REGNUM);
1636
1637 for ( ; i < NIOS2_NUM_REGS; i++)
1638 if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
1639 {
1640 cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
1641 cache->reg_saved[i].addr += gp;
1642 }
1643 }
1644
1645 /* Work out what the value of SP was on the first instruction of
1646 this function. If we didn't switch stacks then this can be
1647 trivially computed from the base address. */
1648 if (cache->reg_saved[NIOS2_SP_REGNUM].basereg == NIOS2_Z_REGNUM)
1649 cache->cfa
1650 = read_memory_unsigned_integer (cache->reg_saved[NIOS2_SP_REGNUM].addr,
1651 4, byte_order);
1652 else
1653 cache->cfa = frame_high;
1654
1655 /* Exception handlers restore ESTATUS into STATUS. */
1656 if (exception_handler)
1657 {
1658 cache->reg_saved[NIOS2_STATUS_REGNUM]
1659 = cache->reg_saved[NIOS2_ESTATUS_REGNUM];
1660 cache->reg_saved[NIOS2_ESTATUS_REGNUM].basereg = -1;
1661 }
1662
1663 if (nios2_debug)
1664 fprintf_unfiltered (gdb_stdlog, "cfa=%s }\n",
1665 paddress (gdbarch, cache->cfa));
1666
1667 return prologue_end;
1668}
1669
1670/* Implement the skip_prologue gdbarch hook. */
1671
1672static CORE_ADDR
1673nios2_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1674{
a1217d97
SL
1675 CORE_ADDR func_addr;
1676
1677 struct nios2_unwind_cache cache;
1678
1679 /* See if we can determine the end of the prologue via the symbol
1680 table. If so, then return either PC, or the PC after the
1681 prologue, whichever is greater. */
1682 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
1683 {
1684 CORE_ADDR post_prologue_pc
1685 = skip_prologue_using_sal (gdbarch, func_addr);
1686
1687 if (post_prologue_pc != 0)
325fac50 1688 return std::max (start_pc, post_prologue_pc);
a1217d97
SL
1689 }
1690
1691 /* Prologue analysis does the rest.... */
1692 nios2_init_cache (&cache, start_pc);
1693 return nios2_analyze_prologue (gdbarch, start_pc, start_pc, &cache, NULL);
1694}
1695
cd6c3b4f
YQ
1696/* Implement the breakpoint_kind_from_pc gdbarch method. */
1697
d19280ad
YQ
1698static int
1699nios2_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1700{
1701 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1702
1703 if (mach == bfd_mach_nios2r2)
1704 {
1705 unsigned int insn;
1706 const struct nios2_opcode *op
1707 = nios2_fetch_insn (gdbarch, *pcptr, &insn);
1708
1709 if (op && op->size == NIOS2_CDX_OPCODE_SIZE)
1710 return NIOS2_CDX_OPCODE_SIZE;
1711 else
1712 return NIOS2_OPCODE_SIZE;
1713 }
1714 else
1715 return NIOS2_OPCODE_SIZE;
1716}
b73c49b7 1717
cd6c3b4f
YQ
1718/* Implement the sw_breakpoint_from_kind gdbarch method. */
1719
d19280ad
YQ
1720static const gdb_byte *
1721nios2_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1722{
1723/* The Nios II ABI for Linux says: "Userspace programs should not use
b73c49b7
SL
1724 the break instruction and userspace debuggers should not insert
1725 one." and "Userspace breakpoints are accomplished using the trap
1726 instruction with immediate operand 31 (all ones)."
1727
1728 So, we use "trap 31" consistently as the breakpoint on bare-metal
1729 as well as Linux targets. */
a1217d97 1730
d19280ad
YQ
1731 /* R2 trap encoding:
1732 ((0x2d << 26) | (0x1f << 21) | (0x1d << 16) | (0x20 << 0))
1733 0xb7fd0020
1734 CDX trap.n encoding:
1735 ((0xd << 12) | (0x1f << 6) | (0x9 << 0))
1736 0xd7c9
1737 Note that code is always little-endian on R2. */
1738 *size = kind;
d53c26c7 1739
d19280ad 1740 if (kind == NIOS2_CDX_OPCODE_SIZE)
af60a1ef 1741 {
af60a1ef 1742 static const gdb_byte cdx_breakpoint_le[] = {0xc9, 0xd7};
af60a1ef 1743
d19280ad
YQ
1744 return cdx_breakpoint_le;
1745 }
1746 else
1747 {
1748 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1749
1750 if (mach == bfd_mach_nios2r2)
af60a1ef 1751 {
d19280ad
YQ
1752 static const gdb_byte r2_breakpoint_le[] = {0x20, 0x00, 0xfd, 0xb7};
1753
1754 return r2_breakpoint_le;
af60a1ef
SL
1755 }
1756 else
1757 {
d19280ad
YQ
1758 enum bfd_endian byte_order_for_code
1759 = gdbarch_byte_order_for_code (gdbarch);
1760 /* R1 trap encoding:
1761 ((0x1d << 17) | (0x2d << 11) | (0x1f << 6) | (0x3a << 0))
1762 0x003b6ffa */
1763 static const gdb_byte r1_breakpoint_le[] = {0xfa, 0x6f, 0x3b, 0x0};
1764 static const gdb_byte r1_breakpoint_be[] = {0x0, 0x3b, 0x6f, 0xfa};
1765
1766 if (byte_order_for_code == BFD_ENDIAN_BIG)
1767 return r1_breakpoint_be;
1768 else
1769 return r1_breakpoint_le;
af60a1ef
SL
1770 }
1771 }
a1217d97
SL
1772}
1773
a1217d97
SL
1774/* Implement the frame_align gdbarch method. */
1775
1776static CORE_ADDR
1777nios2_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1778{
1779 return align_down (addr, 4);
1780}
1781
1782
1783/* Implement the return_value gdbarch method. */
1784
1785static enum return_value_convention
1786nios2_return_value (struct gdbarch *gdbarch, struct value *function,
1787 struct type *type, struct regcache *regcache,
1788 gdb_byte *readbuf, const gdb_byte *writebuf)
1789{
1790 if (TYPE_LENGTH (type) > 8)
1791 return RETURN_VALUE_STRUCT_CONVENTION;
1792
1793 if (readbuf)
1794 nios2_extract_return_value (gdbarch, type, regcache, readbuf);
1795 if (writebuf)
1796 nios2_store_return_value (gdbarch, type, regcache, writebuf);
1797
1798 return RETURN_VALUE_REGISTER_CONVENTION;
1799}
1800
1801/* Implement the dummy_id gdbarch method. */
1802
1803static struct frame_id
1804nios2_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1805{
1806 return frame_id_build
1807 (get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM),
1808 get_frame_pc (this_frame));
1809}
1810
1811/* Implement the push_dummy_call gdbarch method. */
1812
1813static CORE_ADDR
1814nios2_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1815 struct regcache *regcache, CORE_ADDR bp_addr,
1816 int nargs, struct value **args, CORE_ADDR sp,
1817 int struct_return, CORE_ADDR struct_addr)
1818{
1819 int argreg;
a1217d97
SL
1820 int argnum;
1821 int len = 0;
1822 int stack_offset = 0;
a1217d97
SL
1823 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1824
1825 /* Set the return address register to point to the entry point of
1826 the program, where a breakpoint lies in wait. */
1827 regcache_cooked_write_signed (regcache, NIOS2_RA_REGNUM, bp_addr);
1828
1829 /* Now make space on the stack for the args. */
1830 for (argnum = 0; argnum < nargs; argnum++)
1831 len += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);
1832 sp -= len;
1833
1834 /* Initialize the register pointer. */
1835 argreg = NIOS2_FIRST_ARGREG;
1836
1837 /* The struct_return pointer occupies the first parameter-passing
1838 register. */
1839 if (struct_return)
1840 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
1841
1842 /* Now load as many as possible of the first arguments into
1843 registers, and push the rest onto the stack. Loop through args
1844 from first to last. */
1845 for (argnum = 0; argnum < nargs; argnum++)
1846 {
1847 const gdb_byte *val;
a1217d97
SL
1848 struct value *arg = args[argnum];
1849 struct type *arg_type = check_typedef (value_type (arg));
1850 int len = TYPE_LENGTH (arg_type);
a1217d97
SL
1851
1852 val = value_contents (arg);
1853
1854 /* Copy the argument to general registers or the stack in
1855 register-sized pieces. Large arguments are split between
1856 registers and stack. */
1857 while (len > 0)
1858 {
1859 int partial_len = (len < 4 ? len : 4);
1860
1861 if (argreg <= NIOS2_LAST_ARGREG)
1862 {
1863 /* The argument is being passed in a register. */
1864 CORE_ADDR regval = extract_unsigned_integer (val, partial_len,
1865 byte_order);
1866
1867 regcache_cooked_write_unsigned (regcache, argreg, regval);
1868 argreg++;
1869 }
1870 else
1871 {
1872 /* The argument is being passed on the stack. */
1873 CORE_ADDR addr = sp + stack_offset;
1874
1875 write_memory (addr, val, partial_len);
1876 stack_offset += align_up (partial_len, 4);
1877 }
1878
1879 len -= partial_len;
1880 val += partial_len;
1881 }
1882 }
1883
1884 regcache_cooked_write_signed (regcache, NIOS2_SP_REGNUM, sp);
1885
1886 /* Return adjusted stack pointer. */
1887 return sp;
1888}
1889
1890/* Implement the unwind_pc gdbarch method. */
1891
1892static CORE_ADDR
1893nios2_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1894{
1895 gdb_byte buf[4];
1896
1897 frame_unwind_register (next_frame, NIOS2_PC_REGNUM, buf);
1898 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1899}
1900
1901/* Implement the unwind_sp gdbarch method. */
1902
1903static CORE_ADDR
1904nios2_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
1905{
1906 return frame_unwind_register_unsigned (this_frame, NIOS2_SP_REGNUM);
1907}
1908
1909/* Use prologue analysis to fill in the register cache
1910 *THIS_PROLOGUE_CACHE for THIS_FRAME. This function initializes
1911 *THIS_PROLOGUE_CACHE first. */
1912
1913static struct nios2_unwind_cache *
1914nios2_frame_unwind_cache (struct frame_info *this_frame,
1915 void **this_prologue_cache)
1916{
1917 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1918 CORE_ADDR current_pc;
1919 struct nios2_unwind_cache *cache;
a1217d97
SL
1920
1921 if (*this_prologue_cache)
19ba03f4 1922 return (struct nios2_unwind_cache *) *this_prologue_cache;
a1217d97
SL
1923
1924 cache = FRAME_OBSTACK_ZALLOC (struct nios2_unwind_cache);
1925 *this_prologue_cache = cache;
1926
1927 /* Zero all fields. */
1928 nios2_init_cache (cache, get_frame_func (this_frame));
1929
1930 /* Prologue analysis does the rest... */
1931 current_pc = get_frame_pc (this_frame);
1932 if (cache->pc != 0)
1933 nios2_analyze_prologue (gdbarch, cache->pc, current_pc, cache, this_frame);
1934
1935 return cache;
1936}
1937
1938/* Implement the this_id function for the normal unwinder. */
1939
1940static void
1941nios2_frame_this_id (struct frame_info *this_frame, void **this_cache,
1942 struct frame_id *this_id)
1943{
1944 struct nios2_unwind_cache *cache =
1945 nios2_frame_unwind_cache (this_frame, this_cache);
1946
1947 /* This marks the outermost frame. */
1948 if (cache->base == 0)
1949 return;
1950
1951 *this_id = frame_id_build (cache->cfa, cache->pc);
1952}
1953
1954/* Implement the prev_register function for the normal unwinder. */
1955
1956static struct value *
1957nios2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1958 int regnum)
1959{
1960 struct nios2_unwind_cache *cache =
1961 nios2_frame_unwind_cache (this_frame, this_cache);
1962
1963 gdb_assert (regnum >= 0 && regnum < NIOS2_NUM_REGS);
1964
1965 /* The PC of the previous frame is stored in the RA register of
1966 the current frame. Frob regnum so that we pull the value from
1967 the correct place. */
1968 if (regnum == NIOS2_PC_REGNUM)
1969 regnum = cache->return_regnum;
1970
1971 if (regnum == NIOS2_SP_REGNUM && cache->cfa)
1972 return frame_unwind_got_constant (this_frame, regnum, cache->cfa);
1973
1974 /* If we've worked out where a register is stored then load it from
1975 there. */
1976 if (cache->reg_saved[regnum].basereg == NIOS2_Z_REGNUM)
1977 return frame_unwind_got_memory (this_frame, regnum,
1978 cache->reg_saved[regnum].addr);
1979
1980 return frame_unwind_got_register (this_frame, regnum, regnum);
1981}
1982
1983/* Implement the this_base, this_locals, and this_args hooks
1984 for the normal unwinder. */
1985
1986static CORE_ADDR
1987nios2_frame_base_address (struct frame_info *this_frame, void **this_cache)
1988{
1989 struct nios2_unwind_cache *info
1990 = nios2_frame_unwind_cache (this_frame, this_cache);
1991
1992 return info->base;
1993}
1994
1995/* Data structures for the normal prologue-analysis-based
1996 unwinder. */
1997
1998static const struct frame_unwind nios2_frame_unwind =
1999{
2000 NORMAL_FRAME,
2001 default_frame_unwind_stop_reason,
2002 nios2_frame_this_id,
2003 nios2_frame_prev_register,
2004 NULL,
2005 default_frame_sniffer
2006};
2007
2008static const struct frame_base nios2_frame_base =
2009{
2010 &nios2_frame_unwind,
2011 nios2_frame_base_address,
2012 nios2_frame_base_address,
2013 nios2_frame_base_address
2014};
2015
2016/* Fill in the register cache *THIS_CACHE for THIS_FRAME for use
2017 in the stub unwinder. */
2018
2019static struct trad_frame_cache *
2020nios2_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
2021{
2022 CORE_ADDR pc;
2023 CORE_ADDR start_addr;
2024 CORE_ADDR stack_addr;
2025 struct trad_frame_cache *this_trad_cache;
2026 struct gdbarch *gdbarch = get_frame_arch (this_frame);
a1217d97
SL
2027
2028 if (*this_cache != NULL)
19ba03f4 2029 return (struct trad_frame_cache *) *this_cache;
a1217d97
SL
2030 this_trad_cache = trad_frame_cache_zalloc (this_frame);
2031 *this_cache = this_trad_cache;
2032
2033 /* The return address is in the link register. */
2034 trad_frame_set_reg_realreg (this_trad_cache,
2035 gdbarch_pc_regnum (gdbarch),
2036 NIOS2_RA_REGNUM);
2037
2038 /* Frame ID, since it's a frameless / stackless function, no stack
2039 space is allocated and SP on entry is the current SP. */
2040 pc = get_frame_pc (this_frame);
2041 find_pc_partial_function (pc, NULL, &start_addr, NULL);
2042 stack_addr = get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM);
2043 trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr));
2044 /* Assume that the frame's base is the same as the stack pointer. */
2045 trad_frame_set_this_base (this_trad_cache, stack_addr);
2046
2047 return this_trad_cache;
2048}
2049
2050/* Implement the this_id function for the stub unwinder. */
2051
2052static void
2053nios2_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
2054 struct frame_id *this_id)
2055{
2056 struct trad_frame_cache *this_trad_cache
2057 = nios2_stub_frame_cache (this_frame, this_cache);
2058
2059 trad_frame_get_id (this_trad_cache, this_id);
2060}
2061
2062/* Implement the prev_register function for the stub unwinder. */
2063
2064static struct value *
2065nios2_stub_frame_prev_register (struct frame_info *this_frame,
2066 void **this_cache, int regnum)
2067{
2068 struct trad_frame_cache *this_trad_cache
2069 = nios2_stub_frame_cache (this_frame, this_cache);
2070
2071 return trad_frame_get_register (this_trad_cache, this_frame, regnum);
2072}
2073
2074/* Implement the sniffer function for the stub unwinder.
2075 This unwinder is used for cases where the normal
2076 prologue-analysis-based unwinder can't work,
2077 such as PLT stubs. */
2078
2079static int
2080nios2_stub_frame_sniffer (const struct frame_unwind *self,
2081 struct frame_info *this_frame, void **cache)
2082{
2083 gdb_byte dummy[4];
a1217d97
SL
2084 CORE_ADDR pc = get_frame_address_in_block (this_frame);
2085
2086 /* Use the stub unwinder for unreadable code. */
2087 if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2088 return 1;
2089
3e5d3a5a 2090 if (in_plt_section (pc))
a1217d97
SL
2091 return 1;
2092
2093 return 0;
2094}
2095
a1217d97
SL
2096/* Define the data structures for the stub unwinder. */
2097
2098static const struct frame_unwind nios2_stub_frame_unwind =
2099{
2100 NORMAL_FRAME,
2101 default_frame_unwind_stop_reason,
2102 nios2_stub_frame_this_id,
2103 nios2_stub_frame_prev_register,
2104 NULL,
2105 nios2_stub_frame_sniffer
2106};
2107
a1217d97 2108
a1217d97
SL
2109
2110/* Determine where to set a single step breakpoint while considering
2111 branch prediction. */
2112
2113static CORE_ADDR
3889f490 2114nios2_get_next_pc (struct regcache *regcache, CORE_ADDR pc)
a1217d97 2115{
ac7936df 2116 struct gdbarch *gdbarch = regcache->arch ();
a1217d97 2117 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
d53c26c7
SL
2118 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
2119 unsigned int insn;
2120 const struct nios2_opcode *op = nios2_fetch_insn (gdbarch, pc, &insn);
a1217d97
SL
2121 int ra;
2122 int rb;
d53c26c7
SL
2123 int imm;
2124 unsigned int uimm;
af60a1ef 2125 int wb, id, ret;
d53c26c7
SL
2126 enum branch_condition cond;
2127
2128 /* Do something stupid if we can't disassemble the insn at pc. */
2129 if (op == NULL)
2130 return pc + NIOS2_OPCODE_SIZE;
2131
2132 if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
a1217d97 2133 {
3889f490
YQ
2134 int ras = regcache_raw_get_signed (regcache, ra);
2135 int rbs = regcache_raw_get_signed (regcache, rb);
2136 unsigned int rau = regcache_raw_get_unsigned (regcache, ra);
2137 unsigned int rbu = regcache_raw_get_unsigned (regcache, rb);
a1217d97 2138
d53c26c7
SL
2139 pc += op->size;
2140 switch (cond)
a1217d97 2141 {
d53c26c7
SL
2142 case branch_none:
2143 pc += imm;
2144 break;
2145 case branch_eq:
2146 if (ras == rbs)
2147 pc += imm;
2148 break;
2149 case branch_ne:
2150 if (ras != rbs)
2151 pc += imm;
2152 break;
2153 case branch_ge:
2154 if (ras >= rbs)
2155 pc += imm;
2156 break;
2157 case branch_geu:
2158 if (rau >= rbu)
2159 pc += imm;
2160 break;
2161 case branch_lt:
2162 if (ras < rbs)
2163 pc += imm;
2164 break;
2165 case branch_ltu:
2166 if (rau < rbu)
2167 pc += imm;
a1217d97 2168 break;
a1217d97
SL
2169 default:
2170 break;
2171 }
a1217d97 2172 }
d53c26c7
SL
2173
2174 else if (nios2_match_jmpi (insn, op, mach, &uimm)
2175 || nios2_match_calli (insn, op, mach, &uimm))
2176 pc = (pc & 0xf0000000) | uimm;
2177
2178 else if (nios2_match_jmpr (insn, op, mach, &ra)
2179 || nios2_match_callr (insn, op, mach, &ra))
3889f490 2180 pc = regcache_raw_get_unsigned (regcache, ra);
d53c26c7 2181
af60a1ef
SL
2182 else if (nios2_match_ldwm (insn, op, mach, &uimm, &ra, &imm, &wb, &id, &ret)
2183 && ret)
2184 {
2185 /* If ra is in the reglist, we have to use the value saved in the
2186 stack frame rather than the current value. */
2187 if (uimm & (1 << NIOS2_RA_REGNUM))
3889f490 2188 pc = nios2_unwind_pc (gdbarch, get_current_frame ());
af60a1ef 2189 else
3889f490 2190 pc = regcache_raw_get_unsigned (regcache, NIOS2_RA_REGNUM);
af60a1ef
SL
2191 }
2192
2193 else if (nios2_match_trap (insn, op, mach, &uimm) && uimm == 0)
d53c26c7
SL
2194 {
2195 if (tdep->syscall_next_pc != NULL)
3889f490 2196 return tdep->syscall_next_pc (get_current_frame (), op);
d53c26c7
SL
2197 }
2198
2199 else
2200 pc += op->size;
2201
a1217d97
SL
2202 return pc;
2203}
2204
2205/* Implement the software_single_step gdbarch method. */
2206
a0ff9e1a 2207static std::vector<CORE_ADDR>
f5ea389a 2208nios2_software_single_step (struct regcache *regcache)
a1217d97 2209{
3889f490 2210 CORE_ADDR next_pc = nios2_get_next_pc (regcache, regcache_read_pc (regcache));
a1217d97 2211
a0ff9e1a 2212 return {next_pc};
a1217d97
SL
2213}
2214
2215/* Implement the get_longjump_target gdbarch method. */
2216
2217static int
2218nios2_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
2219{
2220 struct gdbarch *gdbarch = get_frame_arch (frame);
2221 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2222 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2223 CORE_ADDR jb_addr = get_frame_register_unsigned (frame, NIOS2_R4_REGNUM);
2224 gdb_byte buf[4];
2225
2226 if (target_read_memory (jb_addr + (tdep->jb_pc * 4), buf, 4))
2227 return 0;
2228
2229 *pc = extract_unsigned_integer (buf, 4, byte_order);
2230 return 1;
2231}
2232
f70e088f
SL
2233/* Implement the type_align gdbarch function. */
2234
2235static ULONGEST
2236nios2_type_align (struct gdbarch *gdbarch, struct type *type)
2237{
2238 type = check_typedef (type);
2239 return std::min<ULONGEST> (4, TYPE_LENGTH (type));
2240}
2241
a1217d97
SL
2242/* Initialize the Nios II gdbarch. */
2243
2244static struct gdbarch *
2245nios2_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2246{
2247 struct gdbarch *gdbarch;
2248 struct gdbarch_tdep *tdep;
870f88f7 2249 int i;
a1217d97
SL
2250 struct tdesc_arch_data *tdesc_data = NULL;
2251 const struct target_desc *tdesc = info.target_desc;
2252
2253 if (!tdesc_has_registers (tdesc))
2254 /* Pick a default target description. */
2255 tdesc = tdesc_nios2;
2256
2257 /* Check any target description for validity. */
2258 if (tdesc_has_registers (tdesc))
2259 {
2260 const struct tdesc_feature *feature;
2261 int valid_p;
2262
2263 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nios2.cpu");
2264 if (feature == NULL)
2265 return NULL;
2266
2267 tdesc_data = tdesc_data_alloc ();
2268
2269 valid_p = 1;
2270
2271 for (i = 0; i < NIOS2_NUM_REGS; i++)
2272 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
2273 nios2_reg_names[i]);
2274
2275 if (!valid_p)
2276 {
2277 tdesc_data_cleanup (tdesc_data);
2278 return NULL;
2279 }
2280 }
2281
2282 /* Find a candidate among the list of pre-declared architectures. */
2283 arches = gdbarch_list_lookup_by_info (arches, &info);
2284 if (arches != NULL)
2285 return arches->gdbarch;
2286
2287 /* None found, create a new architecture from the information
2288 provided. */
8d749320 2289 tdep = XCNEW (struct gdbarch_tdep);
a1217d97
SL
2290 gdbarch = gdbarch_alloc (&info, tdep);
2291
2292 /* longjmp support not enabled by default. */
2293 tdep->jb_pc = -1;
2294
2295 /* Data type sizes. */
2296 set_gdbarch_ptr_bit (gdbarch, 32);
2297 set_gdbarch_addr_bit (gdbarch, 32);
2298 set_gdbarch_short_bit (gdbarch, 16);
2299 set_gdbarch_int_bit (gdbarch, 32);
2300 set_gdbarch_long_bit (gdbarch, 32);
2301 set_gdbarch_long_long_bit (gdbarch, 64);
2302 set_gdbarch_float_bit (gdbarch, 32);
2303 set_gdbarch_double_bit (gdbarch, 64);
2304
f70e088f
SL
2305 set_gdbarch_type_align (gdbarch, nios2_type_align);
2306
a1217d97
SL
2307 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2308 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2309
2310 /* The register set. */
2311 set_gdbarch_num_regs (gdbarch, NIOS2_NUM_REGS);
2312 set_gdbarch_sp_regnum (gdbarch, NIOS2_SP_REGNUM);
2313 set_gdbarch_pc_regnum (gdbarch, NIOS2_PC_REGNUM); /* Pseudo register PC */
2314
2315 set_gdbarch_register_name (gdbarch, nios2_register_name);
2316 set_gdbarch_register_type (gdbarch, nios2_register_type);
2317
2318 /* Provide register mappings for stabs and dwarf2. */
2319 set_gdbarch_stab_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
2320 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
2321
2322 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2323
2324 /* Call dummy code. */
2325 set_gdbarch_frame_align (gdbarch, nios2_frame_align);
2326
2327 set_gdbarch_return_value (gdbarch, nios2_return_value);
2328
2329 set_gdbarch_skip_prologue (gdbarch, nios2_skip_prologue);
c9cf6e20 2330 set_gdbarch_stack_frame_destroyed_p (gdbarch, nios2_stack_frame_destroyed_p);
04180708
YQ
2331 set_gdbarch_breakpoint_kind_from_pc (gdbarch, nios2_breakpoint_kind_from_pc);
2332 set_gdbarch_sw_breakpoint_from_kind (gdbarch, nios2_sw_breakpoint_from_kind);
a1217d97
SL
2333
2334 set_gdbarch_dummy_id (gdbarch, nios2_dummy_id);
2335 set_gdbarch_unwind_pc (gdbarch, nios2_unwind_pc);
2336 set_gdbarch_unwind_sp (gdbarch, nios2_unwind_sp);
2337
2338 /* The dwarf2 unwinder will normally produce the best results if
2339 the debug information is available, so register it first. */
2340 dwarf2_append_unwinders (gdbarch);
2341 frame_unwind_append_unwinder (gdbarch, &nios2_stub_frame_unwind);
2342 frame_unwind_append_unwinder (gdbarch, &nios2_frame_unwind);
2343
2344 /* Single stepping. */
2345 set_gdbarch_software_single_step (gdbarch, nios2_software_single_step);
2346
2347 /* Hook in ABI-specific overrides, if they have been registered. */
2348 gdbarch_init_osabi (info, gdbarch);
2349
2350 if (tdep->jb_pc >= 0)
2351 set_gdbarch_get_longjmp_target (gdbarch, nios2_get_longjmp_target);
2352
2353 frame_base_set_default (gdbarch, &nios2_frame_base);
2354
a1217d97
SL
2355 /* Enable inferior call support. */
2356 set_gdbarch_push_dummy_call (gdbarch, nios2_push_dummy_call);
2357
2358 if (tdesc_data)
2359 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
2360
2361 return gdbarch;
2362}
2363
a1217d97
SL
2364void
2365_initialize_nios2_tdep (void)
2366{
2367 gdbarch_register (bfd_arch_nios2, nios2_gdbarch_init, NULL);
2368 initialize_tdesc_nios2 ();
2369
2370 /* Allow debugging this file's internals. */
2371 add_setshow_boolean_cmd ("nios2", class_maintenance, &nios2_debug,
2372 _("Set Nios II debugging."),
2373 _("Show Nios II debugging."),
2374 _("When on, Nios II specific debugging is enabled."),
2375 NULL,
2376 NULL,
2377 &setdebuglist, &showdebuglist);
2378}
This page took 0.642041 seconds and 4 git commands to generate.