lib.rs 6.07 KB
Newer Older
1
extern crate mu;
qinsoon's avatar
qinsoon committed
2
#[macro_use]
3 4 5
extern crate log;
extern crate simple_logger;

6
mod test_ir;
qinsoon's avatar
qinsoon committed
7
mod test_compiler;
8
mod test_runtime;
9
mod test_api;
qinsoon's avatar
qinsoon committed
10

qinsoon's avatar
qinsoon committed
11 12 13
mod common {
    use std::fmt;
    
14
    pub fn assert_vector_ordered <T: fmt::Debug> (left: &Vec<T>, right: &Vec<T>) {
qinsoon's avatar
qinsoon committed
15 16 17
        assert_debug_str(left, right);
    }
    
18 19 20 21 22 23 24 25 26
    pub fn assert_vector_no_order <T: Ord + fmt::Debug + Clone> (left: &Vec<T>, right: &Vec<T>) {
        let mut left_clone = left.clone();
        left_clone.sort();
        let mut right_clone = right.clone();
        right_clone.sort();
        
        assert_debug_str(left_clone, right_clone);
    }
    
qinsoon's avatar
qinsoon committed
27 28
    pub fn assert_debug_str<T: fmt::Debug, U: fmt::Debug> (left: T, right: U) {
        assert_eq!(format!("{:?}", left), format!("{:?}", right))
qinsoon's avatar
qinsoon committed
29
    }
30
}
31 32 33 34 35 36

mod aot {
    use mu::ast::ir::MuName;
    use mu::runtime;
    use mu::compiler::backend;
    use std::path::PathBuf;
37 38
    use std::process::Command;
    use std::process::Output;
39
    
40 41
    const CC : &'static str = "clang";
    
42 43 44 45 46 47 48 49 50 51 52 53 54
    fn exec (mut cmd: Command) -> Output {
        println!("executing: {:?}", cmd); 
        let output = cmd.output().expect("failed to execute");
        
        println!("---out---");
        println!("{}", String::from_utf8_lossy(&output.stdout));
        println!("---err---");
        println!("{}", String::from_utf8_lossy(&output.stderr));
        
        output
    } 
    
    fn link_executable_internal (files: Vec<PathBuf>, out: PathBuf) -> PathBuf {
55
        let mut gcc = Command::new(CC);
56 57 58 59 60 61 62 63 64 65
        
        for file in files {
            println!("link with {:?}", file.as_path());
            gcc.arg(file.as_path());
        }
        
        println!("output as {:?}", out.as_path());
        gcc.arg("-o");
        gcc.arg(out.as_os_str());
        
66 67 68 69 70 71 72 73 74
        assert!(exec(gcc).status.success());
        
        out
    }
    
    fn link_dylib_internal (files: Vec<PathBuf>, out: PathBuf) -> PathBuf {
        let mut object_files : Vec<PathBuf> = vec![];
        
        for file in files {
75
            let mut gcc = Command::new(CC);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            
            gcc.arg("-c");
            gcc.arg("-fpic");
            
            let mut out = file.clone();
            out.set_extension("o");
            
            gcc.arg(file.as_os_str());
            gcc.arg("-o");
            gcc.arg(out.as_os_str());
            
            object_files.push(out);
            exec(gcc);
        }
        
91
        let mut gcc = Command::new(CC);
92
        gcc.arg("-shared");
93 94 95
        gcc.arg("-Wl");
        gcc.arg("-undefined");
        gcc.arg("dynamic_lookup");
96 97 98 99 100
        for obj in object_files {
            gcc.arg(obj.as_os_str());
        }
        gcc.arg("-o");
        gcc.arg(out.as_os_str());
101
        
102
        exec(gcc);
103 104 105 106
        
        out
    }
    
107 108 109 110 111 112 113 114 115 116 117 118 119 120
    fn get_path_for_mu_func (f: MuName) -> PathBuf {
        let mut ret = PathBuf::from(backend::AOT_EMIT_DIR);
        ret.push(f);
        ret.set_extension("s");
        
        ret
    }
    
    fn get_path_for_mu_context () -> PathBuf {
        let mut ret = PathBuf::from(backend::AOT_EMIT_DIR);
        ret.push(backend::AOT_EMIT_CONTEXT_FILE);
        ret
    }
    
121
    pub fn link_primordial (funcs: Vec<MuName>, out: &str) -> PathBuf {
122
        let emit_dir = PathBuf::from(backend::AOT_EMIT_DIR);
123 124 125 126 127 128 129 130
        
        let files : Vec<PathBuf> = {
            use std::fs;
            
            let mut ret = vec![];
            
            // all interested mu funcs
            for func in funcs {
131
                ret.push(get_path_for_mu_func(func));
132 133 134
            }
            
            // mu context
135
            ret.push(get_path_for_mu_context());
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            
            // copy primoridal entry
            let source   = PathBuf::from(runtime::PRIMORDIAL_ENTRY);
            let mut dest = PathBuf::from(backend::AOT_EMIT_DIR);
            dest.push("main.c");
            fs::copy(source.as_path(), dest.as_path()).unwrap();
            // include the primordial C main
            ret.push(dest);
            
            // include mu static lib
            let libmu = PathBuf::from("target/debug/libmu.a");
            ret.push(libmu);
            
            ret
        };
        
        let mut out_path = emit_dir.clone();
        out_path.push(out);
        
155
        link_executable_internal(files, out_path)
156 157
    }
    
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    pub fn execute(executable: PathBuf) {
        let run = Command::new(executable.as_os_str());
        assert!(exec(run).status.success());
    }
    
    pub fn link_dylib (funcs: Vec<MuName>, out: &str) -> PathBuf {
        let files = {
            let mut ret = vec![];
            
            for func in funcs {
                ret.push(get_path_for_mu_func(func));
            }
            
            ret.push(get_path_for_mu_context());
            
            ret
        };
175
        
176 177
        let mut out_path = PathBuf::from(backend::AOT_EMIT_DIR);
        out_path.push(out);
178
        
179
        link_dylib_internal(files, out_path)
180
    }
John Zhang's avatar
John Zhang committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}

mod testutil {
    extern crate mu;
    extern crate log;
    extern crate simple_logger;
    extern crate libloading as ll;

    use test_ir::test_ir::sum;
    use test_ir::test_ir::factorial;
    use self::mu::compiler::*;
    use self::mu::ast::ir::*;
    use self::mu::vm::*;

    use std::sync::Arc;
    use aot;

    pub fn compile_fnc<'a>(fnc_name: &'static str, build_fnc: &'a Fn() -> VM) -> ll::Library {
        simple_logger::init_with_level(log::LogLevel::Trace).ok();

        let vm = Arc::new(build_fnc());

        let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());

        let func_id = vm.id_of(fnc_name);

        {
            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 libname = &format!("lib{}.dylib", fnc_name);
        let dylib = aot::link_dylib(vec![Mu(fnc_name)], libname);

        ll::Library::new(dylib.as_os_str()).unwrap()
    }
222
}