From ca436dc7a160e3c6367d3d73f28d36ae24678508 Mon Sep 17 00:00:00 2001 From: pfaure Date: Tue, 4 May 2021 13:39:52 +0200 Subject: [PATCH] Cross compilo termine --- Makefile | 7 +++ ReadMe | 0 al.lex | 51 +++++++++++++++ as.y | 122 ++++++++++++++++++++++++++++++++++++ tables.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tables.h | 31 ++++++++++ toto.asm | 8 +++ 7 files changed, 405 insertions(+) create mode 100644 Makefile create mode 100644 ReadMe create mode 100644 al.lex create mode 100644 as.y create mode 100644 tables.c create mode 100644 tables.h create mode 100644 toto.asm diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5c0bab9 --- /dev/null +++ b/Makefile @@ -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 diff --git a/ReadMe b/ReadMe new file mode 100644 index 0000000..e69de29 diff --git a/al.lex b/al.lex new file mode 100644 index 0000000..c6fe54f --- /dev/null +++ b/al.lex @@ -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" + +%% + + diff --git a/as.y b/as.y new file mode 100644 index 0000000..2769840 --- /dev/null +++ b/as.y @@ -0,0 +1,122 @@ +%union { + int nombre; +} +%{ +#include "tables.h" +#include + +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 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; +} diff --git a/tables.c b/tables.c new file mode 100644 index 0000000..a4b2ea6 --- /dev/null +++ b/tables.c @@ -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 +#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); + +#endif diff --git a/toto.asm b/toto.asm new file mode 100644 index 0000000..bc58383 --- /dev/null +++ b/toto.asm @@ -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