From f6b400b80efc64b918c03352e93ec9de4e2369a1 Mon Sep 17 00:00:00 2001 From: Amlal Date: Fri, 25 Apr 2025 19:21:22 +0200 Subject: dev, general: codebase needed refactors and tweaks, for NeKernel's 0.0.2 release. details: - things needed to be cleared off, short sighted decisions fixed. - the inconsistency of certain files have been fixed too. Signed-off-by: Amlal --- dbg.json | 16 - dev/LibC++/make_cxx_headers.sh | 2 +- dev/LibCompiler/Backend/32x0.h | 2 +- dev/LibCompiler/Backend/64x0.h | 31 +- dev/LibCompiler/Backend/Aarch64.h | 41 + dev/LibCompiler/Backend/Amd64.h | 48 + dev/LibCompiler/Backend/PowerPC.h | 1557 +++++++++++++++++++++++++ dev/LibCompiler/Backend/amd64.h | 48 - dev/LibCompiler/Backend/arm64.h | 41 - dev/LibCompiler/Backend/power64.h | 1557 ------------------------- dev/LibCompiler/Detail/AsmUtils.h | 93 ++ dev/LibCompiler/Detail/ClUtils.h | 57 + dev/LibCompiler/src/Assembler64x0.cc | 46 +- dev/LibCompiler/src/AssemblerAMD64.cc | 12 +- dev/LibCompiler/src/AssemblerARM64.cc | 14 +- dev/LibCompiler/src/AssemblerPower.cc | 12 +- dev/LibCompiler/src/CCompilerARM64.cc | 2 +- dev/LibCompiler/src/CCompilerPower64.cc | 2 +- dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc | 9 +- dev/LibCompiler/src/Detail/AsmUtils.h | 96 -- dev/LibCompiler/src/Detail/ClUtils.h | 18 - docs/.keep | 0 docs/SPECIFICATION_CC.md | 23 - docs/md/SPECIFICATION_CC.md | 23 + docs/mermaid/.keep | 0 docs/svg/.keep | 0 docs/tex/.keep | 0 lc-osx.json | 18 + lc-posix.json | 18 + ld-osx.json | 16 + osx.json | 18 - posix.json | 18 - tools/asm.cc | 3 +- tools/ld64.cc | 4 - 34 files changed, 1911 insertions(+), 1934 deletions(-) delete mode 100644 dbg.json create mode 100644 dev/LibCompiler/Backend/Aarch64.h create mode 100644 dev/LibCompiler/Backend/Amd64.h create mode 100644 dev/LibCompiler/Backend/PowerPC.h delete mode 100644 dev/LibCompiler/Backend/amd64.h delete mode 100644 dev/LibCompiler/Backend/arm64.h delete mode 100644 dev/LibCompiler/Backend/power64.h create mode 100644 dev/LibCompiler/Detail/AsmUtils.h create mode 100644 dev/LibCompiler/Detail/ClUtils.h delete mode 100644 dev/LibCompiler/src/Detail/AsmUtils.h delete mode 100644 dev/LibCompiler/src/Detail/ClUtils.h create mode 100644 docs/.keep delete mode 100644 docs/SPECIFICATION_CC.md create mode 100644 docs/md/SPECIFICATION_CC.md create mode 100644 docs/mermaid/.keep create mode 100644 docs/svg/.keep create mode 100644 docs/tex/.keep create mode 100644 lc-osx.json create mode 100644 lc-posix.json create mode 100644 ld-osx.json delete mode 100644 osx.json delete mode 100644 posix.json diff --git a/dbg.json b/dbg.json deleted file mode 100644 index 8b44257..0000000 --- a/dbg.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compiler_path": "g++", - "compiler_std": "c++20", - "headers_path": [ - "./dev/LibDebugger", - "./dev" - ], - "sources_path": ["dev/LibDebugger/src/*.cc"], - "output_name": "/usr/local/lib/libDebugger.dylib", - "compiler_flags": ["-fPIC", "-shared"], - "cpp_macros": [ - "__LIBCOMPILER_DLL__=202401", - "LC_USE_STRUCTS=1", - "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" - ] -} diff --git a/dev/LibC++/make_cxx_headers.sh b/dev/LibC++/make_cxx_headers.sh index cdb89f6..7e23e2c 100755 --- a/dev/LibC++/make_cxx_headers.sh +++ b/dev/LibC++/make_cxx_headers.sh @@ -1,4 +1,4 @@ -#! /bin/bash +#! /bin/sh outputDir=stdcxx/ diff --git a/dev/LibCompiler/Backend/32x0.h b/dev/LibCompiler/Backend/32x0.h index bb0a5aa..49bb978 100644 --- a/dev/LibCompiler/Backend/32x0.h +++ b/dev/LibCompiler/Backend/32x0.h @@ -9,7 +9,7 @@ #include // @brief 32x0 support. -// @file Backend/32x0.hpp +// @file Backend/32x0.h #define kAsmOpcodeDecl(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ {.fName = __NAME, .fOpcode = __OPCODE, .fFunct3 = __FUNCT3, .fFunct7 = __FUNCT7}, diff --git a/dev/LibCompiler/Backend/64x0.h b/dev/LibCompiler/Backend/64x0.h index 745dff0..b2c4b56 100644 --- a/dev/LibCompiler/Backend/64x0.h +++ b/dev/LibCompiler/Backend/64x0.h @@ -10,7 +10,7 @@ #include // @brief 64x0 support. -// @file Backend/64x0.hpp +// @file Backend/64x0.h #define kAsmOpcodeDecl(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ {.fName = __NAME, .fOpcode = __OPCODE, .fFunct3 = __FUNCT3, .fFunct7 = __FUNCT7}, @@ -38,23 +38,26 @@ inline std::vector kOpcodes64x0 = { kAsmJump) // jump to linked return register kAsmOpcodeDecl("jrl", 0b1110011, 0b0001111, kAsmJump) // jump from return register. - kAsmOpcodeDecl("mv", 0b0100011, 0b101, kAsmRegToReg) kAsmOpcodeDecl( - "bg", 0b1100111, 0b111, kAsmRegToReg) kAsmOpcodeDecl("bl", 0b1100111, 0b011, kAsmRegToReg) - kAsmOpcodeDecl("beq", 0b1100111, 0b000, kAsmRegToReg) - kAsmOpcodeDecl("bne", 0b1100111, 0b001, kAsmRegToReg) - kAsmOpcodeDecl("bge", 0b1100111, 0b101, kAsmRegToReg) - kAsmOpcodeDecl("ble", 0b1100111, 0b100, kAsmRegToReg) - kAsmOpcodeDecl("stw", 0b0001111, 0b100, kAsmImmediate) - kAsmOpcodeDecl("ldw", 0b0001111, 0b100, kAsmImmediate) - kAsmOpcodeDecl("lda", 0b0001111, 0b101, kAsmImmediate) - kAsmOpcodeDecl("sta", 0b0001111, 0b001, kAsmImmediate) + kAsmOpcodeDecl("mv", 0b0100011, 0b101, kAsmRegToReg) + kAsmOpcodeDecl( + "bg", 0b1100111, 0b111, kAsmRegToReg) + kAsmOpcodeDecl("bl", 0b1100111, 0b011, kAsmRegToReg) + kAsmOpcodeDecl("beq", 0b1100111, 0b000, kAsmRegToReg) + kAsmOpcodeDecl("bne", 0b1100111, 0b001, kAsmRegToReg) + kAsmOpcodeDecl("bge", 0b1100111, 0b101, kAsmRegToReg) + kAsmOpcodeDecl("ble", 0b1100111, 0b100, kAsmRegToReg) + kAsmOpcodeDecl("stw", 0b0001111, 0b100, kAsmImmediate) + kAsmOpcodeDecl("ldw", 0b0001111, 0b100, kAsmImmediate) + kAsmOpcodeDecl("lda", 0b0001111, 0b101, kAsmImmediate) + kAsmOpcodeDecl("sta", 0b0001111, 0b001, kAsmImmediate) // add/sub without carry flag kAsmOpcodeDecl("add", 0b0101011, 0b100, kAsmImmediate) - kAsmOpcodeDecl("sub", 0b0101011, 0b101, kAsmImmediate) + kAsmOpcodeDecl("sub", 0b0101011, 0b101, kAsmImmediate) // add/sub with carry flag kAsmOpcodeDecl("addc", 0b0101011, 0b110, kAsmImmediate) - kAsmOpcodeDecl("subc", 0b0101011, 0b111, kAsmImmediate) - kAsmOpcodeDecl("sc", 0b1110011, 0b00, kAsmSyscall)}; + kAsmOpcodeDecl("subc", 0b0101011, 0b111, kAsmImmediate) + kAsmOpcodeDecl("sc", 0b1110011, 0b00, kAsmSyscall) +}; // \brief 64x0 register prefix // example: r32, r0 diff --git a/dev/LibCompiler/Backend/Aarch64.h b/dev/LibCompiler/Backend/Aarch64.h new file mode 100644 index 0000000..dcafa0a --- /dev/null +++ b/dev/LibCompiler/Backend/Aarch64.h @@ -0,0 +1,41 @@ +/* ------------------------------------------- + +Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include +#include + +/// @brief ARM64 encoding support. +/// @file Backend/Aarch64.h + +struct CpuOpcodeArm64; + +/// @brief ARM64 opcode header. +struct PACKED CpuOpcodeArm64_Data final { + uint32_t fOpcode : 10; // Bits 31–22: Opcode for operation + uint32_t fRm : 5; // Bits 21–16: Source register Rm + uint32_t fShamt : 6; // Bits 15–10: Shift amount + uint32_t fRn : 5; // Bits 9–5: Source register Rn + uint32_t fRd : 5; // Bits 4–0: Destination register Rd +}; + +typedef struct { + uint32_t opcode : 6; // Bits 31–26: Branch opcode + int32_t offset : 26; // Bits 25–0: Signed offset (branch target) +} PACKED CpuOpcodeArm64_Branch; + +typedef struct { + uint32_t size : 2; // Bits 31–30: Size of the data + uint32_t opcode : 7; // Bits 29–23: Opcode for load/store + uint32_t offset : 12; // Bits 22–10: Offset + uint32_t rn : 5; // Bits 9–5: Base address register Rn + uint32_t rt : 5; // Bits 4–0: Target/source register Rt +} PACKED CpuOpcodeArm64_LoadStore; + +#define kAsmRegisterLimit (30) +#define kAsmRegisterPrefix "x" +#define kOpcodeARM64Count (1000) diff --git a/dev/LibCompiler/Backend/Amd64.h b/dev/LibCompiler/Backend/Amd64.h new file mode 100644 index 0000000..a3fb8a2 --- /dev/null +++ b/dev/LibCompiler/Backend/Amd64.h @@ -0,0 +1,48 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include + +// @brief AMD64 support. +// @file Backend/Amd64.h + +#define kAsmOpcodeDecl(__NAME, __OPCODE) {.fName = __NAME, .fOpcode = __OPCODE}, + +typedef char i64_character_t; +typedef uint8_t i64_byte_t; +typedef uint16_t i64_hword_t; +typedef uint32_t i64_word_t; + +#define kAsmRegisterPrefix "r" + +struct CpuOpcodeAMD64 { + std::string fName; + i64_byte_t fPrefixBytes[4]; + i64_hword_t fOpcode; + i64_hword_t fModReg; + i64_word_t fDisplacment; + i64_word_t fImmediate; +}; + +/// these two are edge cases +#define kAsmIntOpcode 0xCC +#define kasmIntOpcodeAlt 0xCD + +#define kAsmJumpOpcode 0x0F80 +#define kJumpLimit 30 +#define kJumpLimitStandard 0xE3 +#define kJumpLimitStandardLimit 0xEB + +inline std::vector kOpcodesAMD64 = { + kAsmOpcodeDecl("int", 0xCD) kAsmOpcodeDecl("into", 0xCE) kAsmOpcodeDecl("intd", 0xF1) + kAsmOpcodeDecl("int3", 0xC3) kAsmOpcodeDecl("iret", 0xCF) kAsmOpcodeDecl("retf", 0xCB) + kAsmOpcodeDecl("retn", 0xC3) kAsmOpcodeDecl("ret", 0xC3) kAsmOpcodeDecl("sti", 0xfb) + kAsmOpcodeDecl("cli", 0xfa) kAsmOpcodeDecl("hlt", 0xf4) kAsmOpcodeDecl("nop", 0x90) + kAsmOpcodeDecl("mov", 0x48) kAsmOpcodeDecl("call", 0xFF)}; + +#define kAsmRegisterLimit 16 diff --git a/dev/LibCompiler/Backend/PowerPC.h b/dev/LibCompiler/Backend/PowerPC.h new file mode 100644 index 0000000..03aea49 --- /dev/null +++ b/dev/LibCompiler/Backend/PowerPC.h @@ -0,0 +1,1557 @@ +/* ------------------------------------------- + + Some modifications are copyrighted under: + Amlal El Mahrouss + + Original author: + Apple Inc + +------------------------------------------- */ + +#pragma once + +#include + +/// @note Based of: +/// https://opensource.apple.com/source/cctools/cctools-750/as/ppc-opcode.h.auto.html + +#define kOpcodePPCCount (1073U) + +/* + * These defines are use in the cpus field of the instructions. If the field + * is zero it can execute on all cpus. The defines are or'ed together. This + * information is used to set the cpusubtype in the resulting object file. + */ +#define CPU601 0x1 +#define IMPL64 0x2 +#define OPTIONAL 0x4 +#define VMX 0x8 +#define CPU970 0x10 /* added to OPTIONAL insts that the 970 has */ +#define CPUMAHROUSS 0x12 /* optional mahrouss insts. */ + +enum OpcodeType { + NONE, /* no operand */ + JBSR, /* jbsr pseudo op */ + PCREL, /* PC relative (branch offset) */ + BADDR, /* Branch address (sign extended absolute address) */ + D, /* 16 bit displacement */ + DS, /* 14 bit displacement (double word) */ + SI, /* signed 16 bit immediate */ + UI, /* unsigned 16 bit immediate */ + HI, /* high 16 bit immediate (with truncation) */ + GREG, /* general register */ + G0REG, /* general register r1-r31 or 0 */ + FREG, /* float register */ + VREG, /* vector register */ + SGREG, /* segment register */ + SPREG, /* special register (or 10 bit number, 5 bit halves reversed) */ + BCND, /* branch condition opcode */ + CRF, /* condition register field */ + CRFONLY, /* condition register field only no expression allowed */ + sh, /* 6 bit number (0 - 63) (sh field, split and reversed) */ + mb, /* 6 bit number (0 - 63) (mb field, mb5 || mb0:4 reversed) */ + NUM, /* number */ + SNUM, /* signed number */ + NUM0, /* number (where 1< - -// @brief AMD64 support. -// @file Backend/amd64.hpp - -#define kAsmOpcodeDecl(__NAME, __OPCODE) {.fName = __NAME, .fOpcode = __OPCODE}, - -typedef char i64_character_t; -typedef uint8_t i64_byte_t; -typedef uint16_t i64_hword_t; -typedef uint32_t i64_word_t; - -#define kAsmRegisterPrefix "r" - -struct CpuOpcodeAMD64 { - std::string fName; - i64_byte_t fPrefixBytes[4]; - i64_hword_t fOpcode; - i64_hword_t fModReg; - i64_word_t fDisplacment; - i64_word_t fImmediate; -}; - -/// these two are edge cases -#define kAsmIntOpcode 0xCC -#define kasmIntOpcodeAlt 0xCD - -#define kAsmJumpOpcode 0x0F80 -#define kJumpLimit 30 -#define kJumpLimitStandard 0xE3 -#define kJumpLimitStandardLimit 0xEB - -inline std::vector kOpcodesAMD64 = { - kAsmOpcodeDecl("int", 0xCD) kAsmOpcodeDecl("into", 0xCE) kAsmOpcodeDecl("intd", 0xF1) - kAsmOpcodeDecl("int3", 0xC3) kAsmOpcodeDecl("iret", 0xCF) kAsmOpcodeDecl("retf", 0xCB) - kAsmOpcodeDecl("retn", 0xC3) kAsmOpcodeDecl("ret", 0xC3) kAsmOpcodeDecl("sti", 0xfb) - kAsmOpcodeDecl("cli", 0xfa) kAsmOpcodeDecl("hlt", 0xf4) kAsmOpcodeDecl("nop", 0x90) - kAsmOpcodeDecl("mov", 0x48) kAsmOpcodeDecl("call", 0xFF)}; - -#define kAsmRegisterLimit 16 diff --git a/dev/LibCompiler/Backend/arm64.h b/dev/LibCompiler/Backend/arm64.h deleted file mode 100644 index fb91a1c..0000000 --- a/dev/LibCompiler/Backend/arm64.h +++ /dev/null @@ -1,41 +0,0 @@ -/* ------------------------------------------- - -Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved - -------------------------------------------- */ - -#pragma once - -#include -#include - -/// @brief ARM64 encoding support. -/// @file Backend/arm64.hpp - -struct CpuOpcodeArm64; - -/// @brief ARM64 opcode header. -struct PACKED CpuOpcodeArm64_Data final { - uint32_t fOpcode : 10; // Bits 31–22: Opcode for operation - uint32_t fRm : 5; // Bits 21–16: Source register Rm - uint32_t fShamt : 6; // Bits 15–10: Shift amount - uint32_t fRn : 5; // Bits 9–5: Source register Rn - uint32_t fRd : 5; // Bits 4–0: Destination register Rd -}; - -typedef struct { - uint32_t opcode : 6; // Bits 31–26: Branch opcode - int32_t offset : 26; // Bits 25–0: Signed offset (branch target) -} PACKED CpuOpcodeArm64_Branch; - -typedef struct { - uint32_t size : 2; // Bits 31–30: Size of the data - uint32_t opcode : 7; // Bits 29–23: Opcode for load/store - uint32_t offset : 12; // Bits 22–10: Offset - uint32_t rn : 5; // Bits 9–5: Base address register Rn - uint32_t rt : 5; // Bits 4–0: Target/source register Rt -} PACKED CpuOpcodeArm64_LoadStore; - -#define kAsmRegisterLimit (30) -#define kAsmRegisterPrefix "x" -#define kOpcodeARM64Count (1000) diff --git a/dev/LibCompiler/Backend/power64.h b/dev/LibCompiler/Backend/power64.h deleted file mode 100644 index 03aea49..0000000 --- a/dev/LibCompiler/Backend/power64.h +++ /dev/null @@ -1,1557 +0,0 @@ -/* ------------------------------------------- - - Some modifications are copyrighted under: - Amlal El Mahrouss - - Original author: - Apple Inc - -------------------------------------------- */ - -#pragma once - -#include - -/// @note Based of: -/// https://opensource.apple.com/source/cctools/cctools-750/as/ppc-opcode.h.auto.html - -#define kOpcodePPCCount (1073U) - -/* - * These defines are use in the cpus field of the instructions. If the field - * is zero it can execute on all cpus. The defines are or'ed together. This - * information is used to set the cpusubtype in the resulting object file. - */ -#define CPU601 0x1 -#define IMPL64 0x2 -#define OPTIONAL 0x4 -#define VMX 0x8 -#define CPU970 0x10 /* added to OPTIONAL insts that the 970 has */ -#define CPUMAHROUSS 0x12 /* optional mahrouss insts. */ - -enum OpcodeType { - NONE, /* no operand */ - JBSR, /* jbsr pseudo op */ - PCREL, /* PC relative (branch offset) */ - BADDR, /* Branch address (sign extended absolute address) */ - D, /* 16 bit displacement */ - DS, /* 14 bit displacement (double word) */ - SI, /* signed 16 bit immediate */ - UI, /* unsigned 16 bit immediate */ - HI, /* high 16 bit immediate (with truncation) */ - GREG, /* general register */ - G0REG, /* general register r1-r31 or 0 */ - FREG, /* float register */ - VREG, /* vector register */ - SGREG, /* segment register */ - SPREG, /* special register (or 10 bit number, 5 bit halves reversed) */ - BCND, /* branch condition opcode */ - CRF, /* condition register field */ - CRFONLY, /* condition register field only no expression allowed */ - sh, /* 6 bit number (0 - 63) (sh field, split and reversed) */ - mb, /* 6 bit number (0 - 63) (mb field, mb5 || mb0:4 reversed) */ - NUM, /* number */ - SNUM, /* signed number */ - NUM0, /* number (where 1< +#include + +#include + +using namespace LibCompiler; + +/// @brief Get Number from lineBuffer. +/// @param lineBuffer the lineBuffer to fetch from. +/// @param numberKey where to seek that number. +/// @return +static NumberCast32 GetNumber32(std::string lineBuffer, std::string numberKey) { + auto pos = lineBuffer.find(numberKey) + numberKey.size(); + + while (lineBuffer[pos] == ' ') { + ++pos; + } + + switch (lineBuffer[pos + 1]) { + case 'x': { + if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 16); !res) { + if (errno != 0) { + Detail::print_error("invalid hex number: " + lineBuffer, "LibCompiler"); + throw std::runtime_error("invalid_hex"); + } + } + + NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 16)); + + if (kVerbose) { + kStdOut << "asm: found a base 16 number here: " << lineBuffer.substr(pos) << "\n"; + } + + return numOffset; + } + case 'b': { + if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 2); !res) { + if (errno != 0) { + Detail::print_error("invalid binary number:" + lineBuffer, "LibCompiler"); + throw std::runtime_error("invalid_bin"); + } + } + + NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 2)); + + if (kVerbose) { + kStdOut << "asm: found a base 2 number here:" << lineBuffer.substr(pos) << "\n"; + } + + return numOffset; + } + case 'o': { + if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 7); !res) { + if (errno != 0) { + Detail::print_error("invalid octal number: " + lineBuffer, "LibCompiler"); + throw std::runtime_error("invalid_octal"); + } + } + + NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 7)); + + if (kVerbose) { + kStdOut << "asm: found a base 8 number here:" << lineBuffer.substr(pos) << "\n"; + } + + return numOffset; + } + default: { + if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 10); !res) { + if (errno != 0) { + Detail::print_error("invalid hex number: " + lineBuffer, "LibCompiler"); + throw std::runtime_error("invalid_hex"); + } + } + + NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 10)); + + if (kVerbose) { + kStdOut << "asm: found a base 10 number here:" << lineBuffer.substr(pos) << "\n"; + } + + return numOffset; + } + } +} diff --git a/dev/LibCompiler/Detail/ClUtils.h b/dev/LibCompiler/Detail/ClUtils.h new file mode 100644 index 0000000..b14d472 --- /dev/null +++ b/dev/LibCompiler/Detail/ClUtils.h @@ -0,0 +1,57 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include +#include + +#define kZero64Section ".zero64" +#define kCode64Section ".code64" +#define kData64Section ".data64" + +#define kZero128Section ".zero128" +#define kCode128Section ".code128" +#define kData128Section ".data128" + +#define kBlank "\e[0;30m" +#define kRed "\e[0;31m" +#define kWhite "\e[0;97m" +#define kYellow "\e[0;33m" + +#define kStdOut (std::cout << kWhite) +#define kStdErr (std::cout << kRed) + +inline static UInt32 kErrorLimit = 10; +inline static UInt32 kAcceptableErrors = 0; +inline static bool kVerbose = false; +inline static bool kOutputAsBinary = false; + +namespace Detail { +inline void print_error(std::string reason, std::string file) noexcept { + if (reason[0] == '\n') reason.erase(0, 1); + + kStdErr << kRed << "[ asm ] " << kWhite + << ((file == "LibCompiler") ? "InternalErrorException: " + : ("FileException{ " + file + " }: ")) + << kBlank << std::endl; + kStdErr << kRed << "[ asm ] " << kWhite << reason << kBlank << std::endl; + + if (kAcceptableErrors > kErrorLimit) std::exit(3); + + ++kAcceptableErrors; +} + +inline void print_warning(std::string reason, std::string file) noexcept { + if (reason[0] == '\n') reason.erase(0, 1); + + if (!file.empty()) { + kStdOut << kYellow << "[ asm ] " << kWhite << file << kBlank << std::endl; + } + + kStdOut << kYellow << "[ asm ] " << kWhite << reason << kBlank << std::endl; +} +} // namespace Detail \ No newline at end of file diff --git a/dev/LibCompiler/src/Assembler64x0.cc b/dev/LibCompiler/src/Assembler64x0.cc index 7701982..9dc8e52 100644 --- a/dev/LibCompiler/src/Assembler64x0.cc +++ b/dev/LibCompiler/src/Assembler64x0.cc @@ -28,6 +28,7 @@ #include #include #include +#include ///////////////////// @@ -35,29 +36,16 @@ ///////////////////// -#define kBlank "\e[0;30m" -#define kRed "\e[0;31m" -#define kWhite "\e[0;97m" -#define kYellow "\e[0;33m" - -#define kStdOut (std::cout << kWhite) -#define kStdErr (std::cout << kRed) - static char kOutputArch = LibCompiler::kPefArch64000; -static Boolean kOutputAsBinary = false; -static UInt32 kErrorLimit = 10; -static UInt32 kAcceptableErrors = 0; - -constexpr auto c64x0IPAlignment = 0x4U; +/// @note The 64x0 is VLSIW, so we need to jump to 4 bytes. +constexpr auto k64x0IPAlignment = 0x4U; static std::size_t kCounter = 1UL; static std::uintptr_t kOrigin = kPefBaseOrigin; static std::vector> kOriginLabel; -static bool kVerbose = false; - static std::vector kBytes; static LibCompiler::AERecordHeader kCurrentRecord{ @@ -72,32 +60,6 @@ static const std::string kRelocSymbol = ":RuntimeSymbol:"; // \brief forward decl. static bool asm_read_attributes(std::string& line); -namespace Detail { -void print_error(std::string reason, std::string file) noexcept { - if (reason[0] == '\n') reason.erase(0, 1); - - kStdErr << kRed << "[ asm ] " << kWhite - << ((file == "LibCompiler") ? "InternalErrorException: " - : ("FileException{ " + file + " }: ")) - << kBlank << std::endl; - kStdErr << kRed << "[ asm ] " << kWhite << reason << kBlank << std::endl; - - if (kAcceptableErrors > kErrorLimit) std::exit(3); - - ++kAcceptableErrors; -} - -void print_warning(std::string reason, std::string file) noexcept { - if (reason[0] == '\n') reason.erase(0, 1); - - if (!file.empty()) { - kStdOut << kYellow << "[ asm ] " << kWhite << file << kBlank << std::endl; - } - - kStdOut << kYellow << "[ asm ] " << kWhite << reason << kBlank << std::endl; -} -} // namespace Detail - ///////////////////////////////////////////////////////////////////////////////////////// // @brief 64x0 assembler entrypoint, the program/module starts here. @@ -897,7 +859,7 @@ bool LibCompiler::Encoder64x0::WriteLine(std::string& line, const std::string& f } asm_end_label_cpy: - kOrigin += c64x0IPAlignment; + kOrigin += k64x0IPAlignment; break; } diff --git a/dev/LibCompiler/src/AssemblerAMD64.cc b/dev/LibCompiler/src/AssemblerAMD64.cc index a5a7ee5..7dfa36a 100644 --- a/dev/LibCompiler/src/AssemblerAMD64.cc +++ b/dev/LibCompiler/src/AssemblerAMD64.cc @@ -27,7 +27,7 @@ #define kAssemblerPragmaSym '#' #include -#include +#include #include #include #include @@ -52,12 +52,8 @@ #define kStdErr (std::cout << kRed) static char kOutputArch = LibCompiler::kPefArchAMD64; -static Boolean kOutputAsBinary = false; -static UInt32 kErrorLimit = 10; -static UInt32 kAcceptableErrors = 0; - -constexpr auto kIPAlignement = 0x4U; +constexpr auto kIPAlignement = 0x1U; static std::size_t kCounter = 1UL; @@ -67,8 +63,6 @@ static std::vector> kOriginLabel; /// @brief keep it simple by default. static std::int32_t kRegisterBitWidth = 16U; -static bool kVerbose = false; - static std::vector kAppBytes; static LibCompiler::AERecordHeader kCurrentRecord{ @@ -83,7 +77,7 @@ static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; // \brief forward decl. static bool asm_read_attributes(std::string& line); -#include +#include ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/dev/LibCompiler/src/AssemblerARM64.cc b/dev/LibCompiler/src/AssemblerARM64.cc index d984845..98f12b8 100644 --- a/dev/LibCompiler/src/AssemblerARM64.cc +++ b/dev/LibCompiler/src/AssemblerARM64.cc @@ -18,11 +18,12 @@ #define __ASM_NEED_ARM64__ 1 #include -#include +#include #include #include #include #include +#include #include #include #include @@ -43,21 +44,15 @@ #define kStdOut (std::cout << kWhite) #define kStdErr (std::cout << kRed) -constexpr auto cPowerIPAlignment = 0x4U; +constexpr auto cPowerIPAlignment = 0x1U; static CharType kOutputArch = LibCompiler::kPefArchARM64; -static Boolean kOutputAsBinary = false; - -static UInt32 kErrorLimit = 10; -static UInt32 kAcceptableErrors = 0; static std::size_t kCounter = 1UL; static std::uintptr_t kOrigin = kPefBaseOrigin; static std::vector> kOriginLabel; -static bool kVerbose = false; - static std::vector kBytes; static LibCompiler::AERecordHeader kCurrentRecord{ @@ -72,9 +67,6 @@ static const std::string kRelocSymbol = ":RuntimeSymbol:"; // \brief forward decl. static bool asm_read_attributes(std::string& line); -/// Do not move it on top! it uses the assembler detail namespace! -#include - ///////////////////////////////////////////////////////////////////////////////////////// /// @brief POWER assembler entrypoint, the program/module starts here. diff --git a/dev/LibCompiler/src/AssemblerPower.cc b/dev/LibCompiler/src/AssemblerPower.cc index 30b6d68..d3b3b6d 100644 --- a/dev/LibCompiler/src/AssemblerPower.cc +++ b/dev/LibCompiler/src/AssemblerPower.cc @@ -18,7 +18,7 @@ #define __ASM_NEED_PPC__ 1 #include -#include +#include #include #include #include @@ -28,6 +28,7 @@ #include #include #include +#include ///////////////////// @@ -46,18 +47,12 @@ constexpr auto cPowerIPAlignment = 0x4U; static CharType kOutputArch = LibCompiler::kPefArchPowerPC; -static Boolean kOutputAsBinary = false; - -static UInt32 kErrorLimit = 10; -static UInt32 kAcceptableErrors = 0; static std::size_t kCounter = 1UL; static std::uintptr_t kOrigin = kPefBaseOrigin; static std::vector> kOriginLabel; -static bool kVerbose = false; - static std::vector kBytes; static LibCompiler::AERecordHeader kCurrentRecord{ @@ -72,9 +67,6 @@ static const std::string kRelocSymbol = ":RuntimeSymbol:"; // \brief forward decl. static bool asm_read_attributes(std::string& line); -/// Do not move it on top! it uses the assembler detail namespace! -#include - ///////////////////////////////////////////////////////////////////////////////////////// /// @brief POWER assembler entrypoint, the program/module starts here. diff --git a/dev/LibCompiler/src/CCompilerARM64.cc b/dev/LibCompiler/src/CCompilerARM64.cc index accd9f9..7339840 100644 --- a/dev/LibCompiler/src/CCompilerARM64.cc +++ b/dev/LibCompiler/src/CCompilerARM64.cc @@ -10,7 +10,7 @@ /// BUGS: 0 /// TODO: none -#include +#include #include #include #include diff --git a/dev/LibCompiler/src/CCompilerPower64.cc b/dev/LibCompiler/src/CCompilerPower64.cc index 90031a0..42c4a0a 100644 --- a/dev/LibCompiler/src/CCompilerPower64.cc +++ b/dev/LibCompiler/src/CCompilerPower64.cc @@ -7,7 +7,7 @@ * ======================================================== */ -#include +#include #include #include #include diff --git a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc index 66cf859..558a94d 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc @@ -21,10 +21,12 @@ // extern_segment, @autodelete { ... }, fn foo() -> auto { ... } -#include +#include #include #include +#include + #include /* NE C++ Compiler */ @@ -99,12 +101,9 @@ struct CompilerState final { } // namespace Detail static Detail::CompilerState kState; -static SizeType kErrorLimit = 100; static Int32 kOnClassScope = 0; -static Int32 kAcceptableErrors = 0; - namespace Detail { /// @brief prints an error into stdout. /// @param reason the reason of the error. @@ -939,6 +938,8 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { kKeywords.push_back({.keyword_name = ">=", .keyword_kind = LibCompiler::kKeywordKindGreaterEq}); kKeywords.push_back({.keyword_name = "<=", .keyword_kind = LibCompiler::kKeywordKindLessEq}); + kErrorLimit = 100; + kFactory.Mount(new AssemblyCPlusPlusInterface()); kCompilerFrontend = new CompilerFrontendCPlusPlus(); diff --git a/dev/LibCompiler/src/Detail/AsmUtils.h b/dev/LibCompiler/src/Detail/AsmUtils.h deleted file mode 100644 index 358700a..0000000 --- a/dev/LibCompiler/src/Detail/AsmUtils.h +++ /dev/null @@ -1,96 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved - -------------------------------------------- */ - -#pragma once - -#include -#include - -using namespace LibCompiler; - -namespace Detail { -extern void print_error(std::string reason, std::string file) noexcept; -extern void print_warning(std::string reason, std::string file) noexcept; -} // namespace Detail - -/// @brief Get Number from lineBuffer. -/// @param lineBuffer the lineBuffer to fetch from. -/// @param numberKey where to seek that number. -/// @return -static NumberCast32 GetNumber32(std::string lineBuffer, std::string numberKey) { - auto pos = lineBuffer.find(numberKey) + numberKey.size(); - - while (lineBuffer[pos] == ' ') { - ++pos; - } - - switch (lineBuffer[pos + 1]) { - case 'x': { - if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 16); !res) { - if (errno != 0) { - Detail::print_error("invalid hex number: " + lineBuffer, "LibCompiler"); - throw std::runtime_error("invalid_hex"); - } - } - - NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 16)); - - if (kVerbose) { - kStdOut << "asm: found a base 16 number here: " << lineBuffer.substr(pos) << "\n"; - } - - return numOffset; - } - case 'b': { - if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 2); !res) { - if (errno != 0) { - Detail::print_error("invalid binary number:" + lineBuffer, "LibCompiler"); - throw std::runtime_error("invalid_bin"); - } - } - - NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 2)); - - if (kVerbose) { - kStdOut << "asm: found a base 2 number here:" << lineBuffer.substr(pos) << "\n"; - } - - return numOffset; - } - case 'o': { - if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 7); !res) { - if (errno != 0) { - Detail::print_error("invalid octal number: " + lineBuffer, "LibCompiler"); - throw std::runtime_error("invalid_octal"); - } - } - - NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 7)); - - if (kVerbose) { - kStdOut << "asm: found a base 8 number here:" << lineBuffer.substr(pos) << "\n"; - } - - return numOffset; - } - default: { - if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 10); !res) { - if (errno != 0) { - Detail::print_error("invalid hex number: " + lineBuffer, "LibCompiler"); - throw std::runtime_error("invalid_hex"); - } - } - - NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 10)); - - if (kVerbose) { - kStdOut << "asm: found a base 10 number here:" << lineBuffer.substr(pos) << "\n"; - } - - return numOffset; - } - } -} diff --git a/dev/LibCompiler/src/Detail/ClUtils.h b/dev/LibCompiler/src/Detail/ClUtils.h deleted file mode 100644 index 024d0d2..0000000 --- a/dev/LibCompiler/src/Detail/ClUtils.h +++ /dev/null @@ -1,18 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved - -------------------------------------------- */ - -#pragma once - -#include -#include - -#define kZero64Section ".zero64" -#define kCode64Section ".code64" -#define kData64Section ".data64" - -#define kZero128Section ".zero128" -#define kCode128Section ".code128" -#define kData128Section ".data128" diff --git a/docs/.keep b/docs/.keep new file mode 100644 index 0000000..e69de29 diff --git a/docs/SPECIFICATION_CC.md b/docs/SPECIFICATION_CC.md deleted file mode 100644 index 7d146ae..0000000 --- a/docs/SPECIFICATION_CC.md +++ /dev/null @@ -1,23 +0,0 @@ -# Specification of LibCompiler - -=================================== - -# 0: General Information - -=================================== -- Output format: PEF, XCOFF -- Object format: AE, COFF -- SDK Language: C++ - -=================================== - -# 1: LibCompiler - -=================================== - -- Shall support compilation, assembling and linking. -- Shall be written in C++ -- Shall support the assembler too. -- Shall provide primitives for compilers, assemblers and linkers. -- Shall support multiple architectures. -- Shall have a debugger. \ No newline at end of file diff --git a/docs/md/SPECIFICATION_CC.md b/docs/md/SPECIFICATION_CC.md new file mode 100644 index 0000000..7d146ae --- /dev/null +++ b/docs/md/SPECIFICATION_CC.md @@ -0,0 +1,23 @@ +# Specification of LibCompiler + +=================================== + +# 0: General Information + +=================================== +- Output format: PEF, XCOFF +- Object format: AE, COFF +- SDK Language: C++ + +=================================== + +# 1: LibCompiler + +=================================== + +- Shall support compilation, assembling and linking. +- Shall be written in C++ +- Shall support the assembler too. +- Shall provide primitives for compilers, assemblers and linkers. +- Shall support multiple architectures. +- Shall have a debugger. \ No newline at end of file diff --git a/docs/mermaid/.keep b/docs/mermaid/.keep new file mode 100644 index 0000000..e69de29 diff --git a/docs/svg/.keep b/docs/svg/.keep new file mode 100644 index 0000000..e69de29 diff --git a/docs/tex/.keep b/docs/tex/.keep new file mode 100644 index 0000000..e69de29 diff --git a/lc-osx.json b/lc-osx.json new file mode 100644 index 0000000..8ec532e --- /dev/null +++ b/lc-osx.json @@ -0,0 +1,18 @@ +{ + "compiler_path": "g++", + "compiler_std": "c++20", + "headers_path": [ + "./dev/LibCompiler", + "./dev", + "./dev/LibCompiler/src/", + "./dev/LibCompiler/src/Detail" + ], + "sources_path": ["dev/LibCompiler/src/*.cc"], + "output_name": "/usr/local/lib/libCompiler.dylib", + "compiler_flags": ["-fPIC", "-shared"], + "cpp_macros": [ + "__LIBCOMPILER__=202401", + "LC_USE_STRUCTS=1", + "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" + ] +} diff --git a/lc-posix.json b/lc-posix.json new file mode 100644 index 0000000..ba50e23 --- /dev/null +++ b/lc-posix.json @@ -0,0 +1,18 @@ +{ + "compiler_path": "g++", + "compiler_std": "c++20", + "headers_path": [ + "./dev/LibCompiler", + "./dev", + "./dev/LibCompiler/src", + "./dev/LibCompiler/src/Detail" + ], + "sources_path": ["dev/LibCompiler/src/*.cc"], + "output_name": "/usr/lib/libCompiler.so", + "compiler_flags": ["-fPIC", "-shared"], + "cpp_macros": [ + "__LIBCOMPILER__=202401", + "LC_USE_STRUCTS=1", + "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" + ] +} diff --git a/ld-osx.json b/ld-osx.json new file mode 100644 index 0000000..c26b060 --- /dev/null +++ b/ld-osx.json @@ -0,0 +1,16 @@ +{ + "compiler_path": "g++", + "compiler_std": "c++20", + "headers_path": [ + "./dev/LibDebugger", + "./dev" + ], + "sources_path": ["dev/LibDebugger/src/*.cc"], + "output_name": "/usr/local/lib/libDebugger.dylib", + "compiler_flags": ["-fPIC", "-shared"], + "cpp_macros": [ + "__LIBCOMPILER__=202401", + "LC_USE_STRUCTS=1", + "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" + ] +} diff --git a/osx.json b/osx.json deleted file mode 100644 index 3ee20b3..0000000 --- a/osx.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "compiler_path": "g++", - "compiler_std": "c++20", - "headers_path": [ - "./dev/LibCompiler", - "./dev", - "./dev/LibCompiler/src/", - "./dev/LibCompiler/src/Detail" - ], - "sources_path": ["dev/LibCompiler/src/*.cc"], - "output_name": "/usr/local/lib/libCompiler.dylib", - "compiler_flags": ["-fPIC", "-shared"], - "cpp_macros": [ - "__LIBCOMPILER_DLL__=202401", - "LC_USE_STRUCTS=1", - "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" - ] -} diff --git a/posix.json b/posix.json deleted file mode 100644 index 0f2178e..0000000 --- a/posix.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "compiler_path": "g++", - "compiler_std": "c++20", - "headers_path": [ - "./dev/LibCompiler", - "./dev", - "./dev/LibCompiler/src", - "./dev/LibCompiler/src/Detail" - ], - "sources_path": ["dev/LibCompiler/src/*.cc"], - "output_name": "/usr/lib/libCompiler.so", - "compiler_flags": ["-fPIC", "-shared"], - "cpp_macros": [ - "__LIBCOMPILER_DLL__=202401", - "LC_USE_STRUCTS=1", - "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" - ] -} diff --git a/tools/asm.cc b/tools/asm.cc index 118ab22..2e68a33 100644 --- a/tools/asm.cc +++ b/tools/asm.cc @@ -20,7 +20,8 @@ LC_IMPORT_C int AssemblerMain64x0(int argc, char const* argv[]); LC_IMPORT_C int AssemblerMainAMD64(int argc, char const* argv[]); enum AsmKind : Int32 { - kX64Assembler, + kInvalidAssembler = 0, + kX64Assembler = 100, k64X0Assembler, kPOWER64Assembler, kARM64Assembler, diff --git a/tools/ld64.cc b/tools/ld64.cc index fec1314..ebb7376 100644 --- a/tools/ld64.cc +++ b/tools/ld64.cc @@ -12,9 +12,5 @@ LC_IMPORT_C int DynamicLinker64PEF(int argc, char const* argv[]); int main(int argc, char const* argv[]) { - if (argc < 1) { - return 1; - } - return DynamicLinker64PEF(argc, argv); } -- cgit v1.2.3