Add flags and cpu to options that are configurable

This commit is contained in:
Sofia 2025-07-25 00:28:58 +03:00
parent dbd539fb76
commit 023d3b75b6
5 changed files with 23 additions and 15 deletions

View File

@ -56,6 +56,8 @@ impl Drop for LLVMContext {
pub struct CompiledModule { pub struct CompiledModule {
module_ref: LLVMModuleRef, module_ref: LLVMModuleRef,
_context: LLVMContext, _context: LLVMContext,
cpu: String,
features: String,
} }
pub struct CompileOutput { pub struct CompileOutput {
@ -84,8 +86,8 @@ impl CompiledModule {
let target_machine = LLVMCreateTargetMachine( let target_machine = LLVMCreateTargetMachine(
target, target,
triple, triple,
c"generic".as_ptr(), into_cstring(self.cpu.clone()).as_ptr(),
c"".as_ptr(), into_cstring(self.features.clone()).as_ptr(),
llvm_sys::target_machine::LLVMCodeGenOptLevel::LLVMCodeGenLevelLess, llvm_sys::target_machine::LLVMCodeGenOptLevel::LLVMCodeGenLevelLess,
llvm_sys::target_machine::LLVMRelocMode::LLVMRelocDefault, llvm_sys::target_machine::LLVMRelocMode::LLVMRelocDefault,
llvm_sys::target_machine::LLVMCodeModel::LLVMCodeModelDefault, llvm_sys::target_machine::LLVMCodeModel::LLVMCodeModelDefault,
@ -142,7 +144,7 @@ impl CompiledModule {
} }
impl Context { impl Context {
pub fn compile(&self) -> CompiledModule { pub fn compile(&self, cpu: Option<String>, features: Vec<String>) -> CompiledModule {
unsafe { unsafe {
let context_ref = LLVMContextCreate(); let context_ref = LLVMContextCreate();
@ -173,6 +175,8 @@ impl Context {
CompiledModule { CompiledModule {
module_ref: main_module_ref, module_ref: main_module_ref,
_context: context, _context: context,
cpu: cpu.unwrap_or("generic".to_owned()),
features: features.join(","),
} }
} }
} }

View File

@ -25,7 +25,10 @@ fn main() -> Result<(), std::io::Error> {
let text = fs::read_to_string(&path)?; 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(( Ok((
CompileOutput { CompileOutput {
triple, triple,

View File

@ -19,12 +19,9 @@ use crate::{
intrinsics::IntrinsicFunction, intrinsics::IntrinsicFunction,
lexer::{FullToken, Position}, lexer::{FullToken, Position},
mir::{ mir::{
self, self, implement::TypeCategory, pass::ScopeBinopKey, CustomTypeKey, FunctionDefinitionKind,
implement::TypeCategory, Metadata, NamedVariableRef, SourceModuleId, StructField, StructType, TypeDefinition,
pass::{ScopeBinopDef, ScopeBinopKey}, TypeDefinitionKind, TypeKind, VagueLiteral, WhileStatement,
CustomTypeKey, FunctionDefinitionKind, Metadata, NamedVariableRef, SourceModuleId,
StructField, StructType, TypeDefinition, TypeDefinitionKind, TypeKind, VagueLiteral,
WhileStatement,
}, },
util::try_all, util::try_all,
}; };
@ -45,8 +42,8 @@ pub struct CodegenContext<'ctx> {
impl<'ctx> CodegenContext<'ctx> { impl<'ctx> CodegenContext<'ctx> {
/// Compile contained LLIR into LLVM IR and produce `hello.o` and /// Compile contained LLIR into LLVM IR and produce `hello.o` and
/// `hello.asm` /// `hello.asm`
pub fn compile(&self) -> CompiledModule { pub fn compile(&self, cpu: Option<String>, features: Vec<String>) -> CompiledModule {
self.context.compile() self.context.compile(cpu, features)
} }
} }

View File

@ -216,6 +216,8 @@ pub fn compile_and_pass<'map>(
source: &str, source: &str,
path: PathBuf, path: PathBuf,
module_map: &'map mut ErrorModules, module_map: &'map mut ErrorModules,
cpu: Option<String>,
features: Vec<String>,
) -> Result<(CompileOutput, CustomIRs), ReidError> { ) -> Result<(CompileOutput, CustomIRs), ReidError> {
let path = path.canonicalize().unwrap(); let path = path.canonicalize().unwrap();
let name = path.file_name().unwrap().to_str().unwrap().to_owned(); let name = path.file_name().unwrap().to_str().unwrap().to_owned();
@ -241,7 +243,7 @@ pub fn compile_and_pass<'map>(
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
println!("{}", &codegen_modules.context); println!("{}", &codegen_modules.context);
let compiled = codegen_modules.compile(); let compiled = codegen_modules.compile(cpu, features);
Ok(( Ok((
compiled.output(), compiled.output(),
CustomIRs { CustomIRs {
@ -259,7 +261,9 @@ pub struct CustomIRs {
pub fn compile_simple( pub fn compile_simple(
source: &str, source: &str,
path: PathBuf, path: PathBuf,
cpu: Option<String>,
features: Vec<String>,
) -> Result<(CompileOutput, CustomIRs), ReidError> { ) -> Result<(CompileOutput, CustomIRs), ReidError> {
let mut map = ErrorModules::default(); let mut map = ErrorModules::default();
compile_and_pass(source, path, &mut map) compile_and_pass(source, path, &mut map, cpu, features)
} }

View File

@ -30,7 +30,7 @@ fn test(source: &str, name: &str, expected_exit_code: Option<i32>) {
let codegen = assert_err(mir_context.codegen(&context)); 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 time = SystemTime::now();
let in_path = PathBuf::from(format!( let in_path = PathBuf::from(format!(
"/tmp/temp-{}.o", "/tmp/temp-{}.o",