From 6152ca4953bd23e1b6f7fd68966efc6b48e48361 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 6 Aug 2020 20:24:29 +0200 Subject: started with locals in procedures and adressing --- ecomp-c/ec.c | 27 ++++++++++++++++++++++++++- ecomp-c/test.sh | 1 + ecomp-c/tests/procedure_local_variables.e | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 ecomp-c/tests/procedure_local_variables.e diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c index fc283e0..c98c766 100644 --- a/ecomp-c/ec.c +++ b/ecomp-c/ec.c @@ -675,6 +675,7 @@ typedef struct Symbol { int *intarr_value; /* variable */ int size; + int offset; /* array type */ int dim; /* procedure type */ @@ -796,6 +797,7 @@ static Symbol *insert_symbol( Scope *scope, char *name, SymbolClass class ) sym->name = Allocate( strlen( name ) + 1 ); strlcpy( sym->name, name, strlen( name ) + 1 ); sym->size = 0; + sym->offset = 0; sym->dim = 0; sym->next = scope->symbol; sym->string_value = NULL; @@ -1959,6 +1961,9 @@ static void parseProcedureBlock( Scope *scope ) if( sym == S_const || sym == S_var || sym == S_begin ) { + Symbol *sym; + int size_locals; + Emit( "; PROC %s\n", symbol->name ); Emit( "%s:\n", symbol->label ); @@ -1970,11 +1975,31 @@ static void parseProcedureBlock( Scope *scope ) /* local scope holding all local defitions */ symbol->scope = create_scope( scope, symbol->name ); + + /* TODO: parse parameters, assign parameter values to + * local variables (when passed by value) or set + * addresses for VAR parameters */ parseProcedureDeclarationBlock( symbol->scope ); - /* TODO: allocate space on stack for local variables */ + size_locals = 0; + sym = symbol->scope->symbol; + while( sym != NULL ) { + if( sym->class == SYMBOL_CLASS_VARIABLE ) { + size_locals += get_size( sym ); + } + sym = sym->next; + } + + if( size_locals > 0 ) { + Emit( "mov eax, %d\n", size_locals ); + Emit( "sub esp, eax\n" ); + } + + /* TODO: allocate space on stack for local variables (and + * parameters) */ /* TODO: assign symbols to relative addresses (base pointer) */ + /* TODO: initialize local variables */ parseStatementBlock( symbol->scope ); diff --git a/ecomp-c/test.sh b/ecomp-c/test.sh index 9f192f3..014eb98 100755 --- a/ecomp-c/test.sh +++ b/ecomp-c/test.sh @@ -24,6 +24,7 @@ type_check_comparision while_statement procedure_call procedure_forward_declaration +procedure_local_variables procedure_scoping example_divisors example_divisors_array_result diff --git a/ecomp-c/tests/procedure_local_variables.e b/ecomp-c/tests/procedure_local_variables.e new file mode 100644 index 0000000..2838203 --- /dev/null +++ b/ecomp-c/tests/procedure_local_variables.e @@ -0,0 +1,24 @@ +/* + * procedure call with local variables + */ + +module procedure_call; + +var + i : integer := 0; + +procedure proc; +const + C : integer = 42; +var + j : integer; + k : integer := C; + +begin + j := i; + i := k; +end + +begin + proc; +end -- cgit v1.2.3-54-g00ecf