From 91468b81ccad51eb3f8c0fbc90270a893ff040a9 Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 7 May 2018 02:24:04 +0200 Subject: Extract generator functions (partial commit) --- makefile | 8 +++-- src/generator.c | 53 +++++++++++++++++++++++++++ src/generator.h | 18 ++++++++++ src/tpc.y | 108 +++++++++++++++++--------------------------------------- 4 files changed, 110 insertions(+), 77 deletions(-) create mode 100644 src/generator.c create mode 100644 src/generator.h diff --git a/makefile b/makefile index d7af548..f876d44 100644 --- a/makefile +++ b/makefile @@ -12,6 +12,7 @@ LEX_SRC := tpc.lex YACC_SRC := tpc.y PDF_SRC := rapport.md ST_SRC := symbol_table +GEN_SRC := generator # INTERMEDIATE LEX_GEN := tpc.yy @@ -47,7 +48,7 @@ $(OUT_DIR)/$(FILE_TEST).asm: $(RES_DIR)/$(FILE_TEST).tpc $(OUT_DIR)/$(COMPIL_BIN $(OUT_DIR)/$(LEX_GEN).c: $(SRC_DIR)/$(LEX_SRC) $(LEX) -o $@ $^ -$(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) $(SRC_DIR)/$(ST_SRC).h +$(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) $(SRC_DIR)/$(ST_SRC).h $(SRC_DIR)/$(GEN_SRC).h $(YACC) --output=$(OUT_DIR)/$(YACC_GEN).c --defines=$(OUT_DIR)/$(YACC_GEN).h -v $< $(OUT_DIR)/$(LEX_GEN).o: $(OUT_DIR)/$(LEX_GEN).c $(OUT_DIR)/$(YACC_GEN).h @@ -59,7 +60,10 @@ $(OUT_DIR)/$(YACC_GEN).o: $(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(ST_SRC).o: $(SRC_DIR)/$(ST_SRC).c $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) -$(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o $(OUT_DIR)/$(ST_SRC).o +$(OUT_DIR)/$(GEN_SRC).o: $(SRC_DIR)/$(GEN_SRC).c + $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) + +$(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o $(OUT_DIR)/$(ST_SRC).o $(OUT_DIR)/$(GEN_SRC).o $(CC) -o $@ $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) $(OUT_DIR)/$(REPORT_PDF): $(DOC_DIR)/$(PDF_SRC) diff --git a/src/generator.c b/src/generator.c new file mode 100644 index 0000000..a7a7c72 --- /dev/null +++ b/src/generator.c @@ -0,0 +1,53 @@ +/** + * UPEM / Compilation / Projet + * Pacien TRAN-GIRARD, Adam NAILI + */ + +#include "generator.h" +#include +#include "symbol_table.h" + +void prologue() { + printf("extern printf\n"); + printf("section .data\n"); + printf("format_int db \"%%d\",10,0\n\n"); + printf("section .bss\nsection .text\n\nglobal _start\n"); + printf("print: ;print needs an argument in rax\n"); + printf("push rbp\n"); + printf("mov rbp, rsp\n"); + printf("push rsi\n"); + printf("mov rsi, rax\n"); + printf("mov rdi, format_int\n"); + printf("mov rax, 0\n"); + printf("call printf WRT ..plt\n"); + printf("pop rsi\n"); + printf("pop rbp\n"); + printf("ret\n"); + printf("\n_start:\n"); + printf("push rbp\nmov rbp, rsp\n\n"); +} + +void const_declaration() { + printf("mov rax,60 \n"); + printf("mov rdi,0 \n"); + printf("syscall \n"); + printf(";global table\n"); + glo_display_table(); + printf(";local table\n"); + loc_display_table(); +} + +void declaration(const char name[], int type, Scope scope) { + switch (scope) { + case GLOBAL: + glo_addVar(name, type); + break; + case LOCAL: + loc_addVar(name, type); + break; + } + + printf("push 0\n"); +} + + diff --git a/src/generator.h b/src/generator.h new file mode 100644 index 0000000..53acdef --- /dev/null +++ b/src/generator.h @@ -0,0 +1,18 @@ +/** + * UPEM / Compilation / Projet + * Pacien TRAN-GIRARD, Adam NAILI + */ + +#ifndef __GENERATOR_H__ +#define __GENERATOR_H__ + +typedef enum scope { + GLOBAL, + LOCAL +} Scope; + +void prologue(); +void const_declaration(); +void declaration(const char name[], int type, Scope scope); + +#endif diff --git a/src/tpc.y b/src/tpc.y index b0d3633..14f3488 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -13,13 +13,12 @@ #include #include "symbol_table.h" +#include "generator.h" extern int lineno; int yylex(); void yyerror(char *); -#define GLOBAL 0 -#define LOCAL 1 -static int status = GLOBAL; +static Scope scope = GLOBAL; static int num_label = 0; static int num_if = 0; %} @@ -50,100 +49,59 @@ static int num_if = 0; %precedence ELSE %% -Prog:{printf("extern printf\n"); - printf("section .data\n"); - printf("format_int db \"%%d\",10,0\n\n"); - printf("section .bss\nsection .text\n\nglobal _start\n"); - printf("print: ;print needs an argument in rax\n"); - printf("push rbp\n"); - printf("mov rbp, rsp\n"); - printf("push rsi\n"); - printf("mov rsi, rax\n"); - printf("mov rdi, format_int\n"); - printf("mov rax, 0\n"); - printf("call printf WRT ..plt\n"); - printf("pop rsi\n"); - printf("pop rbp\n"); - printf("ret\n"); - printf("\n_start:\n"); - printf("push rbp\nmov rbp, rsp\n\n"); - } - DeclConsts DeclVars DeclFoncts - { - printf("mov rax,60 \n"); - printf("mov rdi,0 \n"); - printf("syscall \n"); - printf(";global table\n"); - glo_display_table(); - printf(";local table\n"); - loc_display_table(); - } +Prog: { prologue(); } + DeclConsts DeclVars DeclFoncts { const_declaration(); } ; DeclConsts: - DeclConsts CONST ListConst ';' + DeclConsts CONST ListConst ';' | ; ListConst: - ListConst ',' IDENT '=' Litteral - | IDENT '=' Litteral + ListConst ',' IDENT '=' Litteral + | IDENT '=' Litteral ; Litteral: - NombreSigne - | CARACTERE + NombreSigne + | CARACTERE ; NombreSigne: - NUM - | ADDSUB NUM + NUM + | ADDSUB NUM ; DeclVars: - DeclVars TYPE Declarateurs ';' + DeclVars TYPE Declarateurs ';' | ; Declarateurs: - Declarateurs ',' Declarateur { - if(status == GLOBAL) glo_addVar($3, $0); - else loc_addVar($3, $0); - printf("push 0\n"); - } - | Declarateur { - if(status == GLOBAL) glo_addVar($1, $0); - else loc_addVar($1, $0); - printf("push 0\n"); - } + Declarateurs ',' Declarateur { declaration($3, $0, scope); } + | Declarateur { declaration($1, $0, scope); } ; Declarateur: - IDENT - | IDENT '[' NUM ']' + IDENT + | IDENT '[' NUM ']' ; DeclFoncts: DeclFoncts DeclFonct | DeclFonct ; DeclFonct: - EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;} + EnTeteFonct { scope = LOCAL; } + Corps { scope = GLOBAL; } ; EnTeteFonct: - TYPE IDENT '(' Parametres ')' - | VOID IDENT '(' Parametres ')' + TYPE IDENT '(' Parametres ')' + | VOID IDENT '(' Parametres ')' ; Parametres: - VOID - | ListTypVar + VOID + | ListTypVar ; ListTypVar: - ListTypVar ',' TYPE IDENT { - if(status == GLOBAL) glo_addVar($4, $3); - else loc_addVar($4, $3); - printf("push 0\n"); - } - | TYPE IDENT { - if(status == GLOBAL) glo_addVar($2, $1); - else loc_addVar($2, $1); - printf("push 0\n"); - } + ListTypVar ',' TYPE IDENT { declaration($4, $3, scope); } + | TYPE IDENT { declaration($2, $1, scope); } ; Corps: - '{' DeclConsts DeclVars SuiteInstr '}' + '{' DeclConsts DeclVars SuiteInstr '}' ; SuiteInstr: SuiteInstr Instr @@ -154,11 +112,11 @@ Instr: | RETURN Exp ';' | RETURN ';' | READE '(' IDENT ')' ';' { - if(status == GLOBAL) glo_lookup($3); + if(scope == GLOBAL) glo_lookup($3); else loc_lookup($3); } | READC '(' IDENT ')' ';' { - if(status == GLOBAL) glo_lookup($3); + if(scope == GLOBAL) glo_lookup($3); else loc_lookup($3); } | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} @@ -172,7 +130,7 @@ IfEndHandling: {printf("jmp .end_ifelse%d\n.end_if%d:\n",$-5);}; Exp: LValue '=' Exp { - if(status == GLOBAL){ + if(scope == GLOBAL){ $$ = glo_lookup($1); printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($1),$1); } @@ -277,7 +235,7 @@ F: | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} | '(' Exp ')' {$$ = $2;} | LValue { - if(status == GLOBAL) { + if(scope == GLOBAL) { $$ = glo_lookup($1); printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($1),$1); } @@ -286,13 +244,13 @@ F: printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($1),$1); } } - | NUM {$$ = INT; if(status == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique - | CARACTERE {$$ = CHAR; if(status == LOCAL) printf("push %d\n",$1);} + | NUM {$$ = INT; if(scope == LOCAL) printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique + | CARACTERE {$$ = CHAR; if(scope == LOCAL) printf("push %d\n",$1);} | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement ; LValue: IDENT { - if(status == GLOBAL) { + if(scope == GLOBAL) { glo_lookup($1); } else { @@ -300,7 +258,7 @@ LValue: } } | IDENT '[' Exp ']' { - if(status == GLOBAL) { + if(scope == GLOBAL) { glo_lookup($1); } else { -- cgit v1.2.3