From c8001a3a584ed2207a0d802d04f3c214ce662c05 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Mon, 21 Apr 2025 15:56:10 +0100 Subject: [PATCH] Made the process actually output to the file --- Main.cpp => Frontend-Main.cpp | 3 ++- LX-LLVM.vcxproj | 4 ++-- LX-LLVM.vcxproj.filters | 2 +- Parser/src/GenIR.cpp | 13 +++++++++---- common/Parser.h | 5 ++++- 5 files changed, 18 insertions(+), 9 deletions(-) rename Main.cpp => Frontend-Main.cpp (98%) diff --git a/Main.cpp b/Frontend-Main.cpp similarity index 98% rename from Main.cpp rename to Frontend-Main.cpp index 2ba0a7c..d26b630 100644 --- a/Main.cpp +++ b/Frontend-Main.cpp @@ -46,6 +46,7 @@ int main(int argc, char** argv) // Opens / Creates the output file // std::ofstream outFile(outPath); LX::ThrowIf(outFile.is_open() == false); + outFile.close(); // Opened just to check we can // Opens the log file (if there is one specified // if (argc == 4) @@ -67,7 +68,7 @@ int main(int argc, char** argv) LX::SafeFlush(log.get()); // Turns the AST into LLVM IR // - LX::GenerateIR(AST, inpPath.filename().string()); + LX::GenerateIR(AST, inpPath.filename().string(), outPath); LX::SafeFlush(log.get()); // Returns success diff --git a/LX-LLVM.vcxproj b/LX-LLVM.vcxproj index 2bf7af3..9541de2 100644 --- a/LX-LLVM.vcxproj +++ b/LX-LLVM.vcxproj @@ -24,7 +24,7 @@ {cc37e36f-b3b3-41b0-a887-01e8efe84994} LXLLVM 10.0 - LX-Compiler + Compiler-Frontend @@ -137,7 +137,7 @@ - + diff --git a/LX-LLVM.vcxproj.filters b/LX-LLVM.vcxproj.filters index d3e9077..6695468 100644 --- a/LX-LLVM.vcxproj.filters +++ b/LX-LLVM.vcxproj.filters @@ -1,6 +1,6 @@  - + \ No newline at end of file diff --git a/Parser/src/GenIR.cpp b/Parser/src/GenIR.cpp index 70baf07..cbb3aa5 100644 --- a/Parser/src/GenIR.cpp +++ b/Parser/src/GenIR.cpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace LX @@ -38,12 +39,16 @@ namespace LX } // Verifies the function works // - ThrowIf(llvm::verifyFunction(*func), &llvm::errs()); // <- TODO: Make error type + ThrowIf(llvm::verifyFunction(*func)); // <- TODO: Make error type } // Turns an abstract binary tree into LLVM intermediate representation // - void GenerateIR(FileAST& ast, const std::string& name) + void GenerateIR(FileAST& ast, const std::string& name, const std::filesystem::path& IRPath) { + // Opens the file to output the IR // + std::error_code EC; + llvm::raw_fd_ostream file(IRPath.string(), EC); + // Creates the LLVM variables needed for generating IR that are shared between functions // InfoLLVM LLVM(name); @@ -53,7 +58,7 @@ namespace LX GenerateFunctionIR(func, LLVM); } - // Outputs the IR to the console // - LLVM.module.print(llvm::outs(), nullptr); + // Outputs the IR to the output file // + LLVM.module.print(file, nullptr); } } diff --git a/common/Parser.h b/common/Parser.h index 5d19845..42e74ca 100644 --- a/common/Parser.h +++ b/common/Parser.h @@ -5,6 +5,9 @@ #include +// Foward declares STD stuff that is passed around // +namespace std::filesystem { class path; } + // Foward declares all items of the llvm lib that we need // // Done to avoid including LLVM.h to shorten compile times // namespace llvm { class Value; } @@ -85,5 +88,5 @@ namespace LX FileAST TurnTokensIntoAbstractSyntaxTree(std::vector& tokens, std::ofstream* log); // Turns an abstract binary tree into LLVM intermediate representation // - void GenerateIR(FileAST& ast, const std::string& name); + void GenerateIR(FileAST& ast, const std::string& name, const std::filesystem::path& IRPath); }