test_regalloc.rs 3.63 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 16 17 18 19 20 21 22 23 24 25 26
#[test]
fn test_ir_liveness_fac() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
    let vm_context = Arc::new(factorial());
    
    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()),
    ]), vm_context.clone());
    
    let funcs = vm_context.funcs().read().unwrap();
27 28 29
    let factorial_func = funcs.get("fac").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    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 33 34 35 36 37 38
    
    let cf_lock = vm_context.compiled_funcs().read().unwrap();
    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 70 71 72 73 74 75 76 77
#[test]
fn test_regalloc_fac() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
    let vm_context = Arc::new(factorial());
    
    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()),
78
            Box::new(backend::reg_alloc::RegisterAllocation::new()),
79
            Box::new(backend::peephole_opt::PeepholeOptimization::new()),
80
            Box::new(backend::code_emission::CodeEmission::new())
81 82 83
    ]), vm_context.clone());
    
    let funcs = vm_context.funcs().read().unwrap();
84 85 86
    let factorial_func = funcs.get("fac").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    let mut factorial_func_ver = func_vers.get(&(factorial_func.fn_name, factorial_func.cur_ver.unwrap())).unwrap().borrow_mut();
87
    
88
    compiler.compile(&mut factorial_func_ver); 
89
}