From 1750df51f69df6732b0f5a10140cf3b65b96f2c6 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 3 Sep 2025 11:16:19 +0200 Subject: [PATCH] Implement binary logical expressions --- boot/stage5.elna | 66 +++++++++++++++++++++++++++++++++++++++++++++- boot/stage6.elna | 68 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 131 insertions(+), 3 deletions(-) diff --git a/boot/stage5.elna b/boot/stage5.elna index 96ce2e8..c0cbc8b 100644 --- a/boot/stage5.elna +++ b/boot/stage5.elna @@ -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; diff --git a/boot/stage6.elna b/boot/stage6.elna index e9c2ac5..c0cbc8b 100644 --- a/boot/stage6.elna +++ b/boot/stage6.elna @@ -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;