summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-07-24 17:00:55 -0400
committerIan Moffett <ian@osmora.org>2025-07-24 17:04:10 -0400
commitaa3820495e8c2f103f82923240e7936ca78fab84 (patch)
treefe1441e28054a3e7b7300e2597df9444ac6f018b
parent24dabe54c845b88ccd4842eb0e7c0fa097a7beaf (diff)
oasm: Introduce encoding for the NOP instruction
The NOP (no operation) instruction simply tells the processor to do nothing. This can be useful for various things such as padding, timing, etc. -- nop nop nop ... -- Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--etc/oemu/test-00.s3
-rw-r--r--usr.bin/oasm/emit.c23
-rw-r--r--usr.bin/oasm/include/oasm/lex.h1
-rw-r--r--usr.bin/oasm/lex.c8
-rw-r--r--usr.bin/oasm/parse.c5
5 files changed, 39 insertions, 1 deletions
diff --git a/etc/oemu/test-00.s b/etc/oemu/test-00.s
index ca0094d..ccd7da1 100644
--- a/etc/oemu/test-00.s
+++ b/etc/oemu/test-00.s
@@ -12,4 +12,5 @@ some_label: ! X
inc x1 ! ~ 0x00000018
add x2, #3 ! ~ 0x0000001C
mrow x4, #0 ! ~ 0x00000020
- hlt ! ~ 0x00000024
+ nop ! ~ 0x00000024
+ hlt ! ~ 0x00000028
diff --git a/usr.bin/oasm/emit.c b/usr.bin/oasm/emit.c
index 3261522..139cf47 100644
--- a/usr.bin/oasm/emit.c
+++ b/usr.bin/oasm/emit.c
@@ -357,6 +357,26 @@ emit_encode_mro(struct emit_state *state, struct oasm_token *tok)
return TAILQ_NEXT(tok, link);
}
+/*
+ * Encode a NOP instruction
+ *
+ * 'nop' - no operands
+ *
+ * Returns the next token on success,
+ * otherwise NULL.
+ */
+static struct oasm_token *
+emit_encode_nop(struct emit_state *state, struct oasm_token *tok)
+{
+ inst_t curinst;
+
+ curinst.opcode = OSMX64_NOP;
+ curinst.rd = 0;
+ 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)
{
@@ -428,6 +448,9 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit)
curtok = TAILQ_FIRST(&emit->ir);
while (curtok != NULL) {
switch (curtok->type) {
+ case TT_NOP:
+ curtok = emit_encode_nop(emit, curtok);
+ break;
case TT_MOV:
curtok = emit_encode_mov(emit, curtok);
break;
diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h
index b392403..375771e 100644
--- a/usr.bin/oasm/include/oasm/lex.h
+++ b/usr.bin/oasm/include/oasm/lex.h
@@ -90,6 +90,7 @@ struct oasm_state;
*/
typedef enum {
TT_UNKNOWN, /* Unknown token */
+ TT_NOP, /* No operation */
/* Arithmetic instructions */
TT_ADD, /* 'add' */
diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c
index dcd7c1f..fea9dc3 100644
--- a/usr.bin/oasm/lex.c
+++ b/usr.bin/oasm/lex.c
@@ -40,6 +40,7 @@
static char putback = '\0';
/* Instruction mnemonic strings */
+#define S_IMN_NOP "nop"
#define S_IMN_MOV "mov"
#define S_IMN_ADD "add"
#define S_IMN_SUB "sub"
@@ -372,6 +373,13 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
return 0;
}
+ /* No operation? */
+ if (strcmp(p, S_IMN_NOP) == 0) {
+ ttp->type = TT_NOP;
+ ttp->raw = p;
+ return 0;
+ }
+
/* Arithmetic operation? */
if ((tok = token_arith(p)) != TT_UNKNOWN) {
ttp->type = tok;
diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c
index 229455b..ba97306 100644
--- a/usr.bin/oasm/parse.c
+++ b/usr.bin/oasm/parse.c
@@ -39,6 +39,7 @@
static struct emit_state emit_state;
static const char *tokstr[] = {
[ TT_UNKNOWN] = "bad",
+ [ TT_NOP ] = "nop",
[ TT_ADD ] = "add",
[ TT_SUB ] = "sub",
[ TT_MUL ] = "mul",
@@ -154,6 +155,10 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
int error;
switch (tok->type) {
+ case TT_NOP:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
case TT_BR:
state->last = tok->type;
emit_osmx64(&emit_state, tok);