2019-01-17 01:59:12 +00:00
|
|
|
use crate::{
|
|
|
|
func_env::FuncEnv,
|
|
|
|
module::{Converter, Module},
|
|
|
|
};
|
|
|
|
use cranelift_codegen::{ir, isa};
|
|
|
|
use cranelift_wasm::{self, translate_module, FuncTranslator, ModuleEnvironment};
|
2019-02-07 00:26:45 +00:00
|
|
|
use std::sync::Arc;
|
2019-01-22 19:02:06 +00:00
|
|
|
use wasmer_runtime_core::{
|
2019-01-18 18:54:16 +00:00
|
|
|
error::{CompileError, CompileResult},
|
2019-02-07 00:26:45 +00:00
|
|
|
module::{
|
|
|
|
DataInitializer, ExportIndex, ImportName, NameIndex, NamespaceIndex, StringTableBuilder,
|
|
|
|
TableInitializer,
|
|
|
|
},
|
2019-01-17 01:59:12 +00:00
|
|
|
structures::{Map, TypedIndex},
|
|
|
|
types::{
|
2019-02-07 00:26:45 +00:00
|
|
|
ElementType, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer, LocalFuncIndex,
|
|
|
|
LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value,
|
2019-01-17 01:59:12 +00:00
|
|
|
},
|
2019-01-29 23:44:15 +00:00
|
|
|
units::Pages,
|
2019-01-17 01:59:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct ModuleEnv<'module, 'isa> {
|
|
|
|
pub module: &'module mut Module,
|
|
|
|
isa: &'isa isa::TargetIsa,
|
|
|
|
pub signatures: Map<SigIndex, ir::Signature>,
|
|
|
|
globals: Map<GlobalIndex, cranelift_wasm::Global>,
|
|
|
|
func_bodies: Map<LocalFuncIndex, ir::Function>,
|
2019-02-07 00:26:45 +00:00
|
|
|
namespace_table_builder: StringTableBuilder<NamespaceIndex>,
|
|
|
|
name_table_builder: StringTableBuilder<NameIndex>,
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'module, 'isa> ModuleEnv<'module, 'isa> {
|
|
|
|
pub fn new(module: &'module mut Module, isa: &'isa isa::TargetIsa) -> Self {
|
|
|
|
Self {
|
|
|
|
module,
|
|
|
|
isa,
|
|
|
|
signatures: Map::new(),
|
|
|
|
globals: Map::new(),
|
|
|
|
func_bodies: Map::new(),
|
2019-02-07 00:26:45 +00:00
|
|
|
namespace_table_builder: StringTableBuilder::new(),
|
|
|
|
name_table_builder: StringTableBuilder::new(),
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:54:16 +00:00
|
|
|
pub fn translate(mut self, wasm: &[u8]) -> CompileResult<Map<LocalFuncIndex, ir::Function>> {
|
|
|
|
translate_module(wasm, &mut self)
|
|
|
|
.map_err(|e| CompileError::InternalError { msg: e.to_string() })?;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
self.module.info.namespace_table = self.namespace_table_builder.finish();
|
|
|
|
self.module.info.name_table = self.name_table_builder.finish();
|
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
Ok(self.func_bodies)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> {
|
|
|
|
/// Get the information needed to produce Cranelift IR for the current target.
|
|
|
|
fn target_config(&self) -> isa::TargetFrontendConfig {
|
|
|
|
self.isa.frontend_config()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a function signature to the environment.
|
|
|
|
fn declare_signature(&mut self, sig: &ir::Signature) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.signatures.push(sig.clone());
|
|
|
|
self.module
|
|
|
|
.info
|
|
|
|
.signatures
|
|
|
|
.push(Arc::new(Converter(sig).into()));
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the signature with the given index.
|
2019-01-29 18:16:39 +00:00
|
|
|
fn get_signature(&self, clif_sig_index: cranelift_wasm::SignatureIndex) -> &ir::Signature {
|
|
|
|
&self.signatures[Converter(clif_sig_index).into()]
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a function import to the environment.
|
|
|
|
fn declare_func_import(
|
|
|
|
&mut self,
|
2019-01-29 18:16:39 +00:00
|
|
|
clif_sig_index: cranelift_wasm::SignatureIndex,
|
2019-01-17 01:59:12 +00:00
|
|
|
namespace: &'data str,
|
|
|
|
name: &'data str,
|
|
|
|
) {
|
2019-02-07 00:26:45 +00:00
|
|
|
// We convert the cranelift signature index to
|
|
|
|
// a wasmer signature index without deduplicating
|
|
|
|
// because we'll deduplicate later.
|
|
|
|
let sig_index = Converter(clif_sig_index).into();
|
|
|
|
self.module.info.func_assoc.push(sig_index);
|
|
|
|
|
|
|
|
let namespace_index = self.namespace_table_builder.register(namespace);
|
|
|
|
let name_index = self.name_table_builder.register(name);
|
2019-01-17 01:59:12 +00:00
|
|
|
|
|
|
|
// Add import names to list of imported functions
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.imported_functions.push(ImportName {
|
|
|
|
namespace_index,
|
|
|
|
name_index,
|
2019-01-17 01:59:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the number of imported funcs.
|
|
|
|
fn get_num_func_imports(&self) -> usize {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.imported_functions.len()
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares the type (signature) of a local function in the module.
|
2019-01-29 18:16:39 +00:00
|
|
|
fn declare_func_type(&mut self, clif_sig_index: cranelift_wasm::SignatureIndex) {
|
2019-02-07 00:26:45 +00:00
|
|
|
// We convert the cranelift signature index to
|
|
|
|
// a wasmer signature index without deduplicating
|
|
|
|
// because we'll deduplicate later.
|
|
|
|
let sig_index = Converter(clif_sig_index).into();
|
|
|
|
self.module.info.func_assoc.push(sig_index);
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the signature index for the given function index.
|
|
|
|
fn get_func_type(
|
|
|
|
&self,
|
|
|
|
func_index: cranelift_wasm::FuncIndex,
|
|
|
|
) -> cranelift_wasm::SignatureIndex {
|
2019-02-07 00:26:45 +00:00
|
|
|
let sig_index: SigIndex = self.module.info.func_assoc[Converter(func_index).into()];
|
|
|
|
Converter(sig_index).into()
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a global to the environment.
|
|
|
|
fn declare_global(&mut self, global: cranelift_wasm::Global) {
|
2019-01-29 21:04:42 +00:00
|
|
|
let desc = GlobalDescriptor {
|
2019-01-17 01:59:12 +00:00
|
|
|
mutable: global.mutability,
|
|
|
|
ty: Converter(global.ty).into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let init = match global.initializer {
|
2019-01-28 19:55:44 +00:00
|
|
|
cranelift_wasm::GlobalInit::I32Const(x) => Initializer::Const(Value::I32(x)),
|
|
|
|
cranelift_wasm::GlobalInit::I64Const(x) => Initializer::Const(Value::I64(x)),
|
|
|
|
cranelift_wasm::GlobalInit::F32Const(x) => {
|
|
|
|
Initializer::Const(Value::F32(f32::from_bits(x)))
|
|
|
|
}
|
|
|
|
cranelift_wasm::GlobalInit::F64Const(x) => {
|
|
|
|
Initializer::Const(Value::F64(f64::from_bits(x)))
|
|
|
|
}
|
|
|
|
cranelift_wasm::GlobalInit::GetGlobal(global_index) => {
|
2019-02-01 22:00:07 +00:00
|
|
|
// assert!(!desc.mutable);
|
2019-01-17 01:59:12 +00:00
|
|
|
let global_index: GlobalIndex = Converter(global_index).into();
|
|
|
|
let imported_global_index = global_index
|
|
|
|
.local_or_import(self.module)
|
|
|
|
.import()
|
|
|
|
.expect("invalid global initializer when declaring an imported global");
|
|
|
|
Initializer::GetGlobal(imported_global_index)
|
|
|
|
}
|
|
|
|
_ => panic!("invalid global initializer when declaring a local global"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add global ir to the list of globals
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.globals.push(GlobalInit { desc, init });
|
2019-01-17 01:59:12 +00:00
|
|
|
|
|
|
|
self.globals.push(global);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a global import to the environment.
|
|
|
|
fn declare_global_import(
|
|
|
|
&mut self,
|
|
|
|
global: cranelift_wasm::Global,
|
|
|
|
namespace: &'data str,
|
|
|
|
name: &'data str,
|
|
|
|
) {
|
|
|
|
assert!(match global.initializer {
|
|
|
|
cranelift_wasm::GlobalInit::Import => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
let namespace_index = self.namespace_table_builder.register(namespace);
|
|
|
|
let name_index = self.name_table_builder.register(name);
|
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
let import_name = ImportName {
|
2019-02-07 00:26:45 +00:00
|
|
|
namespace_index,
|
|
|
|
name_index,
|
2019-01-17 01:59:12 +00:00
|
|
|
};
|
|
|
|
|
2019-01-29 21:04:42 +00:00
|
|
|
let desc = GlobalDescriptor {
|
2019-01-17 01:59:12 +00:00
|
|
|
mutable: global.mutability,
|
|
|
|
ty: Converter(global.ty).into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add global ir to the list of globals
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.imported_globals.push((import_name, desc));
|
2019-01-17 01:59:12 +00:00
|
|
|
|
|
|
|
self.globals.push(global);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the global for the given global index.
|
|
|
|
fn get_global(&self, global_index: cranelift_wasm::GlobalIndex) -> &cranelift_wasm::Global {
|
|
|
|
&self.globals[Converter(global_index).into()]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a table to the environment.
|
|
|
|
fn declare_table(&mut self, table: cranelift_wasm::Table) {
|
|
|
|
use cranelift_wasm::TableElementType;
|
|
|
|
// Add table ir to the list of tables
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.tables.push(TableDescriptor {
|
2019-01-29 21:04:42 +00:00
|
|
|
element: match table.ty {
|
2019-01-17 01:59:12 +00:00
|
|
|
TableElementType::Func => ElementType::Anyfunc,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
},
|
2019-01-29 22:15:59 +00:00
|
|
|
minimum: table.minimum,
|
|
|
|
maximum: table.maximum,
|
2019-01-17 01:59:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a table import to the environment.
|
|
|
|
fn declare_table_import(
|
|
|
|
&mut self,
|
|
|
|
table: cranelift_wasm::Table,
|
|
|
|
namespace: &'data str,
|
|
|
|
name: &'data str,
|
|
|
|
) {
|
|
|
|
use cranelift_wasm::TableElementType;
|
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
let namespace_index = self.namespace_table_builder.register(namespace);
|
|
|
|
let name_index = self.name_table_builder.register(name);
|
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
let import_name = ImportName {
|
2019-02-07 00:26:45 +00:00
|
|
|
namespace_index,
|
|
|
|
name_index,
|
2019-01-17 01:59:12 +00:00
|
|
|
};
|
|
|
|
|
2019-01-29 21:04:42 +00:00
|
|
|
let imported_table = TableDescriptor {
|
|
|
|
element: match table.ty {
|
2019-01-17 01:59:12 +00:00
|
|
|
TableElementType::Func => ElementType::Anyfunc,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
},
|
2019-01-29 22:15:59 +00:00
|
|
|
minimum: table.minimum,
|
|
|
|
maximum: table.maximum,
|
2019-01-17 01:59:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add import names to list of imported tables
|
|
|
|
self.module
|
2019-02-07 00:26:45 +00:00
|
|
|
.info
|
2019-01-17 01:59:12 +00:00
|
|
|
.imported_tables
|
|
|
|
.push((import_name, imported_table));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fills a declared table with references to functions in the module.
|
|
|
|
fn declare_table_elements(
|
|
|
|
&mut self,
|
|
|
|
table_index: cranelift_wasm::TableIndex,
|
|
|
|
base: Option<cranelift_wasm::GlobalIndex>,
|
|
|
|
offset: usize,
|
|
|
|
elements: Vec<cranelift_wasm::FuncIndex>,
|
|
|
|
) {
|
|
|
|
// Convert Cranelift GlobalIndex to wamser GlobalIndex
|
|
|
|
// let base = base.map(|index| WasmerGlobalIndex::new(index.index()));
|
|
|
|
let base = match base {
|
|
|
|
Some(global_index) => {
|
|
|
|
let global_index: GlobalIndex = Converter(global_index).into();
|
|
|
|
Initializer::GetGlobal(match global_index.local_or_import(self.module) {
|
|
|
|
LocalOrImport::Import(imported_global_index) => imported_global_index,
|
|
|
|
LocalOrImport::Local(_) => {
|
|
|
|
panic!("invalid global initializer when declaring an imported global")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None => Initializer::Const((offset as i32).into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add table initializer to list of table initializers
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.elem_initializers.push(TableInitializer {
|
2019-01-17 01:59:12 +00:00
|
|
|
table_index: Converter(table_index).into(),
|
|
|
|
base,
|
|
|
|
elements: elements
|
|
|
|
.iter()
|
|
|
|
.map(|&func_index| Converter(func_index).into())
|
|
|
|
.collect(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a memory to the environment
|
|
|
|
fn declare_memory(&mut self, memory: cranelift_wasm::Memory) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.memories.push(MemoryDescriptor {
|
2019-01-29 23:44:15 +00:00
|
|
|
minimum: Pages(memory.minimum),
|
|
|
|
maximum: memory.maximum.map(|max| Pages(max)),
|
2019-01-17 01:59:12 +00:00
|
|
|
shared: memory.shared,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a memory import to the environment.
|
|
|
|
fn declare_memory_import(
|
|
|
|
&mut self,
|
|
|
|
memory: cranelift_wasm::Memory,
|
|
|
|
namespace: &'data str,
|
|
|
|
name: &'data str,
|
|
|
|
) {
|
2019-02-07 00:26:45 +00:00
|
|
|
let namespace_index = self.namespace_table_builder.register(namespace);
|
|
|
|
let name_index = self.name_table_builder.register(name);
|
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
let import_name = ImportName {
|
2019-02-07 00:26:45 +00:00
|
|
|
namespace_index,
|
|
|
|
name_index,
|
2019-01-17 01:59:12 +00:00
|
|
|
};
|
|
|
|
|
2019-01-29 21:04:42 +00:00
|
|
|
let memory = MemoryDescriptor {
|
2019-01-29 23:44:15 +00:00
|
|
|
minimum: Pages(memory.minimum),
|
|
|
|
maximum: memory.maximum.map(|max| Pages(max)),
|
2019-01-17 01:59:12 +00:00
|
|
|
shared: memory.shared,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add import names to list of imported memories
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module
|
|
|
|
.info
|
|
|
|
.imported_memories
|
|
|
|
.push((import_name, memory));
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fills a declared memory with bytes at module instantiation.
|
|
|
|
fn declare_data_initialization(
|
|
|
|
&mut self,
|
|
|
|
memory_index: cranelift_wasm::MemoryIndex,
|
|
|
|
base: Option<cranelift_wasm::GlobalIndex>,
|
|
|
|
offset: usize,
|
|
|
|
data: &'data [u8],
|
|
|
|
) {
|
|
|
|
// Convert Cranelift GlobalIndex to wamser GlobalIndex
|
|
|
|
let base = match base {
|
|
|
|
Some(global_index) => {
|
|
|
|
let global_index: GlobalIndex = Converter(global_index).into();
|
|
|
|
Initializer::GetGlobal(match global_index.local_or_import(self.module) {
|
|
|
|
LocalOrImport::Import(imported_global_index) => imported_global_index,
|
|
|
|
LocalOrImport::Local(_) => {
|
|
|
|
panic!("invalid global initializer when declaring an imported global")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None => Initializer::Const((offset as i32).into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add data initializer to list of data initializers
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.data_initializers.push(DataInitializer {
|
2019-01-17 01:59:12 +00:00
|
|
|
memory_index: Converter(memory_index).into(),
|
|
|
|
base,
|
|
|
|
data: data.to_vec(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a function export to the environment.
|
|
|
|
fn declare_func_export(&mut self, func_index: cranelift_wasm::FuncIndex, name: &'data str) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.exports.insert(
|
2019-01-17 01:59:12 +00:00
|
|
|
name.to_string(),
|
|
|
|
ExportIndex::Func(Converter(func_index).into()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/// Declares a table export to the environment.
|
|
|
|
fn declare_table_export(&mut self, table_index: cranelift_wasm::TableIndex, name: &'data str) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.exports.insert(
|
2019-01-17 01:59:12 +00:00
|
|
|
name.to_string(),
|
|
|
|
ExportIndex::Table(Converter(table_index).into()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/// Declares a memory export to the environment.
|
|
|
|
fn declare_memory_export(
|
|
|
|
&mut self,
|
|
|
|
memory_index: cranelift_wasm::MemoryIndex,
|
|
|
|
name: &'data str,
|
|
|
|
) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.exports.insert(
|
2019-01-17 01:59:12 +00:00
|
|
|
name.to_string(),
|
|
|
|
ExportIndex::Memory(Converter(memory_index).into()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/// Declares a global export to the environment.
|
|
|
|
fn declare_global_export(
|
|
|
|
&mut self,
|
|
|
|
global_index: cranelift_wasm::GlobalIndex,
|
|
|
|
name: &'data str,
|
|
|
|
) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.exports.insert(
|
2019-01-17 01:59:12 +00:00
|
|
|
name.to_string(),
|
|
|
|
ExportIndex::Global(Converter(global_index).into()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a start function.
|
|
|
|
fn declare_start_func(&mut self, func_index: cranelift_wasm::FuncIndex) {
|
2019-02-07 00:26:45 +00:00
|
|
|
self.module.info.start_func = Some(Converter(func_index).into());
|
2019-01-17 01:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provides the contents of a function body.
|
|
|
|
fn define_function_body(&mut self, body_bytes: &'data [u8]) -> cranelift_wasm::WasmResult<()> {
|
|
|
|
let mut func_translator = FuncTranslator::new();
|
|
|
|
|
|
|
|
let func_body = {
|
|
|
|
let mut func_env = FuncEnv::new(self);
|
|
|
|
let func_index = self.func_bodies.next_index();
|
|
|
|
let name = ir::ExternalName::user(0, func_index.index() as u32);
|
|
|
|
|
|
|
|
let sig = func_env.generate_signature(
|
|
|
|
self.get_func_type(Converter(func_index.convert_up(self.module)).into()),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut func = ir::Function::with_name_signature(name, sig);
|
|
|
|
|
|
|
|
func_translator.translate(body_bytes, &mut func, &mut func_env)?;
|
|
|
|
|
2019-02-06 23:43:27 +00:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
{
|
|
|
|
use cranelift_codegen::cursor::{Cursor, FuncCursor};
|
|
|
|
use cranelift_codegen::ir::InstBuilder;
|
|
|
|
let entry_ebb = func.layout.entry_block().unwrap();
|
|
|
|
let ebb = func.dfg.make_ebb();
|
|
|
|
func.layout.insert_ebb(ebb, entry_ebb);
|
|
|
|
let mut pos = FuncCursor::new(&mut func).at_first_insertion_point(ebb);
|
|
|
|
let params = pos.func.dfg.ebb_params(entry_ebb).to_vec();
|
|
|
|
|
|
|
|
let new_ebb_params: Vec<_> = params
|
|
|
|
.iter()
|
|
|
|
.map(|¶m| {
|
|
|
|
pos.func
|
|
|
|
.dfg
|
|
|
|
.append_ebb_param(ebb, pos.func.dfg.value_type(param))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let start_debug = {
|
|
|
|
let signature = pos.func.import_signature(ir::Signature {
|
|
|
|
call_conv: self.target_config().default_call_conv,
|
|
|
|
params: vec![
|
|
|
|
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
|
2019-02-12 18:49:43 +00:00
|
|
|
ir::AbiParam::new(ir::types::I32),
|
2019-02-06 23:43:27 +00:00
|
|
|
],
|
|
|
|
returns: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
let name = ir::ExternalName::testcase("strtdbug");
|
|
|
|
|
|
|
|
pos.func.import_function(ir::ExtFuncData {
|
|
|
|
name,
|
|
|
|
signature,
|
|
|
|
colocated: false,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let end_debug = {
|
|
|
|
let signature = pos.func.import_signature(ir::Signature {
|
|
|
|
call_conv: self.target_config().default_call_conv,
|
|
|
|
params: vec![ir::AbiParam::special(
|
|
|
|
ir::types::I64,
|
|
|
|
ir::ArgumentPurpose::VMContext,
|
|
|
|
)],
|
|
|
|
returns: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
let name = ir::ExternalName::testcase("enddbug");
|
|
|
|
|
|
|
|
pos.func.import_function(ir::ExtFuncData {
|
|
|
|
name,
|
|
|
|
signature,
|
|
|
|
colocated: false,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let i32_print = {
|
|
|
|
let signature = pos.func.import_signature(ir::Signature {
|
|
|
|
call_conv: self.target_config().default_call_conv,
|
|
|
|
params: vec![
|
|
|
|
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
|
2019-02-12 18:49:43 +00:00
|
|
|
ir::AbiParam::new(ir::types::I32),
|
2019-02-06 23:43:27 +00:00
|
|
|
],
|
|
|
|
returns: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
let name = ir::ExternalName::testcase("i32print");
|
|
|
|
|
|
|
|
pos.func.import_function(ir::ExtFuncData {
|
|
|
|
name,
|
|
|
|
signature,
|
|
|
|
colocated: false,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let i64_print = {
|
|
|
|
let signature = pos.func.import_signature(ir::Signature {
|
|
|
|
call_conv: self.target_config().default_call_conv,
|
|
|
|
params: vec![
|
|
|
|
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
|
2019-02-12 18:49:43 +00:00
|
|
|
ir::AbiParam::new(ir::types::I64),
|
2019-02-06 23:43:27 +00:00
|
|
|
],
|
|
|
|
returns: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
let name = ir::ExternalName::testcase("i64print");
|
|
|
|
|
|
|
|
pos.func.import_function(ir::ExtFuncData {
|
|
|
|
name,
|
|
|
|
signature,
|
|
|
|
colocated: false,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let f32_print = {
|
|
|
|
let signature = pos.func.import_signature(ir::Signature {
|
|
|
|
call_conv: self.target_config().default_call_conv,
|
|
|
|
params: vec![
|
|
|
|
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
|
2019-02-12 18:49:43 +00:00
|
|
|
ir::AbiParam::new(ir::types::F32),
|
2019-02-06 23:43:27 +00:00
|
|
|
],
|
|
|
|
returns: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
let name = ir::ExternalName::testcase("f32print");
|
|
|
|
|
|
|
|
pos.func.import_function(ir::ExtFuncData {
|
|
|
|
name,
|
|
|
|
signature,
|
|
|
|
colocated: false,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let f64_print = {
|
|
|
|
let signature = pos.func.import_signature(ir::Signature {
|
|
|
|
call_conv: self.target_config().default_call_conv,
|
|
|
|
params: vec![
|
|
|
|
ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext),
|
2019-02-12 18:49:43 +00:00
|
|
|
ir::AbiParam::new(ir::types::F64),
|
2019-02-06 23:43:27 +00:00
|
|
|
],
|
|
|
|
returns: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
let name = ir::ExternalName::testcase("f64print");
|
|
|
|
|
|
|
|
pos.func.import_function(ir::ExtFuncData {
|
|
|
|
name,
|
|
|
|
signature,
|
|
|
|
colocated: false,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let vmctx = pos
|
|
|
|
.func
|
|
|
|
.special_param(ir::ArgumentPurpose::VMContext)
|
|
|
|
.expect("missing vmctx parameter");
|
|
|
|
|
|
|
|
let func_index = pos.ins().iconst(ir::types::I32, func_index.index() as i64);
|
|
|
|
|
2019-02-09 22:18:53 +00:00
|
|
|
pos.ins().call(start_debug, &[vmctx, func_index]);
|
2019-02-06 23:43:27 +00:00
|
|
|
|
|
|
|
for param in new_ebb_params.iter().cloned() {
|
|
|
|
match pos.func.dfg.value_type(param) {
|
2019-02-09 22:18:53 +00:00
|
|
|
ir::types::I32 => pos.ins().call(i32_print, &[vmctx, param]),
|
|
|
|
ir::types::I64 => pos.ins().call(i64_print, &[vmctx, param]),
|
|
|
|
ir::types::F32 => pos.ins().call(f32_print, &[vmctx, param]),
|
|
|
|
ir::types::F64 => pos.ins().call(f64_print, &[vmctx, param]),
|
2019-02-06 23:43:27 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pos.ins().call(end_debug, &[vmctx]);
|
|
|
|
|
|
|
|
pos.ins().jump(entry_ebb, new_ebb_params.as_slice());
|
|
|
|
}
|
|
|
|
|
2019-01-17 01:59:12 +00:00
|
|
|
func
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add function body to list of function bodies.
|
|
|
|
self.func_bodies.push(func_body);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|