From 0947940c43c1d08caec50e92e9aa33a45762501f Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Sun, 12 Dec 2021 20:11:28 -0800 Subject: [PATCH] Refactor --- src/backend/locations.rs | 9 ++++++++ src/backend/mod.rs | 2 ++ src/frontend.rs | 50 +++++++++++++++++++--------------------- src/ir.rs | 2 +- 4 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 src/backend/locations.rs diff --git a/src/backend/locations.rs b/src/backend/locations.rs new file mode 100644 index 0000000..2098cbb --- /dev/null +++ b/src/backend/locations.rs @@ -0,0 +1,9 @@ +//! Location assignment (pseudo-regalloc) for SSA values onto +//! locals/operand-stack values. + +use crate::LocalId; + +pub enum Location { + Local(LocalId), + // TODO: use operand stack +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 3b2699b..9cbdf46 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -2,3 +2,5 @@ mod stackify; pub(crate) use stackify::*; +mod locations; +pub(crate) use locations::*; diff --git a/src/frontend.rs b/src/frontend.rs index 8a69dc3..8a2ea5f 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -554,7 +554,7 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { match &op { wasmparser::Operator::Unreachable => { if let Some(block) = self.cur_block { - self.body.blocks[block].terminator = Terminator::None; + self.body.end_block(block, Terminator::None); self.locals.finish_block(); } self.cur_block = None; @@ -1033,12 +1033,11 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { ); if let Some(block) = self.cur_block { let args = args.to_vec(); - self.body.add_edge(block, target); let target = BlockTarget { block: target, args, }; - self.body.blocks[block].terminator = Terminator::Br { target }; + self.body.end_block(block, Terminator::Br { target }); self.cur_block = None; self.locals.finish_block(); } @@ -1063,19 +1062,20 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { if let Some(block) = self.cur_block { let if_true_args = if_true_args.to_vec(); let if_false_args = if_false_args.to_vec(); - self.body.blocks[block].terminator = Terminator::CondBr { - cond, - if_true: BlockTarget { - block: if_true, - args: if_true_args, + self.body.end_block( + block, + Terminator::CondBr { + cond, + if_true: BlockTarget { + block: if_true, + args: if_true_args, + }, + if_false: BlockTarget { + block: if_false, + args: if_false_args, + }, }, - if_false: BlockTarget { - block: if_false, - args: if_false_args, - }, - }; - self.body.add_edge(block, if_true); - self.body.add_edge(block, if_false); + ); self.cur_block = None; self.locals.finish_block(); } @@ -1112,16 +1112,14 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { args: default_args, }; - for &target in indexed_targets { - self.body.add_edge(block, target); - } - self.body.add_edge(block, default_target); - - self.body.blocks[block].terminator = Terminator::Select { - value: index, - targets, - default, - }; + self.body.end_block( + block, + Terminator::Select { + value: index, + targets, + default, + }, + ); self.cur_block = None; self.locals.finish_block(); } @@ -1130,7 +1128,7 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { fn emit_ret(&mut self, values: &[Value]) { if let Some(block) = self.cur_block { let values = values.to_vec(); - self.body.blocks[block].terminator = Terminator::Return { values }; + self.body.end_block(block, Terminator::Return { values }); } } diff --git a/src/ir.rs b/src/ir.rs index 49007d7..138c508 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -90,7 +90,7 @@ impl FunctionBody { 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); + let to = self.resolve_and_update_alias(to); // Disallow cycles. if to == value { panic!("Cannot create an alias cycle");