[PATCH 2/2] RISC-V: Reject incomplete matches of Zcmp register lists
Jiawei
jiawei@iscas.ac.cn
Wed Nov 25 11:52:02 GMT 2026
GAS accepted cm.push {ra,s0,s1},-32 and encoded a push of only ra
and s0. The register-list parser returned early after s0, and its
tokenizer also discarded empty fields and ignored excess ranges.
Parse the ABI and numeric forms directly, requiring the closing brace
after the last encodable register. Use the existing GPR lookup for
register aliases and RV32E register limits. Add tests for malformed
lists and retain coverage of the fp alias and the numeric x18 range.
gas/ChangeLog:
* config/tc-riscv.c (reglist_lookup): Parse the complete ABI or
numeric register list.
(reglist_lookup_internal): Remove.
(REG_TO_REG_LIST, REG_NUMERIC, REG_CONFLICT): Remove macros.
* testsuite/gas/riscv/zcmp-push-pop-fail.l: Expect register lists.
* testsuite/gas/riscv/zcmp-push-pop-fail.s: Add numeric lists.
* testsuite/gas/riscv/zcmp-push-pop.d: Check encodings.
* testsuite/gas/riscv/zcmp-push-pop.s: Test range.
---
gas/config/tc-riscv.c | 153 ++++++-------------
gas/testsuite/gas/riscv/zcmp-push-pop-fail.l | 10 ++
gas/testsuite/gas/riscv/zcmp-push-pop-fail.s | 11 ++
gas/testsuite/gas/riscv/zcmp-push-pop.d | 2 +
gas/testsuite/gas/riscv/zcmp-push-pop.s | 4 +
5 files changed, 70 insertions(+), 110 deletions(-)
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index a597db50898..f0ca02a662f 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -1240,129 +1240,62 @@ regno_to_reg_list (unsigned regno)
return 0;
}
-/* Parse register list, and return the last register by regno_to_reg_list.
+/* Parse a Zcmp register list. Its encoding is determined by the last
+ register, but all preceding registers and delimiters must match.
+ Leave the closing brace for the opcode operand parser. */
- If ABI register names are used (e.g. ra and s0), the register
- list could be "{ra}", "{ra, s0}", "{ra, s0-sN}", where 0 < N < 10 or
- N == 11.
-
- If numeric register names are used (e.g. x1 and x8), the register list
- could be "{x1}", "{x1,x8}", "{x1,x8-x9}", "{x1,x8-x9,x18}" and
- "{x1,x8-x9,x18-xN}", where 19 < N < 25 or N == 27.
-
- The numeric and ABI register names cannot be used at the same time.
+static bool
+reglist_lookup (char **s, unsigned *reg_list)
+{
+ char *p = *s;
+ bool numeric = *p == 'x';
+ unsigned regno;
- TODO: Report errors for the following cases,
- 1. Too many registers in the list.
- 2. Cases which return 0.
- 3. Illegal formats, for example, {x1,x8-NULL,x18-x24/x18}, {x1-x2,x8}. */
+ if (!reg_lookup (&p, RCLASS_GPR, ®no) || regno != X_RA)
+ return false;
-static unsigned
-reglist_lookup_internal (char *reglist)
-{
- unsigned regno = 0;
- unsigned reg_list = 0;
- char *regname[3][2] = {{NULL}};
- char *save_tok, *save_subtok;
- unsigned i, j;
-
- char *token = strtok_r (reglist, ",", &save_tok);
- for (i = 0; i < 3 && token != NULL;
- token = strtok_r (NULL, ",", &save_tok), i++)
+ /* Both forms can include s0 (x8), followed by a range ending at sN
+ for ABI names or at x9 for numeric names. */
+ if (*p == ',')
{
- char *subtoken = strtok_r (token, "-", &save_subtok);
- for (j = 0; j < 2 && subtoken != NULL;
- subtoken = strtok_r (NULL, "-", &save_subtok), j++)
- regname[i][j] = subtoken;
- }
+ ++p;
+ if ((*p == 'x') != numeric
+ || !reg_lookup (&p, RCLASS_GPR, ®no) || regno != X_S0)
+ return false;
- bool reg1_numeric = false;
- for (i = 0; i < 3; i++)
- {
- if (regname[i][0] == NULL)
- continue;
-#define REG_TO_REG_LIST(NAME, NUM, LIST) \
- (reg_lookup (&NAME, RCLASS_GPR, &NUM) && (LIST = regno_to_reg_list (NUM)))
-#define REG_NUMERIC(NAME) (NAME[0] == 'x')
-#define REG_CONFLICT(NAME, REG_NUMERIC) \
- ((NAME[0] == 'x' && !REG_NUMERIC) || (NAME[0] != 'x' && REG_NUMERIC))
- switch (i)
+ if (*p == '-')
{
- case 0:
- reg1_numeric = REG_NUMERIC (regname[i][0]);
- if (!REG_TO_REG_LIST (regname[i][0], regno, reg_list)
- || regno != X_RA)
- return 0;
- break;
- case 1:
- if (REG_CONFLICT (regname[i][0], reg1_numeric)
- /* The second register should be s0 or its numeric names x8. */
- || !REG_TO_REG_LIST (regname[i][0], regno, reg_list)
- || regno != X_S0)
- return 0;
- else if (regname[i][1] == NULL)
- return reg_list;
-
- if (REG_CONFLICT (regname[i][1], reg1_numeric)
- /* The third register is x9 if the numeric name is used.
- Otherwise, it could be any other sN register, where N > 0. */
- || !REG_TO_REG_LIST (regname[i][1], regno, reg_list)
- || regno <= X_S0
- || (reg1_numeric && regno != X_S1))
- return 0;
- break;
- case 2:
- /* Must use register numeric names. */
- if (!reg1_numeric
- || !REG_NUMERIC (regname[i][0])
- /* The fourth register should be s2. */
- || !REG_TO_REG_LIST (regname[i][0], regno, reg_list)
- || regno != X_S2)
- return 0;
- else if (regname[i][1] == NULL)
- return reg_list;
-
- if (!reg1_numeric
- || !REG_NUMERIC (regname[i][1])
- /* The fifth register could be any other sN register, where N > 1. */
- || !REG_TO_REG_LIST (regname[i][1], regno, reg_list)
- || regno <= X_S2)
- return 0;
- break;
- default:
- return 0;
+ ++p;
+ if ((*p == 'x') != numeric
+ || !reg_lookup (&p, RCLASS_GPR, ®no)
+ || regno <= X_S0 || (numeric && regno != X_S1))
+ return false;
}
-#undef REG_TO_REG_LIST
-#undef REG_NUMERIC
-#undef REG_CONFLICT
}
- return reg_list;
-}
-/* Parse register list. Return false if REG_LIST is zero, which is an
- invalid value. */
-
-static bool
-reglist_lookup (char **s, unsigned *reg_list)
-{
- *reg_list = 0;
- char *reglist = xstrdup (*s);
- if (reglist != NULL)
+ /* The remaining numeric registers form a separate range starting at
+ x18, immediately after x8-x9 in the list. */
+ if (numeric && regno == X_S1 && *p == ',')
{
- char *token = strtok (reglist, "}");
- if (token != NULL)
- {
- *s += strlen (token);
- *reg_list = reglist_lookup_internal (reglist);
- }
- else
+ ++p;
+ if (*p != 'x' || !reg_lookup (&p, RCLASS_GPR, ®no) || regno != X_S2)
+ return false;
+
+ if (*p == '-')
{
- as_bad (_("cannot find `}' for cm.push/cm.pop"));
- *reg_list = 0;
+ ++p;
+ if (*p != 'x' || !reg_lookup (&p, RCLASS_GPR, ®no)
+ || regno <= X_S2)
+ return false;
}
}
- free (reglist);
- return *reg_list == 0 ? false : true;
+
+ if (*p != '}')
+ return false;
+
+ *reg_list = regno_to_reg_list (regno);
+ *s = p;
+ return *reg_list != 0;
}
#define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
diff --git a/gas/testsuite/gas/riscv/zcmp-push-pop-fail.l b/gas/testsuite/gas/riscv/zcmp-push-pop-fail.l
index 955e495d5bb..f5ee32ae179 100644
--- a/gas/testsuite/gas/riscv/zcmp-push-pop-fail.l
+++ b/gas/testsuite/gas/riscv/zcmp-push-pop-fail.l
@@ -3,6 +3,16 @@
.*: Error: illegal operands `cm.pop \{ra,s1\},-64'
.*: Error: illegal operands `cm.popret \{ra,s2-s3\},-64'
.*: Error: illegal operands `cm.popretz \{ra,s0-s10\},-112'
+.*: Error: illegal operands `cm.push \{ra,s0,s1\},-32'
+.*: Error: illegal operands `cm.pop \{x1,x8,x18\},32'
+.*: Error: illegal operands `cm.popret \{x1,x8-x9,x18,x19\},32'
+.*: Error: illegal operands `cm.popretz \{ra-s0\},16'
+.*: Error: illegal operands `cm.push \{ra,s0-s1-s2\},-32'
+.*: Error: illegal operands `cm.pop \{ra,,s0\},32'
+.*: Error: illegal operands `cm.popret \{ra,s0-\},32'
+.*: Error: illegal operands `cm.popretz \{x1,x8-x9,\},32'
+.*: Error: illegal operands `cm.push \{x1,x8-s1\},-32'
+.*: Error: illegal operands `cm.pop \{x1,x8-x9,x18-x26\},112'
.*: Error: illegal operands `cm.push \{ra\},0'
.*: Error: illegal operands `cm.pop \{ra,s0\},-80'
.*: Error: illegal operands `cm.popret \{ra,s0-s1\},-15'
diff --git a/gas/testsuite/gas/riscv/zcmp-push-pop-fail.s b/gas/testsuite/gas/riscv/zcmp-push-pop-fail.s
index 0e8df5800dd..3782032331b 100644
--- a/gas/testsuite/gas/riscv/zcmp-push-pop-fail.s
+++ b/gas/testsuite/gas/riscv/zcmp-push-pop-fail.s
@@ -6,6 +6,17 @@ target:
cm.popret {ra, s2-s3}, -64
cm.popretz {ra, s0-s10}, -112
+ cm.push {ra,s0,s1},-32
+ cm.pop {x1,x8,x18},32
+ cm.popret {x1,x8-x9,x18,x19},32
+ cm.popretz {ra-s0},16
+ cm.push {ra,s0-s1-s2},-32
+ cm.pop {ra,,s0},32
+ cm.popret {ra,s0-},32
+ cm.popretz {x1,x8-x9,},32
+ cm.push {x1,x8-s1},-32
+ cm.pop {x1,x8-x9,x18-x26},112
+
# spimm
cm.push {ra}, 0
cm.pop {ra, s0}, -80
diff --git a/gas/testsuite/gas/riscv/zcmp-push-pop.d b/gas/testsuite/gas/riscv/zcmp-push-pop.d
index e21295051ec..83a963e000f 100644
--- a/gas/testsuite/gas/riscv/zcmp-push-pop.d
+++ b/gas/testsuite/gas/riscv/zcmp-push-pop.d
@@ -152,3 +152,5 @@ Disassembly of section .text:
[ ]*[0-9a-f]+:[ ]+bcf6[ ]+cm.popretz[ ]+\{ra,s0-s11\},128
[ ]*[0-9a-f]+:[ ]+bcfa[ ]+cm.popretz[ ]+\{ra,s0-s11\},144
[ ]*[0-9a-f]+:[ ]+bcfe[ ]+cm.popretz[ ]+\{ra,s0-s11\},160
+[ ]*[0-9a-f]+:[ ]+b862[ ]+cm.push[ ]+\{ra,s0-s1\},-32
+[ ]*[0-9a-f]+:[ ]+b882[ ]+cm.push[ ]+\{ra,s0-s3\},-48
diff --git a/gas/testsuite/gas/riscv/zcmp-push-pop.s b/gas/testsuite/gas/riscv/zcmp-push-pop.s
index dc441bc2776..dafaab36a88 100644
--- a/gas/testsuite/gas/riscv/zcmp-push-pop.s
+++ b/gas/testsuite/gas/riscv/zcmp-push-pop.s
@@ -160,3 +160,7 @@ target:
cm.popretz {ra, s0-s11}, 128
cm.popretz {ra, s0-s11}, 144
cm.popretz {ra, s0-s11}, 160
+
+ # Alias for s0 and the first numeric range ending beyond x18.
+ cm.push {ra,fp-s1}, -32
+ cm.push {x1,x8-x9,x18-x19}, -48
More information about the Binutils
mailing list