From b9c59fb5c0da643a401017adb95f60acb3376d8a Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Sun, 19 Dec 2021 14:47:40 -0800 Subject: [PATCH] WIP. --- src/cfg/serialize.rs | 22 +++++++++++++++++----- src/cfg/structured.rs | 14 ++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/cfg/serialize.rs b/src/cfg/serialize.rs index 4adb44d..0232d2a 100644 --- a/src/cfg/serialize.rs +++ b/src/cfg/serialize.rs @@ -24,11 +24,11 @@ pub enum SerializedBlockTarget { pub enum SerializedOperator { StartBlock { header: BlockId, - params: Vec<(Value, wasmparser::Type)>, + params: Vec<(wasmparser::Type, Value)>, }, StartLoop { header: BlockId, - param: Vec<(Value, wasmparser::Type)>, + param: Vec<(wasmparser::Type, Value)>, }, Br(SerializedBlockTarget), BrIf { @@ -61,10 +61,22 @@ impl SerializedBody { operators: &mut Vec, ) { match entry { - &BlockOrderEntry::StartBlock(header, ref param_tys) => { - todo!() + &BlockOrderEntry::StartBlock(header, ref params) => { + operators.push(SerializedOperator::StartBlock { + header, + params: params.clone(), + }); } - _ => { + &BlockOrderEntry::StartLoop(header, ref params) => { + operators.push(SerializedOperator::StartBlock { + header, + params: params.clone(), + }); + } + &BlockOrderEntry::End => { + operators.push(SerializedOperator::End); + } + &BlockOrderEntry::BasicBlock(block, ref targets) => { todo!() } } diff --git a/src/cfg/structured.rs b/src/cfg/structured.rs index 3a2ad34..87c92f7 100644 --- a/src/cfg/structured.rs +++ b/src/cfg/structured.rs @@ -295,6 +295,7 @@ pub struct BlockOrderTarget { pub target: BlockId, /// `None` means fallthrough. pub relative_branch: Option, + pub args: Vec, } impl BlockOrder { @@ -360,27 +361,28 @@ impl BlockOrder { &WasmRegion::Leaf(block) => { let mut targets = vec![]; - for &succ in &cfg.block_succs[block] { + f.blocks[block].terminator.visit_targets(|target| { log::trace!( "BlockOrder::generate_region: looking for succ {} in stack {:?} fallthrough {:?}", - succ, + target.block, target_stack, fallthrough, ); - let relative_branch = if Some(succ) == fallthrough { + let relative_branch = if Some(target.block) == fallthrough { None } else { let pos = target_stack .iter() - .position(|entry| *entry == succ) + .position(|entry| *entry == target.block) .expect("Malformed Wasm structured control flow"); Some(target_stack.len() - 1 - pos) }; targets.push(BlockOrderTarget { - target: succ, + target: target.block, relative_branch, + args: target.args.clone(), }); - } + }); entries.push(BlockOrderEntry::BasicBlock(block, targets)); } }