diff --git a/lib/wasm/src/code_translator.rs b/lib/wasm/src/code_translator.rs index 152c20ca20..e7a62b65fe 100644 --- a/lib/wasm/src/code_translator.rs +++ b/lib/wasm/src/code_translator.rs @@ -380,9 +380,9 @@ pub fn translate_operator( let callee = state.pop1(); let call = environ.translate_call_indirect( builder.cursor(), - table_index as TableIndex, + TableIndex::new(table_index as usize), table, - index as SignatureIndex, + SignatureIndex::new(index as usize), sigref, callee, state.peekn(num_args), @@ -403,13 +403,13 @@ pub fn translate_operator( Operator::MemoryGrow { reserved } => { // The WebAssembly MVP only supports one linear memory, but we expect the reserved // argument to be a memory index. - let heap_index = reserved as MemoryIndex; + let heap_index = MemoryIndex::new(reserved as usize); let heap = state.get_heap(builder.func, reserved, environ); let val = state.pop1(); state.push1(environ.translate_memory_grow(builder.cursor(), heap_index, heap, val)?) } Operator::MemorySize { reserved } => { - let heap_index = reserved as MemoryIndex; + let heap_index = MemoryIndex::new(reserved as usize); let heap = state.get_heap(builder.func, reserved, environ); state.push1(environ.translate_memory_size(builder.cursor(), heap_index, heap)?); } diff --git a/lib/wasm/src/environ/dummy.rs b/lib/wasm/src/environ/dummy.rs index ae0edb96e3..5608abec6d 100644 --- a/lib/wasm/src/environ/dummy.rs +++ b/lib/wasm/src/environ/dummy.rs @@ -51,7 +51,7 @@ pub struct DummyModuleInfo { pub flags: settings::Flags, /// Signatures as provided by `declare_signature`. - pub signatures: Vec, + pub signatures: PrimaryMap, /// Module and field names of imported functions as provided by `declare_func_import`. pub imported_funcs: Vec<(String, String)>, @@ -63,13 +63,13 @@ pub struct DummyModuleInfo { pub function_bodies: PrimaryMap, /// Tables as provided by `declare_table`. - pub tables: Vec>, + pub tables: PrimaryMap>, /// Memories as provided by `declare_memory`. - pub memories: Vec>, + pub memories: PrimaryMap>, /// Globals as provided by `declare_global`. - pub globals: Vec>, + pub globals: PrimaryMap>, /// The start function. pub start_func: Option, @@ -81,13 +81,13 @@ impl DummyModuleInfo { Self { triple, flags, - signatures: Vec::new(), + signatures: PrimaryMap::new(), imported_funcs: Vec::new(), functions: PrimaryMap::new(), function_bodies: PrimaryMap::new(), - tables: Vec::new(), - memories: Vec::new(), - globals: Vec::new(), + tables: PrimaryMap::new(), + memories: PrimaryMap::new(), + globals: PrimaryMap::new(), start_func: None, } } @@ -179,7 +179,7 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ fn make_global(&mut self, func: &mut ir::Function, index: GlobalIndex) -> GlobalVariable { // Just create a dummy `vmctx` global. - let offset = ((index * 8) as i64 + 8).into(); + let offset = ((index.index() * 8) as i64 + 8).into(); let vmctx = func.create_global_value(ir::GlobalValueData::VMContext {}); let iadd = func.create_global_value(ir::GlobalValueData::IAddImm { base: vmctx, diff --git a/lib/wasm/src/sections_translator.rs b/lib/wasm/src/sections_translator.rs index 1ea3ce26ec..fa4e288d04 100644 --- a/lib/wasm/src/sections_translator.rs +++ b/lib/wasm/src/sections_translator.rs @@ -72,7 +72,11 @@ pub fn parse_import_section<'data>( // becomes a concern here. let module_name = from_utf8(module).unwrap(); let field_name = from_utf8(field).unwrap(); - environ.declare_func_import(sig as SignatureIndex, module_name, field_name); + environ.declare_func_import( + SignatureIndex::new(sig as usize), + module_name, + field_name, + ); } ParserState::ImportSectionEntry { ty: @@ -125,7 +129,7 @@ pub fn parse_function_section( loop { match *parser.read() { ParserState::FunctionSectionEntry(sigindex) => { - environ.declare_func_type(sigindex as SignatureIndex); + environ.declare_func_type(SignatureIndex::new(sigindex as usize)); } ParserState::EndSection => break, ParserState::Error(e) => return Err(WasmError::from_binary_reader_error(e)), @@ -151,12 +155,20 @@ 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 = FuncIndex::new(index as usize); + let index = index as usize; match *kind { - ExternalKind::Function => environ.declare_func_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), + ExternalKind::Function => { + environ.declare_func_export(FuncIndex::new(index), name) + } + ExternalKind::Table => { + environ.declare_table_export(TableIndex::new(index), name) + } + ExternalKind::Memory => { + environ.declare_memory_export(MemoryIndex::new(index), name) + } + ExternalKind::Global => { + environ.declare_global_export(GlobalIndex::new(index), name) + } } } ParserState::EndSection => break, @@ -235,7 +247,7 @@ pub fn parse_global_section( GlobalInit::F64Const(value.bits()) } ParserState::InitExpressionOperator(Operator::GetGlobal { global_index }) => { - GlobalInit::GlobalRef(global_index as GlobalIndex) + GlobalInit::GlobalRef(GlobalIndex::new(global_index as usize)) } ParserState::Error(e) => return Err(WasmError::from_binary_reader_error(e)), ref s => panic!("unexpected section content: {:?}", s), @@ -281,9 +293,12 @@ pub fn parse_data_section<'data>( (None, value as u32 as usize) } ParserState::InitExpressionOperator(Operator::GetGlobal { global_index }) => { - match environ.get_global(global_index as GlobalIndex).initializer { + match environ + .get_global(GlobalIndex::new(global_index as usize)) + .initializer + { GlobalInit::I32Const(value) => (None, value as u32 as usize), - GlobalInit::Import() => (Some(global_index as GlobalIndex), 0), + GlobalInit::Import() => (Some(GlobalIndex::new(global_index as usize)), 0), _ => panic!("should not happen"), } } @@ -309,7 +324,7 @@ pub fn parse_data_section<'data>( ref s => panic!("unexpected section content: {:?}", s), }; environ.declare_data_initialization( - memory_index as MemoryIndex, + MemoryIndex::new(memory_index as usize), base, running_offset, data, @@ -352,7 +367,9 @@ pub fn parse_elements_section( ) -> WasmResult<()> { loop { let table_index = match *parser.read() { - ParserState::BeginElementSectionEntry(table_index) => table_index as TableIndex, + ParserState::BeginElementSectionEntry(table_index) => { + TableIndex::new(table_index as usize) + } ParserState::EndSection => break, ParserState::Error(e) => return Err(WasmError::from_binary_reader_error(e)), ref s => panic!("unexpected section content: {:?}", s), @@ -367,9 +384,12 @@ pub fn parse_elements_section( (None, value as u32 as usize) } ParserState::InitExpressionOperator(Operator::GetGlobal { global_index }) => { - match environ.get_global(global_index as GlobalIndex).initializer { + match environ + .get_global(GlobalIndex::new(global_index as usize)) + .initializer + { GlobalInit::I32Const(value) => (None, value as u32 as usize), - GlobalInit::Import() => (Some(global_index as GlobalIndex), 0), + GlobalInit::Import() => (Some(GlobalIndex::new(global_index as usize)), 0), _ => panic!("should not happen"), } } diff --git a/lib/wasm/src/state.rs b/lib/wasm/src/state.rs index 2d6d3610af..df909d8b38 100644 --- a/lib/wasm/src/state.rs +++ b/lib/wasm/src/state.rs @@ -287,7 +287,7 @@ impl TranslationState { index: u32, environ: &mut FE, ) -> GlobalVariable { - let index = index as GlobalIndex; + let index = GlobalIndex::new(index as usize); *self .globals .entry(index) @@ -302,7 +302,7 @@ impl TranslationState { index: u32, environ: &mut FE, ) -> ir::Heap { - let index = index as MemoryIndex; + let index = MemoryIndex::new(index as usize); *self .heaps .entry(index) @@ -317,7 +317,7 @@ impl TranslationState { index: u32, environ: &mut FE, ) -> ir::Table { - let index = index as TableIndex; + let index = TableIndex::new(index as usize); *self .tables .entry(index) @@ -334,7 +334,7 @@ impl TranslationState { index: u32, environ: &mut FE, ) -> (ir::SigRef, usize) { - let index = index as SignatureIndex; + let index = SignatureIndex::new(index as usize); *self.signatures.entry(index).or_insert_with(|| { let sig = environ.make_indirect_sig(func, index); (sig, normal_args(&func.dfg.signatures[sig])) diff --git a/lib/wasm/src/translation_utils.rs b/lib/wasm/src/translation_utils.rs index 5d40539720..d76d55494f 100644 --- a/lib/wasm/src/translation_utils.rs +++ b/lib/wasm/src/translation_utils.rs @@ -13,14 +13,25 @@ entity_impl!(FuncIndex); 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. -pub type GlobalIndex = usize; -/// Index of a linear memory (imported or defined) inside the WebAssembly module. -pub type MemoryIndex = usize; -/// Index of a signature (imported or defined) inside the WebAssembly module. -pub type SignatureIndex = usize; +/// Index type of a table (imported or defined) inside the WebAssembly module. +#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] +pub struct TableIndex(u32); +entity_impl!(TableIndex); + +/// Index type of a global variable (imported or defined) inside the WebAssembly module. +#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] +pub struct GlobalIndex(u32); +entity_impl!(GlobalIndex); + +/// Index type of a linear memory (imported or defined) inside the WebAssembly module. +#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] +pub struct MemoryIndex(u32); +entity_impl!(MemoryIndex); + +/// Index type of a signature (imported or defined) inside the WebAssembly module. +#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] +pub struct SignatureIndex(u32); +entity_impl!(SignatureIndex); /// WebAssembly global. #[derive(Debug, Clone, Copy)]