From 3606b7c2ad086d415fdc2ecbe3c33c2e37759da2 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 15 Mar 2026 16:44:48 +0100 Subject: [FEAT] final structure changes. Signed-off-by: Amlal El Mahrouss --- src/CompilerKit/src/Assemblers/Assembler+32x0.cpp | 38 - src/CompilerKit/src/Assemblers/Assembler+64x0.cpp | 869 --------- src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp | 1849 -------------------- src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp | 573 ------ .../src/Assemblers/Assembler+PowerPC.cpp | 893 ---------- src/CompilerKit/src/Backends/Assembler+32x0.cpp | 38 + src/CompilerKit/src/Backends/Assembler+64x0.cpp | 869 +++++++++ src/CompilerKit/src/Backends/Assembler+AMD64.cpp | 1849 ++++++++++++++++++++ src/CompilerKit/src/Backends/Assembler+ARM64.cpp | 573 ++++++ src/CompilerKit/src/Backends/Assembler+PowerPC.cpp | 893 ++++++++++ .../src/CodeGenerator+AssemblyFactory.cpp | 2 + .../src/Frontends/NectarCompiler+AMD64.cpp | 14 +- .../src/Frontends/NectarCompiler+Chk.cpp | 17 + .../src/Frontends/NectarCompiler+PTX.cpp | 14 +- .../src/Generators/DynamicLinker64+MachO.cpp | 637 +++++++ .../src/Generators/DynamicLinker64+PEF.cpp | 677 +++++++ .../src/Linkers/DynamicLinker64+MachO.cpp | 637 ------- .../src/Linkers/DynamicLinker64+PEF.cpp | 677 ------- .../src/Optimizers/NectarOptimize+AMD64.cpp | 17 + .../src/Preprocess/Preprocessor+Generic.cpp | 918 ++++++++++ .../src/Preprocessors/Preprocessor+Generic.cpp | 918 ---------- 21 files changed, 6492 insertions(+), 6480 deletions(-) delete mode 100644 src/CompilerKit/src/Assemblers/Assembler+32x0.cpp delete mode 100644 src/CompilerKit/src/Assemblers/Assembler+64x0.cpp delete mode 100644 src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp delete mode 100644 src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp delete mode 100644 src/CompilerKit/src/Assemblers/Assembler+PowerPC.cpp create mode 100644 src/CompilerKit/src/Backends/Assembler+32x0.cpp create mode 100644 src/CompilerKit/src/Backends/Assembler+64x0.cpp create mode 100644 src/CompilerKit/src/Backends/Assembler+AMD64.cpp create mode 100644 src/CompilerKit/src/Backends/Assembler+ARM64.cpp create mode 100644 src/CompilerKit/src/Backends/Assembler+PowerPC.cpp create mode 100644 src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp create mode 100644 src/CompilerKit/src/Generators/DynamicLinker64+MachO.cpp create mode 100644 src/CompilerKit/src/Generators/DynamicLinker64+PEF.cpp delete mode 100644 src/CompilerKit/src/Linkers/DynamicLinker64+MachO.cpp delete mode 100644 src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cpp create mode 100644 src/CompilerKit/src/Optimizers/NectarOptimize+AMD64.cpp create mode 100644 src/CompilerKit/src/Preprocess/Preprocessor+Generic.cpp delete mode 100644 src/CompilerKit/src/Preprocessors/Preprocessor+Generic.cpp (limited to 'src') diff --git a/src/CompilerKit/src/Assemblers/Assembler+32x0.cpp b/src/CompilerKit/src/Assemblers/Assembler+32x0.cpp deleted file mode 100644 index a2eb191..0000000 --- a/src/CompilerKit/src/Assemblers/Assembler+32x0.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2024-2025, 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 - -///////////////////////////////////////////////////////////////////////////////////////// - -// @file Assembler+32x0.cc -// @author Amlal El Mahrouss -// @brief 32x0 Assembler. - -// REMINDER: when dealing with an undefined symbol use (string -// size):LinkerFindSymbol:(string) so that ld will look for it. - -///////////////////////////////////////////////////////////////////////////////////////// - -#ifndef __ASM_NEED_32x0__ -#define __ASM_NEED_32x0__ -#endif - -#include -#include -#include -#include -#include - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief 32x0 Assembler entrypoint, the program/module starts here. - -///////////////////////////////////////////////////////////////////////////////////////// - -NECTAR_MODULE(NEAssemblerMain32000) { - CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); - return EXIT_SUCCESS; -} diff --git a/src/CompilerKit/src/Assemblers/Assembler+64x0.cpp b/src/CompilerKit/src/Assemblers/Assembler+64x0.cpp deleted file mode 100644 index 4f33246..0000000 --- a/src/CompilerKit/src/Assemblers/Assembler+64x0.cpp +++ /dev/null @@ -1,869 +0,0 @@ -// 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 - -/// bugs: 0 - -///////////////////////////////////////////////////////////////////////////////////////// - -// @file Assembler+64x0.cpp -// @author Amlal El Mahrouss -// @brief 64x000 Assembler. - -// REMINDER: when dealing with an undefined symbol use (string -// size):LinkerFindSymbol:(string) so that ld will look for it. - -///////////////////////////////////////////////////////////////////////////////////////// - -#ifndef __ASM_NEED_64x0__ -#define __ASM_NEED_64x0__ -#endif - -#include -#include -#include -#include -#include - -static char kOutputArch = CompilerKit::kPefArch64000; - -constexpr auto k64x0IPAlignment = 0x1U; - -static std::size_t kCounter = 1UL; - -static std::uintptr_t kOrigin = kPefBaseOrigin; -static std::vector> kOriginLabel; - -static std::vector kBytes; - -static CompilerKit::AERecordHeader kCurrentRecord{ - .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; - -static std::vector kRecords; -static std::vector kUndefinedSymbols; - -static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; -static const std::string kRelocSymbol = ":RuntimeSymbol:"; - -// \brief forward decl. -static bool asm_read_attributes(std::string line); - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief 64x0 assembler entrypoint, the program/module starts here. - -///////////////////////////////////////////////////////////////////////////////////////// - -NECTAR_MODULE(AssemblerMain64x0) { - CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); - - for (size_t i = 1; i < argc; ++i) { - if (argv[i][0] == '-') { - if (strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "--v") == 0) { - kStdOut - << "Assembler64x0: 64x0 Assembler.\nAssembler64x0: v1.10\nAssembler64x0: Copyright (c) " - "Amlal El Mahrouss\n"; - return 0; - } else if (strcmp(argv[i], "--help") == 0) { - kStdOut << "Assembler64x0: 64x0 Assembler.\nAssembler64x0: Copyright (c) 2024 Mahrouss " - "Logic.\n"; - kStdOut << "--version: Print program version.\n"; - kStdOut << "--fverbose: Print verbose output.\n"; - kStdOut << "--fbinary: Output as flat binary.\n"; - kStdOut << "--64xxx: Compile for a subset of the X64000.\n"; - - return 0; - } else if (strcmp(argv[i], "--fbinary") == 0) { - kOutputAsBinary = true; - continue; - } else if (strcmp(argv[i], "--fverbose") == 0) { - kVerbose = true; - continue; - } - - kStdOut << "Assembler64x0: ignore " << argv[i] << "\n"; - continue; - } - - if (!std::filesystem::exists(argv[i])) { - kStdOut << "Assembler64x0: can't open: " << argv[i] << std::endl; - goto asm_fail_exit; - } - - std::string object_output(argv[i]); - - for (auto& ext : kAsmFileExts) { - if (object_output.find(ext) != std::string::npos) { - object_output.erase(object_output.find(ext), std::strlen(ext)); - } - } - - object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; - - std::ifstream file_ptr(argv[i]); - std::ofstream file_ptr_out(object_output, std::ofstream::binary); - - if (file_ptr_out.bad()) { - if (kVerbose) { - kStdOut << "Assembler64x0: error: " << strerror(errno) << "\n"; - } - } - - std::string line; - - CompilerKit::AEHeader hdr{0}; - - memset(hdr.fPad, kAENullType, kAEPad); - - hdr.fMagic[0] = kAEMag0; - hdr.fMagic[1] = kAEMag1; - hdr.fMagic[2] = kAEMag2; - hdr.fSize = sizeof(CompilerKit::AEHeader); - hdr.fArch = kOutputArch; - - ///////////////////////////////////////////////////////////////////////////////////////// - - // COMPILATION LOOP - - ///////////////////////////////////////////////////////////////////////////////////////// - - CompilerKit::Encoder64x0 asm64; - - while (std::getline(file_ptr, line)) { - if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { - CompilerKit::Detail::print_error(ln, argv[i]); - continue; - } - - try { - asm_read_attributes(line); - asm64.WriteLine(line, argv[i]); - } catch (const std::exception& e) { - if (kVerbose) { - std::string what = e.what(); - CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); - } - - std::filesystem::remove(object_output); - goto asm_fail_exit; - } - } - - if (!kOutputAsBinary) { - if (kVerbose) { - kStdOut << "Assembler64x0: Writing object file...\n"; - } - - // this is the final step, write everything to the file. - - auto pos = file_ptr_out.tellp(); - - hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); - - file_ptr_out << hdr; - - if (kRecords.empty()) { - kStdErr << "Assembler64x0: At least one record is needed to write an object " - "file.\nAssembler64x0: Make one using `public_segment .code64 foo_bar`.\n"; - - std::filesystem::remove(object_output); - return 1; - } - - kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - std::size_t record_count = 0UL; - - for (auto& rec : kRecords) { - if (kVerbose) kStdOut << "Assembler64x0: Wrote record " << rec.fName << " to file...\n"; - - rec.fFlags |= CompilerKit::kKindRelocationAtRuntime; - rec.fOffset = record_count; - ++record_count; - - file_ptr_out << rec; - } - - // increment once again, so that we won't lie about the kUndefinedSymbols. - ++record_count; - - for (auto& sym : kUndefinedSymbols) { - CompilerKit::AERecordHeader _record_hdr{0}; - - if (kVerbose) kStdOut << "Assembler64x0: Wrote symbol " << sym << " to file...\n"; - - _record_hdr.fKind = kAENullType; - _record_hdr.fSize = sym.size(); - _record_hdr.fOffset = record_count; - - ++record_count; - - memset(_record_hdr.fPad, kAENullType, kAEPad); - memcpy(_record_hdr.fName, sym.c_str(), sym.size()); - - file_ptr_out << _record_hdr; - - ++kCounter; - } - - auto pos_end = file_ptr_out.tellp(); - - file_ptr_out.seekp(pos); - - hdr.fStartCode = pos_end; - hdr.fCodeSize = kBytes.size(); - - file_ptr_out << hdr; - - file_ptr_out.seekp(pos_end); - } else { - if (kVerbose) { - kStdOut << "Assembler64x0: Write raw binary...\n"; - } - } - - // byte from byte, we write this. - for (auto& byte : kBytes) { - file_ptr_out.write(reinterpret_cast(&byte), sizeof(byte)); - } - - if (kVerbose) kStdOut << "Assembler64x0: Wrote file with program in it.\n"; - - file_ptr_out.flush(); - file_ptr_out.close(); - - if (kVerbose) kStdOut << "Assembler64x0: Exit succeeded.\n"; - - return 0; - } - -asm_fail_exit: - - if (kVerbose) kStdOut << "Assembler64x0: Exit failed.\n"; - - return 1; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for attributes -// returns true if any was found. - -///////////////////////////////////////////////////////////////////////////////////////// - -static bool asm_read_attributes(std::string line) { - // extern_segment is the opposite of public_segment, it signals to the ld - // that we need this symbol. - if (CompilerKit::ast_find_needle(line, "extern_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid extern_segment directive in flat binary mode.", - "CompilerKit"); - throw std::runtime_error("invalid_extern_segment_bin"); - } - - auto name = line.substr(line.find("extern_segment") + strlen("extern_segment")); - - /// sanity check to avoid stupid linker errors. - if (name.size() == 0) { - CompilerKit::Detail::print_error("Invalid extern_segment", "Nectar"); - throw std::runtime_error("invalid_extern_segment"); - } - - std::string result = std::to_string(name.size()); - result += kUndefinedSymbol; - - // mangle this - for (char& j : name) { - if (j == ' ' || j == ',') j = '$'; - } - - result += name; - - if (name.find(kPefCode64) != std::string::npos) { - // data is treated as code. - kCurrentRecord.fKind = CompilerKit::kPefCode; - } else if (name.find(kPefData64) != std::string::npos) { - // no code will be executed from here. - kCurrentRecord.fKind = CompilerKit::kPefData; - } else if (name.find(kPefZero64) != std::string::npos) { - // this is a bss section. - kCurrentRecord.fKind = CompilerKit::kPefZero; - } - - // this is a special case for the start stub. - // we want this so that ld can find it. - - if (name == kPefStart) { - kCurrentRecord.fKind = CompilerKit::kPefCode; - } - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, result.c_str(), result.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - // public_segment is a special keyword used by Assembler64x0 to tell the AE output stage to - // mark this section as a header. it currently supports .code64, .data64., - // .zero64 - else if (CompilerKit::ast_find_needle(line, "public_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid public_segment directive in flat binary mode.", - "CompilerKit"); - throw std::runtime_error("invalid_public_segment_bin"); - } - - auto name = line.substr(line.find("public_segment") + strlen("public_segment")); - - std::string name_copy = name; - - for (char& j : name) { - if (j == ' ') j = '$'; - } - - if (name.find(".code64") != std::string::npos) { - // data is treated as code. - - name_copy.erase(name_copy.find(".code64"), strlen(".code64")); - kCurrentRecord.fKind = CompilerKit::kPefCode; - } else if (name.find(".data64") != std::string::npos) { - // no code will be executed from here. - - name_copy.erase(name_copy.find(".data64"), strlen(".data64")); - kCurrentRecord.fKind = CompilerKit::kPefData; - } else if (name.find(".zero64") != std::string::npos) { - // this is a bss section. - - name_copy.erase(name_copy.find(".zero64"), strlen(".zero64")); - kCurrentRecord.fKind = CompilerKit::kPefZero; - } - - // this is a special case for the start stub. - // we want this so that ld can find it. - - if (name == kPefStart) { - kCurrentRecord.fKind = CompilerKit::kPefCode; - } - - while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); - - kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); - ++kOrigin; - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, name.c_str(), name.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - - return false; -} - -// \brief algorithms and helpers. - -namespace CompilerKit::Detail::algorithm { -// \brief authorize a brief set of characters. -static inline bool is_not_alnum_space(char c) { - return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') || (c == ',') || (c == '(') || - (c == ')') || (c == '"') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || - (c == '_') || (c == ':') || (c == '@') || (c == '.')); -} - -bool is_valid_64x0(std::string str) { - return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); -} -} // namespace CompilerKit::Detail::algorithm - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for line (syntax check) - -///////////////////////////////////////////////////////////////////////////////////////// - -std::string CompilerKit::Encoder64x0::CheckLine(std::string line, std::string file) { - std::string err_str; - - if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || - CompilerKit::ast_find_needle(line, "public_segment") || line.find('#') != std::string::npos || - CompilerKit::ast_find_needle(line, ";")) { - if (line.find('#') != std::string::npos) { - line.erase(line.find('#')); - } else if (line.find(';') != std::string::npos) { - line.erase(line.find(';')); - } else { - // now check the line for validity - if (!CompilerKit::Detail::algorithm::is_valid_64x0(line)) { - err_str = "Line contains non alphanumeric characters.\nhere -> "; - err_str += line; - } - } - - return err_str; - } - - if (!CompilerKit::Detail::algorithm::is_valid_64x0(line)) { - err_str = "Line contains non alphanumeric characters.\nhere -> "; - err_str += line; - - return err_str; - } - - // check for a valid instruction format. - - if (line.find(',') != std::string::npos) { - if (line.find(',') + 1 == line.size()) { - err_str += "\nInstruction lacks right register, here -> "; - err_str += line.substr(line.find(',')); - - return err_str; - } else { - bool nothing_on_right = true; - - if (line.find(',') + 1 > line.size()) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - - auto substr = line.substr(line.find(',') + 1); - - for (auto& ch : substr) { - if (ch != ' ' && ch != '\t') { - nothing_on_right = false; - } - } - - // this means we found nothing after that ',' . - if (nothing_on_right) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - } - } - - // these do take an argument. - std::vector operands_inst = {"stw", "ldw", "lda", "sta"}; - - // these don't. - std::vector filter_inst = {"jlr", "jrl", "int"}; - - for (auto& opcode64x0 : kOpcodes64x0) { - if (line.find(opcode64x0.fName) != std::string::npos) { - if (opcode64x0.fFunct7 == kAsmNoArgs) return err_str; - - for (auto& op : operands_inst) { - // if only the instruction was found. - if (line == op) { - err_str += "\nMalformed "; - err_str += op; - err_str += " instruction, here -> "; - err_str += line; - } - } - - // if it is like that -> addr1, 0x0 - if (auto it = std::find(filter_inst.begin(), filter_inst.end(), opcode64x0.fName); - it == filter_inst.cend()) { - if (CompilerKit::ast_find_needle(line, opcode64x0.fName)) { - if (!isspace(line[line.find(opcode64x0.fName) + strlen(opcode64x0.fName)])) { - err_str += "\nMissing space between "; - err_str += opcode64x0.fName; - err_str += " and operands.\nhere -> "; - err_str += line; - } - } - } - - return err_str; - } - } - - err_str += "Unrecognized instruction: " + line; - - return err_str; -} - -bool CompilerKit::Encoder64x0::WriteNumber(const std::size_t& pos, std::string& jump_label) { - if (!isdigit(jump_label[pos])) return false; - - switch (jump_label[pos + 1]) { - case 'x': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_hex_number"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); - - for (char& i : num.number) { - kBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "Assembler64x0: found a base 16 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; - } - case 'b': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_bin"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); - - if (kVerbose) { - kStdOut << "Assembler64x0: found a base 2 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kBytes.push_back(i); - } - - return true; - } - case 'o': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_octal"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); - - if (kVerbose) { - kStdOut << "Assembler64x0: found a base 8 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kBytes.push_back(i); - } - - return true; - } - default: { - break; - } - } - - /* check for errno and stuff like that */ - if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { - if (errno != 0) { - return false; - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); - - for (char& i : num.number) { - kBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "Assembler64x0: found a base 10 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Read and write an instruction to the output array. - -///////////////////////////////////////////////////////////////////////////////////////// - -bool CompilerKit::Encoder64x0::WriteLine(std::string line, std::string file) { - if (CompilerKit::ast_find_needle(line, "public_segment ")) return true; - - for (auto& opcode64x0 : kOpcodes64x0) { - // strict check here - if (CompilerKit::ast_find_needle(line, opcode64x0.fName) && - CompilerKit::Detail::algorithm::is_valid_64x0(line)) { - std::string name(opcode64x0.fName); - std::string jump_label, cpy_jump_label; - - kBytes.emplace_back(opcode64x0.fOpcode); - kBytes.emplace_back(opcode64x0.fFunct3); - kBytes.emplace_back(opcode64x0.fFunct7); - - // check funct7 type. - switch (opcode64x0.fFunct7) { - // reg to reg means register to register transfer operation. - case kAsmRegToReg: - case kAsmImmediate: { - // \brief how many registers we found. - std::size_t found_some = 0UL; - - for (size_t line_index = 0UL; line_index < line.size(); line_index++) { - if (line[line_index] == kAsmRegisterPrefix[0] && isdigit(line[line_index + 1])) { - std::string register_syntax = kAsmRegisterPrefix; - register_syntax += line[line_index + 1]; - - if (isdigit(line[line_index + 2])) register_syntax += line[line_index + 2]; - - std::string reg_str; - reg_str += line[line_index + 1]; - - if (isdigit(line[line_index + 2])) reg_str += line[line_index + 2]; - - // it ranges from r0 to r19 - // something like r190 doesn't exist in the instruction set. - if (kOutputArch == CompilerKit::kPefArch64000) { - if (isdigit(line[line_index + 3]) && isdigit(line[line_index + 2])) { - reg_str += line[line_index + 3]; - CompilerKit::Detail::print_error( - "invalid register index, r" + reg_str + - "\nnote: The 64x0 accepts registers from r0 to r20.", - file); - throw std::runtime_error("invalid_register_index"); - } - } - - // finally cast to a size_t - std::size_t reg_index = strtol(reg_str.c_str(), nullptr, 10); - - if (reg_index > kAsmRegisterLimit) { - CompilerKit::Detail::print_error("invalid register index, r" + reg_str, file); - throw std::runtime_error("invalid_register_index"); - } - - kBytes.emplace_back(reg_index); - ++found_some; - - if (kVerbose) { - kStdOut << "Assembler64x0: Register found: " << register_syntax << "\n"; - kStdOut << "Assembler64x0: Register amount in instruction: " << found_some << "\n"; - } - } - } - - // we're not in immediate addressing, reg to reg. - if (opcode64x0.fFunct7 != kAsmImmediate) { - // remember! register to register! - if (found_some == 1) { - CompilerKit::Detail::print_error( - "Too few registers.\ntip: each Assembler64x0 register " - "starts with 'r'.\nline: " + - line, - file); - throw std::runtime_error("not_a_register"); - } - } - - if (found_some < 1 && name != "ldw" && name != "lda" && name != "stw") { - CompilerKit::Detail::print_error( - "invalid combination of opcode and registers.\nline: " + line, file); - throw std::runtime_error("invalid_comb_op_reg"); - } else if (found_some == 1 && name == "add") { - CompilerKit::Detail::print_error( - "invalid combination of opcode and registers.\nline: " + line, file); - throw std::runtime_error("invalid_comb_op_reg"); - } else if (found_some == 1 && name == "sub") { - CompilerKit::Detail::print_error( - "invalid combination of opcode and registers.\nline: " + line, file); - throw std::runtime_error("invalid_comb_op_reg"); - } - - if (found_some > 0 && name == "pop") { - CompilerKit::Detail::print_error( - "invalid combination for opcode 'pop'.\ntip: it expects " - "nothing.\nline: " + - line, - file); - throw std::runtime_error("invalid_comb_op_pop"); - } - } - default: - break; - } - - // try to fetch a number from the name - if (name == "stw" || name == "ldw" || name == "lda" || name == "sta") { - auto where_string = name; - - // if we load something, we'd need it's symbol/literal - if (name == "stw" || name == "sta" || name == "ldw" || name == "lda" || name == "sta") - where_string = ","; - - jump_label = line; - - auto found_sym = false; - - while (jump_label.find(where_string) != std::string::npos) { - jump_label = jump_label.substr(jump_label.find(where_string) + where_string.size()); - - while (jump_label.find(" ") != std::string::npos) { - jump_label.erase(jump_label.find(" "), 1); - } - - if (jump_label[0] != kAsmRegisterPrefix[0] && !isdigit(jump_label[1])) { - if (found_sym) { - CompilerKit::Detail::print_error( - "invalid combination of opcode and operands.\nhere -> " + jump_label, file); - throw std::runtime_error("invalid_comb_op_ops"); - } else { - // death trap installed. - found_sym = true; - } - } - } - - cpy_jump_label = jump_label; - - // replace any spaces with $ - if (jump_label[0] == ' ') { - while (jump_label.find(' ') != std::string::npos) { - if (isalnum(jump_label[0]) || isdigit(jump_label[0])) break; - - jump_label.erase(jump_label.find(' '), 1); - } - } - - if (!this->WriteNumber(0, jump_label)) { - // sta expects this: sta 0x000000, r0 - if (name == "sta") { - CompilerKit::Detail::print_error( - "invalid combination of opcode and operands.\nHere ->" + line, file); - throw std::runtime_error("invalid_comb_op_ops"); - } - } else { - if (name == "sta" && cpy_jump_label.find("extern_segment ") != std::string::npos) { - CompilerKit::Detail::print_error("invalid usage extern_segment on 'sta', here: " + line, - file); - throw std::runtime_error("invalid_sta_usage"); - } - } - - goto asm_write_label; - } - - // This is the case where we jump to a label, it is also used as a goto. - if (name == "lda" || name == "sta") { - asm_write_label: - if (cpy_jump_label.find('\n') != std::string::npos) - cpy_jump_label.erase(cpy_jump_label.find('\n'), 1); - - if (cpy_jump_label.find("extern_segment") != std::string::npos) { - cpy_jump_label.erase(cpy_jump_label.find("extern_segment"), strlen("extern_segment")); - - if (name == "sta") { - CompilerKit::Detail::print_error("extern_segment is not allowed on a sta operation.", - file); - throw std::runtime_error("extern_segment_sta_op"); - } else { - goto asm_end_label_cpy; - } - } - - if (name == "lda" || name == "sta") { - for (auto& label : kOriginLabel) { - if (cpy_jump_label == label.first) { - if (kVerbose) { - kStdOut << "Assembler64x0: Replace label " << cpy_jump_label - << " to address: " << label.second << std::endl; - } - - CompilerKit::NumberCast64 num(label.second); - - for (auto& num : num.number) { - kBytes.push_back(num); - } - - goto asm_end_label_cpy; - } - } - - if (cpy_jump_label[0] == '0') { - switch (cpy_jump_label[1]) { - case 'x': - case 'o': - case 'b': - if (this->WriteNumber(0, cpy_jump_label)) goto asm_end_label_cpy; - - break; - default: - break; - } - - if (isdigit(cpy_jump_label[0])) { - if (this->WriteNumber(0, cpy_jump_label)) goto asm_end_label_cpy; - - break; - } - } - } - - if (cpy_jump_label.size() < 1) { - CompilerKit::Detail::print_error("label is empty, can't jump on it.", file); - throw std::runtime_error("label_empty"); - } - - /// don't go any further if: - /// load word (ldw) or store word. (stw) - - if (name == "ldw" || name == "stw") break; - - auto mld_reloc_str = std::to_string(cpy_jump_label.size()); - mld_reloc_str += kUndefinedSymbol; - mld_reloc_str += cpy_jump_label; - - bool ignore_back_slash = false; - - for (auto& reloc_chr : mld_reloc_str) { - if (reloc_chr == '\\') { - ignore_back_slash = true; - continue; - } - - if (ignore_back_slash) { - ignore_back_slash = false; - continue; - } - - kBytes.push_back(reloc_chr); - } - - kBytes.push_back('\0'); - goto asm_end_label_cpy; - } - - asm_end_label_cpy: - kOrigin += k64x0IPAlignment; - - break; - } - } - - return true; -} - -// Last rev 13-1-24 diff --git a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp deleted file mode 100644 index f064a39..0000000 --- a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp +++ /dev/null @@ -1,1849 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// 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 - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @file Assembler+AMD64.cpp -/// @author Amlal El Mahrouss. (amlal@nekernel.org) -/// @brief AMD64 Assembler. -/// REMINDER: when dealing with an undefined symbol use (string -/// size):LinkerFindSymbol:(string) so that ld will look for it. - -///////////////////////////////////////////////////////////////////////////////////////// - -/// BUG: 0 - -/// Feature request: 1 -/// Encode registers in mov, add, xor... - -///////////////////////////////////////////////////////////////////////////////////////// - -#ifndef __ASM_NEED_AMD64__ -#define __ASM_NEED_AMD64__ -#endif - -#define kAssemblerPragmaSymStr "%" -#define kAssemblerPragmaSym '%' - -#include -#include -#include -#include -#include - -static char kOutputArch = CompilerKit::kPefArchAMD64; - -static constexpr auto kIPAlignement = 0x1U; -static auto kCounter = 0x1UL; - -static std::uintptr_t kOrigin = kPefBaseOrigin; -static std::vector> kOriginLabel; - -/// @brief keep it simple by default. -static std::int32_t kRegisterBitWidth = 16U; - -static std::vector kAppBytes; - -static CompilerKit::AERecordHeader kCurrentRecord{ - .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; - -static std::vector kRecords; -static std::vector kDefinedSymbols; -static std::vector kUndefinedSymbols; - -static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; - -// @brief forward decl. -static bool asm_read_attributes(std::string line); - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @brief AMD64 assembler entrypoint, the program/module starts here. - -///////////////////////////////////////////////////////////////////////////////////////// - -NECTAR_MODULE(AssemblerMainAMD64) { - //////////////// CPU OPCODES BEGIN //////////////// - - CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); - - std::string opcodes_jump[kJumpLimit] = {"ja", "jae", "jb", "jbe", "jc", "je", "jg", "jge", - "jl", "jle", "jna", "jnae", "jnb", "jnbe", "jnc", "jne", - "jng", "jnge", "jnl", "jnle", "jno", "jnp", "jns", "jnz", - "jo", "jp", "jpe", "jpo", "js", "jz"}; - - for (i64_hword_t i{}; i < kJumpLimit; i++) { - CpuOpcodeAMD64 code{.fName = opcodes_jump[i], - .fOpcode = static_cast(kAsmJumpOpcode + i)}; - kOpcodesAMD64.push_back(code); - } - - CpuOpcodeAMD64 code{.fName = "jcxz", .fOpcode = 0xE3}; - kOpcodesAMD64.push_back(code); - - for (i64_hword_t i{kJumpLimitStandard}; i < kJumpLimitStandardLimit; i++) { - CpuOpcodeAMD64 code{.fName = "jmp", .fOpcode = i}; - kOpcodesAMD64.push_back(code); - } - - CpuOpcodeAMD64 lahf{.fName = "lahf", .fOpcode = 0x9F}; - kOpcodesAMD64.push_back(lahf); - - CpuOpcodeAMD64 lds{.fName = "lds", .fOpcode = 0xC5}; - kOpcodesAMD64.push_back(lds); - - CpuOpcodeAMD64 lea{.fName = "lea", .fOpcode = 0x8D}; - kOpcodesAMD64.push_back(lea); - - CpuOpcodeAMD64 nop{.fName = "nop", .fOpcode = 0x90}; - kOpcodesAMD64.push_back(nop); - - //////////////// CPU OPCODES END //////////////// - - 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"; - 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 << "-version: Print program version.\n"; - kStdOut << "-fverbose: Print verbose output.\n"; - kStdOut << "-fbinary: Output as flat binary.\n"; - - return 0; - } else if (strcmp(argv[i], "-fbinary") == 0) { - kOutputAsBinary = true; - continue; - } else if (strcmp(argv[i], "-fverbose") == 0) { - kVerbose = true; - continue; - } - - kStdOut << "AssemblerAMD64: ignore " << argv[i] << "\n"; - continue; - } - - if (!std::filesystem::exists(argv[i])) { - kStdOut << "AssemblerAMD64: can't open: " << argv[i] << std::endl; - goto asm_fail_exit; - } - - std::string object_output(argv[i]); - std::string asm_input(argv[i]); - - for (auto& ext : kAsmFileExts) { - if (object_output.ends_with(ext)) { - object_output.erase(object_output.find(ext), std::strlen(ext)); - break; - } - } - - object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; - - std::ifstream file_ptr(argv[i]); - std::ofstream file_ptr_out(object_output, std::ofstream::binary); - - kStdOut << "AssemblerAMD64: Assembling: " << argv[i] << "\n"; - - if (file_ptr_out.bad()) { - if (kVerbose) { - kStdOut << "AssemblerAMD64: error: " << strerror(errno) << "\n"; - } - - return 1; - } - - std::string line; - - CompilerKit::AEHeader hdr{0}; - - memset(hdr.fPad, kAENullType, kAEPad); - - hdr.fMagic[0] = kAEMag0; - hdr.fMagic[1] = kAEMag1; - hdr.fMagic[2] = kAEMag2; - hdr.fSize = sizeof(CompilerKit::AEHeader); - hdr.fArch = kOutputArch; - - ///////////////////////////////////////////////////////////////////////////////////////// - - // COMPILATION LOOP - - ///////////////////////////////////////////////////////////////////////////////////////// - - CompilerKit::EncoderAMD64 asm64; - - if (kVerbose) { - kStdOut << "Compiling: " + asm_input << "\n"; - } - - while (std::getline(file_ptr, line)) { - try { - if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { - CompilerKit::Detail::print_error(ln, argv[i]); - continue; - } - - asm_read_attributes(line); - asm64.WriteLine(line, argv[i]); - } catch (const std::exception& e) { - if (kVerbose) { - std::string what = e.what(); - CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); - } - - try { - std::filesystem::remove(object_output); - } catch (...) { - } - - goto asm_fail_exit; - } - } - - if (!kOutputAsBinary) { - if (kVerbose) { - kStdOut << "AssemblerAMD64: Writing object file...\n"; - } - - // this is the final step, write everything to the file. - - auto pos = file_ptr_out.tellp(); - - hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); - - file_ptr_out << hdr; - - if (kRecords.empty()) { - kStdErr << "AssemblerAMD64: At least one record is needed to write an object " - "file.\nAssemblerAMD64: Make one using `public_segment .code64 foo_bar`.\n"; - - std::filesystem::remove(object_output); - return 1; - } - - kRecords[kRecords.size() - 1].fSize = kAppBytes.size(); - - std::size_t record_count = 0UL; - - for (auto& rec : kRecords) { - if (kVerbose) kStdOut << "AssemblerAMD64: Wrote record " << rec.fName << " to file...\n"; - - rec.fFlags |= CompilerKit::kKindRelocationAtRuntime; - rec.fOffset = record_count; - ++record_count; - - file_ptr_out << rec; - } - - // increment once again, so that we won't lie about the kUndefinedSymbols. - ++record_count; - - for (auto& sym : kUndefinedSymbols) { - CompilerKit::AERecordHeader undefined_sym{0}; - - if (kVerbose) kStdOut << "AssemblerAMD64: Wrote symbol " << sym << " to file...\n"; - - undefined_sym.fKind = CompilerKit::kKindRelocationAtRuntime; - undefined_sym.fSize = sym.size(); - undefined_sym.fOffset = record_count; - - ++record_count; - - memset(undefined_sym.fPad, kAENullType, kAEPad); - memcpy(undefined_sym.fName, sym.c_str(), sym.size()); - - file_ptr_out << undefined_sym; - - ++kCounter; - } - - auto pos_end = file_ptr_out.tellp(); - - file_ptr_out.seekp(pos); - - hdr.fStartCode = pos_end; - hdr.fCodeSize = kAppBytes.size(); - - file_ptr_out << hdr; - - file_ptr_out.seekp(pos_end); - } else { - if (kVerbose) { - kStdOut << "AssemblerAMD64: Write raw binary...\n"; - } - } - - // byte from byte, we write this. - for (auto& byte : kAppBytes) { - file_ptr_out << reinterpret_cast(&byte)[0]; - } - - if (kVerbose) kStdOut << "AssemblerAMD64: Wrote file with program in it.\n"; - - file_ptr_out.flush(); - file_ptr_out.close(); - - if (kVerbose) kStdOut << "AssemblerAMD64: Exiting: Succeeded.\n"; - - return 0; - } - -asm_fail_exit: - - if (kVerbose) kStdOut << "AssemblerAMD64: Exiting: Failed.\n"; - - return 1; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for attributes -// returns true if any was found. - -///////////////////////////////////////////////////////////////////////////////////////// - -static bool asm_read_attributes(CompilerKit::STLString line) { - // extern_segment is the opposite of public_segment, it signals to the ld - // that we need this symbol. - if (CompilerKit::ast_find_needle(line, "extern_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid directive in flat binary mode.", "CompilerKit"); - throw std::runtime_error("invalid_extern_segment_bin"); - } - - auto pos = line.find("extern_segment"); - auto name_pos = pos + strlen("extern_segment") + 1; - - if (pos == std::string::npos || name_pos >= line.size()) { - CompilerKit::Detail::print_error("Invalid extern_segment", "Nectar"); - throw std::runtime_error("invalid_extern_segment"); - } - - auto name = line.substr(name_pos); - - if (name.size() == 0) { - CompilerKit::Detail::print_error("Invalid extern_segment", "Nectar"); - throw std::runtime_error("invalid_extern_segment"); - } - - kUndefinedSymbols.push_back(name); - - std::string result = std::to_string(name.size()); - result += kUndefinedSymbol; - - // mangle this - for (char& j : name) { - if (j == ' ' || j == ',') j = '$'; - } - - result += name; - - if (name.find(kPefCode64) != std::string::npos) { - // data is treated as code. - kCurrentRecord.fKind = CompilerKit::kPefCode; - } else if (name.find(kPefData64) != std::string::npos) { - // no code will be executed from here. - kCurrentRecord.fKind = CompilerKit::kPefData; - } else if (name.find(kPefZero64) != std::string::npos) { - // this will be a bss section. - kCurrentRecord.fKind = CompilerKit::kPefZero; - } - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kAppBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, result.c_str(), result.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - // public_segment is a special keyword used by AssemblerAMD64 to tell the AE output stage to - // mark this section as a header. it currently supports .code64, .data64 and - // .zero64. - else if (CompilerKit::ast_find_needle(line, "public_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid directive in flat binary mode.", "CompilerKit"); - throw std::runtime_error("invalid_public_segment_bin"); - } - - auto res_sym_at = (line.find("public_segment") + strlen("public_segment") + 1); - if (res_sym_at > line.size()) { - CompilerKit::Detail::print_error("Invalid symbol for public_segment.", "CompilerKit"); - throw std::runtime_error("invalid_public_segment_symbol"); - } - - auto name = line.substr(res_sym_at); - - std::string name_copy = name; - - for (char& j : name) { - if (j == ' ') j = '$'; - } - - if (std::find(kDefinedSymbols.begin(), kDefinedSymbols.end(), name) != kDefinedSymbols.end()) { - CompilerKit::Detail::print_error("Symbol already defined.", "CompilerKit"); - throw std::runtime_error("invalid_public_segment_bin"); - } - - kDefinedSymbols.push_back(name); - - if (name.find(kPefCode64) != std::string::npos) { - // data is treated as code. - kCurrentRecord.fKind = CompilerKit::kPefCode; - } else if (name.find(kPefData64) != std::string::npos) { - // no code will be executed from here. - kCurrentRecord.fKind = CompilerKit::kPefData; - } else if (name.find(kPefZero64) != std::string::npos) { - // this is a bss section. - kCurrentRecord.fKind = CompilerKit::kPefZero; - } - - while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); - - kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); - ++kOrigin; - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kAppBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, name.c_str(), name.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - - return false; -} - -// \brief algorithms and helpers. - -namespace CompilerKit::Detail::Algorithm { - -// \brief authorize a brief set of characters. -static inline bool is_not_valid(char c) { - if ((isalpha(c) || isdigit(c)) || - ((c == ' ') || (c == '\t') || (c == ',') || (c == '(') || (c == ')') || (c == '"') || - (c == '*') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || (c == '_') || - (c == ':') || (c == '@') || (c == '.') || (c == '#') || (c == '%') || (c == '~') || - (c == ';'))) - return false; - - return true; -} - -bool is_valid_amd64(std::string str) { - return std::find_if(str.begin(), str.end(), is_not_valid) == str.end(); -} - -} // namespace CompilerKit::Detail::Algorithm - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for line (syntax check) - -///////////////////////////////////////////////////////////////////////////////////////// - -std::string CompilerKit::EncoderAMD64::CheckLine(std::string line, std::string file) { - std::string err_str; - - if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || - CompilerKit::ast_find_needle(line, "public_segment") || - CompilerKit::ast_find_needle(line, kAssemblerPragmaSymStr) || - CompilerKit::ast_find_needle(line, ";") || line[0] == kAssemblerPragmaSym) { - if (line.find(';') != std::string::npos) { - line.erase(line.find(';')); - } else { - // now check the line for validity - if (!CompilerKit::Detail::Algorithm::is_valid_amd64(line)) { - err_str = "Line contains non valid characters.\nhere -> "; - err_str += line; - } - } - - return err_str; - } - - // check for a valid instruction format. - - if (line.find(',') != std::string::npos) { - if (line.find(',') + 1 == line.size()) { - err_str += "\nInstruction lacks right register, here -> "; - err_str += line.substr(line.find(',')); - - return err_str; - } else { - bool nothing_on_right = true; - - if (line.find(',') + 1 > line.size()) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - - auto substr = line.substr(line.find(',') + 1); - - for (auto& ch : substr) { - if (ch != ' ' && ch != '\t') { - nothing_on_right = false; - } - } - - // this means we found nothing after that ',' . - if (nothing_on_right) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - } - } - for (auto& opcodeAMD64 : kOpcodesAMD64) { - if (CompilerKit::ast_find_needle(line, opcodeAMD64.fName)) { - return err_str; - } - } - - err_str += "\nUnrecognized instruction -> " + line; - - return err_str; -} - -/// @brief Read and write a number to the output array. -bool CompilerKit::EncoderAMD64::WriteNumber(const std::size_t& pos, std::string& jump_label) { - if (!isdigit(jump_label[pos])) return false; - - switch (jump_label[pos + 1]) { - case 'x': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) - << "\n"; - } - - return true; - } - case 'b': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - return true; - } - case 'o': - case '0': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - return true; - } - default: { - break; - } - } - - /// @note We assume base 10 here. As other cases have failed. - auto res = strtol(jump_label.substr(pos + 1).c_str(), nullptr, 10); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); - - for (auto& nidx : num.number) { - kAppBytes.push_back(nidx); - } - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos + 1) - << "\n"; - } - - return true; -} - -bool CompilerKit::EncoderAMD64::WriteNumber32(const std::size_t& pos, std::string& jump_label) { - if (!isdigit(jump_label[pos])) return false; - - switch (jump_label[pos + 1]) { - case 'x': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) - << "\n"; - } - - return true; - } - case 'b': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - return true; - } - case 'o': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - return true; - } - default: { - break; - } - } - - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 10); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; -} - -bool CompilerKit::EncoderAMD64::WriteNumber16(const std::size_t& pos, std::string& jump_label) { - if (!isdigit(jump_label[pos])) return false; - - switch (jump_label[pos + 1]) { - case 'x': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("Invalid hex number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_hex"); - } - } - - CompilerKit::NumberCast16 num = - CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) - << "\n"; - } - - return true; - } - case 'b': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("Invalid binary number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_bin"); - } - } - - CompilerKit::NumberCast16 num = - CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - return true; - } - case 'o': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("Invalid octal number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_octal"); - } - } - - CompilerKit::NumberCast16 num = - CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8)); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - return true; - } - default: { - break; - } - } - - /* check for errno and stuff like that */ - if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { - if (errno != 0) { - return false; - } - } - - CompilerKit::NumberCast16 num = - CompilerKit::NumberCast16(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); - - for (char& i : num.number) { - kAppBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; -} - -bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string& jump_label) { - if (!isdigit(jump_label[pos])) return false; - - switch (jump_label[pos + 1]) { - case 'x': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); - - kAppBytes.push_back(num.number); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) - << "\n"; - } - - return true; - } - case 'b': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; - } - - kAppBytes.push_back(num.number); - - return true; - } - case 'o': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; - } - - kAppBytes.push_back(num.number); - - return true; - } - default: { - break; - } - } - - auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); - - if (errno != 0) { - return false; - } - - CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); - - kAppBytes.push_back(num.number); - - if (kVerbose) { - kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Read and write an instruction to the output array. - -///////////////////////////////////////////////////////////////////////////////////////// - -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; - }; - - std::vector kRegisterList{ - {.fName = "ax", .fModRM = 0x0}, {.fName = "cx", .fModRM = 1}, - {.fName = "dx", .fModRM = 0x2}, {.fName = "bx", .fModRM = 3}, - {.fName = "sp", .fModRM = 0x4}, {.fName = "bp", .fModRM = 5}, - {.fName = "si", .fModRM = 0x6}, {.fName = "di", .fModRM = 7}, - }; - - bool foundInstruction = false; - - for (auto& opcodeAMD64 : kOpcodesAMD64) { - // strict check here - if (CompilerKit::ast_find_needle(line, opcodeAMD64.fName) && - CompilerKit::Detail::Algorithm::is_valid_amd64(line)) { - foundInstruction = true; - std::string name(opcodeAMD64.fName); - - /// Move instruction handler. - if (line.find(name) != std::string::npos) { - 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; - - if (substr.find(",") == std::string::npos) { - CompilerKit::Detail::print_error("Syntax error: missing right operand.", "CompilerKit"); - throw std::runtime_error("syntax_err"); - } - - /// Handle [reg+n] or [reg-n] memory addressing for any register - if (substr.find('[') != std::string::npos) { - // Parse the memory operand - auto bracketStart = substr.find('['); - auto bracketEnd = substr.find(']'); - - if (bracketStart == std::string::npos || bracketEnd == std::string::npos) { - CompilerKit::Detail::print_error("Syntax error: malformed memory operand.", file); - throw std::runtime_error("syntax_err"); - } - - std::string memOperand = substr.substr(bracketStart + 1, bracketEnd - bracketStart - 1); - - // Register lookup table - struct RegInfo final { - const char* name{}; - i64_byte_t code{}; - }; - - RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, - {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; - - // Find base register in memory operand - i64_byte_t baseReg = 0; - bool foundBase = false; - - for (auto& reg : regs64) { - if (memOperand.find(reg.name) != std::string::npos) { - baseReg = reg.code; - foundBase = true; - break; - } - } - - if (!foundBase) { - CompilerKit::Detail::print_error("Invalid base register in memory operand.", file); - throw std::runtime_error("invalid_base_reg"); - } - - bool isRbp = (baseReg == 5); - bool isRsp = (baseReg == 4); - - // Parse displacement - int32_t displacement = 0; - bool hasDisp = false; - - auto plusPos = memOperand.find('+'); - auto minusPos = memOperand.find('-'); - - if (plusPos != std::string::npos) { - std::string dispStr = memOperand.substr(plusPos + 1); - displacement = static_cast(strtol(dispStr.c_str(), nullptr, 0)); - hasDisp = true; - } else if (minusPos != std::string::npos) { - std::string dispStr = memOperand.substr(minusPos + 1); - displacement = -static_cast(strtol(dispStr.c_str(), nullptr, 0)); - hasDisp = true; - } - - // Determine if destination is memory or register - auto commaPos = substr.find(','); - bool destIsMemory = bracketStart < commaPos; - - // Find register in the other operand - std::string otherOperand; - - if (destIsMemory) { - otherOperand = substr.substr(commaPos + 1); - } else { - otherOperand = substr.substr(0, commaPos); - } - - // Remove whitespace - while (!otherOperand.empty() && (otherOperand[0] == ' ' || otherOperand[0] == '\t')) { - otherOperand.erase(0, 1); - } - - // Check for register in other operand - i64_byte_t regCode = 0; - bool foundReg = false; - bool isImmediate = false; - int64_t immValue = 0; - - for (auto& reg : regs64) { - if (otherOperand.find(reg.name) != std::string::npos) { - regCode = reg.code; - foundReg = true; - break; - } - } - - if (!foundReg) { - // Check if it's an immediate value - std::string immStr = otherOperand; - - while (!immStr.empty() && (immStr[0] == ' ' || immStr[0] == '\t')) { - immStr.erase(0, 1); - } - if (!immStr.empty() && (isdigit(immStr[0]) || immStr[0] == '-')) { - isImmediate = true; - immValue = strtol(immStr.c_str(), nullptr, 0); - } - } - - // Determine mod field based on displacement size - // mod=00: [reg] no displacement (except rbp which requires disp8) - // mod=01: [reg+disp8] - // mod=10: [reg+disp32] - i64_byte_t mod = 0; - if (!hasDisp && displacement == 0) { - // [rbp] requires disp8 with 0, can't use mod=00 (it means RIP-relative) - mod = isRbp ? 0x01 : 0x00; - } else if (displacement >= -128 && displacement <= 127) { - mod = 0x01; // 8-bit displacement - } else { - mod = 0x02; // 32-bit displacement - } - - if (destIsMemory) { - if (foundReg) { - // mov [reg+n], reg - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0x89); // MOV r/m64, r64 - - // ModR/M: mod | reg << 3 | r/m - i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; - kAppBytes.emplace_back(modrm); - - // RSP needs SIB byte - if (isRsp) { - kAppBytes.emplace_back(0x24); // SIB: scale=0, index=4(none), base=4(rsp) - } - } else if (isImmediate) { - // mov qword [reg+n], imm32 - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0xC7); // MOV r/m64, imm32 - - // ModR/M: mod | 0 << 3 | r/m (reg field is 0 for this opcode) - i64_byte_t modrm = (mod << 6) | (0 << 3) | baseReg; - kAppBytes.emplace_back(modrm); - - // RSP needs SIB byte - if (isRsp) { - kAppBytes.emplace_back(0x24); - } - } else { - CompilerKit::Detail::print_error("Invalid source operand for mov to memory.", file); - throw std::runtime_error("invalid_operand"); - } - } else { - // mov reg, [reg+n] - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0x8B); // MOV r64, r/m64 - - // ModR/M: mod | reg << 3 | r/m - i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; - kAppBytes.emplace_back(modrm); - - // RSP needs SIB byte - if (isRsp) { - kAppBytes.emplace_back(0x24); - } - } - - // Write displacement - if (mod == 0x01) { - // 8-bit displacement - kAppBytes.emplace_back(static_cast(displacement & 0xFF)); - } else if (mod == 0x02) { - // 32-bit displacement - kAppBytes.emplace_back(static_cast(displacement & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 8) & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 16) & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 24) & 0xFF)); - } else if (isRbp) { - // rbp with mod=00 still needs disp8=0 - kAppBytes.emplace_back(0x00); - } - - // Write immediate if present - if (destIsMemory && isImmediate) { - kAppBytes.emplace_back(static_cast(immValue & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 8) & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 16) & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 24) & 0xFF)); - } - - break; - } - - bool onlyOneReg = true; - - std::vector currentRegList; - - currentRegList.reserve(3); - - for (auto reg : kRegisterList) { - std::string registerName; - - if (bits == 32) - registerName.push_back('e'); - else if (bits == 64) - registerName.push_back('r'); - else { - CompilerKit::Detail::print_error("Invalid size for register, current bit width is: " + - std::to_string(kRegisterBitWidth), - file); - throw std::runtime_error("invalid_reg_size"); - } - - registerName += reg.fName; - - while (line.find(registerName) != std::string::npos) { - line.erase(line.find(registerName), registerName.size()); - - if (bits == 16) { - if (registerName[0] == 'r') { - CompilerKit::Detail::print_error( - "Invalid size for register, current bit width is: " + - std::to_string(kRegisterBitWidth), - file); - throw std::runtime_error("invalid_reg_size"); - } - } - - currentRegList.push_back({.fName = registerName, .fModRM = reg.fModRM}); - } - } - - if (currentRegList.size() > 1) onlyOneReg = false; - - bool hasRBasedRegs = false; - - if (!onlyOneReg && currentRegList.size() == 2) { - if (currentRegList[0].fName[0] == 'r' || currentRegList[1].fName[0] == 'r') { - if (isdigit(currentRegList[0].fName[1]) && isdigit(currentRegList[1].fName[1])) { - kAppBytes.emplace_back(0x4d); - hasRBasedRegs = true; - } else if (isdigit(currentRegList[0].fName[1]) || - isdigit(currentRegList[1].fName[1])) { - kAppBytes.emplace_back(0x4c); - hasRBasedRegs = true; - } - } - } - - if (name == "mov") { - if (bits == 64 || bits == 32) { - if (!hasRBasedRegs && bits >= 32) { - kAppBytes.emplace_back(opcodeAMD64.fOpcode); - } else if (hasRBasedRegs && bits == 32) { - CompilerKit::Detail::print_error("Invalid combination of operands and registers.", - "CompilerKit"); - throw std::runtime_error("comb_op_reg"); - } - - if (!onlyOneReg) kAppBytes.emplace_back(0x89); - } else if (bits == 16) { - if (hasRBasedRegs) { - CompilerKit::Detail::print_error("Invalid combination of operands and registers.", - "CompilerKit"); - throw std::runtime_error("comb_op_reg"); - } else { - kAppBytes.emplace_back(0x66); - kAppBytes.emplace_back(0x89); - } - } - } else { - if (!hasRBasedRegs && bits >= 32) { - kAppBytes.emplace_back(opcodeAMD64.fOpcode); - } - - kAppBytes.emplace_back(0x31); - } - - if (onlyOneReg && currentRegList.size() > 0) { - auto num = GetNumber32(line, ","); - auto modrm = (0x3 << 6 | currentRegList[0].fModRM); - - kAppBytes.emplace_back(0xC7); // prefixed before placing the modrm and then the number. - kAppBytes.emplace_back(modrm); - - if (name != "xor") { - kAppBytes.emplace_back(num.number[0]); - kAppBytes.emplace_back(num.number[1]); - kAppBytes.emplace_back(num.number[2]); - kAppBytes.emplace_back(num.number[3]); - } - - 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 (bits == 16) { - if (currentRegList[0].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[1].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"); - } - } else { - if (currentRegList[0].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[1].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"); - } - } - - /// encode register using the modrm encoding. - - auto modrm = (0x3 << 6 | currentRegList[1].fModRM << 3 | currentRegList[0].fModRM); - - kAppBytes.emplace_back(modrm); - - break; - } - - /// Compare instruction handler. - if (name == "cmp") { - std::string substr = line.substr(line.find(name) + name.size()); - - if (substr.find(",") == std::string::npos) { - CompilerKit::Detail::print_error("Syntax error: missing right operand.", "CompilerKit"); - throw std::runtime_error("syntax_err"); - } - - // Register lookup table - struct RegInfo final { - CompilerKit::STLString name; - i64_byte_t code; - }; - - RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, - {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; - - /// Handle [reg+n] memory addressing - if (substr.find('[') != std::string::npos) { - auto bracketStart = substr.find('['); - auto bracketEnd = substr.find(']'); - - if (bracketEnd == std::string::npos) { - CompilerKit::Detail::print_error("Syntax error: malformed memory operand.", file); - throw std::runtime_error("syntax_err"); - } - - std::string memOperand = substr.substr(bracketStart + 1, bracketEnd - bracketStart - 1); - - // Find base register - i64_byte_t baseReg = 0; - bool foundBase = false; - - for (auto& reg : regs64) { - if (memOperand.find(reg.name) != std::string::npos) { - baseReg = reg.code; - foundBase = true; - break; - } - } - - if (!foundBase) { - CompilerKit::Detail::print_error("Invalid base register in memory operand.", file); - throw std::runtime_error("invalid_base_reg"); - } - - bool isRbp = (baseReg == 5); - bool isRsp = (baseReg == 4); - - // Parse displacement - int32_t displacement = 0; - bool hasDisp = false; - - auto plusPos = memOperand.find('+'); - auto minusPos = memOperand.find('-'); - - if (plusPos != std::string::npos) { - std::string dispStr = memOperand.substr(plusPos + 1); - displacement = static_cast(strtol(dispStr.c_str(), nullptr, 0)); - hasDisp = true; - } else if (minusPos != std::string::npos) { - std::string dispStr = memOperand.substr(minusPos + 1); - displacement = -static_cast(strtol(dispStr.c_str(), nullptr, 0)); - hasDisp = true; - } - - auto commaPos = substr.find(','); - bool destIsMemory = bracketStart < commaPos; - - std::string otherOperand; - if (destIsMemory) { - otherOperand = substr.substr(commaPos + 1); - } else { - otherOperand = substr.substr(0, commaPos); - } - - while (!otherOperand.empty() && (otherOperand[0] == ' ' || otherOperand[0] == '\t')) { - otherOperand.erase(0, 1); - } - - i64_byte_t regCode = 0; - bool foundReg = false; - bool isImmediate = false; - int64_t immValue = 0; - - for (auto& reg : regs64) { - if (otherOperand.find(reg.name) != std::string::npos) { - regCode = reg.code; - foundReg = true; - break; - } - } - - if (!foundReg) { - std::string immStr = otherOperand; - while (!immStr.empty() && (immStr[0] == ' ' || immStr[0] == '\t')) { - immStr.erase(0, 1); - } - if (!immStr.empty() && (isdigit(immStr[0]) || immStr[0] == '-')) { - isImmediate = true; - immValue = strtol(immStr.c_str(), nullptr, 0); - } - } - - // Determine mod field - i64_byte_t mod = 0; - if (!hasDisp && displacement == 0) { - mod = isRbp ? 0x01 : 0x00; - } else if (displacement >= -128 && displacement <= 127) { - mod = 0x01; - } else { - mod = 0x02; - } - - if (destIsMemory) { - if (foundReg) { - // cmp [reg+n], reg - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0x39); // CMP r/m64, r64 - - i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; - kAppBytes.emplace_back(modrm); - - if (isRsp) { - kAppBytes.emplace_back(0x24); - } - } else if (isImmediate) { - // cmp qword [reg+n], imm32 - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0x81); // CMP r/m64, imm32 - - // reg field = 7 for CMP - i64_byte_t modrm = (mod << 6) | (7 << 3) | baseReg; - kAppBytes.emplace_back(modrm); - - if (isRsp) { - kAppBytes.emplace_back(0x24); - } - } - } else { - // cmp reg, [reg+n] - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0x3B); // CMP r64, r/m64 - - i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; - kAppBytes.emplace_back(modrm); - - if (isRsp) { - kAppBytes.emplace_back(0x24); - } - } - - // Write displacement - if (mod == 0x01) { - kAppBytes.emplace_back(static_cast(displacement & 0xFF)); - } else if (mod == 0x02) { - kAppBytes.emplace_back(static_cast(displacement & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 8) & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 16) & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 24) & 0xFF)); - } else if (isRbp) { - kAppBytes.emplace_back(0x00); - } - - // Write immediate - if (destIsMemory && isImmediate) { - kAppBytes.emplace_back(static_cast(immValue & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 8) & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 16) & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 24) & 0xFF)); - } - - break; - } - - // Handle register-to-register and register-to-immediate - i64_byte_t reg1Code = 0; - i64_byte_t reg2Code = 0; - bool foundReg1 = false; - bool foundReg2 = false; - bool isImmediate = false; - int64_t immValue = 0; - - auto commaPos = substr.find(','); - std::string leftOperand = substr.substr(0, commaPos); - std::string rightOperand = substr.substr(commaPos + 1); - - while (!leftOperand.empty() && (leftOperand[0] == ' ' || leftOperand[0] == '\t')) { - leftOperand.erase(0, 1); - } - while (!rightOperand.empty() && (rightOperand[0] == ' ' || rightOperand[0] == '\t')) { - rightOperand.erase(0, 1); - } - - for (auto& reg : regs64) { - if (leftOperand.find(reg.name) != std::string::npos) { - reg1Code = reg.code; - foundReg1 = true; - break; - } - } - - for (auto& reg : regs64) { - if (rightOperand.find(reg.name) != std::string::npos) { - reg2Code = reg.code; - foundReg2 = true; - break; - } - } - - if (!foundReg2) { - if (!rightOperand.empty() && (isdigit(rightOperand[0]) || rightOperand[0] == '-')) { - isImmediate = true; - immValue = strtol(rightOperand.c_str(), nullptr, 0); - } - } - - if (foundReg1 && foundReg2) { - // cmp reg1, reg2 - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0x39); // CMP r/m64, r64 - - i64_byte_t modrm = (0x3 << 6) | (reg2Code << 3) | reg1Code; - kAppBytes.emplace_back(modrm); - } else if (foundReg1 && isImmediate) { - // cmp reg, imm - kAppBytes.emplace_back(0x48); // REX.W - kAppBytes.emplace_back(0x81); // CMP r/m64, imm32 - - // reg field = 7 for CMP - i64_byte_t modrm = (0x3 << 6) | (7 << 3) | reg1Code; - kAppBytes.emplace_back(modrm); - - kAppBytes.emplace_back(static_cast(immValue & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 8) & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 16) & 0xFF)); - kAppBytes.emplace_back(static_cast((immValue >> 24) & 0xFF)); - } else { - CompilerKit::Detail::print_error("Invalid operands for cmp instruction.", file); - throw std::runtime_error("invalid_cmp_operands"); - } - - break; - } - - /// LEA instruction handler. - if (name == "lea") { - std::string substr = line.substr(line.find(name) + name.size()); - - // Remove leading whitespace - while (!substr.empty() && (substr[0] == ' ' || substr[0] == '\t')) { - substr.erase(0, 1); - } - - if (substr.find(",") == std::string::npos || substr.find('[') == std::string::npos) { - CompilerKit::Detail::print_error("Syntax error: lea requires reg, [mem] format.", file); - throw std::runtime_error("syntax_err"); - } - - // Register lookup table - struct RegInfo final { - CompilerKit::STLString name; - i64_byte_t code; - }; - - RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, - {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; - - auto commaPos = substr.find(','); - std::string destOperand = substr.substr(0, commaPos); - std::string srcOperand = substr.substr(commaPos + 1); - - // Remove whitespace - while (!destOperand.empty() && (destOperand[0] == ' ' || destOperand[0] == '\t')) { - destOperand.erase(0, 1); - } - while (!srcOperand.empty() && (srcOperand[0] == ' ' || srcOperand[0] == '\t')) { - srcOperand.erase(0, 1); - } - - // Find destination register - i64_byte_t destReg = 0; - bool foundDest = false; - - for (auto& reg : regs64) { - if (destOperand.find(reg.name) != std::string::npos) { - destReg = reg.code; - foundDest = true; - break; - } - } - - if (!foundDest) { - CompilerKit::Detail::print_error("Invalid destination register for lea.", file); - throw std::runtime_error("invalid_dest_reg"); - } - - // Parse memory operand [base+disp] or [base-disp] - auto bracketStart = srcOperand.find('['); - auto bracketEnd = srcOperand.find(']'); - - if (bracketStart == std::string::npos || bracketEnd == std::string::npos) { - CompilerKit::Detail::print_error("Syntax error: malformed memory operand for lea.", - file); - throw std::runtime_error("syntax_err"); - } - - std::string memOperand = - srcOperand.substr(bracketStart + 1, bracketEnd - bracketStart - 1); - - // Find base register - i64_byte_t baseReg = 0; - bool foundBase = false; - - for (auto& reg : regs64) { - if (memOperand.find(reg.name) != std::string::npos) { - baseReg = reg.code; - foundBase = true; - break; - } - } - - if (!foundBase) { - CompilerKit::Detail::print_error("Invalid base register in memory operand for lea.", - file); - throw std::runtime_error("invalid_base_reg"); - } - - bool isRbp = (baseReg == 5); - bool isRsp = (baseReg == 4); - int32_t displacement = 0; - bool hasDisp = false; - - // Look for +/- displacement - auto plusPos = memOperand.find('+'); - auto minusPos = memOperand.find('-'); - - if (plusPos != std::string::npos) { - std::string dispStr = memOperand.substr(plusPos + 1); - displacement = static_cast(strtol(dispStr.c_str(), nullptr, 0)); - hasDisp = true; - } else if (minusPos != std::string::npos) { - std::string dispStr = memOperand.substr(minusPos + 1); - displacement = -static_cast(strtol(dispStr.c_str(), nullptr, 0)); - hasDisp = true; - } - - // Determine mod field - i64_byte_t mod = 0x00; - if (hasDisp || isRbp) { - if (displacement >= -128 && displacement <= 127) { - mod = 0x01; // 8-bit displacement - } else { - mod = 0x02; // 32-bit displacement - } - } - - // Emit REX.W prefix for 64-bit - kAppBytes.emplace_back(0x48); - - // Emit LEA opcode - kAppBytes.emplace_back(0x8D); - - // Emit ModR/M byte - i64_byte_t modrm = (mod << 6) | (destReg << 3) | baseReg; - kAppBytes.emplace_back(modrm); - - // RSP needs SIB byte - if (isRsp) { - kAppBytes.emplace_back(0x24); - } - - // Emit displacement - if (mod == 0x01) { - kAppBytes.emplace_back(static_cast(displacement & 0xFF)); - } else if (mod == 0x02) { - kAppBytes.emplace_back(static_cast(displacement & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 8) & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 16) & 0xFF)); - kAppBytes.emplace_back(static_cast((displacement >> 24) & 0xFF)); - } else if (isRbp) { - // RBP with no displacement needs [rbp+0] - kAppBytes.emplace_back(0x00); - } - - break; - } - - /// Push instruction handler. - if (name == "push" || name == "pop") { - std::string substr = line.substr(line.find(name) + name.size()); - - // Remove leading whitespace - while (!substr.empty() && (substr[0] == ' ' || substr[0] == '\t')) { - substr.erase(0, 1); - } - - i64_byte_t baseOpcode = (name == "push") ? kAsmPushOpcode : kAsmPopOpcode; - bool found = false; - - // Check for extended registers r8-r15 - if (substr.size() >= 2 && substr[0] == 'r' && isdigit(substr[1])) { - int regNum = 0; - - if (substr.size() >= 3 && isdigit(substr[2])) { - regNum = (substr[1] - '0') * 10 + (substr[2] - '0'); - } else { - regNum = substr[1] - '0'; - } - - if (regNum >= 8 && regNum <= 15) { - // REX.B prefix for r8-r15 - kAppBytes.emplace_back(0x41); - kAppBytes.emplace_back(baseOpcode + (regNum - 8)); - found = true; - } - } - - // Check for standard 64-bit registers rax-rdi - if (!found) { - struct RegPushPop { - const char* name; - i64_byte_t offset; - }; - - RegPushPop regs[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, - {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; - - for (auto& reg : regs) { - if (substr.find(reg.name) != std::string::npos) { - kAppBytes.emplace_back(baseOpcode + reg.offset); - found = true; - break; - } - } - } - - if (!found) { - if (isnumber(substr[0])) { - kAppBytes.emplace_back(name == "push" ? 0x68 : 0x8F); - - // push imm always takes a 32-bit immediate (sign-extended in 64-bit mode) - // Parse the immediate value without adding kOrigin - long imm = 0; - if (substr.size() > 2 && substr[0] == '0' && substr[1] == 'x') { - imm = strtol(substr.c_str() + 2, nullptr, 16); - } else if (substr.size() > 2 && substr[0] == '0' && substr[1] == 'b') { - imm = strtol(substr.c_str() + 2, nullptr, 2); - } else if (substr.size() > 2 && substr[0] == '0' && substr[1] == 'o') { - imm = strtol(substr.c_str() + 2, nullptr, 8); - } else { - imm = strtol(substr.c_str(), nullptr, 10); - } - - CompilerKit::NumberCast32 num(imm); - if (kRegisterBitWidth == 64 || kRegisterBitWidth == 32) { - kAppBytes.emplace_back(num.number[0]); - kAppBytes.emplace_back(num.number[1]); - kAppBytes.emplace_back(num.number[2]); - kAppBytes.emplace_back(num.number[3]); - } else if (kRegisterBitWidth == 16) { - kAppBytes.emplace_back(num.number[0]); - kAppBytes.emplace_back(num.number[1]); - } - - break; - } - - CompilerKit::Detail::print_error("Invalid operand for " + name + ": " + substr, - "CompilerKit"); - throw std::runtime_error("invalid_push_pop_operand"); - } - - break; - } - } - - if (name == "int" || name == "into" || name == "intd") { - kAppBytes.emplace_back(opcodeAMD64.fOpcode); - this->WriteNumber8(line.find(name) + name.size() + 1, line); - - break; - } else if (name == "jmp" || name == "call") { - kAppBytes.emplace_back(opcodeAMD64.fOpcode); - - if (auto it = std::find(kUndefinedSymbols.begin(), kUndefinedSymbols.end(), name); - it != kUndefinedSymbols.end()) { - auto number_str = std::to_string(kOrigin + name.size()); - this->WriteNumber(0, number_str); - kOrigin += name.size(); - } - - if (kRegisterBitWidth == 64) { - this->WriteNumber(line.find(name) + name.size() + 1, line); - } else { - this->WriteNumber32(line.find(name) + name.size() + 1, line); - } - - break; - } - - if (name == "syscall") { - kAppBytes.emplace_back(opcodeAMD64.fOpcode); - kAppBytes.emplace_back(0x05); - break; - } else { - kAppBytes.emplace_back(opcodeAMD64.fOpcode); - - break; - } - } - } - - if (line[0] == kAssemblerPragmaSym) { - if (foundInstruction) { - CompilerKit::Detail::print_error("Syntax error: " + line, file); - throw std::runtime_error("syntax_err"); - } - - if (line.find("bits 64") != std::string::npos) { - kRegisterBitWidth = 64U; - return true; - } else if (line.find("bits 32") != std::string::npos) { - kRegisterBitWidth = 32U; - return true; - } else if (line.find("bits 16") != std::string::npos) { - kRegisterBitWidth = 16U; - return true; - } - - if (auto org_pos = line.find("org"); org_pos != std::string::npos) { - auto value_pos = org_pos + strlen("org") + 1; - - if (value_pos >= line.size()) { - CompilerKit::Detail::print_error("Invalid org directive", "CompilerKit"); - throw std::runtime_error("invalid_org"); - } - - size_t base[] = {10, 16, 2, 8}; - - for (size_t i = 0; i < 4; i++) { - if (kOrigin = strtol(line.substr(value_pos).c_str(), nullptr, base[i]); kOrigin) { - if (errno != 0) { - continue; - } else { - if (kVerbose) { - kStdOut << "AssemblerAMD64: Origin Set: " << kOrigin << std::endl; - } - - break; - } - } - } - } - } - - /// write a dword - else if (auto pos = line.find(".dword"); pos != std::string::npos) { - this->WriteNumber32(pos + strlen(".dword") + 1, line); - } - /// write a long - else if (auto pos = line.find(".long"); pos != std::string::npos) { - this->WriteNumber(pos + strlen(".long") + 1, line); - } - /// write a 16-bit number - else if (auto pos = line.find(".word"); pos != std::string::npos) { - this->WriteNumber16(pos + strlen(".word") + 1, line); - } - - kOrigin += kIPAlignement; - - return true; -} - -// Last rev 13-1-24 diff --git a/src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp b/src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp deleted file mode 100644 index 1c25338..0000000 --- a/src/CompilerKit/src/Assemblers/Assembler+ARM64.cpp +++ /dev/null @@ -1,573 +0,0 @@ -// 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 - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @file Assembler+ARM64.cpp -/// @author Amlal El Mahrouss -/// @brief 'ACORN' Assembler. - -/// REMINDER: when dealing with an undefined symbol use (string -/// size):LinkerFindSymbol:(string) so that li will look for it. - -///////////////////////////////////////////////////////////////////////////////////////// - -#ifndef __ASM_NEED_ARM64__ -#define __ASM_NEED_ARM64__ -#endif - -#include -#include -#include -#include -#include -#include -#include - -constexpr auto kArm64Alignment = 0x1U; - -static char kOutputArch = CompilerKit::kPefArchARM64; - -static std::size_t kCounter = 1UL; - -static std::uintptr_t kOrigin = kPefBaseOrigin; -static std::vector> kOriginLabel; - -static std::vector kBytes; - -static CompilerKit::AERecordHeader kCurrentRecord{ - .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; - -static std::vector kRecords; -static std::vector kUndefinedSymbols; - -static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; -static const std::string kRelocSymbol = ":RuntimeSymbol:"; - -// \brief forward decl. -static bool asm_read_attributes(std::string line); - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @brief POWER assembler entrypoint, the program/module starts here. - -///////////////////////////////////////////////////////////////////////////////////////// - -NECTAR_MODULE(AssemblerMainARM64) { - CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); - - for (size_t i = 1; i < argc; ++i) { - if (argv[i][0] == '-') { - if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "-v") == 0) { - kStdOut << "ACORN: AARCH64 Assembler Driver.\nACORN: " << kDistVersion - << "\nACORN: " - "Copyright (c) " - "Amlal El Mahrouss\n"; - return 0; - } else if (strcmp(argv[i], "-help") == 0) { - kStdOut << "ACORN: AARCH64 Assembler Driver.\nACORN: Copyright (c) 2024 " - "Amlal El Mahrouss\n"; - kStdOut << "-version,--v: print program version.\n"; - kStdOut << "-fverbose: print verbose output.\n"; - kStdOut << "-fbinary: output as flat binary.\n"; - - return 0; - } else if (strcmp(argv[i], "-fbinary") == 0) { - kOutputAsBinary = true; - continue; - } else if (strcmp(argv[i], "-fverbose") == 0) { - kVerbose = true; - continue; - } - - kStdOut << "ACORN: ignore " << argv[i] << "\n"; - continue; - } - - if (!std::filesystem::exists(argv[i])) { - kStdOut << "ACORN: can't open: " << argv[i] << std::endl; - goto asm_fail_exit; - } - - std::string object_output(argv[i]); - - for (auto& ext : kAsmFileExts) { - if (object_output.find(ext) != std::string::npos) { - object_output.erase(object_output.find(ext), std::strlen(ext)); - } - } - - object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; - - std::ifstream file_ptr(argv[i]); - std::ofstream file_ptr_out(object_output, std::ofstream::binary); - - if (file_ptr_out.bad()) { - if (kVerbose) { - kStdOut << "ACORN: error: " << strerror(errno) << "\n"; - } - } - - std::string line; - - CompilerKit::AEHeader hdr{0}; - - memset(hdr.fPad, kAENullType, kAEPad); - - hdr.fMagic[0] = kAEMag0; - hdr.fMagic[1] = kAEMag1; - hdr.fMagic[2] = kAEMag2; - hdr.fSize = sizeof(CompilerKit::AEHeader); - hdr.fArch = kOutputArch; - - ///////////////////////////////////////////////////////////////////////////////////////// - - // COMPILATION LOOP - - ///////////////////////////////////////////////////////////////////////////////////////// - - CompilerKit::EncoderARM64 asm64; - - while (std::getline(file_ptr, line)) { - if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { - CompilerKit::Detail::print_error(ln, argv[i]); - continue; - } - - try { - asm_read_attributes(line); - asm64.WriteLine(line, argv[i]); - } catch (const std::exception& e) { - if (kVerbose) { - std::string what = e.what(); - CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); - } - - std::filesystem::remove(object_output); - goto asm_fail_exit; - } - } - - if (!kOutputAsBinary) { - if (kVerbose) { - kStdOut << "AssemblerARM64: Writing object file...\n"; - } - - // this is the final step, write everything to the file. - - auto pos = file_ptr_out.tellp(); - - hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); - - file_ptr_out << hdr; - - if (kRecords.empty()) { - kStdErr << "AssemblerARM64: At least one record is needed to write an object " - "file.\nAssemblerARM64: Make one using `public_segment .code64 foo_bar`.\n"; - - std::filesystem::remove(object_output); - return 1; - } - - kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - std::size_t record_count = 0UL; - - for (auto& record_hdr : kRecords) { - record_hdr.fFlags |= CompilerKit::kKindRelocationAtRuntime; - record_hdr.fOffset = record_count; - ++record_count; - - file_ptr_out << record_hdr; - - if (kVerbose) kStdOut << "AssemblerARM64: Wrote record " << record_hdr.fName << "...\n"; - } - - // increment once again, so that we won't lie about the kUndefinedSymbols. - ++record_count; - - for (auto& sym : kUndefinedSymbols) { - CompilerKit::AERecordHeader undefined_sym{0}; - - if (kVerbose) kStdOut << "AssemblerARM64: Wrote symbol " << sym << " to file...\n"; - - undefined_sym.fKind = CompilerKit::kKindRelocationAtRuntime; - undefined_sym.fSize = sym.size(); - undefined_sym.fOffset = record_count; - - ++record_count; - - memset(undefined_sym.fPad, kAENullType, kAEPad); - memcpy(undefined_sym.fName, sym.c_str(), sym.size()); - - file_ptr_out << undefined_sym; - - ++kCounter; - } - - auto pos_end = file_ptr_out.tellp(); - - file_ptr_out.seekp(pos); - - hdr.fStartCode = pos_end; - hdr.fCodeSize = kBytes.size(); - - file_ptr_out << hdr; - - file_ptr_out.seekp(pos_end); - } else { - if (kVerbose) { - kStdOut << "AssemblerARM64: Write raw binary...\n"; - } - } - - // byte from byte, we write this. - for (auto& byte : kBytes) { - file_ptr_out.write(reinterpret_cast(&byte), sizeof(byte)); - } - - if (kVerbose) kStdOut << "AssemblerARM64: Wrote file with program in it.\n"; - - file_ptr_out.flush(); - file_ptr_out.close(); - - if (kVerbose) kStdOut << "AssemblerARM64: Exit succeeded.\n"; - - return 0; - } - -asm_fail_exit: - - if (kVerbose) kStdOut << "AssemblerARM64: Exit failed.\n"; - - return NECTAR_EXEC_ERROR; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for attributes -// returns true if any was found. - -///////////////////////////////////////////////////////////////////////////////////////// - -static bool asm_read_attributes(std::string line) { - // extern_segment is the opposite of public_segment, it signals to the li - // that we need this symbol. - if (CompilerKit::ast_find_needle(line, "extern_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid extern_segment directive in flat binary mode.", - "CompilerKit"); - throw std::runtime_error("invalid_extern_segment_bin"); - } - - auto name = line.substr(line.find("extern_segment") + strlen("extern_segment") + 1); - - if (name.size() == 0) { - CompilerKit::Detail::print_error("Invalid extern_segment", "CompilerKit"); - throw std::runtime_error("invalid_extern_segment"); - } - - std::string result = std::to_string(name.size()); - result += kUndefinedSymbol; - - // mangle this - for (char& j : name) { - if (j == ' ' || j == ',') j = '$'; - } - - result += name; - - if (name.find(kPefCode64) != std::string::npos) { - // data is treated as code. - kCurrentRecord.fKind = CompilerKit::kPefCode; - } else if (name.find(kPefData64) != std::string::npos) { - // no code will be executed from here. - kCurrentRecord.fKind = CompilerKit::kPefData; - } else if (name.find(kPefZero64) != std::string::npos) { - // this is a bss section. - kCurrentRecord.fKind = CompilerKit::kPefZero; - } - - // this is a special case for the start stub. - // we want this so that li can find it. - - if (name == kPefStart) { - kCurrentRecord.fKind = CompilerKit::kPefCode; - } - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, result.c_str(), result.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - // public_segment is a special keyword used by Assembler to tell the AE output stage to - // mark this section as a header. it currently supports .code64, .data64., - // .zero64 - else if (CompilerKit::ast_find_needle(line, "public_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid public_segment directive in flat binary mode.", - "CompilerKit"); - throw std::runtime_error("invalid_public_segment_bin"); - } - - auto name = line.substr(line.find("public_segment") + strlen("public_segment")); - - std::string name_copy = name; - - for (char& j : name) { - if (j == ' ') j = '$'; - } - - if (name.find(".code64") != std::string::npos) { - // data is treated as code. - - name_copy.erase(name_copy.find(".code64"), strlen(".code64")); - kCurrentRecord.fKind = CompilerKit::kPefCode; - } else if (name.find(".data64") != std::string::npos) { - // no code will be executed from here. - - name_copy.erase(name_copy.find(".data64"), strlen(".data64")); - kCurrentRecord.fKind = CompilerKit::kPefData; - } else if (name.find(".zero64") != std::string::npos) { - // this is a bss section. - - name_copy.erase(name_copy.find(".zero64"), strlen(".zero64")); - kCurrentRecord.fKind = CompilerKit::kPefZero; - } - - // this is a special case for the start stub. - // we want this so that li can find it. - - if (name == kPefStart) { - kCurrentRecord.fKind = CompilerKit::kPefCode; - } - - while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); - - kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); - ++kOrigin; - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, name.c_str(), name.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - - return false; -} - -// \brief algorithms and helpers. - -namespace CompilerKit::Detail::algorithm { -// \brief authorize a brief set of characters. -static inline bool is_not_alnum_space(char c) { - return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') || (c == ',') || (c == '(') || - (c == ')') || (c == '"') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || - (c == '_') || (c == ':') || (c == '@') || (c == '.')); -} - -bool is_valid_arm64(std::string str) { - return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); -} -} // namespace CompilerKit::Detail::algorithm - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for line (syntax check) - -///////////////////////////////////////////////////////////////////////////////////////// - -std::string CompilerKit::EncoderARM64::CheckLine(std::string line, std::string file) { - std::string err_str; - - if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || - CompilerKit::ast_find_needle(line, "public_segment") || line.find('#') != std::string::npos || - CompilerKit::ast_find_needle(line, ";")) { - if (line.find('#') != std::string::npos) { - line.erase(line.find('#')); - } else if (line.find(';') != std::string::npos) { - line.erase(line.find(';')); - } else { - /// does the line contains valid input? - if (!CompilerKit::Detail::algorithm::is_valid_arm64(line)) { - err_str = "Line contains non alphanumeric characters.\nhere -> "; - err_str += line; - } - } - - return err_str; - } - - if (!CompilerKit::Detail::algorithm::is_valid_arm64(line)) { - err_str = "Line contains non alphanumeric characters.\nhere -> "; - err_str += line; - - return err_str; - } - - // check for a valid instruction format. - - if (line.find(',') != std::string::npos) { - if (line.find(',') + 1 == line.size()) { - err_str += "\nInstruction lacks right register, here -> "; - err_str += line.substr(line.find(',')); - - return err_str; - } else { - bool nothing_on_right = true; - - if (line.find(',') + 1 > line.size()) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - - auto substr = line.substr(line.find(',') + 1); - - for (auto& ch : substr) { - if (ch != ' ' && ch != '\t') { - nothing_on_right = false; - } - } - - // this means we found nothing after that ',' . - if (nothing_on_right) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - } - } - - return err_str; -} - -bool CompilerKit::EncoderARM64::WriteNumber(const std::size_t& pos, std::string& jump_label) { - if (!isdigit(jump_label[pos])) return false; - - switch (jump_label[pos + 1]) { - case 'x': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_hex"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); - - for (char& i : num.number) { - kBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "AssemblerARM64: found a base 16 number here: " << jump_label.substr(pos) - << "\n"; - } - - return true; - } - case 'b': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_bin"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); - - if (kVerbose) { - kStdOut << "AssemblerARM64: found a base 2 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kBytes.push_back(i); - } - - return true; - } - case 'o': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_octal"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); - - if (kVerbose) { - kStdOut << "AssemblerARM64: found a base 8 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kBytes.push_back(i); - } - - return true; - } - default: { - break; - } - } - - /* check for errno and stuff like that */ - if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { - if (errno != 0) { - return false; - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); - - for (char& i : num.number) { - kBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "AssemblerARM64: found a base 10 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @brief Read and write an instruction to the output array. - -///////////////////////////////////////////////////////////////////////////////////////// - -bool CompilerKit::EncoderARM64::WriteLine(std::string line, std::string file) { - if (CompilerKit::ast_find_needle(line, "public_segment")) return false; - - if (!CompilerKit::Detail::algorithm::is_valid_arm64(line)) return false; - - return true; -} - -// Last rev 13-1-24 diff --git a/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cpp b/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cpp deleted file mode 100644 index 1c05a1c..0000000 --- a/src/CompilerKit/src/Assemblers/Assembler+PowerPC.cpp +++ /dev/null @@ -1,893 +0,0 @@ -// 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 - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @file Assembler+PowerPC.cc -/// @author Amlal El Mahrouss -/// @brief POWER Assembler. - -/// REMINDER: when dealing with an undefined symbol use (string -/// size):LinkerFindSymbol:(string) so that li will look for it. - -///////////////////////////////////////////////////////////////////////////////////////// - -#ifndef __ASM_NEED_PPC__ -#define __ASM_NEED_PPC__ -#endif - -#include -#include -#include -#include -#include -#include -#include - -constexpr auto kPowerIPAlignment = 0x1U; - -static char kOutputArch = CompilerKit::kPefArchPowerPC; - -static std::size_t kCounter = 1UL; - -static std::uintptr_t kOrigin = kPefBaseOrigin; -static std::vector> kOriginLabel; - -static std::vector kBytes; - -static CompilerKit::AERecordHeader kCurrentRecord{ - .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; - -static std::vector kRecords; -static std::vector kUndefinedSymbols; - -static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; -static const std::string kRelocSymbol = ":RuntimeSymbol:"; - -// \brief forward decl. -static bool asm_read_attributes(std::string line); - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @brief POWER assembler entrypoint, the program/module starts here. - -///////////////////////////////////////////////////////////////////////////////////////// - -NECTAR_MODULE(AssemblerMainPower64) { - CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); - - for (size_t i = 1; i < argc; ++i) { - if (argv[i][0] == '-') { - if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "-v") == 0) { - kStdOut << "POWER: POWER64 Assembler Driver.\nPOWER: " << kDistVersion - << "\nPOWER: " - "Copyright (c) " - "Amlal El Mahrouss\n"; - return 0; - } else if (strcmp(argv[i], "-h") == 0) { - kStdOut << "POWER: POWER64 Assembler Driver.\nPOWER: Copyright (c) 2024 " - "Amlal El Mahrouss\n"; - kStdOut << "-version,-v: print program version.\n"; - kStdOut << "-fverbose: print verbose output.\n"; - kStdOut << "-fbinary: output as flat binary.\n"; - - return 0; - } else if (strcmp(argv[i], "-fbinary") == 0) { - kOutputAsBinary = true; - continue; - } else if (strcmp(argv[i], "-fverbose") == 0) { - kVerbose = true; - continue; - } - - kStdOut << "POWER: ignore " << argv[i] << "\n"; - continue; - } - - if (!std::filesystem::exists(argv[i])) { - kStdOut << "POWER: can't open: " << argv[i] << std::endl; - goto asm_fail_exit; - } - - std::string object_output(argv[i]); - - for (auto& ext : kAsmFileExts) { - if (object_output.find(ext) != std::string::npos) { - object_output.erase(object_output.find(ext), std::strlen(ext)); - } - } - - object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; - - std::ifstream file_ptr(argv[i]); - std::ofstream file_ptr_out(object_output, std::ofstream::binary); - - if (file_ptr_out.bad()) { - if (kVerbose) { - kStdOut << "POWER: error: " << strerror(errno) << "\n"; - } - } - - std::string line; - - CompilerKit::AEHeader hdr{0}; - - memset(hdr.fPad, kAENullType, kAEPad); - - hdr.fMagic[0] = kAEMag0; - hdr.fMagic[1] = kAEMag1; - hdr.fMagic[2] = kAEMag2; - hdr.fSize = sizeof(CompilerKit::AEHeader); - hdr.fArch = kOutputArch; - - ///////////////////////////////////////////////////////////////////////////////////////// - - // COMPILATION LOOP - - ///////////////////////////////////////////////////////////////////////////////////////// - - CompilerKit::EncoderPowerPC asm64; - - while (std::getline(file_ptr, line)) { - if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { - CompilerKit::Detail::print_error(ln, argv[i]); - continue; - } - - try { - asm_read_attributes(line); - asm64.WriteLine(line, argv[i]); - } catch (const std::exception& e) { - if (kVerbose) { - std::string what = e.what(); - CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); - } - - std::filesystem::remove(object_output); - goto asm_fail_exit; - } - } - - if (!kOutputAsBinary) { - if (kVerbose) { - kStdOut << "POWER: Writing object file...\n"; - } - - // this is the final step, write everything to the file. - - auto pos = file_ptr_out.tellp(); - - hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); - - file_ptr_out << hdr; - - if (kRecords.empty()) { - kStdErr << "POWER: At least one record is needed to write an object " - "file.\nPOWER: Make one using `public_segment .code64 foo_bar`.\n"; - - std::filesystem::remove(object_output); - return 1; - } - - kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - std::size_t record_count = 0UL; - - for (auto& record_hdr : kRecords) { - record_hdr.fFlags |= CompilerKit::kKindRelocationAtRuntime; - record_hdr.fOffset = record_count; - ++record_count; - - file_ptr_out << record_hdr; - - if (kVerbose) kStdOut << "POWER: Wrote record " << record_hdr.fName << "...\n"; - } - - // increment once again, so that we won't lie about the kUndefinedSymbols. - ++record_count; - - for (auto& sym : kUndefinedSymbols) { - CompilerKit::AERecordHeader undefined_sym{0}; - - if (kVerbose) kStdOut << "POWER: Wrote symbol " << sym << " to file...\n"; - - undefined_sym.fKind = kAENullType; - undefined_sym.fSize = sym.size(); - undefined_sym.fOffset = record_count; - - ++record_count; - - memset(undefined_sym.fPad, kAENullType, kAEPad); - memcpy(undefined_sym.fName, sym.c_str(), sym.size()); - - file_ptr_out << undefined_sym; - - ++kCounter; - } - - auto pos_end = file_ptr_out.tellp(); - - file_ptr_out.seekp(pos); - - hdr.fStartCode = pos_end; - hdr.fCodeSize = kBytes.size(); - - file_ptr_out << hdr; - - file_ptr_out.seekp(pos_end); - } else { - if (kVerbose) { - kStdOut << "POWER: Write raw binary...\n"; - } - } - - // byte from byte, we write this. - for (auto& byte : kBytes) { - file_ptr_out.write(reinterpret_cast(&byte), sizeof(byte)); - } - - if (kVerbose) kStdOut << "POWER: Wrote file with program in it.\n"; - - file_ptr_out.flush(); - file_ptr_out.close(); - - if (kVerbose) kStdOut << "POWER: Exit succeeded.\n"; - - return 0; - } - -asm_fail_exit: - - if (kVerbose) kStdOut << "POWER: Exit failed.\n"; - - return NECTAR_EXEC_ERROR; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for attributes -// returns true if any was found. - -///////////////////////////////////////////////////////////////////////////////////////// - -static bool asm_read_attributes(std::string line) { - // extern_segment is the opposite of public_segment, it signals to the li - // that we need this symbol. - if (CompilerKit::ast_find_needle(line, "extern_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid extern_segment directive in flat binary mode.", - "CompilerKit"); - throw std::runtime_error("invalid_extern_segment_bin"); - } - - auto name = line.substr(line.find("extern_segment") + strlen("extern_segment") + 1); - - if (name.size() == 0) { - CompilerKit::Detail::print_error("Invalid extern_segment", "CompilerKit"); - throw std::runtime_error("invalid_extern_segment"); - } - - std::string result = std::to_string(name.size()); - result += kUndefinedSymbol; - - // mangle this - for (char& j : name) { - if (j == ' ' || j == ',') j = '$'; - } - - result += name; - - if (name.find(kPefCode64) != std::string::npos) { - // data is treated as code. - kCurrentRecord.fKind = CompilerKit::kPefCode; - } else if (name.find(kPefData64) != std::string::npos) { - // no code will be executed from here. - kCurrentRecord.fKind = CompilerKit::kPefData; - } else if (name.find(kPefZero64) != std::string::npos) { - // this is a bss section. - kCurrentRecord.fKind = CompilerKit::kPefZero; - } - - // this is a special case for the start stub. - // we want this so that li can find it. - - if (name == kPefStart) { - kCurrentRecord.fKind = CompilerKit::kPefCode; - } - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, result.c_str(), result.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - // public_segment is a special keyword used by POWER to tell the AE output stage to - // mark this section as a header. it currently supports .code64, .data64., - // .zero64 - else if (CompilerKit::ast_find_needle(line, "public_segment")) { - if (kOutputAsBinary) { - CompilerKit::Detail::print_error("Invalid public_segment directive in flat binary mode.", - "CompilerKit"); - throw std::runtime_error("invalid_public_segment_bin"); - } - - auto name = line.substr(line.find("public_segment") + strlen("public_segment")); - - std::string name_copy = name; - - for (char& j : name) { - if (j == ' ') j = '$'; - } - - kCurrentRecord.fKind = CompilerKit::kKindExportSymbol; - - if (name.find(kPefCode64) != std::string::npos) { - // data is treated as code. - kCurrentRecord.fKind |= CompilerKit::kPefCode; - } else if (name.find(kPefData64) != std::string::npos) { - // no code will be executed from here. - kCurrentRecord.fKind |= CompilerKit::kPefData; - } else if (name.find(kPefZero64) != std::string::npos) { - // this is a bss section. - kCurrentRecord.fKind |= CompilerKit::kPefZero; - } - - // this is a special case for the start stub. - // we want this so that li can find it. - - if (name == kPefStart) { - kCurrentRecord.fKind = CompilerKit::kPefCode; - } - - while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); - - kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); - ++kOrigin; - - // now we can tell the code size of the previous kCurrentRecord. - - if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); - - memset(kCurrentRecord.fName, 0, kAESymbolLen); - memcpy(kCurrentRecord.fName, name.c_str(), name.size()); - - ++kCounter; - - memset(kCurrentRecord.fPad, kAENullType, kAEPad); - - kRecords.emplace_back(kCurrentRecord); - - return true; - } - - return false; -} - -// \brief algorithms and helpers. - -namespace CompilerKit::Detail::algorithm { -// \brief authorize a brief set of characters. -static inline bool is_not_alnum_space(char c) { - return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') || (c == ',') || (c == '(') || - (c == ')') || (c == '"') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || - (c == '_') || (c == ':') || (c == '@') || (c == '.')); -} - -bool is_valid_power64(std::string str) { - return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); -} -} // namespace CompilerKit::Detail::algorithm - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Check for line (syntax check) - -///////////////////////////////////////////////////////////////////////////////////////// - -std::string CompilerKit::EncoderPowerPC::CheckLine(std::string line, std::string file) { - std::string err_str; - - if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || - CompilerKit::ast_find_needle(line, "public_segment") || line.find('#') != std::string::npos || - CompilerKit::ast_find_needle(line, ";")) { - if (line.find('#') != std::string::npos) { - line.erase(line.find('#')); - } else if (line.find(';') != std::string::npos) { - line.erase(line.find(';')); - } else { - /// does the line contains valid input? - if (!CompilerKit::Detail::algorithm::is_valid_power64(line)) { - err_str = "Line contains non alphanumeric characters.\nhere -> "; - err_str += line; - } - } - - return err_str; - } - - if (!CompilerKit::Detail::algorithm::is_valid_power64(line)) { - err_str = "Line contains non alphanumeric characters.\nhere -> "; - err_str += line; - - return err_str; - } - - // check for a valid instruction format. - - if (line.find(',') != std::string::npos) { - if (line.find(',') + 1 == line.size()) { - err_str += "\nInstruction lacks right register, here -> "; - err_str += line.substr(line.find(',')); - - return err_str; - } else { - bool nothing_on_right = true; - - if (line.find(',') + 1 > line.size()) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - - auto substr = line.substr(line.find(',') + 1); - - for (auto& ch : substr) { - if (ch != ' ' && ch != '\t') { - nothing_on_right = false; - } - } - - // this means we found nothing after that ',' . - if (nothing_on_right) { - err_str += "\nInstruction not complete, here -> "; - err_str += line; - - return err_str; - } - } - } - - // these do take an argument. - std::vector operands_inst = {"stw", "li"}; - - // these don't. - std::vector filter_inst = {"blr", "bl", "sc"}; - - for (auto& opcode_risc : kOpcodesPowerPC) { - if (CompilerKit::ast_find_needle(line, opcode_risc.name)) { - for (auto& op : operands_inst) { - // if only the instruction was found. - if (line == op) { - err_str += "\nMalformed "; - err_str += op; - err_str += " instruction, here -> "; - err_str += line; - } - } - - // if it is like that -> addr1, 0x0 - if (auto it = std::find(filter_inst.begin(), filter_inst.end(), opcode_risc.name); - it == filter_inst.cend()) { - if (CompilerKit::ast_find_needle(line, opcode_risc.name)) { - if (!isspace(line[line.find(opcode_risc.name) + strlen(opcode_risc.name)])) { - err_str += "\nMissing space between "; - err_str += opcode_risc.name; - err_str += " and operands.\nhere -> "; - err_str += line; - } - } - } - - return err_str; - } - } - - err_str += "Unrecognized instruction: " + line; - - return err_str; -} - -bool CompilerKit::EncoderPowerPC::WriteNumber(const std::size_t& pos, std::string& jump_label) { - if (!isdigit(jump_label[pos])) return false; - - switch (jump_label[pos + 1]) { - case 'x': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_hex"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); - - for (char& i : num.number) { - kBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "POWER: found a base 16 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; - } - case 'b': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_bin"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); - - if (kVerbose) { - kStdOut << "POWER: found a base 2 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kBytes.push_back(i); - } - - return true; - } - case 'o': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) { - if (errno != 0) { - CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit"); - throw std::runtime_error("invalid_octal"); - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); - - if (kVerbose) { - kStdOut << "POWER: found a base 8 number here: " << jump_label.substr(pos) << "\n"; - } - - for (char& i : num.number) { - kBytes.push_back(i); - } - - return true; - } - default: { - break; - } - } - - /* check for errno and stuff like that */ - if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { - if (errno != 0) { - return false; - } - } - - CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); - - for (char& i : num.number) { - kBytes.push_back(i); - } - - if (kVerbose) { - kStdOut << "POWER: found a base 10 number here: " << jump_label.substr(pos) << "\n"; - } - - return true; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -/// @brief Read and write an instruction to the output array. - -///////////////////////////////////////////////////////////////////////////////////////// - -bool CompilerKit::EncoderPowerPC::WriteLine(std::string line, std::string file) { - if (CompilerKit::ast_find_needle(line, "public_segment")) return false; - if (!CompilerKit::Detail::algorithm::is_valid_power64(line)) return false; - - for (auto& opcode_risc : kOpcodesPowerPC) { - // strict check here - if (CompilerKit::ast_find_needle(line, opcode_risc.name)) { - std::string name(opcode_risc.name); - std::string jump_label, cpy_jump_label; - std::vector found_registers_index; - - // check funct7 type. - switch (opcode_risc.ops->type) { - default: { - NumberCast32 num(opcode_risc.opcode); - - for (auto ch : num.number) { - kBytes.emplace_back(ch); - } - break; - } - case BADDR: - case PCREL: { - auto num = GetNumber32(line, name); - - kBytes.emplace_back(num.number[0]); - kBytes.emplace_back(num.number[1]); - kBytes.emplace_back(num.number[2]); - kBytes.emplace_back(0x48); - - break; - } - /// General purpose, float, vector operations. Everything that involve - /// registers. - case G0REG: - case FREG: - case VREG: - case GREG: { - // \brief how many registers we found. - std::size_t found_some_count = 0UL; - std::size_t register_count = 0UL; - std::string opcodeName = opcode_risc.name; - std::size_t register_sum = 0; - - NumberCast64 num(opcode_risc.opcode); - - for (size_t line_index = 0UL; line_index < line.size(); line_index++) { - if (line[line_index] == kAsmRegisterPrefix[0] && isdigit(line[line_index + 1])) { - std::string register_syntax = kAsmRegisterPrefix; - register_syntax += line[line_index + 1]; - - if (isdigit(line[line_index + 2])) register_syntax += line[line_index + 2]; - - std::string reg_str; - reg_str += line[line_index + 1]; - - if (isdigit(line[line_index + 2])) reg_str += line[line_index + 2]; - - // it ranges from r0 to r19 - // something like r190 doesn't exist in the instruction set. - if (isdigit(line[line_index + 3]) && isdigit(line[line_index + 2])) { - reg_str += line[line_index + 3]; - CompilerKit::Detail::print_error( - "invalid register index, r" + reg_str + - "\nnote: The POWER accepts registers from r0 to r32.", - file); - throw std::runtime_error("invalid_register_index"); - } - - // finally cast to a size_t - std::size_t reg_index = strtol(reg_str.c_str(), nullptr, 10); - - if (reg_index > kAsmRegisterLimit) { - CompilerKit::Detail::print_error("invalid register index, r" + reg_str, file); - throw std::runtime_error("invalid_register_index"); - } - - if (opcodeName == "li") { - char numIndex = 0; - - for (size_t i = 0; i != reg_index; i++) { - numIndex += 0x20; - } - - auto num = GetNumber32(line, reg_str); - - kBytes.push_back(num.number[0]); - kBytes.push_back(num.number[1]); - kBytes.push_back(numIndex); - kBytes.push_back(0x38); - - // check if bigger than two. - for (size_t i = 2; i < 4; i++) { - if (num.number[i] > 0) { - CompilerKit::Detail::print_warning("number overflow on li operation.", file); - break; - } - } - - break; - } - - if ((opcodeName[0] == 's' && opcodeName[1] == 't')) { - if (register_sum == 0) { - for (size_t indexReg = 0UL; indexReg < reg_index; ++indexReg) { - register_sum += 0x20; - } - } else { - register_sum += reg_index; - } - } - - if (opcodeName == "mr") { - switch (register_count) { - case 0: { - kBytes.push_back(0x78); - - char numIndex = 0x3; - - for (size_t i = 0; i != reg_index; i++) { - numIndex += 0x8; - } - - kBytes.push_back(numIndex); - - break; - } - case 1: { - char numIndex = 0x1; - - for (size_t i = 0; i != reg_index; i++) { - numIndex += 0x20; - } - - for (size_t i = 0; i != reg_index; i++) { - kBytes[kBytes.size() - 1] += 0x8; - } - - kBytes[kBytes.size() - 1] -= 0x8; - - kBytes.push_back(numIndex); - - if (reg_index >= 10 && reg_index < 20) - kBytes.push_back(0x7d); - else if (reg_index >= 20 && reg_index < 30) - kBytes.push_back(0x7e); - else if (reg_index >= 30) - kBytes.push_back(0x7f); - else - kBytes.push_back(0x7c); - - break; - } - default: - break; - } - - ++register_count; - ++found_some_count; - } - - if (opcodeName == "addi") { - if (found_some_count == 2 || found_some_count == 0) - kBytes.emplace_back(reg_index); - else if (found_some_count == 1) - kBytes.emplace_back(0x00); - - ++found_some_count; - - if (found_some_count > 3) { - CompilerKit::Detail::print_error("Too much registers. -> " + line, file); - throw std::runtime_error("too_much_regs"); - } - } - - if (opcodeName.find("cmp") != std::string::npos) { - ++found_some_count; - - if (found_some_count > 3) { - CompilerKit::Detail::print_error("Too much registers. -> " + line, file); - throw std::runtime_error("too_much_regs"); - } - } - - if (opcodeName.find("mf") != std::string::npos || - opcodeName.find("mt") != std::string::npos) { - char numIndex = 0; - - for (size_t i = 0; i != reg_index; i++) { - numIndex += 0x20; - } - - num.number[2] += numIndex; - - ++found_some_count; - - if (found_some_count > 1) { - CompilerKit::Detail::print_error("Too much registers. -> " + line, file); - throw std::runtime_error("too_much_regs"); - } - - if (kVerbose) { - kStdOut << "POWER: Found register: " << register_syntax << "\n"; - kStdOut << "POWER: Amount of registers in instruction: " << found_some_count - << "\n"; - } - - if (reg_index >= 10 && reg_index < 20) - num.number[3] = 0x7d; - else if (reg_index >= 20 && reg_index < 30) - num.number[3] = 0x7e; - else if (reg_index >= 30) - num.number[3] = 0x7f; - else - num.number[3] = 0x7c; - - for (auto ch : num.number) { - kBytes.emplace_back(ch); - } - } - - found_registers_index.push_back(reg_index); - } - } - - if (opcodeName == "addi") { - kBytes.emplace_back(0x38); - } - - if (opcodeName.find("cmp") != std::string::npos) { - char rightReg = 0x0; - - for (size_t i = 0; i != found_registers_index[1]; i++) { - rightReg += 0x08; - } - - kBytes.emplace_back(0x00); - kBytes.emplace_back(rightReg); - kBytes.emplace_back(found_registers_index[0]); - kBytes.emplace_back(0x7c); - } - - if ((opcodeName[0] == 's' && opcodeName[1] == 't')) { - size_t offset = 0UL; - - if (line.find('+') != std::string::npos) { - auto number = GetNumber32(line.substr(line.find("+")), "+"); - offset = number.raw; - } - - kBytes.push_back(offset); - kBytes.push_back(0x00); - kBytes.push_back(register_sum); - - kBytes.emplace_back(0x90); - } - - if (opcodeName == "mr") { - if (register_count == 1) { - CompilerKit::Detail::print_error("Too few registers. -> " + line, file); - throw std::runtime_error("too_few_registers"); - } - } - - // we're not in immediate addressing, reg to reg. - if (opcode_risc.ops->type != GREG) { - // remember! register to register! - if (found_some_count == 1) { - CompilerKit::Detail::print_error( - "Unrecognized register found.\ntip: each POWER register " - "starts with 'r'.\nline: " + - line, - file); - - throw std::runtime_error("not_a_register"); - } - } - - if (found_some_count < 1 && name[0] != 'l' && name[0] != 's') { - CompilerKit::Detail::print_error( - "invalid combination of opcode and registers.\nline: " + line, file); - throw std::runtime_error("invalid_comb_op_reg"); - } - - break; - } - } - - kOrigin += kPowerIPAlignment; - break; - } - } - - return true; -} - -// Last rev 13-1-24 diff --git a/src/CompilerKit/src/Backends/Assembler+32x0.cpp b/src/CompilerKit/src/Backends/Assembler+32x0.cpp new file mode 100644 index 0000000..a2eb191 --- /dev/null +++ b/src/CompilerKit/src/Backends/Assembler+32x0.cpp @@ -0,0 +1,38 @@ +// Copyright 2024-2025, 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 + +///////////////////////////////////////////////////////////////////////////////////////// + +// @file Assembler+32x0.cc +// @author Amlal El Mahrouss +// @brief 32x0 Assembler. + +// REMINDER: when dealing with an undefined symbol use (string +// size):LinkerFindSymbol:(string) so that ld will look for it. + +///////////////////////////////////////////////////////////////////////////////////////// + +#ifndef __ASM_NEED_32x0__ +#define __ASM_NEED_32x0__ +#endif + +#include +#include +#include +#include +#include + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief 32x0 Assembler entrypoint, the program/module starts here. + +///////////////////////////////////////////////////////////////////////////////////////// + +NECTAR_MODULE(NEAssemblerMain32000) { + CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); + return EXIT_SUCCESS; +} diff --git a/src/CompilerKit/src/Backends/Assembler+64x0.cpp b/src/CompilerKit/src/Backends/Assembler+64x0.cpp new file mode 100644 index 0000000..4f33246 --- /dev/null +++ b/src/CompilerKit/src/Backends/Assembler+64x0.cpp @@ -0,0 +1,869 @@ +// 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 + +/// bugs: 0 + +///////////////////////////////////////////////////////////////////////////////////////// + +// @file Assembler+64x0.cpp +// @author Amlal El Mahrouss +// @brief 64x000 Assembler. + +// REMINDER: when dealing with an undefined symbol use (string +// size):LinkerFindSymbol:(string) so that ld will look for it. + +///////////////////////////////////////////////////////////////////////////////////////// + +#ifndef __ASM_NEED_64x0__ +#define __ASM_NEED_64x0__ +#endif + +#include +#include +#include +#include +#include + +static char kOutputArch = CompilerKit::kPefArch64000; + +constexpr auto k64x0IPAlignment = 0x1U; + +static std::size_t kCounter = 1UL; + +static std::uintptr_t kOrigin = kPefBaseOrigin; +static std::vector> kOriginLabel; + +static std::vector kBytes; + +static CompilerKit::AERecordHeader kCurrentRecord{ + .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; + +static std::vector kRecords; +static std::vector kUndefinedSymbols; + +static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; +static const std::string kRelocSymbol = ":RuntimeSymbol:"; + +// \brief forward decl. +static bool asm_read_attributes(std::string line); + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief 64x0 assembler entrypoint, the program/module starts here. + +///////////////////////////////////////////////////////////////////////////////////////// + +NECTAR_MODULE(AssemblerMain64x0) { + CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); + + for (size_t i = 1; i < argc; ++i) { + if (argv[i][0] == '-') { + if (strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "--v") == 0) { + kStdOut + << "Assembler64x0: 64x0 Assembler.\nAssembler64x0: v1.10\nAssembler64x0: Copyright (c) " + "Amlal El Mahrouss\n"; + return 0; + } else if (strcmp(argv[i], "--help") == 0) { + kStdOut << "Assembler64x0: 64x0 Assembler.\nAssembler64x0: Copyright (c) 2024 Mahrouss " + "Logic.\n"; + kStdOut << "--version: Print program version.\n"; + kStdOut << "--fverbose: Print verbose output.\n"; + kStdOut << "--fbinary: Output as flat binary.\n"; + kStdOut << "--64xxx: Compile for a subset of the X64000.\n"; + + return 0; + } else if (strcmp(argv[i], "--fbinary") == 0) { + kOutputAsBinary = true; + continue; + } else if (strcmp(argv[i], "--fverbose") == 0) { + kVerbose = true; + continue; + } + + kStdOut << "Assembler64x0: ignore " << argv[i] << "\n"; + continue; + } + + if (!std::filesystem::exists(argv[i])) { + kStdOut << "Assembler64x0: can't open: " << argv[i] << std::endl; + goto asm_fail_exit; + } + + std::string object_output(argv[i]); + + for (auto& ext : kAsmFileExts) { + if (object_output.find(ext) != std::string::npos) { + object_output.erase(object_output.find(ext), std::strlen(ext)); + } + } + + object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; + + std::ifstream file_ptr(argv[i]); + std::ofstream file_ptr_out(object_output, std::ofstream::binary); + + if (file_ptr_out.bad()) { + if (kVerbose) { + kStdOut << "Assembler64x0: error: " << strerror(errno) << "\n"; + } + } + + std::string line; + + CompilerKit::AEHeader hdr{0}; + + memset(hdr.fPad, kAENullType, kAEPad); + + hdr.fMagic[0] = kAEMag0; + hdr.fMagic[1] = kAEMag1; + hdr.fMagic[2] = kAEMag2; + hdr.fSize = sizeof(CompilerKit::AEHeader); + hdr.fArch = kOutputArch; + + ///////////////////////////////////////////////////////////////////////////////////////// + + // COMPILATION LOOP + + ///////////////////////////////////////////////////////////////////////////////////////// + + CompilerKit::Encoder64x0 asm64; + + while (std::getline(file_ptr, line)) { + if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { + CompilerKit::Detail::print_error(ln, argv[i]); + continue; + } + + try { + asm_read_attributes(line); + asm64.WriteLine(line, argv[i]); + } catch (const std::exception& e) { + if (kVerbose) { + std::string what = e.what(); + CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); + } + + std::filesystem::remove(object_output); + goto asm_fail_exit; + } + } + + if (!kOutputAsBinary) { + if (kVerbose) { + kStdOut << "Assembler64x0: Writing object file...\n"; + } + + // this is the final step, write everything to the file. + + auto pos = file_ptr_out.tellp(); + + hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); + + file_ptr_out << hdr; + + if (kRecords.empty()) { + kStdErr << "Assembler64x0: At least one record is needed to write an object " + "file.\nAssembler64x0: Make one using `public_segment .code64 foo_bar`.\n"; + + std::filesystem::remove(object_output); + return 1; + } + + kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + std::size_t record_count = 0UL; + + for (auto& rec : kRecords) { + if (kVerbose) kStdOut << "Assembler64x0: Wrote record " << rec.fName << " to file...\n"; + + rec.fFlags |= CompilerKit::kKindRelocationAtRuntime; + rec.fOffset = record_count; + ++record_count; + + file_ptr_out << rec; + } + + // increment once again, so that we won't lie about the kUndefinedSymbols. + ++record_count; + + for (auto& sym : kUndefinedSymbols) { + CompilerKit::AERecordHeader _record_hdr{0}; + + if (kVerbose) kStdOut << "Assembler64x0: Wrote symbol " << sym << " to file...\n"; + + _record_hdr.fKind = kAENullType; + _record_hdr.fSize = sym.size(); + _record_hdr.fOffset = record_count; + + ++record_count; + + memset(_record_hdr.fPad, kAENullType, kAEPad); + memcpy(_record_hdr.fName, sym.c_str(), sym.size()); + + file_ptr_out << _record_hdr; + + ++kCounter; + } + + auto pos_end = file_ptr_out.tellp(); + + file_ptr_out.seekp(pos); + + hdr.fStartCode = pos_end; + hdr.fCodeSize = kBytes.size(); + + file_ptr_out << hdr; + + file_ptr_out.seekp(pos_end); + } else { + if (kVerbose) { + kStdOut << "Assembler64x0: Write raw binary...\n"; + } + } + + // byte from byte, we write this. + for (auto& byte : kBytes) { + file_ptr_out.write(reinterpret_cast(&byte), sizeof(byte)); + } + + if (kVerbose) kStdOut << "Assembler64x0: Wrote file with program in it.\n"; + + file_ptr_out.flush(); + file_ptr_out.close(); + + if (kVerbose) kStdOut << "Assembler64x0: Exit succeeded.\n"; + + return 0; + } + +asm_fail_exit: + + if (kVerbose) kStdOut << "Assembler64x0: Exit failed.\n"; + + return 1; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for attributes +// returns true if any was found. + +///////////////////////////////////////////////////////////////////////////////////////// + +static bool asm_read_attributes(std::string line) { + // extern_segment is the opposite of public_segment, it signals to the ld + // that we need this symbol. + if (CompilerKit::ast_find_needle(line, "extern_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid extern_segment directive in flat binary mode.", + "CompilerKit"); + throw std::runtime_error("invalid_extern_segment_bin"); + } + + auto name = line.substr(line.find("extern_segment") + strlen("extern_segment")); + + /// sanity check to avoid stupid linker errors. + if (name.size() == 0) { + CompilerKit::Detail::print_error("Invalid extern_segment", "Nectar"); + throw std::runtime_error("invalid_extern_segment"); + } + + std::string result = std::to_string(name.size()); + result += kUndefinedSymbol; + + // mangle this + for (char& j : name) { + if (j == ' ' || j == ',') j = '$'; + } + + result += name; + + if (name.find(kPefCode64) != std::string::npos) { + // data is treated as code. + kCurrentRecord.fKind = CompilerKit::kPefCode; + } else if (name.find(kPefData64) != std::string::npos) { + // no code will be executed from here. + kCurrentRecord.fKind = CompilerKit::kPefData; + } else if (name.find(kPefZero64) != std::string::npos) { + // this is a bss section. + kCurrentRecord.fKind = CompilerKit::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that ld can find it. + + if (name == kPefStart) { + kCurrentRecord.fKind = CompilerKit::kPefCode; + } + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, result.c_str(), result.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + // public_segment is a special keyword used by Assembler64x0 to tell the AE output stage to + // mark this section as a header. it currently supports .code64, .data64., + // .zero64 + else if (CompilerKit::ast_find_needle(line, "public_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid public_segment directive in flat binary mode.", + "CompilerKit"); + throw std::runtime_error("invalid_public_segment_bin"); + } + + auto name = line.substr(line.find("public_segment") + strlen("public_segment")); + + std::string name_copy = name; + + for (char& j : name) { + if (j == ' ') j = '$'; + } + + if (name.find(".code64") != std::string::npos) { + // data is treated as code. + + name_copy.erase(name_copy.find(".code64"), strlen(".code64")); + kCurrentRecord.fKind = CompilerKit::kPefCode; + } else if (name.find(".data64") != std::string::npos) { + // no code will be executed from here. + + name_copy.erase(name_copy.find(".data64"), strlen(".data64")); + kCurrentRecord.fKind = CompilerKit::kPefData; + } else if (name.find(".zero64") != std::string::npos) { + // this is a bss section. + + name_copy.erase(name_copy.find(".zero64"), strlen(".zero64")); + kCurrentRecord.fKind = CompilerKit::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that ld can find it. + + if (name == kPefStart) { + kCurrentRecord.fKind = CompilerKit::kPefCode; + } + + while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); + + kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); + ++kOrigin; + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, name.c_str(), name.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + + return false; +} + +// \brief algorithms and helpers. + +namespace CompilerKit::Detail::algorithm { +// \brief authorize a brief set of characters. +static inline bool is_not_alnum_space(char c) { + return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') || (c == ',') || (c == '(') || + (c == ')') || (c == '"') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || + (c == '_') || (c == ':') || (c == '@') || (c == '.')); +} + +bool is_valid_64x0(std::string str) { + return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); +} +} // namespace CompilerKit::Detail::algorithm + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for line (syntax check) + +///////////////////////////////////////////////////////////////////////////////////////// + +std::string CompilerKit::Encoder64x0::CheckLine(std::string line, std::string file) { + std::string err_str; + + if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || + CompilerKit::ast_find_needle(line, "public_segment") || line.find('#') != std::string::npos || + CompilerKit::ast_find_needle(line, ";")) { + if (line.find('#') != std::string::npos) { + line.erase(line.find('#')); + } else if (line.find(';') != std::string::npos) { + line.erase(line.find(';')); + } else { + // now check the line for validity + if (!CompilerKit::Detail::algorithm::is_valid_64x0(line)) { + err_str = "Line contains non alphanumeric characters.\nhere -> "; + err_str += line; + } + } + + return err_str; + } + + if (!CompilerKit::Detail::algorithm::is_valid_64x0(line)) { + err_str = "Line contains non alphanumeric characters.\nhere -> "; + err_str += line; + + return err_str; + } + + // check for a valid instruction format. + + if (line.find(',') != std::string::npos) { + if (line.find(',') + 1 == line.size()) { + err_str += "\nInstruction lacks right register, here -> "; + err_str += line.substr(line.find(',')); + + return err_str; + } else { + bool nothing_on_right = true; + + if (line.find(',') + 1 > line.size()) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + + auto substr = line.substr(line.find(',') + 1); + + for (auto& ch : substr) { + if (ch != ' ' && ch != '\t') { + nothing_on_right = false; + } + } + + // this means we found nothing after that ',' . + if (nothing_on_right) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + } + } + + // these do take an argument. + std::vector operands_inst = {"stw", "ldw", "lda", "sta"}; + + // these don't. + std::vector filter_inst = {"jlr", "jrl", "int"}; + + for (auto& opcode64x0 : kOpcodes64x0) { + if (line.find(opcode64x0.fName) != std::string::npos) { + if (opcode64x0.fFunct7 == kAsmNoArgs) return err_str; + + for (auto& op : operands_inst) { + // if only the instruction was found. + if (line == op) { + err_str += "\nMalformed "; + err_str += op; + err_str += " instruction, here -> "; + err_str += line; + } + } + + // if it is like that -> addr1, 0x0 + if (auto it = std::find(filter_inst.begin(), filter_inst.end(), opcode64x0.fName); + it == filter_inst.cend()) { + if (CompilerKit::ast_find_needle(line, opcode64x0.fName)) { + if (!isspace(line[line.find(opcode64x0.fName) + strlen(opcode64x0.fName)])) { + err_str += "\nMissing space between "; + err_str += opcode64x0.fName; + err_str += " and operands.\nhere -> "; + err_str += line; + } + } + } + + return err_str; + } + } + + err_str += "Unrecognized instruction: " + line; + + return err_str; +} + +bool CompilerKit::Encoder64x0::WriteNumber(const std::size_t& pos, std::string& jump_label) { + if (!isdigit(jump_label[pos])) return false; + + switch (jump_label[pos + 1]) { + case 'x': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_hex_number"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); + + for (char& i : num.number) { + kBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "Assembler64x0: found a base 16 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; + } + case 'b': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_bin"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); + + if (kVerbose) { + kStdOut << "Assembler64x0: found a base 2 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kBytes.push_back(i); + } + + return true; + } + case 'o': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_octal"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); + + if (kVerbose) { + kStdOut << "Assembler64x0: found a base 8 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kBytes.push_back(i); + } + + return true; + } + default: { + break; + } + } + + /* check for errno and stuff like that */ + if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { + if (errno != 0) { + return false; + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); + + for (char& i : num.number) { + kBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "Assembler64x0: found a base 10 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Read and write an instruction to the output array. + +///////////////////////////////////////////////////////////////////////////////////////// + +bool CompilerKit::Encoder64x0::WriteLine(std::string line, std::string file) { + if (CompilerKit::ast_find_needle(line, "public_segment ")) return true; + + for (auto& opcode64x0 : kOpcodes64x0) { + // strict check here + if (CompilerKit::ast_find_needle(line, opcode64x0.fName) && + CompilerKit::Detail::algorithm::is_valid_64x0(line)) { + std::string name(opcode64x0.fName); + std::string jump_label, cpy_jump_label; + + kBytes.emplace_back(opcode64x0.fOpcode); + kBytes.emplace_back(opcode64x0.fFunct3); + kBytes.emplace_back(opcode64x0.fFunct7); + + // check funct7 type. + switch (opcode64x0.fFunct7) { + // reg to reg means register to register transfer operation. + case kAsmRegToReg: + case kAsmImmediate: { + // \brief how many registers we found. + std::size_t found_some = 0UL; + + for (size_t line_index = 0UL; line_index < line.size(); line_index++) { + if (line[line_index] == kAsmRegisterPrefix[0] && isdigit(line[line_index + 1])) { + std::string register_syntax = kAsmRegisterPrefix; + register_syntax += line[line_index + 1]; + + if (isdigit(line[line_index + 2])) register_syntax += line[line_index + 2]; + + std::string reg_str; + reg_str += line[line_index + 1]; + + if (isdigit(line[line_index + 2])) reg_str += line[line_index + 2]; + + // it ranges from r0 to r19 + // something like r190 doesn't exist in the instruction set. + if (kOutputArch == CompilerKit::kPefArch64000) { + if (isdigit(line[line_index + 3]) && isdigit(line[line_index + 2])) { + reg_str += line[line_index + 3]; + CompilerKit::Detail::print_error( + "invalid register index, r" + reg_str + + "\nnote: The 64x0 accepts registers from r0 to r20.", + file); + throw std::runtime_error("invalid_register_index"); + } + } + + // finally cast to a size_t + std::size_t reg_index = strtol(reg_str.c_str(), nullptr, 10); + + if (reg_index > kAsmRegisterLimit) { + CompilerKit::Detail::print_error("invalid register index, r" + reg_str, file); + throw std::runtime_error("invalid_register_index"); + } + + kBytes.emplace_back(reg_index); + ++found_some; + + if (kVerbose) { + kStdOut << "Assembler64x0: Register found: " << register_syntax << "\n"; + kStdOut << "Assembler64x0: Register amount in instruction: " << found_some << "\n"; + } + } + } + + // we're not in immediate addressing, reg to reg. + if (opcode64x0.fFunct7 != kAsmImmediate) { + // remember! register to register! + if (found_some == 1) { + CompilerKit::Detail::print_error( + "Too few registers.\ntip: each Assembler64x0 register " + "starts with 'r'.\nline: " + + line, + file); + throw std::runtime_error("not_a_register"); + } + } + + if (found_some < 1 && name != "ldw" && name != "lda" && name != "stw") { + CompilerKit::Detail::print_error( + "invalid combination of opcode and registers.\nline: " + line, file); + throw std::runtime_error("invalid_comb_op_reg"); + } else if (found_some == 1 && name == "add") { + CompilerKit::Detail::print_error( + "invalid combination of opcode and registers.\nline: " + line, file); + throw std::runtime_error("invalid_comb_op_reg"); + } else if (found_some == 1 && name == "sub") { + CompilerKit::Detail::print_error( + "invalid combination of opcode and registers.\nline: " + line, file); + throw std::runtime_error("invalid_comb_op_reg"); + } + + if (found_some > 0 && name == "pop") { + CompilerKit::Detail::print_error( + "invalid combination for opcode 'pop'.\ntip: it expects " + "nothing.\nline: " + + line, + file); + throw std::runtime_error("invalid_comb_op_pop"); + } + } + default: + break; + } + + // try to fetch a number from the name + if (name == "stw" || name == "ldw" || name == "lda" || name == "sta") { + auto where_string = name; + + // if we load something, we'd need it's symbol/literal + if (name == "stw" || name == "sta" || name == "ldw" || name == "lda" || name == "sta") + where_string = ","; + + jump_label = line; + + auto found_sym = false; + + while (jump_label.find(where_string) != std::string::npos) { + jump_label = jump_label.substr(jump_label.find(where_string) + where_string.size()); + + while (jump_label.find(" ") != std::string::npos) { + jump_label.erase(jump_label.find(" "), 1); + } + + if (jump_label[0] != kAsmRegisterPrefix[0] && !isdigit(jump_label[1])) { + if (found_sym) { + CompilerKit::Detail::print_error( + "invalid combination of opcode and operands.\nhere -> " + jump_label, file); + throw std::runtime_error("invalid_comb_op_ops"); + } else { + // death trap installed. + found_sym = true; + } + } + } + + cpy_jump_label = jump_label; + + // replace any spaces with $ + if (jump_label[0] == ' ') { + while (jump_label.find(' ') != std::string::npos) { + if (isalnum(jump_label[0]) || isdigit(jump_label[0])) break; + + jump_label.erase(jump_label.find(' '), 1); + } + } + + if (!this->WriteNumber(0, jump_label)) { + // sta expects this: sta 0x000000, r0 + if (name == "sta") { + CompilerKit::Detail::print_error( + "invalid combination of opcode and operands.\nHere ->" + line, file); + throw std::runtime_error("invalid_comb_op_ops"); + } + } else { + if (name == "sta" && cpy_jump_label.find("extern_segment ") != std::string::npos) { + CompilerKit::Detail::print_error("invalid usage extern_segment on 'sta', here: " + line, + file); + throw std::runtime_error("invalid_sta_usage"); + } + } + + goto asm_write_label; + } + + // This is the case where we jump to a label, it is also used as a goto. + if (name == "lda" || name == "sta") { + asm_write_label: + if (cpy_jump_label.find('\n') != std::string::npos) + cpy_jump_label.erase(cpy_jump_label.find('\n'), 1); + + if (cpy_jump_label.find("extern_segment") != std::string::npos) { + cpy_jump_label.erase(cpy_jump_label.find("extern_segment"), strlen("extern_segment")); + + if (name == "sta") { + CompilerKit::Detail::print_error("extern_segment is not allowed on a sta operation.", + file); + throw std::runtime_error("extern_segment_sta_op"); + } else { + goto asm_end_label_cpy; + } + } + + if (name == "lda" || name == "sta") { + for (auto& label : kOriginLabel) { + if (cpy_jump_label == label.first) { + if (kVerbose) { + kStdOut << "Assembler64x0: Replace label " << cpy_jump_label + << " to address: " << label.second << std::endl; + } + + CompilerKit::NumberCast64 num(label.second); + + for (auto& num : num.number) { + kBytes.push_back(num); + } + + goto asm_end_label_cpy; + } + } + + if (cpy_jump_label[0] == '0') { + switch (cpy_jump_label[1]) { + case 'x': + case 'o': + case 'b': + if (this->WriteNumber(0, cpy_jump_label)) goto asm_end_label_cpy; + + break; + default: + break; + } + + if (isdigit(cpy_jump_label[0])) { + if (this->WriteNumber(0, cpy_jump_label)) goto asm_end_label_cpy; + + break; + } + } + } + + if (cpy_jump_label.size() < 1) { + CompilerKit::Detail::print_error("label is empty, can't jump on it.", file); + throw std::runtime_error("label_empty"); + } + + /// don't go any further if: + /// load word (ldw) or store word. (stw) + + if (name == "ldw" || name == "stw") break; + + auto mld_reloc_str = std::to_string(cpy_jump_label.size()); + mld_reloc_str += kUndefinedSymbol; + mld_reloc_str += cpy_jump_label; + + bool ignore_back_slash = false; + + for (auto& reloc_chr : mld_reloc_str) { + if (reloc_chr == '\\') { + ignore_back_slash = true; + continue; + } + + if (ignore_back_slash) { + ignore_back_slash = false; + continue; + } + + kBytes.push_back(reloc_chr); + } + + kBytes.push_back('\0'); + goto asm_end_label_cpy; + } + + asm_end_label_cpy: + kOrigin += k64x0IPAlignment; + + break; + } + } + + return true; +} + +// Last rev 13-1-24 diff --git a/src/CompilerKit/src/Backends/Assembler+AMD64.cpp b/src/CompilerKit/src/Backends/Assembler+AMD64.cpp new file mode 100644 index 0000000..f064a39 --- /dev/null +++ b/src/CompilerKit/src/Backends/Assembler+AMD64.cpp @@ -0,0 +1,1849 @@ +// SPDX-License-Identifier: Apache-2.0 +// 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 + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @file Assembler+AMD64.cpp +/// @author Amlal El Mahrouss. (amlal@nekernel.org) +/// @brief AMD64 Assembler. +/// REMINDER: when dealing with an undefined symbol use (string +/// size):LinkerFindSymbol:(string) so that ld will look for it. + +///////////////////////////////////////////////////////////////////////////////////////// + +/// BUG: 0 + +/// Feature request: 1 +/// Encode registers in mov, add, xor... + +///////////////////////////////////////////////////////////////////////////////////////// + +#ifndef __ASM_NEED_AMD64__ +#define __ASM_NEED_AMD64__ +#endif + +#define kAssemblerPragmaSymStr "%" +#define kAssemblerPragmaSym '%' + +#include +#include +#include +#include +#include + +static char kOutputArch = CompilerKit::kPefArchAMD64; + +static constexpr auto kIPAlignement = 0x1U; +static auto kCounter = 0x1UL; + +static std::uintptr_t kOrigin = kPefBaseOrigin; +static std::vector> kOriginLabel; + +/// @brief keep it simple by default. +static std::int32_t kRegisterBitWidth = 16U; + +static std::vector kAppBytes; + +static CompilerKit::AERecordHeader kCurrentRecord{ + .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; + +static std::vector kRecords; +static std::vector kDefinedSymbols; +static std::vector kUndefinedSymbols; + +static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; + +// @brief forward decl. +static bool asm_read_attributes(std::string line); + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @brief AMD64 assembler entrypoint, the program/module starts here. + +///////////////////////////////////////////////////////////////////////////////////////// + +NECTAR_MODULE(AssemblerMainAMD64) { + //////////////// CPU OPCODES BEGIN //////////////// + + CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); + + std::string opcodes_jump[kJumpLimit] = {"ja", "jae", "jb", "jbe", "jc", "je", "jg", "jge", + "jl", "jle", "jna", "jnae", "jnb", "jnbe", "jnc", "jne", + "jng", "jnge", "jnl", "jnle", "jno", "jnp", "jns", "jnz", + "jo", "jp", "jpe", "jpo", "js", "jz"}; + + for (i64_hword_t i{}; i < kJumpLimit; i++) { + CpuOpcodeAMD64 code{.fName = opcodes_jump[i], + .fOpcode = static_cast(kAsmJumpOpcode + i)}; + kOpcodesAMD64.push_back(code); + } + + CpuOpcodeAMD64 code{.fName = "jcxz", .fOpcode = 0xE3}; + kOpcodesAMD64.push_back(code); + + for (i64_hword_t i{kJumpLimitStandard}; i < kJumpLimitStandardLimit; i++) { + CpuOpcodeAMD64 code{.fName = "jmp", .fOpcode = i}; + kOpcodesAMD64.push_back(code); + } + + CpuOpcodeAMD64 lahf{.fName = "lahf", .fOpcode = 0x9F}; + kOpcodesAMD64.push_back(lahf); + + CpuOpcodeAMD64 lds{.fName = "lds", .fOpcode = 0xC5}; + kOpcodesAMD64.push_back(lds); + + CpuOpcodeAMD64 lea{.fName = "lea", .fOpcode = 0x8D}; + kOpcodesAMD64.push_back(lea); + + CpuOpcodeAMD64 nop{.fName = "nop", .fOpcode = 0x90}; + kOpcodesAMD64.push_back(nop); + + //////////////// CPU OPCODES END //////////////// + + 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"; + 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 << "-version: Print program version.\n"; + kStdOut << "-fverbose: Print verbose output.\n"; + kStdOut << "-fbinary: Output as flat binary.\n"; + + return 0; + } else if (strcmp(argv[i], "-fbinary") == 0) { + kOutputAsBinary = true; + continue; + } else if (strcmp(argv[i], "-fverbose") == 0) { + kVerbose = true; + continue; + } + + kStdOut << "AssemblerAMD64: ignore " << argv[i] << "\n"; + continue; + } + + if (!std::filesystem::exists(argv[i])) { + kStdOut << "AssemblerAMD64: can't open: " << argv[i] << std::endl; + goto asm_fail_exit; + } + + std::string object_output(argv[i]); + std::string asm_input(argv[i]); + + for (auto& ext : kAsmFileExts) { + if (object_output.ends_with(ext)) { + object_output.erase(object_output.find(ext), std::strlen(ext)); + break; + } + } + + object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; + + std::ifstream file_ptr(argv[i]); + std::ofstream file_ptr_out(object_output, std::ofstream::binary); + + kStdOut << "AssemblerAMD64: Assembling: " << argv[i] << "\n"; + + if (file_ptr_out.bad()) { + if (kVerbose) { + kStdOut << "AssemblerAMD64: error: " << strerror(errno) << "\n"; + } + + return 1; + } + + std::string line; + + CompilerKit::AEHeader hdr{0}; + + memset(hdr.fPad, kAENullType, kAEPad); + + hdr.fMagic[0] = kAEMag0; + hdr.fMagic[1] = kAEMag1; + hdr.fMagic[2] = kAEMag2; + hdr.fSize = sizeof(CompilerKit::AEHeader); + hdr.fArch = kOutputArch; + + ///////////////////////////////////////////////////////////////////////////////////////// + + // COMPILATION LOOP + + ///////////////////////////////////////////////////////////////////////////////////////// + + CompilerKit::EncoderAMD64 asm64; + + if (kVerbose) { + kStdOut << "Compiling: " + asm_input << "\n"; + } + + while (std::getline(file_ptr, line)) { + try { + if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { + CompilerKit::Detail::print_error(ln, argv[i]); + continue; + } + + asm_read_attributes(line); + asm64.WriteLine(line, argv[i]); + } catch (const std::exception& e) { + if (kVerbose) { + std::string what = e.what(); + CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); + } + + try { + std::filesystem::remove(object_output); + } catch (...) { + } + + goto asm_fail_exit; + } + } + + if (!kOutputAsBinary) { + if (kVerbose) { + kStdOut << "AssemblerAMD64: Writing object file...\n"; + } + + // this is the final step, write everything to the file. + + auto pos = file_ptr_out.tellp(); + + hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); + + file_ptr_out << hdr; + + if (kRecords.empty()) { + kStdErr << "AssemblerAMD64: At least one record is needed to write an object " + "file.\nAssemblerAMD64: Make one using `public_segment .code64 foo_bar`.\n"; + + std::filesystem::remove(object_output); + return 1; + } + + kRecords[kRecords.size() - 1].fSize = kAppBytes.size(); + + std::size_t record_count = 0UL; + + for (auto& rec : kRecords) { + if (kVerbose) kStdOut << "AssemblerAMD64: Wrote record " << rec.fName << " to file...\n"; + + rec.fFlags |= CompilerKit::kKindRelocationAtRuntime; + rec.fOffset = record_count; + ++record_count; + + file_ptr_out << rec; + } + + // increment once again, so that we won't lie about the kUndefinedSymbols. + ++record_count; + + for (auto& sym : kUndefinedSymbols) { + CompilerKit::AERecordHeader undefined_sym{0}; + + if (kVerbose) kStdOut << "AssemblerAMD64: Wrote symbol " << sym << " to file...\n"; + + undefined_sym.fKind = CompilerKit::kKindRelocationAtRuntime; + undefined_sym.fSize = sym.size(); + undefined_sym.fOffset = record_count; + + ++record_count; + + memset(undefined_sym.fPad, kAENullType, kAEPad); + memcpy(undefined_sym.fName, sym.c_str(), sym.size()); + + file_ptr_out << undefined_sym; + + ++kCounter; + } + + auto pos_end = file_ptr_out.tellp(); + + file_ptr_out.seekp(pos); + + hdr.fStartCode = pos_end; + hdr.fCodeSize = kAppBytes.size(); + + file_ptr_out << hdr; + + file_ptr_out.seekp(pos_end); + } else { + if (kVerbose) { + kStdOut << "AssemblerAMD64: Write raw binary...\n"; + } + } + + // byte from byte, we write this. + for (auto& byte : kAppBytes) { + file_ptr_out << reinterpret_cast(&byte)[0]; + } + + if (kVerbose) kStdOut << "AssemblerAMD64: Wrote file with program in it.\n"; + + file_ptr_out.flush(); + file_ptr_out.close(); + + if (kVerbose) kStdOut << "AssemblerAMD64: Exiting: Succeeded.\n"; + + return 0; + } + +asm_fail_exit: + + if (kVerbose) kStdOut << "AssemblerAMD64: Exiting: Failed.\n"; + + return 1; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for attributes +// returns true if any was found. + +///////////////////////////////////////////////////////////////////////////////////////// + +static bool asm_read_attributes(CompilerKit::STLString line) { + // extern_segment is the opposite of public_segment, it signals to the ld + // that we need this symbol. + if (CompilerKit::ast_find_needle(line, "extern_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid directive in flat binary mode.", "CompilerKit"); + throw std::runtime_error("invalid_extern_segment_bin"); + } + + auto pos = line.find("extern_segment"); + auto name_pos = pos + strlen("extern_segment") + 1; + + if (pos == std::string::npos || name_pos >= line.size()) { + CompilerKit::Detail::print_error("Invalid extern_segment", "Nectar"); + throw std::runtime_error("invalid_extern_segment"); + } + + auto name = line.substr(name_pos); + + if (name.size() == 0) { + CompilerKit::Detail::print_error("Invalid extern_segment", "Nectar"); + throw std::runtime_error("invalid_extern_segment"); + } + + kUndefinedSymbols.push_back(name); + + std::string result = std::to_string(name.size()); + result += kUndefinedSymbol; + + // mangle this + for (char& j : name) { + if (j == ' ' || j == ',') j = '$'; + } + + result += name; + + if (name.find(kPefCode64) != std::string::npos) { + // data is treated as code. + kCurrentRecord.fKind = CompilerKit::kPefCode; + } else if (name.find(kPefData64) != std::string::npos) { + // no code will be executed from here. + kCurrentRecord.fKind = CompilerKit::kPefData; + } else if (name.find(kPefZero64) != std::string::npos) { + // this will be a bss section. + kCurrentRecord.fKind = CompilerKit::kPefZero; + } + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kAppBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, result.c_str(), result.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + // public_segment is a special keyword used by AssemblerAMD64 to tell the AE output stage to + // mark this section as a header. it currently supports .code64, .data64 and + // .zero64. + else if (CompilerKit::ast_find_needle(line, "public_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid directive in flat binary mode.", "CompilerKit"); + throw std::runtime_error("invalid_public_segment_bin"); + } + + auto res_sym_at = (line.find("public_segment") + strlen("public_segment") + 1); + if (res_sym_at > line.size()) { + CompilerKit::Detail::print_error("Invalid symbol for public_segment.", "CompilerKit"); + throw std::runtime_error("invalid_public_segment_symbol"); + } + + auto name = line.substr(res_sym_at); + + std::string name_copy = name; + + for (char& j : name) { + if (j == ' ') j = '$'; + } + + if (std::find(kDefinedSymbols.begin(), kDefinedSymbols.end(), name) != kDefinedSymbols.end()) { + CompilerKit::Detail::print_error("Symbol already defined.", "CompilerKit"); + throw std::runtime_error("invalid_public_segment_bin"); + } + + kDefinedSymbols.push_back(name); + + if (name.find(kPefCode64) != std::string::npos) { + // data is treated as code. + kCurrentRecord.fKind = CompilerKit::kPefCode; + } else if (name.find(kPefData64) != std::string::npos) { + // no code will be executed from here. + kCurrentRecord.fKind = CompilerKit::kPefData; + } else if (name.find(kPefZero64) != std::string::npos) { + // this is a bss section. + kCurrentRecord.fKind = CompilerKit::kPefZero; + } + + while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); + + kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); + ++kOrigin; + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kAppBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, name.c_str(), name.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + + return false; +} + +// \brief algorithms and helpers. + +namespace CompilerKit::Detail::Algorithm { + +// \brief authorize a brief set of characters. +static inline bool is_not_valid(char c) { + if ((isalpha(c) || isdigit(c)) || + ((c == ' ') || (c == '\t') || (c == ',') || (c == '(') || (c == ')') || (c == '"') || + (c == '*') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || (c == '_') || + (c == ':') || (c == '@') || (c == '.') || (c == '#') || (c == '%') || (c == '~') || + (c == ';'))) + return false; + + return true; +} + +bool is_valid_amd64(std::string str) { + return std::find_if(str.begin(), str.end(), is_not_valid) == str.end(); +} + +} // namespace CompilerKit::Detail::Algorithm + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for line (syntax check) + +///////////////////////////////////////////////////////////////////////////////////////// + +std::string CompilerKit::EncoderAMD64::CheckLine(std::string line, std::string file) { + std::string err_str; + + if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || + CompilerKit::ast_find_needle(line, "public_segment") || + CompilerKit::ast_find_needle(line, kAssemblerPragmaSymStr) || + CompilerKit::ast_find_needle(line, ";") || line[0] == kAssemblerPragmaSym) { + if (line.find(';') != std::string::npos) { + line.erase(line.find(';')); + } else { + // now check the line for validity + if (!CompilerKit::Detail::Algorithm::is_valid_amd64(line)) { + err_str = "Line contains non valid characters.\nhere -> "; + err_str += line; + } + } + + return err_str; + } + + // check for a valid instruction format. + + if (line.find(',') != std::string::npos) { + if (line.find(',') + 1 == line.size()) { + err_str += "\nInstruction lacks right register, here -> "; + err_str += line.substr(line.find(',')); + + return err_str; + } else { + bool nothing_on_right = true; + + if (line.find(',') + 1 > line.size()) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + + auto substr = line.substr(line.find(',') + 1); + + for (auto& ch : substr) { + if (ch != ' ' && ch != '\t') { + nothing_on_right = false; + } + } + + // this means we found nothing after that ',' . + if (nothing_on_right) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + } + } + for (auto& opcodeAMD64 : kOpcodesAMD64) { + if (CompilerKit::ast_find_needle(line, opcodeAMD64.fName)) { + return err_str; + } + } + + err_str += "\nUnrecognized instruction -> " + line; + + return err_str; +} + +/// @brief Read and write a number to the output array. +bool CompilerKit::EncoderAMD64::WriteNumber(const std::size_t& pos, std::string& jump_label) { + if (!isdigit(jump_label[pos])) return false; + + switch (jump_label[pos + 1]) { + case 'x': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) + << "\n"; + } + + return true; + } + case 'b': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + return true; + } + case 'o': + case '0': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + return true; + } + default: { + break; + } + } + + /// @note We assume base 10 here. As other cases have failed. + auto res = strtol(jump_label.substr(pos + 1).c_str(), nullptr, 10); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast64 num = CompilerKit::NumberCast64(res); + + for (auto& nidx : num.number) { + kAppBytes.push_back(nidx); + } + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos + 1) + << "\n"; + } + + return true; +} + +bool CompilerKit::EncoderAMD64::WriteNumber32(const std::size_t& pos, std::string& jump_label) { + if (!isdigit(jump_label[pos])) return false; + + switch (jump_label[pos + 1]) { + case 'x': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) + << "\n"; + } + + return true; + } + case 'b': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + return true; + } + case 'o': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + return true; + } + default: { + break; + } + } + + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 10); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast32 num = CompilerKit::NumberCast32(res); + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; +} + +bool CompilerKit::EncoderAMD64::WriteNumber16(const std::size_t& pos, std::string& jump_label) { + if (!isdigit(jump_label[pos])) return false; + + switch (jump_label[pos + 1]) { + case 'x': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("Invalid hex number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_hex"); + } + } + + CompilerKit::NumberCast16 num = + CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) + << "\n"; + } + + return true; + } + case 'b': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("Invalid binary number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_bin"); + } + } + + CompilerKit::NumberCast16 num = + CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + return true; + } + case 'o': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("Invalid octal number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_octal"); + } + } + + CompilerKit::NumberCast16 num = + CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8)); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + return true; + } + default: { + break; + } + } + + /* check for errno and stuff like that */ + if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { + if (errno != 0) { + return false; + } + } + + CompilerKit::NumberCast16 num = + CompilerKit::NumberCast16(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); + + for (char& i : num.number) { + kAppBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; +} + +bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string& jump_label) { + if (!isdigit(jump_label[pos])) return false; + + switch (jump_label[pos + 1]) { + case 'x': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); + + kAppBytes.push_back(num.number); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 16 number here: " << jump_label.substr(pos) + << "\n"; + } + + return true; + } + case 'b': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 2 number here: " << jump_label.substr(pos) << "\n"; + } + + kAppBytes.push_back(num.number); + + return true; + } + case 'o': { + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; + } + + kAppBytes.push_back(num.number); + + return true; + } + default: { + break; + } + } + + auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); + + if (errno != 0) { + return false; + } + + CompilerKit::NumberCast8 num = CompilerKit::NumberCast8(res); + + kAppBytes.push_back(num.number); + + if (kVerbose) { + kStdOut << "AssemblerAMD64: Found a base 10 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Read and write an instruction to the output array. + +///////////////////////////////////////////////////////////////////////////////////////// + +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; + }; + + std::vector kRegisterList{ + {.fName = "ax", .fModRM = 0x0}, {.fName = "cx", .fModRM = 1}, + {.fName = "dx", .fModRM = 0x2}, {.fName = "bx", .fModRM = 3}, + {.fName = "sp", .fModRM = 0x4}, {.fName = "bp", .fModRM = 5}, + {.fName = "si", .fModRM = 0x6}, {.fName = "di", .fModRM = 7}, + }; + + bool foundInstruction = false; + + for (auto& opcodeAMD64 : kOpcodesAMD64) { + // strict check here + if (CompilerKit::ast_find_needle(line, opcodeAMD64.fName) && + CompilerKit::Detail::Algorithm::is_valid_amd64(line)) { + foundInstruction = true; + std::string name(opcodeAMD64.fName); + + /// Move instruction handler. + if (line.find(name) != std::string::npos) { + 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; + + if (substr.find(",") == std::string::npos) { + CompilerKit::Detail::print_error("Syntax error: missing right operand.", "CompilerKit"); + throw std::runtime_error("syntax_err"); + } + + /// Handle [reg+n] or [reg-n] memory addressing for any register + if (substr.find('[') != std::string::npos) { + // Parse the memory operand + auto bracketStart = substr.find('['); + auto bracketEnd = substr.find(']'); + + if (bracketStart == std::string::npos || bracketEnd == std::string::npos) { + CompilerKit::Detail::print_error("Syntax error: malformed memory operand.", file); + throw std::runtime_error("syntax_err"); + } + + std::string memOperand = substr.substr(bracketStart + 1, bracketEnd - bracketStart - 1); + + // Register lookup table + struct RegInfo final { + const char* name{}; + i64_byte_t code{}; + }; + + RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, + {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; + + // Find base register in memory operand + i64_byte_t baseReg = 0; + bool foundBase = false; + + for (auto& reg : regs64) { + if (memOperand.find(reg.name) != std::string::npos) { + baseReg = reg.code; + foundBase = true; + break; + } + } + + if (!foundBase) { + CompilerKit::Detail::print_error("Invalid base register in memory operand.", file); + throw std::runtime_error("invalid_base_reg"); + } + + bool isRbp = (baseReg == 5); + bool isRsp = (baseReg == 4); + + // Parse displacement + int32_t displacement = 0; + bool hasDisp = false; + + auto plusPos = memOperand.find('+'); + auto minusPos = memOperand.find('-'); + + if (plusPos != std::string::npos) { + std::string dispStr = memOperand.substr(plusPos + 1); + displacement = static_cast(strtol(dispStr.c_str(), nullptr, 0)); + hasDisp = true; + } else if (minusPos != std::string::npos) { + std::string dispStr = memOperand.substr(minusPos + 1); + displacement = -static_cast(strtol(dispStr.c_str(), nullptr, 0)); + hasDisp = true; + } + + // Determine if destination is memory or register + auto commaPos = substr.find(','); + bool destIsMemory = bracketStart < commaPos; + + // Find register in the other operand + std::string otherOperand; + + if (destIsMemory) { + otherOperand = substr.substr(commaPos + 1); + } else { + otherOperand = substr.substr(0, commaPos); + } + + // Remove whitespace + while (!otherOperand.empty() && (otherOperand[0] == ' ' || otherOperand[0] == '\t')) { + otherOperand.erase(0, 1); + } + + // Check for register in other operand + i64_byte_t regCode = 0; + bool foundReg = false; + bool isImmediate = false; + int64_t immValue = 0; + + for (auto& reg : regs64) { + if (otherOperand.find(reg.name) != std::string::npos) { + regCode = reg.code; + foundReg = true; + break; + } + } + + if (!foundReg) { + // Check if it's an immediate value + std::string immStr = otherOperand; + + while (!immStr.empty() && (immStr[0] == ' ' || immStr[0] == '\t')) { + immStr.erase(0, 1); + } + if (!immStr.empty() && (isdigit(immStr[0]) || immStr[0] == '-')) { + isImmediate = true; + immValue = strtol(immStr.c_str(), nullptr, 0); + } + } + + // Determine mod field based on displacement size + // mod=00: [reg] no displacement (except rbp which requires disp8) + // mod=01: [reg+disp8] + // mod=10: [reg+disp32] + i64_byte_t mod = 0; + if (!hasDisp && displacement == 0) { + // [rbp] requires disp8 with 0, can't use mod=00 (it means RIP-relative) + mod = isRbp ? 0x01 : 0x00; + } else if (displacement >= -128 && displacement <= 127) { + mod = 0x01; // 8-bit displacement + } else { + mod = 0x02; // 32-bit displacement + } + + if (destIsMemory) { + if (foundReg) { + // mov [reg+n], reg + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0x89); // MOV r/m64, r64 + + // ModR/M: mod | reg << 3 | r/m + i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; + kAppBytes.emplace_back(modrm); + + // RSP needs SIB byte + if (isRsp) { + kAppBytes.emplace_back(0x24); // SIB: scale=0, index=4(none), base=4(rsp) + } + } else if (isImmediate) { + // mov qword [reg+n], imm32 + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0xC7); // MOV r/m64, imm32 + + // ModR/M: mod | 0 << 3 | r/m (reg field is 0 for this opcode) + i64_byte_t modrm = (mod << 6) | (0 << 3) | baseReg; + kAppBytes.emplace_back(modrm); + + // RSP needs SIB byte + if (isRsp) { + kAppBytes.emplace_back(0x24); + } + } else { + CompilerKit::Detail::print_error("Invalid source operand for mov to memory.", file); + throw std::runtime_error("invalid_operand"); + } + } else { + // mov reg, [reg+n] + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0x8B); // MOV r64, r/m64 + + // ModR/M: mod | reg << 3 | r/m + i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; + kAppBytes.emplace_back(modrm); + + // RSP needs SIB byte + if (isRsp) { + kAppBytes.emplace_back(0x24); + } + } + + // Write displacement + if (mod == 0x01) { + // 8-bit displacement + kAppBytes.emplace_back(static_cast(displacement & 0xFF)); + } else if (mod == 0x02) { + // 32-bit displacement + kAppBytes.emplace_back(static_cast(displacement & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 8) & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 16) & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 24) & 0xFF)); + } else if (isRbp) { + // rbp with mod=00 still needs disp8=0 + kAppBytes.emplace_back(0x00); + } + + // Write immediate if present + if (destIsMemory && isImmediate) { + kAppBytes.emplace_back(static_cast(immValue & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 8) & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 16) & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 24) & 0xFF)); + } + + break; + } + + bool onlyOneReg = true; + + std::vector currentRegList; + + currentRegList.reserve(3); + + for (auto reg : kRegisterList) { + std::string registerName; + + if (bits == 32) + registerName.push_back('e'); + else if (bits == 64) + registerName.push_back('r'); + else { + CompilerKit::Detail::print_error("Invalid size for register, current bit width is: " + + std::to_string(kRegisterBitWidth), + file); + throw std::runtime_error("invalid_reg_size"); + } + + registerName += reg.fName; + + while (line.find(registerName) != std::string::npos) { + line.erase(line.find(registerName), registerName.size()); + + if (bits == 16) { + if (registerName[0] == 'r') { + CompilerKit::Detail::print_error( + "Invalid size for register, current bit width is: " + + std::to_string(kRegisterBitWidth), + file); + throw std::runtime_error("invalid_reg_size"); + } + } + + currentRegList.push_back({.fName = registerName, .fModRM = reg.fModRM}); + } + } + + if (currentRegList.size() > 1) onlyOneReg = false; + + bool hasRBasedRegs = false; + + if (!onlyOneReg && currentRegList.size() == 2) { + if (currentRegList[0].fName[0] == 'r' || currentRegList[1].fName[0] == 'r') { + if (isdigit(currentRegList[0].fName[1]) && isdigit(currentRegList[1].fName[1])) { + kAppBytes.emplace_back(0x4d); + hasRBasedRegs = true; + } else if (isdigit(currentRegList[0].fName[1]) || + isdigit(currentRegList[1].fName[1])) { + kAppBytes.emplace_back(0x4c); + hasRBasedRegs = true; + } + } + } + + if (name == "mov") { + if (bits == 64 || bits == 32) { + if (!hasRBasedRegs && bits >= 32) { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + } else if (hasRBasedRegs && bits == 32) { + CompilerKit::Detail::print_error("Invalid combination of operands and registers.", + "CompilerKit"); + throw std::runtime_error("comb_op_reg"); + } + + if (!onlyOneReg) kAppBytes.emplace_back(0x89); + } else if (bits == 16) { + if (hasRBasedRegs) { + CompilerKit::Detail::print_error("Invalid combination of operands and registers.", + "CompilerKit"); + throw std::runtime_error("comb_op_reg"); + } else { + kAppBytes.emplace_back(0x66); + kAppBytes.emplace_back(0x89); + } + } + } else { + if (!hasRBasedRegs && bits >= 32) { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + } + + kAppBytes.emplace_back(0x31); + } + + if (onlyOneReg && currentRegList.size() > 0) { + auto num = GetNumber32(line, ","); + auto modrm = (0x3 << 6 | currentRegList[0].fModRM); + + kAppBytes.emplace_back(0xC7); // prefixed before placing the modrm and then the number. + kAppBytes.emplace_back(modrm); + + if (name != "xor") { + kAppBytes.emplace_back(num.number[0]); + kAppBytes.emplace_back(num.number[1]); + kAppBytes.emplace_back(num.number[2]); + kAppBytes.emplace_back(num.number[3]); + } + + 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 (bits == 16) { + if (currentRegList[0].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[1].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"); + } + } else { + if (currentRegList[0].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[1].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"); + } + } + + /// encode register using the modrm encoding. + + auto modrm = (0x3 << 6 | currentRegList[1].fModRM << 3 | currentRegList[0].fModRM); + + kAppBytes.emplace_back(modrm); + + break; + } + + /// Compare instruction handler. + if (name == "cmp") { + std::string substr = line.substr(line.find(name) + name.size()); + + if (substr.find(",") == std::string::npos) { + CompilerKit::Detail::print_error("Syntax error: missing right operand.", "CompilerKit"); + throw std::runtime_error("syntax_err"); + } + + // Register lookup table + struct RegInfo final { + CompilerKit::STLString name; + i64_byte_t code; + }; + + RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, + {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; + + /// Handle [reg+n] memory addressing + if (substr.find('[') != std::string::npos) { + auto bracketStart = substr.find('['); + auto bracketEnd = substr.find(']'); + + if (bracketEnd == std::string::npos) { + CompilerKit::Detail::print_error("Syntax error: malformed memory operand.", file); + throw std::runtime_error("syntax_err"); + } + + std::string memOperand = substr.substr(bracketStart + 1, bracketEnd - bracketStart - 1); + + // Find base register + i64_byte_t baseReg = 0; + bool foundBase = false; + + for (auto& reg : regs64) { + if (memOperand.find(reg.name) != std::string::npos) { + baseReg = reg.code; + foundBase = true; + break; + } + } + + if (!foundBase) { + CompilerKit::Detail::print_error("Invalid base register in memory operand.", file); + throw std::runtime_error("invalid_base_reg"); + } + + bool isRbp = (baseReg == 5); + bool isRsp = (baseReg == 4); + + // Parse displacement + int32_t displacement = 0; + bool hasDisp = false; + + auto plusPos = memOperand.find('+'); + auto minusPos = memOperand.find('-'); + + if (plusPos != std::string::npos) { + std::string dispStr = memOperand.substr(plusPos + 1); + displacement = static_cast(strtol(dispStr.c_str(), nullptr, 0)); + hasDisp = true; + } else if (minusPos != std::string::npos) { + std::string dispStr = memOperand.substr(minusPos + 1); + displacement = -static_cast(strtol(dispStr.c_str(), nullptr, 0)); + hasDisp = true; + } + + auto commaPos = substr.find(','); + bool destIsMemory = bracketStart < commaPos; + + std::string otherOperand; + if (destIsMemory) { + otherOperand = substr.substr(commaPos + 1); + } else { + otherOperand = substr.substr(0, commaPos); + } + + while (!otherOperand.empty() && (otherOperand[0] == ' ' || otherOperand[0] == '\t')) { + otherOperand.erase(0, 1); + } + + i64_byte_t regCode = 0; + bool foundReg = false; + bool isImmediate = false; + int64_t immValue = 0; + + for (auto& reg : regs64) { + if (otherOperand.find(reg.name) != std::string::npos) { + regCode = reg.code; + foundReg = true; + break; + } + } + + if (!foundReg) { + std::string immStr = otherOperand; + while (!immStr.empty() && (immStr[0] == ' ' || immStr[0] == '\t')) { + immStr.erase(0, 1); + } + if (!immStr.empty() && (isdigit(immStr[0]) || immStr[0] == '-')) { + isImmediate = true; + immValue = strtol(immStr.c_str(), nullptr, 0); + } + } + + // Determine mod field + i64_byte_t mod = 0; + if (!hasDisp && displacement == 0) { + mod = isRbp ? 0x01 : 0x00; + } else if (displacement >= -128 && displacement <= 127) { + mod = 0x01; + } else { + mod = 0x02; + } + + if (destIsMemory) { + if (foundReg) { + // cmp [reg+n], reg + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0x39); // CMP r/m64, r64 + + i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; + kAppBytes.emplace_back(modrm); + + if (isRsp) { + kAppBytes.emplace_back(0x24); + } + } else if (isImmediate) { + // cmp qword [reg+n], imm32 + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0x81); // CMP r/m64, imm32 + + // reg field = 7 for CMP + i64_byte_t modrm = (mod << 6) | (7 << 3) | baseReg; + kAppBytes.emplace_back(modrm); + + if (isRsp) { + kAppBytes.emplace_back(0x24); + } + } + } else { + // cmp reg, [reg+n] + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0x3B); // CMP r64, r/m64 + + i64_byte_t modrm = (mod << 6) | (regCode << 3) | baseReg; + kAppBytes.emplace_back(modrm); + + if (isRsp) { + kAppBytes.emplace_back(0x24); + } + } + + // Write displacement + if (mod == 0x01) { + kAppBytes.emplace_back(static_cast(displacement & 0xFF)); + } else if (mod == 0x02) { + kAppBytes.emplace_back(static_cast(displacement & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 8) & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 16) & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 24) & 0xFF)); + } else if (isRbp) { + kAppBytes.emplace_back(0x00); + } + + // Write immediate + if (destIsMemory && isImmediate) { + kAppBytes.emplace_back(static_cast(immValue & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 8) & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 16) & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 24) & 0xFF)); + } + + break; + } + + // Handle register-to-register and register-to-immediate + i64_byte_t reg1Code = 0; + i64_byte_t reg2Code = 0; + bool foundReg1 = false; + bool foundReg2 = false; + bool isImmediate = false; + int64_t immValue = 0; + + auto commaPos = substr.find(','); + std::string leftOperand = substr.substr(0, commaPos); + std::string rightOperand = substr.substr(commaPos + 1); + + while (!leftOperand.empty() && (leftOperand[0] == ' ' || leftOperand[0] == '\t')) { + leftOperand.erase(0, 1); + } + while (!rightOperand.empty() && (rightOperand[0] == ' ' || rightOperand[0] == '\t')) { + rightOperand.erase(0, 1); + } + + for (auto& reg : regs64) { + if (leftOperand.find(reg.name) != std::string::npos) { + reg1Code = reg.code; + foundReg1 = true; + break; + } + } + + for (auto& reg : regs64) { + if (rightOperand.find(reg.name) != std::string::npos) { + reg2Code = reg.code; + foundReg2 = true; + break; + } + } + + if (!foundReg2) { + if (!rightOperand.empty() && (isdigit(rightOperand[0]) || rightOperand[0] == '-')) { + isImmediate = true; + immValue = strtol(rightOperand.c_str(), nullptr, 0); + } + } + + if (foundReg1 && foundReg2) { + // cmp reg1, reg2 + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0x39); // CMP r/m64, r64 + + i64_byte_t modrm = (0x3 << 6) | (reg2Code << 3) | reg1Code; + kAppBytes.emplace_back(modrm); + } else if (foundReg1 && isImmediate) { + // cmp reg, imm + kAppBytes.emplace_back(0x48); // REX.W + kAppBytes.emplace_back(0x81); // CMP r/m64, imm32 + + // reg field = 7 for CMP + i64_byte_t modrm = (0x3 << 6) | (7 << 3) | reg1Code; + kAppBytes.emplace_back(modrm); + + kAppBytes.emplace_back(static_cast(immValue & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 8) & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 16) & 0xFF)); + kAppBytes.emplace_back(static_cast((immValue >> 24) & 0xFF)); + } else { + CompilerKit::Detail::print_error("Invalid operands for cmp instruction.", file); + throw std::runtime_error("invalid_cmp_operands"); + } + + break; + } + + /// LEA instruction handler. + if (name == "lea") { + std::string substr = line.substr(line.find(name) + name.size()); + + // Remove leading whitespace + while (!substr.empty() && (substr[0] == ' ' || substr[0] == '\t')) { + substr.erase(0, 1); + } + + if (substr.find(",") == std::string::npos || substr.find('[') == std::string::npos) { + CompilerKit::Detail::print_error("Syntax error: lea requires reg, [mem] format.", file); + throw std::runtime_error("syntax_err"); + } + + // Register lookup table + struct RegInfo final { + CompilerKit::STLString name; + i64_byte_t code; + }; + + RegInfo regs64[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, + {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; + + auto commaPos = substr.find(','); + std::string destOperand = substr.substr(0, commaPos); + std::string srcOperand = substr.substr(commaPos + 1); + + // Remove whitespace + while (!destOperand.empty() && (destOperand[0] == ' ' || destOperand[0] == '\t')) { + destOperand.erase(0, 1); + } + while (!srcOperand.empty() && (srcOperand[0] == ' ' || srcOperand[0] == '\t')) { + srcOperand.erase(0, 1); + } + + // Find destination register + i64_byte_t destReg = 0; + bool foundDest = false; + + for (auto& reg : regs64) { + if (destOperand.find(reg.name) != std::string::npos) { + destReg = reg.code; + foundDest = true; + break; + } + } + + if (!foundDest) { + CompilerKit::Detail::print_error("Invalid destination register for lea.", file); + throw std::runtime_error("invalid_dest_reg"); + } + + // Parse memory operand [base+disp] or [base-disp] + auto bracketStart = srcOperand.find('['); + auto bracketEnd = srcOperand.find(']'); + + if (bracketStart == std::string::npos || bracketEnd == std::string::npos) { + CompilerKit::Detail::print_error("Syntax error: malformed memory operand for lea.", + file); + throw std::runtime_error("syntax_err"); + } + + std::string memOperand = + srcOperand.substr(bracketStart + 1, bracketEnd - bracketStart - 1); + + // Find base register + i64_byte_t baseReg = 0; + bool foundBase = false; + + for (auto& reg : regs64) { + if (memOperand.find(reg.name) != std::string::npos) { + baseReg = reg.code; + foundBase = true; + break; + } + } + + if (!foundBase) { + CompilerKit::Detail::print_error("Invalid base register in memory operand for lea.", + file); + throw std::runtime_error("invalid_base_reg"); + } + + bool isRbp = (baseReg == 5); + bool isRsp = (baseReg == 4); + int32_t displacement = 0; + bool hasDisp = false; + + // Look for +/- displacement + auto plusPos = memOperand.find('+'); + auto minusPos = memOperand.find('-'); + + if (plusPos != std::string::npos) { + std::string dispStr = memOperand.substr(plusPos + 1); + displacement = static_cast(strtol(dispStr.c_str(), nullptr, 0)); + hasDisp = true; + } else if (minusPos != std::string::npos) { + std::string dispStr = memOperand.substr(minusPos + 1); + displacement = -static_cast(strtol(dispStr.c_str(), nullptr, 0)); + hasDisp = true; + } + + // Determine mod field + i64_byte_t mod = 0x00; + if (hasDisp || isRbp) { + if (displacement >= -128 && displacement <= 127) { + mod = 0x01; // 8-bit displacement + } else { + mod = 0x02; // 32-bit displacement + } + } + + // Emit REX.W prefix for 64-bit + kAppBytes.emplace_back(0x48); + + // Emit LEA opcode + kAppBytes.emplace_back(0x8D); + + // Emit ModR/M byte + i64_byte_t modrm = (mod << 6) | (destReg << 3) | baseReg; + kAppBytes.emplace_back(modrm); + + // RSP needs SIB byte + if (isRsp) { + kAppBytes.emplace_back(0x24); + } + + // Emit displacement + if (mod == 0x01) { + kAppBytes.emplace_back(static_cast(displacement & 0xFF)); + } else if (mod == 0x02) { + kAppBytes.emplace_back(static_cast(displacement & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 8) & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 16) & 0xFF)); + kAppBytes.emplace_back(static_cast((displacement >> 24) & 0xFF)); + } else if (isRbp) { + // RBP with no displacement needs [rbp+0] + kAppBytes.emplace_back(0x00); + } + + break; + } + + /// Push instruction handler. + if (name == "push" || name == "pop") { + std::string substr = line.substr(line.find(name) + name.size()); + + // Remove leading whitespace + while (!substr.empty() && (substr[0] == ' ' || substr[0] == '\t')) { + substr.erase(0, 1); + } + + i64_byte_t baseOpcode = (name == "push") ? kAsmPushOpcode : kAsmPopOpcode; + bool found = false; + + // Check for extended registers r8-r15 + if (substr.size() >= 2 && substr[0] == 'r' && isdigit(substr[1])) { + int regNum = 0; + + if (substr.size() >= 3 && isdigit(substr[2])) { + regNum = (substr[1] - '0') * 10 + (substr[2] - '0'); + } else { + regNum = substr[1] - '0'; + } + + if (regNum >= 8 && regNum <= 15) { + // REX.B prefix for r8-r15 + kAppBytes.emplace_back(0x41); + kAppBytes.emplace_back(baseOpcode + (regNum - 8)); + found = true; + } + } + + // Check for standard 64-bit registers rax-rdi + if (!found) { + struct RegPushPop { + const char* name; + i64_byte_t offset; + }; + + RegPushPop regs[] = {{"rax", 0}, {"rcx", 1}, {"rdx", 2}, {"rbx", 3}, + {"rsp", 4}, {"rbp", 5}, {"rsi", 6}, {"rdi", 7}}; + + for (auto& reg : regs) { + if (substr.find(reg.name) != std::string::npos) { + kAppBytes.emplace_back(baseOpcode + reg.offset); + found = true; + break; + } + } + } + + if (!found) { + if (isnumber(substr[0])) { + kAppBytes.emplace_back(name == "push" ? 0x68 : 0x8F); + + // push imm always takes a 32-bit immediate (sign-extended in 64-bit mode) + // Parse the immediate value without adding kOrigin + long imm = 0; + if (substr.size() > 2 && substr[0] == '0' && substr[1] == 'x') { + imm = strtol(substr.c_str() + 2, nullptr, 16); + } else if (substr.size() > 2 && substr[0] == '0' && substr[1] == 'b') { + imm = strtol(substr.c_str() + 2, nullptr, 2); + } else if (substr.size() > 2 && substr[0] == '0' && substr[1] == 'o') { + imm = strtol(substr.c_str() + 2, nullptr, 8); + } else { + imm = strtol(substr.c_str(), nullptr, 10); + } + + CompilerKit::NumberCast32 num(imm); + if (kRegisterBitWidth == 64 || kRegisterBitWidth == 32) { + kAppBytes.emplace_back(num.number[0]); + kAppBytes.emplace_back(num.number[1]); + kAppBytes.emplace_back(num.number[2]); + kAppBytes.emplace_back(num.number[3]); + } else if (kRegisterBitWidth == 16) { + kAppBytes.emplace_back(num.number[0]); + kAppBytes.emplace_back(num.number[1]); + } + + break; + } + + CompilerKit::Detail::print_error("Invalid operand for " + name + ": " + substr, + "CompilerKit"); + throw std::runtime_error("invalid_push_pop_operand"); + } + + break; + } + } + + if (name == "int" || name == "into" || name == "intd") { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + this->WriteNumber8(line.find(name) + name.size() + 1, line); + + break; + } else if (name == "jmp" || name == "call") { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + + if (auto it = std::find(kUndefinedSymbols.begin(), kUndefinedSymbols.end(), name); + it != kUndefinedSymbols.end()) { + auto number_str = std::to_string(kOrigin + name.size()); + this->WriteNumber(0, number_str); + kOrigin += name.size(); + } + + if (kRegisterBitWidth == 64) { + this->WriteNumber(line.find(name) + name.size() + 1, line); + } else { + this->WriteNumber32(line.find(name) + name.size() + 1, line); + } + + break; + } + + if (name == "syscall") { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + kAppBytes.emplace_back(0x05); + break; + } else { + kAppBytes.emplace_back(opcodeAMD64.fOpcode); + + break; + } + } + } + + if (line[0] == kAssemblerPragmaSym) { + if (foundInstruction) { + CompilerKit::Detail::print_error("Syntax error: " + line, file); + throw std::runtime_error("syntax_err"); + } + + if (line.find("bits 64") != std::string::npos) { + kRegisterBitWidth = 64U; + return true; + } else if (line.find("bits 32") != std::string::npos) { + kRegisterBitWidth = 32U; + return true; + } else if (line.find("bits 16") != std::string::npos) { + kRegisterBitWidth = 16U; + return true; + } + + if (auto org_pos = line.find("org"); org_pos != std::string::npos) { + auto value_pos = org_pos + strlen("org") + 1; + + if (value_pos >= line.size()) { + CompilerKit::Detail::print_error("Invalid org directive", "CompilerKit"); + throw std::runtime_error("invalid_org"); + } + + size_t base[] = {10, 16, 2, 8}; + + for (size_t i = 0; i < 4; i++) { + if (kOrigin = strtol(line.substr(value_pos).c_str(), nullptr, base[i]); kOrigin) { + if (errno != 0) { + continue; + } else { + if (kVerbose) { + kStdOut << "AssemblerAMD64: Origin Set: " << kOrigin << std::endl; + } + + break; + } + } + } + } + } + + /// write a dword + else if (auto pos = line.find(".dword"); pos != std::string::npos) { + this->WriteNumber32(pos + strlen(".dword") + 1, line); + } + /// write a long + else if (auto pos = line.find(".long"); pos != std::string::npos) { + this->WriteNumber(pos + strlen(".long") + 1, line); + } + /// write a 16-bit number + else if (auto pos = line.find(".word"); pos != std::string::npos) { + this->WriteNumber16(pos + strlen(".word") + 1, line); + } + + kOrigin += kIPAlignement; + + return true; +} + +// Last rev 13-1-24 diff --git a/src/CompilerKit/src/Backends/Assembler+ARM64.cpp b/src/CompilerKit/src/Backends/Assembler+ARM64.cpp new file mode 100644 index 0000000..1c25338 --- /dev/null +++ b/src/CompilerKit/src/Backends/Assembler+ARM64.cpp @@ -0,0 +1,573 @@ +// 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 + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @file Assembler+ARM64.cpp +/// @author Amlal El Mahrouss +/// @brief 'ACORN' Assembler. + +/// REMINDER: when dealing with an undefined symbol use (string +/// size):LinkerFindSymbol:(string) so that li will look for it. + +///////////////////////////////////////////////////////////////////////////////////////// + +#ifndef __ASM_NEED_ARM64__ +#define __ASM_NEED_ARM64__ +#endif + +#include +#include +#include +#include +#include +#include +#include + +constexpr auto kArm64Alignment = 0x1U; + +static char kOutputArch = CompilerKit::kPefArchARM64; + +static std::size_t kCounter = 1UL; + +static std::uintptr_t kOrigin = kPefBaseOrigin; +static std::vector> kOriginLabel; + +static std::vector kBytes; + +static CompilerKit::AERecordHeader kCurrentRecord{ + .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; + +static std::vector kRecords; +static std::vector kUndefinedSymbols; + +static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; +static const std::string kRelocSymbol = ":RuntimeSymbol:"; + +// \brief forward decl. +static bool asm_read_attributes(std::string line); + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @brief POWER assembler entrypoint, the program/module starts here. + +///////////////////////////////////////////////////////////////////////////////////////// + +NECTAR_MODULE(AssemblerMainARM64) { + CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); + + for (size_t i = 1; i < argc; ++i) { + if (argv[i][0] == '-') { + if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "-v") == 0) { + kStdOut << "ACORN: AARCH64 Assembler Driver.\nACORN: " << kDistVersion + << "\nACORN: " + "Copyright (c) " + "Amlal El Mahrouss\n"; + return 0; + } else if (strcmp(argv[i], "-help") == 0) { + kStdOut << "ACORN: AARCH64 Assembler Driver.\nACORN: Copyright (c) 2024 " + "Amlal El Mahrouss\n"; + kStdOut << "-version,--v: print program version.\n"; + kStdOut << "-fverbose: print verbose output.\n"; + kStdOut << "-fbinary: output as flat binary.\n"; + + return 0; + } else if (strcmp(argv[i], "-fbinary") == 0) { + kOutputAsBinary = true; + continue; + } else if (strcmp(argv[i], "-fverbose") == 0) { + kVerbose = true; + continue; + } + + kStdOut << "ACORN: ignore " << argv[i] << "\n"; + continue; + } + + if (!std::filesystem::exists(argv[i])) { + kStdOut << "ACORN: can't open: " << argv[i] << std::endl; + goto asm_fail_exit; + } + + std::string object_output(argv[i]); + + for (auto& ext : kAsmFileExts) { + if (object_output.find(ext) != std::string::npos) { + object_output.erase(object_output.find(ext), std::strlen(ext)); + } + } + + object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; + + std::ifstream file_ptr(argv[i]); + std::ofstream file_ptr_out(object_output, std::ofstream::binary); + + if (file_ptr_out.bad()) { + if (kVerbose) { + kStdOut << "ACORN: error: " << strerror(errno) << "\n"; + } + } + + std::string line; + + CompilerKit::AEHeader hdr{0}; + + memset(hdr.fPad, kAENullType, kAEPad); + + hdr.fMagic[0] = kAEMag0; + hdr.fMagic[1] = kAEMag1; + hdr.fMagic[2] = kAEMag2; + hdr.fSize = sizeof(CompilerKit::AEHeader); + hdr.fArch = kOutputArch; + + ///////////////////////////////////////////////////////////////////////////////////////// + + // COMPILATION LOOP + + ///////////////////////////////////////////////////////////////////////////////////////// + + CompilerKit::EncoderARM64 asm64; + + while (std::getline(file_ptr, line)) { + if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { + CompilerKit::Detail::print_error(ln, argv[i]); + continue; + } + + try { + asm_read_attributes(line); + asm64.WriteLine(line, argv[i]); + } catch (const std::exception& e) { + if (kVerbose) { + std::string what = e.what(); + CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); + } + + std::filesystem::remove(object_output); + goto asm_fail_exit; + } + } + + if (!kOutputAsBinary) { + if (kVerbose) { + kStdOut << "AssemblerARM64: Writing object file...\n"; + } + + // this is the final step, write everything to the file. + + auto pos = file_ptr_out.tellp(); + + hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); + + file_ptr_out << hdr; + + if (kRecords.empty()) { + kStdErr << "AssemblerARM64: At least one record is needed to write an object " + "file.\nAssemblerARM64: Make one using `public_segment .code64 foo_bar`.\n"; + + std::filesystem::remove(object_output); + return 1; + } + + kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + std::size_t record_count = 0UL; + + for (auto& record_hdr : kRecords) { + record_hdr.fFlags |= CompilerKit::kKindRelocationAtRuntime; + record_hdr.fOffset = record_count; + ++record_count; + + file_ptr_out << record_hdr; + + if (kVerbose) kStdOut << "AssemblerARM64: Wrote record " << record_hdr.fName << "...\n"; + } + + // increment once again, so that we won't lie about the kUndefinedSymbols. + ++record_count; + + for (auto& sym : kUndefinedSymbols) { + CompilerKit::AERecordHeader undefined_sym{0}; + + if (kVerbose) kStdOut << "AssemblerARM64: Wrote symbol " << sym << " to file...\n"; + + undefined_sym.fKind = CompilerKit::kKindRelocationAtRuntime; + undefined_sym.fSize = sym.size(); + undefined_sym.fOffset = record_count; + + ++record_count; + + memset(undefined_sym.fPad, kAENullType, kAEPad); + memcpy(undefined_sym.fName, sym.c_str(), sym.size()); + + file_ptr_out << undefined_sym; + + ++kCounter; + } + + auto pos_end = file_ptr_out.tellp(); + + file_ptr_out.seekp(pos); + + hdr.fStartCode = pos_end; + hdr.fCodeSize = kBytes.size(); + + file_ptr_out << hdr; + + file_ptr_out.seekp(pos_end); + } else { + if (kVerbose) { + kStdOut << "AssemblerARM64: Write raw binary...\n"; + } + } + + // byte from byte, we write this. + for (auto& byte : kBytes) { + file_ptr_out.write(reinterpret_cast(&byte), sizeof(byte)); + } + + if (kVerbose) kStdOut << "AssemblerARM64: Wrote file with program in it.\n"; + + file_ptr_out.flush(); + file_ptr_out.close(); + + if (kVerbose) kStdOut << "AssemblerARM64: Exit succeeded.\n"; + + return 0; + } + +asm_fail_exit: + + if (kVerbose) kStdOut << "AssemblerARM64: Exit failed.\n"; + + return NECTAR_EXEC_ERROR; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for attributes +// returns true if any was found. + +///////////////////////////////////////////////////////////////////////////////////////// + +static bool asm_read_attributes(std::string line) { + // extern_segment is the opposite of public_segment, it signals to the li + // that we need this symbol. + if (CompilerKit::ast_find_needle(line, "extern_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid extern_segment directive in flat binary mode.", + "CompilerKit"); + throw std::runtime_error("invalid_extern_segment_bin"); + } + + auto name = line.substr(line.find("extern_segment") + strlen("extern_segment") + 1); + + if (name.size() == 0) { + CompilerKit::Detail::print_error("Invalid extern_segment", "CompilerKit"); + throw std::runtime_error("invalid_extern_segment"); + } + + std::string result = std::to_string(name.size()); + result += kUndefinedSymbol; + + // mangle this + for (char& j : name) { + if (j == ' ' || j == ',') j = '$'; + } + + result += name; + + if (name.find(kPefCode64) != std::string::npos) { + // data is treated as code. + kCurrentRecord.fKind = CompilerKit::kPefCode; + } else if (name.find(kPefData64) != std::string::npos) { + // no code will be executed from here. + kCurrentRecord.fKind = CompilerKit::kPefData; + } else if (name.find(kPefZero64) != std::string::npos) { + // this is a bss section. + kCurrentRecord.fKind = CompilerKit::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that li can find it. + + if (name == kPefStart) { + kCurrentRecord.fKind = CompilerKit::kPefCode; + } + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, result.c_str(), result.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + // public_segment is a special keyword used by Assembler to tell the AE output stage to + // mark this section as a header. it currently supports .code64, .data64., + // .zero64 + else if (CompilerKit::ast_find_needle(line, "public_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid public_segment directive in flat binary mode.", + "CompilerKit"); + throw std::runtime_error("invalid_public_segment_bin"); + } + + auto name = line.substr(line.find("public_segment") + strlen("public_segment")); + + std::string name_copy = name; + + for (char& j : name) { + if (j == ' ') j = '$'; + } + + if (name.find(".code64") != std::string::npos) { + // data is treated as code. + + name_copy.erase(name_copy.find(".code64"), strlen(".code64")); + kCurrentRecord.fKind = CompilerKit::kPefCode; + } else if (name.find(".data64") != std::string::npos) { + // no code will be executed from here. + + name_copy.erase(name_copy.find(".data64"), strlen(".data64")); + kCurrentRecord.fKind = CompilerKit::kPefData; + } else if (name.find(".zero64") != std::string::npos) { + // this is a bss section. + + name_copy.erase(name_copy.find(".zero64"), strlen(".zero64")); + kCurrentRecord.fKind = CompilerKit::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that li can find it. + + if (name == kPefStart) { + kCurrentRecord.fKind = CompilerKit::kPefCode; + } + + while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); + + kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); + ++kOrigin; + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, name.c_str(), name.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + + return false; +} + +// \brief algorithms and helpers. + +namespace CompilerKit::Detail::algorithm { +// \brief authorize a brief set of characters. +static inline bool is_not_alnum_space(char c) { + return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') || (c == ',') || (c == '(') || + (c == ')') || (c == '"') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || + (c == '_') || (c == ':') || (c == '@') || (c == '.')); +} + +bool is_valid_arm64(std::string str) { + return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); +} +} // namespace CompilerKit::Detail::algorithm + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for line (syntax check) + +///////////////////////////////////////////////////////////////////////////////////////// + +std::string CompilerKit::EncoderARM64::CheckLine(std::string line, std::string file) { + std::string err_str; + + if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || + CompilerKit::ast_find_needle(line, "public_segment") || line.find('#') != std::string::npos || + CompilerKit::ast_find_needle(line, ";")) { + if (line.find('#') != std::string::npos) { + line.erase(line.find('#')); + } else if (line.find(';') != std::string::npos) { + line.erase(line.find(';')); + } else { + /// does the line contains valid input? + if (!CompilerKit::Detail::algorithm::is_valid_arm64(line)) { + err_str = "Line contains non alphanumeric characters.\nhere -> "; + err_str += line; + } + } + + return err_str; + } + + if (!CompilerKit::Detail::algorithm::is_valid_arm64(line)) { + err_str = "Line contains non alphanumeric characters.\nhere -> "; + err_str += line; + + return err_str; + } + + // check for a valid instruction format. + + if (line.find(',') != std::string::npos) { + if (line.find(',') + 1 == line.size()) { + err_str += "\nInstruction lacks right register, here -> "; + err_str += line.substr(line.find(',')); + + return err_str; + } else { + bool nothing_on_right = true; + + if (line.find(',') + 1 > line.size()) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + + auto substr = line.substr(line.find(',') + 1); + + for (auto& ch : substr) { + if (ch != ' ' && ch != '\t') { + nothing_on_right = false; + } + } + + // this means we found nothing after that ',' . + if (nothing_on_right) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + } + } + + return err_str; +} + +bool CompilerKit::EncoderARM64::WriteNumber(const std::size_t& pos, std::string& jump_label) { + if (!isdigit(jump_label[pos])) return false; + + switch (jump_label[pos + 1]) { + case 'x': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_hex"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); + + for (char& i : num.number) { + kBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "AssemblerARM64: found a base 16 number here: " << jump_label.substr(pos) + << "\n"; + } + + return true; + } + case 'b': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_bin"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); + + if (kVerbose) { + kStdOut << "AssemblerARM64: found a base 2 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kBytes.push_back(i); + } + + return true; + } + case 'o': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_octal"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); + + if (kVerbose) { + kStdOut << "AssemblerARM64: found a base 8 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kBytes.push_back(i); + } + + return true; + } + default: { + break; + } + } + + /* check for errno and stuff like that */ + if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { + if (errno != 0) { + return false; + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); + + for (char& i : num.number) { + kBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "AssemblerARM64: found a base 10 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @brief Read and write an instruction to the output array. + +///////////////////////////////////////////////////////////////////////////////////////// + +bool CompilerKit::EncoderARM64::WriteLine(std::string line, std::string file) { + if (CompilerKit::ast_find_needle(line, "public_segment")) return false; + + if (!CompilerKit::Detail::algorithm::is_valid_arm64(line)) return false; + + return true; +} + +// Last rev 13-1-24 diff --git a/src/CompilerKit/src/Backends/Assembler+PowerPC.cpp b/src/CompilerKit/src/Backends/Assembler+PowerPC.cpp new file mode 100644 index 0000000..1c05a1c --- /dev/null +++ b/src/CompilerKit/src/Backends/Assembler+PowerPC.cpp @@ -0,0 +1,893 @@ +// 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 + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @file Assembler+PowerPC.cc +/// @author Amlal El Mahrouss +/// @brief POWER Assembler. + +/// REMINDER: when dealing with an undefined symbol use (string +/// size):LinkerFindSymbol:(string) so that li will look for it. + +///////////////////////////////////////////////////////////////////////////////////////// + +#ifndef __ASM_NEED_PPC__ +#define __ASM_NEED_PPC__ +#endif + +#include +#include +#include +#include +#include +#include +#include + +constexpr auto kPowerIPAlignment = 0x1U; + +static char kOutputArch = CompilerKit::kPefArchPowerPC; + +static std::size_t kCounter = 1UL; + +static std::uintptr_t kOrigin = kPefBaseOrigin; +static std::vector> kOriginLabel; + +static std::vector kBytes; + +static CompilerKit::AERecordHeader kCurrentRecord{ + .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0}; + +static std::vector kRecords; +static std::vector kUndefinedSymbols; + +static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; +static const std::string kRelocSymbol = ":RuntimeSymbol:"; + +// \brief forward decl. +static bool asm_read_attributes(std::string line); + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @brief POWER assembler entrypoint, the program/module starts here. + +///////////////////////////////////////////////////////////////////////////////////////// + +NECTAR_MODULE(AssemblerMainPower64) { + CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); + + for (size_t i = 1; i < argc; ++i) { + if (argv[i][0] == '-') { + if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "-v") == 0) { + kStdOut << "POWER: POWER64 Assembler Driver.\nPOWER: " << kDistVersion + << "\nPOWER: " + "Copyright (c) " + "Amlal El Mahrouss\n"; + return 0; + } else if (strcmp(argv[i], "-h") == 0) { + kStdOut << "POWER: POWER64 Assembler Driver.\nPOWER: Copyright (c) 2024 " + "Amlal El Mahrouss\n"; + kStdOut << "-version,-v: print program version.\n"; + kStdOut << "-fverbose: print verbose output.\n"; + kStdOut << "-fbinary: output as flat binary.\n"; + + return 0; + } else if (strcmp(argv[i], "-fbinary") == 0) { + kOutputAsBinary = true; + continue; + } else if (strcmp(argv[i], "-fverbose") == 0) { + kVerbose = true; + continue; + } + + kStdOut << "POWER: ignore " << argv[i] << "\n"; + continue; + } + + if (!std::filesystem::exists(argv[i])) { + kStdOut << "POWER: can't open: " << argv[i] << std::endl; + goto asm_fail_exit; + } + + std::string object_output(argv[i]); + + for (auto& ext : kAsmFileExts) { + if (object_output.find(ext) != std::string::npos) { + object_output.erase(object_output.find(ext), std::strlen(ext)); + } + } + + object_output += kOutputAsBinary ? kBinaryFileExt : kObjectFileExt; + + std::ifstream file_ptr(argv[i]); + std::ofstream file_ptr_out(object_output, std::ofstream::binary); + + if (file_ptr_out.bad()) { + if (kVerbose) { + kStdOut << "POWER: error: " << strerror(errno) << "\n"; + } + } + + std::string line; + + CompilerKit::AEHeader hdr{0}; + + memset(hdr.fPad, kAENullType, kAEPad); + + hdr.fMagic[0] = kAEMag0; + hdr.fMagic[1] = kAEMag1; + hdr.fMagic[2] = kAEMag2; + hdr.fSize = sizeof(CompilerKit::AEHeader); + hdr.fArch = kOutputArch; + + ///////////////////////////////////////////////////////////////////////////////////////// + + // COMPILATION LOOP + + ///////////////////////////////////////////////////////////////////////////////////////// + + CompilerKit::EncoderPowerPC asm64; + + while (std::getline(file_ptr, line)) { + if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { + CompilerKit::Detail::print_error(ln, argv[i]); + continue; + } + + try { + asm_read_attributes(line); + asm64.WriteLine(line, argv[i]); + } catch (const std::exception& e) { + if (kVerbose) { + std::string what = e.what(); + CompilerKit::Detail::print_warning("exit because of: " + what, "CompilerKit"); + } + + std::filesystem::remove(object_output); + goto asm_fail_exit; + } + } + + if (!kOutputAsBinary) { + if (kVerbose) { + kStdOut << "POWER: Writing object file...\n"; + } + + // this is the final step, write everything to the file. + + auto pos = file_ptr_out.tellp(); + + hdr.fCount = kRecords.size() + kUndefinedSymbols.size(); + + file_ptr_out << hdr; + + if (kRecords.empty()) { + kStdErr << "POWER: At least one record is needed to write an object " + "file.\nPOWER: Make one using `public_segment .code64 foo_bar`.\n"; + + std::filesystem::remove(object_output); + return 1; + } + + kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + std::size_t record_count = 0UL; + + for (auto& record_hdr : kRecords) { + record_hdr.fFlags |= CompilerKit::kKindRelocationAtRuntime; + record_hdr.fOffset = record_count; + ++record_count; + + file_ptr_out << record_hdr; + + if (kVerbose) kStdOut << "POWER: Wrote record " << record_hdr.fName << "...\n"; + } + + // increment once again, so that we won't lie about the kUndefinedSymbols. + ++record_count; + + for (auto& sym : kUndefinedSymbols) { + CompilerKit::AERecordHeader undefined_sym{0}; + + if (kVerbose) kStdOut << "POWER: Wrote symbol " << sym << " to file...\n"; + + undefined_sym.fKind = kAENullType; + undefined_sym.fSize = sym.size(); + undefined_sym.fOffset = record_count; + + ++record_count; + + memset(undefined_sym.fPad, kAENullType, kAEPad); + memcpy(undefined_sym.fName, sym.c_str(), sym.size()); + + file_ptr_out << undefined_sym; + + ++kCounter; + } + + auto pos_end = file_ptr_out.tellp(); + + file_ptr_out.seekp(pos); + + hdr.fStartCode = pos_end; + hdr.fCodeSize = kBytes.size(); + + file_ptr_out << hdr; + + file_ptr_out.seekp(pos_end); + } else { + if (kVerbose) { + kStdOut << "POWER: Write raw binary...\n"; + } + } + + // byte from byte, we write this. + for (auto& byte : kBytes) { + file_ptr_out.write(reinterpret_cast(&byte), sizeof(byte)); + } + + if (kVerbose) kStdOut << "POWER: Wrote file with program in it.\n"; + + file_ptr_out.flush(); + file_ptr_out.close(); + + if (kVerbose) kStdOut << "POWER: Exit succeeded.\n"; + + return 0; + } + +asm_fail_exit: + + if (kVerbose) kStdOut << "POWER: Exit failed.\n"; + + return NECTAR_EXEC_ERROR; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for attributes +// returns true if any was found. + +///////////////////////////////////////////////////////////////////////////////////////// + +static bool asm_read_attributes(std::string line) { + // extern_segment is the opposite of public_segment, it signals to the li + // that we need this symbol. + if (CompilerKit::ast_find_needle(line, "extern_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid extern_segment directive in flat binary mode.", + "CompilerKit"); + throw std::runtime_error("invalid_extern_segment_bin"); + } + + auto name = line.substr(line.find("extern_segment") + strlen("extern_segment") + 1); + + if (name.size() == 0) { + CompilerKit::Detail::print_error("Invalid extern_segment", "CompilerKit"); + throw std::runtime_error("invalid_extern_segment"); + } + + std::string result = std::to_string(name.size()); + result += kUndefinedSymbol; + + // mangle this + for (char& j : name) { + if (j == ' ' || j == ',') j = '$'; + } + + result += name; + + if (name.find(kPefCode64) != std::string::npos) { + // data is treated as code. + kCurrentRecord.fKind = CompilerKit::kPefCode; + } else if (name.find(kPefData64) != std::string::npos) { + // no code will be executed from here. + kCurrentRecord.fKind = CompilerKit::kPefData; + } else if (name.find(kPefZero64) != std::string::npos) { + // this is a bss section. + kCurrentRecord.fKind = CompilerKit::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that li can find it. + + if (name == kPefStart) { + kCurrentRecord.fKind = CompilerKit::kPefCode; + } + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, result.c_str(), result.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + // public_segment is a special keyword used by POWER to tell the AE output stage to + // mark this section as a header. it currently supports .code64, .data64., + // .zero64 + else if (CompilerKit::ast_find_needle(line, "public_segment")) { + if (kOutputAsBinary) { + CompilerKit::Detail::print_error("Invalid public_segment directive in flat binary mode.", + "CompilerKit"); + throw std::runtime_error("invalid_public_segment_bin"); + } + + auto name = line.substr(line.find("public_segment") + strlen("public_segment")); + + std::string name_copy = name; + + for (char& j : name) { + if (j == ' ') j = '$'; + } + + kCurrentRecord.fKind = CompilerKit::kKindExportSymbol; + + if (name.find(kPefCode64) != std::string::npos) { + // data is treated as code. + kCurrentRecord.fKind |= CompilerKit::kPefCode; + } else if (name.find(kPefData64) != std::string::npos) { + // no code will be executed from here. + kCurrentRecord.fKind |= CompilerKit::kPefData; + } else if (name.find(kPefZero64) != std::string::npos) { + // this is a bss section. + kCurrentRecord.fKind |= CompilerKit::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that li can find it. + + if (name == kPefStart) { + kCurrentRecord.fKind = CompilerKit::kPefCode; + } + + while (name_copy.find(" ") != std::string::npos) name_copy.erase(name_copy.find(" "), 1); + + kOriginLabel.push_back(std::make_pair(name_copy, kOrigin)); + ++kOrigin; + + // now we can tell the code size of the previous kCurrentRecord. + + if (!kRecords.empty()) kRecords[kRecords.size() - 1].fSize = kBytes.size(); + + memset(kCurrentRecord.fName, 0, kAESymbolLen); + memcpy(kCurrentRecord.fName, name.c_str(), name.size()); + + ++kCounter; + + memset(kCurrentRecord.fPad, kAENullType, kAEPad); + + kRecords.emplace_back(kCurrentRecord); + + return true; + } + + return false; +} + +// \brief algorithms and helpers. + +namespace CompilerKit::Detail::algorithm { +// \brief authorize a brief set of characters. +static inline bool is_not_alnum_space(char c) { + return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') || (c == ',') || (c == '(') || + (c == ')') || (c == '"') || (c == '\'') || (c == '[') || (c == ']') || (c == '+') || + (c == '_') || (c == ':') || (c == '@') || (c == '.')); +} + +bool is_valid_power64(std::string str) { + return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); +} +} // namespace CompilerKit::Detail::algorithm + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for line (syntax check) + +///////////////////////////////////////////////////////////////////////////////////////// + +std::string CompilerKit::EncoderPowerPC::CheckLine(std::string line, std::string file) { + std::string err_str; + + if (line.empty() || CompilerKit::ast_find_needle(line, "extern_segment") || + CompilerKit::ast_find_needle(line, "public_segment") || line.find('#') != std::string::npos || + CompilerKit::ast_find_needle(line, ";")) { + if (line.find('#') != std::string::npos) { + line.erase(line.find('#')); + } else if (line.find(';') != std::string::npos) { + line.erase(line.find(';')); + } else { + /// does the line contains valid input? + if (!CompilerKit::Detail::algorithm::is_valid_power64(line)) { + err_str = "Line contains non alphanumeric characters.\nhere -> "; + err_str += line; + } + } + + return err_str; + } + + if (!CompilerKit::Detail::algorithm::is_valid_power64(line)) { + err_str = "Line contains non alphanumeric characters.\nhere -> "; + err_str += line; + + return err_str; + } + + // check for a valid instruction format. + + if (line.find(',') != std::string::npos) { + if (line.find(',') + 1 == line.size()) { + err_str += "\nInstruction lacks right register, here -> "; + err_str += line.substr(line.find(',')); + + return err_str; + } else { + bool nothing_on_right = true; + + if (line.find(',') + 1 > line.size()) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + + auto substr = line.substr(line.find(',') + 1); + + for (auto& ch : substr) { + if (ch != ' ' && ch != '\t') { + nothing_on_right = false; + } + } + + // this means we found nothing after that ',' . + if (nothing_on_right) { + err_str += "\nInstruction not complete, here -> "; + err_str += line; + + return err_str; + } + } + } + + // these do take an argument. + std::vector operands_inst = {"stw", "li"}; + + // these don't. + std::vector filter_inst = {"blr", "bl", "sc"}; + + for (auto& opcode_risc : kOpcodesPowerPC) { + if (CompilerKit::ast_find_needle(line, opcode_risc.name)) { + for (auto& op : operands_inst) { + // if only the instruction was found. + if (line == op) { + err_str += "\nMalformed "; + err_str += op; + err_str += " instruction, here -> "; + err_str += line; + } + } + + // if it is like that -> addr1, 0x0 + if (auto it = std::find(filter_inst.begin(), filter_inst.end(), opcode_risc.name); + it == filter_inst.cend()) { + if (CompilerKit::ast_find_needle(line, opcode_risc.name)) { + if (!isspace(line[line.find(opcode_risc.name) + strlen(opcode_risc.name)])) { + err_str += "\nMissing space between "; + err_str += opcode_risc.name; + err_str += " and operands.\nhere -> "; + err_str += line; + } + } + } + + return err_str; + } + } + + err_str += "Unrecognized instruction: " + line; + + return err_str; +} + +bool CompilerKit::EncoderPowerPC::WriteNumber(const std::size_t& pos, std::string& jump_label) { + if (!isdigit(jump_label[pos])) return false; + + switch (jump_label[pos + 1]) { + case 'x': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_hex"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); + + for (char& i : num.number) { + kBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "POWER: found a base 16 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; + } + case 'b': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_bin"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); + + if (kVerbose) { + kStdOut << "POWER: found a base 2 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kBytes.push_back(i); + } + + return true; + } + case 'o': { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) { + if (errno != 0) { + CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit"); + throw std::runtime_error("invalid_octal"); + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); + + if (kVerbose) { + kStdOut << "POWER: found a base 8 number here: " << jump_label.substr(pos) << "\n"; + } + + for (char& i : num.number) { + kBytes.push_back(i); + } + + return true; + } + default: { + break; + } + } + + /* check for errno and stuff like that */ + if (auto res = strtol(jump_label.substr(pos).c_str(), nullptr, 10); !res) { + if (errno != 0) { + return false; + } + } + + CompilerKit::NumberCast64 num(strtol(jump_label.substr(pos).c_str(), nullptr, 10)); + + for (char& i : num.number) { + kBytes.push_back(i); + } + + if (kVerbose) { + kStdOut << "POWER: found a base 10 number here: " << jump_label.substr(pos) << "\n"; + } + + return true; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @brief Read and write an instruction to the output array. + +///////////////////////////////////////////////////////////////////////////////////////// + +bool CompilerKit::EncoderPowerPC::WriteLine(std::string line, std::string file) { + if (CompilerKit::ast_find_needle(line, "public_segment")) return false; + if (!CompilerKit::Detail::algorithm::is_valid_power64(line)) return false; + + for (auto& opcode_risc : kOpcodesPowerPC) { + // strict check here + if (CompilerKit::ast_find_needle(line, opcode_risc.name)) { + std::string name(opcode_risc.name); + std::string jump_label, cpy_jump_label; + std::vector found_registers_index; + + // check funct7 type. + switch (opcode_risc.ops->type) { + default: { + NumberCast32 num(opcode_risc.opcode); + + for (auto ch : num.number) { + kBytes.emplace_back(ch); + } + break; + } + case BADDR: + case PCREL: { + auto num = GetNumber32(line, name); + + kBytes.emplace_back(num.number[0]); + kBytes.emplace_back(num.number[1]); + kBytes.emplace_back(num.number[2]); + kBytes.emplace_back(0x48); + + break; + } + /// General purpose, float, vector operations. Everything that involve + /// registers. + case G0REG: + case FREG: + case VREG: + case GREG: { + // \brief how many registers we found. + std::size_t found_some_count = 0UL; + std::size_t register_count = 0UL; + std::string opcodeName = opcode_risc.name; + std::size_t register_sum = 0; + + NumberCast64 num(opcode_risc.opcode); + + for (size_t line_index = 0UL; line_index < line.size(); line_index++) { + if (line[line_index] == kAsmRegisterPrefix[0] && isdigit(line[line_index + 1])) { + std::string register_syntax = kAsmRegisterPrefix; + register_syntax += line[line_index + 1]; + + if (isdigit(line[line_index + 2])) register_syntax += line[line_index + 2]; + + std::string reg_str; + reg_str += line[line_index + 1]; + + if (isdigit(line[line_index + 2])) reg_str += line[line_index + 2]; + + // it ranges from r0 to r19 + // something like r190 doesn't exist in the instruction set. + if (isdigit(line[line_index + 3]) && isdigit(line[line_index + 2])) { + reg_str += line[line_index + 3]; + CompilerKit::Detail::print_error( + "invalid register index, r" + reg_str + + "\nnote: The POWER accepts registers from r0 to r32.", + file); + throw std::runtime_error("invalid_register_index"); + } + + // finally cast to a size_t + std::size_t reg_index = strtol(reg_str.c_str(), nullptr, 10); + + if (reg_index > kAsmRegisterLimit) { + CompilerKit::Detail::print_error("invalid register index, r" + reg_str, file); + throw std::runtime_error("invalid_register_index"); + } + + if (opcodeName == "li") { + char numIndex = 0; + + for (size_t i = 0; i != reg_index; i++) { + numIndex += 0x20; + } + + auto num = GetNumber32(line, reg_str); + + kBytes.push_back(num.number[0]); + kBytes.push_back(num.number[1]); + kBytes.push_back(numIndex); + kBytes.push_back(0x38); + + // check if bigger than two. + for (size_t i = 2; i < 4; i++) { + if (num.number[i] > 0) { + CompilerKit::Detail::print_warning("number overflow on li operation.", file); + break; + } + } + + break; + } + + if ((opcodeName[0] == 's' && opcodeName[1] == 't')) { + if (register_sum == 0) { + for (size_t indexReg = 0UL; indexReg < reg_index; ++indexReg) { + register_sum += 0x20; + } + } else { + register_sum += reg_index; + } + } + + if (opcodeName == "mr") { + switch (register_count) { + case 0: { + kBytes.push_back(0x78); + + char numIndex = 0x3; + + for (size_t i = 0; i != reg_index; i++) { + numIndex += 0x8; + } + + kBytes.push_back(numIndex); + + break; + } + case 1: { + char numIndex = 0x1; + + for (size_t i = 0; i != reg_index; i++) { + numIndex += 0x20; + } + + for (size_t i = 0; i != reg_index; i++) { + kBytes[kBytes.size() - 1] += 0x8; + } + + kBytes[kBytes.size() - 1] -= 0x8; + + kBytes.push_back(numIndex); + + if (reg_index >= 10 && reg_index < 20) + kBytes.push_back(0x7d); + else if (reg_index >= 20 && reg_index < 30) + kBytes.push_back(0x7e); + else if (reg_index >= 30) + kBytes.push_back(0x7f); + else + kBytes.push_back(0x7c); + + break; + } + default: + break; + } + + ++register_count; + ++found_some_count; + } + + if (opcodeName == "addi") { + if (found_some_count == 2 || found_some_count == 0) + kBytes.emplace_back(reg_index); + else if (found_some_count == 1) + kBytes.emplace_back(0x00); + + ++found_some_count; + + if (found_some_count > 3) { + CompilerKit::Detail::print_error("Too much registers. -> " + line, file); + throw std::runtime_error("too_much_regs"); + } + } + + if (opcodeName.find("cmp") != std::string::npos) { + ++found_some_count; + + if (found_some_count > 3) { + CompilerKit::Detail::print_error("Too much registers. -> " + line, file); + throw std::runtime_error("too_much_regs"); + } + } + + if (opcodeName.find("mf") != std::string::npos || + opcodeName.find("mt") != std::string::npos) { + char numIndex = 0; + + for (size_t i = 0; i != reg_index; i++) { + numIndex += 0x20; + } + + num.number[2] += numIndex; + + ++found_some_count; + + if (found_some_count > 1) { + CompilerKit::Detail::print_error("Too much registers. -> " + line, file); + throw std::runtime_error("too_much_regs"); + } + + if (kVerbose) { + kStdOut << "POWER: Found register: " << register_syntax << "\n"; + kStdOut << "POWER: Amount of registers in instruction: " << found_some_count + << "\n"; + } + + if (reg_index >= 10 && reg_index < 20) + num.number[3] = 0x7d; + else if (reg_index >= 20 && reg_index < 30) + num.number[3] = 0x7e; + else if (reg_index >= 30) + num.number[3] = 0x7f; + else + num.number[3] = 0x7c; + + for (auto ch : num.number) { + kBytes.emplace_back(ch); + } + } + + found_registers_index.push_back(reg_index); + } + } + + if (opcodeName == "addi") { + kBytes.emplace_back(0x38); + } + + if (opcodeName.find("cmp") != std::string::npos) { + char rightReg = 0x0; + + for (size_t i = 0; i != found_registers_index[1]; i++) { + rightReg += 0x08; + } + + kBytes.emplace_back(0x00); + kBytes.emplace_back(rightReg); + kBytes.emplace_back(found_registers_index[0]); + kBytes.emplace_back(0x7c); + } + + if ((opcodeName[0] == 's' && opcodeName[1] == 't')) { + size_t offset = 0UL; + + if (line.find('+') != std::string::npos) { + auto number = GetNumber32(line.substr(line.find("+")), "+"); + offset = number.raw; + } + + kBytes.push_back(offset); + kBytes.push_back(0x00); + kBytes.push_back(register_sum); + + kBytes.emplace_back(0x90); + } + + if (opcodeName == "mr") { + if (register_count == 1) { + CompilerKit::Detail::print_error("Too few registers. -> " + line, file); + throw std::runtime_error("too_few_registers"); + } + } + + // we're not in immediate addressing, reg to reg. + if (opcode_risc.ops->type != GREG) { + // remember! register to register! + if (found_some_count == 1) { + CompilerKit::Detail::print_error( + "Unrecognized register found.\ntip: each POWER register " + "starts with 'r'.\nline: " + + line, + file); + + throw std::runtime_error("not_a_register"); + } + } + + if (found_some_count < 1 && name[0] != 'l' && name[0] != 's') { + CompilerKit::Detail::print_error( + "invalid combination of opcode and registers.\nline: " + line, file); + throw std::runtime_error("invalid_comb_op_reg"); + } + + break; + } + } + + kOrigin += kPowerIPAlignment; + break; + } + } + + return true; +} + +// Last rev 13-1-24 diff --git a/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp b/src/CompilerKit/src/CodeGenerator+AssemblyFactory.cpp index 7e9e1af..9ea310f 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; @@ -51,4 +52,5 @@ WeakRef AssemblyFactory::Unmount() noexcept { return WeakRef{mount_prev}; } + } // namespace CompilerKit diff --git a/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp index 6769355..e299b06 100644 --- a/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp +++ b/src/CompilerKit/src/Frontends/NectarCompiler+AMD64.cpp @@ -6,25 +6,13 @@ /// BUGS: 0 -/////////////////////// - -// ANSI ESCAPE CODES // - -/////////////////////// - -/////////////////////// - -// MACROS // - -/////////////////////// - #include #include #include #include #include -/* NeKernel Nectar Compiler Driver. */ +/* Nectar Compiler Driver. */ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss 2024-2026 */ diff --git a/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+Chk.cpp new file mode 100644 index 0000000..f3f4da6 --- /dev/null +++ b/src/CompilerKit/src/Frontends/NectarCompiler+Chk.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 +#include +#include +#include +#include + +/* Nectar Compiler Check 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/Frontends/NectarCompiler+PTX.cpp b/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp index 7922c37..4b7fa5f 100644 --- a/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp +++ b/src/CompilerKit/src/Frontends/NectarCompiler+PTX.cpp @@ -8,23 +8,11 @@ /// BUGS: 0 -/////////////////////// - -// ANSI ESCAPE CODES // - -/////////////////////// - -/////////////////////// - -// MACROS // - -/////////////////////// - #include #include #include -/* NeKernel Nectar Compiler Driver. */ +/* Nectar Compiler Driver. */ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss 2024-2026 */ diff --git a/src/CompilerKit/src/Generators/DynamicLinker64+MachO.cpp b/src/CompilerKit/src/Generators/DynamicLinker64+MachO.cpp new file mode 100644 index 0000000..911b582 --- /dev/null +++ b/src/CompilerKit/src/Generators/DynamicLinker64+MachO.cpp @@ -0,0 +1,637 @@ +// SPDX-License-Identifier: Apache-2.0 +// 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 + +/// @author Amlal El Mahrouss (amlal@nekernel.org) +/// @brief NeKernel.org 64-bit Mach-O Linker. +/// @version Last Rev: 2026 +/// @note Outputs Mach-O executables with __TEXT and __DATA segments. + +#ifdef CK_USE_MACHO_LINKER + +#include +#include +#include +#include +#include +#include +#include +#include + +#define kLatestOSX (15) + +#define kMachODefaultEntrypoint "_main" +#define kMachODefaultOutput {"a" kMachOExt} + +#define kLinkerVersionStr "Nectar 64-Bit Linker (OS X Mach-O)" + +#define kLinkerSplash() kStdOut << kLinkerVersionStr << kStdEndl + +#define kConsoleOut \ + (std::cout << "\e[0;31m" \ + << "mld64: " \ + << "\e[0;97m") + +static CompilerKit::STLString kOutput = kMachODefaultOutput; +static cpu_type_t kCpuType = CPU_TYPE_X86_64; +static cpu_subtype_t kCpuSubType = CPU_SUBTYPE_X86_64_ALL; +static bool kFatBinaryEnable = false; +static bool kStartFound = false; +static bool kDuplicateSymbols = false; +static bool kIsDylib = false; +static Int64 kMachODefaultStackSz = 0; + +static CompilerKit::STLString kLinkerStart = kMachODefaultEntrypoint; + +/* object code and list. */ +static std::vector kObjectList; +static std::vector kTextBytes; +static std::vector kDataBytes; + +/* @brief symbol tables */ +static std::vector kSymbolTable; +static std::vector kStringTable; +static std::map kSymbolOffsets; + +/// @brief Structure to hold section information from AE records +struct SectionInfo { + CompilerKit::STLString name; + UInt32 kind; + std::vector bytes; + UInt64 address; + UInt64 size; +}; + +using SectionInfoVec = std::vector; + +/// @brief Extract clean symbol name from AE record name +/// AE format: ".code64$symbolname" or "symbolname.code64" +static CompilerKit::STLString macho_extract_symbol_name(const CompilerKit::STLString& aeName) { + CompilerKit::STLString name = aeName; + + // Remove section prefixes/suffixes + const char* sections[] = {".code64", ".data64", ".zero64", "$"}; + + for (const auto& sec : sections) { + size_t pos; + while ((pos = name.find(sec)) != CompilerKit::STLString::npos) { + name.erase(pos, strlen(sec)); + } + } + + // Trim whitespace + while (!name.empty() && (name.front() == ' ' || name.front() == '\t')) { + name.erase(0, 1); + } + + while (!name.empty() && (name.back() == ' ' || name.back() == '\t')) { + name.pop_back(); + } + + return name; +} + +/// @brief Add a symbol to the symbol table +static UInt32 macho_add_symbol(const CompilerKit::STLString& name, uint8_t type, uint8_t sect, + UInt64 value) { + // Add name to string table (offset 0 is reserved for empty string) + if (kStringTable.empty()) { + kStringTable.push_back('\0'); // First byte is null + } + + UInt32 strOffset = static_cast(kStringTable.size()); + + for (const char& c : name) { + kStringTable.push_back(c); + } + + kStringTable.push_back('\0'); + + // Create nlist_64 entry + nlist_64 sym{}; + sym.n_un.n_strx = strOffset; + sym.n_type = type; + sym.n_sect = sect; + sym.n_desc = 0; + sym.n_value = value; + + kSymbolTable.push_back(sym); + kSymbolOffsets[name] = value; + + return static_cast(kSymbolTable.size() - 1); +} + +/// @brief Nectar 64-bit Mach-O Linker. +/// @note This linker outputs Mach-O executables for macOS/iOS. +NECTAR_MODULE(DynamicLinker64MachO) { + CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); + + /** + * @brief parse flags and trigger options. + */ + for (size_t linker_arg{1}; linker_arg < argc; ++linker_arg) { + if (std::strcmp(argv[linker_arg], "-help") == 0) { + kLinkerSplash(); + + kConsoleOut << "-version: Show linker version.\n"; + kConsoleOut << "-help: Show linker help.\n"; + kConsoleOut << "-verbose: Enable linker trace.\n"; + kConsoleOut << "-fdylib: Output as a Dynamic Library.\n"; + kConsoleOut << "-ffat: Output as a FAT binary.\n"; + kConsoleOut << "-famd64: Output as an x86_64 Mach-O.\n"; + kConsoleOut << "-farm64: Output as an ARM64 Mach-O.\n"; + kConsoleOut << "-output: Select the output file name.\n"; + kConsoleOut << "-fstart: Specify entry point symbol.\n"; + + return NECTAR_SUCCESS; + } else if (std::strcmp(argv[linker_arg], "-version") == 0) { + kLinkerSplash(); + + return NECTAR_SUCCESS; + } else if (std::strcmp(argv[linker_arg], "-ffat") == 0) { + kFatBinaryEnable = true; + + continue; + } else if (std::strcmp(argv[linker_arg], "-famd64") == 0) { + kCpuType = CPU_TYPE_X86_64; + kCpuSubType = CPU_SUBTYPE_X86_64_ALL; + + continue; + } else if (std::strcmp(argv[linker_arg], "-farm64") == 0) { + kCpuType = CPU_TYPE_ARM64; + kCpuSubType = CPU_SUBTYPE_ARM64_ALL; + + continue; + } else if (std::strcmp(argv[linker_arg], "-fstart") == 0) { + if (argv[linker_arg + 1] == nullptr || argv[linker_arg + 1][0] == '-') continue; + + kLinkerStart = argv[linker_arg + 1]; + linker_arg += 1; + + continue; + } else if (std::strcmp(argv[linker_arg], "-verbose") == 0) { + kVerbose = true; + + continue; + } else if (std::strcmp(argv[linker_arg], "-fdylib") == 0) { + kIsDylib = true; + + if (kOutput.find(kMachOExt) != CompilerKit::STLString::npos) { + kOutput.erase(kOutput.find(kMachOExt), strlen(kMachOExt)); + kOutput += kMachODylibExt; + } + + continue; + } else if (std::strcmp(argv[linker_arg], "-output") == 0) { + if ((linker_arg + 1) > argc) continue; + + kOutput = argv[linker_arg + 1]; + ++linker_arg; + + continue; + } else { + if (argv[linker_arg][0] == '-') { + kConsoleOut << "unknown option: " << argv[linker_arg] << "\n"; + return EXIT_FAILURE; + } + + kObjectList.emplace_back(argv[linker_arg]); + + continue; + } + } + + if (kOutput.empty()) { + kConsoleOut << "no output filename set." << std::endl; + return NECTAR_EXEC_ERROR; + } else if (kObjectList.empty()) { + kConsoleOut << "no input files." << std::endl; + return NECTAR_EXEC_ERROR; + } else { + namespace FS = std::filesystem; + + // check for existing files, if they don't throw an error. + for (auto& obj : kObjectList) { + if (!FS::exists(obj)) { + kConsoleOut << "no such file: " << obj << std::endl; + return NECTAR_EXEC_ERROR; + } + } + } + + SectionInfoVec sections; + CompilerKit::Utils::AEReadableProtocol reader_protocol{}; + + entry_point_command entryCommand{}; + entryCommand.stacksize = kMachODefaultStackSz; + + // Collect all text and data from AE object files + for (const auto& objectFile : kObjectList) { + if (!std::filesystem::exists(objectFile)) continue; + + CompilerKit::AEHeader hdr{}; + + reader_protocol.fFilePtr = std::ifstream(objectFile, std::ifstream::binary); + reader_protocol.fFilePtr >> hdr; + + if (hdr.fMagic[0] == kAEMag0 && hdr.fMagic[1] == kAEMag1 && + hdr.fSize == sizeof(CompilerKit::AEHeader) && hdr.fMagic[2] == kAEMag2) { + std::size_t cnt = hdr.fCount; + + char* raw_ae_records = new char[cnt * sizeof(CompilerKit::AERecordHeader)]; + + if (!raw_ae_records) { + return NECTAR_EXEC_ERROR; + } + + std::memset(raw_ae_records, 0, cnt * sizeof(CompilerKit::AERecordHeader)); + + auto* ae_records = reader_protocol.Read(raw_ae_records, cnt); + + for (size_t ae_record_index = 0; ae_record_index < cnt; ++ae_record_index) { + SectionInfo section; + section.name = ae_records[ae_record_index].fName; + section.kind = ae_records[ae_record_index].fKind; + section.size = ae_records[ae_record_index].fSize; + + // Extract clean symbol name and add to symbol table + CompilerKit::STLString symbolName = macho_extract_symbol_name(section.name); + + if (!symbolName.empty()) { + // Determine section number (1 = __text, 2 = __data) + uint8_t sectNum = 0; + if (section.kind & CompilerKit::kPefCode) { + sectNum = 1; // __text section + } else if (section.kind & CompilerKit::kPefData) { + sectNum = 2; // __data section + } else if (section.kind & CompilerKit::kPefZero) { + sectNum = 3; // __bss section + } + + // N_EXT = external, N_SECT = defined in section + uint8_t symType = N_EXT | N_SECT; + + macho_add_symbol(symbolName, symType, sectNum, ae_records[ae_record_index].fOffset); + } + + sections.push_back(section); + } + + // Look up entry point from symbol table + auto entryIt = kSymbolOffsets.find(kLinkerStart); + + if (entryIt != kSymbolOffsets.end()) { + entryCommand.entryoff = entryIt->second; + kStartFound = true; + } + + delete[] raw_ae_records; + raw_ae_records = nullptr; + + // Read the actual code bytes + std::vector bytes; + bytes.resize(hdr.fCodeSize); + + reader_protocol.fFilePtr.seekg(std::streamsize(hdr.fStartCode)); + reader_protocol.fFilePtr.read(bytes.data(), std::streamsize(hdr.fCodeSize)); + + // Separate code and data based on section kind + for (auto& section : sections) { + if (section.kind == CompilerKit::kPefCode) { + kTextBytes.push_back({.mBlob = bytes, .mOffset = 0}); + } else if (section.kind == CompilerKit::kPefData) { + kDataBytes.push_back({.mBlob = bytes, .mOffset = 0}); + } + } + + reader_protocol.fFilePtr.close(); + continue; + } + + kConsoleOut << "not an object container: " << objectFile << std::endl; + return NECTAR_EXEC_ERROR; + } + + // Check for entry point in executables + if (!kStartFound && !kIsDylib) { + kConsoleOut << "undefined entrypoint " << kLinkerStart << " for executable: " << kOutput + << "\n"; + } + + // Calculate sizes + UInt64 textSize = 0; + UInt64 dataSize = 0; + + for (auto& blob : kTextBytes) { + textSize += blob.mBlob.size(); + } + + for (auto& blob : kDataBytes) { + dataSize += blob.mBlob.size(); + } + + // Open output file + std::ofstream output_fc(kOutput, std::ofstream::binary); + + if (output_fc.bad()) { + return NECTAR_FILE_NOT_FOUND; + } + + using namespace CompilerKit::MachO; + + UInt32 numCommands = 8; // __PAGEZERO, LC_BUILD_VERSION, __TEXT, __LINKEDIT, LC_LOAD_DYLINKER, + // LC_UUID, LC_SYMTAB, LC_DYSYMTAB + + if (!kIsDylib) { + numCommands += 1; // LC_MAIN + } + + UInt32 dataSegCmdSize = + kDataBytes.size() > 0 ? sizeof(segment_command_64) + sizeof(section_64) : 0; + + if (dataSegCmdSize > 0) ++numCommands; // __DATA segment + + UInt32 sizeOfCmds = 0; + UInt32 headerSize = sizeof(mach_header_64); + UInt32 pageZeroSize = sizeof(segment_command_64); + UInt32 textSegCmdSize = sizeof(segment_command_64) + sizeof(section_64); + UInt32 buildCmdSize = sizeof(build_version_command); + UInt32 mainCmdSize = sizeof(entry_point_command); + UInt32 uuidCmdSize = sizeof(uuid_command); + 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 + + sizeOfCmds = pageZeroSize + textSegCmdSize + dataSegCmdSize + buildCmdSize + uuidCmdSize + + symtabCmdSize + dysymtabCmdSize + linkeditCmdSize; + + if (!kIsDylib) sizeOfCmds += mainCmdSize; + + UInt64 headerAndCmdsSize = headerSize + sizeOfCmds; + UInt64 textFileOffset = AlignToPage(headerAndCmdsSize); + UInt64 textVMAddr = kDefaultBaseAddress; + UInt64 textSegmentSize = AlignToPage(textSize > 0 ? textSize : kPageSize); + UInt64 textVMSize = textFileOffset + textSegmentSize; // __TEXT includes header + + UInt64 dataFileOffset = textFileOffset + textSegmentSize; + UInt64 dataVMAddr = textVMAddr + textVMSize; + UInt64 dataSegmentSize = dataSize > 0 ? AlignToPage(dataSize) : 0; // 0 if no data + + // __LINKEDIT segment comes after data segment (or __TEXT if no data) + UInt64 linkeditFileOffset = + dataSegmentSize > 0 ? dataFileOffset + dataSegmentSize : textFileOffset + textSegmentSize; + UInt64 linkeditVMAddr = + dataSegmentSize > 0 ? dataVMAddr + dataSegmentSize : textVMAddr + textVMSize; + UInt64 symtabFileOffset = linkeditFileOffset; + UInt64 strtabFileOffset = symtabFileOffset + (kSymbolTable.size() * sizeof(nlist_64)); + UInt64 linkeditFileSize = (kSymbolTable.size() * sizeof(nlist_64)) + kStringTable.size(); + UInt64 linkeditVMSize = AlignToPage(linkeditFileSize > 0 ? linkeditFileSize : 1); + + // Write Mach-O header + mach_header_64 header{}; + + header.magic = MH_MAGIC_64; + header.cputype = kCpuType; + header.cpusubtype = kCpuSubType; + header.filetype = kIsDylib ? MH_DYLIB : MH_EXECUTE; + header.ncmds = numCommands; + header.sizeofcmds = sizeOfCmds; + header.flags = MH_NOUNDEFS | MH_DYLDLINK | MH_TWOLEVEL | MH_PIE; + header.reserved = 0; + + output_fc.write(reinterpret_cast(&header), sizeof(header)); + + segment_command_64 pageZeroSegment{}; + pageZeroSegment.cmd = LC_SEGMENT_64; + pageZeroSegment.cmdsize = sizeof(segment_command_64); + CopySegmentName(pageZeroSegment.segname, kSegmentPageZero); + pageZeroSegment.vmaddr = 0; + pageZeroSegment.vmsize = 0x100000000; + pageZeroSegment.fileoff = 0; + pageZeroSegment.filesize = 0; + pageZeroSegment.maxprot = 0; + pageZeroSegment.initprot = 0; + pageZeroSegment.nsects = 0; + pageZeroSegment.flags = 0; + + output_fc.write(reinterpret_cast(&pageZeroSegment), sizeof(pageZeroSegment)); + + build_version_command build = {.cmd = LC_BUILD_VERSION, + .cmdsize = sizeof(build_version_command), + .platform = PLATFORM_MACOS, + .minos = (kLatestOSX << 16), // macOS 11.0 + .sdk = (kLatestOSX << 16), // macOS 11.0 + .ntools = 0}; + + output_fc.write(reinterpret_cast(&build), sizeof(build)); + + // Write __TEXT segment command + segment_command_64 textSegment{}; + textSegment.cmd = LC_SEGMENT_64; + textSegment.cmdsize = sizeof(segment_command_64) + sizeof(section_64); // 1 section + CopySegmentName(textSegment.segname, kSegmentText); + textSegment.vmaddr = textVMAddr; + textSegment.vmsize = textVMSize; // Header + code (page-aligned) + textSegment.fileoff = 0; // Must include Mach-O header + textSegment.filesize = + dataSegmentSize > 0 ? dataFileOffset : linkeditFileOffset; // Extend to next segment + textSegment.maxprot = VM_PROT_READ | VM_PROT_EXECUTE; + textSegment.initprot = VM_PROT_READ | VM_PROT_EXECUTE; + textSegment.nsects = 1; + textSegment.flags = 0; + + output_fc.write(reinterpret_cast(&textSegment), sizeof(textSegment)); + + // Write __text section header + section_64 textSection{}; + CopySegmentName(textSection.sectname, kSectionText); + CopySegmentName(textSection.segname, kSegmentText); + textSection.addr = textVMAddr + textFileOffset; // Section is at offset within segment + textSection.size = textSize; + textSection.offset = static_cast(textFileOffset); + textSection.align = kSectionAlign; + textSection.reloff = 0; + textSection.nreloc = 0; + textSection.flags = S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS; + textSection.reserved1 = 0; + textSection.reserved2 = 0; + textSection.reserved3 = 0; + + output_fc.write(reinterpret_cast(&textSection), sizeof(textSection)); + + // Write __DATA segment command + segment_command_64 dataSegment{}; + dataSegment.cmd = LC_SEGMENT_64; + dataSegment.cmdsize = sizeof(segment_command_64) + sizeof(section_64); // 1 section + CopySegmentName(dataSegment.segname, kSegmentData); + dataSegment.vmaddr = dataVMAddr; + dataSegment.vmsize = dataSegmentSize; + dataSegment.fileoff = dataFileOffset; + dataSegment.filesize = dataSize; + dataSegment.maxprot = VM_PROT_READ | VM_PROT_WRITE; + dataSegment.initprot = VM_PROT_READ | VM_PROT_WRITE; + dataSegment.nsects = 1; + dataSegment.flags = 0; + + if (dataSegCmdSize > 0) + output_fc.write(reinterpret_cast(&dataSegment), sizeof(dataSegment)); + + // Write __data section header + section_64 dataSection{}; + CopySegmentName(dataSection.sectname, kSectionData); + CopySegmentName(dataSection.segname, kSegmentData); + dataSection.addr = dataVMAddr; + dataSection.size = dataSize; + dataSection.offset = static_cast(dataFileOffset); + dataSection.align = kSectionAlign; + dataSection.reloff = 0; + dataSection.nreloc = 0; + dataSection.flags = 0; + dataSection.reserved1 = 0; + dataSection.reserved2 = 0; + dataSection.reserved3 = 0; + + if (dataSegCmdSize > 0) + output_fc.write(reinterpret_cast(&dataSection), sizeof(dataSection)); + + // Write __LINKEDIT segment command (contains symbol/string tables) + segment_command_64 linkeditSegment{}; + linkeditSegment.cmd = LC_SEGMENT_64; + linkeditSegment.cmdsize = sizeof(segment_command_64); // No sections + CopySegmentName(linkeditSegment.segname, "__LINKEDIT"); + linkeditSegment.vmaddr = linkeditVMAddr; + linkeditSegment.vmsize = linkeditVMSize; + linkeditSegment.fileoff = linkeditFileOffset; + linkeditSegment.filesize = linkeditFileSize; + linkeditSegment.maxprot = VM_PROT_READ; + linkeditSegment.initprot = VM_PROT_READ; + linkeditSegment.nsects = 0; + linkeditSegment.flags = 0; + + output_fc.write(reinterpret_cast(&linkeditSegment), sizeof(linkeditSegment)); + + // Write LC_LOAD_DYLINKER command + constexpr const char* dyldPath = "/usr/lib/dyld"; + std::vector dylinkerCmd(dylinkerCmdSize, 0); + dylinker_command* dylinker = reinterpret_cast(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()); + + // Write LC_MAIN entry point command (executables only) + if (!kIsDylib) { + entryCommand.cmd = LC_MAIN; + entryCommand.cmdsize = sizeof(entry_point_command); + // entryoff is relative to __TEXT segment file offset + entryCommand.entryoff = textFileOffset + entryCommand.entryoff; + + output_fc.write(reinterpret_cast(&entryCommand), sizeof(entryCommand)); + } + + // Write LC_UUID command + uuid_command uuidCmd{}; + uuidCmd.cmd = LC_UUID; + uuidCmd.cmdsize = sizeof(uuid_command); + + // Generate a random UUID (version 4) + std::random_device rd; + std::mt19937 gen(rd()); + uuids::uuid_random_generator uuidGen(gen); + uuids::uuid generatedUuid = uuidGen(); + auto uuidBytes = generatedUuid.as_bytes(); + std::memcpy(uuidCmd.uuid, uuidBytes.data(), 16); + + output_fc.write(reinterpret_cast(&uuidCmd), sizeof(uuidCmd)); + + // Write LC_SYMTAB command + symtab_command symtabCmd{}; + symtabCmd.cmd = LC_SYMTAB; + symtabCmd.cmdsize = sizeof(symtab_command); + symtabCmd.symoff = static_cast(symtabFileOffset); + symtabCmd.nsyms = static_cast(kSymbolTable.size()); + symtabCmd.stroff = static_cast(strtabFileOffset); + symtabCmd.strsize = static_cast(kStringTable.size()); + + output_fc.write(reinterpret_cast(&symtabCmd), sizeof(symtabCmd)); + + // Write LC_DYSYMTAB command + dysymtab_command dysymtabCmd{}; + std::memset(&dysymtabCmd, 0, sizeof(dysymtabCmd)); + dysymtabCmd.cmd = LC_DYSYMTAB; + dysymtabCmd.cmdsize = sizeof(dysymtab_command); + + // All symbols are local for now + dysymtabCmd.ilocalsym = 0; + dysymtabCmd.nlocalsym = static_cast(kSymbolTable.size()); + + // External symbols start after locals + dysymtabCmd.iextdefsym = static_cast(kSymbolTable.size()); + dysymtabCmd.nextdefsym = 0; + + // Undefined symbols + dysymtabCmd.iundefsym = static_cast(kSymbolTable.size()); + dysymtabCmd.nundefsym = 0; + + output_fc.write(reinterpret_cast(&dysymtabCmd), sizeof(dysymtabCmd)); + + // Pad to text section offset + UInt64 currentPos = output_fc.tellp(); + UInt64 padding = textFileOffset - currentPos; + + if (padding > 0) { + std::vector zeros(padding, 0); + output_fc.write(zeros.data(), zeros.size()); + } + + // Write __text content + for (auto& blob : kTextBytes) { + output_fc.write(blob.mBlob.data(), blob.mBlob.size()); + } + + // Pad to data section offset + currentPos = output_fc.tellp(); + padding = dataFileOffset - currentPos; + + if (padding > 0) { + std::vector zeros(padding, 0); + output_fc.write(zeros.data(), zeros.size()); + } + + // Write __data content + for (auto& blob : kDataBytes) { + output_fc.write(blob.mBlob.data(), blob.mBlob.size()); + } + + // Pad to symbol table offset + currentPos = output_fc.tellp(); + padding = symtabFileOffset - currentPos; + + if (padding > 0) { + std::vector zeros(padding, 0); + output_fc.write(zeros.data(), zeros.size()); + } + + // Write symbol table (nlist_64 entries) + for (auto& sym : kSymbolTable) { + output_fc.write(reinterpret_cast(&sym), sizeof(nlist_64)); + } + + + // Write string table + output_fc.write(kStringTable.data(), kStringTable.size()); + + output_fc.flush(); + + return NECTAR_SUCCESS; +} + +// Last rev - 2026 + +#endif // ifdef CK_USE_MACHO_LINKER diff --git a/src/CompilerKit/src/Generators/DynamicLinker64+PEF.cpp b/src/CompilerKit/src/Generators/DynamicLinker64+PEF.cpp new file mode 100644 index 0000000..34333a3 --- /dev/null +++ b/src/CompilerKit/src/Generators/DynamicLinker64+PEF.cpp @@ -0,0 +1,677 @@ +// SPDX-License-Identifier: Apache-2.0 +// 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 + +/// @author Amlal El Mahrouss (amlal@nekernel.org) +/// @brief NeKernel.org 64-bit PEF Linker. +/// @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. + +#include +#include +#include +#include +#include +#include +#include + +#define kLinkerPefNoCpu (0U) +#define kLinkerPefNoSubCpu (0U) +#define kLinkerPefDefaultOutput {"a" kPefExt} + +#define kLinkerVersionStr "Nectar 64-Bit Linker (NeKernel PEF)" + +#define kLinkerDefaultOrigin kPefBaseOrigin + +#define kLinkerId (0x5046FF) + +#define kLinkerAbiContainer "__PEFContainer:ABI:" +#define kLinkerGuidContainer "__PEFContainer:GUID:" +#define kEndContainer "__PEFContainer:END:" + +#define kLinkerSplash() kStdOut << kLinkerVersionStr << kStdEndl + +/// @brief PEF stack size symbol. +#define kLinkerStackSizeSymbol "__PEFSizeOfReserveStack" + +#define kConsoleOut \ + (std::cout << "\e[0;31m" \ + << "ld64: " \ + << "\e[0;97m") + +enum struct ABIType : Int32 { + kABITypeNull = 0, + kABITypeStart = 0x1010, /* The start of ABI list. */ + kABITypeNE = 0x5046, /* PF (NeKernel.org's PEF ABI) */ + kABITypeEnd = kABITypeNull, + kABITypeInvalid = 0xFFFF, +}; + +static CompilerKit::STLString kOutput = kLinkerPefDefaultOutput; +static ABIType kAbi = ABIType::kABITypeNE; +static Int32 kSubArch = kLinkerPefNoSubCpu; +static Int32 kArch = CompilerKit::kPefArchInvalid; +static bool kFatBinaryEnable = false; +static bool kStartFound = false; +static bool kDuplicateSymbols = false; + +/* ld64 is to be found, mld is to be found at runtime. */ +static const char* kLinkerDefineSymbol = ":UndefinedSymbol:"; +static const char* kLinkerDynamicSym = ":RuntimeSymbol:"; + +static CompilerKit::STLString kLinkerStart = kPefStart; + +/* object code and list. */ +static std::vector kObjectList; +static std::vector kObjectBytes; + +/// @brief Nectar 64-bit Linker. +/// @note This linker is made for PEF executable, thus Nectar based OSes. +NECTAR_MODULE(DynamicLinker64PEF) { + bool is_executable = true; + + CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); + + /** + * @brief parse flags and trigger options. + */ + for (size_t linker_arg = 1; linker_arg < argc; ++linker_arg) { + if (std::strcmp(argv[linker_arg], "-help") == 0) { + kLinkerSplash(); + + kConsoleOut << "-version: Show linker version.\n"; + kConsoleOut << "-help: Show linker help.\n"; + kConsoleOut << "-verbose: Enable linker trace.\n"; + kConsoleOut << "-fdylib: Output as a Dynamic PEF.\n"; + kConsoleOut << "-ffat: Output as a FAT PEF.\n"; + kConsoleOut << "-f32k: Output as a 32x0 PEF.\n"; + kConsoleOut << "-f64k: Output as a 64x0 PEF.\n"; + kConsoleOut << "-famd64: Output as a AMD64 PEF.\n"; + kConsoleOut << "-frv64: Output as a RISC-V PEF.\n"; + kConsoleOut << "-fpower64: Output as a POWER PEF.\n"; + kConsoleOut << "-farm64: Output as a ARM64 PEF.\n"; + kConsoleOut << "-output: Select the output file name.\n"; + + return NECTAR_SUCCESS; + } else if (std::strcmp(argv[linker_arg], "-version") == 0) { + kLinkerSplash(); + + return NECTAR_SUCCESS; + } else if (std::strcmp(argv[linker_arg], "-ffat") == 0) { + kFatBinaryEnable = true; + + continue; + } else if (std::strcmp(argv[linker_arg], "-f64k") == 0) { + kArch = CompilerKit::kPefArch64000; + + continue; + } else if (std::strcmp(argv[linker_arg], "-famd64") == 0) { + kArch = CompilerKit::kPefArchAMD64; + + continue; + } else if (std::strcmp(argv[linker_arg], "-fstart") == 0) { + if (argv[linker_arg + 1] == nullptr || argv[linker_arg + 1][0] == '-') continue; + + kLinkerStart = argv[linker_arg + 1]; + linker_arg += 1; + + continue; + } else if (std::strcmp(argv[linker_arg], "-f32k") == 0) { + kArch = CompilerKit::kPefArch32000; + + continue; + } else if (std::strcmp(argv[linker_arg], "-fpower64") == 0) { + kArch = CompilerKit::kPefArchPowerPC; + + continue; + } else if (std::strcmp(argv[linker_arg], "-friscv64") == 0) { + kArch = CompilerKit::kPefArchRISCV; + + continue; + } else if (std::strcmp(argv[linker_arg], "-farm64") == 0) { + kArch = CompilerKit::kPefArchARM64; + + continue; + } else if (std::strcmp(argv[linker_arg], "-verbose") == 0) { + kVerbose = true; + + continue; + } else if (std::strcmp(argv[linker_arg], "-fdylib") == 0) { + if (kOutput.empty()) { + continue; + } + + if (kOutput.find(kPefExt) != CompilerKit::STLString::npos) + kOutput.erase(kOutput.find(kPefExt), strlen(kPefExt)); + + kOutput += kPefDylibExt; + is_executable = false; + + continue; + } else if (std::strcmp(argv[linker_arg], "-output") == 0) { + if ((linker_arg + 1) > argc) continue; + + kOutput = argv[linker_arg + 1]; + ++linker_arg; + + continue; + } else { + if (argv[linker_arg][0] == '-') { + kConsoleOut << "unknown option: " << argv[linker_arg] << "\n"; + return EXIT_FAILURE; + } + + kObjectList.emplace_back(argv[linker_arg]); + + continue; + } + } + + if (kOutput.empty()) { + kConsoleOut << "no output filename set." << std::endl; + return NECTAR_EXEC_ERROR; + } else if (kObjectList.empty()) { + kConsoleOut << "no input files." << std::endl; + return NECTAR_EXEC_ERROR; + } else { + namespace FS = std::filesystem; + + // check for existing files, if they don't throw an error. + for (auto& obj : kObjectList) { + if (!FS::exists(obj)) { + // if filesystem doesn't find file + // -> throw error. + kConsoleOut << "no such file: " << obj << std::endl; + return NECTAR_EXEC_ERROR; + } + } + } + + // 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 NECTAR_EXEC_ERROR; + } + + CompilerKit::PEFContainer pef_container{}; + + int32_t archs = kArch; + + pef_container.Count = 0UL; + pef_container.Kind = is_executable ? CompilerKit::kPefKindExec : CompilerKit::kPefKindDylib; + pef_container.SubCpu = kSubArch; + pef_container.Linker = kLinkerId; // Nectar Linker + pef_container.Abi = static_cast(kAbi); // Multi-Processor UX ABI + pef_container.Magic[0] = kPefMagic[kFatBinaryEnable ? 2 : 0]; + pef_container.Magic[1] = kPefMagic[1]; + pef_container.Magic[2] = kPefMagic[kFatBinaryEnable ? 0 : 2]; + pef_container.Magic[3] = kPefMagic[3]; + pef_container.Version = kPefVersion; + + // specify the start address, can be 0x10000 + pef_container.Start = kLinkerDefaultOrigin; + pef_container.HdrSz = sizeof(CompilerKit::PEFContainer); + pef_container.Checksum = 0UL; + + std::ofstream output_fc(kOutput, std::ofstream::binary); + + if (output_fc.bad()) { + if (kVerbose) { + kConsoleOut << "error: " << strerror(errno) << "\n"; + } + + return NECTAR_FILE_NOT_FOUND; + } + + //! Read AE to convert as PEF. + + std::vector command_headers; + CompilerKit::Utils::AEReadableProtocol reader_protocol{}; + + for (const auto& objectFile : kObjectList) { + if (!std::filesystem::exists(objectFile)) continue; + + CompilerKit::AEHeader hdr{}; + + reader_protocol.fFilePtr = std::ifstream(objectFile, std::ifstream::binary); + reader_protocol.fFilePtr >> hdr; + + if (hdr.fMagic[0] == kAEMag0 && hdr.fMagic[1] == kAEMag1 && + hdr.fSize == sizeof(CompilerKit::AEHeader) && hdr.fMagic[2] == kAEMag2) { + if (hdr.fArch != kArch && hdr.fVersion == kAEIdentVersion) { + if (kVerbose) kConsoleOut << "is this a FAT binary? : "; + + if (!kFatBinaryEnable) { + if (kVerbose) kConsoleOut << "not a FAT binary.\n"; + + kConsoleOut << "object " << objectFile + << " is a different kind of architecture and output isn't " + "treated as a FAT binary." + << std::endl; + + return NECTAR_FAT_ERROR; + } else { + if (kVerbose) { + kConsoleOut << "Architecture matches what we expect.\n"; + } + } + } + + // append arch type to archs varaible. + archs |= hdr.fArch; + std::size_t cnt = hdr.fCount; + + if (kVerbose) kConsoleOut << "header found, record count: " << cnt << "\n"; + + pef_container.Count = cnt; + + char* raw_ae_records = new char[cnt * sizeof(CompilerKit::AERecordHeader)]; + + if (!raw_ae_records) { + if (kVerbose) kConsoleOut << "allocation failed for records of count: " << cnt << "\n"; + } + + std::memset(raw_ae_records, 0, cnt * sizeof(CompilerKit::AERecordHeader)); + + auto* ae_records = reader_protocol.Read(raw_ae_records, cnt); + + size_t org = kLinkerDefaultOrigin; + + for (size_t ae_record_index = 0; ae_record_index < cnt; ++ae_record_index) { + CompilerKit::PEFCommandHeader command_header{{}}; + std::size_t offset_of_obj = ae_records[ae_record_index].fOffset; + + std::memcpy(command_header.Name, ae_records[ae_record_index].fName, kPefNameLen); + + CompilerKit::STLString cmd_hdr_name(command_header.Name); + + // check this header if it's any valid. + if (cmd_hdr_name.find(kPefCode64) == CompilerKit::STLString::npos && + cmd_hdr_name.find(kPefData64) == CompilerKit::STLString::npos && + cmd_hdr_name.find(kPefZero64) == CompilerKit::STLString::npos) { + if (cmd_hdr_name.find(kLinkerStart) == CompilerKit::STLString::npos && + *command_header.Name == 0) { + if (cmd_hdr_name.find(kLinkerDefineSymbol) != CompilerKit::STLString::npos) { + goto ld_mark_header; + } else { + continue; + } + } + } + + if (cmd_hdr_name.find(kLinkerStart) != CompilerKit::STLString::npos && + cmd_hdr_name.find(kPefCode64) != CompilerKit::STLString::npos) { + kStartFound = true; + } + + ld_mark_header: + command_header.Offset = offset_of_obj; + command_header.Kind = ae_records[ae_record_index].fKind; + command_header.VirtualSize = ae_records[ae_record_index].fSize; + command_header.Cpu = hdr.fArch; + command_header.VirtualAddress = org; + command_header.SubCpu = hdr.fSubArch; + command_header.OffsetSize = ae_records[ae_record_index].fSize; + + org += command_header.VirtualSize; + + if (kVerbose) { + kConsoleOut << "Record: " << ae_records[ae_record_index].fName << " is marked.\n"; + kConsoleOut << "Offset: " << command_header.Offset << "\n"; + } + + command_headers.emplace_back(command_header); + } + + delete[] raw_ae_records; + raw_ae_records = nullptr; + + std::vector bytes; + bytes.resize(hdr.fCodeSize); + + reader_protocol.fFilePtr.seekg(std::streamsize(hdr.fStartCode)); + reader_protocol.fFilePtr.read(bytes.data(), std::streamsize(hdr.fCodeSize)); + + kObjectBytes.push_back({.mBlob = bytes, .mOffset = hdr.fStartCode}); + + // Blob was written, close fp. + + reader_protocol.fFilePtr.close(); + + continue; + } + + kConsoleOut << "not an object container: " << objectFile << std::endl; + + // don't continue, it is a fatal error. + return NECTAR_EXEC_ERROR; + } + + pef_container.Cpu = archs; + + output_fc << pef_container; + + if (kVerbose) { + kConsoleOut << "wrote container to: " << output_fc.tellp() << ".\n"; + } + + output_fc.seekp(std::streamsize(pef_container.HdrSz)); + + std::vector not_found; + std::vector symbols; + + // step 2: check for errors (multiple symbols, undefined ones) + + CompilerKit::STLString cmd_hdr_name; + + for (auto& command_hdr : command_headers) { + // check if this symbol needs to be resolved. + if (CompilerKit::STLString(command_hdr.Name).find(kLinkerDefineSymbol) != + CompilerKit::STLString::npos && + CompilerKit::STLString(command_hdr.Name).find(kLinkerDynamicSym) == + CompilerKit::STLString::npos) { + if (kVerbose) kConsoleOut << "Found undefined symbol: " << command_hdr.Name << "\n"; + + cmd_hdr_name = command_hdr.Name; + + if (auto it = std::find(not_found.begin(), not_found.end(), cmd_hdr_name); + it == not_found.end()) { + not_found.emplace_back(cmd_hdr_name); + } + } + + symbols.emplace_back(command_hdr.Name); + } + + // Now try to solve these symbols. + + for (size_t not_found_idx = 0; not_found_idx < command_headers.size(); ++not_found_idx) { + if (const auto it = std::find(not_found.begin(), not_found.end(), + CompilerKit::STLString(command_headers[not_found_idx].Name)); + it != not_found.end()) { + CompilerKit::STLString symbol_imp = *it; + + if (symbol_imp.find(kLinkerDefineSymbol) == CompilerKit::STLString::npos) continue; + + // erase the lookup prefix. + symbol_imp.erase(0, symbol_imp.find(kLinkerDefineSymbol) + strlen(kLinkerDefineSymbol)); + + // demangle everything. + while (symbol_imp.find('$') != CompilerKit::STLString::npos) + symbol_imp.erase(symbol_imp.find('$'), 1); + + // the reason we do is because, this may not match the symbol, and we need + // to look for other matching symbols. + for (auto& command_hdr : command_headers) { + if (CompilerKit::STLString(command_hdr.Name).find(symbol_imp) != + CompilerKit::STLString::npos && + CompilerKit::STLString(command_hdr.Name).find(kLinkerDefineSymbol) == + CompilerKit::STLString::npos) { + CompilerKit::STLString undefined_symbol = command_hdr.Name; + auto result_of_sym = undefined_symbol.substr(undefined_symbol.find(symbol_imp)); + + for (int i = 0; result_of_sym[i] != 0; ++i) { + if (result_of_sym[i] != symbol_imp[i]) goto ld_continue_search; + } + + not_found.erase(it); + + if (kVerbose) kConsoleOut << "Found symbol: " << command_hdr.Name << "\n"; + + break; + } + } + + ld_continue_search: + continue; + } + } + + // step 3: check for errors (recheck if we have those symbols.) + + if (!kStartFound && is_executable) { + if (kVerbose) + kConsoleOut << "Undefined entrypoint: " << kLinkerStart + << ", you may have forget to link " + "against your runtime library.\n"; + + kConsoleOut << "Undefined entrypoint " << kLinkerStart << " for executable: " << kOutput + << "\n"; + } + + // step 4: write all PEF commands. + + CompilerKit::PEFCommandHeader date_cmd_hdr{}; + + time_t timestamp = time(nullptr); + + CompilerKit::STLString timeStampStr = "__PEFContainer:BuildEpoch:"; + timeStampStr += std::to_string(timestamp); + + strncpy(date_cmd_hdr.Name, timeStampStr.c_str(), timeStampStr.size()); + + date_cmd_hdr.Flags = 0; + date_cmd_hdr.Kind = CompilerKit::kPefZero; + date_cmd_hdr.Offset = output_fc.tellp(); + date_cmd_hdr.VirtualSize = timeStampStr.size(); + + command_headers.push_back(std::move(date_cmd_hdr)); + + CompilerKit::PEFCommandHeader abi_cmd_hdr{}; + + CompilerKit::STLString abi = kLinkerAbiContainer; + + switch (kArch) { + case CompilerKit::kPefArchAMD64: { + abi += "MSFT"; + break; + } + case CompilerKit::kPefArchPowerPC: + case CompilerKit::kPefArchARM64: + case CompilerKit::kPefArchRISCV: + case CompilerKit::kPefArch32000: + case CompilerKit::kPefArch64000: { + abi += "KRNL"; + break; + } + default: { + abi += "NOOP"; + break; + } + } + + std::memcpy(abi_cmd_hdr.Name, abi.c_str(), abi.size()); + + abi_cmd_hdr.VirtualSize = abi.size(); + abi_cmd_hdr.Offset = output_fc.tellp(); + abi_cmd_hdr.Flags = 0; + abi_cmd_hdr.Kind = CompilerKit::kPefLinkerID; + + command_headers.push_back(std::move(abi_cmd_hdr)); + + CompilerKit::PEFCommandHeader stack_cmd_hdr{{}}; + + stack_cmd_hdr.Cpu = kArch; + stack_cmd_hdr.Flags = 0; + stack_cmd_hdr.VirtualSize = sizeof(uintptr_t); + stack_cmd_hdr.Offset = 0; + + std::memcpy(stack_cmd_hdr.Name, kLinkerStackSizeSymbol, strlen(kLinkerStackSizeSymbol)); + + command_headers.push_back(std::move(stack_cmd_hdr)); + + CompilerKit::PEFCommandHeader uuid_cmd_hdr{}; + + std::random_device rd; + + auto seedData = std::array{}; + std::generate(std::begin(seedData), std::end(seedData), std::ref(rd)); + std::seed_seq seq(std::begin(seedData), std::end(seedData)); + std::mt19937 generator(seq); + + auto gen = uuids::uuid_random_generator{generator}; + uuids::uuid id = gen(); + auto uuidStr = uuids::to_string(id); + + std::memcpy(uuid_cmd_hdr.Name, kLinkerGuidContainer, strlen(kLinkerGuidContainer)); + std::memcpy(uuid_cmd_hdr.Name + strlen(kLinkerGuidContainer), uuidStr.c_str(), uuidStr.size()); + + uuid_cmd_hdr.VirtualSize = strlen(uuid_cmd_hdr.Name); + uuid_cmd_hdr.Offset = output_fc.tellp(); + uuid_cmd_hdr.Flags = CompilerKit::kPefLinkerID; + uuid_cmd_hdr.Kind = CompilerKit::kPefZero; + + command_headers.push_back(std::move(uuid_cmd_hdr)); + + // prepare a symbol vector. + std::vector undef_symbols; + std::vector dupl_symbols; + std::vector resolve_symbols; + + constexpr Int32 kPaddingOffset = 16; + + size_t previous_offset = + (command_headers.size() * sizeof(CompilerKit::PEFCommandHeader)) + kPaddingOffset; + + CompilerKit::PEFCommandHeader end_exec_hdr; + + end_exec_hdr.Offset = output_fc.tellp(); + end_exec_hdr.Flags = CompilerKit::kPefLinkerID; + end_exec_hdr.Kind = CompilerKit::kPefZero; + + std::memcpy(end_exec_hdr.Name, kEndContainer, strlen(kEndContainer)); + + end_exec_hdr.VirtualSize = strlen(end_exec_hdr.Name); + + command_headers.push_back(std::move(end_exec_hdr)); + + // Finally write down the command headers. + // And check for any duplications + for (size_t commandHeaderIndex = 0UL; commandHeaderIndex < command_headers.size(); + ++commandHeaderIndex) { + if (CompilerKit::STLString(command_headers[commandHeaderIndex].Name) + .find(kLinkerDefineSymbol) != CompilerKit::STLString::npos && + CompilerKit::STLString(command_headers[commandHeaderIndex].Name).find(kLinkerDynamicSym) == + CompilerKit::STLString::npos) { + // ignore :UndefinedSymbol: headers, they do not contain code. + continue; + } + + CompilerKit::STLString symbol_name = command_headers[commandHeaderIndex].Name; + + if (!symbol_name.empty()) { + undef_symbols.emplace_back(symbol_name); + } + + command_headers[commandHeaderIndex].Offset += previous_offset; + previous_offset += command_headers[commandHeaderIndex].VirtualSize; + + CompilerKit::STLString name = command_headers[commandHeaderIndex].Name; + + /// so this is valid when we get to the entrypoint. + /// it is always a code64 container. And should equal to kLinkerStart as well. + /// this chunk of code updates the pef_container.Start with the updated offset. + if (name.find(kLinkerStart) != CompilerKit::STLString::npos && + name.find(kPefCode64) != CompilerKit::STLString::npos) { + pef_container.Start = command_headers[commandHeaderIndex].Offset; + auto tellCurPos = output_fc.tellp(); + + output_fc.seekp(0); + output_fc << pef_container; + + output_fc.seekp(tellCurPos); + } + + if (kVerbose) { + kConsoleOut << "Command name: " << name << "\n"; + kConsoleOut << "Virtual address of command content: " + << command_headers[commandHeaderIndex].Offset << "\n"; + } + + output_fc << command_headers[commandHeaderIndex]; + + for (size_t sub_command_header_index = 0UL; sub_command_header_index < command_headers.size(); + ++sub_command_header_index) { + if (sub_command_header_index == commandHeaderIndex) continue; + + if (CompilerKit::STLString(command_headers[sub_command_header_index].Name) + .find(kLinkerDefineSymbol) != CompilerKit::STLString::npos && + CompilerKit::STLString(command_headers[sub_command_header_index].Name) + .find(kLinkerDynamicSym) == CompilerKit::STLString::npos) { + if (kVerbose) { + kConsoleOut << "Ignoring :UndefinedSymbol: headers...\n"; + } + + // Let's ignore :UndefinedSymbol: headers, they do not contain code. + continue; + } + + auto& command_hdr = command_headers[sub_command_header_index]; + + if (command_hdr.Name == CompilerKit::STLString(command_headers[commandHeaderIndex].Name)) { + if (std::find(dupl_symbols.cbegin(), dupl_symbols.cend(), command_hdr.Name) == + dupl_symbols.cend()) { + dupl_symbols.emplace_back(command_hdr.Name); + } + + if (kVerbose) kConsoleOut << "Found duplicate symbols of: " << command_hdr.Name << "\n"; + + kDuplicateSymbols = true; + } + } + } + + if (!dupl_symbols.empty()) { + for (auto& symbol : dupl_symbols) { + kConsoleOut << "Multiple symbols of: " << symbol << " detected, cannot continue.\n"; + } + + return NECTAR_EXEC_ERROR; + } + + // step 2.5: write program bytes. + + for (auto& struct_of_blob : kObjectBytes) { + output_fc.write(struct_of_blob.mBlob.data(), struct_of_blob.mBlob.size()); + } + + if (kVerbose) { + kConsoleOut << "Wrote contents of: " << kOutput << "\n"; + } + + // step 3: check if we have those symbols + + std::vector unreferenced_symbols; + + for (auto& command_hdr : command_headers) { + if (auto it = + std::find(not_found.begin(), not_found.end(), CompilerKit::STLString(command_hdr.Name)); + it != not_found.end()) { + unreferenced_symbols.emplace_back(command_hdr.Name); + } + } + + if (!unreferenced_symbols.empty()) { + for (auto& unreferenced_symbol : unreferenced_symbols) { + kConsoleOut << "Undefined symbol " << unreferenced_symbol << "\n"; + } + + return NECTAR_EXEC_ERROR; + } + + if ((!kStartFound || kDuplicateSymbols) && + (std::filesystem::exists(kOutput) || !unreferenced_symbols.empty())) { + if (kVerbose) { + kConsoleOut << "File: " << kOutput << " is corrupt now...\n"; + } + + return NECTAR_EXEC_ERROR; + } + + return NECTAR_SUCCESS; +} + +// Last rev 2026 diff --git a/src/CompilerKit/src/Linkers/DynamicLinker64+MachO.cpp b/src/CompilerKit/src/Linkers/DynamicLinker64+MachO.cpp deleted file mode 100644 index 911b582..0000000 --- a/src/CompilerKit/src/Linkers/DynamicLinker64+MachO.cpp +++ /dev/null @@ -1,637 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// 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 - -/// @author Amlal El Mahrouss (amlal@nekernel.org) -/// @brief NeKernel.org 64-bit Mach-O Linker. -/// @version Last Rev: 2026 -/// @note Outputs Mach-O executables with __TEXT and __DATA segments. - -#ifdef CK_USE_MACHO_LINKER - -#include -#include -#include -#include -#include -#include -#include -#include - -#define kLatestOSX (15) - -#define kMachODefaultEntrypoint "_main" -#define kMachODefaultOutput {"a" kMachOExt} - -#define kLinkerVersionStr "Nectar 64-Bit Linker (OS X Mach-O)" - -#define kLinkerSplash() kStdOut << kLinkerVersionStr << kStdEndl - -#define kConsoleOut \ - (std::cout << "\e[0;31m" \ - << "mld64: " \ - << "\e[0;97m") - -static CompilerKit::STLString kOutput = kMachODefaultOutput; -static cpu_type_t kCpuType = CPU_TYPE_X86_64; -static cpu_subtype_t kCpuSubType = CPU_SUBTYPE_X86_64_ALL; -static bool kFatBinaryEnable = false; -static bool kStartFound = false; -static bool kDuplicateSymbols = false; -static bool kIsDylib = false; -static Int64 kMachODefaultStackSz = 0; - -static CompilerKit::STLString kLinkerStart = kMachODefaultEntrypoint; - -/* object code and list. */ -static std::vector kObjectList; -static std::vector kTextBytes; -static std::vector kDataBytes; - -/* @brief symbol tables */ -static std::vector kSymbolTable; -static std::vector kStringTable; -static std::map kSymbolOffsets; - -/// @brief Structure to hold section information from AE records -struct SectionInfo { - CompilerKit::STLString name; - UInt32 kind; - std::vector bytes; - UInt64 address; - UInt64 size; -}; - -using SectionInfoVec = std::vector; - -/// @brief Extract clean symbol name from AE record name -/// AE format: ".code64$symbolname" or "symbolname.code64" -static CompilerKit::STLString macho_extract_symbol_name(const CompilerKit::STLString& aeName) { - CompilerKit::STLString name = aeName; - - // Remove section prefixes/suffixes - const char* sections[] = {".code64", ".data64", ".zero64", "$"}; - - for (const auto& sec : sections) { - size_t pos; - while ((pos = name.find(sec)) != CompilerKit::STLString::npos) { - name.erase(pos, strlen(sec)); - } - } - - // Trim whitespace - while (!name.empty() && (name.front() == ' ' || name.front() == '\t')) { - name.erase(0, 1); - } - - while (!name.empty() && (name.back() == ' ' || name.back() == '\t')) { - name.pop_back(); - } - - return name; -} - -/// @brief Add a symbol to the symbol table -static UInt32 macho_add_symbol(const CompilerKit::STLString& name, uint8_t type, uint8_t sect, - UInt64 value) { - // Add name to string table (offset 0 is reserved for empty string) - if (kStringTable.empty()) { - kStringTable.push_back('\0'); // First byte is null - } - - UInt32 strOffset = static_cast(kStringTable.size()); - - for (const char& c : name) { - kStringTable.push_back(c); - } - - kStringTable.push_back('\0'); - - // Create nlist_64 entry - nlist_64 sym{}; - sym.n_un.n_strx = strOffset; - sym.n_type = type; - sym.n_sect = sect; - sym.n_desc = 0; - sym.n_value = value; - - kSymbolTable.push_back(sym); - kSymbolOffsets[name] = value; - - return static_cast(kSymbolTable.size() - 1); -} - -/// @brief Nectar 64-bit Mach-O Linker. -/// @note This linker outputs Mach-O executables for macOS/iOS. -NECTAR_MODULE(DynamicLinker64MachO) { - CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); - - /** - * @brief parse flags and trigger options. - */ - for (size_t linker_arg{1}; linker_arg < argc; ++linker_arg) { - if (std::strcmp(argv[linker_arg], "-help") == 0) { - kLinkerSplash(); - - kConsoleOut << "-version: Show linker version.\n"; - kConsoleOut << "-help: Show linker help.\n"; - kConsoleOut << "-verbose: Enable linker trace.\n"; - kConsoleOut << "-fdylib: Output as a Dynamic Library.\n"; - kConsoleOut << "-ffat: Output as a FAT binary.\n"; - kConsoleOut << "-famd64: Output as an x86_64 Mach-O.\n"; - kConsoleOut << "-farm64: Output as an ARM64 Mach-O.\n"; - kConsoleOut << "-output: Select the output file name.\n"; - kConsoleOut << "-fstart: Specify entry point symbol.\n"; - - return NECTAR_SUCCESS; - } else if (std::strcmp(argv[linker_arg], "-version") == 0) { - kLinkerSplash(); - - return NECTAR_SUCCESS; - } else if (std::strcmp(argv[linker_arg], "-ffat") == 0) { - kFatBinaryEnable = true; - - continue; - } else if (std::strcmp(argv[linker_arg], "-famd64") == 0) { - kCpuType = CPU_TYPE_X86_64; - kCpuSubType = CPU_SUBTYPE_X86_64_ALL; - - continue; - } else if (std::strcmp(argv[linker_arg], "-farm64") == 0) { - kCpuType = CPU_TYPE_ARM64; - kCpuSubType = CPU_SUBTYPE_ARM64_ALL; - - continue; - } else if (std::strcmp(argv[linker_arg], "-fstart") == 0) { - if (argv[linker_arg + 1] == nullptr || argv[linker_arg + 1][0] == '-') continue; - - kLinkerStart = argv[linker_arg + 1]; - linker_arg += 1; - - continue; - } else if (std::strcmp(argv[linker_arg], "-verbose") == 0) { - kVerbose = true; - - continue; - } else if (std::strcmp(argv[linker_arg], "-fdylib") == 0) { - kIsDylib = true; - - if (kOutput.find(kMachOExt) != CompilerKit::STLString::npos) { - kOutput.erase(kOutput.find(kMachOExt), strlen(kMachOExt)); - kOutput += kMachODylibExt; - } - - continue; - } else if (std::strcmp(argv[linker_arg], "-output") == 0) { - if ((linker_arg + 1) > argc) continue; - - kOutput = argv[linker_arg + 1]; - ++linker_arg; - - continue; - } else { - if (argv[linker_arg][0] == '-') { - kConsoleOut << "unknown option: " << argv[linker_arg] << "\n"; - return EXIT_FAILURE; - } - - kObjectList.emplace_back(argv[linker_arg]); - - continue; - } - } - - if (kOutput.empty()) { - kConsoleOut << "no output filename set." << std::endl; - return NECTAR_EXEC_ERROR; - } else if (kObjectList.empty()) { - kConsoleOut << "no input files." << std::endl; - return NECTAR_EXEC_ERROR; - } else { - namespace FS = std::filesystem; - - // check for existing files, if they don't throw an error. - for (auto& obj : kObjectList) { - if (!FS::exists(obj)) { - kConsoleOut << "no such file: " << obj << std::endl; - return NECTAR_EXEC_ERROR; - } - } - } - - SectionInfoVec sections; - CompilerKit::Utils::AEReadableProtocol reader_protocol{}; - - entry_point_command entryCommand{}; - entryCommand.stacksize = kMachODefaultStackSz; - - // Collect all text and data from AE object files - for (const auto& objectFile : kObjectList) { - if (!std::filesystem::exists(objectFile)) continue; - - CompilerKit::AEHeader hdr{}; - - reader_protocol.fFilePtr = std::ifstream(objectFile, std::ifstream::binary); - reader_protocol.fFilePtr >> hdr; - - if (hdr.fMagic[0] == kAEMag0 && hdr.fMagic[1] == kAEMag1 && - hdr.fSize == sizeof(CompilerKit::AEHeader) && hdr.fMagic[2] == kAEMag2) { - std::size_t cnt = hdr.fCount; - - char* raw_ae_records = new char[cnt * sizeof(CompilerKit::AERecordHeader)]; - - if (!raw_ae_records) { - return NECTAR_EXEC_ERROR; - } - - std::memset(raw_ae_records, 0, cnt * sizeof(CompilerKit::AERecordHeader)); - - auto* ae_records = reader_protocol.Read(raw_ae_records, cnt); - - for (size_t ae_record_index = 0; ae_record_index < cnt; ++ae_record_index) { - SectionInfo section; - section.name = ae_records[ae_record_index].fName; - section.kind = ae_records[ae_record_index].fKind; - section.size = ae_records[ae_record_index].fSize; - - // Extract clean symbol name and add to symbol table - CompilerKit::STLString symbolName = macho_extract_symbol_name(section.name); - - if (!symbolName.empty()) { - // Determine section number (1 = __text, 2 = __data) - uint8_t sectNum = 0; - if (section.kind & CompilerKit::kPefCode) { - sectNum = 1; // __text section - } else if (section.kind & CompilerKit::kPefData) { - sectNum = 2; // __data section - } else if (section.kind & CompilerKit::kPefZero) { - sectNum = 3; // __bss section - } - - // N_EXT = external, N_SECT = defined in section - uint8_t symType = N_EXT | N_SECT; - - macho_add_symbol(symbolName, symType, sectNum, ae_records[ae_record_index].fOffset); - } - - sections.push_back(section); - } - - // Look up entry point from symbol table - auto entryIt = kSymbolOffsets.find(kLinkerStart); - - if (entryIt != kSymbolOffsets.end()) { - entryCommand.entryoff = entryIt->second; - kStartFound = true; - } - - delete[] raw_ae_records; - raw_ae_records = nullptr; - - // Read the actual code bytes - std::vector bytes; - bytes.resize(hdr.fCodeSize); - - reader_protocol.fFilePtr.seekg(std::streamsize(hdr.fStartCode)); - reader_protocol.fFilePtr.read(bytes.data(), std::streamsize(hdr.fCodeSize)); - - // Separate code and data based on section kind - for (auto& section : sections) { - if (section.kind == CompilerKit::kPefCode) { - kTextBytes.push_back({.mBlob = bytes, .mOffset = 0}); - } else if (section.kind == CompilerKit::kPefData) { - kDataBytes.push_back({.mBlob = bytes, .mOffset = 0}); - } - } - - reader_protocol.fFilePtr.close(); - continue; - } - - kConsoleOut << "not an object container: " << objectFile << std::endl; - return NECTAR_EXEC_ERROR; - } - - // Check for entry point in executables - if (!kStartFound && !kIsDylib) { - kConsoleOut << "undefined entrypoint " << kLinkerStart << " for executable: " << kOutput - << "\n"; - } - - // Calculate sizes - UInt64 textSize = 0; - UInt64 dataSize = 0; - - for (auto& blob : kTextBytes) { - textSize += blob.mBlob.size(); - } - - for (auto& blob : kDataBytes) { - dataSize += blob.mBlob.size(); - } - - // Open output file - std::ofstream output_fc(kOutput, std::ofstream::binary); - - if (output_fc.bad()) { - return NECTAR_FILE_NOT_FOUND; - } - - using namespace CompilerKit::MachO; - - UInt32 numCommands = 8; // __PAGEZERO, LC_BUILD_VERSION, __TEXT, __LINKEDIT, LC_LOAD_DYLINKER, - // LC_UUID, LC_SYMTAB, LC_DYSYMTAB - - if (!kIsDylib) { - numCommands += 1; // LC_MAIN - } - - UInt32 dataSegCmdSize = - kDataBytes.size() > 0 ? sizeof(segment_command_64) + sizeof(section_64) : 0; - - if (dataSegCmdSize > 0) ++numCommands; // __DATA segment - - UInt32 sizeOfCmds = 0; - UInt32 headerSize = sizeof(mach_header_64); - UInt32 pageZeroSize = sizeof(segment_command_64); - UInt32 textSegCmdSize = sizeof(segment_command_64) + sizeof(section_64); - UInt32 buildCmdSize = sizeof(build_version_command); - UInt32 mainCmdSize = sizeof(entry_point_command); - UInt32 uuidCmdSize = sizeof(uuid_command); - 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 - - sizeOfCmds = pageZeroSize + textSegCmdSize + dataSegCmdSize + buildCmdSize + uuidCmdSize + - symtabCmdSize + dysymtabCmdSize + linkeditCmdSize; - - if (!kIsDylib) sizeOfCmds += mainCmdSize; - - UInt64 headerAndCmdsSize = headerSize + sizeOfCmds; - UInt64 textFileOffset = AlignToPage(headerAndCmdsSize); - UInt64 textVMAddr = kDefaultBaseAddress; - UInt64 textSegmentSize = AlignToPage(textSize > 0 ? textSize : kPageSize); - UInt64 textVMSize = textFileOffset + textSegmentSize; // __TEXT includes header - - UInt64 dataFileOffset = textFileOffset + textSegmentSize; - UInt64 dataVMAddr = textVMAddr + textVMSize; - UInt64 dataSegmentSize = dataSize > 0 ? AlignToPage(dataSize) : 0; // 0 if no data - - // __LINKEDIT segment comes after data segment (or __TEXT if no data) - UInt64 linkeditFileOffset = - dataSegmentSize > 0 ? dataFileOffset + dataSegmentSize : textFileOffset + textSegmentSize; - UInt64 linkeditVMAddr = - dataSegmentSize > 0 ? dataVMAddr + dataSegmentSize : textVMAddr + textVMSize; - UInt64 symtabFileOffset = linkeditFileOffset; - UInt64 strtabFileOffset = symtabFileOffset + (kSymbolTable.size() * sizeof(nlist_64)); - UInt64 linkeditFileSize = (kSymbolTable.size() * sizeof(nlist_64)) + kStringTable.size(); - UInt64 linkeditVMSize = AlignToPage(linkeditFileSize > 0 ? linkeditFileSize : 1); - - // Write Mach-O header - mach_header_64 header{}; - - header.magic = MH_MAGIC_64; - header.cputype = kCpuType; - header.cpusubtype = kCpuSubType; - header.filetype = kIsDylib ? MH_DYLIB : MH_EXECUTE; - header.ncmds = numCommands; - header.sizeofcmds = sizeOfCmds; - header.flags = MH_NOUNDEFS | MH_DYLDLINK | MH_TWOLEVEL | MH_PIE; - header.reserved = 0; - - output_fc.write(reinterpret_cast(&header), sizeof(header)); - - segment_command_64 pageZeroSegment{}; - pageZeroSegment.cmd = LC_SEGMENT_64; - pageZeroSegment.cmdsize = sizeof(segment_command_64); - CopySegmentName(pageZeroSegment.segname, kSegmentPageZero); - pageZeroSegment.vmaddr = 0; - pageZeroSegment.vmsize = 0x100000000; - pageZeroSegment.fileoff = 0; - pageZeroSegment.filesize = 0; - pageZeroSegment.maxprot = 0; - pageZeroSegment.initprot = 0; - pageZeroSegment.nsects = 0; - pageZeroSegment.flags = 0; - - output_fc.write(reinterpret_cast(&pageZeroSegment), sizeof(pageZeroSegment)); - - build_version_command build = {.cmd = LC_BUILD_VERSION, - .cmdsize = sizeof(build_version_command), - .platform = PLATFORM_MACOS, - .minos = (kLatestOSX << 16), // macOS 11.0 - .sdk = (kLatestOSX << 16), // macOS 11.0 - .ntools = 0}; - - output_fc.write(reinterpret_cast(&build), sizeof(build)); - - // Write __TEXT segment command - segment_command_64 textSegment{}; - textSegment.cmd = LC_SEGMENT_64; - textSegment.cmdsize = sizeof(segment_command_64) + sizeof(section_64); // 1 section - CopySegmentName(textSegment.segname, kSegmentText); - textSegment.vmaddr = textVMAddr; - textSegment.vmsize = textVMSize; // Header + code (page-aligned) - textSegment.fileoff = 0; // Must include Mach-O header - textSegment.filesize = - dataSegmentSize > 0 ? dataFileOffset : linkeditFileOffset; // Extend to next segment - textSegment.maxprot = VM_PROT_READ | VM_PROT_EXECUTE; - textSegment.initprot = VM_PROT_READ | VM_PROT_EXECUTE; - textSegment.nsects = 1; - textSegment.flags = 0; - - output_fc.write(reinterpret_cast(&textSegment), sizeof(textSegment)); - - // Write __text section header - section_64 textSection{}; - CopySegmentName(textSection.sectname, kSectionText); - CopySegmentName(textSection.segname, kSegmentText); - textSection.addr = textVMAddr + textFileOffset; // Section is at offset within segment - textSection.size = textSize; - textSection.offset = static_cast(textFileOffset); - textSection.align = kSectionAlign; - textSection.reloff = 0; - textSection.nreloc = 0; - textSection.flags = S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS; - textSection.reserved1 = 0; - textSection.reserved2 = 0; - textSection.reserved3 = 0; - - output_fc.write(reinterpret_cast(&textSection), sizeof(textSection)); - - // Write __DATA segment command - segment_command_64 dataSegment{}; - dataSegment.cmd = LC_SEGMENT_64; - dataSegment.cmdsize = sizeof(segment_command_64) + sizeof(section_64); // 1 section - CopySegmentName(dataSegment.segname, kSegmentData); - dataSegment.vmaddr = dataVMAddr; - dataSegment.vmsize = dataSegmentSize; - dataSegment.fileoff = dataFileOffset; - dataSegment.filesize = dataSize; - dataSegment.maxprot = VM_PROT_READ | VM_PROT_WRITE; - dataSegment.initprot = VM_PROT_READ | VM_PROT_WRITE; - dataSegment.nsects = 1; - dataSegment.flags = 0; - - if (dataSegCmdSize > 0) - output_fc.write(reinterpret_cast(&dataSegment), sizeof(dataSegment)); - - // Write __data section header - section_64 dataSection{}; - CopySegmentName(dataSection.sectname, kSectionData); - CopySegmentName(dataSection.segname, kSegmentData); - dataSection.addr = dataVMAddr; - dataSection.size = dataSize; - dataSection.offset = static_cast(dataFileOffset); - dataSection.align = kSectionAlign; - dataSection.reloff = 0; - dataSection.nreloc = 0; - dataSection.flags = 0; - dataSection.reserved1 = 0; - dataSection.reserved2 = 0; - dataSection.reserved3 = 0; - - if (dataSegCmdSize > 0) - output_fc.write(reinterpret_cast(&dataSection), sizeof(dataSection)); - - // Write __LINKEDIT segment command (contains symbol/string tables) - segment_command_64 linkeditSegment{}; - linkeditSegment.cmd = LC_SEGMENT_64; - linkeditSegment.cmdsize = sizeof(segment_command_64); // No sections - CopySegmentName(linkeditSegment.segname, "__LINKEDIT"); - linkeditSegment.vmaddr = linkeditVMAddr; - linkeditSegment.vmsize = linkeditVMSize; - linkeditSegment.fileoff = linkeditFileOffset; - linkeditSegment.filesize = linkeditFileSize; - linkeditSegment.maxprot = VM_PROT_READ; - linkeditSegment.initprot = VM_PROT_READ; - linkeditSegment.nsects = 0; - linkeditSegment.flags = 0; - - output_fc.write(reinterpret_cast(&linkeditSegment), sizeof(linkeditSegment)); - - // Write LC_LOAD_DYLINKER command - constexpr const char* dyldPath = "/usr/lib/dyld"; - std::vector dylinkerCmd(dylinkerCmdSize, 0); - dylinker_command* dylinker = reinterpret_cast(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()); - - // Write LC_MAIN entry point command (executables only) - if (!kIsDylib) { - entryCommand.cmd = LC_MAIN; - entryCommand.cmdsize = sizeof(entry_point_command); - // entryoff is relative to __TEXT segment file offset - entryCommand.entryoff = textFileOffset + entryCommand.entryoff; - - output_fc.write(reinterpret_cast(&entryCommand), sizeof(entryCommand)); - } - - // Write LC_UUID command - uuid_command uuidCmd{}; - uuidCmd.cmd = LC_UUID; - uuidCmd.cmdsize = sizeof(uuid_command); - - // Generate a random UUID (version 4) - std::random_device rd; - std::mt19937 gen(rd()); - uuids::uuid_random_generator uuidGen(gen); - uuids::uuid generatedUuid = uuidGen(); - auto uuidBytes = generatedUuid.as_bytes(); - std::memcpy(uuidCmd.uuid, uuidBytes.data(), 16); - - output_fc.write(reinterpret_cast(&uuidCmd), sizeof(uuidCmd)); - - // Write LC_SYMTAB command - symtab_command symtabCmd{}; - symtabCmd.cmd = LC_SYMTAB; - symtabCmd.cmdsize = sizeof(symtab_command); - symtabCmd.symoff = static_cast(symtabFileOffset); - symtabCmd.nsyms = static_cast(kSymbolTable.size()); - symtabCmd.stroff = static_cast(strtabFileOffset); - symtabCmd.strsize = static_cast(kStringTable.size()); - - output_fc.write(reinterpret_cast(&symtabCmd), sizeof(symtabCmd)); - - // Write LC_DYSYMTAB command - dysymtab_command dysymtabCmd{}; - std::memset(&dysymtabCmd, 0, sizeof(dysymtabCmd)); - dysymtabCmd.cmd = LC_DYSYMTAB; - dysymtabCmd.cmdsize = sizeof(dysymtab_command); - - // All symbols are local for now - dysymtabCmd.ilocalsym = 0; - dysymtabCmd.nlocalsym = static_cast(kSymbolTable.size()); - - // External symbols start after locals - dysymtabCmd.iextdefsym = static_cast(kSymbolTable.size()); - dysymtabCmd.nextdefsym = 0; - - // Undefined symbols - dysymtabCmd.iundefsym = static_cast(kSymbolTable.size()); - dysymtabCmd.nundefsym = 0; - - output_fc.write(reinterpret_cast(&dysymtabCmd), sizeof(dysymtabCmd)); - - // Pad to text section offset - UInt64 currentPos = output_fc.tellp(); - UInt64 padding = textFileOffset - currentPos; - - if (padding > 0) { - std::vector zeros(padding, 0); - output_fc.write(zeros.data(), zeros.size()); - } - - // Write __text content - for (auto& blob : kTextBytes) { - output_fc.write(blob.mBlob.data(), blob.mBlob.size()); - } - - // Pad to data section offset - currentPos = output_fc.tellp(); - padding = dataFileOffset - currentPos; - - if (padding > 0) { - std::vector zeros(padding, 0); - output_fc.write(zeros.data(), zeros.size()); - } - - // Write __data content - for (auto& blob : kDataBytes) { - output_fc.write(blob.mBlob.data(), blob.mBlob.size()); - } - - // Pad to symbol table offset - currentPos = output_fc.tellp(); - padding = symtabFileOffset - currentPos; - - if (padding > 0) { - std::vector zeros(padding, 0); - output_fc.write(zeros.data(), zeros.size()); - } - - // Write symbol table (nlist_64 entries) - for (auto& sym : kSymbolTable) { - output_fc.write(reinterpret_cast(&sym), sizeof(nlist_64)); - } - - - // Write string table - output_fc.write(kStringTable.data(), kStringTable.size()); - - output_fc.flush(); - - return NECTAR_SUCCESS; -} - -// Last rev - 2026 - -#endif // ifdef CK_USE_MACHO_LINKER diff --git a/src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cpp b/src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cpp deleted file mode 100644 index 34333a3..0000000 --- a/src/CompilerKit/src/Linkers/DynamicLinker64+PEF.cpp +++ /dev/null @@ -1,677 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// 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 - -/// @author Amlal El Mahrouss (amlal@nekernel.org) -/// @brief NeKernel.org 64-bit PEF Linker. -/// @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. - -#include -#include -#include -#include -#include -#include -#include - -#define kLinkerPefNoCpu (0U) -#define kLinkerPefNoSubCpu (0U) -#define kLinkerPefDefaultOutput {"a" kPefExt} - -#define kLinkerVersionStr "Nectar 64-Bit Linker (NeKernel PEF)" - -#define kLinkerDefaultOrigin kPefBaseOrigin - -#define kLinkerId (0x5046FF) - -#define kLinkerAbiContainer "__PEFContainer:ABI:" -#define kLinkerGuidContainer "__PEFContainer:GUID:" -#define kEndContainer "__PEFContainer:END:" - -#define kLinkerSplash() kStdOut << kLinkerVersionStr << kStdEndl - -/// @brief PEF stack size symbol. -#define kLinkerStackSizeSymbol "__PEFSizeOfReserveStack" - -#define kConsoleOut \ - (std::cout << "\e[0;31m" \ - << "ld64: " \ - << "\e[0;97m") - -enum struct ABIType : Int32 { - kABITypeNull = 0, - kABITypeStart = 0x1010, /* The start of ABI list. */ - kABITypeNE = 0x5046, /* PF (NeKernel.org's PEF ABI) */ - kABITypeEnd = kABITypeNull, - kABITypeInvalid = 0xFFFF, -}; - -static CompilerKit::STLString kOutput = kLinkerPefDefaultOutput; -static ABIType kAbi = ABIType::kABITypeNE; -static Int32 kSubArch = kLinkerPefNoSubCpu; -static Int32 kArch = CompilerKit::kPefArchInvalid; -static bool kFatBinaryEnable = false; -static bool kStartFound = false; -static bool kDuplicateSymbols = false; - -/* ld64 is to be found, mld is to be found at runtime. */ -static const char* kLinkerDefineSymbol = ":UndefinedSymbol:"; -static const char* kLinkerDynamicSym = ":RuntimeSymbol:"; - -static CompilerKit::STLString kLinkerStart = kPefStart; - -/* object code and list. */ -static std::vector kObjectList; -static std::vector kObjectBytes; - -/// @brief Nectar 64-bit Linker. -/// @note This linker is made for PEF executable, thus Nectar based OSes. -NECTAR_MODULE(DynamicLinker64PEF) { - bool is_executable = true; - - CompilerKit::install_signal(SIGSEGV, CompilerKit::Detail::drvi_crash_handler); - - /** - * @brief parse flags and trigger options. - */ - for (size_t linker_arg = 1; linker_arg < argc; ++linker_arg) { - if (std::strcmp(argv[linker_arg], "-help") == 0) { - kLinkerSplash(); - - kConsoleOut << "-version: Show linker version.\n"; - kConsoleOut << "-help: Show linker help.\n"; - kConsoleOut << "-verbose: Enable linker trace.\n"; - kConsoleOut << "-fdylib: Output as a Dynamic PEF.\n"; - kConsoleOut << "-ffat: Output as a FAT PEF.\n"; - kConsoleOut << "-f32k: Output as a 32x0 PEF.\n"; - kConsoleOut << "-f64k: Output as a 64x0 PEF.\n"; - kConsoleOut << "-famd64: Output as a AMD64 PEF.\n"; - kConsoleOut << "-frv64: Output as a RISC-V PEF.\n"; - kConsoleOut << "-fpower64: Output as a POWER PEF.\n"; - kConsoleOut << "-farm64: Output as a ARM64 PEF.\n"; - kConsoleOut << "-output: Select the output file name.\n"; - - return NECTAR_SUCCESS; - } else if (std::strcmp(argv[linker_arg], "-version") == 0) { - kLinkerSplash(); - - return NECTAR_SUCCESS; - } else if (std::strcmp(argv[linker_arg], "-ffat") == 0) { - kFatBinaryEnable = true; - - continue; - } else if (std::strcmp(argv[linker_arg], "-f64k") == 0) { - kArch = CompilerKit::kPefArch64000; - - continue; - } else if (std::strcmp(argv[linker_arg], "-famd64") == 0) { - kArch = CompilerKit::kPefArchAMD64; - - continue; - } else if (std::strcmp(argv[linker_arg], "-fstart") == 0) { - if (argv[linker_arg + 1] == nullptr || argv[linker_arg + 1][0] == '-') continue; - - kLinkerStart = argv[linker_arg + 1]; - linker_arg += 1; - - continue; - } else if (std::strcmp(argv[linker_arg], "-f32k") == 0) { - kArch = CompilerKit::kPefArch32000; - - continue; - } else if (std::strcmp(argv[linker_arg], "-fpower64") == 0) { - kArch = CompilerKit::kPefArchPowerPC; - - continue; - } else if (std::strcmp(argv[linker_arg], "-friscv64") == 0) { - kArch = CompilerKit::kPefArchRISCV; - - continue; - } else if (std::strcmp(argv[linker_arg], "-farm64") == 0) { - kArch = CompilerKit::kPefArchARM64; - - continue; - } else if (std::strcmp(argv[linker_arg], "-verbose") == 0) { - kVerbose = true; - - continue; - } else if (std::strcmp(argv[linker_arg], "-fdylib") == 0) { - if (kOutput.empty()) { - continue; - } - - if (kOutput.find(kPefExt) != CompilerKit::STLString::npos) - kOutput.erase(kOutput.find(kPefExt), strlen(kPefExt)); - - kOutput += kPefDylibExt; - is_executable = false; - - continue; - } else if (std::strcmp(argv[linker_arg], "-output") == 0) { - if ((linker_arg + 1) > argc) continue; - - kOutput = argv[linker_arg + 1]; - ++linker_arg; - - continue; - } else { - if (argv[linker_arg][0] == '-') { - kConsoleOut << "unknown option: " << argv[linker_arg] << "\n"; - return EXIT_FAILURE; - } - - kObjectList.emplace_back(argv[linker_arg]); - - continue; - } - } - - if (kOutput.empty()) { - kConsoleOut << "no output filename set." << std::endl; - return NECTAR_EXEC_ERROR; - } else if (kObjectList.empty()) { - kConsoleOut << "no input files." << std::endl; - return NECTAR_EXEC_ERROR; - } else { - namespace FS = std::filesystem; - - // check for existing files, if they don't throw an error. - for (auto& obj : kObjectList) { - if (!FS::exists(obj)) { - // if filesystem doesn't find file - // -> throw error. - kConsoleOut << "no such file: " << obj << std::endl; - return NECTAR_EXEC_ERROR; - } - } - } - - // 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 NECTAR_EXEC_ERROR; - } - - CompilerKit::PEFContainer pef_container{}; - - int32_t archs = kArch; - - pef_container.Count = 0UL; - pef_container.Kind = is_executable ? CompilerKit::kPefKindExec : CompilerKit::kPefKindDylib; - pef_container.SubCpu = kSubArch; - pef_container.Linker = kLinkerId; // Nectar Linker - pef_container.Abi = static_cast(kAbi); // Multi-Processor UX ABI - pef_container.Magic[0] = kPefMagic[kFatBinaryEnable ? 2 : 0]; - pef_container.Magic[1] = kPefMagic[1]; - pef_container.Magic[2] = kPefMagic[kFatBinaryEnable ? 0 : 2]; - pef_container.Magic[3] = kPefMagic[3]; - pef_container.Version = kPefVersion; - - // specify the start address, can be 0x10000 - pef_container.Start = kLinkerDefaultOrigin; - pef_container.HdrSz = sizeof(CompilerKit::PEFContainer); - pef_container.Checksum = 0UL; - - std::ofstream output_fc(kOutput, std::ofstream::binary); - - if (output_fc.bad()) { - if (kVerbose) { - kConsoleOut << "error: " << strerror(errno) << "\n"; - } - - return NECTAR_FILE_NOT_FOUND; - } - - //! Read AE to convert as PEF. - - std::vector command_headers; - CompilerKit::Utils::AEReadableProtocol reader_protocol{}; - - for (const auto& objectFile : kObjectList) { - if (!std::filesystem::exists(objectFile)) continue; - - CompilerKit::AEHeader hdr{}; - - reader_protocol.fFilePtr = std::ifstream(objectFile, std::ifstream::binary); - reader_protocol.fFilePtr >> hdr; - - if (hdr.fMagic[0] == kAEMag0 && hdr.fMagic[1] == kAEMag1 && - hdr.fSize == sizeof(CompilerKit::AEHeader) && hdr.fMagic[2] == kAEMag2) { - if (hdr.fArch != kArch && hdr.fVersion == kAEIdentVersion) { - if (kVerbose) kConsoleOut << "is this a FAT binary? : "; - - if (!kFatBinaryEnable) { - if (kVerbose) kConsoleOut << "not a FAT binary.\n"; - - kConsoleOut << "object " << objectFile - << " is a different kind of architecture and output isn't " - "treated as a FAT binary." - << std::endl; - - return NECTAR_FAT_ERROR; - } else { - if (kVerbose) { - kConsoleOut << "Architecture matches what we expect.\n"; - } - } - } - - // append arch type to archs varaible. - archs |= hdr.fArch; - std::size_t cnt = hdr.fCount; - - if (kVerbose) kConsoleOut << "header found, record count: " << cnt << "\n"; - - pef_container.Count = cnt; - - char* raw_ae_records = new char[cnt * sizeof(CompilerKit::AERecordHeader)]; - - if (!raw_ae_records) { - if (kVerbose) kConsoleOut << "allocation failed for records of count: " << cnt << "\n"; - } - - std::memset(raw_ae_records, 0, cnt * sizeof(CompilerKit::AERecordHeader)); - - auto* ae_records = reader_protocol.Read(raw_ae_records, cnt); - - size_t org = kLinkerDefaultOrigin; - - for (size_t ae_record_index = 0; ae_record_index < cnt; ++ae_record_index) { - CompilerKit::PEFCommandHeader command_header{{}}; - std::size_t offset_of_obj = ae_records[ae_record_index].fOffset; - - std::memcpy(command_header.Name, ae_records[ae_record_index].fName, kPefNameLen); - - CompilerKit::STLString cmd_hdr_name(command_header.Name); - - // check this header if it's any valid. - if (cmd_hdr_name.find(kPefCode64) == CompilerKit::STLString::npos && - cmd_hdr_name.find(kPefData64) == CompilerKit::STLString::npos && - cmd_hdr_name.find(kPefZero64) == CompilerKit::STLString::npos) { - if (cmd_hdr_name.find(kLinkerStart) == CompilerKit::STLString::npos && - *command_header.Name == 0) { - if (cmd_hdr_name.find(kLinkerDefineSymbol) != CompilerKit::STLString::npos) { - goto ld_mark_header; - } else { - continue; - } - } - } - - if (cmd_hdr_name.find(kLinkerStart) != CompilerKit::STLString::npos && - cmd_hdr_name.find(kPefCode64) != CompilerKit::STLString::npos) { - kStartFound = true; - } - - ld_mark_header: - command_header.Offset = offset_of_obj; - command_header.Kind = ae_records[ae_record_index].fKind; - command_header.VirtualSize = ae_records[ae_record_index].fSize; - command_header.Cpu = hdr.fArch; - command_header.VirtualAddress = org; - command_header.SubCpu = hdr.fSubArch; - command_header.OffsetSize = ae_records[ae_record_index].fSize; - - org += command_header.VirtualSize; - - if (kVerbose) { - kConsoleOut << "Record: " << ae_records[ae_record_index].fName << " is marked.\n"; - kConsoleOut << "Offset: " << command_header.Offset << "\n"; - } - - command_headers.emplace_back(command_header); - } - - delete[] raw_ae_records; - raw_ae_records = nullptr; - - std::vector bytes; - bytes.resize(hdr.fCodeSize); - - reader_protocol.fFilePtr.seekg(std::streamsize(hdr.fStartCode)); - reader_protocol.fFilePtr.read(bytes.data(), std::streamsize(hdr.fCodeSize)); - - kObjectBytes.push_back({.mBlob = bytes, .mOffset = hdr.fStartCode}); - - // Blob was written, close fp. - - reader_protocol.fFilePtr.close(); - - continue; - } - - kConsoleOut << "not an object container: " << objectFile << std::endl; - - // don't continue, it is a fatal error. - return NECTAR_EXEC_ERROR; - } - - pef_container.Cpu = archs; - - output_fc << pef_container; - - if (kVerbose) { - kConsoleOut << "wrote container to: " << output_fc.tellp() << ".\n"; - } - - output_fc.seekp(std::streamsize(pef_container.HdrSz)); - - std::vector not_found; - std::vector symbols; - - // step 2: check for errors (multiple symbols, undefined ones) - - CompilerKit::STLString cmd_hdr_name; - - for (auto& command_hdr : command_headers) { - // check if this symbol needs to be resolved. - if (CompilerKit::STLString(command_hdr.Name).find(kLinkerDefineSymbol) != - CompilerKit::STLString::npos && - CompilerKit::STLString(command_hdr.Name).find(kLinkerDynamicSym) == - CompilerKit::STLString::npos) { - if (kVerbose) kConsoleOut << "Found undefined symbol: " << command_hdr.Name << "\n"; - - cmd_hdr_name = command_hdr.Name; - - if (auto it = std::find(not_found.begin(), not_found.end(), cmd_hdr_name); - it == not_found.end()) { - not_found.emplace_back(cmd_hdr_name); - } - } - - symbols.emplace_back(command_hdr.Name); - } - - // Now try to solve these symbols. - - for (size_t not_found_idx = 0; not_found_idx < command_headers.size(); ++not_found_idx) { - if (const auto it = std::find(not_found.begin(), not_found.end(), - CompilerKit::STLString(command_headers[not_found_idx].Name)); - it != not_found.end()) { - CompilerKit::STLString symbol_imp = *it; - - if (symbol_imp.find(kLinkerDefineSymbol) == CompilerKit::STLString::npos) continue; - - // erase the lookup prefix. - symbol_imp.erase(0, symbol_imp.find(kLinkerDefineSymbol) + strlen(kLinkerDefineSymbol)); - - // demangle everything. - while (symbol_imp.find('$') != CompilerKit::STLString::npos) - symbol_imp.erase(symbol_imp.find('$'), 1); - - // the reason we do is because, this may not match the symbol, and we need - // to look for other matching symbols. - for (auto& command_hdr : command_headers) { - if (CompilerKit::STLString(command_hdr.Name).find(symbol_imp) != - CompilerKit::STLString::npos && - CompilerKit::STLString(command_hdr.Name).find(kLinkerDefineSymbol) == - CompilerKit::STLString::npos) { - CompilerKit::STLString undefined_symbol = command_hdr.Name; - auto result_of_sym = undefined_symbol.substr(undefined_symbol.find(symbol_imp)); - - for (int i = 0; result_of_sym[i] != 0; ++i) { - if (result_of_sym[i] != symbol_imp[i]) goto ld_continue_search; - } - - not_found.erase(it); - - if (kVerbose) kConsoleOut << "Found symbol: " << command_hdr.Name << "\n"; - - break; - } - } - - ld_continue_search: - continue; - } - } - - // step 3: check for errors (recheck if we have those symbols.) - - if (!kStartFound && is_executable) { - if (kVerbose) - kConsoleOut << "Undefined entrypoint: " << kLinkerStart - << ", you may have forget to link " - "against your runtime library.\n"; - - kConsoleOut << "Undefined entrypoint " << kLinkerStart << " for executable: " << kOutput - << "\n"; - } - - // step 4: write all PEF commands. - - CompilerKit::PEFCommandHeader date_cmd_hdr{}; - - time_t timestamp = time(nullptr); - - CompilerKit::STLString timeStampStr = "__PEFContainer:BuildEpoch:"; - timeStampStr += std::to_string(timestamp); - - strncpy(date_cmd_hdr.Name, timeStampStr.c_str(), timeStampStr.size()); - - date_cmd_hdr.Flags = 0; - date_cmd_hdr.Kind = CompilerKit::kPefZero; - date_cmd_hdr.Offset = output_fc.tellp(); - date_cmd_hdr.VirtualSize = timeStampStr.size(); - - command_headers.push_back(std::move(date_cmd_hdr)); - - CompilerKit::PEFCommandHeader abi_cmd_hdr{}; - - CompilerKit::STLString abi = kLinkerAbiContainer; - - switch (kArch) { - case CompilerKit::kPefArchAMD64: { - abi += "MSFT"; - break; - } - case CompilerKit::kPefArchPowerPC: - case CompilerKit::kPefArchARM64: - case CompilerKit::kPefArchRISCV: - case CompilerKit::kPefArch32000: - case CompilerKit::kPefArch64000: { - abi += "KRNL"; - break; - } - default: { - abi += "NOOP"; - break; - } - } - - std::memcpy(abi_cmd_hdr.Name, abi.c_str(), abi.size()); - - abi_cmd_hdr.VirtualSize = abi.size(); - abi_cmd_hdr.Offset = output_fc.tellp(); - abi_cmd_hdr.Flags = 0; - abi_cmd_hdr.Kind = CompilerKit::kPefLinkerID; - - command_headers.push_back(std::move(abi_cmd_hdr)); - - CompilerKit::PEFCommandHeader stack_cmd_hdr{{}}; - - stack_cmd_hdr.Cpu = kArch; - stack_cmd_hdr.Flags = 0; - stack_cmd_hdr.VirtualSize = sizeof(uintptr_t); - stack_cmd_hdr.Offset = 0; - - std::memcpy(stack_cmd_hdr.Name, kLinkerStackSizeSymbol, strlen(kLinkerStackSizeSymbol)); - - command_headers.push_back(std::move(stack_cmd_hdr)); - - CompilerKit::PEFCommandHeader uuid_cmd_hdr{}; - - std::random_device rd; - - auto seedData = std::array{}; - std::generate(std::begin(seedData), std::end(seedData), std::ref(rd)); - std::seed_seq seq(std::begin(seedData), std::end(seedData)); - std::mt19937 generator(seq); - - auto gen = uuids::uuid_random_generator{generator}; - uuids::uuid id = gen(); - auto uuidStr = uuids::to_string(id); - - std::memcpy(uuid_cmd_hdr.Name, kLinkerGuidContainer, strlen(kLinkerGuidContainer)); - std::memcpy(uuid_cmd_hdr.Name + strlen(kLinkerGuidContainer), uuidStr.c_str(), uuidStr.size()); - - uuid_cmd_hdr.VirtualSize = strlen(uuid_cmd_hdr.Name); - uuid_cmd_hdr.Offset = output_fc.tellp(); - uuid_cmd_hdr.Flags = CompilerKit::kPefLinkerID; - uuid_cmd_hdr.Kind = CompilerKit::kPefZero; - - command_headers.push_back(std::move(uuid_cmd_hdr)); - - // prepare a symbol vector. - std::vector undef_symbols; - std::vector dupl_symbols; - std::vector resolve_symbols; - - constexpr Int32 kPaddingOffset = 16; - - size_t previous_offset = - (command_headers.size() * sizeof(CompilerKit::PEFCommandHeader)) + kPaddingOffset; - - CompilerKit::PEFCommandHeader end_exec_hdr; - - end_exec_hdr.Offset = output_fc.tellp(); - end_exec_hdr.Flags = CompilerKit::kPefLinkerID; - end_exec_hdr.Kind = CompilerKit::kPefZero; - - std::memcpy(end_exec_hdr.Name, kEndContainer, strlen(kEndContainer)); - - end_exec_hdr.VirtualSize = strlen(end_exec_hdr.Name); - - command_headers.push_back(std::move(end_exec_hdr)); - - // Finally write down the command headers. - // And check for any duplications - for (size_t commandHeaderIndex = 0UL; commandHeaderIndex < command_headers.size(); - ++commandHeaderIndex) { - if (CompilerKit::STLString(command_headers[commandHeaderIndex].Name) - .find(kLinkerDefineSymbol) != CompilerKit::STLString::npos && - CompilerKit::STLString(command_headers[commandHeaderIndex].Name).find(kLinkerDynamicSym) == - CompilerKit::STLString::npos) { - // ignore :UndefinedSymbol: headers, they do not contain code. - continue; - } - - CompilerKit::STLString symbol_name = command_headers[commandHeaderIndex].Name; - - if (!symbol_name.empty()) { - undef_symbols.emplace_back(symbol_name); - } - - command_headers[commandHeaderIndex].Offset += previous_offset; - previous_offset += command_headers[commandHeaderIndex].VirtualSize; - - CompilerKit::STLString name = command_headers[commandHeaderIndex].Name; - - /// so this is valid when we get to the entrypoint. - /// it is always a code64 container. And should equal to kLinkerStart as well. - /// this chunk of code updates the pef_container.Start with the updated offset. - if (name.find(kLinkerStart) != CompilerKit::STLString::npos && - name.find(kPefCode64) != CompilerKit::STLString::npos) { - pef_container.Start = command_headers[commandHeaderIndex].Offset; - auto tellCurPos = output_fc.tellp(); - - output_fc.seekp(0); - output_fc << pef_container; - - output_fc.seekp(tellCurPos); - } - - if (kVerbose) { - kConsoleOut << "Command name: " << name << "\n"; - kConsoleOut << "Virtual address of command content: " - << command_headers[commandHeaderIndex].Offset << "\n"; - } - - output_fc << command_headers[commandHeaderIndex]; - - for (size_t sub_command_header_index = 0UL; sub_command_header_index < command_headers.size(); - ++sub_command_header_index) { - if (sub_command_header_index == commandHeaderIndex) continue; - - if (CompilerKit::STLString(command_headers[sub_command_header_index].Name) - .find(kLinkerDefineSymbol) != CompilerKit::STLString::npos && - CompilerKit::STLString(command_headers[sub_command_header_index].Name) - .find(kLinkerDynamicSym) == CompilerKit::STLString::npos) { - if (kVerbose) { - kConsoleOut << "Ignoring :UndefinedSymbol: headers...\n"; - } - - // Let's ignore :UndefinedSymbol: headers, they do not contain code. - continue; - } - - auto& command_hdr = command_headers[sub_command_header_index]; - - if (command_hdr.Name == CompilerKit::STLString(command_headers[commandHeaderIndex].Name)) { - if (std::find(dupl_symbols.cbegin(), dupl_symbols.cend(), command_hdr.Name) == - dupl_symbols.cend()) { - dupl_symbols.emplace_back(command_hdr.Name); - } - - if (kVerbose) kConsoleOut << "Found duplicate symbols of: " << command_hdr.Name << "\n"; - - kDuplicateSymbols = true; - } - } - } - - if (!dupl_symbols.empty()) { - for (auto& symbol : dupl_symbols) { - kConsoleOut << "Multiple symbols of: " << symbol << " detected, cannot continue.\n"; - } - - return NECTAR_EXEC_ERROR; - } - - // step 2.5: write program bytes. - - for (auto& struct_of_blob : kObjectBytes) { - output_fc.write(struct_of_blob.mBlob.data(), struct_of_blob.mBlob.size()); - } - - if (kVerbose) { - kConsoleOut << "Wrote contents of: " << kOutput << "\n"; - } - - // step 3: check if we have those symbols - - std::vector unreferenced_symbols; - - for (auto& command_hdr : command_headers) { - if (auto it = - std::find(not_found.begin(), not_found.end(), CompilerKit::STLString(command_hdr.Name)); - it != not_found.end()) { - unreferenced_symbols.emplace_back(command_hdr.Name); - } - } - - if (!unreferenced_symbols.empty()) { - for (auto& unreferenced_symbol : unreferenced_symbols) { - kConsoleOut << "Undefined symbol " << unreferenced_symbol << "\n"; - } - - return NECTAR_EXEC_ERROR; - } - - if ((!kStartFound || kDuplicateSymbols) && - (std::filesystem::exists(kOutput) || !unreferenced_symbols.empty())) { - if (kVerbose) { - kConsoleOut << "File: " << kOutput << " is corrupt now...\n"; - } - - return NECTAR_EXEC_ERROR; - } - - return NECTAR_SUCCESS; -} - -// Last rev 2026 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 +#include +#include +#include +#include + +/* 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/Preprocess/Preprocessor+Generic.cpp b/src/CompilerKit/src/Preprocess/Preprocessor+Generic.cpp new file mode 100644 index 0000000..e2d20df --- /dev/null +++ b/src/CompilerKit/src/Preprocess/Preprocessor+Generic.cpp @@ -0,0 +1,918 @@ +// SPDX-License-Identifier: Apache-2.0 +// 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 + +/// BUGS: 0 + +#include +#include +#include +#include +#include +#include +#include +#include + +#define kMacroPrefix '#' + +/// @author Amlal El Mahrouss (amlal@nekernel.org) +/// @file Preprocessor+Generic.cpp +/// @brief Nectar Preprocessor. + +using pp_parser_fn_t = Int32 (*)(CompilerKit::STLString&, std::ifstream&, + std::ofstream&); + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Preprocessor internal types. + +///////////////////////////////////////////////////////////////////////////////////////// + +namespace Detail { +enum PPOperatorType : Int32 { + kInvalid = 0, + kEqual = 100, + kGreaterEqThan, + kLesserEqThan, + kGreaterThan, + kLesserThan, + kNotEqual, + kCount = 6, +}; + +struct pp_macro_condition final { + int32_t fType; + CompilerKit::STLString fTypeName; + + void Print() { + std::cout << "type: " << fType << "\n"; + std::cout << "type_name: " << fTypeName << "\n"; + } +}; + +struct pp_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 pp_parse_if_condition +// @brief parse #if condition + +///////////////////////////////////////////////////////////////////////////////////////// + +int32_t pp_parse_if_condition(Detail::pp_macro_condition& cond, Detail::pp_macro& macro, + bool& inactive_code, bool& defined, + CompilerKit::STLString& macro_str) { + if (cond.fType == Detail::PPOperatorType::kEqual) { + auto pos = macro_str.find(macro.fName); + if (pos == CompilerKit::STLString::npos) return 0; + + auto substr_macro = macro_str.substr(pos + 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 pos = macro_str.find(macro.fName); + if (pos == CompilerKit::STLString::npos) return 0; + + auto substr_macro = macro_str.substr(pos + 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 pos = macro_str.find(macro.fName); + if (pos == CompilerKit::STLString::npos) return 0; + + auto substr_macro = macro_str.substr(pos + 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.fValue.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::PPOperatorType::kGreaterThan) { + if (lhs > rhs) { + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + if (cond.fType == Detail::PPOperatorType::kGreaterEqThan) { + if (lhs >= rhs) { + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + if (cond.fType == Detail::PPOperatorType::kLesserEqThan) { + if (lhs <= rhs) { + defined = true; + inactive_code = false; + + return 1; + } + + return 0; + } + + if (cond.fType == Detail::PPOperatorType::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 pp_parse_file +// @brief parse file to preprocess it. + +///////////////////////////////////////////////////////////////////////////////////////// + +void pp_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] == '#' && + 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] == '#' && 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::ast_find_needle(hdr_line, macro.fName)) { + auto macro_pos = hdr_line.find(macro.fName); + if (macro_pos == CompilerKit::STLString::npos) continue; + + if (hdr_line.substr(macro_pos).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 ) + + auto paren_pos = hdr_line.find(")"); + if (paren_pos != CompilerKit::STLString::npos) { + hdr_line.erase(paren_pos, 1); + } + + auto macro_call_pos = hdr_line.find(macro.fName + '('); + if (macro_call_pos != CompilerKit::STLString::npos) { + hdr_line.replace(macro_call_pos, len, symbol_val); + } + } else { + auto value = macro.fValue; + + hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), value); + } + } + } + } + + if (hdr_line[0] == '#' && hdr_line.find("define") != CompilerKit::STLString::npos) { + auto define_pos = hdr_line.find("define"); + if (define_pos == CompilerKit::STLString::npos) continue; + + auto line_after_define = hdr_line.substr(define_pos + 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::pp_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 ifndef_pos = hdr_line.find("ifndef"); + if (ifndef_pos == CompilerKit::STLString::npos) continue; + + auto line_after_ifndef = hdr_line.substr(ifndef_pos + 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] == '#' && + 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] == '#' && + hdr_line.find("ifdef") != CompilerKit::STLString::npos) { + auto ifdef_pos = hdr_line.find("ifdef"); + if (ifdef_pos == CompilerKit::STLString::npos) continue; + + auto line_after_ifdef = hdr_line.substr(ifdef_pos + 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] == '#' && + hdr_line.find("if") != CompilerKit::STLString::npos) { + inactive_code = true; + + std::vector pp_macro_condition_list = { + { + .fType = Detail::PPOperatorType::kEqual, + .fTypeName = "==", + }, + { + .fType = Detail::PPOperatorType::kNotEqual, + .fTypeName = "!=", + }, + { + .fType = Detail::PPOperatorType::kLesserThan, + .fTypeName = "<", + }, + { + .fType = Detail::PPOperatorType::kGreaterThan, + .fTypeName = ">", + }, + { + .fType = Detail::PPOperatorType::kLesserEqThan, + .fTypeName = "<=", + }, + { + .fType = Detail::PPOperatorType::kGreaterEqThan, + .fTypeName = ">=", + }, + }; + + int32_t good_to_go = 0; + + for (auto& macro_condition : pp_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 = pp_parse_if_condition(macro_condition, found_macro, inactive_code, + defined, hdr_line); + + break; + } + } + } + } + + if (good_to_go) continue; + + auto if_pos = hdr_line.find("if"); + if (if_pos == CompilerKit::STLString::npos) continue; + + auto line_after_if = hdr_line.substr(if_pos + 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] == '#' && + hdr_line.find("warning") != CompilerKit::STLString::npos) { + auto warning_pos = hdr_line.find("warning"); + if (warning_pos == CompilerKit::STLString::npos) continue; + + auto line_after_warning = hdr_line.substr(warning_pos + strlen("warning") + 1); + CompilerKit::STLString message; + + for (auto& ch : line_after_warning) { + if (ch == '\r' || ch == '\n') { + break; + } + + message += ch; + } + + std::cerr << "warn: " << message << std::endl; + } 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; + + auto line_after_warning = hdr_line.substr(error_pos + 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] == '#' && + hdr_line.find("include ") != CompilerKit::STLString::npos) { + auto include_pos = hdr_line.find("include "); + if (include_pos == CompilerKit::STLString::npos) continue; + + line_after_include = hdr_line.substr(include_pos + 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; + + pp_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); + + pp_parse_file(header, pp_out); + } + } else { + std::cerr << ("cppdrv: unknown pre-processor directive, " + hdr_line) << "\n"; + continue; + } + } + } catch (const std::out_of_range& oor) { + return; + } +} + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief main entrypoint of app. + +///////////////////////////////////////////////////////////////////////////////////////// + +NECTAR_MODULE(GenericPreprocessorMain) { + try { + bool skip = false; + bool double_skip = false; + + Detail::pp_macro macro_1; + + macro_1.fName = "__true"; + macro_1.fValue = "1"; + + kMacros.push_back(macro_1); + + Detail::pp_macro macro_unreachable; + + macro_unreachable.fName = "__unreachable"; + macro_unreachable.fValue = "__compilerkit_unreachable"; + + kMacros.push_back(macro_unreachable); + + Detail::pp_macro macro_unused; + + macro_unused.fName = "__unused"; + macro_unused.fValue = "__compilerkit_unused"; + + kMacros.push_back(macro_unused); + + Detail::pp_macro macro_0; + + macro_0.fName = "__false"; + macro_0.fValue = "0"; + + kMacros.push_back(macro_0); + + Detail::pp_macro macro_nectar; + + macro_nectar.fName = "__NECTAR__"; + macro_nectar.fValue = "1"; + + kMacros.push_back(macro_nectar); + + Detail::pp_macro macro_lang; + + macro_lang.fName = "__ncpp"; + macro_lang.fValue = "202601L"; + + kMacros.push_back(macro_lang); + + Detail::pp_macro macro_nil; + + macro_nil.fName = "nil"; + macro_nil.fValue = "0"; + + kMacros.push_back(macro_nil); + + 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", + "Nectar Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2026 all rights " + "reserved."); + + return NECTAR_SUCCESS; + } + + if (strcmp(argv[index], "-cpp-help") == 0) { + printf("%s\n", + "Nectar Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2026 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 NECTAR_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 + 2]); ++argv_find_len) { + if (!isdigit(argv[index + 2][argv_find_len])) { + is_string = true; + macro_value += "\""; + + break; + } + } + + macro_value += argv[index + 2]; + + if (is_string) macro_value += "\""; + + Detail::pp_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 NECTAR_EXEC_ERROR; + + for (auto& file : kFiles) { + if (!std::filesystem::exists(file)) continue; + + std::ifstream file_descriptor(file); + std::ofstream file_descriptor_pp(file + ".pp"); + + pp_parse_file(file_descriptor, file_descriptor_pp); + } + + return NECTAR_SUCCESS; + } catch (const std::runtime_error& e) { + std::cout << e.what() << '\n'; + } + + return NECTAR_EXEC_ERROR; +} + +// Last rev 8-1-24 diff --git a/src/CompilerKit/src/Preprocessors/Preprocessor+Generic.cpp b/src/CompilerKit/src/Preprocessors/Preprocessor+Generic.cpp deleted file mode 100644 index e2d20df..0000000 --- a/src/CompilerKit/src/Preprocessors/Preprocessor+Generic.cpp +++ /dev/null @@ -1,918 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// 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 - -/// BUGS: 0 - -#include -#include -#include -#include -#include -#include -#include -#include - -#define kMacroPrefix '#' - -/// @author Amlal El Mahrouss (amlal@nekernel.org) -/// @file Preprocessor+Generic.cpp -/// @brief Nectar Preprocessor. - -using pp_parser_fn_t = Int32 (*)(CompilerKit::STLString&, std::ifstream&, - std::ofstream&); - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief Preprocessor internal types. - -///////////////////////////////////////////////////////////////////////////////////////// - -namespace Detail { -enum PPOperatorType : Int32 { - kInvalid = 0, - kEqual = 100, - kGreaterEqThan, - kLesserEqThan, - kGreaterThan, - kLesserThan, - kNotEqual, - kCount = 6, -}; - -struct pp_macro_condition final { - int32_t fType; - CompilerKit::STLString fTypeName; - - void Print() { - std::cout << "type: " << fType << "\n"; - std::cout << "type_name: " << fTypeName << "\n"; - } -}; - -struct pp_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 pp_parse_if_condition -// @brief parse #if condition - -///////////////////////////////////////////////////////////////////////////////////////// - -int32_t pp_parse_if_condition(Detail::pp_macro_condition& cond, Detail::pp_macro& macro, - bool& inactive_code, bool& defined, - CompilerKit::STLString& macro_str) { - if (cond.fType == Detail::PPOperatorType::kEqual) { - auto pos = macro_str.find(macro.fName); - if (pos == CompilerKit::STLString::npos) return 0; - - auto substr_macro = macro_str.substr(pos + 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 pos = macro_str.find(macro.fName); - if (pos == CompilerKit::STLString::npos) return 0; - - auto substr_macro = macro_str.substr(pos + 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 pos = macro_str.find(macro.fName); - if (pos == CompilerKit::STLString::npos) return 0; - - auto substr_macro = macro_str.substr(pos + 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.fValue.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::PPOperatorType::kGreaterThan) { - if (lhs > rhs) { - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - if (cond.fType == Detail::PPOperatorType::kGreaterEqThan) { - if (lhs >= rhs) { - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - if (cond.fType == Detail::PPOperatorType::kLesserEqThan) { - if (lhs <= rhs) { - defined = true; - inactive_code = false; - - return 1; - } - - return 0; - } - - if (cond.fType == Detail::PPOperatorType::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 pp_parse_file -// @brief parse file to preprocess it. - -///////////////////////////////////////////////////////////////////////////////////////// - -void pp_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] == '#' && - 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] == '#' && 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::ast_find_needle(hdr_line, macro.fName)) { - auto macro_pos = hdr_line.find(macro.fName); - if (macro_pos == CompilerKit::STLString::npos) continue; - - if (hdr_line.substr(macro_pos).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 ) - - auto paren_pos = hdr_line.find(")"); - if (paren_pos != CompilerKit::STLString::npos) { - hdr_line.erase(paren_pos, 1); - } - - auto macro_call_pos = hdr_line.find(macro.fName + '('); - if (macro_call_pos != CompilerKit::STLString::npos) { - hdr_line.replace(macro_call_pos, len, symbol_val); - } - } else { - auto value = macro.fValue; - - hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), value); - } - } - } - } - - if (hdr_line[0] == '#' && hdr_line.find("define") != CompilerKit::STLString::npos) { - auto define_pos = hdr_line.find("define"); - if (define_pos == CompilerKit::STLString::npos) continue; - - auto line_after_define = hdr_line.substr(define_pos + 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::pp_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 ifndef_pos = hdr_line.find("ifndef"); - if (ifndef_pos == CompilerKit::STLString::npos) continue; - - auto line_after_ifndef = hdr_line.substr(ifndef_pos + 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] == '#' && - 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] == '#' && - hdr_line.find("ifdef") != CompilerKit::STLString::npos) { - auto ifdef_pos = hdr_line.find("ifdef"); - if (ifdef_pos == CompilerKit::STLString::npos) continue; - - auto line_after_ifdef = hdr_line.substr(ifdef_pos + 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] == '#' && - hdr_line.find("if") != CompilerKit::STLString::npos) { - inactive_code = true; - - std::vector pp_macro_condition_list = { - { - .fType = Detail::PPOperatorType::kEqual, - .fTypeName = "==", - }, - { - .fType = Detail::PPOperatorType::kNotEqual, - .fTypeName = "!=", - }, - { - .fType = Detail::PPOperatorType::kLesserThan, - .fTypeName = "<", - }, - { - .fType = Detail::PPOperatorType::kGreaterThan, - .fTypeName = ">", - }, - { - .fType = Detail::PPOperatorType::kLesserEqThan, - .fTypeName = "<=", - }, - { - .fType = Detail::PPOperatorType::kGreaterEqThan, - .fTypeName = ">=", - }, - }; - - int32_t good_to_go = 0; - - for (auto& macro_condition : pp_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 = pp_parse_if_condition(macro_condition, found_macro, inactive_code, - defined, hdr_line); - - break; - } - } - } - } - - if (good_to_go) continue; - - auto if_pos = hdr_line.find("if"); - if (if_pos == CompilerKit::STLString::npos) continue; - - auto line_after_if = hdr_line.substr(if_pos + 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] == '#' && - hdr_line.find("warning") != CompilerKit::STLString::npos) { - auto warning_pos = hdr_line.find("warning"); - if (warning_pos == CompilerKit::STLString::npos) continue; - - auto line_after_warning = hdr_line.substr(warning_pos + strlen("warning") + 1); - CompilerKit::STLString message; - - for (auto& ch : line_after_warning) { - if (ch == '\r' || ch == '\n') { - break; - } - - message += ch; - } - - std::cerr << "warn: " << message << std::endl; - } 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; - - auto line_after_warning = hdr_line.substr(error_pos + 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] == '#' && - hdr_line.find("include ") != CompilerKit::STLString::npos) { - auto include_pos = hdr_line.find("include "); - if (include_pos == CompilerKit::STLString::npos) continue; - - line_after_include = hdr_line.substr(include_pos + 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; - - pp_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); - - pp_parse_file(header, pp_out); - } - } else { - std::cerr << ("cppdrv: unknown pre-processor directive, " + hdr_line) << "\n"; - continue; - } - } - } catch (const std::out_of_range& oor) { - return; - } -} - -///////////////////////////////////////////////////////////////////////////////////////// - -// @brief main entrypoint of app. - -///////////////////////////////////////////////////////////////////////////////////////// - -NECTAR_MODULE(GenericPreprocessorMain) { - try { - bool skip = false; - bool double_skip = false; - - Detail::pp_macro macro_1; - - macro_1.fName = "__true"; - macro_1.fValue = "1"; - - kMacros.push_back(macro_1); - - Detail::pp_macro macro_unreachable; - - macro_unreachable.fName = "__unreachable"; - macro_unreachable.fValue = "__compilerkit_unreachable"; - - kMacros.push_back(macro_unreachable); - - Detail::pp_macro macro_unused; - - macro_unused.fName = "__unused"; - macro_unused.fValue = "__compilerkit_unused"; - - kMacros.push_back(macro_unused); - - Detail::pp_macro macro_0; - - macro_0.fName = "__false"; - macro_0.fValue = "0"; - - kMacros.push_back(macro_0); - - Detail::pp_macro macro_nectar; - - macro_nectar.fName = "__NECTAR__"; - macro_nectar.fValue = "1"; - - kMacros.push_back(macro_nectar); - - Detail::pp_macro macro_lang; - - macro_lang.fName = "__ncpp"; - macro_lang.fValue = "202601L"; - - kMacros.push_back(macro_lang); - - Detail::pp_macro macro_nil; - - macro_nil.fName = "nil"; - macro_nil.fValue = "0"; - - kMacros.push_back(macro_nil); - - 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", - "Nectar Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2026 all rights " - "reserved."); - - return NECTAR_SUCCESS; - } - - if (strcmp(argv[index], "-cpp-help") == 0) { - printf("%s\n", - "Nectar Preprocessor Driver v1.11, (c) Amlal El Mahrouss 2024-2026 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 NECTAR_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 + 2]); ++argv_find_len) { - if (!isdigit(argv[index + 2][argv_find_len])) { - is_string = true; - macro_value += "\""; - - break; - } - } - - macro_value += argv[index + 2]; - - if (is_string) macro_value += "\""; - - Detail::pp_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 NECTAR_EXEC_ERROR; - - for (auto& file : kFiles) { - if (!std::filesystem::exists(file)) continue; - - std::ifstream file_descriptor(file); - std::ofstream file_descriptor_pp(file + ".pp"); - - pp_parse_file(file_descriptor, file_descriptor_pp); - } - - return NECTAR_SUCCESS; - } catch (const std::runtime_error& e) { - std::cout << e.what() << '\n'; - } - - return NECTAR_EXEC_ERROR; -} - -// Last rev 8-1-24 -- cgit v1.2.3