Support imported globals.

This commit is contained in:
losfair 2019-03-09 00:38:13 +08:00
parent 4c4743e7cd
commit 3efccbe0f7

View File

@ -1453,15 +1453,27 @@ impl FunctionCodeGenerator for X64FunctionCode {
match op { match op {
Operator::GetGlobal { global_index } => { Operator::GetGlobal { global_index } => {
let global_index = global_index as usize; let mut global_index = global_index as usize;
if global_index >= module_info.globals.len() { if global_index < module_info.imported_globals.len() {
return Err(CodegenError { dynasm!(
message: "global out of bounds", assembler
}); ; mov rax, r14 => vm::Ctx.imported_globals
);
} else {
global_index -= module_info.imported_globals.len();
if global_index >= module_info.globals.len() {
return Err(CodegenError {
message: "global out of bounds",
});
}
dynasm!(
assembler
; mov rax, r14 => vm::Ctx.globals
);
} }
dynasm!( dynasm!(
assembler assembler
; mov rax, r14 => vm::Ctx.globals
; mov rax, [rax + (global_index as i32) * 8] ; mov rax, [rax + (global_index as i32) * 8]
; mov rax, rax => LocalGlobal.data ; mov rax, rax => LocalGlobal.data
); );
@ -1476,13 +1488,29 @@ impl FunctionCodeGenerator for X64FunctionCode {
)?; )?;
} }
Operator::SetGlobal { global_index } => { 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)?; let ty = Self::emit_pop_into_ax(assembler, &mut self.value_stack)?;
let mut global_index = global_index as usize;
if global_index < module_info.imported_globals.len() {
dynasm!(
assembler
; push rbx
; mov rbx, r14 => vm::Ctx.imported_globals
);
} else {
global_index -= module_info.imported_globals.len();
if global_index >= module_info.globals.len() {
return Err(CodegenError {
message: "global out of bounds",
});
}
dynasm!(
assembler
; push rbx
; mov rbx, r14 => vm::Ctx.globals
);
}
if ty if ty
!= type_to_wp_type( != type_to_wp_type(
module_info.globals[LocalGlobalIndex::new(global_index)] module_info.globals[LocalGlobalIndex::new(global_index)]
@ -1496,8 +1524,6 @@ impl FunctionCodeGenerator for X64FunctionCode {
} }
dynasm!( dynasm!(
assembler assembler
; push rbx
; mov rbx, r14 => vm::Ctx.globals
; mov rbx, [rbx + (global_index as i32) * 8] ; mov rbx, [rbx + (global_index as i32) * 8]
; mov rbx => LocalGlobal.data, rax ; mov rbx => LocalGlobal.data, rax
; pop rbx ; pop rbx