Use native rust to write source file

This commit is contained in:
greg 2017-01-18 01:56:42 -08:00
parent e103ba221c
commit 3e231b4715
2 changed files with 11 additions and 12 deletions

View File

@ -1,6 +1,8 @@
extern crate llvm_sys; extern crate llvm_sys;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use self::llvm_sys::prelude::*; use self::llvm_sys::prelude::*;
use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate}; use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate};
@ -20,7 +22,12 @@ pub fn compilation_sequence(ast: AST, sourcefile: &str) {
_ => panic!("Bad filename {}", sourcefile), _ => panic!("Bad filename {}", sourcefile),
}; };
compile_ast(ast, ll_filename, false); let llvm_code = compile_ast(ast);
println!("Compilation process finished for {}", ll_filename);
File::create(ll_filename)
.and_then(|mut f| f.write_all(llvm_code.as_bytes()))
.expect("Error writing file");
let llc_output = Command::new("llc") let llc_output = Command::new("llc")
.arg("-filetype=obj") .arg("-filetype=obj")
.arg(ll_filename) .arg(ll_filename)
@ -58,7 +65,7 @@ struct CompilationData {
current_function: Option<LLVMValueRef>, current_function: Option<LLVMValueRef>,
} }
pub fn compile_ast(ast: AST, filename: &str, return_string: bool) -> Option<String> { pub fn compile_ast(ast: AST) -> String {
println!("Compiling!"); println!("Compiling!");
let names: VariableMap = HashMap::new(); let names: VariableMap = HashMap::new();
@ -86,20 +93,12 @@ pub fn compile_ast(ast: AST, filename: &str, return_string: bool) -> Option<Stri
LLVMWrap::BuildRet(builder, value); LLVMWrap::BuildRet(builder, value);
println!("Compilation process finished for {}", filename); let ret = LLVMWrap::PrintModuleToString(module);
let ret = if return_string {
let s = LLVMWrap::PrintModuleToString(module);
Some(s)
} else {
LLVMWrap::PrintModuleToFile(module, filename);
None
};
// Clean up. Values created in the context mostly get cleaned up there. // Clean up. Values created in the context mostly get cleaned up there.
LLVMWrap::DisposeBuilder(builder); LLVMWrap::DisposeBuilder(builder);
LLVMWrap::DisposeModule(module); LLVMWrap::DisposeModule(module);
LLVMWrap::ContextDispose(context); LLVMWrap::ContextDispose(context);
ret ret
} }

View File

@ -165,7 +165,7 @@ impl<'a> Repl<'a> {
} }
if self.show_llvm_ir { if self.show_llvm_ir {
let s = compile_ast(ast, "«repl llvm»", true).unwrap(); let s = compile_ast(ast);
output.push_str(&s); output.push_str(&s);
} else { } else {
// for now only handle last output // for now only handle last output