Commit 36bb9928 authored by qinsoon's avatar qinsoon

[wip] Handles (for client)

parent 04c10e0b
......@@ -503,7 +503,7 @@ impl Value {
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.v {
Value_::SSAVar(id) => {
Value_::SSAVar(_) => {
write!(f, "+({} %{})", self.ty, self.hdr)
},
Value_::Constant(ref c) => {
......@@ -559,7 +559,7 @@ impl fmt::Display for SSAVarEntry {
#[derive(Debug, Clone, PartialEq)]
pub enum Constant {
Int(usize),
Int(u64),
Float(f32),
Double(f64),
IRef(Address),
......
......@@ -36,10 +36,10 @@
//! implementation changes (using a special thread-local heap, for example).
//! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated.
use std::fmt::{self, Display, Debug};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::ops::DerefMut;
//use std::fmt::{self, Display, Debug};
//use std::hash::{Hash, Hasher};
//use std::ops::Deref;
//use std::ops::DerefMut;
use std::sync::Arc;
......
......@@ -11,7 +11,6 @@ use utils::string_utils;
use ast::ptr::P;
use ast::ir::*;
use ast::inst::*;
use std::collections::HashMap;
use std::str;
......
use ast::ptr::P;
use ast::ir::*;
use ast::inst::*;
use vm::MachineCode;
......
......@@ -307,7 +307,7 @@ pub fn is_callee_saved(reg_id: MuID) -> bool {
pub fn is_valid_x86_imm(op: &P<Value>) -> bool {
use std::u32;
match op.v {
Value_::Constant(Constant::Int(val)) if val <= u32::MAX as usize => {
Value_::Constant(Constant::Int(val)) if val <= u32::MAX as u64 => {
true
},
_ => false
......
......@@ -8,7 +8,6 @@ use vm::bundle::*;
use std::mem;
use std::os::raw;
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
......@@ -74,23 +73,23 @@ impl MuVM {
pub type MuArraySize = usize;
pub type MuIntValue = P<Value>;
pub type MuIntValue = MuValue;
pub type MuBundleNode = *mut MuBundle;
pub type MuChildNode = MuIRNode;
pub type MuTypeNode = P<MuType>;
pub type MuFuncSigNode= P<MuFuncSig>;
pub type MuConstNode = P<Value>;
pub type MuGlobalNode = P<Value>;
pub type MuFuncNode = RefCell<MuFunction>;
pub type MuFuncVerNode= RefCell<MuFunctionVersion>;
pub type MuBBNode = P<Block>;
pub type MuNorParamNode = P<Value>;
pub type MuExcParamNode = P<Value>;
pub type MuInstNode = P<TreeNode>;
pub type MuInstResNode = P<TreeNode>;
pub type MuLocalVarNode = P<TreeNode>;
pub type MuVarNode = MuIRNode;
pub type MuBundleNode = MuValue;
pub type MuChildNode = MuValue;
pub type MuTypeNode = MuValue;
pub type MuFuncSigNode= MuValue;
pub type MuConstNode = MuValue;
pub type MuGlobalNode = MuValue;
pub type MuFuncNode = MuValue;
pub type MuFuncVerNode= MuValue;
pub type MuBBNode = MuValue;
pub type MuNorParamNode = MuValue;
pub type MuExcParamNode = MuValue;
pub type MuInstNode = MuValue;
pub type MuInstResNode = MuValue;
pub type MuLocalVarNode = MuValue;
pub type MuVarNode = MuValue;
pub type MuFlag = usize;
pub type MuDestKind = MuFlag;
......@@ -233,6 +232,7 @@ pub struct MuCtx {
struct MuCtxInternal {
vm: Arc<VM>,
cur_bundles: HashMap<MuID, MuBundle>
}
impl MuCtx {
......@@ -251,14 +251,25 @@ impl MuCtx {
#[allow(unused_variables)]
pub fn new_bundle(ctx: &mut MuCtx) -> MuBundleNode {
let bundle = Box::new(MuBundle::new());
let id = ctx.internal.vm.next_id();
let bundle = MuBundle::new(id);
Box::into_raw(bundle)
ctx.internal.cur_bundles.insert(id, bundle);
handle_bundle(id)
}
fn get_bundle(&self, b: MuBundleNode) -> &MuBundle {
self.internal.cur_bundles.get(&b.id).unwrap()
}
fn get_bundle_mut(&mut self, b: MuBundleNode) -> &mut MuBundle {
self.internal.cur_bundles.get_mut(&b.id).unwrap()
}
#[allow(unused_variables)]
pub fn load_bundle_from_node(ctx: &mut MuCtx, b: MuBundleNode) {
let bundle = unsafe{Box::from_raw(b)};
let bundle = ctx.get_bundle(b);
// load it
unimplemented!()
......@@ -266,24 +277,24 @@ impl MuCtx {
#[allow(unused_variables)]
pub fn abort_bundle_node(ctx: &mut MuCtx, b: MuBundleNode) {
let bundle = unsafe{Box::from_raw(b)};
ctx.internal.cur_bundles.remove(&b.id);
}
pub fn get_node(ctx: &mut MuCtx, b: MuBundleNode, id: MuID) -> MuChildNode {
let bundle = unsafe{b.as_mut()}.unwrap();
let bundle = ctx.get_bundle(b);
if bundle.type_defs.contains_key(&id) {
MuIRNode::new(id, MuIRNodeKind::Type)
handle_type(id)
} else if bundle.func_sigs.contains_key(&id) {
MuIRNode::new(id, MuIRNodeKind::FuncSig)
handle_funcsig(id)
} else if bundle.constants.contains_key(&id) {
MuIRNode::new(id, MuIRNodeKind::Var(MuVarNodeKind::Global(MuGlobalVarNodeKind::Const)))
handle_const(id)
} else if bundle.globals.contains_key(&id) {
MuIRNode::new(id, MuIRNodeKind::Var(MuVarNodeKind::Global(MuGlobalVarNodeKind::Global)))
handle_global(id)
} else if bundle.func_defs.contains_key(&id) {
MuIRNode::new(id, MuIRNodeKind::Var(MuVarNodeKind::Global(MuGlobalVarNodeKind::Func)))
handle_func(id)
} else if bundle.func_decls.contains_key(&id) {
MuIRNode::new(id, MuIRNodeKind::FuncVer)
handle_funcver(id)
} else {
panic!("expecting ID of a top level definition")
}
......@@ -294,6 +305,120 @@ impl MuCtx {
node.id
}
fn next_id(&self) -> MuID {
self.internal.vm.next_id()
}
#[allow(unused_variables)]
pub fn set_name(ctx: &mut MuCtx, b: MuBundleNode, node: MuChildNode, name: MuName) {
// what can be 'set_name'?
unimplemented!()
}
pub fn new_int_type(ctx: &mut MuCtx, b: MuBundleNode, len: raw::c_int) -> MuTypeNode {
let id = ctx.next_id();
let ty = P(MuType::new(id, MuType_::Int(len as usize)));
ctx.get_bundle_mut(b).type_defs.insert(id, ty);
handle_type(id)
}
pub fn new_float_type(ctx: &mut MuCtx, b: MuBundleNode) -> MuTypeNode {
let id = ctx.next_id();
let ty = P(MuType::new(id, MuType_::Float));
ctx.get_bundle_mut(b).type_defs.insert(id, ty);
handle_type(id)
}
pub fn new_double_type(ctx: &mut MuCtx, b: MuBundleNode) -> MuTypeNode {
let id = ctx.next_id();
let ty = P(MuType::new(id, MuType_::Double));
ctx.get_bundle_mut(b).type_defs.insert(id, ty);
handle_type(id)
}
pub fn new_funcsig(ctx: &mut MuCtx, b: MuBundleNode,
paramtys: *const MuTypeNode, nparamtys: MuArraySize,
rettys: *const MuTypeNode, nrettys: MuArraySize) -> MuFuncSigNode {
let arg_tys = {
let mut ret = vec![];
let bundle = ctx.get_bundle(b);
for i in 0..nparamtys {
let ty_handle = unsafe{paramtys.offset(i as isize).as_ref()}.unwrap();
let ty = bundle.get_type(ty_handle).clone();
ret.push(ty);
}
ret
};
let ret_tys = {
let mut ret = vec![];
let bundle = ctx.get_bundle(b);
for i in 0..nrettys {
let ty_handle = unsafe{rettys.offset(i as isize).as_ref()}.unwrap();
let ty = bundle.get_type(ty_handle).clone();
ret.push(ty);
}
ret
};
let id = ctx.next_id();
let func_sig = P(MuFuncSig{
hdr: MuEntityHeader::unnamed(id),
ret_tys: ret_tys,
arg_tys: arg_tys
});
ctx.get_bundle_mut(b).func_sigs.insert(id, func_sig);
handle_funcsig(id)
}
pub fn new_const_int(ctx: &mut MuCtx, b: MuBundleNode, ty: MuTypeNode, value: u64) -> MuConstNode {
let id = ctx.next_id();
let val = P(Value{
hdr: MuEntityHeader::unnamed(id),
ty: ctx.get_bundle(b).get_type(&ty).clone(),
v: Value_::Constant(Constant::Int(value))
});
ctx.get_bundle_mut(b).constants.insert(id, val);
handle_const(id)
}
pub fn new_const_float(ctx: &mut MuCtx, b: MuBundleNode, ty: MuTypeNode, value: f32) -> MuConstNode {
let id = ctx.next_id();
let val = P(Value{
hdr: MuEntityHeader::unnamed(id),
ty: ctx.get_bundle(b).get_type(&ty).clone(),
v: Value_::Constant(Constant::Float(value))
});
ctx.get_bundle_mut(b).constants.insert(id, val);
handle_const(id)
}
pub fn new_const_double(ctx: &mut MuCtx, b: MuBundleNode, ty: MuTypeNode, value: f64) -> MuConstNode {
let id = ctx.next_id();
let val = P(Value{
hdr: MuEntityHeader::unnamed(id),
ty: ctx.get_bundle(b).get_type(&ty).clone(),
v: Value_::Constant(Constant::Double(value))
});
ctx.get_bundle_mut(b).constants.insert(id, val);
handle_const(id)
}
fn new(vm: Arc<VM>) -> MuCtx {
MuCtx {
internal: Box::new(MuCtxInternal::new(vm)),
......@@ -327,19 +452,19 @@ impl MuCtx {
get_node: api!(MuCtx::get_node),
get_id : api!(MuCtx::get_id),
set_name: unimplemented_api!(),
set_name: api!(MuCtx::set_name),
// create types
new_type_int : unimplemented_api!(),
new_type_float : unimplemented_api!(),
new_type_double: unimplemented_api!(),
new_type_int : api!(MuCtx::new_int_type),
new_type_float : api!(MuCtx::new_float_type),
new_type_double: api!(MuCtx::new_double_type),
// ... a lot more
new_funcsig: unimplemented_api!(),
new_funcsig: api!(MuCtx::new_funcsig),
new_const_int : unimplemented_api!(),
new_const_float : unimplemented_api!(),
new_const_double: unimplemented_api!(),
new_const_int : api!(MuCtx::new_const_int),
new_const_float : api!(MuCtx::new_const_float),
new_const_double: api!(MuCtx::new_const_double),
// ... a lot more
new_global_cell : unimplemented_api!(),
......@@ -418,7 +543,8 @@ impl MuCtx {
impl MuCtxInternal {
fn new(vm: Arc<VM>) -> MuCtxInternal {
MuCtxInternal {
vm: vm
vm: vm,
cur_bundles: HashMap::new()
}
}
}
\ No newline at end of file
use ast::ir::*;
use ast::ptr::*;
use ast::types::*;
use vm::api::*;
use std::collections::HashMap;
pub struct MuBundle {
pub id: MuID,
pub type_defs: HashMap<MuID, P<MuType>>,
pub func_sigs: HashMap<MuID, P<MuFuncSig>>,
pub constants: HashMap<MuID, P<Value>>,
......@@ -17,8 +20,10 @@ pub struct MuBundle {
}
impl MuBundle {
pub fn new() -> MuBundle {
pub fn new(id: MuID) -> MuBundle {
MuBundle {
id: id,
type_defs: HashMap::new(),
func_sigs: HashMap::new(),
constants: HashMap::new(),
......@@ -30,45 +35,103 @@ impl MuBundle {
// name_id_map: HashMap::new()
}
}
pub fn get_type(&self, ty: &MuTypeNode) -> &P<MuType> {
self.type_defs.get(&ty.id).unwrap()
}
}
pub struct MuIRNode {
#[derive(Copy, Clone, Debug)]
pub struct MuValue {
pub id: MuID,
pub v: MuIRNodeKind
}
impl MuIRNode {
pub fn new(id: MuID, v: MuIRNodeKind) -> MuIRNode {
MuIRNode {
id: id,
v: v
}
}
}
pub v: MuValueKind
}
pub enum MuIRNodeKind {
#[derive(Copy, Clone, Debug)]
pub enum MuValueKind {
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,
Var(MuVarNodeKind),
FuncVer,
BB,
Inst
}
pub enum MuVarNodeKind {
Global(MuGlobalVarNodeKind),
Local(MuLocalVarNodeKind)
}
pub enum MuGlobalVarNodeKind {
Inst,
// GenRef->IR->Child->Var->Global
Const,
Global,
Func,
ExpFunc
}
pub enum MuLocalVarNodeKind {
ExpFunc,
// GenRef->IR->Child->Var->Local
NorParam,
ExcParam,
MuInstRes
}
\ No newline at end of file
InstRes,
}
macro_rules! handle_constructor {
($fn_name: ident, $kind: ident) => {
pub fn $fn_name(id: MuID) -> MuValue {
MuValue{
id: id, v: MuValueKind::$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);
\ No newline at end of file
......@@ -19,8 +19,8 @@ fn test_builder_factorial() {
}
fn builder_factorial() {
let mvm = MuVM::new();
let mvm_ref = unsafe {mvm.as_mut()}.unwrap();
let ctx = (mvm_ref.new_context)(mvm);
let ctx_ref = unsafe {ctx.as_mut()}.unwrap();
// let mvm = MuVM::new();
// let mvm_ref = unsafe {mvm.as_mut()}.unwrap();
// let ctx = (mvm_ref.new_context)(mvm);
// let ctx_ref = unsafe {ctx.as_mut()}.unwrap();
}
......@@ -3,10 +3,8 @@ extern crate mu;
use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::ast::ptr::*;
use self::mu::ast::op::*;
use self::mu::vm::*;
use self::mu::vm::api::*;
use std::cell::RefCell;
use std::collections::HashMap;
......@@ -33,24 +31,24 @@ pub fn sum() -> VM {
let vm = VM::new();
// .typedef @int_64 = int<64>
let mut type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
let type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
vm.set_name(type_def_int64.as_entity(), "int_64");
let mut type_def_int1 = vm.declare_type(vm.next_id(), MuType_::int(1));
let type_def_int1 = vm.declare_type(vm.next_id(), MuType_::int(1));
vm.set_name(type_def_int1.as_entity(), "int_1");
// .const @int_64_0 <@int_64> = 0
// .const @int_64_1 <@int_64> = 1
let mut const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
let const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
vm.set_name(const_def_int64_0.as_entity(), "int64_0");
let mut const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
let const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
vm.set_name(const_def_int64_1.as_entity(), "int64_1");
// .funcsig @sum_sig = (@int_64) -> (@int_64)
let mut sum_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
let sum_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
vm.set_name(sum_sig.as_entity(), "sum_sig");
// .funcdecl @sum <@sum_sig>
let mut func = MuFunction::new(vm.next_id(), sum_sig.clone());
let func = MuFunction::new(vm.next_id(), sum_sig.clone());
vm.set_name(func.as_entity(), "sum");
let func_id = func.id();
vm.declare_func(func);
......@@ -188,33 +186,33 @@ pub fn factorial() -> VM {
// .typedef @void = void
// .typedef @int_8 = int<8>
// .typedef @int_32 = int<32>
let mut type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
let type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
vm.set_name(type_def_int64.as_entity(), "int_64");
let mut type_def_int1 = vm.declare_type(vm.next_id(), MuType_::int(1));
let type_def_int1 = vm.declare_type(vm.next_id(), MuType_::int(1));
vm.set_name(type_def_int1.as_entity(), "int_1");
let mut type_def_float = vm.declare_type(vm.next_id(), MuType_::float());
let type_def_float = vm.declare_type(vm.next_id(), MuType_::float());
vm.set_name(type_def_float.as_entity(), "float");
let mut type_def_double = vm.declare_type(vm.next_id(), MuType_::double());
let type_def_double = vm.declare_type(vm.next_id(), MuType_::double());
vm.set_name(type_def_double.as_entity(), "double");
let mut type_def_void = vm.declare_type(vm.next_id(), MuType_::void());
let type_def_void = vm.declare_type(vm.next_id(), MuType_::void());
vm.set_name(type_def_void.as_entity(), "void");
let mut type_def_int8 = vm.declare_type(vm.next_id(), MuType_::int(8));
let type_def_int8 = vm.declare_type(vm.next_id(), MuType_::int(8));
vm.set_name(type_def_int8.as_entity(), "int8");
let mut type_def_int32 = vm.declare_type(vm.next_id(), MuType_::int(32));
let type_def_int32 = vm.declare_type(vm.next_id(), MuType_::int(32));
vm.set_name(type_def_int32.as_entity(), "int32");
// .const @int_64_1 <@int_64> = 1
let mut const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
let const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
vm.set_name(const_def_int64_1.as_entity(), "int64_1");
// .funcsig @fac_sig = (@int_64) -> (@int_64)
let mut fac_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
let fac_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
vm.set_name(fac_sig.as_entity(), "fac_sig");
let mut type_def_funcref_fac = vm.declare_type(vm.next_id(), MuType_::funcref(fac_sig.clone()));
let type_def_funcref_fac = vm.declare_type(vm.next_id(), MuType_::funcref(fac_sig.clone()));
vm.set_name(type_def_funcref_fac.as_entity(), "fac_sig");
// .funcdecl @fac <@fac_sig>
let mut func = MuFunction::new(vm.next_id(), fac_sig.clone());
let func = MuFunction::new(vm.next_id(), fac_sig.clone());
vm.set_name(func.as_entity(), "fac");
let func_id = func.id();
vm.declare_func(func);
......@@ -358,28 +356,28 @@ pub fn global_access() -> VM {
// .typedef @int64 = int<64>
// .typedef @iref_int64 = iref<int<64>>
let mut type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
let type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
vm.set_name(type_def_int64.as_entity(), "int64");
let mut type_def_iref_int64 = vm.declare_type(vm.next_id(), MuType_::iref(type_def_int64.clone()));
let type_def_iref_int64 = vm.declare_type(vm.next_id(), MuType_::iref(type_def_int64.clone()));
vm.set_name(type_def_iref_int64.as_entity(), "iref_int64");
// .const @int_64_0 <@int_64> = 0
// .const @int_64_1 <@int_64> = 1
let mut const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
let const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
vm.set_name(const_def_int64_0.as_entity(), "int64_0");
let mut const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
let const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
vm.set_name(const_def_int64_1.as_entity(), "int64_1");
// .global @a <@int_64>
let mut global_a = vm.declare_global(vm.next_id(), type_def_int64.clone());
let global_a = vm.declare_global(vm.next_id(), type_def_int64.clone());
vm.set_name(global_a.as_entity(), "a");
// .funcsig @global_access_sig = () -> ()
let mut func_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![]);
let func_sig = vm.declare_func_sig(vm.next_id(), vec![type_def_int64.clone()], vec![]);
vm.set_name(func_sig.as_entity(), "global_access_sig");
// .funcdecl @global_access <@global_access_sig>
let mut func = MuFunction::new(vm.next_id(), func_sig.clone());
let func = MuFunction::new(vm.next_id(), func_sig.clone());
vm.set_name(func.as_entity(), "global_access");
let func_id = func.id();
vm.declare_func(func);
......
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