diff options
author | Bobby Bingham <koorogi@koorogi.info> | 2017-01-18 18:55:22 -0600 |
---|---|---|
committer | Bobby Bingham <koorogi@koorogi.info> | 2017-01-18 18:55:22 -0600 |
commit | 619c5cc4ab4bd94f9b270a9868bb5f08ffd76acb (patch) | |
tree | 5d58bf4f4cc66621f5327edb30ba593c72b4c5da /parser.y | |
parent | 17d11b2e0af9cbbe9e2c02c4d1a3d2cb4509701e (diff) |
nqasm: allow specifying labels for operands
Diffstat (limited to 'parser.y')
-rw-r--r-- | parser.y | 64 |
1 files changed, 24 insertions, 40 deletions
@@ -13,7 +13,8 @@ void yyerror(const char *msg) #define INSTRUCTION_ARGS(m,a) (struct instruction) { MNEM_ ## m, a } #define INSTRUCTION(m) (struct instruction) { MNEM_ ## m } #define MKARGS(...) (struct arguments) { { __VA_ARGS__ } } -#define MKARGVALUE(v) (struct argument) { .value = (v) } +#define MKARGVALUE(v) (struct argument) { .type = ARG_INTEGER, .value = (v) } +#define MKARGLABEL(l) (struct argument) { .type = ARG_LABEL, .label = (l) } %} @@ -84,16 +85,14 @@ void yyerror(const char *msg) %token T_COLON ":" %type <inst> inst -%type <arg> imm -%type <arg> pcoff +%type <arg> expr %type <args> a_reg2 %type <args> a_reg3 %type <args> a_regp %type <args> a_regp_reg %type <args> a_reg_regp -%type <args> a_reg_imm -%type <args> a_reg_pcoff -%type <args> a_pcoff +%type <args> a_reg_expr +%type <args> a_expr %type <lval> intlit %% @@ -136,17 +135,17 @@ inst: | T_LD_BL a_reg_regp { $$ = INSTRUCTION_ARGS(LD_BL, $2); } | T_LD_BH a_reg_regp { $$ = INSTRUCTION_ARGS(LD_BH, $2); } | T_LD_W a_reg_regp { $$ = INSTRUCTION_ARGS(LD_W, $2); } - | T_LDI_BL a_reg_imm { $$ = INSTRUCTION_ARGS(LDI_BL, $2); } - | T_LDI_BH a_reg_imm { $$ = INSTRUCTION_ARGS(LDI_BH, $2); } - | T_BEQ a_pcoff { $$ = INSTRUCTION_ARGS(BEQ, $2); } - | T_BNE a_pcoff { $$ = INSTRUCTION_ARGS(BNE, $2); } - | T_BGT a_pcoff { $$ = INSTRUCTION_ARGS(BGT, $2); } - | T_BGE a_pcoff { $$ = INSTRUCTION_ARGS(BGE, $2); } - | T_BLT a_pcoff { $$ = INSTRUCTION_ARGS(BLT, $2); } - | T_BLE a_pcoff { $$ = INSTRUCTION_ARGS(BLE, $2); } - | T_BRA a_pcoff { $$ = INSTRUCTION_ARGS(BRA, $2); } + | T_LDI_BL a_reg_expr { $$ = INSTRUCTION_ARGS(LDI_BL, $2); } + | T_LDI_BH a_reg_expr { $$ = INSTRUCTION_ARGS(LDI_BH, $2); } + | T_BEQ a_expr { $$ = INSTRUCTION_ARGS(BEQ, $2); } + | T_BNE a_expr { $$ = INSTRUCTION_ARGS(BNE, $2); } + | T_BGT a_expr { $$ = INSTRUCTION_ARGS(BGT, $2); } + | T_BGE a_expr { $$ = INSTRUCTION_ARGS(BGE, $2); } + | T_BLT a_expr { $$ = INSTRUCTION_ARGS(BLT, $2); } + | T_BLE a_expr { $$ = INSTRUCTION_ARGS(BLE, $2); } + | T_BRA a_expr { $$ = INSTRUCTION_ARGS(BRA, $2); } | T_JMP a_regp { $$ = INSTRUCTION_ARGS(JMP, $2); } - | T_ADDPC a_reg_pcoff { $$ = INSTRUCTION_ARGS(ADDPC, $2); } + | T_ADDPC a_reg_expr { $$ = INSTRUCTION_ARGS(ADDPC, $2); } | T_NOP { $$ = INSTRUCTION(NOP); } ; @@ -160,30 +159,15 @@ a_regp_reg: T_SPACE T_REGPTR "," T_REG { $$ = MKARGS($2, $4); } a_reg_regp: T_SPACE T_REG "," T_REGPTR { $$ = MKARGS($2, $4); } -a_reg_imm: - T_SPACE T_REG "," imm { $$ = MKARGS($2, $4); } -a_reg_pcoff: - T_SPACE T_REG "," pcoff { $$ = MKARGS($2, $4); } -a_pcoff: - T_SPACE pcoff { $$ = MKARGS($2); } - -pcoff: intlit { - if (-0x80 > $1 || $1 > 0x7f) { - yyerror("pc offset operand out of range"); - YYERROR; - } else { - $$ = MKARGVALUE((uint8_t) $1); - } - } - -imm: intlit { - if (-0x80 > $1 || $1 > 0xff) { - yyerror("immediate operand out of range"); - YYERROR; - } else { - $$ = MKARGVALUE((uint8_t) $1); - } - } +a_reg_expr: + T_SPACE T_REG "," expr { $$ = MKARGS($2, $4); } +a_expr: + T_SPACE expr { $$ = MKARGS($2); } + +expr: + intlit { $$ = MKARGVALUE($1); } + | T_LABEL { $$ = MKARGLABEL($1); } + ; intlit: T_INT | T_BADINT { |