Add shortcuts for native operations

This commit is contained in:
Rerumu 2022-05-21 04:11:04 -04:00
parent c3a3a09107
commit ce54d834a5
4 changed files with 21 additions and 20 deletions

View File

@ -159,26 +159,11 @@ do
end
do
local band = {}
local bor = {}
local bxor = {}
local bnot = {}
band.i32 = bit.band
band.i64 = bit.band
bnot.i32 = bit.bnot
bnot.i64 = bit.bnot
bor.i32 = bit.bor
bor.i64 = bit.bor
bxor.i32 = bit.bxor
bxor.i64 = bit.bxor
module.band = band
module.bor = bor
module.bxor = bxor
module.bnot = bnot
end

View File

@ -230,12 +230,21 @@ fn build_func_list(wasm: &Module, type_info: &TypeInfo) -> Vec<Intermediate> {
.collect()
}
fn write_local_operation(head: &str, tail: &str, w: &mut dyn Write) -> Result<()> {
match (head, tail) {
("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _) | ("band" | "bor" | "bxor", _) => {
write!(w, "local {head}_{tail} = math.{head} ")
}
_ => write!(w, "local {head}_{tail} = rt.{head}.{tail} "),
}
}
fn write_localize_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result<()> {
let loc_set: BTreeSet<_> = func_list.iter().flat_map(localize::visit).collect();
loc_set
.into_iter()
.try_for_each(|(a, b)| write!(w, "local {a}_{b} = rt.{a}.{b} "))
.try_for_each(|(a, b)| write_local_operation(a, b, w))
}
fn write_memory_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result<Vec<usize>> {

View File

@ -180,16 +180,13 @@ do
local bxor = {}
local bnot = {}
band.i32 = bit32.band
band.i64 = I64.bit_and
bnot.i32 = bit32.bnot
bnot.i64 = I64.bit_not
bor.i32 = bit32.bor
bor.i64 = I64.bit_or
bxor.i32 = bit32.bxor
bxor.i64 = I64.bit_xor
module.band = band

View File

@ -229,12 +229,22 @@ fn build_func_list(wasm: &Module, type_info: &TypeInfo) -> Vec<Intermediate> {
.collect()
}
fn write_local_operation(head: &str, tail: &str, w: &mut dyn Write) -> Result<()> {
match (head, tail) {
("abs" | "ceil" | "floor" | "sqrt" | "min" | "max", _)
| ("band" | "bor" | "bxor", "i32") => {
write!(w, "local {head}_{tail} = math.{head} ")
}
_ => write!(w, "local {head}_{tail} = rt.{head}.{tail} "),
}
}
fn write_localize_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result<()> {
let loc_set: BTreeSet<_> = func_list.iter().flat_map(localize::visit).collect();
loc_set
.into_iter()
.try_for_each(|(a, b)| write!(w, "local {a}_{b} = rt.{a}.{b} "))
.try_for_each(|(a, b)| write_local_operation(a, b, w))
}
fn write_memory_used(func_list: &[Intermediate], w: &mut dyn Write) -> Result<Vec<usize>> {