1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::fs::File;
use std::path::PathBuf;
use std::io::Write;
use std::env;
use cc;

use super::Asm;

impl Asm {
    #[cfg(not(windows))]
    pub fn generate(&self) {
        let out_dir = env::var("OUT_DIR").unwrap();
        let path = PathBuf::from(out_dir).join(self.name.clone() + ".S");
        let mut output = File::create(&path).unwrap();
        writeln!(
            output,
            ".intel_syntax\n.text\n.globl {}\n{}:",
            self.name, self.name
        ).unwrap();
        for line in &self.body {
            writeln!(output, "  {}", line).unwrap();
        }

        writeln!(output, "  ret").unwrap();

        cc::Build::new().file(&path).compile(&self.name.clone());
    }

    #[cfg(windows)]
    pub fn generate(&self) {
        let out_dir = env::var("OUT_DIR").unwrap();
        let path = PathBuf::from(out_dir).join(self.name.clone() + ".asm");
        let mut output = File::create(&path).unwrap();
        writeln!(output, ".code\nPUBLIC {}\n{} PROC", self.name, self.name).unwrap();
        for line in &self.body {
            writeln!(output, "{}", line).unwrap();
        }

        writeln!(output, "ret\n{} ENDP\nEND", self.name).unwrap();

        cc::Build::new().file(&path).compile(&self.name.clone());
    }
}