Add production of .llir and .mir -files for LLIR and MIR

This commit is contained in:
Sofia 2025-07-22 23:32:32 +03:00
parent 97f5eebf22
commit b39b829061
3 changed files with 35 additions and 10 deletions

2
.gitignore vendored
View File

@ -9,3 +9,5 @@ main
*.ll *.ll
*.asm *.asm
*.out *.out
*.llir
*.mir

View File

@ -1,6 +1,6 @@
use std::{env, fs, path::PathBuf}; use std::{env, fs, path::PathBuf};
use reid::compile_simple; use reid::{compile_simple, CustomIRs};
use reid_lib::compile::CompileOutput; use reid_lib::compile::CompileOutput;
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
@ -10,18 +10,23 @@ fn main() -> Result<(), std::io::Error> {
let parent = path.with_extension(""); let parent = path.with_extension("");
let llvm_ir_path = parent.with_extension("ll"); let llvm_ir_path = parent.with_extension("ll");
let object_path = parent.with_extension("o"); let object_path = parent.with_extension("o");
let llir_path = parent.with_extension("llir");
let mir_path = parent.with_extension("mir");
let asm_path = parent.with_extension("asm"); let asm_path = parent.with_extension("asm");
let before = std::time::SystemTime::now(); let before = std::time::SystemTime::now();
let text = fs::read_to_string(&path)?; let text = fs::read_to_string(&path)?;
match compile_simple(&text, PathBuf::from(&path)) { match compile_simple(&text, PathBuf::from(&path)) {
Ok(CompileOutput { Ok((
triple, CompileOutput {
assembly, triple,
obj_buffer, assembly,
llvm_ir, obj_buffer,
}) => { llvm_ir,
},
CustomIRs { llir, mir },
)) => {
println!("{}", llvm_ir); println!("{}", llvm_ir);
let after = std::time::SystemTime::now(); let after = std::time::SystemTime::now();
@ -32,6 +37,10 @@ fn main() -> Result<(), std::io::Error> {
println!("Output Assembly to {:?}", asm_path); println!("Output Assembly to {:?}", asm_path);
fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!"); fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!");
println!("Output Object-file to {:?}\n", object_path); println!("Output Object-file to {:?}\n", object_path);
fs::write(&llir_path, &llir).expect("Could not write LLIR-file!");
println!("Output LLIR-file to {:?}\n", llir_path);
fs::write(&mir_path, &mir).expect("Could not write MIR-file!");
println!("Output MIR-file to {:?}\n", llir_path);
println!( println!(
"Compilation took: {:.2}ms\n", "Compilation took: {:.2}ms\n",
(after.duration_since(before).unwrap().as_micros() as f32) / 1000. (after.duration_since(before).unwrap().as_micros() as f32) / 1000.

View File

@ -197,7 +197,7 @@ pub fn compile_and_pass<'map>(
source: &str, source: &str,
path: PathBuf, path: PathBuf,
module_map: &'map mut ErrorModules, module_map: &'map mut ErrorModules,
) -> Result<CompileOutput, 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();
@ -220,10 +220,24 @@ pub fn compile_and_pass<'map>(
println!("{}", &codegen_modules.context); println!("{}", &codegen_modules.context);
let compiled = codegen_modules.compile(); let compiled = codegen_modules.compile();
Ok(compiled.output()) Ok((
compiled.output(),
CustomIRs {
llir: format!("{}", codegen_modules.context),
mir: format!("{}", mir_context),
},
))
} }
pub fn compile_simple(source: &str, path: PathBuf) -> Result<CompileOutput, ReidError> { pub struct CustomIRs {
pub llir: String,
pub mir: String,
}
pub fn compile_simple(
source: &str,
path: PathBuf,
) -> 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)
} }