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