OK a tester

This commit is contained in:
Paul Faure 2021-05-10 14:15:26 +02:00
parent 80022a688b
commit 78216ace0a
5 changed files with 242 additions and 88 deletions

9
al.lex
View file

@ -13,7 +13,7 @@ yyerror (char const *s)
%%
"ADD" { return tADD ;}
"SUB" { return tSUB;}
"SOU" { return tSUB;}
"MUL" { return tMUL; }
"DIV" { return tDIV; }
"INF" { return tINF; }
@ -23,7 +23,6 @@ yyerror (char const *s)
"AFC" { return tAFC; }
"COP" { return tCPY; }
"AFCA" { return tAFCA; }
"COPA" { return tCPYA; }
"READ" { return tREAD; }
@ -42,9 +41,9 @@ yyerror (char const *s)
[0-9]+ { yylval.nombre = atoi(yytext); return tNB; }
"\n"
" "
"\t"
"\n" {}
" " {}
"\t" {}
%%

127
as.y
View file

@ -6,11 +6,12 @@
#include <stdio.h>
FILE * file;
FILE * file2;
%}
%token tMUL tDIV tADD tSUB tINF tSUP tEQU
%token tAFC tCPY tAFCA tCPYA
%token tAFC tCPY tAFCA
%token tREAD tWR
%token tJMP tJMF
%token tGET tPRI
@ -25,134 +26,132 @@ 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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &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_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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &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_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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &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_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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
add_instruction(SUB, 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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
add_instruction(INF, 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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
add_instruction(SUP, 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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src1 = get_reg_read($3, &added_instruction);
int reg_src2 = get_reg_read($4, &added_instruction);
add_instruction(EQU, 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);
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_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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_src = get_reg_read($3, &added_instruction);
add_instruction(CPY, reg_dest, reg_src, 0);
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);};
int reg_aux = get_reg_write(-1, &added_instruction);
add_instruction(AFC, reg_aux, $3, 0);
add_instruction(STOREA, $2, reg_aux, 0);
new_instruction(added_instruction + 2);};
Instruction : tJMP tNB {increment_time();
fprintf(file, "JMP %d\n", $2);
add_instruction(JMP, $2, 0, 0);
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);
int reg_src = get_reg_read($2, &added_instruction);
int reg_aux = get_reg_write(-1, &added_instruction);
add_instruction(SUB, reg_aux, reg_aux, reg_src);
add_instruction(JMZ, $3, 0, 0);
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);
int reg_dest = get_reg_write($2, &added_instruction);
int reg_addr = get_reg_read($3, &added_instruction);
add_instruction(LOADI, reg_dest, reg_addr, 0);
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);
int reg_addr = get_reg_read($2, &added_instruction);
int reg_value = get_reg_read($3, &added_instruction);
add_instruction(STOREI, reg_addr, reg_value, 0);
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);
int reg_dest = get_reg_write($2, &added_instruction);
add_instruction(GET, reg_dest, 0, 0);
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);
int reg_src = get_reg_read($2, &added_instruction);
add_instruction(PRI, reg_src, 0, 0);
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);
add_instruction(STOP, $2, $3, 0);
new_instruction(added_instruction + 1);};
Instruction : tRET {increment_time();
int added_instruction = flush_and_init(file);
fprintf(file, "RET\n");
add_instruction(RET, 0, 0, 0);
new_instruction(added_instruction + 1);};
Instruction : tSTOP {increment_time();
fprintf(file, "STOP\n");
Instruction : tSTOP tNB {increment_time();
add_instruction(STOP, $2, 0, 0);
new_instruction(1);};
%%
int main(void) {
file = stdout;
init();
file = fopen("output.asm", "w");
file2 = fopen("output.bin", "w");
init();
yyparse();
write_asm(file);
write_code_machine(file2);
return 0;
}

138
tables.c
View file

@ -13,9 +13,122 @@
#include "tables.h"
#define NB_REG 4
#define MEM_SIZE 1024
#define NB_INSTRUCTIONS 1024
#define MEM_SIZE 16
#define NB_INSTRUCTIONS 64
#define MEM_INST_SIZE 64
#define NB_BITS_INSTRUCTION 5
#define NB_BITS 8
int traduction_JMP[NB_INSTRUCTIONS];
struct str_instruction {
enum instruction_t instruction;
int param1;
int param2;
int param3;
};
int last_instruction = 0;
struct str_instruction buffer[3*NB_INSTRUCTIONS];
void add_instruction(enum instruction_t inst, int param1, int param2, int param3) {
struct str_instruction my_instruction = {inst, param1, param2, param3};
buffer[last_instruction] = my_instruction;
last_instruction++;
}
void write_asm(FILE * file) {
int i = 0;
while (i<MEM_INST_SIZE) {
if (buffer[i].instruction == ADD) {
fprintf(file, "ADD %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
} else if (buffer[i].instruction == SUB) {
fprintf(file, "SUB %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
} else if (buffer[i].instruction == MUL) {
fprintf(file, "MUL %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
} else if (buffer[i].instruction == DIV) {
fprintf(file, "DIV %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
} else if (buffer[i].instruction == INF) {
fprintf(file, "INF %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
} else if (buffer[i].instruction == SUP) {
fprintf(file, "SUP %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
} else if (buffer[i].instruction == EQU) {
fprintf(file, "EQU %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
} else if (buffer[i].instruction == AFC) {
fprintf(file, "AFC %d %d\n", buffer[i].param1, buffer[i].param2);
} else if (buffer[i].instruction == CPY) {
fprintf(file, "CPY %d %d\n", buffer[i].param1, buffer[i].param2);
} else if (buffer[i].instruction == LOAD) {
fprintf(file, "LOAD %d %d\n", buffer[i].param1, buffer[i].param2);
} else if (buffer[i].instruction == STORE) {
fprintf(file, "STORE %d %d\n", buffer[i].param1, buffer[i].param2);
} else if (buffer[i].instruction == LOADI) {
fprintf(file, "LOADI %d %d\n", buffer[i].param1, buffer[i].param2);
} else if (buffer[i].instruction == STOREI) {
fprintf(file, "STOREI %d %d\n", buffer[i].param1, buffer[i].param2);
} else if (buffer[i].instruction == STOREA) {
fprintf(file, "STOREA %d %d\n", buffer[i].param1, buffer[i].param2);
} else if (buffer[i].instruction == JMP) {
fprintf(file, "JMP %d\n", traduction_JMP[buffer[i].param1]);
} else if (buffer[i].instruction == JMZ) {
fprintf(file, "JMZ %d\n", traduction_JMP[buffer[i].param1]);
} else if (buffer[i].instruction == GET) {
fprintf(file, "GET %d\n", buffer[i].param1);
} else if (buffer[i].instruction == PRI) {
fprintf(file, "PRI %d\n", buffer[i].param1);
} else if (buffer[i].instruction == CALL) {
fprintf(file, "CALL %d %d\n", traduction_JMP[buffer[i].param1], buffer[i].param2);
} else if (buffer[i].instruction == RET) {
fprintf(file, "RET\n");
} else if (buffer[i].instruction == STOP) {
fprintf(file, "STOP %d\n", buffer[i].param1);
}
i++;
}
}
void int_2_bin(char * buff, int n) {
int _m = n;
for (int i = 0; i < 32; i++) {
buff[31 - i] = ((_m & (1 << 31)) ? '1' : '0');
_m = _m << 1;
}
}
void convert_to_binary_on_N(int value, int N, char * buff) {
char tampon[33];
int_2_bin(tampon, value);
int i;
for (i = N-1; i>=0; i--) {
buff[N-1-i] = tampon[i];
}
buff[N] = '\0';
}
void write_instruction_binary(FILE * file, struct str_instruction instr) {
char buff1[33];
char buff2[33];
char buff3[33];
char buff4[33];
convert_to_binary_on_N(instr.instruction, NB_BITS_INSTRUCTION, buff1);
convert_to_binary_on_N(instr.param1, NB_BITS, buff2);
convert_to_binary_on_N(instr.param2, NB_BITS, buff3);
convert_to_binary_on_N(instr.param3, NB_BITS, buff4);
fprintf(file, "\"%s%s%s%s\" & ", buff1, buff2, buff3, buff4);
}
void write_code_machine(FILE * file) {
int i = MEM_INST_SIZE - 1;
while (i>=0) {
write_instruction_binary(file, buffer[i]);
i--;
}
}
struct case_adresse {
@ -26,7 +139,6 @@ struct case_adresse {
struct case_adresse tableau[MEM_SIZE];
int registres[NB_REG];
int traduction_JMP[NB_INSTRUCTIONS];
void init (void) {
int i;
@ -38,6 +150,10 @@ void init (void) {
for (i=0; i<NB_REG; i++) {
registres[i] = 0;
}
struct str_instruction nop = {NOP, 0, 0, 0};
for (i=0; i<MEM_INST_SIZE; i++) {
buffer[i] = nop;
}
}
void print_case_adresse(struct case_adresse case_courante) {
@ -98,14 +214,14 @@ int get_register() {
return index_max;
}
int get_reg_write(int adresse, FILE * file, int * added_instruction) {
int get_reg_write(int adresse, int * added_instruction) {
if (adresse == -1) {
int dispo = get_register();
int previous_addr = get_adresse(dispo);
if (previous_addr != -1) {
struct case_adresse ancienne_case = get_info(previous_addr);
if (ancienne_case.modifie == 1) {
fprintf(file, "STORE %d %d\n", previous_addr, dispo);
add_instruction(STORE, previous_addr, dispo, 0);
*added_instruction = (*added_instruction) + 1;
set_modifie(previous_addr, 0);
}
@ -122,7 +238,7 @@ int get_reg_write(int adresse, FILE * file, int * added_instruction) {
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);
add_instruction(STORE, previous_addr, dispo, 0);
set_modifie(previous_addr, 0);
}
set_registre(previous_addr, -1);
@ -137,7 +253,7 @@ int get_reg_write(int adresse, FILE * file, int * added_instruction) {
}
}
int get_reg_read(int adresse, FILE * file, int * added_instruction) {
int get_reg_read(int adresse, int * added_instruction) {
struct case_adresse ma_case = get_info(adresse);
if (ma_case.registre == -1) {
int dispo = get_register();
@ -146,13 +262,13 @@ int get_reg_read(int adresse, FILE * file, int * added_instruction) {
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);
add_instruction(STORE, previous_addr, dispo, 0);
set_modifie(previous_addr, 0);
}
set_registre(previous_addr, -1);
}
*added_instruction = (*added_instruction) + 1;
fprintf(file, "LOAD %d %d\n", dispo, adresse);
add_instruction(LOAD, dispo, adresse, 0);
set_registre(adresse, dispo);
refresh_registre(dispo);
return dispo;
@ -163,7 +279,7 @@ int get_reg_read(int adresse, FILE * file, int * added_instruction) {
}
int flush_and_init(FILE * file) {
int flush_and_init() {
int i;
int added_instruction = 0;
for (i = 0; i<MEM_SIZE; i++) {
@ -171,7 +287,7 @@ int flush_and_init(FILE * file) {
if (tableau[i].modifie == 0) {
tableau[i].registre = -1;
} else {
fprintf(file, "STORE %d %d\n", i, tableau[i].registre);
add_instruction(STORE, i, tableau[i].registre, 0);
added_instruction++;
tableau[i].registre = -1;
tableau[i].modifie = 0;

View file

@ -13,14 +13,21 @@
----------------------------------------
*/
#include <stdint.h>
#include <stdio.h>
enum instruction_t {NOP, ADD, MUL, SUB, DIV, INF, SUP, EQU, CPY, AFC, LOAD, STORE, LOADI, STOREI, STOREA, JMP, JMZ, PRI, GET, CALL, RET, STOP};
void init(void);
void increment_time();
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);
int get_reg_read(int adresse, int * added_instruction);
int get_reg_write(int adresse, int * added_instruction);
int flush_and_init();
void new_instruction(int nb_inst);
void write_asm(FILE * file);
void write_code_machine(FILE * file);
void add_instruction(enum instruction_t inst, int param1, int param2, int param3);
#endif

View file

@ -1,8 +1,41 @@
AFC 0 1
JMP 31
AFC 1 1
AFC 2 1
COP 2 0
AFC 3 1
AFC 4 1
MUL 1 3 1
ADD 1 2 1
AFC 2 10
WR 1 2
AFC 1 0
COP 2 0
AFC 3 1
MUL 1 3 1
ADD 1 2 1
READ 1 1
PRI 1
STOP 15
AFC 1 2
COP 0 1
RET
AFC 1 0
COP 2 0
AFC 3 1
MUL 1 3 1
ADD 1 2 1
AFC 2 1
WR 1 2
COP 1 0
CALL 1 1
AFC 1 1
COP 0 1
RET
AFCA 5 0
CALL 19 5
AFC 5 1
AFC 6 1
MUL 0 1 2
AFCA 6 0
AFC 7 1
MUL 5 7 5
ADD 5 6 5
READ 5 5
PRI 5
STOP 0