Implement string literals

This commit is contained in:
2025-09-04 22:07:01 +02:00
parent 4b42c59649
commit 4888252274
2 changed files with 132 additions and 132 deletions

View File

@@ -2,7 +2,7 @@
# 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/.
# Stage4 compiler.
# Stage5 compiler.
#
# - Stack size increased to 128 bytes per procedure.
# 7 words on the stack, 92 - 120, are reversed for procedure arguments (caller side).
@@ -13,6 +13,7 @@
# - Binary addition "+" and multiplication "*".
# - Binary logical operations: & (and), or and xor.
# - Binary comparison operations: =, <, <=, >, >=, <>.
# - Return statement.
.section .rodata
@@ -23,7 +24,7 @@ keyword_section: .ascii ".section"
keyword_type: .ascii ".type"
.type keyword_ret, @object
keyword_ret: .ascii "ret"
keyword_ret: .ascii "\tret"
.type keyword_global, @object
keyword_global: .ascii ".globl"
@@ -97,6 +98,9 @@ asm_or: .string "\tor "
.type asm_xor, @object
asm_xor: .string "\txor "
.type asm_xori, @object
asm_xori: .string "\txori "
.type asm_sub, @object
asm_sub: .string "\tsub "
@@ -109,6 +113,9 @@ asm_snez: .string "\tsnez "
.type asm_slt, @object
asm_slt: .string "\tslt "
.type asm_mv, @object
asm_mv: .string "\tmv "
.type asm_comma, @object
asm_comma: .string ", "
@@ -889,16 +896,18 @@ begin
_write_z(@asm_slt);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 1);
_write_z(@asm_comma);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 1);
_write_c('\n');
# Execute the operation.
_write_z(@asm_not);
_write_z(@asm_xori);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_z(@asm_comma);
_write_i(1);
_write_c('\n');
goto .compile_expression_end;
@@ -933,16 +942,18 @@ begin
_write_z(@asm_slt);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 1);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
# Execute the operation.
_write_z(@asm_not);
_write_z(@asm_xori);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_z(@asm_comma);
_write_i(1);
_write_c('\n');
goto .compile_expression_end;
@@ -1167,6 +1178,19 @@ begin
_write_c(')');
end;
proc _compile_return_statement();
begin
# Skip "return" keyword and whitespace after it.
_advance_token(7);
_compile_expression();
_write_z(@asm_mv);
_write_register('a', 0);
_write_z(@asm_comma);
_write_register('t', 0);
end;
proc _compile_statement();
begin
# This is a call if the statement starts with an underscore.
@@ -1185,6 +1209,10 @@ begin
li t1, 'v'
beq t0, t1, .compile_statement_assignment
# keyword_ret contains "\tret", so it's 4 bytes long.
_memcmp(source_code_position, @keyword_ret, 4);
beqz a0, .compile_statement_return
_compile_line();
goto .compile_statement_end;
@@ -1206,9 +1234,15 @@ begin
goto .compile_statement_semicolon;
.compile_statement_return:
_advance_token(1);
_compile_return_statement();
_write_c('\n');
goto .compile_statement_end;
.compile_statement_semicolon:
_advance_token(2);
_write_c('\n');
.compile_statement_end: