CrossAssembleur/as.y

92 lines
3.1 KiB
Text

%union {
int nombre;
}
%{
#include "tables.h"
#include <stdio.h>
FILE * file;
FILE * file2;
%}
%token tMUL tDIV tADD tSUB
%token tAFC tCPY
%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 : 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);};
%%
#include <string.h>
int main(int argc, char *argv[]) {
char *output_filename = "output_cross";
if (argc >= 2) {
output_filename = argv[1];
}
char *asm_filename = malloc(strlen(output_filename) + 5);
char *bin_filename = malloc(strlen(output_filename) + 9);
strcpy(asm_filename, output_filename);
strcpy(bin_filename, output_filename);
strcat(asm_filename, ".asm");
strcat(bin_filename, "_bin.txt");
file = fopen(asm_filename, "w");
file2 = fopen(bin_filename, "w");
init();
yyparse();
flush_and_init();
write_asm(file);
write_code_machine(file2);
free(asm_filename);
free(bin_filename);
return 0;
}