diff options
author | Nelson Chu <nelson.chu@sifive.com> | 2020-02-12 02:18:49 -0800 |
---|---|---|
committer | Jim Wilson <jimw@sifive.com> | 2020-02-20 16:49:09 -0800 |
commit | bd0cf5a6bae180f65f3b9298619d1bd695abcdd8 (patch) | |
tree | a9573166927fdbd4f0b702bd2207fe3a7e6f3d72 /gas | |
parent | 10a95fcc1f8fb621dfc82b1097336cc58a3574f5 (diff) | |
download | fsf-binutils-gdb-bd0cf5a6bae180f65f3b9298619d1bd695abcdd8.zip fsf-binutils-gdb-bd0cf5a6bae180f65f3b9298619d1bd695abcdd8.tar.gz fsf-binutils-gdb-bd0cf5a6bae180f65f3b9298619d1bd695abcdd8.tar.bz2 |
RISC-V: Support the ISA-dependent CSR checking.
According to the riscv privilege spec, some CSR are only valid when rv32 or
the specific extension is set. We extend the DECLARE_CSR and DECLARE_CSR_ALIAS
to record more informaton we need, and then check whether the CSR is valid
according to these information. We report warning message when the CSR is
invalid, so we have a choice between error and warning by --fatal-warnings
option. Also, a --no-warn/-W option is used to turn the warnings off, if
people don't want the warnings.
gas/
* config/tc-riscv.c (enum riscv_csr_class): New enum. Used to decide
whether or not this CSR is legal in the current ISA string.
(struct riscv_csr_extra): New structure to hold all extra information
of CSR.
(riscv_init_csr_hash): New function. According to the DECLARE_CSR and
DECLARE_CSR_ALIAS, insert CSR extra information into csr_extra_hash.
Call hash_reg_name to insert CSR address into reg_names_hash.
(md_begin): Call riscv_init_csr_hashes for each DECLARE_CSR.
(reg_csr_lookup_internal, riscv_csr_class_check): New functions.
Decide whether the CSR is valid according to the csr_extra_hash.
(init_opcode_hash): Update 'if (hash_error != NULL)' as hash_error is
not a boolean. This is same as riscv_init_csr_hash, so keep the
consistent usage.
* testsuite/gas/riscv/csr-dw-regnums.d: Add -march=rv32if option.
* testsuite/gas/riscv/priv-reg.d: Add f-ext by -march option.
* testsuite/gas/riscv/priv-reg-fail-fext.d: New testcase. The source
file is `priv-reg.s`, and the ISA is rv32i without f-ext, so the
f-ext CSR are not allowed.
* testsuite/gas/riscv/priv-reg-fail-fext.l: Likewise.
* testsuite/gas/riscv/priv-reg-fail-rv32-only.d: New testcase. The
source file is `priv-reg.s`, and the ISA is rv64if, so the
rv32-only CSR are not allowed.
* testsuite/gas/riscv/priv-reg-fail-rv32-only.l: Likewise.
include/
* opcode/riscv-opc.h: Extend DECLARE_CSR and DECLARE_CSR_ALIAS to
record riscv_csr_class.
opcodes/
* riscv-dis.c (print_insn_args): Updated since the DECLARE_CSR is changed.
gdb/
* riscv-tdep.c: Updated since the DECLARE_CSR is changed.
* riscv-tdep.h: Likewise.
* features/riscv/rebuild-csr-xml.sh: Generate the 64bit-csr.xml without
rv32-only CSR.
* features/riscv/64bit-csr.xml: Regernated.
binutils/
* dwarf.c: Updated since the DECLARE_CSR is changed.
Diffstat (limited to 'gas')
-rw-r--r-- | gas/ChangeLog | 28 | ||||
-rw-r--r-- | gas/config/tc-riscv.c | 99 | ||||
-rw-r--r-- | gas/testsuite/gas/riscv/csr-dw-regnums.d | 2 | ||||
-rw-r--r-- | gas/testsuite/gas/riscv/priv-reg-fail-fext.d | 3 | ||||
-rw-r--r-- | gas/testsuite/gas/riscv/priv-reg-fail-fext.l | 4 | ||||
-rw-r--r-- | gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.d | 3 | ||||
-rw-r--r-- | gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.l | 66 | ||||
-rw-r--r-- | gas/testsuite/gas/riscv/priv-reg.d | 2 |
8 files changed, 198 insertions, 9 deletions
diff --git a/gas/ChangeLog b/gas/ChangeLog index 01cce9f..904d45a 100644 --- a/gas/ChangeLog +++ b/gas/ChangeLog @@ -1,3 +1,31 @@ +2020-02-20 Nelson Chu <nelson.chu@sifive.com> + + * config/tc-riscv.c (csr_extra_hash): New. + (enum riscv_csr_class): New enum. Used to decide + whether or not this CSR is legal in the current ISA string. + (struct riscv_csr_extra): New structure to hold all extra information + of CSR. + (riscv_init_csr_hashes): New. According to the DECLARE_CSR and + DECLARE_CSR_ALIAS, insert CSR extra information into csr_extra_hash. + Call hash_reg_name to insert CSR address into reg_names_hash. + (reg_csr_lookup_internal, riscv_csr_class_check): New functions. + Decide whether the CSR is valid according to the csr_extra_hash. + (reg_lookup_internal): Call reg_csr_lookup_internal for CSRs. + (init_opcode_hash): Update 'if (hash_error != NULL)' as hash_error is + not a boolean. This is same as riscv_init_csr_hash, so keep the + consistent usage. + (md_begin): Call riscv_init_csr_hashes for each DECLARE_CSR. + * testsuite/gas/riscv/csr-dw-regnums.d: Add -march=rv32if option. + * testsuite/gas/riscv/priv-reg.d: Add f-ext by -march option. + * testsuite/gas/riscv/priv-reg-fail-fext.d: New testcase. The source + file is `priv-reg.s`, and the ISA is rv32i without f-ext, so the + f-ext CSR are not allowed. + * testsuite/gas/riscv/priv-reg-fail-fext.l: Likewise. + * testsuite/gas/riscv/priv-reg-fail-rv32-only.d: New testcase. The + source file is `priv-reg.s`, and the ISA is rv64if, so the + rv32-only CSR are not allowed. + * testsuite/gas/riscv/priv-reg-fail-rv32-only.l: Likewise. + 2020-02-21 Alan Modra <amodra@gmail.com> * config/tc-pdp11.c (md_apply_fix): Handle BFD_RELOC_32. diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c index 21fff8f..2f95d41 100644 --- a/gas/config/tc-riscv.c +++ b/gas/config/tc-riscv.c @@ -455,6 +455,7 @@ enum reg_class }; static struct hash_control *reg_names_hash = NULL; +static struct hash_control *csr_extra_hash = NULL; #define ENCODE_REG_HASH(cls, n) \ ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1)) @@ -480,6 +481,86 @@ hash_reg_names (enum reg_class class, const char * const names[], unsigned n) hash_reg_name (class, names[i], i); } +/* All RISC-V CSRs belong to one of these classes. */ + +enum riscv_csr_class +{ + CSR_CLASS_NONE, + + CSR_CLASS_I, + CSR_CLASS_I_32, /* rv32 only */ + CSR_CLASS_F, /* f-ext only */ +}; + +/* This structure holds all restricted conditions for a CSR. */ + +struct riscv_csr_extra +{ + /* Class to which this CSR belongs. Used to decide whether or + not this CSR is legal in the current -march context. */ + enum riscv_csr_class csr_class; +}; + +/* Init two hashes, csr_extra_hash and reg_names_hash, for CSR. */ + +static void +riscv_init_csr_hashes (const char *name, + unsigned address, + enum riscv_csr_class class) +{ + struct riscv_csr_extra *entry = XNEW (struct riscv_csr_extra); + entry->csr_class = class; + + const char *hash_error = + hash_insert (csr_extra_hash, name, (void *) entry); + if (hash_error != NULL) + { + fprintf (stderr, _("internal error: can't hash `%s': %s\n"), + name, hash_error); + /* Probably a memory allocation problem? Give up now. */ + as_fatal (_("Broken assembler. No assembly attempted.")); + } + + hash_reg_name (RCLASS_CSR, name, address); +} + +/* Check wether the CSR is valid according to the ISA. */ + +static bfd_boolean +riscv_csr_class_check (enum riscv_csr_class csr_class) +{ + switch (csr_class) + { + case CSR_CLASS_I: return riscv_subset_supports ("i"); + case CSR_CLASS_F: return riscv_subset_supports ("f"); + case CSR_CLASS_I_32: + return (xlen == 32 && riscv_subset_supports ("i")); + + default: + return FALSE; + } +} + +/* If the CSR is defined, then we call `riscv_csr_class_check` to do the + further checking. Return FALSE if the CSR is not defined. Otherwise, + return TRUE. */ + +static bfd_boolean +reg_csr_lookup_internal (const char *s) +{ + struct riscv_csr_extra *r = + (struct riscv_csr_extra *) hash_find (csr_extra_hash, s); + + if (r == NULL) + return FALSE; + + /* We just report the warning when the CSR is invalid. */ + if (!riscv_csr_class_check (r->csr_class)) + as_warn (_("Invalid CSR `%s' for the current ISA"), s); + + return TRUE; +} + static unsigned int reg_lookup_internal (const char *s, enum reg_class class) { @@ -491,6 +572,9 @@ reg_lookup_internal (const char *s, enum reg_class class) if (riscv_opts.rve && class == RCLASS_GPR && DECODE_REG_NUM (r) > 15) return -1; + if (class == RCLASS_CSR && !reg_csr_lookup_internal (s)) + return -1; + return DECODE_REG_NUM (r); } @@ -721,7 +805,7 @@ init_opcode_hash (const struct riscv_opcode *opcodes, const char *hash_error = hash_insert (hash, name, (void *) &opcodes[i]); - if (hash_error) + if (hash_error != NULL) { fprintf (stderr, _("internal error: can't hash `%s': %s\n"), opcodes[i].name, hash_error); @@ -769,18 +853,19 @@ md_begin (void) hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR); hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR); hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR); - /* Add "fp" as an alias for "s0". */ hash_reg_name (RCLASS_GPR, "fp", 8); - opcode_names_hash = hash_new (); - init_opcode_names_hash (); - -#define DECLARE_CSR(name, num) hash_reg_name (RCLASS_CSR, #name, num); -#define DECLARE_CSR_ALIAS(name, num) DECLARE_CSR(name, num); + /* Create and insert CSR hash tables. */ + csr_extra_hash = hash_new (); +#define DECLARE_CSR(name, num, class) riscv_init_csr_hashes (#name, num, class); +#define DECLARE_CSR_ALIAS(name, num, class) DECLARE_CSR(name, num, class); #include "opcode/riscv-opc.h" #undef DECLARE_CSR + opcode_names_hash = hash_new (); + init_opcode_names_hash (); + /* Set the default alignment for the text section. */ record_alignment (text_section, riscv_opts.rvc ? 1 : 2); } diff --git a/gas/testsuite/gas/riscv/csr-dw-regnums.d b/gas/testsuite/gas/riscv/csr-dw-regnums.d index 597747c..a7b415e 100644 --- a/gas/testsuite/gas/riscv/csr-dw-regnums.d +++ b/gas/testsuite/gas/riscv/csr-dw-regnums.d @@ -1,4 +1,4 @@ -#as: +#as: -march=rv32if #objdump: --dwarf=frames diff --git a/gas/testsuite/gas/riscv/priv-reg-fail-fext.d b/gas/testsuite/gas/riscv/priv-reg-fail-fext.d new file mode 100644 index 0000000..78ab758 --- /dev/null +++ b/gas/testsuite/gas/riscv/priv-reg-fail-fext.d @@ -0,0 +1,3 @@ +#as: -march=rv32i +#source: priv-reg.s +#warning_output: priv-reg-fail-fext.l diff --git a/gas/testsuite/gas/riscv/priv-reg-fail-fext.l b/gas/testsuite/gas/riscv/priv-reg-fail-fext.l new file mode 100644 index 0000000..76818c8 --- /dev/null +++ b/gas/testsuite/gas/riscv/priv-reg-fail-fext.l @@ -0,0 +1,4 @@ +.*Assembler messages: +.*Warning: Invalid CSR `fflags' for the current ISA +.*Warning: Invalid CSR `frm' for the current ISA +.*Warning: Invalid CSR `fcsr' for the current ISA diff --git a/gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.d b/gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.d new file mode 100644 index 0000000..5dc840a --- /dev/null +++ b/gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.d @@ -0,0 +1,3 @@ +#as: -march=rv64if +#source: priv-reg.s +#warning_output: priv-reg-fail-rv32-only.l diff --git a/gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.l b/gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.l new file mode 100644 index 0000000..9123672 --- /dev/null +++ b/gas/testsuite/gas/riscv/priv-reg-fail-rv32-only.l @@ -0,0 +1,66 @@ +.*Assembler messages: +.*Warning: Invalid CSR `cycleh' for the current ISA +.*Warning: Invalid CSR `timeh' for the current ISA +.*Warning: Invalid CSR `instreth' for the current ISA +.*Warning: Invalid CSR `hpmcounter3h' for the current ISA +.*Warning: Invalid CSR `hpmcounter4h' for the current ISA +.*Warning: Invalid CSR `hpmcounter5h' for the current ISA +.*Warning: Invalid CSR `hpmcounter6h' for the current ISA +.*Warning: Invalid CSR `hpmcounter7h' for the current ISA +.*Warning: Invalid CSR `hpmcounter8h' for the current ISA +.*Warning: Invalid CSR `hpmcounter9h' for the current ISA +.*Warning: Invalid CSR `hpmcounter10h' for the current ISA +.*Warning: Invalid CSR `hpmcounter11h' for the current ISA +.*Warning: Invalid CSR `hpmcounter12h' for the current ISA +.*Warning: Invalid CSR `hpmcounter13h' for the current ISA +.*Warning: Invalid CSR `hpmcounter14h' for the current ISA +.*Warning: Invalid CSR `hpmcounter15h' for the current ISA +.*Warning: Invalid CSR `hpmcounter16h' for the current ISA +.*Warning: Invalid CSR `hpmcounter17h' for the current ISA +.*Warning: Invalid CSR `hpmcounter18h' for the current ISA +.*Warning: Invalid CSR `hpmcounter19h' for the current ISA +.*Warning: Invalid CSR `hpmcounter20h' for the current ISA +.*Warning: Invalid CSR `hpmcounter21h' for the current ISA +.*Warning: Invalid CSR `hpmcounter22h' for the current ISA +.*Warning: Invalid CSR `hpmcounter23h' for the current ISA +.*Warning: Invalid CSR `hpmcounter24h' for the current ISA +.*Warning: Invalid CSR `hpmcounter25h' for the current ISA +.*Warning: Invalid CSR `hpmcounter26h' for the current ISA +.*Warning: Invalid CSR `hpmcounter27h' for the current ISA +.*Warning: Invalid CSR `hpmcounter28h' for the current ISA +.*Warning: Invalid CSR `hpmcounter29h' for the current ISA +.*Warning: Invalid CSR `hpmcounter30h' for the current ISA +.*Warning: Invalid CSR `hpmcounter31h' for the current ISA +.*Warning: Invalid CSR `mcycleh' for the current ISA +.*Warning: Invalid CSR `minstreth' for the current ISA +.*Warning: Invalid CSR `mhpmcounter3h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter4h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter5h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter6h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter7h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter8h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter9h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter10h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter11h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter12h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter13h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter14h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter15h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter16h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter17h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter18h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter19h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter20h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter21h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter22h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter23h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter24h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter25h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter26h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter27h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter28h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter29h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter30h' for the current ISA +.*Warning: Invalid CSR `mhpmcounter31h' for the current ISA +.*Warning: Invalid CSR `pmpcfg1' for the current ISA +.*Warning: Invalid CSR `pmpcfg3' for the current ISA diff --git a/gas/testsuite/gas/riscv/priv-reg.d b/gas/testsuite/gas/riscv/priv-reg.d index d8ec868..8b7a7bf 100644 --- a/gas/testsuite/gas/riscv/priv-reg.d +++ b/gas/testsuite/gas/riscv/priv-reg.d @@ -1,4 +1,4 @@ -#as: -march=rv32i +#as: -march=rv32if #objdump: -dr .*:[ ]+file format .* |