Commit d70ebbd5 authored by qinsoon's avatar qinsoon

[wip] inst select can walk through a factorial tree. One test is broken, gonna fix it

parent c910df03
......@@ -211,9 +211,9 @@ pub enum EdgeKind {
#[derive(Debug)]
pub struct BlockContent {
pub args: Vec<P<TreeNode>>,
pub args: Vec<P<Value>>,
pub body: Vec<P<TreeNode>>,
pub keepalives: Option<Vec<P<TreeNode>>>
pub keepalives: Option<Vec<P<Value>>>
}
#[derive(Debug, Clone)]
......@@ -310,6 +310,19 @@ impl Value {
}
}
pub fn is_fp_reg(&self) -> bool {
match self.v {
Value_::SSAVar(_) => {
if is_scalar(&self.ty) && is_fp(&self.ty) {
true
} else {
false
}
},
_ => false
}
}
pub fn is_int_const(&self) -> bool {
match self.v {
Value_::Constant(_) => {
......@@ -382,8 +395,8 @@ pub enum Constant {
Float(f32),
Double(f64),
IRef(Address),
FuncRef(Address),
UFuncRef(Address),
FuncRef(MuTag),
UFuncRef(MuTag),
Vector(Vec<Constant>),
}
......
......@@ -66,6 +66,14 @@ impl CodeGenerator for ASMCodeGen {
trace!("emit: sub {}, {} -> {}", dest, src, dest);
}
fn emit_mul_r64(&mut self, src: &P<Value>) {
trace!("emit: mul rax, {} -> rax", src);
}
fn emit_mul_mem64(&mut self, src: &P<Value>) {
trace!("emit: mul rax, {} -> rax", src);
}
fn emit_jmp(&mut self, dest: &Destination) {
trace!("emit: jmp {}", dest.target);
}
......@@ -110,11 +118,27 @@ impl CodeGenerator for ASMCodeGen {
trace!("emit: jle {}", dest.target);
}
fn emit_call(&mut self, func: &P<Value>) {
fn emit_call_near_rel32(&mut self, func: MuTag) {
trace!("emit: call {}", func);
}
fn emit_call_near_r64(&mut self, func: &P<Value>) {
trace!("emit: call {}", func);
}
fn emit_call_near_mem64(&mut self, func: &P<Value>) {
trace!("emit: call {}", func);
}
fn emit_ret(&mut self) {
trace!("emit: ret");
}
fn emit_push(&mut self, src: &P<Value>) {
trace!("emit: push {}", src);
}
fn emit_pop(&mut self, dest: &P<Value>) {
trace!("emit: pop {}", dest);
}
}
\ No newline at end of file
......@@ -19,6 +19,9 @@ pub trait CodeGenerator {
fn emit_sub_r64_mem64(&mut self, dest: &P<Value>, src: &P<Value>);
fn emit_sub_r64_imm32(&mut self, dest: &P<Value>, src: u32);
fn emit_mul_r64(&mut self, src: &P<Value>);
fn emit_mul_mem64(&mut self, src: &P<Value>);
fn emit_jmp(&mut self, dest: &Destination);
fn emit_je(&mut self, dest: &Destination);
fn emit_jne(&mut self, dest: &Destination);
......@@ -31,6 +34,12 @@ pub trait CodeGenerator {
fn emit_jl(&mut self, dest: &Destination);
fn emit_jle(&mut self, dest: &Destination);
fn emit_call(&mut self, func: &P<Value>);
fn emit_call_near_rel32(&mut self, func: MuTag);
fn emit_call_near_r64(&mut self, func: &P<Value>);
fn emit_call_near_mem64(&mut self, func: &P<Value>);
fn emit_ret(&mut self);
fn emit_push(&mut self, src: &P<Value>);
fn emit_pop(&mut self, dest: &P<Value>);
}
\ No newline at end of file
This diff is collapsed.
......@@ -17,18 +17,22 @@ impl DefUse {
fn use_op(op: &P<TreeNode>, func_context: &mut FunctionContext) {
match op.v {
TreeNode_::Value(ref val) => {
match val.v {
Value_::SSAVar(ref id) => {
let entry = func_context.values.get_mut(id).unwrap();
entry.use_count.set(entry.use_count.get() + 1);
},
_ => {} // dont worry about constants
}
use_value(val, func_context);
},
_ => {} // dont worry about instruction
}
}
fn use_value(val: &P<Value>, func_context: &mut FunctionContext) {
match val.v {
Value_::SSAVar(ref id) => {
let entry = func_context.values.get_mut(id).unwrap();
entry.use_count.set(entry.use_count.get() + 1);
},
_ => {} // dont worry about constants
}
}
impl CompilerPass for DefUse {
fn name(&self) -> &'static str {
self.name
......@@ -40,7 +44,7 @@ impl CompilerPass for DefUse {
let ref mut keepalives = block.content.as_mut().unwrap().keepalives;
if keepalives.is_some() {
for op in keepalives.as_mut().unwrap().iter_mut() {
use_op(op, func_context);
use_value(op, func_context);
}
}
}
......
......@@ -57,7 +57,7 @@ pub fn sum() -> VMContext {
});
let blk_entry_content = BlockContent {
args: vec![blk_entry_n.clone()],
args: vec![blk_entry_n.clone_value()],
body: vec![blk_entry_term],
keepalives: None
};
......@@ -112,7 +112,7 @@ pub fn sum() -> VMContext {
});
let blk_head_content = BlockContent {
args: vec![blk_head_n.clone(), blk_head_s.clone(), blk_head_i.clone()],
args: vec![blk_head_n.clone_value(), blk_head_s.clone_value(), blk_head_i.clone_value()],
body: vec![blk_head_inst0, blk_head_inst1, blk_head_inst2, blk_head_term],
keepalives: None
};
......@@ -130,7 +130,7 @@ pub fn sum() -> VMContext {
});
let blk_ret_content = BlockContent {
args: vec![blk_ret_s.clone()],
args: vec![blk_ret_s.clone_value()],
body: vec![blk_ret_term],
keepalives: None
};
......@@ -177,8 +177,10 @@ pub fn factorial() -> VMContext {
// .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 type_def_funcref_fac = vm.declare_type("fac_sig", P(MuType::funcref(fac_sig.clone())));
// .funcdef @fac VERSION @fac_v1 <@fac_sig>
let const_func_fac = vm.declare_const("fac", type_def_funcref_fac, Constant::FuncRef("fac"));
let mut func = MuFunction::new("fac", fac_sig.clone());
// %blk_0(<@int_64> %n_3):
......@@ -213,7 +215,7 @@ pub fn factorial() -> VMContext {
});
let blk_0_content = BlockContent {
args: vec![blk_0_n_3.clone()],
args: vec![blk_0_n_3.clone_value()],
body: vec![blk_0_inst0, blk_0_term],
keepalives: None
};
......@@ -231,7 +233,7 @@ pub fn factorial() -> VMContext {
});
let blk_2_content = BlockContent {
args: vec![blk_2_v53.clone()],
args: vec![blk_2_v53.clone_value()],
body: vec![blk_2_term],
keepalives: None
};
......@@ -251,7 +253,7 @@ pub fn factorial() -> VMContext {
// %v51 = CALL <@fac_sig> @fac (%v50)
let blk_1_v51 = func.new_ssa("blk_1_v51", type_def_int64.clone());
let blk_1_fac = func.new_ssa("blk_1_fac", P(MuType::funcref(fac_sig.clone())));
let blk_1_fac = func.new_constant(const_func_fac.clone());
let blk_1_inst1 = func.new_inst(Instruction{
value: Some(vec![blk_1_v51.clone_value()]),
ops: RefCell::new(vec![blk_1_fac, blk_1_v50.clone()]),
......@@ -283,7 +285,7 @@ pub fn factorial() -> VMContext {
});
let blk_1_content = BlockContent {
args: vec![blk_1_n_3.clone()],
args: vec![blk_1_n_3.clone_value()],
body: vec![blk_1_inst0, blk_1_inst1, blk_1_inst2, blk_1_term],
keepalives: None
};
......
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