diff --git a/src/tiny8/compiler.py b/src/tiny8/compiler.py index 214b6ff..7348c7f 100644 --- a/src/tiny8/compiler.py +++ b/src/tiny8/compiler.py @@ -1536,6 +1536,36 @@ def gen_expr(self, expr: ASTNode) -> None: else: raise SyntaxError(f"Invalid lvalue for {'++' if expr.is_inc else '--'}") + elif isinstance(expr, BinaryOp) and expr.op in ("&&", "||"): + # C requires short-circuit evaluation: the right operand must not + # be evaluated when the left operand already decides the result. + # Emit the right operand inside a branch so its code is skipped, + # not merely its value discarded. + lbl_end = self.new_label("logic_end") + self.gen_expr(expr.left) + self.emit("CPI R16, 0") + if expr.op == "&&": + lbl_short = self.new_label("and_false") + self.emit(f"BREQ {lbl_short}") + self.gen_expr(expr.right) + self.emit("CPI R16, 0") + self.emit(f"BREQ {lbl_short}") + self.emit("LDI R16, 1") + self.emit(f"JMP {lbl_end}") + self.emit_label(lbl_short) + self.emit("LDI R16, 0") + else: + lbl_short = self.new_label("or_true") + self.emit(f"BRNE {lbl_short}") + self.gen_expr(expr.right) + self.emit("CPI R16, 0") + self.emit(f"BRNE {lbl_short}") + self.emit("LDI R16, 0") + self.emit(f"JMP {lbl_end}") + self.emit_label(lbl_short) + self.emit("LDI R16, 1") + self.emit_label(lbl_end) + elif ( isinstance(expr, BinaryOp) and expr.op in ("<<", ">>") @@ -1604,30 +1634,6 @@ def gen_expr(self, expr: ASTNode) -> None: self.emit("DEC R17") self.emit(f"JMP {lbl_loop}") self.emit_label(lbl_end) - elif expr.op == "&&": - lbl_false = self.new_label("and_false") - lbl_end = self.new_label("and_end") - self.emit("CPI R16, 0") - self.emit(f"BREQ {lbl_false}") - self.emit("CPI R17, 0") - self.emit(f"BREQ {lbl_false}") - self.emit("LDI R16, 1") - self.emit(f"JMP {lbl_end}") - self.emit_label(lbl_false) - self.emit("LDI R16, 0") - self.emit_label(lbl_end) - elif expr.op == "||": - lbl_true = self.new_label("or_true") - lbl_end = self.new_label("or_end") - self.emit("CPI R16, 0") - self.emit(f"BRNE {lbl_true}") - self.emit("CPI R17, 0") - self.emit(f"BRNE {lbl_true}") - self.emit("LDI R16, 0") - self.emit(f"JMP {lbl_end}") - self.emit_label(lbl_true) - self.emit("LDI R16, 1") - self.emit_label(lbl_end) elif expr.op in ("==", "!=", "<", "<=", ">", ">="): lbl_true = self.new_label("cmp_true") lbl_end = self.new_label("cmp_end") diff --git a/tests/test_compiler.py b/tests/test_compiler.py index 1cfd513..264f34d 100644 --- a/tests/test_compiler.py +++ b/tests/test_compiler.py @@ -1800,3 +1800,88 @@ def test_compiler_standalone_vardecl_and_assign_without_frame(): asm_text = "\n".join(gen.asm) assert "ST R30, R16" in asm_text assert "LDI R31, 6" in asm_text + + +def test_short_circuit_and_skips_rhs_side_effect(): + """`&&` must not evaluate its right operand when the left one is zero.""" + c_code = """ + int main() { + int zero = 0; + int one = 1; + int taken = 0; + if (zero && poke(0x80, 7)) { + taken = 1; + } + if (one && poke(0x81, 8)) { + taken = taken + 2; + } + poke(0x82, taken); + return 0; + } + """ + cpu = CPU() + cpu.load_program(assemble(compile_c(c_code))) + cpu.run(max_steps=500, show_progress=False) + + # poke() returns the byte it wrote, so `one && poke(...)` is true and only + # the second branch runs. + assert cpu.read_ram(0x80) == 0 + assert cpu.read_ram(0x81) == 8 + assert cpu.read_ram(0x82) == 2 + + +def test_short_circuit_or_skips_rhs_side_effect(): + """`||` must not evaluate its right operand when the left one is non-zero.""" + c_code = """ + int main() { + int zero = 0; + int one = 1; + int taken = 0; + if (one || poke(0x80, 7)) { + taken = 1; + } + if (zero || poke(0x81, 8)) { + taken = taken + 2; + } + poke(0x82, taken); + return 0; + } + """ + cpu = CPU() + cpu.load_program(assemble(compile_c(c_code))) + cpu.run(max_steps=500, show_progress=False) + + assert cpu.read_ram(0x80) == 0 + assert cpu.read_ram(0x81) == 8 + assert cpu.read_ram(0x82) == 3 + + +def test_short_circuit_result_values(): + """Short-circuiting must keep the boolean 0/1 result of C logical operators.""" + c_code = """ + int main() { + int a = 5; + int b = 0; + int c = 7; + int land = (a > 1) && (c > 2); + int land_short = (b != 0) && (c > 2); + int lor = (b != 0) || (c > 2); + int lor_short = (a > 1) || (c > 2); + int nested = (a && (b || c)) && c; + poke(0x80, land); + poke(0x81, land_short); + poke(0x82, lor); + poke(0x83, lor_short); + poke(0x84, nested); + return 0; + } + """ + cpu = CPU() + cpu.load_program(assemble(compile_c(c_code))) + cpu.run(max_steps=1000, show_progress=False) + + assert cpu.read_ram(0x80) == 1 + assert cpu.read_ram(0x81) == 0 + assert cpu.read_ram(0x82) == 1 + assert cpu.read_ram(0x83) == 1 + assert cpu.read_ram(0x84) == 1