Fix BrIf reachability and simplify block adds

This commit is contained in:
Rerumu 2022-06-12 04:02:46 -04:00
parent c4c0bf8deb
commit a379fb2e91

View File

@ -382,7 +382,7 @@ impl<'a> Builder<'a> {
} }
} }
fn start_block(&mut self, typ: BlockType) { fn start_block(&mut self, typ: BlockType, stat: Statement) {
let (num_param, num_result) = match typ { let (num_param, num_result) = match typ {
BlockType::NoResult => (0, 0), BlockType::NoResult => (0, 0),
BlockType::Value(_) => (0, 1), BlockType::Value(_) => (0, 1),
@ -397,6 +397,7 @@ impl<'a> Builder<'a> {
let mut old = std::mem::take(&mut self.target); let mut old = std::mem::take(&mut self.target);
old.leak_all(); old.leak_all();
old.code.push(stat);
self.target.stack = old.pop_len(num_param); self.target.stack = old.pop_len(num_param);
self.target.num_result = num_result; self.target.num_result = num_result;
@ -540,26 +541,23 @@ impl<'a> Builder<'a> {
} }
Inst::Nop => {} Inst::Nop => {}
Inst::Block(typ) => { Inst::Block(typ) => {
let data = Statement::Forward(Forward::default()); let stat = Statement::Forward(Forward::default());
self.start_block(typ); self.start_block(typ, stat);
self.pending.last_mut().unwrap().code.push(data);
} }
Inst::Loop(typ) => { Inst::Loop(typ) => {
let data = Statement::Backward(Backward::default()); let stat = Statement::Backward(Backward::default());
self.start_block(typ); self.start_block(typ, stat);
self.pending.last_mut().unwrap().code.push(data);
} }
Inst::If(typ) => { Inst::If(typ) => {
let data = Statement::If(If { let stat = Statement::If(If {
cond: self.target.pop_required(), cond: self.target.pop_required(),
truthy: Forward::default(), truthy: Forward::default(),
falsey: None, falsey: None,
}); });
self.start_block(typ); self.start_block(typ, stat);
self.pending.last_mut().unwrap().code.push(data);
} }
Inst::Else => { Inst::Else => {
self.set_return_data(0); self.set_return_data(0);
@ -576,16 +574,17 @@ impl<'a> Builder<'a> {
} }
Inst::BrIf(v) => { Inst::BrIf(v) => {
let target: usize = v.try_into().unwrap(); let target: usize = v.try_into().unwrap();
let data = Statement::If(If { let stat = Statement::If(If {
cond: self.target.pop_required(), cond: self.target.pop_required(),
truthy: Forward::default(), truthy: Forward::default(),
falsey: None, falsey: None,
}); });
self.start_block(BlockType::NoResult); self.start_block(BlockType::NoResult, stat);
self.pending.last_mut().unwrap().code.push(data);
self.set_br_to_block(target + 1); self.set_br_to_block(target + 1);
self.end_block(); self.end_block();
self.nested_unreachable -= 1;
} }
Inst::BrTable(ref v) => { Inst::BrTable(ref v) => {
self.nested_unreachable += 1; self.nested_unreachable += 1;