summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2020-08-06 20:24:29 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2020-08-06 20:24:29 +0200
commit6152ca4953bd23e1b6f7fd68966efc6b48e48361 (patch)
treebae246b9f0b12df4fe285f91cb1cdc7391fe34f4
parent97805d0244c41d0131c3cf010c6bc596b411cf81 (diff)
downloadcompilertests-6152ca4953bd23e1b6f7fd68966efc6b48e48361.tar.gz
compilertests-6152ca4953bd23e1b6f7fd68966efc6b48e48361.tar.bz2
started with locals in procedures and adressing
-rw-r--r--ecomp-c/ec.c27
-rwxr-xr-xecomp-c/test.sh1
-rw-r--r--ecomp-c/tests/procedure_local_variables.e24
3 files changed, 51 insertions, 1 deletions
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