interpreter: lower && and || as short-circuit branches#73
Open
Franklin-Qi wants to merge 1 commit into
Open
Conversation
Lower logical AND/OR at codegen time using Branch and Jump instead of dedicated And/Or VM instructions. Results are coerced to integer truth values (0 or 1), matching gawk mkbool() behavior. Closes uutils#69
Alonely0
requested changes
Jul 10, 2026
Alonely0
left a comment
Collaborator
There was a problem hiding this comment.
Quite nice. The only thing I'd say is that the two helpers are unnecessarily complex (see the other review comment).
Comment on lines
+438
to
+459
| fn lower_and_into(&mut self, lhs: &Expr<'_>, rhs: &Expr<'_>, dest: Reg) { | ||
| let lhs_reg = self.alloc_reg(); | ||
| self.lower_expr_into(lhs, *lhs_reg); | ||
| let br = self.bc.emit(Instruction::br(*lhs_reg, Label(0))); | ||
| let false_label = self.following_instr(0); | ||
| self.bc.nth(br).set_label(false_label); | ||
| self.free_reg(lhs_reg); | ||
|
|
||
| self.bc | ||
| .emit(Instruction::Copy { dest, arg: Arg { imm: 0 }, ty: ArgTy::Imm }); | ||
| let jmp = self.bc.emit(Instruction::Jump { to: Label(0) }); | ||
|
|
||
| let true_label = self.following_instr(0); | ||
| self.bc.nth(br).set_then_label(true_label); | ||
| let rhs_reg = self.alloc_reg(); | ||
| self.lower_expr_into(rhs, *rhs_reg); | ||
| self.truthify(*rhs_reg, dest); | ||
| self.free_reg(rhs_reg); | ||
|
|
||
| let end = self.following_instr(0); | ||
| self.bc.nth(jmp).set_label(end); | ||
| } |
Collaborator
There was a problem hiding this comment.
Pretty sure we can use the helpers like emit_branch, emit_jump to make this shorter and more robust, right?
Something like:
fn lower_and_into(&mut self, lhs: &Expr<'_>, rhs: &Expr<'_>, dest: Reg) {
let (if_label, _) = self.emit_branch(lhs, |this| {
let rhs_reg = this.alloc_reg();
this.lower_expr_into(rhs, *rhs_reg);
this.truthify(*rhs_reg, dest);
this.free_reg(rhs_reg);
});
self.bc.nth(if_label).push_end_label();
self.emit_jump(|this| {
this.bc
.emit(Instruction::Copy { dest, arg: Arg { imm: 0 }, ty: ArgTy::Imm });
});
}| self.bc.nth(jmp).set_label(end); | ||
| } | ||
|
|
||
| fn lower_or_into(&mut self, lhs: &Expr<'_>, rhs: &Expr<'_>, dest: Reg) { |
Comment on lines
+487
to
+490
| self.bc | ||
| .emit(Instruction::Negation { dest: *tmp, arg: Arg { reg: src }, ty: ArgTy::Reg }); | ||
| self.bc | ||
| .emit(Instruction::Negation { dest, arg: Arg { reg: *tmp }, ty: ArgTy::Reg }); |
Collaborator
There was a problem hiding this comment.
As a former pythonist, this amuses me to no end. IYKYK.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lower logical AND/OR at codegen time using Branch and Jump instead of dedicated And/Or VM instructions. Results are coerced to integer truth values (0 or 1), matching gawk mkbool() behavior.
Closes #69