Cross compilo termine

This commit is contained in:
Paul Faure 2021-05-04 13:39:52 +02:00
commit ca436dc7a1
7 changed files with 405 additions and 0 deletions

7
Makefile Normal file
View file

@ -0,0 +1,7 @@
build :
gcc -Wall -c tables.c -o tables.o
bison -d -t as.y
flex -o lex.yy.c al.lex
gcc -Wall -c as.tab.c -o as.tab.o
gcc -Wall -c lex.yy.c -o lex.yy.o
gcc as.tab.o lex.yy.o tables.o -ll -o rondoudou_cross_assembleur

0
ReadMe Normal file
View file

51
al.lex Normal file
View file

@ -0,0 +1,51 @@
%{
#include "as.tab.h"
int yywrap(void){return 1;}
void
yyerror (char const *s)
{
fprintf (stderr, "%s\n", s);
}
%}
%%
"ADD" { return tADD ;}
"SUB" { return tSUB;}
"MUL" { return tMUL; }
"DIV" { return tDIV; }
"INF" { return tINF; }
"SUP" { return tSUP; }
"EQU" { return tEQU; }
"AFC" { return tAFC; }
"COP" { return tCPY; }
"AFCA" { return tAFCA; }
"COPA" { return tCPYA; }
"READ" { return tREAD; }
"WR" { return tWR; }
"JMP" { return tJMP; }
"JMF" { return tJMF; }
"GET" { return tGET; }
"PRI" { return tPRI; }
"CALL" { return tCALL; }
"RET" { return tRET; }
"STOP" { return tSTOP; }
[0-9]+ { yylval.nombre = atoi(yytext); return tNB; }
"\n"
" "
"\t"
%%

122
as.y Normal file
View file

@ -0,0 +1,122 @@
%union {
int nombre;
}
%{
#include "tables.h"
#include <stdio.h>
FILE * file;
%}
%token tMUL tDIV tADD tSUB tINF tSUP tEQU
%token tAFC tCPY tAFCA tCPYA
%token tREAD tWR
%token tJMP tJMF
%token tGET tPRI
%token tCALL tRET
%token tSTOP
%token<nombre> tNB
%%
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);};
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);};
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);};
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);};
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);};
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);};
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);};
Instruction : tAFC tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file);
fprintf(file, "AFC %d %d\n", reg_dest, $3);};
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);};
Instruction : tAFCA tNB tNB {increment_time();
int reg_dest = get_reg_write($2, file);
fprintf(file, "AFCA %d %d\n", reg_dest, $3);};
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);};
Instruction : tJMP tNB {increment_time();
fprintf(file, "JMP %d\n", $2);};
Instruction : tJMF tNB tNB {increment_time();
int reg_src = get_reg_read($2, file);
int reg_aux = get_reg_write(-1, file);
fprintf(file, "SUB %d %d %d\n", reg_aux, reg_aux, reg_src);
fprintf(file, "JMZ %d\n", $3);};
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);};
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);};
Instruction : tGET tNB {increment_time();
int reg_dest = get_reg_write($2, file);
fprintf(file, "GET %d\n", reg_dest);};
Instruction : tPRI tNB {increment_time();
int reg_src = get_reg_read($2, file);
fprintf(file, "PRI %d\n", reg_src);};
Instruction : tCALL tNB tNB {increment_time();
flush_and_init(file);
fprintf(file, "CALL %d %d\n", $2, $3);};
Instruction : tRET {increment_time();
flush_and_init(file);
fprintf(file, "RET\n");};
Instruction : tSTOP {increment_time();
fprintf(file, "STOP\n");};
%%
int main(void) {
file = stdout;
init();
yyparse();
return 0;
}

186
tables.c Normal file
View file

@ -0,0 +1,186 @@
/*
----------------------------------------
| Adresse | Registre | Modifié |
----------------------------------------
| | | |
| | | |
| | | |
| i | 0x777756b8 | int |
| size | 0x777756b8 | int |
----------------------------------------
*/
#include "tables.h"
#define NB_REG 4
#define MEM_SIZE 1024
struct case_adresse tableau[MEM_SIZE];
int registres[NB_REG];
void init (void) {
int i;
struct case_adresse case_courante = {0, -1, 0};
for (i=0; i<MEM_SIZE; i++) {
case_courante.adresse = i;
tableau[i] = case_courante;
}
for (i=0; i<NB_REG; i++) {
registres[i] = 0;
}
}
void print_case_adresse(struct case_adresse case_courante) {
printf("{addr : %d ; reg : %d ; modi : %d}\n", case_courante.adresse, case_courante.registre, (int)case_courante.modifie);
}
void print() {
int i;
for (i=0; i<MEM_SIZE; i++) {
print_case_adresse(tableau[i]);
}
}
struct case_adresse get_info(int adresse) {
return tableau[adresse];
}
int get_adresse (int registre) {
int i = 0;
while (i < MEM_SIZE && tableau[i].registre != registre) {
i++;
}
if (i == MEM_SIZE) {
return -1;
} else {
return tableau[i].adresse;
}
}
void set_registre(int adresse, int registre) {
tableau[adresse].registre = registre;
}
void set_modifie(int adresse, char modifie) {
tableau[adresse].modifie = modifie;
}
void increment_time() {
int i;
for (i=0; i<NB_REG; i++) {
registres[i]++;
}
}
void refresh_registre(int registre) {
registres[registre] = 0;
}
int get_register() {
int i;
int index_max = 0;
for (i=0; i<NB_REG; i++) {
if (registres[index_max] < registres[i]) {
index_max = i;
}
}
return index_max;
}
int get_reg_write(int adresse, FILE * file) {
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);
set_modifie(previous_addr, 0);
}
set_registre(previous_addr, -1);
}
return dispo;
} else {
set_modifie(adresse, 1);
struct case_adresse ma_case = get_info(adresse);
if (ma_case.registre == -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);
set_modifie(previous_addr, 0);
}
set_registre(previous_addr, -1);
}
set_registre(adresse, dispo);
refresh_registre(dispo);
return dispo;
} else {
refresh_registre(ma_case.registre);
return ma_case.registre;
}
}
}
int get_reg_read(int adresse, FILE * file) {
struct case_adresse ma_case = get_info(adresse);
if (ma_case.registre == -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);
set_modifie(previous_addr, 0);
}
set_registre(previous_addr, -1);
}
fprintf(file, "LOAD %d %d\n", dispo, adresse);
set_registre(adresse, dispo);
refresh_registre(dispo);
return dispo;
} else {
refresh_registre(ma_case.registre);
return ma_case.registre;
}
}
void flush_and_init(FILE * file) {
int i;
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);
tableau[i].registre = -1;
tableau[i].modifie = 0;
}
}
}
for (i=0; i<NB_REG; i++) {
registres[i] = 0;
}
}

31
tables.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef TABLE_H
#define TABLE_H
/*
----------------------------------------
| Adresse | Registre | Modifié |
----------------------------------------
| | | |
| | | |
| | | |
| i | 0x777756b8 | int |
| size | 0x777756b8 | int |
----------------------------------------
*/
#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);
#endif

8
toto.asm Normal file
View file

@ -0,0 +1,8 @@
AFC 0 1
AFC 1 1
AFC 2 1
AFC 3 1
AFC 4 1
AFC 5 1
AFC 6 1
MUL 0 1 2