GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

Commit 287179b6 authored by qinsoon's avatar qinsoon

introduce a macro to make code look simpler

parent a5745df4
......@@ -274,9 +274,7 @@ impl ASMCode {
let ref mut asm = self.code;
for i in 0..n_insts {
if TRACE_CFA {
trace!("---inst {}---", i);
}
trace_if!(TRACE_CFA, "---inst {}---", i);
// skip symbol
if asm[i].is_symbol {
......@@ -298,9 +296,7 @@ impl ASMCode {
if !asm[i].preds.contains(&last_inst) {
asm[i].preds.push(last_inst);
if TRACE_CFA {
trace!("inst {}: set PREDS as previous inst - fallthrough {}", i, last_inst);
}
trace_if!(TRACE_CFA, "inst {}: set PREDS as previous inst - fallthrough {}", i, last_inst);
}
}
// otherwise do nothing
......@@ -324,12 +320,10 @@ impl ASMCode {
// target's pred is cur
asm[target_n].preds.push(i);
if TRACE_CFA {
trace!("inst {}: is a branch to {}", i, target);
trace!("inst {}: branch target index is {}", i, target_n);
trace!("inst {}: set SUCCS as branch target {}", i, target_n);
trace!("inst {}: set PREDS as branch source {}", target_n, i);
}
trace_if!(TRACE_CFA, "inst {}: is a branch to {}", i, target);
trace_if!(TRACE_CFA, "inst {}: branch target index is {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set SUCCS as branch target {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set PREDS as branch source {}", target_n, i);
},
ASMBranchTarget::Conditional(ref target) => {
// branch to target
......@@ -338,17 +332,13 @@ impl ASMCode {
// cur insts' succ is target
asm[i].succs.push(target_n);
if TRACE_CFA {
trace!("inst {}: is a cond branch to {}", i, target);
trace!("inst {}: branch target index is {}", i, target_n);
trace!("inst {}: set SUCCS as branch target {}", i, target_n);
}
trace_if!(TRACE_CFA, "inst {}: is a cond branch to {}", i, target);
trace_if!(TRACE_CFA, "inst {}: branch target index is {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set SUCCS as branch target {}", i, target_n);
// target's pred is cur
asm[target_n].preds.push(i);
if TRACE_CFA {
trace!("inst {}: set PREDS as {}", target_n, i);
}
trace_if!(TRACE_CFA, "inst {}: set PREDS as {}", target_n, i);
if let Some(next_inst) = ASMCode::find_next_inst(i, asm) {
// cur succ is next inst
......@@ -357,9 +347,7 @@ impl ASMCode {
// next inst's pred is cur
asm[next_inst].preds.push(i);
if TRACE_CFA {
trace!("inst {}: SET SUCCS as c-branch fallthrough target {}", i, next_inst);
}
trace_if!(TRACE_CFA, "inst {}: SET SUCCS as c-branch fallthrough target {}", i, next_inst);
} else {
panic!("conditional branch does not have a fallthrough target");
}
......@@ -371,11 +359,9 @@ impl ASMCode {
// cur inst's succ is target
asm[i].succs.push(target_n);
if TRACE_CFA {
trace!("inst {}: is potentially excepting to {}", i, target);
trace!("inst {}: excepting target index is {}", i, target_n);
trace!("inst {}: set SUCCS as excepting target {}", i, target_n);
}
trace_if!(TRACE_CFA, "inst {}: is potentially excepting to {}", i, target);
trace_if!(TRACE_CFA, "inst {}: excepting target index is {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set SUCCS as excepting target {}", i, target_n);
asm[target_n].preds.push(i);
......@@ -386,36 +372,26 @@ impl ASMCode {
// next inst's pred is cur
asm[next_inst].preds.push(i);
if TRACE_CFA {
trace!("inst {}: SET SUCCS as PEI fallthrough target {}", i, next_inst);
}
trace_if!(TRACE_CFA, "inst {}: SET SUCCS as PEI fallthrough target {}", i, next_inst);
} else {
panic!("PEI does not have a fallthrough target");
}
},
ASMBranchTarget::Return => {
if TRACE_CFA {
trace!("inst {}: is a return", i);
trace!("inst {}: has no successor", i);
}
trace_if!(TRACE_CFA, "inst {}: is a return", i);
trace_if!(TRACE_CFA, "inst {}: has no successor", i);
}
ASMBranchTarget::None => {
// not branch nor cond branch, succ is next inst
if TRACE_CFA {
trace!("inst {}: not a branch inst", i);
}
trace_if!(TRACE_CFA, "inst {}: not a branch inst", i);
if let Some(next_inst) = ASMCode::find_next_inst(i, asm) {
if TRACE_CFA {
trace!("inst {}: set SUCCS as next inst {}", i, next_inst);
}
trace_if!(TRACE_CFA, "inst {}: set SUCCS as next inst {}", i, next_inst);
asm[i].succs.push(next_inst);
}
}
ASMBranchTarget::UnconditionalReg(id) => {
if TRACE_CFA {
trace!("inst {}: is an unconditional branch to reg {}", i, id);
trace!("inst {}: has no successor", i);
}
trace_if!(TRACE_CFA, "inst {}: is an unconditional branch to reg {}", i, id);
trace_if!(TRACE_CFA, "inst {}: has no successor", i);
}
}
}
......
......@@ -394,9 +394,7 @@ impl ASMCode {
let ref mut asm = self.code;
for i in 0..n_insts {
if TRACE_CFA {
trace!("---inst {}---", i);
}
trace_if!(TRACE_CFA, "---inst {}---", i);
// skip symbol
if asm[i].is_symbol {
......@@ -418,9 +416,7 @@ impl ASMCode {
ASMBranchTarget::None => {
if !asm[i].preds.contains(&last_inst) {
asm[i].preds.push(last_inst);
if TRACE_CFA {
trace!("inst {}: set PREDS as previous inst - fallthrough {}", i, last_inst);
}
trace_if!(TRACE_CFA, "inst {}: set PREDS as previous inst - fallthrough {}", i, last_inst);
}
}
// otherwise do nothing
......@@ -444,12 +440,10 @@ impl ASMCode {
// target's pred is cur
asm[target_n].preds.push(i);
if TRACE_CFA {
trace!("inst {}: is a branch to {}", i, target);
trace!("inst {}: branch target index is {}", i, target_n);
trace!("inst {}: set SUCCS as branch target {}", i, target_n);
trace!("inst {}: set PREDS as branch source {}", target_n, i);
}
trace_if!(TRACE_CFA, "inst {}: is a branch to {}", i, target);
trace_if!(TRACE_CFA, "inst {}: branch target index is {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set SUCCS as branch target {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set PREDS as branch source {}", target_n, i);
},
ASMBranchTarget::Conditional(ref target) => {
// branch-to target
......@@ -458,17 +452,13 @@ impl ASMCode {
// cur insts' succ is target
asm[i].succs.push(target_n);
if TRACE_CFA {
trace!("inst {}: is a cond branch to {}", i, target);
trace!("inst {}: branch target index is {}", i, target_n);
trace!("inst {}: set SUCCS as branch target {}", i, target_n);
}
trace_if!(TRACE_CFA, "inst {}: is a cond branch to {}", i, target);
trace_if!(TRACE_CFA, "inst {}: branch target index is {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set SUCCS as branch target {}", i, target_n);
// target's pred is cur
asm[target_n].preds.push(i);
if TRACE_CFA {
trace!("inst {}: set PREDS as {}", target_n, i);
}
trace_if!(TRACE_CFA, "inst {}: set PREDS as {}", target_n, i);
if let Some(next_inst) = ASMCode::find_next_inst(i, asm) {
// cur succ is next inst
......@@ -477,9 +467,7 @@ impl ASMCode {
// next inst's pred is cur
asm[next_inst].preds.push(i);
if TRACE_CFA {
trace!("inst {}: SET SUCCS as c-branch fallthrough target {}", i, next_inst);
}
trace_if!(TRACE_CFA, "inst {}: SET SUCCS as c-branch fallthrough target {}", i, next_inst);
} else {
panic!("conditional branch does not have a fallthrough target");
}
......@@ -491,11 +479,9 @@ impl ASMCode {
// cur inst's succ is target
asm[i].succs.push(target_n);
if TRACE_CFA {
trace!("inst {}: is potentially excepting to {}", i, target);
trace!("inst {}: excepting target index is {}", i, target_n);
trace!("inst {}: set SUCCS as excepting target {}", i, target_n);
}
trace_if!(TRACE_CFA, "inst {}: is potentially excepting to {}", i, target);
trace_if!(TRACE_CFA, "inst {}: excepting target index is {}", i, target_n);
trace_if!(TRACE_CFA, "inst {}: set SUCCS as excepting target {}", i, target_n);
asm[target_n].preds.push(i);
......@@ -506,28 +492,20 @@ impl ASMCode {
// next inst's pred is cur
asm[next_inst].preds.push(i);
if TRACE_CFA {
trace!("inst {}: SET SUCCS as PEI fallthrough target {}", i, next_inst);
}
trace_if!(TRACE_CFA, "inst {}: SET SUCCS as PEI fallthrough target {}", i, next_inst);
} else {
panic!("PEI does not have a fallthrough target");
}
},
ASMBranchTarget::Return => {
if TRACE_CFA {
trace!("inst {}: is a return", i);
trace!("inst {}: has no successor", i);
}
trace_if!(TRACE_CFA, "inst {}: is a return", i);
trace_if!(TRACE_CFA, "inst {}: has no successor", i);
}
ASMBranchTarget::None => {
// not branch nor cond branch, succ is next inst
if TRACE_CFA {
trace!("inst {}: not a branch inst", i);
}
trace_if!(TRACE_CFA, "inst {}: not a branch inst", i);
if let Some(next_inst) = ASMCode::find_next_inst(i, asm) {
if TRACE_CFA {
trace!("inst {}: set SUCCS as next inst {}", i, next_inst);
}
trace_if!(TRACE_CFA, "inst {}: set SUCCS as next inst {}", i, next_inst);
asm[i].succs.push(next_inst);
}
}
......
......@@ -319,13 +319,11 @@ pub fn build_interference_graph_chaitin_briggs(cf: &mut CompiledFunction, func:
warn!("Block{}: has no range (no instructions?)", block);
continue;
}
if TRACE_LIVENESS {
trace!("Block{}: range = {:?}", block, range.as_ref().unwrap());
}
trace_if!(TRACE_LIVENESS, "Block{}: range = {:?}", block, range.as_ref().unwrap());
// for every inst I in reverse order
for i in range.unwrap().rev() {
if cfg!(debug_assertions) {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}", block, i);
cf.mc().trace_inst(i);
trace!("current live: ");
......@@ -348,7 +346,7 @@ pub fn build_interference_graph_chaitin_briggs(cf: &mut CompiledFunction, func:
if src.len() == 1 {
let node1 = ig.get_node(src[0]);
let node2 = ig.get_node(dst[0]);
trace!("add move between {} and {}",
trace_if!(TRACE_LIVENESS, "add move between {} and {}",
func.context.get_temp_display(src[0]),
func.context.get_temp_display(dst[0]));
ig.add_move(node1, node2);
......@@ -362,42 +360,34 @@ pub fn build_interference_graph_chaitin_briggs(cf: &mut CompiledFunction, func:
None
}
};
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: src={:?}", block, i, src);
}
trace_if!(TRACE_LIVENESS, "Block{}: Inst{}: src={:?}", block, i, src);
// for every definition D in I
for d in cf.mc().get_inst_reg_defines(i) {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: for definition {}", block, i, func.context.get_temp_display(d));
}
trace_if!(TRACE_LIVENESS, "Block{}: Inst{}: for definition {}",
block, i, func.context.get_temp_display(d));
// add an interference from D to every element E in Current_Live - {D}
// creating nodes if necessary
for e in current_live.iter() {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: for each live {}", block, i, func.context.get_temp_display(*e));
}
trace_if!(TRACE_LIVENESS, "Block{}: Inst{}: for each live {}",
block, i, func.context.get_temp_display(*e));
if src.is_none() || (src.is_some() && *e != src.unwrap()) {
let from = ig.get_node(d);
let to = ig.get_node(*e);
if !ig.is_same_node(from, to) &&ig.is_same_group(from, to) && !ig.is_adj(from, to) {
if !ig.is_colored(from) {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: add interference between {} and {}",
block, i,
func.context.get_temp_display(d),
func.context.get_temp_display(*e));
}
trace_if!(TRACE_LIVENESS, "Block{}: Inst{}: add interference between {} and {}",
block, i,
func.context.get_temp_display(d),
func.context.get_temp_display(*e));
ig.add_interference_edge(from, to);
}
if !ig.is_colored(to) {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: add interference between {} and {}",
block, i,
func.context.get_temp_display(*e),
func.context.get_temp_display(d));
}
trace_if!(TRACE_LIVENESS, "Block{}: Inst{}: add interference between {} and {}",
block, i,
func.context.get_temp_display(*e),
func.context.get_temp_display(d));
ig.add_interference_edge(to, from);
}
}
......@@ -407,30 +397,24 @@ pub fn build_interference_graph_chaitin_briggs(cf: &mut CompiledFunction, func:
// for every definition D in I
for d in cf.mc().get_inst_reg_defines(i) {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: remove define {} from current_live",
block, i,
func.context.get_temp_display(d));
}
trace_if!(TRACE_LIVENESS, "Block{}: Inst{}: remove define {} from current_live",
block, i,
func.context.get_temp_display(d));
// remove D from Current_Live
current_live.remove(&d);
}
// for every use U in I
for u in cf.mc().get_inst_reg_uses(i) {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: add use {} to current_live",
block, i,
func.context.get_temp_display(u));
}
trace_if!(TRACE_LIVENESS, "Block{}: Inst{}: add use {} to current_live",
block, i,
func.context.get_temp_display(u));
// add U to Current_live
current_live.insert(u);
}
if cfg!(debug_assertions) {
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: done. current_live:", block, i);
}
if TRACE_LIVENESS {
trace!("Block{}: Inst{}: done. current_live:", block, i);
for ele in current_live.iter() {
trace!("{}", func.context.get_temp_display(*ele));
}
......@@ -495,7 +479,7 @@ fn build_cfg_nodes(cf: &mut CompiledFunction) -> LinkedHashMap<String, CFGBlockN
Some(last) => last,
None => panic!("cannot find last instruction in block {}, this block contains no instruction?", block)
};
trace!("Block {}: start_inst={}, end_inst(inclusive)={}", block, first_inst, last_inst);
trace_if!(TRACE_LIVENESS, "Block {}: start_inst={}, end_inst(inclusive)={}", block, first_inst, last_inst);
start_inst_map.insert(first_inst, block);
end_inst_map.insert(last_inst, block);
......@@ -506,7 +490,7 @@ fn build_cfg_nodes(cf: &mut CompiledFunction) -> LinkedHashMap<String, CFGBlockN
// collect info for each basic block
for block in mc.get_all_blocks().iter() {
trace!("---block {}---", block);
trace_if!(TRACE_LIVENESS, "---block {}---", block);
let range = mc.get_block_range(block).unwrap();
let start_inst = range.start;
let end = range.end;
......@@ -577,7 +561,7 @@ fn build_cfg_nodes(cf: &mut CompiledFunction) -> LinkedHashMap<String, CFGBlockN
defs: defs
};
trace!("as CFGNode {:?}", node);
trace_if!(TRACE_LIVENESS, "as CFGNode {:?}", node);
ret.insert(block.clone(), node);
}
......@@ -611,7 +595,7 @@ fn global_liveness_analysis(blocks: LinkedHashMap<String, CFGBlockNode>, cf: &mu
let mut i = 0;
while is_changed {
trace!("---iteration {}---", i);
trace_if!(TRACE_LIVENESS, "---iteration {}---", i);
i += 1;
// reset
......@@ -672,14 +656,14 @@ fn global_liveness_analysis(blocks: LinkedHashMap<String, CFGBlockNode>, cf: &mu
// set live in and live out
for block in blocks.keys() {
let livein : Vec<MuID> = livein.get(block).unwrap().clone().iter().map(|x| *x).collect();
{
if TRACE_LIVENESS {
let display_array : Vec<String> = livein.iter().map(|x| func.context.get_temp_display(*x)).collect();
trace!("livein for block {}: {:?}", block, display_array);
}
cf.mc_mut().set_ir_block_livein(block, livein);
let liveout : Vec<MuID> = liveout.get(block).unwrap().clone().iter().map(|x| *x).collect();
{
if TRACE_LIVENESS {
let display_array : Vec<String> = liveout.iter().map(|x| func.context.get_temp_display(*x)).collect();
trace!("liveout for block {}: {:?}", block, display_array);
}
......
......@@ -72,6 +72,51 @@ macro_rules! linked_hashset {
};
}
#[macro_export]
macro_rules! trace_if {
($cond: expr, $($arg:tt)*) => {
if $cond {
trace!($($arg)*)
}
}
}
#[macro_export]
macro_rules! info_if {
($cond: expr, $($arg:tt)*) => {
if $cond {
info!($($arg)*)
}
}
}
#[macro_export]
macro_rules! debug_if {
($cond: expr, $($arg:tt)*) => {
if $cond {
debug!($($arg)*)
}
}
}
#[macro_export]
macro_rules! warn_if {
($cond: expr, $($arg:tt)*) => {
if $cond {
warn!($($arg)*)
}
}
}
#[macro_export]
macro_rules! error_if {
($cond: expr, $($arg:tt)*) => {
if $cond {
error!($($arg)*)
}
}
}
pub mod math;
mod address;
......
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