summaryrefslogtreecommitdiff
path: root/usr.bin/oasm
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/oasm')
-rw-r--r--usr.bin/oasm/emit.c72
-rw-r--r--usr.bin/oasm/include/oasm/lex.h5
-rw-r--r--usr.bin/oasm/lex.c9
-rw-r--r--usr.bin/oasm/parse.c26
4 files changed, 105 insertions, 7 deletions
diff --git a/usr.bin/oasm/emit.c b/usr.bin/oasm/emit.c
index 139cf47..63f716a 100644
--- a/usr.bin/oasm/emit.c
+++ b/usr.bin/oasm/emit.c
@@ -377,6 +377,71 @@ emit_encode_nop(struct emit_state *state, struct oasm_token *tok)
return TAILQ_NEXT(tok, link);
}
+/*
+ * Encode a bitwise instruction:
+ *
+ * and r, r/imm
+ * or r, r/imm
+ * xor r, r/imm
+ */
+static struct oasm_token *
+emit_encode_bitw(struct emit_state *state, struct oasm_token *tok)
+{
+ inst_t curinst;
+ imm_t imm;
+ reg_t rd;
+ uint8_t opcode = OSMX64_AND;
+ char *inst_str = "and";
+
+ switch (tok->type) {
+ case TT_OR:
+ opcode = OSMX64_OR;
+ inst_str = "or";
+ break;
+ case TT_XOR:
+ opcode = OSMX64_XOR;
+ inst_str = "xor";
+ break;
+ }
+
+ /* Next token should be a register */
+ tok = TAILQ_NEXT(tok, link);
+ if (tok == NULL) {
+ oasm_err("[emit error]: expected register for '%s'\n", inst_str);
+ return NULL;
+ }
+ if (!tok_is_xreg(tok->type)) {
+ oasm_err("[emit error]: bad register for '%s'\n", inst_str);
+ return NULL;
+ }
+
+ rd = ir_to_reg(tok->type);
+ tok = TAILQ_NEXT(tok, link);
+ if (tok == NULL) {
+ oasm_err("[emit error]: missing operand in '%s'\n", inst_str);
+ return NULL;
+ }
+
+ /*
+ * Check that the next token is an immediate
+ * value.
+ *
+ * TODO: Allow a register operand to be passed
+ * to these instructions.
+ */
+ if (tok->type != TT_IMM) {
+ oasm_err("[emit error]: expected <imm> for '%s'\n", inst_str);
+ return NULL;
+ }
+
+ imm = tok->imm;
+ curinst.opcode = opcode;
+ curinst.rd = rd;
+ curinst.imm = imm;
+ emit_bytes(state, &curinst, sizeof(curinst));
+ return TAILQ_NEXT(tok, link);
+}
+
int
emit_osmx64(struct emit_state *state, struct oasm_token *tp)
{
@@ -464,6 +529,11 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit)
case TT_DIV:
curtok = emit_encode_arith(emit, curtok);
break;
+ case TT_AND:
+ case TT_OR:
+ case TT_XOR:
+ curtok = emit_encode_bitw(emit, curtok);
+ break;
case TT_BR:
curtok = emit_encode_br(emit, curtok);
break;
@@ -471,7 +541,7 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit)
curtok = emit_encode_hlt(emit, curtok);
break;
default:
- if (lex_is_mro(curtok->type)) {
+ if (tok_is_mro(curtok->type)) {
curtok = emit_encode_mro(emit, curtok);
break;
}
diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h
index 375771e..4c81bc4 100644
--- a/usr.bin/oasm/include/oasm/lex.h
+++ b/usr.bin/oasm/include/oasm/lex.h
@@ -103,6 +103,9 @@ typedef enum {
TT_MROW, /* 'mrow' */
TT_MROD, /* 'mrod' */
TT_MROQ, /* 'mroq' */
+ TT_AND, /* 'and' */
+ TT_OR, /* 'or' */
+ TT_XOR, /* 'xor' */
/* Register ops */
TT_MOV, /* 'mov' */
@@ -167,7 +170,7 @@ tok_is_xreg(tt_t tok)
* instruction. Returns true on match.
*/
__always_inline static inline bool
-lex_is_mro(tt_t tok)
+tok_is_mro(tt_t tok)
{
switch (tok) {
case TT_MROB:
diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c
index fea9dc3..ce97400 100644
--- a/usr.bin/oasm/lex.c
+++ b/usr.bin/oasm/lex.c
@@ -54,6 +54,9 @@ static char putback = '\0';
#define S_IMN_MROW "mrow"
#define S_IMN_MROD "mrod"
#define S_IMN_MROQ "mroq"
+#define S_IMN_AND "and"
+#define S_IMN_OR "or"
+#define S_IMN_XOR "xor"
/* Instruction length */
#define OSMX64_INST_LEN 4
@@ -210,6 +213,8 @@ token_arith(char *p)
return TT_HLT;
} else if (strcmp(p, S_IMN_MUL) == 0) {
return TT_MUL;
+ } else if (strcmp(p, S_IMN_XOR) == 0) {
+ return TT_XOR;
}
return TT_UNKNOWN;
@@ -242,6 +247,10 @@ token_bitw_mro(char *p)
return TT_MROD;
} else if (strcmp(p, S_IMN_MROQ) == 0) {
return TT_MROQ;
+ } else if (strcmp(p, S_IMN_AND) == 0) {
+ return TT_AND;
+ } else if (strcmp(p, S_IMN_OR) == 0) {
+ return TT_OR;
}
return TT_UNKNOWN;
diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c
index ba97306..ce5a446 100644
--- a/usr.bin/oasm/parse.c
+++ b/usr.bin/oasm/parse.c
@@ -58,7 +58,9 @@ static const char *tokstr[] = {
[ TT_MROW ] = "mrow",
[ TT_MROD ] = "mrod",
[ TT_MROQ ] = "mroq",
-
+ [ TT_AND ] = "and",
+ [ TT_OR ] = "or",
+ [ TT_XOR ] = "xor",
/* X<n> registers */
[ TT_X0 ] = "x0",
@@ -124,16 +126,18 @@ parse_reg(struct oasm_state *state, struct oasm_token *tok)
case TT_MUL:
case TT_DIV:
case TT_BR:
+ case TT_AND:
+ case TT_OR:
+ case TT_XOR:
state->last = tok->type;
break;
default:
- if (lex_is_mro(state->last)) {
- state->last = tok->type;
+ if (tok_is_mro(state->last)) {
break;
}
p = tokstr[state->last];
- oasm_err("bad instruction '%s' for regop\n", p);
+ oasm_err("bad token '%s' for regop\n", p);
return -1;
}
@@ -167,6 +171,18 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
state->last = tok->type;
label_enter(tok->raw, state->pip);
break;
+ case TT_AND:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
+ case TT_OR:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
+ case TT_XOR:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
case TT_HLT:
state->last = tok->type;
emit_osmx64(&emit_state, tok);
@@ -205,7 +221,7 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
emit_osmx64(&emit_state, tok);
break;
default:
- if (lex_is_mro(tok->type)) {
+ if (tok_is_mro(tok->type)) {
state->last = tok->type;
emit_osmx64(&emit_state, tok);
return 0;