diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2020-05-16 21:39:11 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2020-05-16 21:39:11 +0200 |
commit | 44fa2eba837fdeb75e4f89e8c206f0b05564bcbe (patch) | |
tree | 29562bc95011fbd7854a450bed6d0c915affd9f4 | |
parent | d530fcbacbfaf4f2a3c0db789b89a882ca54bbb2 (diff) | |
download | compilertests-44fa2eba837fdeb75e4f89e8c206f0b05564bcbe.tar.gz compilertests-44fa2eba837fdeb75e4f89e8c206f0b05564bcbe.tar.bz2 |
finished variable initialization from simple const expression
-rw-r--r-- | ecomp-c/ec.c | 41 | ||||
-rw-r--r-- | ecomp-c/tests/bool_conditions.easm | 8 | ||||
-rw-r--r-- | ecomp-c/tests/boolean_variable.easm | 6 | ||||
-rw-r--r-- | ecomp-c/tests/example_divisors.easm | 6 | ||||
-rw-r--r-- | ecomp-c/tests/if_statement.easm | 6 | ||||
-rw-r--r-- | ecomp-c/tests/type_check_assignment.easm | 4 | ||||
-rw-r--r-- | ecomp-c/tests/type_check_comparision.easm | 4 | ||||
-rw-r--r-- | ecomp-c/tests/variable_assign_from_constant.easm | 2 | ||||
-rw-r--r-- | ecomp-c/tests/variable_assign_from_expression.easm | 10 | ||||
-rw-r--r-- | ecomp-c/tests/variable_assign_from_type.easm | 2 | ||||
-rw-r--r-- | ecomp-c/tests/variable_assign_from_variable.easm | 4 | ||||
-rw-r--r-- | ecomp-c/tests/variable_initialization.e | 2 | ||||
-rw-r--r-- | ecomp-c/tests/variable_initialization.easm | 16 | ||||
-rw-r--r-- | ecomp-c/tests/variable_initialization.easm_err | 0 | ||||
-rw-r--r-- | ecomp-c/tests/variable_initialization.ecomp_err | 0 | ||||
-rw-r--r-- | ecomp-c/tests/variable_initialization.erun | 162 | ||||
-rw-r--r-- | ecomp-c/tests/variable_name_as_type.easm | 2 | ||||
-rw-r--r-- | ecomp-c/tests/variable_not_initialized.easm | 4 | ||||
-rw-r--r-- | ecomp-c/tests/while_statement.easm | 2 |
19 files changed, 241 insertions, 40 deletions
diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c index fbc054a..2d4cf00 100644 --- a/ecomp-c/ec.c +++ b/ecomp-c/ec.c @@ -528,10 +528,10 @@ static void Emit_Hexbyte( int d ) static void Emit_DD( int d ) { - Emit_Hexbyte( d & 0xFF ); - Emit_Hexbyte( ( d >> 8 ) & 0xFF ); - Emit_Hexbyte( ( d >> 16 ) & 0xFF ); Emit_Hexbyte( ( d >> 24 ) & 0xFF ); + Emit_Hexbyte( ( d >> 16 ) & 0xFF ); + Emit_Hexbyte( ( d >> 8 ) & 0xFF ); + Emit_Hexbyte( d & 0xFF ); } /* parser */ @@ -1249,8 +1249,6 @@ static void parseConstDeclaration( void ) constant[i]->type = node->symbol->type; Emit( "; CONST %s -> %s, %d\n", constant[i]->name, constant[i]->type->name, constant[i]->value ); } - } else { - Abort( "Complex constant expressions not implemented yet" ); } } @@ -1275,6 +1273,7 @@ static void parseVariableDeclaration( void ) int nof_variables = 0; Symbol *variable[MAX_NUMBER_OF_ENUMERATIONS], *type; int i; + ExpressionNode *node; do { if( sym == S_begin ) { @@ -1299,12 +1298,36 @@ static void parseVariableDeclaration( void ) Abort( "'%s' is defined, but is not a type as expected", ident ); } - for( i = 0; i < nof_variables; i++ ) { - variable[i]->type = type; - Emit( "; DECL %s -> %s\n", variable[i]->name, variable[i]->type->name ); + sym = getSym( ); + + if( sym == S_assign ) { + sym = getSym( ); + node = parseConstExpression( ); + if( type != node->actual_type ) { + Abort( "Assigning a variable of type '%s' from an expression of type '%s'", + type->name, node->actual_type->name ); + } + if( node->type == EXPRESSION_NODE_TYPE_CONST ) { + for( i = 0; i < nof_variables; i++ ) { + variable[i]->value = node->number; + variable[i]->type = node->actual_type; + } + } else if( node->type == EXPRESSION_NODE_TYPE_VAR ) { + for( i = 0; i < nof_variables; i++ ) { + variable[i]->value = node->symbol->value; + variable[i]->type = node->symbol->type; + } + } + } else { + for( i = 0; i < nof_variables; i++ ) { + variable[i]->value = 0; + variable[i]->type = type; + } } - sym = getSym( ); + for( i = 0; i < nof_variables; i++ ) { + Emit( "; DECL %s -> %s, %d\n", variable[i]->name, variable[i]->type->name, variable[i]->value ); + } } static void parseVariableBlock( void ) diff --git a/ecomp-c/tests/bool_conditions.easm b/ecomp-c/tests/bool_conditions.easm index 466f445..aaebb3e 100644 --- a/ecomp-c/tests/bool_conditions.easm +++ b/ecomp-c/tests/bool_conditions.easm @@ -1,10 +1,10 @@ format binary use32 org $1000000 -; DECL a -> integer -; DECL b -> integer -; DECL c -> integer -; DECL d -> integer +; DECL a -> integer, 0 +; DECL b -> integer, 0 +; DECL c -> integer, 0 +; DECL d -> integer, 0 ; LET a <- 2 mov eax, 2 push eax diff --git a/ecomp-c/tests/boolean_variable.easm b/ecomp-c/tests/boolean_variable.easm index 13c5493..d41269d 100644 --- a/ecomp-c/tests/boolean_variable.easm +++ b/ecomp-c/tests/boolean_variable.easm @@ -1,9 +1,9 @@ format binary use32 org $1000000 -; DECL a -> integer -; DECL b -> integer -; DECL flag -> boolean +; DECL a -> integer, 0 +; DECL b -> integer, 0 +; DECL flag -> boolean, 0 ; LET a <- 1 mov eax, 1 push eax diff --git a/ecomp-c/tests/example_divisors.easm b/ecomp-c/tests/example_divisors.easm index 564918b..9c3a7b3 100644 --- a/ecomp-c/tests/example_divisors.easm +++ b/ecomp-c/tests/example_divisors.easm @@ -1,9 +1,9 @@ format binary use32 org $1000000 -; DECL n -> integer -; DECL i -> integer -; DECL nof -> integer +; DECL n -> integer, 0 +; DECL i -> integer, 0 +; DECL nof -> integer, 0 ; LET n <- 100 mov eax, 100 push eax diff --git a/ecomp-c/tests/if_statement.easm b/ecomp-c/tests/if_statement.easm index bb5f163..212211a 100644 --- a/ecomp-c/tests/if_statement.easm +++ b/ecomp-c/tests/if_statement.easm @@ -1,9 +1,9 @@ format binary use32 org $1000000 -; DECL a -> integer -; DECL b -> integer -; DECL c -> integer +; DECL a -> integer, 0 +; DECL b -> integer, 0 +; DECL c -> integer, 0 ; LET a <- 2 mov eax, 2 push eax diff --git a/ecomp-c/tests/type_check_assignment.easm b/ecomp-c/tests/type_check_assignment.easm index 5c13f33..73e7972 100644 --- a/ecomp-c/tests/type_check_assignment.easm +++ b/ecomp-c/tests/type_check_assignment.easm @@ -1,8 +1,8 @@ format binary use32 org $1000000 -; DECL i -> integer -; DECL b -> boolean +; DECL i -> integer, 0 +; DECL b -> boolean, 0 ; LET i <- 1 mov eax, 1 push eax diff --git a/ecomp-c/tests/type_check_comparision.easm b/ecomp-c/tests/type_check_comparision.easm index 2441f17..7bc8e90 100644 --- a/ecomp-c/tests/type_check_comparision.easm +++ b/ecomp-c/tests/type_check_comparision.easm @@ -1,8 +1,8 @@ format binary use32 org $1000000 -; DECL i -> integer -; DECL b -> boolean +; DECL i -> integer, 0 +; DECL b -> boolean, 0 ; LET i <- 1 mov eax, 1 push eax diff --git a/ecomp-c/tests/variable_assign_from_constant.easm b/ecomp-c/tests/variable_assign_from_constant.easm index 3fb03d6..d92d138 100644 --- a/ecomp-c/tests/variable_assign_from_constant.easm +++ b/ecomp-c/tests/variable_assign_from_constant.easm @@ -2,7 +2,7 @@ format binary use32 org $1000000 ; CONST N -> integer, 20 -; DECL a -> integer +; DECL a -> integer, 0 ; LET a <- 20 mov eax, 20 push eax diff --git a/ecomp-c/tests/variable_assign_from_expression.easm b/ecomp-c/tests/variable_assign_from_expression.easm index 0ea76fb..7ed32c1 100644 --- a/ecomp-c/tests/variable_assign_from_expression.easm +++ b/ecomp-c/tests/variable_assign_from_expression.easm @@ -2,11 +2,11 @@ format binary use32 org $1000000 ; CONST N -> integer, 20 -; DECL a -> integer -; DECL b -> integer -; DECL c -> integer -; DECL d -> integer -; DECL e -> integer +; DECL a -> integer, 0 +; DECL b -> integer, 0 +; DECL c -> integer, 0 +; DECL d -> integer, 0 +; DECL e -> integer, 0 ; LET a <- 1 mov eax, 1 push eax diff --git a/ecomp-c/tests/variable_assign_from_type.easm b/ecomp-c/tests/variable_assign_from_type.easm index 3c00c78..c89a9c2 100644 --- a/ecomp-c/tests/variable_assign_from_type.easm +++ b/ecomp-c/tests/variable_assign_from_type.easm @@ -2,4 +2,4 @@ format binary use32 org $1000000 ; CONST N -> integer, 20 -; DECL a -> integer +; DECL a -> integer, 0 diff --git a/ecomp-c/tests/variable_assign_from_variable.easm b/ecomp-c/tests/variable_assign_from_variable.easm index e3024ed..2a480af 100644 --- a/ecomp-c/tests/variable_assign_from_variable.easm +++ b/ecomp-c/tests/variable_assign_from_variable.easm @@ -2,8 +2,8 @@ format binary use32 org $1000000 ; CONST N -> integer, 20 -; DECL a -> integer -; DECL b -> integer +; DECL a -> integer, 0 +; DECL b -> integer, 0 ; LET a <- 20 mov eax, 20 push eax diff --git a/ecomp-c/tests/variable_initialization.e b/ecomp-c/tests/variable_initialization.e index 8387cdd..775829b 100644 --- a/ecomp-c/tests/variable_initialization.e +++ b/ecomp-c/tests/variable_initialization.e @@ -5,7 +5,7 @@ module variable_initialization; const - N : integer = 1; this is a constant, exists only during compilation + N : integer = 1; // this is a constant, exists only during compilation var a : integer; // not initialized diff --git a/ecomp-c/tests/variable_initialization.easm b/ecomp-c/tests/variable_initialization.easm new file mode 100644 index 0000000..facf5d4 --- /dev/null +++ b/ecomp-c/tests/variable_initialization.easm @@ -0,0 +1,16 @@ +format binary +use32 +org $1000000 +; CONST N -> integer, 1 +; DECL a -> integer, 0 +; DECL b -> integer, 1 +; DECL c -> integer, 0 +; LET c <- 2 +mov eax, 2 +push eax +pop eax +mov [c], eax +hlt +c: dd $00000000 +b: dd $00000001 +a: dd $00000000 diff --git a/ecomp-c/tests/variable_initialization.easm_err b/ecomp-c/tests/variable_initialization.easm_err new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ecomp-c/tests/variable_initialization.easm_err diff --git a/ecomp-c/tests/variable_initialization.ecomp_err b/ecomp-c/tests/variable_initialization.ecomp_err new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ecomp-c/tests/variable_initialization.ecomp_err diff --git a/ecomp-c/tests/variable_initialization.erun b/ecomp-c/tests/variable_initialization.erun new file mode 100644 index 0000000..4c00a35 --- /dev/null +++ b/ecomp-c/tests/variable_initialization.erun @@ -0,0 +1,162 @@ +Read 25 bytes of code.. +1000000: B802000000 mov eax, 2 +1000005: 50 push eax +1000006: 58 pop eax +1000007: A30D000001 mov dword ptr [0x100000d], eax +100000C: F4 hlt +100000D: 0000 data +100000F: 0000 data +1000011: 0100 data +1000013: 0000 data +1000015: 0000 data +1000017: 0000 data +core start 1000000 +data start 100000d +stack start 1800000 +Single step execution: +-- iteration 1 +1000000: B802000000 mov eax, 2 +EIP: 01000005 +ESP: 01800000 +EBP: 00000000 +EAX: 00000002 +EBX: 00000000 +ECX: 00000000 +EDX: 00000000 +ESI: 00000000 +EDI: 00000000 +stack: +data: +0100000D: 00000000 +01000011: 00000001 +01000015: 00000000 +01000019: 00000000 +0100001D: 00000000 +01000021: 00000000 +01000025: 00000000 +01000029: 00000000 +0100002D: 00000000 +01000031: 00000000 +01000035: 00000000 +01000039: 00000000 +0100003D: 00000000 +01000041: 00000000 +01000045: 00000000 +01000049: 00000000 +-- iteration 2 +1000005: 50 push eax +EIP: 01000006 +ESP: 017FFFFC +EBP: 00000000 +EAX: 00000002 +EBX: 00000000 +ECX: 00000000 +EDX: 00000000 +ESI: 00000000 +EDI: 00000000 +stack: +017FFFFC: 00000002 +data: +0100000D: 00000000 +01000011: 00000001 +01000015: 00000000 +01000019: 00000000 +0100001D: 00000000 +01000021: 00000000 +01000025: 00000000 +01000029: 00000000 +0100002D: 00000000 +01000031: 00000000 +01000035: 00000000 +01000039: 00000000 +0100003D: 00000000 +01000041: 00000000 +01000045: 00000000 +01000049: 00000000 +-- iteration 3 +1000006: 58 pop eax +EIP: 01000007 +ESP: 01800000 +EBP: 00000000 +EAX: 00000002 +EBX: 00000000 +ECX: 00000000 +EDX: 00000000 +ESI: 00000000 +EDI: 00000000 +stack: +data: +0100000D: 00000000 +01000011: 00000001 +01000015: 00000000 +01000019: 00000000 +0100001D: 00000000 +01000021: 00000000 +01000025: 00000000 +01000029: 00000000 +0100002D: 00000000 +01000031: 00000000 +01000035: 00000000 +01000039: 00000000 +0100003D: 00000000 +01000041: 00000000 +01000045: 00000000 +01000049: 00000000 +-- iteration 4 +1000007: A30D000001 mov dword ptr [0x100000d], eax +EIP: 0100000C +ESP: 01800000 +EBP: 00000000 +EAX: 00000002 +EBX: 00000000 +ECX: 00000000 +EDX: 00000000 +ESI: 00000000 +EDI: 00000000 +stack: +data: +0100000D: 00000002 +01000011: 00000001 +01000015: 00000000 +01000019: 00000000 +0100001D: 00000000 +01000021: 00000000 +01000025: 00000000 +01000029: 00000000 +0100002D: 00000000 +01000031: 00000000 +01000035: 00000000 +01000039: 00000000 +0100003D: 00000000 +01000041: 00000000 +01000045: 00000000 +01000049: 00000000 +-- iteration 5 +100000C: F4 hlt +EIP: 0100000D +ESP: 01800000 +EBP: 00000000 +EAX: 00000002 +EBX: 00000000 +ECX: 00000000 +EDX: 00000000 +ESI: 00000000 +EDI: 00000000 +stack: +data: +0100000D: 00000002 +01000011: 00000001 +01000015: 00000000 +01000019: 00000000 +0100001D: 00000000 +01000021: 00000000 +01000025: 00000000 +01000029: 00000000 +0100002D: 00000000 +01000031: 00000000 +01000035: 00000000 +01000039: 00000000 +0100003D: 00000000 +01000041: 00000000 +01000045: 00000000 +01000049: 00000000 diff --git a/ecomp-c/tests/variable_name_as_type.easm b/ecomp-c/tests/variable_name_as_type.easm index ffbfbe0..f0e7391 100644 --- a/ecomp-c/tests/variable_name_as_type.easm +++ b/ecomp-c/tests/variable_name_as_type.easm @@ -1,4 +1,4 @@ format binary use32 org $1000000 -; DECL a -> integer +; DECL a -> integer, 0 diff --git a/ecomp-c/tests/variable_not_initialized.easm b/ecomp-c/tests/variable_not_initialized.easm index 665c916..92f06c6 100644 --- a/ecomp-c/tests/variable_not_initialized.easm +++ b/ecomp-c/tests/variable_not_initialized.easm @@ -1,5 +1,5 @@ format binary use32 org $1000000 -; DECL a -> integer -; DECL b -> integer +; DECL a -> integer, 0 +; DECL b -> integer, 0 diff --git a/ecomp-c/tests/while_statement.easm b/ecomp-c/tests/while_statement.easm index d683add..6b60250 100644 --- a/ecomp-c/tests/while_statement.easm +++ b/ecomp-c/tests/while_statement.easm @@ -1,7 +1,7 @@ format binary use32 org $1000000 -; DECL i -> integer +; DECL i -> integer, 0 ; LET i <- 0 mov eax, 0 push eax |