diff options
| -rw-r--r-- | examples/instructions.s | 43 | ||||
| -rw-r--r-- | src/lexer.l | 6 | ||||
| -rw-r--r-- | src/parser.y | 60 |
3 files changed, 75 insertions, 34 deletions
diff --git a/examples/instructions.s b/examples/instructions.s index f16e6c1..5379657 100644 --- a/examples/instructions.s +++ b/examples/instructions.s @@ -1,8 +1,35 @@ -movl $4, %eax -movl $1, %ebx -movl $1, %eax -movl $0, %ebx -int $0x80 - -movl $str, %ecx -movl $str_len, %edx +# R-Type +add x5, x6, x7 +sub x10, x11, x12 +and x1, x2, x3 +sll x8, x9, x10o + +# I-Type +addi x5, x6, 10 +andi x1, x2, 3 + +# I-Type +lw x5, 0(x6) +lh x3, 4(x8) + +# I-Type +jalr x1, 0(x5) + +# S-Type +sw x5, 0(x6) +sb x3, 8(x4) +sh x10, 12(x2) + +# B-Type +beq x5, x6, loop +bne x1, x2, done +blt x3, x4, less +bge x7, x8, end + +# U-Type +lui x5, 0x12345 +auipc x10, 0x20000 + +# J-Type +jal x1, function +jal x0, loop diff --git a/src/lexer.l b/src/lexer.l index 46f6bc6..26be828 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -8,7 +8,7 @@ extern size_t line_number; %option noyywrap %% ^[a-zA-Z]+ { yylval.symbol = strdup(yytext); return T_INSTRUCTION; } -%*[a-zA-Z\_\-]+[a-zA-Z0-9\_\-]* { yylval.symbol = strdup(yytext); return T_LABEL; } +%*[a-zA-Z\_\-]+[a-zA-Z0-9\_\-]* { yylval.symbol = strdup(yytext); return T_SYMBOL; } 0x[0-9A-Fa-f]+ { yylval.i_val = atoi(yytext); return T_INTEGER; } [0-9]+ { yylval.i_val = atoi(yytext); return T_INTEGER; } -*[0-9]*\.*[0-9]* { yylval.i_val = atof(yytext); return T_FLOAT; } @@ -21,7 +21,9 @@ extern size_t line_number; return T_STRING; } , { return T_COMMA; } -\$ { return T_IMMEDIATE; } +\( { return T_OPENPAREN; } +\) { return T_CLOSEPAREN; } +#.* ; [ \t] ; \n { ++line_number; return T_ENDL; } . ; diff --git a/src/parser.y b/src/parser.y index 719643d..e63935a 100644 --- a/src/parser.y +++ b/src/parser.y @@ -22,11 +22,12 @@ void yyerror(const char *s); %token <i_val> T_INTEGER %token <f_val> T_FLOAT %token <s_val> T_STRING -%token <symbol> T_LABEL +%token <symbol> T_SYMBOL %token <symbol> T_INSTRUCTION %token T_ENDL %token T_COMMA -%token T_IMMEDIATE +%token T_OPENPAREN +%token T_CLOSEPAREN %% asm: statements @@ -38,37 +39,48 @@ statements: ; statement: - instruction_one_param_symbol T_ENDL - | instruction_one_param_immediate T_ENDL - | instruction_two_param_symbols T_ENDL - | instruction_symbol_immediate T_ENDL - | instruction_immediate_symbol T_ENDL + instructions | T_ENDL ; -instruction_one_param_symbol: - T_INSTRUCTION T_LABEL { - printf("Found instruction %s(%s)\n", $1, $2); +instructions: + rb_type + | i_type + | s_type + | u_type + | j_type + ; + +rb_type: + T_INSTRUCTION T_SYMBOL T_COMMA T_SYMBOL T_COMMA T_SYMBOL T_ENDL + { + printf("Read instruction: %s(%s, %s, %s)\n", $1, $2, $4, $6); }; -instruction_one_param_immediate: - T_INSTRUCTION T_IMMEDIATE T_INTEGER { - printf("Found instruction %s(%d)\n", $1, $3); +i_type: + T_INSTRUCTION T_SYMBOL T_COMMA T_SYMBOL T_COMMA T_INTEGER T_ENDL + { + printf("Read instruction: %s(%s, %s, %d)\n", $1, $2, $4, $6); }; -instruction_two_param_symbols: - T_INSTRUCTION T_LABEL T_COMMA T_LABEL { - printf("Found instruction %s(%s, %s)\n", $1, $2, $4); + +s_type: + T_INSTRUCTION T_SYMBOL T_COMMA T_INTEGER T_OPENPAREN T_SYMBOL T_CLOSEPAREN T_ENDL + { + printf("Read instruction: %s(%s, %s + %d)\n", $1, $2, $6, $4); }; -instruction_symbol_immediate: - T_INSTRUCTION T_LABEL T_COMMA T_IMMEDIATE T_INTEGER { - printf("Found instruction %s(%s, %d)\n", $1, $2, $5); - } +u_type: + T_INSTRUCTION T_SYMBOL T_COMMA T_INTEGER T_ENDL + { + printf("Read instruction: %s(%s, %d)\n", $1, $2, $4); + }; + +j_type: + T_INSTRUCTION T_SYMBOL T_COMMA T_SYMBOL T_ENDL + { + printf("Read instruction: %s(%s, %s)\n", $1, $2, $4); + }; -instruction_immediate_symbol: - T_INSTRUCTION T_IMMEDIATE T_INTEGER T_COMMA T_LABEL { - printf("Found instruction %s(%d, %s)\n", $1, $3, $5); - } %% void yyerror(const char *s) { |
