Implement binary logical expressions

This commit is contained in:
2025-09-03 11:16:19 +02:00
parent ed3e0e043c
commit 4b42c59649
2 changed files with 443 additions and 5 deletions

View File

@@ -10,7 +10,9 @@
# The first parameter is in 88, the second in 84 and so forth. # The first parameter is in 88, the second in 84 and so forth.
# - Unary negate operation, e.g. -5. # - Unary negate operation, e.g. -5.
# - Unary locical not operation "~". # - Unary locical not operation "~".
# - Binary addition "+". # - Binary addition "+" and multiplication "*".
# - Binary logical operations: & (and), or and xor.
# - Binary comparison operations: =, <, <=, >, >=, <>.
.section .rodata .section .rodata
@@ -86,6 +88,27 @@ asm_neg: .string "\tneg "
.type asm_not, @object .type asm_not, @object
asm_not: .string "\tnot " 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_sub, @object
asm_sub: .string "\tsub "
.type asm_seqz, @object
asm_seqz: .string "\tseqz "
.type asm_snez, @object
asm_snez: .string "\tsnez "
.type asm_slt, @object
asm_slt: .string "\tslt "
.type asm_comma, @object .type asm_comma, @object
asm_comma: .string ", " asm_comma: .string ", "
@@ -683,7 +706,7 @@ begin
_write_c(')'); _write_c(')');
_write_c('\n'); _write_c('\n');
# Skip the operator and surrounding whitespaces. # Skip surrounding whitespace in front of the operator.
_advance_token(1); _advance_token(1);
la t0, source_code_position la t0, source_code_position
lw t0, (t0) lw t0, (t0)
@@ -695,6 +718,24 @@ begin
li t1, '*' li t1, '*'
beq t0, t1, .compile_expression_mul 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
li t1, '='
beq t0, t1, .compile_expression_equals
li t1, '<'
beq t0, t1, .compile_expression_less
li t1, '>'
beq t0, t1, .compile_expression_greater
# Unknown binary operator. # Unknown binary operator.
unimp unimp
@@ -728,6 +769,184 @@ begin
goto .compile_expression_end; 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(2);
_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(3);
_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_equals:
_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');
_write_z(@asm_seqz);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_less:
_advance_token(1);
la t0, source_code_position
lw t0, (t0)
lb t0, (t0)
li t1, '>'
beq t0, t1, .compile_expression_not_equal
li t1, '='
beq t0, t1, .compile_expression_less_equal
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
goto .compile_expression_end;
.compile_expression_not_equal:
_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');
_write_z(@asm_snez);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_less_equal:
_advance_token(1);
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
# Execute the operation.
_write_z(@asm_not);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_greater:
_advance_token(1);
la t0, source_code_position
lw t0, (t0)
lb t0, (t0)
li t1, '='
beq t0, t1, .compile_expression_greater_equal
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
goto .compile_expression_end;
.compile_expression_greater_equal:
_advance_token(1);
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
# Execute the operation.
_write_z(@asm_not);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_end: .compile_expression_end:
end; end;

View File

@@ -10,7 +10,9 @@
# The first parameter is in 88, the second in 84 and so forth. # The first parameter is in 88, the second in 84 and so forth.
# - Unary negate operation, e.g. -5. # - Unary negate operation, e.g. -5.
# - Unary locical not operation "~". # - Unary locical not operation "~".
# - Binary addition "+". # - Binary addition "+" and multiplication "*".
# - Binary logical operations: & (and), or and xor.
# - Binary comparison operations: =, <, <=, >, >=, <>.
.section .rodata .section .rodata
@@ -86,6 +88,27 @@ asm_neg: .string "\tneg "
.type asm_not, @object .type asm_not, @object
asm_not: .string "\tnot " 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_sub, @object
asm_sub: .string "\tsub "
.type asm_seqz, @object
asm_seqz: .string "\tseqz "
.type asm_snez, @object
asm_snez: .string "\tsnez "
.type asm_slt, @object
asm_slt: .string "\tslt "
.type asm_comma, @object .type asm_comma, @object
asm_comma: .string ", " asm_comma: .string ", "
@@ -683,7 +706,7 @@ begin
_write_c(')'); _write_c(')');
_write_c('\n'); _write_c('\n');
# Skip the operator and surrounding whitespaces. # Skip surrounding whitespace in front of the operator.
_advance_token(1); _advance_token(1);
la t0, source_code_position la t0, source_code_position
lw t0, (t0) lw t0, (t0)
@@ -695,6 +718,24 @@ begin
li t1, '*' li t1, '*'
beq t0, t1, .compile_expression_mul 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
li t1, '='
beq t0, t1, .compile_expression_equals
li t1, '<'
beq t0, t1, .compile_expression_less
li t1, '>'
beq t0, t1, .compile_expression_greater
# Unknown binary operator. # Unknown binary operator.
unimp unimp
@@ -728,6 +769,184 @@ begin
goto .compile_expression_end; 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(2);
_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(3);
_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_equals:
_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');
_write_z(@asm_seqz);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_less:
_advance_token(1);
la t0, source_code_position
lw t0, (t0)
lb t0, (t0)
li t1, '>'
beq t0, t1, .compile_expression_not_equal
li t1, '='
beq t0, t1, .compile_expression_less_equal
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
goto .compile_expression_end;
.compile_expression_not_equal:
_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');
_write_z(@asm_snez);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_less_equal:
_advance_token(1);
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
# Execute the operation.
_write_z(@asm_not);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_greater:
_advance_token(1);
la t0, source_code_position
lw t0, (t0)
lb t0, (t0)
li t1, '='
beq t0, t1, .compile_expression_greater_equal
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
goto .compile_expression_end;
.compile_expression_greater_equal:
_advance_token(1);
_compile_binary_rhs();
# Execute the operation.
_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_c('\n');
# Execute the operation.
_write_z(@asm_not);
_write_register('t', 0);
_write_z(@asm_comma);
_write_register('t', 0);
_write_c('\n');
goto .compile_expression_end;
.compile_expression_end: .compile_expression_end:
end; end;
@@ -1229,6 +1448,6 @@ begin
_read_file(@source_code, 81920); _read_file(@source_code, 81920);
_compile(); _compile();
_exit(2 * 3); _exit(0);
end; end;