From f7930b3a1279922cf9e6e75e651fe9b5df247bc6 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 2 Dec 2025 01:39:42 -0500 Subject: chore: source level tweaks and breaking API changes. Signed-off-by: Amlal El Mahrouss --- src/CompilerKit/AE.h | 1 + src/CompilerKit/AST.h | 3 +- src/CompilerKit/CodeGenerator.h | 223 +++++++++++++++++++++ src/CompilerKit/Compiler.h | 222 -------------------- src/CompilerKit/Config.h | 33 ++- src/CompilerKit/ErrorOr.h | 1 - src/CompilerKit/PEF.h | 1 + src/CompilerKit/StringKit.h | 1 + src/CompilerKit/Version.h | 15 -- src/CompilerKit/impl/32x0.h | 1 + src/CompilerKit/impl/X64.h | 1 + src/CompilerKit/src/AssemblyFactory.cc | 2 +- src/CompilerKit/src/Backend/AssemblerARM64.cc | 2 +- src/CompilerKit/src/Backend/AssemblerPowerPC.cc | 2 +- src/CompilerKit/src/Frontend/CCompiler64x0.cc | 2 +- src/CompilerKit/src/Frontend/CCompilerARM64.cc | 2 +- src/CompilerKit/src/Frontend/CCompilerPower64.cc | 2 +- .../src/Frontend/CPlusPlusCompilerAMD64.cc | 1 + src/CompilerKit/src/Linker/DynamicLinker64PEF.cc | 5 +- src/CompilerKit/src/StringKit.cc | 1 + src/CompilerKit/utils/AsmUtils.h | 2 +- src/CompilerKit/utils/CompilerUtils.h | 4 +- 22 files changed, 266 insertions(+), 261 deletions(-) create mode 100644 src/CompilerKit/CodeGenerator.h delete mode 100644 src/CompilerKit/Compiler.h delete mode 100644 src/CompilerKit/Version.h (limited to 'src/CompilerKit') diff --git a/src/CompilerKit/AE.h b/src/CompilerKit/AE.h index a1f56e6..a856689 100644 --- a/src/CompilerKit/AE.h +++ b/src/CompilerKit/AE.h @@ -11,6 +11,7 @@ #define _NECTI_AE_H_ #include +#include #define kAEIdentVersion (0x0122) diff --git a/src/CompilerKit/AST.h b/src/CompilerKit/AST.h index 3cb5a08..fb38ac8 100644 --- a/src/CompilerKit/AST.h +++ b/src/CompilerKit/AST.h @@ -6,7 +6,8 @@ #pragma once -#include +#include +#include #define CK_COMPILER_FRONTEND : public ::CompilerKit::CompilerFrontendInterface diff --git a/src/CompilerKit/CodeGenerator.h b/src/CompilerKit/CodeGenerator.h new file mode 100644 index 0000000..f3f71d0 --- /dev/null +++ b/src/CompilerKit/CodeGenerator.h @@ -0,0 +1,223 @@ +/* ======================================== + + Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license + +======================================== */ + +#pragma once + +#include +#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; +}; + +/// =========================================================== /// +/// @brief Number casting unions for different sizes. +/// =========================================================== /// +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; +}; + +/// =========================================================== /// +/// @brief Assembly encoder interface. +/// =========================================================== /// +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; +}; + +/// =========================================================== /// +/// @brief Different architecture encoders. +/// =========================================================== /// + +#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/src/CompilerKit/Compiler.h b/src/CompilerKit/Compiler.h deleted file mode 100644 index b02f1ef..0000000 --- a/src/CompilerKit/Compiler.h +++ /dev/null @@ -1,222 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license - -======================================== */ - -#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; -}; - -/// =========================================================== /// -/// @brief Number casting unions for different sizes. -/// =========================================================== /// -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; -}; - -/// =========================================================== /// -/// @brief Assembly encoder interface. -/// =========================================================== /// -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; -}; - -/// =========================================================== /// -/// @brief Different architecture encoders. -/// =========================================================== /// - -#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/src/CompilerKit/Config.h b/src/CompilerKit/Config.h index 59efcf0..637d56e 100644 --- a/src/CompilerKit/Config.h +++ b/src/CompilerKit/Config.h @@ -56,18 +56,29 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include -#include -#include +#include + +#define kDistVersion "v0.0.7-compilerkit" +#define kDistVersionBCD 0x0002 + +#define ToString(X) Stringify(X) +#define Stringify(X) #X + +#define kDistRelease ToString(kDistReleaseBranch) + +#ifndef kDistRelease + +#define kDistVersion "v0.0.7-compilerkit" +#define kDistVersionBCD 0x0002 + +#define ToString(X) Stringify(X) +#define Stringify(X) #X + +#define kDistRelease ToString(kDistReleaseBranch) + +#endif // !kDistRelease #define nullPtr std::nullptr_t diff --git a/src/CompilerKit/ErrorOr.h b/src/CompilerKit/ErrorOr.h index 54c1c45..9e1e801 100644 --- a/src/CompilerKit/ErrorOr.h +++ b/src/CompilerKit/ErrorOr.h @@ -18,7 +18,6 @@ #include #include #include -#include namespace CompilerKit { using ErrorT = Int32; diff --git a/src/CompilerKit/PEF.h b/src/CompilerKit/PEF.h index f69102a..ad47efd 100644 --- a/src/CompilerKit/PEF.h +++ b/src/CompilerKit/PEF.h @@ -7,6 +7,7 @@ #pragma once #include +#include // @file PEF.h // @brief Preferred Executable Format diff --git a/src/CompilerKit/StringKit.h b/src/CompilerKit/StringKit.h index e4bc1c1..4f07a07 100644 --- a/src/CompilerKit/StringKit.h +++ b/src/CompilerKit/StringKit.h @@ -12,6 +12,7 @@ #include #include +#include /// =========================================================== /// /// @file StringKit.h diff --git a/src/CompilerKit/Version.h b/src/CompilerKit/Version.h deleted file mode 100644 index efd174c..0000000 --- a/src/CompilerKit/Version.h +++ /dev/null @@ -1,15 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license - -======================================== */ - -#pragma once - -#define kDistVersion "v0.0.7-compilerkit" -#define kDistVersionBCD 0x0002 - -#define ToString(X) Stringify(X) -#define Stringify(X) #X - -#define kDistRelease ToString(kDistReleaseBranch) diff --git a/src/CompilerKit/impl/32x0.h b/src/CompilerKit/impl/32x0.h index 5996708..f55e4a6 100644 --- a/src/CompilerKit/impl/32x0.h +++ b/src/CompilerKit/impl/32x0.h @@ -7,6 +7,7 @@ #pragma once #include +#include // @brief Open32x0 support. // @file impl/32x0.h diff --git a/src/CompilerKit/impl/X64.h b/src/CompilerKit/impl/X64.h index e69d509..f97812d 100644 --- a/src/CompilerKit/impl/X64.h +++ b/src/CompilerKit/impl/X64.h @@ -7,6 +7,7 @@ #pragma once #include +#include // @brief AMD64 support. // @file impl/X64.h diff --git a/src/CompilerKit/src/AssemblyFactory.cc b/src/CompilerKit/src/AssemblyFactory.cc index 185c522..f386083 100644 --- a/src/CompilerKit/src/AssemblyFactory.cc +++ b/src/CompilerKit/src/AssemblyFactory.cc @@ -4,7 +4,7 @@ ======================================== */ -#include +#include #include /** diff --git a/src/CompilerKit/src/Backend/AssemblerARM64.cc b/src/CompilerKit/src/Backend/AssemblerARM64.cc index 6aa8dea..2114fe6 100644 --- a/src/CompilerKit/src/Backend/AssemblerARM64.cc +++ b/src/CompilerKit/src/Backend/AssemblerARM64.cc @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/CompilerKit/src/Backend/AssemblerPowerPC.cc b/src/CompilerKit/src/Backend/AssemblerPowerPC.cc index a845f8b..5da36cf 100644 --- a/src/CompilerKit/src/Backend/AssemblerPowerPC.cc +++ b/src/CompilerKit/src/Backend/AssemblerPowerPC.cc @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/CompilerKit/src/Frontend/CCompiler64x0.cc b/src/CompilerKit/src/Frontend/CCompiler64x0.cc index ecc2c34..45306d7 100644 --- a/src/CompilerKit/src/Frontend/CCompiler64x0.cc +++ b/src/CompilerKit/src/Frontend/CCompiler64x0.cc @@ -1185,7 +1185,7 @@ class AssemblyCCInterface final CK_ASSEMBLY_INTERFACE { ///////////////////////////////////////////////////////////////////////////////////////// -#include +#include #define kPrintF printf #define kSplashCxx() kPrintF(kWhite "NeCTI C Driver, %s, (c) Amlal El Mahrouss\n", kDistVersion) diff --git a/src/CompilerKit/src/Frontend/CCompilerARM64.cc b/src/CompilerKit/src/Frontend/CCompilerARM64.cc index 665c02d..b519193 100644 --- a/src/CompilerKit/src/Frontend/CCompilerARM64.cc +++ b/src/CompilerKit/src/Frontend/CCompilerARM64.cc @@ -1184,7 +1184,7 @@ class AssemblyCCInterface final CK_ASSEMBLY_INTERFACE { ///////////////////////////////////////////////////////////////////////////////////////// -#include +#include #define kPrintF printf #define kSplashCxx() kPrintF(kWhite "NeCTI C Driver, %s, (c) Amlal El Mahrouss\n", kDistVersion) diff --git a/src/CompilerKit/src/Frontend/CCompilerPower64.cc b/src/CompilerKit/src/Frontend/CCompilerPower64.cc index 8a1a943..f67040d 100644 --- a/src/CompilerKit/src/Frontend/CCompilerPower64.cc +++ b/src/CompilerKit/src/Frontend/CCompilerPower64.cc @@ -1202,7 +1202,7 @@ class AssemblyMountpointCLang final CK_ASSEMBLY_INTERFACE { ///////////////////////////////////////////////////////////////////////////////////////// -#include +#include #define kPrintF printf #define kSplashCxx() kPrintF(kWhite "cc, %s, (c) Amlal El Mahrouss\n", kDistVersion) diff --git a/src/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc b/src/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc index c0addff..8cc5af0 100644 --- a/src/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc +++ b/src/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc @@ -38,6 +38,7 @@ #include #include #include +#include /* NeKernel C++ Compiler Driver */ /* This is part of the CompilerKit. */ diff --git a/src/CompilerKit/src/Linker/DynamicLinker64PEF.cc b/src/CompilerKit/src/Linker/DynamicLinker64PEF.cc index 7c56624..8c49601 100644 --- a/src/CompilerKit/src/Linker/DynamicLinker64PEF.cc +++ b/src/CompilerKit/src/Linker/DynamicLinker64PEF.cc @@ -14,13 +14,14 @@ /// It will be loaded when the program loader will start the image. #include -#include +#include #include #include #include #include -#include +#include #include +#include #define kLinkerVersionStr "NeCTI 64-Bit Linker (Preferred Executable Format)" diff --git a/src/CompilerKit/src/StringKit.cc b/src/CompilerKit/src/StringKit.cc index 95326bc..b3584e7 100644 --- a/src/CompilerKit/src/StringKit.cc +++ b/src/CompilerKit/src/StringKit.cc @@ -19,6 +19,7 @@ */ #include +#include namespace CompilerKit { diff --git a/src/CompilerKit/utils/AsmUtils.h b/src/CompilerKit/utils/AsmUtils.h index 40fa133..2cde706 100644 --- a/src/CompilerKit/utils/AsmUtils.h +++ b/src/CompilerKit/utils/AsmUtils.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include using namespace CompilerKit; diff --git a/src/CompilerKit/utils/CompilerUtils.h b/src/CompilerKit/utils/CompilerUtils.h index 8ae6291..e24ee37 100644 --- a/src/CompilerKit/utils/CompilerUtils.h +++ b/src/CompilerKit/utils/CompilerUtils.h @@ -7,9 +7,9 @@ #pragma once #include -#include +#include #include -#include +#include #include #include -- cgit v1.2.3