Wrap the mmap2 syscall
This commit is contained in:
189
boot/stage1.s
189
boot/stage1.s
@@ -8,6 +8,7 @@
|
||||
# Registers used as global variables:
|
||||
# s1 - Contains the current position in the source text.
|
||||
# s2 - Label counter.
|
||||
# s3 - Dynamic memory region.
|
||||
#
|
||||
# - The compiler expects valid input, otherwise it will generate invalid
|
||||
# assembly or hang. There is no error checking, no semantic analysis, no
|
||||
@@ -95,13 +96,13 @@ _compile_import:
|
||||
.Lcompile_import_loop:
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
li t0, TOKEN_IMPORT
|
||||
lw t1, 0(sp)
|
||||
bne t0, t1, .Lcompile_import_end
|
||||
# a0 is set from the previous _tokenize_next call. Skip the module name.
|
||||
# a0 is set from the previous lex_next call. Skip the module name.
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
j .Lcompile_import_loop
|
||||
@@ -113,8 +114,8 @@ _compile_import:
|
||||
addi sp, sp, 24
|
||||
ret
|
||||
|
||||
.type _build_binary_expression, @function
|
||||
_build_binary_expression:
|
||||
.type compile_binary_expression, @function
|
||||
compile_binary_expression:
|
||||
# Prologue.
|
||||
addi sp, sp, -32
|
||||
sw ra, 28(sp)
|
||||
@@ -126,30 +127,30 @@ _build_binary_expression:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
lw t0, 12(sp)
|
||||
|
||||
li t1, TOKEN_AND
|
||||
beq t0, t1, .Lbuild_binary_expression_and
|
||||
beq t0, t1, .Lcompile_binary_expression_and
|
||||
|
||||
li t1, TOKEN_OR
|
||||
beq t0, t1, .Lbuild_binary_expression_or
|
||||
beq t0, t1, .Lcompile_binary_expression_or
|
||||
|
||||
li t1, TOKEN_PLUS
|
||||
beq t0, t1, .Lbuild_binary_expression_plus
|
||||
beq t0, t1, .Lcompile_binary_expression_plus
|
||||
|
||||
li t1, TOKEN_EQUALS
|
||||
beq t0, t1, .Lbuild_binary_expression_equal
|
||||
beq t0, t1, .Lcompile_binary_expression_equal
|
||||
|
||||
li t1, TOKEN_ASTERISK
|
||||
beq t0, t1, .Lbuild_binary_expression_product
|
||||
beq t0, t1, .Lcompile_binary_expression_product
|
||||
|
||||
li t1, TOKEN_MINUS
|
||||
beq t0, t1, .Lbuild_binary_expression_minus
|
||||
beq t0, t1, .Lcompile_binary_expression_minus
|
||||
|
||||
j .Lbuild_binary_expression_end
|
||||
j .Lcompile_binary_expression_end
|
||||
|
||||
.Lbuild_binary_expression_equal:
|
||||
.Lcompile_binary_expression_equal:
|
||||
mv s1, a0 # Skip =.
|
||||
li a0, 1
|
||||
call _build_expression
|
||||
@@ -161,9 +162,9 @@ _build_binary_expression:
|
||||
li a1, ASM_SEQZ_A0_SIZE
|
||||
call _write_out
|
||||
|
||||
j .Lbuild_binary_expression_end
|
||||
j .Lcompile_binary_expression_end
|
||||
|
||||
.Lbuild_binary_expression_and:
|
||||
.Lcompile_binary_expression_and:
|
||||
mv s1, a0 # Skip &.
|
||||
li a0, 1
|
||||
call _build_expression
|
||||
@@ -171,9 +172,9 @@ _build_binary_expression:
|
||||
li a1, ASM_AND_A0_A1_SIZE
|
||||
call _write_out
|
||||
|
||||
j .Lbuild_binary_expression_end
|
||||
j .Lcompile_binary_expression_end
|
||||
|
||||
.Lbuild_binary_expression_or:
|
||||
.Lcompile_binary_expression_or:
|
||||
mv s1, a0 # Skip or.
|
||||
li a0, 1
|
||||
call _build_expression
|
||||
@@ -181,9 +182,9 @@ _build_binary_expression:
|
||||
li a1, ASM_OR_A0_A1_SIZE
|
||||
call _write_out
|
||||
|
||||
j .Lbuild_binary_expression_end
|
||||
j .Lcompile_binary_expression_end
|
||||
|
||||
.Lbuild_binary_expression_plus:
|
||||
.Lcompile_binary_expression_plus:
|
||||
mv s1, a0 # Skip +.
|
||||
li a0, 1
|
||||
call _build_expression
|
||||
@@ -191,9 +192,9 @@ _build_binary_expression:
|
||||
li a1, ASM_ADD_A0_A1_SIZE
|
||||
call _write_out
|
||||
|
||||
j .Lbuild_binary_expression_end
|
||||
j .Lcompile_binary_expression_end
|
||||
|
||||
.Lbuild_binary_expression_minus:
|
||||
.Lcompile_binary_expression_minus:
|
||||
mv s1, a0 # Skip -.
|
||||
li a0, 1
|
||||
call _build_expression
|
||||
@@ -201,9 +202,9 @@ _build_binary_expression:
|
||||
li a1, ASM_SUB_A0_A1_SIZE
|
||||
call _write_out
|
||||
|
||||
j .Lbuild_binary_expression_end
|
||||
j .Lcompile_binary_expression_end
|
||||
|
||||
.Lbuild_binary_expression_product:
|
||||
.Lcompile_binary_expression_product:
|
||||
mv s1, a0 # Skip *.
|
||||
li a0, 1
|
||||
call _build_expression
|
||||
@@ -211,9 +212,9 @@ _build_binary_expression:
|
||||
li a1, ASM_MUL_A0_A1_SIZE
|
||||
call _write_out
|
||||
|
||||
j .Lbuild_binary_expression_end
|
||||
j .Lcompile_binary_expression_end
|
||||
|
||||
.Lbuild_binary_expression_end:
|
||||
.Lcompile_binary_expression_end:
|
||||
# Epilogue.
|
||||
lw ra, 28(sp)
|
||||
lw s0, 24(sp)
|
||||
@@ -408,7 +409,7 @@ _build_expression:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 24
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
sw a0, 20(sp)
|
||||
|
||||
lw a0, 24(sp)
|
||||
@@ -462,7 +463,7 @@ _build_expression:
|
||||
|
||||
lw a0, 20(sp) # Skip @.
|
||||
addi a1, sp, 24
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
lw a0, 32(sp)
|
||||
@@ -479,7 +480,7 @@ _build_expression:
|
||||
.Lbuild_expression_call:
|
||||
lw a0, 20(sp)
|
||||
addi a1, sp, 8
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
lw a0, 32(sp)
|
||||
@@ -613,9 +614,9 @@ _compile_identifier:
|
||||
# Save the pointer to the identifier and its length on the stack.
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
lw t0, 0(sp)
|
||||
@@ -636,7 +637,7 @@ _compile_identifier:
|
||||
j .Lcompile_identifier_end
|
||||
|
||||
.Lcompile_identifier_assign:
|
||||
call _build_binary_expression
|
||||
call compile_binary_expression
|
||||
lw a0, 20(sp)
|
||||
lw a1, 16(sp)
|
||||
call _compile_designator_expression
|
||||
@@ -758,7 +759,7 @@ _compile_procedure_section:
|
||||
.Lcompile_procedure_section_loop:
|
||||
mv a0, s1
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
li t0, TOKEN_PROC
|
||||
lw t1, 4(sp)
|
||||
bne t0, t1, .Lcompile_procedure_section_end
|
||||
@@ -789,7 +790,7 @@ _compile_module_declaration:
|
||||
# Skip "program".
|
||||
mv a0, s1
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
# Epilogue.
|
||||
@@ -808,7 +809,7 @@ _compile_constant_section:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
li t0, TOKEN_CONST
|
||||
lw t1, 4(sp)
|
||||
bne t0, t1, .Lcompile_constant_section_end
|
||||
@@ -821,7 +822,7 @@ _compile_constant_section:
|
||||
.Lcompile_constant_section_item:
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
|
||||
lw t0, 12(sp)
|
||||
li t1, TOKEN_IDENTIFIER
|
||||
@@ -849,9 +850,9 @@ _compile_constant:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next # Skip the assignment sign.
|
||||
call lex_next # Skip the assignment sign.
|
||||
mv s1, a0
|
||||
# Write identifier the identifier.
|
||||
lw a0, 20(sp)
|
||||
@@ -869,7 +870,7 @@ _compile_constant:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
lw a0, 20(sp) # Save the literal pointer before advancing it.
|
||||
@@ -895,7 +896,7 @@ _compile_variable_section:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
li t0, TOKEN_VAR
|
||||
lw t1, 4(sp)
|
||||
bne t0, t1, .Lcompile_variable_section_end
|
||||
@@ -908,7 +909,7 @@ _compile_variable_section:
|
||||
.Lcompile_variable_section_item:
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
lw t0, 12(sp)
|
||||
li t1, TOKEN_IDENTIFIER
|
||||
|
||||
@@ -937,17 +938,17 @@ _compile_variable:
|
||||
# Save the identifier on the stack since it should emitted multiple times.
|
||||
mv a0, s1
|
||||
addi a1, sp, 28
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next # Skip the colon in front of the type.
|
||||
call lex_next # Skip the colon in front of the type.
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next # Skip the opening bracket.
|
||||
call lex_next # Skip the opening bracket.
|
||||
addi a1, sp, 16
|
||||
call _tokenize_next # Save the array size on the stack since it has to be emitted multiple times.
|
||||
call lex_next # Save the array size on the stack since it has to be emitted multiple times.
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next # Skip the closing bracket.
|
||||
call lex_next # Skip the closing bracket.
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next # Skip the type.
|
||||
call lex_next # Skip the type.
|
||||
mv s1, a0
|
||||
|
||||
# .type identifier, @object
|
||||
@@ -989,8 +990,9 @@ _compile_variable:
|
||||
addi sp, sp, 48
|
||||
ret
|
||||
|
||||
.type _compile_type_expression, @function
|
||||
_compile_type_expression:
|
||||
# Sets a0 to the type pointer.
|
||||
.type compile_type_expression, @function
|
||||
compile_type_expression:
|
||||
# Prologue.
|
||||
addi sp, sp, -32
|
||||
sw ra, 28(sp)
|
||||
@@ -1000,19 +1002,49 @@ _compile_type_expression:
|
||||
.Lcompile_type_expression_type:
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
lw t0, 12(sp)
|
||||
|
||||
# Skip the pointer designator and handle the rest of the type.
|
||||
li t1, TOKEN_HAT
|
||||
beq t0, t1, .Lcompile_type_expression_type
|
||||
li t1, TOKEN_HAT # Pointer type.
|
||||
beq t0, t1, .Lcompile_type_expression_pointer
|
||||
|
||||
# Named type.
|
||||
mv a0, zero
|
||||
lw a1, 16(sp)
|
||||
lw a2, 20(sp)
|
||||
call symbol_table_find
|
||||
|
||||
/* DEBUG
|
||||
sw a0, 4(sp)
|
||||
|
||||
lw a0, 4(a0)
|
||||
addi a0, a0, '0'
|
||||
sw a0, 8(sp)
|
||||
|
||||
/* DEBUG */
|
||||
lw a0, 20(sp)
|
||||
lw a1, 16(sp)
|
||||
call _write_error
|
||||
|
||||
addi a0, sp, 8
|
||||
li a1, 1
|
||||
call _write_error
|
||||
|
||||
lw a0, 4(sp) */
|
||||
/* DEBUG */
|
||||
|
||||
j .Lcompile_type_expression_end
|
||||
|
||||
.Lcompile_type_expression_pointer:
|
||||
call compile_type_expression
|
||||
mv a1, s3
|
||||
call symbol_table_make_pointer
|
||||
add s3, s3, a0
|
||||
sub a0, s3, a0
|
||||
|
||||
j .Lcompile_type_expression_end
|
||||
|
||||
.Lcompile_type_expression_end:
|
||||
# Epilogue.
|
||||
lw ra, 28(sp)
|
||||
lw s0, 24(sp)
|
||||
@@ -1029,12 +1061,12 @@ _compile_parameters:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0 # Skip the opening paren.
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
|
||||
lw t0, 12(sp)
|
||||
li t1, TOKEN_RIGHT_PAREN
|
||||
@@ -1045,21 +1077,21 @@ _compile_parameters:
|
||||
.Lcompile_parameters_parameter:
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0 # Skip the ":" in front of the type.
|
||||
|
||||
call _compile_type_expression
|
||||
call compile_type_expression
|
||||
# Read the comma between the parameters or a closing paren.
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
|
||||
lw t0, 0(sp)
|
||||
li t1, TOKEN_COMMA
|
||||
bne t0, t1, .Lcompile_parameters_end
|
||||
# If it is a comma, read the name of the next parameter.
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
j .Lcompile_parameters_parameter
|
||||
@@ -1083,9 +1115,9 @@ _compile_procedure:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next # Skip proc.
|
||||
call lex_next # Skip proc.
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
lw a0, 16(sp)
|
||||
@@ -1098,7 +1130,7 @@ _compile_procedure:
|
||||
.Lcompile_procedure_begin:
|
||||
mv a0, s1
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
lw t0, 4(sp)
|
||||
li t1, TOKEN_BEGIN
|
||||
@@ -1140,11 +1172,11 @@ _compile_goto:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next # Skip the goto keyword.
|
||||
call lex_next # Skip the goto keyword.
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next # We should be on dot the label is beginning with.
|
||||
call lex_next # We should be on dot the label is beginning with.
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next# Save the label name.
|
||||
call lex_next# Save the label name.
|
||||
mv s1, a0
|
||||
|
||||
li t0, 0x2e206a # j .
|
||||
@@ -1177,9 +1209,9 @@ _compile_label:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 8
|
||||
call _tokenize_next # Dot starting the label.
|
||||
call lex_next # Dot starting the label.
|
||||
addi a1, sp, 8
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0
|
||||
|
||||
li a0, '.'
|
||||
@@ -1209,10 +1241,10 @@ _compile_return:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 12
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0 # Skip return.
|
||||
|
||||
call _build_binary_expression
|
||||
call compile_binary_expression
|
||||
|
||||
# Epilogue.
|
||||
lw ra, 28(sp)
|
||||
@@ -1230,14 +1262,14 @@ _compile_if:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0 # Skip the if.
|
||||
|
||||
call _build_binary_expression
|
||||
call compile_binary_expression
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0 # Skip the then.
|
||||
|
||||
# Label prefix.
|
||||
@@ -1351,7 +1383,7 @@ _compile_statements:
|
||||
# Generate the body of the procedure.
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
lw t0, 0(sp)
|
||||
li t1, TOKEN_END
|
||||
|
||||
@@ -1362,7 +1394,7 @@ _compile_statements:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
lw t0, 0(sp)
|
||||
li t1, TOKEN_SEMICOLON
|
||||
|
||||
@@ -1389,7 +1421,7 @@ _compile_statement:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 0
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
lw t0, 0(sp)
|
||||
|
||||
li t1, TOKEN_IDENTIFIER
|
||||
@@ -1470,7 +1502,7 @@ _compile_entry_point:
|
||||
|
||||
mv a0, s1
|
||||
addi a1, sp, 4
|
||||
call _tokenize_next
|
||||
call lex_next
|
||||
mv s1, a0 # Skip begin.
|
||||
|
||||
# Generate the body of the procedure.
|
||||
@@ -1518,6 +1550,9 @@ _start:
|
||||
call _read_file
|
||||
|
||||
li s2, 1
|
||||
call _mmap
|
||||
mv s3, a0
|
||||
|
||||
call symbol_table_build
|
||||
call _compile
|
||||
|
||||
|
Reference in New Issue
Block a user