From fef46ee8affe5e8cf4c10d251a16ddac72c1584f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Quei=C3=9Fner?= Date: Thu, 30 Jun 2016 17:35:01 +0200 Subject: [PATCH] Improves assembler: Now has support for modifiers. --- libvm/as/as.c | 109 +++++++++++++++++++++++++++++++++++++++++++--- libvm/as/tokens.c | 96 ++++++++++++++++++++-------------------- libvm/as/tokens.y | 2 +- 3 files changed, 153 insertions(+), 54 deletions(-) diff --git a/libvm/as/as.c b/libvm/as/as.c index 613dfc5..f6db6fa 100644 --- a/libvm/as/as.c +++ b/libvm/as/as.c @@ -57,17 +57,23 @@ int listSymbols = 0; // current token int tok; +void apply_modifier(instruction_t *i, const char *mod); + void apply_modifiers(instruction_t *i) { while(tok == TOK_MOD) { - printf("modifier, "); + char *s = yytext + 1; + int len = strlen(s); + s[len - 1] = 0; + apply_modifier(i, s); tok = yylex(); } } + // line format: -// TOK_LABEL? TOK_MOD* TOK_MNEMONIC TOK_MOD* ()? TOK_MOD* TOK_NEWLINE +// TOK_LABEL? TOK_MOD* TOK_MNEMONIC TOK_MOD* (TOK_INT|TOK_HEX|TOK_CHAR|TOK_REF)? TOK_MOD* TOK_NEWLINE void assemble() { uint32_t index = 0; @@ -156,7 +162,6 @@ void assemble() // Increase command index by one index += 1; } - printf("NEXT\n"); } if(listSymbols) @@ -168,7 +173,7 @@ void assemble() } } - printf("Patches:\n"); + // printf("Patches:\n"); for(llist_t *it = patches; it != NULL; it = it->next) { uint32_t target = list_find(&labels, it->name); @@ -177,7 +182,7 @@ void assemble() fseek(output, sizeof(instruction_t) * it->value + 4, SEEK_SET); fwrite(&target, 1, sizeof(uint32_t), output); - printf("%d -> %d (%s)\n", it->value, target, it->name); + // printf("%d -> %d (%s)\n", it->value, target, it->name); } } @@ -238,4 +243,98 @@ int main(int argc, char **argv) } return 0; +} + +void apply_modifier(instruction_t *i, const char *mod) +{ + // printf("[%s]\n", mod); + if(strncmp("ci:", mod, 3) == 0) + { + i->cmdinfo = atoi(mod + 3); + return; + } + + if(strncmp("cmd:", mod, 4) == 0) + { + fprintf(stderr, "Command specification %s not supported yet.\n"); + exit(1); + } + + if(strcmp("f:yes", mod) == 0) + { + i->flags = 1; + } + else if(strcmp("f:no", mod) == 0) + { + i->flags = 0; + } + else if(strcmp("r:discard", mod) == 0) + { + i->output = VM_OUTPUT_DISCARD; + } + else if(strcmp("r:push", mod) == 0) + { + i->output = VM_OUTPUT_PUSH; + } + else if(strcmp("r:jump", mod) == 0) + { + i->output = VM_OUTPUT_JUMP; + } + else if(strcmp("r:jumpr", mod) == 0) + { + i->output = VM_OUTPUT_JUMPR; + } + else if(strcmp("i0:zero", mod) == 0) + { + i->input0 = VM_INPUT_ZERO; + } + else if(strcmp("i0:pop", mod) == 0) + { + i->input0 = VM_INPUT_POP; + } + else if(strcmp("i0:peek", mod) == 0) + { + i->input0 = VM_INPUT_PEEK; + } + else if(strcmp("i0:arg", mod) == 0) + { + i->input0 = VM_INPUT_ARG; + } + else if(strcmp("i1:zero", mod) == 0) + { + i->input1 = VM_INPUT_ZERO; + } + else if(strcmp("i1:pop", mod) == 0) + { + i->input1 = VM_INPUT_POP; + } + else if(strcmp("ex(z)=x", mod) == 0) + { + i->execZ = VM_EXEC_X; + } + else if(strcmp("ex(z)=0", mod) == 0) + { + i->execZ = VM_EXEC_0; + } + else if(strcmp("ex(z)=1", mod) == 0) + { + i->execZ = VM_EXEC_1; + } + else if(strcmp("ex(n)=x", mod) == 0) + { + i->execN = VM_EXEC_X; + } + else if(strcmp("ex(n)=0", mod) == 0) + { + i->execN = VM_EXEC_0; + } + else if(strcmp("ex(n)=1", mod) == 0) + { + i->execN = VM_EXEC_1; + } + else + { + fprintf(stderr, "Unknown modifier: [%s]\n", mod); + exit(1); + } } \ No newline at end of file diff --git a/libvm/as/tokens.c b/libvm/as/tokens.c index 3a576fb..7733dd3 100644 --- a/libvm/as/tokens.c +++ b/libvm/as/tokens.c @@ -385,11 +385,11 @@ struct yy_trans_info flex_int32_t yy_verify; flex_int32_t yy_nxt; }; -static yyconst flex_int16_t yy_accept[28] = +static yyconst flex_int16_t yy_accept[30] = { 0, - 0, 0, 13, 11, 4, 10, 11, 3, 3, 1, - 11, 5, 11, 11, 0, 3, 6, 0, 0, 1, - 9, 5, 0, 8, 2, 7, 0 + 0, 0, 13, 11, 4, 10, 11, 11, 3, 3, + 1, 11, 5, 11, 11, 0, 3, 3, 6, 0, + 0, 1, 9, 5, 0, 8, 2, 7, 0 } ; static yyconst YY_CHAR yy_ec[256] = @@ -398,16 +398,16 @@ static yyconst YY_CHAR yy_ec[256] = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4, 1, - 1, 1, 1, 1, 1, 1, 1, 5, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 7, 8, 1, - 1, 1, 1, 9, 10, 10, 10, 10, 10, 10, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 12, 1, 13, 1, 14, 1, 10, 10, 10, 10, + 1, 1, 1, 1, 5, 1, 1, 6, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 8, 9, 1, + 1, 1, 1, 10, 11, 11, 11, 11, 11, 11, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 13, 1, 14, 1, 15, 1, 11, 11, 11, 11, - 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 15, - 11, 11, 1, 1, 1, 1, 1, 1, 1, 1, + 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, + 12, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -424,50 +424,50 @@ static yyconst YY_CHAR yy_ec[256] = 1, 1, 1, 1, 1 } ; -static yyconst YY_CHAR yy_meta[16] = +static yyconst YY_CHAR yy_meta[17] = { 0, - 1, 1, 2, 1, 3, 3, 1, 1, 1, 3, - 3, 1, 4, 3, 3 + 1, 1, 2, 1, 1, 3, 3, 1, 1, 1, + 3, 3, 1, 4, 3, 3 } ; -static yyconst flex_uint16_t yy_base[32] = +static yyconst flex_uint16_t yy_base[34] = { 0, - 0, 0, 31, 61, 61, 61, 0, 11, 15, 0, - 0, 22, 0, 28, 20, 8, 61, 0, 39, 0, - 0, 0, 7, 61, 0, 61, 61, 49, 53, 16, - 57 + 0, 0, 38, 64, 64, 64, 0, 11, 13, 21, + 0, 0, 24, 0, 30, 30, 16, 17, 64, 0, + 41, 0, 0, 0, 13, 64, 0, 64, 64, 52, + 56, 23, 60 } ; -static yyconst flex_int16_t yy_def[32] = +static yyconst flex_int16_t yy_def[34] = { 0, - 27, 1, 27, 27, 27, 27, 28, 27, 8, 29, - 30, 27, 31, 12, 27, 8, 27, 14, 14, 29, - 30, 12, 31, 27, 19, 27, 0, 27, 27, 27, - 27 + 29, 1, 29, 29, 29, 29, 30, 29, 29, 9, + 31, 32, 29, 33, 13, 29, 29, 9, 29, 15, + 15, 31, 32, 13, 33, 29, 21, 29, 0, 29, + 29, 29, 29 } ; -static yyconst flex_uint16_t yy_nxt[77] = +static yyconst flex_uint16_t yy_nxt[81] = { 0, - 4, 5, 6, 7, 8, 9, 4, 10, 11, 12, - 12, 13, 4, 14, 12, 16, 16, 17, 21, 26, - 18, 18, 18, 24, 18, 19, 18, 18, 17, 18, - 27, 22, 22, 27, 27, 18, 22, 18, 18, 27, - 27, 27, 18, 25, 25, 27, 27, 27, 25, 15, - 27, 15, 15, 20, 27, 20, 20, 23, 23, 23, - 3, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 27, 27, 27, 27, 27, 27 + 4, 5, 6, 7, 8, 9, 10, 4, 11, 12, + 13, 13, 14, 4, 15, 13, 17, 17, 18, 18, + 19, 17, 17, 20, 20, 23, 28, 20, 21, 20, + 20, 19, 20, 26, 24, 24, 20, 29, 20, 24, + 20, 20, 29, 29, 29, 20, 27, 27, 29, 29, + 29, 27, 16, 29, 16, 16, 22, 29, 22, 22, + 25, 25, 25, 3, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 } ; -static yyconst flex_int16_t yy_chk[77] = +static yyconst flex_int16_t yy_chk[81] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 8, 8, 8, 30, 23, - 8, 8, 16, 15, 8, 8, 12, 12, 12, 9, - 3, 12, 12, 0, 0, 12, 12, 14, 14, 0, - 0, 0, 14, 19, 19, 0, 0, 0, 19, 28, - 0, 28, 28, 29, 0, 29, 29, 31, 31, 31, - 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 27, 27, 27, 27, 27, 27 + 1, 1, 1, 1, 1, 1, 8, 8, 9, 9, + 9, 17, 17, 9, 9, 32, 25, 9, 9, 13, + 13, 13, 18, 16, 13, 13, 10, 3, 13, 13, + 15, 15, 0, 0, 0, 15, 21, 21, 0, 0, + 0, 21, 30, 0, 30, 30, 31, 0, 31, 31, + 33, 33, 33, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 } ; static yy_state_type yy_last_accepting_state; @@ -735,13 +735,13 @@ yy_match: while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 28 ) + if ( yy_current_state >= 30 ) yy_c = yy_meta[(unsigned int) yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; ++yy_cp; } - while ( yy_base[yy_current_state] != 61 ); + while ( yy_base[yy_current_state] != 64 ); yy_find_action: yy_act = yy_accept[yy_current_state]; @@ -1120,7 +1120,7 @@ static int yy_get_next_buffer (void) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 28 ) + if ( yy_current_state >= 30 ) yy_c = yy_meta[(unsigned int) yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; @@ -1148,11 +1148,11 @@ static int yy_get_next_buffer (void) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 28 ) + if ( yy_current_state >= 30 ) yy_c = yy_meta[(unsigned int) yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; - yy_is_jam = (yy_current_state == 27); + yy_is_jam = (yy_current_state == 29); return yy_is_jam ? 0 : yy_current_state; } diff --git a/libvm/as/tokens.y b/libvm/as/tokens.y index 0935dc3..c5d5a95 100644 --- a/libvm/as/tokens.y +++ b/libvm/as/tokens.y @@ -7,7 +7,7 @@ whitespace [ \t] newline [\n] digit [0-9] hexdigit [0-9a-fA-F] -intnum {digit}+ +intnum -?{digit}+ hexnum "0x"{hexdigit}+ mnemonic [a-zA-Z]+ labelname [a-zA-Z0-9_]+