From a33ad7d7a547dc59cbee6146e1bd0cf16f4fdb62 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Mon, 23 Apr 2018 12:14:30 +0200 Subject: Not finished modifications : beginning of the traduction --- src/symboltable.c | 85 +++++++++++++++++++++++++++++++++++++++---------------- src/symboltable.h | 10 ++++--- src/tpc.y | 64 ++++++++++++++++++++++++++++++++--------- 3 files changed, 117 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/symboltable.c b/src/symboltable.c index 3f22b44..cdbd442 100644 --- a/src/symboltable.c +++ b/src/symboltable.c @@ -2,55 +2,92 @@ extern int lineno; /* from lexical analyser */ -SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0}; +SymbolTable glo_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; +SymbolTable loc_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; -void addVar(const char name[], int type) { +void glo_addVar(const char name[], int type) { int count; - for (count = 0; count < symbol_table.size; count++) { - if (!strcmp(symbol_table.entries[count].name, name)) { - printf("semantic error, redefinition of variable %s near line %d\n", name, + 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 (++symbol_table.size > symbol_table.maxsize) { - printf("too many variables near line %d\n", lineno); + if (++glo_symbol_table.size > glo_symbol_table.maxsize) { + fprintf(stderr,"too many variables near line %d\n", lineno); exit(1); } - strcpy(symbol_table.entries[symbol_table.size - 1].name, name); - symbol_table.entries[symbol_table.size - 1].type = type; + 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; } -void lookup(const char name[]) { +//Verifies that the variable exists and returns the type +int glo_lookup(const char name[]) { int count; - for (count = 0; count < symbol_table.size; count++) { - if (!strcmp(symbol_table.entries[count].name, name)) { - return; + 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; } } - printf("No definition of the variable %s near line %d\n", name, + fprintf(stderr,"No definition of the variable %s near line %d\n", name, lineno); + return -1; } -void display_table(){ + +void glo_display_table(){ int count; - for (count=0;count 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; +} + + +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; + } + } + 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 TYPE -%type Exp EB TB FB M E T F LValue +%type Exp EB TB FB M E T F +%type LValue %left ',' %precedence ')' %precedence ELSE %% -Prog: - DeclConsts DeclVars DeclFoncts {display_table();} +Prog:{printf("section .data\n"); + printf("format_entier db \"%%d \n\", 10,0"); + printf("section .bss\nsection .text\nglobal _start\n"); + printf("print:\n"); + printf("push rbp\n"); + printf("mov rbp, rsp\n"); + + printf("push rax\n"); + printf("push rcx\n"); + printf("push rdx\n"); + printf("push rdi\n"); + printf("push rsi\n"); + printf("push r8\n"); + + printf("mov r8, rdx \n"); + printf("mov rcx, rcx\n"); + printf("mov rdx, rbx\n"); + printf(" mov rdi, format_entier\n"); + printf(" mov rax, 0\n"); + printf(" call printf WRT ..plt\n"); + + printf("pop r8\n"); + printf("pop rsi\n"); + printf("pop rdi\n"); + printf("pop rdx\n"); + printf("pop rcx\n"); + printf("pop rax\n"); + + printf("pop rbp\n"); + printf("ret\n"); + } + DeclConsts DeclVars DeclFoncts { + + glo_display_table();} ; DeclConsts: DeclConsts CONST ListConst ';' @@ -62,8 +98,8 @@ DeclVars: | ; Declarateurs: - Declarateurs ',' Declarateur {addVar($3, $0);} - | Declarateur {addVar($1, $0);} + Declarateurs ',' Declarateur {glo_addVar($3, $0);} + | Declarateur {glo_addVar($1, $0);} ; Declarateur: IDENT @@ -74,7 +110,7 @@ DeclFoncts: | DeclFonct ; DeclFonct: - EnTeteFonct Corps + EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;} ; EnTeteFonct: TYPE IDENT '(' Parametres ')' @@ -85,8 +121,8 @@ Parametres: | ListTypVar ; ListTypVar: - ListTypVar ',' TYPE IDENT {addVar($4, $3);} - | TYPE IDENT {addVar($2, $1);} + ListTypVar ',' TYPE IDENT {glo_addVar($4, $3);} + | TYPE IDENT {glo_addVar($2, $1);} ; Corps: '{' DeclConsts DeclVars SuiteInstr '}' @@ -99,8 +135,8 @@ Instr: | ';' | RETURN Exp ';' | RETURN ';' - | READE '(' IDENT ')' ';' {lookup($3);} - | READC '(' IDENT ')' ';' {lookup($3);} + | READE '(' IDENT ')' ';' {glo_lookup($3);} + | READC '(' IDENT ')' ';' {glo_lookup($3);} | PRINT '(' Exp ')' ';' | IF '(' Exp ')' Instr | IF '(' Exp ')' Instr ELSE Instr @@ -108,7 +144,7 @@ Instr: | '{' SuiteInstr '}' ; Exp: - LValue '=' Exp + LValue '=' Exp {$$ = glo_lookup($1);} | EB ; EB: @@ -139,14 +175,14 @@ F: ADDSUB F {$$ = $2;} //on fait remonter le type | '!' F {$$ = $2;} | '(' Exp ')' {$$ = $2;} - | LValue {$$ = $1;} + | LValue {$$ = glo_lookup($1);} | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique | CARACTERE {$$ = CHAR;} | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement ; LValue: - IDENT {lookup($1);get_type($1);} - | IDENT '[' Exp ']' {lookup($1);get_type($1);} + IDENT {glo_lookup($1);} + | IDENT '[' Exp ']' {glo_lookup($1);} ; Arguments: ListExp -- cgit v1.2.3