Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Simplify ModuleEnvironment.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sunfishcode committed Jan 7, 2019
1 parent 48ec9dc commit 459f6dd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 48 deletions.
25 changes: 9 additions & 16 deletions lib/wasm/src/environ/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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,
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down
13 changes: 1 addition & 12 deletions lib/wasm/src/environ/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand All @@ -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);

Expand Down
18 changes: 2 additions & 16 deletions lib/wasm/src/sections_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 459f6dd

Please sign in to comment.