From d8d943929c25151fd6a8aadd6ea4cdbd5065845a Mon Sep 17 00:00:00 2001 From: Amlal Date: Wed, 7 May 2025 09:18:33 +0200 Subject: feat(LibCompiler): Reworking architecture to integrate the Arch() method in the AssemblyInterface. Signed-off-by: Amlal --- dev/LibCompiler/AssemblyInterface.h | 34 ++++--- dev/LibCompiler/Detail/ClUtils.h | 2 +- dev/LibCompiler/ErrorID.h | 1 + dev/LibCompiler/Parser.h | 2 +- .../src/AssemblyFactory+AssemblyInterface.cc | 51 ++++++++++ dev/LibCompiler/src/AssemblyFactory.cc | 51 ---------- dev/LibCompiler/src/CCompiler64x0.cc | 10 +- dev/LibCompiler/src/CCompilerARM64.cc | 8 +- dev/LibCompiler/src/CCompilerPower64.cc | 8 +- dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc | 107 ++++++--------------- 10 files changed, 113 insertions(+), 161 deletions(-) create mode 100644 dev/LibCompiler/src/AssemblyFactory+AssemblyInterface.cc delete mode 100644 dev/LibCompiler/src/AssemblyFactory.cc (limited to 'dev/LibCompiler') diff --git a/dev/LibCompiler/AssemblyInterface.h b/dev/LibCompiler/AssemblyInterface.h index 5450f84..b612962 100644 --- a/dev/LibCompiler/AssemblyInterface.h +++ b/dev/LibCompiler/AssemblyInterface.h @@ -13,19 +13,8 @@ #define ASSEMBLY_INTERFACE : public LibCompiler::AssemblyInterface namespace LibCompiler { -/// @brief Assembly to binary generator class. -/// @note This interface creates according to the CPU target of the child class. -class AssemblyInterface { - public: - explicit AssemblyInterface() = default; - virtual ~AssemblyInterface() = default; - - LIBCOMPILER_COPY_DEFAULT(AssemblyInterface); - - /// @brief compile to object file. - /// @note Example C++ -> MASM -> AE object. - virtual Int32 CompileToFormat(std::string src, Int32 arch) = 0; -}; +class AssemblyFactory; +class AssemblyInterface; /// @brief Simple assembly factory class AssemblyFactory final { @@ -37,7 +26,8 @@ class AssemblyFactory final { public: enum { - kArchAMD64, + kArchInvalid = 0, + kArchAMD64 = 100, kArch32x0, kArch64x0, kArchRISCV, @@ -55,6 +45,22 @@ class AssemblyFactory final { AssemblyInterface* fMounted{nullptr}; }; +/// @brief Assembly to binary generator class. +/// @note This interface creates according to the CPU target of the child class. +class AssemblyInterface { + public: + explicit AssemblyInterface() = default; + virtual ~AssemblyInterface() = default; + + LIBCOMPILER_COPY_DEFAULT(AssemblyInterface); + + [[maybe_unused]] virtual Int32 Arch() noexcept { return AssemblyFactory::kArchAMD64; } + + /// @brief compile to object file. + /// @note Example C++ -> MASM -> AE object. + virtual Int32 CompileToFormat(std::string src, Int32 arch) = 0; +}; + union NumberCastBase { NumberCastBase() = default; ~NumberCastBase() = default; diff --git a/dev/LibCompiler/Detail/ClUtils.h b/dev/LibCompiler/Detail/ClUtils.h index feb17fc..f47101a 100644 --- a/dev/LibCompiler/Detail/ClUtils.h +++ b/dev/LibCompiler/Detail/ClUtils.h @@ -51,7 +51,7 @@ inline void print_warning(std::string reason, std::string file) noexcept { /// @internal inline void segfault_handler(std::int32_t _) { - pfd::notify("LibCompiler", "Driver just crashed, please report this to the developers."); + pfd::notify("LibCompiler", "Driver just crashed, please report this on the GitHub issues page."); std::exit(LIBCOMPILER_EXEC_ERROR); } } // namespace Detail diff --git a/dev/LibCompiler/ErrorID.h b/dev/LibCompiler/ErrorID.h index 380fa69..45bb393 100644 --- a/dev/LibCompiler/ErrorID.h +++ b/dev/LibCompiler/ErrorID.h @@ -21,3 +21,4 @@ #define LIBCOMPILER_INVALID_DATA -35 #define LIBCOMPILER_UNIMPLEMENTED -36 #define LIBCOMPILER_FAT_ERROR -37 +#define LIBCOMPILER_INVALID_ARCH -38 diff --git a/dev/LibCompiler/Parser.h b/dev/LibCompiler/Parser.h index 64c46d6..6baff7e 100644 --- a/dev/LibCompiler/Parser.h +++ b/dev/LibCompiler/Parser.h @@ -26,7 +26,7 @@ class ICompilerFrontend { //! @brief Compile a syntax tree ouf of the text. //! Also takes the source file name for metadata. - virtual bool Compile(std::string text, const std::string file) = 0; + virtual bool Compile(std::string text, std::string file) = 0; //! @brief What language are we dealing with? virtual const char* Language() { return kInvalidFrontend; } diff --git a/dev/LibCompiler/src/AssemblyFactory+AssemblyInterface.cc b/dev/LibCompiler/src/AssemblyFactory+AssemblyInterface.cc new file mode 100644 index 0000000..11655fb --- /dev/null +++ b/dev/LibCompiler/src/AssemblyFactory+AssemblyInterface.cc @@ -0,0 +1,51 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + +------------------------------------------- */ + +#include +#include + +/** + * @file AssemblyFactory.cxx + * @author amlal (amlal@nekernel.org) + * @brief Assembler Kit + * @version 0.1 + * @date 2024-01-27 + * + * @copyright Copyright (c) 2024-2025 Amlal El Mahrouss + * + */ + +//! @file Asm.cpp +//! @brief AssemblyKit source implementation. + +namespace LibCompiler { +///! @brief Compile for specific format (ELF, PEF, ZBIN) +Int32 AssemblyFactory::Compile(std::string sourceFile, const Int32& arch) noexcept { + if (sourceFile.length() < 1 || !fMounted) return LIBCOMPILER_UNIMPLEMENTED; + + if (arch != fMounted->Arch()) return LIBCOMPILER_INVALID_ARCH; + + return fMounted->CompileToFormat(sourceFile, arch); +} + +///! @brief mount assembly backend. +void AssemblyFactory::Mount(AssemblyInterface* mountPtr) noexcept { + if (mountPtr) { + fMounted = mountPtr; + } +} + +///! @brief Unmount assembler. +AssemblyInterface* AssemblyFactory::Unmount() noexcept { + auto mount_prev = fMounted; + + if (mount_prev) { + fMounted = nullptr; + } + + return mount_prev; +} +} // namespace LibCompiler diff --git a/dev/LibCompiler/src/AssemblyFactory.cc b/dev/LibCompiler/src/AssemblyFactory.cc deleted file mode 100644 index 8fa12a8..0000000 --- a/dev/LibCompiler/src/AssemblyFactory.cc +++ /dev/null @@ -1,51 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved - -------------------------------------------- */ - -#include -#include - -/** - * @file AssemblyFactory.cxx - * @author amlal (amlal@nekernel.org) - * @brief Assembler Kit - * @version 0.1 - * @date 2024-01-27 - * - * @copyright Copyright (c) 2024-2025 Amlal El Mahrouss - * - */ - -#include - -//! @file Asm.cpp -//! @brief AssemblyKit source implementation. - -namespace LibCompiler { -///! @brief Compile for specific format (ELF, PEF, ZBIN) -Int32 AssemblyFactory::Compile(std::string sourceFile, const Int32& arch) noexcept { - if (sourceFile.length() < 1 || !fMounted) return LIBCOMPILER_UNIMPLEMENTED; - - return fMounted->CompileToFormat(sourceFile, arch); -} - -///! @brief mount assembly backend. -void AssemblyFactory::Mount(AssemblyInterface* mountPtr) noexcept { - if (mountPtr) { - fMounted = mountPtr; - } -} - -///! @brief Unmount assembler. -AssemblyInterface* AssemblyFactory::Unmount() noexcept { - auto mount_prev = fMounted; - - if (mount_prev) { - fMounted = nullptr; - } - - return mount_prev; -} -} // namespace LibCompiler diff --git a/dev/LibCompiler/src/CCompiler64x0.cc b/dev/LibCompiler/src/CCompiler64x0.cc index c100e96..79d32ce 100644 --- a/dev/LibCompiler/src/CCompiler64x0.cc +++ b/dev/LibCompiler/src/CCompiler64x0.cc @@ -142,7 +142,7 @@ class CompilerFrontend64x0 final : public LibCompiler::ICompilerFrontend { LIBCOMPILER_COPY_DEFAULT(CompilerFrontend64x0); std::string Check(const char* text, const char* file); - bool Compile(std::string text, const std::string file) override; + bool Compile(std::string text, std::string file) override; const char* Language() override { return "64k C"; } }; @@ -182,7 +182,7 @@ union double_cast final { ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerFrontend64x0::Compile(std::string text_, const std::string file) { +bool CompilerFrontend64x0::Compile(std::string text_, std::string file) { std::string text = text_; bool typeFound = false; @@ -1051,11 +1051,11 @@ class AssemblyCCInterface final ASSEMBLY_INTERFACE { LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); - [[maybe_unused]] static Int32 Arch() noexcept { return LibCompiler::AssemblyFactory::kArch64x0; } + [[maybe_unused]] Int32 Arch() noexcept override { + return LibCompiler::AssemblyFactory::kArch64x0; + } Int32 CompileToFormat(std::string src, Int32 arch) override { - if (arch != AssemblyCCInterface::Arch()) return 1; - if (kCompilerFrontend == nullptr) return 1; /* @brief copy contents wihtout extension */ diff --git a/dev/LibCompiler/src/CCompilerARM64.cc b/dev/LibCompiler/src/CCompilerARM64.cc index a992349..a5ddf43 100644 --- a/dev/LibCompiler/src/CCompilerARM64.cc +++ b/dev/LibCompiler/src/CCompilerARM64.cc @@ -142,7 +142,7 @@ class CompilerFrontendARM64 final : public LibCompiler::ICompilerFrontend { LIBCOMPILER_COPY_DEFAULT(CompilerFrontendARM64); std::string Check(const char* text, const char* file); - bool Compile(std::string text, const std::string file) override; + bool Compile(std::string text, std::string file) override; const char* Language() override { return "64k C"; } }; @@ -182,7 +182,7 @@ union double_cast final { ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerFrontendARM64::Compile(std::string text, const std::string file) { +bool CompilerFrontendARM64::Compile(std::string text, std::string file) { bool typeFound = false; bool fnFound = false; @@ -1049,13 +1049,11 @@ class AssemblyCCInterface final ASSEMBLY_INTERFACE { LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); - [[maybe_unused]] static Int32 Arch() noexcept { + [[maybe_unused]] Int32 Arch() noexcept override { return LibCompiler::AssemblyFactory::kArchAARCH64; } Int32 CompileToFormat(std::string src, Int32 arch) override { - if (arch != AssemblyCCInterface::Arch()) return 1; - if (kCompilerFrontend == nullptr) return 1; /* @brief copy contents wihtout extension */ diff --git a/dev/LibCompiler/src/CCompilerPower64.cc b/dev/LibCompiler/src/CCompilerPower64.cc index c65f575..f2eba43 100644 --- a/dev/LibCompiler/src/CCompilerPower64.cc +++ b/dev/LibCompiler/src/CCompilerPower64.cc @@ -133,7 +133,7 @@ class CompilerFrontendPower64 final : public LibCompiler::ICompilerFrontend { LIBCOMPILER_COPY_DEFAULT(CompilerFrontendPower64); std::string Check(const char* text, const char* file); - bool Compile(std::string text, const std::string file) override; + bool Compile(std::string text, std::string file) override; const char* Language() override { return "POWER C"; } }; @@ -173,7 +173,7 @@ union double_cast final { ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerFrontendPower64::Compile(std::string text_, const std::string file) { +bool CompilerFrontendPower64::Compile(std::string text_, std::string file) { std::string text = text_; bool typeFound = false; @@ -1069,13 +1069,11 @@ class AssemblyMountpointCLang final ASSEMBLY_INTERFACE { LIBCOMPILER_COPY_DEFAULT(AssemblyMountpointCLang); - [[maybe_unused]] static Int32 Arch() noexcept { + [[maybe_unused]] Int32 Arch() noexcept override { return LibCompiler::AssemblyFactory::kArchPowerPC; } Int32 CompileToFormat(std::string src, Int32 arch) override { - if (arch != AssemblyMountpointCLang::Arch()) return 1; - if (kCompilerFrontend == nullptr) return 1; /* @brief copy contents wihtout extension */ diff --git a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc index f5e9bf7..344622e 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc @@ -148,7 +148,7 @@ class CompilerFrontendCPlusPlus final : public LibCompiler::ICompilerFrontend { LIBCOMPILER_COPY_DEFAULT(CompilerFrontendCPlusPlus); - Boolean Compile(const std::string text, const std::string file) override; + Boolean Compile(const std::string text, std::string file) override; const char* Language() override; }; @@ -174,7 +174,7 @@ static std::size_t kFunctionEmbedLevel = 0UL; /// detail namespaces const char* CompilerFrontendCPlusPlus::Language() { - return "NeKernel C++"; + return "AMD64 C++"; } static std::uintptr_t kOrigin = 0x1000000; @@ -187,37 +187,15 @@ static std::vector> kOriginMap; ///////////////////////////////////////////////////////////////////////////////////////// -Boolean CompilerFrontendCPlusPlus::Compile(std::string text, const std::string file) { +Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { if (text.empty()) return false; - // Clean whitespace and tabs - std::string cleanLine = text; - cleanLine.erase(std::remove(cleanLine.begin(), cleanLine.end(), '\t'), cleanLine.end()); - cleanLine.erase(0, cleanLine.find_first_not_of(" \r\n")); - cleanLine.erase(cleanLine.find_last_not_of(" \r\n") + 1); - - // Skip empty, doc, or block comment lines - if (cleanLine.empty() || cleanLine.starts_with("///") || cleanLine.starts_with("//") || - cleanLine.starts_with("/*")) - return false; - std::size_t index = 0UL; std::vector> keywords_list; - Boolean found = false; - static Boolean commentBlock = false; - for (auto& keyword : kKeywords) { if (text.find(keyword.keyword_name) != std::string::npos) { switch (keyword.keyword_kind) { - case LibCompiler::kKeywordKindCommentMultiLineStart: { - commentBlock = true; - return true; - } - case LibCompiler::kKeywordKindCommentMultiLineEnd: { - commentBlock = false; - break; - } case LibCompiler::kKeywordKindCommentInline: { break; } @@ -239,22 +217,13 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, const std::string f keywords_list.emplace_back(std::make_pair(keyword, index)); ++index; - - found = true; } } - if (!found && !commentBlock) { - for (size_t i = 0; i < text.size(); i++) { - if (isalnum(text[i])) { - Detail::print_error("syntax error: " + text, file); - return NO; - } - } - } + auto syntax_tree = LibCompiler::SyntaxLeafList::SyntaxLeaf(); for (auto& keyword : keywords_list) { - auto syntax_tree = LibCompiler::SyntaxLeafList::SyntaxLeaf(); + kStdOut << keyword.second; switch (keyword.first.keyword_kind) { case LibCompiler::KeywordKind::kKeywordKindClass: { @@ -408,10 +377,10 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, const std::string f return fnName == pair.first; }); - std::stringstream ss; - ss << std::hex << it->second; - if (it != kOriginMap.end()) { + std::stringstream ss; + ss << std::hex << it->second; + syntax_tree.fUserValue = "jmp " + ss.str() + "\n"; kOrigin += 1UL; } @@ -506,12 +475,12 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, const std::string f valueOfVar.erase(i, 1); } - constexpr auto cTrueVal = "true"; - constexpr auto cFalseVal = "false"; + constexpr auto kTrueVal = "true"; + constexpr auto kFalseVal = "false"; - if (valueOfVar == cTrueVal) { + if (valueOfVar == kTrueVal) { valueOfVar = "1"; - } else if (valueOfVar == cFalseVal) { + } else if (valueOfVar == kFalseVal) { valueOfVar = "0"; } @@ -620,14 +589,14 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, const std::string f valueOfVar.erase(valueOfVar.find("\t"), 1); } - constexpr auto cTrueVal = "true"; - constexpr auto cFalseVal = "false"; + constexpr auto kTrueVal = "true"; + constexpr auto kFalseVal = "false"; /// interpet boolean values, since we're on C++ - if (valueOfVar == cTrueVal) { + if (valueOfVar == kTrueVal) { valueOfVar = "1"; - } else if (valueOfVar == cFalseVal) { + } else if (valueOfVar == kFalseVal) { valueOfVar = "0"; } @@ -726,16 +695,15 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, const std::string f } } default: { - break; + continue; } } - syntax_tree.fUserData = keyword.first; - - kState.fOutputAssembly << syntax_tree.fUserValue; + break; } lc_compile_ok: + kState.fOutputAssembly << syntax_tree.fUserValue; return true; } @@ -754,37 +722,23 @@ class AssemblyCPlusPlusInterface final ASSEMBLY_INTERFACE { LIBCOMPILER_COPY_DEFAULT(AssemblyCPlusPlusInterface); - [[maybe_unused]] static Int32 Arch() noexcept { return LibCompiler::AssemblyFactory::kArchAMD64; } + [[maybe_unused]] Int32 Arch() noexcept override { + return LibCompiler::AssemblyFactory::kArchAMD64; + } Int32 CompileToFormat(std::string src, Int32 arch) override { - if (arch != AssemblyCPlusPlusInterface::Arch()) return 1; - - if (kCompilerFrontend == nullptr) return 1; + if (kCompilerFrontend == nullptr) return kExitNO; /* @brief copy contents wihtout extension */ - std::string src_file = src; - std::ifstream src_fp = std::ifstream(src_file, std::ios::in); + std::ifstream src_fp = std::ifstream(src, std::ios::in); - const char* cExts[] = kAsmFileExts; + std::string dest = src; + dest += ".masm"; - std::string dest = src_file; - dest += cExts[2]; + std::string line_source; kState.fOutputAssembly.open(dest); - if (!kState.fOutputAssembly.good()) return kExitNO; - - kState.fOutputAssembly - << "; Assembler Dialect: AMD64 LibCompiler Assembler. (Generated from C++)\n"; - kState.fOutputAssembly << "; Date: " << LibCompiler::current_date() << "\n" - << "#bits 64\n#org " << kOrigin << "\n"; - - // =================================== - // Parse source file. - // =================================== - - std::string line_source; - while (std::getline(src_fp, line_source)) { kCompilerFrontend->Compile(line_source, src); } @@ -951,12 +905,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { return kExitNO; } - kStdOut << "CPlusPlusCompilerAMD64: Building: " << argv[index] << std::endl; - - if (kFactory.Compile(argv_i, kMachine) != kExitOK) { - kStdOut << "CPlusPlusCompilerAMD64: Failed to build: " << argv[index] << std::endl; - return kExitNO; - } + kFactory.Compile(argv_i, kMachine); } return kExitOK; -- cgit v1.2.3