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 44 45 46 47 48 49 50 51 52 53 54 55
use std::fs::File; use std::path::PathBuf; use std::io::Write; use std::env; use cc; use super::Asm; impl Asm { #[cfg(unix)] pub fn generate(&self) { if let Some(ref ret) = self.ret { if ret != "%rax" { panic!("for now, the return argument for an assembly function must be %rax."); } } 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(not(unix))] pub fn generate(&self) { if let Some(ref ret) = self.ret { if ret != "rax" { panic!("for now, the return argument for an assembly function must be %rax."); } } 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()); } }