From 563092e4f35c28f0cf84725ea76f5538f57efd13 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Sun, 12 Dec 2021 10:34:19 -0800 Subject: [PATCH] WIP. --- src/frontend.rs | 22 ++++++++++++++++------ src/ir.rs | 10 +++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/frontend.rs b/src/frontend.rs index 17cb7b4..56def17 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -559,18 +559,26 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { wasmparser::Operator::LocalGet { local_index } => { let ty = self.body.locals[*local_index as usize]; - let value = self.locals.get(&mut self.body, *local_index); - self.op_stack.push((ty, value)); + if self.cur_block.is_some() { + let value = self.locals.get(&mut self.body, *local_index); + self.op_stack.push((ty, value)); + } else { + self.op_stack.push((ty, Value::undef())); + } } wasmparser::Operator::LocalSet { local_index } => { let (_, value) = self.op_stack.pop().unwrap(); - self.locals.set(*local_index, value); + if self.cur_block.is_some() { + self.locals.set(*local_index, value); + } } wasmparser::Operator::LocalTee { local_index } => { let (_ty, value) = *self.op_stack.last().unwrap(); - self.locals.set(*local_index, value); + if self.cur_block.is_some() { + self.locals.set(*local_index, value); + } } wasmparser::Operator::Call { .. } @@ -978,9 +986,11 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { .map(|(_ty, value)| *value) .collect::>(); self.emit_cond_branch(cond, frame.br_target(), &args[..], cont, &[]); - self.cur_block = Some(cont); self.locals.seal_block_preds(cont, &mut self.body); - self.locals.start_block(cont); + if self.cur_block.is_some() { + self.cur_block = Some(cont); + self.locals.start_block(cont); + } } } } diff --git a/src/ir.rs b/src/ir.rs index 29a6346..49007d7 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -87,6 +87,7 @@ impl FunctionBody { } pub fn set_alias(&mut self, value: Value, to: Value) { + assert_ne!(to, Value::undef()); log::trace!("set_alias: value {:?} to {:?}", value, to); // Resolve the `to` value through all existing aliases. let to = self.resolve_alias(to); @@ -116,8 +117,15 @@ impl FunctionBody { value } - pub fn add_blockparam(&mut self, block: BlockId, ty: Type) { + pub fn add_blockparam(&mut self, block: BlockId, ty: Type) -> usize { + let index = self.blocks[block].params.len(); self.blocks[block].params.push(ty); + index + } + + pub fn add_blockparam_value(&mut self, block: BlockId, ty: Type) -> Value { + let index = self.add_blockparam(block, ty); + self.add_value(ValueDef::BlockParam(block, index), Some(ty)) } pub fn add_placeholder(&mut self, ty: Type) -> Value {