This commit is contained in:
losfair 2019-03-09 00:32:18 +08:00
parent 258dea64d8
commit 4c4743e7cd
3 changed files with 58 additions and 12 deletions

View File

@ -4,6 +4,7 @@ use wasmer_runtime_core::{
structures::Map,
types::{FuncIndex, FuncSig, SigIndex},
units::Pages,
module::ModuleInfo,
};
use wasmparser::{Operator, Type as WpType};
@ -26,7 +27,7 @@ pub trait FunctionCodeGenerator {
fn feed_param(&mut self, ty: WpType) -> Result<(), CodegenError>;
fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), CodegenError>;
fn begin_body(&mut self) -> Result<(), CodegenError>;
fn feed_opcode(&mut self, op: Operator) -> Result<(), CodegenError>;
fn feed_opcode(&mut self, op: Operator, module_info: &ModuleInfo) -> Result<(), CodegenError>;
fn finalize(&mut self) -> Result<(), CodegenError>;
}

View File

@ -14,10 +14,10 @@ use wasmer_runtime_core::{
module::{ModuleInfo, ModuleInner, StringTable},
structures::{Map, TypedIndex},
types::{
FuncIndex, FuncSig, GlobalIndex, LocalFuncIndex, MemoryIndex, SigIndex, TableIndex, Type,
Value,
FuncIndex, FuncSig, GlobalIndex, LocalFuncIndex, LocalGlobalIndex, MemoryIndex, SigIndex,
TableIndex, Type, Value,
},
vm::{self, ImportBacking},
vm::{self, ImportBacking, LocalGlobal},
};
use wasmparser::{Operator, Type as WpType};
@ -203,12 +203,6 @@ pub struct NativeTrampolines {
trap_unreachable: DynamicLabel,
}
#[repr(transparent)]
struct CtxPtr(*mut vm::Ctx);
unsafe impl Send for CtxPtr {}
unsafe impl Sync for CtxPtr {}
pub struct X64ModuleCodeGenerator {
functions: Vec<X64FunctionCode>,
signatures: Option<Arc<Map<SigIndex, Arc<FuncSig>>>>,
@ -1434,7 +1428,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
));
Ok(())
}
fn feed_opcode(&mut self, op: Operator) -> Result<(), CodegenError> {
fn feed_opcode(&mut self, op: Operator, module_info: &ModuleInfo) -> Result<(), CodegenError> {
let was_unreachable;
if self.unreachable_depth > 0 {
@ -1458,6 +1452,57 @@ impl FunctionCodeGenerator for X64FunctionCode {
let assembler = self.assembler.as_mut().unwrap();
match op {
Operator::GetGlobal { global_index } => {
let global_index = global_index as usize;
if global_index >= module_info.globals.len() {
return Err(CodegenError {
message: "global out of bounds",
});
}
dynasm!(
assembler
; mov rax, r14 => vm::Ctx.globals
; mov rax, [rax + (global_index as i32) * 8]
; mov rax, rax => LocalGlobal.data
);
Self::emit_push_from_ax(
assembler,
&mut self.value_stack,
type_to_wp_type(
module_info.globals[LocalGlobalIndex::new(global_index)]
.desc
.ty,
),
)?;
}
Operator::SetGlobal { global_index } => {
let global_index = global_index as usize;
if global_index >= module_info.globals.len() {
return Err(CodegenError {
message: "global out of bounds",
});
}
let ty = Self::emit_pop_into_ax(assembler, &mut self.value_stack)?;
if ty
!= type_to_wp_type(
module_info.globals[LocalGlobalIndex::new(global_index)]
.desc
.ty,
)
{
return Err(CodegenError {
message: "type mismatch in SetGlobal",
});
}
dynasm!(
assembler
; push rbx
; mov rbx, r14 => vm::Ctx.globals
; mov rbx, [rbx + (global_index as i32) * 8]
; mov rbx => LocalGlobal.data, rax
; pop rbx
);
}
Operator::GetLocal { local_index } => {
let local_index = local_index as usize;
if local_index >= self.locals.len() {

View File

@ -303,7 +303,7 @@ pub fn read_module<
fcg.begin_body()?;
for op in item.get_operators_reader()? {
let op = op?;
fcg.feed_opcode(op)?;
fcg.feed_opcode(op, &info)?;
}
fcg.finalize()?;
}