Skip to content

Commit

Permalink
merge and respond to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
xmclark committed Mar 28, 2019
2 parents d8347a3 + 4bbf990 commit 5294eb6
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 41 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include = [
errno = "0.2.4"
structopt = "0.2.11"
wabt = "0.7.2"
hashbrown = "0.1.8"
wasmer-clif-backend = { path = "lib/clif-backend" }
wasmer-dynasm-backend = { path = "lib/dynasm-backend", optional = true }
wasmer-runtime = { path = "lib/runtime" }
Expand Down
11 changes: 8 additions & 3 deletions lib/clif-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use target_lexicon::Triple;

use wasmer_runtime_core::cache::{Artifact, Error as CacheError};
use wasmer_runtime_core::{
backend::{Compiler, Token},
backend::{Compiler, CompilerConfig, Token},
error::{CompileError, CompileResult},
module::ModuleInner,
};
Expand All @@ -39,12 +39,17 @@ impl CraneliftCompiler {

impl Compiler for CraneliftCompiler {
/// Compiles wasm binary to a wasmer module.
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner> {
fn compile(
&self,
wasm: &[u8],
compiler_config: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner> {
validate(wasm)?;

let isa = get_isa();

let mut module = module::Module::new();
let mut module = module::Module::new(&compiler_config);
let module_env = module_env::ModuleEnv::new(&mut module, &*isa);

let func_bodies = module_env.translate(wasm)?;
Expand Down
5 changes: 3 additions & 2 deletions lib/clif-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;
use wasmer_runtime_core::cache::{Artifact, Error as CacheError};

use wasmer_runtime_core::{
backend::Backend,
backend::{Backend, CompilerConfig},
error::CompileResult,
module::{ModuleInfo, ModuleInner, StringTable},
structures::{Map, TypedIndex},
Expand All @@ -25,7 +25,7 @@ pub struct Module {
}

impl Module {
pub fn new() -> Self {
pub fn new(compiler_config: &CompilerConfig) -> Self {
Self {
info: ModuleInfo {
memories: Map::new(),
Expand All @@ -50,6 +50,7 @@ impl Module {

namespace_table: StringTable::new(),
name_table: StringTable::new(),
em_symbol_map: compiler_config.symbol_map.clone(),

custom_sections: HashMap::new(),
},
Expand Down
20 changes: 13 additions & 7 deletions lib/clif-backend/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,26 @@ fn round_up(n: usize, multiple: usize) -> usize {
}

extern "C" fn i32_print(_ctx: &mut vm::Ctx, n: i32) {
print!(" i32: {},", n);
eprint!(" i32: {},", n);
}
extern "C" fn i64_print(_ctx: &mut vm::Ctx, n: i64) {
print!(" i64: {},", n);
eprint!(" i64: {},", n);
}
extern "C" fn f32_print(_ctx: &mut vm::Ctx, n: f32) {
print!(" f32: {},", n);
eprint!(" f32: {},", n);
}
extern "C" fn f64_print(_ctx: &mut vm::Ctx, n: f64) {
print!(" f64: {},", n);
eprint!(" f64: {},", n);
}
extern "C" fn start_debug(_ctx: &mut vm::Ctx, func_index: u32) {
print!("func ({}), args: [", func_index);
extern "C" fn start_debug(ctx: &mut vm::Ctx, func_index: u32) {
if let Some(symbol_map) = unsafe { ctx.borrow_symbol_map() } {
if let Some(fn_name) = symbol_map.get(&func_index) {
eprint!("func ({} ({})), args: [", fn_name, func_index);
return;
}
}
eprint!("func ({}), args: [", func_index);
}
extern "C" fn end_debug(_ctx: &mut vm::Ctx) {
println!(" ]");
eprintln!(" ]");
}
11 changes: 8 additions & 3 deletions lib/dynasm-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod stack;
use crate::codegen::{CodegenError, ModuleCodeGenerator};
use crate::parse::LoadError;
use wasmer_runtime_core::{
backend::{sys::Memory, Backend, CacheGen, Compiler, Token},
backend::{sys::Memory, Backend, CacheGen, Compiler, CompilerConfig, Token},
cache::{Artifact, Error as CacheError},
error::{CompileError, CompileResult},
module::{ModuleInfo, ModuleInner},
Expand All @@ -51,9 +51,14 @@ impl SinglePassCompiler {
}

impl Compiler for SinglePassCompiler {
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner> {
fn compile(
&self,
wasm: &[u8],
compiler_config: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner> {
let mut mcg = codegen_x64::X64ModuleCodeGenerator::new();
let info = parse::read_module(wasm, Backend::Dynasm, &mut mcg)?;
let info = parse::read_module(wasm, Backend::Dynasm, &mut mcg, &compiler_config)?;
let (ec, resolver) = mcg.finalize(&info)?;
Ok(ModuleInner {
cache_gen: Box::new(Placeholder),
Expand Down
5 changes: 4 additions & 1 deletion lib/dynasm-backend/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::codegen::{CodegenError, FunctionCodeGenerator, ModuleCodeGenerator};
use hashbrown::HashMap;
use wasmer_runtime_core::{
backend::{Backend, FuncResolver, ProtectedCaller},
backend::{Backend, CompilerConfig, FuncResolver, ProtectedCaller},
module::{
DataInitializer, ExportIndex, ImportName, ModuleInfo, StringTable, StringTableBuilder,
TableInitializer,
Expand Down Expand Up @@ -71,6 +71,7 @@ pub fn read_module<
wasm: &[u8],
backend: Backend,
mcg: &mut MCG,
compiler_config: &CompilerConfig,
) -> Result<ModuleInfo, LoadError> {
validate(wasm)?;
let mut info = ModuleInfo {
Expand All @@ -97,6 +98,8 @@ pub fn read_module<
namespace_table: StringTable::new(),
name_table: StringTable::new(),

em_symbol_map: compiler_config.symbol_map.clone(),

custom_sections: HashMap::new(),
};

Expand Down
2 changes: 1 addition & 1 deletion lib/emscripten/src/syscalls/emscripten_vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl EmscriptenVfs {
Some(FileHandle::Vf(file)) => {
let count = {
let mut result = RefCell::borrow_mut(&file);
let result = result.read(buf_slice);
let result = result.read_file(buf_slice, 0);
result.unwrap()
};
count as _
Expand Down
13 changes: 9 additions & 4 deletions lib/llvm-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use inkwell::{
OptimizationLevel,
};
use wasmer_runtime_core::{
backend::{Compiler, Token},
backend::{Compiler, CompilerConfig, Token},
cache::{Artifact, Error as CacheError},
error::CompileError,
module::ModuleInner,
Expand All @@ -32,10 +32,15 @@ impl LLVMCompiler {
}

impl Compiler for LLVMCompiler {
fn compile(&self, wasm: &[u8], _: Token) -> Result<ModuleInner, CompileError> {
fn compile(
&self,
wasm: &[u8],
compiler_config: CompilerConfig,
_: Token,
) -> Result<ModuleInner, CompileError> {
validate(wasm)?;

let (info, code_reader) = read_info::read_module(wasm).unwrap();
let (info, code_reader) = read_info::read_module(wasm, compiler_config).unwrap();
let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();

let (backend, protected_caller) = backend::LLVMBackend::new(module, intrinsics);
Expand Down Expand Up @@ -121,7 +126,7 @@ fn test_read_module() {
"#;
let wasm = wat2wasm(wat).unwrap();

let (info, code_reader) = read_info::read_module(&wasm).unwrap();
let (info, code_reader) = read_info::read_module(&wasm, Default::default()).unwrap();

let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();

Expand Down
9 changes: 7 additions & 2 deletions lib/llvm-backend/src/read_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use wasmer_runtime_core::{
backend::Backend,
backend::{Backend, CompilerConfig},
module::{
DataInitializer, ExportIndex, ImportName, ModuleInfo, StringTable, StringTableBuilder,
TableInitializer,
Expand All @@ -20,7 +20,10 @@ use wasmparser::{

use hashbrown::HashMap;

pub fn read_module(wasm: &[u8]) -> Result<(ModuleInfo, CodeSectionReader), BinaryReaderError> {
pub fn read_module(
wasm: &[u8],
compiler_config: CompilerConfig,
) -> Result<(ModuleInfo, CodeSectionReader), BinaryReaderError> {
let mut info = ModuleInfo {
memories: Map::new(),
globals: Map::new(),
Expand All @@ -45,6 +48,8 @@ pub fn read_module(wasm: &[u8]) -> Result<(ModuleInfo, CodeSectionReader), Binar
namespace_table: StringTable::new(),
name_table: StringTable::new(),

em_symbol_map: compiler_config.symbol_map.clone(),

custom_sections: HashMap::new(),
};

Expand Down
39 changes: 32 additions & 7 deletions lib/runtime-abi/src/vfs/device_file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::vfs::file_like::{FileLike, Metadata};
use failure::Error;
use std::io;
use std::io::{Read, Write};
use std::io::{Seek, Write};

pub struct Stdin;
pub struct Stdout;
Expand All @@ -15,10 +15,18 @@ impl FileLike for Stdin {
fn write_file(&mut self, _buf: &[u8], _offset: usize) -> Result<usize, io::Error> {
unimplemented!()
}

fn read_file(&mut self, _buf: &mut [u8], _offset: usize) -> Result<usize, io::Error> {
unimplemented!()
}

fn set_file_len(&mut self, _len: usize) -> Result<(), failure::Error> {
panic!("Cannot set length of stdin");
}
}

impl Read for Stdin {
fn read(&mut self, _buf: &mut [u8]) -> Result<usize, io::Error> {
impl io::Seek for Stdin {
fn seek(&mut self, _pos: io::SeekFrom) -> Result<u64, io::Error> {
unimplemented!()
}
}
Expand All @@ -33,10 +41,19 @@ impl FileLike for Stdout {
let mut handle = stdout.lock();
handle.write(buf)
}

fn read_file(&mut self, _buf: &mut [u8], _offset: usize) -> Result<usize, io::Error> {
unimplemented!()
}

fn set_file_len(&mut self, _len: usize) -> Result<(), failure::Error> {
panic!("Cannot set length of stdout");

}
}

impl Read for Stdout {
fn read(&mut self, _buf: &mut [u8]) -> Result<usize, io::Error> {
impl io::Seek for Stdout {
fn seek(&mut self, _pos: io::SeekFrom) -> Result<u64, io::Error> {
unimplemented!()
}
}
Expand All @@ -51,10 +68,18 @@ impl FileLike for Stderr {
let mut handle = stderr.lock();
handle.write(buf)
}

fn read_file(&mut self, _buf: &mut [u8], _offset: usize) -> Result<usize, io::Error> {
unimplemented!()
}

fn set_file_len(&mut self, _len: usize) -> Result<(), failure::Error> {
panic!("Cannot set length of stderr");
}
}

impl Read for Stderr {
fn read(&mut self, _buf: &mut [u8]) -> Result<usize, io::Error> {
impl io::Seek for Stderr {
fn seek(&mut self, _pos: io::SeekFrom) -> Result<u64, io::Error> {
unimplemented!()
}
}
8 changes: 7 additions & 1 deletion lib/runtime-abi/src/vfs/file_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ pub struct Metadata {
pub is_file: bool,
}

pub trait FileLike: std::io::Read {
pub trait FileLike: std::io::Seek {
// get metadata
fn metadata(&self) -> Result<Metadata, failure::Error>;

// write
fn write_file(&mut self, buf: &[u8], offset: usize) -> Result<usize, io::Error>;

// read
fn read_file(&mut self, buf: &mut [u8], offset: usize) -> Result<usize, io::Error>;

// set_file_len
fn set_file_len(&mut self, len: usize) -> Result<(), failure::Error>;
}
11 changes: 10 additions & 1 deletion lib/runtime-abi/src/vfs/virtual_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use failure::Error;

use crate::vfs::file_like::{FileLike, Metadata};
use std::io;
use std::io::{Seek, SeekFrom, Write};
use std::io::{Seek, SeekFrom, Write, Read};

impl FileLike for zbox::File {
fn metadata(&self) -> Result<Metadata, Error> {
Expand All @@ -20,4 +20,13 @@ impl FileLike for zbox::File {
self.finish().unwrap();
result
}

fn read_file(&mut self, buf: &mut [u8], offset: usize) -> Result<usize, io::Error> {
self.seek(io::SeekFrom::Start(offset as u64))?;
self.read(buf)
}

fn set_file_len(&mut self, len: usize) -> Result<(), failure::Error> {
self.set_len(len).map_err(|e| e.into())
}
}
21 changes: 20 additions & 1 deletion lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::{
};
use std::{any::Any, ptr::NonNull};

use hashbrown::HashMap;

pub mod sys {
pub use crate::sys::*;
}
Expand All @@ -38,11 +40,28 @@ impl Token {
}
}

/// Configuration data for the compiler
pub struct CompilerConfig {
/// Symbol information generated from emscripten; used for more detailed debug messages
pub symbol_map: Option<HashMap<u32, String>>,
}

impl Default for CompilerConfig {
fn default() -> CompilerConfig {
CompilerConfig { symbol_map: None }
}
}

pub trait Compiler {
/// Compiles a `Module` from WebAssembly binary format.
/// The `CompileToken` parameter ensures that this can only
/// be called from inside the runtime.
fn compile(&self, wasm: &[u8], _: Token) -> CompileResult<ModuleInner>;
fn compile(
&self,
wasm: &[u8],
comp_conf: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner>;

unsafe fn from_cache(&self, cache: Artifact, _: Token) -> Result<ModuleInner, CacheError>;
}
Expand Down
Loading

0 comments on commit 5294eb6

Please sign in to comment.