summaryrefslogtreecommitdiff
path: root/usr.bin/oasm
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/oasm')
-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.c35
-rw-r--r--usr.bin/oasm/parse.c6
5 files changed, 82 insertions, 3 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..a33a570 100644
--- a/usr.bin/oasm/lex.c
+++ b/usr.bin/oasm/lex.c
@@ -34,6 +34,7 @@
#include <oasm/lex.h>
#include <oasm/log.h>
+#define COMMENT '!'
#define is_num(c) ((c) >= '0' && (c) <= '9')
static char putback = '\0';
@@ -47,6 +48,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 +192,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)
{
@@ -254,6 +269,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
{
char *p = NULL;
char c = ' ';
+ short in_comment = 0;
int tmp;
tt_t tok;
@@ -262,13 +278,19 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
}
/*
- * Grab characters. If they are skippable,
- * don't use them.
+ * Grab characters. If they are skippable or
+ * comments, don't use them.
*/
- while (lex_skippable(state, c) == 0) {
+ while (lex_skippable(state, c) == 0 || in_comment) {
if ((c = lex_cin(state)) == 0) {
return -1;
}
+
+ if (c == COMMENT) {
+ in_comment = 1;
+ } else if (c == '\n') {
+ in_comment = 0;
+ }
}
switch (c) {
@@ -293,6 +315,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);