mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-25 14:41:32 +00:00
Globals.
This commit is contained in:
@ -4,6 +4,7 @@ use wasmer_runtime_core::{
|
|||||||
structures::Map,
|
structures::Map,
|
||||||
types::{FuncIndex, FuncSig, SigIndex},
|
types::{FuncIndex, FuncSig, SigIndex},
|
||||||
units::Pages,
|
units::Pages,
|
||||||
|
module::ModuleInfo,
|
||||||
};
|
};
|
||||||
use wasmparser::{Operator, Type as WpType};
|
use wasmparser::{Operator, Type as WpType};
|
||||||
|
|
||||||
@ -26,7 +27,7 @@ pub trait FunctionCodeGenerator {
|
|||||||
fn feed_param(&mut self, ty: WpType) -> Result<(), CodegenError>;
|
fn feed_param(&mut self, ty: WpType) -> Result<(), CodegenError>;
|
||||||
fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), CodegenError>;
|
fn feed_local(&mut self, ty: WpType, n: usize) -> Result<(), CodegenError>;
|
||||||
fn begin_body(&mut self) -> 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>;
|
fn finalize(&mut self) -> Result<(), CodegenError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,10 +14,10 @@ use wasmer_runtime_core::{
|
|||||||
module::{ModuleInfo, ModuleInner, StringTable},
|
module::{ModuleInfo, ModuleInner, StringTable},
|
||||||
structures::{Map, TypedIndex},
|
structures::{Map, TypedIndex},
|
||||||
types::{
|
types::{
|
||||||
FuncIndex, FuncSig, GlobalIndex, LocalFuncIndex, MemoryIndex, SigIndex, TableIndex, Type,
|
FuncIndex, FuncSig, GlobalIndex, LocalFuncIndex, LocalGlobalIndex, MemoryIndex, SigIndex,
|
||||||
Value,
|
TableIndex, Type, Value,
|
||||||
},
|
},
|
||||||
vm::{self, ImportBacking},
|
vm::{self, ImportBacking, LocalGlobal},
|
||||||
};
|
};
|
||||||
use wasmparser::{Operator, Type as WpType};
|
use wasmparser::{Operator, Type as WpType};
|
||||||
|
|
||||||
@ -203,12 +203,6 @@ pub struct NativeTrampolines {
|
|||||||
trap_unreachable: DynamicLabel,
|
trap_unreachable: DynamicLabel,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(transparent)]
|
|
||||||
struct CtxPtr(*mut vm::Ctx);
|
|
||||||
|
|
||||||
unsafe impl Send for CtxPtr {}
|
|
||||||
unsafe impl Sync for CtxPtr {}
|
|
||||||
|
|
||||||
pub struct X64ModuleCodeGenerator {
|
pub struct X64ModuleCodeGenerator {
|
||||||
functions: Vec<X64FunctionCode>,
|
functions: Vec<X64FunctionCode>,
|
||||||
signatures: Option<Arc<Map<SigIndex, Arc<FuncSig>>>>,
|
signatures: Option<Arc<Map<SigIndex, Arc<FuncSig>>>>,
|
||||||
@ -1434,7 +1428,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
|
|||||||
));
|
));
|
||||||
Ok(())
|
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;
|
let was_unreachable;
|
||||||
|
|
||||||
if self.unreachable_depth > 0 {
|
if self.unreachable_depth > 0 {
|
||||||
@ -1458,6 +1452,57 @@ impl FunctionCodeGenerator for X64FunctionCode {
|
|||||||
let assembler = self.assembler.as_mut().unwrap();
|
let assembler = self.assembler.as_mut().unwrap();
|
||||||
|
|
||||||
match op {
|
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 } => {
|
Operator::GetLocal { local_index } => {
|
||||||
let local_index = local_index as usize;
|
let local_index = local_index as usize;
|
||||||
if local_index >= self.locals.len() {
|
if local_index >= self.locals.len() {
|
||||||
|
@ -303,7 +303,7 @@ pub fn read_module<
|
|||||||
fcg.begin_body()?;
|
fcg.begin_body()?;
|
||||||
for op in item.get_operators_reader()? {
|
for op in item.get_operators_reader()? {
|
||||||
let op = op?;
|
let op = op?;
|
||||||
fcg.feed_opcode(op)?;
|
fcg.feed_opcode(op, &info)?;
|
||||||
}
|
}
|
||||||
fcg.finalize()?;
|
fcg.finalize()?;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user