Skip to content

Commit 8a28fe6

Browse files
committed
fixed bug where if statements required a terminator instruction
1 parent 5f3b51b commit 8a28fe6

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

wellick/src/compiler/translate.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,27 @@ impl<'a, 'b> FunctionTranslator<'a, 'b> {
4848
self.builder.switch_to_block(then_block);
4949
self.builder.seal_block(then_block);
5050

51-
let mut then_return: Option<Value> = None;
52-
for expr in if_body {
53-
match expr {
51+
for stmt in if_body {
52+
match stmt {
5453
// If a terminator expression, like return, is encountered, we don't
5554
// need to process the rest of the instructions since they're automatically
5655
// unreachable code.
57-
ast::Stmt::Return(stmt) => {
58-
then_return = Some(self.translate_return(stmt));
59-
break;
56+
ast::Stmt::Return(_) => {
57+
self.translate_stmt(&stmt);
58+
self.builder.switch_to_block(merge_block);
59+
self.builder.seal_block(merge_block);
60+
return self.builder.ins().iconst(types::I32, 0);
61+
}
62+
_ => {
63+
self.translate_stmt(&stmt);
6064
}
61-
stmt => then_return = Some(self.translate_stmt(stmt)),
6265
}
6366
}
64-
if then_return.is_none() {
65-
panic!("If statement has no body");
66-
}
6767

68+
self.builder.ins().jump(merge_block, &[]);
6869
self.builder.switch_to_block(merge_block);
69-
then_return.unwrap()
70+
self.builder.seal_block(merge_block);
71+
self.builder.ins().iconst(types::I32, 0)
7072
}
7173

7274
pub fn translate_stmt(&mut self, stmt: &ast::Stmt) -> Value {
@@ -163,7 +165,7 @@ impl<'a, 'b> FunctionTranslator<'a, 'b> {
163165

164166
fn translate_call(&mut self, expr: &ast::Call) -> Value {
165167
let expected_sig = match expr.func.as_str() {
166-
"iadd" | "isub" | "idiv" | "imul" | "ieq" | "ilteq" | "ilt" => Signature {
168+
"iadd" | "isub" | "idiv" | "imul" | "ieq" | "ilteq" | "ilt" | "imod" => Signature {
167169
params: vec![AbiParam::new(types::I32), AbiParam::new(types::I32)],
168170
returns: vec![AbiParam::new(types::I32)],
169171
call_conv: self.module.isa().default_call_conv(),

0 commit comments

Comments
 (0)