From e53d801107eeebeeabffb27f51586ab8b5e61019 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Thu, 10 Nov 2022 00:29:56 -0800 Subject: [PATCH] WIP. --- src/frontend.rs | 6 +++--- src/ir/func.rs | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/frontend.rs b/src/frontend.rs index f29b1cf..356db26 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -452,7 +452,7 @@ impl LocalTracker { )), _ => todo!("unsupported type: {:?}", ty), }; - body.blocks[at_block].insts.push(val); + body.append_to_block(at_block, val); log::trace!( "created default value {} of type {} at block {}", val, @@ -1342,7 +1342,7 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { log::trace!(" -> value: {:?}", value); if let Some(block) = self.cur_block { - self.body.blocks[block].insts.push(value); + self.body.append_to_block(block, value); } if n_outputs == 1 { @@ -1354,7 +1354,7 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { .body .add_value(ValueDef::PickOutput(value, i, output_ty)); if let Some(block) = self.cur_block { - self.body.blocks[block].insts.push(pick); + self.body.append_to_block(block, pick); } self.op_stack.push((output_ty, pick)); log::trace!(" -> pick {}: {:?} ty {:?}", i, pick, output_ty); diff --git a/src/ir/func.rs b/src/ir/func.rs index b17e4e7..e88bee6 100644 --- a/src/ir/func.rs +++ b/src/ir/func.rs @@ -1,5 +1,5 @@ use super::{Block, FunctionBodyDisplay, Local, Module, Signature, Type, Value, ValueDef}; -use crate::entity::EntityVec; +use crate::entity::{EntityVec, PerEntity}; #[derive(Clone, Debug)] pub enum FuncDecl { @@ -45,6 +45,8 @@ pub struct FunctionBody { pub blocks: EntityVec, /// Value definitions, indexed by `Value`. pub values: EntityVec, + /// Blocks in which values are computed. Each may be `Block::invalid()` if not placed. + pub value_blocks: PerEntity, } impl FunctionBody { @@ -55,9 +57,11 @@ impl FunctionBody { let mut blocks = EntityVec::default(); let entry = blocks.push(BlockDef::default()); let mut values = EntityVec::default(); + let mut value_blocks = PerEntity::default(); for (i, &arg_ty) in locals.values().enumerate() { let value = values.push(ValueDef::BlockParam(entry, i, arg_ty)); blocks[entry].params.push((arg_ty, value)); + value_blocks[value] = entry; } FunctionBody { n_params, @@ -66,6 +70,7 @@ impl FunctionBody { entry, blocks, values, + value_blocks, } } @@ -119,6 +124,7 @@ impl FunctionBody { let index = self.blocks[block].params.len(); let value = self.add_value(ValueDef::BlockParam(block, index, ty)); self.blocks[block].params.push((ty, value)); + self.value_blocks[value] = block; value } @@ -149,6 +155,7 @@ impl FunctionBody { pub fn append_to_block(&mut self, block: Block, value: Value) { self.blocks[block].insts.push(value); + self.value_blocks[value] = block; } pub fn end_block(&mut self, block: Block, terminator: Terminator) {