Commit a247e2b2 authored by qinsoon's avatar qinsoon

set global int seems working

(cannot load this test as dylib, it would involve PIC code on linux)
parent 290b22ce
...@@ -1026,8 +1026,17 @@ impl ASMCodeGen { ...@@ -1026,8 +1026,17 @@ impl ASMCodeGen {
loc_cursor += 1; loc_cursor += 1;
}, },
Value_::Memory(MemoryLocation::Symbolic{ref base, ref label}) => { Value_::Memory(MemoryLocation::Symbolic{ref base, ref label}) => {
result_str.push_str(&symbol(label.clone())); if base.is_some() && base.as_ref().unwrap().id() == x86_64::RIP.id() {
loc_cursor += label.len(); // pc relative address
// let pic_symbol = pic_symbol(label.clone());
let pic_symbol = symbol(label.clone()); // not sure if we need this
result_str.push_str(&pic_symbol);
loc_cursor += label.len();
} else {
let symbol = symbol(label.clone());
result_str.push_str(&symbol);
loc_cursor += label.len();
}
if base.is_some() { if base.is_some() {
result_str.push('('); result_str.push('(');
...@@ -2838,12 +2847,22 @@ fn directive_comm(name: String, size: ByteSize, align: ByteSize) -> String { ...@@ -2838,12 +2847,22 @@ fn directive_comm(name: String, size: ByteSize, align: ByteSize) -> String {
pub fn symbol(name: String) -> String { pub fn symbol(name: String) -> String {
name name
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn symbol(name: String) -> String { pub fn symbol(name: String) -> String {
format!("_{}", name) format!("_{}", name)
} }
#[allow(dead_code)]
#[cfg(target_os = "linux")]
pub fn pic_symbol(name: String) -> String {
format!("{}@GOTPCREL", name)
}
#[allow(dead_code)]
#[cfg(target_os = "macos")]
pub fn pic_symbol(name: String) -> String {
symbol(name)
}
use compiler::machine_code::CompiledFunction; use compiler::machine_code::CompiledFunction;
pub fn spill_rewrite( pub fn spill_rewrite(
......
...@@ -72,23 +72,23 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo { ...@@ -72,23 +72,23 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo {
match size_in_bit { match size_in_bit {
1 => BackendTypeInfo{ 1 => BackendTypeInfo{
size: 1, alignment: 1, struct_layout: None, size: 1, alignment: 1, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(1)) gc_type: mm::add_gc_type(GCType::new_noreftype(1, 1))
}, },
8 => BackendTypeInfo{ 8 => BackendTypeInfo{
size: 1, alignment: 1, struct_layout: None, size: 1, alignment: 1, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(1)) gc_type: mm::add_gc_type(GCType::new_noreftype(1, 1))
}, },
16 => BackendTypeInfo{ 16 => BackendTypeInfo{
size: 2, alignment: 2, struct_layout: None, size: 2, alignment: 2, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(2)) gc_type: mm::add_gc_type(GCType::new_noreftype(2, 2))
}, },
32 => BackendTypeInfo{ 32 => BackendTypeInfo{
size: 4, alignment: 4, struct_layout: None, size: 4, alignment: 4, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(4)) gc_type: mm::add_gc_type(GCType::new_noreftype(4, 4))
}, },
64 => BackendTypeInfo{ 64 => BackendTypeInfo{
size: 8, alignment: 8, struct_layout: None, size: 8, alignment: 8, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(8)) gc_type: mm::add_gc_type(GCType::new_noreftype(8, 8))
}, },
_ => unimplemented!() _ => unimplemented!()
} }
...@@ -107,18 +107,18 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo { ...@@ -107,18 +107,18 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo {
| MuType_::ThreadRef | MuType_::ThreadRef
| MuType_::StackRef => BackendTypeInfo{ | MuType_::StackRef => BackendTypeInfo{
size: 8, alignment: 8, struct_layout: None, size: 8, alignment: 8, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(8)) gc_type: mm::add_gc_type(GCType::new_noreftype(8, 8))
}, },
// tagref // tagref
MuType_::Tagref64 => unimplemented!(), MuType_::Tagref64 => unimplemented!(),
// floating point // floating point
MuType_::Float => BackendTypeInfo{ MuType_::Float => BackendTypeInfo{
size: 4, alignment: 4, struct_layout: None, size: 4, alignment: 4, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(4)) gc_type: mm::add_gc_type(GCType::new_noreftype(4, 4))
}, },
MuType_::Double => BackendTypeInfo { MuType_::Double => BackendTypeInfo {
size: 8, alignment: 8, struct_layout: None, size: 8, alignment: 8, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(8)) gc_type: mm::add_gc_type(GCType::new_noreftype(8, 8))
}, },
// array // array
MuType_::Array(ref ty, len) => { MuType_::Array(ref ty, len) => {
...@@ -131,6 +131,7 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo { ...@@ -131,6 +131,7 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo {
gc_type : mm::add_gc_type(GCType { gc_type : mm::add_gc_type(GCType {
id : GCTYPE_INIT_ID, id : GCTYPE_INIT_ID,
size : ele_ty.size * len, size : ele_ty.size * len,
alignment: ele_ty.alignment,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs : Some(RepeatingRefPattern{ repeat_refs : Some(RepeatingRefPattern{
pattern: RefPattern::NestedType(vec![ele_ty.gc_type]), pattern: RefPattern::NestedType(vec![ele_ty.gc_type]),
...@@ -184,7 +185,7 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo { ...@@ -184,7 +185,7 @@ pub fn resolve_backend_type_info (ty: &MuType, vm: &VM) -> BackendTypeInfo {
// void // void
MuType_::Void => BackendTypeInfo{ MuType_::Void => BackendTypeInfo{
size: 0, alignment: 8, struct_layout: None, size: 0, alignment: 8, struct_layout: None,
gc_type: mm::add_gc_type(GCType::new_noreftype(0)) gc_type: mm::add_gc_type(GCType::new_noreftype(0, 8))
}, },
// vector // vector
MuType_::Vector(_, _) => unimplemented!() MuType_::Vector(_, _) => unimplemented!()
...@@ -249,6 +250,7 @@ fn layout_struct(tys: &Vec<P<MuType>>, vm: &VM) -> BackendTypeInfo { ...@@ -249,6 +250,7 @@ fn layout_struct(tys: &Vec<P<MuType>>, vm: &VM) -> BackendTypeInfo {
gc_type : mm::add_gc_type(GCType { gc_type : mm::add_gc_type(GCType {
id : GCTYPE_INIT_ID, id : GCTYPE_INIT_ID,
size : size, size : size,
alignment: struct_align,
non_repeat_refs: Some(if use_ref_offsets { non_repeat_refs: Some(if use_ref_offsets {
RefPattern::Map { RefPattern::Map {
offsets: ref_offsets, offsets: ref_offsets,
......
...@@ -11,15 +11,17 @@ pub const GCTYPE_INIT_ID: u32 = u32::MAX; ...@@ -11,15 +11,17 @@ pub const GCTYPE_INIT_ID: u32 = u32::MAX;
pub struct GCType { pub struct GCType {
pub id: u32, pub id: u32,
pub size: ByteSize, pub size: ByteSize,
pub alignment: ByteSize,
pub non_repeat_refs: Option<RefPattern>, pub non_repeat_refs: Option<RefPattern>,
pub repeat_refs : Option<RepeatingRefPattern>, pub repeat_refs : Option<RepeatingRefPattern>,
} }
impl GCType { impl GCType {
pub fn new_noreftype(size: ByteSize) -> GCType { pub fn new_noreftype(size: ByteSize, align: ByteSize) -> GCType {
GCType { GCType {
id: GCTYPE_INIT_ID, id: GCTYPE_INIT_ID,
size: size, size: size,
alignment: align,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs : None, repeat_refs : None,
} }
...@@ -29,6 +31,7 @@ impl GCType { ...@@ -29,6 +31,7 @@ impl GCType {
GCType { GCType {
id: GCTYPE_INIT_ID, id: GCTYPE_INIT_ID,
size: POINTER_SIZE, size: POINTER_SIZE,
alignment: POINTER_SIZE,
non_repeat_refs: Some(RefPattern::Map{ non_repeat_refs: Some(RefPattern::Map{
offsets: vec![0], offsets: vec![0],
size: POINTER_SIZE size: POINTER_SIZE
...@@ -126,6 +129,7 @@ mod tests { ...@@ -126,6 +129,7 @@ mod tests {
let a = GCType{ let a = GCType{
id: 0, id: 0,
size: 16, size: 16,
alignment: 8,
non_repeat_refs: Some(RefPattern::Map{ non_repeat_refs: Some(RefPattern::Map{
offsets: vec![0], offsets: vec![0],
size: 16 size: 16
...@@ -137,6 +141,7 @@ mod tests { ...@@ -137,6 +141,7 @@ mod tests {
let b = GCType { let b = GCType {
id: 1, id: 1,
size: 160, size: 160,
alignment: 8,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs : Some(RepeatingRefPattern { repeat_refs : Some(RepeatingRefPattern {
pattern: RefPattern::Map{ pattern: RefPattern::Map{
...@@ -151,6 +156,7 @@ mod tests { ...@@ -151,6 +156,7 @@ mod tests {
let c = GCType { let c = GCType {
id: 2, id: 2,
size: 1600, size: 1600,
alignment: 8,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs : Some(RepeatingRefPattern { repeat_refs : Some(RepeatingRefPattern {
pattern: RefPattern::NestedType(vec![Arc::new(b.clone()).clone()]), pattern: RefPattern::NestedType(vec![Arc::new(b.clone()).clone()]),
...@@ -177,6 +183,7 @@ mod tests { ...@@ -177,6 +183,7 @@ mod tests {
let int = GCType { let int = GCType {
id: 3, id: 3,
size: 8, size: 8,
alignment: 8,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs: None repeat_refs: None
}; };
......
...@@ -273,6 +273,7 @@ mod tests { ...@@ -273,6 +273,7 @@ mod tests {
let a = GCType{ let a = GCType{
id: 0, id: 0,
size: 16, size: 16,
alignment: 8,
non_repeat_refs: Some(RefPattern::Map{ non_repeat_refs: Some(RefPattern::Map{
offsets: vec![0], offsets: vec![0],
size: 16 size: 16
...@@ -295,6 +296,7 @@ mod tests { ...@@ -295,6 +296,7 @@ mod tests {
let a = GCType{ let a = GCType{
id: 0, id: 0,
size: 32, size: 32,
alignment: 8,
non_repeat_refs: Some(RefPattern::Map{ non_repeat_refs: Some(RefPattern::Map{
offsets: vec![0, 8], offsets: vec![0, 8],
size: 32 size: 32
...@@ -317,6 +319,7 @@ mod tests { ...@@ -317,6 +319,7 @@ mod tests {
const N_REF : usize = 64; const N_REF : usize = 64;
let a = GCType{ let a = GCType{
id: 999, id: 999,
alignment: 8,
size: N_REF * POINTER_SIZE, size: N_REF * POINTER_SIZE,
non_repeat_refs: Some(RefPattern::Map{ non_repeat_refs: Some(RefPattern::Map{
offsets: (0..N_REF).map(|x| x * POINTER_SIZE).collect(), offsets: (0..N_REF).map(|x| x * POINTER_SIZE).collect(),
...@@ -339,6 +342,7 @@ mod tests { ...@@ -339,6 +342,7 @@ mod tests {
let a = GCType { let a = GCType {
id: 1, id: 1,
size: 160, size: 160,
alignment: 8,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs : Some(RepeatingRefPattern { repeat_refs : Some(RepeatingRefPattern {
pattern: RefPattern::Map{ pattern: RefPattern::Map{
...@@ -364,6 +368,7 @@ mod tests { ...@@ -364,6 +368,7 @@ mod tests {
let b = GCType { let b = GCType {
id: 1, id: 1,
size: 160, size: 160,
alignment: 8,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs : Some(RepeatingRefPattern { repeat_refs : Some(RepeatingRefPattern {
pattern: RefPattern::Map{ pattern: RefPattern::Map{
...@@ -378,6 +383,7 @@ mod tests { ...@@ -378,6 +383,7 @@ mod tests {
let a = GCType { let a = GCType {
id: 2, id: 2,
size: 1600, size: 1600,
alignment: 8,
non_repeat_refs: None, non_repeat_refs: None,
repeat_refs : Some(RepeatingRefPattern { repeat_refs : Some(RepeatingRefPattern {
pattern: RefPattern::NestedType(vec![Arc::new(b.clone()).clone()]), pattern: RefPattern::NestedType(vec![Arc::new(b.clone()).clone()]),
......
...@@ -55,6 +55,7 @@ fn link_dylib_internal (files: Vec<PathBuf>, out: PathBuf) -> PathBuf { ...@@ -55,6 +55,7 @@ fn link_dylib_internal (files: Vec<PathBuf>, out: PathBuf) -> PathBuf {
let mut gcc = Command::new(get_test_clang_path()); let mut gcc = Command::new(get_test_clang_path());
gcc.arg("-shared"); gcc.arg("-shared");
gcc.arg("-fPIC");
gcc.arg("-Wl"); gcc.arg("-Wl");
gcc.arg("-undefined"); gcc.arg("-undefined");
gcc.arg("dynamic_lookup"); gcc.arg("dynamic_lookup");
......
...@@ -505,7 +505,7 @@ impl <'a> VM { ...@@ -505,7 +505,7 @@ impl <'a> VM {
while expect_id < gc_type.id { while expect_id < gc_type.id {
use runtime::mm::common::gctype::GCType; use runtime::mm::common::gctype::GCType;
mm::add_gc_type(GCType::new_noreftype(0)); mm::add_gc_type(GCType::new_noreftype(0, 8));
expect_id += 1; expect_id += 1;
} }
} }
......
...@@ -3,6 +3,7 @@ extern crate log; ...@@ -3,6 +3,7 @@ extern crate log;
extern crate libloading; extern crate libloading;
use test_ir::test_ir::global_access; use test_ir::test_ir::global_access;
use test_compiler::test_call::gen_ccall_exit;
use self::mu::compiler::*; use self::mu::compiler::*;
use self::mu::vm::VM; use self::mu::vm::VM;
use self::mu::runtime::thread::MuThread; use self::mu::runtime::thread::MuThread;
...@@ -13,6 +14,7 @@ use self::mu::ast::op::*; ...@@ -13,6 +14,7 @@ use self::mu::ast::op::*;
use utils::Address; use utils::Address;
use utils::LinkedHashMap; use utils::LinkedHashMap;
use mu::testutil; use mu::testutil;
use mu::testutil::aot;
use mu::vm::handle; use mu::vm::handle;
use std::sync::RwLock; use std::sync::RwLock;
...@@ -53,9 +55,9 @@ fn test_set_global_by_api() { ...@@ -53,9 +55,9 @@ fn test_set_global_by_api() {
set_global_by_api(&vm); set_global_by_api(&vm);
let compiler = Compiler::new(CompilerPolicy::default(), vm.clone()); let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
let func_id = vm.id_of("set_global_by_api");
{ {
let func_id = vm.id_of("set_global_by_api");
let funcs = vm.funcs().read().unwrap(); let funcs = vm.funcs().read().unwrap();
let func = funcs.get(&func_id).unwrap().read().unwrap(); let func = funcs.get(&func_id).unwrap().read().unwrap();
let func_vers = vm.func_vers().read().unwrap(); let func_vers = vm.func_vers().read().unwrap();
...@@ -66,39 +68,35 @@ fn test_set_global_by_api() { ...@@ -66,39 +68,35 @@ fn test_set_global_by_api() {
// set global by api here // set global by api here
{ {
let global_id = vm.id_of("a"); let global_id = vm.id_of("my_global");
let global_handle = vm.handle_from_global(global_id); let global_handle = vm.handle_from_global(global_id);
let uint64_1_handle = vm.handle_from_uint64(1, 64); let uint64_10_handle = vm.handle_from_uint64(10, 64);
debug!("write {:?} to location {:?}", uint64_1_handle, global_handle); debug!("write {:?} to location {:?}", uint64_10_handle, global_handle);
handle::store(MemoryOrder::Relaxed, global_handle, uint64_1_handle); handle::store(MemoryOrder::Relaxed, global_handle, uint64_10_handle);
} }
// then emit context (global will be put into context.s // then emit context (global will be put into context.s
vm.make_primordial_thread(func_id, vec![]);
backend::emit_context(&vm); backend::emit_context(&vm);
// link // link
let lib = { let executable = aot::link_primordial(vec![Mu("set_global_by_api")], "set_global_by_api_test", &vm);
let libname = &testutil::get_dylib_name("set_global_by_api"); let output = aot::execute_nocheck(executable);
let dylib = testutil::aot::link_dylib(vec![Mu("set_global_by_api")], libname, &vm);
libloading::Library::new(dylib.as_os_str()).unwrap()
};
unsafe { assert!(output.status.code().is_some());
let set_global_by_api : libloading::Symbol<unsafe extern fn () -> u64> = lib.get(b"set_global_by_api").unwrap();
let res = set_global_by_api(); let ret_code = output.status.code().unwrap();
println!("set_global_by_api() = {}", res); println!("return code: {} (i.e. the value set before)", ret_code);
assert!(res == 1); assert!(ret_code == 10);
}
} }
fn set_global_by_api(vm: &VM) { fn set_global_by_api(vm: &VM) {
typedef! ((vm) int64 = mu_int(64)); typedef! ((vm) int64 = mu_int(64));
typedef! ((vm) iref_int64 = mu_iref(int64)); typedef! ((vm) iref_int64 = mu_iref(int64));
globaldef! ((vm) <int64> a); globaldef! ((vm) <int64> my_global);
funcsig! ((vm) sig = () -> (int64)); funcsig! ((vm) sig = () -> (int64));
funcdecl! ((vm) <sig> set_global_by_api); funcdecl! ((vm) <sig> set_global_by_api);
...@@ -107,17 +105,20 @@ fn set_global_by_api(vm: &VM) { ...@@ -107,17 +105,20 @@ fn set_global_by_api(vm: &VM) {
// blk entry // blk entry
block! ((vm, set_global_by_api_v1) blk_entry); block! ((vm, set_global_by_api_v1) blk_entry);
ssa! ((vm, set_global_by_api_v1) <int64> val); ssa! ((vm, set_global_by_api_v1) <int64> val);
global! ((vm, set_global_by_api_v1) blk_entry_a = a); global! ((vm, set_global_by_api_v1) blk_entry_my_global = my_global);
inst! ((vm, set_global_by_api_v1) blk_entry_load: inst! ((vm, set_global_by_api_v1) blk_entry_load:
val = LOAD blk_entry_a (is_ptr: false, order: MemoryOrder::SeqCst) val = LOAD blk_entry_my_global (is_ptr: false, order: MemoryOrder::SeqCst)
); );
let blk_entry_exit = gen_ccall_exit(val.clone(), &mut set_global_by_api_v1, &vm);
// won't execute this inst
inst! ((vm, set_global_by_api_v1) blk_entry_ret: inst! ((vm, set_global_by_api_v1) blk_entry_ret:
RET (val) RET (val)
); );
define_block! ((vm, set_global_by_api_v1) blk_entry() { define_block! ((vm, set_global_by_api_v1) blk_entry() {
blk_entry_load, blk_entry_ret blk_entry_load, blk_entry_exit, blk_entry_ret
}); });
define_func_ver!((vm) set_global_by_api_v1 (entry: blk_entry) { define_func_ver!((vm) set_global_by_api_v1 (entry: blk_entry) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment