gdb: Don't skip prologue for explicit line breakpoints in assembler
[deliverable/binutils-gdb.git] / gdb / riscv-tdep.c
CommitLineData
dbbb1059
AB
1/* Target-dependent code for the RISC-V architecture, for GDB.
2
42a4f53d 3 Copyright (C) 2018-2019 Free Software Foundation, Inc.
dbbb1059 4
dbbb1059
AB
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "frame.h"
22#include "inferior.h"
23#include "symtab.h"
24#include "value.h"
25#include "gdbcmd.h"
26#include "language.h"
27#include "gdbcore.h"
28#include "symfile.h"
29#include "objfiles.h"
30#include "gdbtypes.h"
31#include "target.h"
32#include "arch-utils.h"
33#include "regcache.h"
34#include "osabi.h"
35#include "riscv-tdep.h"
36#include "block.h"
37#include "reggroups.h"
38#include "opcode/riscv.h"
39#include "elf/riscv.h"
40#include "elf-bfd.h"
41#include "symcat.h"
42#include "dis-asm.h"
43#include "frame-unwind.h"
44#include "frame-base.h"
45#include "trad-frame.h"
46#include "infcall.h"
47#include "floatformat.h"
48#include "remote.h"
49#include "target-descriptions.h"
50#include "dwarf2-frame.h"
51#include "user-regs.h"
52#include "valprint.h"
0747795c 53#include "common/common-defs.h"
dbbb1059
AB
54#include "opcode/riscv-opc.h"
55#include "cli/cli-decode.h"
76727919 56#include "observable.h"
78a3b0fa 57#include "prologue-value.h"
b5ffee31 58#include "arch/riscv.h"
dbbb1059
AB
59
60/* The stack must be 16-byte aligned. */
61#define SP_ALIGNMENT 16
62
ef2de9e7
JW
63/* The biggest alignment that the target supports. */
64#define BIGGEST_ALIGNMENT 16
65
dbbb1059
AB
66/* Define a series of is_XXX_insn functions to check if the value INSN
67 is an instance of instruction XXX. */
68#define DECLARE_INSN(INSN_NAME, INSN_MATCH, INSN_MASK) \
69static inline bool is_ ## INSN_NAME ## _insn (long insn) \
70{ \
71 return (insn & INSN_MASK) == INSN_MATCH; \
72}
73#include "opcode/riscv-opc.h"
74#undef DECLARE_INSN
75
78a3b0fa
AB
76/* Cached information about a frame. */
77
78struct riscv_unwind_cache
79{
80 /* The register from which we can calculate the frame base. This is
81 usually $sp or $fp. */
82 int frame_base_reg;
83
84 /* The offset from the current value in register FRAME_BASE_REG to the
85 actual frame base address. */
86 int frame_base_offset;
87
88 /* Information about previous register values. */
89 struct trad_frame_saved_reg *regs;
90
91 /* The id for this frame. */
92 struct frame_id this_id;
93
94 /* The base (stack) address for this frame. This is the stack pointer
95 value on entry to this frame before any adjustments are made. */
96 CORE_ADDR frame_base;
97};
98
b5ffee31 99/* RISC-V specific register group for CSRs. */
dbbb1059 100
b5ffee31 101static reggroup *csr_reggroup = NULL;
dbbb1059 102
b5ffee31
AB
103/* A set of registers that we expect to find in a tdesc_feature. These
104 are use in RISCV_GDBARCH_INIT when processing the target description. */
dbbb1059 105
b5ffee31 106struct riscv_register_feature
dbbb1059 107{
b5ffee31
AB
108 /* Information for a single register. */
109 struct register_info
110 {
111 /* The GDB register number for this register. */
112 int regnum;
113
114 /* List of names for this register. The first name in this list is the
115 preferred name, the name GDB should use when describing this
116 register. */
117 std::vector <const char *> names;
118
119 /* When true this register is required in this feature set. */
120 bool required_p;
121 };
122
123 /* The name for this feature. This is the name used to find this feature
124 within the target description. */
dbbb1059
AB
125 const char *name;
126
b5ffee31
AB
127 /* List of all the registers that we expect that we might find in this
128 register set. */
129 std::vector <struct register_info> registers;
130};
131
132/* The general x-registers feature set. */
133
134static const struct riscv_register_feature riscv_xreg_feature =
135{
136 "org.gnu.gdb.riscv.cpu",
137 {
138 { RISCV_ZERO_REGNUM + 0, { "zero", "x0" }, true },
139 { RISCV_ZERO_REGNUM + 1, { "ra", "x1" }, true },
140 { RISCV_ZERO_REGNUM + 2, { "sp", "x2" }, true },
141 { RISCV_ZERO_REGNUM + 3, { "gp", "x3" }, true },
142 { RISCV_ZERO_REGNUM + 4, { "tp", "x4" }, true },
143 { RISCV_ZERO_REGNUM + 5, { "t0", "x5" }, true },
144 { RISCV_ZERO_REGNUM + 6, { "t1", "x6" }, true },
145 { RISCV_ZERO_REGNUM + 7, { "t2", "x7" }, true },
146 { RISCV_ZERO_REGNUM + 8, { "fp", "x8", "s0" }, true },
147 { RISCV_ZERO_REGNUM + 9, { "s1", "x9" }, true },
148 { RISCV_ZERO_REGNUM + 10, { "a0", "x10" }, true },
149 { RISCV_ZERO_REGNUM + 11, { "a1", "x11" }, true },
150 { RISCV_ZERO_REGNUM + 12, { "a2", "x12" }, true },
151 { RISCV_ZERO_REGNUM + 13, { "a3", "x13" }, true },
152 { RISCV_ZERO_REGNUM + 14, { "a4", "x14" }, true },
153 { RISCV_ZERO_REGNUM + 15, { "a5", "x15" }, true },
154 { RISCV_ZERO_REGNUM + 16, { "a6", "x16" }, true },
155 { RISCV_ZERO_REGNUM + 17, { "a7", "x17" }, true },
156 { RISCV_ZERO_REGNUM + 18, { "s2", "x18" }, true },
157 { RISCV_ZERO_REGNUM + 19, { "s3", "x19" }, true },
158 { RISCV_ZERO_REGNUM + 20, { "s4", "x20" }, true },
159 { RISCV_ZERO_REGNUM + 21, { "s5", "x21" }, true },
160 { RISCV_ZERO_REGNUM + 22, { "s6", "x22" }, true },
161 { RISCV_ZERO_REGNUM + 23, { "s7", "x23" }, true },
162 { RISCV_ZERO_REGNUM + 24, { "s8", "x24" }, true },
163 { RISCV_ZERO_REGNUM + 25, { "s9", "x25" }, true },
164 { RISCV_ZERO_REGNUM + 26, { "s10", "x26" }, true },
165 { RISCV_ZERO_REGNUM + 27, { "s11", "x27" }, true },
166 { RISCV_ZERO_REGNUM + 28, { "t3", "x28" }, true },
167 { RISCV_ZERO_REGNUM + 29, { "t4", "x29" }, true },
168 { RISCV_ZERO_REGNUM + 30, { "t5", "x30" }, true },
169 { RISCV_ZERO_REGNUM + 31, { "t6", "x31" }, true },
170 { RISCV_ZERO_REGNUM + 32, { "pc" }, true }
171 }
172};
173
174/* The f-registers feature set. */
175
176static const struct riscv_register_feature riscv_freg_feature =
177{
178 "org.gnu.gdb.riscv.fpu",
179 {
180 { RISCV_FIRST_FP_REGNUM + 0, { "ft0", "f0" }, true },
181 { RISCV_FIRST_FP_REGNUM + 1, { "ft1", "f1" }, true },
182 { RISCV_FIRST_FP_REGNUM + 2, { "ft2", "f2" }, true },
183 { RISCV_FIRST_FP_REGNUM + 3, { "ft3", "f3" }, true },
184 { RISCV_FIRST_FP_REGNUM + 4, { "ft4", "f4" }, true },
185 { RISCV_FIRST_FP_REGNUM + 5, { "ft5", "f5" }, true },
186 { RISCV_FIRST_FP_REGNUM + 6, { "ft6", "f6" }, true },
187 { RISCV_FIRST_FP_REGNUM + 7, { "ft7", "f7" }, true },
592d8c0a 188 { RISCV_FIRST_FP_REGNUM + 8, { "fs0", "f8" }, true },
b5ffee31
AB
189 { RISCV_FIRST_FP_REGNUM + 9, { "fs1", "f9" }, true },
190 { RISCV_FIRST_FP_REGNUM + 10, { "fa0", "f10" }, true },
191 { RISCV_FIRST_FP_REGNUM + 11, { "fa1", "f11" }, true },
192 { RISCV_FIRST_FP_REGNUM + 12, { "fa2", "f12" }, true },
193 { RISCV_FIRST_FP_REGNUM + 13, { "fa3", "f13" }, true },
194 { RISCV_FIRST_FP_REGNUM + 14, { "fa4", "f14" }, true },
195 { RISCV_FIRST_FP_REGNUM + 15, { "fa5", "f15" }, true },
196 { RISCV_FIRST_FP_REGNUM + 16, { "fa6", "f16" }, true },
197 { RISCV_FIRST_FP_REGNUM + 17, { "fa7", "f17" }, true },
198 { RISCV_FIRST_FP_REGNUM + 18, { "fs2", "f18" }, true },
199 { RISCV_FIRST_FP_REGNUM + 19, { "fs3", "f19" }, true },
200 { RISCV_FIRST_FP_REGNUM + 20, { "fs4", "f20" }, true },
201 { RISCV_FIRST_FP_REGNUM + 21, { "fs5", "f21" }, true },
202 { RISCV_FIRST_FP_REGNUM + 22, { "fs6", "f22" }, true },
203 { RISCV_FIRST_FP_REGNUM + 23, { "fs7", "f23" }, true },
204 { RISCV_FIRST_FP_REGNUM + 24, { "fs8", "f24" }, true },
205 { RISCV_FIRST_FP_REGNUM + 25, { "fs9", "f25" }, true },
206 { RISCV_FIRST_FP_REGNUM + 26, { "fs10", "f26" }, true },
207 { RISCV_FIRST_FP_REGNUM + 27, { "fs11", "f27" }, true },
208 { RISCV_FIRST_FP_REGNUM + 28, { "ft8", "f28" }, true },
209 { RISCV_FIRST_FP_REGNUM + 29, { "ft9", "f29" }, true },
210 { RISCV_FIRST_FP_REGNUM + 30, { "ft10", "f30" }, true },
211 { RISCV_FIRST_FP_REGNUM + 31, { "ft11", "f31" }, true },
212
213 { RISCV_CSR_FFLAGS_REGNUM, { "fflags" }, true },
214 { RISCV_CSR_FRM_REGNUM, { "frm" }, true },
215 { RISCV_CSR_FCSR_REGNUM, { "fcsr" }, true },
216
217 }
218};
219
220/* Set of virtual registers. These are not physical registers on the
221 hardware, but might be available from the target. These are not pseudo
222 registers, reading these really does result in a register read from the
223 target, it is just that there might not be a physical register backing
224 the result. */
225
226static const struct riscv_register_feature riscv_virtual_feature =
227{
228 "org.gnu.gdb.riscv.virtual",
229 {
230 { RISCV_PRIV_REGNUM, { "priv" }, false }
231 }
dbbb1059
AB
232};
233
b5ffee31
AB
234/* Feature set for CSRs. This set is NOT constant as the register names
235 list for each register is not complete. The aliases are computed
236 during RISCV_CREATE_CSR_ALIASES. */
237
238static struct riscv_register_feature riscv_csr_feature =
239{
240 "org.gnu.gdb.riscv.csr",
241 {
242#define DECLARE_CSR(NAME,VALUE) \
243 { RISCV_ ## VALUE ## _REGNUM, { # NAME }, false },
244#include "opcode/riscv-opc.h"
245#undef DECLARE_CSR
246 }
dbbb1059
AB
247};
248
b5ffee31
AB
249/* Complete RISCV_CSR_FEATURE, building the CSR alias names and adding them
250 to the name list for each register. */
251
252static void
253riscv_create_csr_aliases ()
254{
255 for (auto &reg : riscv_csr_feature.registers)
256 {
257 int csr_num = reg.regnum - RISCV_FIRST_CSR_REGNUM;
258 const char *alias = xstrprintf ("csr%d", csr_num);
259 reg.names.push_back (alias);
260 }
261}
262
dbbb1059
AB
263/* Controls whether we place compressed breakpoints or not. When in auto
264 mode GDB tries to determine if the target supports compressed
265 breakpoints, and uses them if it does. */
266
267static enum auto_boolean use_compressed_breakpoints;
268
269/* The show callback for 'show riscv use-compressed-breakpoints'. */
270
271static void
272show_use_compressed_breakpoints (struct ui_file *file, int from_tty,
273 struct cmd_list_element *c,
274 const char *value)
275{
dbbb1059
AB
276 fprintf_filtered (file,
277 _("Debugger's use of compressed breakpoints is set "
f37bc8b1 278 "to %s.\n"), value);
dbbb1059
AB
279}
280
281/* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
282
283static struct cmd_list_element *setriscvcmdlist = NULL;
284static struct cmd_list_element *showriscvcmdlist = NULL;
285
286/* The show callback for the 'show riscv' prefix command. */
287
288static void
289show_riscv_command (const char *args, int from_tty)
290{
291 help_list (showriscvcmdlist, "show riscv ", all_commands, gdb_stdout);
292}
293
294/* The set callback for the 'set riscv' prefix command. */
295
296static void
297set_riscv_command (const char *args, int from_tty)
298{
299 printf_unfiltered
300 (_("\"set riscv\" must be followed by an appropriate subcommand.\n"));
301 help_list (setriscvcmdlist, "set riscv ", all_commands, gdb_stdout);
302}
303
304/* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
305
306static struct cmd_list_element *setdebugriscvcmdlist = NULL;
307static struct cmd_list_element *showdebugriscvcmdlist = NULL;
308
309/* The show callback for the 'show debug riscv' prefix command. */
310
311static void
312show_debug_riscv_command (const char *args, int from_tty)
313{
314 help_list (showdebugriscvcmdlist, "show debug riscv ", all_commands, gdb_stdout);
315}
316
317/* The set callback for the 'set debug riscv' prefix command. */
318
319static void
320set_debug_riscv_command (const char *args, int from_tty)
321{
322 printf_unfiltered
323 (_("\"set debug riscv\" must be followed by an appropriate subcommand.\n"));
324 help_list (setdebugriscvcmdlist, "set debug riscv ", all_commands, gdb_stdout);
325}
326
327/* The show callback for all 'show debug riscv VARNAME' variables. */
328
329static void
330show_riscv_debug_variable (struct ui_file *file, int from_tty,
331 struct cmd_list_element *c,
332 const char *value)
333{
334 fprintf_filtered (file,
335 _("RiscV debug variable `%s' is set to: %s\n"),
336 c->name, value);
337}
338
f37bc8b1
JB
339/* When this is set to non-zero debugging information about breakpoint
340 kinds will be printed. */
341
342static unsigned int riscv_debug_breakpoints = 0;
343
dbbb1059
AB
344/* When this is set to non-zero debugging information about inferior calls
345 will be printed. */
346
347static unsigned int riscv_debug_infcall = 0;
348
78a3b0fa
AB
349/* When this is set to non-zero debugging information about stack unwinding
350 will be printed. */
351
352static unsigned int riscv_debug_unwinder = 0;
353
b5ffee31
AB
354/* When this is set to non-zero debugging information about gdbarch
355 initialisation will be printed. */
dbbb1059 356
b5ffee31 357static unsigned int riscv_debug_gdbarch = 0;
dbbb1059 358
8a613826 359/* See riscv-tdep.h. */
dbbb1059 360
411baa47 361int
dbbb1059
AB
362riscv_isa_xlen (struct gdbarch *gdbarch)
363{
113b7b81
AB
364 return gdbarch_tdep (gdbarch)->isa_features.xlen;
365}
366
367/* See riscv-tdep.h. */
368
369int
370riscv_abi_xlen (struct gdbarch *gdbarch)
371{
372 return gdbarch_tdep (gdbarch)->abi_features.xlen;
dbbb1059
AB
373}
374
8a613826 375/* See riscv-tdep.h. */
dbbb1059 376
8a613826 377int
dbbb1059
AB
378riscv_isa_flen (struct gdbarch *gdbarch)
379{
113b7b81
AB
380 return gdbarch_tdep (gdbarch)->isa_features.flen;
381}
382
383/* See riscv-tdep.h. */
384
385int
386riscv_abi_flen (struct gdbarch *gdbarch)
387{
388 return gdbarch_tdep (gdbarch)->abi_features.flen;
dbbb1059
AB
389}
390
391/* Return true if the target for GDBARCH has floating point hardware. */
392
393static bool
394riscv_has_fp_regs (struct gdbarch *gdbarch)
395{
396 return (riscv_isa_flen (gdbarch) > 0);
397}
398
399/* Return true if GDBARCH is using any of the floating point hardware ABIs. */
400
401static bool
402riscv_has_fp_abi (struct gdbarch *gdbarch)
403{
113b7b81 404 return gdbarch_tdep (gdbarch)->abi_features.flen > 0;
dbbb1059
AB
405}
406
8c49aa89
AB
407/* Return true if REGNO is a floating pointer register. */
408
409static bool
410riscv_is_fp_regno_p (int regno)
411{
412 return (regno >= RISCV_FIRST_FP_REGNUM
413 && regno <= RISCV_LAST_FP_REGNUM);
414}
415
dbbb1059
AB
416/* Implement the breakpoint_kind_from_pc gdbarch method. */
417
418static int
419riscv_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
420{
421 if (use_compressed_breakpoints == AUTO_BOOLEAN_AUTO)
422 {
3ba2ee38 423 bool unaligned_p = false;
f37bc8b1
JB
424 gdb_byte buf[1];
425
3ba2ee38
JW
426 /* Some targets don't support unaligned reads. The address can only
427 be unaligned if the C extension is supported. So it is safe to
428 use a compressed breakpoint in this case. */
429 if (*pcptr & 0x2)
430 unaligned_p = true;
431 else
432 {
c01660c6
AB
433 /* Read the opcode byte to determine the instruction length. If
434 the read fails this may be because we tried to set the
435 breakpoint at an invalid address, in this case we provide a
436 fake result which will give a breakpoint length of 4.
437 Hopefully when we try to actually insert the breakpoint we
438 will see a failure then too which will be reported to the
439 user. */
440 if (target_read_code (*pcptr, buf, 1) == -1)
441 buf[0] = 0;
3ba2ee38
JW
442 read_code (*pcptr, buf, 1);
443 }
f37bc8b1
JB
444
445 if (riscv_debug_breakpoints)
3ba2ee38
JW
446 {
447 const char *bp = (unaligned_p || riscv_insn_length (buf[0]) == 2
448 ? "C.EBREAK" : "EBREAK");
449
450 fprintf_unfiltered (gdb_stdlog, "Using %s for breakpoint at %s ",
451 bp, paddress (gdbarch, *pcptr));
452 if (unaligned_p)
453 fprintf_unfiltered (gdb_stdlog, "(unaligned address)\n");
454 else
455 fprintf_unfiltered (gdb_stdlog, "(instruction length %d)\n",
456 riscv_insn_length (buf[0]));
457 }
458 if (unaligned_p || riscv_insn_length (buf[0]) == 2)
dbbb1059
AB
459 return 2;
460 else
461 return 4;
462 }
463 else if (use_compressed_breakpoints == AUTO_BOOLEAN_TRUE)
464 return 2;
465 else
466 return 4;
467}
468
469/* Implement the sw_breakpoint_from_kind gdbarch method. */
470
471static const gdb_byte *
472riscv_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
473{
474 static const gdb_byte ebreak[] = { 0x73, 0x00, 0x10, 0x00, };
475 static const gdb_byte c_ebreak[] = { 0x02, 0x90 };
476
477 *size = kind;
478 switch (kind)
479 {
480 case 2:
481 return c_ebreak;
482 case 4:
483 return ebreak;
484 default:
89a3b63e 485 gdb_assert_not_reached (_("unhandled breakpoint kind"));
dbbb1059
AB
486 }
487}
488
489/* Callback function for user_reg_add. */
490
491static struct value *
492value_of_riscv_user_reg (struct frame_info *frame, const void *baton)
493{
494 const int *reg_p = (const int *) baton;
495 return value_of_register (*reg_p, frame);
496}
497
b5ffee31
AB
498/* Implement the register_name gdbarch method. This is used instead of
499 the function supplied by calling TDESC_USE_REGISTERS so that we can
500 ensure the preferred names are offered. */
dbbb1059
AB
501
502static const char *
503riscv_register_name (struct gdbarch *gdbarch, int regnum)
504{
b5ffee31
AB
505 /* Lookup the name through the target description. If we get back NULL
506 then this is an unknown register. If we do get a name back then we
507 look up the registers preferred name below. */
508 const char *name = tdesc_register_name (gdbarch, regnum);
509 if (name == NULL || name[0] == '\0')
510 return NULL;
511
512 if (regnum >= RISCV_ZERO_REGNUM && regnum < RISCV_FIRST_FP_REGNUM)
513 {
514 gdb_assert (regnum < riscv_xreg_feature.registers.size ());
515 return riscv_xreg_feature.registers[regnum].names[0];
516 }
517
518 if (regnum >= RISCV_FIRST_FP_REGNUM && regnum <= RISCV_LAST_FP_REGNUM)
dbbb1059 519 {
b5ffee31
AB
520 if (riscv_has_fp_regs (gdbarch))
521 {
522 regnum -= RISCV_FIRST_FP_REGNUM;
523 gdb_assert (regnum < riscv_freg_feature.registers.size ());
524 return riscv_freg_feature.registers[regnum].names[0];
525 }
526 else
527 return NULL;
dbbb1059
AB
528 }
529
0dbfcfff
AB
530 /* Check that there's no gap between the set of registers handled above,
531 and the set of registers handled next. */
532 gdb_assert ((RISCV_LAST_FP_REGNUM + 1) == RISCV_FIRST_CSR_REGNUM);
dbbb1059
AB
533
534 if (regnum >= RISCV_FIRST_CSR_REGNUM && regnum <= RISCV_LAST_CSR_REGNUM)
535 {
420ecd9c
AB
536#define DECLARE_CSR(NAME,VALUE) \
537 case RISCV_ ## VALUE ## _REGNUM: return # NAME;
dbbb1059 538
420ecd9c
AB
539 switch (regnum)
540 {
69cb2952 541#include "opcode/riscv-opc.h"
420ecd9c
AB
542 }
543#undef DECLARE_CSR
dbbb1059
AB
544 }
545
546 if (regnum == RISCV_PRIV_REGNUM)
547 return "priv";
548
b5ffee31
AB
549 /* It is possible that that the target provides some registers that GDB
550 is unaware of, in that case just return the NAME from the target
551 description. */
552 return name;
dbbb1059
AB
553}
554
270b9329
JW
555/* Construct a type for 64-bit FP registers. */
556
557static struct type *
558riscv_fpreg_d_type (struct gdbarch *gdbarch)
559{
560 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
561
562 if (tdep->riscv_fpreg_d_type == nullptr)
563 {
564 const struct builtin_type *bt = builtin_type (gdbarch);
565
566 /* The type we're building is this: */
567#if 0
568 union __gdb_builtin_type_fpreg_d
569 {
570 float f;
571 double d;
572 };
573#endif
574
575 struct type *t;
576
577 t = arch_composite_type (gdbarch,
578 "__gdb_builtin_type_fpreg_d", TYPE_CODE_UNION);
579 append_composite_type_field (t, "float", bt->builtin_float);
580 append_composite_type_field (t, "double", bt->builtin_double);
581 TYPE_VECTOR (t) = 1;
582 TYPE_NAME (t) = "builtin_type_fpreg_d";
583 tdep->riscv_fpreg_d_type = t;
584 }
585
586 return tdep->riscv_fpreg_d_type;
587}
588
b5ffee31
AB
589/* Implement the register_type gdbarch method. This is installed as an
590 for the override setup by TDESC_USE_REGISTERS, for most registers we
591 delegate the type choice to the target description, but for a few
592 registers we try to improve the types if the target description has
593 taken a simplistic approach. */
270b9329
JW
594
595static struct type *
b5ffee31 596riscv_register_type (struct gdbarch *gdbarch, int regnum)
270b9329 597{
b5ffee31
AB
598 struct type *type = tdesc_register_type (gdbarch, regnum);
599 int xlen = riscv_isa_xlen (gdbarch);
270b9329 600
b5ffee31
AB
601 /* We want to perform some specific type "fixes" in cases where we feel
602 that we really can do better than the target description. For all
603 other cases we just return what the target description says. */
604 if (riscv_is_fp_regno_p (regnum))
270b9329 605 {
b5ffee31
AB
606 /* This spots the case for RV64 where the double is defined as
607 either 'ieee_double' or 'float' (which is the generic name that
608 converts to 'double' on 64-bit). In these cases its better to
609 present the registers using a union type. */
610 int flen = riscv_isa_flen (gdbarch);
611 if (flen == 8
612 && TYPE_CODE (type) == TYPE_CODE_FLT
613 && TYPE_LENGTH (type) == flen
614 && (strcmp (TYPE_NAME (type), "builtin_type_ieee_double") == 0
615 || strcmp (TYPE_NAME (type), "double") == 0))
616 type = riscv_fpreg_d_type (gdbarch);
270b9329
JW
617 }
618
b5ffee31
AB
619 if ((regnum == gdbarch_pc_regnum (gdbarch)
620 || regnum == RISCV_RA_REGNUM
621 || regnum == RISCV_FP_REGNUM
622 || regnum == RISCV_SP_REGNUM
623 || regnum == RISCV_GP_REGNUM
624 || regnum == RISCV_TP_REGNUM)
625 && TYPE_CODE (type) == TYPE_CODE_INT
626 && TYPE_LENGTH (type) == xlen)
dbbb1059 627 {
b5ffee31
AB
628 /* This spots the case where some interesting registers are defined
629 as simple integers of the expected size, we force these registers
630 to be pointers as we believe that is more useful. */
dbbb1059 631 if (regnum == gdbarch_pc_regnum (gdbarch)
b5ffee31
AB
632 || regnum == RISCV_RA_REGNUM)
633 type = builtin_type (gdbarch)->builtin_func_ptr;
634 else if (regnum == RISCV_FP_REGNUM
635 || regnum == RISCV_SP_REGNUM
636 || regnum == RISCV_GP_REGNUM
637 || regnum == RISCV_TP_REGNUM)
638 type = builtin_type (gdbarch)->builtin_data_ptr;
dbbb1059 639 }
dbbb1059 640
b5ffee31 641 return type;
dbbb1059
AB
642}
643
644/* Helper for riscv_print_registers_info, prints info for a single register
645 REGNUM. */
646
647static void
648riscv_print_one_register_info (struct gdbarch *gdbarch,
649 struct ui_file *file,
650 struct frame_info *frame,
651 int regnum)
652{
653 const char *name = gdbarch_register_name (gdbarch, regnum);
b5ffee31
AB
654 struct value *val;
655 struct type *regtype;
dbbb1059
AB
656 int print_raw_format;
657 enum tab_stops { value_column_1 = 15 };
658
659 fputs_filtered (name, file);
660 print_spaces_filtered (value_column_1 - strlen (name), file);
661
a70b8144 662 try
b5ffee31
AB
663 {
664 val = value_of_register (regnum, frame);
665 regtype = value_type (val);
666 }
230d2906 667 catch (const gdb_exception_error &ex)
b5ffee31
AB
668 {
669 /* Handle failure to read a register without interrupting the entire
670 'info registers' flow. */
3d6e9d23 671 fprintf_filtered (file, "%s\n", ex.what ());
b5ffee31
AB
672 return;
673 }
b5ffee31 674
dbbb1059
AB
675 print_raw_format = (value_entirely_available (val)
676 && !value_optimized_out (val));
677
270b9329
JW
678 if (TYPE_CODE (regtype) == TYPE_CODE_FLT
679 || (TYPE_CODE (regtype) == TYPE_CODE_UNION
680 && TYPE_NFIELDS (regtype) == 2
681 && TYPE_CODE (TYPE_FIELD_TYPE (regtype, 0)) == TYPE_CODE_FLT
682 && TYPE_CODE (TYPE_FIELD_TYPE (regtype, 1)) == TYPE_CODE_FLT)
683 || (TYPE_CODE (regtype) == TYPE_CODE_UNION
684 && TYPE_NFIELDS (regtype) == 3
685 && TYPE_CODE (TYPE_FIELD_TYPE (regtype, 0)) == TYPE_CODE_FLT
686 && TYPE_CODE (TYPE_FIELD_TYPE (regtype, 1)) == TYPE_CODE_FLT
687 && TYPE_CODE (TYPE_FIELD_TYPE (regtype, 2)) == TYPE_CODE_FLT))
dbbb1059
AB
688 {
689 struct value_print_options opts;
690 const gdb_byte *valaddr = value_contents_for_printing (val);
691 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (regtype));
692
693 get_user_print_options (&opts);
694 opts.deref_ref = 1;
695
696 val_print (regtype,
697 value_embedded_offset (val), 0,
698 file, 0, val, &opts, current_language);
699
700 if (print_raw_format)
701 {
702 fprintf_filtered (file, "\t(raw ");
703 print_hex_chars (file, valaddr, TYPE_LENGTH (regtype), byte_order,
704 true);
705 fprintf_filtered (file, ")");
706 }
707 }
708 else
709 {
710 struct value_print_options opts;
711
712 /* Print the register in hex. */
713 get_formatted_print_options (&opts, 'x');
714 opts.deref_ref = 1;
715 val_print (regtype,
716 value_embedded_offset (val), 0,
717 file, 0, val, &opts, current_language);
718
719 if (print_raw_format)
720 {
721 if (regnum == RISCV_CSR_MSTATUS_REGNUM)
722 {
723 LONGEST d;
724 int size = register_size (gdbarch, regnum);
725 unsigned xlen;
726
b7c8601a
JW
727 /* The SD field is always in the upper bit of MSTATUS, regardless
728 of the number of bits in MSTATUS. */
dbbb1059 729 d = value_as_long (val);
b7c8601a 730 xlen = size * 8;
dbbb1059
AB
731 fprintf_filtered (file,
732 "\tSD:%X VM:%02X MXR:%X PUM:%X MPRV:%X XS:%X "
733 "FS:%X MPP:%x HPP:%X SPP:%X MPIE:%X HPIE:%X "
734 "SPIE:%X UPIE:%X MIE:%X HIE:%X SIE:%X UIE:%X",
735 (int) ((d >> (xlen - 1)) & 0x1),
736 (int) ((d >> 24) & 0x1f),
737 (int) ((d >> 19) & 0x1),
738 (int) ((d >> 18) & 0x1),
739 (int) ((d >> 17) & 0x1),
740 (int) ((d >> 15) & 0x3),
741 (int) ((d >> 13) & 0x3),
742 (int) ((d >> 11) & 0x3),
743 (int) ((d >> 9) & 0x3),
744 (int) ((d >> 8) & 0x1),
745 (int) ((d >> 7) & 0x1),
746 (int) ((d >> 6) & 0x1),
747 (int) ((d >> 5) & 0x1),
748 (int) ((d >> 4) & 0x1),
749 (int) ((d >> 3) & 0x1),
750 (int) ((d >> 2) & 0x1),
751 (int) ((d >> 1) & 0x1),
752 (int) ((d >> 0) & 0x1));
753 }
754 else if (regnum == RISCV_CSR_MISA_REGNUM)
755 {
756 int base;
757 unsigned xlen, i;
758 LONGEST d;
b7c8601a 759 int size = register_size (gdbarch, regnum);
dbbb1059 760
b7c8601a
JW
761 /* The MXL field is always in the upper two bits of MISA,
762 regardless of the number of bits in MISA. Mask out other
763 bits to ensure we have a positive value. */
dbbb1059 764 d = value_as_long (val);
b7c8601a 765 base = (d >> ((size * 8) - 2)) & 0x3;
dbbb1059
AB
766 xlen = 16;
767
768 for (; base > 0; base--)
769 xlen *= 2;
770 fprintf_filtered (file, "\tRV%d", xlen);
771
772 for (i = 0; i < 26; i++)
773 {
774 if (d & (1 << i))
775 fprintf_filtered (file, "%c", 'A' + i);
776 }
777 }
778 else if (regnum == RISCV_CSR_FCSR_REGNUM
779 || regnum == RISCV_CSR_FFLAGS_REGNUM
780 || regnum == RISCV_CSR_FRM_REGNUM)
781 {
782 LONGEST d;
783
784 d = value_as_long (val);
785
786 fprintf_filtered (file, "\t");
787 if (regnum != RISCV_CSR_FRM_REGNUM)
788 fprintf_filtered (file,
789 "RD:%01X NV:%d DZ:%d OF:%d UF:%d NX:%d",
790 (int) ((d >> 5) & 0x7),
791 (int) ((d >> 4) & 0x1),
792 (int) ((d >> 3) & 0x1),
793 (int) ((d >> 2) & 0x1),
794 (int) ((d >> 1) & 0x1),
795 (int) ((d >> 0) & 0x1));
796
797 if (regnum != RISCV_CSR_FFLAGS_REGNUM)
798 {
799 static const char * const sfrm[] =
800 {
801 "RNE (round to nearest; ties to even)",
802 "RTZ (Round towards zero)",
803 "RDN (Round down towards -INF)",
804 "RUP (Round up towards +INF)",
805 "RMM (Round to nearest; ties to max magnitude)",
806 "INVALID[5]",
807 "INVALID[6]",
808 "dynamic rounding mode",
809 };
810 int frm = ((regnum == RISCV_CSR_FCSR_REGNUM)
811 ? (d >> 5) : d) & 0x3;
812
813 fprintf_filtered (file, "%sFRM:%i [%s]",
814 (regnum == RISCV_CSR_FCSR_REGNUM
815 ? " " : ""),
816 frm, sfrm[frm]);
817 }
818 }
819 else if (regnum == RISCV_PRIV_REGNUM)
820 {
821 LONGEST d;
822 uint8_t priv;
823
824 d = value_as_long (val);
825 priv = d & 0xff;
826
827 if (priv < 4)
828 {
829 static const char * const sprv[] =
830 {
831 "User/Application",
832 "Supervisor",
833 "Hypervisor",
834 "Machine"
835 };
836 fprintf_filtered (file, "\tprv:%d [%s]",
837 priv, sprv[priv]);
838 }
839 else
840 fprintf_filtered (file, "\tprv:%d [INVALID]", priv);
841 }
842 else
843 {
844 /* If not a vector register, print it also according to its
845 natural format. */
846 if (TYPE_VECTOR (regtype) == 0)
847 {
848 get_user_print_options (&opts);
849 opts.deref_ref = 1;
850 fprintf_filtered (file, "\t");
851 val_print (regtype,
852 value_embedded_offset (val), 0,
853 file, 0, val, &opts, current_language);
854 }
855 }
856 }
857 }
858 fprintf_filtered (file, "\n");
859}
860
0dbfcfff
AB
861/* Return true if REGNUM is a valid CSR register. The CSR register space
862 is sparsely populated, so not every number is a named CSR. */
863
864static bool
865riscv_is_regnum_a_named_csr (int regnum)
866{
867 gdb_assert (regnum >= RISCV_FIRST_CSR_REGNUM
868 && regnum <= RISCV_LAST_CSR_REGNUM);
869
870 switch (regnum)
871 {
872#define DECLARE_CSR(name, num) case RISCV_ ## num ## _REGNUM:
873#include "opcode/riscv-opc.h"
874#undef DECLARE_CSR
875 return true;
876
877 default:
878 return false;
879 }
880}
881
dbbb1059
AB
882/* Implement the register_reggroup_p gdbarch method. Is REGNUM a member
883 of REGGROUP? */
884
885static int
886riscv_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
887 struct reggroup *reggroup)
888{
dbbb1059
AB
889 /* Used by 'info registers' and 'info registers <groupname>'. */
890
891 if (gdbarch_register_name (gdbarch, regnum) == NULL
892 || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
893 return 0;
894
b5ffee31
AB
895 if (regnum > RISCV_LAST_REGNUM)
896 {
897 int ret = tdesc_register_in_reggroup_p (gdbarch, regnum, reggroup);
898 if (ret != -1)
899 return ret;
900
901 return default_register_reggroup_p (gdbarch, regnum, reggroup);
902 }
903
dbbb1059
AB
904 if (reggroup == all_reggroup)
905 {
906 if (regnum < RISCV_FIRST_CSR_REGNUM || regnum == RISCV_PRIV_REGNUM)
907 return 1;
0dbfcfff
AB
908 if (riscv_is_regnum_a_named_csr (regnum))
909 return 1;
dbbb1059
AB
910 return 0;
911 }
912 else if (reggroup == float_reggroup)
8c49aa89
AB
913 return (riscv_is_fp_regno_p (regnum)
914 || regnum == RISCV_CSR_FCSR_REGNUM
915 || regnum == RISCV_CSR_FFLAGS_REGNUM
916 || regnum == RISCV_CSR_FRM_REGNUM);
dbbb1059
AB
917 else if (reggroup == general_reggroup)
918 return regnum < RISCV_FIRST_FP_REGNUM;
919 else if (reggroup == restore_reggroup || reggroup == save_reggroup)
920 {
921 if (riscv_has_fp_regs (gdbarch))
ecc82c05
AB
922 return (regnum <= RISCV_LAST_FP_REGNUM
923 || regnum == RISCV_CSR_FCSR_REGNUM
924 || regnum == RISCV_CSR_FFLAGS_REGNUM
925 || regnum == RISCV_CSR_FRM_REGNUM);
dbbb1059
AB
926 else
927 return regnum < RISCV_FIRST_FP_REGNUM;
928 }
b5ffee31 929 else if (reggroup == system_reggroup || reggroup == csr_reggroup)
dbbb1059
AB
930 {
931 if (regnum == RISCV_PRIV_REGNUM)
932 return 1;
933 if (regnum < RISCV_FIRST_CSR_REGNUM || regnum > RISCV_LAST_CSR_REGNUM)
934 return 0;
0dbfcfff
AB
935 if (riscv_is_regnum_a_named_csr (regnum))
936 return 1;
dbbb1059
AB
937 return 0;
938 }
939 else if (reggroup == vector_reggroup)
940 return 0;
941 else
942 return 0;
943}
944
945/* Implement the print_registers_info gdbarch method. This is used by
946 'info registers' and 'info all-registers'. */
947
948static void
949riscv_print_registers_info (struct gdbarch *gdbarch,
950 struct ui_file *file,
951 struct frame_info *frame,
952 int regnum, int print_all)
953{
954 if (regnum != -1)
955 {
956 /* Print one specified register. */
dbbb1059
AB
957 if (gdbarch_register_name (gdbarch, regnum) == NULL
958 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
959 error (_("Not a valid register for the current processor type"));
960 riscv_print_one_register_info (gdbarch, file, frame, regnum);
961 }
962 else
963 {
964 struct reggroup *reggroup;
965
966 if (print_all)
967 reggroup = all_reggroup;
968 else
969 reggroup = general_reggroup;
970
971 for (regnum = 0; regnum <= RISCV_LAST_REGNUM; ++regnum)
972 {
973 /* Zero never changes, so might as well hide by default. */
974 if (regnum == RISCV_ZERO_REGNUM && !print_all)
975 continue;
976
977 /* Registers with no name are not valid on this ISA. */
978 if (gdbarch_register_name (gdbarch, regnum) == NULL
979 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
980 continue;
981
982 /* Is the register in the group we're interested in? */
b5ffee31 983 if (!gdbarch_register_reggroup_p (gdbarch, regnum, reggroup))
dbbb1059
AB
984 continue;
985
986 riscv_print_one_register_info (gdbarch, file, frame, regnum);
987 }
988 }
989}
990
991/* Class that handles one decoded RiscV instruction. */
992
993class riscv_insn
994{
995public:
996
997 /* Enum of all the opcodes that GDB cares about during the prologue scan. */
998 enum opcode
999 {
1000 /* Unknown value is used at initialisation time. */
1001 UNKNOWN = 0,
1002
1003 /* These instructions are all the ones we are interested in during the
1004 prologue scan. */
1005 ADD,
1006 ADDI,
1007 ADDIW,
1008 ADDW,
1009 AUIPC,
1010 LUI,
1011 SD,
1012 SW,
5c720ed8
JW
1013 /* These are needed for software breakopint support. */
1014 JAL,
1015 JALR,
1016 BEQ,
1017 BNE,
1018 BLT,
1019 BGE,
1020 BLTU,
1021 BGEU,
1022 /* These are needed for stepping over atomic sequences. */
1023 LR,
1024 SC,
dbbb1059
AB
1025
1026 /* Other instructions are not interesting during the prologue scan, and
1027 are ignored. */
1028 OTHER
1029 };
1030
1031 riscv_insn ()
1032 : m_length (0),
1033 m_opcode (OTHER),
1034 m_rd (0),
1035 m_rs1 (0),
1036 m_rs2 (0)
1037 {
1038 /* Nothing. */
1039 }
1040
1041 void decode (struct gdbarch *gdbarch, CORE_ADDR pc);
1042
1043 /* Get the length of the instruction in bytes. */
1044 int length () const
1045 { return m_length; }
1046
1047 /* Get the opcode for this instruction. */
1048 enum opcode opcode () const
1049 { return m_opcode; }
1050
1051 /* Get destination register field for this instruction. This is only
1052 valid if the OPCODE implies there is such a field for this
1053 instruction. */
1054 int rd () const
1055 { return m_rd; }
1056
1057 /* Get the RS1 register field for this instruction. This is only valid
1058 if the OPCODE implies there is such a field for this instruction. */
1059 int rs1 () const
1060 { return m_rs1; }
1061
1062 /* Get the RS2 register field for this instruction. This is only valid
1063 if the OPCODE implies there is such a field for this instruction. */
1064 int rs2 () const
1065 { return m_rs2; }
1066
1067 /* Get the immediate for this instruction in signed form. This is only
1068 valid if the OPCODE implies there is such a field for this
1069 instruction. */
1070 int imm_signed () const
1071 { return m_imm.s; }
1072
1073private:
1074
1075 /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
1076 int decode_register_index (unsigned long opcode, int offset)
1077 {
1078 return (opcode >> offset) & 0x1F;
1079 }
1080
5c720ed8
JW
1081 /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
1082 int decode_register_index_short (unsigned long opcode, int offset)
1083 {
1084 return ((opcode >> offset) & 0x7) + 8;
1085 }
1086
dbbb1059
AB
1087 /* Helper for DECODE, decode 32-bit R-type instruction. */
1088 void decode_r_type_insn (enum opcode opcode, ULONGEST ival)
1089 {
1090 m_opcode = opcode;
1091 m_rd = decode_register_index (ival, OP_SH_RD);
1092 m_rs1 = decode_register_index (ival, OP_SH_RS1);
1093 m_rs2 = decode_register_index (ival, OP_SH_RS2);
1094 }
1095
1096 /* Helper for DECODE, decode 16-bit compressed R-type instruction. */
1097 void decode_cr_type_insn (enum opcode opcode, ULONGEST ival)
1098 {
1099 m_opcode = opcode;
1100 m_rd = m_rs1 = decode_register_index (ival, OP_SH_CRS1S);
1101 m_rs2 = decode_register_index (ival, OP_SH_CRS2);
1102 }
1103
1104 /* Helper for DECODE, decode 32-bit I-type instruction. */
1105 void decode_i_type_insn (enum opcode opcode, ULONGEST ival)
1106 {
1107 m_opcode = opcode;
1108 m_rd = decode_register_index (ival, OP_SH_RD);
1109 m_rs1 = decode_register_index (ival, OP_SH_RS1);
1110 m_imm.s = EXTRACT_ITYPE_IMM (ival);
1111 }
1112
1113 /* Helper for DECODE, decode 16-bit compressed I-type instruction. */
1114 void decode_ci_type_insn (enum opcode opcode, ULONGEST ival)
1115 {
1116 m_opcode = opcode;
1117 m_rd = m_rs1 = decode_register_index (ival, OP_SH_CRS1S);
1118 m_imm.s = EXTRACT_RVC_IMM (ival);
1119 }
1120
1121 /* Helper for DECODE, decode 32-bit S-type instruction. */
1122 void decode_s_type_insn (enum opcode opcode, ULONGEST ival)
1123 {
1124 m_opcode = opcode;
1125 m_rs1 = decode_register_index (ival, OP_SH_RS1);
1126 m_rs2 = decode_register_index (ival, OP_SH_RS2);
1127 m_imm.s = EXTRACT_STYPE_IMM (ival);
1128 }
1129
ff3a05b3
AB
1130 /* Helper for DECODE, decode 16-bit CS-type instruction. The immediate
1131 encoding is different for each CS format instruction, so extracting
1132 the immediate is left up to the caller, who should pass the extracted
1133 immediate value through in IMM. */
1134 void decode_cs_type_insn (enum opcode opcode, ULONGEST ival, int imm)
1135 {
1136 m_opcode = opcode;
1137 m_imm.s = imm;
1138 m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
1139 m_rs2 = decode_register_index_short (ival, OP_SH_CRS2S);
1140 }
1141
1142 /* Helper for DECODE, decode 16-bit CSS-type instruction. The immediate
1143 encoding is different for each CSS format instruction, so extracting
1144 the immediate is left up to the caller, who should pass the extracted
1145 immediate value through in IMM. */
1146 void decode_css_type_insn (enum opcode opcode, ULONGEST ival, int imm)
1147 {
1148 m_opcode = opcode;
1149 m_imm.s = imm;
1150 m_rs1 = RISCV_SP_REGNUM;
1151 /* Not a compressed register number in this case. */
1152 m_rs2 = decode_register_index (ival, OP_SH_CRS2);
1153 }
1154
dbbb1059
AB
1155 /* Helper for DECODE, decode 32-bit U-type instruction. */
1156 void decode_u_type_insn (enum opcode opcode, ULONGEST ival)
1157 {
1158 m_opcode = opcode;
1159 m_rd = decode_register_index (ival, OP_SH_RD);
1160 m_imm.s = EXTRACT_UTYPE_IMM (ival);
1161 }
1162
5c720ed8
JW
1163 /* Helper for DECODE, decode 32-bit J-type instruction. */
1164 void decode_j_type_insn (enum opcode opcode, ULONGEST ival)
1165 {
1166 m_opcode = opcode;
1167 m_rd = decode_register_index (ival, OP_SH_RD);
1168 m_imm.s = EXTRACT_UJTYPE_IMM (ival);
1169 }
1170
1171 /* Helper for DECODE, decode 32-bit J-type instruction. */
1172 void decode_cj_type_insn (enum opcode opcode, ULONGEST ival)
1173 {
1174 m_opcode = opcode;
1175 m_imm.s = EXTRACT_RVC_J_IMM (ival);
1176 }
1177
1178 void decode_b_type_insn (enum opcode opcode, ULONGEST ival)
1179 {
1180 m_opcode = opcode;
1181 m_rs1 = decode_register_index (ival, OP_SH_RS1);
1182 m_rs2 = decode_register_index (ival, OP_SH_RS2);
1183 m_imm.s = EXTRACT_SBTYPE_IMM (ival);
1184 }
1185
1186 void decode_cb_type_insn (enum opcode opcode, ULONGEST ival)
1187 {
1188 m_opcode = opcode;
1189 m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
1190 m_imm.s = EXTRACT_RVC_B_IMM (ival);
1191 }
1192
dbbb1059
AB
1193 /* Fetch instruction from target memory at ADDR, return the content of
1194 the instruction, and update LEN with the instruction length. */
1195 static ULONGEST fetch_instruction (struct gdbarch *gdbarch,
1196 CORE_ADDR addr, int *len);
1197
1198 /* The length of the instruction in bytes. Should be 2 or 4. */
1199 int m_length;
1200
1201 /* The instruction opcode. */
1202 enum opcode m_opcode;
1203
1204 /* The three possible registers an instruction might reference. Not
1205 every instruction fills in all of these registers. Which fields are
1206 valid depends on the opcode. The naming of these fields matches the
1207 naming in the riscv isa manual. */
1208 int m_rd;
1209 int m_rs1;
1210 int m_rs2;
1211
1212 /* Possible instruction immediate. This is only valid if the instruction
1213 format contains an immediate, not all instruction, whether this is
1214 valid depends on the opcode. Despite only having one format for now
1215 the immediate is packed into a union, later instructions might require
1216 an unsigned formatted immediate, having the union in place now will
1217 reduce the need for code churn later. */
1218 union riscv_insn_immediate
1219 {
1220 riscv_insn_immediate ()
1221 : s (0)
1222 {
1223 /* Nothing. */
1224 }
1225
1226 int s;
1227 } m_imm;
1228};
1229
1230/* Fetch instruction from target memory at ADDR, return the content of the
1231 instruction, and update LEN with the instruction length. */
1232
1233ULONGEST
1234riscv_insn::fetch_instruction (struct gdbarch *gdbarch,
1235 CORE_ADDR addr, int *len)
1236{
1237 enum bfd_endian byte_order = gdbarch_byte_order_for_code (gdbarch);
1238 gdb_byte buf[8];
1239 int instlen, status;
1240
1241 /* All insns are at least 16 bits. */
1242 status = target_read_memory (addr, buf, 2);
1243 if (status)
1244 memory_error (TARGET_XFER_E_IO, addr);
1245
1246 /* If we need more, grab it now. */
1247 instlen = riscv_insn_length (buf[0]);
89a3b63e 1248 gdb_assert (instlen <= sizeof (buf));
dbbb1059 1249 *len = instlen;
89a3b63e
AB
1250
1251 if (instlen > 2)
dbbb1059
AB
1252 {
1253 status = target_read_memory (addr + 2, buf + 2, instlen - 2);
1254 if (status)
1255 memory_error (TARGET_XFER_E_IO, addr + 2);
1256 }
1257
1258 return extract_unsigned_integer (buf, instlen, byte_order);
1259}
1260
17cf2897
AB
1261/* Fetch from target memory an instruction at PC and decode it. This can
1262 throw an error if the memory access fails, callers are responsible for
1263 handling this error if that is appropriate. */
dbbb1059
AB
1264
1265void
1266riscv_insn::decode (struct gdbarch *gdbarch, CORE_ADDR pc)
1267{
1268 ULONGEST ival;
1269
1270 /* Fetch the instruction, and the instructions length. */
1271 ival = fetch_instruction (gdbarch, pc, &m_length);
1272
1273 if (m_length == 4)
1274 {
1275 if (is_add_insn (ival))
1276 decode_r_type_insn (ADD, ival);
1277 else if (is_addw_insn (ival))
1278 decode_r_type_insn (ADDW, ival);
1279 else if (is_addi_insn (ival))
1280 decode_i_type_insn (ADDI, ival);
1281 else if (is_addiw_insn (ival))
1282 decode_i_type_insn (ADDIW, ival);
1283 else if (is_auipc_insn (ival))
1284 decode_u_type_insn (AUIPC, ival);
1285 else if (is_lui_insn (ival))
1286 decode_u_type_insn (LUI, ival);
1287 else if (is_sd_insn (ival))
1288 decode_s_type_insn (SD, ival);
1289 else if (is_sw_insn (ival))
1290 decode_s_type_insn (SW, ival);
5c720ed8
JW
1291 else if (is_jal_insn (ival))
1292 decode_j_type_insn (JAL, ival);
1293 else if (is_jalr_insn (ival))
1294 decode_i_type_insn (JALR, ival);
1295 else if (is_beq_insn (ival))
1296 decode_b_type_insn (BEQ, ival);
1297 else if (is_bne_insn (ival))
1298 decode_b_type_insn (BNE, ival);
1299 else if (is_blt_insn (ival))
1300 decode_b_type_insn (BLT, ival);
1301 else if (is_bge_insn (ival))
1302 decode_b_type_insn (BGE, ival);
1303 else if (is_bltu_insn (ival))
1304 decode_b_type_insn (BLTU, ival);
1305 else if (is_bgeu_insn (ival))
1306 decode_b_type_insn (BGEU, ival);
1307 else if (is_lr_w_insn (ival))
1308 decode_r_type_insn (LR, ival);
1309 else if (is_lr_d_insn (ival))
1310 decode_r_type_insn (LR, ival);
1311 else if (is_sc_w_insn (ival))
1312 decode_r_type_insn (SC, ival);
1313 else if (is_sc_d_insn (ival))
1314 decode_r_type_insn (SC, ival);
dbbb1059
AB
1315 else
1316 /* None of the other fields are valid in this case. */
1317 m_opcode = OTHER;
1318 }
1319 else if (m_length == 2)
1320 {
5c720ed8
JW
1321 int xlen = riscv_isa_xlen (gdbarch);
1322
1323 /* C_ADD and C_JALR have the same opcode. If RS2 is 0, then this is a
1324 C_JALR. So must try to match C_JALR first as it has more bits in
1325 mask. */
1326 if (is_c_jalr_insn (ival))
1327 decode_cr_type_insn (JALR, ival);
1328 else if (is_c_add_insn (ival))
dbbb1059 1329 decode_cr_type_insn (ADD, ival);
5c720ed8
JW
1330 /* C_ADDW is RV64 and RV128 only. */
1331 else if (xlen != 4 && is_c_addw_insn (ival))
dbbb1059
AB
1332 decode_cr_type_insn (ADDW, ival);
1333 else if (is_c_addi_insn (ival))
1334 decode_ci_type_insn (ADDI, ival);
5c720ed8
JW
1335 /* C_ADDIW and C_JAL have the same opcode. C_ADDIW is RV64 and RV128
1336 only and C_JAL is RV32 only. */
1337 else if (xlen != 4 && is_c_addiw_insn (ival))
dbbb1059 1338 decode_ci_type_insn (ADDIW, ival);
5c720ed8
JW
1339 else if (xlen == 4 && is_c_jal_insn (ival))
1340 decode_cj_type_insn (JAL, ival);
1341 /* C_ADDI16SP and C_LUI have the same opcode. If RD is 2, then this is a
1342 C_ADDI16SP. So must try to match C_ADDI16SP first as it has more bits
1343 in mask. */
dbbb1059
AB
1344 else if (is_c_addi16sp_insn (ival))
1345 {
1346 m_opcode = ADDI;
1347 m_rd = m_rs1 = decode_register_index (ival, OP_SH_RD);
1348 m_imm.s = EXTRACT_RVC_ADDI16SP_IMM (ival);
1349 }
ff3a05b3
AB
1350 else if (is_c_addi4spn_insn (ival))
1351 {
1352 m_opcode = ADDI;
1353 m_rd = decode_register_index_short (ival, OP_SH_CRS2S);
1354 m_rs1 = RISCV_SP_REGNUM;
1355 m_imm.s = EXTRACT_RVC_ADDI4SPN_IMM (ival);
1356 }
5c720ed8 1357 else if (is_c_lui_insn (ival))
d354055e
AB
1358 {
1359 m_opcode = LUI;
1360 m_rd = decode_register_index (ival, OP_SH_CRS1S);
1361 m_imm.s = EXTRACT_RVC_LUI_IMM (ival);
1362 }
5c720ed8
JW
1363 /* C_SD and C_FSW have the same opcode. C_SD is RV64 and RV128 only,
1364 and C_FSW is RV32 only. */
1365 else if (xlen != 4 && is_c_sd_insn (ival))
ff3a05b3 1366 decode_cs_type_insn (SD, ival, EXTRACT_RVC_LD_IMM (ival));
5c720ed8 1367 else if (is_c_sw_insn (ival))
ff3a05b3
AB
1368 decode_cs_type_insn (SW, ival, EXTRACT_RVC_LW_IMM (ival));
1369 else if (is_c_swsp_insn (ival))
1370 decode_css_type_insn (SW, ival, EXTRACT_RVC_SWSP_IMM (ival));
1371 else if (xlen != 4 && is_c_sdsp_insn (ival))
1372 decode_css_type_insn (SW, ival, EXTRACT_RVC_SDSP_IMM (ival));
5c720ed8
JW
1373 /* C_JR and C_MV have the same opcode. If RS2 is 0, then this is a C_JR.
1374 So must try to match C_JR first as it ahs more bits in mask. */
1375 else if (is_c_jr_insn (ival))
1376 decode_cr_type_insn (JALR, ival);
1377 else if (is_c_j_insn (ival))
1378 decode_cj_type_insn (JAL, ival);
1379 else if (is_c_beqz_insn (ival))
1380 decode_cb_type_insn (BEQ, ival);
1381 else if (is_c_bnez_insn (ival))
1382 decode_cb_type_insn (BNE, ival);
dbbb1059
AB
1383 else
1384 /* None of the other fields of INSN are valid in this case. */
1385 m_opcode = OTHER;
1386 }
1387 else
312617a3
AB
1388 {
1389 /* This must be a 6 or 8 byte instruction, we don't currently decode
1390 any of these, so just ignore it. */
1391 gdb_assert (m_length == 6 || m_length == 8);
1392 m_opcode = OTHER;
1393 }
dbbb1059
AB
1394}
1395
1396/* The prologue scanner. This is currently only used for skipping the
1397 prologue of a function when the DWARF information is not sufficient.
1398 However, it is written with filling of the frame cache in mind, which
1399 is why different groups of stack setup instructions are split apart
1400 during the core of the inner loop. In the future, the intention is to
1401 extend this function to fully support building up a frame cache that
1402 can unwind register values when there is no DWARF information. */
1403
1404static CORE_ADDR
1405riscv_scan_prologue (struct gdbarch *gdbarch,
78a3b0fa
AB
1406 CORE_ADDR start_pc, CORE_ADDR end_pc,
1407 struct riscv_unwind_cache *cache)
dbbb1059 1408{
78a3b0fa 1409 CORE_ADDR cur_pc, next_pc, after_prologue_pc;
dbbb1059
AB
1410 CORE_ADDR end_prologue_addr = 0;
1411
78a3b0fa
AB
1412 /* Find an upper limit on the function prologue using the debug
1413 information. If the debug information could not be used to provide
1414 that bound, then use an arbitrary large number as the upper bound. */
1415 after_prologue_pc = skip_prologue_using_sal (gdbarch, start_pc);
1416 if (after_prologue_pc == 0)
1417 after_prologue_pc = start_pc + 100; /* Arbitrary large number. */
1418 if (after_prologue_pc < end_pc)
1419 end_pc = after_prologue_pc;
1420
1421 pv_t regs[RISCV_NUM_INTEGER_REGS]; /* Number of GPR. */
1422 for (int regno = 0; regno < RISCV_NUM_INTEGER_REGS; regno++)
1423 regs[regno] = pv_register (regno, 0);
1424 pv_area stack (RISCV_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1425
1426 if (riscv_debug_unwinder)
1427 fprintf_unfiltered
1428 (gdb_stdlog,
1429 "Prologue scan for function starting at %s (limit %s)\n",
1430 core_addr_to_string (start_pc),
1431 core_addr_to_string (end_pc));
1432
1433 for (next_pc = cur_pc = start_pc; cur_pc < end_pc; cur_pc = next_pc)
dbbb1059
AB
1434 {
1435 struct riscv_insn insn;
1436
1437 /* Decode the current instruction, and decide where the next
1438 instruction lives based on the size of this instruction. */
1439 insn.decode (gdbarch, cur_pc);
1440 gdb_assert (insn.length () > 0);
1441 next_pc = cur_pc + insn.length ();
1442
1443 /* Look for common stack adjustment insns. */
1444 if ((insn.opcode () == riscv_insn::ADDI
1445 || insn.opcode () == riscv_insn::ADDIW)
1446 && insn.rd () == RISCV_SP_REGNUM
1447 && insn.rs1 () == RISCV_SP_REGNUM)
1448 {
1449 /* Handle: addi sp, sp, -i
1450 or: addiw sp, sp, -i */
78a3b0fa
AB
1451 gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
1452 gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
1453 regs[insn.rd ()]
1454 = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
dbbb1059
AB
1455 }
1456 else if ((insn.opcode () == riscv_insn::SW
1457 || insn.opcode () == riscv_insn::SD)
1458 && (insn.rs1 () == RISCV_SP_REGNUM
1459 || insn.rs1 () == RISCV_FP_REGNUM))
1460 {
1461 /* Handle: sw reg, offset(sp)
1462 or: sd reg, offset(sp)
1463 or: sw reg, offset(s0)
1464 or: sd reg, offset(s0) */
1465 /* Instruction storing a register onto the stack. */
78a3b0fa
AB
1466 gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
1467 gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
1468 stack.store (pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ()),
1469 (insn.opcode () == riscv_insn::SW ? 4 : 8),
1470 regs[insn.rs2 ()]);
dbbb1059
AB
1471 }
1472 else if (insn.opcode () == riscv_insn::ADDI
1473 && insn.rd () == RISCV_FP_REGNUM
1474 && insn.rs1 () == RISCV_SP_REGNUM)
1475 {
1476 /* Handle: addi s0, sp, size */
1477 /* Instructions setting up the frame pointer. */
78a3b0fa
AB
1478 gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
1479 gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
1480 regs[insn.rd ()]
1481 = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
dbbb1059
AB
1482 }
1483 else if ((insn.opcode () == riscv_insn::ADD
1484 || insn.opcode () == riscv_insn::ADDW)
1485 && insn.rd () == RISCV_FP_REGNUM
1486 && insn.rs1 () == RISCV_SP_REGNUM
1487 && insn.rs2 () == RISCV_ZERO_REGNUM)
1488 {
1489 /* Handle: add s0, sp, 0
1490 or: addw s0, sp, 0 */
1491 /* Instructions setting up the frame pointer. */
78a3b0fa
AB
1492 gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
1493 gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
1494 regs[insn.rd ()] = pv_add_constant (regs[insn.rs1 ()], 0);
dbbb1059 1495 }
d354055e
AB
1496 else if ((insn.opcode () == riscv_insn::ADDI
1497 && insn.rd () == RISCV_ZERO_REGNUM
1498 && insn.rs1 () == RISCV_ZERO_REGNUM
1499 && insn.imm_signed () == 0))
dbbb1059 1500 {
d354055e 1501 /* Handle: add x0, x0, 0 (NOP) */
dbbb1059 1502 }
d354055e
AB
1503 else if (insn.opcode () == riscv_insn::AUIPC)
1504 {
1505 gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
1506 regs[insn.rd ()] = pv_constant (cur_pc + insn.imm_signed ());
1507 }
1508 else if (insn.opcode () == riscv_insn::LUI)
1509 {
1510 /* Handle: lui REG, n
1511 Where REG is not gp register. */
1512 gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
1513 regs[insn.rd ()] = pv_constant (insn.imm_signed ());
1514 }
1515 else if (insn.opcode () == riscv_insn::ADDI)
1516 {
1517 /* Handle: addi REG1, REG2, IMM */
1518 gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
1519 gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
1520 regs[insn.rd ()]
1521 = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
1522 }
1523 else if (insn.opcode () == riscv_insn::ADD)
1524 {
1525 /* Handle: addi REG1, REG2, IMM */
1526 gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
1527 gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
1528 gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
1529 regs[insn.rd ()] = pv_add (regs[insn.rs1 ()], regs[insn.rs2 ()]);
1530 }
dbbb1059
AB
1531 else
1532 {
78a3b0fa
AB
1533 end_prologue_addr = cur_pc;
1534 break;
dbbb1059
AB
1535 }
1536 }
1537
1538 if (end_prologue_addr == 0)
1539 end_prologue_addr = cur_pc;
1540
78a3b0fa
AB
1541 if (riscv_debug_unwinder)
1542 fprintf_unfiltered (gdb_stdlog, "End of prologue at %s\n",
1543 core_addr_to_string (end_prologue_addr));
1544
1545 if (cache != NULL)
1546 {
1547 /* Figure out if it is a frame pointer or just a stack pointer. Also
1548 the offset held in the pv_t is from the original register value to
1549 the current value, which for a grows down stack means a negative
1550 value. The FRAME_BASE_OFFSET is the negation of this, how to get
1551 from the current value to the original value. */
1552 if (pv_is_register (regs[RISCV_FP_REGNUM], RISCV_SP_REGNUM))
1553 {
1554 cache->frame_base_reg = RISCV_FP_REGNUM;
1555 cache->frame_base_offset = -regs[RISCV_FP_REGNUM].k;
1556 }
1557 else
1558 {
1559 cache->frame_base_reg = RISCV_SP_REGNUM;
1560 cache->frame_base_offset = -regs[RISCV_SP_REGNUM].k;
1561 }
1562
1563 /* Assign offset from old SP to all saved registers. As we don't
1564 have the previous value for the frame base register at this
1565 point, we store the offset as the address in the trad_frame, and
1566 then convert this to an actual address later. */
1567 for (int i = 0; i <= RISCV_NUM_INTEGER_REGS; i++)
1568 {
1569 CORE_ADDR offset;
1570 if (stack.find_reg (gdbarch, i, &offset))
1571 {
1572 if (riscv_debug_unwinder)
a96bd1cc
AB
1573 {
1574 /* Display OFFSET as a signed value, the offsets are from
1575 the frame base address to the registers location on
1576 the stack, with a descending stack this means the
1577 offsets are always negative. */
1578 fprintf_unfiltered (gdb_stdlog,
1579 "Register $%s at stack offset %s\n",
1580 gdbarch_register_name (gdbarch, i),
1581 plongest ((LONGEST) offset));
1582 }
78a3b0fa
AB
1583 trad_frame_set_addr (cache->regs, i, offset);
1584 }
1585 }
1586 }
1587
dbbb1059
AB
1588 return end_prologue_addr;
1589}
1590
1591/* Implement the riscv_skip_prologue gdbarch method. */
1592
1593static CORE_ADDR
78a3b0fa 1594riscv_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
dbbb1059 1595{
dbbb1059
AB
1596 CORE_ADDR func_addr;
1597
1598 /* See if we can determine the end of the prologue via the symbol
1599 table. If so, then return either PC, or the PC after the
1600 prologue, whichever is greater. */
1601 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1602 {
1603 CORE_ADDR post_prologue_pc
1604 = skip_prologue_using_sal (gdbarch, func_addr);
1605
1606 if (post_prologue_pc != 0)
1607 return std::max (pc, post_prologue_pc);
1608 }
1609
1610 /* Can't determine prologue from the symbol table, need to examine
78a3b0fa
AB
1611 instructions. Pass -1 for the end address to indicate the prologue
1612 scanner can scan as far as it needs to find the end of the prologue. */
1613 return riscv_scan_prologue (gdbarch, pc, ((CORE_ADDR) -1), NULL);
dbbb1059
AB
1614}
1615
1616/* Implement the gdbarch push dummy code callback. */
1617
1618static CORE_ADDR
1619riscv_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
1620 CORE_ADDR funaddr, struct value **args, int nargs,
1621 struct type *value_type, CORE_ADDR *real_pc,
1622 CORE_ADDR *bp_addr, struct regcache *regcache)
1623{
1624 /* Allocate space for a breakpoint, and keep the stack correctly
1625 aligned. */
1626 sp -= 16;
1627 *bp_addr = sp;
1628 *real_pc = funaddr;
1629 return sp;
1630}
1631
a9158a86
AB
1632/* Implement the gdbarch type alignment method, overrides the generic
1633 alignment algorithm for anything that is RISC-V specific. */
dbbb1059 1634
a9158a86
AB
1635static ULONGEST
1636riscv_type_align (gdbarch *gdbarch, type *type)
dbbb1059 1637{
a9158a86
AB
1638 type = check_typedef (type);
1639 if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1640 return std::min (TYPE_LENGTH (type), (ULONGEST) BIGGEST_ALIGNMENT);
dbbb1059 1641
a9158a86
AB
1642 /* Anything else will be aligned by the generic code. */
1643 return 0;
dbbb1059
AB
1644}
1645
1646/* Holds information about a single argument either being passed to an
1647 inferior function, or returned from an inferior function. This includes
1648 information about the size, type, etc of the argument, and also
1649 information about how the argument will be passed (or returned). */
1650
1651struct riscv_arg_info
1652{
1653 /* Contents of the argument. */
1654 const gdb_byte *contents;
1655
1656 /* Length of argument. */
1657 int length;
1658
1659 /* Alignment required for an argument of this type. */
1660 int align;
1661
1662 /* The type for this argument. */
1663 struct type *type;
1664
1665 /* Each argument can have either 1 or 2 locations assigned to it. Each
1666 location describes where part of the argument will be placed. The
1667 second location is valid based on the LOC_TYPE and C_LENGTH fields
1668 of the first location (which is always valid). */
1669 struct location
1670 {
1671 /* What type of location this is. */
1672 enum location_type
1673 {
1674 /* Argument passed in a register. */
1675 in_reg,
1676
1677 /* Argument passed as an on stack argument. */
1678 on_stack,
1679
1680 /* Argument passed by reference. The second location is always
1681 valid for a BY_REF argument, and describes where the address
1682 of the BY_REF argument should be placed. */
1683 by_ref
1684 } loc_type;
1685
1686 /* Information that depends on the location type. */
1687 union
1688 {
1689 /* Which register number to use. */
1690 int regno;
1691
1692 /* The offset into the stack region. */
1693 int offset;
1694 } loc_data;
1695
1696 /* The length of contents covered by this location. If this is less
1697 than the total length of the argument, then the second location
1698 will be valid, and will describe where the rest of the argument
1699 will go. */
1700 int c_length;
1701
1702 /* The offset within CONTENTS for this part of the argument. Will
1703 always be 0 for the first part. For the second part of the
1704 argument, this might be the C_LENGTH value of the first part,
1705 however, if we are passing a structure in two registers, and there's
1706 is padding between the first and second field, then this offset
1707 might be greater than the length of the first argument part. When
1708 the second argument location is not holding part of the argument
1709 value, but is instead holding the address of a reference argument,
1710 then this offset will be set to 0. */
1711 int c_offset;
1712 } argloc[2];
8b2d40cb
JW
1713
1714 /* TRUE if this is an unnamed argument. */
1715 bool is_unnamed;
dbbb1059
AB
1716};
1717
1718/* Information about a set of registers being used for passing arguments as
1719 part of a function call. The register set must be numerically
1720 sequential from NEXT_REGNUM to LAST_REGNUM. The register set can be
1721 disabled from use by setting NEXT_REGNUM greater than LAST_REGNUM. */
1722
1723struct riscv_arg_reg
1724{
1725 riscv_arg_reg (int first, int last)
1726 : next_regnum (first),
1727 last_regnum (last)
1728 {
1729 /* Nothing. */
1730 }
1731
1732 /* The GDB register number to use in this set. */
1733 int next_regnum;
1734
1735 /* The last GDB register number to use in this set. */
1736 int last_regnum;
1737};
1738
1739/* Arguments can be passed as on stack arguments, or by reference. The
1740 on stack arguments must be in a continuous region starting from $sp,
1741 while the by reference arguments can be anywhere, but we'll put them
1742 on the stack after (at higher address) the on stack arguments.
1743
1744 This might not be the right approach to take. The ABI is clear that
1745 an argument passed by reference can be modified by the callee, which
1746 us placing the argument (temporarily) onto the stack will not achieve
1747 (changes will be lost). There's also the possibility that very large
1748 arguments could overflow the stack.
1749
1750 This struct is used to track offset into these two areas for where
1751 arguments are to be placed. */
1752struct riscv_memory_offsets
1753{
1754 riscv_memory_offsets ()
1755 : arg_offset (0),
1756 ref_offset (0)
1757 {
1758 /* Nothing. */
1759 }
1760
1761 /* Offset into on stack argument area. */
1762 int arg_offset;
1763
1764 /* Offset into the pass by reference area. */
1765 int ref_offset;
1766};
1767
1768/* Holds information about where arguments to a call will be placed. This
1769 is updated as arguments are added onto the call, and can be used to
1770 figure out where the next argument should be placed. */
1771
1772struct riscv_call_info
1773{
1774 riscv_call_info (struct gdbarch *gdbarch)
1775 : int_regs (RISCV_A0_REGNUM, RISCV_A0_REGNUM + 7),
1776 float_regs (RISCV_FA0_REGNUM, RISCV_FA0_REGNUM + 7)
1777 {
113b7b81
AB
1778 xlen = riscv_abi_xlen (gdbarch);
1779 flen = riscv_abi_flen (gdbarch);
dbbb1059
AB
1780
1781 /* Disable use of floating point registers if needed. */
1782 if (!riscv_has_fp_abi (gdbarch))
1783 float_regs.next_regnum = float_regs.last_regnum + 1;
1784 }
1785
1786 /* Track the memory areas used for holding in-memory arguments to a
1787 call. */
1788 struct riscv_memory_offsets memory;
1789
1790 /* Holds information about the next integer register to use for passing
1791 an argument. */
1792 struct riscv_arg_reg int_regs;
1793
1794 /* Holds information about the next floating point register to use for
1795 passing an argument. */
1796 struct riscv_arg_reg float_regs;
1797
1798 /* The XLEN and FLEN are copied in to this structure for convenience, and
113b7b81 1799 are just the results of calling RISCV_ABI_XLEN and RISCV_ABI_FLEN. */
dbbb1059
AB
1800 int xlen;
1801 int flen;
1802};
1803
1804/* Return the number of registers available for use as parameters in the
1805 register set REG. Returned value can be 0 or more. */
1806
1807static int
1808riscv_arg_regs_available (struct riscv_arg_reg *reg)
1809{
1810 if (reg->next_regnum > reg->last_regnum)
1811 return 0;
1812
1813 return (reg->last_regnum - reg->next_regnum + 1);
1814}
1815
1816/* If there is at least one register available in the register set REG then
1817 the next register from REG is assigned to LOC and the length field of
1818 LOC is updated to LENGTH. The register set REG is updated to indicate
1819 that the assigned register is no longer available and the function
1820 returns true.
1821
1822 If there are no registers available in REG then the function returns
1823 false, and LOC and REG are unchanged. */
1824
1825static bool
1826riscv_assign_reg_location (struct riscv_arg_info::location *loc,
1827 struct riscv_arg_reg *reg,
1828 int length, int offset)
1829{
1830 if (reg->next_regnum <= reg->last_regnum)
1831 {
1832 loc->loc_type = riscv_arg_info::location::in_reg;
1833 loc->loc_data.regno = reg->next_regnum;
1834 reg->next_regnum++;
1835 loc->c_length = length;
1836 loc->c_offset = offset;
1837 return true;
1838 }
1839
1840 return false;
1841}
1842
1843/* Assign LOC a location as the next stack parameter, and update MEMORY to
1844 record that an area of stack has been used to hold the parameter
1845 described by LOC.
1846
1847 The length field of LOC is updated to LENGTH, the length of the
1848 parameter being stored, and ALIGN is the alignment required by the
1849 parameter, which will affect how memory is allocated out of MEMORY. */
1850
1851static void
1852riscv_assign_stack_location (struct riscv_arg_info::location *loc,
1853 struct riscv_memory_offsets *memory,
1854 int length, int align)
1855{
1856 loc->loc_type = riscv_arg_info::location::on_stack;
1857 memory->arg_offset
1858 = align_up (memory->arg_offset, align);
1859 loc->loc_data.offset = memory->arg_offset;
1860 memory->arg_offset += length;
1861 loc->c_length = length;
1862
1863 /* Offset is always 0, either we're the first location part, in which
1864 case we're reading content from the start of the argument, or we're
1865 passing the address of a reference argument, so 0. */
1866 loc->c_offset = 0;
1867}
1868
1869/* Update AINFO, which describes an argument that should be passed or
1870 returned using the integer ABI. The argloc fields within AINFO are
1871 updated to describe the location in which the argument will be passed to
1872 a function, or returned from a function.
1873
1874 The CINFO structure contains the ongoing call information, the holds
1875 information such as which argument registers are remaining to be
1876 assigned to parameter, and how much memory has been used by parameters
1877 so far.
1878
1879 By examining the state of CINFO a suitable location can be selected,
1880 and assigned to AINFO. */
1881
1882static void
1883riscv_call_arg_scalar_int (struct riscv_arg_info *ainfo,
1884 struct riscv_call_info *cinfo)
1885{
1886 if (ainfo->length > (2 * cinfo->xlen))
1887 {
1888 /* Argument is going to be passed by reference. */
1889 ainfo->argloc[0].loc_type
1890 = riscv_arg_info::location::by_ref;
1891 cinfo->memory.ref_offset
1892 = align_up (cinfo->memory.ref_offset, ainfo->align);
1893 ainfo->argloc[0].loc_data.offset = cinfo->memory.ref_offset;
1894 cinfo->memory.ref_offset += ainfo->length;
1895 ainfo->argloc[0].c_length = ainfo->length;
1896
1897 /* The second location for this argument is given over to holding the
1898 address of the by-reference data. Pass 0 for the offset as this
1899 is not part of the actual argument value. */
1900 if (!riscv_assign_reg_location (&ainfo->argloc[1],
1901 &cinfo->int_regs,
1902 cinfo->xlen, 0))
1903 riscv_assign_stack_location (&ainfo->argloc[1],
1904 &cinfo->memory, cinfo->xlen,
1905 cinfo->xlen);
1906 }
1907 else
1908 {
174f8ac8
JW
1909 int len = std::min (ainfo->length, cinfo->xlen);
1910 int align = std::max (ainfo->align, cinfo->xlen);
dbbb1059 1911
8b2d40cb
JW
1912 /* Unnamed arguments in registers that require 2*XLEN alignment are
1913 passed in an aligned register pair. */
1914 if (ainfo->is_unnamed && (align == cinfo->xlen * 2)
1915 && cinfo->int_regs.next_regnum & 1)
1916 cinfo->int_regs.next_regnum++;
1917
dbbb1059
AB
1918 if (!riscv_assign_reg_location (&ainfo->argloc[0],
1919 &cinfo->int_regs, len, 0))
1920 riscv_assign_stack_location (&ainfo->argloc[0],
174f8ac8 1921 &cinfo->memory, len, align);
dbbb1059
AB
1922
1923 if (len < ainfo->length)
1924 {
1925 len = ainfo->length - len;
1926 if (!riscv_assign_reg_location (&ainfo->argloc[1],
1927 &cinfo->int_regs, len,
1928 cinfo->xlen))
1929 riscv_assign_stack_location (&ainfo->argloc[1],
1930 &cinfo->memory, len, cinfo->xlen);
1931 }
1932 }
1933}
1934
1935/* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
1936 is being passed with the floating point ABI. */
1937
1938static void
1939riscv_call_arg_scalar_float (struct riscv_arg_info *ainfo,
1940 struct riscv_call_info *cinfo)
1941{
4de3d8d0 1942 if (ainfo->length > cinfo->flen || ainfo->is_unnamed)
dbbb1059
AB
1943 return riscv_call_arg_scalar_int (ainfo, cinfo);
1944 else
1945 {
1946 if (!riscv_assign_reg_location (&ainfo->argloc[0],
1947 &cinfo->float_regs,
1948 ainfo->length, 0))
1949 return riscv_call_arg_scalar_int (ainfo, cinfo);
1950 }
1951}
1952
1953/* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
1954 is a complex floating point argument, and is therefore handled
1955 differently to other argument types. */
1956
1957static void
1958riscv_call_arg_complex_float (struct riscv_arg_info *ainfo,
1959 struct riscv_call_info *cinfo)
1960{
1961 if (ainfo->length <= (2 * cinfo->flen)
4de3d8d0
AB
1962 && riscv_arg_regs_available (&cinfo->float_regs) >= 2
1963 && !ainfo->is_unnamed)
dbbb1059
AB
1964 {
1965 bool result;
1966 int len = ainfo->length / 2;
1967
1968 result = riscv_assign_reg_location (&ainfo->argloc[0],
9f0272f8 1969 &cinfo->float_regs, len, 0);
dbbb1059
AB
1970 gdb_assert (result);
1971
1972 result = riscv_assign_reg_location (&ainfo->argloc[1],
1973 &cinfo->float_regs, len, len);
1974 gdb_assert (result);
1975 }
1976 else
1977 return riscv_call_arg_scalar_int (ainfo, cinfo);
1978}
1979
1980/* A structure used for holding information about a structure type within
1981 the inferior program. The RiscV ABI has special rules for handling some
1982 structures with a single field or with two fields. The counting of
1983 fields here is done after flattening out all nested structures. */
1984
1985class riscv_struct_info
1986{
1987public:
1988 riscv_struct_info ()
1989 : m_number_of_fields (0),
9f0272f8
AB
1990 m_types { nullptr, nullptr },
1991 m_offsets { 0, 0 }
dbbb1059
AB
1992 {
1993 /* Nothing. */
1994 }
1995
1996 /* Analyse TYPE descending into nested structures, count the number of
1997 scalar fields and record the types of the first two fields found. */
9f0272f8
AB
1998 void analyse (struct type *type)
1999 {
2000 analyse_inner (type, 0);
2001 }
dbbb1059
AB
2002
2003 /* The number of scalar fields found in the analysed type. This is
2004 currently only accurate if the value returned is 0, 1, or 2 as the
2005 analysis stops counting when the number of fields is 3. This is
2006 because the RiscV ABI only has special cases for 1 or 2 fields,
2007 anything else we just don't care about. */
2008 int number_of_fields () const
2009 { return m_number_of_fields; }
2010
2011 /* Return the type for scalar field INDEX within the analysed type. Will
2012 return nullptr if there is no field at that index. Only INDEX values
2013 0 and 1 can be requested as the RiscV ABI only has special cases for
2014 structures with 1 or 2 fields. */
2015 struct type *field_type (int index) const
2016 {
2017 gdb_assert (index < (sizeof (m_types) / sizeof (m_types[0])));
2018 return m_types[index];
2019 }
2020
9f0272f8
AB
2021 /* Return the offset of scalar field INDEX within the analysed type. Will
2022 return 0 if there is no field at that index. Only INDEX values 0 and
2023 1 can be requested as the RiscV ABI only has special cases for
2024 structures with 1 or 2 fields. */
2025 int field_offset (int index) const
2026 {
2027 gdb_assert (index < (sizeof (m_offsets) / sizeof (m_offsets[0])));
2028 return m_offsets[index];
2029 }
2030
dbbb1059
AB
2031private:
2032 /* The number of scalar fields found within the structure after recursing
2033 into nested structures. */
2034 int m_number_of_fields;
2035
2036 /* The types of the first two scalar fields found within the structure
2037 after recursing into nested structures. */
2038 struct type *m_types[2];
9f0272f8
AB
2039
2040 /* The offsets of the first two scalar fields found within the structure
2041 after recursing into nested structures. */
2042 int m_offsets[2];
2043
2044 /* Recursive core for ANALYSE, the OFFSET parameter tracks the byte
2045 offset from the start of the top level structure being analysed. */
2046 void analyse_inner (struct type *type, int offset);
dbbb1059
AB
2047};
2048
9f0272f8 2049/* See description in class declaration. */
dbbb1059
AB
2050
2051void
9f0272f8 2052riscv_struct_info::analyse_inner (struct type *type, int offset)
dbbb1059
AB
2053{
2054 unsigned int count = TYPE_NFIELDS (type);
2055 unsigned int i;
2056
2057 for (i = 0; i < count; ++i)
2058 {
2059 if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_BITPOS)
2060 continue;
2061
2062 struct type *field_type = TYPE_FIELD_TYPE (type, i);
2063 field_type = check_typedef (field_type);
9f0272f8
AB
2064 int field_offset
2065 = offset + TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT;
dbbb1059
AB
2066
2067 switch (TYPE_CODE (field_type))
2068 {
2069 case TYPE_CODE_STRUCT:
9f0272f8 2070 analyse_inner (field_type, field_offset);
dbbb1059
AB
2071 break;
2072
2073 default:
2074 /* RiscV only flattens out structures. Anything else does not
2075 need to be flattened, we just record the type, and when we
2076 look at the analysis results we'll realise this is not a
2077 structure we can special case, and pass the structure in
2078 memory. */
2079 if (m_number_of_fields < 2)
9f0272f8
AB
2080 {
2081 m_types[m_number_of_fields] = field_type;
2082 m_offsets[m_number_of_fields] = field_offset;
2083 }
dbbb1059
AB
2084 m_number_of_fields++;
2085 break;
2086 }
2087
2088 /* RiscV only has special handling for structures with 1 or 2 scalar
2089 fields, any more than that and the structure is just passed in
2090 memory. We can safely drop out early when we find 3 or more
2091 fields then. */
2092
2093 if (m_number_of_fields > 2)
2094 return;
2095 }
2096}
2097
2098/* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
2099 is a structure. Small structures on RiscV have some special case
2100 handling in order that the structure might be passed in register.
2101 Larger structures are passed in memory. After assigning location
2102 information to AINFO, CINFO will have been updated. */
2103
2104static void
2105riscv_call_arg_struct (struct riscv_arg_info *ainfo,
2106 struct riscv_call_info *cinfo)
2107{
2108 if (riscv_arg_regs_available (&cinfo->float_regs) >= 1)
2109 {
2110 struct riscv_struct_info sinfo;
2111
2112 sinfo.analyse (ainfo->type);
2113 if (sinfo.number_of_fields () == 1
2114 && TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_COMPLEX)
2115 {
9f0272f8
AB
2116 /* The following is similar to RISCV_CALL_ARG_COMPLEX_FLOAT,
2117 except we use the type of the complex field instead of the
2118 type from AINFO, and the first location might be at a non-zero
2119 offset. */
2120 if (TYPE_LENGTH (sinfo.field_type (0)) <= (2 * cinfo->flen)
2121 && riscv_arg_regs_available (&cinfo->float_regs) >= 2
2122 && !ainfo->is_unnamed)
2123 {
2124 bool result;
2125 int len = TYPE_LENGTH (sinfo.field_type (0)) / 2;
2126 int offset = sinfo.field_offset (0);
2127
2128 result = riscv_assign_reg_location (&ainfo->argloc[0],
2129 &cinfo->float_regs, len,
2130 offset);
2131 gdb_assert (result);
2132
2133 result = riscv_assign_reg_location (&ainfo->argloc[1],
2134 &cinfo->float_regs, len,
2135 (offset + len));
2136 gdb_assert (result);
2137 }
2138 else
2139 riscv_call_arg_scalar_int (ainfo, cinfo);
2140 return;
dbbb1059
AB
2141 }
2142
2143 if (sinfo.number_of_fields () == 1
2144 && TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_FLT)
2145 {
9f0272f8
AB
2146 /* The following is similar to RISCV_CALL_ARG_SCALAR_FLOAT,
2147 except we use the type of the first scalar field instead of
2148 the type from AINFO. Also the location might be at a non-zero
2149 offset. */
2150 if (TYPE_LENGTH (sinfo.field_type (0)) > cinfo->flen
2151 || ainfo->is_unnamed)
2152 riscv_call_arg_scalar_int (ainfo, cinfo);
2153 else
2154 {
2155 int offset = sinfo.field_offset (0);
2156 int len = TYPE_LENGTH (sinfo.field_type (0));
2157
2158 if (!riscv_assign_reg_location (&ainfo->argloc[0],
2159 &cinfo->float_regs,
2160 len, offset))
2161 riscv_call_arg_scalar_int (ainfo, cinfo);
2162 }
2163 return;
dbbb1059
AB
2164 }
2165
2166 if (sinfo.number_of_fields () == 2
2167 && TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_FLT
2168 && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->flen
2169 && TYPE_CODE (sinfo.field_type (1)) == TYPE_CODE_FLT
2170 && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->flen
2171 && riscv_arg_regs_available (&cinfo->float_regs) >= 2)
2172 {
9f0272f8
AB
2173 int len0 = TYPE_LENGTH (sinfo.field_type (0));
2174 int offset = sinfo.field_offset (0);
dbbb1059 2175 if (!riscv_assign_reg_location (&ainfo->argloc[0],
9f0272f8 2176 &cinfo->float_regs, len0, offset))
dbbb1059
AB
2177 error (_("failed during argument setup"));
2178
9f0272f8
AB
2179 int len1 = TYPE_LENGTH (sinfo.field_type (1));
2180 offset = sinfo.field_offset (1);
dbbb1059
AB
2181 gdb_assert (len1 <= (TYPE_LENGTH (ainfo->type)
2182 - TYPE_LENGTH (sinfo.field_type (0))));
2183
2184 if (!riscv_assign_reg_location (&ainfo->argloc[1],
2185 &cinfo->float_regs,
2186 len1, offset))
2187 error (_("failed during argument setup"));
2188 return;
2189 }
2190
2191 if (sinfo.number_of_fields () == 2
2192 && riscv_arg_regs_available (&cinfo->int_regs) >= 1
2193 && (TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_FLT
2194 && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->flen
2195 && is_integral_type (sinfo.field_type (1))
2196 && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->xlen))
2197 {
9f0272f8
AB
2198 int len0 = TYPE_LENGTH (sinfo.field_type (0));
2199 int offset = sinfo.field_offset (0);
dbbb1059 2200 if (!riscv_assign_reg_location (&ainfo->argloc[0],
9f0272f8 2201 &cinfo->float_regs, len0, offset))
dbbb1059
AB
2202 error (_("failed during argument setup"));
2203
9f0272f8
AB
2204 int len1 = TYPE_LENGTH (sinfo.field_type (1));
2205 offset = sinfo.field_offset (1);
dbbb1059
AB
2206 gdb_assert (len1 <= cinfo->xlen);
2207 if (!riscv_assign_reg_location (&ainfo->argloc[1],
2208 &cinfo->int_regs, len1, offset))
2209 error (_("failed during argument setup"));
2210 return;
2211 }
2212
2213 if (sinfo.number_of_fields () == 2
2214 && riscv_arg_regs_available (&cinfo->int_regs) >= 1
2215 && (is_integral_type (sinfo.field_type (0))
2216 && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->xlen
2217 && TYPE_CODE (sinfo.field_type (1)) == TYPE_CODE_FLT
2218 && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->flen))
2219 {
9f0272f8
AB
2220 int len0 = TYPE_LENGTH (sinfo.field_type (0));
2221 int len1 = TYPE_LENGTH (sinfo.field_type (1));
dbbb1059
AB
2222
2223 gdb_assert (len0 <= cinfo->xlen);
2224 gdb_assert (len1 <= cinfo->flen);
2225
9f0272f8 2226 int offset = sinfo.field_offset (0);
dbbb1059 2227 if (!riscv_assign_reg_location (&ainfo->argloc[0],
9f0272f8 2228 &cinfo->int_regs, len0, offset))
dbbb1059
AB
2229 error (_("failed during argument setup"));
2230
9f0272f8 2231 offset = sinfo.field_offset (1);
dbbb1059
AB
2232 if (!riscv_assign_reg_location (&ainfo->argloc[1],
2233 &cinfo->float_regs,
2234 len1, offset))
2235 error (_("failed during argument setup"));
2236
2237 return;
2238 }
2239 }
2240
2241 /* Non of the structure flattening cases apply, so we just pass using
2242 the integer ABI. */
dbbb1059
AB
2243 riscv_call_arg_scalar_int (ainfo, cinfo);
2244}
2245
2246/* Assign a location to call (or return) argument AINFO, the location is
2247 selected from CINFO which holds information about what call argument
2248 locations are available for use next. The TYPE is the type of the
2249 argument being passed, this information is recorded into AINFO (along
8b2d40cb
JW
2250 with some additional information derived from the type). IS_UNNAMED
2251 is true if this is an unnamed (stdarg) argument, this info is also
2252 recorded into AINFO.
dbbb1059
AB
2253
2254 After assigning a location to AINFO, CINFO will have been updated. */
2255
2256static void
2257riscv_arg_location (struct gdbarch *gdbarch,
2258 struct riscv_arg_info *ainfo,
2259 struct riscv_call_info *cinfo,
8b2d40cb 2260 struct type *type, bool is_unnamed)
dbbb1059
AB
2261{
2262 ainfo->type = type;
2263 ainfo->length = TYPE_LENGTH (ainfo->type);
a9158a86 2264 ainfo->align = type_align (ainfo->type);
8b2d40cb 2265 ainfo->is_unnamed = is_unnamed;
dbbb1059 2266 ainfo->contents = nullptr;
9f0272f8
AB
2267 ainfo->argloc[0].c_length = 0;
2268 ainfo->argloc[1].c_length = 0;
dbbb1059
AB
2269
2270 switch (TYPE_CODE (ainfo->type))
2271 {
2272 case TYPE_CODE_INT:
2273 case TYPE_CODE_BOOL:
2274 case TYPE_CODE_CHAR:
2275 case TYPE_CODE_RANGE:
2276 case TYPE_CODE_ENUM:
2277 case TYPE_CODE_PTR:
2278 if (ainfo->length <= cinfo->xlen)
2279 {
2280 ainfo->type = builtin_type (gdbarch)->builtin_long;
2281 ainfo->length = cinfo->xlen;
2282 }
2283 else if (ainfo->length <= (2 * cinfo->xlen))
2284 {
2285 ainfo->type = builtin_type (gdbarch)->builtin_long_long;
2286 ainfo->length = 2 * cinfo->xlen;
2287 }
2288
2289 /* Recalculate the alignment requirement. */
a9158a86 2290 ainfo->align = type_align (ainfo->type);
dbbb1059
AB
2291 riscv_call_arg_scalar_int (ainfo, cinfo);
2292 break;
2293
2294 case TYPE_CODE_FLT:
2295 riscv_call_arg_scalar_float (ainfo, cinfo);
2296 break;
2297
2298 case TYPE_CODE_COMPLEX:
2299 riscv_call_arg_complex_float (ainfo, cinfo);
2300 break;
2301
2302 case TYPE_CODE_STRUCT:
2303 riscv_call_arg_struct (ainfo, cinfo);
2304 break;
2305
2306 default:
2307 riscv_call_arg_scalar_int (ainfo, cinfo);
2308 break;
2309 }
2310}
2311
cab5bb9d
AB
2312/* Used for printing debug information about the call argument location in
2313 INFO to STREAM. The addresses in SP_REFS and SP_ARGS are the base
2314 addresses for the location of pass-by-reference and
2315 arguments-on-the-stack memory areas. */
2316
dbbb1059 2317static void
cab5bb9d 2318riscv_print_arg_location (ui_file *stream, struct gdbarch *gdbarch,
dbbb1059
AB
2319 struct riscv_arg_info *info,
2320 CORE_ADDR sp_refs, CORE_ADDR sp_args)
2321{
cab5bb9d 2322 fprintf_unfiltered (stream, "type: '%s', length: 0x%x, alignment: 0x%x",
42ecac17 2323 TYPE_SAFE_NAME (info->type), info->length, info->align);
dbbb1059
AB
2324 switch (info->argloc[0].loc_type)
2325 {
2326 case riscv_arg_info::location::in_reg:
cab5bb9d
AB
2327 fprintf_unfiltered
2328 (stream, ", register %s",
2329 gdbarch_register_name (gdbarch, info->argloc[0].loc_data.regno));
dbbb1059
AB
2330 if (info->argloc[0].c_length < info->length)
2331 {
2332 switch (info->argloc[1].loc_type)
2333 {
2334 case riscv_arg_info::location::in_reg:
cab5bb9d
AB
2335 fprintf_unfiltered
2336 (stream, ", register %s",
2337 gdbarch_register_name (gdbarch,
2338 info->argloc[1].loc_data.regno));
dbbb1059
AB
2339 break;
2340
2341 case riscv_arg_info::location::on_stack:
cab5bb9d
AB
2342 fprintf_unfiltered (stream, ", on stack at offset 0x%x",
2343 info->argloc[1].loc_data.offset);
dbbb1059
AB
2344 break;
2345
2346 case riscv_arg_info::location::by_ref:
2347 default:
2348 /* The second location should never be a reference, any
2349 argument being passed by reference just places its address
2350 in the first location and is done. */
2351 error (_("invalid argument location"));
2352 break;
2353 }
2354
2355 if (info->argloc[1].c_offset > info->argloc[0].c_length)
cab5bb9d
AB
2356 fprintf_unfiltered (stream, " (offset 0x%x)",
2357 info->argloc[1].c_offset);
dbbb1059
AB
2358 }
2359 break;
2360
2361 case riscv_arg_info::location::on_stack:
cab5bb9d
AB
2362 fprintf_unfiltered (stream, ", on stack at offset 0x%x",
2363 info->argloc[0].loc_data.offset);
dbbb1059
AB
2364 break;
2365
2366 case riscv_arg_info::location::by_ref:
cab5bb9d
AB
2367 fprintf_unfiltered
2368 (stream, ", by reference, data at offset 0x%x (%s)",
2369 info->argloc[0].loc_data.offset,
2370 core_addr_to_string (sp_refs + info->argloc[0].loc_data.offset));
dbbb1059
AB
2371 if (info->argloc[1].loc_type
2372 == riscv_arg_info::location::in_reg)
cab5bb9d
AB
2373 fprintf_unfiltered
2374 (stream, ", address in register %s",
2375 gdbarch_register_name (gdbarch, info->argloc[1].loc_data.regno));
dbbb1059
AB
2376 else
2377 {
2378 gdb_assert (info->argloc[1].loc_type
2379 == riscv_arg_info::location::on_stack);
cab5bb9d
AB
2380 fprintf_unfiltered
2381 (stream, ", address on stack at offset 0x%x (%s)",
2382 info->argloc[1].loc_data.offset,
2383 core_addr_to_string (sp_args + info->argloc[1].loc_data.offset));
dbbb1059
AB
2384 }
2385 break;
2386
2387 default:
89a3b63e 2388 gdb_assert_not_reached (_("unknown argument location type"));
dbbb1059
AB
2389 }
2390}
2391
2392/* Implement the push dummy call gdbarch callback. */
2393
2394static CORE_ADDR
2395riscv_push_dummy_call (struct gdbarch *gdbarch,
2396 struct value *function,
2397 struct regcache *regcache,
2398 CORE_ADDR bp_addr,
2399 int nargs,
2400 struct value **args,
2401 CORE_ADDR sp,
cf84fa6b 2402 function_call_return_method return_method,
dbbb1059
AB
2403 CORE_ADDR struct_addr)
2404{
2405 int i;
2406 CORE_ADDR sp_args, sp_refs;
2407 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
dbbb1059
AB
2408
2409 struct riscv_arg_info *arg_info =
2410 (struct riscv_arg_info *) alloca (nargs * sizeof (struct riscv_arg_info));
dbbb1059
AB
2411
2412 struct riscv_call_info call_info (gdbarch);
2413
2414 CORE_ADDR osp = sp;
2415
8b2d40cb
JW
2416 struct type *ftype = check_typedef (value_type (function));
2417
2418 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
2419 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
2420
dbbb1059 2421 /* We'll use register $a0 if we're returning a struct. */
cf84fa6b 2422 if (return_method == return_method_struct)
dbbb1059
AB
2423 ++call_info.int_regs.next_regnum;
2424
b926417a 2425 for (i = 0; i < nargs; ++i)
dbbb1059
AB
2426 {
2427 struct value *arg_value;
2428 struct type *arg_type;
b926417a 2429 struct riscv_arg_info *info = &arg_info[i];
dbbb1059
AB
2430
2431 arg_value = args[i];
2432 arg_type = check_typedef (value_type (arg_value));
2433
8b2d40cb
JW
2434 riscv_arg_location (gdbarch, info, &call_info, arg_type,
2435 TYPE_VARARGS (ftype) && i >= TYPE_NFIELDS (ftype));
dbbb1059
AB
2436
2437 if (info->type != arg_type)
2438 arg_value = value_cast (info->type, arg_value);
2439 info->contents = value_contents (arg_value);
2440 }
2441
2442 /* Adjust the stack pointer and align it. */
2443 sp = sp_refs = align_down (sp - call_info.memory.ref_offset, SP_ALIGNMENT);
2444 sp = sp_args = align_down (sp - call_info.memory.arg_offset, SP_ALIGNMENT);
2445
2446 if (riscv_debug_infcall > 0)
2447 {
2448 fprintf_unfiltered (gdb_stdlog, "dummy call args:\n");
2449 fprintf_unfiltered (gdb_stdlog, ": floating point ABI %s in use\n",
2450 (riscv_has_fp_abi (gdbarch) ? "is" : "is not"));
2451 fprintf_unfiltered (gdb_stdlog, ": xlen: %d\n: flen: %d\n",
2452 call_info.xlen, call_info.flen);
cf84fa6b 2453 if (return_method == return_method_struct)
dbbb1059
AB
2454 fprintf_unfiltered (gdb_stdlog,
2455 "[*] struct return pointer in register $A0\n");
2456 for (i = 0; i < nargs; ++i)
2457 {
2458 struct riscv_arg_info *info = &arg_info [i];
2459
2460 fprintf_unfiltered (gdb_stdlog, "[%2d] ", i);
cab5bb9d 2461 riscv_print_arg_location (gdb_stdlog, gdbarch, info, sp_refs, sp_args);
dbbb1059
AB
2462 fprintf_unfiltered (gdb_stdlog, "\n");
2463 }
2464 if (call_info.memory.arg_offset > 0
2465 || call_info.memory.ref_offset > 0)
2466 {
cab5bb9d
AB
2467 fprintf_unfiltered (gdb_stdlog, " Original sp: %s\n",
2468 core_addr_to_string (osp));
dbbb1059 2469 fprintf_unfiltered (gdb_stdlog, "Stack required (for args): 0x%x\n",
cab5bb9d 2470 call_info.memory.arg_offset);
dbbb1059 2471 fprintf_unfiltered (gdb_stdlog, "Stack required (for refs): 0x%x\n",
cab5bb9d 2472 call_info.memory.ref_offset);
fb294655
AB
2473 fprintf_unfiltered (gdb_stdlog, " Stack allocated: %s\n",
2474 core_addr_to_string_nz (osp - sp));
dbbb1059
AB
2475 }
2476 }
2477
2478 /* Now load the argument into registers, or onto the stack. */
2479
cf84fa6b 2480 if (return_method == return_method_struct)
dbbb1059
AB
2481 {
2482 gdb_byte buf[sizeof (LONGEST)];
2483
2484 store_unsigned_integer (buf, call_info.xlen, byte_order, struct_addr);
b66f5587 2485 regcache->cooked_write (RISCV_A0_REGNUM, buf);
dbbb1059
AB
2486 }
2487
2488 for (i = 0; i < nargs; ++i)
2489 {
2490 CORE_ADDR dst;
2491 int second_arg_length = 0;
2492 const gdb_byte *second_arg_data;
2493 struct riscv_arg_info *info = &arg_info [i];
2494
2495 gdb_assert (info->length > 0);
2496
2497 switch (info->argloc[0].loc_type)
2498 {
2499 case riscv_arg_info::location::in_reg:
2500 {
2501 gdb_byte tmp [sizeof (ULONGEST)];
2502
2503 gdb_assert (info->argloc[0].c_length <= info->length);
3399f1b3
JW
2504 /* FP values in FP registers must be NaN-boxed. */
2505 if (riscv_is_fp_regno_p (info->argloc[0].loc_data.regno)
2506 && info->argloc[0].c_length < call_info.flen)
2507 memset (tmp, -1, sizeof (tmp));
2508 else
2509 memset (tmp, 0, sizeof (tmp));
9f0272f8
AB
2510 memcpy (tmp, (info->contents + info->argloc[0].c_offset),
2511 info->argloc[0].c_length);
b66f5587 2512 regcache->cooked_write (info->argloc[0].loc_data.regno, tmp);
dbbb1059 2513 second_arg_length =
9f0272f8 2514 (((info->argloc[0].c_length + info->argloc[0].c_offset) < info->length)
dbbb1059
AB
2515 ? info->argloc[1].c_length : 0);
2516 second_arg_data = info->contents + info->argloc[1].c_offset;
2517 }
2518 break;
2519
2520 case riscv_arg_info::location::on_stack:
2521 dst = sp_args + info->argloc[0].loc_data.offset;
2522 write_memory (dst, info->contents, info->length);
2523 second_arg_length = 0;
2524 break;
2525
2526 case riscv_arg_info::location::by_ref:
2527 dst = sp_refs + info->argloc[0].loc_data.offset;
2528 write_memory (dst, info->contents, info->length);
2529
2530 second_arg_length = call_info.xlen;
2531 second_arg_data = (gdb_byte *) &dst;
2532 break;
2533
2534 default:
89a3b63e 2535 gdb_assert_not_reached (_("unknown argument location type"));
dbbb1059
AB
2536 }
2537
2538 if (second_arg_length > 0)
2539 {
2540 switch (info->argloc[1].loc_type)
2541 {
2542 case riscv_arg_info::location::in_reg:
2543 {
2544 gdb_byte tmp [sizeof (ULONGEST)];
2545
8c49aa89
AB
2546 gdb_assert ((riscv_is_fp_regno_p (info->argloc[1].loc_data.regno)
2547 && second_arg_length <= call_info.flen)
2548 || second_arg_length <= call_info.xlen);
3399f1b3
JW
2549 /* FP values in FP registers must be NaN-boxed. */
2550 if (riscv_is_fp_regno_p (info->argloc[1].loc_data.regno)
2551 && second_arg_length < call_info.flen)
2552 memset (tmp, -1, sizeof (tmp));
2553 else
2554 memset (tmp, 0, sizeof (tmp));
dbbb1059 2555 memcpy (tmp, second_arg_data, second_arg_length);
b66f5587 2556 regcache->cooked_write (info->argloc[1].loc_data.regno, tmp);
dbbb1059
AB
2557 }
2558 break;
2559
2560 case riscv_arg_info::location::on_stack:
2561 {
2562 CORE_ADDR arg_addr;
2563
2564 arg_addr = sp_args + info->argloc[1].loc_data.offset;
2565 write_memory (arg_addr, second_arg_data, second_arg_length);
2566 break;
2567 }
2568
2569 case riscv_arg_info::location::by_ref:
2570 default:
2571 /* The second location should never be a reference, any
2572 argument being passed by reference just places its address
2573 in the first location and is done. */
2574 error (_("invalid argument location"));
2575 break;
2576 }
2577 }
2578 }
2579
2580 /* Set the dummy return value to bp_addr.
2581 A dummy breakpoint will be setup to execute the call. */
2582
2583 if (riscv_debug_infcall > 0)
cab5bb9d
AB
2584 fprintf_unfiltered (gdb_stdlog, ": writing $ra = %s\n",
2585 core_addr_to_string (bp_addr));
dbbb1059
AB
2586 regcache_cooked_write_unsigned (regcache, RISCV_RA_REGNUM, bp_addr);
2587
2588 /* Finally, update the stack pointer. */
2589
2590 if (riscv_debug_infcall > 0)
cab5bb9d
AB
2591 fprintf_unfiltered (gdb_stdlog, ": writing $sp = %s\n",
2592 core_addr_to_string (sp));
dbbb1059
AB
2593 regcache_cooked_write_unsigned (regcache, RISCV_SP_REGNUM, sp);
2594
2595 return sp;
2596}
2597
2598/* Implement the return_value gdbarch method. */
2599
2600static enum return_value_convention
2601riscv_return_value (struct gdbarch *gdbarch,
2602 struct value *function,
2603 struct type *type,
2604 struct regcache *regcache,
2605 gdb_byte *readbuf,
2606 const gdb_byte *writebuf)
2607{
dbbb1059
AB
2608 struct riscv_call_info call_info (gdbarch);
2609 struct riscv_arg_info info;
2610 struct type *arg_type;
2611
2612 arg_type = check_typedef (type);
8b2d40cb 2613 riscv_arg_location (gdbarch, &info, &call_info, arg_type, false);
dbbb1059
AB
2614
2615 if (riscv_debug_infcall > 0)
2616 {
2617 fprintf_unfiltered (gdb_stdlog, "riscv return value:\n");
2618 fprintf_unfiltered (gdb_stdlog, "[R] ");
cab5bb9d 2619 riscv_print_arg_location (gdb_stdlog, gdbarch, &info, 0, 0);
dbbb1059
AB
2620 fprintf_unfiltered (gdb_stdlog, "\n");
2621 }
2622
2623 if (readbuf != nullptr || writebuf != nullptr)
2624 {
74e3300d
AB
2625 unsigned int arg_len;
2626 struct value *abi_val;
2627 gdb_byte *old_readbuf = nullptr;
2628 int regnum;
2629
2630 /* We only do one thing at a time. */
2631 gdb_assert (readbuf == nullptr || writebuf == nullptr);
2632
2633 /* In some cases the argument is not returned as the declared type,
2634 and we need to cast to or from the ABI type in order to
2635 correctly access the argument. When writing to the machine we
2636 do the cast here, when reading from the machine the cast occurs
2637 later, after extracting the value. As the ABI type can be
2638 larger than the declared type, then the read or write buffers
2639 passed in might be too small. Here we ensure that we are using
2640 buffers of sufficient size. */
2641 if (writebuf != nullptr)
2642 {
2643 struct value *arg_val = value_from_contents (arg_type, writebuf);
2644 abi_val = value_cast (info.type, arg_val);
2645 writebuf = value_contents_raw (abi_val);
2646 }
2647 else
2648 {
2649 abi_val = allocate_value (info.type);
2650 old_readbuf = readbuf;
2651 readbuf = value_contents_raw (abi_val);
2652 }
2653 arg_len = TYPE_LENGTH (info.type);
dbbb1059
AB
2654
2655 switch (info.argloc[0].loc_type)
2656 {
2657 /* Return value in register(s). */
2658 case riscv_arg_info::location::in_reg:
2659 {
2660 regnum = info.argloc[0].loc_data.regno;
74e3300d
AB
2661 gdb_assert (info.argloc[0].c_length <= arg_len);
2662 gdb_assert (info.argloc[0].c_length
2663 <= register_size (gdbarch, regnum));
dbbb1059
AB
2664
2665 if (readbuf)
9f0272f8
AB
2666 {
2667 gdb_byte *ptr = readbuf + info.argloc[0].c_offset;
2668 regcache->cooked_read_part (regnum, 0,
2669 info.argloc[0].c_length,
2670 ptr);
2671 }
dbbb1059
AB
2672
2673 if (writebuf)
9f0272f8
AB
2674 {
2675 const gdb_byte *ptr = writebuf + info.argloc[0].c_offset;
2676 regcache->cooked_write_part (regnum, 0,
2677 info.argloc[0].c_length,
2678 ptr);
2679 }
dbbb1059
AB
2680
2681 /* A return value in register can have a second part in a
2682 second register. */
9f0272f8 2683 if (info.argloc[1].c_length > 0)
dbbb1059
AB
2684 {
2685 switch (info.argloc[1].loc_type)
2686 {
2687 case riscv_arg_info::location::in_reg:
2688 regnum = info.argloc[1].loc_data.regno;
2689
74e3300d
AB
2690 gdb_assert ((info.argloc[0].c_length
2691 + info.argloc[1].c_length) <= arg_len);
2692 gdb_assert (info.argloc[1].c_length
2693 <= register_size (gdbarch, regnum));
2694
dbbb1059
AB
2695 if (readbuf)
2696 {
2697 readbuf += info.argloc[1].c_offset;
74e3300d
AB
2698 regcache->cooked_read_part (regnum, 0,
2699 info.argloc[1].c_length,
2700 readbuf);
dbbb1059
AB
2701 }
2702
2703 if (writebuf)
2704 {
2705 writebuf += info.argloc[1].c_offset;
74e3300d
AB
2706 regcache->cooked_write_part (regnum, 0,
2707 info.argloc[1].c_length,
2708 writebuf);
dbbb1059
AB
2709 }
2710 break;
2711
2712 case riscv_arg_info::location::by_ref:
2713 case riscv_arg_info::location::on_stack:
2714 default:
2715 error (_("invalid argument location"));
2716 break;
2717 }
2718 }
2719 }
2720 break;
2721
2722 /* Return value by reference will have its address in A0. */
2723 case riscv_arg_info::location::by_ref:
2724 {
b2970c23 2725 ULONGEST addr;
dbbb1059
AB
2726
2727 regcache_cooked_read_unsigned (regcache, RISCV_A0_REGNUM,
2728 &addr);
2729 if (readbuf != nullptr)
2730 read_memory (addr, readbuf, info.length);
2731 if (writebuf != nullptr)
2732 write_memory (addr, writebuf, info.length);
2733 }
2734 break;
2735
2736 case riscv_arg_info::location::on_stack:
2737 default:
2738 error (_("invalid argument location"));
2739 break;
2740 }
74e3300d
AB
2741
2742 /* This completes the cast from abi type back to the declared type
2743 in the case that we are reading from the machine. See the
2744 comment at the head of this block for more details. */
2745 if (readbuf != nullptr)
2746 {
2747 struct value *arg_val = value_cast (arg_type, abi_val);
2748 memcpy (old_readbuf, value_contents_raw (arg_val),
2749 TYPE_LENGTH (arg_type));
2750 }
dbbb1059
AB
2751 }
2752
2753 switch (info.argloc[0].loc_type)
2754 {
2755 case riscv_arg_info::location::in_reg:
2756 return RETURN_VALUE_REGISTER_CONVENTION;
2757 case riscv_arg_info::location::by_ref:
2758 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
2759 case riscv_arg_info::location::on_stack:
2760 default:
2761 error (_("invalid argument location"));
2762 }
2763}
2764
2765/* Implement the frame_align gdbarch method. */
2766
2767static CORE_ADDR
2768riscv_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2769{
2770 return align_down (addr, 16);
2771}
2772
dbbb1059
AB
2773/* Generate, or return the cached frame cache for the RiscV frame
2774 unwinder. */
2775
78a3b0fa 2776static struct riscv_unwind_cache *
dbbb1059
AB
2777riscv_frame_cache (struct frame_info *this_frame, void **this_cache)
2778{
78a3b0fa
AB
2779 CORE_ADDR pc, start_addr;
2780 struct riscv_unwind_cache *cache;
dbbb1059 2781 struct gdbarch *gdbarch = get_frame_arch (this_frame);
78a3b0fa 2782 int numregs, regno;
dbbb1059
AB
2783
2784 if ((*this_cache) != NULL)
78a3b0fa 2785 return (struct riscv_unwind_cache *) *this_cache;
dbbb1059 2786
78a3b0fa
AB
2787 cache = FRAME_OBSTACK_ZALLOC (struct riscv_unwind_cache);
2788 cache->regs = trad_frame_alloc_saved_regs (this_frame);
2789 (*this_cache) = cache;
dbbb1059 2790
78a3b0fa
AB
2791 /* Scan the prologue, filling in the cache. */
2792 start_addr = get_frame_func (this_frame);
dbbb1059 2793 pc = get_frame_pc (this_frame);
78a3b0fa
AB
2794 riscv_scan_prologue (gdbarch, start_addr, pc, cache);
2795
2796 /* We can now calculate the frame base address. */
2797 cache->frame_base
6c9d681b
AB
2798 = (get_frame_register_signed (this_frame, cache->frame_base_reg)
2799 + cache->frame_base_offset);
78a3b0fa
AB
2800 if (riscv_debug_unwinder)
2801 fprintf_unfiltered (gdb_stdlog, "Frame base is %s ($%s + 0x%x)\n",
2802 core_addr_to_string (cache->frame_base),
2803 gdbarch_register_name (gdbarch,
2804 cache->frame_base_reg),
2805 cache->frame_base_offset);
2806
2807 /* The prologue scanner sets the address of registers stored to the stack
2808 as the offset of that register from the frame base. The prologue
2809 scanner doesn't know the actual frame base value, and so is unable to
2810 compute the exact address. We do now know the frame base value, so
2811 update the address of registers stored to the stack. */
2812 numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
2813 for (regno = 0; regno < numregs; ++regno)
2814 {
2815 if (trad_frame_addr_p (cache->regs, regno))
2816 cache->regs[regno].addr += cache->frame_base;
2817 }
2818
2819 /* The previous $pc can be found wherever the $ra value can be found.
2820 The previous $ra value is gone, this would have been stored be the
2821 previous frame if required. */
2822 cache->regs[gdbarch_pc_regnum (gdbarch)] = cache->regs[RISCV_RA_REGNUM];
2823 trad_frame_set_unknown (cache->regs, RISCV_RA_REGNUM);
2824
2825 /* Build the frame id. */
2826 cache->this_id = frame_id_build (cache->frame_base, start_addr);
dbbb1059 2827
78a3b0fa
AB
2828 /* The previous $sp value is the frame base value. */
2829 trad_frame_set_value (cache->regs, gdbarch_sp_regnum (gdbarch),
2830 cache->frame_base);
dbbb1059 2831
78a3b0fa 2832 return cache;
dbbb1059
AB
2833}
2834
2835/* Implement the this_id callback for RiscV frame unwinder. */
2836
2837static void
2838riscv_frame_this_id (struct frame_info *this_frame,
2839 void **prologue_cache,
2840 struct frame_id *this_id)
2841{
78a3b0fa 2842 struct riscv_unwind_cache *cache;
dbbb1059 2843
a70b8144 2844 try
17cf2897
AB
2845 {
2846 cache = riscv_frame_cache (this_frame, prologue_cache);
2847 *this_id = cache->this_id;
2848 }
230d2906 2849 catch (const gdb_exception_error &ex)
17cf2897
AB
2850 {
2851 /* Ignore errors, this leaves the frame id as the predefined outer
2852 frame id which terminates the backtrace at this point. */
2853 }
dbbb1059
AB
2854}
2855
2856/* Implement the prev_register callback for RiscV frame unwinder. */
2857
2858static struct value *
2859riscv_frame_prev_register (struct frame_info *this_frame,
2860 void **prologue_cache,
2861 int regnum)
2862{
78a3b0fa 2863 struct riscv_unwind_cache *cache;
dbbb1059 2864
78a3b0fa
AB
2865 cache = riscv_frame_cache (this_frame, prologue_cache);
2866 return trad_frame_get_prev_register (this_frame, cache->regs, regnum);
dbbb1059
AB
2867}
2868
2869/* Structure defining the RiscV normal frame unwind functions. Since we
2870 are the fallback unwinder (DWARF unwinder is used first), we use the
2871 default frame sniffer, which always accepts the frame. */
2872
2873static const struct frame_unwind riscv_frame_unwind =
2874{
2875 /*.type =*/ NORMAL_FRAME,
2876 /*.stop_reason =*/ default_frame_unwind_stop_reason,
2877 /*.this_id =*/ riscv_frame_this_id,
2878 /*.prev_register =*/ riscv_frame_prev_register,
2879 /*.unwind_data =*/ NULL,
2880 /*.sniffer =*/ default_frame_sniffer,
2881 /*.dealloc_cache =*/ NULL,
2882 /*.prev_arch =*/ NULL,
2883};
2884
90af0679
AB
2885/* Extract a set of required target features out of INFO, specifically the
2886 bfd being executed is examined to see what target features it requires.
2887 IF there is no current bfd, or the bfd doesn't indicate any useful
2888 features then a RISCV_GDBARCH_FEATURES is returned in its default state. */
dbbb1059 2889
90af0679
AB
2890static struct riscv_gdbarch_features
2891riscv_features_from_gdbarch_info (const struct gdbarch_info info)
dbbb1059 2892{
b5ffee31 2893 struct riscv_gdbarch_features features;
dbbb1059 2894
b5ffee31
AB
2895 /* Now try to improve on the defaults by looking at the binary we are
2896 going to execute. We assume the user knows what they are doing and
2897 that the target will match the binary. Remember, this code path is
2898 only used at all if the target hasn't given us a description, so this
2899 is really a last ditched effort to do something sane before giving
2900 up. */
dbbb1059
AB
2901 if (info.abfd != NULL
2902 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
2903 {
2904 unsigned char eclass = elf_elfheader (info.abfd)->e_ident[EI_CLASS];
2905 int e_flags = elf_elfheader (info.abfd)->e_flags;
2906
2907 if (eclass == ELFCLASS32)
b5ffee31 2908 features.xlen = 4;
dbbb1059 2909 else if (eclass == ELFCLASS64)
b5ffee31 2910 features.xlen = 8;
dbbb1059 2911 else
b5ffee31 2912 internal_error (__FILE__, __LINE__,
dbbb1059
AB
2913 _("unknown ELF header class %d"), eclass);
2914
dbbb1059 2915 if (e_flags & EF_RISCV_FLOAT_ABI_DOUBLE)
113b7b81 2916 features.flen = 8;
dbbb1059 2917 else if (e_flags & EF_RISCV_FLOAT_ABI_SINGLE)
113b7b81 2918 features.flen = 4;
dbbb1059 2919 }
dbbb1059 2920
90af0679
AB
2921 return features;
2922}
2923
2924/* Find a suitable default target description. Use the contents of INFO,
2925 specifically the bfd object being executed, to guide the selection of a
2926 suitable default target description. */
2927
2928static const struct target_desc *
2929riscv_find_default_target_description (const struct gdbarch_info info)
2930{
2931 /* Extract desired feature set from INFO. */
2932 struct riscv_gdbarch_features features
2933 = riscv_features_from_gdbarch_info (info);
2934
2935 /* If the XLEN field is still 0 then we got nothing useful from INFO. In
2936 this case we fall back to a minimal useful target, 8-byte x-registers,
2937 with no floating point. */
2938 if (features.xlen == 0)
2939 features.xlen = 8;
2940
b5ffee31
AB
2941 /* Now build a target description based on the feature set. */
2942 return riscv_create_target_description (features);
2943}
2944
2945/* All of the registers in REG_SET are checked for in FEATURE, TDESC_DATA
2946 is updated with the register numbers for each register as listed in
2947 REG_SET. If any register marked as required in REG_SET is not found in
2948 FEATURE then this function returns false, otherwise, it returns true. */
2949
2950static bool
2951riscv_check_tdesc_feature (struct tdesc_arch_data *tdesc_data,
2952 const struct tdesc_feature *feature,
2953 const struct riscv_register_feature *reg_set)
2954{
2955 for (const auto &reg : reg_set->registers)
2956 {
2957 bool found = false;
2958
2959 for (const char *name : reg.names)
2960 {
2961 found =
2962 tdesc_numbered_register (feature, tdesc_data, reg.regnum, name);
2963
2964 if (found)
2965 break;
2966 }
2967
2968 if (!found && reg.required_p)
2969 return false;
2970 }
2971
2972 return true;
2973}
2974
2975/* Add all the expected register sets into GDBARCH. */
2976
2977static void
2978riscv_add_reggroups (struct gdbarch *gdbarch)
2979{
2980 /* Add predefined register groups. */
2981 reggroup_add (gdbarch, all_reggroup);
2982 reggroup_add (gdbarch, save_reggroup);
2983 reggroup_add (gdbarch, restore_reggroup);
2984 reggroup_add (gdbarch, system_reggroup);
2985 reggroup_add (gdbarch, vector_reggroup);
2986 reggroup_add (gdbarch, general_reggroup);
2987 reggroup_add (gdbarch, float_reggroup);
2988
2989 /* Add RISC-V specific register groups. */
2990 reggroup_add (gdbarch, csr_reggroup);
2991}
2992
2993/* Create register aliases for all the alternative names that exist for
2994 registers in REG_SET. */
2995
2996static void
2997riscv_setup_register_aliases (struct gdbarch *gdbarch,
2998 const struct riscv_register_feature *reg_set)
2999{
3000 for (auto &reg : reg_set->registers)
3001 {
3002 /* The first item in the names list is the preferred name for the
3003 register, this is what RISCV_REGISTER_NAME returns, and so we
3004 don't need to create an alias with that name here. */
3005 for (int i = 1; i < reg.names.size (); ++i)
3006 user_reg_add (gdbarch, reg.names[i], value_of_riscv_user_reg,
3007 &reg.regnum);
3008 }
3009}
3010
fb44d95a
AB
3011/* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
3012
3013static int
3014riscv_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
3015{
3016 if (reg < RISCV_DWARF_REGNUM_X31)
3017 return RISCV_ZERO_REGNUM + (reg - RISCV_DWARF_REGNUM_X0);
3018
3019 else if (reg < RISCV_DWARF_REGNUM_F31)
3020 return RISCV_FIRST_FP_REGNUM + (reg - RISCV_DWARF_REGNUM_F0);
3021
3022 return -1;
3023}
3024
b5ffee31
AB
3025/* Initialize the current architecture based on INFO. If possible,
3026 re-use an architecture from ARCHES, which is a list of
3027 architectures already created during this debugging session.
3028
3029 Called e.g. at program startup, when reading a core file, and when
3030 reading a binary file. */
3031
3032static struct gdbarch *
3033riscv_gdbarch_init (struct gdbarch_info info,
3034 struct gdbarch_list *arches)
3035{
3036 struct gdbarch *gdbarch;
3037 struct gdbarch_tdep *tdep;
3038 struct riscv_gdbarch_features features;
3039 const struct target_desc *tdesc = info.target_desc;
3040
3041 /* Ensure we always have a target description. */
3042 if (!tdesc_has_registers (tdesc))
3043 tdesc = riscv_find_default_target_description (info);
3044 gdb_assert (tdesc);
3045
3046 if (riscv_debug_gdbarch)
3047 fprintf_unfiltered (gdb_stdlog, "Have got a target description\n");
3048
3049 const struct tdesc_feature *feature_cpu
3050 = tdesc_find_feature (tdesc, riscv_xreg_feature.name);
3051 const struct tdesc_feature *feature_fpu
3052 = tdesc_find_feature (tdesc, riscv_freg_feature.name);
3053 const struct tdesc_feature *feature_virtual
3054 = tdesc_find_feature (tdesc, riscv_virtual_feature.name);
3055 const struct tdesc_feature *feature_csr
3056 = tdesc_find_feature (tdesc, riscv_csr_feature.name);
3057
3058 if (feature_cpu == NULL)
3059 return NULL;
3060
3061 struct tdesc_arch_data *tdesc_data = tdesc_data_alloc ();
3062
3063 bool valid_p = riscv_check_tdesc_feature (tdesc_data,
3064 feature_cpu,
3065 &riscv_xreg_feature);
3066 if (valid_p)
3067 {
3068 /* Check that all of the core cpu registers have the same bitsize. */
3069 int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
3070
3071 for (auto &tdesc_reg : feature_cpu->registers)
3072 valid_p &= (tdesc_reg->bitsize == xlen_bitsize);
3073
3074 if (riscv_debug_gdbarch)
3075 fprintf_filtered
3076 (gdb_stdlog,
3077 "From target-description, xlen = %d\n", xlen_bitsize);
3078
3079 features.xlen = (xlen_bitsize / 8);
3080 }
3081
3082 if (feature_fpu != NULL)
3083 {
3084 valid_p &= riscv_check_tdesc_feature (tdesc_data, feature_fpu,
3085 &riscv_freg_feature);
3086
0a5954bd
SC
3087 /* Search for the first floating point register (by any alias), to
3088 determine the bitsize. */
3089 int bitsize = -1;
3090 const auto &fp0 = riscv_freg_feature.registers[0];
3091
3092 for (const char *name : fp0.names)
3093 {
3094 if (tdesc_unnumbered_register (feature_fpu, name))
3095 {
3096 bitsize = tdesc_register_bitsize (feature_fpu, name);
3097 break;
3098 }
3099 }
3100
3101 gdb_assert (bitsize != -1);
b5ffee31 3102 features.flen = (bitsize / 8);
b5ffee31
AB
3103
3104 if (riscv_debug_gdbarch)
3105 fprintf_filtered
3106 (gdb_stdlog,
3107 "From target-description, flen = %d\n", bitsize);
3108 }
3109 else
3110 {
3111 features.flen = 0;
b5ffee31
AB
3112
3113 if (riscv_debug_gdbarch)
3114 fprintf_filtered
3115 (gdb_stdlog,
3116 "No FPU in target-description, assume soft-float ABI\n");
3117 }
3118
3119 if (feature_virtual)
3120 riscv_check_tdesc_feature (tdesc_data, feature_virtual,
3121 &riscv_virtual_feature);
3122
3123 if (feature_csr)
3124 riscv_check_tdesc_feature (tdesc_data, feature_csr,
3125 &riscv_csr_feature);
3126
3127 if (!valid_p)
3128 {
3129 if (riscv_debug_gdbarch)
3130 fprintf_unfiltered (gdb_stdlog, "Target description is not valid\n");
3131 tdesc_data_cleanup (tdesc_data);
3132 return NULL;
3133 }
3134
90af0679
AB
3135 /* Have a look at what the supplied (if any) bfd object requires of the
3136 target, then check that this matches with what the target is
3137 providing. */
113b7b81 3138 struct riscv_gdbarch_features abi_features
90af0679 3139 = riscv_features_from_gdbarch_info (info);
113b7b81
AB
3140 /* In theory a binary compiled for RV32 could run on an RV64 target,
3141 however, this has not been tested in GDB yet, so for now we require
3142 that the requested xlen match the targets xlen. */
3143 if (abi_features.xlen != 0 && abi_features.xlen != features.xlen)
90af0679 3144 error (_("bfd requires xlen %d, but target has xlen %d"),
113b7b81
AB
3145 abi_features.xlen, features.xlen);
3146 /* We do support running binaries compiled for 32-bit float on targets
3147 with 64-bit float, so we only complain if the binary requires more
3148 than the target has available. */
3149 if (abi_features.flen > features.flen)
90af0679 3150 error (_("bfd requires flen %d, but target has flen %d"),
113b7b81 3151 abi_features.flen, features.flen);
90af0679 3152
113b7b81
AB
3153 /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
3154 features from the INFO object. In this case we assume that the xlen
3155 abi matches the hardware. */
3156 if (abi_features.xlen == 0)
3157 abi_features.xlen = features.xlen;
90af0679 3158
dbbb1059
AB
3159 /* Find a candidate among the list of pre-declared architectures. */
3160 for (arches = gdbarch_list_lookup_by_info (arches, &info);
3161 arches != NULL;
3162 arches = gdbarch_list_lookup_by_info (arches->next, &info))
b5ffee31
AB
3163 {
3164 /* Check that the feature set of the ARCHES matches the feature set
3165 we are looking for. If it doesn't then we can't reuse this
3166 gdbarch. */
3167 struct gdbarch_tdep *other_tdep = gdbarch_tdep (arches->gdbarch);
3168
113b7b81
AB
3169 if (other_tdep->isa_features != features
3170 || other_tdep->abi_features != abi_features)
b5ffee31
AB
3171 continue;
3172
3173 break;
3174 }
3175
3176 if (arches != NULL)
3177 {
3178 tdesc_data_cleanup (tdesc_data);
dbbb1059 3179 return arches->gdbarch;
b5ffee31 3180 }
dbbb1059
AB
3181
3182 /* None found, so create a new architecture from the information provided. */
b5ffee31 3183 tdep = new (struct gdbarch_tdep);
dbbb1059 3184 gdbarch = gdbarch_alloc (&info, tdep);
113b7b81
AB
3185 tdep->isa_features = features;
3186 tdep->abi_features = abi_features;
dbbb1059
AB
3187
3188 /* Target data types. */
3189 set_gdbarch_short_bit (gdbarch, 16);
3190 set_gdbarch_int_bit (gdbarch, 32);
3191 set_gdbarch_long_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
3192 set_gdbarch_long_long_bit (gdbarch, 64);
3193 set_gdbarch_float_bit (gdbarch, 32);
3194 set_gdbarch_double_bit (gdbarch, 64);
3195 set_gdbarch_long_double_bit (gdbarch, 128);
3196 set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
3197 set_gdbarch_ptr_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
3198 set_gdbarch_char_signed (gdbarch, 0);
a9158a86 3199 set_gdbarch_type_align (gdbarch, riscv_type_align);
dbbb1059
AB
3200
3201 /* Information about the target architecture. */
3202 set_gdbarch_return_value (gdbarch, riscv_return_value);
3203 set_gdbarch_breakpoint_kind_from_pc (gdbarch, riscv_breakpoint_kind_from_pc);
3204 set_gdbarch_sw_breakpoint_from_kind (gdbarch, riscv_sw_breakpoint_from_kind);
5a77b1b4 3205 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
dbbb1059 3206
dbbb1059 3207 /* Functions to analyze frames. */
dbbb1059
AB
3208 set_gdbarch_skip_prologue (gdbarch, riscv_skip_prologue);
3209 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3210 set_gdbarch_frame_align (gdbarch, riscv_frame_align);
3211
dbbb1059
AB
3212 /* Functions handling dummy frames. */
3213 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
3214 set_gdbarch_push_dummy_code (gdbarch, riscv_push_dummy_code);
3215 set_gdbarch_push_dummy_call (gdbarch, riscv_push_dummy_call);
dbbb1059
AB
3216
3217 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own
3218 unwinder. */
3219 dwarf2_append_unwinders (gdbarch);
3220 frame_unwind_append_unwinder (gdbarch, &riscv_frame_unwind);
3221
b5ffee31
AB
3222 /* Register architecture. */
3223 riscv_add_reggroups (gdbarch);
3224
fb44d95a
AB
3225 /* Internal <-> external register number maps. */
3226 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, riscv_dwarf_reg_to_regnum);
3227
b5ffee31
AB
3228 /* We reserve all possible register numbers for the known registers.
3229 This means the target description mechanism will add any target
3230 specific registers after this number. This helps make debugging GDB
3231 just a little easier. */
3232 set_gdbarch_num_regs (gdbarch, RISCV_LAST_REGNUM + 1);
3233
3234 /* We don't have to provide the count of 0 here (its the default) but
3235 include this line to make it explicit that, right now, we don't have
3236 any pseudo registers on RISC-V. */
3237 set_gdbarch_num_pseudo_regs (gdbarch, 0);
3238
3239 /* Some specific register numbers GDB likes to know about. */
3240 set_gdbarch_sp_regnum (gdbarch, RISCV_SP_REGNUM);
3241 set_gdbarch_pc_regnum (gdbarch, RISCV_PC_REGNUM);
3242
3243 set_gdbarch_print_registers_info (gdbarch, riscv_print_registers_info);
3244
3245 /* Finalise the target description registers. */
3246 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
3247
3248 /* Override the register type callback setup by the target description
3249 mechanism. This allows us to provide special type for floating point
3250 registers. */
3251 set_gdbarch_register_type (gdbarch, riscv_register_type);
3252
3253 /* Override the register name callback setup by the target description
3254 mechanism. This allows us to force our preferred names for the
3255 registers, no matter what the target description called them. */
3256 set_gdbarch_register_name (gdbarch, riscv_register_name);
3257
3258 /* Override the register group callback setup by the target description
3259 mechanism. This allows us to force registers into the groups we
3260 want, ignoring what the target tells us. */
3261 set_gdbarch_register_reggroup_p (gdbarch, riscv_register_reggroup_p);
3262
3263 /* Create register aliases for alternative register names. */
3264 riscv_setup_register_aliases (gdbarch, &riscv_xreg_feature);
3265 if (riscv_has_fp_regs (gdbarch))
3266 riscv_setup_register_aliases (gdbarch, &riscv_freg_feature);
3267 riscv_setup_register_aliases (gdbarch, &riscv_csr_feature);
dbbb1059 3268
117a0e99
JW
3269 /* Hook in OS ABI-specific overrides, if they have been registered. */
3270 gdbarch_init_osabi (info, gdbarch);
3271
dbbb1059
AB
3272 return gdbarch;
3273}
3274
5c720ed8
JW
3275/* This decodes the current instruction and determines the address of the
3276 next instruction. */
3277
3278static CORE_ADDR
3279riscv_next_pc (struct regcache *regcache, CORE_ADDR pc)
3280{
3281 struct gdbarch *gdbarch = regcache->arch ();
3282 struct riscv_insn insn;
3283 CORE_ADDR next_pc;
3284
3285 insn.decode (gdbarch, pc);
3286 next_pc = pc + insn.length ();
3287
3288 if (insn.opcode () == riscv_insn::JAL)
3289 next_pc = pc + insn.imm_signed ();
3290 else if (insn.opcode () == riscv_insn::JALR)
3291 {
3292 LONGEST source;
3293 regcache->cooked_read (insn.rs1 (), &source);
3294 next_pc = (source + insn.imm_signed ()) & ~(CORE_ADDR) 0x1;
3295 }
3296 else if (insn.opcode () == riscv_insn::BEQ)
3297 {
3298 LONGEST src1, src2;
3299 regcache->cooked_read (insn.rs1 (), &src1);
3300 regcache->cooked_read (insn.rs2 (), &src2);
3301 if (src1 == src2)
3302 next_pc = pc + insn.imm_signed ();
3303 }
3304 else if (insn.opcode () == riscv_insn::BNE)
3305 {
3306 LONGEST src1, src2;
3307 regcache->cooked_read (insn.rs1 (), &src1);
3308 regcache->cooked_read (insn.rs2 (), &src2);
3309 if (src1 != src2)
3310 next_pc = pc + insn.imm_signed ();
3311 }
3312 else if (insn.opcode () == riscv_insn::BLT)
3313 {
3314 LONGEST src1, src2;
3315 regcache->cooked_read (insn.rs1 (), &src1);
3316 regcache->cooked_read (insn.rs2 (), &src2);
3317 if (src1 < src2)
3318 next_pc = pc + insn.imm_signed ();
3319 }
3320 else if (insn.opcode () == riscv_insn::BGE)
3321 {
3322 LONGEST src1, src2;
3323 regcache->cooked_read (insn.rs1 (), &src1);
3324 regcache->cooked_read (insn.rs2 (), &src2);
3325 if (src1 >= src2)
3326 next_pc = pc + insn.imm_signed ();
3327 }
3328 else if (insn.opcode () == riscv_insn::BLTU)
3329 {
3330 ULONGEST src1, src2;
3331 regcache->cooked_read (insn.rs1 (), &src1);
3332 regcache->cooked_read (insn.rs2 (), &src2);
3333 if (src1 < src2)
3334 next_pc = pc + insn.imm_signed ();
3335 }
3336 else if (insn.opcode () == riscv_insn::BGEU)
3337 {
3338 ULONGEST src1, src2;
3339 regcache->cooked_read (insn.rs1 (), &src1);
3340 regcache->cooked_read (insn.rs2 (), &src2);
3341 if (src1 >= src2)
3342 next_pc = pc + insn.imm_signed ();
3343 }
3344
3345 return next_pc;
3346}
3347
3348/* We can't put a breakpoint in the middle of a lr/sc atomic sequence, so look
3349 for the end of the sequence and put the breakpoint there. */
3350
3351static bool
3352riscv_next_pc_atomic_sequence (struct regcache *regcache, CORE_ADDR pc,
3353 CORE_ADDR *next_pc)
3354{
3355 struct gdbarch *gdbarch = regcache->arch ();
3356 struct riscv_insn insn;
3357 CORE_ADDR cur_step_pc = pc;
3358 CORE_ADDR last_addr = 0;
3359
3360 /* First instruction has to be a load reserved. */
3361 insn.decode (gdbarch, cur_step_pc);
3362 if (insn.opcode () != riscv_insn::LR)
3363 return false;
3364 cur_step_pc = cur_step_pc + insn.length ();
3365
3366 /* Next instruction should be branch to exit. */
3367 insn.decode (gdbarch, cur_step_pc);
3368 if (insn.opcode () != riscv_insn::BNE)
3369 return false;
3370 last_addr = cur_step_pc + insn.imm_signed ();
3371 cur_step_pc = cur_step_pc + insn.length ();
3372
3373 /* Next instruction should be store conditional. */
3374 insn.decode (gdbarch, cur_step_pc);
3375 if (insn.opcode () != riscv_insn::SC)
3376 return false;
3377 cur_step_pc = cur_step_pc + insn.length ();
3378
3379 /* Next instruction should be branch to start. */
3380 insn.decode (gdbarch, cur_step_pc);
3381 if (insn.opcode () != riscv_insn::BNE)
3382 return false;
3383 if (pc != (cur_step_pc + insn.imm_signed ()))
3384 return false;
3385 cur_step_pc = cur_step_pc + insn.length ();
3386
3387 /* We should now be at the end of the sequence. */
3388 if (cur_step_pc != last_addr)
3389 return false;
3390
3391 *next_pc = cur_step_pc;
3392 return true;
3393}
3394
3395/* This is called just before we want to resume the inferior, if we want to
3396 single-step it but there is no hardware or kernel single-step support. We
3397 find the target of the coming instruction and breakpoint it. */
3398
3399std::vector<CORE_ADDR>
3400riscv_software_single_step (struct regcache *regcache)
3401{
3402 CORE_ADDR pc, next_pc;
3403
3404 pc = regcache_read_pc (regcache);
3405
3406 if (riscv_next_pc_atomic_sequence (regcache, pc, &next_pc))
3407 return {next_pc};
3408
3409 next_pc = riscv_next_pc (regcache, pc);
3410
3411 return {next_pc};
3412}
3413
b5ffee31
AB
3414/* Create RISC-V specific reggroups. */
3415
3416static void
3417riscv_init_reggroups ()
3418{
3419 csr_reggroup = reggroup_new ("csr", USER_REGGROUP);
3420}
3421
dbbb1059
AB
3422void
3423_initialize_riscv_tdep (void)
3424{
b5ffee31
AB
3425 riscv_create_csr_aliases ();
3426 riscv_init_reggroups ();
3427
dbbb1059
AB
3428 gdbarch_register (bfd_arch_riscv, riscv_gdbarch_init, NULL);
3429
dbbb1059
AB
3430 /* Add root prefix command for all "set debug riscv" and "show debug
3431 riscv" commands. */
3432 add_prefix_cmd ("riscv", no_class, set_debug_riscv_command,
3433 _("RISC-V specific debug commands."),
3434 &setdebugriscvcmdlist, "set debug riscv ", 0,
3435 &setdebuglist);
3436
3437 add_prefix_cmd ("riscv", no_class, show_debug_riscv_command,
3438 _("RISC-V specific debug commands."),
3439 &showdebugriscvcmdlist, "show debug riscv ", 0,
3440 &showdebuglist);
3441
f37bc8b1
JB
3442 add_setshow_zuinteger_cmd ("breakpoints", class_maintenance,
3443 &riscv_debug_breakpoints, _("\
3444Set riscv breakpoint debugging."), _("\
3445Show riscv breakpoint debugging."), _("\
3446When non-zero, print debugging information for the riscv specific parts\n\
3447of the breakpoint mechanism."),
3448 NULL,
3449 show_riscv_debug_variable,
3450 &setdebugriscvcmdlist, &showdebugriscvcmdlist);
3451
dbbb1059
AB
3452 add_setshow_zuinteger_cmd ("infcall", class_maintenance,
3453 &riscv_debug_infcall, _("\
3454Set riscv inferior call debugging."), _("\
3455Show riscv inferior call debugging."), _("\
3456When non-zero, print debugging information for the riscv specific parts\n\
3457of the inferior call mechanism."),
3458 NULL,
3459 show_riscv_debug_variable,
3460 &setdebugriscvcmdlist, &showdebugriscvcmdlist);
78a3b0fa
AB
3461
3462 add_setshow_zuinteger_cmd ("unwinder", class_maintenance,
3463 &riscv_debug_unwinder, _("\
3464Set riscv stack unwinding debugging."), _("\
3465Show riscv stack unwinding debugging."), _("\
3466When non-zero, print debugging information for the riscv specific parts\n\
3467of the stack unwinding mechanism."),
3468 NULL,
3469 show_riscv_debug_variable,
3470 &setdebugriscvcmdlist, &showdebugriscvcmdlist);
b5ffee31
AB
3471
3472 add_setshow_zuinteger_cmd ("gdbarch", class_maintenance,
3473 &riscv_debug_gdbarch, _("\
3474Set riscv gdbarch initialisation debugging."), _("\
3475Show riscv gdbarch initialisation debugging."), _("\
3476When non-zero, print debugging information for the riscv gdbarch\n\
3477initialisation process."),
3478 NULL,
3479 show_riscv_debug_variable,
3480 &setdebugriscvcmdlist, &showdebugriscvcmdlist);
dbbb1059
AB
3481
3482 /* Add root prefix command for all "set riscv" and "show riscv" commands. */
3483 add_prefix_cmd ("riscv", no_class, set_riscv_command,
3484 _("RISC-V specific commands."),
3485 &setriscvcmdlist, "set riscv ", 0, &setlist);
3486
3487 add_prefix_cmd ("riscv", no_class, show_riscv_command,
3488 _("RISC-V specific commands."),
3489 &showriscvcmdlist, "show riscv ", 0, &showlist);
3490
3491
3492 use_compressed_breakpoints = AUTO_BOOLEAN_AUTO;
3493 add_setshow_auto_boolean_cmd ("use-compressed-breakpoints", no_class,
3494 &use_compressed_breakpoints,
3495 _("\
3496Set debugger's use of compressed breakpoints."), _(" \
3497Show debugger's use of compressed breakpoints."), _("\
f37bc8b1
JB
3498Debugging compressed code requires compressed breakpoints to be used. If\n\
3499left to 'auto' then gdb will use them if the existing instruction is a\n\
3500compressed instruction. If that doesn't give the correct behavior, then\n\
3501this option can be used."),
dbbb1059
AB
3502 NULL,
3503 show_use_compressed_breakpoints,
3504 &setriscvcmdlist,
3505 &showriscvcmdlist);
3506}
This page took 0.516321 seconds and 4 git commands to generate.