summaryrefslogtreecommitdiff
path: root/usr.bin/oasm/lex.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/oasm/lex.c')
-rw-r--r--usr.bin/oasm/lex.c118
1 files changed, 112 insertions, 6 deletions
diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c
index f8427e0..dcd7c1f 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';
@@ -42,9 +43,30 @@ static char putback = '\0';
#define S_IMN_MOV "mov"
#define S_IMN_ADD "add"
#define S_IMN_SUB "sub"
+#define S_IMN_MUL "mul"
#define S_IMN_DIV "div"
#define S_IMN_INC "inc"
#define S_IMN_DEC "dec"
+#define S_IMN_HLT "hlt"
+#define S_IMN_BR "br"
+#define S_IMN_MROB "mrob"
+#define S_IMN_MROW "mrow"
+#define S_IMN_MROD "mrod"
+#define S_IMN_MROQ "mroq"
+
+/* Instruction length */
+#define OSMX64_INST_LEN 4
+
+/*
+ * Update the state when the caller encounters
+ * a newline.
+ */
+static inline void
+lex_newline(struct oasm_state *state)
+{
+ ++state->line;
+ state->pip += OSMX64_INST_LEN;
+}
/*
* Returns 0 if a char is counted as a
@@ -59,7 +81,7 @@ lex_skippable(struct oasm_state *state, char c)
case '\t': return 0;
case '\r': return 0;
case '\n':
- ++state->line;
+ lex_newline(state);
return 0;
}
@@ -150,6 +172,10 @@ lex_nomstr(struct oasm_state *state, char **res)
retval = tmp;
break;
}
+ if (tmp == ':') {
+ retval = tmp;
+ break;
+ }
if (tmp == '\n') {
++state->line;
retval = tmp;
@@ -179,6 +205,58 @@ 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;
+ } else if (strcmp(p, S_IMN_MUL) == 0) {
+ return TT_MUL;
+ }
+
+ 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;
+}
+
+/*
+ * Bitwise MRO instructions
+ */
+static tt_t
+token_bitw_mro(char *p)
+{
+ if (strcmp(p, S_IMN_MROB) == 0) {
+ return TT_MROB;
+ } else if (strcmp(p, S_IMN_MROW) == 0) {
+ return TT_MROW;
+ } else if (strcmp(p, S_IMN_MROD) == 0) {
+ return TT_MROD;
+ } else if (strcmp(p, S_IMN_MROQ) == 0) {
+ return TT_MROQ;
+ }
+
+ return TT_UNKNOWN;
+}
+
+/*
+ * Bitwise instructions
+ */
+static tt_t
+token_bitw(char *p)
+{
+ tt_t token;
+
+ token = token_bitw_mro(p);
+ if (token != TT_UNKNOWN) {
+ return token;
}
return TT_UNKNOWN;
@@ -248,6 +326,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;
@@ -256,18 +335,24 @@ 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) {
case '\n':
- ++state->line;
+ lex_newline(state);
return 0;
case '\0':
return -1;
@@ -278,7 +363,14 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
ttp->raw = NULL;
lex_putback(c);
- lex_nomstr(state, &p);
+ c = lex_nomstr(state, &p);
+
+ while (c == ':') {
+ ttp->type = TT_LABEL;
+ ttp->raw = p;
+ state->label_ip = state->pip;
+ return 0;
+ }
/* Arithmetic operation? */
if ((tok = token_arith(p)) != TT_UNKNOWN) {
@@ -287,6 +379,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;
@@ -295,6 +394,12 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
return 0;
}
+ if ((tok = token_bitw(p)) != TT_UNKNOWN) {
+ ttp->type = tok;
+ ttp->raw = p;
+ return 0;
+ }
+
/* Immediate operand? */
if ((tok = token_operand(p)) != TT_UNKNOWN) {
if (tok == TT_IMM) {
@@ -305,6 +410,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
ttp->raw = p;
return 0;
}
+
oasm_err("bad token \"%s\"\n", p);
lex_try_free(p);
return -1;