diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index e393025..521fb94 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -56,6 +56,8 @@ impl Drop for LLVMContext { pub struct CompiledModule { module_ref: LLVMModuleRef, _context: LLVMContext, + cpu: String, + features: String, } pub struct CompileOutput { @@ -84,8 +86,8 @@ impl CompiledModule { let target_machine = LLVMCreateTargetMachine( target, triple, - c"generic".as_ptr(), - c"".as_ptr(), + into_cstring(self.cpu.clone()).as_ptr(), + into_cstring(self.features.clone()).as_ptr(), llvm_sys::target_machine::LLVMCodeGenOptLevel::LLVMCodeGenLevelLess, llvm_sys::target_machine::LLVMRelocMode::LLVMRelocDefault, llvm_sys::target_machine::LLVMCodeModel::LLVMCodeModelDefault, @@ -142,7 +144,7 @@ impl CompiledModule { } impl Context { - pub fn compile(&self) -> CompiledModule { + pub fn compile(&self, cpu: Option, features: Vec) -> CompiledModule { unsafe { let context_ref = LLVMContextCreate(); @@ -173,6 +175,8 @@ impl Context { CompiledModule { module_ref: main_module_ref, _context: context, + cpu: cpu.unwrap_or("generic".to_owned()), + features: features.join(","), } } } diff --git a/reid/examples/cli.rs b/reid/examples/cli.rs index 89fad8f..a39a9c4 100644 --- a/reid/examples/cli.rs +++ b/reid/examples/cli.rs @@ -25,7 +25,10 @@ fn main() -> Result<(), std::io::Error> { let text = fs::read_to_string(&path)?; - match compile_simple(&text, PathBuf::from(&path)) { + let cpu = std::env::var("CPU").unwrap_or("generic".to_owned()); + let features = std::env::var("REIDFLAGS").unwrap_or("".to_owned()); + + match compile_simple(&text, PathBuf::from(&path), Some(cpu), vec![features]) { Ok(( CompileOutput { triple, diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index aa0bc2a..ee0f259 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -19,12 +19,9 @@ use crate::{ intrinsics::IntrinsicFunction, lexer::{FullToken, Position}, mir::{ - self, - implement::TypeCategory, - pass::{ScopeBinopDef, ScopeBinopKey}, - CustomTypeKey, FunctionDefinitionKind, Metadata, NamedVariableRef, SourceModuleId, - StructField, StructType, TypeDefinition, TypeDefinitionKind, TypeKind, VagueLiteral, - WhileStatement, + self, implement::TypeCategory, pass::ScopeBinopKey, CustomTypeKey, FunctionDefinitionKind, + Metadata, NamedVariableRef, SourceModuleId, StructField, StructType, TypeDefinition, + TypeDefinitionKind, TypeKind, VagueLiteral, WhileStatement, }, util::try_all, }; @@ -45,8 +42,8 @@ pub struct CodegenContext<'ctx> { impl<'ctx> CodegenContext<'ctx> { /// Compile contained LLIR into LLVM IR and produce `hello.o` and /// `hello.asm` - pub fn compile(&self) -> CompiledModule { - self.context.compile() + pub fn compile(&self, cpu: Option, features: Vec) -> CompiledModule { + self.context.compile(cpu, features) } } diff --git a/reid/src/lib.rs b/reid/src/lib.rs index 3fc9547..40230fd 100644 --- a/reid/src/lib.rs +++ b/reid/src/lib.rs @@ -216,6 +216,8 @@ pub fn compile_and_pass<'map>( source: &str, path: PathBuf, module_map: &'map mut ErrorModules, + cpu: Option, + features: Vec, ) -> Result<(CompileOutput, CustomIRs), ReidError> { let path = path.canonicalize().unwrap(); let name = path.file_name().unwrap().to_str().unwrap().to_owned(); @@ -241,7 +243,7 @@ pub fn compile_and_pass<'map>( #[cfg(debug_assertions)] println!("{}", &codegen_modules.context); - let compiled = codegen_modules.compile(); + let compiled = codegen_modules.compile(cpu, features); Ok(( compiled.output(), CustomIRs { @@ -259,7 +261,9 @@ pub struct CustomIRs { pub fn compile_simple( source: &str, path: PathBuf, + cpu: Option, + features: Vec, ) -> Result<(CompileOutput, CustomIRs), ReidError> { let mut map = ErrorModules::default(); - compile_and_pass(source, path, &mut map) + compile_and_pass(source, path, &mut map, cpu, features) } diff --git a/reid/tests/e2e.rs b/reid/tests/e2e.rs index d2aa144..a11940d 100644 --- a/reid/tests/e2e.rs +++ b/reid/tests/e2e.rs @@ -30,7 +30,7 @@ fn test(source: &str, name: &str, expected_exit_code: Option) { let codegen = assert_err(mir_context.codegen(&context)); - let output = codegen.compile().output(); + let output = codegen.compile(None, Vec::new()).output(); let time = SystemTime::now(); let in_path = PathBuf::from(format!( "/tmp/temp-{}.o",