diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-29 10:48:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-29 10:48:12 +0200 |
| commit | 433bb5ef102b2bfa0049468be00d63011da8b973 (patch) | |
| tree | e0893a30710477045a5bb085cb7a27aada425c14 /dev/LibCompiler | |
| parent | 1ddeab9a4426abd781a5066ba79af2ba64de11d9 (diff) | |
| parent | 756ee7f8dc954e27350fe5bdfbaa83b9f69780c8 (diff) | |
Merge pull request #6 from nekernel-org/dev
0.0.2e3
Diffstat (limited to 'dev/LibCompiler')
| -rw-r--r-- | dev/LibCompiler/AE.h | 14 | ||||
| -rw-r--r-- | dev/LibCompiler/Backend/32x0.h | 28 | ||||
| -rw-r--r-- | dev/LibCompiler/Backend/64x0.h | 45 | ||||
| -rw-r--r-- | dev/LibCompiler/Backend/Aarch64.h | 2 | ||||
| -rw-r--r-- | dev/LibCompiler/Backend/X64.h (renamed from dev/LibCompiler/Backend/Amd64.h) | 17 | ||||
| -rw-r--r-- | dev/LibCompiler/BasicString.h (renamed from dev/LibCompiler/StringView.h) | 41 | ||||
| -rw-r--r-- | dev/LibCompiler/CodeGen.h (renamed from dev/LibCompiler/AssemblyInterface.h) | 18 | ||||
| -rw-r--r-- | dev/LibCompiler/Defines.h | 28 | ||||
| -rw-r--r-- | dev/LibCompiler/Detail/ClUtils.h | 57 | ||||
| -rw-r--r-- | dev/LibCompiler/ErrorID.h | 1 | ||||
| -rw-r--r-- | dev/LibCompiler/ErrorOr.h | 5 | ||||
| -rw-r--r-- | dev/LibCompiler/Frontend.h (renamed from dev/LibCompiler/Parser.h) | 94 | ||||
| -rw-r--r-- | dev/LibCompiler/Macros.h | 2 | ||||
| -rw-r--r-- | dev/LibCompiler/PEF.h | 6 | ||||
| -rw-r--r-- | dev/LibCompiler/Ref.h | 5 | ||||
| -rw-r--r-- | dev/LibCompiler/Util/AsmUtils.h (renamed from dev/LibCompiler/Detail/AsmUtils.h) | 8 | ||||
| -rw-r--r-- | dev/LibCompiler/Util/CompilerUtils.h | 109 | ||||
| -rw-r--r-- | dev/LibCompiler/Util/DylibHelpers.h | 13 | ||||
| -rw-r--r-- | dev/LibCompiler/Version.h | 6 | ||||
| -rw-r--r-- | dev/LibCompiler/XCOFF.h | 2 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/.keep | 0 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/Frontend/CCompiler64x0.cc (renamed from dev/LibCompiler/src/CCompiler64x0.cc) | 14 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/Frontend/CCompilerARM64.cc (renamed from dev/LibCompiler/src/CCompilerARM64.cc) | 14 | ||||
| -rw-r--r-- | dev/LibCompiler/deprecated/Frontend/CCompilerPower64.cc (renamed from dev/LibCompiler/src/CCompilerPower64.cc) | 14 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/Assembler32x0.cc (renamed from dev/LibCompiler/src/Assembler32x0.cc) | 24 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/Assembler64x0.cc (renamed from dev/LibCompiler/src/Assembler64x0.cc) | 13 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/AssemblerAMD64.cc (renamed from dev/LibCompiler/src/AssemblerAMD64.cc) | 227 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/AssemblerARM64.cc (renamed from dev/LibCompiler/src/AssemblerARM64.cc) | 15 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Backend/AssemblerPowerPC.cc (renamed from dev/LibCompiler/src/AssemblerPowerPC.cc) | 15 | ||||
| -rw-r--r-- | dev/LibCompiler/src/BasicString.cc (renamed from dev/LibCompiler/src/StringView.cc) | 34 | ||||
| -rw-r--r-- | dev/LibCompiler/src/CodeGen.cc (renamed from dev/LibCompiler/src/AssemblyFactory+AssemblyInterface.cc) | 17 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Frontend.cc | 51 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Frontend/CompilerCPlusPlusAMD64.cc (renamed from dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc) | 433 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc (renamed from dev/LibCompiler/src/DynamicLinkerPEF.cc) | 253 | ||||
| -rw-r--r-- | dev/LibCompiler/src/Macro/CPlusPlusCompilerPreProcessor.cc (renamed from dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc) | 139 |
35 files changed, 927 insertions, 837 deletions
diff --git a/dev/LibCompiler/AE.h b/dev/LibCompiler/AE.h index fcc37a5..8c05c3a 100644 --- a/dev/LibCompiler/AE.h +++ b/dev/LibCompiler/AE.h @@ -28,14 +28,14 @@ namespace LibCompiler { // One thing to keep in mind. // This object format, is reloctable. typedef struct AEHeader final { - CharType fMagic[kAEMagLen]; - CharType fArch; - CharType fSubArch; + Char fMagic[kAEMagLen]; + Char fArch; + Char fSubArch; SizeType fCount; - CharType fSize; + Char fSize; SizeType fStartCode; SizeType fCodeSize; - CharType fPad[kAEPad]; + Char fPad[kAEPad]; } PACKED AEHeader, *AEHeaderPtr; // @brief Advanced Executable Record. @@ -43,12 +43,12 @@ typedef struct AEHeader final { // fKind must be filled with PEF fields. typedef struct AERecordHeader final { - CharType fName[kAESymbolLen]; + Char fName[kAESymbolLen]; SizeType fKind; SizeType fSize; SizeType fFlags; UIntPtr fOffset; - CharType fPad[kAEPad]; + Char fPad[kAEPad]; } PACKED AERecordHeader, *AERecordHeaderPtr; enum { diff --git a/dev/LibCompiler/Backend/32x0.h b/dev/LibCompiler/Backend/32x0.h index 49bb978..2857a38 100644 --- a/dev/LibCompiler/Backend/32x0.h +++ b/dev/LibCompiler/Backend/32x0.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -11,7 +11,7 @@ // @brief 32x0 support. // @file Backend/32x0.h -#define kAsmOpcodeDecl(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ +#define LC_ASM_OPCODE(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ {.fName = __NAME, .fOpcode = __OPCODE, .fFunct3 = __FUNCT3, .fFunct7 = __FUNCT7}, #define kAsmImmediate 0x01 @@ -37,18 +37,18 @@ struct CpuCode32x0 { #define kAsmByteStr ".byte" /* 8-bit */ inline std::vector<CpuCode32x0> kOpcodes32x0 = { - kAsmOpcodeDecl("nop", 0b0100011, 0b000, kAsmNoArgs) // nothing to do. (1C) - kAsmOpcodeDecl("jmp", 0b1110011, 0b001, kAsmJump) // jump to branch (2C) - kAsmOpcodeDecl("mov", 0b0100011, 0b101, kAsmImmediate) // move registers (3C) - kAsmOpcodeDecl("psh", 0b0111011, 0b000, kAsmImmediate) // push to sp (2C) - kAsmOpcodeDecl("pop", 0b0111011, 0b001, kAsmImmediate) // pop from sp. (1C) - kAsmOpcodeDecl("lea", 0b0111011, 0b010, - kAsmImmediate) // setup stack and call, store address to CR (1C). - kAsmOpcodeDecl("ret", 0b0111011, 0b110, - kAsmImmediate) // return from procedure (2C). - kAsmOpcodeDecl("uc", 0b0111111, 0b000, kAsmSyscall) // user call (1C) - kAsmOpcodeDecl("kc", 0b0111111, 0b001, kAsmSyscall) // kernel call (1C) - kAsmOpcodeDecl("int", 0b0111111, 0b010, kAsmSyscall) // raise interrupt (1C) + LC_ASM_OPCODE("nop", 0b0100011, 0b000, kAsmNoArgs) // nothing to do. (1C) + LC_ASM_OPCODE("jmp", 0b1110011, 0b001, kAsmJump) // jump to branch (2C) + LC_ASM_OPCODE("mov", 0b0100011, 0b101, kAsmImmediate) // move registers (3C) + LC_ASM_OPCODE("psh", 0b0111011, 0b000, kAsmImmediate) // push to sp (2C) + LC_ASM_OPCODE("pop", 0b0111011, 0b001, kAsmImmediate) // pop from sp. (1C) + LC_ASM_OPCODE("lea", 0b0111011, 0b010, + kAsmImmediate) // setup stack and call, store address to CR (1C). + LC_ASM_OPCODE("ret", 0b0111011, 0b110, + kAsmImmediate) // return from procedure (2C). + LC_ASM_OPCODE("uc", 0b0111111, 0b000, kAsmSyscall) // user call (1C) + LC_ASM_OPCODE("kc", 0b0111111, 0b001, kAsmSyscall) // kernel call (1C) + LC_ASM_OPCODE("int", 0b0111111, 0b010, kAsmSyscall) // raise interrupt (1C) }; // \brief 64x0 register prefix diff --git a/dev/LibCompiler/Backend/64x0.h b/dev/LibCompiler/Backend/64x0.h index f929995..c68b6cf 100644 --- a/dev/LibCompiler/Backend/64x0.h +++ b/dev/LibCompiler/Backend/64x0.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -12,7 +12,7 @@ // @brief 64x0 support. // @file Backend/64x0.h -#define kAsmOpcodeDecl(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ +#define LC_ASM_OPCODE(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ {.fName = __NAME, .fOpcode = __OPCODE, .fFunct3 = __FUNCT3, .fFunct7 = __FUNCT7}, #define kAsmImmediate 0x01 @@ -32,29 +32,28 @@ struct CpuOpcode64x0 { }; inline std::vector<CpuOpcode64x0> kOpcodes64x0 = { - kAsmOpcodeDecl("nop", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. - kAsmOpcodeDecl("np", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. - kAsmOpcodeDecl("jlr", 0b1110011, 0b0000111, - kAsmJump) // jump to linked return register - kAsmOpcodeDecl("jrl", 0b1110011, 0b0001111, - kAsmJump) // jump from return register. - kAsmOpcodeDecl("mv", 0b0100011, 0b101, kAsmRegToReg) kAsmOpcodeDecl( - "bg", 0b1100111, 0b111, kAsmRegToReg) kAsmOpcodeDecl("bl", 0b1100111, 0b011, kAsmRegToReg) - kAsmOpcodeDecl("beq", 0b1100111, 0b000, kAsmRegToReg) - kAsmOpcodeDecl("bne", 0b1100111, 0b001, kAsmRegToReg) - kAsmOpcodeDecl("bge", 0b1100111, 0b101, kAsmRegToReg) - kAsmOpcodeDecl("ble", 0b1100111, 0b100, kAsmRegToReg) - kAsmOpcodeDecl("stw", 0b0001111, 0b100, kAsmImmediate) - kAsmOpcodeDecl("ldw", 0b0001111, 0b100, kAsmImmediate) - kAsmOpcodeDecl("lda", 0b0001111, 0b101, kAsmImmediate) - kAsmOpcodeDecl("sta", 0b0001111, 0b001, kAsmImmediate) + LC_ASM_OPCODE("nop", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. + LC_ASM_OPCODE("np", 0b0000000, 0b0000000, kAsmNoArgs) // no-operation. + LC_ASM_OPCODE("jlr", 0b1110011, 0b0000111, + kAsmJump) // jump to linked return register + LC_ASM_OPCODE("jrl", 0b1110011, 0b0001111, + kAsmJump) // jump from return register. + LC_ASM_OPCODE("mv", 0b0100011, 0b101, kAsmRegToReg) LC_ASM_OPCODE( + "bg", 0b1100111, 0b111, kAsmRegToReg) LC_ASM_OPCODE("bl", 0b1100111, 0b011, kAsmRegToReg) + LC_ASM_OPCODE("beq", 0b1100111, 0b000, kAsmRegToReg) + LC_ASM_OPCODE("bne", 0b1100111, 0b001, kAsmRegToReg) + LC_ASM_OPCODE("bge", 0b1100111, 0b101, kAsmRegToReg) + LC_ASM_OPCODE("ble", 0b1100111, 0b100, kAsmRegToReg) + LC_ASM_OPCODE("stw", 0b0001111, 0b100, kAsmImmediate) + LC_ASM_OPCODE("ldw", 0b0001111, 0b100, kAsmImmediate) + LC_ASM_OPCODE("lda", 0b0001111, 0b101, kAsmImmediate) + LC_ASM_OPCODE("sta", 0b0001111, 0b001, kAsmImmediate) // add/sub without carry flag - kAsmOpcodeDecl("add", 0b0101011, 0b100, kAsmImmediate) - kAsmOpcodeDecl("sub", 0b0101011, 0b101, kAsmImmediate) + LC_ASM_OPCODE("add", 0b0101011, 0b100, kAsmImmediate) + LC_ASM_OPCODE("sub", 0b0101011, 0b101, kAsmImmediate) // add/sub with carry flag - kAsmOpcodeDecl("addc", 0b0101011, 0b110, kAsmImmediate) - kAsmOpcodeDecl("subc", 0b0101011, 0b111, kAsmImmediate) - kAsmOpcodeDecl("sc", 0b1110011, 0b00, kAsmSyscall)}; + LC_ASM_OPCODE("addc", 0b0101011, 0b110, kAsmImmediate) LC_ASM_OPCODE( + "subc", 0b0101011, 0b111, kAsmImmediate) LC_ASM_OPCODE("sc", 0b1110011, 0b00, kAsmSyscall)}; // \brief 64x0 register prefix // example: r32, r0 diff --git a/dev/LibCompiler/Backend/Aarch64.h b/dev/LibCompiler/Backend/Aarch64.h index dcafa0a..2676306 100644 --- a/dev/LibCompiler/Backend/Aarch64.h +++ b/dev/LibCompiler/Backend/Aarch64.h @@ -1,6 +1,6 @@ /* ------------------------------------------- -Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved +Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ diff --git a/dev/LibCompiler/Backend/Amd64.h b/dev/LibCompiler/Backend/X64.h index a3fb8a2..f489515 100644 --- a/dev/LibCompiler/Backend/Amd64.h +++ b/dev/LibCompiler/Backend/X64.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -9,9 +9,9 @@ #include <LibCompiler/Defines.h> // @brief AMD64 support. -// @file Backend/Amd64.h +// @file Backend/X64.h -#define kAsmOpcodeDecl(__NAME, __OPCODE) {.fName = __NAME, .fOpcode = __OPCODE}, +#define LC_ASM_OPCODE(__NAME, __OPCODE) {.fName = __NAME, .fOpcode = __OPCODE}, typedef char i64_character_t; typedef uint8_t i64_byte_t; @@ -39,10 +39,11 @@ struct CpuOpcodeAMD64 { #define kJumpLimitStandardLimit 0xEB inline std::vector<CpuOpcodeAMD64> kOpcodesAMD64 = { - kAsmOpcodeDecl("int", 0xCD) kAsmOpcodeDecl("into", 0xCE) kAsmOpcodeDecl("intd", 0xF1) - kAsmOpcodeDecl("int3", 0xC3) kAsmOpcodeDecl("iret", 0xCF) kAsmOpcodeDecl("retf", 0xCB) - kAsmOpcodeDecl("retn", 0xC3) kAsmOpcodeDecl("ret", 0xC3) kAsmOpcodeDecl("sti", 0xfb) - kAsmOpcodeDecl("cli", 0xfa) kAsmOpcodeDecl("hlt", 0xf4) kAsmOpcodeDecl("nop", 0x90) - kAsmOpcodeDecl("mov", 0x48) kAsmOpcodeDecl("call", 0xFF)}; + LC_ASM_OPCODE("int", 0xCD) LC_ASM_OPCODE("into", 0xCE) LC_ASM_OPCODE("intd", 0xF1) + LC_ASM_OPCODE("int3", 0xC3) LC_ASM_OPCODE("iret", 0xCF) LC_ASM_OPCODE("retf", 0xCB) + LC_ASM_OPCODE("retn", 0xC3) LC_ASM_OPCODE("ret", 0xC3) LC_ASM_OPCODE("sti", 0xfb) + LC_ASM_OPCODE("cli", 0xfa) LC_ASM_OPCODE("hlt", 0xf4) LC_ASM_OPCODE("nop", 0x90) + LC_ASM_OPCODE("mov", 0x48) LC_ASM_OPCODE("call", 0xFF) + LC_ASM_OPCODE("syscall", 0x0F) LC_ASM_OPCODE("xor", 0x48)}; #define kAsmRegisterLimit 16 diff --git a/dev/LibCompiler/StringView.h b/dev/LibCompiler/BasicString.h index 18003a3..5af9da9 100644 --- a/dev/LibCompiler/StringView.h +++ b/dev/LibCompiler/BasicString.h @@ -13,21 +13,24 @@ #include <LibCompiler/ErrorOr.h> namespace LibCompiler { +class StringBuilder; +class BasicString; + /** - * @brief StringView 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 StringView final { +class BasicString final { public: - explicit StringView() = delete; + explicit BasicString() = delete; - explicit StringView(SizeType Sz) noexcept : m_Sz(Sz) { - m_Data = new CharType[Sz]; + explicit BasicString(SizeType Sz) noexcept : m_Sz(Sz) { + m_Data = new Char[Sz]; assert(m_Data); } - ~StringView() noexcept { + ~BasicString() noexcept { if (m_Data) { memset(m_Data, 0, m_Sz); delete[] m_Data; @@ -36,27 +39,27 @@ class StringView final { } } - LIBCOMPILER_COPY_DEFAULT(StringView); + LIBCOMPILER_COPY_DEFAULT(BasicString); - CharType* Data(); - const CharType* CData() const; + Char* Data(); + const Char* CData() const; SizeType Length() const; - bool operator==(const CharType* rhs) const; - bool operator!=(const CharType* rhs) const; + bool operator==(const Char* rhs) const; + bool operator!=(const Char* rhs) const; - bool operator==(const StringView& rhs) const; - bool operator!=(const StringView& rhs) const; + bool operator==(const BasicString& rhs) const; + bool operator!=(const BasicString& rhs) const; - StringView& operator+=(const CharType* rhs); - StringView& operator+=(const StringView& rhs); + BasicString& operator+=(const Char* rhs); + BasicString& operator+=(const BasicString& rhs); operator bool() { return m_Data && m_Data[0] != 0; } bool operator!() { return !m_Data || m_Data[0] == 0; } private: - CharType* m_Data{nullptr}; + Char* m_Data{nullptr}; SizeType m_Sz{0}; SizeType m_Cur{0}; @@ -65,13 +68,15 @@ class StringView final { /** * @brief StringBuilder class - * @note These results shall call delete[] after they're used. + * @note These results shall call be delete[] after they're used. */ struct StringBuilder final { - static StringView Construct(const CharType* data); + static BasicString Construct(const Char* 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<BasicString>; } // namespace LibCompiler diff --git a/dev/LibCompiler/AssemblyInterface.h b/dev/LibCompiler/CodeGen.h index b612962..b932cdf 100644 --- a/dev/LibCompiler/AssemblyInterface.h +++ b/dev/LibCompiler/CodeGen.h @@ -1,16 +1,17 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ #pragma once +#include <LibCompiler/BasicString.h> #include <LibCompiler/Defines.h> #include <LibCompiler/Macros.h> -#include <LibCompiler/StringView.h> -#define ASSEMBLY_INTERFACE : public LibCompiler::AssemblyInterface +#define LC_ASSEMBLY_INTERFACE : public ::LibCompiler::AssemblyInterface +#define LC_ENCODER : public ::LibCompiler::EncoderInterface namespace LibCompiler { class AssemblyFactory; @@ -34,6 +35,7 @@ class AssemblyFactory final { kArchPowerPC, kArchAARCH64, kArchUnknown, + kArchCount = kArchUnknown - kArchAMD64, }; Int32 Compile(std::string sourceFile, const Int32& arch) noexcept; @@ -54,7 +56,7 @@ class AssemblyInterface { LIBCOMPILER_COPY_DEFAULT(AssemblyInterface); - [[maybe_unused]] virtual Int32 Arch() noexcept { return AssemblyFactory::kArchAMD64; } + virtual UInt32 Arch() noexcept { return AssemblyFactory::kArchAMD64; } /// @brief compile to object file. /// @note Example C++ -> MASM -> AE object. @@ -72,7 +74,7 @@ union NumberCast64 final { ~NumberCast64() { raw = 0; } - CharType number[8]; + Char number[8]; UInt64 raw; }; @@ -82,7 +84,7 @@ union NumberCast32 final { ~NumberCast32() { raw = 0; } - CharType number[4]; + Char number[4]; UInt32 raw; }; @@ -92,7 +94,7 @@ union NumberCast16 final { ~NumberCast16() { raw = 0; } - CharType number[2]; + Char number[2]; UInt16 raw; }; @@ -102,7 +104,7 @@ union NumberCast8 final { ~NumberCast8() { raw = 0; } - CharType number; + Char number; UInt8 raw; }; diff --git a/dev/LibCompiler/Defines.h b/dev/LibCompiler/Defines.h index eacf9c0..1e1d4d9 100644 --- a/dev/LibCompiler/Defines.h +++ b/dev/LibCompiler/Defines.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -48,7 +48,7 @@ #define Int8 int8_t #define UInt8 uint8_t -#define CharType char +#define Char char #define Boolean bool #include <cassert> @@ -99,22 +99,16 @@ #define LC_IMPORT_C extern "C" #define LC_IMPORT extern - -#include <ctime> -#include <fstream> -#include <string> -#include <vector> - namespace LibCompiler { inline constexpr int kBaseYear = 1900; -typedef std::string String; +typedef std::string STLString; -inline String current_date() noexcept { +inline STLString current_date() noexcept { auto time_data = time(nullptr); auto time_struct = gmtime(&time_data); - String fmt = std::to_string(kBaseYear + time_struct->tm_year); + STLString fmt = std::to_string(kBaseYear + time_struct->tm_year); fmt += "-"; fmt += std::to_string(time_struct->tm_mon + 1); @@ -124,7 +118,7 @@ inline String current_date() noexcept { return fmt; } -inline bool to_str(CharType* str, Int32 limit, Int32 base) noexcept { +inline bool to_str(Char* str, Int32 limit, Int32 base) noexcept { if (limit == 0) return false; Int32 copy_limit = limit; @@ -144,7 +138,15 @@ inline bool to_str(CharType* str, Int32 limit, Int32 base) noexcept { return true; } -using String = std::basic_string<CharType>; +inline bool install_signal(Int32 signal, void (*handler)(int)) noexcept { + if (handler == nullptr) return false; + + if (::signal(signal, handler) == SIG_ERR) { + return false; + } + + return true; +} } // namespace LibCompiler #define PACKED __attribute__((packed)) diff --git a/dev/LibCompiler/Detail/ClUtils.h b/dev/LibCompiler/Detail/ClUtils.h deleted file mode 100644 index f47101a..0000000 --- a/dev/LibCompiler/Detail/ClUtils.h +++ /dev/null @@ -1,57 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved - -------------------------------------------- */ - -#pragma once - -#include <LibCompiler/AssemblyInterface.h> -#include <LibCompiler/ErrorID.h> -#include <LibCompiler/Parser.h> -#include <Vendor/Dialogs.h> - -#define kZero64Section ".zero64" -#define kCode64Section ".code64" -#define kData64Section ".data64" - -#define kZero128Section ".zero128" -#define kCode128Section ".code128" -#define kData128Section ".data128" - -#define kBlank "\e[0;30m" -#define kRed "\e[0;31m" -#define kWhite "\e[0;97m" -#define kYellow "\e[0;33m" - -#define kStdOut (std::cout << kWhite) -#define kStdErr (std::cout << kRed) - -inline static UInt32 kErrorLimit = 10; -inline static UInt32 kAcceptableErrors = 0; -inline static bool kVerbose = false; -inline static bool kOutputAsBinary = false; - -namespace Detail { -inline void print_error(std::string reason, std::string file) noexcept { - if (reason[0] == '\n') reason.erase(0, 1); - - kStdErr << kRed << "drv: " << kWhite << reason << kBlank << std::endl; - - if (kAcceptableErrors > kErrorLimit) std::exit(LIBCOMPILER_EXEC_ERROR); - - ++kAcceptableErrors; -} - -inline void print_warning(std::string reason, std::string file) noexcept { - if (reason[0] == '\n') reason.erase(0, 1); - - kStdOut << kYellow << "drv: " << kWhite << reason << kBlank << std::endl; -} - -/// @internal -inline void segfault_handler(std::int32_t _) { - 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 45bb393..c82fb16 100644 --- a/dev/LibCompiler/ErrorID.h +++ b/dev/LibCompiler/ErrorID.h @@ -10,7 +10,6 @@ #pragma once #include <LibCompiler/Defines.h> -#include <LibCompiler/ErrorOr.h> #define LIBCOMPILER_SUCCESS 0 #define LIBCOMPILER_EXEC_ERROR -30 diff --git a/dev/LibCompiler/ErrorOr.h b/dev/LibCompiler/ErrorOr.h index ca93dd2..77015b1 100644 --- a/dev/LibCompiler/ErrorOr.h +++ b/dev/LibCompiler/ErrorOr.h @@ -10,6 +10,7 @@ #pragma once #include <LibCompiler/Defines.h> +#include <LibCompiler/ErrorID.h> #include <LibCompiler/Ref.h> namespace LibCompiler { @@ -33,6 +34,10 @@ class ErrorOr final { Ref<T> Leak() { return mRef; } + Int32 Error() { return mId; } + + BOOL HasError() { return mId != LIBCOMPILER_SUCCESS; } + operator bool() { return mRef; } private: diff --git a/dev/LibCompiler/Parser.h b/dev/LibCompiler/Frontend.h index 6baff7e..f39c9d4 100644 --- a/dev/LibCompiler/Parser.h +++ b/dev/LibCompiler/Frontend.h @@ -1,38 +1,17 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ #pragma once -#include <LibCompiler/AssemblyInterface.h> +#include <LibCompiler/CodeGen.h> -namespace LibCompiler { -inline auto kInvalidFrontend = "NA"; - -/// @brief Compiler backend, implements a frontend, such as C, C++... -/// See Toolchain, for some examples. -class ICompilerFrontend { - public: - explicit ICompilerFrontend() = default; - virtual ~ICompilerFrontend() = default; - - LIBCOMPILER_COPY_DEFAULT(ICompilerFrontend); - - // 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; } +#define LC_COMPILER_FRONTEND : public ::LibCompiler::CompilerFrontendInterface - virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } -}; +namespace LibCompiler { +inline static auto kInvalidFrontend = "?"; struct SyntaxLeafList; struct SyntaxLeafList; @@ -79,22 +58,22 @@ enum KeywordKind { /// \brief Compiler keyword information struct. struct CompilerKeyword { - std::string keyword_name; - KeywordKind keyword_kind = kKeywordKindInvalid; + STLString keyword_name{""}; + KeywordKind keyword_kind{kKeywordKindInvalid}; }; + struct SyntaxLeafList final { struct SyntaxLeaf final { Int32 fUserType; + #ifdef LC_USE_STRUCTS CompilerKeyword fUserData; #else std::string fUserData; #endif - SyntaxLeaf() = default; - - std::string fUserValue; - struct SyntaxLeaf* fNext; + std::string fUserValue{""}; + struct SyntaxLeaf* fNext{nullptr}; }; std::vector<SyntaxLeaf> fLeafList; @@ -109,41 +88,34 @@ struct SyntaxLeafList final { /// \param haystack base string /// \param needle the string we search for. /// \return if we found it or not. -inline bool find_word(std::string haystack, std::string needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == std::string::npos) return false; - - // declare lambda - auto not_part_of_word = [&](int index) { - if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; - - if (index <= 0 || index >= haystack.size()) return true; - - return false; - }; - - return not_part_of_word(index - 1) && not_part_of_word(index + needle.size()); -} +BOOL find_word(std::string haystack, std::string needle) noexcept; /// find a word within strict conditions and returns a range of it. /// \param haystack /// \param needle /// \return position of needle. -inline std::size_t find_word_range(std::string haystack, std::string needle) noexcept { - auto index = haystack.find(needle); +std::size_t find_word_range(std::string haystack, std::string needle) noexcept; - // check for needle validity. - if (index == std::string::npos) return false; +/// @brief Compiler backend, implements a frontend, such as C, C++... +/// See Toolchain, for some examples. +class CompilerFrontendInterface { + public: + explicit CompilerFrontendInterface() = default; + virtual ~CompilerFrontendInterface() = default; - if (!isalnum((haystack[index + needle.size() + 1])) && - !isdigit(haystack[index + needle.size() + 1]) && - !isalnum((haystack[index - needle.size() - 1])) && - !isdigit(haystack[index - needle.size() - 1])) { - return index; - } + LIBCOMPILER_COPY_DEFAULT(CompilerFrontendInterface); - return false; -} + // 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/Macros.h b/dev/LibCompiler/Macros.h index cce89d6..5027ffa 100644 --- a/dev/LibCompiler/Macros.h +++ b/dev/LibCompiler/Macros.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ diff --git a/dev/LibCompiler/PEF.h b/dev/LibCompiler/PEF.h index fe6cfe1..3f5a2b1 100644 --- a/dev/LibCompiler/PEF.h +++ b/dev/LibCompiler/PEF.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -71,7 +71,7 @@ enum { /* PEF container */ typedef struct PEFContainer final { - CharType Magic[kPefMagicLen]; + Char Magic[kPefMagicLen]; UInt32 Linker; /* Linker used to link executable */ UInt32 Version; UInt32 Kind; @@ -90,7 +90,7 @@ typedef struct PEFContainer final { /* PEF executable section and commands. */ typedef struct PEFCommandHeader final { - CharType Name[kPefNameLen]; /* container name */ + Char Name[kPefNameLen]; /* container name */ UInt32 Cpu; /* container cpu */ UInt32 SubCpu; /* container sub-cpu */ UInt32 Flags; /* container flags */ diff --git a/dev/LibCompiler/Ref.h b/dev/LibCompiler/Ref.h index c5188b5..60eafc3 100644 --- a/dev/LibCompiler/Ref.h +++ b/dev/LibCompiler/Ref.h @@ -22,14 +22,15 @@ class Ref final { ~Ref() { if (m_Strong) { - (*m_Class).~T(); + if (m_Class) delete m_Class; + m_Class = nullptr; } } LIBCOMPILER_COPY_DEFAULT(Ref); public: - explicit Ref(T cls, const Bool& strong = false) : m_Class(&cls), m_Strong(strong) {} + explicit Ref(T* cls, const Bool& strong = false) : m_Class(cls), m_Strong(strong) {} Ref& operator=(T ref) { *m_Class = ref; diff --git a/dev/LibCompiler/Detail/AsmUtils.h b/dev/LibCompiler/Util/AsmUtils.h index 559df5f..f176eda 100644 --- a/dev/LibCompiler/Detail/AsmUtils.h +++ b/dev/LibCompiler/Util/AsmUtils.h @@ -1,15 +1,15 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ #pragma once -#include <LibCompiler/AssemblyInterface.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/CodeGen.h> +#include <LibCompiler/Frontend.h> -#include <LibCompiler/Detail/ClUtils.h> +#include <LibCompiler/Util/CompilerUtils.h> using namespace LibCompiler; diff --git a/dev/LibCompiler/Util/CompilerUtils.h b/dev/LibCompiler/Util/CompilerUtils.h new file mode 100644 index 0000000..19e247d --- /dev/null +++ b/dev/LibCompiler/Util/CompilerUtils.h @@ -0,0 +1,109 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include <LibCompiler/CodeGen.h> +#include <LibCompiler/ErrorID.h> +#include <LibCompiler/Frontend.h> +#include <LibCompiler/Version.h> +#include <Vendor/Dialogs.h> + +#define kZero64Section ".zero64" +#define kCode64Section ".code64" +#define kData64Section ".data64" + +#define kZero128Section ".zero128" +#define kCode128Section ".code128" +#define kData128Section ".data128" + +#define kBlank "\e[0;30m" +#define kRed "\e[0;31m" +#define kWhite "\e[0;97m" +#define kYellow "\e[0;33m" + +#define kStdOut (std::cout << kRed << "drv: " << kWhite) +#define kStdErr (std::cout << kYellow << "drv: " << kWhite) + +inline static UInt32 kErrorLimit = 10; +inline static UInt32 kAcceptableErrors = 0; +inline static bool kVerbose = false; +inline static bool kOutputAsBinary = false; + +namespace Detail { +inline void print_error(std::string reason, std::string file) noexcept { + if (reason[0] == '\n') reason.erase(0, 1); + + kStdErr << reason << kBlank << std::endl; + + if (kAcceptableErrors > kErrorLimit) std::exit(LIBCOMPILER_EXEC_ERROR); + + ++kAcceptableErrors; +} + +inline void print_warning(std::string reason, std::string file) noexcept { + if (reason[0] == '\n') reason.erase(0, 1); + + kStdOut << kYellow << reason << kBlank << std::endl; +} + +/// @internal +/// @brief Handler for SIGSEGV signal. +inline void drvi_crash_handler(std::int32_t id) { + LibCompiler::STLString verbose_header = "LIBCOMPILER CRASH REPORT - "; + verbose_header += kDistVersion; + verbose_header += " - "; + verbose_header += LibCompiler::current_date(); + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + + std::cout << verbose_header << std::endl; + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + + kStdOut << "DATE: " << LibCompiler::current_date() << std::endl; + kStdOut << "VERSION: " << kDistVersion << std::endl; + kStdOut << "ERRNO: " << errno << std::endl; + kStdOut << "ERRNO(STRING): " << strerror(errno) << std::endl; + + switch (id) { + case SIGSEGV: { + kStdOut << "SIGNAL: Segmentation Fault." << kBlank << std::endl; + break; + } + case SIGABRT: { + kStdOut << "SIGNAL: Aborted." << kBlank << std::endl; + break; + } + } + + std::cout << kWhite; + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + + std::cout << verbose_header << std::endl; + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + + std::exit(LIBCOMPILER_EXEC_ERROR); +} +} // namespace Detail diff --git a/dev/LibCompiler/Util/DylibHelpers.h b/dev/LibCompiler/Util/DylibHelpers.h new file mode 100644 index 0000000..be8119f --- /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, Char const* argv[]); +typedef VoidPtr DylibHandle; diff --git a/dev/LibCompiler/Version.h b/dev/LibCompiler/Version.h index 601bb28..93a7c00 100644 --- a/dev/LibCompiler/Version.h +++ b/dev/LibCompiler/Version.h @@ -1,13 +1,13 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ #pragma once -#define kDistVersion "v0.0.1" -#define kDistVersionBCD 0x0001 +#define kDistVersion "v0.0.2-libcompiler" +#define kDistVersionBCD 0x0002 #define ToString(X) Stringify(X) #define Stringify(X) #X diff --git a/dev/LibCompiler/XCOFF.h b/dev/LibCompiler/XCOFF.h index 047a20f..7aa13d6 100644 --- a/dev/LibCompiler/XCOFF.h +++ b/dev/LibCompiler/XCOFF.h @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved File: XCOFF.hpp Purpose: XCOFF for NeKernel. 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/CCompiler64x0.cc b/dev/LibCompiler/deprecated/Frontend/CCompiler64x0.cc index 79d32ce..7b908f7 100644 --- a/dev/LibCompiler/src/CCompiler64x0.cc +++ b/dev/LibCompiler/deprecated/Frontend/CCompiler64x0.cc @@ -11,9 +11,9 @@ /// TODO: none #include <LibCompiler/Backend/64x0.h> -#include <LibCompiler/Detail/ClUtils.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/UUID.h> +#include <LibCompiler/Util/CompilerUtils.h> #include <cstdio> #include <fstream> #include <iostream> @@ -134,7 +134,7 @@ static bool kIfFound = false; static size_t kBracesCount = 0UL; /* @brief C compiler backend for C */ -class CompilerFrontend64x0 final : public LibCompiler::ICompilerFrontend { +class CompilerFrontend64x0 final : public LibCompiler::CompilerFrontendInterface { public: explicit CompilerFrontend64x0() = default; ~CompilerFrontend64x0() override = default; @@ -1044,16 +1044,14 @@ skip_braces_check: ///////////////////////////////////////////////////////////////////////////////////////// -class AssemblyCCInterface final ASSEMBLY_INTERFACE { +class AssemblyCCInterface final LC_ASSEMBLY_INTERFACE { public: explicit AssemblyCCInterface() = default; ~AssemblyCCInterface() override = default; LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); - [[maybe_unused]] Int32 Arch() noexcept override { - return LibCompiler::AssemblyFactory::kArch64x0; - } + UInt32 Arch() noexcept override { return LibCompiler::AssemblyFactory::kArch64x0; } Int32 CompileToFormat(std::string src, Int32 arch) override { if (kCompilerFrontend == nullptr) return 1; @@ -1200,7 +1198,7 @@ static void cc_print_help() { #define kExt ".c" LIBCOMPILER_MODULE(NeOSCompilerCLang64x0) { - ::signal(SIGSEGV, Detail::segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/CCompilerARM64.cc b/dev/LibCompiler/deprecated/Frontend/CCompilerARM64.cc index a5ddf43..9e62747 100644 --- a/dev/LibCompiler/src/CCompilerARM64.cc +++ b/dev/LibCompiler/deprecated/Frontend/CCompilerARM64.cc @@ -11,9 +11,9 @@ /// TODO: none #include <LibCompiler/Backend/Aarch64.h> -#include <LibCompiler/Detail/ClUtils.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/UUID.h> +#include <LibCompiler/Util/CompilerUtils.h> #include <cstdio> #include <fstream> #include <iostream> @@ -134,7 +134,7 @@ static bool kIfFound = false; static size_t kBracesCount = 0UL; /* @brief C compiler backend for C */ -class CompilerFrontendARM64 final : public LibCompiler::ICompilerFrontend { +class CompilerFrontendARM64 final : public LibCompiler::CompilerFrontendInterface { public: explicit CompilerFrontendARM64() = default; ~CompilerFrontendARM64() override = default; @@ -1042,16 +1042,14 @@ skip_braces_check: ///////////////////////////////////////////////////////////////////////////////////////// -class AssemblyCCInterface final ASSEMBLY_INTERFACE { +class AssemblyCCInterface final LC_ASSEMBLY_INTERFACE { public: explicit AssemblyCCInterface() = default; ~AssemblyCCInterface() override = default; LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); - [[maybe_unused]] Int32 Arch() noexcept override { - return LibCompiler::AssemblyFactory::kArchAARCH64; - } + UInt32 Arch() noexcept override { return LibCompiler::AssemblyFactory::kArchAARCH64; } Int32 CompileToFormat(std::string src, Int32 arch) override { if (kCompilerFrontend == nullptr) return 1; @@ -1198,7 +1196,7 @@ static void cc_print_help() { #define kCExtension ".c" LIBCOMPILER_MODULE(NeOSCompilerCLangARM64) { - ::signal(SIGSEGV, Detail::segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/CCompilerPower64.cc b/dev/LibCompiler/deprecated/Frontend/CCompilerPower64.cc index f2eba43..233ad98 100644 --- a/dev/LibCompiler/src/CCompilerPower64.cc +++ b/dev/LibCompiler/deprecated/Frontend/CCompilerPower64.cc @@ -8,9 +8,9 @@ */ #include <LibCompiler/Backend/PowerPC.h> -#include <LibCompiler/Detail/ClUtils.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/UUID.h> +#include <LibCompiler/Util/CompilerUtils.h> #include <cstdio> #include <fstream> #include <iostream> @@ -125,7 +125,7 @@ static bool kIfFound = false; static size_t kBracesCount = 0UL; /* @brief C compiler backend for C */ -class CompilerFrontendPower64 final : public LibCompiler::ICompilerFrontend { +class CompilerFrontendPower64 final : public LibCompiler::CompilerFrontendInterface { public: explicit CompilerFrontendPower64() = default; ~CompilerFrontendPower64() override = default; @@ -1062,16 +1062,14 @@ skip_braces_check: ///////////////////////////////////////////////////////////////////////////////////////// -class AssemblyMountpointCLang final ASSEMBLY_INTERFACE { +class AssemblyMountpointCLang final LC_ASSEMBLY_INTERFACE { public: explicit AssemblyMountpointCLang() = default; ~AssemblyMountpointCLang() override = default; LIBCOMPILER_COPY_DEFAULT(AssemblyMountpointCLang); - [[maybe_unused]] Int32 Arch() noexcept override { - return LibCompiler::AssemblyFactory::kArchPowerPC; - } + UInt32 Arch() noexcept override { return LibCompiler::AssemblyFactory::kArchPowerPC; } Int32 CompileToFormat(std::string src, Int32 arch) override { if (kCompilerFrontend == nullptr) return 1; @@ -1217,7 +1215,7 @@ static void cc_print_help() { #define kExt ".c" LIBCOMPILER_MODULE(NeOSCompilerCLangPowerPC) { - ::signal(SIGSEGV, Detail::segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/Assembler32x0.cc b/dev/LibCompiler/src/Backend/Assembler32x0.cc index ac24946..b7ff321 100644 --- a/dev/LibCompiler/src/Assembler32x0.cc +++ b/dev/LibCompiler/src/Backend/Assembler32x0.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -17,26 +17,15 @@ ///////////////////////////////////////////////////////////////////////////////////////// +#ifndef __ASM_NEED_32x0__ #define __ASM_NEED_32x0__ 1 +#endif #include <LibCompiler/AE.h> #include <LibCompiler/Backend/32x0.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/PEF.h> -#include <LibCompiler/Parser.h> - -///////////////////// - -// ANSI ESCAPE CODES - -///////////////////// - -#define kBlank "\e[0;30m" -#define kRed "\e[0;31m" -#define kWhite "\e[0;97m" -#define kYellow "\e[0;33m" - -#define kStdOut (std::cout << kWhite) -#define kStdErr (std::cout << kRed) +#include <LibCompiler/Util/CompilerUtils.h> ///////////////////////////////////////////////////////////////////////////////////////// @@ -45,5 +34,6 @@ ///////////////////////////////////////////////////////////////////////////////////////// LIBCOMPILER_MODULE(NEAssemblerMain32000) { - return 0; + LibCompiler::install_signal(SIGSEGV, Detail::drvi_crash_handler); + return EXIT_SUCCESS; } diff --git a/dev/LibCompiler/src/Assembler64x0.cc b/dev/LibCompiler/src/Backend/Assembler64x0.cc index 8483e9e..765bea2 100644 --- a/dev/LibCompiler/src/Assembler64x0.cc +++ b/dev/LibCompiler/src/Backend/Assembler64x0.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -17,13 +17,15 @@ ///////////////////////////////////////////////////////////////////////////////////////// +#ifndef __ASM_NEED_64x0__ #define __ASM_NEED_64x0__ 1 +#endif #include <LibCompiler/AE.h> #include <LibCompiler/Backend/64x0.h> -#include <LibCompiler/Detail/ClUtils.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/PEF.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Util/CompilerUtils.h> #include <algorithm> #include <filesystem> #include <fstream> @@ -38,8 +40,7 @@ static char kOutputArch = LibCompiler::kPefArch64000; -/// @note The 64x0 is VLSIW, so we need to jump to 4 bytes. -constexpr auto k64x0IPAlignment = 0x4U; +constexpr auto k64x0IPAlignment = 0x1U; static std::size_t kCounter = 1UL; @@ -67,7 +68,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// LIBCOMPILER_MODULE(AssemblerMain64x0) { - ::signal(SIGSEGV, Detail::segfault_handler); + LibCompiler::install_signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { if (argv[i][0] == '-') { diff --git a/dev/LibCompiler/src/AssemblerAMD64.cc b/dev/LibCompiler/src/Backend/AssemblerAMD64.cc index 5e9a7cd..6551743 100644 --- a/dev/LibCompiler/src/AssemblerAMD64.cc +++ b/dev/LibCompiler/src/Backend/AssemblerAMD64.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -21,15 +21,17 @@ ///////////////////////////////////////////////////////////////////////////////////////// +#ifndef __ASM_NEED_AMD64__ #define __ASM_NEED_AMD64__ 1 +#endif #define kAssemblerPragmaSymStr "#" #define kAssemblerPragmaSym '#' #include <LibCompiler/AE.h> -#include <LibCompiler/Backend/Amd64.h> +#include <LibCompiler/Backend/X64.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/PEF.h> -#include <LibCompiler/Parser.h> #include <algorithm> #include <cstdlib> #include <filesystem> @@ -48,9 +50,6 @@ #define kWhite "\e[0;97m" #define kYellow "\e[0;33m" -#define kStdOut (std::cout << kWhite) -#define kStdErr (std::cout << kRed) - static char kOutputArch = LibCompiler::kPefArchAMD64; constexpr auto kIPAlignement = 0x1U; @@ -77,7 +76,7 @@ static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; // \brief forward decl. static bool asm_read_attributes(std::string line); -#include <LibCompiler/Detail/AsmUtils.h> +#include <LibCompiler/Util/AsmUtils.h> ///////////////////////////////////////////////////////////////////////////////////////// @@ -88,7 +87,7 @@ static bool asm_read_attributes(std::string line); LIBCOMPILER_MODULE(AssemblerMainAMD64) { //////////////// CPU OPCODES BEGIN //////////////// - ::signal(SIGSEGV, Detail::segfault_handler); + LibCompiler::install_signal(SIGSEGV, Detail::drvi_crash_handler); std::string opcodes_jump[kJumpLimit] = {"ja", "jae", "jb", "jbe", "jc", "je", "jg", "jge", "jl", "jle", "jna", "jnae", "jnb", "jnbe", "jnc", "jne", @@ -965,13 +964,9 @@ bool LibCompiler::EncoderAMD64::WriteLine(std::string line, std::string file) { {.fName = "dx", .fModRM = 0x2}, {.fName = "bx", .fModRM = 3}, {.fName = "sp", .fModRM = 0x4}, {.fName = "bp", .fModRM = 5}, {.fName = "si", .fModRM = 0x6}, {.fName = "di", .fModRM = 7}, - {.fName = "r8", .fModRM = 8}, {.fName = "r13", .fModRM = 9}, - {.fName = "r9", .fModRM = 10}, {.fName = "r14", .fModRM = 11}, - {.fName = "r10", .fModRM = 12}, {.fName = "r15", .fModRM = 13}, - {.fName = "r11", .fModRM = 14}, }; - bool foundInstruction = false; + BOOL foundInstruction = false; for (auto& opcodeAMD64 : kOpcodesAMD64) { // strict check here @@ -981,140 +976,157 @@ bool LibCompiler::EncoderAMD64::WriteLine(std::string line, std::string file) { std::string name(opcodeAMD64.fName); /// Move instruction handler. - if (line.find(name) != std::string::npos && name == "mov") { - std::string substr = line.substr(line.find(name) + name.size()); + if (line.find(name) != std::string::npos) { + if (name == "mov" || name == "xor") { + std::string substr = line.substr(line.find(name) + name.size()); - uint64_t bits = kRegisterBitWidth; + uint64_t bits = kRegisterBitWidth; - if (substr.find(",") == std::string::npos) { - Detail::print_error("Syntax error: missing right operand.", "LibCompiler"); - throw std::runtime_error("syntax_err"); - } + if (substr.find(",") == std::string::npos) { + Detail::print_error("Syntax error: missing right operand.", "LibCompiler"); + throw std::runtime_error("syntax_err"); + } - bool onlyOneReg = true; + bool onlyOneReg = true; - std::vector<RegMapAMD64> currentRegList; + std::vector<RegMapAMD64> currentRegList; - for (auto& reg : kRegisterList) { - std::vector<char> regExt = {'e', 'r'}; + for (auto& reg : kRegisterList) { + std::vector<char> regExt = {'e', 'r'}; - for (auto& ext : regExt) { - std::string registerName; + for (auto& ext : regExt) { + std::string registerName; - if (bits > 16) registerName.push_back(ext); + if (bits > 16) registerName.push_back(ext); - registerName += reg.fName; + registerName += reg.fName; - while (line.find(registerName) != std::string::npos) { - line.erase(line.find(registerName), registerName.size()); + while (line.find(registerName) != std::string::npos) { + line.erase(line.find(registerName), registerName.size()); - if (bits == 16) { - if (registerName[0] == 'r') { - Detail::print_error("invalid size for register, current bit width is: " + - std::to_string(kRegisterBitWidth), - file); - throw std::runtime_error("invalid_reg_size"); + if (bits == 16) { + if (registerName[0] == 'r') { + Detail::print_error("invalid size for register, current bit width is: " + + std::to_string(kRegisterBitWidth), + file); + throw std::runtime_error("invalid_reg_size"); + } } - } - currentRegList.push_back({.fName = registerName, .fModRM = reg.fModRM}); + currentRegList.push_back({.fName = registerName, .fModRM = reg.fModRM}); + } } } - } - - if (currentRegList.size() > 1) onlyOneReg = false; - bool hasRBasedRegs = false; - - if (!onlyOneReg) { - /// very tricky to understand. - /// but this checks for a r8 through r15 register. - if (currentRegList[0].fName[0] == 'r' || currentRegList[1].fName[0] == 'r') { - if (isdigit(currentRegList[0].fName[1]) && isdigit(currentRegList[1].fName[1])) { - kAppBytes.emplace_back(0x4d); - hasRBasedRegs = true; - } else if (isdigit(currentRegList[0].fName[1]) || isdigit(currentRegList[1].fName[1])) { - kAppBytes.emplace_back(0x4c); - hasRBasedRegs = true; + if (currentRegList.size() > 1) onlyOneReg = false; + + bool hasRBasedRegs = false; + + if (!onlyOneReg) { + /// very tricky to understand. + /// but this checks for a r8 through r15 register. + if (currentRegList[0].fName[0] == 'r' || currentRegList[1].fName[0] == 'r') { + if (isdigit(currentRegList[0].fName[1]) && isdigit(currentRegList[1].fName[1])) { + kAppBytes.emplace_back(0x4d); + hasRBasedRegs = true; + } else if (isdigit(currentRegList[0].fName[1]) || + isdigit(currentRegList[1].fName[1])) { + kAppBytes.emplace_back(0x4c); + hasRBasedRegs = true; + } } } - } - if (bits == 64 || bits == 32) { - if (!hasRBasedRegs && bits >= 32) { - kAppBytes.emplace_back(opcodeAMD64.fOpcode); - } + if (name == "mov") { + if (bits == 64 || bits == 32) { + if (!hasRBasedRegs && bits >= 32) { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + } - if (!onlyOneReg) kAppBytes.emplace_back(0x89); - } else if (bits == 16) { - if (hasRBasedRegs) { - Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); - throw std::runtime_error("comb_op_reg"); + if (!onlyOneReg) kAppBytes.emplace_back(0x89); + } else if (bits == 16) { + if (hasRBasedRegs) { + Detail::print_error("Invalid combination of operands and registers.", + "LibCompiler"); + throw std::runtime_error("comb_op_reg"); + } else { + kAppBytes.emplace_back(0x66); + kAppBytes.emplace_back(0x89); + } + } } else { - kAppBytes.emplace_back(0x66); - kAppBytes.emplace_back(0x89); - } - } - - if (onlyOneReg) { - auto num = GetNumber32(line, ","); + if (!hasRBasedRegs && bits >= 32) { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + } - for (auto& num_idx : num.number) { - if (num_idx == 0) num_idx = 0xFF; + kAppBytes.emplace_back(0x31); } - auto modrm = (0x3 << 6 | currentRegList[0].fModRM); + if (onlyOneReg) { + auto num = GetNumber32(line, ","); - kAppBytes.emplace_back(0xC7); // prefixed before placing the modrm and then the number. - kAppBytes.emplace_back(modrm); - kAppBytes.emplace_back(num.number[0]); - kAppBytes.emplace_back(num.number[1]); - kAppBytes.emplace_back(num.number[2]); - kAppBytes.emplace_back(num.number[3]); + for (auto& num_idx : num.number) { + if (num_idx == 0) num_idx = 0xFF; + } - break; - } + auto modrm = (0x3 << 6 | currentRegList[0].fModRM); - if (currentRegList[1].fName[0] == 'r' && currentRegList[0].fName[0] == 'e') { - Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); - throw std::runtime_error("comb_op_reg"); - } + kAppBytes.emplace_back(0xC7); // prefixed before placing the modrm and then the number. + kAppBytes.emplace_back(modrm); - if (currentRegList[0].fName[0] == 'r' && currentRegList[1].fName[0] == 'e') { - Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); - throw std::runtime_error("comb_op_reg"); - } + if (name != "xor") { + kAppBytes.emplace_back(num.number[0]); + kAppBytes.emplace_back(num.number[1]); + kAppBytes.emplace_back(num.number[2]); + kAppBytes.emplace_back(num.number[3]); + } - if (bits == 16) { - if (currentRegList[0].fName[0] == 'r' || currentRegList[0].fName[0] == 'e') { - Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); - throw std::runtime_error("comb_op_reg"); + break; } - if (currentRegList[1].fName[0] == 'r' || currentRegList[1].fName[0] == 'e') { + if (currentRegList[1].fName[0] == 'r' && currentRegList[0].fName[0] == 'e') { Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); throw std::runtime_error("comb_op_reg"); } - } else { - if (currentRegList[0].fName[0] != 'r' || currentRegList[0].fName[0] == 'e') { + + if (currentRegList[0].fName[0] == 'r' && currentRegList[1].fName[0] == 'e') { Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); throw std::runtime_error("comb_op_reg"); } - if (currentRegList[1].fName[0] != 'r' || currentRegList[1].fName[0] == 'e') { - Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); - throw std::runtime_error("comb_op_reg"); + if (bits == 16) { + if (currentRegList[0].fName[0] == 'r' || currentRegList[0].fName[0] == 'e') { + Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); + throw std::runtime_error("comb_op_reg"); + } + + if (currentRegList[1].fName[0] == 'r' || currentRegList[1].fName[0] == 'e') { + Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); + throw std::runtime_error("comb_op_reg"); + } + } else { + if (currentRegList[0].fName[0] != 'r' || currentRegList[0].fName[0] == 'e') { + Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); + throw std::runtime_error("comb_op_reg"); + } + + if (currentRegList[1].fName[0] != 'r' || currentRegList[1].fName[0] == 'e') { + Detail::print_error("Invalid combination of operands and registers.", "LibCompiler"); + throw std::runtime_error("comb_op_reg"); + } } - } - /// encode register using the modrm encoding. + /// encode register using the modrm encoding. - auto modrm = (0x3 << 6 | currentRegList[1].fModRM << 3 | currentRegList[0].fModRM); + auto modrm = (0x3 << 6 | currentRegList[1].fModRM << 3 | currentRegList[0].fModRM); - kAppBytes.emplace_back(modrm); + kAppBytes.emplace_back(modrm); - break; - } else if (name == "int" || name == "into" || name == "intd") { + break; + } + } + + if (name == "int" || name == "into" || name == "intd") { kAppBytes.emplace_back(opcodeAMD64.fOpcode); this->WriteNumber8(line.find(name) + name.size() + 1, line); @@ -1127,6 +1139,11 @@ bool LibCompiler::EncoderAMD64::WriteLine(std::string line, std::string file) { } break; + } else if (name == "syscall") { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + kAppBytes.emplace_back(0x05); + + break; } else { kAppBytes.emplace_back(opcodeAMD64.fOpcode); diff --git a/dev/LibCompiler/src/AssemblerARM64.cc b/dev/LibCompiler/src/Backend/AssemblerARM64.cc index 8686edb..a1cc6dc 100644 --- a/dev/LibCompiler/src/AssemblerARM64.cc +++ b/dev/LibCompiler/src/Backend/AssemblerARM64.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -15,14 +15,16 @@ ///////////////////////////////////////////////////////////////////////////////////////// +#ifndef __ASM_NEED_ARM64__ #define __ASM_NEED_ARM64__ 1 +#endif #include <LibCompiler/AE.h> #include <LibCompiler/Backend/Aarch64.h> -#include <LibCompiler/Detail/AsmUtils.h> #include <LibCompiler/ErrorID.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/PEF.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Util/AsmUtils.h> #include <LibCompiler/Version.h> #include <algorithm> #include <filesystem> @@ -41,12 +43,9 @@ #define kWhite "\e[0;97m" #define kYellow "\e[0;33m" -#define kStdOut (std::cout << kWhite) -#define kStdErr (std::cout << kRed) - constexpr auto cPowerIPAlignment = 0x1U; -static CharType kOutputArch = LibCompiler::kPefArchARM64; +static Char kOutputArch = LibCompiler::kPefArchARM64; static std::size_t kCounter = 1UL; @@ -74,7 +73,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// LIBCOMPILER_MODULE(AssemblerMainARM64) { - ::signal(SIGSEGV, Detail::segfault_handler); + LibCompiler::install_signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { if (argv[i][0] == '-') { diff --git a/dev/LibCompiler/src/AssemblerPowerPC.cc b/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc index f2c3be3..b979f64 100644 --- a/dev/LibCompiler/src/AssemblerPowerPC.cc +++ b/dev/LibCompiler/src/Backend/AssemblerPowerPC.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ @@ -15,14 +15,16 @@ ///////////////////////////////////////////////////////////////////////////////////////// +#ifndef __ASM_NEED_PPC__ #define __ASM_NEED_PPC__ 1 +#endif #include <LibCompiler/AE.h> #include <LibCompiler/Backend/PowerPC.h> -#include <LibCompiler/Detail/AsmUtils.h> #include <LibCompiler/ErrorID.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/PEF.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Util/AsmUtils.h> #include <LibCompiler/Version.h> #include <algorithm> #include <filesystem> @@ -41,12 +43,9 @@ #define kWhite "\e[0;97m" #define kYellow "\e[0;33m" -#define kStdOut (std::cout << kWhite) -#define kStdErr (std::cout << kRed) - constexpr auto cPowerIPAlignment = 0x4U; -static CharType kOutputArch = LibCompiler::kPefArchPowerPC; +static Char kOutputArch = LibCompiler::kPefArchPowerPC; static std::size_t kCounter = 1UL; @@ -74,7 +73,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// LIBCOMPILER_MODULE(AssemblerMainPower64) { - ::signal(SIGSEGV, Detail::segfault_handler); + LibCompiler::install_signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { if (argv[i][0] == '-') { diff --git a/dev/LibCompiler/src/StringView.cc b/dev/LibCompiler/src/BasicString.cc index 41ee636..41989fe 100644 --- a/dev/LibCompiler/src/StringView.cc +++ b/dev/LibCompiler/src/BasicString.cc @@ -8,7 +8,7 @@ */ /** - * @file String.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/StringView.h> +#include <LibCompiler/BasicString.h> namespace LibCompiler { -CharType* StringView::Data() { +Char* BasicString::Data() { return m_Data; } -const CharType* StringView::CData() const { +const Char* BasicString::CData() const { return m_Data; } -SizeType StringView::Length() const { +SizeType BasicString::Length() const { return strlen(m_Data); } -bool StringView::operator==(const StringView& 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 StringView::operator==(const StringView& rhs) const { return true; } -bool StringView::operator==(const CharType* rhs) const { +bool BasicString::operator==(const Char* rhs) const { if (string_length(rhs) != Length()) return false; for (SizeType index = 0; index < string_length(rhs); ++index) { @@ -53,7 +53,7 @@ bool StringView::operator==(const CharType* rhs) const { return true; } -bool StringView::operator!=(const StringView& 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 StringView::operator!=(const StringView& rhs) const { return true; } -bool StringView::operator!=(const CharType* rhs) const { +bool BasicString::operator!=(const Char* rhs) const { if (string_length(rhs) != Length()) return false; for (SizeType index = 0; index < string_length(rhs); ++index) { @@ -73,10 +73,10 @@ bool StringView::operator!=(const CharType* rhs) const { return true; } -StringView StringBuilder::Construct(const CharType* data) { - if (!data || *data == 0) return StringView(0); +BasicString StringBuilder::Construct(const Char* data) { + if (!data || *data == 0) return BasicString(0); - StringView view(strlen(data)); + BasicString view(strlen(data)); view += data; return view; @@ -92,7 +92,7 @@ const char* StringBuilder::FromInt(const char* fmt, int i) { memset(ret, 0, ret_len); - CharType result[sizeof(int64_t)]; + Char result[sizeof(int64_t)]; if (!to_str(result, sizeof(int64_t), i)) { delete[] ret; @@ -183,9 +183,9 @@ const char* StringBuilder::Format(const char* fmt, const char* fmtRight) { return ret; } -StringView& StringView::operator+=(const CharType* rhs) { +BasicString& BasicString::operator+=(const Char* rhs) { if (strlen(rhs) > this->m_Sz) { - throw std::runtime_error("out_of_bounds: StringView"); + throw std::runtime_error("out_of_bounds: BasicString"); } memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs)); @@ -194,9 +194,9 @@ StringView& StringView::operator+=(const CharType* rhs) { return *this; } -StringView& StringView::operator+=(const StringView& rhs) { +BasicString& BasicString::operator+=(const BasicString& rhs) { if (rhs.m_Cur > this->m_Sz) { - throw std::runtime_error("out_of_bounds: StringView"); + 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/AssemblyFactory+AssemblyInterface.cc b/dev/LibCompiler/src/CodeGen.cc index 11655fb..3f215c5 100644 --- a/dev/LibCompiler/src/AssemblyFactory+AssemblyInterface.cc +++ b/dev/LibCompiler/src/CodeGen.cc @@ -1,10 +1,10 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ -#include <LibCompiler/AssemblyInterface.h> +#include <LibCompiler/CodeGen.h> #include <LibCompiler/ErrorID.h> /** @@ -23,12 +23,17 @@ 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; +Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) noexcept { + if (sourceFile.length() < 1) return LIBCOMPILER_UNIMPLEMENTED; + if (!fMounted) return LIBCOMPILER_UNIMPLEMENTED; if (arch != fMounted->Arch()) return LIBCOMPILER_INVALID_ARCH; - return fMounted->CompileToFormat(sourceFile, arch); + try { + return this->fMounted->CompileToFormat(sourceFile, arch); + } catch (std::exception& e) { + return LIBCOMPILER_EXEC_ERROR; + } } ///! @brief mount assembly backend. @@ -42,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/Frontend.cc b/dev/LibCompiler/src/Frontend.cc new file mode 100644 index 0000000..69ff6fd --- /dev/null +++ b/dev/LibCompiler/src/Frontend.cc @@ -0,0 +1,51 @@ +/* ------------------------------------------- + + Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#include <LibCompiler/Frontend.h> + +namespace LibCompiler { +/// find the perfect matching word in a haystack. +/// \param haystack base string +/// \param needle the string we search for. +/// \return if we found it or not. +bool find_word(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + // declare lambda + auto not_part_of_word = [&](int index) { + if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; + + if (index <= 0 || index >= haystack.size()) return true; + + return false; + }; + + return not_part_of_word(index - 1) && not_part_of_word(index + needle.size()); +} + +/// find a word within strict conditions and returns a range of it. +/// \param haystack +/// \param needle +/// \return position of needle. +std::size_t find_word_range(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + if (!isalnum((haystack[index + needle.size() + 1])) && + !isdigit(haystack[index + needle.size() + 1]) && + !isalnum((haystack[index - needle.size() - 1])) && + !isdigit(haystack[index - needle.size() - 1])) { + return index; + } + + return std::string::npos; +} +} // namespace LibCompiler
\ No newline at end of file diff --git a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/Frontend/CompilerCPlusPlusAMD64.cc index ace6d17..df9035d 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/Frontend/CompilerCPlusPlusAMD64.cc @@ -16,14 +16,12 @@ // extern_segment, @autodelete { ... }, fn foo() -> auto { ... } -#include <LibCompiler/Backend/Amd64.h> -#include <LibCompiler/Detail/ClUtils.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Backend/X64.h> +#include <LibCompiler/Frontend.h> #include <LibCompiler/UUID.h> +#include <LibCompiler/Util/CompilerUtils.h> -#include <cstdio> - -/* NE C++ Compiler */ +/* NeKernel C++ Compiler Driver */ /* This is part of the LibCompiler. */ /* (c) Amlal El Mahrouss */ @@ -69,29 +67,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 +/// \brief Offset based struct/class struct CompilerStructMap final { - std::string fName; - std::string fReg; - - // offset counter - std::size_t fOffsetsCnt; - - // offset array - std::vector<std::pair<Int32, 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; - Boolean fVerbose; + LibCompiler::STLString fLastFile; + LibCompiler::STLString fLastError; }; } // namespace Detail @@ -99,22 +91,10 @@ static Detail::CompilerState kState; static Int32 kOnClassScope = 0; -namespace Detail { -/// @brief prints an error into stdout. -/// @param reason the reason of the error. -/// @param file where does it originate from? -void print_error(std::string reason, std::string file) noexcept; - -struct CompilerType final { - std::string fName; - std::string fValue; -}; -} // namespace Detail - ///////////////////////////////////////////////////////////////////////////////////////// // Target architecture. -static int kMachine = LibCompiler::AssemblyFactory::kArchAMD64; +static Int32 kMachine = LibCompiler::AssemblyFactory::kArchAMD64; ///////////////////////////////////////// @@ -122,10 +102,6 @@ static int kMachine = LibCompiler::AssemblyFactory::kArchAMD64; ///////////////////////////////////////// -static size_t kRegisterCnt = kAsmRegisterLimit; -static size_t kStartUsable = 8; -static size_t kUsableLimit = 15; -static size_t kRegisterCounter = kStartUsable; static std::vector<LibCompiler::CompilerKeyword> kKeywords; ///////////////////////////////////////// @@ -141,15 +117,16 @@ static Boolean kOnForLoop = false; static Boolean kInBraces = false; static size_t kBracesCount = 0UL; -/* @brief C++ compiler backend for the NE C++ driver */ -class CompilerFrontendCPlusPlus final : public LibCompiler::ICompilerFrontend { +/* @brief C++ compiler backend for the NeKernel C++ driver */ +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; }; @@ -158,15 +135,15 @@ class CompilerFrontendCPlusPlus final : public LibCompiler::ICompilerFrontend { 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", }; @@ -178,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; ///////////////////////////////////////////////////////////////////////////////////////// @@ -188,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.empty()) 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; @@ -204,16 +184,22 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { break; } - if (text[text.find(keyword.keyword_name) - 1] == '+' && - keyword.keyword_kind == LibCompiler::KeywordKind::kKeywordKindVariableAssign) + std::size_t pos = text.find(keyword.keyword_name); + if (pos == std::string::npos) continue; + + // Safe guard: can't go before start of string + if (pos > 0 && text[pos - 1] == '+' && + keyword.keyword_kind == LibCompiler::kKeywordKindVariableAssign) continue; - if (text[text.find(keyword.keyword_name) - 1] == '-' && - keyword.keyword_kind == LibCompiler::KeywordKind::kKeywordKindVariableAssign) + if (pos > 0 && text[pos - 1] == '-' && + keyword.keyword_kind == LibCompiler::kKeywordKindVariableAssign) continue; - if (text[text.find(keyword.keyword_name) + 1] == '=' && - keyword.keyword_kind == LibCompiler::KeywordKind::kKeywordKindVariableAssign) + // Safe guard: don't go out of range + if ((pos + keyword.keyword_name.size()) < text.size() && + text[pos + keyword.keyword_name.size()] == '=' && + keyword.keyword_kind == LibCompiler::kKeywordKindVariableAssign) continue; keywords_list.emplace_back(std::make_pair(keyword, index)); @@ -221,20 +207,29 @@ 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) == LibCompiler::STLString::npos) continue; + switch (keyword.first.keyword_kind) { case LibCompiler::KeywordKind::kKeywordKindClass: { ++kOnClassScope; break; } case LibCompiler::KeywordKind::kKeywordKindIf: { - auto expr = text.substr( - text.find(keyword.first.keyword_name) + keyword.first.keyword_name.size() + 1, - text.find(")") - 1); + std::size_t keywordPos = text.find(keyword.first.keyword_name); + std::size_t openParen = text.find("(", keywordPos); + std::size_t closeParen = text.find(")", openParen); + + if (keywordPos == LibCompiler::STLString::npos || + openParen == LibCompiler::STLString::npos || + closeParen == LibCompiler::STLString::npos || closeParen <= openParen) { + Detail::print_error("Malformed if expression: " + text, file); + 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("<=")); @@ -272,34 +267,36 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { auto& valueOfVar = !isdigit(left[0]) ? left : right; - for (auto pairRight : kRegisterMap) { - ++indexRight; + if (!valueOfVar.empty()) { + for (auto pairRight : kRegisterMap) { + ++indexRight; + + if (pairRight != valueOfVar) { + auto& valueOfVarOpposite = isdigit(left[0]) ? left : right; + + syntax_tree.fUserValue += + "mov " + kRegisterList[indexRight + 1] + ", " + valueOfVarOpposite + "\n"; + syntax_tree.fUserValue += "cmp " + kRegisterList[kRegisterMap.size() - 1] + "," + + kRegisterList[indexRight + 1] + "\n"; + + goto done_iterarting_on_if; + } - if (pairRight != valueOfVar) { auto& valueOfVarOpposite = isdigit(left[0]) ? left : right; syntax_tree.fUserValue += "mov " + kRegisterList[indexRight + 1] + ", " + valueOfVarOpposite + "\n"; - syntax_tree.fUserValue += "cmp " + kRegisterList[kRegisterMap.size() - 1] + "," + + syntax_tree.fUserValue += "cmp " + kRegisterList[kRegisterMap.size() - 1] + ", " + kRegisterList[indexRight + 1] + "\n"; - goto done_iterarting_on_if; + break; } - - auto& valueOfVarOpposite = isdigit(left[0]) ? left : right; - - syntax_tree.fUserValue += - "mov " + kRegisterList[indexRight + 1] + ", " + valueOfVarOpposite + "\n"; - syntax_tree.fUserValue += "cmp " + kRegisterList[kRegisterMap.size() - 1] + ", " + - kRegisterList[indexRight + 1] + "\n"; - - break; } } done_iterarting_on_if: - std::string fnName = text; + LibCompiler::STLString fnName = text; fnName.erase(fnName.find(keyword.first.keyword_name)); for (auto& ch : fnName) { @@ -322,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) { @@ -338,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); @@ -359,7 +356,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { ++indexFnName; } - if (fnName.find("(") != LibCompiler::String::npos) { + if (fnName.find("(") != LibCompiler::STLString::npos) { fnName.erase(fnName.find("(")); } @@ -371,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; @@ -403,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); @@ -417,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("+=")); @@ -440,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; @@ -451,7 +449,9 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } } - std::string instr = "mov "; + LibCompiler::STLString instr = "mov "; + + std::vector<LibCompiler::STLString> newVars; if (typeFound && keyword.first.keyword_kind != LibCompiler::KeywordKind::kKeywordKindVariableInc && @@ -460,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); } @@ -527,24 +527,27 @@ 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; } } - kRegisterMap.push_back(varName); + newVars.push_back(varName); break; } - if (kKeywords[keyword.second - 1].keyword_kind == LibCompiler::kKeywordKindType || + kRegisterMap.insert(kRegisterMap.end(), newVars.begin(), newVars.end()); + + if (keyword.second > 0 && + kKeywords[keyword.second - 1].keyword_kind == LibCompiler::kKeywordKindType || kKeywords[keyword.second - 1].keyword_kind == LibCompiler::kKeywordKindTypePtr) { syntax_tree.fUserValue = "\n"; continue; @@ -562,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); } @@ -580,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); } @@ -622,6 +625,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { break; } + newVars.push_back(varName); break; } @@ -629,14 +633,16 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { Detail::print_error("Variable not declared: " + varName, file); } + kRegisterMap.insert(kRegisterMap.end(), newVars.begin(), newVars.end()); + break; } 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])) { @@ -666,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); @@ -699,8 +705,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } } - kState.fOutputValue = syntax_tree.fUserValue; - return true; + return syntax_tree; } ///////////////////////////////////////////////////////////////////////////////////////// @@ -711,39 +716,33 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { ///////////////////////////////////////////////////////////////////////////////////////// -class AssemblyCPlusPlusInterface final ASSEMBLY_INTERFACE { +class AssemblyCPlusPlusInterface final LC_ASSEMBLY_INTERFACE { public: explicit AssemblyCPlusPlusInterface() = default; ~AssemblyCPlusPlusInterface() override = default; LIBCOMPILER_COPY_DEFAULT(AssemblyCPlusPlusInterface); - [[maybe_unused]] Int32 Arch() noexcept override { - return LibCompiler::AssemblyFactory::kArchAMD64; - } + 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; - /* @brief copy contents wihtout extension */ - std::ifstream src_fp = std::ifstream(src, std::ios::in); + LibCompiler::STLString dest = src; + dest += ".pp.masm"; - std::string dest = src; - dest += ".masm"; + std::ofstream out_fp(dest); + std::ifstream src_fp = std::ifstream(src + ".pp"); - std::string line_source; + LibCompiler::STLString line_source; - std::ofstream out(dest); + out_fp << "#bits 64\n"; + out_fp << "#org " << kOrigin << "\n\n"; while (std::getline(src_fp, line_source)) { - kCompilerFrontend->Compile(line_source, src); - out << kState.fOutputValue; + out_fp << kCompilerFrontend->Compile(line_source, src).fUserValue; } - out.flush(); - - if (kAcceptableErrors > 0) return kExitNO; - return kExitOK; } }; @@ -758,87 +757,74 @@ class AssemblyCPlusPlusInterface final ASSEMBLY_INTERFACE { LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { Boolean skip = false; - kKeywords.push_back({.keyword_name = "if", .keyword_kind = LibCompiler::kKeywordKindIf}); - kKeywords.push_back({.keyword_name = "else", .keyword_kind = LibCompiler::kKeywordKindElse}); - kKeywords.push_back({.keyword_name = "else if", .keyword_kind = LibCompiler::kKeywordKindElseIf}); - - kKeywords.push_back({.keyword_name = "class", .keyword_kind = LibCompiler::kKeywordKindClass}); - kKeywords.push_back({.keyword_name = "struct", .keyword_kind = LibCompiler::kKeywordKindClass}); - kKeywords.push_back( - {.keyword_name = "namespace", .keyword_kind = LibCompiler::kKeywordKindNamespace}); - kKeywords.push_back( - {.keyword_name = "typedef", .keyword_kind = LibCompiler::kKeywordKindTypedef}); - kKeywords.push_back({.keyword_name = "using", .keyword_kind = LibCompiler::kKeywordKindTypedef}); - kKeywords.push_back({.keyword_name = "{", .keyword_kind = LibCompiler::kKeywordKindBodyStart}); - kKeywords.push_back({.keyword_name = "}", .keyword_kind = LibCompiler::kKeywordKindBodyEnd}); - kKeywords.push_back({.keyword_name = "auto", .keyword_kind = LibCompiler::kKeywordKindVariable}); - kKeywords.push_back({.keyword_name = "int", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "Boolean", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "unsigned", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "short", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "char", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "long", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "float", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "double", .keyword_kind = LibCompiler::kKeywordKindType}); - kKeywords.push_back({.keyword_name = "void", .keyword_kind = LibCompiler::kKeywordKindType}); - - kKeywords.push_back( - {.keyword_name = "auto*", .keyword_kind = LibCompiler::kKeywordKindVariablePtr}); - kKeywords.push_back({.keyword_name = "int*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back( - {.keyword_name = "Boolean*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back( - {.keyword_name = "unsigned*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back({.keyword_name = "short*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back({.keyword_name = "char*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back({.keyword_name = "long*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back({.keyword_name = "float*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back( - {.keyword_name = "double*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - kKeywords.push_back({.keyword_name = "void*", .keyword_kind = LibCompiler::kKeywordKindTypePtr}); - - kKeywords.push_back( - {.keyword_name = "(", .keyword_kind = LibCompiler::kKeywordKindFunctionStart}); - kKeywords.push_back({.keyword_name = ")", .keyword_kind = LibCompiler::kKeywordKindFunctionEnd}); - kKeywords.push_back( - {.keyword_name = "=", .keyword_kind = LibCompiler::kKeywordKindVariableAssign}); - kKeywords.push_back({.keyword_name = "+=", .keyword_kind = LibCompiler::kKeywordKindVariableInc}); - kKeywords.push_back({.keyword_name = "-=", .keyword_kind = LibCompiler::kKeywordKindVariableDec}); - kKeywords.push_back({.keyword_name = "const", .keyword_kind = LibCompiler::kKeywordKindConstant}); - kKeywords.push_back({.keyword_name = "*", .keyword_kind = LibCompiler::kKeywordKindPtr}); - kKeywords.push_back({.keyword_name = "->", .keyword_kind = LibCompiler::kKeywordKindPtrAccess}); - kKeywords.push_back({.keyword_name = ".", .keyword_kind = LibCompiler::kKeywordKindAccess}); - kKeywords.push_back({.keyword_name = ",", .keyword_kind = LibCompiler::kKeywordKindArgSeparator}); - kKeywords.push_back({.keyword_name = ";", .keyword_kind = LibCompiler::kKeywordKindEndInstr}); - kKeywords.push_back({.keyword_name = ":", .keyword_kind = LibCompiler::kKeywordKindSpecifier}); - kKeywords.push_back( - {.keyword_name = "public:", .keyword_kind = LibCompiler::kKeywordKindSpecifier}); - kKeywords.push_back( - {.keyword_name = "private:", .keyword_kind = LibCompiler::kKeywordKindSpecifier}); - kKeywords.push_back( - {.keyword_name = "protected:", .keyword_kind = LibCompiler::kKeywordKindSpecifier}); - kKeywords.push_back( - {.keyword_name = "final", .keyword_kind = LibCompiler::kKeywordKindSpecifier}); - kKeywords.push_back({.keyword_name = "return", .keyword_kind = LibCompiler::kKeywordKindReturn}); - kKeywords.push_back( - {.keyword_name = "--*", .keyword_kind = LibCompiler::kKeywordKindCommentMultiLineStart}); - kKeywords.push_back( - {.keyword_name = "*/", .keyword_kind = LibCompiler::kKeywordKindCommentMultiLineStart}); - kKeywords.push_back( - {.keyword_name = "--/", .keyword_kind = LibCompiler::kKeywordKindCommentInline}); - kKeywords.push_back({.keyword_name = "==", .keyword_kind = LibCompiler::kKeywordKindEq}); - kKeywords.push_back({.keyword_name = "!=", .keyword_kind = LibCompiler::kKeywordKindNotEq}); - kKeywords.push_back({.keyword_name = ">=", .keyword_kind = LibCompiler::kKeywordKindGreaterEq}); - kKeywords.push_back({.keyword_name = "<=", .keyword_kind = LibCompiler::kKeywordKindLessEq}); - - kErrorLimit = 100; + kKeywords.emplace_back("if", LibCompiler::kKeywordKindIf); + kKeywords.emplace_back("else", LibCompiler::kKeywordKindElse); + kKeywords.emplace_back("else if", LibCompiler::kKeywordKindElseIf); + + kKeywords.emplace_back("class", LibCompiler::kKeywordKindClass); + kKeywords.emplace_back("struct", LibCompiler::kKeywordKindClass); + kKeywords.emplace_back("namespace", LibCompiler::kKeywordKindNamespace); + kKeywords.emplace_back("typedef", LibCompiler::kKeywordKindTypedef); + kKeywords.emplace_back("using", LibCompiler::kKeywordKindTypedef); + kKeywords.emplace_back("{", LibCompiler::kKeywordKindBodyStart); + kKeywords.emplace_back("}", LibCompiler::kKeywordKindBodyEnd); + kKeywords.emplace_back("auto", LibCompiler::kKeywordKindVariable); + kKeywords.emplace_back("int", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("bool", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("unsigned", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("short", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("char", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("long", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("float", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("double", LibCompiler::kKeywordKindType); + kKeywords.emplace_back("void", LibCompiler::kKeywordKindType); + + kKeywords.emplace_back("auto*", LibCompiler::kKeywordKindVariablePtr); + kKeywords.emplace_back("int*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("bool*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("unsigned*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("short*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("char*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("long*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("float*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("double*", LibCompiler::kKeywordKindTypePtr); + kKeywords.emplace_back("void*", LibCompiler::kKeywordKindTypePtr); + + kKeywords.emplace_back("(", LibCompiler::kKeywordKindFunctionStart); + kKeywords.emplace_back(")", LibCompiler::kKeywordKindFunctionEnd); + kKeywords.emplace_back("=", LibCompiler::kKeywordKindVariableAssign); + kKeywords.emplace_back("+=", LibCompiler::kKeywordKindVariableInc); + kKeywords.emplace_back("-=", LibCompiler::kKeywordKindVariableDec); + kKeywords.emplace_back("const", LibCompiler::kKeywordKindConstant); + kKeywords.emplace_back("*", LibCompiler::kKeywordKindPtr); + kKeywords.emplace_back("->", LibCompiler::kKeywordKindPtrAccess); + kKeywords.emplace_back(".", LibCompiler::kKeywordKindAccess); + kKeywords.emplace_back(",", LibCompiler::kKeywordKindArgSeparator); + kKeywords.emplace_back(";", LibCompiler::kKeywordKindEndInstr); + kKeywords.emplace_back(":", LibCompiler::kKeywordKindSpecifier); + kKeywords.emplace_back("public:", LibCompiler::kKeywordKindSpecifier); + kKeywords.emplace_back("private:", LibCompiler::kKeywordKindSpecifier); + kKeywords.emplace_back("protected:", LibCompiler::kKeywordKindSpecifier); + kKeywords.emplace_back("final", LibCompiler::kKeywordKindSpecifier); + kKeywords.emplace_back("return", LibCompiler::kKeywordKindReturn); + kKeywords.emplace_back("/*", LibCompiler::kKeywordKindCommentMultiLineStart); + kKeywords.emplace_back("*/", LibCompiler::kKeywordKindCommentMultiLineEnd); + kKeywords.emplace_back("//", LibCompiler::kKeywordKindCommentInline); + kKeywords.emplace_back("==", LibCompiler::kKeywordKindEq); + kKeywords.emplace_back("!=", LibCompiler::kKeywordKindNotEq); + kKeywords.emplace_back(">=", LibCompiler::kKeywordKindGreaterEq); + kKeywords.emplace_back("<=", LibCompiler::kKeywordKindLessEq); + + kErrorLimit = 0; kCompilerFrontend = new CompilerFrontendCPlusPlus(); kFactory.Mount(new AssemblyCPlusPlusInterface()); - ::signal(SIGSEGV, Detail::segfault_handler); + LibCompiler::install_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; @@ -846,7 +832,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { } if (strcmp(argv[index], "-cxx-verbose") == 0) { - kState.fVerbose = true; + kVerbose = true; continue; } @@ -854,7 +840,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { if (strcmp(argv[index], "-cxx-dialect") == 0) { if (kCompilerFrontend) std::cout << kCompilerFrontend->Language() << "\n"; - return kExitOK; + return LIBCOMPILER_SUCCESS; } if (strcmp(argv[index], "-cxx-max-err") == 0) { @@ -871,7 +857,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { continue; } - std::string err = "Unknown option: "; + LibCompiler::STLString err = "Unknown option: "; err += argv[index]; Detail::print_error(err, "cxxdrv"); @@ -879,31 +865,26 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { continue; } - std::string argv_i = argv[index]; + LibCompiler::STLString argv_i = argv[index]; - std::vector exts = kExtListCxx; - Boolean found = false; + std::vector<LibCompiler::STLString> exts = kExtListCxx; - for (std::string ext : exts) { - if (argv_i.find(ext) != std::string::npos) { - found = true; - break; - } - } + for (LibCompiler::STLString ext : exts) { + if (argv_i.ends_with(ext)) { + if (kFactory.Compile(argv_i, kMachine) != kExitOK) { + return LIBCOMPILER_INVALID_DATA; + } - if (!found) { - if (kState.fVerbose) { - Detail::print_error(argv_i + " is not a valid C++ source.\n", "cxxdrv"); + break; } - - return kExitNO; } - - kFactory.Compile(argv_i, kMachine); } - return kExitNO; + kFactory.Unmount(); + + return LIBCOMPILER_SUCCESS; } -// Last rev 8-1-24 +// +// Last rev 23-5-25 // diff --git a/dev/LibCompiler/src/DynamicLinkerPEF.cc b/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc index 9e3be82..b58c786 100644 --- a/dev/LibCompiler/src/DynamicLinkerPEF.cc +++ b/dev/LibCompiler/src/Linkers/DynamicLinkerPEF.cc @@ -20,7 +20,7 @@ #include <LibCompiler/ErrorID.h> //! Assembler Kit -#include <LibCompiler/AssemblyInterface.h> +#include <LibCompiler/CodeGen.h> //! Preferred Executable Format #include <LibCompiler/PEF.h> @@ -34,17 +34,14 @@ //! Advanced Executable Object Format. #include <LibCompiler/AE.h> -//! Format header. -#include <format> - //! LibCompiler utils. -#include <LibCompiler/Detail/ClUtils.h> +#include <LibCompiler/Util/CompilerUtils.h> //! I/O stream from std c++ #include <iostream> #define kLinkerVersionStr \ - "NeKernel 64-Bit Linker (Preferred Executable Format) {}, (c) Amlal El Mahrouss " \ + "NeKernel 64-Bit Linker (Preferred Executable Format) %s, (c) Amlal El Mahrouss " \ "2024-2025 " \ "all rights reserved.\n" @@ -59,19 +56,19 @@ #define kLinkerAbiContainer "__PEFContainer:ABI:" #define kPrintF printf -#define kLinkerSplash() kOutCon << std::format(kLinkerVersionStr, kDistVersion) +#define kLinkerSplash() kConsoleOut << std::printf(kLinkerVersionStr, kDistVersion) /// @brief PEF stack size symbol. #define kLinkerStackSizeSymbol "__PEFSizeOfReserveStack" -#define kOutCon \ +#define kConsoleOut \ (std::cout << "\e[0;31m" \ << "ld64: " \ << "\e[0;97m") namespace Detail { struct DynamicLinkerBlob final { - std::vector<CharType> mBlob{}; // PEF code/bss/data blob. + std::vector<Char> mBlob{}; // PEF code/bss/data blob. UIntPtr mOffset{0UL}; // the offset of the PEF container header... }; } // namespace Detail @@ -83,20 +80,20 @@ enum { kABITypeInvalid = 0xFFFF, }; -static LibCompiler::String kOutput = "a" kPefExt; -static Int32 kAbi = kABITypeNE; -static Int32 kSubArch = kPefNoSubCpu; -static Int32 kArch = LibCompiler::kPefArchInvalid; -static Bool kFatBinaryEnable = false; -static Bool kStartFound = false; -static Bool kDuplicateSymbols = false; +static LibCompiler::STLString kOutput = "a" kPefExt; +static Int32 kAbi = kABITypeNE; +static Int32 kSubArch = kPefNoSubCpu; +static Int32 kArch = LibCompiler::kPefArchInvalid; +static Bool kFatBinaryEnable = false; +static Bool kStartFound = false; +static Bool kDuplicateSymbols = false; /* ld64 is to be found, mld is to be found at runtime. */ -static const CharType* kLdDefineSymbol = ":UndefinedSymbol:"; -static const CharType* kLdDynamicSym = ":RuntimeSymbol:"; +static const Char* kLdDefineSymbol = ":UndefinedSymbol:"; +static const Char* kLdDynamicSym = ":RuntimeSymbol:"; /* object code and list. */ -static std::vector<LibCompiler::String> kObjectList; +static std::vector<LibCompiler::STLString> kObjectList; static std::vector<Detail::DynamicLinkerBlob> kObjectBytes; /// @brief NE 64-bit Linker. @@ -104,7 +101,7 @@ static std::vector<Detail::DynamicLinkerBlob> kObjectBytes; LIBCOMPILER_MODULE(DynamicLinker64PEF) { bool is_executable = true; - ::signal(SIGSEGV, Detail::segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); /** * @brief parse flags and trigger options. @@ -113,23 +110,24 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if (StringCompare(argv[linker_arg], "-help") == 0) { kLinkerSplash(); - kOutCon << "-version: Show linker version.\n"; - kOutCon << "-help: Show linker help.\n"; - kOutCon << "-ld-verbose: Enable linker trace.\n"; - kOutCon << "-dylib: Output as a Dynamic PEF.\n"; - kOutCon << "-fat: Output as a FAT PEF.\n"; - kOutCon << "-32k: Output as a 32x0 PEF.\n"; - kOutCon << "-64k: Output as a 64x0 PEF.\n"; - kOutCon << "-amd64: Output as a AMD64 PEF.\n"; - kOutCon << "-rv64: Output as a RISC-V PEF.\n"; - kOutCon << "-power64: Output as a POWER PEF.\n"; - kOutCon << "-arm64: Output as a ARM64 PEF.\n"; - kOutCon << "-output: Select the output file name.\n"; - - return EXIT_SUCCESS; + kConsoleOut << "-version: Show linker version.\n"; + kConsoleOut << "-help: Show linker help.\n"; + kConsoleOut << "-ld-verbose: Enable linker trace.\n"; + kConsoleOut << "-dylib: Output as a Dynamic PEF.\n"; + kConsoleOut << "-fat: Output as a FAT PEF.\n"; + kConsoleOut << "-32k: Output as a 32x0 PEF.\n"; + kConsoleOut << "-64k: Output as a 64x0 PEF.\n"; + kConsoleOut << "-amd64: Output as a AMD64 PEF.\n"; + kConsoleOut << "-rv64: Output as a RISC-V PEF.\n"; + kConsoleOut << "-power64: Output as a POWER PEF.\n"; + kConsoleOut << "-arm64: Output as a ARM64 PEF.\n"; + kConsoleOut << "-output: Select the output file name.\n"; + + return LIBCOMPILER_SUCCESS; } else if (StringCompare(argv[linker_arg], "-version") == 0) { kLinkerSplash(); - return EXIT_SUCCESS; + + return LIBCOMPILER_SUCCESS; } else if (StringCompare(argv[linker_arg], "-fat-binary") == 0) { kFatBinaryEnable = true; @@ -167,7 +165,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { continue; } - if (kOutput.find(kPefExt) != LibCompiler::String::npos) + if (kOutput.find(kPefExt) != LibCompiler::STLString::npos) kOutput.erase(kOutput.find(kPefExt), strlen(kPefExt)); kOutput += kPefDylibExt; @@ -184,7 +182,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { continue; } else { if (argv[linker_arg][0] == '-') { - kOutCon << "unknown flag: " << argv[linker_arg] << "\n"; + kConsoleOut << "unknown flag: " << argv[linker_arg] << "\n"; return EXIT_FAILURE; } @@ -195,10 +193,10 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { } if (kOutput.empty()) { - kOutCon << "no output filename set." << std::endl; + kConsoleOut << "no output filename set." << std::endl; return LIBCOMPILER_EXEC_ERROR; } else if (kObjectList.empty()) { - kOutCon << "no input files." << std::endl; + kConsoleOut << "no input files." << std::endl; return LIBCOMPILER_EXEC_ERROR; } else { namespace FS = std::filesystem; @@ -208,15 +206,15 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if (!FS::exists(obj)) { // if filesystem doesn't find file // -> throw error. - kOutCon << "no such file: " << obj << std::endl; + kConsoleOut << "no such file: " << obj << std::endl; return LIBCOMPILER_EXEC_ERROR; } } } // PEF expects a valid target architecture when outputing a binary. - if (kArch == 0) { - kOutCon << "no target architecture set, can't continue." << std::endl; + if (kArch == LibCompiler::kPefArchInvalid) { + kConsoleOut << "no target architecture set, can't continue." << std::endl; return LIBCOMPILER_EXEC_ERROR; } @@ -244,7 +242,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if (output_fc.bad()) { if (kVerbose) { - kOutCon << "error: " << strerror(errno) << "\n"; + kConsoleOut << "error: " << strerror(errno) << "\n"; } return LIBCOMPILER_FILE_NOT_FOUND; @@ -263,41 +261,39 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { reader_protocol.FP = std::ifstream(objectFile, std::ifstream::binary); reader_protocol.FP >> hdr; - auto ae_header = hdr; - - if (ae_header.fMagic[0] == kAEMag0 && ae_header.fMagic[1] == kAEMag1 && - ae_header.fSize == sizeof(LibCompiler::AEHeader)) { - if (ae_header.fArch != kArch) { - if (kVerbose) kOutCon << "Info: is this a FAT binary? : "; + if (hdr.fMagic[0] == kAEMag0 && hdr.fMagic[1] == kAEMag1 && + hdr.fSize == sizeof(LibCompiler::AEHeader)) { + if (hdr.fArch != kArch) { + if (kVerbose) kConsoleOut << "is this a FAT binary? : "; if (!kFatBinaryEnable) { - if (kVerbose) kOutCon << "No.\n"; + if (kVerbose) kConsoleOut << "not a FAT binary.\n"; - kOutCon << "Error: object " << objectFile - << " is a different kind of architecture and output isn't " - "treated as a FAT binary." - << std::endl; + kConsoleOut << "object " << objectFile + << " is a different kind of architecture and output isn't " + "treated as a FAT binary." + << std::endl; return LIBCOMPILER_FAT_ERROR; } else { if (kVerbose) { - kOutCon << "Architecture matches what we expect.\n"; + kConsoleOut << "Architecture matches what we expect.\n"; } } } // append arch type to archs varaible. - archs |= ae_header.fArch; - std::size_t cnt = ae_header.fCount; + archs |= hdr.fArch; + std::size_t cnt = hdr.fCount; - if (kVerbose) kOutCon << "Object header found, record count: " << cnt << "\n"; + if (kVerbose) kConsoleOut << "Object header found, record count: " << cnt << "\n"; pef_container.Count = cnt; char_type* raw_ae_records = new char_type[cnt * sizeof(LibCompiler::AERecordHeader)]; if (!raw_ae_records) { - if (kVerbose) kOutCon << "Allocation failure for records of count: " << cnt << "\n"; + if (kVerbose) kConsoleOut << "Allocation failure for records of count: " << cnt << "\n"; } memset(raw_ae_records, 0, cnt * sizeof(LibCompiler::AERecordHeader)); @@ -312,15 +308,15 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { MemoryCopy(command_header.Name, ae_records[ae_record_index].fName, kPefNameLen); - LibCompiler::String cmd_hdr_name(command_header.Name); + LibCompiler::STLString cmd_hdr_name(command_header.Name); // check this header if it's any valid. - if (cmd_hdr_name.find(kPefCode64) == LibCompiler::String::npos && - cmd_hdr_name.find(kPefData64) == LibCompiler::String::npos && - cmd_hdr_name.find(kPefZero64) == LibCompiler::String::npos) { - if (cmd_hdr_name.find(kPefStart) == LibCompiler::String::npos && + if (cmd_hdr_name.find(kPefCode64) == LibCompiler::STLString::npos && + cmd_hdr_name.find(kPefData64) == LibCompiler::STLString::npos && + cmd_hdr_name.find(kPefZero64) == LibCompiler::STLString::npos) { + if (cmd_hdr_name.find(kPefStart) == LibCompiler::STLString::npos && *command_header.Name == 0) { - if (cmd_hdr_name.find(kLdDefineSymbol) != LibCompiler::String::npos) { + if (cmd_hdr_name.find(kLdDefineSymbol) != LibCompiler::STLString::npos) { goto ld_mark_header; } else { continue; @@ -328,8 +324,8 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { } } - if (cmd_hdr_name.find(kPefStart) != LibCompiler::String::npos && - cmd_hdr_name.find(kPefCode64) != LibCompiler::String::npos) { + if (cmd_hdr_name.find(kPefStart) != LibCompiler::STLString::npos && + cmd_hdr_name.find(kPefCode64) != LibCompiler::STLString::npos) { kStartFound = true; } @@ -337,16 +333,16 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { command_header.Offset = offset_of_obj; command_header.Kind = ae_records[ae_record_index].fKind; command_header.Size = ae_records[ae_record_index].fSize; - command_header.Cpu = ae_header.fArch; + command_header.Cpu = hdr.fArch; command_header.VMAddress = org; /// TODO: - command_header.SubCpu = ae_header.fSubArch; + command_header.SubCpu = hdr.fSubArch; org += command_header.Size; if (kVerbose) { - kOutCon << "Record: " << ae_records[ae_record_index].fName << " is marked.\n"; + kConsoleOut << "Record: " << ae_records[ae_record_index].fName << " is marked.\n"; - kOutCon << "Offset: " << command_header.Offset << "\n"; + kConsoleOut << "Offset: " << command_header.Offset << "\n"; } command_headers.emplace_back(command_header); @@ -356,12 +352,12 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { raw_ae_records = nullptr; std::vector<char> bytes; - bytes.resize(ae_header.fCodeSize); + bytes.resize(hdr.fCodeSize); - reader_protocol.FP.seekg(std::streamsize(ae_header.fStartCode)); - reader_protocol.FP.read(bytes.data(), std::streamsize(ae_header.fCodeSize)); + reader_protocol.FP.seekg(std::streamsize(hdr.fStartCode)); + reader_protocol.FP.read(bytes.data(), std::streamsize(hdr.fCodeSize)); - kObjectBytes.push_back({.mBlob = bytes, .mOffset = ae_header.fStartCode}); + kObjectBytes.push_back({.mBlob = bytes, .mOffset = hdr.fStartCode}); // Blob was written, close fp. @@ -370,7 +366,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { continue; } - kOutCon << "Not an container: " << objectFile << std::endl; + kConsoleOut << "Not an AE container: " << objectFile << std::endl; // don't continue, it is a fatal error. return LIBCOMPILER_EXEC_ERROR; } @@ -380,24 +376,26 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { output_fc << pef_container; if (kVerbose) { - kOutCon << "Wrote container to: " << output_fc.tellp() << ".\n"; + kConsoleOut << "Wrote container to: " << output_fc.tellp() << ".\n"; } output_fc.seekp(std::streamsize(pef_container.HdrSz)); - std::vector<LibCompiler::String> not_found; - std::vector<LibCompiler::String> symbols; + std::vector<LibCompiler::STLString> not_found; + std::vector<LibCompiler::STLString> symbols; // step 2: check for errors (multiple symbols, undefined ones) for (auto& command_hdr : command_headers) { // check if this symbol needs to be resolved. - if (LibCompiler::String(command_hdr.Name).find(kLdDefineSymbol) != LibCompiler::String::npos && - LibCompiler::String(command_hdr.Name).find(kLdDynamicSym) == LibCompiler::String::npos) { - if (kVerbose) kOutCon << "Found undefined symbol: " << command_hdr.Name << "\n"; - - if (auto it = - std::find(not_found.begin(), not_found.end(), LibCompiler::String(command_hdr.Name)); + if (LibCompiler::STLString(command_hdr.Name).find(kLdDefineSymbol) != + LibCompiler::STLString::npos && + LibCompiler::STLString(command_hdr.Name).find(kLdDynamicSym) == + LibCompiler::STLString::npos) { + if (kVerbose) kConsoleOut << "Found undefined symbol: " << command_hdr.Name << "\n"; + + if (auto it = std::find(not_found.begin(), not_found.end(), + LibCompiler::STLString(command_hdr.Name)); it == not_found.end()) { not_found.emplace_back(command_hdr.Name); } @@ -410,26 +408,27 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { for (size_t not_found_idx = 0; not_found_idx < command_headers.size(); ++not_found_idx) { if (const auto it = std::find(not_found.begin(), not_found.end(), - LibCompiler::String(command_headers[not_found_idx].Name)); + LibCompiler::STLString(command_headers[not_found_idx].Name)); it != not_found.end()) { - LibCompiler::String symbol_imp = *it; + LibCompiler::STLString symbol_imp = *it; - if (symbol_imp.find(kLdDefineSymbol) == LibCompiler::String::npos) continue; + if (symbol_imp.find(kLdDefineSymbol) == LibCompiler::STLString::npos) continue; // erase the lookup prefix. symbol_imp.erase(0, symbol_imp.find(kLdDefineSymbol) + strlen(kLdDefineSymbol)); // demangle everything. - while (symbol_imp.find('$') != LibCompiler::String::npos) + while (symbol_imp.find('$') != LibCompiler::STLString::npos) symbol_imp.erase(symbol_imp.find('$'), 1); // the reason we do is because, this may not match the symbol, and we need // to look for other matching symbols. for (auto& command_hdr : command_headers) { - if (LibCompiler::String(command_hdr.Name).find(symbol_imp) != LibCompiler::String::npos && - LibCompiler::String(command_hdr.Name).find(kLdDefineSymbol) == - LibCompiler::String::npos) { - LibCompiler::String undefined_symbol = command_hdr.Name; + if (LibCompiler::STLString(command_hdr.Name).find(symbol_imp) != + LibCompiler::STLString::npos && + LibCompiler::STLString(command_hdr.Name).find(kLdDefineSymbol) == + LibCompiler::STLString::npos) { + LibCompiler::STLString undefined_symbol = command_hdr.Name; auto result_of_sym = undefined_symbol.substr(undefined_symbol.find(symbol_imp)); for (int i = 0; result_of_sym[i] != 0; ++i) { @@ -438,7 +437,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { not_found.erase(it); - if (kVerbose) kOutCon << "Found symbol: " << command_hdr.Name << "\n"; + if (kVerbose) kConsoleOut << "Found symbol: " << command_hdr.Name << "\n"; break; } @@ -453,11 +452,11 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if (!kStartFound && is_executable) { if (kVerbose) - kOutCon << "Undefined entrypoint: " << kPefStart - << ", you may have forget to link " - "against the C++ runtime library.\n"; + kConsoleOut << "Undefined entrypoint: " << kPefStart + << ", you may have forget to link " + "against the C++ runtime library.\n"; - kOutCon << "Undefined entrypoint " << kPefStart << " for executable: " << kOutput << "\n"; + kConsoleOut << "Undefined entrypoint " << kPefStart << " for executable: " << kOutput << "\n"; } // step 4: write all PEF commands. @@ -466,7 +465,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { time_t timestamp = time(nullptr); - LibCompiler::String timeStampStr = "Container:BuildEpoch:"; + LibCompiler::STLString timeStampStr = "Container:BuildEpoch:"; timeStampStr += std::to_string(timestamp); strncpy(date_cmd_hdr.Name, timeStampStr.c_str(), timeStampStr.size()); @@ -480,7 +479,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { LibCompiler::PEFCommandHeader abi_cmd_hdr{}; - LibCompiler::String abi = kLinkerAbiContainer; + LibCompiler::STLString abi = kLinkerAbiContainer; switch (kArch) { case LibCompiler::kPefArchAMD64: { @@ -546,9 +545,9 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { command_headers.push_back(uuid_cmd_hdr); // prepare a symbol vector. - std::vector<LibCompiler::String> undef_symbols; - std::vector<LibCompiler::String> dupl_symbols; - std::vector<LibCompiler::String> resolve_symbols; + std::vector<LibCompiler::STLString> undef_symbols; + std::vector<LibCompiler::STLString> dupl_symbols; + std::vector<LibCompiler::STLString> resolve_symbols; constexpr Int32 kPaddingOffset = 16; @@ -571,15 +570,15 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { // And check for any duplications for (size_t commandHeaderIndex = 0UL; commandHeaderIndex < command_headers.size(); ++commandHeaderIndex) { - if (LibCompiler::String(command_headers[commandHeaderIndex].Name).find(kLdDefineSymbol) != - LibCompiler::String::npos && - LibCompiler::String(command_headers[commandHeaderIndex].Name).find(kLdDynamicSym) == - LibCompiler::String::npos) { + if (LibCompiler::STLString(command_headers[commandHeaderIndex].Name).find(kLdDefineSymbol) != + LibCompiler::STLString::npos && + LibCompiler::STLString(command_headers[commandHeaderIndex].Name).find(kLdDynamicSym) == + LibCompiler::STLString::npos) { // ignore :UndefinedSymbol: headers, they do not contain code. continue; } - LibCompiler::String symbol_name = command_headers[commandHeaderIndex].Name; + LibCompiler::STLString symbol_name = command_headers[commandHeaderIndex].Name; if (!symbol_name.empty()) { undef_symbols.emplace_back(symbol_name); @@ -588,13 +587,13 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { command_headers[commandHeaderIndex].Offset += previous_offset; previous_offset += command_headers[commandHeaderIndex].Size; - LibCompiler::String name = command_headers[commandHeaderIndex].Name; + LibCompiler::STLString name = command_headers[commandHeaderIndex].Name; /// so this is valid when we get to the entrypoint. /// it is always a code64 container. And should equal to kPefStart as well. /// this chunk of code updates the pef_container.Start with the updated offset. - if (name.find(kPefStart) != LibCompiler::String::npos && - name.find(kPefCode64) != LibCompiler::String::npos) { + if (name.find(kPefStart) != LibCompiler::STLString::npos && + name.find(kPefCode64) != LibCompiler::STLString::npos) { pef_container.Start = command_headers[commandHeaderIndex].Offset; auto tellCurPos = output_fc.tellp(); @@ -605,9 +604,9 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { } if (kVerbose) { - kOutCon << "Command name: " << name << "\n"; - kOutCon << "VMAddress of command content: " << command_headers[commandHeaderIndex].Offset - << "\n"; + kConsoleOut << "Command name: " << name << "\n"; + kConsoleOut << "VMAddress of command content: " << command_headers[commandHeaderIndex].Offset + << "\n"; } output_fc << command_headers[commandHeaderIndex]; @@ -616,12 +615,12 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { ++sub_command_header_index) { if (sub_command_header_index == commandHeaderIndex) continue; - if (LibCompiler::String(command_headers[sub_command_header_index].Name) - .find(kLdDefineSymbol) != LibCompiler::String::npos && - LibCompiler::String(command_headers[sub_command_header_index].Name).find(kLdDynamicSym) == - LibCompiler::String::npos) { + if (LibCompiler::STLString(command_headers[sub_command_header_index].Name) + .find(kLdDefineSymbol) != LibCompiler::STLString::npos && + LibCompiler::STLString(command_headers[sub_command_header_index].Name) + .find(kLdDynamicSym) == LibCompiler::STLString::npos) { if (kVerbose) { - kOutCon << "Ignoring :UndefinedSymbol: headers...\n"; + kConsoleOut << "Ignoring :UndefinedSymbol: headers...\n"; } // ignore :UndefinedSymbol: headers, they do not contain code. @@ -630,13 +629,13 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { auto& command_hdr = command_headers[sub_command_header_index]; - if (command_hdr.Name == LibCompiler::String(command_headers[commandHeaderIndex].Name)) { + if (command_hdr.Name == LibCompiler::STLString(command_headers[commandHeaderIndex].Name)) { if (std::find(dupl_symbols.cbegin(), dupl_symbols.cend(), command_hdr.Name) == dupl_symbols.cend()) { dupl_symbols.emplace_back(command_hdr.Name); } - if (kVerbose) kOutCon << "Found duplicate symbols of: " << command_hdr.Name << "\n"; + if (kVerbose) kConsoleOut << "Found duplicate symbols of: " << command_hdr.Name << "\n"; kDuplicateSymbols = true; } @@ -645,7 +644,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if (!dupl_symbols.empty()) { for (auto& symbol : dupl_symbols) { - kOutCon << "Multiple symbols of: " << symbol << " detected, cannot continue.\n"; + kConsoleOut << "Multiple symbols of: " << symbol << " detected, cannot continue.\n"; } return LIBCOMPILER_EXEC_ERROR; @@ -658,16 +657,16 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { } if (kVerbose) { - kOutCon << "Wrote contents of: " << kOutput << "\n"; + kConsoleOut << "Wrote contents of: " << kOutput << "\n"; } // step 3: check if we have those symbols - std::vector<LibCompiler::String> unreferenced_symbols; + std::vector<LibCompiler::STLString> unreferenced_symbols; for (auto& command_hdr : command_headers) { if (auto it = - std::find(not_found.begin(), not_found.end(), LibCompiler::String(command_hdr.Name)); + std::find(not_found.begin(), not_found.end(), LibCompiler::STLString(command_hdr.Name)); it != not_found.end()) { unreferenced_symbols.emplace_back(command_hdr.Name); } @@ -675,7 +674,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if (!unreferenced_symbols.empty()) { for (auto& unreferenced_symbol : unreferenced_symbols) { - kOutCon << "Undefined symbol " << unreferenced_symbol << "\n"; + kConsoleOut << "Undefined symbol " << unreferenced_symbol << "\n"; } return LIBCOMPILER_EXEC_ERROR; @@ -684,7 +683,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if ((!kStartFound || kDuplicateSymbols) && (std::filesystem::exists(kOutput) || !unreferenced_symbols.empty())) { if (kVerbose) { - kOutCon << "File: " << kOutput << ", is corrupt, removing file...\n"; + kConsoleOut << "File: " << kOutput << ", is corrupt, removing file...\n"; } return LIBCOMPILER_EXEC_ERROR; diff --git a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc b/dev/LibCompiler/src/Macro/CPlusPlusCompilerPreProcessor.cc index 5d035da..2c7c551 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc +++ b/dev/LibCompiler/src/Macro/CPlusPlusCompilerPreProcessor.cc @@ -10,7 +10,7 @@ /// BUGS: 0 #include <LibCompiler/ErrorID.h> -#include <LibCompiler/Parser.h> +#include <LibCompiler/Frontend.h> #include <algorithm> #include <filesystem> #include <fstream> @@ -24,7 +24,7 @@ /// @file bpp.cxx /// @brief Preprocessor. -typedef Int32 (*bpp_parser_fn_t)(LibCompiler::String& line, std::ifstream& hdr_file, +typedef Int32 (*bpp_parser_fn_t)(LibCompiler::STLString& line, std::ifstream& hdr_file, std::ofstream& pp_out); ///////////////////////////////////////////////////////////////////////////////////////// @@ -46,8 +46,8 @@ enum { }; struct bpp_macro_condition final { - int32_t fType; - LibCompiler::String fTypeName; + int32_t fType; + LibCompiler::STLString fTypeName; void Print() { std::cout << "type: " << fType << "\n"; @@ -56,9 +56,9 @@ struct bpp_macro_condition final { }; struct bpp_macro final { - std::vector<LibCompiler::String> fArgs; - LibCompiler::String fName; - LibCompiler::String fValue; + std::vector<LibCompiler::STLString> fArgs; + LibCompiler::STLString fName; + LibCompiler::STLString fValue; void Print() { std::cout << "name: " << fName << "\n"; @@ -71,11 +71,11 @@ struct bpp_macro final { }; } // namespace Detail -static std::vector<LibCompiler::String> kFiles; -static std::vector<Detail::bpp_macro> kMacros; -static std::vector<LibCompiler::String> kIncludes; +static std::vector<LibCompiler::STLString> kFiles; +static std::vector<Detail::bpp_macro> kMacros; +static std::vector<LibCompiler::STLString> kIncludes; -static LibCompiler::String kWorkingDir = ""; +static LibCompiler::STLString kWorkingDir = ""; ///////////////////////////////////////////////////////////////////////////////////////// @@ -85,11 +85,12 @@ static LibCompiler::String kWorkingDir = ""; ///////////////////////////////////////////////////////////////////////////////////////// int32_t bpp_parse_if_condition(Detail::bpp_macro_condition& cond, Detail::bpp_macro& macro, - bool& inactive_code, bool& defined, LibCompiler::String& macro_str) { + bool& inactive_code, bool& defined, + LibCompiler::STLString& macro_str) { if (cond.fType == Detail::kEqual) { auto substr_macro = macro_str.substr(macro_str.find(macro.fName) + macro.fName.size()); - if (substr_macro.find(macro.fValue) != LibCompiler::String::npos) { + if (substr_macro.find(macro.fValue) != LibCompiler::STLString::npos) { if (macro.fValue == "0") { defined = false; inactive_code = true; @@ -105,8 +106,8 @@ int32_t bpp_parse_if_condition(Detail::bpp_macro_condition& cond, Detail::bpp_ma } else if (cond.fType == Detail::kNotEqual) { auto substr_macro = macro_str.substr(macro_str.find(macro.fName) + macro.fName.size()); - if (substr_macro.find(macro.fName) != LibCompiler::String::npos) { - if (substr_macro.find(macro.fValue) != LibCompiler::String::npos) { + if (substr_macro.find(macro.fName) != LibCompiler::STLString::npos) { + if (substr_macro.find(macro.fValue) != LibCompiler::STLString::npos) { defined = false; inactive_code = true; @@ -124,10 +125,10 @@ int32_t bpp_parse_if_condition(Detail::bpp_macro_condition& cond, Detail::bpp_ma auto substr_macro = macro_str.substr(macro_str.find(macro.fName) + macro.fName.size()); - LibCompiler::String number; + LibCompiler::STLString number; for (auto& macro_num : kMacros) { - if (substr_macro.find(macro_num.fName) != LibCompiler::String::npos) { + if (substr_macro.find(macro_num.fName) != LibCompiler::STLString::npos) { for (size_t i = 0; i < macro_num.fName.size(); ++i) { if (isdigit(macro_num.fValue[i])) { number += macro_num.fValue[i]; @@ -231,7 +232,7 @@ int32_t bpp_parse_if_condition(Detail::bpp_macro_condition& cond, Detail::bpp_ma ///////////////////////////////////////////////////////////////////////////////////////// -std::vector<LibCompiler::String> kAllIncludes; +std::vector<LibCompiler::STLString> kAllIncludes; ///////////////////////////////////////////////////////////////////////////////////////// @@ -241,8 +242,8 @@ std::vector<LibCompiler::String> kAllIncludes; ///////////////////////////////////////////////////////////////////////////////////////// void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { - LibCompiler::String hdr_line; - LibCompiler::String line_after_include; + LibCompiler::STLString hdr_line; + LibCompiler::STLString line_after_include; bool inactive_code = false; bool defined = false; @@ -250,36 +251,36 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { try { while (std::getline(hdr_file, hdr_line)) { if (inactive_code) { - if (hdr_line.find("#endif") == LibCompiler::String::npos) { + if (hdr_line.find("#endif") == LibCompiler::STLString::npos) { continue; } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("#endif") != LibCompiler::String::npos) { + hdr_line.find("#endif") != LibCompiler::STLString::npos) { inactive_code = false; } - if (hdr_line.find("*/") != LibCompiler::String::npos) { + if (hdr_line.find("*/") != LibCompiler::STLString::npos) { continue; } } - if (hdr_line.find("--/") != LibCompiler::String::npos) { + if (hdr_line.find("--/") != LibCompiler::STLString::npos) { hdr_line.erase(hdr_line.find("--/")); } - if (hdr_line.find("--*") != LibCompiler::String::npos) { + if (hdr_line.find("--*") != LibCompiler::STLString::npos) { inactive_code = true; // get rid of comment. hdr_line.erase(hdr_line.find("--*")); } /// BPP 'brief' documentation. - if (hdr_line.find("@brief") != LibCompiler::String::npos) { + if (hdr_line.find("@brief") != LibCompiler::STLString::npos) { hdr_line.erase(hdr_line.find("@brief")); // TODO: Write an <file_name>.html or append to it. } - if (hdr_line[0] == kMacroPrefix && hdr_line.find("endif") != LibCompiler::String::npos) { + if (hdr_line[0] == kMacroPrefix && hdr_line.find("endif") != LibCompiler::STLString::npos) { if (!defined && inactive_code) { inactive_code = false; defined = false; @@ -301,17 +302,17 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { for (auto macro : kMacros) { if (LibCompiler::find_word(hdr_line, macro.fName)) { if (hdr_line.substr(hdr_line.find(macro.fName)).find(macro.fName + '(') != - LibCompiler::String::npos) { + LibCompiler::STLString::npos) { if (!macro.fArgs.empty()) { - LibCompiler::String symbol_val = macro.fValue; - std::vector<LibCompiler::String> args; + LibCompiler::STLString symbol_val = macro.fValue; + std::vector<LibCompiler::STLString> args; size_t x_arg_indx = 0; - LibCompiler::String line_after_define = hdr_line; - LibCompiler::String str_arg; + LibCompiler::STLString line_after_define = hdr_line; + LibCompiler::STLString str_arg; - if (line_after_define.find("(") != LibCompiler::String::npos) { + if (line_after_define.find("(") != LibCompiler::STLString::npos) { line_after_define.erase(0, line_after_define.find("(") + 1); for (auto& subc : line_after_define) { @@ -332,7 +333,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { } for (auto arg : macro.fArgs) { - if (symbol_val.find(macro.fArgs[x_arg_indx]) != LibCompiler::String::npos) { + if (symbol_val.find(macro.fArgs[x_arg_indx]) != LibCompiler::STLString::npos) { symbol_val.replace(symbol_val.find(macro.fArgs[x_arg_indx]), macro.fArgs[x_arg_indx].size(), args[x_arg_indx]); ++x_arg_indx; @@ -358,16 +359,16 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { } } - if (hdr_line[0] == kMacroPrefix && hdr_line.find("define ") != LibCompiler::String::npos) { + if (hdr_line[0] == kMacroPrefix && hdr_line.find("define ") != LibCompiler::STLString::npos) { auto line_after_define = hdr_line.substr(hdr_line.find("define ") + strlen("define ")); - LibCompiler::String macro_value; - LibCompiler::String macro_key; + LibCompiler::STLString macro_value; + LibCompiler::STLString macro_key; std::size_t pos = 0UL; - std::vector<LibCompiler::String> args; - bool on_args = false; + std::vector<LibCompiler::STLString> args; + bool on_args = false; for (auto& ch : line_after_define) { ++pos; @@ -397,9 +398,9 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { macro_key += ch; } - LibCompiler::String str; + LibCompiler::STLString str; - if (line_after_define.find("(") != LibCompiler::String::npos) { + if (line_after_define.find("(") != LibCompiler::STLString::npos) { line_after_define.erase(0, line_after_define.find("(") + 1); for (auto& subc : line_after_define) { @@ -438,9 +439,9 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { continue; } - if (hdr_line[0] == kMacroPrefix && hdr_line.find("ifndef") != LibCompiler::String::npos) { + if (hdr_line[0] == kMacroPrefix && hdr_line.find("ifndef") != LibCompiler::STLString::npos) { auto line_after_ifndef = hdr_line.substr(hdr_line.find("ifndef") + strlen("ifndef") + 1); - LibCompiler::String macro; + LibCompiler::STLString macro; for (auto& ch : line_after_ifndef) { if (ch == ' ') { @@ -469,7 +470,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { inactive_code = false; for (auto& macro_ref : kMacros) { - if (hdr_line.find(macro_ref.fName) != LibCompiler::String::npos) { + if (hdr_line.find(macro_ref.fName) != LibCompiler::STLString::npos) { found = true; break; } @@ -482,7 +483,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { continue; } } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("else") != LibCompiler::String::npos) { + hdr_line.find("else") != LibCompiler::STLString::npos) { if (!defined && inactive_code) { inactive_code = false; defined = true; @@ -495,9 +496,9 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { continue; } } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("ifdef") != LibCompiler::String::npos) { + hdr_line.find("ifdef") != LibCompiler::STLString::npos) { auto line_after_ifdef = hdr_line.substr(hdr_line.find("ifdef") + strlen("ifdef") + 1); - LibCompiler::String macro; + LibCompiler::STLString macro; for (auto& ch : line_after_ifdef) { if (ch == ' ') { @@ -525,14 +526,15 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { inactive_code = true; for (auto& macro_ref : kMacros) { - if (hdr_line.find(macro_ref.fName) != LibCompiler::String::npos) { + if (hdr_line.find(macro_ref.fName) != LibCompiler::STLString::npos) { defined = true; inactive_code = false; break; } } - } else if (hdr_line[0] == kMacroPrefix && hdr_line.find("if") != LibCompiler::String::npos) { + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("if") != LibCompiler::STLString::npos) { inactive_code = true; std::vector<Detail::bpp_macro_condition> bpp_macro_condition_list = { @@ -565,9 +567,9 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { int32_t good_to_go = 0; for (auto& macro_condition : bpp_macro_condition_list) { - if (hdr_line.find(macro_condition.fTypeName) != LibCompiler::String::npos) { + if (hdr_line.find(macro_condition.fTypeName) != LibCompiler::STLString::npos) { for (auto& found_macro : kMacros) { - if (hdr_line.find(found_macro.fName) != LibCompiler::String::npos) { + if (hdr_line.find(found_macro.fName) != LibCompiler::STLString::npos) { good_to_go = bpp_parse_if_condition(macro_condition, found_macro, inactive_code, defined, hdr_line); @@ -579,8 +581,8 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { if (good_to_go) continue; - auto line_after_if = hdr_line.substr(hdr_line.find("if") + strlen("if") + 1); - LibCompiler::String macro; + auto line_after_if = hdr_line.substr(hdr_line.find("if") + strlen("if") + 1); + LibCompiler::STLString macro; for (auto& ch : line_after_if) { if (ch == ' ') { @@ -605,7 +607,8 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { // last try, is it defined to be one? for (auto& macro_ref : kMacros) { - if (macro_ref.fName.find(macro) != LibCompiler::String::npos && macro_ref.fValue == "1") { + if (macro_ref.fName.find(macro) != LibCompiler::STLString::npos && + macro_ref.fValue == "1") { inactive_code = false; defined = true; @@ -613,9 +616,9 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { } } } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("warning") != LibCompiler::String::npos) { + hdr_line.find("warning") != LibCompiler::STLString::npos) { auto line_after_warning = hdr_line.substr(hdr_line.find("warning") + strlen("warning") + 1); - LibCompiler::String message; + LibCompiler::STLString message; for (auto& ch : line_after_warning) { if (ch == '\r' || ch == '\n') { @@ -627,9 +630,9 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { std::cout << "warn: " << message << std::endl; } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("error") != LibCompiler::String::npos) { + hdr_line.find("error") != LibCompiler::STLString::npos) { auto line_after_warning = hdr_line.substr(hdr_line.find("error") + strlen("error") + 1); - LibCompiler::String message; + LibCompiler::STLString message; for (auto& ch : line_after_warning) { if (ch == '\r' || ch == '\n') { @@ -641,7 +644,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { throw std::runtime_error("error: " + message); } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("include ") != LibCompiler::String::npos) { + hdr_line.find("include ") != LibCompiler::STLString::npos) { line_after_include = hdr_line.substr(hdr_line.find("include ") + strlen("include ")); kIncludeFile: @@ -651,7 +654,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { continue; } - LibCompiler::String path; + LibCompiler::STLString path; kAllIncludes.push_back(line_after_include); @@ -691,7 +694,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { } for (auto& include : kIncludes) { - LibCompiler::String header_path = include; + LibCompiler::STLString header_path = include; header_path.push_back('/'); header_path += path; @@ -830,7 +833,7 @@ LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { } if (strcmp(argv[index], "--cppdrv:include-dir") == 0) { - LibCompiler::String inc = argv[index + 1]; + LibCompiler::STLString inc = argv[index + 1]; skip = true; @@ -838,17 +841,17 @@ LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { } if (strcmp(argv[index], "--cppdrv:working-dir") == 0) { - LibCompiler::String inc = argv[index + 1]; - skip = true; - kWorkingDir = inc; + LibCompiler::STLString inc = argv[index + 1]; + skip = true; + kWorkingDir = inc; } if (strcmp(argv[index], "--cppdrv:def") == 0 && argv[index + 1] != nullptr && argv[index + 2] != nullptr) { - LibCompiler::String macro_key = argv[index + 1]; + LibCompiler::STLString macro_key = argv[index + 1]; - LibCompiler::String macro_value; - bool is_string = false; + LibCompiler::STLString macro_value; + bool is_string = false; for (int argv_find_len = 0; argv_find_len < strlen(argv[index]); ++argv_find_len) { if (!isdigit(argv[index][argv_find_len])) { |
