lib.rs 3.19 KB
Newer Older
1 2 3 4
extern crate mu;
extern crate log;
extern crate simple_logger;

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

qinsoon's avatar
qinsoon committed
10 11 12 13
#[macro_use]
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

mod aot {
    use mu::ast::ir::MuName;
    use mu::runtime;
    use mu::compiler::backend;
    use std::path::PathBuf;
    use std::process::Command;        
    
    fn link (files: Vec<PathBuf>, out: PathBuf) -> PathBuf {
        let mut gcc = Command::new("gcc");
        
        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());
        
        println!("executing: {:?}", gcc);
        
        let status = gcc.status().expect("failed to link generated code");
        assert!(status.success());
        
        out
    }
    
    pub fn link_primordial (funcs: Vec<MuName>, out: &str) -> PathBuf {
        let emit_dir = PathBuf::from(backend::AOT_EMIT_DIR);        
        
        let files : Vec<PathBuf> = {
            use std::fs;
            
            let mut ret = vec![];
            
            // all interested mu funcs
            for func in funcs {
                let mut p = emit_dir.clone();
                p.push(func);
                p.set_extension("s");
                
                ret.push(p);
            }
            
            // mu context
            let mut p = emit_dir.clone();
            p.push(backend::AOT_EMIT_CONTEXT_FILE);
            ret.push(p);
            
            // 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);
        
        link(files, out_path)
    }
    
    pub fn execute(exec: PathBuf) {
        let mut run = Command::new(exec.as_os_str());
        
        let output = run.output().expect("failed to execute");
        
        println!("---out---");
        println!("{}", String::from_utf8_lossy(&output.stdout));
        println!("---err---");
        println!("{}", String::from_utf8_lossy(&output.stderr));
        
        assert!(output.status.success());
    }
}