diff --git a/as.y b/as.y index 2769840..b472542 100644 --- a/as.y +++ b/as.y @@ -24,93 +24,129 @@ Programme : Instruction Programme; Programme : Instruction; Instruction : tMUL tNB tNB tNB {increment_time(); - int reg_dest = get_reg_write($2, file); - int reg_src1 = get_reg_read($3, file); - int reg_src2 = get_reg_read($4, file); - fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src1 = get_reg_read($3, file); - int reg_src2 = get_reg_read($4, file); - fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src1 = get_reg_read($3, file); - int reg_src2 = get_reg_read($4, file); - fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src1 = get_reg_read($3, file); - int reg_src2 = get_reg_read($4, file); - fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src1 = get_reg_read($3, file); - int reg_src2 = get_reg_read($4, file); - fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src1 = get_reg_read($3, file); - int reg_src2 = get_reg_read($4, file); - fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src1 = get_reg_read($3, file); - int reg_src2 = get_reg_read($4, file); - fprintf(file, "EQU %d %d %d\n", reg_dest, reg_src1, reg_src2);}; + 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 reg_dest = get_reg_write($2, file); - fprintf(file, "AFC %d %d\n", reg_dest, $3);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src = get_reg_read($3, file); - fprintf(file, "CPY %d %d\n", reg_dest, reg_src);}; + 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 reg_dest = get_reg_write($2, file); - fprintf(file, "AFCA %d %d\n", reg_dest, $3);}; + 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 reg_dest = get_reg_write($2, file); - int reg_src = get_reg_read($3, file); - fprintf(file, "CPYA %d %d\n", reg_dest, reg_src);}; + 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);}; + fprintf(file, "JMP %d\n", $2); + new_instruction(1);}; Instruction : tJMF tNB tNB {increment_time(); - int reg_src = get_reg_read($2, file); - int reg_aux = get_reg_write(-1, file); + 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);}; + fprintf(file, "JMZ %d\n", $3); + new_instruction(added_instruction + 2);}; Instruction : tWR tNB tNB {increment_time(); - int reg_dest = get_reg_write($2, file); - int reg_addr = get_reg_read($3, file); - fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr);}; + 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 reg_addr = get_reg_read($2, file); - int reg_value = get_reg_read($3, file); - fprintf(file, "STOREA %d %d\n", reg_addr, reg_value);}; + 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 reg_dest = get_reg_write($2, file); - fprintf(file, "GET %d\n", reg_dest);}; + 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 reg_src = get_reg_read($2, file); - fprintf(file, "PRI %d\n", reg_src);}; + 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(); - flush_and_init(file); - fprintf(file, "CALL %d %d\n", $2, $3);}; + int added_instruction = flush_and_init(file); + fprintf(file, "CALL %d %d\n", $2, $3); + new_instruction(added_instruction + 1);}; Instruction : tRET {increment_time(); - flush_and_init(file); - fprintf(file, "RET\n");}; + int added_instruction = flush_and_init(file); + fprintf(file, "RET\n"); + new_instruction(added_instruction + 1);}; Instruction : tSTOP {increment_time(); - fprintf(file, "STOP\n");}; + fprintf(file, "STOP\n"); + new_instruction(1);}; %% diff --git a/tables.c b/tables.c index a4b2ea6..f879642 100644 --- a/tables.c +++ b/tables.c @@ -14,9 +14,19 @@ #include "tables.h" #define NB_REG 4 #define MEM_SIZE 1024 +#define NB_INSTRUCTIONS 1024 + + + +struct case_adresse { + int adresse; + int registre; + char modifie; +}; struct case_adresse tableau[MEM_SIZE]; int registres[NB_REG]; +int traduction_JMP[NB_INSTRUCTIONS]; void init (void) { int i; @@ -27,7 +37,7 @@ void init (void) { } for (i=0; i #include -struct case_adresse { - int adresse; - int registre; - char modifie; -}; - void init(void); void increment_time(); -int get_reg_read(int adresse, FILE * file); -int get_reg_write(int adresse, FILE * file); -void flush_and_init(FILE * file); +int get_reg_read(int adresse, FILE * file, int * added_instruction); +int get_reg_write(int adresse, FILE * file, int * added_instruction); +int flush_and_init(FILE * file); +void new_instruction(int nb_inst); #endif