WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

Commit cdb01047 authored by qinsoon's avatar qinsoon
Browse files

[wip] working on coloring

parent 27231f1d
pub use inst_sel; pub use inst_sel;
\ No newline at end of file
pub const GPR_COUNT : usize = 16;
pub const FPR_COUNT : usize = 16;
\ No newline at end of file
...@@ -116,6 +116,9 @@ lazy_static!{ ...@@ -116,6 +116,9 @@ lazy_static!{
pub static ref CALLEE_SAVED_FPRs : [P<Value>; 0] = []; pub static ref CALLEE_SAVED_FPRs : [P<Value>; 0] = [];
} }
pub const GPR_COUNT : usize = 16;
pub const FPR_COUNT : usize = 16;
lazy_static! { lazy_static! {
pub static ref ALL_MACHINE_REGs : Vec<P<Value>> = vec![ pub static ref ALL_MACHINE_REGs : Vec<P<Value>> = vec![
RAX.clone(), RAX.clone(),
......
...@@ -10,4 +10,14 @@ mod x86_64; ...@@ -10,4 +10,14 @@ mod x86_64;
mod arm; mod arm;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::get_name_for_value; pub use compiler::backend::x86_64::get_name_for_value;
\ No newline at end of file
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::GPR_COUNT;
#[cfg(target_arch = "x86_64")]
pub use compiler::backend::x86_64::FPR_COUNT;
#[cfg(target_arch = "arm")]
pub use compiler::backend::arm::GPR_COUNT;
#[cfg(target_arch = "arm")]
pub use compiler::backend::arm::FPR_COUNT;
\ No newline at end of file
use compiler::backend::reg_alloc::liveness::InterferenceGraph;
use vm::machine_code::CompiledFunction;
pub struct GraphColoring <'a> {
ig: InterferenceGraph,
cur_cf: &'a CompiledFunction,
}
impl <'a> GraphColoring <'a> {
pub fn start (cf: &CompiledFunction, ig: InterferenceGraph) {
let mut coloring = GraphColoring {
ig: ig,
cur_cf: cf,
};
coloring.init();
}
fn init (&mut self) {
}
}
\ No newline at end of file
...@@ -10,10 +10,10 @@ use std::collections::{HashMap, HashSet}; ...@@ -10,10 +10,10 @@ use std::collections::{HashMap, HashSet};
use self::nalgebra::DMatrix; use self::nalgebra::DMatrix;
type MatrixIndex = usize; type Node = usize;
pub struct InterferenceGraph { pub struct InterferenceGraph {
nodes: HashMap<MuID, MatrixIndex>, nodes: HashMap<MuID, Node>,
matrix: Option<DMatrix<bool>>, matrix: Option<DMatrix<bool>>,
color: HashMap<MuID, MuID>, color: HashMap<MuID, MuID>,
...@@ -31,21 +31,21 @@ impl InterferenceGraph { ...@@ -31,21 +31,21 @@ impl InterferenceGraph {
} }
} }
fn new_node(&mut self, node: MuID) -> MatrixIndex { fn new_node(&mut self, reg: MuID) -> Node {
if !self.nodes.contains_key(&node) { if !self.nodes.contains_key(&reg) {
let index = self.nodes.len(); let index = self.nodes.len();
self.nodes.insert(node, index); self.nodes.insert(reg, index);
index index
} else { } else {
* self.nodes.get(&node).unwrap() * self.nodes.get(&reg).unwrap()
} }
} }
fn get_node(&self, node: MuID) -> MatrixIndex { fn get_node(&self, reg: MuID) -> Node {
match self.nodes.get(&node) { match self.nodes.get(&reg) {
Some(index) => *index, Some(index) => *index,
None => panic!("do not have a node for {}", node) None => panic!("do not have a node for {}", reg)
} }
} }
...@@ -65,28 +65,28 @@ impl InterferenceGraph { ...@@ -65,28 +65,28 @@ impl InterferenceGraph {
self.matrix.as_mut().unwrap()[(from_ix, to_ix)] = true; self.matrix.as_mut().unwrap()[(from_ix, to_ix)] = true;
} }
fn color_node(&mut self, node: MuID, color: MuID) { fn color_node(&mut self, reg: MuID, color: MuID) {
self.color.insert(node, color); self.color.insert(reg, color);
} }
fn node_has_color(&self, node: MuID) -> bool { fn node_has_color(&self, reg: MuID) -> bool {
self.color.contains_key(&node) self.color.contains_key(&reg)
} }
fn is_same_node(&self, node1: MuID, node2: MuID) -> bool { fn is_same_node(&self, reg1: MuID, reg2: MuID) -> bool {
let ix1 = self.get_node(node1); let node1 = self.get_node(reg1);
let ix2 = self.get_node(node2); let node2 = self.get_node(reg2);
ix1 == ix2 node1 == node2
} }
fn is_adj(&self, from: MuID, to: MuID) -> bool { fn is_adj(&self, from: MuID, to: MuID) -> bool {
let from_ix = self.get_node(from); let from_node = self.get_node(from);
let to_ix = self.get_node(to); let to_node = self.get_node(to);
let ref matrix = self.matrix.as_ref().unwrap(); let ref matrix = self.matrix.as_ref().unwrap();
matrix[(from_ix, to_ix)] || matrix[(to_ix, from_ix)] matrix[(from_node, to_node)] || matrix[(to_node, from_node)]
} }
pub fn print(&self) { pub fn print(&self) {
...@@ -103,11 +103,11 @@ impl InterferenceGraph { ...@@ -103,11 +103,11 @@ impl InterferenceGraph {
} }
println!("graph:"); println!("graph:");
{ {
let idx_to_node_id = { let node_to_reg_id = {
let mut ret : HashMap<MatrixIndex, MuID> = HashMap::new(); let mut ret : HashMap<Node, MuID> = HashMap::new();
for node_id in self.nodes.keys() { for reg in self.nodes.keys() {
ret.insert(*self.nodes.get(node_id).unwrap(), *node_id); ret.insert(*self.nodes.get(reg).unwrap(), *reg);
} }
ret ret
...@@ -117,8 +117,8 @@ impl InterferenceGraph { ...@@ -117,8 +117,8 @@ impl InterferenceGraph {
for i in 0..matrix.ncols() { for i in 0..matrix.ncols() {
for j in 0..matrix.nrows() { for j in 0..matrix.nrows() {
if matrix[(i, j)] { if matrix[(i, j)] {
let from_node = idx_to_node_id.get(&i).unwrap(); let from_node = node_to_reg_id.get(&i).unwrap();
let to_node = idx_to_node_id.get(&j).unwrap(); let to_node = node_to_reg_id.get(&j).unwrap();
println!("Node {} -> Node {}", from_node, to_node); println!("Node {} -> Node {}", from_node, to_node);
} }
...@@ -146,7 +146,7 @@ impl InterferenceGraph { ...@@ -146,7 +146,7 @@ impl InterferenceGraph {
println!("graph:"); println!("graph:");
{ {
let idx_to_node_id = { let idx_to_node_id = {
let mut ret : HashMap<MatrixIndex, MuID> = HashMap::new(); let mut ret : HashMap<Node, MuID> = HashMap::new();
for node_id in self.nodes.keys() { for node_id in self.nodes.keys() {
ret.insert(*self.nodes.get(node_id).unwrap(), *node_id); ret.insert(*self.nodes.get(node_id).unwrap(), *node_id);
......
...@@ -3,6 +3,7 @@ use ast::ir::*; ...@@ -3,6 +3,7 @@ use ast::ir::*;
use vm::context::VMContext; use vm::context::VMContext;
mod liveness; mod liveness;
mod coloring;
pub struct RegisterAllocation { pub struct RegisterAllocation {
name: &'static str name: &'static str
...@@ -30,5 +31,7 @@ impl CompilerPass for RegisterAllocation { ...@@ -30,5 +31,7 @@ impl CompilerPass for RegisterAllocation {
let liveness = liveness::build(&mut cf); let liveness = liveness::build(&mut cf);
liveness.print(); liveness.print();
coloring::GraphColoring::start(&mut cf, liveness);
} }
} }
\ No newline at end of file
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