More code reorganization.

This commit is contained in:
Dan Gohman
2018-08-03 13:45:46 -07:00
parent 6659ef7018
commit 831b481f13
10 changed files with 177 additions and 180 deletions

View File

@@ -8,22 +8,18 @@ use cranelift_codegen::isa;
use cranelift_codegen::Context;
use cranelift_wasm::{FuncTranslator, FunctionIndex};
use environ::{get_func_name, ModuleTranslation};
use module::Module;
/// An Instance of a WebAssemby module.
/// The result of compiling a WebAssemby module's functions.
#[derive(Debug)]
pub struct Compilation<'module> {
/// The module this `Compilation` is compiled from.
pub module: &'module Module,
pub struct Compilation {
/// Compiled machine code for the function bodies.
pub functions: Vec<Vec<u8>>,
}
impl<'module> Compilation<'module> {
impl Compilation {
/// Allocates the runtime data structures with the given flags.
pub fn new(module: &'module Module, functions: Vec<Vec<u8>>) -> Self {
Self { module, functions }
pub fn new(functions: Vec<Vec<u8>>) -> Self {
Self { functions }
}
}
@@ -103,7 +99,7 @@ pub type Relocations = Vec<Vec<Relocation>>;
pub fn compile_module<'data, 'module>(
translation: &ModuleTranslation<'data, 'module>,
isa: &isa::TargetIsa,
) -> Result<(Compilation<'module>, Relocations), String> {
) -> Result<(Compilation, Relocations), String> {
let mut functions = Vec::new();
let mut relocations = Vec::new();
for (i, input) in translation.lazy.function_body_inputs.iter().enumerate() {
@@ -127,5 +123,5 @@ pub fn compile_module<'data, 'module>(
functions.push(code_buf);
relocations.push(reloc_sink.func_relocs);
}
Ok((Compilation::new(translation.module, functions), relocations))
Ok((Compilation::new(functions), relocations))
}

View File

@@ -13,8 +13,7 @@ use cranelift_wasm::{
FunctionIndex, Global, GlobalIndex, GlobalVariable, Memory, MemoryIndex, SignatureIndex, Table,
TableIndex, WasmResult,
};
use module;
use module::Module;
use module::{DataInitializer, Export, LazyContents, Module, TableElements};
use target_lexicon::Triple;
/// Compute a `ir::ExternalName` for a given wasm function index.
@@ -23,37 +22,6 @@ pub fn get_func_name(func_index: FunctionIndex) -> ir::ExternalName {
ir::ExternalName::user(0, func_index as u32)
}
/// A data initializer for linear memory.
pub struct DataInitializer<'data> {
/// The index of the memory to initialize.
pub memory_index: MemoryIndex,
/// Optionally a globalvar base to initialize at.
pub base: Option<GlobalIndex>,
/// A constant offset to initialize at.
pub offset: usize,
/// The initialization data.
pub data: &'data [u8],
}
/// References to the input wasm data buffer to be decoded and processed later.
/// separately from the main module translation.
pub struct LazyContents<'data> {
/// References to the function bodies.
pub function_body_inputs: Vec<&'data [u8]>,
/// References to the data initializers.
pub data_initializers: Vec<DataInitializer<'data>>,
}
impl<'data> LazyContents<'data> {
fn new() -> Self {
Self {
function_body_inputs: Vec::new(),
data_initializers: Vec::new(),
}
}
}
/// Object containing the standalone runtime information. To be passed after creation as argument
/// to `cranelift_wasm::translatemodule`.
pub struct ModuleEnvironment<'data, 'module> {
@@ -224,7 +192,7 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
elements: Vec<FunctionIndex>,
) {
debug_assert!(base.is_none(), "global-value offsets not supported yet");
self.module.table_elements.push(module::TableElements {
self.module.table_elements.push(TableElements {
table_index,
base,
offset,
@@ -255,25 +223,25 @@ impl<'data, 'module> cranelift_wasm::ModuleEnvironment<'data>
fn declare_func_export(&mut self, func_index: FunctionIndex, name: &str) {
self.module
.exports
.insert(String::from(name), module::Export::Function(func_index));
.insert(String::from(name), Export::Function(func_index));
}
fn declare_table_export(&mut self, table_index: TableIndex, name: &str) {
self.module
.exports
.insert(String::from(name), module::Export::Table(table_index));
.insert(String::from(name), Export::Table(table_index));
}
fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &str) {
self.module
.exports
.insert(String::from(name), module::Export::Memory(memory_index));
.insert(String::from(name), Export::Memory(memory_index));
}
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &str) {
self.module
.exports
.insert(String::from(name), module::Export::Global(global_index));
.insert(String::from(name), Export::Global(global_index));
}
fn declare_start_func(&mut self, func_index: FunctionIndex) {

View File

@@ -1,99 +0,0 @@
//! An `Instance` contains all the runtime state used by execution of a wasm
//! module.
use cranelift_codegen::ir;
use cranelift_wasm::GlobalIndex;
use environ::DataInitializer;
use module::{Module, TableElements};
const PAGE_SIZE: usize = 65536;
/// An Instance of a WebAssemby module.
#[derive(Debug)]
pub struct Instance {
/// WebAssembly table data.
pub tables: Vec<Vec<usize>>,
/// WebAssembly linear memory data.
pub memories: Vec<Vec<u8>>,
/// WebAssembly global variable data.
pub globals: Vec<u8>,
}
impl Instance {
/// Create a new `Instance`.
pub fn new(module: &Module, data_initializers: &[DataInitializer]) -> Self {
let mut result = Self {
tables: Vec::new(),
memories: Vec::new(),
globals: Vec::new(),
};
result.instantiate_tables(module, &module.table_elements);
result.instantiate_memories(module, data_initializers);
result.instantiate_globals(module);
result
}
/// Allocate memory in `self` for just the tables of the current module.
fn instantiate_tables(&mut self, module: &Module, table_initializers: &[TableElements]) {
debug_assert!(self.tables.is_empty());
self.tables.reserve_exact(module.tables.len());
for table in &module.tables {
let len = table.size;
let mut v = Vec::with_capacity(len);
v.resize(len, 0);
self.tables.push(v);
}
for init in table_initializers {
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
let to_init =
&mut self.tables[init.table_index][init.offset..init.offset + init.elements.len()];
to_init.copy_from_slice(&init.elements);
}
}
/// Allocate memory in `instance` for just the memories of the current module.
fn instantiate_memories(&mut self, module: &Module, data_initializers: &[DataInitializer]) {
debug_assert!(self.memories.is_empty());
// Allocate the underlying memory and initialize it to all zeros.
self.memories.reserve_exact(module.memories.len());
for memory in &module.memories {
let len = memory.pages_count * PAGE_SIZE;
let mut v = Vec::with_capacity(len);
v.resize(len, 0);
self.memories.push(v);
}
for init in data_initializers {
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
let to_init =
&mut self.memories[init.memory_index][init.offset..init.offset + init.data.len()];
to_init.copy_from_slice(init.data);
}
}
/// Allocate memory in `instance` for just the globals of the current module,
/// without any initializers applied yet.
fn instantiate_globals(&mut self, module: &Module) {
debug_assert!(self.globals.is_empty());
// Allocate the underlying memory and initialize it to all zeros.
let globals_data_size = module.globals.len() * 8;
self.globals.resize(globals_data_size, 0);
}
/// Returns a slice of the contents of allocated linear memory.
pub fn inspect_memory(&self, memory_index: usize, address: usize, len: usize) -> &[u8] {
&self
.memories
.get(memory_index)
.unwrap_or_else(|| panic!("no memory for index {}", memory_index))
[address..address + len]
}
/// Shows the value of a global variable.
pub fn inspect_global(&self, global_index: GlobalIndex, ty: ir::Type) -> &[u8] {
let offset = global_index * 8;
let len = ty.bytes() as usize;
&self.globals[offset..offset + len]
}
}

View File

@@ -11,10 +11,8 @@ extern crate target_lexicon;
mod compilation;
mod environ;
mod instance;
mod module;
pub use compilation::{Compilation, compile_module, Relocation, Relocations};
pub use compilation::{compile_module, Compilation, Relocation, Relocations};
pub use environ::{ModuleEnvironment, ModuleTranslation};
pub use instance::Instance;
pub use module::Module;
pub use module::{DataInitializer, Module, TableElements};

View File

@@ -1,5 +1,4 @@
//! A `Module` contains all the relevant information translated from a
//! WebAssembly module, except for the function bodies and data initializers.
//! Data structures for representing decoded wasm modules.
use cranelift_codegen::ir;
use cranelift_wasm::{
@@ -81,3 +80,34 @@ impl Module {
}
}
}
/// A data initializer for linear memory.
pub struct DataInitializer<'data> {
/// The index of the memory to initialize.
pub memory_index: MemoryIndex,
/// Optionally a globalvar base to initialize at.
pub base: Option<GlobalIndex>,
/// A constant offset to initialize at.
pub offset: usize,
/// The initialization data.
pub data: &'data [u8],
}
/// References to the input wasm data buffer to be decoded and processed later,
/// separately from the main module translation.
pub struct LazyContents<'data> {
/// References to the function bodies.
pub function_body_inputs: Vec<&'data [u8]>,
/// References to the data initializers.
pub data_initializers: Vec<DataInitializer<'data>>,
}
impl<'data> LazyContents<'data> {
pub fn new() -> Self {
Self {
function_body_inputs: Vec::new(),
data_initializers: Vec::new(),
}
}
}