Commit cee7d9be authored by qinsoon's avatar qinsoon

[wip] refactoring

parent c626fc25
...@@ -92,14 +92,15 @@ impl MuFunctionVersion { ...@@ -92,14 +92,15 @@ impl MuFunctionVersion {
pub fn new_ssa(&mut self, id: MuID, tag: MuName, ty: P<MuType>) -> P<TreeNode> { pub fn new_ssa(&mut self, id: MuID, tag: MuName, ty: P<MuType>) -> P<TreeNode> {
self.context.value_tags.insert(tag, id); self.context.value_tags.insert(tag, id);
self.context.values.insert(id, SSAVarEntry{id: id, tag: tag, ty: ty.clone(), use_count: Cell::new(0), expr: None}); self.context.values.insert(id, SSAVarEntry{id: id, name: Some(tag), ty: ty.clone(), use_count: Cell::new(0), expr: None});
P(TreeNode { P(TreeNode {
id: id, id: id,
name: None, name: None,
op: pick_op_code_for_ssa(&ty), op: pick_op_code_for_ssa(&ty),
v: TreeNode_::Value(P(Value{ v: TreeNode_::Value(P(Value{
tag: tag, id: id,
name: Some(tag),
ty: ty, ty: ty,
v: Value_::SSAVar(id) v: Value_::SSAVar(id)
})) }))
...@@ -414,22 +415,7 @@ impl TreeNode { ...@@ -414,22 +415,7 @@ impl TreeNode {
impl fmt::Display for TreeNode { impl fmt::Display for TreeNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.v { match self.v {
TreeNode_::Value(ref pv) => { TreeNode_::Value(ref pv) => pv.fmt(f),
match pv.v {
Value_::SSAVar(id) => {
write!(f, "+({} %{}#{})", pv.ty, pv.tag, id)
},
Value_::Constant(ref c) => {
write!(f, "+({} {})", pv.ty, c)
},
Value_::Global(ref g) => {
write!(f, "+({} to GLOBAL {} @{})", pv.ty, g.ty, g.tag)
},
Value_::Memory(ref mem) => {
write!(f, "+({})", mem)
}
}
},
TreeNode_::Instruction(ref inst) => { TreeNode_::Instruction(ref inst) => {
write!(f, "+({})", inst) write!(f, "+({})", inst)
} }
...@@ -446,7 +432,8 @@ pub enum TreeNode_ { ...@@ -446,7 +432,8 @@ pub enum TreeNode_ {
/// always use with P<Value> /// always use with P<Value>
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Value { pub struct Value {
pub tag: MuName, pub id: MuID,
pub name: Option<MuName>,
pub ty: P<MuType>, pub ty: P<MuType>,
pub v: Value_ pub v: Value_
} }
...@@ -501,9 +488,16 @@ impl Value { ...@@ -501,9 +488,16 @@ impl Value {
impl fmt::Display for Value { impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let tag = {
if self.name.is_some() {
self.name.unwrap()
} else {
"???"
}
};
match self.v { match self.v {
Value_::SSAVar(id) => { Value_::SSAVar(id) => {
write!(f, "+({} %{}#{})", self.ty, self.tag, id) write!(f, "+({} %{}#{})", self.ty, tag, id)
}, },
Value_::Constant(ref c) => { Value_::Constant(ref c) => {
write!(f, "+({} {})", self.ty, c) write!(f, "+({} {})", self.ty, c)
...@@ -529,7 +523,7 @@ pub enum Value_ { ...@@ -529,7 +523,7 @@ pub enum Value_ {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SSAVarEntry { pub struct SSAVarEntry {
pub id: MuID, pub id: MuID,
pub tag: MuName, pub name: Option<MuName>,
pub ty: P<MuType>, pub ty: P<MuType>,
// how many times this entry is used // how many times this entry is used
...@@ -548,7 +542,11 @@ impl SSAVarEntry { ...@@ -548,7 +542,11 @@ impl SSAVarEntry {
impl fmt::Display for SSAVarEntry { impl fmt::Display for SSAVarEntry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}#{}", self.ty, self.tag, self.id) if self.name.is_some() {
write!(f, "{} {}#{}", self.ty, self.name.unwrap(), self.id)
} else {
write!(f, "{} {}#{}", self.ty, "???", self.id)
}
} }
} }
......
...@@ -77,7 +77,7 @@ impl MachineCode for ASMCode { ...@@ -77,7 +77,7 @@ impl MachineCode for ASMCode {
} }
fn replace_reg(&mut self, from: MuID, to: MuID) { fn replace_reg(&mut self, from: MuID, to: MuID) {
let to_reg_tag : MuName = backend::all_regs()[to].tag; let to_reg_tag : MuName = backend::all_regs()[to].name.unwrap();
let to_reg_string = "%".to_string() + to_reg_tag; let to_reg_string = "%".to_string() + to_reg_tag;
match self.reg_defines.get(&from) { match self.reg_defines.get(&from) {
...@@ -498,7 +498,7 @@ impl ASMCodeGen { ...@@ -498,7 +498,7 @@ impl ASMCodeGen {
let id = op.extract_ssa_id().unwrap(); let id = op.extract_ssa_id().unwrap();
if id < RESERVED_NODE_IDS_FOR_MACHINE { if id < RESERVED_NODE_IDS_FOR_MACHINE {
// machine reg // machine reg
format!("%{}", op.tag) format!("%{}", op.name.unwrap())
} else { } else {
// virtual register, use place holder // virtual register, use place holder
REG_PLACEHOLDER.clone() REG_PLACEHOLDER.clone()
......
...@@ -662,7 +662,8 @@ impl <'a> InstructionSelection { ...@@ -662,7 +662,8 @@ impl <'a> InstructionSelection {
TreeNode_::Value(ref pv) => { TreeNode_::Value(ref pv) => {
match pv.v { match pv.v {
Value_::SSAVar(_) => P(Value{ Value_::SSAVar(_) => P(Value{
tag: "", id: vm.next_id(),
name: None,
ty: types::get_referent_ty(& pv.ty).unwrap(), ty: types::get_referent_ty(& pv.ty).unwrap(),
v: Value_::Memory(MemoryLocation::Address{ v: Value_::Memory(MemoryLocation::Address{
base: pv.clone(), base: pv.clone(),
...@@ -678,7 +679,8 @@ impl <'a> InstructionSelection { ...@@ -678,7 +679,8 @@ impl <'a> InstructionSelection {
} else { } else {
// symbolic // symbolic
P(Value{ P(Value{
tag: "", id: vm.next_id(),
name: None,
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()),
......
...@@ -19,7 +19,8 @@ use compiler::backend::RegGroup; ...@@ -19,7 +19,8 @@ use compiler::backend::RegGroup;
macro_rules! GPR { macro_rules! GPR {
($name: expr, $id: expr) => { ($name: expr, $id: expr) => {
P(Value { P(Value {
tag: $name, id: $id,
name: Some($name),
ty: GPR_TY.clone(), ty: GPR_TY.clone(),
v: Value_::SSAVar($id) v: Value_::SSAVar($id)
}) })
...@@ -29,7 +30,8 @@ macro_rules! GPR { ...@@ -29,7 +30,8 @@ macro_rules! GPR {
macro_rules! FPR { macro_rules! FPR {
($name: expr, $id: expr) => { ($name: expr, $id: expr) => {
P(Value { P(Value {
tag: $name, id: $id,
name: Some($name),
ty: FPR_TY.clone(), ty: FPR_TY.clone(),
v: Value_::SSAVar($id) v: Value_::SSAVar($id)
}) })
...@@ -242,13 +244,13 @@ pub fn init_machine_regs_for_func (func_context: &mut FunctionContext) { ...@@ -242,13 +244,13 @@ pub fn init_machine_regs_for_func (func_context: &mut FunctionContext) {
let reg_id = reg.extract_ssa_id().unwrap(); let reg_id = reg.extract_ssa_id().unwrap();
let entry = SSAVarEntry { let entry = SSAVarEntry {
id: reg_id, id: reg_id,
tag: reg.tag, name: reg.name,
ty: reg.ty.clone(), ty: reg.ty.clone(),
use_count: Cell::new(0), use_count: Cell::new(0),
expr: None expr: None
}; };
func_context.value_tags.insert(reg.tag, reg_id); func_context.value_tags.insert(reg.name.unwrap(), reg_id);
func_context.values.insert(reg_id, entry); func_context.values.insert(reg_id, entry);
} }
} }
......
...@@ -67,7 +67,7 @@ impl CompilerPass for DefUse { ...@@ -67,7 +67,7 @@ impl CompilerPass for DefUse {
debug!("check use count for variables"); debug!("check use count for variables");
for entry in func.context.values.values() { for entry in func.context.values.values() {
debug!("{}#{}: {}", entry.tag, entry.id, entry.use_count.get()) debug!("{}: {}", entry, entry.use_count.get())
} }
} }
} }
...@@ -78,24 +78,25 @@ impl <'a> VM { ...@@ -78,24 +78,25 @@ impl <'a> VM {
self.is_running.load(Ordering::Relaxed) self.is_running.load(Ordering::Relaxed)
} }
pub fn declare_const(&self, const_name: MuName, ty: P<MuType>, val: Constant) -> P<Value> { pub fn declare_const(&self, id: MuID, const_name: MuName, 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(const_name));
let ret = P(Value{tag: const_name, ty: ty, v: Value_::Constant(val)}); let ret = P(Value{id: id, name: Some(const_name), ty: ty, v: Value_::Constant(val)});
constants.insert(const_name, ret.clone()); constants.insert(const_name, ret.clone());
ret ret
} }
pub fn declare_global(&self, global_name: MuName, ty: P<MuType>) -> P<Value> { pub fn declare_global(&self, id: MuID, global_name: MuName, ty: P<MuType>) -> P<Value> {
let global = P(GlobalCell{tag: global_name, ty: ty.clone()}); let global = P(GlobalCell{tag: global_name, ty: ty.clone()});
let mut globals = self.globals.write().unwrap(); let mut globals = self.globals.write().unwrap();
globals.insert(global_name, global.clone()); globals.insert(global_name, global.clone());
P(Value{ P(Value{
tag: "", id: id,
name: Some(global_name),
ty: P(MuType::iref(ty)), ty: P(MuType::iref(ty)),
v: Value_::Global(global.clone()) v: Value_::Global(global.clone())
}) })
......
...@@ -38,8 +38,8 @@ pub fn sum() -> VM { ...@@ -38,8 +38,8 @@ 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("int64_0", type_def_int64.clone(), Constant::Int(0)); let const_def_int64_0 = vm.declare_const(vm.next_id(), "int64_0", type_def_int64.clone(), Constant::Int(0));
let const_def_int64_1 = vm.declare_const("int64_1", type_def_int64.clone(), Constant::Int(1)); let const_def_int64_1 = vm.declare_const(vm.next_id(), "int64_1", type_def_int64.clone(), Constant::Int(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()]);
...@@ -192,7 +192,7 @@ pub fn factorial() -> VM { ...@@ -192,7 +192,7 @@ pub fn factorial() -> VM {
let type_def_int32 = vm.declare_type("int32", P(MuType::int(32))); let type_def_int32 = vm.declare_type("int32", P(MuType::int(32)));
// .const @int_64_1 <@int_64> = 1 // .const @int_64_1 <@int_64> = 1
let const_def_int64_1 = vm.declare_const("int64_1", type_def_int64.clone(), Constant::Int(1)); let const_def_int64_1 = vm.declare_const(vm.next_id(), "int64_1", type_def_int64.clone(), Constant::Int(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()]);
...@@ -204,7 +204,7 @@ pub fn factorial() -> VM { ...@@ -204,7 +204,7 @@ 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("fac", type_def_funcref_fac, Constant::FuncRef("fac")); let const_func_fac = vm.declare_const(vm.next_id(), "fac", type_def_funcref_fac, Constant::FuncRef("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):
...@@ -347,11 +347,11 @@ pub fn global_access() -> VM { ...@@ -347,11 +347,11 @@ 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("int64_0", type_def_int64.clone(), Constant::Int(0)); let const_def_int64_0 = vm.declare_const(vm.next_id(), "int64_0", type_def_int64.clone(), Constant::Int(0));
let const_def_int64_1 = vm.declare_const("int64_1", type_def_int64.clone(), Constant::Int(1)); let const_def_int64_1 = vm.declare_const(vm.next_id(), "int64_1", type_def_int64.clone(), Constant::Int(1));
// .global @a <@int_64> // .global @a <@int_64>
let global_a = vm.declare_global("a", type_def_int64.clone()); let global_a = vm.declare_global(vm.next_id(), "a", type_def_int64.clone());
// .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