summaryrefslogtreecommitdiff
path: root/boot/stage1.s
diff options
context:
space:
mode:
Diffstat (limited to 'boot/stage1.s')
-rw-r--r--boot/stage1.s265
1 files changed, 117 insertions, 148 deletions
diff --git a/boot/stage1.s b/boot/stage1.s
index 525da11..b39f5bc 100644
--- a/boot/stage1.s
+++ b/boot/stage1.s
@@ -411,14 +411,6 @@ _build_expression:
call _tokenize_next
sw a0, 20(sp)
- /* DEBUG
- lw a0, 32(sp)
- lw a1, 28(sp)
- call _write_error
- lw a0, 28(sp)
- li a1, 8
- call _write_error */
-
lw a0, 24(sp)
li t0, TOKEN_MINUS
@@ -754,74 +746,6 @@ _compile_call:
addi sp, sp, 32
ret
-# Skips the spaces till the next non space character.
-.type _skip_spaces, @function
-_skip_spaces:
-.Lspace_loop_do:
- lbu t0, (s1) # t0 = Current character.
-
- li t1, ' '
- beq t0, t1, .Lspace_loop_repeat
- li t1, '\t'
- beq t0, t1, .Lspace_loop_repeat
- li t1, '\n'
- beq t0, t1, .Lspace_loop_repeat
- li t1, '\r'
- beq t0, t1, .Lspace_loop_repeat
-
- j .Lspace_loop_end
-.Lspace_loop_repeat:
- addi s1, s1, 1
- j .Lspace_loop_do
-
-.Lspace_loop_end:
- ret
-
-# Parameters:
-# a0 - Line length.
-.type _skip_comment, @function
-_skip_comment:
- # Prologue.
- addi sp, sp, -16
- sw ra, 12(sp)
- sw s0, 8(sp)
- addi s0, sp, 16
-
- # Check whether this is a comment.
- li t0, 0x2a28 # (*
- sw t0, 4(sp)
- addi a0, sp, 4
- mv a1, s1
- li a2, 2
- call _memcmp
- bnez a0, .Lskip_comment_end
-
- addi s1, s1, 2 # Skip (*.
-
- li t0, 0x292a # *)
- sw t0, 4(sp)
-
-.Lskip_comment_loop:
- addi a0, sp, 4
- mv a1, s1
- li a2, 2
- call _memcmp
- beqz a0, .Lskip_comment_close
-
- addi s1, s1, 1
-
- j .Lskip_comment_loop
-
-.Lskip_comment_close:
- addi s1, s1, 2 # Skip *).
-
-.Lskip_comment_end:
- # Epilogue.
- lw ra, 12(sp)
- lw s0, 8(sp)
- addi sp, sp, 16
- ret
-
# Walks through the procedure definitions.
.type _compile_procedure_section, @function
_compile_procedure_section:
@@ -832,9 +756,6 @@ _compile_procedure_section:
addi s0, sp, 32
.Lcompile_procedure_section_loop:
- call _skip_spaces
- call _skip_comment
-
mv a0, s1
addi a1, sp, 4
call _tokenize_next
@@ -1083,28 +1004,9 @@ _compile_procedure:
call _tokenize_next
mv s1, a0
- # .type identifier, @function
- la a0, asm_type
- li a1, ASM_TYPE_SIZE
- call _write_out
-
- lw a0, 20(sp)
- lw a1, 16(sp)
- call _write_out
-
- la a0, asm_type_function
- li a1, ASM_TYPE_FUNCTION_SIZE
- call _write_out
-
- lw a0, 20(sp)
- lw a1, 16(sp)
- call _write_out
-
- li t0, 0x0a3a # :\n
- sw t0, 12(sp)
- addi a0, sp, 12
- li a1, 2
- call _write_out
+ lw a0, 16(sp)
+ lw a1, 20(sp)
+ call _write_procedure_head
# Skip all declarations until we find the "begin" keyword, denoting the
# beginning of the procedure body.
@@ -1128,21 +1030,24 @@ _compile_procedure:
call _write_out
# Generate the body of the procedure.
-.Lcompile_procedure_body:
- li t0, 0x0a646e65 # end\n
- sw t0, 8(sp)
- mv a0, s1
- addi a1, sp, 8
- li a2, 4
- call _memcmp
-
- beqz a0, .Lcompile_procedure_end
-
- call _compile_statement
- j .Lcompile_procedure_body
+ call _compile_statements
+ mv s1, a0 # Skip end.
-.Lcompile_procedure_end:
- add s1, s1, 4 # Skip end\n.
+ /* DEBUG
+ sw a0, 8(sp)
+ lw a1, 12(sp)
+ li a2, TOKEN_END
+ sub a1, a1, a2
+ seqz a1, a1
+ seqz a0, a0
+ addi a0, a0, '0'
+ addi a1, a1, '0'
+ sb a0, 4(sp)
+ sb a1, 5(sp)
+ addi a0, sp, 4
+ li a1, 2
+ call _write_error
+ lw a0, 8(sp) */
# Generate the procedure epilogue with a predefined stack size.
la a0, epilogue
@@ -1288,21 +1193,8 @@ _compile_if:
li a0, '\n'
call _put_char
-.Lcompile_if_loop:
- mv a0, s1
- addi a1, sp, 0
- call _tokenize_next
-
- lw t0, 0(sp)
- li t1, TOKEN_END
- beq t0, t1, .Lcompile_if_end
-
- call _compile_statement
-
- j .Lcompile_if_loop
-
-.Lcompile_if_end:
- mv s1, a0 # Skip the end with newline. a0 is set by the last call to _tokenize_next.
+ call _compile_statements
+ mv s1, a0 # Skip end.
# Write the label prefix.
addi a0, sp, 20
@@ -1328,6 +1220,95 @@ _compile_if:
addi sp, sp, 32
ret
+# Writes:
+# .type identifier, @function
+# identifier:
+#
+# Parameters:
+# a0 - Identifier length.
+# a0 - Identifier pointer.
+.type _write_procedure_head, @function
+_write_procedure_head:
+ # Prologue.
+ addi sp, sp, -32
+ sw ra, 28(sp)
+ sw s0, 24(sp)
+ addi s0, sp, 32
+
+ sw a0, 16(sp)
+ sw a1, 20(sp)
+
+ # .type identifier, @function
+ la a0, asm_type
+ li a1, ASM_TYPE_SIZE
+ call _write_out
+
+ lw a0, 20(sp)
+ lw a1, 16(sp)
+ call _write_out
+
+ la a0, asm_type_function
+ li a1, ASM_TYPE_FUNCTION_SIZE
+ call _write_out
+
+ lw a0, 20(sp)
+ lw a1, 16(sp)
+ call _write_out
+
+ li t0, 0x0a3a # :\n
+ sw t0, 12(sp)
+ addi a0, sp, 12
+ li a1, 2
+ call _write_out
+
+ # Epilogue.
+ lw ra, 28(sp)
+ lw s0, 24(sp)
+ addi sp, sp, 32
+ ret
+
+# Compiles a list of statements delimited by semicolons.
+#
+# Sets a0 to the end of the token finishing the list
+# (should be the "end" token in a valid program).
+.type _compile_statements, @function
+_compile_statements:
+ # Prologue.
+ addi sp, sp, -32
+ sw ra, 28(sp)
+ sw s0, 24(sp)
+ addi s0, sp, 32
+
+ # Generate the body of the procedure.
+ mv a0, s1
+ addi a1, sp, 0
+ call _tokenize_next
+ lw t0, 0(sp)
+ li t1, TOKEN_END
+
+ beq t0, t1, .Lcompile_statements_end
+
+.Lcompile_statements_body:
+ call _compile_statement
+
+ mv a0, s1
+ addi a1, sp, 0
+ call _tokenize_next
+ lw t0, 0(sp)
+ li t1, TOKEN_SEMICOLON
+
+ bne t0, t1, .Lcompile_statements_end
+ mv s1, a0
+
+ j .Lcompile_statements_body
+
+.Lcompile_statements_end:
+ # Epilogue.
+ lw ra, 28(sp)
+ lw s0, 24(sp)
+ addi sp, sp, 32
+ ret
+
# Checks for the type of the current statement and compiles it.
.type _compile_statement, @function
_compile_statement:
@@ -1357,7 +1338,7 @@ _compile_statement:
li t1, TOKEN_DOT
beq t0, t1, .Lcompile_statement_label
- j .Lcompile_statement_empty # Else.
+ unimp # Else.
.Lcompile_statement_if:
call _compile_if
@@ -1379,10 +1360,6 @@ _compile_statement:
call _compile_identifier
j .Lcompile_statement_end
-.Lcompile_statement_empty:
- addi s1, s1, 1
- j .Lcompile_statement_end
-
.Lcompile_statement_end:
# Epilogue.
lw ra, 28(sp)
@@ -1422,23 +1399,14 @@ _compile_entry_point:
li a1, ASM_START_SIZE
call _write_out
- addi s1, s1, 6 # Skip begin\n.
-
- # Generate the body of the procedure.
-.Lcompile_entry_point_body:
mv a0, s1
addi a1, sp, 4
call _tokenize_next
+ mv s1, a0 # Skip begin.
- lw t0, 4(sp)
- li t1, TOKEN_END
- beq t0, t1, .Lcompile_entry_point_end
-
- call _compile_statement
- j .Lcompile_entry_point_body
-
-.Lcompile_entry_point_end:
- mv s1, a0 # Skip end. a0 is set by the last _tokenize_next call.
+ # Generate the body of the procedure.
+ call _compile_statements
+ mv s1, a0 # Skip end.
la a0, asm_exit
li a1, ASM_EXIT_SIZE
@@ -1481,6 +1449,7 @@ _start:
call _read_file
li s2, 1
+ call symbol_table_build
call _compile
# Call exit.