Reste a creer la fonction patchant les jumps
This commit is contained in:
parent
ca436dc7a1
commit
80022a688b
3 changed files with 126 additions and 70 deletions
150
as.y
150
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);};
|
||||
|
||||
%%
|
||||
|
||||
|
|
33
tables.c
33
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<NB_REG; i++) {
|
||||
registres[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_case_adresse(struct case_adresse case_courante) {
|
||||
|
@ -88,7 +98,7 @@ int get_register() {
|
|||
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) {
|
||||
int dispo = get_register();
|
||||
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);
|
||||
if (ancienne_case.modifie == 1) {
|
||||
fprintf(file, "STORE %d %d\n", previous_addr, dispo);
|
||||
*added_instruction = (*added_instruction) + 1;
|
||||
set_modifie(previous_addr, 0);
|
||||
}
|
||||
set_registre(previous_addr, -1);
|
||||
|
@ -110,6 +121,7 @@ int get_reg_write(int adresse, FILE * file) {
|
|||
if (previous_addr != -1) {
|
||||
struct case_adresse ancienne_case = get_info(previous_addr);
|
||||
if (ancienne_case.modifie == 1) {
|
||||
*added_instruction = (*added_instruction) + 1;
|
||||
fprintf(file, "STORE %d %d\n", previous_addr, dispo);
|
||||
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);
|
||||
if (ma_case.registre == -1) {
|
||||
int dispo = get_register();
|
||||
|
@ -133,11 +145,13 @@ int get_reg_read(int adresse, FILE * file) {
|
|||
if (previous_addr != -1) {
|
||||
struct case_adresse ancienne_case = get_info(previous_addr);
|
||||
if (ancienne_case.modifie == 1) {
|
||||
*added_instruction = (*added_instruction) + 1;
|
||||
fprintf(file, "STORE %d %d\n", previous_addr, dispo);
|
||||
set_modifie(previous_addr, 0);
|
||||
}
|
||||
set_registre(previous_addr, -1);
|
||||
}
|
||||
*added_instruction = (*added_instruction) + 1;
|
||||
fprintf(file, "LOAD %d %d\n", dispo, adresse);
|
||||
set_registre(adresse, 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 added_instruction = 0;
|
||||
for (i = 0; i<MEM_SIZE; i++) {
|
||||
if (tableau[i].registre != -1) {
|
||||
if (tableau[i].modifie == 0) {
|
||||
tableau[i].registre = -1;
|
||||
} else {
|
||||
fprintf(file, "STORE %d %d\n", i, tableau[i].registre);
|
||||
added_instruction++;
|
||||
tableau[i].registre = -1;
|
||||
tableau[i].modifie = 0;
|
||||
}
|
||||
|
@ -165,8 +181,17 @@ void flush_and_init(FILE * file) {
|
|||
for (i=0; i<NB_REG; i++) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
13
tables.h
13
tables.h
|
@ -16,16 +16,11 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue