CrossAssembleur/LexYacc/as.y
2021-05-18 14:49:45 +02:00

158 lines
6.7 KiB
Text

%union {
int nombre;
}
%{
#include "tables.h"
#include <stdio.h>
FILE * file;
FILE * file2;
%}
%token tMUL tDIV tADD tSUB tINF tSUP tEQU
%token tAFC tCPY tAFCA
%token tREAD tWR
%token tJMP tJMF
%token tGET tPRI
%token tCALL tRET
%token tSTOP
%token<nombre> tNB
%%
Programme : Instruction Programme;
Programme : Instruction;
Instruction : tMUL tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(MUL, reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tADD tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(ADD, reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tDIV tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(DIV, reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tSUB tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(SUB, reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tINF tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(INF, reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tSUP tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(SUP, reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tEQU tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(EQU, reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tAFC tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(AFC, reg_dest, $3, 0);
new_instruction(added_instruction + 1);};
Instruction : tCPY tNB tNB {increment_time();
int added_instruction = 0;
int reg_src = get_reg_read($3, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(CPY, reg_dest, reg_src, 0);
new_instruction(added_instruction + 1);};
Instruction : tAFCA tNB tNB {increment_time();
int added_instruction = 0;
int reg_aux = get_reg_write(-1, &added_instruction);
add_instruction(AFC, reg_aux, $3, 0);
add_instruction(STOREA, $2, reg_aux, 0);
unlink($2);
new_instruction(added_instruction + 2);};
Instruction : tJMP tNB {increment_time();
add_instruction(JMP, $2, 0, 0);
new_instruction(1);};
Instruction : tJMF tNB tNB {increment_time();
int added_instruction = 0;
int reg_src = get_reg_read($2, &added_instruction);
int reg_aux = get_reg_write(-1, &added_instruction);
add_instruction(SUB, reg_aux, reg_aux, reg_src);
add_instruction(JMZ, $3, 0, 0);
new_instruction(added_instruction + 2);};
Instruction : tREAD tNB tNB {increment_time();
int added_instruction = 0;
int reg_addr = get_reg_read($3, &added_instruction);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(LOADI, reg_dest, reg_addr, 0);
new_instruction(added_instruction + 1);};
Instruction : tWR tNB tNB {increment_time();
int added_instruction = 0;
int reg_addr = get_reg_read($2, &added_instruction);
int reg_value = get_reg_read($3, &added_instruction);
add_instruction(STOREI, reg_addr, reg_value, 0);
new_instruction(added_instruction + 1);};
Instruction : tGET tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(GET, reg_dest, 0, 0);
new_instruction(added_instruction + 1);};
Instruction : tPRI tNB {increment_time();
int added_instruction = 0;
int reg_src = get_reg_read($2, &added_instruction);
add_instruction(PRI, reg_src, 0, 0);
new_instruction(added_instruction + 1);};
Instruction : tCALL tNB tNB {increment_time();
int added_instruction = flush_and_init(file);
add_instruction(CALL, $2, $3, 0);
new_instruction(added_instruction + 1);};
Instruction : tRET {increment_time();
int added_instruction = flush_and_init(file);
add_instruction(RET, 0, 0, 0);
new_instruction(added_instruction + 1);};
Instruction : tSTOP tNB {increment_time();
add_instruction(STOP, $2, 0, 0);
new_instruction(1);};
%%
int main(void) {
file = fopen("output.asm", "w");
file2 = fopen("output.bin", "w");
init();
yyparse();
write_asm(file);
write_code_machine_compact(file2);
return 0;
}