Support simple variable assignment
This commit is contained in:
229
boot/stage5.elna
229
boot/stage5.elna
@@ -2,14 +2,8 @@
|
||||
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
# obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
# Stage3 compiler.
|
||||
# Stage5 compiler.
|
||||
#
|
||||
# - Procedures without none or one argument.
|
||||
# - Goto statements.
|
||||
# - Character and integer literals.
|
||||
# - Passing local variables to procedures.
|
||||
# - Local variables should have the format: v00,
|
||||
# where 00 is its offset from the sp register.
|
||||
|
||||
.section .rodata
|
||||
|
||||
@@ -64,23 +58,26 @@ asm_li: .string "\tli "
|
||||
.type asm_lw, @object
|
||||
asm_lw: .string "\tlw "
|
||||
|
||||
.type asm_la, @object
|
||||
asm_la: .string "\tla "
|
||||
|
||||
.type asm_sw, @object
|
||||
asm_sw: .string "\tsw "
|
||||
|
||||
.type asm_mv, @object
|
||||
asm_mv: .string "mv "
|
||||
.type asm_addi, @object
|
||||
asm_addi: .string "\taddi "
|
||||
|
||||
.type asm_t0, @object
|
||||
asm_t0: .string "t0"
|
||||
|
||||
.type asm_a0, @object
|
||||
asm_a0: .string "a0"
|
||||
.type asm_t1, @object
|
||||
asm_t1: .string "t1"
|
||||
|
||||
.type asm_comma, @object
|
||||
asm_comma: .string ", "
|
||||
|
||||
.type asm_sp, @object
|
||||
asm_sp: .string "(sp)"
|
||||
asm_sp: .string "sp"
|
||||
|
||||
.section .bss
|
||||
|
||||
@@ -257,7 +254,7 @@ begin
|
||||
_is_upper();
|
||||
sw a0, 4(sp)
|
||||
|
||||
_is_lower(v00);
|
||||
_is_lower(v0);
|
||||
|
||||
lw t0, 0(sp)
|
||||
xori t1, t0, '_'
|
||||
@@ -289,10 +286,10 @@ proc _is_alnum();
|
||||
begin
|
||||
sw a0, 4(sp)
|
||||
|
||||
_is_alpha();
|
||||
_is_alpha(v4);
|
||||
sw a0, 0(sp)
|
||||
|
||||
_is_digit(v04);
|
||||
_is_digit(v4);
|
||||
|
||||
lw a1, 0(sp)
|
||||
or a0, a0, a1
|
||||
@@ -413,12 +410,7 @@ end;
|
||||
proc _write_token();
|
||||
begin
|
||||
sw a0, 0(sp)
|
||||
|
||||
la a0, source_code_position
|
||||
lw a0, (a0)
|
||||
lw a1, 0(sp)
|
||||
_write_s();
|
||||
|
||||
_write_s(source_code_position, v0);
|
||||
lw a0, 0(sp)
|
||||
end;
|
||||
|
||||
@@ -491,7 +483,7 @@ begin
|
||||
la a0, asm_li
|
||||
_write_z();
|
||||
|
||||
la a0, asm_a0
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
@@ -509,7 +501,7 @@ begin
|
||||
la a0, asm_li
|
||||
_write_z();
|
||||
|
||||
la a0, asm_a0
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
@@ -542,23 +534,22 @@ end;
|
||||
|
||||
proc _compile_variable_expression();
|
||||
begin
|
||||
_compile_designator();
|
||||
|
||||
la a0, asm_lw
|
||||
_write_z();
|
||||
|
||||
la a0, asm_a0
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
|
||||
_advance_token(1);
|
||||
_read_token();
|
||||
_write_token();
|
||||
_advance_token();
|
||||
|
||||
la a0, asm_sp
|
||||
_write_c('(');
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
_write_c(')');
|
||||
_write_c('\n');
|
||||
|
||||
end;
|
||||
@@ -568,17 +559,15 @@ begin
|
||||
la t0, source_code_position
|
||||
lw t0, (t0)
|
||||
lb a0, (t0)
|
||||
sw a0, 0(sp)
|
||||
|
||||
li t1, '\''
|
||||
beq a0, t1, .compile_expression_character_literal
|
||||
|
||||
li t1, 'v'
|
||||
beq a0, t1, .compile_expression_variable
|
||||
|
||||
_is_digit();
|
||||
_is_digit(v0);
|
||||
bnez a0, .compile_expression_integer_literal
|
||||
|
||||
goto .compile_expression_end;
|
||||
goto .compile_expression_variable;
|
||||
|
||||
.compile_expression_character_literal:
|
||||
_compile_character_literal();
|
||||
@@ -590,7 +579,7 @@ begin
|
||||
|
||||
.compile_expression_variable:
|
||||
_compile_variable_expression();
|
||||
goto .compile_expression_end;;
|
||||
goto .compile_expression_end;
|
||||
|
||||
.compile_expression_end:
|
||||
end;
|
||||
@@ -604,11 +593,8 @@ begin
|
||||
|
||||
_read_token();
|
||||
sw a0, 0(sp)
|
||||
la t0, source_code_position
|
||||
lw t0, (t0)
|
||||
sw t0, 4(sp)
|
||||
|
||||
sw zero, 8(sp)
|
||||
v4 := source_code_position;
|
||||
v8 := 0;
|
||||
|
||||
# Skip the identifier and left paren.
|
||||
addi a0, a0, 1
|
||||
@@ -628,7 +614,7 @@ begin
|
||||
la a0, asm_sw
|
||||
_write_z();
|
||||
|
||||
la a0, asm_a0
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
@@ -642,8 +628,10 @@ begin
|
||||
sub a0, t1, t0
|
||||
_write_i();
|
||||
|
||||
_write_c('(')
|
||||
la a0, asm_sp
|
||||
_write_z();
|
||||
_write_c(')')
|
||||
|
||||
_write_c('\n');
|
||||
|
||||
@@ -677,8 +665,7 @@ begin
|
||||
_write_z();
|
||||
|
||||
_write_c('a');
|
||||
lw a0, 8(sp)
|
||||
_write_i();
|
||||
_write_i(v8);
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
@@ -691,9 +678,11 @@ begin
|
||||
sub a0, t1, t0
|
||||
_write_i();
|
||||
|
||||
_write_c('(');
|
||||
la a0, asm_sp
|
||||
_write_z();
|
||||
|
||||
_write_c(')');
|
||||
_write_c('\n');
|
||||
|
||||
goto .compile_call_finalize;
|
||||
@@ -702,7 +691,7 @@ begin
|
||||
la a0, asm_call
|
||||
_write_z();
|
||||
|
||||
_write_s(v04, v00);
|
||||
_write_s(v4, v0);
|
||||
|
||||
# Skip the right paren.
|
||||
_advance_token(1);
|
||||
@@ -718,10 +707,142 @@ begin
|
||||
la a0, asm_j
|
||||
_write_z();
|
||||
|
||||
_write_token(v00);
|
||||
_write_token(v0);
|
||||
_advance_token();
|
||||
end;
|
||||
|
||||
proc _compile_local_designator();
|
||||
begin
|
||||
# Skip "v" in the local variable name.
|
||||
_advance_token(1);
|
||||
|
||||
la a0, asm_addi
|
||||
_write_z();
|
||||
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
|
||||
la a0, asm_sp
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
|
||||
# Read local variable stack offset and save it.
|
||||
v0 := source_code_position;
|
||||
|
||||
_read_token();
|
||||
sw a0, 4(sp)
|
||||
|
||||
_write_token();
|
||||
_advance_token();
|
||||
|
||||
_write_c('\n');
|
||||
|
||||
end;
|
||||
|
||||
proc _compile_global_designator();
|
||||
begin
|
||||
la a0, asm_la
|
||||
_write_z();
|
||||
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
|
||||
_read_token();
|
||||
_write_token();
|
||||
_advance_token();
|
||||
|
||||
_write_c('\n');
|
||||
|
||||
end;
|
||||
|
||||
proc _compile_designator();
|
||||
begin
|
||||
la t0, source_code_position
|
||||
lw t0, (t0)
|
||||
lb a0, (t0)
|
||||
|
||||
li t1, 'v'
|
||||
beq a0, t1, .compile_designator_local
|
||||
|
||||
goto .compile_designator_global;
|
||||
|
||||
.compile_designator_local:
|
||||
_compile_local_designator();
|
||||
goto .compile_designator_end;
|
||||
|
||||
.compile_designator_global:
|
||||
_compile_global_designator();
|
||||
goto .compile_designator_end;
|
||||
|
||||
.compile_designator_end:
|
||||
end;
|
||||
|
||||
proc _compile_assignment();
|
||||
begin
|
||||
_compile_designator();
|
||||
|
||||
# Save the assignee address on the stack.
|
||||
la a0, asm_sw
|
||||
_write_z();
|
||||
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
|
||||
_write_i(20);
|
||||
_write_c('(');
|
||||
la a0, asm_sp
|
||||
_write_z();
|
||||
_write_c(')');
|
||||
_write_c('\n');
|
||||
|
||||
# Skip the assignment sign (:=) with surrounding whitespaces.
|
||||
_advance_token(4);
|
||||
|
||||
# Compile the assignment.
|
||||
_compile_expression();
|
||||
|
||||
la a0, asm_lw
|
||||
_write_z();
|
||||
|
||||
la a0, asm_t1
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
|
||||
_write_i(20);
|
||||
_write_c('(');
|
||||
la a0, asm_sp
|
||||
_write_z();
|
||||
_write_c(')');
|
||||
_write_c('\n');
|
||||
|
||||
la a0, asm_sw
|
||||
_write_z();
|
||||
|
||||
la a0, asm_t0
|
||||
_write_z();
|
||||
|
||||
la a0, asm_comma
|
||||
_write_z();
|
||||
|
||||
_write_c('(');
|
||||
la a0, asm_t1
|
||||
_write_z();
|
||||
_write_c(')');
|
||||
end;
|
||||
|
||||
proc _compile_statement();
|
||||
begin
|
||||
# This is a call if the statement starts with an underscore.
|
||||
@@ -737,6 +858,9 @@ begin
|
||||
li t1, 'g'
|
||||
beq t0, t1, .compile_statement_goto
|
||||
|
||||
li t1, 'v'
|
||||
beq t0, t1, .compile_statement_assignment
|
||||
|
||||
_compile_line();
|
||||
goto .compile_statement_end;
|
||||
|
||||
@@ -752,6 +876,12 @@ begin
|
||||
|
||||
goto .compile_statement_semicolon;
|
||||
|
||||
.compile_statement_assignment:
|
||||
_advance_token(1);
|
||||
_compile_assignment();
|
||||
|
||||
goto .compile_statement_semicolon;
|
||||
|
||||
.compile_statement_semicolon:
|
||||
_advance_token(2);
|
||||
|
||||
@@ -789,13 +919,13 @@ begin
|
||||
la a0, asm_type_directive
|
||||
_write_z();
|
||||
|
||||
_write_token(v00);
|
||||
_write_token(v0);
|
||||
|
||||
la a0, asm_type_function
|
||||
_write_z();
|
||||
|
||||
# Write procedure label, _procedure_name:
|
||||
_write_token(v00);
|
||||
_write_token(v0);
|
||||
|
||||
la a0, asm_colon
|
||||
_write_z();
|
||||
@@ -834,9 +964,6 @@ begin
|
||||
|
||||
# Read the symbol type.
|
||||
_read_token();
|
||||
la t0, source_code_position
|
||||
lw t0, (t0)
|
||||
sw t0, 12(sp)
|
||||
|
||||
# Print the symbol type and newline.
|
||||
addi a0, a0, 1
|
||||
|
Reference in New Issue
Block a user