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 {
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<String>, features: Vec<String>) -> 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(","),
}
}
}

View File

@ -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,

View File

@ -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<String>, features: Vec<String>) -> CompiledModule {
self.context.compile(cpu, features)
}
}

View File

@ -216,6 +216,8 @@ pub fn compile_and_pass<'map>(
source: &str,
path: PathBuf,
module_map: &'map mut ErrorModules,
cpu: Option<String>,
features: Vec<String>,
) -> 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<String>,
features: Vec<String>,
) -> 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)
}

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 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",