summaryrefslogtreecommitdiff
path: root/usr.bin
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
parentff5a77f6f2ee9e7086b0f2c16cf0752a2d105d33 (diff)
oasm: Add encoding for BR instruction
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/oasm/emit.c42
-rw-r--r--usr.bin/oasm/include/oasm/emit.h1
-rw-r--r--usr.bin/oasm/include/oasm/lex.h1
-rw-r--r--usr.bin/oasm/lex.c21
-rw-r--r--usr.bin/oasm/parse.c6
5 files changed, 71 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;
diff --git a/usr.bin/oasm/include/oasm/emit.h b/usr.bin/oasm/include/oasm/emit.h
index d289adb..dab63d0 100644
--- a/usr.bin/oasm/include/oasm/emit.h
+++ b/usr.bin/oasm/include/oasm/emit.h
@@ -61,6 +61,7 @@
#define OSMX64_SRL 0x12 /* Shift right logical operation */
#define OSMX64_MOV_IMM 0x13 /* Data move operation from IMM */
#define OSMX64_HLT 0x14 /* Halt the processor */
+#define OSMX64_BR 0x15 /* Branch */
/*
* OSMX64 register definitions
diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h
index 6ffaf79..62183e0 100644
--- a/usr.bin/oasm/include/oasm/lex.h
+++ b/usr.bin/oasm/include/oasm/lex.h
@@ -96,6 +96,7 @@ typedef enum {
TT_MUL, /* 'mul' */
TT_DIV, /* 'div' */
TT_HLT, /* 'hlt' */
+ TT_BR, /* 'br' */
/* Register ops */
TT_MOV, /* 'mov' */
diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c
index 7b78d54..3ca9e8d 100644
--- a/usr.bin/oasm/lex.c
+++ b/usr.bin/oasm/lex.c
@@ -47,6 +47,7 @@ static char putback = '\0';
#define S_IMN_INC "inc"
#define S_IMN_DEC "dec"
#define S_IMN_HLT "hlt"
+#define S_IMN_BR "br"
/*
* Returns 0 if a char is counted as a
@@ -190,6 +191,19 @@ token_arith(char *p)
return TT_UNKNOWN;
}
+/*
+ * Control flow instructions
+ */
+static tt_t
+token_cfi(char *p)
+{
+ if (strcmp(p, S_IMN_BR) == 0) {
+ return TT_BR;
+ }
+
+ return TT_UNKNOWN;
+}
+
static tt_t
token_xreg(char *p)
{
@@ -293,6 +307,13 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
return 0;
}
+ /* Control flow instruction? */
+ if ((tok = token_cfi(p)) != TT_UNKNOWN) {
+ ttp->type = tok;
+ ttp->raw = p;
+ return 0;
+ }
+
/* Register? */
if ((tok = token_reg(p)) != TT_UNKNOWN) {
ttp->is_reg = 1;
diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c
index 9a4dd0e..e288d6e 100644
--- a/usr.bin/oasm/parse.c
+++ b/usr.bin/oasm/parse.c
@@ -43,6 +43,7 @@ static const char *tokstr[] = {
[ TT_MUL ] = "mul",
[ TT_DIV ] = "div",
[ TT_HLT ] = "hlt",
+ [ TT_BR ] = "br",
[ TT_COMMA ] = ",",
[ TT_INC ] = "inc",
[ TT_DEC ] = "dec",
@@ -112,6 +113,7 @@ parse_reg(struct oasm_state *state, struct oasm_token *tok)
case TT_SUB:
case TT_MUL:
case TT_DIV:
+ case TT_BR:
state->last = tok->type;
break;
default:
@@ -144,6 +146,10 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
int error;
switch (tok->type) {
+ case TT_BR:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
case TT_HLT:
state->last = tok->type;
emit_osmx64(&emit_state, tok);