Commit 54361cad authored by Kunshan Wang's avatar Kunshan Wang

IRBuilder: Building int and uptr types.

Implemented dependency resolution.
parent b44d389f
......@@ -46,18 +46,18 @@ fn from_MuVM_ptr(ptr: *mut CMuVM) -> *mut MuVM {
}
#[inline(always)]
fn from_MuCtx_ptr<'v>(ptr: *mut CMuCtx) -> *mut MuCtx<'v> {
fn from_MuCtx_ptr<'v>(ptr: *mut CMuCtx) -> *mut MuCtx {
debug_assert!(!ptr.is_null());
unsafe {
(*ptr).header as *mut MuCtx<'v>
(*ptr).header as *mut MuCtx
}
}
#[inline(always)]
fn from_MuIRBuilder_ptr<'c>(ptr: *mut CMuIRBuilder) -> *mut MuIRBuilder<'c> {
fn from_MuIRBuilder_ptr<'c>(ptr: *mut CMuIRBuilder) -> *mut MuIRBuilder {
debug_assert!(!ptr.is_null());
unsafe {
(*ptr).header as *mut MuIRBuilder<'c>
(*ptr).header as *mut MuIRBuilder
}
}
......
use super::common::*;
pub struct MuCtx<'v> {
pub struct MuCtx {
/// ref to MuVM
mvm: &'v MuVM,
mvm: *const MuVM,
/// Point to the C-visible CMuCtx so that `close_context` can deallocate itself.
pub c_struct: *mut CMuCtx,
}
impl<'v> MuCtx<'v> {
pub fn new(mvm: &'v MuVM) -> Box<MuCtx> {
impl MuCtx {
pub fn new(mvm: *const MuVM) -> Box<MuCtx> {
Box::new(MuCtx {
mvm: mvm,
c_struct: ptr::null_mut(),
......@@ -18,8 +18,8 @@ impl<'v> MuCtx<'v> {
#[inline(always)]
fn get_mvm(&mut self) -> &MuVM {
self.mvm
//unsafe { &mut *self.mvm }
//self.mvm
unsafe { & *self.mvm }
}
pub fn id_of(&mut self, name: MuName) -> MuID {
......
use super::common::*;
pub struct MuIRBuilder<'v> {
pub struct MuIRBuilder {
/// ref to MuVM
mvm: &'v MuVM,
mvm: *const MuVM,
/// Point to the C-visible CMuIRBuilder so that `load` and `abort` can deallocate itself.
pub c_struct: *mut CMuIRBuilder,
......@@ -33,8 +33,8 @@ pub struct TrantientBundle {
ka_clauses: IdMap<NodeKeepaliveClause>,
}
impl<'v> MuIRBuilder<'v> {
pub fn new(mvm: &'v MuVM) -> Box<MuIRBuilder> {
impl MuIRBuilder {
pub fn new(mvm: *const MuVM) -> Box<MuIRBuilder> {
Box::new(MuIRBuilder {
mvm: mvm,
c_struct: ptr::null_mut(),
......@@ -44,12 +44,13 @@ impl<'v> MuIRBuilder<'v> {
}
#[inline(always)]
fn get_mvm(&mut self) -> &MuVM {
self.mvm
fn get_mvm<'a, 'b>(&'a mut self) -> &'b MuVM {
//self.mvm
unsafe { & *self.mvm }
}
#[inline(always)]
fn get_vm(&mut self) -> &VM {
fn get_vm<'a, 'b>(&'a mut self) -> &'b VM {
&self.get_mvm().vm
}
......@@ -439,14 +440,77 @@ impl<'v> MuIRBuilder<'v> {
}
}
pub fn load_bundle(b: &mut MuIRBuilder) {
let mut built_types: IdMap<MuType> = Default::default();
let mut built_sigs: IdMap<MuFuncSig> = Default::default();
type IdPMap<T> = HashMap<MuID, P<T>>;
fn load_bundle(b: &mut MuIRBuilder) {
let mut visited: HashSet<MuID> = Default::default();
let mut built_types: IdPMap<MuType> = Default::default();
let mut built_sigs: IdPMap<MuFuncSig> = Default::default();
let vm = b.get_vm();
for (id, ty) in &b.bundle.types {
println!("{} {:?}", id, ty);
ensure_type_top(*id, ty, b, &mut visited, &mut built_types, &mut built_sigs, &vm);
}
}
fn ensure_type_top(id: MuID,
ty: &Box<NodeType>,
b: &MuIRBuilder,
visited: &mut HashSet<MuID>,
built_types: &mut IdPMap<MuType>,
built_sigs: &mut IdPMap<MuFuncSig>,
vm: &VM) {
if !visited.contains(&id) {
build_type(id, ty, b, visited, built_types, built_sigs, vm)
}
}
fn build_type(id: MuID,
ty: &Box<NodeType>,
b: &MuIRBuilder,
visited: &mut HashSet<MuID>,
built_types: &mut IdPMap<MuType>,
built_sigs: &mut IdPMap<MuFuncSig>,
vm: &VM) {
trace!("Building type {} {:?}", id, ty);
visited.insert(id);
let impl_ty = MuType::new(id, match **ty {
NodeType::TypeInt { id, len } => {
MuType_::int(len as usize)
},
NodeType::TypeUPtr { id, ty: toty } => {
let toty_i = ensure_type_rec(toty, ty, b, visited, built_types, built_sigs, vm);
MuType_::uptr(toty_i)
},
ref t => panic!("{:?} not implemented", t),
});
trace!("Type built: {} {:?}", id, impl_ty);
built_types.insert(id, P(impl_ty));
}
fn ensure_type_rec(id: MuID,
ty: &Box<NodeType>,
b: &MuIRBuilder,
visited: &mut HashSet<MuID>,
built_types: &mut IdPMap<MuType>,
built_sigs: &mut IdPMap<MuFuncSig>,
vm: &VM) -> P<MuType> {
if b.bundle.types.contains_key(&id) {
if visited.contains(&id) {
match built_types.get(&id) {
Some(t) => t.clone(),
None => panic!("Cyclic types found. id: {}", id)
}
} else {
build_type(id, ty, b, visited, built_types, built_sigs, vm);
built_types.get(&id).unwrap().clone()
}
} else {
vm.get_type(id)
}
}
......@@ -55,3 +55,32 @@ fn test_startup_shutdown() {
}
}
#[test]
#[allow(unused_variables)]
fn test_types_sigs_loading() {
unsafe {
simple_logger::init_with_level(log::LogLevel::Trace).ok();
info!("Starting micro VM...");
let mvm = mu_fastimpl_new();
let ctx = ((*mvm).new_context)(mvm);
let b = ((*ctx).new_ir_builder)(ctx);
let id1 = ((*b).gen_sym)(b, ptr::null_mut());
let id2 = ((*b).gen_sym)(b, CString::new("@id2").unwrap().as_ptr());
let id3 = ((*b).gen_sym)(b, ptr::null_mut());
((*b).new_type_int)(b, id1, 8);
((*b).new_type_int)(b, id2, 32);
((*b).new_type_uptr)(b, id3, id2);
((*b).load)(b);
((*ctx).close_context)(ctx);
info!("Finished.");
}
}
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