From 15390a3b612562a8f1c995e968cd3b1943375ab9 Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 7 May 2018 01:33:55 +0200 Subject: Code cleanup and reformating --- makefile | 5 +- src/symbol_table.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/symbol_table.h | 40 +++++++++++++ src/symboltable.c | 151 ------------------------------------------------- src/symboltable.h | 36 ------------ src/tpc.lex | 7 +-- src/tpc.y | 40 ++++++------- 7 files changed, 225 insertions(+), 217 deletions(-) create mode 100644 src/symbol_table.c create mode 100644 src/symbol_table.h delete mode 100644 src/symboltable.c delete mode 100644 src/symboltable.h diff --git a/makefile b/makefile index 8a6ddb4..d7af548 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,7 @@ DOC_DIR := doc LEX_SRC := tpc.lex YACC_SRC := tpc.y PDF_SRC := rapport.md -ST_SRC := symboltable +ST_SRC := symbol_table # INTERMEDIATE LEX_GEN := tpc.yy @@ -41,7 +41,7 @@ test: $(OUT_DIR)/$(FILE_TEST).o $(OUT_DIR)/$(FILE_TEST).o: $(OUT_DIR)/$(FILE_TEST).asm $(ASM) $(AFLAGS) $< -o $@ -$(OUT_DIR)/$(FILE_TEST).asm: $(RES_DIR)/$(FILE_TEST).tpc $(OUT_DIR)/$(COMPIL_BIN) +$(OUT_DIR)/$(FILE_TEST).asm: $(RES_DIR)/$(FILE_TEST).tpc $(OUT_DIR)/$(COMPIL_BIN) $(OUT_DIR)/$(COMPIL_BIN) < $< > $@ $(OUT_DIR)/$(LEX_GEN).c: $(SRC_DIR)/$(LEX_SRC) @@ -70,4 +70,3 @@ $(OUT_DIR)/$(COMMIT_LOG): clean: rm -rf $(OUT_DIR)/* - diff --git a/src/symbol_table.c b/src/symbol_table.c new file mode 100644 index 0000000..6f84ce4 --- /dev/null +++ b/src/symbol_table.c @@ -0,0 +1,163 @@ +/** + * UPEM / Compilation / Projet + * Pacien TRAN-GIRARD, Adam NAILI + */ + +#include "symbol_table.h" + +extern int lineno; /* from lexical analyser */ + +SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; +SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; + +void glo_addVar(const char name[], int type) { + int count; + for (count = 0; count < glo_symbol_table.size; count++) { + if (!strcmp(glo_symbol_table.entries[count].name, name)) { + fprintf(stderr, + "semantic error, redefinition of variable %s near line %d\n", + name, lineno); + return; + } + } + if (++glo_symbol_table.size > glo_symbol_table.maxsize) { + fprintf(stderr, "too many variables near line %d\n", lineno); + exit(1); + } + strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); + glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; + glo_symbol_table.entries[glo_symbol_table.size - 1].addr = + (glo_symbol_table.size - 1) * 8; +} + +// Verifies that the variable exists and returns the type +int glo_lookup(const char name[]) { + int count; + + for (count = 0; count < glo_symbol_table.size; count++) { + if (!strcmp(glo_symbol_table.entries[count].name, name)) { + return glo_symbol_table.entries[count].type; + } + } + fprintf(stderr, "No definition of the variable %s near line %d\n", name, + lineno); + return -1; +} + +int glo_get_addr(const char name[]) { + int count; + + for (count = 0; count < glo_symbol_table.size; count++) { + if (!strcmp(glo_symbol_table.entries[count].name, name)) { + return glo_symbol_table.entries[count].addr; + } + } + fprintf(stderr, "No definition of the variable %s near line %d\n", name, + lineno); + return -1; +} + +void glo_display_table() { + int count; + for (count = 0; count < glo_symbol_table.size; count++) { + if (glo_symbol_table.entries[count].type == INT) + printf(";entier: %s, pos: %d \n", + glo_symbol_table.entries[count].name, + glo_symbol_table.entries[count].addr); + else + printf(";caractere: %s, pos: %d \n", + glo_symbol_table.entries[count].name, + glo_symbol_table.entries[count].addr); + } + printf("\n"); +} + +void loc_addVar(const char name[], int type) { + int count; + for (count = 0; count < loc_symbol_table.size; count++) { + if (!strcmp(loc_symbol_table.entries[count].name, name)) { + fprintf(stderr, + "semantic error, redefinition of variable %s near line %d\n", + name, lineno); + return; + } + } + if (++loc_symbol_table.size > loc_symbol_table.maxsize) { + fprintf(stderr, "too many variables near line %d\n", lineno); + exit(1); + } + strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); + loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; + loc_symbol_table.entries[loc_symbol_table.size - 1].addr = + (loc_symbol_table.size - 1) * 8; +} + +int loc_lookup(const char name[]) { + int count; + + for (count = 0; count < loc_symbol_table.size; count++) { + if (!strcmp(loc_symbol_table.entries[count].name, name)) { + return loc_symbol_table.entries[count].type; + } + } + //Check in global table + for (count = 0; count < glo_symbol_table.size; count++) { + if (!strcmp(glo_symbol_table.entries[count].name, name)) { + return glo_symbol_table.entries[count].type; + } + } + fprintf(stderr, "No definition of the variable %s near line %d\n", name, + lineno); + return -1; +} + +int loc_get_addr(const char name[]) { + int count; + + for (count = 0; count < loc_symbol_table.size; count++) { + if (!strcmp(loc_symbol_table.entries[count].name, name)) { + return loc_symbol_table.entries[count].addr; + } + } + fprintf(stderr, "No definition of the variable %s near line %d\n", name, + lineno); + return -1; +} +void loc_display_table() { + int count; + for (count = 0; count < loc_symbol_table.size; count++) { + if (loc_symbol_table.entries[count].type == INT) + printf(";entier: %s, pos: %d \n", + loc_symbol_table.entries[count].name, + loc_symbol_table.entries[count].addr); + else + printf(";caractere: %s, pos: %d \n", + loc_symbol_table.entries[count].name, + loc_symbol_table.entries[count].addr); + } + printf("\n"); +} + +void loc_clean_table() { + int i; + for (i = 0; i < loc_symbol_table.size; i++) { + printf("pop eax\n"); + } + loc_symbol_table.size = 0; +} + +static char *string_of_type(int type) { + switch (type) { + case INT: return "INT"; + case CHAR: return "CHAR"; + default: return "UNEXPECTED"; + } +} + +void check_expected_type(int type_to_check, int type_expected) { + if (type_to_check != type_expected) + fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n", + string_of_type(type_to_check), + string_of_type(type_to_check), + lineno); +} diff --git a/src/symbol_table.h b/src/symbol_table.h new file mode 100644 index 0000000..9926fec --- /dev/null +++ b/src/symbol_table.h @@ -0,0 +1,40 @@ +/** + * UPEM / Compilation / Projet + * Pacien TRAN-GIRARD, Adam NAILI + */ + +#ifndef __SYMBOL_TABLE_H__ +#define __SYMBOL_TABLE_H__ + +#include +#include +#include + +#define MAXNAME 32 +#define INT 0 +#define CHAR 1 +#define MAXSYMBOLS 256 + +typedef struct { + char name[MAXNAME]; + int type; + int addr; +} STentry; + +typedef struct { + STentry entries[MAXSYMBOLS]; + int maxsize; + int size; +} SymbolTable; + +void glo_addVar(const char name[], int type); +int glo_lookup(const char name[]); +int glo_get_addr(const char name[]); +void glo_display_table(); +void loc_addVar(const char name[], int type); +int loc_lookup(const char name[]); +int loc_get_addr(const char name[]); +void loc_display_table(); +void check_expected_type(int type_to_check, int type_expected); + +#endif diff --git a/src/symboltable.c b/src/symboltable.c deleted file mode 100644 index d20b0dc..0000000 --- a/src/symboltable.c +++ /dev/null @@ -1,151 +0,0 @@ -#include "symboltable.h" - -extern int lineno; /* from lexical analyser */ - -SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; -SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; - -void glo_addVar(const char name[], int type) { - int count; - for (count = 0; count < glo_symbol_table.size; count++) { - if (!strcmp(glo_symbol_table.entries[count].name, name)) { - fprintf(stderr, - "semantic error, redefinition of variable %s near line %d\n", - name, lineno); - return; - } - } - if (++glo_symbol_table.size > glo_symbol_table.maxsize) { - fprintf(stderr, "too many variables near line %d\n", lineno); - exit(1); - } - strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); - glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; - glo_symbol_table.entries[glo_symbol_table.size - 1].addr = - (glo_symbol_table.size - 1) * 8; -} - -// Verifies that the variable exists and returns the type -int glo_lookup(const char name[]) { - int count; - - for (count = 0; count < glo_symbol_table.size; count++) { - if (!strcmp(glo_symbol_table.entries[count].name, name)) { - return glo_symbol_table.entries[count].type; - } - } - fprintf(stderr, "No definition of the variable %s near line %d\n", name, - lineno); - return -1; -} -int glo_get_addr(const char name[]) { - int count; - - for (count = 0; count < glo_symbol_table.size; count++) { - if (!strcmp(glo_symbol_table.entries[count].name, name)) { - return glo_symbol_table.entries[count].addr; - } - } - fprintf(stderr, "No definition of the variable %s near line %d\n", name, - lineno); - return -1; -} - -void glo_display_table() { - int count; - for (count = 0; count < glo_symbol_table.size; count++) { - if (glo_symbol_table.entries[count].type == INT) - printf(";entier: %s, pos: %d \n", - glo_symbol_table.entries[count].name, - glo_symbol_table.entries[count].addr); - else - printf(";caractere: %s, pos: %d \n", - glo_symbol_table.entries[count].name, - glo_symbol_table.entries[count].addr); - } - printf("\n"); -} - -void loc_addVar(const char name[], int type) { - int count; - for (count = 0; count < loc_symbol_table.size; count++) { - if (!strcmp(loc_symbol_table.entries[count].name, name)) { - fprintf(stderr, - "semantic error, redefinition of variable %s near line %d\n", - name, lineno); - return; - } - } - if (++loc_symbol_table.size > loc_symbol_table.maxsize) { - fprintf(stderr, "too many variables near line %d\n", lineno); - exit(1); - } - strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); - loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; - loc_symbol_table.entries[loc_symbol_table.size - 1].addr = - (loc_symbol_table.size - 1) * 8; -} - -int loc_lookup(const char name[]) { - int count; - - for (count = 0; count < loc_symbol_table.size; count++) { - if (!strcmp(loc_symbol_table.entries[count].name, name)) { - return loc_symbol_table.entries[count].type; - } - } - //Check in global table - for (count = 0; count < glo_symbol_table.size; count++) { - if (!strcmp(glo_symbol_table.entries[count].name, name)) { - return glo_symbol_table.entries[count].type; - } - } - fprintf(stderr, "No definition of the variable %s near line %d\n", name, - lineno); - return -1; -} - -int loc_get_addr(const char name[]) { - int count; - - for (count = 0; count < loc_symbol_table.size; count++) { - if (!strcmp(loc_symbol_table.entries[count].name, name)) { - return loc_symbol_table.entries[count].addr; - } - } - fprintf(stderr, "No definition of the variable %s near line %d\n", name, - lineno); - return -1; -} -void loc_display_table() { - int count; - for (count = 0; count < loc_symbol_table.size; count++) { - if (loc_symbol_table.entries[count].type == INT) - printf(";entier: %s, pos: %d \n", - loc_symbol_table.entries[count].name, - loc_symbol_table.entries[count].addr); - else - printf(";caractere: %s, pos: %d \n", - loc_symbol_table.entries[count].name, - loc_symbol_table.entries[count].addr); - } - printf("\n"); -} - -void loc_clean_table() { - int i; - for (i = 0; i < loc_symbol_table.size; i++) { - printf("pop eax\n"); - } - loc_symbol_table.size = 0; -} - -void check_expected_type(int type_to_check, int type_expected) { - if (type_to_check != type_expected) - fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n", - type_expected == INT ? "INT" : type_expected == CHAR ? "CHAR" - : "UNDEFINED", - type_to_check == INT ? "INT" : type_to_check == CHAR ? "CHAR" - : "UNDEFINED", - lineno); -} \ No newline at end of file diff --git a/src/symboltable.h b/src/symboltable.h deleted file mode 100644 index ac6db36..0000000 --- a/src/symboltable.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef __SYMBOLTABLE_H__ - -#include -#include -#include - -#define MAXNAME 32 -#define INT 0 -#define CHAR 1 -#define MAXSYMBOLS 256 - - -typedef struct { - char name[MAXNAME]; - int type; - int addr; -} STentry; - - -typedef struct { - STentry entries[MAXSYMBOLS]; - int maxsize; - int size; -} SymbolTable; - - -void glo_addVar(const char name[], int type); -int glo_lookup(const char name[]); -int glo_get_addr(const char name[]); -void glo_display_table(); -void loc_addVar(const char name[], int type); -int loc_lookup(const char name[]); -int loc_get_addr(const char name[]); -void loc_display_table(); -void check_expected_type(int type_to_check, int type_expected); -#endif \ No newline at end of file diff --git a/src/tpc.lex b/src/tpc.lex index 7a0d6b6..bdf6bb5 100644 --- a/src/tpc.lex +++ b/src/tpc.lex @@ -1,8 +1,8 @@ %{ /** - * UPEM / Compilation / Projet - * Pacien TRAN-GIRARD, Adam NAILI - */ + * UPEM / Compilation / Projet + * Pacien TRAN-GIRARD, Adam NAILI + */ #include "tpc.tab.h" #define INT 0 @@ -49,4 +49,3 @@ reade { return READE; } \n { lineno++; } . ; %% - diff --git a/src/tpc.y b/src/tpc.y index 94d89fe..b0d3633 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -3,21 +3,16 @@ * UPEM / Compilation / Projet * Pacien TRAN-GIRARD, Adam NAILI * - * - * - * - * - * - * TODO : + * TODO : * ------ * - Gérer les globales avec .bss (Il faut donc décaler le début du programme après l'analyse des globales pour savoir combien de place réserver.) * - Gestion des tableaux * - Tableau des fonctions - * + * */ #include -#include "symboltable.h" +#include "symbol_table.h" extern int lineno; int yylex(); @@ -30,13 +25,13 @@ static int num_if = 0; %} %union { - char caractere; - int num; - char ident[64]; - int type; - char comp[3]; - char addsub; - char divstar; + char caractere; + int num; + char ident[64]; + int type; + char comp[3]; + char addsub; + char divstar; } %token CARACTERE %token NUM @@ -47,7 +42,7 @@ static int num_if = 0; %token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE %token TYPE -%type Exp EB TB FB M E T F +%type Exp EB TB FB M E T F %type LValue %left ',' @@ -66,17 +61,17 @@ Prog:{printf("extern printf\n"); printf("mov rsi, rax\n"); printf("mov rdi, format_int\n"); printf("mov rax, 0\n"); - printf("call printf WRT ..plt\n"); + printf("call printf WRT ..plt\n"); printf("pop rsi\n"); printf("pop rbp\n"); printf("ret\n"); - printf("\n_start:\n"); + printf("\n_start:\n"); printf("push rbp\nmov rbp, rsp\n\n"); } - DeclConsts DeclVars DeclFoncts + DeclConsts DeclVars DeclFoncts { printf("mov rax,60 \n"); - printf("mov rdi,0 \n"); + printf("mov rdi,0 \n"); printf("syscall \n"); printf(";global table\n"); glo_display_table(); @@ -278,8 +273,8 @@ F: else{ printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n"); } - } - | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} + } + | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} | '(' Exp ')' {$$ = $2;} | LValue { if(status == GLOBAL) { @@ -330,4 +325,3 @@ void yyerror(char *msg){ int main(int argc, char **argv) { return yyparse(); } - -- cgit v1.2.3