Reste a creer la fonction patchant les jumps

This commit is contained in:
Paul Faure 2021-05-04 14:06:44 +02:00
parent ca436dc7a1
commit 80022a688b
3 changed files with 126 additions and 70 deletions

150
as.y
View file

@ -24,93 +24,129 @@ Programme : Instruction Programme;
Programme : Instruction; Programme : Instruction;
Instruction : tMUL tNB tNB tNB {increment_time(); Instruction : tMUL tNB tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src1 = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src2 = get_reg_read($4, file); int reg_src1 = get_reg_read($3, file, &added_instruction);
fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2);}; 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(); Instruction : tADD tNB tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src1 = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src2 = get_reg_read($4, file); int reg_src1 = get_reg_read($3, file, &added_instruction);
fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2);}; 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(); Instruction : tDIV tNB tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src1 = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src2 = get_reg_read($4, file); int reg_src1 = get_reg_read($3, file, &added_instruction);
fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2);}; 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(); Instruction : tSUB tNB tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src1 = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src2 = get_reg_read($4, file); int reg_src1 = get_reg_read($3, file, &added_instruction);
fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2);}; 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(); Instruction : tINF tNB tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src1 = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src2 = get_reg_read($4, file); int reg_src1 = get_reg_read($3, file, &added_instruction);
fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2);}; 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(); Instruction : tSUP tNB tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src1 = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src2 = get_reg_read($4, file); int reg_src1 = get_reg_read($3, file, &added_instruction);
fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2);}; 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(); Instruction : tEQU tNB tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src1 = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
int reg_src2 = get_reg_read($4, file); int reg_src1 = get_reg_read($3, file, &added_instruction);
fprintf(file, "EQU %d %d %d\n", reg_dest, reg_src1, reg_src2);}; 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(); Instruction : tAFC tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
fprintf(file, "AFC %d %d\n", reg_dest, $3);}; 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(); Instruction : tCPY tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
fprintf(file, "CPY %d %d\n", reg_dest, reg_src);}; 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(); Instruction : tAFCA tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
fprintf(file, "AFCA %d %d\n", reg_dest, $3);}; 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(); Instruction : tCPYA tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_src = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
fprintf(file, "CPYA %d %d\n", reg_dest, reg_src);}; 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(); 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(); Instruction : tJMF tNB tNB {increment_time();
int reg_src = get_reg_read($2, file); int added_instruction = 0;
int reg_aux = get_reg_write(-1, file); 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, "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(); Instruction : tWR tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
int reg_addr = get_reg_read($3, file); int reg_dest = get_reg_write($2, file, &added_instruction);
fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr);}; 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(); Instruction : tREAD tNB tNB {increment_time();
int reg_addr = get_reg_read($2, file); int added_instruction = 0;
int reg_value = get_reg_read($3, file); int reg_addr = get_reg_read($2, file, &added_instruction);
fprintf(file, "STOREA %d %d\n", reg_addr, reg_value);}; 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(); Instruction : tGET tNB {increment_time();
int reg_dest = get_reg_write($2, file); int added_instruction = 0;
fprintf(file, "GET %d\n", reg_dest);}; 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(); Instruction : tPRI tNB {increment_time();
int reg_src = get_reg_read($2, file); int added_instruction = 0;
fprintf(file, "PRI %d\n", reg_src);}; 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(); Instruction : tCALL tNB tNB {increment_time();
flush_and_init(file); int added_instruction = flush_and_init(file);
fprintf(file, "CALL %d %d\n", $2, $3);}; fprintf(file, "CALL %d %d\n", $2, $3);
new_instruction(added_instruction + 1);};
Instruction : tRET {increment_time(); Instruction : tRET {increment_time();
flush_and_init(file); int added_instruction = flush_and_init(file);
fprintf(file, "RET\n");}; fprintf(file, "RET\n");
new_instruction(added_instruction + 1);};
Instruction : tSTOP {increment_time(); Instruction : tSTOP {increment_time();
fprintf(file, "STOP\n");}; fprintf(file, "STOP\n");
new_instruction(1);};
%% %%

View file

