From b39b829061fa504b3dcb869b37efe47650085aa5 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 23:32:32 +0300 Subject: [PATCH] Add production of .llir and .mir -files for LLIR and MIR --- .gitignore | 2 ++ reid/examples/cli.rs | 23 ++++++++++++++++------- reid/src/lib.rs | 20 +++++++++++++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index e53a635..e51f1fd 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ main *.ll *.asm *.out +*.llir +*.mir \ No newline at end of file diff --git a/reid/examples/cli.rs b/reid/examples/cli.rs index 9d7a9d8..d78b477 100644 --- a/reid/examples/cli.rs +++ b/reid/examples/cli.rs @@ -1,6 +1,6 @@ use std::{env, fs, path::PathBuf}; -use reid::compile_simple; +use reid::{compile_simple, CustomIRs}; use reid_lib::compile::CompileOutput; fn main() -> Result<(), std::io::Error> { @@ -10,18 +10,23 @@ fn main() -> Result<(), std::io::Error> { let parent = path.with_extension(""); let llvm_ir_path = parent.with_extension("ll"); 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 before = std::time::SystemTime::now(); let text = fs::read_to_string(&path)?; match compile_simple(&text, PathBuf::from(&path)) { - Ok(CompileOutput { - triple, - assembly, - obj_buffer, - llvm_ir, - }) => { + Ok(( + CompileOutput { + triple, + assembly, + obj_buffer, + llvm_ir, + }, + CustomIRs { llir, mir }, + )) => { println!("{}", llvm_ir); let after = std::time::SystemTime::now(); @@ -32,6 +37,10 @@ fn main() -> Result<(), std::io::Error> { println!("Output Assembly to {:?}", asm_path); fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!"); 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!( "Compilation took: {:.2}ms\n", (after.duration_since(before).unwrap().as_micros() as f32) / 1000. diff --git a/reid/src/lib.rs b/reid/src/lib.rs index 61903ad..c1de2cb 100644 --- a/reid/src/lib.rs +++ b/reid/src/lib.rs @@ -197,7 +197,7 @@ pub fn compile_and_pass<'map>( source: &str, path: PathBuf, module_map: &'map mut ErrorModules, -) -> Result { +) -> Result<(CompileOutput, CustomIRs), ReidError> { let path = path.canonicalize().unwrap(); 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); 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 { +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(); compile_and_pass(source, path, &mut map) }