Implement binary logical expressions

This commit is contained in:
2025-09-03 11:16:19 +02:00
parent ed3e0e043c
commit 1750df51f6
2 changed files with 131 additions and 3 deletions

View File

@@ -10,7 +10,8 @@
# The first parameter is in 88, the second in 84 and so forth.
# - Unary negate operation, e.g. -5.
# - Unary locical not operation "~".
# - Binary addition "+".
# - Binary addition "+" and multiplication "*".
# - Binary logical operations: & (and), or and xor.
.section .rodata
@@ -86,6 +87,15 @@ asm_neg: .string "\tneg "
.type asm_not, @object
asm_not: .string "\tnot "
.type asm_and, @object
asm_and: .string "\tand "
.type asm_or, @object
asm_or: .string "\tor "
.type asm_xor, @object
asm_xor: .string "\txor "
.type asm_comma, @object
asm_comma: .string ", "
@@ -695,6 +705,15 @@ begin
li t1, '*'
beq t0, t1, .compile_expression_mul
li t1, '&'
beq t0, t1, .compile_expression_and
li t1, 'o'
beq t0, t1, .compile_expression_or
li t1, 'x'
beq t0, t1, .compile_expression_xor
# Unknown binary operator.
unimp
@@ -728,6 +747,51 @@ begin
goto .compile_expression_end;
.compile_expression_and:
_advance_token(1);
_compile_binary_rhs();
# Execute the operation.
_write_z(@asm_and);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 1);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_or:
_advance_token(1);
_compile_binary_rhs();
# Execute the operation.
_write_z(@asm_or);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 1);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_xor:
_advance_token(1);
_compile_binary_rhs();
# Execute the operation.
_write_z(@asm_xor);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 1);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_end:
end;
@@ -1229,6 +1293,6 @@ begin
_read_file(@source_code, 81920);
_compile();
_exit(2 * 3);
_exit(0);
end;