Support procedure without _ in the first stage
This commit is contained in:
220
boot/stage1.s
220
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
|
||||
|
||||
.Lcompile_designator_expression_local:
|
||||
li t0, 0x202c30 # 0,_
|
||||
sw t0, 12(sp)
|
||||
li t0, 0x61207773 # sw a
|
||||
sw t0, 8(sp)
|
||||
li a0, 7
|
||||
addi a1, sp, 8
|
||||
call _write_s
|
||||
call _write_error */
|
||||
|
||||
.Lcompile_designator_expression_by_address:
|
||||
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
|
||||
call symbol_table_lookup
|
||||
sw a0, 12(sp)
|
||||
|
||||
li t0, '\n'
|
||||
sw t0, 12(sp)
|
||||
li t0, 0x29707328 # (sp)
|
||||
li t0, 0x202c30 # 0,_
|
||||
sw t0, 8(sp)
|
||||
li a0, 5
|
||||
addi a1, sp, 8
|
||||
li t0, 0x61207773 # sw a
|
||||
sw t0, 4(sp)
|
||||
li a0, 7
|
||||
addi a1, sp, 4
|
||||
call _write_s
|
||||
|
||||
j .Lcompile_designator_expression_end
|
||||
lw a0, 12(sp)
|
||||
lw a0, 8(a0)
|
||||
call _write_i
|
||||
|
||||
li a0, '('
|
||||
call _write_c
|
||||
|
||||
lw a0, 12(sp)
|
||||
addi a1, sp, 4
|
||||
call take_address
|
||||
addi a1, sp, 4
|
||||
call _write_s
|
||||
|
||||
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
|
||||
|
Reference in New Issue
Block a user