summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--boot/definitions.inc2
-rw-r--r--boot/stage1.s216
-rw-r--r--boot/stage2.elna75
-rw-r--r--boot/symbol.s56
-rw-r--r--boot/test.elna11
5 files changed, 102 insertions, 258 deletions
diff --git a/boot/definitions.inc b/boot/definitions.inc
index 731dda3..88f6e8b 100644
--- a/boot/definitions.inc
+++ b/boot/definitions.inc
@@ -62,5 +62,7 @@
#
.equ TYPE_PRIMITIVE, 0x01
.equ TYPE_POINTER, 0x02
+.equ TYPE_PROCEDURE, 0x03
.equ INFO_PARAMETER, 0x10
.equ INFO_LOCAL, 0x20
+.equ INFO_PROCEDURE, 0x30
diff --git a/boot/stage1.s b/boot/stage1.s
index 15e3d20..a45d8ab 100644
--- a/boot/stage1.s
+++ b/boot/stage1.s
@@ -221,34 +221,6 @@ compile_binary_expression:
addi sp, sp, 32
ret
-# Checks whether the given identifier starts with "loca".
-# Parameters:
-# a0 - Pointer to the identifier.
-# a1 - Identifier length.
-#
-# Sets a0 to 1 if the identifier starts with "loca", otherwise to 0.
-.type is_local_identifier, @function
-is_local_identifier:
- # Prologue.
- addi sp, sp, -16
- sw ra, 12(sp)
- sw s0, 8(sp)
- addi s0, sp, 16
-
- li t0, 0x61636f6c # loca
- sw t0, 4(sp)
- # a0 is already set.
- addi a1, sp, 4
- li a2, 4
- call _memcmp
- seqz a0, a0
-
- # Epilogue.
- lw ra, 12(sp)
- lw s0, 8(sp)
- addi sp, sp, 16
- ret
-
# Looks for a register that can be used to calculate a symbol address. Writes it
# as string, like sp or s0 into the provided buffer.
#
@@ -308,7 +280,7 @@ compile_identifier_expression:
lw a0, 20(sp)
mv a1, s1
- call symbol_table_find
+ call symbol_table_lookup
sw a0, 12(sp)
beqz a0, .Lcompile_identifier_expression_by_name
@@ -412,21 +384,22 @@ compile_expression:
call lex_next
sw a0, 20(sp)
- lw a0, 24(sp)
+ lw t0, 24(sp)
- li t0, TOKEN_MINUS
- beq a0, t0, .Lcompile_expression_negate
+ li t1, TOKEN_MINUS
+ beq t0, t1, .Lcompile_expression_negate
- li t0, TOKEN_AT
- beq a0, t0, .Lcompile_expression_address
+ li t1, TOKEN_AT
+ beq t0, t1, .Lcompile_expression_address
- li t0, TOKEN_INTEGER
- beq a0, t0, .Lcompile_expression_literal
+ li t1, TOKEN_INTEGER
+ beq t0, t1, .Lcompile_expression_literal
- lw a0, 32(sp)
- lbu a0, (a0)
- li t0, '_'
- beq a0, t0, .Lcompile_expression_call
+ addi a1, sp, 8
+ call lex_next
+ lw t0, 8(sp)
+ li t1, TOKEN_LEFT_PAREN
+ beq t0, t1, .Lcompile_expression_call
lw s1, 32(sp)
lw a0, 28(sp)
@@ -456,9 +429,6 @@ compile_expression:
j .Lcompile_expression_end
.Lcompile_expression_call:
- lw a0, 20(sp)
- addi a1, sp, 8
- call lex_next
mv s1, a0
lw a0, 32(sp)
@@ -518,7 +488,7 @@ compile_at_expression:
lw a0, 28(sp)
lw a1, 32(sp)
- call symbol_table_find
+ call symbol_table_lookup
sw a0, 20(sp)
li t0, 0x20 # _
@@ -576,37 +546,43 @@ compile_designator_expression:
sw a0, 20(sp) # Identifier pointer.
sw a1, 16(sp) # Identifier length.
+ /* DEBUG
lw a0, 20(sp)
lw a1, 16(sp)
- call is_local_identifier
- bnez a0, .Lcompile_designator_expression_local
+ call _write_error */
+
+.Lcompile_designator_expression_by_address:
+ lw a0, 16(sp)
+ lw a1, 20(sp)
+ call symbol_table_lookup
+ sw a0, 12(sp)
-.Lcompile_designator_expression_local:
li t0, 0x202c30 # 0,_
- sw t0, 12(sp)
- li t0, 0x61207773 # sw a
sw t0, 8(sp)
+ li t0, 0x61207773 # sw a
+ sw t0, 4(sp)
li a0, 7
- addi a1, sp, 8
+ addi a1, sp, 4
call _write_s
- lw a0, 16(sp)
- lw a1, 20(sp)
- addi a0, a0, -4 # Skip the "loca" variable prefix.
- addi a1, a1, 4 # Skip the "loca" variable prefix.
- call _write_s
+ lw a0, 12(sp)
+ lw a0, 8(a0)
+ call _write_i
- li t0, '\n'
- sw t0, 12(sp)
- li t0, 0x29707328 # (sp)
- sw t0, 8(sp)
- li a0, 5
- addi a1, sp, 8
+ li a0, '('
+ call _write_c
+
+ lw a0, 12(sp)
+ addi a1, sp, 4
+ call take_address
+ addi a1, sp, 4
call _write_s
- j .Lcompile_designator_expression_end
+ li a0, ')'
+ call _write_c
+ li a0, '\n'
+ call _write_c
-.Lcompile_designator_expression_end:
# Epilogue.
lw ra, 28(sp)
lw s0, 24(sp)
@@ -635,12 +611,12 @@ compile_identifier:
lw t0, 0(sp)
- li t1, TOKEN_ASSIGN
- beq t0, t1, .Lcompile_identifier_assign
-
li t1, TOKEN_LEFT_PAREN
beq t0, t1, .Lcompile_identifier_call
+ li t1, TOKEN_ASSIGN
+ beq t0, t1, .Lcompile_identifier_assign
+
j .Lcompile_identifier_end
.Lcompile_identifier_call:
@@ -813,93 +789,6 @@ compile_module_declaration:
addi sp, sp, 24
ret
-.type compile_constant_section, @function
-compile_constant_section:
- # Prologue.
- addi sp, sp, -32
- sw ra, 28(sp)
- sw s0, 24(sp)
- addi s0, sp, 32
-
- mv a0, s1
- addi a1, sp, 4
- call lex_next
- li t0, TOKEN_CONST
- lw t1, 4(sp)
- bne t0, t1, .Lcompile_constant_section_end
- mv s1, a0
-
- li a0, SECTION_RODATA_SIZE
- la a1, section_rodata
- call _write_s
-
-.Lcompile_constant_section_item:
- mv a0, s1
- addi a1, sp, 12
- call lex_next
-
- lw t0, 12(sp)
- li t1, TOKEN_IDENTIFIER
-
- bne t0, t1, .Lcompile_constant_section_end
- lw s1, 20(sp)
-
- call compile_constant
- j .Lcompile_constant_section_item
-
-.Lcompile_constant_section_end:
- # Epilogue.
- lw ra, 28(sp)
- lw s0, 24(sp)
- addi sp, sp, 32
- ret
-
-.type compile_constant, @function
-compile_constant:
- # Prologue.
- addi sp, sp, -32
- sw ra, 28(sp)
- sw s0, 24(sp)
- addi s0, sp, 32
-
- mv a0, s1
- addi a1, sp, 12
- call lex_next
- addi a1, sp, 0
- call lex_next # Skip the assignment sign.
- mv s1, a0
- # Write identifier the identifier.
- lw a0, 16(sp)
- lw a1, 20(sp)
- call _write_s
-
- # : .long
- li t0, 0x20676e6f # ong_
- sw t0, 4(sp)
- li t0, 0x6c2e203a # : .l
- sw t0, 0(sp)
- li a0, 8
- mv a1, sp
- call _write_s
-
- mv a0, s1
- addi a1, sp, 12
- call lex_next
- mv s1, a0
-
- lw a0, 16(sp) # The literal length.
- lw a1, 20(sp) # Save the literal pointer before advancing it.
- call _write_s
-
- li a0, '\n'
- call _write_c
-
- # Epilogue.
- lw ra, 28(sp)
- lw s0, 24(sp)
- addi sp, sp, 32
- ret
-
# Compiles global variable section.
.type compile_global_section, @function
compile_global_section:
@@ -1028,7 +917,7 @@ compile_type_expression:
# Named type.
lw a0, 16(sp)
lw a1, 20(sp)
- call symbol_table_find
+ call symbol_table_lookup
j .Lcompile_type_expression_end
@@ -1093,7 +982,7 @@ compile_local_section:
lw a0, 32(sp)
lw a1, 36(sp)
- call symbol_table_insert
+ call symbol_table_enter
lw t0, 12(sp)
addi t0, t0, 4
@@ -1152,7 +1041,7 @@ compile_parameters:
lw a0, 32(sp)
lw a1, 36(sp)
- call symbol_table_insert
+ call symbol_table_enter
lw t0, 12(sp)
addi t0, t0, -4
@@ -1201,10 +1090,16 @@ compile_procedure:
lw a1, 20(sp)
call write_procedure_head
- /* DEBUG */
- lw a0, 20(sp)
- lw a1, 16(sp)
- call _write_error
+ # Register the procedure in the symbol table.
+ mv a0, s3
+ call symbol_table_make_procedure
+
+ mv a2, s3
+ add s3, s3, a0
+
+ lw a0, 16(sp)
+ lw a1, 20(sp)
+ call symbol_table_enter
# Save the state of the symbol table before we enter the procedure scope.
la t0, symbol_table
@@ -1618,7 +1513,6 @@ compile:
call compile_module_declaration
call compile_import
- call compile_constant_section
call compile_global_section
call compile_text_section
call compile_procedure_section
diff --git a/boot/stage2.elna b/boot/stage2.elna
index e38874c..b4f359e 100644
--- a/boot/stage2.elna
+++ b/boot/stage2.elna
@@ -2,9 +2,6 @@ program
import dummy
-const
- SOURCE_BUFFER_SIZE := 81920
-
var
source_code: [81920]Byte
@@ -661,66 +658,6 @@ begin
_advance(8)
end
-proc _compile_constant_section()
-var
- loca0: Word
- loca4: ^Byte
-begin
- loca0 := 0x6365732e;
- _write_s(4, @loca0);
- loca0 := 0x6e6f6974;
- _write_s(4, @loca0);
- loca0 := 0x6f722e20;
- _write_s(4, @loca0);
- loca0 := 0x61746164;
- _write_s(4, @loca0);
- loca0 := 0x0a;
- _write_s(1, @loca0);
-
- _advance(6);
-
- .Lcompile_constant_section_item;
- _skip_spaces();
- loca4 := _current();
- loca0 := _front(loca4);
- if _is_upper(loca0) then
- _compile_constant();
- goto .Lcompile_constant_section_item
- end;
-
- .Lcompile_constant_section_end
-end
-
-proc _compile_constant()
-var
- loca0: Word
- loca4: Word
- loca8: ^Byte
-begin
- loca4 := _read_token();
- loca8 := _current();
-
- _write_s(loca4, loca8);
- _advance(loca4);
-
- _skip_spaces();
- _advance(2);
-
- loca0 := 0x6c2e203a;
- _write_s(4, @loca0);
- loca0 := 0x20676e6f;
- _write_s(4, @loca0);
-
- _skip_spaces();
- loca4 := _read_token();
- loca8 := _current();
-
- _write_s(loca4, loca8);
- _advance(loca4);
-
- _write_c(0x0a)
-end
-
proc _compile_variable_section()
var
loca0: Word
@@ -1186,12 +1123,6 @@ begin
goto .Lcompile_line_program
end;
- loca12 := 0x736e6f63;
- loca4 := _memcmp(loca16, @loca12, 4);
- if loca4 = 0 then
- goto .Lcompile_line_const
- end;
-
loca12 := 0x0a726176;
loca4 := _memcmp(loca16, @loca12, 4);
if loca4 = 0 then
@@ -1297,10 +1228,6 @@ begin
loca8 := 1;
goto .Lcompile_line_end;
- .Lcompile_line_const;
- _compile_constant_section();
- goto .Lcompile_line_section;
-
.Lcompile_line_procedure;
if loca80 = 1 then
goto .Lcompile_line_compile_procedure
@@ -1455,7 +1382,7 @@ end
proc _main()
begin
- _read_file(source_code, SOURCE_BUFFER_SIZE);
+ _read_file(source_code, 81920);
_label_counter(0)
end
diff --git a/boot/symbol.s b/boot/symbol.s
index fe88aef..fdf4ad2 100644
--- a/boot/symbol.s
+++ b/boot/symbol.s
@@ -3,8 +3,8 @@
# obtain one at https://mozilla.org/MPL/2.0/.
.global symbol_table
-.global symbol_table_build, symbol_table_find, symbol_table_insert, symbol_table_dump
-.global symbol_table_make_pointer, symbol_table_make_parameter, symbol_table_make_local
+.global symbol_table_build, symbol_table_lookup, symbol_table_enter, symbol_table_dump
+.global symbol_table_make_pointer, symbol_table_make_parameter, symbol_table_make_local, symbol_table_make_procedure
.include "boot/definitions.inc"
@@ -101,8 +101,8 @@ symbol_table_dump:
# a1 - Pointer to the symbol name.
#
# Sets a0 to the symbol info pointer or 0 if the symbol has not been found.
-.type symbol_table_find, @function
-symbol_table_find:
+.type symbol_table_lookup, @function
+symbol_table_lookup:
# Prologue.
addi sp, sp, -32
sw ra, 28(sp)
@@ -121,8 +121,8 @@ symbol_table_find:
lw s2, 0(s1)
addi s1, s1, 4 # Advance to the first symbol in the table.
-.Lsymbol_table_find_loop:
- beqz s2, .Lsymbol_table_find_not_found
+.Lsymbol_table_lookup_loop:
+ beqz s2, .Lsymbol_table_lookup_not_found
# Compare string lengths.
mv a0, s3
@@ -131,20 +131,20 @@ symbol_table_find:
lw a3, 4(s1)
call _string_equal
- beqz a0, .Lsymbol_table_find_continue
+ beqz a0, .Lsymbol_table_lookup_continue
lw a0, 8(s1) # Pointer to the symbol.
- j .Lsymbol_table_find_end
+ j .Lsymbol_table_lookup_end
-.Lsymbol_table_find_continue:
+.Lsymbol_table_lookup_continue:
addi s1, s1, 12
addi s2, s2, -1
- j .Lsymbol_table_find_loop
+ j .Lsymbol_table_lookup_loop
-.Lsymbol_table_find_not_found:
+.Lsymbol_table_lookup_not_found:
li a0, 0
-.Lsymbol_table_find_end:
+.Lsymbol_table_lookup_end:
lw s1, 20(sp)
lw s2, 16(sp)
lw s3, 12(sp)
@@ -208,14 +208,32 @@ symbol_table_make_local:
li a0, 12
ret
+# Creates a procedure type and procedure info objects refering the type.
+#
+# Parameters:
+# a0 - Output memory.
+#
+# Sets a0 to the size of newly created info object in bytes.
+.type symbol_table_make_procedure, @function
+symbol_table_make_procedure:
+ li t0, TYPE_PROCEDURE
+ sw t0, 8(a0)
+
+ li t0, INFO_PROCEDURE
+ sw t0, 0(a0)
+ sw a0, 4(a0) # Procedure type stored in the same memory segment.
+
+ li a0, 12
+ ret
+
# Inserts a symbol into the table.
#
# Parameters:
# a0 - Symbol name length.
# a1 - Symbol name pointer.
# a2 - Symbol pointer.
-.type symbol_table_insert, @function
-symbol_table_insert:
+.type symbol_table_enter, @function
+symbol_table_enter:
la t0, symbol_table
lw t1, 0(t0) # Current table length.
@@ -250,27 +268,27 @@ symbol_table_build:
li a0, 3 # Length of the word "Int".
la a1, symbol_builtin_name_int
la a2, symbol_builtin_type_int
- call symbol_table_insert
+ call symbol_table_enter
li a0, 4 # Length of the word "Word".
la a1, symbol_builtin_name_word
la a2, symbol_builtin_type_word
- call symbol_table_insert
+ call symbol_table_enter
li a0, 4 # Length of the word "Byte".
la a1, symbol_builtin_name_byte
la a2, symbol_builtin_type_byte
- call symbol_table_insert
+ call symbol_table_enter
li a0, 4 # Length of the word "Char".
la a1, symbol_builtin_name_char
la a2, symbol_builtin_type_char
- call symbol_table_insert
+ call symbol_table_enter
li a0, 4 # Length of the word "Bool".
la a1, symbol_builtin_name_bool
la a2, symbol_builtin_type_bool
- call symbol_table_insert
+ call symbol_table_enter
# Epilogue.
lw ra, 12(sp)
diff --git a/boot/test.elna b/boot/test.elna
index 3b7a962..e56547d 100644
--- a/boot/test.elna
+++ b/boot/test.elna
@@ -1,11 +1,14 @@
program
-proc _main(x: Word, y: Word)
+proc main(x: Word, y: Word)
begin
- _write_s(@x, 4);
- _write_s(@y, 4)
+ _write_s(4, @x);
+ _write_s(4, @y);
+
+ y := 0x0a2c3063;
+ _write_s(4, @y)
end
begin
- _main(0x0a2c3061, 0x0a2c3062)
+ main(0x0a2c3061, 0x0a2c3062)
end.