From 1c8c5cff67b20d86c442b0917d6c1fc6407140df Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 30 Jul 2025 08:50:15 +0100 Subject: feat! Breaking API changes of NeCTI's LibCompiler and LibDebugger. what: - They've now become CompilerKit and DebuggerKit. - Expanding XCoff for NeBoot PowerPC backend. Signed-off-by: Amlal El Mahrouss --- dev/CompilerKit/utils/CompilerUtils.h | 116 ++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 dev/CompilerKit/utils/CompilerUtils.h (limited to 'dev/CompilerKit/utils/CompilerUtils.h') diff --git a/dev/CompilerKit/utils/CompilerUtils.h b/dev/CompilerKit/utils/CompilerUtils.h new file mode 100644 index 0000000..da35b2b --- /dev/null +++ b/dev/CompilerKit/utils/CompilerUtils.h @@ -0,0 +1,116 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#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 { +/// @brief Linker specific blob metadata structure +struct DynamicLinkerBlob final { + std::vector mBlob{}; // PEF code/bss/data blob. + UIntPtr mOffset{0UL}; // the offset of the PEF container header... +}; + +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) { + CompilerKit::STLString verbose_header = "LIBCOMPILER CRASH REPORT - "; + verbose_header += kDistVersion; + verbose_header += " - "; + verbose_header += CompilerKit::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: " << CompilerKit::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 -- cgit v1.2.3 From b0c5f2a2683657182f5ce3dc3bcbf252164b0077 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 5 Aug 2025 09:47:53 +0100 Subject: feat! compiler_kit & debugger_kit: breaking changes, big name refactors on the source code have been done. Signed-off-by: Amlal El Mahrouss --- dev/CompilerKit/AE.h | 2 +- dev/CompilerKit/BasicString.h | 2 +- dev/CompilerKit/CodeGen.h | 16 ++++++------- dev/CompilerKit/Defines.h | 16 ++++++------- dev/CompilerKit/ErrorID.h | 20 ++++++++-------- dev/CompilerKit/ErrorOr.h | 2 +- dev/CompilerKit/Frontend.h | 2 +- dev/CompilerKit/Macros.h | 14 +++++------ dev/CompilerKit/Ref.h | 2 +- dev/CompilerKit/lc-osx-san.json | 2 +- dev/CompilerKit/lc-osx.json | 2 +- dev/CompilerKit/lc-posix.json | 2 +- dev/CompilerKit/src/Backend/Assembler32x0.cc | 2 +- dev/CompilerKit/src/Backend/Assembler64x0.cc | 2 +- dev/CompilerKit/src/Backend/AssemblerAMD64.cc | 2 +- dev/CompilerKit/src/Backend/AssemblerARM64.cc | 4 ++-- dev/CompilerKit/src/Backend/AssemblerPowerPC.cc | 4 ++-- dev/CompilerKit/src/CodeGen.cc | 8 +++---- dev/CompilerKit/src/Frontend/CCompiler64x0.cc | 8 +++---- dev/CompilerKit/src/Frontend/CCompilerARM64.cc | 8 +++---- dev/CompilerKit/src/Frontend/CCompilerPower64.cc | 8 +++---- .../src/Frontend/CPlusPlusCompilerAMD64.cc | 28 +++++++++++----------- dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc | 28 +++++++++++----------- .../src/Macro/CPlusPlusCompilerPreProcessor.cc | 14 +++++------ dev/CompilerKit/utils/CompilerUtils.h | 4 ++-- dev/DebuggerKit/ld-nekernel.json | 2 +- dev/DebuggerKit/ld-osx.json | 2 +- dev/DebuggerKit/src/NeKernelContractCLI.cc | 2 +- dev/DebuggerKit/src/POSIXMachContractCLI.cc | 2 +- dev/LibC++/__power64.inc | 2 +- dev/LibC++/defines.h | 8 +++---- dev/LibC++/filesystem.h | 6 ++--- tools/cppdrv.cc | 4 ++-- tools/pef-amd64-cxxdrv.cc | 2 +- tools/pef-arm64-cdrv.cc | 2 +- 35 files changed, 117 insertions(+), 117 deletions(-) (limited to 'dev/CompilerKit/utils/CompilerUtils.h') diff --git a/dev/CompilerKit/AE.h b/dev/CompilerKit/AE.h index 1115e0a..6561baf 100644 --- a/dev/CompilerKit/AE.h +++ b/dev/CompilerKit/AE.h @@ -97,7 +97,7 @@ class AEReadableProtocol final { explicit AEReadableProtocol() = default; ~AEReadableProtocol() = default; - LIBCOMPILER_COPY_DELETE(AEReadableProtocol); + NECTI_COPY_DELETE(AEReadableProtocol); /** * @brief Read AE Record headers. diff --git a/dev/CompilerKit/BasicString.h b/dev/CompilerKit/BasicString.h index a1ada68..c4cb55a 100644 --- a/dev/CompilerKit/BasicString.h +++ b/dev/CompilerKit/BasicString.h @@ -39,7 +39,7 @@ class BasicString final { } } - LIBCOMPILER_COPY_DEFAULT(BasicString); + NECTI_COPY_DEFAULT(BasicString); Char* Data(); const Char* CData() const; diff --git a/dev/CompilerKit/CodeGen.h b/dev/CompilerKit/CodeGen.h index 9a3077b..1439c27 100644 --- a/dev/CompilerKit/CodeGen.h +++ b/dev/CompilerKit/CodeGen.h @@ -23,7 +23,7 @@ class AssemblyFactory final { explicit AssemblyFactory() = default; ~AssemblyFactory() = default; - LIBCOMPILER_COPY_DEFAULT(AssemblyFactory); + NECTI_COPY_DEFAULT(AssemblyFactory); public: enum { @@ -54,7 +54,7 @@ class AssemblyInterface { explicit AssemblyInterface() = default; virtual ~AssemblyInterface() = default; - LIBCOMPILER_COPY_DEFAULT(AssemblyInterface); + NECTI_COPY_DEFAULT(AssemblyInterface); virtual UInt32 Arch() noexcept { return AssemblyFactory::kArchAMD64; } @@ -113,7 +113,7 @@ class EncoderInterface { explicit EncoderInterface() = default; virtual ~EncoderInterface() = default; - LIBCOMPILER_COPY_DEFAULT(EncoderInterface); + NECTI_COPY_DEFAULT(EncoderInterface); virtual std::string CheckLine(std::string line, std::string file) = 0; virtual bool WriteLine(std::string line, std::string file) = 0; @@ -127,7 +127,7 @@ class EncoderAMD64 final : public EncoderInterface { explicit EncoderAMD64() = default; ~EncoderAMD64() override = default; - LIBCOMPILER_COPY_DEFAULT(EncoderAMD64); + NECTI_COPY_DEFAULT(EncoderAMD64); virtual std::string CheckLine(std::string line, std::string file) override; virtual bool WriteLine(std::string line, std::string file) override; @@ -147,7 +147,7 @@ class EncoderARM64 final : public EncoderInterface { explicit EncoderARM64() = default; ~EncoderARM64() override = default; - LIBCOMPILER_COPY_DEFAULT(EncoderARM64); + NECTI_COPY_DEFAULT(EncoderARM64); virtual std::string CheckLine(std::string line, std::string file) override; virtual bool WriteLine(std::string line, std::string file) override; @@ -163,7 +163,7 @@ class Encoder64x0 final : public EncoderInterface { explicit Encoder64x0() = default; ~Encoder64x0() override = default; - LIBCOMPILER_COPY_DEFAULT(Encoder64x0); + NECTI_COPY_DEFAULT(Encoder64x0); virtual std::string CheckLine(std::string line, std::string file) override; virtual bool WriteLine(std::string line, std::string file) override; @@ -179,7 +179,7 @@ class Encoder32x0 final : public EncoderInterface { explicit Encoder32x0() = default; ~Encoder32x0() override = default; - LIBCOMPILER_COPY_DEFAULT(Encoder32x0); + NECTI_COPY_DEFAULT(Encoder32x0); virtual std::string CheckLine(std::string line, std::string file) override; virtual bool WriteLine(std::string line, std::string file) override; @@ -195,7 +195,7 @@ class EncoderPowerPC final : public EncoderInterface { explicit EncoderPowerPC() = default; ~EncoderPowerPC() override = default; - LIBCOMPILER_COPY_DEFAULT(EncoderPowerPC); + NECTI_COPY_DEFAULT(EncoderPowerPC); virtual std::string CheckLine(std::string line, std::string file) override; virtual bool WriteLine(std::string line, std::string file) override; diff --git a/dev/CompilerKit/Defines.h b/dev/CompilerKit/Defines.h index c3d40ac..e3203ba 100644 --- a/dev/CompilerKit/Defines.h +++ b/dev/CompilerKit/Defines.h @@ -4,8 +4,8 @@ ------------------------------------------- */ -#ifndef __LIBCOMPILER_DEFINES_H__ -#define __LIBCOMPILER_DEFINES_H__ +#ifndef __NECTI_DEFINES_H__ +#define __NECTI_DEFINES_H__ #ifndef Yes #define Yes true @@ -81,19 +81,19 @@ #define rt_copy_memory(dst, src, len) memcpy(dst, src, len) #endif -#define LIBCOMPILER_COPY_DELETE(KLASS) \ +#define NECTI_COPY_DELETE(KLASS) \ KLASS& operator=(const KLASS&) = delete; \ KLASS(const KLASS&) = delete; -#define LIBCOMPILER_COPY_DEFAULT(KLASS) \ +#define NECTI_COPY_DEFAULT(KLASS) \ KLASS& operator=(const KLASS&) = default; \ KLASS(const KLASS&) = default; -#define LIBCOMPILER_MOVE_DELETE(KLASS) \ +#define NECTI_MOVE_DELETE(KLASS) \ KLASS& operator=(KLASS&&) = delete; \ KLASS(KLASS&&) = delete; -#define LIBCOMPILER_MOVE_DEFAULT(KLASS) \ +#define NECTI_MOVE_DEFAULT(KLASS) \ KLASS& operator=(KLASS&&) = default; \ KLASS(KLASS&&) = default; @@ -162,10 +162,10 @@ typedef char char_type; #define kAsmFileExtsMax (7U) -#define LIBCOMPILER_MODULE(name) extern "C" int name(int argc, char** argv) +#define NECTI_MODULE(name) extern "C" int name(int argc, char** argv) #ifdef MSVC #pragma scalar_storage_order big - endian #endif // ifdef MSVC -#endif /* ifndef __LIBCOMPILER_DEFINES_H__ */ +#endif /* ifndef __NECTI_DEFINES_H__ */ diff --git a/dev/CompilerKit/ErrorID.h b/dev/CompilerKit/ErrorID.h index 22ca242..5b8d1e8 100644 --- a/dev/CompilerKit/ErrorID.h +++ b/dev/CompilerKit/ErrorID.h @@ -11,13 +11,13 @@ #include -#define LIBCOMPILER_SUCCESS 0 -#define LIBCOMPILER_EXEC_ERROR -30 -#define LIBCOMPILER_FILE_NOT_FOUND -31 -#define LIBCOMPILER_DIR_NOT_FOUND -32 -#define LIBCOMPILER_FILE_EXISTS -33 -#define LIBCOMPILER_TOO_LONG -34 -#define LIBCOMPILER_INVALID_DATA -35 -#define LIBCOMPILER_UNIMPLEMENTED -36 -#define LIBCOMPILER_FAT_ERROR -37 -#define LIBCOMPILER_INVALID_ARCH -38 +#define NECTI_SUCCESS 0 +#define NECTI_EXEC_ERROR -30 +#define NECTI_FILE_NOT_FOUND -31 +#define NECTI_DIR_NOT_FOUND -32 +#define NECTI_FILE_EXISTS -33 +#define NECTI_TOO_LONG -34 +#define NECTI_INVALID_DATA -35 +#define NECTI_UNIMPLEMENTED -36 +#define NECTI_FAT_ERROR -37 +#define NECTI_INVALID_ARCH -38 diff --git a/dev/CompilerKit/ErrorOr.h b/dev/CompilerKit/ErrorOr.h index 700da23..218bec5 100644 --- a/dev/CompilerKit/ErrorOr.h +++ b/dev/CompilerKit/ErrorOr.h @@ -36,7 +36,7 @@ class ErrorOr final { Int32 Error() { return mId; } - BOOL HasError() { return mId != LIBCOMPILER_SUCCESS; } + BOOL HasError() { return mId != NECTI_SUCCESS; } operator bool() { return mRef; } diff --git a/dev/CompilerKit/Frontend.h b/dev/CompilerKit/Frontend.h index 8af3066..168aa63 100644 --- a/dev/CompilerKit/Frontend.h +++ b/dev/CompilerKit/Frontend.h @@ -103,7 +103,7 @@ class CompilerFrontendInterface { explicit CompilerFrontendInterface() = default; virtual ~CompilerFrontendInterface() = default; - LIBCOMPILER_COPY_DEFAULT(CompilerFrontendInterface); + NECTI_COPY_DEFAULT(CompilerFrontendInterface); // NOTE: cast this to your user defined ast. typedef void* AstType; diff --git a/dev/CompilerKit/Macros.h b/dev/CompilerKit/Macros.h index 844bc23..ee42be5 100644 --- a/dev/CompilerKit/Macros.h +++ b/dev/CompilerKit/Macros.h @@ -9,25 +9,25 @@ #ifndef _MACROS_H_ #define _MACROS_H_ -#define LIBCOMPILER_COPY_DELETE(KLASS) \ +#define NECTI_COPY_DELETE(KLASS) \ KLASS& operator=(const KLASS&) = delete; \ KLASS(const KLASS&) = delete; -#define LIBCOMPILER_COPY_DEFAULT(KLASS) \ +#define NECTI_COPY_DEFAULT(KLASS) \ KLASS& operator=(const KLASS&) = default; \ KLASS(const KLASS&) = default; -#define LIBCOMPILER_MOVE_DELETE(KLASS) \ +#define NECTI_MOVE_DELETE(KLASS) \ KLASS& operator=(KLASS&&) = delete; \ KLASS(KLASS&&) = delete; -#define LIBCOMPILER_MOVE_DEFAULT(KLASS) \ +#define NECTI_MOVE_DEFAULT(KLASS) \ KLASS& operator=(KLASS&&) = default; \ KLASS(KLASS&&) = default; /// @note xxxx is the error placeholder, in hexadecimal. -#define LIBCOMPILER_ERROR_PREFIX_CXX "CXXxxxx" -#define LIBCOMPILER_ERROR_PREFIX_CL "CLxxxx" -#define LIBCOMPILER_ERROR_PREFIX_ASM "ASMxxxx" +#define NECTI_ERROR_PREFIX_CXX "CXXxxxx" +#define NECTI_ERROR_PREFIX_CL "CLxxxx" +#define NECTI_ERROR_PREFIX_ASM "ASMxxxx" #endif /* ifndef _MACROS_H_ */ diff --git a/dev/CompilerKit/Ref.h b/dev/CompilerKit/Ref.h index a3640ac..bd432e0 100644 --- a/dev/CompilerKit/Ref.h +++ b/dev/CompilerKit/Ref.h @@ -27,7 +27,7 @@ class Ref final { } } - LIBCOMPILER_COPY_DEFAULT(Ref); + NECTI_COPY_DEFAULT(Ref); public: explicit Ref(T* cls, const Bool& strong = false) : m_Class(cls), m_Strong(strong) {} diff --git a/dev/CompilerKit/lc-osx-san.json b/dev/CompilerKit/lc-osx-san.json index 244936d..5ec4209 100644 --- a/dev/CompilerKit/lc-osx-san.json +++ b/dev/CompilerKit/lc-osx-san.json @@ -22,7 +22,7 @@ "-fsanitize=undefined" ], "cpp_macros": [ - "__LIBCOMPILER__=202505", + "__NECTI__=202505", "LC_USE_STRUCTS=1", "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" ] diff --git a/dev/CompilerKit/lc-osx.json b/dev/CompilerKit/lc-osx.json index 3116045..e434ee8 100644 --- a/dev/CompilerKit/lc-osx.json +++ b/dev/CompilerKit/lc-osx.json @@ -17,7 +17,7 @@ "-shared" ], "cpp_macros": [ - "__LIBCOMPILER__=202505", + "__NECTI__=202505", "LC_USE_STRUCTS=1", "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" ] diff --git a/dev/CompilerKit/lc-posix.json b/dev/CompilerKit/lc-posix.json index 6e824d4..e8668b9 100644 --- a/dev/CompilerKit/lc-posix.json +++ b/dev/CompilerKit/lc-posix.json @@ -17,7 +17,7 @@ "-shared" ], "cpp_macros": [ - "__LIBCOMPILER__=202505", + "__NECTI__=202505", "LC_USE_STRUCTS=1", "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" ] diff --git a/dev/CompilerKit/src/Backend/Assembler32x0.cc b/dev/CompilerKit/src/Backend/Assembler32x0.cc index e298d2d..bc52d25 100644 --- a/dev/CompilerKit/src/Backend/Assembler32x0.cc +++ b/dev/CompilerKit/src/Backend/Assembler32x0.cc @@ -33,7 +33,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -LIBCOMPILER_MODULE(NEAssemblerMain32000) { +NECTI_MODULE(NEAssemblerMain32000) { CompilerKit::install_signal(SIGSEGV, Detail::drvi_crash_handler); return EXIT_SUCCESS; } diff --git a/dev/CompilerKit/src/Backend/Assembler64x0.cc b/dev/CompilerKit/src/Backend/Assembler64x0.cc index 32b199d..511c64d 100644 --- a/dev/CompilerKit/src/Backend/Assembler64x0.cc +++ b/dev/CompilerKit/src/Backend/Assembler64x0.cc @@ -67,7 +67,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// -LIBCOMPILER_MODULE(AssemblerMain64x0) { +NECTI_MODULE(AssemblerMain64x0) { CompilerKit::install_signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { diff --git a/dev/CompilerKit/src/Backend/AssemblerAMD64.cc b/dev/CompilerKit/src/Backend/AssemblerAMD64.cc index 6542d6e..b085773 100644 --- a/dev/CompilerKit/src/Backend/AssemblerAMD64.cc +++ b/dev/CompilerKit/src/Backend/AssemblerAMD64.cc @@ -84,7 +84,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// -LIBCOMPILER_MODULE(AssemblerMainAMD64) { +NECTI_MODULE(AssemblerMainAMD64) { //////////////// CPU OPCODES BEGIN //////////////// CompilerKit::install_signal(SIGSEGV, Detail::drvi_crash_handler); diff --git a/dev/CompilerKit/src/Backend/AssemblerARM64.cc b/dev/CompilerKit/src/Backend/AssemblerARM64.cc index d290e24..6890b2c 100644 --- a/dev/CompilerKit/src/Backend/AssemblerARM64.cc +++ b/dev/CompilerKit/src/Backend/AssemblerARM64.cc @@ -72,7 +72,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// -LIBCOMPILER_MODULE(AssemblerMainARM64) { +NECTI_MODULE(AssemblerMainARM64) { CompilerKit::install_signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { @@ -258,7 +258,7 @@ asm_fail_exit: if (kVerbose) kStdOut << "AssemblerARM64: Exit failed.\n"; - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc b/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc index a04a52b..b02a112 100644 --- a/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc +++ b/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc @@ -72,7 +72,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// -LIBCOMPILER_MODULE(AssemblerMainPower64) { +NECTI_MODULE(AssemblerMainPower64) { CompilerKit::install_signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { @@ -258,7 +258,7 @@ asm_fail_exit: if (kVerbose) kStdOut << "AssemblerPower: Exit failed.\n"; - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/dev/CompilerKit/src/CodeGen.cc b/dev/CompilerKit/src/CodeGen.cc index e59001d..693d7a5 100644 --- a/dev/CompilerKit/src/CodeGen.cc +++ b/dev/CompilerKit/src/CodeGen.cc @@ -20,15 +20,15 @@ namespace CompilerKit { ///! @brief Compile for specific format (ELF, PEF, ZBIN) Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) noexcept { - if (sourceFile.length() < 1) return LIBCOMPILER_UNIMPLEMENTED; + if (sourceFile.length() < 1) return NECTI_UNIMPLEMENTED; - if (!fMounted) return LIBCOMPILER_UNIMPLEMENTED; - if (arch != fMounted->Arch()) return LIBCOMPILER_INVALID_ARCH; + if (!fMounted) return NECTI_UNIMPLEMENTED; + if (arch != fMounted->Arch()) return NECTI_INVALID_ARCH; try { return this->fMounted->CompileToFormat(sourceFile, arch); } catch (std::exception& e) { - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } } diff --git a/dev/CompilerKit/src/Frontend/CCompiler64x0.cc b/dev/CompilerKit/src/Frontend/CCompiler64x0.cc index ae2939b..c7ac387 100644 --- a/dev/CompilerKit/src/Frontend/CCompiler64x0.cc +++ b/dev/CompilerKit/src/Frontend/CCompiler64x0.cc @@ -139,7 +139,7 @@ class CompilerFrontend64x0 final : public CompilerKit::CompilerFrontendInterface explicit CompilerFrontend64x0() = default; ~CompilerFrontend64x0() override = default; - LIBCOMPILER_COPY_DEFAULT(CompilerFrontend64x0); + NECTI_COPY_DEFAULT(CompilerFrontend64x0); std::string Check(const char* text, const char* file); CompilerKit::SyntaxLeafList::SyntaxLeaf Compile(std::string text, std::string file) override; @@ -324,7 +324,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontend64x0::Compile(std::strin if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); - kIfFunction = "__LIBCOMPILER_IF_PROC_"; + kIfFunction = "__NECTI_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntaxLeaf.fUserValue = "\tlda r12, extern_segment "; @@ -1049,7 +1049,7 @@ class AssemblyCCInterface final LC_ASSEMBLY_INTERFACE { explicit AssemblyCCInterface() = default; ~AssemblyCCInterface() override = default; - LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); + NECTI_COPY_DEFAULT(AssemblyCCInterface); UInt32 Arch() noexcept override { return CompilerKit::AssemblyFactory::kArch64x0; } @@ -1197,7 +1197,7 @@ static void cc_print_help() { #define kExt ".c" -LIBCOMPILER_MODULE(CompilerCLang64x0) { +NECTI_MODULE(CompilerCLang64x0) { ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); diff --git a/dev/CompilerKit/src/Frontend/CCompilerARM64.cc b/dev/CompilerKit/src/Frontend/CCompilerARM64.cc index 0cd08d0..dd230c5 100644 --- a/dev/CompilerKit/src/Frontend/CCompilerARM64.cc +++ b/dev/CompilerKit/src/Frontend/CCompilerARM64.cc @@ -140,7 +140,7 @@ class CompilerFrontendARM64 final : public CompilerKit::CompilerFrontendInterfac explicit CompilerFrontendARM64() = default; ~CompilerFrontendARM64() override = default; - LIBCOMPILER_COPY_DEFAULT(CompilerFrontendARM64); + NECTI_COPY_DEFAULT(CompilerFrontendARM64); std::string Check(const char* text, const char* file); CompilerKit::SyntaxLeafList::SyntaxLeaf Compile(std::string text, std::string file) override; @@ -323,7 +323,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendARM64::Compile(std::stri if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); - kIfFunction = "__LIBCOMPILER_IF_PROC_"; + kIfFunction = "__NECTI_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntaxLeaf.fUserValue = "\tlda r12, extern_segment "; @@ -1048,7 +1048,7 @@ class AssemblyCCInterface final LC_ASSEMBLY_INTERFACE { explicit AssemblyCCInterface() = default; ~AssemblyCCInterface() override = default; - LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); + NECTI_COPY_DEFAULT(AssemblyCCInterface); UInt32 Arch() noexcept override { return CompilerKit::AssemblyFactory::kArchAARCH64; } @@ -1196,7 +1196,7 @@ static void cc_print_help() { #define kCExtension ".c" -LIBCOMPILER_MODULE(CompilerCLangARM64) { +NECTI_MODULE(CompilerCLangARM64) { ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); diff --git a/dev/CompilerKit/src/Frontend/CCompilerPower64.cc b/dev/CompilerKit/src/Frontend/CCompilerPower64.cc index aaa2308..0ffbef0 100644 --- a/dev/CompilerKit/src/Frontend/CCompilerPower64.cc +++ b/dev/CompilerKit/src/Frontend/CCompilerPower64.cc @@ -130,7 +130,7 @@ class CompilerFrontendPower64 final : public CompilerKit::CompilerFrontendInterf explicit CompilerFrontendPower64() = default; ~CompilerFrontendPower64() override = default; - LIBCOMPILER_COPY_DEFAULT(CompilerFrontendPower64); + NECTI_COPY_DEFAULT(CompilerFrontendPower64); std::string Check(const char* text, const char* file); CompilerKit::SyntaxLeafList::SyntaxLeaf Compile(std::string text, std::string file) override; @@ -330,7 +330,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendPower64::Compile(std::st if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); - kIfFunction = "__LIBCOMPILER_IF_PROC_"; + kIfFunction = "__NECTI_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntax_leaf.fUserValue = @@ -1067,7 +1067,7 @@ class AssemblyMountpointCLang final LC_ASSEMBLY_INTERFACE { explicit AssemblyMountpointCLang() = default; ~AssemblyMountpointCLang() override = default; - LIBCOMPILER_COPY_DEFAULT(AssemblyMountpointCLang); + NECTI_COPY_DEFAULT(AssemblyMountpointCLang); UInt32 Arch() noexcept override { return CompilerKit::AssemblyFactory::kArchPowerPC; } @@ -1214,7 +1214,7 @@ static void cc_print_help() { #define kExt ".c" -LIBCOMPILER_MODULE(CompilerCLangPowerPC) { +NECTI_MODULE(CompilerCLangPowerPC) { ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); diff --git a/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc b/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc index c38378a..5a0fc02 100644 --- a/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc +++ b/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc @@ -122,7 +122,7 @@ class CompilerFrontendCPlusPlusAMD64 final LC_COMPILER_FRONTEND { explicit CompilerFrontendCPlusPlusAMD64() = default; ~CompilerFrontendCPlusPlusAMD64() override = default; - LIBCOMPILER_COPY_DEFAULT(CompilerFrontendCPlusPlusAMD64); + NECTI_COPY_DEFAULT(CompilerFrontendCPlusPlusAMD64); CompilerKit::SyntaxLeafList::SyntaxLeaf Compile(const CompilerKit::STLString text, CompilerKit::STLString file) override; @@ -362,10 +362,10 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendCPlusPlusAMD64::Compile( symbol_name_fn.erase(symbol_name_fn.find("(")); } - syntax_tree.fUserValue = "public_segment .code64 __LIBCOMPILER_" + symbol_name_fn + "\n"; + syntax_tree.fUserValue = "public_segment .code64 __NECTI_" + symbol_name_fn + "\n"; ++kFunctionEmbedLevel; - kOriginMap.push_back({"__LIBCOMPILER_" + symbol_name_fn, kOrigin}); + kOriginMap.push_back({"__NECTI_" + symbol_name_fn, kOrigin}); break; @@ -492,10 +492,10 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendCPlusPlusAMD64::Compile( if (pairRight != valueOfVar) { if (valueOfVar[0] == '\"') { - syntax_tree.fUserValue = "segment .data64 __LIBCOMPILER_LOCAL_VAR_" + varName + + syntax_tree.fUserValue = "segment .data64 __NECTI_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n\n"; syntax_tree.fUserValue += instr + kRegisterList[kRegisterMap.size() - 1] + ", " + - "__LIBCOMPILER_LOCAL_VAR_" + varName + "\n"; + "__NECTI_LOCAL_VAR_" + varName + "\n"; kOrigin += 1UL; } else { syntax_tree.fUserValue = @@ -509,10 +509,10 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendCPlusPlusAMD64::Compile( if (((int) indexRight - 1) < 0) { if (valueOfVar[0] == '\"') { - syntax_tree.fUserValue = "segment .data64 __LIBCOMPILER_LOCAL_VAR_" + varName + + syntax_tree.fUserValue = "segment .data64 __NECTI_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n"; syntax_tree.fUserValue += instr + kRegisterList[kRegisterMap.size()] + ", " + - "__LIBCOMPILER_LOCAL_VAR_" + varName + "\n"; + "__NECTI_LOCAL_VAR_" + varName + "\n"; kOrigin += 1UL; } else { syntax_tree.fUserValue = @@ -665,8 +665,8 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendCPlusPlusAMD64::Compile( break; } } else { - syntax_tree.fUserValue = "__LIBCOMPILER_LOCAL_RETURN_STRING: db " + subText + - ", 0\nmov rcx, __LIBCOMPILER_LOCAL_RETURN_STRING\n"; + syntax_tree.fUserValue = "__NECTI_LOCAL_RETURN_STRING: db " + subText + + ", 0\nmov rcx, __NECTI_LOCAL_RETURN_STRING\n"; syntax_tree.fUserValue += "mov rax, rcx\nret\n"; kOrigin += 1UL; @@ -723,7 +723,7 @@ class AssemblyCPlusPlusInterfaceAMD64 final LC_ASSEMBLY_INTERFACE { explicit AssemblyCPlusPlusInterfaceAMD64() = default; ~AssemblyCPlusPlusInterfaceAMD64() override = default; - LIBCOMPILER_COPY_DEFAULT(AssemblyCPlusPlusInterfaceAMD64); + NECTI_COPY_DEFAULT(AssemblyCPlusPlusInterfaceAMD64); UInt32 Arch() noexcept override { return CompilerKit::AssemblyFactory::kArchAMD64; } @@ -756,7 +756,7 @@ class AssemblyCPlusPlusInterfaceAMD64 final LC_ASSEMBLY_INTERFACE { #define kExtListCxx \ { ".cpp", ".cxx", ".cc", ".c++", ".cp" } -LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { +NECTI_MODULE(CompilerCPlusPlusAMD64) { Boolean skip = false; kKeywords.emplace_back("if", CompilerKit::kKeywordKindIf); @@ -842,7 +842,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { if (strcmp(argv[index], "-cxx-dialect") == 0) { if (kCompilerFrontend) std::cout << kCompilerFrontend->Language() << "\n"; - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } if (strcmp(argv[index], "-cxx-max-err") == 0) { @@ -874,7 +874,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { for (CompilerKit::STLString ext : exts) { if (argv_i.ends_with(ext)) { if (kFactory.Compile(argv_i, kMachine) != kExitOK) { - return LIBCOMPILER_INVALID_DATA; + return NECTI_INVALID_DATA; } break; @@ -884,7 +884,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { kFactory.Unmount(); - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } // diff --git a/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc b/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc index b2906a6..0aa2b78 100644 --- a/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc +++ b/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc @@ -74,7 +74,7 @@ static std::vector kObjectBytes; /// @brief NE 64-bit Linker. /// @note This linker is made for PEF executable, thus NE based OSes. -LIBCOMPILER_MODULE(DynamicLinker64PEF) { +NECTI_MODULE(DynamicLinker64PEF) { bool is_executable = true; ::signal(SIGSEGV, Detail::drvi_crash_handler); @@ -99,11 +99,11 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { kConsoleOut << "-arm64: Output as a ARM64 PEF.\n"; kConsoleOut << "-output: Select the output file name.\n"; - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } else if (StringCompare(argv[linker_arg], "-version") == 0) { kLinkerSplash(); - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } else if (StringCompare(argv[linker_arg], "-fat") == 0) { kFatBinaryEnable = true; @@ -170,10 +170,10 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { if (kOutput.empty()) { kConsoleOut << "no output filename set." << std::endl; - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } else if (kObjectList.empty()) { kConsoleOut << "no input files." << std::endl; - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } else { namespace FS = std::filesystem; @@ -183,7 +183,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { // if filesystem doesn't find file // -> throw error. kConsoleOut << "no such file: " << obj << std::endl; - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } } } @@ -191,7 +191,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { // PEF expects a valid target architecture when outputing a binary. if (kArch == CompilerKit::kPefArchInvalid) { kConsoleOut << "no target architecture set, can't continue." << std::endl; - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } CompilerKit::PEFContainer pef_container{}; @@ -221,7 +221,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { kConsoleOut << "error: " << strerror(errno) << "\n"; } - return LIBCOMPILER_FILE_NOT_FOUND; + return NECTI_FILE_NOT_FOUND; } //! Read AE to convert as PEF. @@ -250,7 +250,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { "treated as a FAT binary." << std::endl; - return LIBCOMPILER_FAT_ERROR; + return NECTI_FAT_ERROR; } else { if (kVerbose) { kConsoleOut << "Architecture matches what we expect.\n"; @@ -345,7 +345,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { kConsoleOut << "not an object container: " << objectFile << std::endl; // don't continue, it is a fatal error. - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } pef_container.Cpu = archs; @@ -624,7 +624,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { kConsoleOut << "Multiple symbols of: " << symbol << " detected, cannot continue.\n"; } - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } // step 2.5: write program bytes. @@ -654,7 +654,7 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { kConsoleOut << "Undefined symbol " << unreferenced_symbol << "\n"; } - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } if ((!kStartFound || kDuplicateSymbols) && @@ -663,10 +663,10 @@ LIBCOMPILER_MODULE(DynamicLinker64PEF) { kConsoleOut << "File: " << kOutput << ", is corrupt, removing file...\n"; } - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } // Last rev 13-1-24 diff --git a/dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc b/dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc index 12a69c8..9845967 100644 --- a/dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc +++ b/dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc @@ -725,7 +725,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { ///////////////////////////////////////////////////////////////////////////////////////// -LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { +NECTI_MODULE(CPlusPlusPreprocessorMain) { try { bool skip = false; bool double_skip = false; @@ -760,7 +760,7 @@ LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { Detail::bpp_macro macro_zka; - macro_zka.fName = "__LIBCOMPILER__"; + macro_zka.fName = "__NECTI__"; macro_zka.fValue = "1"; kMacros.push_back(macro_zka); @@ -806,7 +806,7 @@ LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { "NeKernel Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2025 all rights " "reserved."); - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } if (strcmp(argv[index], "-cpp-help") == 0) { @@ -819,7 +819,7 @@ LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { printf("%s\n", "-cpp-ver: print the version."); printf("%s\n", "-cpp-help: show help (this current command)."); - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } if (strcmp(argv[index], "-cpp-include-dir") == 0) { @@ -871,7 +871,7 @@ LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { kFiles.emplace_back(argv[index]); } - if (kFiles.empty()) return LIBCOMPILER_EXEC_ERROR; + if (kFiles.empty()) return NECTI_EXEC_ERROR; for (auto& file : kFiles) { if (!std::filesystem::exists(file)) continue; @@ -882,12 +882,12 @@ LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { bpp_parse_file(file_descriptor, file_descriptor_pp); } - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } catch (const std::runtime_error& e) { std::cout << e.what() << '\n'; } - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } // Last rev 8-1-24 diff --git a/dev/CompilerKit/utils/CompilerUtils.h b/dev/CompilerKit/utils/CompilerUtils.h index da35b2b..bf48b79 100644 --- a/dev/CompilerKit/utils/CompilerUtils.h +++ b/dev/CompilerKit/utils/CompilerUtils.h @@ -46,7 +46,7 @@ inline void print_error(std::string reason, std::string file) noexcept { kStdErr << reason << kBlank << std::endl; - if (kAcceptableErrors > kErrorLimit) std::exit(LIBCOMPILER_EXEC_ERROR); + if (kAcceptableErrors > kErrorLimit) std::exit(NECTI_EXEC_ERROR); ++kAcceptableErrors; } @@ -111,6 +111,6 @@ inline void drvi_crash_handler(std::int32_t id) { std::cout << std::endl; - std::exit(LIBCOMPILER_EXEC_ERROR); + std::exit(NECTI_EXEC_ERROR); } } // namespace Detail diff --git a/dev/DebuggerKit/ld-nekernel.json b/dev/DebuggerKit/ld-nekernel.json index d2aeb24..be55d51 100644 --- a/dev/DebuggerKit/ld-nekernel.json +++ b/dev/DebuggerKit/ld-nekernel.json @@ -9,7 +9,7 @@ "output_name": "/usr/local/lib/libDebuggerKit.dylib", "compiler_flags": ["-fPIC", "-shared"], "cpp_macros": [ - "__LIBCOMPILER__=202505", + "__NECTI__=202505", "LC_USE_STRUCTS=1", "LD_NEKERNEL_DEBUGGER", "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" diff --git a/dev/DebuggerKit/ld-osx.json b/dev/DebuggerKit/ld-osx.json index 26922f4..a7c5fac 100644 --- a/dev/DebuggerKit/ld-osx.json +++ b/dev/DebuggerKit/ld-osx.json @@ -9,7 +9,7 @@ "output_name": "/usr/local/lib/libDebuggerKit.dylib", "compiler_flags": ["-fPIC", "-shared"], "cpp_macros": [ - "__LIBCOMPILER__=202505", + "__NECTI__=202505", "LC_USE_STRUCTS=1", "DEBUGGERKIT_POSIX", "LD_MACH_DEBUGGER", diff --git a/dev/DebuggerKit/src/NeKernelContractCLI.cc b/dev/DebuggerKit/src/NeKernelContractCLI.cc index 9d078c8..49cf6c8 100644 --- a/dev/DebuggerKit/src/NeKernelContractCLI.cc +++ b/dev/DebuggerKit/src/NeKernelContractCLI.cc @@ -28,7 +28,7 @@ static void dbgi_ctrlc_handler(std::int32_t _) { kKeepRunning = false; } -LIBCOMPILER_MODULE(DebuggerNeKernel) { +NECTI_MODULE(DebuggerNeKernel) { pfd::notify("Debugger Event", "NeKernel Debugger\n(C) 2025 Amlal El Mahrouss and NeKernel.org contributors, all rights reserved."); diff --git a/dev/DebuggerKit/src/POSIXMachContractCLI.cc b/dev/DebuggerKit/src/POSIXMachContractCLI.cc index 11c05f8..80825c3 100644 --- a/dev/DebuggerKit/src/POSIXMachContractCLI.cc +++ b/dev/DebuggerKit/src/POSIXMachContractCLI.cc @@ -26,7 +26,7 @@ static void dbgi_ctrlc_handler(std::int32_t _) { kKeepRunning = false; } -LIBCOMPILER_MODULE(DebuggerMachPOSIX) { +NECTI_MODULE(DebuggerMachPOSIX) { pfd::notify("Debugger Event", "Userland Debugger\n(C) 2025 Amlal El Mahrouss, all rights reserved."); diff --git a/dev/LibC++/__power64.inc b/dev/LibC++/__power64.inc index b09bdcc..c06863a 100644 --- a/dev/LibC++/__power64.inc +++ b/dev/LibC++/__power64.inc @@ -2,7 +2,7 @@ # Language: CompilerKit POWER Assembly support for GNU. # Build Date: 2024-6-4 -#ifdef __LIBCOMPILER__ +#ifdef __NECTI__ #ifdef __ASSEMBLER__ diff --git a/dev/LibC++/defines.h b/dev/LibC++/defines.h index 493ed0c..2fedac5 100644 --- a/dev/LibC++/defines.h +++ b/dev/LibC++/defines.h @@ -4,8 +4,8 @@ ------------------------------------------- */ -#ifndef __LIBCOMPILER_DEFINES_H__ -#define __LIBCOMPILER_DEFINES_H__ +#ifndef __NECTI_DEFINES_H__ +#define __NECTI_DEFINES_H__ extern "C" { #include @@ -34,7 +34,7 @@ typedef char* caddr_t; #ifdef __GNUC__ #include -#elif defined(__LIBCOMPILER__) +#elif defined(__NECTI__) #define __alloca(sz) __lc_alloca(sz) #endif @@ -85,4 +85,4 @@ typedef union double_cast { #endif // ifndef __GNUC__ -#endif /* __LIBCOMPILER_DEFINES_H__ */ +#endif /* __NECTI_DEFINES_H__ */ diff --git a/dev/LibC++/filesystem.h b/dev/LibC++/filesystem.h index 254bfab..1095da1 100644 --- a/dev/LibC++/filesystem.h +++ b/dev/LibC++/filesystem.h @@ -4,8 +4,8 @@ ------------------------------------------- */ -#ifndef __LIBCOMPILER_FS_H__ -#define __LIBCOMPILER_FS_H__ +#ifndef __NECTI_FS_H__ +#define __NECTI_FS_H__ namespace std { class path; @@ -14,4 +14,4 @@ class directory_entry; class directory_iterator; } // namespace std -#endif // __LIBCOMPILER_FS_H__ \ No newline at end of file +#endif // __NECTI_FS_H__ \ No newline at end of file diff --git a/tools/cppdrv.cc b/tools/cppdrv.cc index 91b27b3..e46c8ee 100644 --- a/tools/cppdrv.cc +++ b/tools/cppdrv.cc @@ -20,8 +20,8 @@ int main(int argc, char const* argv[]) { if (auto code = CPlusPlusPreprocessorMain(2, argv); code > 0) { std::printf("cppdrv: preprocessor exited with code %i.\n", code); - return LIBCOMPILER_EXEC_ERROR; + return NECTI_EXEC_ERROR; } - return LIBCOMPILER_SUCCESS; + return NECTI_SUCCESS; } \ No newline at end of file diff --git a/tools/pef-amd64-cxxdrv.cc b/tools/pef-amd64-cxxdrv.cc index 5ddeb16..c7ca39e 100644 --- a/tools/pef-amd64-cxxdrv.cc +++ b/tools/pef-amd64-cxxdrv.cc @@ -37,7 +37,7 @@ Int32 main(Int32 argc, Char const* argv[]) { return EXIT_FAILURE; } - auto ret = (entrypoint_cxx(argc, argv) == LIBCOMPILER_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; + auto ret = (entrypoint_cxx(argc, argv) == NECTI_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; dlclose(handler); diff --git a/tools/pef-arm64-cdrv.cc b/tools/pef-arm64-cdrv.cc index f8b9e5c..1e4b149 100644 --- a/tools/pef-arm64-cdrv.cc +++ b/tools/pef-arm64-cdrv.cc @@ -37,7 +37,7 @@ Int32 main(Int32 argc, Char const* argv[]) { return EXIT_FAILURE; } - auto ret = (entrypoint_cxx(argc, argv) == LIBCOMPILER_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; + auto ret = (entrypoint_cxx(argc, argv) == NECTI_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; dlclose(handler); -- cgit v1.2.3 From 3658cb8407814603aceaf2970a5c1016b6c9fdc8 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 6 Aug 2025 16:41:31 +0100 Subject: feat! breaking API changes before NeKernel.org 0.0.4. Signed-off-by: Amlal El Mahrouss --- MailMap | 2 +- dev/CompilerKit/BasicString.h | 82 -- dev/CompilerKit/CodeGen.h | 206 ----- dev/CompilerKit/Compiler.h | 206 +++++ dev/CompilerKit/Frontend.h | 2 +- dev/CompilerKit/StringKit.h | 83 ++ dev/CompilerKit/ck-osx-san.json | 29 + dev/CompilerKit/ck-osx.json | 24 + dev/CompilerKit/ck-posix.json | 24 + dev/CompilerKit/lc-osx-san.json | 29 - dev/CompilerKit/lc-osx.json | 24 - dev/CompilerKit/lc-posix.json | 24 - dev/CompilerKit/src/AssemblyFactory.cc | 52 ++ dev/CompilerKit/src/Backend/Assembler32x0.cc | 2 +- dev/CompilerKit/src/Backend/Assembler64x0.cc | 2 +- dev/CompilerKit/src/Backend/AssemblerARM64.cc | 2 +- dev/CompilerKit/src/Backend/AssemblerPowerPC.cc | 2 +- dev/CompilerKit/src/BasicString.cc | 178 ---- dev/CompilerKit/src/CodeGen.cc | 52 -- dev/CompilerKit/src/Frontend.cc | 51 -- dev/CompilerKit/src/Frontend/CCompiler64x0.cc | 2 +- dev/CompilerKit/src/Frontend/CCompilerARM64.cc | 2 +- dev/CompilerKit/src/Frontend/CCompilerPower64.cc | 2 +- dev/CompilerKit/src/FrontendHelpers.cc | 51 ++ dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc | 5 +- .../src/Macro/CPlusPlusCompilerPreProcessor.cc | 893 --------------------- dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc | 893 +++++++++++++++++++++ dev/CompilerKit/src/StringKit.cc | 179 +++++ dev/CompilerKit/utils/AsmUtils.h | 3 +- dev/CompilerKit/utils/CompilerUtils.h | 2 +- docs/drawio/COMPILERKIT_DESIGN.drawio | 64 ++ docs/drawio/DEBUGGERKIT_DESIGN.drawio | 47 ++ docs/drawio/LIBCOMPILER_DESIGN.drawio | 64 -- docs/drawio/LIBDEBUGGER_DESIGN.drawio | 47 -- tools/asm.cc | 2 +- tools/dbg.cc | 2 +- tools/kdbg.cc | 2 +- tools/ld64.cc | 2 +- 38 files changed, 1669 insertions(+), 1669 deletions(-) delete mode 100644 dev/CompilerKit/BasicString.h delete mode 100644 dev/CompilerKit/CodeGen.h create mode 100644 dev/CompilerKit/Compiler.h create mode 100644 dev/CompilerKit/StringKit.h create mode 100644 dev/CompilerKit/ck-osx-san.json create mode 100644 dev/CompilerKit/ck-osx.json create mode 100644 dev/CompilerKit/ck-posix.json delete mode 100644 dev/CompilerKit/lc-osx-san.json delete mode 100644 dev/CompilerKit/lc-osx.json delete mode 100644 dev/CompilerKit/lc-posix.json create mode 100644 dev/CompilerKit/src/AssemblyFactory.cc delete mode 100644 dev/CompilerKit/src/BasicString.cc delete mode 100644 dev/CompilerKit/src/CodeGen.cc delete mode 100644 dev/CompilerKit/src/Frontend.cc create mode 100644 dev/CompilerKit/src/FrontendHelpers.cc delete mode 100644 dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc create mode 100644 dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc create mode 100644 dev/CompilerKit/src/StringKit.cc create mode 100644 docs/drawio/COMPILERKIT_DESIGN.drawio create mode 100644 docs/drawio/DEBUGGERKIT_DESIGN.drawio delete mode 100644 docs/drawio/LIBCOMPILER_DESIGN.drawio delete mode 100644 docs/drawio/LIBDEBUGGER_DESIGN.drawio (limited to 'dev/CompilerKit/utils/CompilerUtils.h') diff --git a/MailMap b/MailMap index 6f36f25..99f9dd1 100644 --- a/MailMap +++ b/MailMap @@ -1 +1 @@ -@amlel-el-mahrouss - amlal@nekernel.org +@amlel-el-mahrouss - amlal@nekernel.org \ No newline at end of file diff --git a/dev/CompilerKit/BasicString.h b/dev/CompilerKit/BasicString.h deleted file mode 100644 index c4cb55a..0000000 --- a/dev/CompilerKit/BasicString.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * ======================================================== - * - * CompilerKit - * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include -#include - -namespace CompilerKit { -class StringBuilder; -class BasicString; - -/** - * @brief BasicString class, contains a C string and manages it. - * @note No need to manage it it's getting deleted by default. - */ - -class BasicString final { - public: - explicit BasicString() = delete; - - explicit BasicString(SizeType Sz) noexcept : m_Sz(Sz) { - m_Data = new Char[Sz]; - assert(m_Data); - } - - ~BasicString() noexcept { - if (m_Data) { - memset(m_Data, 0, m_Sz); - delete[] m_Data; - - m_Data = nullptr; - } - } - - NECTI_COPY_DEFAULT(BasicString); - - Char* Data(); - const Char* CData() const; - SizeType Length() const; - - bool operator==(const Char* rhs) const; - bool operator!=(const Char* rhs) const; - - bool operator==(const BasicString& rhs) const; - bool operator!=(const BasicString& rhs) const; - - 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: - Char* m_Data{nullptr}; - SizeType m_Sz{0}; - SizeType m_Cur{0}; - - friend class StringBuilder; -}; - -/** - * @brief StringBuilder class - * @note These results shall call be delete[] after they're used. - */ -struct StringBuilder final { - 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; -} // namespace CompilerKit diff --git a/dev/CompilerKit/CodeGen.h b/dev/CompilerKit/CodeGen.h deleted file mode 100644 index 99c968b..0000000 --- a/dev/CompilerKit/CodeGen.h +++ /dev/null @@ -1,206 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved - -------------------------------------------- */ - -#pragma once - -#include -#include -#include - -#define CK_ASSEMBLY_INTERFACE : public ::CompilerKit::AssemblyInterface -#define CK_ENCODER : public ::CompilerKit::EncoderInterface - -namespace CompilerKit { -class AssemblyFactory; -class AssemblyInterface; - -/// @brief Simple assembly factory -class AssemblyFactory final { - public: - explicit AssemblyFactory() = default; - ~AssemblyFactory() = default; - - NECTI_COPY_DEFAULT(AssemblyFactory); - - public: - enum { - kArchInvalid = 0, - kArchAMD64 = 100, - kArch32x0, - kArch64x0, - kArchRISCV, - kArchPowerPC, - kArchAARCH64, - kArchUnknown, - kArchCount = kArchUnknown - kArchAMD64, - }; - - Int32 Compile(std::string sourceFile, const Int32& arch) noexcept; - - void Mount(AssemblyInterface* mountPtr) noexcept; - AssemblyInterface* Unmount() noexcept; - - private: - AssemblyInterface* fMounted{nullptr}; -}; - -/// @brief Assembly to binary generator class. -/// @note This interface creates according to the CPU target of the child class. -class AssemblyInterface { - public: - explicit AssemblyInterface() = default; - virtual ~AssemblyInterface() = default; - - NECTI_COPY_DEFAULT(AssemblyInterface); - - virtual UInt32 Arch() noexcept { return AssemblyFactory::kArchAMD64; } - - /// @brief compile to object file. - /// @note Example C++ -> MASM -> AE object. - virtual Int32 CompileToFormat(std::string src, Int32 arch) = 0; -}; - -union NumberCastBase { - NumberCastBase() = default; - ~NumberCastBase() = default; -}; - -union NumberCast64 final { - NumberCast64() = default; - explicit NumberCast64(UInt64 raw) : raw(raw) {} - - ~NumberCast64() { raw = 0; } - - Char number[8]; - UInt64 raw; -}; - -union NumberCast32 final { - NumberCast32() = default; - explicit NumberCast32(UInt32 raw) : raw(raw) {} - - ~NumberCast32() { raw = 0; } - - Char number[4]; - UInt32 raw; -}; - -union NumberCast16 final { - NumberCast16() = default; - explicit NumberCast16(UInt16 raw) : raw(raw) {} - - ~NumberCast16() { raw = 0; } - - Char number[2]; - UInt16 raw; -}; - -union NumberCast8 final { - NumberCast8() = default; - explicit NumberCast8(UInt8 raw) : raw(raw) {} - - ~NumberCast8() { raw = 0; } - - Char number; - UInt8 raw; -}; - -class EncoderInterface { - public: - explicit EncoderInterface() = default; - virtual ~EncoderInterface() = default; - - NECTI_COPY_DEFAULT(EncoderInterface); - - virtual std::string CheckLine(std::string line, std::string file) = 0; - virtual bool WriteLine(std::string line, std::string file) = 0; - virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) = 0; -}; - -#ifdef __ASM_NEED_AMD64__ - -class EncoderAMD64 final : public EncoderInterface { - public: - explicit EncoderAMD64() = default; - ~EncoderAMD64() override = default; - - NECTI_COPY_DEFAULT(EncoderAMD64); - - virtual std::string CheckLine(std::string line, std::string file) override; - virtual bool WriteLine(std::string line, std::string file) override; - virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; - - virtual bool WriteNumber16(const std::size_t& pos, std::string& from_what); - virtual bool WriteNumber32(const std::size_t& pos, std::string& from_what); - virtual bool WriteNumber8(const std::size_t& pos, std::string& from_what); -}; - -#endif // __ASM_NEED_AMD64__ - -#ifdef __ASM_NEED_ARM64__ - -class EncoderARM64 final : public EncoderInterface { - public: - explicit EncoderARM64() = default; - ~EncoderARM64() override = default; - - NECTI_COPY_DEFAULT(EncoderARM64); - - virtual std::string CheckLine(std::string line, std::string file) override; - virtual bool WriteLine(std::string line, std::string file) override; - virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; -}; - -#endif // __ASM_NEED_ARM64__ - -#ifdef __ASM_NEED_64x0__ - -class Encoder64x0 final : public EncoderInterface { - public: - explicit Encoder64x0() = default; - ~Encoder64x0() override = default; - - NECTI_COPY_DEFAULT(Encoder64x0); - - virtual std::string CheckLine(std::string line, std::string file) override; - virtual bool WriteLine(std::string line, std::string file) override; - virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; -}; - -#endif // __ASM_NEED_64x0__ - -#ifdef __ASM_NEED_32x0__ - -class Encoder32x0 final : public EncoderInterface { - public: - explicit Encoder32x0() = default; - ~Encoder32x0() override = default; - - NECTI_COPY_DEFAULT(Encoder32x0); - - virtual std::string CheckLine(std::string line, std::string file) override; - virtual bool WriteLine(std::string line, std::string file) override; - virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; -}; - -#endif // __ASM_NEED_32x0__ - -#ifdef __ASM_NEED_PPC__ - -class EncoderPowerPC final : public EncoderInterface { - public: - explicit EncoderPowerPC() = default; - ~EncoderPowerPC() override = default; - - NECTI_COPY_DEFAULT(EncoderPowerPC); - - virtual std::string CheckLine(std::string line, std::string file) override; - virtual bool WriteLine(std::string line, std::string file) override; - virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; -}; - -#endif // __ASM_NEED_32x0__ -} // namespace CompilerKit diff --git a/dev/CompilerKit/Compiler.h b/dev/CompilerKit/Compiler.h new file mode 100644 index 0000000..60d63ff --- /dev/null +++ b/dev/CompilerKit/Compiler.h @@ -0,0 +1,206 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include +#include +#include + +#define CK_ASSEMBLY_INTERFACE : public ::CompilerKit::AssemblyInterface +#define CK_ENCODER : public ::CompilerKit::EncoderInterface + +namespace CompilerKit { +class AssemblyFactory; +class AssemblyInterface; + +/// @brief Simple assembly factory +class AssemblyFactory final { + public: + explicit AssemblyFactory() = default; + ~AssemblyFactory() = default; + + NECTI_COPY_DEFAULT(AssemblyFactory); + + public: + enum { + kArchInvalid = 0, + kArchAMD64 = 100, + kArch32x0, + kArch64x0, + kArchRISCV, + kArchPowerPC, + kArchAARCH64, + kArchUnknown, + kArchCount = kArchUnknown - kArchAMD64, + }; + + Int32 Compile(std::string sourceFile, const Int32& arch) noexcept; + + void Mount(AssemblyInterface* mountPtr) noexcept; + AssemblyInterface* Unmount() noexcept; + + private: + AssemblyInterface* fMounted{nullptr}; +}; + +/// @brief Assembly to binary generator class. +/// @note This interface creates according to the CPU target of the child class. +class AssemblyInterface { + public: + explicit AssemblyInterface() = default; + virtual ~AssemblyInterface() = default; + + NECTI_COPY_DEFAULT(AssemblyInterface); + + virtual UInt32 Arch() noexcept { return AssemblyFactory::kArchAMD64; } + + /// @brief compile to object file. + /// @note Example C++ -> MASM -> AE object. + virtual Int32 CompileToFormat(std::string src, Int32 arch) = 0; +}; + +union NumberCastBase { + NumberCastBase() = default; + ~NumberCastBase() = default; +}; + +union NumberCast64 final { + NumberCast64() = default; + explicit NumberCast64(UInt64 raw) : raw(raw) {} + + ~NumberCast64() { raw = 0; } + + Char number[8]; + UInt64 raw; +}; + +union NumberCast32 final { + NumberCast32() = default; + explicit NumberCast32(UInt32 raw) : raw(raw) {} + + ~NumberCast32() { raw = 0; } + + Char number[4]; + UInt32 raw; +}; + +union NumberCast16 final { + NumberCast16() = default; + explicit NumberCast16(UInt16 raw) : raw(raw) {} + + ~NumberCast16() { raw = 0; } + + Char number[2]; + UInt16 raw; +}; + +union NumberCast8 final { + NumberCast8() = default; + explicit NumberCast8(UInt8 raw) : raw(raw) {} + + ~NumberCast8() { raw = 0; } + + Char number; + UInt8 raw; +}; + +class EncoderInterface { + public: + explicit EncoderInterface() = default; + virtual ~EncoderInterface() = default; + + NECTI_COPY_DEFAULT(EncoderInterface); + + virtual std::string CheckLine(std::string line, std::string file) = 0; + virtual bool WriteLine(std::string line, std::string file) = 0; + virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) = 0; +}; + +#ifdef __ASM_NEED_AMD64__ + +class EncoderAMD64 final : public EncoderInterface { + public: + explicit EncoderAMD64() = default; + ~EncoderAMD64() override = default; + + NECTI_COPY_DEFAULT(EncoderAMD64); + + virtual std::string CheckLine(std::string line, std::string file) override; + virtual bool WriteLine(std::string line, std::string file) override; + virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; + + virtual bool WriteNumber16(const std::size_t& pos, std::string& from_what); + virtual bool WriteNumber32(const std::size_t& pos, std::string& from_what); + virtual bool WriteNumber8(const std::size_t& pos, std::string& from_what); +}; + +#endif // __ASM_NEED_AMD64__ + +#ifdef __ASM_NEED_ARM64__ + +class EncoderARM64 final : public EncoderInterface { + public: + explicit EncoderARM64() = default; + ~EncoderARM64() override = default; + + NECTI_COPY_DEFAULT(EncoderARM64); + + virtual std::string CheckLine(std::string line, std::string file) override; + virtual bool WriteLine(std::string line, std::string file) override; + virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; +}; + +#endif // __ASM_NEED_ARM64__ + +#ifdef __ASM_NEED_64x0__ + +class Encoder64x0 final : public EncoderInterface { + public: + explicit Encoder64x0() = default; + ~Encoder64x0() override = default; + + NECTI_COPY_DEFAULT(Encoder64x0); + + virtual std::string CheckLine(std::string line, std::string file) override; + virtual bool WriteLine(std::string line, std::string file) override; + virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; +}; + +#endif // __ASM_NEED_64x0__ + +#ifdef __ASM_NEED_32x0__ + +class Encoder32x0 final : public EncoderInterface { + public: + explicit Encoder32x0() = default; + ~Encoder32x0() override = default; + + NECTI_COPY_DEFAULT(Encoder32x0); + + virtual std::string CheckLine(std::string line, std::string file) override; + virtual bool WriteLine(std::string line, std::string file) override; + virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; +}; + +#endif // __ASM_NEED_32x0__ + +#ifdef __ASM_NEED_PPC__ + +class EncoderPowerPC final : public EncoderInterface { + public: + explicit EncoderPowerPC() = default; + ~EncoderPowerPC() override = default; + + NECTI_COPY_DEFAULT(EncoderPowerPC); + + virtual std::string CheckLine(std::string line, std::string file) override; + virtual bool WriteLine(std::string line, std::string file) override; + virtual bool WriteNumber(const std::size_t& pos, std::string& from_what) override; +}; + +#endif // __ASM_NEED_32x0__ +} // namespace CompilerKit diff --git a/dev/CompilerKit/Frontend.h b/dev/CompilerKit/Frontend.h index baf00d9..4719c3d 100644 --- a/dev/CompilerKit/Frontend.h +++ b/dev/CompilerKit/Frontend.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #define CK_COMPILER_FRONTEND : public ::CompilerKit::CompilerFrontendInterface diff --git a/dev/CompilerKit/StringKit.h b/dev/CompilerKit/StringKit.h new file mode 100644 index 0000000..18b3ad8 --- /dev/null +++ b/dev/CompilerKit/StringKit.h @@ -0,0 +1,83 @@ +/* + * ======================================================== + * + * CompilerKit + * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include +#include + +namespace CompilerKit { +class StringBuilder; +class BasicString; + +/** + * @brief BasicString class, contains a C string and manages it. + * @note No need to manage it it's getting deleted by default. + */ + +class BasicString final { + public: + explicit BasicString() = delete; + + explicit BasicString(SizeType Sz) noexcept : m_Sz(Sz) { + m_Data = new Char[Sz]; + assert(m_Data); + } + + ~BasicString() noexcept { + if (m_Data) { + memset(m_Data, 0, m_Sz); + delete[] m_Data; + + m_Data = nullptr; + } + } + + NECTI_COPY_DEFAULT(BasicString); + + Char* Data(); + const Char* CData() const; + SizeType Length() const; + + bool operator==(const Char* rhs) const; + bool operator!=(const Char* rhs) const; + + bool operator==(const BasicString& rhs) const; + bool operator!=(const BasicString& rhs) const; + + BasicString& operator+=(const Char* 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: + Char* m_Data{nullptr}; + SizeType m_Sz{0}; + SizeType m_Cur{0}; + + friend class StringBuilder; +}; + +/** + * @brief StringBuilder class + * @note These results shall call be delete[] after they're used. + */ +struct StringBuilder final { + static BasicString Construct(const Char* data); + static BasicString FromInt(const char* fmt, int n); + static BasicString FromBool(const char* fmt, bool n); + static BasicString Format(const char* fmt, const char* from); + static BOOL Equals(const char* lhs, const char* rhs); +}; + +using BasicStringOr = ErrorOr; +} // namespace CompilerKit diff --git a/dev/CompilerKit/ck-osx-san.json b/dev/CompilerKit/ck-osx-san.json new file mode 100644 index 0000000..f190db2 --- /dev/null +++ b/dev/CompilerKit/ck-osx-san.json @@ -0,0 +1,29 @@ +{ + "compiler_path": "clang++", + "compiler_std": "c++20", + "headers_path": [ + "../CompilerKit", + "../", + "../CompilerKit/src/", + "../CompilerKit/src/impl" + ], + "sources_path": [ + "src/*.cc", + "src/*/*.cc" + ], + "output_name": "/usr/local/lib/libCompilerKit.dylib", + "compiler_flags": [ + "-fPIC", + "-shared", + "-fstack-protector-all", + "-fno-omit-frame-pointer", + "-g", + "-fsanitize=address", + "-fsanitize=undefined" + ], + "cpp_macros": [ + "__NECTI__=202505", + "CK_USE_STRUCTS=1", + "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" + ] +} \ No newline at end of file diff --git a/dev/CompilerKit/ck-osx.json b/dev/CompilerKit/ck-osx.json new file mode 100644 index 0000000..4880763 --- /dev/null +++ b/dev/CompilerKit/ck-osx.json @@ -0,0 +1,24 @@ +{ + "compiler_path": "clang++", + "compiler_std": "c++20", + "headers_path": [ + "../CompilerKit", + "../", + "../CompilerKit/src/", + "../CompilerKit/src/impl" + ], + "sources_path": [ + "src/*.cc", + "src/*/*.cc" + ], + "output_name": "/usr/local/lib/libCompilerKit.dylib", + "compiler_flags": [ + "-fPIC", + "-shared" + ], + "cpp_macros": [ + "__NECTI__=202505", + "CK_USE_STRUCTS=1", + "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" + ] +} \ No newline at end of file diff --git a/dev/CompilerKit/ck-posix.json b/dev/CompilerKit/ck-posix.json new file mode 100644 index 0000000..e80ce65 --- /dev/null +++ b/dev/CompilerKit/ck-posix.json @@ -0,0 +1,24 @@ +{ + "compiler_path": "g++", + "compiler_std": "c++20", + "headers_path": [ + "../CompilerKit", + "../", + "../CompilerKit/src/", + "../CompilerKit/src/impl" + ], + "sources_path": [ + "src/*.cc", + "src/*/*.cc" + ], + "output_name": "/usr/local/lib/libCompilerKit.so", + "compiler_flags": [ + "-fPIC", + "-shared" + ], + "cpp_macros": [ + "__NECTI__=202505", + "CK_USE_STRUCTS=1", + "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" + ] +} \ No newline at end of file diff --git a/dev/CompilerKit/lc-osx-san.json b/dev/CompilerKit/lc-osx-san.json deleted file mode 100644 index f190db2..0000000 --- a/dev/CompilerKit/lc-osx-san.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "compiler_path": "clang++", - "compiler_std": "c++20", - "headers_path": [ - "../CompilerKit", - "../", - "../CompilerKit/src/", - "../CompilerKit/src/impl" - ], - "sources_path": [ - "src/*.cc", - "src/*/*.cc" - ], - "output_name": "/usr/local/lib/libCompilerKit.dylib", - "compiler_flags": [ - "-fPIC", - "-shared", - "-fstack-protector-all", - "-fno-omit-frame-pointer", - "-g", - "-fsanitize=address", - "-fsanitize=undefined" - ], - "cpp_macros": [ - "__NECTI__=202505", - "CK_USE_STRUCTS=1", - "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" - ] -} \ No newline at end of file diff --git a/dev/CompilerKit/lc-osx.json b/dev/CompilerKit/lc-osx.json deleted file mode 100644 index 4880763..0000000 --- a/dev/CompilerKit/lc-osx.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "compiler_path": "clang++", - "compiler_std": "c++20", - "headers_path": [ - "../CompilerKit", - "../", - "../CompilerKit/src/", - "../CompilerKit/src/impl" - ], - "sources_path": [ - "src/*.cc", - "src/*/*.cc" - ], - "output_name": "/usr/local/lib/libCompilerKit.dylib", - "compiler_flags": [ - "-fPIC", - "-shared" - ], - "cpp_macros": [ - "__NECTI__=202505", - "CK_USE_STRUCTS=1", - "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" - ] -} \ No newline at end of file diff --git a/dev/CompilerKit/lc-posix.json b/dev/CompilerKit/lc-posix.json deleted file mode 100644 index e80ce65..0000000 --- a/dev/CompilerKit/lc-posix.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "compiler_path": "g++", - "compiler_std": "c++20", - "headers_path": [ - "../CompilerKit", - "../", - "../CompilerKit/src/", - "../CompilerKit/src/impl" - ], - "sources_path": [ - "src/*.cc", - "src/*/*.cc" - ], - "output_name": "/usr/local/lib/libCompilerKit.so", - "compiler_flags": [ - "-fPIC", - "-shared" - ], - "cpp_macros": [ - "__NECTI__=202505", - "CK_USE_STRUCTS=1", - "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" - ] -} \ No newline at end of file diff --git a/dev/CompilerKit/src/AssemblyFactory.cc b/dev/CompilerKit/src/AssemblyFactory.cc new file mode 100644 index 0000000..1f08b32 --- /dev/null +++ b/dev/CompilerKit/src/AssemblyFactory.cc @@ -0,0 +1,52 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#include +#include + +/** + * @file AssemblyFactory.cc + * @author Amlal El Mahrouss (amlal@nekernel.org) + * @brief Compiler API of NeCTI + * @version 0.0.2 + * + * @copyright Copyright (c) 2024-2025 Amlal El Mahrouss + * + */ + +namespace CompilerKit { +///! @brief Compile for specific format (ELF, PEF, ZBIN) +Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) noexcept { + if (sourceFile.length() < 1) return NECTI_UNIMPLEMENTED; + + if (!fMounted) return NECTI_UNIMPLEMENTED; + if (arch != fMounted->Arch()) return NECTI_INVALID_ARCH; + + try { + return this->fMounted->CompileToFormat(sourceFile, arch); + } catch (std::exception& e) { + return NECTI_EXEC_ERROR; + } +} + +///! @brief mount assembly backend. +void AssemblyFactory::Mount(AssemblyInterface* mountPtr) noexcept { + if (mountPtr) { + fMounted = mountPtr; + } +} + +///! @brief Unmount assembler. +AssemblyInterface* AssemblyFactory::Unmount() noexcept { + auto mount_prev = fMounted; + + if (fMounted) { + fMounted = nullptr; + } + + return mount_prev; +} +} // namespace CompilerKit diff --git a/dev/CompilerKit/src/Backend/Assembler32x0.cc b/dev/CompilerKit/src/Backend/Assembler32x0.cc index bc52d25..a7cb022 100644 --- a/dev/CompilerKit/src/Backend/Assembler32x0.cc +++ b/dev/CompilerKit/src/Backend/Assembler32x0.cc @@ -8,7 +8,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -// @file 32asm.cxx +// @file 32asm.cc // @author EL Mahrouss Amlal // @brief 32x0 Assembler. diff --git a/dev/CompilerKit/src/Backend/Assembler64x0.cc b/dev/CompilerKit/src/Backend/Assembler64x0.cc index 511c64d..6bc8842 100644 --- a/dev/CompilerKit/src/Backend/Assembler64x0.cc +++ b/dev/CompilerKit/src/Backend/Assembler64x0.cc @@ -8,7 +8,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -// @file Assembler64x0.cxx +// @file Assembler64x0.cc // @author EL Mahrouss Amlal // @brief 64x000 Assembler. diff --git a/dev/CompilerKit/src/Backend/AssemblerARM64.cc b/dev/CompilerKit/src/Backend/AssemblerARM64.cc index 6890b2c..331e708 100644 --- a/dev/CompilerKit/src/Backend/AssemblerARM64.cc +++ b/dev/CompilerKit/src/Backend/AssemblerARM64.cc @@ -6,7 +6,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -/// @file AssemblerARM64.cxx +/// @file AssemblerARM64.cc /// @author EL Mahrouss Amlal /// @brief 'ACORN' Assembler. diff --git a/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc b/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc index b02a112..bbb5a8b 100644 --- a/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc +++ b/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc @@ -6,7 +6,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -/// @file AssemblerPower.cxx +/// @file AssemblerPower.cc /// @author EL Mahrouss Amlal /// @brief POWER Assembler. diff --git a/dev/CompilerKit/src/BasicString.cc b/dev/CompilerKit/src/BasicString.cc deleted file mode 100644 index dc263aa..0000000 --- a/dev/CompilerKit/src/BasicString.cc +++ /dev/null @@ -1,178 +0,0 @@ -/* - * ======================================================== - * - * CompilerKit - * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. - * - * ======================================================== - */ - -/** - * @file BasicString.cc - * @author Amlal (amlal@nekernel.org) - * @brief C++ string manipulation API. - * @version 0.2 - * @date 2024-01-23 - * - * @copyright Copyright (c) Amlal El Mahrouss - * - */ - -#include - -namespace CompilerKit { - -Char* BasicString::Data() { - return m_Data; -} - -const Char* BasicString::CData() const { - return m_Data; -} - -SizeType BasicString::Length() const { - return strlen(m_Data); -} - -bool BasicString::operator==(const BasicString& rhs) const { - const SizeType len = Length(); - if (rhs.Length() != len) return false; - return memcmp(m_Data, rhs.m_Data, len) == 0; -} - -bool BasicString::operator==(const Char* rhs) const { - const SizeType rhs_len = string_length(rhs); - const SizeType len = Length(); - if (rhs_len != len) return false; - return memcmp(m_Data, rhs, len) == 0; -} - -bool BasicString::operator!=(const BasicString& rhs) const { - return !(*this == rhs); -} - -bool BasicString::operator!=(const Char* rhs) const { - return !(*this == rhs); -} - -BasicString StringBuilder::Construct(const Char* data) { - if (!data || *data == 0) return BasicString(0); - - BasicString view(strlen(data)); - view += data; - - return view; -} - -BasicString StringBuilder::FromInt(const char* fmt, int i) { - if (!fmt) return BasicString(0); - - Char result[sizeof(int64_t)] = {0}; - if (!to_str(result, sizeof(int64_t), i)) return BasicString(0); - - const SizeType fmt_len = string_length(fmt); - const SizeType res_len = string_length(result); - - BasicString output(fmt_len + res_len); - bool inserted = false; - - for (SizeType idx = 0; idx < fmt_len; ++idx) { - if (!inserted && fmt[idx] == '%') { - output += result; - inserted = true; - continue; - } - output += Char{fmt[idx]}; - } - - return output; -} - -BasicString StringBuilder::FromBool(const char* fmt, bool val) { - if (!fmt) return BasicString(0); - - const Char* boolean_expr = val ? "true" : "false"; - const SizeType fmt_len = string_length(fmt); - const SizeType res_len = string_length(boolean_expr); - - BasicString output(fmt_len + res_len); - bool inserted = false; - - for (SizeType idx = 0; idx < fmt_len; ++idx) { - if (!inserted && fmt[idx] == '%') { - output += boolean_expr; - inserted = true; - continue; - } - output += Char{fmt[idx]}; - } - - return output; -} - -bool StringBuilder::Equals(const char* lhs, const char* rhs) { - const SizeType lhs_len = string_length(lhs); - const SizeType rhs_len = string_length(rhs); - - if (lhs_len != rhs_len) return false; - return memcmp(lhs, rhs, lhs_len) == 0; -} - -BasicString StringBuilder::Format(const char* fmt, const char* fmtRight) { - if (!fmt || !fmtRight) return BasicString(0); - - const SizeType fmt_len = string_length(fmt); - const SizeType rhs_len = string_length(fmtRight); - - BasicString output(fmt_len + rhs_len); - bool inserted = false; - - for (SizeType idx = 0; idx < fmt_len; ++idx) { - if (!inserted && fmt[idx] == '%') { - output += fmtRight; - inserted = true; - continue; - } - output += Char{fmt[idx]}; - } - - return output; -} - -BasicString& BasicString::operator+=(const Char* rhs) { - const SizeType rhs_len = strlen(rhs); - if (this->m_Cur + rhs_len >= this->m_Sz) { - throw std::runtime_error("out_of_bounds: BasicString"); - } - - memcpy(this->m_Data + this->m_Cur, rhs, rhs_len); - this->m_Cur += rhs_len; - this->m_Data[this->m_Cur] = '\0'; - - return *this; -} - -BasicString& BasicString::operator+=(const BasicString& rhs) { - if (this->m_Cur + rhs.m_Cur >= this->m_Sz) { - throw std::runtime_error("out_of_bounds: BasicString"); - } - - memcpy(this->m_Data + this->m_Cur, rhs.CData(), rhs.m_Cur); - this->m_Cur += rhs.m_Cur; - this->m_Data[this->m_Cur] = '\0'; - - return *this; -} - -BasicString& BasicString::operator+=(Char ch) { - if (this->m_Cur + 1 >= this->m_Sz) { - throw std::runtime_error("out_of_bounds.."); - } - - this->m_Data[this->m_Cur++] = ch; - this->m_Data[this->m_Cur] = '\0'; - - return *this; -} - -} // namespace CompilerKit diff --git a/dev/CompilerKit/src/CodeGen.cc b/dev/CompilerKit/src/CodeGen.cc deleted file mode 100644 index 693d7a5..0000000 --- a/dev/CompilerKit/src/CodeGen.cc +++ /dev/null @@ -1,52 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved - -------------------------------------------- */ - -#include -#include - -/** - * @file CodeGen.cc - * @author Amlal El Mahrouss (amlal@nekernel.org) - * @brief CodeGen API of NeCTI - * @version 0.0.2 - * - * @copyright Copyright (c) 2024-2025 Amlal El Mahrouss - * - */ - -namespace CompilerKit { -///! @brief Compile for specific format (ELF, PEF, ZBIN) -Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) noexcept { - if (sourceFile.length() < 1) return NECTI_UNIMPLEMENTED; - - if (!fMounted) return NECTI_UNIMPLEMENTED; - if (arch != fMounted->Arch()) return NECTI_INVALID_ARCH; - - try { - return this->fMounted->CompileToFormat(sourceFile, arch); - } catch (std::exception& e) { - return NECTI_EXEC_ERROR; - } -} - -///! @brief mount assembly backend. -void AssemblyFactory::Mount(AssemblyInterface* mountPtr) noexcept { - if (mountPtr) { - fMounted = mountPtr; - } -} - -///! @brief Unmount assembler. -AssemblyInterface* AssemblyFactory::Unmount() noexcept { - auto mount_prev = fMounted; - - if (fMounted) { - fMounted = nullptr; - } - - return mount_prev; -} -} // namespace CompilerKit diff --git a/dev/CompilerKit/src/Frontend.cc b/dev/CompilerKit/src/Frontend.cc deleted file mode 100644 index 37b36f7..0000000 --- a/dev/CompilerKit/src/Frontend.cc +++ /dev/null @@ -1,51 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved - -------------------------------------------- */ - -#include - -namespace CompilerKit { -/// 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(STLString haystack, STLString needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == STLString::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. -SizeType find_word_range(STLString haystack, STLString needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == STLString::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 STLString::npos; -} -} // namespace CompilerKit \ No newline at end of file diff --git a/dev/CompilerKit/src/Frontend/CCompiler64x0.cc b/dev/CompilerKit/src/Frontend/CCompiler64x0.cc index 6ee7e32..1221521 100644 --- a/dev/CompilerKit/src/Frontend/CCompiler64x0.cc +++ b/dev/CompilerKit/src/Frontend/CCompiler64x0.cc @@ -28,7 +28,7 @@ /* (c) Amlal El Mahrouss */ /// @author EL Mahrouss Amlal (amlel) -/// @file 64x0-cc.cxx +/// @file 64x0-cc.cc /// @brief 64x0 C Compiler. /// TODO: support structures, else if, else, . and -> diff --git a/dev/CompilerKit/src/Frontend/CCompilerARM64.cc b/dev/CompilerKit/src/Frontend/CCompilerARM64.cc index 1c1582f..6795c67 100644 --- a/dev/CompilerKit/src/Frontend/CCompilerARM64.cc +++ b/dev/CompilerKit/src/Frontend/CCompilerARM64.cc @@ -29,7 +29,7 @@ /* (c) Amlal El Mahrouss */ /// @author EL Mahrouss Amlal (amlel) -/// @file ARM64-cc.cxx +/// @file ARM64-cc.cc /// @brief ARM64 C Compiler. /// TODO: support structures, else if, else, . and -> diff --git a/dev/CompilerKit/src/Frontend/CCompilerPower64.cc b/dev/CompilerKit/src/Frontend/CCompilerPower64.cc index 40598c9..1999f48 100644 --- a/dev/CompilerKit/src/Frontend/CCompilerPower64.cc +++ b/dev/CompilerKit/src/Frontend/CCompilerPower64.cc @@ -23,7 +23,7 @@ #define kExitOK 0 /// @author EL Mahrouss Amlal (amlal@nekernel.org) -/// @file cc.cxx +/// @file cc.cc /// @brief POWER64 C Compiler. ///////////////////// diff --git a/dev/CompilerKit/src/FrontendHelpers.cc b/dev/CompilerKit/src/FrontendHelpers.cc new file mode 100644 index 0000000..37b36f7 --- /dev/null +++ b/dev/CompilerKit/src/FrontendHelpers.cc @@ -0,0 +1,51 @@ +/* ------------------------------------------- + + Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#include + +namespace CompilerKit { +/// 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(STLString haystack, STLString needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == STLString::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. +SizeType find_word_range(STLString haystack, STLString needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == STLString::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 STLString::npos; +} +} // namespace CompilerKit \ No newline at end of file diff --git a/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc b/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc index 0aa2b78..394014c 100644 --- a/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc +++ b/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc @@ -16,7 +16,7 @@ #include #include -#include +#include #include #include #include @@ -317,7 +317,6 @@ NECTI_MODULE(DynamicLinker64PEF) { if (kVerbose) { kConsoleOut << "Record: " << ae_records[ae_record_index].fName << " is marked.\n"; - kConsoleOut << "Offset: " << command_header.Offset << "\n"; } @@ -660,7 +659,7 @@ NECTI_MODULE(DynamicLinker64PEF) { if ((!kStartFound || kDuplicateSymbols) && (std::filesystem::exists(kOutput) || !unreferenced_symbols.empty())) { if (kVerbose) { - kConsoleOut << "File: " << kOutput << ", is corrupt, removing file...\n"; + kConsoleOut << "File: " << kOutput << " is corrupt now...\n"; } return NECTI_EXEC_ERROR; diff --git a/dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc b/dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc deleted file mode 100644 index 9845967..0000000 --- a/dev/CompilerKit/src/Macro/CPlusPlusCompilerPreProcessor.cc +++ /dev/null @@ -1,893 +0,0 @@ -/* - * ======================================================== - * - * C++ Preprocessor Driver - * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. - * - * ======================================================== - */ - -/// BUGS: 0 - -#include -#include -#include -#include -#include -#include -#include -#include - -#define kMacroPrefix '#' - -/// @author EL Mahrouss Amlal (amlel) -/// @file bpp.cxx -/// @brief Preprocessor. - -typedef Int32 (*bpp_parser_fn_t)(CompilerKit::STLString& line, std::ifstream& hdr_file, - std::ofstream& pp_out); - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Preprocessor internal types. - -///////////////////////////////////////////////////////////////////////////////////////// - -namespace Detail { -enum { - kInvalid = 0, - kEqual = 100, - kGreaterEqThan, - kLesserEqThan, - kGreaterThan, - kLesserThan, - kNotEqual, - kCount = 6, -}; - -struct bpp_macro_condition final { - int32_t fType; - CompilerKit::STLString fTypeName; - - void Print() { - std::cout << "type: " << fType << "\n"; - std::cout << "type_name: " << fTypeName << "\n"; - } -}; - -struct bpp_macro final { - std::vector fArgs; - CompilerKit::STLString fName; - CompilerKit::STLString fValue; - - void Print() { - std::cout << "name: " << fName << "\n"; - std::cout << "value: " << fValue << "\n"; - - for (auto& arg : fArgs) { - std::cout << "arg: " << arg << "\n"; - } - } -}; -} // namespace Detail - -static std::vector kFiles; -static std::vector kMacros; -static std::vector kIncludes; - -static CompilerKit::STLString kWorkingDir = ""; - -///////////////////////////////////////////////////////////////////////////////////////// - -// @name bpp_parse_if_condition -// @brief parse #if condition - -///////////////////////////////////////////////////////////////////////////////////////// - -int32_t bpp_parse_if_condition(Detail::bpp_macro_condition& cond, Detail::bpp_macro& macro, - bool& inactive_code, bool& defined, - CompilerKit::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) != CompilerKit::STLString::npos) { - if (macro.fValue == "0") { - defined = false; - inactive_code = true; - - return 1; - } - - defined = true; - inactive_code = false; - - return 1; - } - } 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) != CompilerKit::STLString::npos) { - if (substr_macro.find(macro.fValue) != CompilerKit::STLString::npos) { - defined = false; - inactive_code = true; - - return 1; - } - - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - auto substr_macro = macro_str.substr(macro_str.find(macro.fName) + macro.fName.size()); - - CompilerKit::STLString number; - - for (auto& macro_num : kMacros) { - if (substr_macro.find(macro_num.fName) != CompilerKit::STLString::npos) { - for (size_t i = 0; i < macro_num.fName.size(); ++i) { - if (isdigit(macro_num.fValue[i])) { - number += macro_num.fValue[i]; - } else { - number.clear(); - break; - } - } - - break; - } - } - - size_t y = 2; - - /* last try */ - for (; y < macro_str.size(); y++) { - if (isdigit(macro_str[y])) { - for (size_t x = y; x < macro_str.size(); x++) { - if (macro_str[x] == ' ') break; - - number += macro_str[x]; - } - - break; - } - } - - size_t rhs = atol(macro.fValue.c_str()); - size_t lhs = atol(number.c_str()); - - if (lhs == 0) { - number.clear(); - ++y; - - for (; y < macro_str.size(); y++) { - if (isdigit(macro_str[y])) { - for (size_t x = y; x < macro_str.size(); x++) { - if (macro_str[x] == ' ') break; - - number += macro_str[x]; - } - - break; - } - } - - lhs = atol(number.c_str()); - } - - if (cond.fType == Detail::kGreaterThan) { - if (lhs < rhs) { - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - if (cond.fType == Detail::kGreaterEqThan) { - if (lhs <= rhs) { - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - if (cond.fType == Detail::kLesserEqThan) { - if (lhs >= rhs) { - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - if (cond.fType == Detail::kLesserThan) { - if (lhs > rhs) { - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - return 0; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief stores every included file here. - -///////////////////////////////////////////////////////////////////////////////////////// - -std::vector kAllIncludes; - -///////////////////////////////////////////////////////////////////////////////////////// - -// @name bpp_parse_file -// @brief parse file to preprocess it. - -///////////////////////////////////////////////////////////////////////////////////////// - -void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { - CompilerKit::STLString hdr_line; - CompilerKit::STLString line_after_include; - - bool inactive_code = false; - bool defined = false; - - try { - while (std::getline(hdr_file, hdr_line)) { - if (inactive_code) { - if (hdr_line.find("#endif") == CompilerKit::STLString::npos) { - continue; - } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("#endif") != CompilerKit::STLString::npos) { - inactive_code = false; - } - } - - if (hdr_line.find("*/") != CompilerKit::STLString::npos) { - hdr_line.erase(hdr_line.find("*/"), strlen("*/")); - } - - if (hdr_line.find("/*") != CompilerKit::STLString::npos) { - inactive_code = true; - - // get rid of comment. - hdr_line.erase(hdr_line.find("/*")); - } - - if (hdr_line[0] == kMacroPrefix && hdr_line.find("endif") != CompilerKit::STLString::npos) { - if (!defined && inactive_code) { - inactive_code = false; - defined = false; - - continue; - } - - continue; - } - - if (!defined && inactive_code) { - continue; - } - - if (defined && inactive_code) { - continue; - } - - for (auto macro : kMacros) { - if (CompilerKit::find_word(hdr_line, macro.fName)) { - if (hdr_line.substr(hdr_line.find(macro.fName)).find(macro.fName + '(') != - CompilerKit::STLString::npos) { - if (!macro.fArgs.empty()) { - CompilerKit::STLString symbol_val = macro.fValue; - std::vector args; - - size_t x_arg_indx = 0; - - CompilerKit::STLString line_after_define = hdr_line; - CompilerKit::STLString str_arg; - - if (line_after_define.find("(") != CompilerKit::STLString::npos) { - line_after_define.erase(0, line_after_define.find("(") + 1); - - for (auto& subc : line_after_define) { - if (subc == ' ' || subc == '\t') continue; - - if (subc == ',' || subc == ')') { - if (str_arg.empty()) continue; - - args.push_back(str_arg); - - str_arg.clear(); - - continue; - } - - str_arg.push_back(subc); - } - } - - for (auto arg : macro.fArgs) { - if (symbol_val.find(macro.fArgs[x_arg_indx]) != CompilerKit::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; - } else { - throw std::runtime_error("cppdrv: Internal error."); - } - } - - auto len = macro.fName.size(); - len += symbol_val.size(); - len += 2; // ( and ) - - hdr_line.erase(hdr_line.find(")"), 1); - - hdr_line.replace(hdr_line.find(hdr_line.substr(hdr_line.find(macro.fName + '('))), - len, symbol_val); - } else { - auto value = macro.fValue; - - hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), value); - } - } - } - } - - if (hdr_line[0] == kMacroPrefix && hdr_line.find("define ") != CompilerKit::STLString::npos) { - auto line_after_define = hdr_line.substr(hdr_line.find("define ") + strlen("define ")); - - CompilerKit::STLString macro_value; - CompilerKit::STLString macro_key; - - std::size_t pos = 0UL; - - std::vector args; - bool on_args = false; - - for (auto& ch : line_after_define) { - ++pos; - - if (ch == '(') { - on_args = true; - continue; - } - - if (ch == ')') { - on_args = false; - continue; - } - - if (ch == '\\') continue; - - if (on_args) continue; - - if (ch == ' ') { - for (size_t i = pos; i < line_after_define.size(); i++) { - macro_value += line_after_define[i]; - } - - break; - } - - macro_key += ch; - } - - CompilerKit::STLString str; - - if (line_after_define.find("(") != CompilerKit::STLString::npos) { - line_after_define.erase(0, line_after_define.find("(") + 1); - - for (auto& subc : line_after_define) { - if (subc == ',' || subc == ')') { - if (str.empty()) continue; - - args.push_back(str); - - str.clear(); - - continue; - } - - str.push_back(subc); - } - } - - Detail::bpp_macro macro; - - macro.fArgs = args; - macro.fName = macro_key; - macro.fValue = macro_value; - - kMacros.emplace_back(macro); - - continue; - } - - if (hdr_line[0] != kMacroPrefix) { - if (inactive_code) { - continue; - } - - pp_out << hdr_line << std::endl; - - continue; - } - - if (hdr_line[0] == kMacroPrefix && hdr_line.find("ifndef") != CompilerKit::STLString::npos) { - auto line_after_ifndef = hdr_line.substr(hdr_line.find("ifndef") + strlen("ifndef") + 1); - CompilerKit::STLString macro; - - for (auto& ch : line_after_ifndef) { - if (ch == ' ') { - break; - } - - macro += ch; - } - - if (macro == "0") { - defined = true; - inactive_code = false; - continue; - } - - if (macro == "1") { - defined = false; - inactive_code = true; - - continue; - } - - bool found = false; - - defined = true; - inactive_code = false; - - for (auto& macro_ref : kMacros) { - if (hdr_line.find(macro_ref.fName) != CompilerKit::STLString::npos) { - found = true; - break; - } - } - - if (found) { - defined = false; - inactive_code = true; - - continue; - } - } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("else") != CompilerKit::STLString::npos) { - if (!defined && inactive_code) { - inactive_code = false; - defined = true; - - continue; - } else { - defined = false; - inactive_code = true; - - continue; - } - } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("ifdef") != CompilerKit::STLString::npos) { - auto line_after_ifdef = hdr_line.substr(hdr_line.find("ifdef") + strlen("ifdef") + 1); - CompilerKit::STLString macro; - - for (auto& ch : line_after_ifdef) { - if (ch == ' ') { - break; - } - - macro += ch; - } - - if (macro == "0") { - defined = false; - inactive_code = true; - - continue; - } - - if (macro == "1") { - defined = true; - inactive_code = false; - - continue; - } - - defined = false; - inactive_code = true; - - for (auto& macro_ref : kMacros) { - if (hdr_line.find(macro_ref.fName) != CompilerKit::STLString::npos) { - defined = true; - inactive_code = false; - - break; - } - } - } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("if") != CompilerKit::STLString::npos) { - inactive_code = true; - - std::vector bpp_macro_condition_list = { - { - .fType = Detail::kEqual, - .fTypeName = "==", - }, - { - .fType = Detail::kNotEqual, - .fTypeName = "!=", - }, - { - .fType = Detail::kLesserThan, - .fTypeName = "<", - }, - { - .fType = Detail::kGreaterThan, - .fTypeName = ">", - }, - { - .fType = Detail::kLesserEqThan, - .fTypeName = "<=", - }, - { - .fType = Detail::kGreaterEqThan, - .fTypeName = ">=", - }, - }; - - int32_t good_to_go = 0; - - for (auto& macro_condition : bpp_macro_condition_list) { - if (hdr_line.find(macro_condition.fTypeName) != CompilerKit::STLString::npos) { - for (auto& found_macro : kMacros) { - if (hdr_line.find(found_macro.fName) != CompilerKit::STLString::npos) { - good_to_go = bpp_parse_if_condition(macro_condition, found_macro, inactive_code, - defined, hdr_line); - - break; - } - } - } - } - - if (good_to_go) continue; - - auto line_after_if = hdr_line.substr(hdr_line.find("if") + strlen("if") + 1); - CompilerKit::STLString macro; - - for (auto& ch : line_after_if) { - if (ch == ' ') { - break; - } - - macro += ch; - } - - if (macro == "0") { - defined = false; - inactive_code = true; - continue; - } - - if (macro == "1") { - defined = true; - inactive_code = false; - - continue; - } - - // last try, is it defined to be one? - for (auto& macro_ref : kMacros) { - if (macro_ref.fName.find(macro) != CompilerKit::STLString::npos && - macro_ref.fValue == "1") { - inactive_code = false; - defined = true; - - break; - } - } - } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("warning") != CompilerKit::STLString::npos) { - auto line_after_warning = hdr_line.substr(hdr_line.find("warning") + strlen("warning") + 1); - CompilerKit::STLString message; - - for (auto& ch : line_after_warning) { - if (ch == '\r' || ch == '\n') { - break; - } - - message += ch; - } - - std::cout << "warn: " << message << std::endl; - } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("error") != CompilerKit::STLString::npos) { - auto line_after_warning = hdr_line.substr(hdr_line.find("error") + strlen("error") + 1); - CompilerKit::STLString message; - - for (auto& ch : line_after_warning) { - if (ch == '\r' || ch == '\n') { - break; - } - - message += ch; - } - - throw std::runtime_error("error: " + message); - } else if (hdr_line[0] == kMacroPrefix && - hdr_line.find("include ") != CompilerKit::STLString::npos) { - line_after_include = hdr_line.substr(hdr_line.find("include ") + strlen("include ")); - - kIncludeFile: - auto it = std::find(kAllIncludes.cbegin(), kAllIncludes.cend(), line_after_include); - - if (it != kAllIncludes.cend()) { - continue; - } - - CompilerKit::STLString path; - - kAllIncludes.push_back(line_after_include); - - bool enable = false; - bool not_local = false; - - for (auto& ch : line_after_include) { - if (ch == ' ') continue; - - if (ch == '<') { - not_local = true; - enable = true; - - continue; - } - - if (ch == '\"') { - not_local = false; - enable = true; - continue; - } - - if (enable) { - path += ch; - } - } - - if (not_local) { - bool open = false; - - if (path.ends_with('>')) { - path.erase(path.find('>')); - } - - if (path.ends_with('"')) { - path.erase(path.find('"')); - } - - for (auto& include : kIncludes) { - CompilerKit::STLString header_path = include; - header_path.push_back('/'); - header_path += path; - - std::ifstream header(header_path); - - if (!header.is_open()) continue; - - open = true; - - bpp_parse_file(header, pp_out); - - break; - } - - if (!open) { - throw std::runtime_error("cppdrv: no such include file: " + path); - } - } else { - std::ifstream header(path); - - if (!header.is_open()) throw std::runtime_error("cppdrv: no such include file: " + path); - - bpp_parse_file(header, pp_out); - } - } else { - std::cerr << ("cppdrv: unknown pre-processor directive, " + hdr_line) << "\n"; - continue; - } - } - } catch (std::out_of_range& oor) { - return; - } -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief main entrypoint of app. - -///////////////////////////////////////////////////////////////////////////////////////// - -NECTI_MODULE(CPlusPlusPreprocessorMain) { - try { - bool skip = false; - bool double_skip = false; - - Detail::bpp_macro macro_1; - - macro_1.fName = "__true"; - macro_1.fValue = "1"; - - kMacros.push_back(macro_1); - - Detail::bpp_macro macro_unreachable; - - macro_unreachable.fName = "__unreachable"; - macro_unreachable.fValue = "__libcompiler_unreachable"; - - kMacros.push_back(macro_unreachable); - - Detail::bpp_macro macro_unused; - - macro_unreachable.fName = "__unused"; - macro_unreachable.fValue = "__libcompiler_unused"; - - kMacros.push_back(macro_unused); - - Detail::bpp_macro macro_0; - - macro_0.fName = "__false"; - macro_0.fValue = "0"; - - kMacros.push_back(macro_0); - - Detail::bpp_macro macro_zka; - - macro_zka.fName = "__NECTI__"; - macro_zka.fValue = "1"; - - kMacros.push_back(macro_zka); - - Detail::bpp_macro macro_cxx; - - macro_cxx.fName = "__cplusplus"; - macro_cxx.fValue = "202302L"; - - kMacros.push_back(macro_cxx); - - Detail::bpp_macro macro_size_t; - macro_size_t.fName = "__SIZE_TYPE__"; - macro_size_t.fValue = "unsigned long long int"; - - kMacros.push_back(macro_size_t); - - macro_size_t.fName = "__UINT32_TYPE__"; - macro_size_t.fValue = "unsigned int"; - - kMacros.push_back(macro_size_t); - - macro_size_t.fName = "__UINTPTR_TYPE__"; - macro_size_t.fValue = "unsigned long long int"; - - kMacros.push_back(macro_size_t); - - for (auto index = 1UL; index < argc; ++index) { - if (skip) { - skip = false; - continue; - } - - if (double_skip) { - ++index; - double_skip = false; - continue; - } - - if (argv[index][0] == '-') { - if (strcmp(argv[index], "-cpp-ver") == 0) { - printf("%s\n", - "NeKernel Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2025 all rights " - "reserved."); - - return NECTI_SUCCESS; - } - - if (strcmp(argv[index], "-cpp-help") == 0) { - printf("%s\n", - "NeKernel Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2025 all rights " - "reserved."); - printf("%s\n", "-cpp-working-dir : set directory to working path."); - printf("%s\n", "-cpp-include-dir : add directory to include path."); - printf("%s\n", "-cpp-def : define a macro."); - printf("%s\n", "-cpp-ver: print the version."); - printf("%s\n", "-cpp-help: show help (this current command)."); - - return NECTI_SUCCESS; - } - - if (strcmp(argv[index], "-cpp-include-dir") == 0) { - CompilerKit::STLString inc = argv[index + 1]; - - skip = true; - - kIncludes.push_back(inc); - } - - if (strcmp(argv[index], "-cpp-working-dir") == 0) { - CompilerKit::STLString inc = argv[index + 1]; - skip = true; - kWorkingDir = inc; - } - - if (strcmp(argv[index], "-cpp-def") == 0 && argv[index + 1] != nullptr && - argv[index + 2] != nullptr) { - CompilerKit::STLString macro_key = argv[index + 1]; - - CompilerKit::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])) { - is_string = true; - macro_value += "\""; - - break; - } - } - - macro_value += argv[index + 2]; - - if (is_string) macro_value += "\""; - - Detail::bpp_macro macro; - macro.fName = macro_key; - macro.fValue = macro_value; - - kMacros.push_back(macro); - - double_skip = true; - } - - continue; - } - - kFiles.emplace_back(argv[index]); - } - - if (kFiles.empty()) return NECTI_EXEC_ERROR; - - for (auto& file : kFiles) { - if (!std::filesystem::exists(file)) continue; - - std::ifstream file_descriptor(file); - std::ofstream file_descriptor_pp(file + ".pp"); - - bpp_parse_file(file_descriptor, file_descriptor_pp); - } - - return NECTI_SUCCESS; - } catch (const std::runtime_error& e) { - std::cout << e.what() << '\n'; - } - - return NECTI_EXEC_ERROR; -} - -// Last rev 8-1-24 diff --git a/dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc b/dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc new file mode 100644 index 0000000..aaa5793 --- /dev/null +++ b/dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc @@ -0,0 +1,893 @@ +/* + * ======================================================== + * + * C++ Preprocessor Driver + * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. + * + * ======================================================== + */ + +/// BUGS: 0 + +#include +#include +#include +#include +#include +#include +#include +#include + +#define kMacroPrefix '#' + +/// @author EL Mahrouss Amlal (amlel) +/// @file CPlusPlusPreprocessor.cc +/// @brief Preprocessor. + +typedef Int32 (*bpp_parser_fn_t)(CompilerKit::STLString& line, std::ifstream& hdr_file, + std::ofstream& pp_out); + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Preprocessor internal types. + +///////////////////////////////////////////////////////////////////////////////////////// + +namespace Detail { +enum { + kInvalid = 0, + kEqual = 100, + kGreaterEqThan, + kLesserEqThan, + kGreaterThan, + kLesserThan, + kNotEqual, + kCount = 6, +}; + +struct bpp_macro_condition final { + int32_t fType; + CompilerKit::STLString fTypeName; + + void Print() { + std::cout << "type: " << fType << "\n"; + std::cout << "type_name: " << fTypeName << "\n"; + } +}; + +struct bpp_macro final { + std::vector fArgs; + CompilerKit::STLString fName; + CompilerKit::STLString fValue; + + void Print() { + std::cout << "name: " << fName << "\n"; + std::cout << "value: " << fValue << "\n"; + + for (auto& arg : fArgs) { + std::cout << "arg: " << arg << "\n"; + } + } +}; +} // namespace Detail + +static std::vector kFiles; +static std::vector kMacros; +static std::vector kIncludes; + +static CompilerKit::STLString kWorkingDir = ""; + +///////////////////////////////////////////////////////////////////////////////////////// + +// @name bpp_parse_if_condition +// @brief parse #if condition + +///////////////////////////////////////////////////////////////////////////////////////// + +int32_t bpp_parse_if_condition(Detail::bpp_macro_condition& cond, Detail::bpp_macro& macro, + bool& inactive_code, bool& defined, + CompilerKit::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) != CompilerKit::STLString::npos) { + if (macro.fValue == "0") { + defined = false; + inactive_code = true; + + return 1; + } + + defined = true; + inactive_code = false; + + return 1; + } + } 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) != CompilerKit::STLString::npos) { + if (substr_macro.find(macro.fValue) != CompilerKit::STLString::npos) { + defined = false; + inactive_code = true; + + return 1; + } + + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + auto substr_macro = macro_str.substr(macro_str.find(macro.fName) + macro.fName.size()); + + CompilerKit::STLString number; + + for (auto& macro_num : kMacros) { + if (substr_macro.find(macro_num.fName) != CompilerKit::STLString::npos) { + for (size_t i = 0; i < macro_num.fName.size(); ++i) { + if (isdigit(macro_num.fValue[i])) { + number += macro_num.fValue[i]; + } else { + number.clear(); + break; + } + } + + break; + } + } + + size_t y = 2; + + /* last try */ + for (; y < macro_str.size(); y++) { + if (isdigit(macro_str[y])) { + for (size_t x = y; x < macro_str.size(); x++) { + if (macro_str[x] == ' ') break; + + number += macro_str[x]; + } + + break; + } + } + + size_t rhs = atol(macro.fValue.c_str()); + size_t lhs = atol(number.c_str()); + + if (lhs == 0) { + number.clear(); + ++y; + + for (; y < macro_str.size(); y++) { + if (isdigit(macro_str[y])) { + for (size_t x = y; x < macro_str.size(); x++) { + if (macro_str[x] == ' ') break; + + number += macro_str[x]; + } + + break; + } + } + + lhs = atol(number.c_str()); + } + + if (cond.fType == Detail::kGreaterThan) { + if (lhs < rhs) { + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + if (cond.fType == Detail::kGreaterEqThan) { + if (lhs <= rhs) { + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + if (cond.fType == Detail::kLesserEqThan) { + if (lhs >= rhs) { + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + if (cond.fType == Detail::kLesserThan) { + if (lhs > rhs) { + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + return 0; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief stores every included file here. + +///////////////////////////////////////////////////////////////////////////////////////// + +std::vector kAllIncludes; + +///////////////////////////////////////////////////////////////////////////////////////// + +// @name bpp_parse_file +// @brief parse file to preprocess it. + +///////////////////////////////////////////////////////////////////////////////////////// + +void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { + CompilerKit::STLString hdr_line; + CompilerKit::STLString line_after_include; + + bool inactive_code = false; + bool defined = false; + + try { + while (std::getline(hdr_file, hdr_line)) { + if (inactive_code) { + if (hdr_line.find("#endif") == CompilerKit::STLString::npos) { + continue; + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("#endif") != CompilerKit::STLString::npos) { + inactive_code = false; + } + } + + if (hdr_line.find("*/") != CompilerKit::STLString::npos) { + hdr_line.erase(hdr_line.find("*/"), strlen("*/")); + } + + if (hdr_line.find("/*") != CompilerKit::STLString::npos) { + inactive_code = true; + + // get rid of comment. + hdr_line.erase(hdr_line.find("/*")); + } + + if (hdr_line[0] == kMacroPrefix && hdr_line.find("endif") != CompilerKit::STLString::npos) { + if (!defined && inactive_code) { + inactive_code = false; + defined = false; + + continue; + } + + continue; + } + + if (!defined && inactive_code) { + continue; + } + + if (defined && inactive_code) { + continue; + } + + for (auto macro : kMacros) { + if (CompilerKit::find_word(hdr_line, macro.fName)) { + if (hdr_line.substr(hdr_line.find(macro.fName)).find(macro.fName + '(') != + CompilerKit::STLString::npos) { + if (!macro.fArgs.empty()) { + CompilerKit::STLString symbol_val = macro.fValue; + std::vector args; + + size_t x_arg_indx = 0; + + CompilerKit::STLString line_after_define = hdr_line; + CompilerKit::STLString str_arg; + + if (line_after_define.find("(") != CompilerKit::STLString::npos) { + line_after_define.erase(0, line_after_define.find("(") + 1); + + for (auto& subc : line_after_define) { + if (subc == ' ' || subc == '\t') continue; + + if (subc == ',' || subc == ')') { + if (str_arg.empty()) continue; + + args.push_back(str_arg); + + str_arg.clear(); + + continue; + } + + str_arg.push_back(subc); + } + } + + for (auto arg : macro.fArgs) { + if (symbol_val.find(macro.fArgs[x_arg_indx]) != CompilerKit::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; + } else { + throw std::runtime_error("cppdrv: Internal error."); + } + } + + auto len = macro.fName.size(); + len += symbol_val.size(); + len += 2; // ( and ) + + hdr_line.erase(hdr_line.find(")"), 1); + + hdr_line.replace(hdr_line.find(hdr_line.substr(hdr_line.find(macro.fName + '('))), + len, symbol_val); + } else { + auto value = macro.fValue; + + hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), value); + } + } + } + } + + if (hdr_line[0] == kMacroPrefix && hdr_line.find("define ") != CompilerKit::STLString::npos) { + auto line_after_define = hdr_line.substr(hdr_line.find("define ") + strlen("define ")); + + CompilerKit::STLString macro_value; + CompilerKit::STLString macro_key; + + std::size_t pos = 0UL; + + std::vector args; + bool on_args = false; + + for (auto& ch : line_after_define) { + ++pos; + + if (ch == '(') { + on_args = true; + continue; + } + + if (ch == ')') { + on_args = false; + continue; + } + + if (ch == '\\') continue; + + if (on_args) continue; + + if (ch == ' ') { + for (size_t i = pos; i < line_after_define.size(); i++) { + macro_value += line_after_define[i]; + } + + break; + } + + macro_key += ch; + } + + CompilerKit::STLString str; + + if (line_after_define.find("(") != CompilerKit::STLString::npos) { + line_after_define.erase(0, line_after_define.find("(") + 1); + + for (auto& subc : line_after_define) { + if (subc == ',' || subc == ')') { + if (str.empty()) continue; + + args.push_back(str); + + str.clear(); + + continue; + } + + str.push_back(subc); + } + } + + Detail::bpp_macro macro; + + macro.fArgs = args; + macro.fName = macro_key; + macro.fValue = macro_value; + + kMacros.emplace_back(macro); + + continue; + } + + if (hdr_line[0] != kMacroPrefix) { + if (inactive_code) { + continue; + } + + pp_out << hdr_line << std::endl; + + continue; + } + + if (hdr_line[0] == kMacroPrefix && hdr_line.find("ifndef") != CompilerKit::STLString::npos) { + auto line_after_ifndef = hdr_line.substr(hdr_line.find("ifndef") + strlen("ifndef") + 1); + CompilerKit::STLString macro; + + for (auto& ch : line_after_ifndef) { + if (ch == ' ') { + break; + } + + macro += ch; + } + + if (macro == "0") { + defined = true; + inactive_code = false; + continue; + } + + if (macro == "1") { + defined = false; + inactive_code = true; + + continue; + } + + bool found = false; + + defined = true; + inactive_code = false; + + for (auto& macro_ref : kMacros) { + if (hdr_line.find(macro_ref.fName) != CompilerKit::STLString::npos) { + found = true; + break; + } + } + + if (found) { + defined = false; + inactive_code = true; + + continue; + } + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("else") != CompilerKit::STLString::npos) { + if (!defined && inactive_code) { + inactive_code = false; + defined = true; + + continue; + } else { + defined = false; + inactive_code = true; + + continue; + } + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("ifdef") != CompilerKit::STLString::npos) { + auto line_after_ifdef = hdr_line.substr(hdr_line.find("ifdef") + strlen("ifdef") + 1); + CompilerKit::STLString macro; + + for (auto& ch : line_after_ifdef) { + if (ch == ' ') { + break; + } + + macro += ch; + } + + if (macro == "0") { + defined = false; + inactive_code = true; + + continue; + } + + if (macro == "1") { + defined = true; + inactive_code = false; + + continue; + } + + defined = false; + inactive_code = true; + + for (auto& macro_ref : kMacros) { + if (hdr_line.find(macro_ref.fName) != CompilerKit::STLString::npos) { + defined = true; + inactive_code = false; + + break; + } + } + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("if") != CompilerKit::STLString::npos) { + inactive_code = true; + + std::vector bpp_macro_condition_list = { + { + .fType = Detail::kEqual, + .fTypeName = "==", + }, + { + .fType = Detail::kNotEqual, + .fTypeName = "!=", + }, + { + .fType = Detail::kLesserThan, + .fTypeName = "<", + }, + { + .fType = Detail::kGreaterThan, + .fTypeName = ">", + }, + { + .fType = Detail::kLesserEqThan, + .fTypeName = "<=", + }, + { + .fType = Detail::kGreaterEqThan, + .fTypeName = ">=", + }, + }; + + int32_t good_to_go = 0; + + for (auto& macro_condition : bpp_macro_condition_list) { + if (hdr_line.find(macro_condition.fTypeName) != CompilerKit::STLString::npos) { + for (auto& found_macro : kMacros) { + if (hdr_line.find(found_macro.fName) != CompilerKit::STLString::npos) { + good_to_go = bpp_parse_if_condition(macro_condition, found_macro, inactive_code, + defined, hdr_line); + + break; + } + } + } + } + + if (good_to_go) continue; + + auto line_after_if = hdr_line.substr(hdr_line.find("if") + strlen("if") + 1); + CompilerKit::STLString macro; + + for (auto& ch : line_after_if) { + if (ch == ' ') { + break; + } + + macro += ch; + } + + if (macro == "0") { + defined = false; + inactive_code = true; + continue; + } + + if (macro == "1") { + defined = true; + inactive_code = false; + + continue; + } + + // last try, is it defined to be one? + for (auto& macro_ref : kMacros) { + if (macro_ref.fName.find(macro) != CompilerKit::STLString::npos && + macro_ref.fValue == "1") { + inactive_code = false; + defined = true; + + break; + } + } + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("warning") != CompilerKit::STLString::npos) { + auto line_after_warning = hdr_line.substr(hdr_line.find("warning") + strlen("warning") + 1); + CompilerKit::STLString message; + + for (auto& ch : line_after_warning) { + if (ch == '\r' || ch == '\n') { + break; + } + + message += ch; + } + + std::cout << "warn: " << message << std::endl; + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("error") != CompilerKit::STLString::npos) { + auto line_after_warning = hdr_line.substr(hdr_line.find("error") + strlen("error") + 1); + CompilerKit::STLString message; + + for (auto& ch : line_after_warning) { + if (ch == '\r' || ch == '\n') { + break; + } + + message += ch; + } + + throw std::runtime_error("error: " + message); + } else if (hdr_line[0] == kMacroPrefix && + hdr_line.find("include ") != CompilerKit::STLString::npos) { + line_after_include = hdr_line.substr(hdr_line.find("include ") + strlen("include ")); + + kIncludeFile: + auto it = std::find(kAllIncludes.cbegin(), kAllIncludes.cend(), line_after_include); + + if (it != kAllIncludes.cend()) { + continue; + } + + CompilerKit::STLString path; + + kAllIncludes.push_back(line_after_include); + + bool enable = false; + bool not_local = false; + + for (auto& ch : line_after_include) { + if (ch == ' ') continue; + + if (ch == '<') { + not_local = true; + enable = true; + + continue; + } + + if (ch == '\"') { + not_local = false; + enable = true; + continue; + } + + if (enable) { + path += ch; + } + } + + if (not_local) { + bool open = false; + + if (path.ends_with('>')) { + path.erase(path.find('>')); + } + + if (path.ends_with('"')) { + path.erase(path.find('"')); + } + + for (auto& include : kIncludes) { + CompilerKit::STLString header_path = include; + header_path.push_back('/'); + header_path += path; + + std::ifstream header(header_path); + + if (!header.is_open()) continue; + + open = true; + + bpp_parse_file(header, pp_out); + + break; + } + + if (!open) { + throw std::runtime_error("cppdrv: no such include file: " + path); + } + } else { + std::ifstream header(path); + + if (!header.is_open()) throw std::runtime_error("cppdrv: no such include file: " + path); + + bpp_parse_file(header, pp_out); + } + } else { + std::cerr << ("cppdrv: unknown pre-processor directive, " + hdr_line) << "\n"; + continue; + } + } + } catch (std::out_of_range& oor) { + return; + } +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief main entrypoint of app. + +///////////////////////////////////////////////////////////////////////////////////////// + +NECTI_MODULE(CPlusPlusPreprocessorMain) { + try { + bool skip = false; + bool double_skip = false; + + Detail::bpp_macro macro_1; + + macro_1.fName = "__true"; + macro_1.fValue = "1"; + + kMacros.push_back(macro_1); + + Detail::bpp_macro macro_unreachable; + + macro_unreachable.fName = "__unreachable"; + macro_unreachable.fValue = "__libcompiler_unreachable"; + + kMacros.push_back(macro_unreachable); + + Detail::bpp_macro macro_unused; + + macro_unreachable.fName = "__unused"; + macro_unreachable.fValue = "__libcompiler_unused"; + + kMacros.push_back(macro_unused); + + Detail::bpp_macro macro_0; + + macro_0.fName = "__false"; + macro_0.fValue = "0"; + + kMacros.push_back(macro_0); + + Detail::bpp_macro macro_zka; + + macro_zka.fName = "__NECTI__"; + macro_zka.fValue = "1"; + + kMacros.push_back(macro_zka); + + Detail::bpp_macro macro_cxx; + + macro_cxx.fName = "__cplusplus"; + macro_cxx.fValue = "202302L"; + + kMacros.push_back(macro_cxx); + + Detail::bpp_macro macro_size_t; + macro_size_t.fName = "__SIZE_TYPE__"; + macro_size_t.fValue = "unsigned long long int"; + + kMacros.push_back(macro_size_t); + + macro_size_t.fName = "__UINT32_TYPE__"; + macro_size_t.fValue = "unsigned int"; + + kMacros.push_back(macro_size_t); + + macro_size_t.fName = "__UINTPTR_TYPE__"; + macro_size_t.fValue = "unsigned long long int"; + + kMacros.push_back(macro_size_t); + + for (auto index = 1UL; index < argc; ++index) { + if (skip) { + skip = false; + continue; + } + + if (double_skip) { + ++index; + double_skip = false; + continue; + } + + if (argv[index][0] == '-') { + if (strcmp(argv[index], "-cpp-ver") == 0) { + printf("%s\n", + "NeKernel Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2025 all rights " + "reserved."); + + return NECTI_SUCCESS; + } + + if (strcmp(argv[index], "-cpp-help") == 0) { + printf("%s\n", + "NeKernel Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2025 all rights " + "reserved."); + printf("%s\n", "-cpp-working-dir : set directory to working path."); + printf("%s\n", "-cpp-include-dir : add directory to include path."); + printf("%s\n", "-cpp-def : define a macro."); + printf("%s\n", "-cpp-ver: print the version."); + printf("%s\n", "-cpp-help: show help (this current command)."); + + return NECTI_SUCCESS; + } + + if (strcmp(argv[index], "-cpp-include-dir") == 0) { + CompilerKit::STLString inc = argv[index + 1]; + + skip = true; + + kIncludes.push_back(inc); + } + + if (strcmp(argv[index], "-cpp-working-dir") == 0) { + CompilerKit::STLString inc = argv[index + 1]; + skip = true; + kWorkingDir = inc; + } + + if (strcmp(argv[index], "-cpp-def") == 0 && argv[index + 1] != nullptr && + argv[index + 2] != nullptr) { + CompilerKit::STLString macro_key = argv[index + 1]; + + CompilerKit::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])) { + is_string = true; + macro_value += "\""; + + break; + } + } + + macro_value += argv[index + 2]; + + if (is_string) macro_value += "\""; + + Detail::bpp_macro macro; + macro.fName = macro_key; + macro.fValue = macro_value; + + kMacros.push_back(macro); + + double_skip = true; + } + + continue; + } + + kFiles.emplace_back(argv[index]); + } + + if (kFiles.empty()) return NECTI_EXEC_ERROR; + + for (auto& file : kFiles) { + if (!std::filesystem::exists(file)) continue; + + std::ifstream file_descriptor(file); + std::ofstream file_descriptor_pp(file + ".pp"); + + bpp_parse_file(file_descriptor, file_descriptor_pp); + } + + return NECTI_SUCCESS; + } catch (const std::runtime_error& e) { + std::cout << e.what() << '\n'; + } + + return NECTI_EXEC_ERROR; +} + +// Last rev 8-1-24 diff --git a/dev/CompilerKit/src/StringKit.cc b/dev/CompilerKit/src/StringKit.cc new file mode 100644 index 0000000..13142a6 --- /dev/null +++ b/dev/CompilerKit/src/StringKit.cc @@ -0,0 +1,179 @@ +/* + * ======================================================== + * + * CompilerKit + * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. + * + * ======================================================== + */ + +/** + * @file BasicString.cc + * @author Amlal (amlal@nekernel.org) + * @brief C++ string manipulation API. + * @version 0.2 + * @date 2024-01-23 + * + * @copyright Copyright (c) Amlal El Mahrouss + * + */ + +#include + +namespace CompilerKit { + +Char* BasicString::Data() { + return m_Data; +} + +const Char* BasicString::CData() const { + return m_Data; +} + +SizeType BasicString::Length() const { + return strlen(m_Data); +} + +bool BasicString::operator==(const BasicString& rhs) const { + const SizeType len = Length(); + if (rhs.Length() != len) return false; + return memcmp(m_Data, rhs.m_Data, len) == 0; +} + +bool BasicString::operator==(const Char* rhs) const { + const SizeType rhs_len = string_length(rhs); + const SizeType len = Length(); + if (rhs_len != len) return false; + return memcmp(m_Data, rhs, len) == 0; +} + +bool BasicString::operator!=(const BasicString& rhs) const { + return !(*this == rhs); +} + +bool BasicString::operator!=(const Char* rhs) const { + return !(*this == rhs); +} + +BasicString StringBuilder::Construct(const Char* data) { + if (!data || *data == 0) return BasicString(0); + + BasicString view(strlen(data)); + view += data; + + return view; +} + +BasicString StringBuilder::FromInt(const char* fmt, int i) { + if (!fmt) return BasicString(0); + + Char result[sizeof(int64_t)] = {0}; + if (!to_str(result, sizeof(int64_t), i)) return BasicString(0); + + const SizeType fmt_len = string_length(fmt); + const SizeType res_len = string_length(result); + + BasicString output(fmt_len + res_len); + bool inserted = false; + + for (SizeType idx = 0; idx < fmt_len; ++idx) { + if (!inserted && fmt[idx] == '%') { + output += result; + inserted = true; + continue; + } + output += Char{fmt[idx]}; + } + + return output; +} + +BasicString StringBuilder::FromBool(const char* fmt, bool val) { + if (!fmt) return BasicString(0); + + const Char* boolean_expr = val ? "true" : "false"; + const SizeType fmt_len = string_length(fmt); + const SizeType res_len = string_length(boolean_expr); + + BasicString output(fmt_len + res_len); + bool inserted = false; + + for (SizeType idx = 0; idx < fmt_len; ++idx) { + if (!inserted && fmt[idx] == '%') { + output += boolean_expr; + inserted = true; + continue; + } + output += Char{fmt[idx]}; + } + + return output; +} + +bool StringBuilder::Equals(const char* lhs, const char* rhs) { + const SizeType lhs_len = string_length(lhs); + const SizeType rhs_len = string_length(rhs); + + if (lhs_len != rhs_len) return false; + return memcmp(lhs, rhs, lhs_len) == 0; +} + +BasicString StringBuilder::Format(const char* fmt, const char* fmtRight) { + if (!fmt || !fmtRight) return BasicString(0); + + const SizeType fmt_len = string_length(fmt); + const SizeType rhs_len = string_length(fmtRight); + + BasicString output(fmt_len + rhs_len); + bool inserted = false; + + for (SizeType idx = 0; idx < fmt_len; ++idx) { + if (!inserted && fmt[idx] == '%') { + output += fmtRight; + inserted = true; + continue; + } + output += Char{fmt[idx]}; + } + + return output; +} + +BasicString& BasicString::operator+=(const Char* rhs) { + const SizeType rhs_len = strlen(rhs); + if (this->m_Cur + rhs_len >= this->m_Sz) { + throw std::runtime_error("out_of_bounds: BasicString"); + } + + memcpy(this->m_Data + this->m_Cur, rhs, rhs_len); + + this->m_Cur += rhs_len; + this->m_Data[this->m_Cur] = '\0'; + + return *this; +} + +BasicString& BasicString::operator+=(const BasicString& rhs) { + if (this->m_Cur + rhs.m_Cur >= this->m_Sz) { + throw std::runtime_error("out_of_bounds: BasicString"); + } + + memcpy(this->m_Data + this->m_Cur, rhs.CData(), rhs.m_Cur); + this->m_Cur += rhs.m_Cur; + this->m_Data[this->m_Cur] = '\0'; + + return *this; +} + +BasicString& BasicString::operator+=(const Char ch) { + if (this->m_Cur + 1 >= this->m_Sz) { + throw std::runtime_error("out_of_bounds.."); + } + + this->m_Data[this->m_Cur++] = ch; + this->m_Data[this->m_Cur] = '\0'; + + return *this; +} + +} // namespace CompilerKit diff --git a/dev/CompilerKit/utils/AsmUtils.h b/dev/CompilerKit/utils/AsmUtils.h index f74fb1b..889d359 100644 --- a/dev/CompilerKit/utils/AsmUtils.h +++ b/dev/CompilerKit/utils/AsmUtils.h @@ -6,9 +6,8 @@ #pragma once -#include +#include #include - #include using namespace CompilerKit; diff --git a/dev/CompilerKit/utils/CompilerUtils.h b/dev/CompilerKit/utils/CompilerUtils.h index bf48b79..f69d117 100644 --- a/dev/CompilerKit/utils/CompilerUtils.h +++ b/dev/CompilerKit/utils/CompilerUtils.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include #include #include diff --git a/docs/drawio/COMPILERKIT_DESIGN.drawio b/docs/drawio/COMPILERKIT_DESIGN.drawio new file mode 100644 index 0000000..78934b6 --- /dev/null +++ b/docs/drawio/COMPILERKIT_DESIGN.drawio @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/drawio/DEBUGGERKIT_DESIGN.drawio b/docs/drawio/DEBUGGERKIT_DESIGN.drawio new file mode 100644 index 0000000..0b3802d --- /dev/null +++ b/docs/drawio/DEBUGGERKIT_DESIGN.drawio @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/drawio/LIBCOMPILER_DESIGN.drawio b/docs/drawio/LIBCOMPILER_DESIGN.drawio deleted file mode 100644 index a13c2a3..0000000 --- a/docs/drawio/LIBCOMPILER_DESIGN.drawio +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/docs/drawio/LIBDEBUGGER_DESIGN.drawio b/docs/drawio/LIBDEBUGGER_DESIGN.drawio deleted file mode 100644 index 0b3802d..0000000 --- a/docs/drawio/LIBDEBUGGER_DESIGN.drawio +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/tools/asm.cc b/tools/asm.cc index f600408..621cb37 100644 --- a/tools/asm.cc +++ b/tools/asm.cc @@ -4,7 +4,7 @@ ------------------------------------------- */ -/// @file asm.cxx +/// @file asm.cc /// @brief Assembler frontend. #include diff --git a/tools/dbg.cc b/tools/dbg.cc index 1ee9c37..4174083 100644 --- a/tools/dbg.cc +++ b/tools/dbg.cc @@ -6,7 +6,7 @@ #include -/// @file dbg.cxx +/// @file dbg.cc /// @brief NE debugger. CK_IMPORT_C Int32 DebuggerMachPOSIX(Int32 argc, Char const* argv[]); diff --git a/tools/kdbg.cc b/tools/kdbg.cc index d7aaa93..8e1f108 100644 --- a/tools/kdbg.cc +++ b/tools/kdbg.cc @@ -6,7 +6,7 @@ #include -/// @file kdbg.cxx +/// @file kdbg.cc /// @brief NeKernel debugger. CK_IMPORT_C Int32 DebuggerNeKernel(Int32 argc, Char const* argv[]); diff --git a/tools/ld64.cc b/tools/ld64.cc index e551fee..982c2fc 100644 --- a/tools/ld64.cc +++ b/tools/ld64.cc @@ -6,7 +6,7 @@ #include -/// @file ld64.cxx +/// @file ld64.cc /// @brief NE Linker for AE objects. CK_IMPORT_C int DynamicLinker64PEF(int argc, char const* argv[]); -- cgit v1.2.3