Use types to distinguish between wasm function body indices and wasm function indices.
This commit is contained in:
@@ -26,6 +26,7 @@ use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
|
||||
use cranelift_codegen::ir::types::*;
|
||||
use cranelift_codegen::ir::{self, InstBuilder, JumpTableData, MemFlags};
|
||||
use cranelift_codegen::packed_option::ReservedValue;
|
||||
use cranelift_entity::EntityRef;
|
||||
use cranelift_frontend::{FunctionBuilder, Variable};
|
||||
use environ::{FuncEnvironment, GlobalVariable, WasmError, WasmResult};
|
||||
use state::{ControlStackFrame, TranslationState};
|
||||
@@ -33,7 +34,7 @@ use std::collections::{hash_map, HashMap};
|
||||
use std::vec::Vec;
|
||||
use std::{i32, u32};
|
||||
use translation_utils::{f32_translation, f64_translation, num_return_values, type_to_type};
|
||||
use translation_utils::{FunctionIndex, MemoryIndex, SignatureIndex, TableIndex};
|
||||
use translation_utils::{FuncIndex, MemoryIndex, SignatureIndex, TableIndex};
|
||||
use wasmparser::{MemoryImmediate, Operator};
|
||||
|
||||
// Clippy warns about "flags: _" but its important to document that the flags field is ignored
|
||||
@@ -358,7 +359,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
|
||||
let (fref, num_args) = state.get_direct_func(builder.func, function_index, environ);
|
||||
let call = environ.translate_call(
|
||||
builder.cursor(),
|
||||
function_index as FunctionIndex,
|
||||
FuncIndex::new(function_index as usize),
|
||||
fref,
|
||||
state.peekn(num_args),
|
||||
)?;
|
||||
|
||||
@@ -5,19 +5,20 @@ use cranelift_codegen::ir::immediates::Imm64;
|
||||
use cranelift_codegen::ir::types::*;
|
||||
use cranelift_codegen::ir::{self, InstBuilder};
|
||||
use cranelift_codegen::settings;
|
||||
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||
use environ::{FuncEnvironment, GlobalVariable, ModuleEnvironment, WasmResult};
|
||||
use func_translator::FuncTranslator;
|
||||
use std::string::String;
|
||||
use std::vec::Vec;
|
||||
use target_lexicon::Triple;
|
||||
use translation_utils::{
|
||||
FunctionIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
|
||||
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
|
||||
};
|
||||
use wasmparser;
|
||||
|
||||
/// Compute a `ir::ExternalName` for a given wasm function index.
|
||||
fn get_func_name(func_index: FunctionIndex) -> ir::ExternalName {
|
||||
ir::ExternalName::user(0, func_index as u32)
|
||||
fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
|
||||
ir::ExternalName::user(0, func_index.index() as u32)
|
||||
}
|
||||
|
||||
/// A collection of names under which a given entity is exported.
|
||||
@@ -55,10 +56,10 @@ pub struct DummyModuleInfo {
|
||||
pub imported_funcs: Vec<(String, String)>,
|
||||
|
||||
/// Functions, imported and local.
|
||||
pub functions: Vec<Exportable<SignatureIndex>>,
|
||||
pub functions: PrimaryMap<FuncIndex, Exportable<SignatureIndex>>,
|
||||
|
||||
/// Function bodies.
|
||||
pub function_bodies: Vec<ir::Function>,
|
||||
pub function_bodies: PrimaryMap<DefinedFuncIndex, ir::Function>,
|
||||
|
||||
/// Tables as provided by `declare_table`.
|
||||
pub tables: Vec<Exportable<Table>>,
|
||||
@@ -70,7 +71,7 @@ pub struct DummyModuleInfo {
|
||||
pub globals: Vec<Exportable<Global>>,
|
||||
|
||||
/// The start function.
|
||||
pub start_func: Option<FunctionIndex>,
|
||||
pub start_func: Option<DefinedFuncIndex>,
|
||||
}
|
||||
|
||||
impl DummyModuleInfo {
|
||||
@@ -81,8 +82,8 @@ impl DummyModuleInfo {
|
||||
flags,
|
||||
signatures: Vec::new(),
|
||||
imported_funcs: Vec::new(),
|
||||
functions: Vec::new(),
|
||||
function_bodies: Vec::new(),
|
||||
functions: PrimaryMap::new(),
|
||||
function_bodies: PrimaryMap::new(),
|
||||
tables: Vec::new(),
|
||||
memories: Vec::new(),
|
||||
globals: Vec::new(),
|
||||
@@ -215,7 +216,7 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
|
||||
func.import_signature(self.vmctx_sig(index))
|
||||
}
|
||||
|
||||
fn make_direct_func(&mut self, func: &mut ir::Function, index: FunctionIndex) -> ir::FuncRef {
|
||||
fn make_direct_func(&mut self, func: &mut ir::Function, index: FuncIndex) -> ir::FuncRef {
|
||||
let sigidx = self.mod_info.functions[index].entity;
|
||||
// A real implementation would probably add a `vmctx` argument.
|
||||
// And maybe attempt some signature de-duplication.
|
||||
@@ -275,7 +276,7 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
|
||||
fn translate_call(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
_callee_index: FunctionIndex,
|
||||
_callee_index: FuncIndex,
|
||||
callee: ir::FuncRef,
|
||||
call_args: &[ir::Value],
|
||||
) -> WasmResult<ir::Inst> {
|
||||
@@ -319,7 +320,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
|
||||
&self.info.flags
|
||||
}
|
||||
|
||||
fn get_func_name(&self, func_index: FunctionIndex) -> ir::ExternalName {
|
||||
fn get_func_name(&self, func_index: FuncIndex) -> ir::ExternalName {
|
||||
get_func_name(func_index)
|
||||
}
|
||||
|
||||
@@ -356,7 +357,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
|
||||
self.info.functions.push(Exportable::new(sig_index));
|
||||
}
|
||||
|
||||
fn get_func_type(&self, func_index: FunctionIndex) -> SignatureIndex {
|
||||
fn get_func_type(&self, func_index: FuncIndex) -> SignatureIndex {
|
||||
self.info.functions[func_index].entity
|
||||
}
|
||||
|
||||
@@ -376,7 +377,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
|
||||
_table_index: TableIndex,
|
||||
_base: Option<GlobalIndex>,
|
||||
_offset: usize,
|
||||
_elements: Vec<FunctionIndex>,
|
||||
_elements: Vec<FuncIndex>,
|
||||
) {
|
||||
// We do nothing
|
||||
}
|
||||
@@ -393,7 +394,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
|
||||
// We do nothing
|
||||
}
|
||||
|
||||
fn declare_func_export(&mut self, func_index: FunctionIndex, name: &'data str) {
|
||||
fn declare_func_export(&mut self, func_index: FuncIndex, name: &'data str) {
|
||||
self.info.functions[func_index]
|
||||
.export_names
|
||||
.push(String::from(name));
|
||||
@@ -417,7 +418,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
|
||||
.push(String::from(name));
|
||||
}
|
||||
|
||||
fn declare_start_func(&mut self, func_index: FunctionIndex) {
|
||||
fn declare_start_func(&mut self, func_index: DefinedFuncIndex) {
|
||||
debug_assert!(self.info.start_func.is_none());
|
||||
self.info.start_func = Some(func_index);
|
||||
}
|
||||
@@ -425,9 +426,9 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
|
||||
fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()> {
|
||||
let func = {
|
||||
let mut func_environ = DummyFuncEnvironment::new(&self.info);
|
||||
let function_index = self.get_num_func_imports() + self.info.function_bodies.len();
|
||||
let name = get_func_name(function_index);
|
||||
let sig = func_environ.vmctx_sig(self.get_func_type(function_index));
|
||||
let func_index = FuncIndex::new(self.get_num_func_imports() + self.info.function_bodies.len());
|
||||
let name = get_func_name(func_index);
|
||||
let sig = func_environ.vmctx_sig(self.get_func_type(func_index));
|
||||
let mut func = ir::Function::with_name_signature(name, sig);
|
||||
let reader = wasmparser::BinaryReader::new(body_bytes);
|
||||
self.trans
|
||||
|
||||
@@ -6,7 +6,7 @@ use cranelift_codegen::settings::Flags;
|
||||
use std::vec::Vec;
|
||||
use target_lexicon::Triple;
|
||||
use translation_utils::{
|
||||
FunctionIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
|
||||
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
|
||||
};
|
||||
use wasmparser::BinaryReaderError;
|
||||
|
||||
@@ -137,7 +137,7 @@ pub trait FuncEnvironment {
|
||||
///
|
||||
/// The function's signature will only be used for direct calls, even if the module has
|
||||
/// indirect calls with the same WebAssembly type.
|
||||
fn make_direct_func(&mut self, func: &mut ir::Function, index: FunctionIndex) -> ir::FuncRef;
|
||||
fn make_direct_func(&mut self, func: &mut ir::Function, index: FuncIndex) -> ir::FuncRef;
|
||||
|
||||
/// Translate a `call_indirect` WebAssembly instruction at `pos`.
|
||||
///
|
||||
@@ -169,7 +169,7 @@ pub trait FuncEnvironment {
|
||||
fn translate_call(
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
_callee_index: FunctionIndex,
|
||||
_callee_index: FuncIndex,
|
||||
callee: ir::FuncRef,
|
||||
call_args: &[ir::Value],
|
||||
) -> WasmResult<ir::Inst> {
|
||||
@@ -222,7 +222,7 @@ pub trait ModuleEnvironment<'data> {
|
||||
fn flags(&self) -> &Flags;
|
||||
|
||||
/// Return the name for the given function index.
|
||||
fn get_func_name(&self, func_index: FunctionIndex) -> ir::ExternalName;
|
||||
fn get_func_name(&self, func_index: FuncIndex) -> ir::ExternalName;
|
||||
|
||||
/// Declares a function signature to the environment.
|
||||
fn declare_signature(&mut self, sig: &ir::Signature);
|
||||
@@ -245,7 +245,7 @@ pub trait ModuleEnvironment<'data> {
|
||||
fn declare_func_type(&mut self, sig_index: SignatureIndex);
|
||||
|
||||
/// Return the signature index for the given function index.
|
||||
fn get_func_type(&self, func_index: FunctionIndex) -> SignatureIndex;
|
||||
fn get_func_type(&self, func_index: FuncIndex) -> SignatureIndex;
|
||||
|
||||
/// Declares a global to the environment.
|
||||
fn declare_global(&mut self, global: Global);
|
||||
@@ -261,7 +261,7 @@ pub trait ModuleEnvironment<'data> {
|
||||
table_index: TableIndex,
|
||||
base: Option<GlobalIndex>,
|
||||
offset: usize,
|
||||
elements: Vec<FunctionIndex>,
|
||||
elements: Vec<FuncIndex>,
|
||||
);
|
||||
/// Declares a memory to the environment
|
||||
fn declare_memory(&mut self, memory: Memory);
|
||||
@@ -275,7 +275,7 @@ pub trait ModuleEnvironment<'data> {
|
||||
);
|
||||
|
||||
/// Declares a function export to the environment.
|
||||
fn declare_func_export(&mut self, func_index: FunctionIndex, name: &'data str);
|
||||
fn declare_func_export(&mut self, func_index: FuncIndex, name: &'data str);
|
||||
/// Declares a table export to the environment.
|
||||
fn declare_table_export(&mut self, table_index: TableIndex, name: &'data str);
|
||||
/// Declares a memory export to the environment.
|
||||
@@ -284,7 +284,7 @@ pub trait ModuleEnvironment<'data> {
|
||||
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &'data str);
|
||||
|
||||
/// Declares a start function.
|
||||
fn declare_start_func(&mut self, index: FunctionIndex);
|
||||
fn declare_start_func(&mut self, index: DefinedFuncIndex);
|
||||
|
||||
/// Provides the contents of a function body.
|
||||
fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()>;
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
|
||||
#[macro_use(dbg)]
|
||||
extern crate cranelift_codegen;
|
||||
#[macro_use]
|
||||
extern crate cranelift_entity;
|
||||
extern crate cranelift_frontend;
|
||||
extern crate target_lexicon;
|
||||
extern crate wasmparser;
|
||||
@@ -48,7 +50,7 @@ pub use environ::{
|
||||
pub use func_translator::FuncTranslator;
|
||||
pub use module_translator::translate_module;
|
||||
pub use translation_utils::{
|
||||
FunctionIndex, Global, GlobalIndex, GlobalInit, Memory, MemoryIndex, SignatureIndex, Table,
|
||||
DefinedFuncIndex, FuncIndex, Global, GlobalIndex, GlobalInit, Memory, MemoryIndex, SignatureIndex, Table,
|
||||
TableIndex,
|
||||
};
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@
|
||||
//! is handled, according to the semantics of WebAssembly, to only specific expressions that are
|
||||
//! interpreted on the fly.
|
||||
use cranelift_codegen::ir::{self, AbiParam, Signature};
|
||||
use cranelift_entity::EntityRef;
|
||||
use environ::{ModuleEnvironment, WasmError, WasmResult};
|
||||
use std::str::from_utf8;
|
||||
use std::vec::Vec;
|
||||
use translation_utils::{
|
||||
type_to_type, FunctionIndex, Global, GlobalIndex, GlobalInit, Memory, MemoryIndex,
|
||||
type_to_type, DefinedFuncIndex, FuncIndex, Global, GlobalIndex, GlobalInit, Memory, MemoryIndex,
|
||||
SignatureIndex, Table, TableElementType, TableIndex,
|
||||
};
|
||||
use wasmparser;
|
||||
@@ -150,12 +151,12 @@ pub fn parse_export_section<'data>(
|
||||
// assume valid UTF-8 and use `from_utf8_unchecked` if performance
|
||||
// becomes a concern here.
|
||||
let name = from_utf8(field).unwrap();
|
||||
let func_index = index as FunctionIndex;
|
||||
let func_index = FuncIndex::new(index as usize);
|
||||
match *kind {
|
||||
ExternalKind::Function => environ.declare_func_export(func_index, name),
|
||||
ExternalKind::Table => environ.declare_table_export(func_index, name),
|
||||
ExternalKind::Memory => environ.declare_memory_export(func_index, name),
|
||||
ExternalKind::Global => environ.declare_global_export(func_index, name),
|
||||
ExternalKind::Table => environ.declare_table_export(func_index.index(), name),
|
||||
ExternalKind::Memory => environ.declare_memory_export(func_index.index(), name),
|
||||
ExternalKind::Global => environ.declare_global_export(func_index.index(), name),
|
||||
}
|
||||
}
|
||||
ParserState::EndSection => break,
|
||||
@@ -171,7 +172,7 @@ pub fn parse_start_section(parser: &mut Parser, environ: &mut ModuleEnvironment)
|
||||
loop {
|
||||
match *parser.read() {
|
||||
ParserState::StartSectionEntry(index) => {
|
||||
environ.declare_start_func(index as FunctionIndex);
|
||||
environ.declare_start_func(DefinedFuncIndex::new(index as usize));
|
||||
}
|
||||
ParserState::EndSection => break,
|
||||
ParserState::Error(e) => return Err(WasmError::from_binary_reader_error(e)),
|
||||
@@ -382,8 +383,8 @@ pub fn parse_elements_section(
|
||||
};
|
||||
match *parser.read() {
|
||||
ParserState::ElementSectionEntryBody(ref elements) => {
|
||||
let elems: Vec<FunctionIndex> =
|
||||
elements.iter().map(|&x| x as FunctionIndex).collect();
|
||||
let elems: Vec<FuncIndex> =
|
||||
elements.iter().map(|&x| FuncIndex::new(x as usize)).collect();
|
||||
environ.declare_table_elements(table_index, base, offset, elems)
|
||||
}
|
||||
ParserState::Error(e) => return Err(WasmError::from_binary_reader_error(e)),
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
//! value and control stacks during the translation of a single function.
|
||||
|
||||
use cranelift_codegen::ir::{self, Ebb, Inst, Value};
|
||||
use cranelift_entity::EntityRef;
|
||||
use environ::{FuncEnvironment, GlobalVariable};
|
||||
use std::collections::HashMap;
|
||||
use std::vec::Vec;
|
||||
use translation_utils::{FunctionIndex, GlobalIndex, MemoryIndex, SignatureIndex, TableIndex};
|
||||
use translation_utils::{FuncIndex, GlobalIndex, MemoryIndex, SignatureIndex, TableIndex};
|
||||
|
||||
/// A control stack frame can be an `if`, a `block` or a `loop`, each one having the following
|
||||
/// fields:
|
||||
@@ -151,7 +152,7 @@ pub struct TranslationState {
|
||||
// Imported and local functions that have been created by
|
||||
// `FuncEnvironment::make_direct_func()`.
|
||||
// Stores both the function reference and the number of WebAssembly arguments
|
||||
functions: HashMap<FunctionIndex, (ir::FuncRef, usize)>,
|
||||
functions: HashMap<FuncIndex, (ir::FuncRef, usize)>,
|
||||
}
|
||||
|
||||
impl TranslationState {
|
||||
@@ -349,7 +350,7 @@ impl TranslationState {
|
||||
index: u32,
|
||||
environ: &mut FE,
|
||||
) -> (ir::FuncRef, usize) {
|
||||
let index = index as FunctionIndex;
|
||||
let index = FuncIndex::new(index as usize);
|
||||
*self.functions.entry(index).or_insert_with(|| {
|
||||
let fref = environ.make_direct_func(func, index);
|
||||
let sig = func.dfg.ext_funcs[fref].signature;
|
||||
|
||||
@@ -3,8 +3,16 @@ use cranelift_codegen::ir;
|
||||
use std::u32;
|
||||
use wasmparser;
|
||||
|
||||
/// Index of a function (imported or defined) inside the WebAssembly module.
|
||||
pub type FunctionIndex = usize;
|
||||
/// Index type of a function (imported or defined) inside the WebAssembly module.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct FuncIndex(u32);
|
||||
entity_impl!(FuncIndex);
|
||||
|
||||
/// Index type of a defined function inside the WebAssembly module.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct DefinedFuncIndex(u32);
|
||||
entity_impl!(DefinedFuncIndex);
|
||||
|
||||
/// Index of a table (imported or defined) inside the WebAssembly module.
|
||||
pub type TableIndex = usize;
|
||||
/// Index of a global variable (imported or defined) inside the WebAssembly module.
|
||||
|
||||
Reference in New Issue
Block a user