summaryrefslogtreecommitdiff
path: root/usr.bin/oasm
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-07-20 22:01:36 -0400
committerIan Moffett <ian@osmora.org>2025-07-20 22:04:53 -0400
commit890dd98ceb61de168b20acfd0aef1ddeb2cc1f39 (patch)
tree2d08d2f6c8e088b19e27f745da5e21e737344738 /usr.bin/oasm
parent7a43fb95afa4cee74b2282f38789bbc363b86dd7 (diff)
oasm: parse: Add tok_is_xreg() helper
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'usr.bin/oasm')
-rw-r--r--usr.bin/oasm/include/oasm/lex.h31
-rw-r--r--usr.bin/oasm/parse.c28
2 files changed, 36 insertions, 23 deletions
diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h
index 1bb8641..2811ca3 100644
--- a/usr.bin/oasm/include/oasm/lex.h
+++ b/usr.bin/oasm/include/oasm/lex.h
@@ -119,4 +119,35 @@ struct oasm_token {
int lex_tok(struct oasm_state *state, struct oasm_token *ttp);
+
+/*
+ * Check if a token is an X<n> register.
+ * Returns true on match.
+ */
+__always_inline static inline bool
+tok_is_xreg(tt_t tok)
+{
+ switch (tok) {
+ case TT_X0:
+ case TT_X1:
+ case TT_X2:
+ case TT_X3:
+ case TT_X4:
+ case TT_X5:
+ case TT_X6:
+ case TT_X7:
+ case TT_X8:
+ case TT_X9:
+ case TT_X10:
+ case TT_X11:
+ case TT_X12:
+ case TT_X13:
+ case TT_X14:
+ case TT_X15:
+ return true;
+ }
+
+ return false;
+}
+
#endif /* !_OASM_LEX_H_ */
diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c
index 6e7d2e6..80d353b 100644
--- a/usr.bin/oasm/parse.c
+++ b/usr.bin/oasm/parse.c
@@ -113,27 +113,9 @@ parse_reg(struct oasm_state *state, struct oasm_token *tok)
return -1;
}
- switch (tok->type) {
- case TT_X0:
- case TT_X1:
- case TT_X2:
- case TT_X3:
- case TT_X4:
- case TT_X5:
- case TT_X6:
- case TT_X7:
- case TT_X8:
- case TT_X9:
- case TT_X10:
- case TT_X11:
- case TT_X12:
- case TT_X13:
- case TT_X14:
- state->last = tok->type;
- break;
- default:
+ if (!tok_is_xreg(tok->type)) {
p = tokstr[tok->type];
- oasm_err("bad register %s\n", p);
+ oasm_err("bad register \"%s\"\n", p);
return -1;
}
@@ -154,9 +136,9 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
state->last = tok->type;
break;
case TT_IMM:
- p = tokstr[TT_MOV];
- if (state->last != TT_MOV) {
- oasm_err("previous token must be %s\n", p);
+ p = tokstr[state->last];
+ if (!tok_is_xreg(state->last)) {
+ printf("expected X<n> but got %s\n", p);
return -1;
}
break;