test_regalloc.rs 3.06 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 func_id = vm.id_of("fac");
qinsoon's avatar
qinsoon committed
27
    let funcs = vm.funcs().read().unwrap();
28
    let func = funcs.get(&func_id).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
29
    let func_vers = vm.func_vers().read().unwrap();
30
    let mut func_ver = func_vers.get(&(func.id(), func.cur_ver.unwrap())).unwrap().write().unwrap();
31
    
qinsoon's avatar
qinsoon committed
32
    compiler.compile(&mut func_ver);
33
    
qinsoon's avatar
qinsoon committed
34
    let cf_lock = vm.compiled_funcs().read().unwrap();
35
    let cf = cf_lock.get(&func_ver.id()).unwrap().read().unwrap();
36 37 38 39
    
    // block 0
    
    let block_0_livein = cf.mc.get_ir_block_livein("blk_0").unwrap();
qinsoon's avatar
qinsoon committed
40
    let blk_0_n_3 = func_ver.context.get_value_by_tag("blk_0_n_3").unwrap().id;
41 42 43 44 45 46 47 48
    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();
qinsoon's avatar
qinsoon committed
49
    let blk_1_n_3 = func_ver.context.get_value_by_tag("blk_1_n_3").unwrap().id;
50 51 52
    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();
qinsoon's avatar
qinsoon committed
53
    let blk_1_v52 = func_ver.context.get_value_by_tag("blk_1_v52").unwrap().id;
54 55 56 57 58
    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();
qinsoon's avatar
qinsoon committed
59
    let blk_2_v53 = func_ver.context.get_value_by_tag("blk_2_v53").unwrap().id;
60 61 62 63 64 65 66
    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));
}

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