Commit cee7d9be authored by qinsoon's avatar qinsoon

[wip] refactoring

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