Add typed locals

This commit is contained in:
Rerumu 2021-12-22 00:23:02 -05:00
parent 76b8810712
commit 25e2730a0c
5 changed files with 32 additions and 11 deletions

View File

@ -1,5 +1,5 @@
use parity_wasm::elements::{ use parity_wasm::elements::{
BlockType, External, FuncBody, FunctionType, ImportEntry, Instruction, Local, Module, Type, BlockType, External, FuncBody, FunctionType, ImportEntry, Instruction, Module, Type, ValueType,
}; };
use super::{ use super::{
@ -120,8 +120,11 @@ fn is_dead_precursor(inst: &Instruction) -> bool {
) )
} }
fn local_sum(body: &FuncBody) -> u32 { fn load_local_list(func: &FuncBody) -> Vec<ValueType> {
body.locals().iter().map(Local::count).sum() func.locals()
.iter()
.flat_map(|l| std::iter::repeat(l.value_type()).take(l.count().try_into().unwrap()))
.collect()
} }
fn load_func_at(wasm: &Module, index: usize) -> &FuncBody { fn load_func_at(wasm: &Module, index: usize) -> &FuncBody {
@ -144,8 +147,8 @@ impl<'a> Builder<'a> {
let func = load_func_at(self.wasm, index); let func = load_func_at(self.wasm, index);
let arity = &self.other.in_arity[index]; let arity = &self.other.in_arity[index];
let local_list = load_local_list(func);
let num_param = arity.num_param; let num_param = arity.num_param;
let num_local = local_sum(func);
self.num_result = arity.num_result; self.num_result = arity.num_result;
@ -153,8 +156,8 @@ impl<'a> Builder<'a> {
let num_stack = self.last_stack.try_into().unwrap(); let num_stack = self.last_stack.try_into().unwrap();
Function { Function {
local_list,
num_param, num_param,
num_local,
num_stack, num_stack,
body, body,
} }

View File

@ -1,6 +1,6 @@
use std::ops::Range; use std::ops::Range;
use parity_wasm::elements::BrTableData; use parity_wasm::elements::{BrTableData, ValueType};
use super::tag::{BinOp, CmpOp, Load, Store, UnOp}; use super::tag::{BinOp, CmpOp, Load, Store, UnOp};
@ -182,8 +182,8 @@ pub enum Statement {
} }
pub struct Function { pub struct Function {
pub local_list: Vec<ValueType>,
pub num_param: u32, pub num_param: u32,
pub num_local: u32,
pub num_stack: u32, pub num_stack: u32,
pub body: Forward, pub body: Forward,
} }

View File

@ -734,6 +734,11 @@ impl<'a> Transpiler<'a> for LuaJIT<'a> {
Self::gen_localize(&func_list, w)?; Self::gen_localize(&func_list, w)?;
write!(w, "local ZERO_i32 = 0 ")?;
write!(w, "local ZERO_i64 = 0LL ")?;
write!(w, "local ZERO_f32 = 0.0 ")?;
write!(w, "local ZERO_f64 = 0.0 ")?;
write!(w, "local table_new = require(\"table.new\")")?; write!(w, "local table_new = require(\"table.new\")")?;
write_list("FUNC_LIST", self.wasm.functions_space(), w)?; write_list("FUNC_LIST", self.wasm.functions_space(), w)?;
write_list("TABLE_LIST", self.wasm.table_space(), w)?; write_list("TABLE_LIST", self.wasm.table_space(), w)?;

View File

@ -723,6 +723,11 @@ impl<'a> Transpiler<'a> for Luau<'a> {
Self::gen_localize(&func_list, w)?; Self::gen_localize(&func_list, w)?;
write!(w, "local ZERO_i32 = 0 ")?;
write!(w, "local ZERO_i64 = 0 ")?;
write!(w, "local ZERO_f32 = 0.0 ")?;
write!(w, "local ZERO_f64 = 0.0 ")?;
write_list("FUNC_LIST", self.wasm.functions_space(), w)?; write_list("FUNC_LIST", self.wasm.functions_space(), w)?;
write_list("TABLE_LIST", self.wasm.table_space(), w)?; write_list("TABLE_LIST", self.wasm.table_space(), w)?;
write_list("MEMORY_LIST", self.wasm.memory_space(), w)?; write_list("MEMORY_LIST", self.wasm.memory_space(), w)?;

View File

@ -104,12 +104,20 @@ pub fn write_result_list(range: Range<u32>, w: Writer) -> Result<()> {
} }
pub fn write_variable_list(func: &Function, w: Writer) -> Result<()> { pub fn write_variable_list(func: &Function, w: Writer) -> Result<()> {
if func.num_local != 0 { if !func.local_list.is_empty() {
let list = vec!["0"; func.num_local as usize].join(", "); let num_local = func.local_list.len().try_into().unwrap();
write!(w, "local ")?; write!(w, "local ")?;
write_in_order("loc", func.num_local, w)?; write_in_order("loc", num_local, w)?;
write!(w, " = {} ", list)?; write!(w, " = ")?;
for (i, t) in func.local_list.iter().enumerate() {
if i != 0 {
write!(w, ", ")?;
}
write!(w, "ZERO_{} ", t)?;
}
} }
if func.num_stack != 0 { if func.num_stack != 0 {