summaryrefslogtreecommitdiff
path: root/usr.bin/oasm/emit.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-07-22 15:23:28 -0400
committerIan Moffett <ian@osmora.org>2025-07-22 15:23:28 -0400
commit61f9cb8c58aac0fcc283b2c352c7cc328bd62b8d (patch)
tree96779b4c4b9fa7e0c86c256df0840b8f453078f4 /usr.bin/oasm/emit.c
parentff5a77f6f2ee9e7086b0f2c16cf0752a2d105d33 (diff)
oasm: Add encoding for BR instruction
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'usr.bin/oasm/emit.c')
-rw-r--r--usr.bin/oasm/emit.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/usr.bin/oasm/emit.c b/usr.bin/oasm/emit.c
index 0e8ad4e..eb38779 100644
--- a/usr.bin/oasm/emit.c
+++ b/usr.bin/oasm/emit.c
@@ -257,6 +257,45 @@ emit_encode_hlt(struct emit_state *state, struct oasm_token *tok)
return TAILQ_NEXT(tok, link);
}
+/*
+ * Encode a BR instruction
+ *
+ * br [r]
+ *
+ * Returns the next token on success,
+ * otherwise NULL.
+ */
+static struct oasm_token *
+emit_encode_br(struct emit_state *state, struct oasm_token *tok)
+{
+ inst_t curinst;
+ reg_t rd;
+ uint8_t opcode = OSMX64_BR;
+ char *inst_str = "br";
+
+ /* Grab the register */
+ tok = TAILQ_NEXT(tok, link);
+ if (tok == NULL) {
+ return NULL;
+ }
+ if (!tok_is_xreg(tok->type)) {
+ oasm_err("[emit error]: expected register in '%s'\n", inst_str);
+ return NULL;
+ }
+
+ rd = ir_to_reg(tok->type);
+ if (rd == OSMX64_R_BAD) {
+ oasm_err("[emit error]: got bad in register in '%s'\n", inst_str);
+ return NULL;
+ }
+
+ curinst.opcode = opcode;
+ curinst.rd = rd;
+ curinst.unused = 0;
+ emit_bytes(state, &curinst, sizeof(curinst));
+ return TAILQ_NEXT(tok, link);
+}
+
int
emit_osmx64(struct emit_state *state, struct oasm_token *tp)
{
@@ -341,6 +380,9 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit)
case TT_DIV:
curtok = emit_encode_arith(emit, curtok);
break;
+ case TT_BR:
+ curtok = emit_encode_br(emit, curtok);
+ break;
case TT_HLT:
curtok = emit_encode_hlt(emit, curtok);
break;