test_regalloc.rs 3.09 KB
Newer Older
1 2 3 4 5 6
extern crate mu;
extern crate log;
extern crate simple_logger;

use test_ir::test_ir::factorial;
use self::mu::compiler::*;
7 8
use self::mu::utils::vec_utils;
use self::mu::ast::ir::*;
9 10 11

use std::sync::Arc;

12 13 14 15
#[test]
fn test_ir_liveness_fac() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
qinsoon's avatar
qinsoon committed
16
    let vm = Arc::new(factorial());
17 18 19 20 21 22 23
    
    let compiler = Compiler::new(CompilerPolicy::new(vec![
            Box::new(passes::DefUse::new()),
            Box::new(passes::TreeGen::new()),
            Box::new(passes::ControlFlowAnalysis::new()),
            Box::new(passes::TraceGen::new()),
            Box::new(backend::inst_sel::InstructionSelection::new()),
qinsoon's avatar
qinsoon committed
24
    ]), vm.clone());
25
    
qinsoon's avatar
qinsoon committed
26
    let funcs = vm.funcs().read().unwrap();
27
    let factorial_func = funcs.get("fac").unwrap().borrow();
qinsoon's avatar
qinsoon committed
28
    let func_vers = vm.func_vers().read().unwrap();
29
    let mut factorial_func_ver = func_vers.get(&(factorial_func.fn_name, factorial_func.cur_ver.unwrap())).unwrap().borrow_mut();
30
    
31
    compiler.compile(&mut factorial_func_ver);
32
    
qinsoon's avatar
qinsoon committed
33
    let cf_lock = vm.compiled_funcs().read().unwrap();
34 35 36 37 38
    let cf = cf_lock.get("fac").unwrap().borrow();
    
    // block 0
    
    let block_0_livein = cf.mc.get_ir_block_livein("blk_0").unwrap();
39
    let blk_0_n_3 = factorial_func_ver.context.get_value_by_tag("blk_0_n_3").unwrap().id;
40 41 42 43 44 45 46 47
    assert!(vec_utils::is_identical_to_str_ignore_order(block_0_livein, vec![blk_0_n_3]));
    
    let block_0_liveout = cf.mc.get_ir_block_liveout("blk_0").unwrap();
    assert!(vec_utils::is_identical_to_str_ignore_order(block_0_liveout, vec![blk_0_n_3]));
    
    // block 1
    
    let block_1_livein = cf.mc.get_ir_block_livein("blk_1").unwrap();
48
    let blk_1_n_3 = factorial_func_ver.context.get_value_by_tag("blk_1_n_3").unwrap().id;
49 50 51
    assert!(vec_utils::is_identical_to_str_ignore_order(block_1_livein, vec![blk_1_n_3]));
    
    let block_1_liveout = cf.mc.get_ir_block_liveout("blk_1").unwrap();
52
    let blk_1_v52 = factorial_func_ver.context.get_value_by_tag("blk_1_v52").unwrap().id;
53 54 55 56 57
    assert!(vec_utils::is_identical_to_str_ignore_order(block_1_liveout, vec![blk_1_v52]));
    
    // block 2
    
    let block_2_livein = cf.mc.get_ir_block_livein("blk_2").unwrap();
58
    let blk_2_v53 = factorial_func_ver.context.get_value_by_tag("blk_2_v53").unwrap().id;
59 60 61 62 63 64 65
    assert!(vec_utils::is_identical_to_str_ignore_order(block_2_livein, vec![blk_2_v53]));
    
    let block_2_liveout = cf.mc.get_ir_block_liveout("blk_2").unwrap();
    let expect : Vec<MuID> = vec![];
    assert!(vec_utils::is_identical_to_str_ignore_order(block_2_liveout, expect));
}

66 67 68 69
#[test]
fn test_regalloc_fac() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
qinsoon's avatar
qinsoon committed
70
    let vm = Arc::new(factorial());
71
    
qinsoon's avatar
qinsoon committed
72
    let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
73
    
qinsoon's avatar
qinsoon committed
74
    let funcs = vm.funcs().read().unwrap();
75
    let factorial_func = funcs.get("fac").unwrap().borrow();
qinsoon's avatar
qinsoon committed
76
    let func_vers = vm.func_vers().read().unwrap();
77
    let mut factorial_func_ver = func_vers.get(&(factorial_func.fn_name, factorial_func.cur_ver.unwrap())).unwrap().borrow_mut();
78
    
79
    compiler.compile(&mut factorial_func_ver); 
80
}