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

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

use std::sync::Arc;
12
use aot;
13

14
15
16
17
#[test]
fn test_ir_liveness_fac() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
qinsoon's avatar
qinsoon committed
18
    let vm = Arc::new(factorial());
19
20
21
22
23
24
25
    
    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
26
    ]), vm.clone());
27
    
qinsoon's avatar
qinsoon committed
28
    let func_id = vm.id_of("fac");
qinsoon's avatar
qinsoon committed
29
    let funcs = vm.funcs().read().unwrap();
30
    let func = funcs.get(&func_id).unwrap().read().unwrap();
qinsoon's avatar
qinsoon committed
31
    let func_vers = vm.func_vers().read().unwrap();
qinsoon's avatar
qinsoon committed
32
    let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
33
    
qinsoon's avatar
qinsoon committed
34
    compiler.compile(&mut func_ver);
35
    
qinsoon's avatar
qinsoon committed
36
    let cf_lock = vm.compiled_funcs().read().unwrap();
37
    let cf = cf_lock.get(&func_ver.id()).unwrap().read().unwrap();
38
39
40
41
    
    // block 0
    
    let block_0_livein = cf.mc.get_ir_block_livein("blk_0").unwrap();
qinsoon's avatar
qinsoon committed
42
    let blk_0_n_3 = vm.id_of("blk_0_n_3");;
43
44
45
46
47
48
49
50
    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
51
    let blk_1_n_3 = vm.id_of("blk_1_n_3");
52
53
54
    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
55
    let blk_1_v52 = vm.id_of("blk_1_v52");
56
57
58
59
60
    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
61
    let blk_2_v53 = vm.id_of("blk_2_v53");
62
63
64
65
66
67
68
    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));
}

69
70
71
72
#[test]
fn test_regalloc_fac() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
qinsoon's avatar
qinsoon committed
73
    let vm = Arc::new(factorial());
74
    
qinsoon's avatar
qinsoon committed
75
    let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
76
    
qinsoon's avatar
qinsoon committed
77
    let func_id = vm.id_of("fac");
78
    
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
        
        compiler.compile(&mut func_ver);
    }
    
    backend::emit_context(&vm);
    
    let dylib = aot::link_dylib(vec!["fac".to_string()], "libfac.dylib");
    
    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
        let fac : libloading::Symbol<unsafe extern fn(u64) -> u64> = lib.get(b"fac").unwrap();
        
        let fac5 = fac(5);
        println!("fac(5) = {}", fac5);
        assert!(fac5 == 120);
    }
100
}