wasmer/lib/llvm-backend/src/lib.rs

161 lines
4.9 KiB
Rust
Raw Normal View History

2019-02-23 01:34:55 +00:00
use inkwell::{
execution_engine::JitFunction,
targets::{CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine},
OptimizationLevel,
};
2019-02-09 23:53:40 +00:00
use wasmer_runtime_core::{
backend::{Compiler, Token},
2019-02-23 01:34:55 +00:00
cache::{Artifact, Error as CacheError},
2019-02-09 23:53:40 +00:00
error::CompileError,
2019-02-12 03:34:04 +00:00
module::ModuleInner,
2019-02-09 23:53:40 +00:00
};
mod code;
mod intrinsics;
mod read_info;
mod state;
2019-02-16 00:02:20 +00:00
// mod backend;
2019-02-09 23:53:40 +00:00
pub struct LLVMCompiler {
_private: (),
}
impl LLVMCompiler {
pub fn new() -> Self {
Self { _private: () }
}
}
impl Compiler for LLVMCompiler {
fn compile(&self, wasm: &[u8], _: Token) -> Result<ModuleInner, CompileError> {
let (_info, _code_reader) = read_info::read_module(wasm).unwrap();
unimplemented!()
}
2019-02-23 01:34:55 +00:00
unsafe fn from_cache(&self, _artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
unimplemented!()
}
2019-02-09 23:53:40 +00:00
}
#[test]
fn test_read_module() {
use wabt::wat2wasm;
2019-02-23 01:34:55 +00:00
use wasmer_runtime_core::vmcalls;
2019-02-15 02:08:20 +00:00
// let wasm = include_bytes!("../../spectests/examples/simple/simple.wasm") as &[u8];
let wat = r#"
2019-02-09 23:53:40 +00:00
(module
(type $t0 (func (param i32) (result i32)))
(type $t1 (func (result i32)))
(memory 1)
2019-02-23 01:34:55 +00:00
(table 10 anyfunc)
(elem (i32.const 0) $foobar)
2019-02-15 02:08:20 +00:00
(global $g0 (mut i32) (i32.const 0))
(func $foo (type $t0) (param i32) (result i32)
get_local 0
2019-02-23 01:34:55 +00:00
i32.const 0
call_indirect (type $t0)
2019-02-16 00:02:20 +00:00
memory.grow
)
(func $foobar (type $t0)
get_local 0
)
(func $bar (type $t0) (param i32) (result i32)
get_local 0
call $foo
))
2019-02-09 23:53:40 +00:00
"#;
let wasm = wat2wasm(wat).unwrap();
2019-02-09 23:53:40 +00:00
let (info, code_reader) = read_info::read_module(&wasm).unwrap();
2019-02-16 00:02:20 +00:00
let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();
{
Target::initialize_x86(&InitializationConfig {
asm_parser: true,
asm_printer: true,
base: true,
disassembler: true,
info: true,
machine_code: true,
});
let triple = TargetMachine::get_default_triple().to_string();
let target = Target::from_triple(&triple).unwrap();
2019-02-23 01:34:55 +00:00
let target_machine = target
.create_target_machine(
&triple,
&TargetMachine::get_host_cpu_name().to_string(),
&TargetMachine::get_host_cpu_features().to_string(),
OptimizationLevel::Default,
RelocMode::PIC,
CodeModel::Default,
)
.unwrap();
2019-02-16 00:02:20 +00:00
2019-02-23 01:34:55 +00:00
let memory_buffer = target_machine
.write_to_memory_buffer(&module, FileType::Object)
.unwrap();
2019-02-16 00:02:20 +00:00
// std::fs::write("memory_buffer", memory_buffer.as_slice()).unwrap();
let mem_buf_slice = memory_buffer.as_slice();
2019-02-23 01:34:55 +00:00
2019-02-16 00:02:20 +00:00
let macho = goblin::mach::MachO::parse(mem_buf_slice, 0).unwrap();
let symbols = macho.symbols.as_ref().unwrap();
let relocations = macho.relocations().unwrap();
for (_, reloc_iter, section) in relocations.into_iter() {
println!("section: {:#?}", section);
for reloc_info in reloc_iter {
let reloc_info = reloc_info.unwrap();
println!("\treloc_info: {:#?}", reloc_info);
2019-02-23 01:34:55 +00:00
println!(
"\tsymbol: {:#?}",
symbols.get(reloc_info.r_symbolnum()).unwrap()
);
2019-02-16 00:02:20 +00:00
}
}
}
2019-02-23 01:34:55 +00:00
let exec_engine = module
.create_jit_execution_engine(OptimizationLevel::Default)
.unwrap();
2019-02-16 00:02:20 +00:00
2019-02-23 01:34:55 +00:00
exec_engine.add_global_mapping(
&intrinsics.memory_grow_dynamic_local,
vmcalls::local_dynamic_memory_grow as usize,
);
exec_engine.add_global_mapping(
&intrinsics.memory_grow_static_local,
vmcalls::local_static_memory_grow as usize,
);
exec_engine.add_global_mapping(
&intrinsics.memory_grow_dynamic_import,
vmcalls::imported_dynamic_memory_grow as usize,
);
exec_engine.add_global_mapping(
&intrinsics.memory_grow_static_import,
vmcalls::imported_static_memory_grow as usize,
);
exec_engine.add_global_mapping(
&intrinsics.memory_size_dynamic_local,
vmcalls::local_dynamic_memory_size as usize,
);
exec_engine.add_global_mapping(
&intrinsics.memory_size_static_local,
vmcalls::local_static_memory_size as usize,
);
exec_engine.add_global_mapping(
&intrinsics.memory_size_dynamic_import,
vmcalls::imported_dynamic_memory_size as usize,
);
exec_engine.add_global_mapping(
&intrinsics.memory_size_static_import,
vmcalls::imported_static_memory_size as usize,
);
2019-02-16 00:02:20 +00:00
// unsafe {
// let func: JitFunction<unsafe extern fn(*mut u8, i32) -> i32> = exec_engine.get_function("fn0").unwrap();
// let result = func.call(0 as _, 0);
// println!("result: {}", result);
// }
2019-02-09 23:53:40 +00:00
}