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