diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CompilerKit/src/Backends/Assembler+32x0.cpp (renamed from src/CompilerKit/src/Assemblers/Assembler+32x0.cpp) | 0 | ||||
| -rw-r--r-- | src/CompilerKit/src/Backends/Assembler+64x0.cpp (renamed from src/CompilerKit/src/Assemblers/Assembler+64x0.cpp) | 8 | ||||
| -rw-r--r-- | src/CompilerKit/src/Backends/Assembler+AMD64.cpp (renamed from src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp) | 73 | ||||
| -rw-r--r-- | src/CompilerKit/src/Backends/Assembler+ARM64.cpp (renamed from src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp) | 13 | ||||
| -rw-r--r-- | src/CompilerKit/src/Backends/Assembler+PowerPC.cpp (renamed from src/CompilerKit/src/Assemblers/Assembler+PowerPC.cpp) | 13 | ||||
| -rw-r--r-- | src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp | 8 | ||||
| -rw-r--r-- | src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp (renamed from src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cpp) | 32 | ||||
| -rw-r--r-- | src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp | 81 | ||||
| -rw-r--r-- | src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp (renamed from src/CompilerKit/src/Compilers/NectarCompiler+PTX.cpp) | 67 | ||||
| -rw-r--r-- | src/CompilerKit/src/Frontends/README.md (renamed from src/CompilerKit/src/Compilers/README.md) | 0 | ||||
| -rw-r--r-- | src/CompilerKit/src/Generators/DynamicLinker64+MachO.cpp (renamed from src/CompilerKit/src/Linkers/DynamicLinker64+MachO.cpp) | 18 | ||||
| -rw-r--r-- | src/CompilerKit/src/Generators/DynamicLinker64+PEF.cpp (renamed from src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cpp) | 2 | ||||
| -rw-r--r-- | src/CompilerKit/src/Optimizers/NectarOptimize+AMD64.cpp | 17 | ||||
| -rw-r--r-- | src/CompilerKit/src/Preprocess/Preprocessor+Generic.cpp (renamed from src/CompilerKit/src/Preprocessors/Preprocessor+Generic.cpp) | 24 | ||||
| -rw-r--r-- | src/CompilerKit/test/Compilers/NectarCodegen.test.cpp | 6 | ||||
| -rw-r--r-- | src/CompilerKit/test/Linkers/DynamicLinker64+MachO.test.cpp | 4 | ||||
| -rw-r--r-- | src/CompilerKit/test/Linkers/DynamicLinker64+PEF.test.cpp | 4 | ||||
| -rw-r--r-- | src/DebuggerKit/src/MachContract.cpp | 6 | ||||
| -rw-r--r-- | src/DebuggerKit/src/NeKernelContract.cpp | 2 |
19 files changed, 207 insertions, 171 deletions
diff --git a/src/CompilerKit/src/Assemblers/Assembler+32x0.cpp b/src/CompilerKit/src/Backends/Assembler+32x0.cpp index a2eb191..a2eb191 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+32x0.cpp +++ b/src/CompilerKit/src/Backends/Assembler+32x0.cpp diff --git a/src/CompilerKit/src/Assemblers/Assembler+64x0.cpp b/src/CompilerKit/src/Backends/Assembler+64x0.cpp index 41b123a..4f33246 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+64x0.cpp +++ b/src/CompilerKit/src/Backends/Assembler+64x0.cpp @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (See accompanying // file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) // Official repository: https://github.com/ne-foss-org/nectar @@ -26,12 +26,6 @@ #include <CompilerKit/PEF.h> #include <CompilerKit/Utilities/Compiler.h> -///////////////////// - -// ANSI ESCAPE CODES - -///////////////////// - static char kOutputArch = CompilerKit::kPefArch64000; constexpr auto k64x0IPAlignment = 0x1U; diff --git a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp b/src/CompilerKit/src/Backends/Assembler+AMD64.cpp index 6ec5a69..e0f7eef 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp +++ b/src/CompilerKit/src/Backends/Assembler+AMD64.cpp @@ -34,17 +34,6 @@ #include <CompilerKit/PEF.h> #include <CompilerKit/Utilities/Assembler.h> -///////////////////// - -// ANSI ESCAPE CODES - -///////////////////// - -#define kBlank "\e[0;30m" -#define kRed "\e[0;31m" -#define kWhite "\e[0;97m" -#define kYellow "\e[0;33m" - static char kOutputArch = CompilerKit::kPefArchAMD64; static constexpr auto kIPAlignement = 0x1U; @@ -114,21 +103,21 @@ NECTAR_MODULE(AssemblerMainAMD64) { //////////////// CPU OPCODES END //////////////// - for (i64_hword_t i{1}; i < argc; ++i) { + for (i64_hword_t i{1}; i < argc; ++i) { if (argv[i][0] == '-') { if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "-v") == 0) { kStdOut << "AssemblerAMD64: AMD64 Assembler Driver.\nAssemblerAMD64: Copyright (c) 2024-2026 " "Amlal El Mahrouss\n"; - kStdOut - << "AssemblerAMD64: This software is part of the Ne.org project. (https://www.nekernel.org)\n"; + kStdOut << "AssemblerAMD64: This software is part of the Ne.org project. " + "(https://www.nekernel.org)\n"; return 0; } else if (strcmp(argv[i], "-help") == 0) { kStdOut << "AssemblerAMD64: AMD64 Assembler Driver.\nAssemblerAMD64: Copyright (c) 2024-2026 " "Amlal El Mahrouss\n"; - kStdOut - << "AssemblerAMD64: This Software is part of the Ne.org project. (https://www.nekernel.org)\n"; + kStdOut << "AssemblerAMD64: This Software is part of the Ne.org project. " + "(https://www.nekernel.org)\n"; kStdOut << "-version: Print program version.\n"; kStdOut << "-fverbose: Print verbose output.\n"; kStdOut << "-fbinary: Output as flat binary.\n"; @@ -901,10 +890,11 @@ bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerKit::STLString file) { +bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, + CompilerKit::STLString file) { if (CompilerKit::ast_find_needle(line, "public_segment ")) return true; if (CompilerKit::ast_find_needle(line, "extern_segment ")) return true; - + struct RegMapAMD64 final { CompilerKit::STLString fName; i64_byte_t fModRM; @@ -928,9 +918,9 @@ bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerK /// Move instruction handler. if (line.find(name) != std::string::npos) { - if ((line.find(name) + name.size()) > line.size()) continue; - - if (name == "mov" || name == "xor") { + if ((line.find(name) + name.size()) > line.size()) continue; + + if (name == "mov" || name == "xor") { std::string substr = line.substr(line.find(name) + name.size()); uint64_t bits = kRegisterBitWidth; @@ -1134,7 +1124,7 @@ bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerK std::vector<RegMapAMD64> currentRegList; - currentRegList.reserve(3); + currentRegList.reserve(3); for (auto reg : kRegisterList) { std::string registerName; @@ -1216,7 +1206,7 @@ bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerK } if (onlyOneReg && currentRegList.size() > 0) { - auto num = GetNumber32(line, ","); + auto num = GetNumber32(line, ","); auto modrm = (0x3 << 6 | currentRegList[0].fModRM); kAppBytes.emplace_back(0xC7); // prefixed before placing the modrm and then the number. @@ -1232,20 +1222,19 @@ bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerK break; } - - if (currentRegList.size() > 0) { - if (currentRegList[1].fName[0] == 'r' && currentRegList[0].fName[0] == 'e') { - CompilerKit::Detail::print_error("Invalid combination of operands and registers.", - "CompilerKit"); - throw std::runtime_error("comb_op_reg"); - } - - if (currentRegList[0].fName[0] == 'r' && currentRegList[1].fName[0] == 'e') { - CompilerKit::Detail::print_error("Invalid combination of operands and registers.", - "CompilerKit"); - throw std::runtime_error("comb_op_reg"); - } - } + if (currentRegList.size() > 0) { + if (currentRegList[1].fName[0] == 'r' && currentRegList[0].fName[0] == 'e') { + CompilerKit::Detail::print_error("Invalid combination of operands and registers.", + "CompilerKit"); + throw std::runtime_error("comb_op_reg"); + } + + if (currentRegList[0].fName[0] == 'r' && currentRegList[1].fName[0] == 'e') { + CompilerKit::Detail::print_error("Invalid combination of operands and registers.", + "CompilerKit"); + throw std::runtime_error("comb_op_reg"); + } + } if (bits == 16) { if (currentRegList[0].fName[0] == 'r' || currentRegList[0].fName[0] == 'e') { @@ -1293,8 +1282,8 @@ bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerK // Register lookup table struct RegInfo final { - CompilerKit::STLString name; - i64_byte_t code; + CompilerKit::STLString name; + i64_byte_t code; }; RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, @@ -1544,8 +1533,8 @@ bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerK // Register lookup table struct RegInfo final { - CompilerKit::STLString name; - i64_byte_t code; + CompilerKit::STLString name; + i64_byte_t code; }; RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, @@ -1838,7 +1827,7 @@ bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerK } } } - + /// write a dword else if (auto pos = line.find(".dword"); pos != std::string::npos) { this->WriteNumber32(pos + strlen(".dword") + 1, line); diff --git a/src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp b/src/CompilerKit/src/Backends/Assembler+ARM64.cpp index 0017af0..1c25338 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp +++ b/src/CompilerKit/src/Backends/Assembler+ARM64.cpp @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (See accompanying // file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) // Official repository: https://github.com/ne-foss-org/nectar @@ -26,17 +26,6 @@ #include <CompilerKit/PEF.h> #include <CompilerKit/Utilities/Assembler.h> -///////////////////// - -// ANSI ESCAPE CODES - -///////////////////// - -#define kBlank "\e[0;30m" -#define kRed "\e[0;31m" -#define kWhite "\e[0;97m" -#define kYellow "\e[0;33m" - constexpr auto kArm64Alignment = 0x1U; static char kOutputArch = CompilerKit::kPefArchARM64; diff --git a/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cpp b/src/CompilerKit/src/Backends/Assembler+PowerPC.cpp index ced2cf2..1c05a1c 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cpp +++ b/src/CompilerKit/src/Backends/Assembler+PowerPC.cpp @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (See accompanying // file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) // Official repository: https://github.com/ne-foss-org/nectar @@ -26,17 +26,6 @@ #include <CompilerKit/PEF.h> #include <CompilerKit/Utilities/Assembler.h> -///////////////////// - -// ANSI ESCAPE CODES - -///////////////////// - -#define kBlank "\e[0;30m" -#define kRed "\e[0;31m" -#define kWhite "\e[0;97m" -#define kYellow "\e[0;33m" - constexpr auto kPowerIPAlignment = 0x1U; static char kOutputArch = CompilerKit::kPefArchPowerPC; diff --git a/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp b/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp index 7e9e1af..85ecc66 100644 --- a/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp +++ b/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp @@ -16,6 +16,7 @@ */ namespace CompilerKit { + ///! @brief Compile for specific format (ELF, PEF, AE) Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) { if (sourceFile.length() == 0) return NECTAR_UNIMPLEMENTED; @@ -27,8 +28,12 @@ Int32 AssemblyFactory::Compile(STLString sourceFile, const Int32& arch) { auto compiledUnit = sourceFile + ".ignore"; - std::filesystem::copy(sourceFile, compiledUnit); + try { + std::filesystem::copy(sourceFile, compiledUnit); + } catch (...) {} + auto ret = this->fMounted->CompileToFormat(compiledUnit, arch); + std::filesystem::remove(compiledUnit); return ret; @@ -51,4 +56,5 @@ WeakRef<IAssembly> AssemblyFactory::Unmount() noexcept { return WeakRef<IAssembly>{mount_prev}; } + } // namespace CompilerKit diff --git a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp index 92cc52d..85e12ea 100644 --- a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cpp +++ b/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp @@ -6,30 +6,18 @@ /// BUGS: 0 -/////////////////////// - -// ANSI ESCAPE CODES // - -/////////////////////// - -/////////////////////// - -// MACROS // - -/////////////////////// - #include <CompilerKit/AST.h> #include <CompilerKit/Detail/AMD64.h> #include <CompilerKit/PEF.h> #include <CompilerKit/UUID.h> #include <CompilerKit/Utilities/Compiler.h> -/* NeKernel Nectar Compiler Driver. */ +/* Nectar Compiler Driver. */ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss 2024-2026 */ /// @author Amlal El Mahrouss (amlal@nekernel.org) -/// @file NectarCompiler+AMD64.cc +/// @file NectarCompiler+AMD64.cpp /// @brief NECTAR Compiler Driver. ///////////////////////////////////// @@ -78,6 +66,8 @@ struct CompilerState final { static CompilerState kState; +static bool kFreestandingMode = true; + /// \brief Embed Scope of a class. static Int32 kOnClassScope = 0; @@ -252,13 +242,13 @@ static std::vector<CompilerKit::STLString> kRegisterConventionCallList = { "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", }; -static std::size_t kFunctionEmbedLevel{}; +static std::size_t kFunctionEmbedLevel{}; static CompilerKit::STLString kCurrentIfSymbol{}; static CompilerKit::STLString kCurrentReturnAddress{}; -static bool kCurrentIfCondition{false}; +static bool kCurrentIfCondition{false}; const char* CompilerFrontendNectarAMD64::Language() { return "Common Nectar (AMD64)"; @@ -291,7 +281,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile( CompilerKit::SyntaxLeafList::SyntaxLeaf syntax_tree; CompilerKit::STLString syntax_rem_buffer; - if (text.empty()) return syntax_tree; + if (!NectarCheckFrontend(text)) return syntax_tree; std::size_t index{}; std::vector<std::pair<CompilerKit::SyntaxKeyword, std::size_t>> keywords_list; @@ -1612,12 +1602,14 @@ class AssemblyNectarInterfaceAMD64 final CK_ASSEMBLY_INTERFACE { prevRes = res.fUserValue; } - // Output header + // Output bits and imports. if (!kNasmOutput) out_fp << "%bits 64\n"; else { out_fp << "[bits 64]\n"; - out_fp << "extern __operator_new\nextern __operator_delete\n"; + if (kFreestandingMode) { + out_fp << "extern __operator_new\nextern __operator_delete\n"; + } } // For NASM output: emit extern declarations for undefined symbols @@ -1720,7 +1712,7 @@ NECTAR_MODULE(CompilerNectarAMD64) { CompilerKit::STLString err = "Unknown option: "; err += argv[index]; - CompilerKit::Detail::print_error(err, "Nectar"); + CompilerKit::Detail::print_error(err, "frontend"); continue; } diff --git a/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp new file mode 100644 index 0000000..5c51309 --- /dev/null +++ b/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/nectar + +/// BUGS: 0 + +#include <CompilerKit/AST.h> +#include <CompilerKit/Detail/AMD64.h> +#include <CompilerKit/PEF.h> +#include <CompilerKit/UUID.h> +#include <CompilerKit/Utilities/Compiler.h> + +/* Nectar Compiler Check Driver. */ +/* This is part of the CompilerKit. */ +/* (c) Amlal El Mahrouss 2026 */ + +using namespace CompilerKit; + +static bool kInIfBody = false; +static bool kInCtxBody = false; + +CK_IMPORT_C bool NectarCheckFrontend(CompilerKit::STLString& input) { + if (input.empty()) return false; + + if (input.ends_with(":")) { + if (!input.ends_with("):")) { + Detail::print_error("Invalid keyword 'else if' is not a Nectar keyword!", "check"); + return false; + } + + kInIfBody = true; + } + + if (input.find("(") != CompilerKit::STLString::npos) { + if (input.find(")") == CompilerKit::STLString::npos) { + Detail::print_error("Invalid call to function, Nectar expects the ')' character at the end!", + "check"); + return false; + } + } + + if (input.find("let ") != CompilerKit::STLString::npos && !input.ends_with(";")) { + if (input.find(":=") != CompilerKit::STLString::npos) { + Detail::print_error("A declaration must always end with ';'", "check"); + return false; + } + } + + if (input.find("const ") != CompilerKit::STLString::npos && !input.ends_with(";")) { + if (input.find(":=") != CompilerKit::STLString::npos) { + Detail::print_error("A declaration must always end with ';'", "check"); + return false; + } + } + + if (input.find("let ") != CompilerKit::STLString::npos && input.ends_with(";")) { + if (input.find(":=") == CompilerKit::STLString::npos) { + Detail::print_error("A declaration must always include with ':='", "check"); + return false; + } + } + + if (input.find("const ") != CompilerKit::STLString::npos && input.ends_with(";")) { + if (input.find(":=") == CompilerKit::STLString::npos) { + Detail::print_error("A declaration must always end with ':='", "check"); + return false; + } + } + + if (input == "}" || input == "}\n" || input == "}\r\n") { + if (kInIfBody) kInIfBody = false; + } + + if (input == "}" || input == "}\n" || input == "}\r\n") { + if (kInCtxBody) kInCtxBody = false; + } + + return true; +} diff --git a/src/CompilerKit/src/Compilers/NectarCompiler+PTX.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp index 5737cfe..5d15a77 100644 --- a/src/CompilerKit/src/Compilers/NectarCompiler+PTX.cpp +++ b/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp @@ -8,23 +8,11 @@ /// BUGS: 0 -/////////////////////// - -// ANSI ESCAPE CODES // - -/////////////////////// - -/////////////////////// - -// MACROS // - -/////////////////////// - #include <CompilerKit/AST.h> #include <CompilerKit/UUID.h> #include <CompilerKit/Utilities/Compiler.h> -/* NeKernel Nectar Compiler Driver. */ +/* Nectar Compiler Driver. */ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss 2024-2026 */ @@ -243,18 +231,17 @@ class CompilerFrontendNectarPTX final CK_COMPILER_FRONTEND { static CompilerFrontendNectarPTX* kFrontend = nullptr; -static constexpr const char* kPtxRetReg = "%rd0"; -static constexpr const char* kPtxTmpRegA = "%rd1"; -static constexpr const char* kPtxTmpRegB = "%rd2"; -static constexpr const char* kPtxThisReg = "%rd3"; -static constexpr const char* kPtxPredReg = "%p1"; +static constexpr const char* kPtxRetReg = "%rd0"; +static constexpr const char* kPtxTmpRegA = "%rd1"; +static constexpr const char* kPtxTmpRegB = "%rd2"; +static constexpr const char* kPtxThisReg = "%rd3"; +static constexpr const char* kPtxPredReg = "%p1"; static std::vector<CompilerKit::STLString> kRegisterList = { - "%rd16", "%rd17", "%rd18", "%rd19", "%rd20", "%rd21", "%rd22", "%rd23", - "%rd24", "%rd25", "%rd26", "%rd27", "%rd28", "%rd29", "%rd30", "%rd31", - "%rd32", "%rd33", "%rd34", "%rd35", "%rd36", "%rd37", "%rd38", "%rd39", - "%rd40", "%rd41", "%rd42", "%rd43", "%rd44", "%rd45", "%rd46", "%rd47", - "%rd48", "%rd49", "%rd50", "%rd51", "%rd52", "%rd53", "%rd54", "%rd55", + "%rd16", "%rd17", "%rd18", "%rd19", "%rd20", "%rd21", "%rd22", "%rd23", "%rd24", "%rd25", + "%rd26", "%rd27", "%rd28", "%rd29", "%rd30", "%rd31", "%rd32", "%rd33", "%rd34", "%rd35", + "%rd36", "%rd37", "%rd38", "%rd39", "%rd40", "%rd41", "%rd42", "%rd43", "%rd44", "%rd45", + "%rd46", "%rd47", "%rd48", "%rd49", "%rd50", "%rd51", "%rd52", "%rd53", "%rd54", "%rd55", "%rd56", "%rd57", "%rd58", "%rd59", "%rd60", "%rd61", "%rd62", "%rd63", }; @@ -285,7 +272,7 @@ static std::vector<std::pair<CompilerKit::STLString, std::uintptr_t>> kOriginMap ///////////////////////////////////////////////////////////////////////////////////////// static auto nectar_get_impl_member(const CompilerKit::STLString& class_name, - const CompilerKit::STLString& member_name) { + const CompilerKit::STLString& member_name) -> CompilerStructMap { // Find or create struct map entry for (auto& sm : kContext.fStructMapVector) { if (sm.fName == class_name) { @@ -293,7 +280,7 @@ static auto nectar_get_impl_member(const CompilerKit::STLString& class_name, } } - return CompilerStructMap{}; + return {}; } CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( @@ -301,7 +288,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( CompilerKit::SyntaxLeafList::SyntaxLeaf syntax_tree; CompilerKit::STLString syntax_rem_buffer; - if (text.empty()) return syntax_tree; + if (!NectarCheckFrontend(text)) return syntax_tree; std::size_t index{}; std::vector<std::pair<CompilerKit::SyntaxKeyword, std::size_t>> keywords_list; @@ -418,7 +405,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( CompilerKit::STLString{kPtxPredReg} + ", " + CompilerKit::STLString{kPtxTmpRegA} + ", " + CompilerKit::STLString{kPtxTmpRegB} + ";\n"; - syntax_tree.fUserValue += "@"+ CompilerKit::STLString{kPtxPredReg} + " bra __ret_" + + syntax_tree.fUserValue += "@" + CompilerKit::STLString{kPtxPredReg} + " bra __ret_" + std::to_string(kOrigin) + "_" + kCurrentIfSymbol + ";\n"; kCurrentIfSymbol = std::to_string(kOrigin) + "_" + kCurrentIfSymbol; @@ -587,8 +574,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( case CompilerKit::KeywordKind::kKeywordKindNew: { if (auto pos = syntax_tree.fUserValue.find(keyword.first.fKeywordName); pos != CompilerKit::STLString::npos) { - syntax_tree.fUserValue.replace(pos, keyword.first.fKeywordName.size(), - "__operator_new"); + syntax_tree.fUserValue.replace(pos, keyword.first.fKeywordName.size(), "__operator_new"); } continue; @@ -657,9 +643,8 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( } if (!nectar_get_variable_ref(nameVar).empty()) - syntax_tree.fUserValue += - "mov.u64 " + CompilerKit::STLString{kPtxThisReg} + ", " + - nectar_get_variable_ref(nameVar) + ";\n"; + syntax_tree.fUserValue += "mov.u64 " + CompilerKit::STLString{kPtxThisReg} + ", " + + nectar_get_variable_ref(nameVar) + ";\n"; if (CompilerKit::KeywordKind::kKeywordKindFunctionAccess != keyword.first.fKeywordKind) method = valueOfVar.erase(valueOfVar.find("(")); @@ -688,8 +673,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( } } - if (!arg.empty()) - buf += "mov.u64 %rd" + std::to_string(index) + ", " + val + ";\n"; + if (!arg.empty()) buf += "mov.u64 %rd" + std::to_string(index) + ", " + val + ";\n"; arg.clear(); ++index; @@ -706,7 +690,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( syntax_tree.fUserValue += "call.uni "; syntax_tree.fUserValue += (keyword.first.fKeywordName.ends_with('>') ? nectar_get_variable_ref(nameVar) - : nectar_get_variable_ref(nameVar)) + + : nectar_get_variable_ref(nameVar)) + method + ";\n"; } else { auto res = buf; @@ -866,9 +850,8 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarPTX::Compile( kExternalSymbols.insert(mangled + valueOfVar); syntax_tree.fUserValue += "call " + mangled + valueOfVar + ";\n"; - syntax_tree.fUserValue += - instr + nectar_get_variable_ref(varName) + ", " + - CompilerKit::STLString{kPtxRetReg} + ";\n"; + syntax_tree.fUserValue += instr + nectar_get_variable_ref(varName) + ", " + + CompilerKit::STLString{kPtxRetReg} + ";\n"; } break; @@ -1444,7 +1427,7 @@ static CompilerKit::STLString nectar_generate_constructor_call( nectar_pop_scope(); CompilerKit::STLString code; - auto objReg = nectar_allocate_register(obj_name); + auto objReg = nectar_allocate_register(obj_name); if (!objReg.empty()) { code += "mov.u64 " + CompilerKit::STLString{kPtxThisReg} + ", " + objReg + ";\n"; } @@ -1479,8 +1462,8 @@ static CompilerKit::STLString nectar_generate_destructor_call( static void nectar_process_function_parameters(const std::vector<CompilerKit::STLString>& args) { for (size_t i = 0; i < args.size(); ++i) { VariableInfo param; - param.fName = "arg" + std::to_string(i); - param.fLocation = VarLocation::kRegister; + param.fName = "arg" + std::to_string(i); + param.fLocation = VarLocation::kRegister; if (i < kRegisterConventionCallList.size()) { param.fRegister = kRegisterConventionCallList[i]; } else { @@ -1653,7 +1636,7 @@ NECTAR_MODULE(CompilerNectarPTX) { CompilerKit::STLString err = "Unknown option: "; err += argv[index]; - CompilerKit::Detail::print_error(err, "Nectar"); + CompilerKit::Detail::print_error(err, "frontend"); continue; } diff --git a/src/CompilerKit/src/Compilers/README.md b/src/CompilerKit/src/Frontends/README.md index 2e32d07..2e32d07 100644 --- a/src/CompilerKit/src/Compilers/README.md +++ b/src/CompilerKit/src/Frontends/README.md diff --git a/src/CompilerKit/src/Linkers/DynamicLinker64+MachO.cpp b/src/CompilerKit/src/Generators/DynamicLinker64+MachO.cpp index f016c6e..823fbd4 100644 --- a/src/CompilerKit/src/Linkers/DynamicLinker64+MachO.cpp +++ b/src/CompilerKit/src/Generators/DynamicLinker64+MachO.cpp @@ -6,7 +6,7 @@ /// @author Amlal El Mahrouss (amlal@nekernel.org) /// @brief NeKernel.org 64-bit Mach-O Linker. -/// Last Rev: 2026 +/// @version Last Rev: 2026 /// @note Outputs Mach-O executables with __TEXT and __DATA segments. #ifdef CK_USE_MACHO_LINKER @@ -363,8 +363,7 @@ NECTAR_MODULE(DynamicLinker64MachO) { UInt32 symtabCmdSize = sizeof(symtab_command); UInt32 dysymtabCmdSize = sizeof(dysymtab_command); UInt32 linkeditCmdSize = sizeof(segment_command_64); // No sections - UInt32 dylinkerCmdSize = - (13 + 1 + 7) & ~7; // "/usr/lib/dyld" + padding to 8-byte align + UInt32 dylinkerCmdSize = (13 + 1 + 7) & ~7; // "/usr/lib/dyld" + padding to 8-byte align sizeOfCmds = pageZeroSize + textSegCmdSize + dataSegCmdSize + buildCmdSize + uuidCmdSize + symtabCmdSize + dysymtabCmdSize + linkeditCmdSize; @@ -515,12 +514,12 @@ NECTAR_MODULE(DynamicLinker64MachO) { output_fc.write(reinterpret_cast<const char*>(&linkeditSegment), sizeof(linkeditSegment)); // Write LC_LOAD_DYLINKER command - constexpr const char* dyldPath = "/usr/lib/dyld"; - std::vector<char> dylinkerCmd(dylinkerCmdSize, 0); - dylinker_command* dylinker = reinterpret_cast<dylinker_command*>(dylinkerCmd.data()); - dylinker->cmd = LC_LOAD_DYLINKER; - dylinker->cmdsize = dylinkerCmdSize; - dylinker->name.offset = sizeof(dylinker_command); + constexpr const char* dyldPath = "/usr/lib/dyld"; + std::vector<char> dylinkerCmd(dylinkerCmdSize, 0); + dylinker_command* dylinker = reinterpret_cast<dylinker_command*>(dylinkerCmd.data()); + dylinker->cmd = LC_LOAD_DYLINKER; + dylinker->cmdsize = dylinkerCmdSize; + dylinker->name.offset = sizeof(dylinker_command); std::memcpy(dylinkerCmd.data() + sizeof(dylinker_command), dyldPath, strlen(dyldPath) + 1); output_fc.write(dylinkerCmd.data(), dylinkerCmd.size()); @@ -623,7 +622,6 @@ NECTAR_MODULE(DynamicLinker64MachO) { output_fc.write(reinterpret_cast<const char*>(&sym), sizeof(nlist_64)); } - // Write string table output_fc.write(kStringTable.data(), kStringTable.size()); diff --git a/src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cpp b/src/CompilerKit/src/Generators/DynamicLinker64+PEF.cpp index 0ef8531..34333a3 100644 --- a/src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cpp +++ b/src/CompilerKit/src/Generators/DynamicLinker64+PEF.cpp @@ -6,7 +6,7 @@ /// @author Amlal El Mahrouss (amlal@nekernel.org) /// @brief NeKernel.org 64-bit PEF Linker. -/// Last Rev: Sun Feb 8 CET 2026 +/// @version Last Rev: Sun Feb 8 CET 2026 /// @note Do not look up for anything with .code64/.data64/.zero64! /// It will be loaded when the program loader will start the image. diff --git a/src/CompilerKit/src/Optimizers/NectarOptimize+AMD64.cpp b/src/CompilerKit/src/Optimizers/NectarOptimize+AMD64.cpp new file mode 100644 index 0000000..e350076 --- /dev/null +++ b/src/CompilerKit/src/Optimizers/NectarOptimize+AMD64.cpp @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/nectar + +/// BUGS: 0 + +#include <CompilerKit/AST.h> +#include <CompilerKit/Detail/AMD64.h> +#include <CompilerKit/PEF.h> +#include <CompilerKit/UUID.h> +#include <CompilerKit/Utilities/Compiler.h> + +/* Nectar Compiler Optimizing Driver. */ +/* This is part of the CompilerKit. */ +/* (c) Amlal El Mahrouss 2024-2026 */
\ No newline at end of file diff --git a/src/CompilerKit/src/Preprocessors/Preprocessor+Generic.cpp b/src/CompilerKit/src/Preprocess/Preprocessor+Generic.cpp index e2d20df..555ea8b 100644 --- a/src/CompilerKit/src/Preprocessors/Preprocessor+Generic.cpp +++ b/src/CompilerKit/src/Preprocess/Preprocessor+Generic.cpp @@ -21,8 +21,7 @@ /// @file Preprocessor+Generic.cpp /// @brief Nectar Preprocessor. -using pp_parser_fn_t = Int32 (*)(CompilerKit::STLString&, std::ifstream&, - std::ofstream&); +using pp_parser_fn_t = Int32 (*)(CompilerKit::STLString&, std::ifstream&, std::ofstream&); ///////////////////////////////////////////////////////////////////////////////////////// @@ -259,8 +258,7 @@ void pp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { if (inactive_code) { if (hdr_line.find("#endif") == CompilerKit::STLString::npos) { continue; - } else if (hdr_line[0] == '#' && - hdr_line.find("#endif") != CompilerKit::STLString::npos) { + } else if (hdr_line[0] == '#' && hdr_line.find("#endif") != CompilerKit::STLString::npos) { inactive_code = false; } } @@ -491,8 +489,7 @@ void pp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { continue; } - } else if (hdr_line[0] == '#' && - hdr_line.find("else") != CompilerKit::STLString::npos) { + } else if (hdr_line[0] == '#' && hdr_line.find("else") != CompilerKit::STLString::npos) { if (!defined && inactive_code) { inactive_code = false; defined = true; @@ -504,8 +501,7 @@ void pp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { continue; } - } else if (hdr_line[0] == '#' && - hdr_line.find("ifdef") != CompilerKit::STLString::npos) { + } else if (hdr_line[0] == '#' && hdr_line.find("ifdef") != CompilerKit::STLString::npos) { auto ifdef_pos = hdr_line.find("ifdef"); if (ifdef_pos == CompilerKit::STLString::npos) continue; @@ -545,8 +541,7 @@ void pp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { break; } } - } else if (hdr_line[0] == '#' && - hdr_line.find("if") != CompilerKit::STLString::npos) { + } else if (hdr_line[0] == '#' && hdr_line.find("if") != CompilerKit::STLString::npos) { inactive_code = true; std::vector<Detail::pp_macro_condition> pp_macro_condition_list = { @@ -630,8 +625,7 @@ void pp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { break; } } - } else if (hdr_line[0] == '#' && - hdr_line.find("warning") != CompilerKit::STLString::npos) { + } else if (hdr_line[0] == '#' && hdr_line.find("warning") != CompilerKit::STLString::npos) { auto warning_pos = hdr_line.find("warning"); if (warning_pos == CompilerKit::STLString::npos) continue; @@ -647,8 +641,7 @@ void pp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { } std::cerr << "warn: " << message << std::endl; - } else if (hdr_line[0] == '#' && - hdr_line.find("error") != CompilerKit::STLString::npos) { + } else if (hdr_line[0] == '#' && hdr_line.find("error") != CompilerKit::STLString::npos) { auto error_pos = hdr_line.find("error"); if (error_pos == CompilerKit::STLString::npos) continue; @@ -664,8 +657,7 @@ void pp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { } throw std::runtime_error("error: " + message); - } else if (hdr_line[0] == '#' && - hdr_line.find("include ") != CompilerKit::STLString::npos) { + } else if (hdr_line[0] == '#' && hdr_line.find("include ") != CompilerKit::STLString::npos) { auto include_pos = hdr_line.find("include "); if (include_pos == CompilerKit::STLString::npos) continue; diff --git a/src/CompilerKit/test/Compilers/NectarCodegen.test.cpp b/src/CompilerKit/test/Compilers/NectarCodegen.test.cpp index 581bfb8..e9632d9 100644 --- a/src/CompilerKit/test/Compilers/NectarCodegen.test.cpp +++ b/src/CompilerKit/test/Compilers/NectarCodegen.test.cpp @@ -11,11 +11,13 @@ TEST(CodegenTest, BasicCodegenTestGrep) { // Compile C++ source to assembly - auto compile_result = std::system("pef-amd64-necdrv ../../../../snippets/test_snippets/inner.nc > /dev/null 2>&1"); + auto compile_result = + std::system("pef-amd64-necdrv ../../../../snippets/test_snippets/inner.nc > /dev/null 2>&1"); EXPECT_TRUE(compile_result == 0) << "C++ compiler driver failed to compile sample.cc"; } TEST(CodegenTest, BasicCodegenTestAssemble) { - auto expr = std::system("pef-amd64-asm ../../../../snippets/test_snippets/inner.masm > /dev/null 2>&1"); + auto expr = + std::system("pef-amd64-asm ../../../../snippets/test_snippets/inner.masm > /dev/null 2>&1"); EXPECT_TRUE(expr == 0) << "ASM Driver did not compile the easy ASM unit."; } diff --git a/src/CompilerKit/test/Linkers/DynamicLinker64+MachO.test.cpp b/src/CompilerKit/test/Linkers/DynamicLinker64+MachO.test.cpp index 8f3ff6b..6cd2167 100644 --- a/src/CompilerKit/test/Linkers/DynamicLinker64+MachO.test.cpp +++ b/src/CompilerKit/test/Linkers/DynamicLinker64+MachO.test.cpp @@ -47,6 +47,8 @@ TEST(LinkerRunMachO, LinkerExitsCorrectly) { return; } - auto ret =(entrypoint_cxx(argc, const_cast<const char**>(argv)) == NECTAR_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; + auto ret = (entrypoint_cxx(argc, const_cast<const char**>(argv)) == NECTAR_SUCCESS) + ? EXIT_SUCCESS + : EXIT_FAILURE; EXPECT_TRUE(ret == EXIT_SUCCESS); } diff --git a/src/CompilerKit/test/Linkers/DynamicLinker64+PEF.test.cpp b/src/CompilerKit/test/Linkers/DynamicLinker64+PEF.test.cpp index b0b00fa..e67916a 100644 --- a/src/CompilerKit/test/Linkers/DynamicLinker64+PEF.test.cpp +++ b/src/CompilerKit/test/Linkers/DynamicLinker64+PEF.test.cpp @@ -47,6 +47,8 @@ TEST(LinkerRunMachO, LinkerExitsCorrectly) { return; } - auto ret =(entrypoint_cxx(argc, const_cast<const char**>(argv)) == NECTAR_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; + auto ret = (entrypoint_cxx(argc, const_cast<const char**>(argv)) == NECTAR_SUCCESS) + ? EXIT_SUCCESS + : EXIT_FAILURE; EXPECT_TRUE(ret == EXIT_SUCCESS); } diff --git a/src/DebuggerKit/src/MachContract.cpp b/src/DebuggerKit/src/MachContract.cpp index 7f9ba5d..8a897be 100644 --- a/src/DebuggerKit/src/MachContract.cpp +++ b/src/DebuggerKit/src/MachContract.cpp @@ -10,6 +10,8 @@ #include <ThirdParty/Dialogs/Dialogs.h> #ifdef DK_MACH_DEBUGGER + +/// @brief a terrible way to import globals. #include <DebuggerKit/Common.inl> /// @internal @@ -33,9 +35,7 @@ NECTAR_MODULE(DebuggerMachPOSIX) { constexpr auto kMaxArgs = 3; - if (argc >= kMaxArgs - && std::string(argv[1]) == "-p" - && argv[2] != nullptr) { + if (argc >= kMaxArgs && std::string(argv[1]) == "-p" && argv[2] != nullptr) { kPath = argv[2]; kUserDebugger.SetPath(kPath); diff --git a/src/DebuggerKit/src/NeKernelContract.cpp b/src/DebuggerKit/src/NeKernelContract.cpp index 4dd6a70..5e7fb5e 100644 --- a/src/DebuggerKit/src/NeKernelContract.cpp +++ b/src/DebuggerKit/src/NeKernelContract.cpp @@ -15,7 +15,7 @@ using namespace DebuggerKit::Detail; using namespace DebuggerKit::NeKernel; -NeKernelContract::NeKernelContract() = default; +NeKernelContract::NeKernelContract() = default; NeKernelContract::~NeKernelContract() = default; bool NeKernelContract::Attach(CompilerKit::STLString path, CompilerKit::STLString argv, |
