Add an offset to cranelift-wasm's GlobalVariable.

This commit is contained in:
Dan Gohman
2018-12-06 17:54:10 -05:00
parent 605f34257b
commit 7b51195f49
5 changed files with 14 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ hashmap_core = { version = "0.1.9", optional = true }
failure = { version = "0.1.1", default-features = false, features = ["derive"] } failure = { version = "0.1.1", default-features = false, features = ["derive"] }
failure_derive = { version = "0.1.1", default-features = false } failure_derive = { version = "0.1.1", default-features = false }
log = { version = "0.4.4", default-features = false } log = { version = "0.4.4", default-features = false }
cast = { version = "0.2.2" }
[dev-dependencies] [dev-dependencies]
wabt = "0.7.0" wabt = "0.7.0"

View File

@@ -75,12 +75,12 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
Operator::GetGlobal { global_index } => { Operator::GetGlobal { global_index } => {
let val = match state.get_global(builder.func, global_index, environ) { let val = match state.get_global(builder.func, global_index, environ) {
GlobalVariable::Const(val) => val, GlobalVariable::Const(val) => val,
GlobalVariable::Memory { gv, ty } => { GlobalVariable::Memory { gv, offset, ty } => {
let addr = builder.ins().global_value(environ.pointer_type(), gv); let addr = builder.ins().global_value(environ.pointer_type(), gv);
let mut flags = ir::MemFlags::new(); let mut flags = ir::MemFlags::new();
flags.set_notrap(); flags.set_notrap();
flags.set_aligned(); flags.set_aligned();
builder.ins().load(ty, flags, addr, 0) builder.ins().load(ty, flags, addr, offset)
} }
}; };
state.push1(val); state.push1(val);
@@ -88,13 +88,14 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
Operator::SetGlobal { global_index } => { Operator::SetGlobal { global_index } => {
match state.get_global(builder.func, global_index, environ) { match state.get_global(builder.func, global_index, environ) {
GlobalVariable::Const(_) => panic!("global #{} is a constant", global_index), GlobalVariable::Const(_) => panic!("global #{} is a constant", global_index),
GlobalVariable::Memory { gv, .. } => { GlobalVariable::Memory { gv, offset, ty } => {
let addr = builder.ins().global_value(environ.pointer_type(), gv); let addr = builder.ins().global_value(environ.pointer_type(), gv);
let mut flags = ir::MemFlags::new(); let mut flags = ir::MemFlags::new();
flags.set_notrap(); flags.set_notrap();
flags.set_aligned(); flags.set_aligned();
let val = state.pop1(); let val = state.pop1();
builder.ins().store(flags, val, addr, 0); debug_assert_eq!(ty, builder.func.dfg.value_type(val));
builder.ins().store(flags, val, addr, offset);
} }
} }
} }

View File

@@ -1,6 +1,7 @@
//! "Dummy" implementations of `ModuleEnvironment` and `FuncEnvironment` for testing //! "Dummy" implementations of `ModuleEnvironment` and `FuncEnvironment` for testing
//! wasm translation. //! wasm translation.
use cast;
use cranelift_codegen::cursor::FuncCursor; use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir::immediates::{Offset32, Uimm64}; use cranelift_codegen::ir::immediates::{Offset32, Uimm64};
use cranelift_codegen::ir::types::*; use cranelift_codegen::ir::types::*;
@@ -169,15 +170,11 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
fn make_global(&mut self, func: &mut ir::Function, index: GlobalIndex) -> GlobalVariable { fn make_global(&mut self, func: &mut ir::Function, index: GlobalIndex) -> GlobalVariable {
// Just create a dummy `vmctx` global. // Just create a dummy `vmctx` global.
let offset = ((index.index() * 8) as i64 + 8).into(); let offset = cast::i32((index.index() * 8) + 8).unwrap().into();
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext {}); let vmctx = func.create_global_value(ir::GlobalValueData::VMContext {});
let iadd = func.create_global_value(ir::GlobalValueData::IAddImm {
base: vmctx,
offset,
global_type: self.pointer_type(),
});
GlobalVariable::Memory { GlobalVariable::Memory {
gv: iadd, gv: vmctx,
offset: offset,
ty: self.mod_info.globals[index].entity.ty, ty: self.mod_info.globals[index].entity.ty,
} }
} }

View File

@@ -1,6 +1,7 @@
//! All the runtime support necessary for the wasm to cranelift translation is formalized by the //! All the runtime support necessary for the wasm to cranelift translation is formalized by the
//! traits `FunctionEnvironment` and `ModuleEnvironment`. //! traits `FunctionEnvironment` and `ModuleEnvironment`.
use cranelift_codegen::cursor::FuncCursor; use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir::immediates::Offset32;
use cranelift_codegen::ir::{self, InstBuilder}; use cranelift_codegen::ir::{self, InstBuilder};
use cranelift_codegen::isa::TargetFrontendConfig; use cranelift_codegen::isa::TargetFrontendConfig;
use std::convert::From; use std::convert::From;
@@ -20,6 +21,8 @@ pub enum GlobalVariable {
Memory { Memory {
/// The address of the global variable storage. /// The address of the global variable storage.
gv: ir::GlobalValue, gv: ir::GlobalValue,
/// An offset to add to the address.
offset: Offset32,
/// The global variable's type. /// The global variable's type.
ty: ir::Type, ty: ir::Type,
}, },

View File

@@ -36,6 +36,7 @@
extern crate cranelift_codegen; extern crate cranelift_codegen;
#[macro_use] #[macro_use]
extern crate cranelift_entity; extern crate cranelift_entity;
extern crate cast;
extern crate cranelift_frontend; extern crate cranelift_frontend;
#[cfg(test)] #[cfg(test)]
extern crate target_lexicon; extern crate target_lexicon;