Refactor transformer interface
This commit is contained in:
parent
21b87f352c
commit
16ce3eca98
@ -18,8 +18,8 @@ use super::{
|
|||||||
pub struct Transformer<'a> {
|
pub struct Transformer<'a> {
|
||||||
// target state
|
// target state
|
||||||
wasm: &'a Module,
|
wasm: &'a Module,
|
||||||
arity: &'a ArityList,
|
other: &'a ArityList,
|
||||||
name: usize,
|
num_result: u32,
|
||||||
|
|
||||||
// translation state
|
// translation state
|
||||||
pending: Vec<Vec<Expression>>,
|
pending: Vec<Vec<Expression>>,
|
||||||
@ -36,25 +36,27 @@ fn is_else_stat(inst: &Instruction) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Transformer<'a> {
|
impl<'a> Transformer<'a> {
|
||||||
pub fn new(wasm: &'a Module, arity: &'a ArityList, name: usize) -> Transformer<'a> {
|
pub fn new(wasm: &'a Module, other: &'a ArityList) -> Transformer<'a> {
|
||||||
Transformer {
|
Transformer {
|
||||||
wasm,
|
wasm,
|
||||||
arity,
|
other,
|
||||||
name,
|
num_result: 0,
|
||||||
pending: Vec::new(),
|
pending: Vec::new(),
|
||||||
stack: Vec::new(),
|
stack: Vec::new(),
|
||||||
last_stack: 0,
|
last_stack: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn consume(mut self) -> Function {
|
pub fn consume(mut self, name: usize) -> Function {
|
||||||
debug_assert!(self.name != usize::MAX, "Not an indexed value");
|
let arity = &self.other.in_arity[name];
|
||||||
|
|
||||||
let func = &self.wasm.code_section().unwrap().bodies()[self.name];
|
self.num_result = arity.num_result;
|
||||||
|
|
||||||
|
let func = &self.wasm.code_section().unwrap().bodies()[name];
|
||||||
let body = self.new_forward(&mut func.code().elements());
|
let body = self.new_forward(&mut func.code().elements());
|
||||||
|
|
||||||
Function {
|
Function {
|
||||||
num_param: self.arity.in_arity[self.name].num_param,
|
num_param: arity.num_param,
|
||||||
num_local: local_sum(func.locals()),
|
num_local: local_sum(func.locals()),
|
||||||
num_stack: u32::try_from(self.last_stack).unwrap(),
|
num_stack: u32::try_from(self.last_stack).unwrap(),
|
||||||
body,
|
body,
|
||||||
@ -121,7 +123,7 @@ impl<'a> Transformer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn gen_return(&mut self, stat: &mut Vec<Statement>) {
|
fn gen_return(&mut self, stat: &mut Vec<Statement>) {
|
||||||
let num = self.arity.in_arity[self.name].num_result as usize;
|
let num = self.num_result as usize;
|
||||||
let list = self.stack.split_off(self.stack.len() - num);
|
let list = self.stack.split_off(self.stack.len() - num);
|
||||||
|
|
||||||
self.gen_leak_pending(stat);
|
self.gen_leak_pending(stat);
|
||||||
@ -129,7 +131,7 @@ impl<'a> Transformer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn gen_call(&mut self, func: u32, stat: &mut Vec<Statement>) {
|
fn gen_call(&mut self, func: u32, stat: &mut Vec<Statement>) {
|
||||||
let arity = self.arity.arity_of(func as usize);
|
let arity = self.other.arity_of(func as usize);
|
||||||
|
|
||||||
let param_list = self
|
let param_list = self
|
||||||
.stack
|
.stack
|
||||||
|
@ -283,7 +283,7 @@ impl<'a> Module<'a> {
|
|||||||
write!(w, "local rt = require({})", ed.runtime())?;
|
write!(w, "local rt = require({})", ed.runtime())?;
|
||||||
|
|
||||||
let func_list: Vec<_> = (0..self.arity.in_arity.len())
|
let func_list: Vec<_> = (0..self.arity.in_arity.len())
|
||||||
.map(|i| Transformer::new(self.wasm, &self.arity, i).consume())
|
.map(|i| Transformer::new(self.wasm, &self.arity).consume(i))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Self::gen_localize(&func_list, w)?;
|
Self::gen_localize(&func_list, w)?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user