GitLab will continue to be upgraded from 11.4.5-ce.0 on November 25th 2019 at 4.00pm (AEDT) to 5.00pm (AEDT) due to Critical Security Patch Availability. During the update, GitLab and Mattermost services will not be available.

Commit 1168f297 authored by qinsoon's avatar qinsoon

[wip] get handle from global, and create handle from int

parent 55aa2fe5
Pipeline #242 failed with stage
in 24 minutes and 15 seconds
use ir::*; use ir::*;
use ptr::*; use ptr::*;
use types::*; use types::*;
use utils::Address;
use std::collections::HashMap; use std::collections::HashMap;
...@@ -36,120 +35,3 @@ impl MuBundle { ...@@ -36,120 +35,3 @@ impl MuBundle {
} }
} }
} }
\ No newline at end of file
#[derive(Copy, Clone, Debug)]
pub struct APIHandleKey {
pub id: MuID,
pub v: APIHandleKeyKind
}
#[derive(Copy, Clone, Debug)]
pub enum APIHandleKeyKind {
Int,
Float,
Double,
UPtr,
UFP,
// SeqValue
Struct,
Array,
Vector,
// GenRef
Ref,
IRef,
TagRef64,
FuncRef,
ThreadRef,
StackRef,
FCRef, // frame cursor ref
// GenRef->IR
Bundle,
// GenRef->IR->Child
Type,
FuncSig,
FuncVer,
BB,
Inst,
// GenRef->IR->Child->Var->Global
Const,
Global,
Func,
ExpFunc,
// GenRef->IR->Child->Var->Local
NorParam,
ExcParam,
InstRes,
}
macro_rules! handle_constructor {
($fn_name: ident, $kind: ident) => {
pub fn $fn_name(id: MuID) -> APIHandleKey {
APIHandleKey{
id: id, v: APIHandleKeyKind::$kind
}
}
}
}
handle_constructor!(handle_int, Int);
handle_constructor!(handle_float, Float);
handle_constructor!(handle_double, Double);
handle_constructor!(handle_uptr, UPtr);
handle_constructor!(handle_ufp, UFP);
handle_constructor!(handle_struct, Struct);
handle_constructor!(handle_array, Array);
handle_constructor!(handle_vector, Vector);
handle_constructor!(handle_ref, Ref);
handle_constructor!(handle_iref, IRef);
handle_constructor!(handle_tagref64, TagRef64);
handle_constructor!(handle_funcref, FuncRef);
handle_constructor!(handle_threadref, ThreadRef);
handle_constructor!(handle_stackref, StackRef);
handle_constructor!(handle_fcref, FCRef);
handle_constructor!(handle_bundle, Bundle);
handle_constructor!(handle_type, Type);
handle_constructor!(handle_funcsig, FuncSig);
handle_constructor!(handle_funcver, FuncVer);
handle_constructor!(handle_bb, BB);
handle_constructor!(handle_inst, Inst);
handle_constructor!(handle_const, Const);
handle_constructor!(handle_global, Global);
handle_constructor!(handle_func, Func);
handle_constructor!(handle_expfunc, ExpFunc);
handle_constructor!(handle_norparam, NorParam);
handle_constructor!(handle_excparam, ExcParam);
handle_constructor!(handle_instres, InstRes);
#[derive(Clone)]
pub enum APIHandleValue {
Int(u64),
Float(f32),
Double(f64),
UPtr(Address),
UFP(Address),
Struct(Vec<APIHandleValue>),
Array(Vec<APIHandleValue>),
Vector(Vec<APIHandleValue>),
Ref(Address),
IRef(Address),
TagRef64(u64),
FuncRef(MuID),
ThreadRef,
StackRef,
FCRef,
IBRef
}
\ No newline at end of file
extern crate byteorder; extern crate byteorder;
extern crate rustc_serialize; extern crate rustc_serialize;
pub type BitSize = usize;
pub type ByteOffset = isize; pub type ByteOffset = isize;
pub type ByteSize = usize; pub type ByteSize = usize;
pub type Word = usize; pub type Word = usize;
......
...@@ -19,7 +19,7 @@ mod deps { ...@@ -19,7 +19,7 @@ mod deps {
pub type MuName = String; pub type MuName = String;
pub type CName = MuName; pub type CName = MuName;
pub struct APIHandleKey { pub struct APIHandle {
// stub // stub
} }
......
This diff is collapsed.
...@@ -105,17 +105,17 @@ fn from_MuBool(cbool: CMuBool) -> bool { ...@@ -105,17 +105,17 @@ fn from_MuBool(cbool: CMuBool) -> bool {
cbool != 0 cbool != 0
} }
// APIHandleKey is immutable when used. // APIHandle is immutable when used.
#[inline(always)] #[inline(always)]
fn from_handle<'a>(cmuvalue: CMuValue) -> &'a APIHandleKey { fn from_handle<'a>(cmuvalue: CMuValue) -> &'a APIHandle {
debug_assert!(!cmuvalue.is_null()); debug_assert!(!cmuvalue.is_null());
unsafe { unsafe {
&*(cmuvalue as *const APIHandleKey) &*(cmuvalue as *const APIHandle)
} }
} }
#[inline(always)] #[inline(always)]
fn from_handle_optional<'a>(cmuvalue: CMuValue) -> Option<&'a APIHandleKey> { fn from_handle_optional<'a>(cmuvalue: CMuValue) -> Option<&'a APIHandle> {
if cmuvalue.is_null() { if cmuvalue.is_null() {
None None
} else { } else {
...@@ -157,11 +157,11 @@ fn from_MuFlag_array<'a>(ptr: *const CMuFlag, len: usize) -> &'a [CMuFlag] { ...@@ -157,11 +157,11 @@ fn from_MuFlag_array<'a>(ptr: *const CMuFlag, len: usize) -> &'a [CMuFlag] {
/// way as raw pointers. So this function will convert each element. This function is only called /// way as raw pointers. So this function will convert each element. This function is only called
/// by `new_thread_nor`. As always, thread creation dominates the time. /// by `new_thread_nor`. As always, thread creation dominates the time.
#[inline(always)] #[inline(always)]
fn from_handle_array<'a>(ptr: *const CMuValue, len: usize) -> Vec<&'a APIHandleKey> { fn from_handle_array<'a>(ptr: *const CMuValue, len: usize) -> Vec<&'a APIHandle> {
let slc = from_array_direct(ptr, len); let slc = from_array_direct(ptr, len);
slc.iter().map(|&e| { slc.iter().map(|&e| {
debug_assert!(!e.is_null()); debug_assert!(!e.is_null());
unsafe { &*(e as *const APIHandleKey) } unsafe { &*(e as *const APIHandle) }
}).collect::<Vec<_>>() }).collect::<Vec<_>>()
} }
...@@ -190,7 +190,7 @@ fn to_MuID(value: MuID) -> CMuID { ...@@ -190,7 +190,7 @@ fn to_MuID(value: MuID) -> CMuID {
} }
#[inline(always)] #[inline(always)]
fn to_handle(muvalue: *const APIHandleKey) -> CMuValue { fn to_handle(muvalue: *const APIHandle) -> CMuValue {
debug_assert!(!muvalue.is_null()); debug_assert!(!muvalue.is_null());
muvalue as CMuValue muvalue as CMuValue
} }
......
...@@ -45,5 +45,7 @@ mod common { ...@@ -45,5 +45,7 @@ mod common {
pub use ast::ir::*; pub use ast::ir::*;
pub use ast::ptr::*; pub use ast::ptr::*;
pub use ast::types::*; pub use ast::types::*;
pub use vm::handle::*;
} }
This diff is collapsed.
...@@ -73,23 +73,23 @@ impl MuVM { ...@@ -73,23 +73,23 @@ impl MuVM {
pub type MuArraySize = usize; pub type MuArraySize = usize;
pub type MuIntValue = APIHandleKey; pub type MuIntValue = APIHandle;
pub type MuBundleNode = APIHandleKey; pub type MuBundleNode = APIHandle;
pub type MuChildNode = APIHandleKey; pub type MuChildNode = APIHandle;
pub type MuTypeNode = APIHandleKey; pub type MuTypeNode = APIHandle;
pub type MuFuncSigNode= APIHandleKey; pub type MuFuncSigNode= APIHandle;
pub type MuConstNode = APIHandleKey; pub type MuConstNode = APIHandle;
pub type MuGlobalNode = APIHandleKey; pub type MuGlobalNode = APIHandle;
pub type MuFuncNode = APIHandleKey; pub type MuFuncNode = APIHandle;
pub type MuFuncVerNode= APIHandleKey; pub type MuFuncVerNode= APIHandle;
pub type MuBBNode = APIHandleKey; pub type MuBBNode = APIHandle;
pub type MuNorParamNode = APIHandleKey; pub type MuNorParamNode = APIHandle;
pub type MuExcParamNode = APIHandleKey; pub type MuExcParamNode = APIHandle;
pub type MuInstNode = APIHandleKey; pub type MuInstNode = APIHandle;
pub type MuInstResNode = APIHandleKey; pub type MuInstResNode = APIHandle;
pub type MuLocalVarNode = APIHandleKey; pub type MuLocalVarNode = APIHandle;
pub type MuVarNode = APIHandleKey; pub type MuVarNode = APIHandle;
pub type MuFlag = usize; pub type MuFlag = usize;
pub type MuDestKind = MuFlag; pub type MuDestKind = MuFlag;
......
../api_bridge.rs
\ No newline at end of file
This diff is collapsed.
../api_c.rs
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
...@@ -23,7 +23,7 @@ mod deps { ...@@ -23,7 +23,7 @@ mod deps {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct APIHandleKey { pub struct APIHandle {
pub ty: MuID, pub ty: MuID,
pub vb: ValueBox, pub vb: ValueBox,
} }
......
...@@ -10,7 +10,7 @@ mod deps { ...@@ -10,7 +10,7 @@ mod deps {
pub use ast::ir::MuID; pub use ast::ir::MuID;
pub use ast::ir::MuName; pub use ast::ir::MuName;
pub use ast::ir::CName; pub use ast::ir::CName;
pub use ast::bundle::APIHandleKey; pub use vm::handle::APIHandle;
extern crate ast; extern crate ast;
} }
......
use ast::ir::*;
use ast::inst::*;
use utils::BitSize;
use utils::Address;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct APIHandle {
pub id: MuID,
pub v: APIHandleValue
}
#[derive(Clone, Debug)]
pub enum APIHandleValue {
Int(u64, BitSize),
Float(f32),
Double(f64),
UPtr(Address),
UFP(Address),
// SeqValue
Struct(Vec<APIHandleValue>),
Array (Vec<APIHandleValue>),
Vector(Vec<APIHandleValue>),
// GenRef
Ref(Address),
IRef(Address),
TagRef64(u64),
FuncRef,
ThreadRef,
StackRef,
FCRef, // frame cursor ref
// GenRef->IR
Bundle,
// GenRef->IR->Child
Type(MuID),
FuncSig(MuID),
FuncVer(MuID),
BB,
Inst,
// GenRef->IR->Child->Var->Global
Const,
Global(MuID),
Func,
ExpFunc,
// GenRef->IR->Child->Var->Local
NorParam,
ExcParam,
InstRes,
}
impl APIHandleValue {
pub fn as_iref(&self) -> Address {
match self {
&APIHandleValue::IRef(addr) => addr,
_ => panic!("expected IRef")
}
}
}
pub fn store(ord: MemoryOrder, loc: Arc<APIHandle>, val: Arc<APIHandle>) {
// FIXME: take memory order into consideration
// get address
let addr = loc.v.as_iref();
// get value and store
// we will store here (its unsafe)
unsafe {
match val.v {
APIHandleValue::Int(ival, bits) => {
match bits {
8 => addr.store::<u8>(ival as u8),
16 => addr.store::<u16>(ival as u16),
32 => addr.store::<u32>(ival as u32),
64 => addr.store::<u64>(ival),
_ => panic!("unimplemented int length")
}
},
APIHandleValue::Float(fval) => addr.store::<f32>(fval),
APIHandleValue::Double(fval) => addr.store::<f64>(fval),
APIHandleValue::UPtr(aval) => addr.store::<Address>(aval),
APIHandleValue::UFP(aval) => addr.store::<Address>(aval),
APIHandleValue::Struct(_)
| APIHandleValue::Array(_)
| APIHandleValue::Vector(_) => panic!("cannot store an aggregated value to an address"),
APIHandleValue::Ref(aval)
| APIHandleValue::IRef(aval) => addr.store::<Address>(aval),
_ => unimplemented!()
}
}
}
\ No newline at end of file
mod vm; mod vm;
mod vm_options; mod vm_options;
pub mod api; pub mod api;
pub mod handle;
pub use vm::vm::VM; pub use vm::vm::VM;
pub use vm::vm_options::VMOptions; pub use vm::vm_options::VMOptions;
...@@ -10,7 +10,9 @@ use compiler::machine_code::CompiledFunction; ...@@ -10,7 +10,9 @@ use compiler::machine_code::CompiledFunction;
use runtime::thread::*; use runtime::thread::*;
use runtime::ValueLocation; use runtime::ValueLocation;
use utils::ByteSize; use utils::ByteSize;
use utils::BitSize;
use runtime::mm as gc; use runtime::mm as gc;
use vm::handle::*;
use vm::vm_options::VMOptions; use vm::vm_options::VMOptions;
use vm::vm_options::MuLogLevel; use vm::vm_options::MuLogLevel;
...@@ -18,6 +20,7 @@ use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; ...@@ -18,6 +20,7 @@ use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
use log::LogLevel; use log::LogLevel;
use std::path; use std::path;
use std::sync::RwLock; use std::sync::RwLock;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, AtomicBool, ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT, Ordering}; use std::sync::atomic::{AtomicUsize, AtomicBool, ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT, Ordering};
// FIXME: // FIXME:
...@@ -58,6 +61,9 @@ pub struct VM { ...@@ -58,6 +61,9 @@ pub struct VM {
// partially serialize // partially serialize
// 13 // 13
compiled_funcs: RwLock<HashMap<MuID, RwLock<CompiledFunction>>>, compiled_funcs: RwLock<HashMap<MuID, RwLock<CompiledFunction>>>,
// not serialize
active_handles: RwLock<HashMap<MuID, Arc<APIHandle>>>
} }
const VM_SERIALIZE_FIELDS : usize = 14; const VM_SERIALIZE_FIELDS : usize = 14;
...@@ -366,6 +372,8 @@ impl Decodable for VM { ...@@ -366,6 +372,8 @@ impl Decodable for VM {
is_running: ATOMIC_BOOL_INIT, is_running: ATOMIC_BOOL_INIT,
vm_options: vm_options, vm_options: vm_options,
compiled_funcs: RwLock::new(compiled_funcs), compiled_funcs: RwLock::new(compiled_funcs),
active_handles: RwLock::new(hashmap!{})
}; };
vm.next_id.store(next_id, Ordering::SeqCst); vm.next_id.store(next_id, Ordering::SeqCst);
...@@ -409,7 +417,8 @@ impl <'a> VM { ...@@ -409,7 +417,8 @@ impl <'a> VM {
funcs: RwLock::new(HashMap::new()), funcs: RwLock::new(HashMap::new()),
compiled_funcs: RwLock::new(HashMap::new()), compiled_funcs: RwLock::new(HashMap::new()),
primordial: RwLock::new(None) primordial: RwLock::new(None),
active_handles: RwLock::new(hashmap!{})
}; };
{ {
...@@ -832,4 +841,37 @@ impl <'a> VM { ...@@ -832,4 +841,37 @@ impl <'a> VM {
unimplemented!() unimplemented!()
} }
pub fn handle_from_global(&self, id: MuID) -> Arc<APIHandle> {
let global_iref = {
let global_locs = self.global_locations.read().unwrap();
global_locs.get(&id).unwrap().to_address()
};
let handle_id = self.next_id();
let ret = Arc::new(APIHandle {
id: handle_id,
v : APIHandleValue::IRef(global_iref)
});
let mut handles = self.active_handles.write().unwrap();
handles.insert(handle_id, ret.clone());
ret
}
pub fn handle_from_uint64(&self, num: u64, len: BitSize) -> Arc<APIHandle> {
let handle_id = self.next_id();
let ret = Arc::new(APIHandle {
id: handle_id,
v : APIHandleValue::Int(num, len)
});
let mut handles = self.active_handles.write().unwrap();
handles.insert(handle_id, ret.clone());
ret
}
} }
...@@ -13,6 +13,7 @@ use self::mu::ast::op::*; ...@@ -13,6 +13,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::vm::handle;
use std::sync::RwLock; use std::sync::RwLock;
use std::sync::Arc; use std::sync::Arc;
...@@ -64,6 +65,14 @@ fn test_set_global_by_api() { ...@@ -64,6 +65,14 @@ 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_handle = vm.handle_from_global(global_id);
let uint64_1_handle = vm.handle_from_uint64(1, 64);
handle::store(MemoryOrder::Relaxed, global_handle, uint64_1_handle);
}
// then emit context (global will be put into context.s // then emit context (global will be put into context.s
backend::emit_context(&vm); backend::emit_context(&vm);
......
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