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