@ -14,9 +14,19 @@
#include "tables.h" #include "tables.h"
#define NB_REG 4 #define NB_REG 4
#define MEM_SIZE 1024 #define MEM_SIZE 1024
#define NB_INSTRUCTIONS 1024
struct case_adresse {
int adresse;
int registre;
char modifie;
};
struct case_adresse tableau[MEM_SIZE]; struct case_adresse tableau[MEM_SIZE];
int registres[NB_REG]; int registres[NB_REG];
int traduction_JMP[NB_INSTRUCTIONS];
void init (void) { void init (void) {
int i; int i;
@ -27,7 +37,7 @@ void init (void) {
} }
for (i=0; i<NB_REG; i++) { for (i=0; i<NB_REG; i++) {
registres[i] = 0; registres[i] = 0;
} }
} }
void print_case_adresse(struct case_adresse case_courante) { void print_case_adresse(struct case_adresse case_courante) {
@ -88,7 +98,7 @@ int get_register() {
return index_max; return index_max;
} }
int get_reg_write(int adresse, FILE * file) { int get_reg_write(int adresse, FILE * file, int * added_instruction) {
if (adresse == -1) { if (adresse == -1) {
int dispo = get_register(); int dispo = get_register();
int previous_addr = get_adresse(dispo); int previous_addr = get_adresse(dispo);
@ -96,6 +106,7 @@ int get_reg_write(int adresse, FILE * file) {
struct case_adresse ancienne_case = get_info(previous_addr); struct case_adresse ancienne_case = get_info(previous_addr);
if (ancienne_case.modifie == 1) { if (ancienne_case.modifie == 1) {
fprintf(file, "STORE %d %d\n", previous_addr, dispo); fprintf(file, "STORE %d %d\n", previous_addr, dispo);
*added_instruction = (*added_instruction) + 1;
set_modifie(previous_addr, 0); set_modifie(previous_addr, 0);
} }
set_registre(previous_addr, -1); set_registre(previous_addr, -1);
@ -110,6 +121,7 @@ int get_reg_write(int adresse, FILE * file) {
if (previous_addr != -1) { if (previous_addr != -1) {
struct case_adresse ancienne_case = get_info(previous_addr); struct case_adresse ancienne_case = get_info(previous_addr);
if (ancienne_case.modifie == 1) { if (ancienne_case.modifie == 1) {
*added_instruction = (*added_instruction) + 1;
fprintf(file, "STORE %d %d\n", previous_addr, dispo); fprintf(file, "STORE %d %d\n", previous_addr, dispo);
set_modifie(previous_addr, 0); set_modifie(previous_addr, 0);
} }
@ -125,7 +137,7 @@ int get_reg_write(int adresse, FILE * file) {
} }
} }
int get_reg_read(int adresse, FILE * file) { int get_reg_read(int adresse, FILE * file, int * added_instruction) {
struct case_adresse ma_case = get_info(adresse); struct case_adresse ma_case = get_info(adresse);
if (ma_case.registre == -1) { if (ma_case.registre == -1) {
int dispo = get_register(); int dispo = get_register();
@ -133,11 +145,13 @@ int get_reg_read(int adresse, FILE * file) {
if (previous_addr != -1) { if (previous_addr != -1) {
struct case_adresse ancienne_case = get_info(previous_addr); struct case_adresse ancienne_case = get_info(previous_addr);
if (ancienne_case.modifie == 1) { if (ancienne_case.modifie == 1) {
*added_instruction = (*added_instruction) + 1;
fprintf(file, "STORE %d %d\n", previous_addr, dispo); fprintf(file, "STORE %d %d\n", previous_addr, dispo);
set_modifie(previous_addr, 0); set_modifie(previous_addr, 0);
} }
set_registre(previous_addr, -1); set_registre(previous_addr, -1);
} }
*added_instruction = (*added_instruction) + 1;
fprintf(file, "LOAD %d %d\n", dispo, adresse); fprintf(file, "LOAD %d %d\n", dispo, adresse);
set_registre(adresse, dispo); set_registre(adresse, dispo);
refresh_registre(dispo); refresh_registre(dispo);
@ -149,14 +163,16 @@ int get_reg_read(int adresse, FILE * file) {
} }
void flush_and_init(FILE * file) { int flush_and_init(FILE * file) {
int i; int i;
int added_instruction = 0;
for (i = 0; i<MEM_SIZE; i++) { for (i = 0; i<MEM_SIZE; i++) {
if (tableau[i].registre != -1) { if (tableau[i].registre != -1) {
if (tableau[i].modifie == 0) { if (tableau[i].modifie == 0) {
tableau[i].registre = -1; tableau[i].registre = -1;
} else { } else {
fprintf(file, "STORE %d %d\n", i, tableau[i].registre); fprintf(file, "STORE %d %d\n", i, tableau[i].registre);
added_instruction++;
tableau[i].registre = -1; tableau[i].registre = -1;
tableau[i].modifie = 0; tableau[i].modifie = 0;
} }
@ -165,8 +181,17 @@ void flush_and_init(FILE * file) {
for (i=0; i<NB_REG; i++) { for (i=0; i<NB_REG; i++) {
registres[i] = 0; registres[i] = 0;
} }
return added_instruction;
} }
void new_instruction(int nb_inst) {
static int last_intruction_adresse = 0;
static int current_instruction = 0;
traduction_JMP[current_instruction] = last_intruction_adresse;
current_instruction++;
last_intruction_adresse += nb_inst;
}

View file

@ -16,16 +16,11 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
struct case_adresse {
int adresse;
int registre;
char modifie;
};
void init(void); void init(void);
void increment_time(); void increment_time();
int get_reg_read(int adresse, FILE * file); int get_reg_read(int adresse, FILE * file, int * added_instruction);
int get_reg_write(int adresse, FILE * file); int get_reg_write(int adresse, FILE * file, int * added_instruction);
void flush_and_init(FILE * file); int flush_and_init(FILE * file);
void new_instruction(int nb_inst);
#endif #endif