Commit 116e0e2d authored by qinsoon's avatar qinsoon

[wip] refactoring const/global

parent e716d22f
...@@ -506,8 +506,8 @@ impl fmt::Display for Value { ...@@ -506,8 +506,8 @@ impl fmt::Display for Value {
Value_::Constant(ref c) => { Value_::Constant(ref c) => {
write!(f, "+({} {})", self.ty, c) write!(f, "+({} {})", self.ty, c)
}, },
Value_::Global(ref g) => { Value_::Global => {
write!(f, "+({} to GLOBAL {} @{})", self.ty, g.ty, g.tag) write!(f, "+(GLOBAL {} @{})", self.ty, self.name.unwrap())
}, },
Value_::Memory(ref mem) => { Value_::Memory(ref mem) => {
write!(f, "+({})", mem) write!(f, "+({})", mem)
...@@ -520,7 +520,7 @@ impl fmt::Display for Value { ...@@ -520,7 +520,7 @@ impl fmt::Display for Value {
pub enum Value_ { pub enum Value_ {
SSAVar(MuID), SSAVar(MuID),
Constant(Constant), Constant(Constant),
Global(P<GlobalCell>), Global,
Memory(MemoryLocation) Memory(MemoryLocation)
} }
...@@ -619,12 +619,6 @@ impl fmt::Display for MemoryLocation { ...@@ -619,12 +619,6 @@ impl fmt::Display for MemoryLocation {
} }
} }
#[derive(Debug, Clone, PartialEq)]
pub struct GlobalCell {
pub tag: MuName,
pub ty: P<MuType>
}
pub fn op_vector_str(vec: &Vec<OpIndex>, ops: &Vec<P<TreeNode>>) -> String { pub fn op_vector_str(vec: &Vec<OpIndex>, ops: &Vec<P<TreeNode>>) -> String {
let mut ret = String::new(); let mut ret = String::new();
for i in 0..vec.len() { for i in 0..vec.len() {
......
...@@ -1131,14 +1131,14 @@ pub fn emit_context(vm: &VM) { ...@@ -1131,14 +1131,14 @@ pub fn emit_context(vm: &VM) {
file.write_fmt(format_args!("\t.bss\n")).unwrap(); file.write_fmt(format_args!("\t.bss\n")).unwrap();
let globals = vm.globals().read().unwrap(); let globals = vm.globals().read().unwrap();
for cell in globals.values() { for global in globals.values() {
let (size, align) = { let (size, align) = {
let ty_info = vm.get_backend_type_info(&cell.ty); let ty_info = vm.get_backend_type_info(&global.ty);
(ty_info.size, ty_info.alignment) (ty_info.size, ty_info.alignment)
}; };
file.write_fmt(format_args!("\t{}\n", directive_globl(symbol(cell.tag)))).unwrap(); file.write_fmt(format_args!("\t{}\n", directive_globl(symbol(global.name.unwrap())))).unwrap();
file.write_fmt(format_args!("\t{}\n", directive_comm(symbol(cell.tag), size, align))).unwrap(); file.write_fmt(format_args!("\t{}\n", directive_comm(symbol(global.name.unwrap()), size, align))).unwrap();
file.write("\n".as_bytes()).unwrap(); file.write("\n".as_bytes()).unwrap();
} }
......
...@@ -621,7 +621,7 @@ impl <'a> InstructionSelection { ...@@ -621,7 +621,7 @@ impl <'a> InstructionSelection {
TreeNode_::Value(ref pv) => { TreeNode_::Value(ref pv) => {
match pv.v { match pv.v {
Value_::Constant(_) Value_::Constant(_)
| Value_::Global(_) | Value_::Global
| Value_::Memory(_) => panic!("expected ireg"), | Value_::Memory(_) => panic!("expected ireg"),
Value_::SSAVar(_) => { Value_::SSAVar(_) => {
pv.clone() pv.clone()
...@@ -672,7 +672,7 @@ impl <'a> InstructionSelection { ...@@ -672,7 +672,7 @@ impl <'a> InstructionSelection {
scale: None scale: None
}) })
}), }),
Value_::Global(ref glob) => { Value_::Global => {
if vm.is_running() { if vm.is_running() {
// get address from vm // get address from vm
unimplemented!() unimplemented!()
...@@ -684,7 +684,7 @@ impl <'a> InstructionSelection { ...@@ -684,7 +684,7 @@ impl <'a> InstructionSelection {
ty: types::get_referent_ty(&pv.ty).unwrap(), ty: types::get_referent_ty(&pv.ty).unwrap(),
v: Value_::Memory(MemoryLocation::Symbolic{ v: Value_::Memory(MemoryLocation::Symbolic{
base: Some(x86_64::RIP.clone()), base: Some(x86_64::RIP.clone()),
label: glob.tag label: pv.name.unwrap()
}) })
}) })
} }
......
...@@ -81,7 +81,7 @@ pub type MuChildNode = MuIRNode; ...@@ -81,7 +81,7 @@ pub type MuChildNode = MuIRNode;
pub type MuTypeNode = P<MuType>; pub type MuTypeNode = P<MuType>;
pub type MuFuncSigNode= P<MuFuncSig>; pub type MuFuncSigNode= P<MuFuncSig>;
pub type MuConstNode = P<Value>; pub type MuConstNode = P<Value>;
pub type MuGlobalNode = P<GlobalCell>; pub type MuGlobalNode = P<Value>;
pub type MuFuncNode = RefCell<MuFunction>; pub type MuFuncNode = RefCell<MuFunction>;
pub type MuFuncVerNode= RefCell<MuFunctionVersion>; pub type MuFuncVerNode= RefCell<MuFunctionVersion>;
pub type MuBBNode = P<Block>; pub type MuBBNode = P<Block>;
...@@ -445,4 +445,5 @@ mu_entity!(MuFunction); ...@@ -445,4 +445,5 @@ mu_entity!(MuFunction);
mu_entity!(MuFunctionVersion); mu_entity!(MuFunctionVersion);
mu_entity!(Block); mu_entity!(Block);
mu_entity!(TreeNode); mu_entity!(TreeNode);
mu_entity!(MuType); mu_entity!(MuType);
\ No newline at end of file mu_entity!(Value);
\ No newline at end of file
...@@ -8,7 +8,7 @@ pub struct MuBundle { ...@@ -8,7 +8,7 @@ pub struct MuBundle {
pub type_defs: HashMap<MuID, P<MuType>>, pub type_defs: HashMap<MuID, P<MuType>>,
pub func_sigs: HashMap<MuID, P<MuFuncSig>>, pub func_sigs: HashMap<MuID, P<MuFuncSig>>,
pub constants: HashMap<MuID, P<Value>>, pub constants: HashMap<MuID, P<Value>>,
pub globals : HashMap<MuID, P<GlobalCell>>, pub globals : HashMap<MuID, P<Value>>,
pub func_defs: HashMap<MuID, MuFunction>, pub func_defs: HashMap<MuID, MuFunction>,
pub func_decls: HashMap<MuID, MuFunctionVersion>, pub func_decls: HashMap<MuID, MuFunctionVersion>,
......
...@@ -22,12 +22,11 @@ pub struct VM { ...@@ -22,12 +22,11 @@ pub struct VM {
id_name_map: RwLock<HashMap<MuID, MuName>>, id_name_map: RwLock<HashMap<MuID, MuName>>,
name_id_map: RwLock<HashMap<MuName, MuID>>, name_id_map: RwLock<HashMap<MuName, MuID>>,
constants: RwLock<HashMap<MuName, P<Value>>>,
types: RwLock<HashMap<MuID, P<MuType>>>, types: RwLock<HashMap<MuID, P<MuType>>>,
backend_type_info: RwLock<HashMap<P<MuType>, BackendTypeInfo>>, backend_type_info: RwLock<HashMap<P<MuType>, BackendTypeInfo>>,
globals: RwLock<HashMap<MuName, P<GlobalCell>>>, constants: RwLock<HashMap<MuID, P<Value>>>,
globals: RwLock<HashMap<MuID, P<Value>>>,
func_sigs: RwLock<HashMap<MuName, P<MuFuncSig>>>, func_sigs: RwLock<HashMap<MuName, P<MuFuncSig>>>,
func_vers: RwLock<HashMap<(MuID, MuID), RefCell<MuFunctionVersion>>>, func_vers: RwLock<HashMap<(MuID, MuID), RefCell<MuFunctionVersion>>>,
...@@ -79,28 +78,28 @@ impl <'a> VM { ...@@ -79,28 +78,28 @@ impl <'a> VM {
self.is_running.load(Ordering::Relaxed) self.is_running.load(Ordering::Relaxed)
} }
pub fn declare_const(&self, id: MuID, const_name: MuName, ty: P<MuType>, val: Constant) -> P<Value> { pub fn declare_const(&self, id: MuID, ty: P<MuType>, val: Constant) -> P<Value> {
let mut constants = self.constants.write().unwrap(); let mut constants = self.constants.write().unwrap();
debug_assert!(!constants.contains_key(const_name)); debug_assert!(!constants.contains_key(&id));
let ret = P(Value{id: id, name: Some(const_name), ty: ty, v: Value_::Constant(val)}); let ret = P(Value{id: id, name: None, ty: ty, v: Value_::Constant(val)});
constants.insert(const_name, ret.clone()); constants.insert(id, ret.clone());
ret ret
} }
pub fn declare_global(&self, id: MuID, global_name: MuName, ty: P<MuType>) -> P<Value> { pub fn declare_global(&self, id: MuID, ty: P<MuType>) -> P<Value> {
let global = P(GlobalCell{tag: global_name, ty: ty.clone()}); let global = P(Value{
id: id,
name: None,
ty: P(MuType::new(self.next_id(), MuType_::iref(ty))),
v: Value_::Global
});
let mut globals = self.globals.write().unwrap(); let mut globals = self.globals.write().unwrap();
globals.insert(global_name, global.clone()); globals.insert(id, global.clone());
P(Value{ global
id: id,
name: Some(global_name),
ty: P(MuType::new(self.next_id(), MuType_::iref(ty))),
v: Value_::Global(global.clone())
})
} }
pub fn declare_type(&self, id: MuID, ty: MuType_) -> P<MuType> { pub fn declare_type(&self, id: MuID, ty: MuType_) -> P<MuType> {
...@@ -190,7 +189,7 @@ impl <'a> VM { ...@@ -190,7 +189,7 @@ impl <'a> VM {
*self.id_name_map.read().unwrap().get(&id).unwrap() *self.id_name_map.read().unwrap().get(&id).unwrap()
} }
pub fn globals(&self) -> &RwLock<HashMap<MuName, P<GlobalCell>>> { pub fn globals(&self) -> &RwLock<HashMap<MuID, P<Value>>> {
&self.globals &self.globals
} }
......
...@@ -40,8 +40,10 @@ pub fn sum() -> VM { ...@@ -40,8 +40,10 @@ pub fn sum() -> VM {
// .const @int_64_0 <@int_64> = 0 // .const @int_64_0 <@int_64> = 0
// .const @int_64_1 <@int_64> = 1 // .const @int_64_1 <@int_64> = 1
let const_def_int64_0 = vm.declare_const(vm.next_id(), "int64_0", type_def_int64.clone(), Constant::Int(0)); let mut const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
let const_def_int64_1 = vm.declare_const(vm.next_id(), "int64_1", type_def_int64.clone(), Constant::Int(1)); const_def_int64_0.set_name("int64_0");
let mut const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
const_def_int64_1.set_name("int64_1");
// .funcsig @sum_sig = (@int_64) -> (@int_64) // .funcsig @sum_sig = (@int_64) -> (@int_64)
let sum_sig = vm.declare_func_sig("sum_sig", vec![type_def_int64.clone()], vec![type_def_int64.clone()]); let sum_sig = vm.declare_func_sig("sum_sig", vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
...@@ -201,7 +203,8 @@ pub fn factorial() -> VM { ...@@ -201,7 +203,8 @@ pub fn factorial() -> VM {
type_def_int32.set_name("int32"); type_def_int32.set_name("int32");
// .const @int_64_1 <@int_64> = 1 // .const @int_64_1 <@int_64> = 1
let const_def_int64_1 = vm.declare_const(vm.next_id(), "int64_1", type_def_int64.clone(), Constant::Int(1)); let mut const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
const_def_int64_1.set_name("int64_1");
// .funcsig @fac_sig = (@int_64) -> (@int_64) // .funcsig @fac_sig = (@int_64) -> (@int_64)
let fac_sig = vm.declare_func_sig("fac_sig", vec![type_def_int64.clone()], vec![type_def_int64.clone()]); let fac_sig = vm.declare_func_sig("fac_sig", vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
...@@ -214,7 +217,8 @@ pub fn factorial() -> VM { ...@@ -214,7 +217,8 @@ pub fn factorial() -> VM {
vm.declare_func(func); vm.declare_func(func);
// .funcdef @fac VERSION @fac_v1 <@fac_sig> // .funcdef @fac VERSION @fac_v1 <@fac_sig>
let const_func_fac = vm.declare_const(vm.next_id(), "fac", type_def_funcref_fac, Constant::FuncRef("fac")); let mut const_func_fac = vm.declare_const(vm.next_id(), type_def_funcref_fac, Constant::FuncRef("fac"));
const_func_fac.set_name("fac");
let mut func_ver = MuFunctionVersion::new(vm.next_id(), func_id, fac_sig.clone()); let mut func_ver = MuFunctionVersion::new(vm.next_id(), func_id, fac_sig.clone());
// %blk_0(<@int_64> %n_3): // %blk_0(<@int_64> %n_3):
...@@ -359,11 +363,14 @@ pub fn global_access() -> VM { ...@@ -359,11 +363,14 @@ pub fn global_access() -> VM {
// .const @int_64_0 <@int_64> = 0 // .const @int_64_0 <@int_64> = 0
// .const @int_64_1 <@int_64> = 1 // .const @int_64_1 <@int_64> = 1
let const_def_int64_0 = vm.declare_const(vm.next_id(), "int64_0", type_def_int64.clone(), Constant::Int(0)); let mut const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
let const_def_int64_1 = vm.declare_const(vm.next_id(), "int64_1", type_def_int64.clone(), Constant::Int(1)); const_def_int64_0.set_name("int64_0");
let mut const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
const_def_int64_1.set_name("int64_1");
// .global @a <@int_64> // .global @a <@int_64>
let global_a = vm.declare_global(vm.next_id(), "a", type_def_int64.clone()); let mut global_a = vm.declare_global(vm.next_id(), type_def_int64.clone());
global_a.set_name("a");
// .funcsig @global_access_sig = () -> () // .funcsig @global_access_sig = () -> ()
let func_sig = vm.declare_func_sig("global_access_sig", vec![type_def_int64.clone()], vec![]); let func_sig = vm.declare_func_sig("global_access_sig", vec![type_def_int64.clone()], vec![]);
......
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