diff options
author | Ian Moffett <ian@osmora.org> | 2025-07-21 09:17:30 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-07-21 09:17:30 -0400 |
commit | 3ab5f07bac34bc9de5b9038407353c707f3f4c2f (patch) | |
tree | 57b028642f4fc81d839994132eda2859d6255162 | |
parent | 60c8f4c34194fcec0e9e7d8da9cd1ad8b38707e7 (diff) |
oasm: Parse and encode the "HLT" instruction
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r-- | usr.bin/oasm/emit.c | 23 | ||||
-rw-r--r-- | usr.bin/oasm/include/oasm/emit.h | 1 | ||||
-rw-r--r-- | usr.bin/oasm/include/oasm/lex.h | 1 | ||||
-rw-r--r-- | usr.bin/oasm/lex.c | 3 | ||||
-rw-r--r-- | usr.bin/oasm/parse.c | 4 |
5 files changed, 32 insertions, 0 deletions
diff --git a/usr.bin/oasm/emit.c b/usr.bin/oasm/emit.c index ee13582..cef90d7 100644 --- a/usr.bin/oasm/emit.c +++ b/usr.bin/oasm/emit.c @@ -220,6 +220,26 @@ emit_encode_add(struct emit_state *state, struct oasm_token *tok) return TAILQ_NEXT(tok, link); } +/* + * Encode a HLT instruction + * + * 'hlt' - no operands + * + * Returns the next token on success, + * otherwise NULL. + */ +static struct oasm_token * +emit_encode_hlt(struct emit_state *state, struct oasm_token *tok) +{ + inst_t curinst; + + curinst.opcode = OSMX64_HLT; + curinst.rd = 0; + curinst.unused = 0; + emit_bytes(state, &curinst, sizeof(curinst)); + return TAILQ_NEXT(tok, link); +} + int emit_osxm64(struct emit_state *state, struct oasm_token *tp) { @@ -301,6 +321,9 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit) case TT_ADD: curtok = emit_encode_add(emit, curtok); break; + case TT_HLT: + curtok = emit_encode_hlt(emit, curtok); + break; default: curtok = TAILQ_NEXT(curtok, link); break; diff --git a/usr.bin/oasm/include/oasm/emit.h b/usr.bin/oasm/include/oasm/emit.h index f4b4f77..b0a2fd1 100644 --- a/usr.bin/oasm/include/oasm/emit.h +++ b/usr.bin/oasm/include/oasm/emit.h @@ -60,6 +60,7 @@ #define OSMX64_SLL 0x11 /* Shift left logical operation */ #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 */ /* * OSMX64 register definitions diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h index 28ad52c..6ffaf79 100644 --- a/usr.bin/oasm/include/oasm/lex.h +++ b/usr.bin/oasm/include/oasm/lex.h @@ -95,6 +95,7 @@ typedef enum { TT_SUB, /* 'sub' */ TT_MUL, /* 'mul' */ TT_DIV, /* 'div' */ + TT_HLT, /* 'hlt' */ /* Register ops */ TT_MOV, /* 'mov' */ diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c index f8427e0..b3af2b1 100644 --- a/usr.bin/oasm/lex.c +++ b/usr.bin/oasm/lex.c @@ -45,6 +45,7 @@ static char putback = '\0'; #define S_IMN_DIV "div" #define S_IMN_INC "inc" #define S_IMN_DEC "dec" +#define S_IMN_HLT "hlt" /* * Returns 0 if a char is counted as a @@ -179,6 +180,8 @@ token_arith(char *p) return TT_SUB; } else if (strcmp(p, S_IMN_DIV) == 0) { return TT_DIV; + } else if (strcmp(p, S_IMN_HLT) == 0) { + return TT_HLT; } return TT_UNKNOWN; diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c index f87f903..6851935 100644 --- a/usr.bin/oasm/parse.c +++ b/usr.bin/oasm/parse.c @@ -42,6 +42,7 @@ static const char *tokstr[] = { [ TT_SUB ] = "sub", [ TT_MUL ] = "mul", [ TT_DIV ] = "div", + [ TT_HLT ] = "hlt", [ TT_COMMA ] = ",", [ TT_INC ] = "inc", [ TT_DEC ] = "dec", @@ -140,6 +141,9 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok) int error; switch (tok->type) { + case TT_HLT: + state->last = tok->type; + emit_osxm64(&emit_state, tok); case TT_MOV: state->last = tok->type; emit_osxm64(&emit_state, tok); |