diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-18 01:57:41 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-18 01:57:41 +0100 |
| commit | 228479a454d325340326f4fd23e13d780884fd2a (patch) | |
| tree | ee35d5eecde396e674d1a0e11c5372352a49fac4 | |
| parent | 80de28e3472613e8f6aa8b2730d56b5307f4fb9a (diff) | |
chore & feat: New DLLLoader API and AST API improvements.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | include/CompilerKit/AST.h | 25 | ||||
| -rw-r--r-- | include/CompilerKit/CodeGenerator.h | 5 | ||||
| -rw-r--r-- | include/CompilerKit/Utilities/DLL.h | 25 | ||||
| -rw-r--r-- | src/CommandLine/pef-amd64-cxxdrv.json | 8 | ||||
| -rw-r--r-- | src/CommandLine/pef-arm64-cdrv.cc | 3 | ||||
| -rw-r--r-- | src/CommandLine/pef-arm64-cdrv.json | 4 | ||||
| -rw-r--r-- | src/CompilerKit/src/Assemblers/Assembler+32x0.cc | 4 | ||||
| -rw-r--r-- | src/CompilerKit/src/Assemblers/Assembler+64x0.cc | 4 | ||||
| -rw-r--r-- | src/CompilerKit/src/Assemblers/Assembler+AMD64.cc | 4 | ||||
| -rw-r--r-- | src/CompilerKit/src/Assemblers/Assembler+ARM64.cc | 4 | ||||
| -rw-r--r-- | src/CompilerKit/src/Assemblers/Assembler+PowerPC.cc | 4 | ||||
| -rw-r--r-- | src/CompilerKit/src/CodeGenerator+AssemblyFactory.cc | 24 | ||||
| -rw-r--r-- | src/CompilerKit/src/Compilers/CCompiler+Power64.cc | 6 | ||||
| -rw-r--r-- | src/CompilerKit/src/Compilers/CPlusPlusCompiler+AMD64.cc | 19 | ||||
| -rw-r--r-- | src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cc (renamed from src/CompilerKit/src/DynamicLinkers/DynamicLinker64+PEF.cc) | 2 | ||||
| -rw-r--r-- | src/CompilerKit/src/Preprocessor/Preprocessor+Generic.cc | 2 |
17 files changed, 78 insertions, 66 deletions
@@ -18,6 +18,7 @@ src/*/kdbg src/*/pef-amd64-cxxdrv src/*/pef-aarch64-cxxdrv src/*/pef-aarch64-cdrv +src/*/pef-arm64-cdrv *.pp *.masm diff --git a/include/CompilerKit/AST.h b/include/CompilerKit/AST.h index aee3ff1..a1819b1 100644 --- a/include/CompilerKit/AST.h +++ b/include/CompilerKit/AST.h @@ -70,7 +70,7 @@ enum KeywordKind { /// \brief Compiler keyword information struct. /// =========================================================== /// struct SyntaxKeyword { - SyntaxKeyword(STLString name, KeywordKind kind) : fKeywordName(name), fKeywordKind(kind) {} + SyntaxKeyword(const STLString& name, KeywordKind kind) : fKeywordName(name), fKeywordKind(kind) {} STLString fKeywordName{""}; KeywordKind fKeywordKind{kKeywordKindInvalid}; @@ -78,10 +78,15 @@ struct SyntaxKeyword { struct SyntaxLeafList final { struct SyntaxLeaf final { - Int32 fUserType{0U}; + // \brief User data type. + Int32 fUserType{0U}; + // \brief User data buffer. SyntaxKeyword fUserData{"", kKeywordKindInvalid}; - STLString fUserValue{""}; + // \brief User data value + STLString fUserValue{""}; + + // \brief Next user data on list. struct SyntaxLeaf* fNext{nullptr}; }; @@ -90,8 +95,14 @@ struct SyntaxLeafList final { ArrayType fLeafList; SizeType fNumLeafs{0}; - SizeType NumLeafs() { return fNumLeafs; } - ArrayType& Get() { return fLeafList; } + public: + const SizeType& NumLeafs() { return fNumLeafs; } + const SizeType& NumLeafs() const { return fNumLeafs; } + const ArrayType& Get() const { return fLeafList; } + ArrayType& Get() { return fLeafList; } + // \brief You can't get a reference of a leaf in a const context. + const SyntaxLeaf& At(const SizeType& index) const = delete; + // \breif Grab leaf from index. SyntaxLeaf& At(const SizeType& index) { return fLeafList[index]; } }; @@ -131,7 +142,7 @@ class ICompilerFrontend { //! @brief Compile a syntax tree ouf of the text. //! Also takes the source file name for metadata. /// =========================================================== /// - virtual SyntaxLeafList::SyntaxLeaf Compile(STLString text, STLString file) = 0; + virtual SyntaxLeafList::SyntaxLeaf Compile(STLString text, STLString file) { return {}; } /// =========================================================== /// //! @brief What language are we dealing with? @@ -145,4 +156,4 @@ class ICompilerFrontend { }; } // namespace CompilerKit -#include <CompilerKit/AST.inl>
\ No newline at end of file +#include <CompilerKit/AST.inl> diff --git a/include/CompilerKit/CodeGenerator.h b/include/CompilerKit/CodeGenerator.h index f2875bf..8bee7b5 100644 --- a/include/CompilerKit/CodeGenerator.h +++ b/include/CompilerKit/CodeGenerator.h @@ -44,9 +44,8 @@ class AssemblyFactory final { kArchCount = kArchUnknown - kArchAMD64, }; - Int32 Compile(STLString sourceFile, const Int32& arch) noexcept; - - void Mount(WeakRef<IAssembly> mountPtr) noexcept; + Int32 Compile(STLString sourceFile, const Int32& arch); + void Mount(WeakRef<IAssembly> mountPtr); WeakRef<IAssembly> Unmount() noexcept; private: diff --git a/include/CompilerKit/Utilities/DLL.h b/include/CompilerKit/Utilities/DLL.h index 6f14c13..ea6789d 100644 --- a/include/CompilerKit/Utilities/DLL.h +++ b/include/CompilerKit/Utilities/DLL.h @@ -13,22 +13,23 @@ namespace CompilerKit { class DLLLoader final { public: - typedef Int32 (*EntryT)(Int32 argc, Char const* argv[]); - using DLL = VoidPtr; - using Mutex = std::mutex; + using EntryT = Int32 (*)(Int32 argc, Char const* argv[]); + using HandleT = VoidPtr; + using MutexT = std::mutex; + EntryT fEntrypoint{nullptr}; private: - DLL mDLL{nullptr}; - Mutex mMutex; + HandleT mDLL{nullptr}; + MutexT mMutex; public: - explicit operator bool() { return this->mDLL && this->fEntrypoint; } + explicit operator bool() { return this->mDLL; } - DLLLoader& operator()(const Char* path, const Char* fEntrypoint) { - if (!path || !fEntrypoint) return *this; + DLLLoader& operator()(const Char* path, const Char* entrypoint) { + if (!path || !entrypoint) return *this; - std::lock_guard<Mutex> lock(this->mMutex); + std::lock_guard<MutexT> lock(this->mMutex); if (this->mDLL) { this->Destroy(); @@ -40,7 +41,7 @@ class DLLLoader final { return *this; } - this->fEntrypoint = reinterpret_cast<EntryT>(::dlsym(this->mDLL, fEntrypoint)); + this->fEntrypoint = reinterpret_cast<EntryT>(::dlsym(this->mDLL, entrypoint)); if (!this->fEntrypoint) { this->Destroy(); @@ -52,7 +53,7 @@ class DLLLoader final { NECTI_COPY_DELETE(DLLLoader) - explicit DLLLoader() = default; + DLLLoader() = default; ~DLLLoader() { this->Destroy(); } private: @@ -65,4 +66,4 @@ class DLLLoader final { this->fEntrypoint = nullptr; } }; -} // namespace CompilerKit
\ No newline at end of file +} // namespace CompilerKit diff --git a/src/CommandLine/pef-amd64-cxxdrv.json b/src/CommandLine/pef-amd64-cxxdrv.json index 62b5b4d..4520b86 100644 --- a/src/CommandLine/pef-amd64-cxxdrv.json +++ b/src/CommandLine/pef-amd64-cxxdrv.json @@ -2,9 +2,9 @@ "compiler_path": "clang++", "compiler_std": "c++20", "headers_path": [ - "../CompilerKit", - "../", - "../CompilerKit/src/Detail" + "../include/CompilerKit", + "../../include", + "../include/CompilerKit/src/Detail" ], "sources_path": [ "pef-amd64-cxxdrv.cc" @@ -16,4 +16,4 @@ "__CXXDRV__=202504", "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" ] -}
\ No newline at end of file +} diff --git a/src/CommandLine/pef-arm64-cdrv.cc b/src/CommandLine/pef-arm64-cdrv.cc index f4be5d0..c03b039 100644 --- a/src/CommandLine/pef-arm64-cdrv.cc +++ b/src/CommandLine/pef-arm64-cdrv.cc @@ -12,7 +12,7 @@ #include <CompilerKit/Utilities/Compiler.h> #include <CompilerKit/Utilities/DLL.h> -#ifdef __APPLE__ +#if defined(__APPLE__) static auto kPath = "/usr/local/lib/libCompilerKit.dylib"; #else static auto kPath = "/usr/lib/libCompilerKit.so"; @@ -22,6 +22,7 @@ static auto kSymbol = "CompilerCLangARM64"; Int32 main(Int32 argc, Char const* argv[]) { CompilerKit::DLLLoader dylib; + dylib(kPath, kSymbol); CompilerKit::DLLLoader::EntryT entrypoint_c = diff --git a/src/CommandLine/pef-arm64-cdrv.json b/src/CommandLine/pef-arm64-cdrv.json index be6a1be..e63e123 100644 --- a/src/CommandLine/pef-arm64-cdrv.json +++ b/src/CommandLine/pef-arm64-cdrv.json @@ -3,7 +3,7 @@ "compiler_std": "c++20", "headers_path": [ "../CompilerKit", - "../", + "../../include", "../CompilerKit/src/Detail" ], "sources_path": [ @@ -16,4 +16,4 @@ "__CXXDRV__=202504", "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" ] -}
\ No newline at end of file +} diff --git a/src/CompilerKit/src/Assemblers/Assembler+32x0.cc b/src/CompilerKit/src/Assemblers/Assembler+32x0.cc index dbf2898..c05bf1f 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+32x0.cc +++ b/src/CompilerKit/src/Assemblers/Assembler+32x0.cc @@ -8,7 +8,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -// @file 32asm.cc +// @file Assembler+32x0.cc // @author Amlal El Mahrouss // @brief 32x0 Assembler. @@ -18,7 +18,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// #ifndef __ASM_NEED_32x0__ -#define __ASM_NEED_32x0__ 1 +#define __ASM_NEED_32x0__ #endif #include <CompilerKit/AE.h> diff --git a/src/CompilerKit/src/Assemblers/Assembler+64x0.cc b/src/CompilerKit/src/Assemblers/Assembler+64x0.cc index 1ddb5d2..c7923e8 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+64x0.cc +++ b/src/CompilerKit/src/Assemblers/Assembler+64x0.cc @@ -8,7 +8,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -// @file Assembler64x0.cc +// @file Assembler+64x0.cc // @author Amlal El Mahrouss // @brief 64x000 Assembler. @@ -18,7 +18,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// #ifndef __ASM_NEED_64x0__ -#define __ASM_NEED_64x0__ 1 +#define __ASM_NEED_64x0__ #endif #include <CompilerKit/AE.h> diff --git a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc index 6d1ca0e..b949e67 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc +++ b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc @@ -6,7 +6,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -/// @file AssemblerAMD64.cc +/// @file Assembler+AMD64.cc /// @author Amlal El Mahrouss /// @brief AMD64 Assembler. /// REMINDER: when dealing with an undefined symbol use (string @@ -22,7 +22,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// #ifndef __ASM_NEED_AMD64__ -#define __ASM_NEED_AMD64__ 1 +#define __ASM_NEED_AMD64__ #endif #define kAssemblerPragmaSymStr "%%" diff --git a/src/CompilerKit/src/Assemblers/Assembler+ARM64.cc b/src/CompilerKit/src/Assemblers/Assembler+ARM64.cc index f5174aa..22b4023 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+ARM64.cc +++ b/src/CompilerKit/src/Assemblers/Assembler+ARM64.cc @@ -6,7 +6,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -/// @file AssemblerARM64.cc +/// @file Assembler+ARM64.cc /// @author Amlal El Mahrouss /// @brief 'ACORN' Assembler. @@ -16,7 +16,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// #ifndef __ASM_NEED_ARM64__ -#define __ASM_NEED_ARM64__ 1 +#define __ASM_NEED_ARM64__ #endif #include <CompilerKit/AE.h> diff --git a/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cc b/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cc index a61de04..48ddefe 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cc +++ b/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cc @@ -6,7 +6,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -/// @file AssemblerPower.cc +/// @file Assembler+PowerPC.cc /// @author Amlal El Mahrouss /// @brief POWER Assembler. @@ -16,7 +16,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// #ifndef __ASM_NEED_PPC__ -#define __ASM_NEED_PPC__ 1 +#define __ASM_NEED_PPC__ #endif #include <CompilerKit/AE.h> diff --git a/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cc b/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cc index c132638..98ba944 100644 --- a/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cc +++ b/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cc @@ -9,7 +9,7 @@ /** * @file AssemblyFactory.cc * @author Amlal El Mahrouss (amlal@nekernel.org) - * @brief Assembly API of Nectar + * @brief Nectar Assembly API * @version 0.0.3 * @copyright Copyright (c) 2024-2025 Amlal El Mahrouss * @@ -17,23 +17,19 @@ namespace CompilerKit { ///! @brief Compile for specific format (ELF, PEF, ZBIN) -Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) noexcept { +Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) { if (sourceFile.length() < 1) return NECTI_UNIMPLEMENTED; - if (!fMounted) return NECTI_UNIMPLEMENTED; - if (arch != fMounted->Arch()) return NECTI_INVALID_ARCH; + if (!this->fMounted) return NECTI_UNIMPLEMENTED; + if (arch != this->fMounted->Arch()) return NECTI_INVALID_ARCH; - try { - return this->fMounted->CompileToFormat(sourceFile, arch); - } catch (...) { - return NECTI_EXEC_ERROR; - } + return this->fMounted->CompileToFormat(sourceFile, arch); } ///! @brief mount assembly backend. -void AssemblyFactory::Mount(WeakRef<IAssembly> mountPtr) noexcept { - if (mountPtr) { - fMounted = mountPtr.Leak(); +void AssemblyFactory::Mount(WeakRef<IAssembly> mountPtr) { + if (mountPtr && !this->fMounted) { + this->fMounted = mountPtr.Leak(); } } @@ -41,8 +37,8 @@ void AssemblyFactory::Mount(WeakRef<IAssembly> mountPtr) noexcept { WeakRef<IAssembly> AssemblyFactory::Unmount() noexcept { auto mount_prev = fMounted; - if (fMounted) { - fMounted = nullptr; + if (this->fMounted) { + this->fMounted = nullptr; } return WeakRef<IAssembly>{mount_prev}; diff --git a/src/CompilerKit/src/Compilers/CCompiler+Power64.cc b/src/CompilerKit/src/Compilers/CCompiler+Power64.cc index 3867be6..39d09fd 100644 --- a/src/CompilerKit/src/Compilers/CCompiler+Power64.cc +++ b/src/CompilerKit/src/Compilers/CCompiler+Power64.cc @@ -33,14 +33,14 @@ ///////////////////////////////////// namespace Detail { -// \brief name to register struct. +/// \brief name to register struct. struct CompilerRegisterMap final { std::string fName; std::string fReg; }; -// \brief Map for C structs -// \author amlal@nekernel.org +/// \brief Map for C structs +/// \author amlal@nekernel.org struct CompilerStructMap final { /// 'struct::my_foo' std::string fName; diff --git a/src/CompilerKit/src/Compilers/CPlusPlusCompiler+AMD64.cc b/src/CompilerKit/src/Compilers/CPlusPlusCompiler+AMD64.cc index 7f92ef6..efe557f 100644 --- a/src/CompilerKit/src/Compilers/CPlusPlusCompiler+AMD64.cc +++ b/src/CompilerKit/src/Compilers/CPlusPlusCompiler+AMD64.cc @@ -30,7 +30,7 @@ #include <cstdlib> #include <filesystem> -/* NeKernel C++ Compiler Driver */ +/* NeKernel C++ Compiler Driver. */ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss 2024-2025 */ @@ -45,7 +45,7 @@ ///////////////////////////////////// /// @internal -// Avoids relative_path which could discard parts of the original. +/// @brief Avoids relative_path which could discard parts of the original. std::filesystem::path necti_expand_home(const std::filesystem::path& input) { const std::string& raw = input.string(); @@ -61,19 +61,20 @@ std::filesystem::path necti_expand_home(const std::filesystem::path& input) { return input; } +/// \brief Register map, i.e ({foobar, rbp+48}, etc...) struct CompilerRegisterMap final { CompilerKit::STLString fName{}; CompilerKit::STLString fReg{}; }; -/// \brief Offset based struct/class +/// \brief Offsets of struct and classes. struct CompilerStructMap final { CompilerKit::STLString fName{}; CompilerKit::STLString fReg{}; std::vector<std::pair<UInt32, CompilerKit::STLString>> fOffsets; }; -/// \brief Compiler state structure. +/// \brief State machine of the compiler. struct CompilerState final { std::vector<CompilerRegisterMap> fStackMapVector; std::vector<CompilerStructMap> fStructMapVector; @@ -83,16 +84,18 @@ struct CompilerState final { static CompilerState kState; +/// \brief Embed Scope of a class. static Int32 kOnClassScope = 0; ///////////////////////////////////////////////////////////////////////////////////////// -// Target architecture. +/// \brief Target architecture. +/// \note This shall never change. static Int32 kMachine = CompilerKit::AssemblyFactory::kArchAMD64; ///////////////////////////////////////// -// ARGUMENTS REGISTERS (R8, R15) +// ARGUMENT REGISTERS (R8, R15) ///////////////////////////////////////// @@ -111,7 +114,7 @@ static bool kOnForLoop = false; static bool kInBraces = false; static size_t kBracesCount = 0UL; -/* @brief C++ compiler backend for the NeKernel C++ driver */ +/* \brief C++ compiler backend for the NeKernel C++ driver */ class CompilerFrontendCPlusPlusAMD64 final CK_COMPILER_FRONTEND { public: explicit CompilerFrontendCPlusPlusAMD64() = default; @@ -146,7 +149,7 @@ static std::size_t kFunctionEmbedLevel = 0UL; /// detail namespaces const char* CompilerFrontendCPlusPlusAMD64::Language() { - return "AMD64 CFront"; + return "AMD64 C++"; } static std::uintptr_t kOrigin = kPefBaseOrigin; diff --git a/src/CompilerKit/src/DynamicLinkers/DynamicLinker64+PEF.cc b/src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cc index f3eaaa9..6bc8063 100644 --- a/src/CompilerKit/src/DynamicLinkers/DynamicLinker64+PEF.cc +++ b/src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cc @@ -2,7 +2,7 @@ Copyright (C) 2024-2025 Amlal El Mahrouss, licensed under the Apache 2.0 license - @file DynamicLinker64PEF.cc + @file DynamicLinker64+PEF.cc @brief: C++ 64-Bit PEF Linker for NeKernel.org's NeKernel ======================================== */ diff --git a/src/CompilerKit/src/Preprocessor/Preprocessor+Generic.cc b/src/CompilerKit/src/Preprocessor/Preprocessor+Generic.cc index 97bf44f..cef97b1 100644 --- a/src/CompilerKit/src/Preprocessor/Preprocessor+Generic.cc +++ b/src/CompilerKit/src/Preprocessor/Preprocessor+Generic.cc @@ -21,7 +21,7 @@ #define kMacroPrefix '#' /// @author Amlal El Mahrouss (amlal@nekernel.org) -/// @file CPlusPlusPreprocessor.cc +/// @file Preprocessor+Generic.cc /// @brief Preprocessor. typedef Int32 (*bpp_parser_fn_t)(CompilerKit::STLString& line, std::ifstream& hdr_file, |
