Add naive local and temporary spills
This commit is contained in:
@@ -92,7 +92,7 @@ pub fn visit(ast: &FuncData) -> (BTreeSet<(&'static str, &'static str)>, BTreeSe
|
||||
memory_set: BTreeSet::new(),
|
||||
};
|
||||
|
||||
if ast.local_data().iter().any(|v| v.1 == ValType::I64) {
|
||||
if ast.local_data().iter().any(|&v| v == ValType::I64) {
|
||||
visit.local_set.insert(("i64", "ZERO"));
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,12 @@ use std::{
|
||||
};
|
||||
|
||||
use wasm_ast::node::{
|
||||
BinOp, CmpOp, Expression, GetGlobal, GetLocal, GetTemporary, LoadAt, MemorySize, Select, UnOp,
|
||||
Value,
|
||||
BinOp, CmpOp, Expression, GetGlobal, LoadAt, Local, MemorySize, Select, Temporary, UnOp, Value,
|
||||
};
|
||||
|
||||
use crate::analyzer::as_symbol::AsSymbol;
|
||||
|
||||
use super::manager::{write_separated, DriverNoContext};
|
||||
use super::manager::{write_separated, Driver, Manager};
|
||||
|
||||
macro_rules! impl_write_number {
|
||||
($name:tt, $numeric:ty) => {
|
||||
@@ -26,43 +25,55 @@ macro_rules! impl_write_number {
|
||||
};
|
||||
}
|
||||
|
||||
impl DriverNoContext for Select {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for Select {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "(if ")?;
|
||||
Condition(self.condition()).write(w)?;
|
||||
Condition(self.condition()).write(mng, w)?;
|
||||
write!(w, " then ")?;
|
||||
self.on_true().write(w)?;
|
||||
self.on_true().write(mng, w)?;
|
||||
write!(w, " else ")?;
|
||||
self.on_false().write(w)?;
|
||||
self.on_false().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for GetTemporary {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "reg_{}", self.var())
|
||||
impl Driver for Temporary {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let var = self.var();
|
||||
|
||||
if let Some(var) = var.checked_sub(mng.num_temp()) {
|
||||
write!(w, "reg_spill[{}]", var + 1)
|
||||
} else {
|
||||
write!(w, "reg_{var}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for GetLocal {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "loc_{}", self.var())
|
||||
impl Driver for Local {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let var = self.var();
|
||||
|
||||
if let Some(var) = var.checked_sub(mng.num_local()) {
|
||||
write!(w, "loc_spill[{}]", var + 1)
|
||||
} else {
|
||||
write!(w, "loc_{var}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for GetGlobal {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for GetGlobal {
|
||||
fn write(&self, _mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "GLOBAL_LIST[{}].value", self.var())
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for LoadAt {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for LoadAt {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let name = self.load_type().as_name();
|
||||
let memory = self.memory();
|
||||
|
||||
write!(w, "load_{name}(memory_at_{memory}, ")?;
|
||||
self.pointer().write(w)?;
|
||||
self.pointer().write(mng, w)?;
|
||||
|
||||
if self.offset() != 0 {
|
||||
write!(w, " + {}", self.offset())?;
|
||||
@@ -72,8 +83,8 @@ impl DriverNoContext for LoadAt {
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for MemorySize {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for MemorySize {
|
||||
fn write(&self, _mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "memory_at_{}.min", self.memory())
|
||||
}
|
||||
}
|
||||
@@ -101,8 +112,8 @@ fn write_i64(number: i64, w: &mut dyn Write) -> Result<()> {
|
||||
impl_write_number!(write_f32, f32);
|
||||
impl_write_number!(write_f64, f64);
|
||||
|
||||
impl DriverNoContext for Value {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for Value {
|
||||
fn write(&self, _mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
match self {
|
||||
Self::I32(i) => write_i32(*i, w),
|
||||
Self::I64(i) => write_i64(*i, w),
|
||||
@@ -112,97 +123,97 @@ impl DriverNoContext for Value {
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for UnOp {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for UnOp {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let (a, b) = self.op_type().as_name();
|
||||
|
||||
write!(w, "{a}_{b}(")?;
|
||||
self.rhs().write(w)?;
|
||||
self.rhs().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for BinOp {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for BinOp {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
if let Some(symbol) = self.op_type().as_symbol() {
|
||||
write!(w, "(")?;
|
||||
self.lhs().write(w)?;
|
||||
self.lhs().write(mng, w)?;
|
||||
write!(w, " {symbol} ")?;
|
||||
} else {
|
||||
let (head, tail) = self.op_type().as_name();
|
||||
|
||||
write!(w, "{head}_{tail}(")?;
|
||||
self.lhs().write(w)?;
|
||||
self.lhs().write(mng, w)?;
|
||||
write!(w, ", ")?;
|
||||
}
|
||||
|
||||
self.rhs().write(w)?;
|
||||
self.rhs().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
struct CmpOpBoolean<'a>(&'a CmpOp);
|
||||
|
||||
impl DriverNoContext for CmpOpBoolean<'_> {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for CmpOpBoolean<'_> {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let cmp = self.0;
|
||||
|
||||
if let Some(symbol) = cmp.op_type().as_symbol() {
|
||||
cmp.lhs().write(w)?;
|
||||
cmp.lhs().write(mng, w)?;
|
||||
write!(w, " {symbol} ")?;
|
||||
cmp.rhs().write(w)
|
||||
cmp.rhs().write(mng, w)
|
||||
} else {
|
||||
let (head, tail) = cmp.op_type().as_name();
|
||||
|
||||
write!(w, "{head}_{tail}(")?;
|
||||
cmp.lhs().write(w)?;
|
||||
cmp.lhs().write(mng, w)?;
|
||||
write!(w, ", ")?;
|
||||
cmp.rhs().write(w)?;
|
||||
cmp.rhs().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for CmpOp {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for CmpOp {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "(if ")?;
|
||||
CmpOpBoolean(self).write(w)?;
|
||||
CmpOpBoolean(self).write(mng, w)?;
|
||||
write!(w, " then 1 else 0)")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Condition<'a>(pub &'a Expression);
|
||||
|
||||
impl DriverNoContext for Condition<'_> {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for Condition<'_> {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
if let Expression::CmpOp(node) = self.0 {
|
||||
CmpOpBoolean(node).write(w)
|
||||
CmpOpBoolean(node).write(mng, w)
|
||||
} else {
|
||||
self.0.write(w)?;
|
||||
self.0.write(mng, w)?;
|
||||
write!(w, " ~= 0")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for Expression {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for Expression {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
match self {
|
||||
Self::Select(e) => e.write(w),
|
||||
Self::GetTemporary(e) => e.write(w),
|
||||
Self::GetLocal(e) => e.write(w),
|
||||
Self::GetGlobal(e) => e.write(w),
|
||||
Self::LoadAt(e) => e.write(w),
|
||||
Self::MemorySize(e) => e.write(w),
|
||||
Self::Value(e) => e.write(w),
|
||||
Self::UnOp(e) => e.write(w),
|
||||
Self::BinOp(e) => e.write(w),
|
||||
Self::CmpOp(e) => e.write(w),
|
||||
Self::Select(e) => e.write(mng, w),
|
||||
Self::GetTemporary(e) => e.write(mng, w),
|
||||
Self::GetLocal(e) => e.write(mng, w),
|
||||
Self::GetGlobal(e) => e.write(mng, w),
|
||||
Self::LoadAt(e) => e.write(mng, w),
|
||||
Self::MemorySize(e) => e.write(mng, w),
|
||||
Self::Value(e) => e.write(mng, w),
|
||||
Self::UnOp(e) => e.write(mng, w),
|
||||
Self::BinOp(e) => e.write(mng, w),
|
||||
Self::CmpOp(e) => e.write(mng, w),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for &[Expression] {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
write_separated(self.iter(), |e, w| e.write(w), w)
|
||||
impl Driver for &[Expression] {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
write_separated(self.iter(), |e, w| e.write(mng, w), w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::{Result, Write},
|
||||
ops::Range,
|
||||
};
|
||||
|
||||
use wasm_ast::node::{BrTable, LabelType};
|
||||
use wasm_ast::node::{BrTable, FuncData, LabelType};
|
||||
|
||||
use crate::analyzer::{br_target, localize};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! indentation {
|
||||
@@ -31,30 +32,87 @@ macro_rules! line {
|
||||
}};
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
fn get_pinned_registers(
|
||||
upvalues: usize,
|
||||
params: usize,
|
||||
locals: usize,
|
||||
temporaries: usize,
|
||||
) -> (usize, usize) {
|
||||
const MAX_LOCAL_COUNT: usize = 170;
|
||||
|
||||
let available = MAX_LOCAL_COUNT
|
||||
.saturating_sub(upvalues)
|
||||
.saturating_sub(params);
|
||||
|
||||
let locals = available.min(locals);
|
||||
let temporaries = available.saturating_sub(locals).min(temporaries);
|
||||
|
||||
(params + locals, temporaries)
|
||||
}
|
||||
|
||||
pub struct Manager {
|
||||
table_map: HashMap<usize, usize>,
|
||||
has_branch: bool,
|
||||
num_local: usize,
|
||||
num_temp: usize,
|
||||
label_list: Vec<Option<LabelType>>,
|
||||
indentation: usize,
|
||||
}
|
||||
|
||||
impl Manager {
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
table_map: HashMap::new(),
|
||||
has_branch: false,
|
||||
num_local: 0,
|
||||
num_temp: usize::MAX,
|
||||
label_list: Vec::new(),
|
||||
indentation: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn function(ast: &FuncData) -> Self {
|
||||
let (upvalues, memories) = localize::visit(ast);
|
||||
let (table_map, has_branch) = br_target::visit(ast);
|
||||
let (num_local, num_temp) = get_pinned_registers(
|
||||
upvalues.len() + memories.len(),
|
||||
ast.num_param(),
|
||||
ast.local_data().len(),
|
||||
ast.num_stack(),
|
||||
);
|
||||
|
||||
Self {
|
||||
table_map,
|
||||
has_branch,
|
||||
num_local,
|
||||
num_temp,
|
||||
label_list: Vec::new(),
|
||||
indentation: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_table_index(&self, table: &BrTable) -> usize {
|
||||
let id = table as *const _ as usize;
|
||||
|
||||
self.table_map[&id]
|
||||
}
|
||||
|
||||
pub fn set_branch_information(&mut self, table_map: HashMap<usize, usize>, has_branch: bool) {
|
||||
self.table_map = table_map;
|
||||
self.has_branch = has_branch;
|
||||
pub fn has_table(&self) -> bool {
|
||||
!self.table_map.is_empty()
|
||||
}
|
||||
|
||||
pub const fn has_branch(&self) -> bool {
|
||||
self.has_branch
|
||||
}
|
||||
|
||||
pub const fn num_local(&self) -> usize {
|
||||
self.num_local
|
||||
}
|
||||
|
||||
pub const fn num_temp(&self) -> usize {
|
||||
self.num_temp
|
||||
}
|
||||
|
||||
pub fn label_list(&self) -> &[Option<LabelType>] {
|
||||
&self.label_list
|
||||
}
|
||||
@@ -84,10 +142,6 @@ pub trait Driver {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()>;
|
||||
}
|
||||
|
||||
pub trait DriverNoContext {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()>;
|
||||
}
|
||||
|
||||
pub fn write_separated<I, T, M>(mut iter: I, mut func: M, w: &mut dyn Write) -> Result<()>
|
||||
where
|
||||
M: FnMut(T, &mut dyn Write) -> Result<()>,
|
||||
@@ -103,7 +157,3 @@ where
|
||||
func(v, w)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn write_ascending(prefix: &str, range: Range<usize>, w: &mut dyn Write) -> Result<()> {
|
||||
write_separated(range, |i, w| write!(w, "{prefix}_{i}"), w)
|
||||
}
|
||||
|
||||
@@ -5,24 +5,30 @@ use std::{
|
||||
|
||||
use wasm_ast::node::{
|
||||
Block, Br, BrIf, BrTable, Call, CallIndirect, FuncData, If, LabelType, MemoryCopy, MemoryFill,
|
||||
MemoryGrow, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, Terminator,
|
||||
MemoryGrow, ResultList, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, Terminator,
|
||||
};
|
||||
use wasmparser::ValType;
|
||||
|
||||
use crate::{analyzer::br_target, indentation, indented, line};
|
||||
use crate::{backend::manager::write_separated, indentation, indented, line};
|
||||
|
||||
use super::{
|
||||
expression::Condition,
|
||||
manager::{write_ascending, write_separated, Driver, DriverNoContext, Manager},
|
||||
manager::{Driver, Manager},
|
||||
};
|
||||
|
||||
impl Driver for ResultList {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
write_separated(self.iter(), |t, w| t.write(mng, w), w)
|
||||
}
|
||||
}
|
||||
|
||||
impl Driver for Br {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
if !self.align().is_aligned() {
|
||||
indentation!(mng, w)?;
|
||||
write_ascending("reg", self.align().new_range(), w)?;
|
||||
self.align().new_range().write(mng, w)?;
|
||||
write!(w, " = ")?;
|
||||
write_ascending("reg", self.align().old_range(), w)?;
|
||||
self.align().old_range().write(mng, w)?;
|
||||
writeln!(w)?;
|
||||
}
|
||||
|
||||
@@ -106,7 +112,7 @@ fn write_table_setup(table: &BrTable, mng: &mut Manager, w: &mut dyn Write) -> R
|
||||
line!(mng, w, "end")?;
|
||||
|
||||
indented!(mng, w, "temp = br_map[{id}][")?;
|
||||
table.condition().write(w)?;
|
||||
table.condition().write(mng, w)?;
|
||||
writeln!(w, "] or {}", table.default().target())
|
||||
}
|
||||
|
||||
@@ -193,7 +199,7 @@ impl Driver for Block {
|
||||
impl Driver for BrIf {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
indented!(mng, w, "if ")?;
|
||||
Condition(self.condition()).write(w)?;
|
||||
Condition(self.condition()).write(mng, w)?;
|
||||
writeln!(w, " then")?;
|
||||
mng.indent();
|
||||
self.target().write(mng, w)?;
|
||||
@@ -205,7 +211,7 @@ impl Driver for BrIf {
|
||||
impl Driver for If {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
indented!(mng, w, "if ")?;
|
||||
Condition(self.condition()).write(w)?;
|
||||
Condition(self.condition()).write(mng, w)?;
|
||||
writeln!(w, " then")?;
|
||||
|
||||
mng.indent();
|
||||
@@ -223,120 +229,119 @@ impl Driver for If {
|
||||
}
|
||||
}
|
||||
|
||||
fn write_call_store(result: Range<usize>, w: &mut dyn Write) -> Result<()> {
|
||||
if result.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
write_ascending("reg", result, w)?;
|
||||
write!(w, " = ")
|
||||
}
|
||||
|
||||
impl DriverNoContext for Call {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
write_call_store(self.result(), w)?;
|
||||
impl Driver for Call {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
if !self.result_list().is_empty() {
|
||||
self.result_list().write(mng, w)?;
|
||||
write!(w, " = ")?;
|
||||
}
|
||||
|
||||
write!(w, "FUNC_LIST[{}](", self.function())?;
|
||||
self.param_list().write(w)?;
|
||||
self.param_list().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for CallIndirect {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
write_call_store(self.result(), w)?;
|
||||
impl Driver for CallIndirect {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
if !self.result_list().is_empty() {
|
||||
self.result_list().write(mng, w)?;
|
||||
write!(w, " = ")?;
|
||||
}
|
||||
|
||||
write!(w, "TABLE_LIST[{}].data[", self.table())?;
|
||||
self.index().write(w)?;
|
||||
self.index().write(mng, w)?;
|
||||
write!(w, "](")?;
|
||||
self.param_list().write(w)?;
|
||||
self.param_list().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for SetTemporary {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "reg_{} = ", self.var())?;
|
||||
self.value().write(w)
|
||||
impl Driver for SetTemporary {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
self.var().write(mng, w)?;
|
||||
write!(w, " = ")?;
|
||||
self.value().write(mng, w)
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for SetLocal {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "loc_{} = ", self.var())?;
|
||||
self.value().write(w)
|
||||
impl Driver for SetLocal {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
self.var().write(mng, w)?;
|
||||
write!(w, " = ")?;
|
||||
self.value().write(mng, w)
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for SetGlobal {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for SetGlobal {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "GLOBAL_LIST[{}].value = ", self.var())?;
|
||||
self.value().write(w)
|
||||
self.value().write(mng, w)
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for StoreAt {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for StoreAt {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let name = self.store_type().as_name();
|
||||
let memory = self.memory();
|
||||
|
||||
write!(w, "store_{name}(memory_at_{memory}, ")?;
|
||||
|
||||
self.pointer().write(w)?;
|
||||
self.pointer().write(mng, w)?;
|
||||
|
||||
if self.offset() != 0 {
|
||||
write!(w, " + {}", self.offset())?;
|
||||
}
|
||||
|
||||
write!(w, ", ")?;
|
||||
self.value().write(w)?;
|
||||
self.value().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for MemoryGrow {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
let result = self.result();
|
||||
impl Driver for MemoryGrow {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let memory = self.memory();
|
||||
|
||||
write!(w, "reg_{result} = rt.allocator.grow(memory_at_{memory}, ")?;
|
||||
self.size().write(w)?;
|
||||
self.result().write(mng, w)?;
|
||||
write!(w, " = rt.allocator.grow(memory_at_{memory}, ")?;
|
||||
self.size().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for MemoryCopy {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for MemoryCopy {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let memory_1 = self.destination().memory();
|
||||
let memory_2 = self.source().memory();
|
||||
|
||||
write!(w, "rt.store.copy(memory_at_{memory_1}, ")?;
|
||||
self.destination().pointer().write(w)?;
|
||||
self.destination().pointer().write(mng, w)?;
|
||||
write!(w, ", memory_at_{memory_2}, ")?;
|
||||
self.source().pointer().write(w)?;
|
||||
self.source().pointer().write(mng, w)?;
|
||||
write!(w, ", ")?;
|
||||
self.size().write(w)?;
|
||||
self.size().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl DriverNoContext for MemoryFill {
|
||||
fn write(&self, w: &mut dyn Write) -> Result<()> {
|
||||
impl Driver for MemoryFill {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let memory = self.destination().memory();
|
||||
|
||||
write!(w, "rt.store.fill(memory_at_{memory}, ")?;
|
||||
self.destination().pointer().write(w)?;
|
||||
self.destination().pointer().write(mng, w)?;
|
||||
write!(w, ", ")?;
|
||||
self.size().write(w)?;
|
||||
self.size().write(mng, w)?;
|
||||
write!(w, ", ")?;
|
||||
self.value().write(w)?;
|
||||
self.value().write(mng, w)?;
|
||||
write!(w, ")")
|
||||
}
|
||||
}
|
||||
|
||||
fn write_stat(stat: &dyn DriverNoContext, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
fn write_stat(stat: &dyn Driver, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
indentation!(mng, w)?;
|
||||
stat.write(w)?;
|
||||
stat.write(mng, w)?;
|
||||
writeln!(w)
|
||||
}
|
||||
|
||||
@@ -359,45 +364,53 @@ impl Driver for Statement {
|
||||
}
|
||||
}
|
||||
|
||||
fn has_sane_variables(ast: &FuncData) -> bool {
|
||||
let local_count = ast.local_data().iter().map(|v| v.0).sum::<u32>();
|
||||
let local_count = usize::try_from(local_count).unwrap();
|
||||
let param_count = ast.num_param();
|
||||
let temp_count = ast.num_stack();
|
||||
|
||||
temp_count + param_count + local_count < 170
|
||||
}
|
||||
|
||||
fn write_parameter_list(ast: &FuncData, w: &mut dyn Write) -> Result<()> {
|
||||
write!(w, "function(")?;
|
||||
write_ascending("loc", 0..ast.num_param(), w)?;
|
||||
write_separated(0..ast.num_param(), |i, w| write!(w, "loc_{i}"), w)?;
|
||||
writeln!(w, ")")
|
||||
}
|
||||
|
||||
const fn type_to_zero(typ: ValType) -> &'static str {
|
||||
match typ {
|
||||
ValType::F32 | ValType::F64 => "0.0",
|
||||
ValType::I64 => "i64_ZERO",
|
||||
_ => "0",
|
||||
}
|
||||
}
|
||||
|
||||
fn write_variable_list(ast: &FuncData, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
let mut total = ast.num_param();
|
||||
let mut locals = ast.local_data().iter().copied();
|
||||
let num_local = mng.num_local() - ast.num_param();
|
||||
|
||||
for data in ast.local_data().iter().filter(|v| v.0 != 0) {
|
||||
let range = total..total + usize::try_from(data.0).unwrap();
|
||||
let zero = if data.1 == ValType::I64 {
|
||||
"i64_ZERO"
|
||||
} else {
|
||||
"0"
|
||||
};
|
||||
for (i, typ) in locals.by_ref().enumerate().take(num_local) {
|
||||
let index = ast.num_param() + i;
|
||||
let zero = type_to_zero(typ);
|
||||
|
||||
total = range.end;
|
||||
|
||||
indented!(mng, w, "local ")?;
|
||||
write_ascending("loc", range.clone(), w)?;
|
||||
write!(w, " = ")?;
|
||||
write_separated(range, |_, w| w.write_all(zero.as_bytes()), w)?;
|
||||
writeln!(w)?;
|
||||
line!(mng, w, "local loc_{index} = {zero}")?;
|
||||
}
|
||||
|
||||
if ast.num_stack() != 0 {
|
||||
indented!(mng, w, "local ")?;
|
||||
write_ascending("reg", 0..ast.num_stack(), w)?;
|
||||
writeln!(w)?;
|
||||
if locals.len() != 0 {
|
||||
indented!(mng, w, "local loc_spill = {{ ")?;
|
||||
|
||||
for typ in locals {
|
||||
let zero = type_to_zero(typ);
|
||||
|
||||
write!(w, "{zero}, ")?;
|
||||
}
|
||||
|
||||
writeln!(w, "}}")?;
|
||||
}
|
||||
|
||||
let mut temporaries = 0..ast.num_stack();
|
||||
|
||||
for i in temporaries.by_ref().take(mng.num_temp()) {
|
||||
line!(mng, w, "local reg_{i}")?;
|
||||
}
|
||||
|
||||
if !temporaries.is_empty() {
|
||||
let len = temporaries.len();
|
||||
|
||||
line!(mng, w, "local reg_spill = table.create({len})")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -405,34 +418,26 @@ fn write_variable_list(ast: &FuncData, mng: &mut Manager, w: &mut dyn Write) ->
|
||||
|
||||
impl Driver for FuncData {
|
||||
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
|
||||
if !has_sane_variables(self) {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"too many variables in function",
|
||||
));
|
||||
}
|
||||
|
||||
let (table_map, has_branch) = br_target::visit(self);
|
||||
|
||||
mng.indent();
|
||||
|
||||
write_parameter_list(self, w)?;
|
||||
write_variable_list(self, mng, w)?;
|
||||
|
||||
if has_branch {
|
||||
if mng.has_branch() {
|
||||
line!(mng, w, "local desired")?;
|
||||
}
|
||||
|
||||
if !table_map.is_empty() {
|
||||
if mng.has_table() {
|
||||
line!(mng, w, "local br_map = {{}}")?;
|
||||
}
|
||||
|
||||
mng.set_branch_information(table_map, has_branch);
|
||||
self.code().write(mng, w)?;
|
||||
|
||||
if self.num_result() != 0 {
|
||||
indented!(mng, w, "return ")?;
|
||||
write_ascending("reg", 0..self.num_result(), w)?;
|
||||
|
||||
ResultList::new(0, self.num_result()).write(mng, w)?;
|
||||
|
||||
writeln!(w)?;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ use wasmparser::{
|
||||
|
||||
use crate::{
|
||||
analyzer::localize,
|
||||
backend::manager::{Driver, DriverNoContext, Manager},
|
||||
backend::manager::{Driver, Manager},
|
||||
};
|
||||
|
||||
trait AsIEName {
|
||||
@@ -51,7 +51,7 @@ fn write_constant(init: &ConstExpr, type_info: &TypeInfo, w: &mut dyn Write) ->
|
||||
let func = Factory::from_type_info(type_info).create_anonymous(&code);
|
||||
|
||||
if let Some(Statement::SetTemporary(stat)) = func.code().code().last() {
|
||||
stat.value().write(w)
|
||||
stat.value().write(&mut Manager::empty(), w)
|
||||
} else {
|
||||
writeln!(w, r#"error("Valueless constant")"#)
|
||||
}
|
||||
@@ -283,7 +283,7 @@ fn write_func_list(wasm: &Module, func_list: &[FuncData], w: &mut dyn Write) ->
|
||||
|
||||
write_func_start(wasm, index, w)?;
|
||||
|
||||
v.write(&mut Manager::default(), w)
|
||||
v.write(&mut Manager::function(v), w)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -322,9 +322,9 @@ fn write_module_start(
|
||||
/// # Errors
|
||||
/// Returns `Err` if writing to `Write` failed.
|
||||
pub fn from_inst_list(code: &[Operator], type_info: &TypeInfo, w: &mut dyn Write) -> Result<()> {
|
||||
Factory::from_type_info(type_info)
|
||||
.create_anonymous(code)
|
||||
.write(&mut Manager::default(), w)
|
||||
let ast = Factory::from_type_info(type_info).create_anonymous(code);
|
||||
|
||||
ast.write(&mut Manager::function(&ast), w)
|
||||
}
|
||||
|
||||
/// # Errors
|
||||
|
||||
Reference in New Issue
Block a user