diff options
author | Bobby Bingham <koorogi@koorogi.info> | 2017-01-18 19:20:20 -0600 |
---|---|---|
committer | Bobby Bingham <koorogi@koorogi.info> | 2017-01-18 19:20:20 -0600 |
commit | 1e0d0202e614f398f491624c84ae9a8309fa2f96 (patch) | |
tree | 1000df0a8eb39453ac48a14809dd250230f67e6c | |
parent | 2e0de3cc53d031ac2fa48eddc9a472b0e4c34a45 (diff) |
nqasm: allow "." to refer to current pc in immediate operand
-rw-r--r-- | lexer.l | 1 | ||||
-rw-r--r-- | nqasm.c | 8 | ||||
-rw-r--r-- | nqasm.h | 1 | ||||
-rw-r--r-- | parser.y | 3 |
4 files changed, 9 insertions, 4 deletions
@@ -74,6 +74,7 @@ HEX [0-9a-f] <inst>addpc/{EOI} { BEGIN(args); return T_ADDPC; } <inst>nop/{EOI} { BEGIN(args); return T_NOP; } +<args>\. { return T_DOT; } <args>@ { return T_DEREF; } <args>[-+]?{DEC}+ { return intlit(yytext, 10, &yylval.lval); } <args>0x{HEX}+ { return intlit(yytext+2, 16, &yylval.lval); } @@ -56,12 +56,12 @@ void add_instruction(const struct instruction *inst) addr += 2; } -static long eval_argument(const struct argument *arg) +static long eval_argument(const struct argument *arg, uint16_t pc) { struct label *l; switch (arg->type) { - case ARG_INTEGER: - return arg->value; + case ARG_INTEGER: return arg->value; + case ARG_PC: return pc; case ARG_LABEL: l = vector_search(&labels->v, &(struct label) { .name = arg->label }, cmp_label); @@ -82,7 +82,7 @@ static void assemble_instruction(const struct instruction *inst, uint16_t pc) const struct operand *ops = mnem->operands; const struct argument *args = inst->args.args; for (int i = 0; i < 3 && ops[i].type; i++) { - long value = eval_argument(args + i); + long value = eval_argument(args + i, pc); switch (ops[i].type) { case REG: @@ -3,6 +3,7 @@ #define ARG_INTEGER 0 #define ARG_LABEL 1 +#define ARG_PC 2 struct argument { int type; @@ -15,6 +15,7 @@ void yyerror(const char *msg) #define MKARGS(...) (struct arguments) { { __VA_ARGS__ } } #define MKARGVALUE(v) (struct argument) { .type = ARG_INTEGER, .value = (v) } #define MKARGLABEL(l) (struct argument) { .type = ARG_LABEL, .label = (l) } +#define MKARGTYPE(t) (struct argument) { .type = (t) } %} @@ -80,6 +81,7 @@ void yyerror(const char *msg) %token T_COMMA "," %token T_COLON ":" +%token T_DOT "." %token T_DEREF "@" %type <inst> inst @@ -178,6 +180,7 @@ reg: T_LABEL { expr: intlit { $$ = MKARGVALUE($1); } | T_LABEL { $$ = MKARGLABEL($1); } + | "." { $$ = MKARGTYPE(ARG_PC); } ; intlit: T_INT |