diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-27 23:02:19 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-27 23:02:19 +0200 |
| commit | 7878653e8dbd65d94ea8ea8bae6e0afd3c3d0af3 (patch) | |
| tree | 5f77d7f9d500efcafdcb4efb787ce71a5d5ec1ab /dev/LibCompiler | |
| parent | d12204e7302b8f3bd521c782094338733abfded1 (diff) | |
fix: fix SEGFAULT on CxxDrv (AMD64), caused by a stack corruption.
refactor: Refactor LibCompiler's codebase, deperecate older C compilers,
and fully focusing on C++ now.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/LibCompiler')
| -rw-r--r-- | dev/LibCompiler/BasicString.h (renamed from dev/LibCompiler/PString.h) | 26 | ||||
| -rw-r--r-- | dev/LibCompiler/CodeGen.h | 6 | ||||
| -rw-r--r-- | dev/LibCompiler/Frontend.h | 50 | ||||
| -rw-r--r-- | dev/LibCompiler/Util/DylibHelpers.h | 13 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/.keep | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/Frontend/CCompiler64x0.cc (renamed from dev/LibCompiler/src/Cl/CCompiler64x0.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/Frontend/CCompilerARM64.cc (renamed from dev/LibCompiler/src/Cl/CCompilerARM64.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/Frontend/CCompilerPower64.cc (renamed from dev/LibCompiler/src/Cl/CCompilerPower64.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/Assembler32x0.cc (renamed from dev/LibCompiler/src/Asm/Assembler32x0.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/Assembler64x0.cc (renamed from dev/LibCompiler/src/Asm/Assembler64x0.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/AssemblerAMD64.cc (renamed from dev/LibCompiler/src/Asm/AssemblerAMD64.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/AssemblerARM64.cc (renamed from dev/LibCompiler/src/Asm/AssemblerARM64.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/AssemblerPowerPC.cc (renamed from dev/LibCompiler/src/Asm/AssemblerPowerPC.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/src/BasicString.cc (renamed from dev/LibCompiler/src/PString.cc) | 32 | ||||
| -rw-r--r-- | dev/LibCompiler/src/CodeGen.cc | 5 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Frontend/CPlusPlusCompilerAMD64.cc (renamed from dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc) | 159 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc (renamed from dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc) | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Macro/CPlusPlusCompilerPreProcessor.cc (renamed from dev/LibCompiler/src/PP/CPlusPlusCompilerPreProcessor.cc) | 0 |
18 files changed, 145 insertions, 146 deletions
diff --git a/dev/LibCompiler/PString.h b/dev/LibCompiler/BasicString.h index a54be06..b69deae 100644 --- a/dev/LibCompiler/PString.h +++ b/dev/LibCompiler/BasicString.h @@ -14,23 +14,23 @@ namespace LibCompiler { class StringBuilder; -class PString; +class BasicString; /** - * @brief PString class, contains a C string and manages it. + * @brief BasicString class, contains a C string and manages it. * @note No need to manage it it's getting deleted by default. */ -class PString final { +class BasicString final { public: - explicit PString() = delete; + explicit BasicString() = delete; - explicit PString(SizeType Sz) noexcept : m_Sz(Sz) { + explicit BasicString(SizeType Sz) noexcept : m_Sz(Sz) { m_Data = new CharType[Sz]; assert(m_Data); } - ~PString() noexcept { + ~BasicString() noexcept { if (m_Data) { memset(m_Data, 0, m_Sz); delete[] m_Data; @@ -39,7 +39,7 @@ class PString final { } } - LIBCOMPILER_COPY_DEFAULT(PString); + LIBCOMPILER_COPY_DEFAULT(BasicString); CharType* Data(); const CharType* CData() const; @@ -48,11 +48,11 @@ class PString final { bool operator==(const CharType* rhs) const; bool operator!=(const CharType* rhs) const; - bool operator==(const PString& rhs) const; - bool operator!=(const PString& rhs) const; + bool operator==(const BasicString& rhs) const; + bool operator!=(const BasicString& rhs) const; - PString& operator+=(const CharType* rhs); - PString& operator+=(const PString& rhs); + BasicString& operator+=(const CharType* rhs); + BasicString& operator+=(const BasicString& rhs); operator bool() { return m_Data && m_Data[0] != 0; } @@ -71,12 +71,12 @@ class PString final { * @note These results shall call be delete[] after they're used. */ struct StringBuilder final { - static PString Construct(const CharType* data); + static BasicString Construct(const CharType* data); static const char* FromInt(const char* fmt, int n); static const char* FromBool(const char* fmt, bool n); static const char* Format(const char* fmt, const char* from); static bool Equals(const char* lhs, const char* rhs); }; -using PStringOr = ErrorOr<PString>; +using PStringOr = ErrorOr<BasicString>; } // namespace LibCompiler diff --git a/dev/LibCompiler/CodeGen.h b/dev/LibCompiler/CodeGen.h index 1e955d8..8edcff1 100644 --- a/dev/LibCompiler/CodeGen.h +++ b/dev/LibCompiler/CodeGen.h @@ -8,10 +8,10 @@ #include <LibCompiler/Defines.h> #include <LibCompiler/Macros.h> -#include <LibCompiler/PString.h> +#include <LibCompiler/BasicString.h> -#define LC_ASSEMBLY_INTERFACE : public LibCompiler::AssemblyInterface -#define LC_ENCODER : public LibCompiler::EncoderInterface +#define LC_ASSEMBLY_INTERFACE : public ::LibCompiler::AssemblyInterface +#define LC_ENCODER : public ::LibCompiler::EncoderInterface namespace LibCompiler { class AssemblyFactory; diff --git a/dev/LibCompiler/Frontend.h b/dev/LibCompiler/Frontend.h index 5f054ac..f39c9d4 100644 --- a/dev/LibCompiler/Frontend.h +++ b/dev/LibCompiler/Frontend.h @@ -8,34 +8,11 @@ #include <LibCompiler/CodeGen.h> -#define LC_COMPILER_FRONTEND : public LibCompiler::CompilerFrontendInterface +#define LC_COMPILER_FRONTEND : public ::LibCompiler::CompilerFrontendInterface namespace LibCompiler { inline static auto kInvalidFrontend = "?"; -/// @brief Compiler backend, implements a frontend, such as C, C++... -/// See Toolchain, for some examples. -class CompilerFrontendInterface { - public: - explicit CompilerFrontendInterface() = default; - virtual ~CompilerFrontendInterface() = default; - - LIBCOMPILER_COPY_DEFAULT(CompilerFrontendInterface); - - // NOTE: cast this to your user defined ast. - typedef void* AstType; - - //! @brief Compile a syntax tree ouf of the text. - //! Also takes the source file name for metadata. - - virtual bool Compile(std::string text, std::string file) = 0; - - //! @brief What language are we dealing with? - virtual const char* Language() { return kInvalidFrontend; } - - virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } -}; - struct SyntaxLeafList; struct SyntaxLeafList; struct CompilerKeyword; @@ -81,7 +58,7 @@ enum KeywordKind { /// \brief Compiler keyword information struct. struct CompilerKeyword { - STLString keyword_name{""}; + STLString keyword_name{""}; KeywordKind keyword_kind{kKeywordKindInvalid}; }; @@ -118,4 +95,27 @@ BOOL find_word(std::string haystack, std::string needle) noexcept; /// \param needle /// \return position of needle. std::size_t find_word_range(std::string haystack, std::string needle) noexcept; + +/// @brief Compiler backend, implements a frontend, such as C, C++... +/// See Toolchain, for some examples. +class CompilerFrontendInterface { + public: + explicit CompilerFrontendInterface() = default; + virtual ~CompilerFrontendInterface() = default; + + LIBCOMPILER_COPY_DEFAULT(CompilerFrontendInterface); + + // NOTE: cast this to your user defined ast. + typedef void* AstType; + + //! @brief Compile a syntax tree ouf of the text. + //! Also takes the source file name for metadata. + + virtual LibCompiler::SyntaxLeafList::SyntaxLeaf Compile(std::string text, std::string file) = 0; + + //! @brief What language are we dealing with? + virtual const char* Language() { return kInvalidFrontend; } + + virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } +}; } // namespace LibCompiler diff --git a/dev/LibCompiler/Util/DylibHelpers.h b/dev/LibCompiler/Util/DylibHelpers.h new file mode 100644 index 0000000..71ca230 --- /dev/null +++ b/dev/LibCompiler/Util/DylibHelpers.h @@ -0,0 +1,13 @@ +/* ------------------------------------------- + + Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include <LibCompiler/Defines.h> +#include <dlfcn.h> + +typedef Int32(*LibCompilerEntrypoint)(Int32 argc, CharType const* argv[]); +typedef VoidPtr DylibHandle; diff --git a/dev/LibCompiler/deprecated/.keep b/dev/LibCompiler/deprecated/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/dev/LibCompiler/deprecated/.keep diff --git a/dev/LibCompiler/src/Cl/CCompiler64x0.cc b/dev/LibCompiler/deprecated/Frontend/CCompiler64x0.cc index fc31ef9..fc31ef9 100644 --- a/dev/LibCompiler/src/Cl/CCompiler64x0.cc +++ b/dev/LibCompiler/deprecated/Frontend/CCompiler64x0.cc diff --git a/dev/LibCompiler/src/Cl/CCompilerARM64.cc b/dev/LibCompiler/deprecated/Frontend/CCompilerARM64.cc index 02e39f9..02e39f9 100644 --- a/dev/LibCompiler/src/Cl/CCompilerARM64.cc +++ b/dev/LibCompiler/deprecated/Frontend/CCompilerARM64.cc diff --git a/dev/LibCompiler/src/Cl/CCompilerPower64.cc b/dev/LibCompiler/deprecated/Frontend/CCompilerPower64.cc index 216891a..216891a 100644 --- a/dev/LibCompiler/src/Cl/CCompilerPower64.cc +++ b/dev/LibCompiler/deprecated/Frontend/CCompilerPower64.cc diff --git a/dev/LibCompiler/src/Asm/Assembler32x0.cc b/dev/LibCompiler/src/Backend/Assembler32x0.cc index 683b866..683b866 100644 --- a/dev/LibCompiler/src/Asm/Assembler32x0.cc +++ b/dev/LibCompiler/src/Backend/Assembler32x0.cc diff --git a/dev/LibCompiler/src/Asm/Assembler64x0.cc b/dev/LibCompiler/src/Backend/Assembler64x0.cc index fb96708..fb96708 100644 --- a/dev/LibCompiler/src/Asm/Assembler64x0.cc +++ b/dev/LibCompiler/src/Backend/Assembler64x0.cc diff --git a/dev/LibCompiler/src/Asm/AssemblerAMD64.cc b/dev/LibCompiler/src/Backend/AssemblerAMD64.cc index e9f3ad5..e9f3ad5 100644 --- a/dev/LibCompiler/src/Asm/AssemblerAMD64.cc +++ b/dev/LibCompiler/src/Backend/AssemblerAMD64.cc diff --git a/dev/LibCompiler/src/Asm/AssemblerARM64.cc b/dev/LibCompiler/src/Backend/AssemblerARM64.cc index 8fad6b5..8fad6b5 100644 --- a/dev/LibCompiler/src/Asm/AssemblerARM64.cc +++ b/dev/LibCompiler/src/Backend/AssemblerARM64.cc diff --git a/dev/LibCompiler/src/Asm/AssemblerPowerPC.cc b/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc index 86a70b9..86a70b9 100644 --- a/dev/LibCompiler/src/Asm/AssemblerPowerPC.cc +++ b/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc diff --git a/dev/LibCompiler/src/PString.cc b/dev/LibCompiler/src/BasicString.cc index c8c060b..78bfcaa 100644 --- a/dev/LibCompiler/src/PString.cc +++ b/dev/LibCompiler/src/BasicString.cc @@ -8,7 +8,7 @@ */ /** - * @file PString.cxx + * @file BasicString.cxx * @author Amlal (amlal@el-mahrouss-logic.com) * @brief C++ string manipulation API. * @version 0.2 @@ -18,22 +18,22 @@ * */ -#include <LibCompiler/PString.h> +#include <LibCompiler/BasicString.h> namespace LibCompiler { -CharType* PString::Data() { +CharType* BasicString::Data() { return m_Data; } -const CharType* PString::CData() const { +const CharType* BasicString::CData() const { return m_Data; } -SizeType PString::Length() const { +SizeType BasicString::Length() const { return strlen(m_Data); } -bool PString::operator==(const PString& rhs) const { +bool BasicString::operator==(const BasicString& rhs) const { if (rhs.Length() != Length()) return false; for (SizeType index = 0; index < Length(); ++index) { @@ -43,7 +43,7 @@ bool PString::operator==(const PString& rhs) const { return true; } -bool PString::operator==(const CharType* rhs) const { +bool BasicString::operator==(const CharType* rhs) const { if (string_length(rhs) != Length()) return false; for (SizeType index = 0; index < string_length(rhs); ++index) { @@ -53,7 +53,7 @@ bool PString::operator==(const CharType* rhs) const { return true; } -bool PString::operator!=(const PString& rhs) const { +bool BasicString::operator!=(const BasicString& rhs) const { if (rhs.Length() != Length()) return false; for (SizeType index = 0; index < rhs.Length(); ++index) { @@ -63,7 +63,7 @@ bool PString::operator!=(const PString& rhs) const { return true; } -bool PString::operator!=(const CharType* rhs) const { +bool BasicString::operator!=(const CharType* rhs) const { if (string_length(rhs) != Length()) return false; for (SizeType index = 0; index < string_length(rhs); ++index) { @@ -73,10 +73,10 @@ bool PString::operator!=(const CharType* rhs) const { return true; } -PString StringBuilder::Construct(const CharType* data) { - if (!data || *data == 0) return PString(0); +BasicString StringBuilder::Construct(const CharType* data) { + if (!data || *data == 0) return BasicString(0); - PString view(strlen(data)); + BasicString view(strlen(data)); view += data; return view; @@ -183,9 +183,9 @@ const char* StringBuilder::Format(const char* fmt, const char* fmtRight) { return ret; } -PString& PString::operator+=(const CharType* rhs) { +BasicString& BasicString::operator+=(const CharType* rhs) { if (strlen(rhs) > this->m_Sz) { - throw std::runtime_error("out_of_bounds: PString"); + throw std::runtime_error("out_of_bounds: BasicString"); } memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); @@ -194,9 +194,9 @@ PString& PString::operator+=(const CharType* rhs) { return *this; } -PString& PString::operator+=(const PString& rhs) { +BasicString& BasicString::operator+=(const BasicString& rhs) { if (rhs.m_Cur > this->m_Sz) { - throw std::runtime_error("out_of_bounds: PString"); + throw std::runtime_error("out_of_bounds: BasicString"); } memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData())); diff --git a/dev/LibCompiler/src/CodeGen.cc b/dev/LibCompiler/src/CodeGen.cc index 31a809b..3f215c5 100644 --- a/dev/LibCompiler/src/CodeGen.cc +++ b/dev/LibCompiler/src/CodeGen.cc @@ -24,10 +24,9 @@ namespace LibCompiler { ///! @brief Compile for specific format (ELF, PEF, ZBIN) Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) noexcept { - if (sourceFile.length() < 1 || !fMounted) return LIBCOMPILER_UNIMPLEMENTED; + if (sourceFile.length() < 1) return LIBCOMPILER_UNIMPLEMENTED; if (!fMounted) return LIBCOMPILER_UNIMPLEMENTED; - if (arch != fMounted->Arch()) return LIBCOMPILER_INVALID_ARCH; try { @@ -48,7 +47,7 @@ void AssemblyFactory::Mount(AssemblyInterface* mountPtr) noexcept { AssemblyInterface* AssemblyFactory::Unmount() noexcept { auto mount_prev = fMounted; - if (mount_prev) { + if (fMounted) { fMounted = nullptr; } diff --git a/dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/Frontend/CPlusPlusCompilerAMD64.cc index bfa1896..217f86b 100644 --- a/dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/Frontend/CPlusPlusCompilerAMD64.cc @@ -68,24 +68,23 @@ std::filesystem::path expand_home(const std::filesystem::path& p) { } struct CompilerRegisterMap final { - std::string fName; - std::string fReg; + LibCompiler::STLString fName; + LibCompiler::STLString fReg; }; /// \brief Offset based struct/class struct CompilerStructMap final { - std::string fName; - std::string fReg; - std::vector<std::pair<UInt32, std::string>> fOffsets; + LibCompiler::STLString fName; + LibCompiler::STLString fReg; + std::vector<std::pair<UInt32, LibCompiler::STLString>> fOffsets; }; /// \brief Compiler state structure. struct CompilerState final { std::vector<CompilerRegisterMap> fStackMapVector; std::vector<CompilerStructMap> fStructMapVector; - std::string fOutputValue; - std::string fLastFile; - std::string fLastError; + LibCompiler::STLString fLastFile; + LibCompiler::STLString fLastError; }; } // namespace Detail @@ -120,14 +119,14 @@ static Boolean kInBraces = false; static size_t kBracesCount = 0UL; /* @brief C++ compiler backend for the NeKernel C++ driver */ -class CompilerFrontendCPlusPlus final : public LibCompiler::CompilerFrontendInterface { +class CompilerFrontendCPlusPlus final LC_COMPILER_FRONTEND { public: explicit CompilerFrontendCPlusPlus() = default; ~CompilerFrontendCPlusPlus() override = default; LIBCOMPILER_COPY_DEFAULT(CompilerFrontendCPlusPlus); - Boolean Compile(const std::string text, std::string file) override; + LibCompiler::SyntaxLeafList::SyntaxLeaf Compile(const LibCompiler::STLString text, LibCompiler::STLString file) override; const char* Language() override; }; @@ -136,15 +135,15 @@ class CompilerFrontendCPlusPlus final : public LibCompiler::CompilerFrontendInte static CompilerFrontendCPlusPlus* kCompilerFrontend = nullptr; -static std::vector<std::string> kRegisterMap; +static std::vector<LibCompiler::STLString> kRegisterMap; -static std::vector<std::string> kRegisterList = { +static std::vector<LibCompiler::STLString> kRegisterList = { "rbx", "rsi", "r10", "r11", "r12", "r13", "r14", "r15", "xmm12", "xmm13", "xmm14", "xmm15", }; /// @brief The PEF calling convention (caller must save rax, rbp) /// @note callee must return via **rax**. -static std::vector<std::string> kRegisterConventionCallList = { +static std::vector<LibCompiler::STLString> kRegisterConventionCallList = { "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", }; @@ -156,8 +155,8 @@ const char* CompilerFrontendCPlusPlus::Language() { return "AMD64 C++"; } -static std::uintptr_t kOrigin = 0x1000000; -static std::vector<std::pair<std::string, std::uintptr_t>> kOriginMap; +static std::uintptr_t kOrigin = 0x1000000; +static std::vector<std::pair<LibCompiler::STLString, std::uintptr_t>> kOriginMap; ///////////////////////////////////////////////////////////////////////////////////////// @@ -166,8 +165,11 @@ static std::vector<std::pair<std::string, std::uintptr_t>> kOriginMap; ///////////////////////////////////////////////////////////////////////////////////////// -Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { - if (text.length() < 1) return false; +LibCompiler::SyntaxLeafList::SyntaxLeaf CompilerFrontendCPlusPlus::Compile( + LibCompiler::STLString text, LibCompiler::STLString file) { + LibCompiler::SyntaxLeafList::SyntaxLeaf syntax_tree; + + if (text.length() < 1) return syntax_tree; std::size_t index = 0UL; std::vector<std::pair<LibCompiler::CompilerKeyword, std::size_t>> keywords_list; @@ -205,10 +207,8 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } } - LibCompiler::SyntaxLeafList::SyntaxLeaf syntax_tree; - for (auto& keyword : keywords_list) { - if (text.find(keyword.first.keyword_name) == std::string::npos) continue; + if (text.find(keyword.first.keyword_name) == LibCompiler::STLString::npos) continue; switch (keyword.first.keyword_kind) { case LibCompiler::KeywordKind::kKeywordKindClass: { @@ -220,15 +220,16 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { std::size_t openParen = text.find("(", keywordPos); std::size_t closeParen = text.find(")", openParen); - if (keywordPos == std::string::npos || openParen == std::string::npos || - closeParen == std::string::npos || closeParen <= openParen) { + if (keywordPos == LibCompiler::STLString::npos || + openParen == LibCompiler::STLString::npos || + closeParen == LibCompiler::STLString::npos || closeParen <= openParen) { Detail::print_error("Malformed if expression: " + text, file); - return false; + break; } auto expr = text.substr(openParen + 1, closeParen - openParen - 1); - if (expr.find(">=") != std::string::npos) { + if (expr.find(">=") != LibCompiler::STLString::npos) { auto left = text.substr( text.find(keyword.first.keyword_name) + keyword.first.keyword_name.size() + 2, expr.find("<=") + strlen("<=")); @@ -295,7 +296,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { done_iterarting_on_if: - std::string fnName = text; + LibCompiler::STLString fnName = text; fnName.erase(fnName.find(keyword.first.keyword_name)); for (auto& ch : fnName) { @@ -318,11 +319,11 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { goto accept; dont_accept: - return false; + break; accept: - std::string fnName = text; - size_t indexFnName = 0; + LibCompiler::STLString fnName = text; + size_t indexFnName = 0; // this one is for the type. for (auto& ch : text) { @@ -334,12 +335,12 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { fnName = text.substr(indexFnName); - if (text.find("return ") != std::string::npos) { + if (text.find("return ") != LibCompiler::STLString::npos) { text.erase(0, text.find("return ")); break; } - if (text.ends_with(";") && text.find("return") == std::string::npos) + if (text.ends_with(";") && text.find("return") == LibCompiler::STLString::npos) goto lc_write_assembly; else if (text.size() <= indexFnName) Detail::print_error("Invalid function name: " + fnName, file); @@ -367,10 +368,11 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { break; lc_write_assembly: - auto it = std::find_if(kOriginMap.begin(), kOriginMap.end(), - [&fnName](std::pair<std::string, std::uintptr_t> pair) -> bool { - return fnName == pair.first; - }); + auto it = + std::find_if(kOriginMap.begin(), kOriginMap.end(), + [&fnName](std::pair<LibCompiler::STLString, std::uintptr_t> pair) -> bool { + return fnName == pair.first; + }); if (it != kOriginMap.end()) { std::stringstream ss; @@ -399,7 +401,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { case LibCompiler::KeywordKind::kKeywordKindVariableInc: case LibCompiler::KeywordKind::kKeywordKindVariableDec: case LibCompiler::KeywordKind::kKeywordKindVariableAssign: { - std::string valueOfVar = ""; + LibCompiler::STLString valueOfVar = ""; if (keyword.first.keyword_kind == LibCompiler::KeywordKind::kKeywordKindVariableInc) { valueOfVar = text.substr(text.find("+=") + 2); @@ -413,12 +415,12 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { break; } - while (valueOfVar.find(";") != std::string::npos && + while (valueOfVar.find(";") != LibCompiler::STLString::npos && keyword.first.keyword_kind != LibCompiler::KeywordKind::kKeywordKindEndInstr) { valueOfVar.erase(valueOfVar.find(";")); } - std::string varName = text; + LibCompiler::STLString varName = text; if (keyword.first.keyword_kind == LibCompiler::KeywordKind::kKeywordKindVariableInc) { varName.erase(varName.find("+=")); @@ -436,7 +438,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { for (auto& keyword : kKeywords) { if (keyword.keyword_kind == LibCompiler::kKeywordKindType) { - if (text.find(keyword.keyword_name) != std::string::npos) { + if (text.find(keyword.keyword_name) != LibCompiler::STLString::npos) { if (text[text.find(keyword.keyword_name)] == ' ') { typeFound = false; continue; @@ -447,9 +449,9 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } } - std::string instr = "mov "; + LibCompiler::STLString instr = "mov "; - std::vector<std::string> newVars; + std::vector<LibCompiler::STLString> newVars; if (typeFound && keyword.first.keyword_kind != LibCompiler::KeywordKind::kKeywordKindVariableInc && @@ -458,11 +460,11 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { ++kFunctionEmbedLevel; } - while (varName.find(" ") != std::string::npos) { + while (varName.find(" ") != LibCompiler::STLString::npos) { varName.erase(varName.find(" "), 1); } - while (varName.find("\t") != std::string::npos) { + while (varName.find("\t") != LibCompiler::STLString::npos) { varName.erase(varName.find("\t"), 1); } @@ -525,13 +527,13 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } Detail::print_error("Variable not declared: " + varName, file); - return false; + break; } done: for (auto& keyword : kKeywords) { if (keyword.keyword_kind == LibCompiler::kKeywordKindType && - varName.find(keyword.keyword_name) != std::string::npos) { + varName.find(keyword.keyword_name) != LibCompiler::STLString::npos) { varName.erase(varName.find(keyword.keyword_name), keyword.keyword_name.size()); break; } @@ -563,13 +565,13 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { instr = "sub "; } - std::string varErrCpy = varName; + LibCompiler::STLString varErrCpy = varName; - while (varName.find(" ") != std::string::npos) { + while (varName.find(" ") != LibCompiler::STLString::npos) { varName.erase(varName.find(" "), 1); } - while (varName.find("\t") != std::string::npos) { + while (varName.find("\t") != LibCompiler::STLString::npos) { varName.erase(varName.find("\t"), 1); } @@ -581,11 +583,11 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { valueOfVar.erase(i, 1); } - while (valueOfVar.find(" ") != std::string::npos) { + while (valueOfVar.find(" ") != LibCompiler::STLString::npos) { valueOfVar.erase(valueOfVar.find(" "), 1); } - while (valueOfVar.find("\t") != std::string::npos) { + while (valueOfVar.find("\t") != LibCompiler::STLString::npos) { valueOfVar.erase(valueOfVar.find("\t"), 1); } @@ -637,10 +639,10 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } case LibCompiler::KeywordKind::kKeywordKindReturn: { try { - auto pos = text.find("return") + strlen("return") + 1; - std::string subText = text.substr(pos); - subText = subText.erase(subText.find(";")); - size_t indxReg = 0UL; + auto pos = text.find("return") + strlen("return") + 1; + LibCompiler::STLString subText = text.substr(pos); + subText = subText.erase(subText.find(";")); + size_t indxReg = 0UL; if (subText[0] != '\"' && subText[0] != '\'') { if (!isdigit(subText[0])) { @@ -670,14 +672,14 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } if (syntax_tree.fUserValue.empty()) { - if (subText.find("(") != std::string::npos) { + if (subText.find("(") != LibCompiler::STLString::npos) { subText.erase(subText.find("(")); - auto it = - std::find_if(kOriginMap.begin(), kOriginMap.end(), - [&subText](std::pair<std::string, std::uintptr_t> pair) -> bool { - return pair.first.find(subText) != std::string::npos; - }); + auto it = std::find_if( + kOriginMap.begin(), kOriginMap.end(), + [&subText](std::pair<LibCompiler::STLString, std::uintptr_t> pair) -> bool { + return pair.first.find(subText) != LibCompiler::STLString::npos; + }); if (it == kOriginMap.end()) Detail::print_error("Invalid return value: " + subText, file); @@ -703,8 +705,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } } - kState.fOutputValue = syntax_tree.fUserValue; - return true; + return syntax_tree; } ///////////////////////////////////////////////////////////////////////////////////////// @@ -724,23 +725,19 @@ class AssemblyCPlusPlusInterface final LC_ASSEMBLY_INTERFACE { UInt32 Arch() noexcept override { return LibCompiler::AssemblyFactory::kArchAMD64; } - Int32 CompileToFormat(std::string src, Int32 arch) override { + Int32 CompileToFormat(LibCompiler::STLString src, Int32 arch) override { if (kCompilerFrontend == nullptr) return kExitNO; - std::string dest = src; + LibCompiler::STLString dest = src; dest += ".pp.masm"; std::ofstream out_fp(dest); - std::ifstream src_fp = std::ifstream(src + ".pp"); - std::string line_source; + LibCompiler::STLString line_source; while (std::getline(src_fp, line_source)) { - kCompilerFrontend->Compile(line_source, src); - - out_fp << kState.fOutputValue; - kState.fOutputValue.clear(); + out_fp << kCompilerFrontend->Compile(line_source, src).fUserValue; } return kExitOK; @@ -823,6 +820,8 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { ::signal(SIGSEGV, Detail::drvi_crash_handler); for (auto index = 1UL; index < argc; ++index) { + if (!argv[index]) break; + if (argv[index][0] == '-') { if (skip) { skip = false; @@ -855,7 +854,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { continue; } - std::string err = "Unknown option: "; + LibCompiler::STLString err = "Unknown option: "; err += argv[index]; Detail::print_error(err, "cxxdrv"); @@ -863,35 +862,23 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { continue; } - std::string argv_i = argv[index]; + LibCompiler::STLString argv_i = argv[index]; - std::vector<std::string> exts = kExtListCxx; - BOOL found = false; + std::vector<LibCompiler::STLString> exts = kExtListCxx; - for (std::string ext : exts) { + for (LibCompiler::STLString ext : exts) { if (argv_i.ends_with(ext)) { - found = true; - if (kFactory.Compile(argv_i, kMachine) != kExitOK) { return LIBCOMPILER_INVALID_DATA; } - } - } - if (!found) { - if (kVerbose) { - Detail::print_error(argv_i + " is not a valid C++ source.", "cxxdrv"); + break; } - - return LIBCOMPILER_INVALID_DATA; } } kFactory.Unmount(); - delete kCompilerFrontend; - kCompilerFrontend = nullptr; - return LIBCOMPILER_SUCCESS; } diff --git a/dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc b/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc index edfd47a..edfd47a 100644 --- a/dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc +++ b/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc diff --git a/dev/LibCompiler/src/PP/CPlusPlusCompilerPreProcessor.cc b/dev/LibCompiler/src/Macro/CPlusPlusCompilerPreProcessor.cc index 2c7c551..2c7c551 100644 --- a/dev/LibCompiler/src/PP/CPlusPlusCompilerPreProcessor.cc +++ b/dev/LibCompiler/src/Macro/CPlusPlusCompilerPreProcessor.cc |
