CrossAssembleur/as.y

158 lines
7.2 KiB
Text

%union {
int nombre;
}
%{
#include "tables.h"
#include <stdio.h>
FILE * file;
%}
%token tMUL tDIV tADD tSUB tINF tSUP tEQU
%token tAFC tCPY tAFCA tCPYA
%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_dest = get_reg_write($2, file, &added_instruction);
int reg_src1 = get_reg_read($3, file, &added_instruction);
int reg_src2 = get_reg_read($4, file, &added_instruction);
fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tADD tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src1 = get_reg_read($3, file, &added_instruction);
int reg_src2 = get_reg_read($4, file, &added_instruction);
fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tDIV tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src1 = get_reg_read($3, file, &added_instruction);
int reg_src2 = get_reg_read($4, file, &added_instruction);
fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tSUB tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src1 = get_reg_read($3, file, &added_instruction);
int reg_src2 = get_reg_read($4, file, &added_instruction);
fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tINF tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src1 = get_reg_read($3, file, &added_instruction);
int reg_src2 = get_reg_read($4, file, &added_instruction);
fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tSUP tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src1 = get_reg_read($3, file, &added_instruction);
int reg_src2 = get_reg_read($4, file, &added_instruction);
fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2);
new_instruction(added_instruction + 1);};
Instruction : tEQU tNB tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src1 = get_reg_read($3, file, &added_instruction);
int reg_src2 = get_reg_read($4, file, &added_instruction);
fprintf(file, "EQU %d %d %d\n", 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, file, &added_instruction);
fprintf(file, "AFC %d %d\n", reg_dest, $3);
new_instruction(added_instruction + 1);};
Instruction : tCPY tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src = get_reg_read($3, file, &added_instruction);
fprintf(file, "CPY %d %d\n", reg_dest, reg_src);
new_instruction(added_instruction + 1);};
Instruction : tAFCA tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
fprintf(file, "AFCA %d %d\n", reg_dest, $3);
new_instruction(added_instruction + 1);};
Instruction : tCPYA tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src = get_reg_read($3, file, &added_instruction);
fprintf(file, "CPYA %d %d\n", reg_dest, reg_src);
new_instruction(added_instruction + 1);};
Instruction : tJMP tNB {increment_time();
fprintf(file, "JMP %d\n", $2);
new_instruction(1);};
Instruction : tJMF tNB tNB {increment_time();
int added_instruction = 0;
int reg_src = get_reg_read($2, file, &added_instruction);
int reg_aux = get_reg_write(-1, file, &added_instruction);
fprintf(file, "SUB %d %d %d\n", reg_aux, reg_aux, reg_src);
fprintf(file, "JMZ %d\n", $3);
new_instruction(added_instruction + 2);};
Instruction : tWR tNB tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_addr = get_reg_read($3, file, &added_instruction);
fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr);
new_instruction(added_instruction + 1);};
Instruction : tREAD tNB tNB {increment_time();
int added_instruction = 0;
int reg_addr = get_reg_read($2, file, &added_instruction);
int reg_value = get_reg_read($3, file, &added_instruction);
fprintf(file, "STOREA %d %d\n", reg_addr, reg_value);
new_instruction(added_instruction + 1);};
Instruction : tGET tNB {increment_time();
int added_instruction = 0;
int reg_dest = get_reg_write($2, file, &added_instruction);
fprintf(file, "GET %d\n", reg_dest);
new_instruction(added_instruction + 1);};
Instruction : tPRI tNB {increment_time();
int added_instruction = 0;
int reg_src = get_reg_read($2, file, &added_instruction);
fprintf(file, "PRI %d\n", reg_src);
new_instruction(added_instruction + 1);};
Instruction : tCALL tNB tNB {increment_time();
int added_instruction = flush_and_init(file);
fprintf(file, "CALL %d %d\n", $2, $3);
new_instruction(added_instruction + 1);};
Instruction : tRET {increment_time();
int added_instruction = flush_and_init(file);
fprintf(file, "RET\n");
new_instruction(added_instruction + 1);};
Instruction : tSTOP {increment_time();
fprintf(file, "STOP\n");
new_instruction(1);};
%%
int main(void) {
file = stdout;
init();
yyparse();
return 0;
}