From 1e0d0202e614f398f491624c84ae9a8309fa2f96 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Wed, 18 Jan 2017 19:20:20 -0600 Subject: nqasm: allow "." to refer to current pc in immediate operand --- lexer.l | 1 + nqasm.c | 8 ++++---- nqasm.h | 1 + parser.y | 3 +++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lexer.l b/lexer.l index 5c8ebfb..053ac4e 100644 --- a/lexer.l +++ b/lexer.l @@ -74,6 +74,7 @@ HEX [0-9a-f] addpc/{EOI} { BEGIN(args); return T_ADDPC; } nop/{EOI} { BEGIN(args); return T_NOP; } +\. { return T_DOT; } @ { return T_DEREF; } [-+]?{DEC}+ { return intlit(yytext, 10, &yylval.lval); } 0x{HEX}+ { return intlit(yytext+2, 16, &yylval.lval); } diff --git a/nqasm.c b/nqasm.c index 0b0bbd8..ee3010a 100644 --- a/nqasm.c +++ b/nqasm.c @@ -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: diff --git a/nqasm.h b/nqasm.h index f2e036d..0bc51f7 100644 --- a/nqasm.h +++ b/nqasm.h @@ -3,6 +3,7 @@ #define ARG_INTEGER 0 #define ARG_LABEL 1 +#define ARG_PC 2 struct argument { int type; diff --git a/parser.y b/parser.y index e698e47..2ec2264 100644 --- a/parser.y +++ b/parser.y @@ -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 @@ -178,6 +180,7 @@ reg: T_LABEL { expr: intlit { $$ = MKARGVALUE($1); } | T_LABEL { $$ = MKARGLABEL($1); } + | "." { $$ = MKARGTYPE(ARG_PC); } ; intlit: T_INT -- cgit v1.2.3