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