This commit is contained in:
Chris Fallin 2022-11-28 21:55:58 -08:00
parent 04ecdb16bd
commit 539af66b88
3 changed files with 26 additions and 6 deletions

View file

@ -1,11 +1,13 @@
//! Backend: IR to Wasm. //! Backend: IR to Wasm.
use crate::cfg::CFGInfo;
use crate::entity::{EntityRef, PerEntity}; use crate::entity::{EntityRef, PerEntity};
use crate::ir::{Block, FunctionBody, Value, ValueDef}; use crate::ir::{Block, FunctionBody, Value, ValueDef};
use crate::passes::rpo::{RPOIndex, RPO}; use crate::passes::rpo::{RPOIndex, RPO};
use anyhow::Result; use anyhow::Result;
pub mod stackify; pub mod stackify;
use stackify::{Context as StackifyContext, WasmBlock};
pub mod treeify; pub mod treeify;
use treeify::Trees; use treeify::Trees;
@ -13,13 +15,24 @@ pub struct WasmBackend<'a> {
body: &'a FunctionBody, body: &'a FunctionBody,
rpo: RPO, rpo: RPO,
trees: Trees, trees: Trees,
ctrl: Vec<WasmBlock<'a>>,
} }
impl<'a> WasmBackend<'a> { impl<'a> WasmBackend<'a> {
pub fn new(body: &'a FunctionBody) -> Result<WasmBackend<'a>> { pub fn new(body: &'a FunctionBody) -> Result<WasmBackend<'a>> {
let cfg = CFGInfo::new(body);
let rpo = RPO::compute(body); let rpo = RPO::compute(body);
log::trace!("RPO:\n{:?}\n", rpo);
let trees = Trees::compute(body); let trees = Trees::compute(body);
Ok(WasmBackend { body, rpo, trees }) log::trace!("Trees:\n{:?}\n", trees);
let ctrl = StackifyContext::new(body, &cfg, &rpo).compute();
log::trace!("Ctrl:\n{:?}\n", ctrl);
Ok(WasmBackend {
body,
rpo,
trees,
ctrl,
})
} }
pub fn compile(&self) -> Result<Vec<u8>> { pub fn compile(&self) -> Result<Vec<u8>> {

View file

@ -69,10 +69,10 @@ impl WasmLabel {
} }
} }
struct Context<'a> { pub struct Context<'a, 'b> {
body: &'a FunctionBody, body: &'a FunctionBody,
cfg: &'a CFGInfo, cfg: &'b CFGInfo,
rpo: &'a RPO, rpo: &'b RPO,
merge_nodes: HashSet<Block>, merge_nodes: HashSet<Block>,
loop_headers: HashSet<Block>, loop_headers: HashSet<Block>,
ctrl_stack: Vec<CtrlEntry>, ctrl_stack: Vec<CtrlEntry>,
@ -95,8 +95,8 @@ impl CtrlEntry {
} }
} }
impl<'a> Context<'a> { impl<'a, 'b> Context<'a, 'b> {
fn new(body: &'a FunctionBody, cfg: &'a CFGInfo, rpo: &'a RPO) -> Self { pub fn new(body: &'a FunctionBody, cfg: &'b CFGInfo, rpo: &'b RPO) -> Self {
let (merge_nodes, loop_headers) = let (merge_nodes, loop_headers) =
Self::compute_merge_nodes_and_loop_headers(body, cfg, rpo); Self::compute_merge_nodes_and_loop_headers(body, cfg, rpo);
Self { Self {
@ -109,6 +109,12 @@ impl<'a> Context<'a> {
} }
} }
pub fn compute(mut self) -> Vec<WasmBlock<'a>> {
let mut body = vec![];
self.handle_dom_subtree(self.cfg.entry, &mut body);
body
}
fn compute_merge_nodes_and_loop_headers( fn compute_merge_nodes_and_loop_headers(
body: &FunctionBody, body: &FunctionBody,
cfg: &CFGInfo, cfg: &CFGInfo,

View file

@ -10,6 +10,7 @@ use std::convert::TryFrom;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ValueArg(Value, u16); pub struct ValueArg(Value, u16);
#[derive(Clone, Debug)]
pub struct Trees { pub struct Trees {
/// Is a value placed "under" the given arg slot of the given /// Is a value placed "under" the given arg slot of the given
/// other value? /// other value?