Simplify ModuleEnvironment.

Remove some unneeded functions, and remove the `GlobalInit` special case
for data and elem initializer offsets; implementations that want that
information can provide it for themselves.
This commit is contained in:
Dan Gohman
2018-12-18 12:11:32 -08:00
parent 03c22ed67a
commit 7f250e340c
4 changed files with 14 additions and 48 deletions

View File

@@ -7,18 +7,16 @@
allow(too_many_arguments, cyclomatic_complexity)
)]
use crate::utils::{parse_sets_and_triple, read_to_end};
use cranelift_codegen::print_errors::{pretty_error, pretty_verifier_error};
use cranelift_codegen::settings::FlagsOrIsa;
use cranelift_codegen::timing;
use cranelift_codegen::Context;
use cranelift_entity::EntityRef;
use cranelift_wasm::{
translate_module, DummyEnvironment, FuncIndex, ModuleEnvironment, ReturnMode,
};
use cranelift_wasm::{translate_module, DummyEnvironment, FuncIndex, ReturnMode};
use std::path::Path;
use std::path::PathBuf;
use term;
use crate::utils::{parse_sets_and_triple, read_to_end};
use wabt::wat2wasm;
macro_rules! vprintln {

View File

@@ -138,6 +138,15 @@ impl DummyEnvironment {
pub fn func_env(&self) -> DummyFuncEnvironment {
DummyFuncEnvironment::new(&self.info, self.return_mode)
}
fn get_func_type(&self, func_index: FuncIndex) -> SignatureIndex {
self.info.functions[func_index].entity
}
/// Return the number of imported functions within this `DummyEnvironment`.
pub fn get_num_func_imports(&self) -> usize {
self.info.imported_funcs.len()
}
}
/// The `FuncEnvironment` implementation for use by the `DummyEnvironment`.
@@ -345,10 +354,6 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
self.info.signatures.push(sig.clone());
}
fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature {
&self.info.signatures[sig_index]
}
fn declare_func_import(
&mut self,
sig_index: SignatureIndex,
@@ -366,18 +371,10 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
.push((String::from(module), String::from(field)));
}
fn get_num_func_imports(&self) -> usize {
self.info.imported_funcs.len()
}
fn declare_func_type(&mut self, sig_index: SignatureIndex) {
self.info.functions.push(Exportable::new(sig_index));
}
fn get_func_type(&self, func_index: FuncIndex) -> SignatureIndex {
self.info.functions[func_index].entity
}
fn declare_global(&mut self, global: Global) {
self.info.globals.push(Exportable::new(global));
}
@@ -389,10 +386,6 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
.push((String::from(module), String::from(field)));
}
fn get_global(&self, global_index: GlobalIndex) -> &Global {
&self.info.globals[global_index].entity
}
fn declare_table(&mut self, table: Table) {
self.info.tables.push(Exportable::new(table));
}

View File

@@ -248,9 +248,6 @@ pub trait ModuleEnvironment<'data> {
/// Declares a function signature to the environment.
fn declare_signature(&mut self, sig: &ir::Signature);
/// Return the signature with the given index.
fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature;
/// Declares a function import to the environment.
fn declare_func_import(
&mut self,
@@ -259,24 +256,15 @@ pub trait ModuleEnvironment<'data> {
field: &'data str,
);
/// Return the number of imported funcs.
fn get_num_func_imports(&self) -> usize;
/// Declares the type (signature) of a local function in the module.
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: FuncIndex) -> SignatureIndex;
/// Declares a global to the environment.
fn declare_global(&mut self, global: Global);
/// Declares a global import to the environment.
fn declare_global_import(&mut self, global: Global, module: &'data str, field: &'data str);
/// Return the global for the given global index.
fn get_global(&self, global_index: GlobalIndex) -> &Global;
/// Declares a table to the environment.
fn declare_table(&mut self, table: Table);
@@ -291,6 +279,7 @@ pub trait ModuleEnvironment<'data> {
offset: usize,
elements: Vec<FuncIndex>,
);
/// Declares a memory to the environment
fn declare_memory(&mut self, memory: Memory);

View File

@@ -244,14 +244,7 @@ pub fn parse_element_section<'data>(
let mut init_expr_reader = init_expr.get_binary_reader();
let (base, offset) = match init_expr_reader.read_operator()? {
Operator::I32Const { value } => (None, value as u32 as usize),
Operator::GetGlobal { global_index } => match environ
.get_global(GlobalIndex::from_u32(global_index))
.initializer
{
GlobalInit::I32Const(value) => (None, value as u32 as usize),
GlobalInit::Import => (Some(GlobalIndex::from_u32(global_index)), 0),
_ => panic!("should not happen"),
},
Operator::GetGlobal { global_index } => (Some(GlobalIndex::from_u32(global_index)), 0),
ref s => panic!("unsupported init expr in element section: {:?}", s),
};
let items_reader = items.get_items_reader()?;
@@ -292,14 +285,7 @@ pub fn parse_data_section<'data>(
let mut init_expr_reader = init_expr.get_binary_reader();
let (base, offset) = match init_expr_reader.read_operator()? {
Operator::I32Const { value } => (None, value as u32 as usize),
Operator::GetGlobal { global_index } => match environ
.get_global(GlobalIndex::from_u32(global_index))
.initializer
{
GlobalInit::I32Const(value) => (None, value as u32 as usize),
GlobalInit::Import => (Some(GlobalIndex::from_u32(global_index)), 0),
_ => panic!("should not happen"),
},
Operator::GetGlobal { global_index } => (Some(GlobalIndex::from_u32(global_index)), 0),
ref s => panic!("unsupported init expr in data section: {:?}", s),
};
environ.declare_data_initialization(