diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2025-01-08 10:28:10 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2025-01-08 10:28:10 +0100 |
| commit | f5a153c3f888f82edaf5038e5762f9bd70356db4 (patch) | |
| tree | 02d3a8f71796105a7e6780eb3327b2c2724e70d5 /dev/LibCompiler | |
| parent | c3b10ee1e28737375d65c3811f390d77a84fc165 (diff) | |
KAN-8: Compiler tweaks and AARCH64 in progress.
- Refactor C compilers.
- Add Encoder for ARM64.
- Add and working on assembler for AARCH64.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/LibCompiler')
24 files changed, 1089 insertions, 341 deletions
diff --git a/dev/LibCompiler/AAL/AssemblyInterface.h b/dev/LibCompiler/AAL/AssemblyInterface.h index 19f768a..6609894 100644 --- a/dev/LibCompiler/AAL/AssemblyInterface.h +++ b/dev/LibCompiler/AAL/AssemblyInterface.h @@ -22,7 +22,7 @@ namespace LibCompiler explicit AssemblyInterface() = default; virtual ~AssemblyInterface() = default; - TOOLCHAINKIT_COPY_DEFAULT(AssemblyInterface); + LIBCOMPILER_COPY_DEFAULT(AssemblyInterface); /// @brief compile to object file. /// @note Example C++ -> MASM -> AE object. @@ -36,7 +36,7 @@ namespace LibCompiler explicit AssemblyFactory() = default; ~AssemblyFactory() = default; - TOOLCHAINKIT_COPY_DEFAULT(AssemblyFactory); + LIBCOMPILER_COPY_DEFAULT(AssemblyFactory); public: enum @@ -46,6 +46,7 @@ namespace LibCompiler kArch64x0, kArchRISCV, kArchPowerPC, + kArchAARCH64, kArchUnknown, }; @@ -133,7 +134,7 @@ namespace LibCompiler explicit EncoderInterface() = default; virtual ~EncoderInterface() = default; - TOOLCHAINKIT_COPY_DEFAULT(EncoderInterface); + LIBCOMPILER_COPY_DEFAULT(EncoderInterface); virtual std::string CheckLine(std::string& line, const std::string& file) = 0; virtual bool WriteLine(std::string& line, const std::string& file) = 0; @@ -148,7 +149,7 @@ namespace LibCompiler explicit EncoderAMD64() = default; ~EncoderAMD64() override = default; - TOOLCHAINKIT_COPY_DEFAULT(EncoderAMD64); + LIBCOMPILER_COPY_DEFAULT(EncoderAMD64); virtual std::string CheckLine(std::string& line, const std::string& file) override; @@ -163,6 +164,25 @@ namespace LibCompiler #endif // __ASM_NEED_AMD64__ +#ifdef __ASM_NEED_ARM64__ + + class EncoderARM64 final : public EncoderInterface + { + public: + explicit EncoderARM64() = default; + ~EncoderARM64() override = default; + + LIBCOMPILER_COPY_DEFAULT(EncoderARM64); + + virtual std::string CheckLine(std::string& line, + const std::string& file) override; + virtual bool WriteLine(std::string& line, const std::string& file) override; + virtual bool WriteNumber(const std::size_t& pos, + std::string& from_what) override; + }; + +#endif // __ASM_NEED_ARM64__ + #ifdef __ASM_NEED_64x0__ class Encoder64x0 final : public EncoderInterface @@ -171,7 +191,7 @@ namespace LibCompiler explicit Encoder64x0() = default; ~Encoder64x0() override = default; - TOOLCHAINKIT_COPY_DEFAULT(Encoder64x0); + LIBCOMPILER_COPY_DEFAULT(Encoder64x0); virtual std::string CheckLine(std::string& line, const std::string& file) override; @@ -190,7 +210,7 @@ namespace LibCompiler explicit Encoder32x0() = default; ~Encoder32x0() override = default; - TOOLCHAINKIT_COPY_DEFAULT(Encoder32x0); + LIBCOMPILER_COPY_DEFAULT(Encoder32x0); virtual std::string CheckLine(std::string& line, const std::string& file) override; @@ -209,7 +229,7 @@ namespace LibCompiler explicit EncoderPowerPC() = default; ~EncoderPowerPC() override = default; - TOOLCHAINKIT_COPY_DEFAULT(EncoderPowerPC); + LIBCOMPILER_COPY_DEFAULT(EncoderPowerPC); virtual std::string CheckLine(std::string& line, const std::string& file) override; diff --git a/dev/LibCompiler/AAL/CPU/amd64.h b/dev/LibCompiler/AAL/CPU/amd64.h index 02f008e..fd75a04 100644 --- a/dev/LibCompiler/AAL/CPU/amd64.h +++ b/dev/LibCompiler/AAL/CPU/amd64.h @@ -41,18 +41,19 @@ struct CpuOpcodeAMD64 inline std::vector<CpuOpcodeAMD64> 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 15 + 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/AAL/CPU/arm64.h b/dev/LibCompiler/AAL/CPU/arm64.h index 0c32073..7c06abb 100644 --- a/dev/LibCompiler/AAL/CPU/arm64.h +++ b/dev/LibCompiler/AAL/CPU/arm64.h @@ -6,6 +6,7 @@ Copyright (C) 2024 Theater Quality Corp, all rights reserved #pragma once +#include <stdint.h> #include <LibCompiler/Defines.h> /// @brief ARM64 encoding support. @@ -14,13 +15,28 @@ Copyright (C) 2024 Theater Quality Corp, all rights reserved struct CpuOpcodeArm64; /// @brief ARM64 opcode header. -struct CpuOpcodeArm64 final +struct PACKED CpuOpcodeArm64_Data final { - uint8_t fOpcode; // opcode - uint8_t fRegisterLeft; // left register index - uint8_t fRegisterRight; // right register index - bool fRegisterLeftHooked; - bool fRegisterRightHooked; - uint32_t fImmediateValue; // immediate 32-bit value - bool fImmediateValueHooked; + 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/AAL/CPU/power64.h b/dev/LibCompiler/AAL/CPU/power64.h index 9112fed..36feef2 100644 --- a/dev/LibCompiler/AAL/CPU/power64.h +++ b/dev/LibCompiler/AAL/CPU/power64.h @@ -10,11 +10,13 @@ #pragma once -#include <cstdint> +#include <stdint.h> /// @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 @@ -27,7 +29,7 @@ #define CPU970 0x10 /* added to OPTIONAL insts that the 970 has */ #define CPUMAHROUSS 0x12 /* optional mahrouss insts. */ -enum optype +enum OpcodeType { NONE, /* no operand */ JBSR, /* jbsr pseudo op */ @@ -57,23 +59,21 @@ enum optype ZERO /* the number zero */ }; -struct op -{ - uint32_t offset : 5; - uint32_t width : 5; - enum optype type : 6; -}; - struct CpuOpcodePPC { + struct OpcodeType final + { + uint32_t offset : 5; + uint32_t width : 5; + uint32_t type : 6; + }; + uint32_t opcode; const char* name; // c++ wants the string to be const, it makes sense here. - struct op ops[5]; + OpcodeType ops[5]; uint32_t cpus; }; -#define kOpcodePPCCount (1073U) - inline CpuOpcodePPC kOpcodesPowerPC[] = { {0x38000000, "addi", {{21, 5, GREG}, {16, 5, G0REG}, {0, 16, SI}}}, {0x38000000, "li", {{21, 5, GREG}, {0, 16, SI}}}, diff --git a/dev/LibCompiler/Defines.h b/dev/LibCompiler/Defines.h index 5fb7a98..3ee0d44 100644 --- a/dev/LibCompiler/Defines.h +++ b/dev/LibCompiler/Defines.h @@ -4,8 +4,8 @@ ------------------------------------------- */ -#ifndef __TOOLCHAINKIT_DEFINES_H__ -#define __TOOLCHAINKIT_DEFINES_H__ +#ifndef __LIBCOMPILER_DEFINES_H__ +#define __LIBCOMPILER_DEFINES_H__ #ifndef Yes #define Yes true @@ -78,19 +78,19 @@ #define rt_copy_memory(dst, src, len) memcpy(dst, src, len) #endif -#define TOOLCHAINKIT_COPY_DELETE(KLASS) \ +#define LIBCOMPILER_COPY_DELETE(KLASS) \ KLASS& operator=(const KLASS&) = delete; \ KLASS(const KLASS&) = delete; -#define TOOLCHAINKIT_COPY_DEFAULT(KLASS) \ +#define LIBCOMPILER_COPY_DEFAULT(KLASS) \ KLASS& operator=(const KLASS&) = default; \ KLASS(const KLASS&) = default; -#define TOOLCHAINKIT_MOVE_DELETE(KLASS) \ +#define LIBCOMPILER_MOVE_DELETE(KLASS) \ KLASS& operator=(KLASS&&) = delete; \ KLASS(KLASS&&) = delete; -#define TOOLCHAINKIT_MOVE_DEFAULT(KLASS) \ +#define LIBCOMPILER_MOVE_DEFAULT(KLASS) \ KLASS& operator=(KLASS&&) = default; \ KLASS(KLASS&&) = default; @@ -163,10 +163,10 @@ typedef char char_type; #define kAsmFileExtsMax 7 -#define TOOLCHAINKIT_MODULE(name) extern "C" int name(int argc, char** argv) +#define LIBCOMPILER_MODULE(name) extern "C" int name(int argc, char** argv) #ifdef MSVC #pragma scalar_storage_order big - endian #endif // ifdef MSVC -#endif /* ifndef __TOOLCHAINKIT_DEFINES_H__ */ +#endif /* ifndef __LIBCOMPILER_DEFINES_H__ */ diff --git a/dev/LibCompiler/Macros.h b/dev/LibCompiler/Macros.h index 25f8ab2..55fb000 100644 --- a/dev/LibCompiler/Macros.h +++ b/dev/LibCompiler/Macros.h @@ -9,25 +9,25 @@ #ifndef _MACROS_H_ #define _MACROS_H_ -#define TOOLCHAINKIT_COPY_DELETE(KLASS) \ +#define LIBCOMPILER_COPY_DELETE(KLASS) \ KLASS& operator=(const KLASS&) = delete; \ KLASS(const KLASS&) = delete; -#define TOOLCHAINKIT_COPY_DEFAULT(KLASS) \ +#define LIBCOMPILER_COPY_DEFAULT(KLASS) \ KLASS& operator=(const KLASS&) = default; \ KLASS(const KLASS&) = default; -#define TOOLCHAINKIT_MOVE_DELETE(KLASS) \ +#define LIBCOMPILER_MOVE_DELETE(KLASS) \ KLASS& operator=(KLASS&&) = delete; \ KLASS(KLASS&&) = delete; -#define TOOLCHAINKIT_MOVE_DEFAULT(KLASS) \ +#define LIBCOMPILER_MOVE_DEFAULT(KLASS) \ KLASS& operator=(KLASS&&) = default; \ KLASS(KLASS&&) = default; /// @note xxxx is the error placeholder, in hexadecimal. -#define TOOLCHAINKIT_ERROR_PREFIX_CXX "CXXxxxx" -#define TOOLCHAINKIT_ERROR_PREFIX_CL "CLxxxx" -#define TOOLCHAINKIT_ERROR_PREFIX_ASM "ASMxxxx" +#define LIBCOMPILER_ERROR_PREFIX_CXX "CXXxxxx" +#define LIBCOMPILER_ERROR_PREFIX_CL "CLxxxx" +#define LIBCOMPILER_ERROR_PREFIX_ASM "ASMxxxx" #endif /* ifndef _MACROS_H_ */ diff --git a/dev/LibCompiler/NFC/AE.h b/dev/LibCompiler/NFC/AE.h index 3aeedf0..2453461 100644 --- a/dev/LibCompiler/NFC/AE.h +++ b/dev/LibCompiler/NFC/AE.h @@ -107,7 +107,7 @@ namespace LibCompiler::Utils explicit AEReadableProtocol() = default; ~AEReadableProtocol() = default; - TOOLCHAINKIT_COPY_DELETE(AEReadableProtocol); + LIBCOMPILER_COPY_DELETE(AEReadableProtocol); /** * @brief Read AE Record headers. diff --git a/dev/LibCompiler/NFC/ErrorID.h b/dev/LibCompiler/NFC/ErrorID.h index 89c4a73..1a5a0db 100644 --- a/dev/LibCompiler/NFC/ErrorID.h +++ b/dev/LibCompiler/NFC/ErrorID.h @@ -12,11 +12,11 @@ #include <LibCompiler/Defines.h> #include <LibCompiler/NFC/ErrorOr.h> -#define TOOLCHAINKIT_EXEC_ERROR -30 -#define TOOLCHAINKIT_FILE_NOT_FOUND -31 -#define TOOLCHAINKIT_DIR_NOT_FOUND -32 -#define TOOLCHAINKIT_FILE_EXISTS -33 -#define TOOLCHAINKIT_TOO_LONG -34 -#define TOOLCHAINKIT_INVALID_DATA -35 -#define TOOLCHAINKIT_UNIMPLEMENTED -36 -#define TOOLCHAINKIT_FAT_ERROR -37 +#define LIBCOMPILER_EXEC_ERROR -30 +#define LIBCOMPILER_FILE_NOT_FOUND -31 +#define LIBCOMPILER_DIR_NOT_FOUND -32 +#define LIBCOMPILER_FILE_EXISTS -33 +#define LIBCOMPILER_TOO_LONG -34 +#define LIBCOMPILER_INVALID_DATA -35 +#define LIBCOMPILER_UNIMPLEMENTED -36 +#define LIBCOMPILER_FAT_ERROR -37 diff --git a/dev/LibCompiler/NFC/Ref.h b/dev/LibCompiler/NFC/Ref.h index d7f5863..91b7568 100644 --- a/dev/LibCompiler/NFC/Ref.h +++ b/dev/LibCompiler/NFC/Ref.h @@ -30,7 +30,7 @@ namespace LibCompiler } } - TOOLCHAINKIT_COPY_DEFAULT(Ref); + LIBCOMPILER_COPY_DEFAULT(Ref); public: explicit Ref(T cls, const Bool& strong = false) diff --git a/dev/LibCompiler/NFC/String.h b/dev/LibCompiler/NFC/String.h index 1a9c0c4..99f6c8b 100644 --- a/dev/LibCompiler/NFC/String.h +++ b/dev/LibCompiler/NFC/String.h @@ -42,7 +42,7 @@ namespace LibCompiler } } - TOOLCHAINKIT_COPY_DEFAULT(StringView); + LIBCOMPILER_COPY_DEFAULT(StringView); CharType* Data(); const CharType* CData() const; diff --git a/dev/LibCompiler/Parser.h b/dev/LibCompiler/Parser.h index b26ffcd..d123e00 100644 --- a/dev/LibCompiler/Parser.h +++ b/dev/LibCompiler/Parser.h @@ -20,7 +20,7 @@ namespace LibCompiler explicit ICompilerFrontend() = default; virtual ~ICompilerFrontend() = default; - TOOLCHAINKIT_COPY_DEFAULT(ICompilerFrontend); + LIBCOMPILER_COPY_DEFAULT(ICompilerFrontend); // NOTE: cast this to your user defined ast. typedef void* AstType; @@ -28,7 +28,7 @@ namespace LibCompiler //! @brief Compile a syntax tree ouf of the text. //! Also takes the source file name for metadata. - virtual bool Compile(const std::string text, const std::string file) = 0; + virtual bool Compile(std::string text, const std::string file) = 0; //! @brief What language are we dealing with? virtual const char* Language() diff --git a/dev/LibCompiler/src/Assembler32x0.cc b/dev/LibCompiler/src/Assembler32x0.cc index e6a055f..5064894 100644 --- a/dev/LibCompiler/src/Assembler32x0.cc +++ b/dev/LibCompiler/src/Assembler32x0.cc @@ -44,7 +44,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// -TOOLCHAINKIT_MODULE(ZKAAssemblerMain32000) +LIBCOMPILER_MODULE(ZKAAssemblerMain32000) { return 0; } diff --git a/dev/LibCompiler/src/Assembler64x0.cc b/dev/LibCompiler/src/Assembler64x0.cc index 8789e1b..b04d50f 100644 --- a/dev/LibCompiler/src/Assembler64x0.cc +++ b/dev/LibCompiler/src/Assembler64x0.cc @@ -111,7 +111,7 @@ namespace Details ///////////////////////////////////////////////////////////////////////////////////////// -TOOLCHAINKIT_MODULE(AssemblerMain64x0) +LIBCOMPILER_MODULE(AssemblerMain64x0) { for (size_t i = 1; i < argc; ++i) { diff --git a/dev/LibCompiler/src/AssemblerAMD64.cc b/dev/LibCompiler/src/AssemblerAMD64.cc index aa87887..330931e 100644 --- a/dev/LibCompiler/src/AssemblerAMD64.cc +++ b/dev/LibCompiler/src/AssemblerAMD64.cc @@ -91,7 +91,7 @@ static bool asm_read_attributes(std::string& line); ///////////////////////////////////////////////////////////////////////////////////////// -TOOLCHAINKIT_MODULE(AssemblerAMD64) +LIBCOMPILER_MODULE(AssemblerMainAMD64) { //////////////// CPU OPCODES BEGIN //////////////// diff --git a/dev/LibCompiler/src/AssemblerARM64.cc b/dev/LibCompiler/src/AssemblerARM64.cc index e69de29..1429f58 100644 --- a/dev/LibCompiler/src/AssemblerARM64.cc +++ b/dev/LibCompiler/src/AssemblerARM64.cc @@ -0,0 +1,712 @@ +/* ------------------------------------------- + + Copyright (C) 2024 Theater Quality Corp, all rights reserved + +------------------------------------------- */ + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @file AssemblerARM64.cxx +/// @author EL Mahrouss Amlal +/// @brief 'ACORN' Assembler. + +/// REMINDER: when dealing with an undefined symbol use (string +/// size):LinkerFindSymbol:(string) so that li will look for it. + +///////////////////////////////////////////////////////////////////////////////////////// + +#define __ASM_NEED_ARM64__ 1 + +#include <LibCompiler/NFC/ErrorID.h> +#include <LibCompiler/AAL/CPU/arm64.h> +#include <LibCompiler/NFC/PEF.h> +#include <LibCompiler/Parser.h> +#include <LibCompiler/NFC/AE.h> +#include <LibCompiler/Version.h> +#include <filesystem> +#include <algorithm> +#include <iostream> +#include <fstream> +#include <vector> + +///////////////////// + +// ANSI ESCAPE CODES + +///////////////////// + +#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) + +constexpr auto cPowerIPAlignment = 0x4U; + +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<std::pair<std::string, std::uintptr_t>> kOriginLabel; + +static bool kVerbose = false; + +static std::vector<uint8_t> kBytes; + +static LibCompiler::AERecordHeader kCurrentRecord{ + .fName = "", .fKind = LibCompiler::kPefCode, .fSize = 0, .fOffset = 0}; + +static std::vector<LibCompiler::AERecordHeader> kRecords; +static std::vector<std::string> kUndefinedSymbols; + +static const std::string kUndefinedSymbol = ":UndefinedSymbol:"; +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 <Detail/AsmUtils.h> + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @brief POWER assembler entrypoint, the program/module starts here. + +///////////////////////////////////////////////////////////////////////////////////////// + +LIBCOMPILER_MODULE(AssemblerMainARM64) +{ + for (size_t i = 1; i < argc; ++i) + { + if (argv[i][0] == '-') + { + if (strcmp(argv[i], "--ver") == 0 || strcmp(argv[i], "--v") == 0) + { + kStdOut << "AssemblerPower: AARCH64 Assembler Driver.\nAssemblerPower: " << kDistVersion << "\nAssemblerPower: " + "Copyright (c) " + "Theater Quality Corp\n"; + return 0; + } + else if (strcmp(argv[i], "--h") == 0) + { + kStdOut << "AssemblerPower: AARCH64 Assembler Driver.\nAssemblerPower: Copyright (c) 2024 " + "Theater Quality Corp\n"; + kStdOut << "--version,/v: print program version.\n"; + kStdOut << "--verbose: print verbose output.\n"; + kStdOut << "--binary: output as flat binary.\n"; + + return 0; + } + else if (strcmp(argv[i], "--binary") == 0) + { + kOutputAsBinary = true; + continue; + } + else if (strcmp(argv[i], "--verbose") == 0) + { + kVerbose = true; + continue; + } + + kStdOut << "AssemblerPower: ignore " << argv[i] << "\n"; + continue; + } + + if (!std::filesystem::exists(argv[i])) + { + kStdOut << "AssemblerPower: 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 << "AssemblerPower: error: " << strerror(errno) << "\n"; + } + } + + std::string line; + + LibCompiler::AEHeader hdr{0}; + + memset(hdr.fPad, kAENullType, kAEPad); + + hdr.fMagic[0] = kAEMag0; + hdr.fMagic[1] = kAEMag1; + hdr.fSize = sizeof(LibCompiler::AEHeader); + hdr.fArch = kOutputArch; + + ///////////////////////////////////////////////////////////////////////////////////////// + + // COMPILATION LOOP + + ///////////////////////////////////////////////////////////////////////////////////////// + + LibCompiler::EncoderARM64 asm64; + + while (std::getline(file_ptr, line)) + { + if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) + { + Details::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(); + Details::print_warning("exit because of: " + what, "LibCompiler"); + } + + std::filesystem::remove(object_output); + goto asm_fail_exit; + } + } + + if (!kOutputAsBinary) + { + if (kVerbose) + { + kStdOut << "AssemblerPower: 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 << "AssemblerPower: At least one record is needed to write an object " + "file.\nAssemblerPower: 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 |= LibCompiler::kKindRelocationAtRuntime; + record_hdr.fOffset = record_count; + ++record_count; + + file_ptr_out << record_hdr; + + if (kVerbose) + kStdOut << "AssemblerPower: Wrote record " << record_hdr.fName << "...\n"; + } + + // increment once again, so that we won't lie about the kUndefinedSymbols. + ++record_count; + + for (auto& sym : kUndefinedSymbols) + { + LibCompiler::AERecordHeader undefined_sym{0}; + + if (kVerbose) + kStdOut << "AssemblerPower: 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 << "AssemblerPower: Write raw binary...\n"; + } + } + + // byte from byte, we write this. + for (auto& byte : kBytes) + { + file_ptr_out.write(reinterpret_cast<const char*>(&byte), sizeof(byte)); + } + + if (kVerbose) + kStdOut << "AssemblerPower: Wrote file with program in it.\n"; + + file_ptr_out.flush(); + file_ptr_out.close(); + + if (kVerbose) + kStdOut << "AssemblerPower: Exit succeeded.\n"; + + return 0; + } + +asm_fail_exit: + + if (kVerbose) + kStdOut << "AssemblerPower: Exit failed.\n"; + + return LIBCOMPILER_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 (LibCompiler::find_word(line, "extern_segment")) + { + if (kOutputAsBinary) + { + Details::print_error("Invalid extern_segment directive in flat binary mode.", + "LibCompiler"); + throw std::runtime_error("invalid_extern_segment_bin"); + } + + auto name = line.substr(line.find("extern_segment") + strlen("extern_segment") + 1); + + if (name.size() == 0) + { + Details::print_error("Invalid extern_segment", "LibCompiler"); + 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(".code64") != std::string::npos) + { + // data is treated as code. + kCurrentRecord.fKind = LibCompiler::kPefCode; + } + else if (name.find(".data64") != std::string::npos) + { + // no code will be executed from here. + kCurrentRecord.fKind = LibCompiler::kPefData; + } + else if (name.find(".zero64") != std::string::npos) + { + // this is a bss section. + kCurrentRecord.fKind = LibCompiler::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that li can find it. + + if (name == kPefStart) + { + kCurrentRecord.fKind = LibCompiler::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 AssemblerPower to tell the AE output stage to + // mark this section as a header. it currently supports .code64, .data64., + // .zero64 + else if (LibCompiler::find_word(line, "public_segment")) + { + if (kOutputAsBinary) + { + Details::print_error("Invalid public_segment directive in flat binary mode.", + "LibCompiler"); + 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 = LibCompiler::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 = LibCompiler::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 = LibCompiler::kPefZero; + } + + // this is a special case for the start stub. + // we want this so that li can find it. + + if (name == kPefStart) + { + kCurrentRecord.fKind = LibCompiler::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 Details::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(const std::string& str) + { + return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); + } +} // namespace Details::algorithm + +///////////////////////////////////////////////////////////////////////////////////////// + +// @brief Check for line (syntax check) + +///////////////////////////////////////////////////////////////////////////////////////// + +std::string LibCompiler::EncoderARM64::CheckLine(std::string& line, + const std::string& file) +{ + std::string err_str; + + if (line.empty() || LibCompiler::find_word(line, "extern_segment") || + LibCompiler::find_word(line, "public_segment") || + line.find('#') != std::string::npos || LibCompiler::find_word(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 (!Details::algorithm::is_valid_arm64(line)) + { + err_str = "Line contains non alphanumeric characters.\nhere -> "; + err_str += line; + } + } + + return err_str; + } + + if (!Details::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 LibCompiler::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) + { + Details::print_error("invalid hex number: " + jump_label, "LibCompiler"); + throw std::runtime_error("invalid_hex"); + } + } + + LibCompiler::NumberCast64 num( + strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16)); + + for (char& i : num.number) + { + kBytes.push_back(i); + } + + if (kVerbose) + { + kStdOut << "AssemblerPower: 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) + { + Details::print_error("invalid binary number: " + jump_label, "LibCompiler"); + throw std::runtime_error("invalid_bin"); + } + } + + LibCompiler::NumberCast64 num( + strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2)); + + if (kVerbose) + { + kStdOut << "AssemblerPower: 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) + { + Details::print_error("invalid octal number: " + jump_label, "LibCompiler"); + throw std::runtime_error("invalid_octal"); + } + } + + LibCompiler::NumberCast64 num( + strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); + + if (kVerbose) + { + kStdOut << "AssemblerPower: 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; + } + } + + LibCompiler::NumberCast64 num( + strtol(jump_label.substr(pos).c_str(), nullptr, 10)); + + for (char& i : num.number) + { + kBytes.push_back(i); + } + + if (kVerbose) + { + kStdOut << "AssemblerPower: found a base 10 number here: " << jump_label.substr(pos) + << "\n"; + } + + return true; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +/// @brief Read and write an instruction to the output array. + +///////////////////////////////////////////////////////////////////////////////////////// + +bool LibCompiler::EncoderARM64::WriteLine(std::string& line, + const std::string& file) +{ + if (LibCompiler::find_word(line, "public_segment")) + return false; + + if (!Details::algorithm::is_valid_arm64(line)) + return false; + + + return true; +} + +// Last rev 13-1-24 diff --git a/dev/LibCompiler/src/AssemblerPower.cc b/dev/LibCompiler/src/AssemblerPower.cc index 5014575..a98e3e8 100644 --- a/dev/LibCompiler/src/AssemblerPower.cc +++ b/dev/LibCompiler/src/AssemblerPower.cc @@ -81,7 +81,7 @@ static bool asm_read_attributes(std::string& line); ///////////////////////////////////////////////////////////////////////////////////////// -TOOLCHAINKIT_MODULE(AssemblerMainPower64) +LIBCOMPILER_MODULE(AssemblerMainPower64) { for (size_t i = 1; i < argc; ++i) { @@ -299,7 +299,7 @@ asm_fail_exit: if (kVerbose) kStdOut << "AssemblerPower: Exit failed.\n"; - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } ///////////////////////////////////////////////////////////////////////////////////////// @@ -573,9 +573,9 @@ std::string LibCompiler::EncoderPowerPC::CheckLine(std::string& line, // these don't. std::vector<std::string> filter_inst = {"blr", "bl", "sc"}; - for (auto& opcodePPC : kOpcodesPowerPC) + for (auto& opcode_risc : kOpcodesPowerPC) { - if (LibCompiler::find_word(line, opcodePPC.name)) + if (LibCompiler::find_word(line, opcode_risc.name)) { for (auto& op : operands_inst) { @@ -591,16 +591,16 @@ std::string LibCompiler::EncoderPowerPC::CheckLine(std::string& line, // if it is like that -> addr1, 0x0 if (auto it = - std::find(filter_inst.begin(), filter_inst.end(), opcodePPC.name); + std::find(filter_inst.begin(), filter_inst.end(), opcode_risc.name); it == filter_inst.cend()) { - if (LibCompiler::find_word(line, opcodePPC.name)) + if (LibCompiler::find_word(line, opcode_risc.name)) { if (!isspace( - line[line.find(opcodePPC.name) + strlen(opcodePPC.name)])) + line[line.find(opcode_risc.name) + strlen(opcode_risc.name)])) { err_str += "\nMissing space between "; - err_str += opcodePPC.name; + err_str += opcode_risc.name; err_str += " and operands.\nhere -> "; err_str += line; } @@ -746,24 +746,24 @@ bool LibCompiler::EncoderPowerPC::WriteLine(std::string& line, const std::string& file) { if (LibCompiler::find_word(line, "public_segment")) - return true; + return false; if (!Details::algorithm::is_valid_power64(line)) - return true; + return false; - for (auto& opcodePPC : kOpcodesPowerPC) + for (auto& opcode_risc : kOpcodesPowerPC) { // strict check here - if (LibCompiler::find_word(line, opcodePPC.name)) + if (LibCompiler::find_word(line, opcode_risc.name)) { - std::string name(opcodePPC.name); + std::string name(opcode_risc.name); std::string jump_label, cpy_jump_label; std::vector<size_t> found_registers_index; // check funct7 type. - switch (opcodePPC.ops->type) + switch (opcode_risc.ops->type) { default: { - NumberCast32 num(opcodePPC.opcode); + NumberCast32 num(opcode_risc.opcode); for (auto ch : num.number) { @@ -791,10 +791,10 @@ bool LibCompiler::EncoderPowerPC::WriteLine(std::string& line, // \brief how many registers we found. std::size_t found_some_count = 0UL; std::size_t register_count = 0UL; - std::string opcodeName = opcodePPC.name; + std::string opcodeName = opcode_risc.name; std::size_t register_sum = 0; - NumberCast64 num(opcodePPC.opcode); + NumberCast64 num(opcode_risc.opcode); for (size_t line_index = 0UL; line_index < line.size(); line_index++) @@ -1058,7 +1058,7 @@ bool LibCompiler::EncoderPowerPC::WriteLine(std::string& line, } // we're not in immediate addressing, reg to reg. - if (opcodePPC.ops->type != GREG) + if (opcode_risc.ops->type != GREG) { // remember! register to register! if (found_some_count == 1) diff --git a/dev/LibCompiler/src/AssemblyFactory.cc b/dev/LibCompiler/src/AssemblyFactory.cc index 1e6ac48..6a2918c 100644 --- a/dev/LibCompiler/src/AssemblyFactory.cc +++ b/dev/LibCompiler/src/AssemblyFactory.cc @@ -30,7 +30,7 @@ namespace LibCompiler const Int32& arch) noexcept { if (sourceFile.length() < 1 || !fMounted) - return TOOLCHAINKIT_UNIMPLEMENTED; + return LIBCOMPILER_UNIMPLEMENTED; return fMounted->CompileToFormat(sourceFile, arch); } diff --git a/dev/LibCompiler/src/CCompiler64x0.cc b/dev/LibCompiler/src/CCompiler64x0.cc index 8888a72..23bd11a 100644 --- a/dev/LibCompiler/src/CCompiler64x0.cc +++ b/dev/LibCompiler/src/CCompiler64x0.cc @@ -147,10 +147,10 @@ public: explicit CompilerFrontend64x0() = default; ~CompilerFrontend64x0() override = default; - TOOLCHAINKIT_COPY_DEFAULT(CompilerFrontend64x0); + LIBCOMPILER_COPY_DEFAULT(CompilerFrontend64x0); std::string Check(const char* text, const char* file); - bool Compile(const std::string text, const std::string file) override; + bool Compile(std::string text, const std::string file) override; const char* Language() override { @@ -200,9 +200,9 @@ namespace Details ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerFrontend64x0::Compile(const std::string text, const std::string file) +bool CompilerFrontend64x0::Compile(std::string text_, const std::string file) { - std::string textBuffer = text; + std::string text = text_; bool typeFound = false; bool fnFound = false; @@ -216,7 +216,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil std::mt19937 generator(seq); // start parsing - for (size_t text_index = 0; text_index < textBuffer.size(); ++text_index) + for (size_t text_index = 0; text_index < text.size(); ++text_index) { auto syntaxLeaf = LibCompiler::SyntaxLeafList::SyntaxLeaf(); @@ -227,7 +227,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil if (!typeFound) { - auto substr = textBuffer.substr(text_index); + auto substr = text.substr(text_index); std::string match_type; for (size_t y = 0; y < substr.size(); ++y) @@ -255,7 +255,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil break; } - if (textBuffer.find('(') != std::string::npos) + if (text.find('(') != std::string::npos) { syntaxLeaf.fUserValue = buf; @@ -274,7 +274,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil } } - if (textBuffer[text_index] == '{') + if (text[text_index] == '{') { if (kInStruct) { @@ -288,7 +288,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil } // return keyword handler - if (textBuffer[text_index] == 'r') + if (text[text_index] == 'r') { std::string return_keyword; return_keyword += "return"; @@ -297,18 +297,18 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil std::string value; - for (size_t return_index = text_index; return_index < textBuffer.size(); + for (size_t return_index = text_index; return_index < text.size(); ++return_index) { - if (textBuffer[return_index] != return_keyword[index]) + if (text[return_index] != return_keyword[index]) { for (size_t value_index = return_index; - value_index < textBuffer.size(); ++value_index) + value_index < text.size(); ++value_index) { - if (textBuffer[value_index] == ';') + if (text[value_index] == ';') break; - value += textBuffer[value_index]; + value += text[value_index]; } break; @@ -354,10 +354,10 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil } } - if (textBuffer[text_index] == 'i' && textBuffer[text_index + 1] == 'f') + if (text[text_index] == 'i' && text[text_index + 1] == 'f') { - auto expr = textBuffer.substr(text_index + 2); - textBuffer.erase(text_index, 2); + auto expr = text.substr(text_index + 2); + text.erase(text_index, 2); if (expr.find("{") != std::string::npos) { @@ -370,7 +370,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); - kIfFunction = "__TOOLCHAINKIT_IF_PROC_"; + kIfFunction = "__LIBCOMPILER_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntaxLeaf.fUserValue = "\tlda r12, extern_segment "; @@ -387,32 +387,32 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil // Parse expressions and instructions here. // what does this mean? // we encounter an assignment, or we reached the end of an expression. - if (textBuffer[text_index] == '=' || textBuffer[text_index] == ';') + if (text[text_index] == '=' || text[text_index] == ';') { if (fnFound) continue; if (kIfFound) continue; - if (textBuffer[text_index] == ';' && kInStruct) + if (text[text_index] == ';' && kInStruct) continue; - if (textBuffer.find("typedef ") != std::string::npos) + if (text.find("typedef ") != std::string::npos) continue; - if (textBuffer[text_index] == '=' && kInStruct) + if (text[text_index] == '=' && kInStruct) { - Details::print_error("assignement of value in struct " + textBuffer, + Details::print_error("assignement of value in struct " + text, file); continue; } - if (textBuffer[text_index] == ';' && kInStruct) + if (text[text_index] == ';' && kInStruct) { bool space_found_ = false; std::string sym; - for (auto& ch : textBuffer) + for (auto& ch : text) { if (ch == ' ') { @@ -437,26 +437,26 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil continue; } - if (textBuffer[text_index] == '=' && kInStruct) + if (text[text_index] == '=' && kInStruct) { continue; } - if (textBuffer[text_index + 1] == '=' || - textBuffer[text_index - 1] == '!' || - textBuffer[text_index - 1] == '<' || - textBuffer[text_index - 1] == '>') + if (text[text_index + 1] == '=' || + text[text_index - 1] == '!' || + text[text_index - 1] == '<' || + text[text_index - 1] == '>') { continue; } std::string substr; - if (textBuffer.find('=') != std::string::npos && kInBraces && !kIfFound) + if (text.find('=') != std::string::npos && kInBraces && !kIfFound) { - if (textBuffer.find("*") != std::string::npos) + if (text.find("*") != std::string::npos) { - if (textBuffer.find("=") > textBuffer.find("*")) + if (text.find("=") > text.find("*")) substr += "\tlda "; else substr += "\tldw "; @@ -466,7 +466,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil substr += "\tldw "; } } - else if (textBuffer.find('=') != std::string::npos && !kInBraces) + else if (text.find('=') != std::string::npos && !kInBraces) { substr += "stw public_segment .data64 "; } @@ -475,10 +475,10 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil std::string str_name; - for (size_t text_index_2 = 0; text_index_2 < textBuffer.size(); + for (size_t text_index_2 = 0; text_index_2 < text.size(); ++text_index_2) { - if (textBuffer[text_index_2] == '\"') + if (text[text_index_2] == '\"') { ++text_index_2; @@ -486,29 +486,29 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil // string. substr += '"'; - for (; text_index_2 < textBuffer.size(); ++text_index_2) + for (; text_index_2 < text.size(); ++text_index_2) { - if (textBuffer[text_index_2] == '\"') + if (text[text_index_2] == '\"') break; - substr += textBuffer[text_index_2]; + substr += text[text_index_2]; } } - if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}') + if (text[text_index_2] == '{' || text[text_index_2] == '}') continue; - if (textBuffer[text_index_2] == ';') + if (text[text_index_2] == ';') { break; } - if (textBuffer[text_index_2] == ' ' || - textBuffer[text_index_2] == '\t') + if (text[text_index_2] == ' ' || + text[text_index_2] == '\t') { if (first_encountered != 2) { - if (textBuffer[text_index] != '=' && + if (text[text_index] != '=' && substr.find("public_segment .data64") == std::string::npos && !kInStruct) substr += "public_segment .data64 "; @@ -519,7 +519,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil continue; } - if (textBuffer[text_index_2] == '=') + if (text[text_index_2] == '=') { if (!kInBraces) { @@ -531,7 +531,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil continue; } - substr += textBuffer[text_index_2]; + substr += text[text_index_2]; } for (auto& clType : kCompilerTypes) @@ -586,13 +586,13 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil syntaxLeaf.fUserValue += substr; kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf); - if (textBuffer[text_index] == '=') + if (text[text_index] == '=') break; } // function handler. - if (textBuffer[text_index] == '(' && !fnFound && !kIfFound) + if (text[text_index] == '(' && !fnFound && !kIfFound) { std::string substr; std::string args_buffer; @@ -600,20 +600,20 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil bool type_crossed = false; - for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size(); + for (size_t idx = text.find('(') + 1; idx < text.size(); ++idx) { - if (textBuffer[idx] == ',') + if (text[idx] == ',') continue; - if (textBuffer[idx] == ' ') + if (text[idx] == ' ') continue; - if (textBuffer[idx] == ')') + if (text[idx] == ')') break; } - for (char substr_first_index : textBuffer) + for (char substr_first_index : text) { if (substr_first_index != ',') args_buffer += substr_first_index; @@ -649,7 +649,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil } } - for (char _text_i : textBuffer) + for (char _text_i : text) { if (_text_i == '\t' || _text_i == ' ') { @@ -692,27 +692,27 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil fnFound = true; } - kCompilerFunctions.push_back(textBuffer); + kCompilerFunctions.push_back(text); } - if (textBuffer[text_index] == '-' && textBuffer[text_index + 1] == '-') + if (text[text_index] == '-' && text[text_index + 1] == '-') { - textBuffer = textBuffer.replace(textBuffer.find("--"), strlen("--"), ""); + text = text.replace(text.find("--"), strlen("--"), ""); - for (int _text_i = 0; _text_i < textBuffer.size(); ++_text_i) + for (int _text_i = 0; _text_i < text.size(); ++_text_i) { - if (textBuffer[_text_i] == '\t' || textBuffer[_text_i] == ' ') - textBuffer.erase(_text_i, 1); + if (text[_text_i] == '\t' || text[_text_i] == ' ') + text.erase(_text_i, 1); } syntaxLeaf.fUserValue += "sub "; - syntaxLeaf.fUserValue += textBuffer; + syntaxLeaf.fUserValue += text; kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf); break; } - if (textBuffer[text_index] == '}') + if (text[text_index] == '}') { kRegisterCounter = kStartUsable; @@ -1300,7 +1300,7 @@ public: explicit AssemblyCCInterface() = default; ~AssemblyCCInterface() override = default; - TOOLCHAINKIT_COPY_DEFAULT(AssemblyCCInterface); + LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); [[maybe_unused]] static Int32 Arch() noexcept { @@ -1478,7 +1478,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////////// -#include <Version.h> +#include <LibCompiler/Version.h> #define kPrintF printf #define kSplashCxx() \ @@ -1493,7 +1493,7 @@ static void cc_print_help() #define kExt ".c" -TOOLCHAINKIT_MODULE(ZkaOSCompilerCLang64x0) +LIBCOMPILER_MODULE(ZkaOSCompilerCLang64x0) { kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/CCompilerARM64.cc b/dev/LibCompiler/src/CCompilerARM64.cc index d985b2e..476596d 100644 --- a/dev/LibCompiler/src/CCompilerARM64.cc +++ b/dev/LibCompiler/src/CCompilerARM64.cc @@ -147,10 +147,10 @@ public: explicit CompilerFrontendARM64() = default; ~CompilerFrontendARM64() override = default; - TOOLCHAINKIT_COPY_DEFAULT(CompilerFrontendARM64); + LIBCOMPILER_COPY_DEFAULT(CompilerFrontendARM64); std::string Check(const char* text, const char* file); - bool Compile(const std::string text, const std::string file) override; + bool Compile(std::string text, const std::string file) override; const char* Language() override { @@ -200,10 +200,8 @@ namespace Details ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerFrontendARM64::Compile(const std::string text, const std::string file) +bool CompilerFrontendARM64::Compile(std::string text, const std::string file) { - std::string textBuffer = text; - bool typeFound = false; bool fnFound = false; @@ -216,7 +214,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi std::mt19937 generator(seq); // start parsing - for (size_t text_index = 0; text_index < textBuffer.size(); ++text_index) + for (size_t text_index = 0; text_index < text.size(); ++text_index) { auto syntaxLeaf = LibCompiler::SyntaxLeafList::SyntaxLeaf(); @@ -227,7 +225,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi if (!typeFound) { - auto substr = textBuffer.substr(text_index); + auto substr = text.substr(text_index); std::string match_type; for (size_t y = 0; y < substr.size(); ++y) @@ -255,7 +253,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi break; } - if (textBuffer.find('(') != std::string::npos) + if (text.find('(') != std::string::npos) { syntaxLeaf.fUserValue = buf; @@ -274,7 +272,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi } } - if (textBuffer[text_index] == '{') + if (text[text_index] == '{') { if (kInStruct) { @@ -288,7 +286,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi } // return keyword handler - if (textBuffer[text_index] == 'r') + if (text[text_index] == 'r') { std::string return_keyword; return_keyword += "return"; @@ -297,18 +295,18 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi std::string value; - for (size_t return_index = text_index; return_index < textBuffer.size(); + for (size_t return_index = text_index; return_index < text.size(); ++return_index) { - if (textBuffer[return_index] != return_keyword[index]) + if (text[return_index] != return_keyword[index]) { for (size_t value_index = return_index; - value_index < textBuffer.size(); ++value_index) + value_index < text.size(); ++value_index) { - if (textBuffer[value_index] == ';') + if (text[value_index] == ';') break; - value += textBuffer[value_index]; + value += text[value_index]; } break; @@ -354,10 +352,10 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi } } - if (textBuffer[text_index] == 'i' && textBuffer[text_index + 1] == 'f') + if (text[text_index] == 'i' && text[text_index + 1] == 'f') { - auto expr = textBuffer.substr(text_index + 2); - textBuffer.erase(text_index, 2); + auto expr = text.substr(text_index + 2); + text.erase(text_index, 2); if (expr.find("{") != std::string::npos) { @@ -370,7 +368,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); - kIfFunction = "__TOOLCHAINKIT_IF_PROC_"; + kIfFunction = "__LIBCOMPILER_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntaxLeaf.fUserValue = "\tlda r12, extern_segment "; @@ -387,32 +385,32 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi // Parse expressions and instructions here. // what does this mean? // we encounter an assignment, or we reached the end of an expression. - if (textBuffer[text_index] == '=' || textBuffer[text_index] == ';') + if (text[text_index] == '=' || text[text_index] == ';') { if (fnFound) continue; if (kIfFound) continue; - if (textBuffer[text_index] == ';' && kInStruct) + if (text[text_index] == ';' && kInStruct) continue; - if (textBuffer.find("typedef ") != std::string::npos) + if (text.find("typedef ") != std::string::npos) continue; - if (textBuffer[text_index] == '=' && kInStruct) + if (text[text_index] == '=' && kInStruct) { - Details::print_error("assignement of value in struct " + textBuffer, + Details::print_error("assignement of value in struct " + text, file); continue; } - if (textBuffer[text_index] == ';' && kInStruct) + if (text[text_index] == ';' && kInStruct) { bool space_found_ = false; std::string sym; - for (auto& ch : textBuffer) + for (auto& ch : text) { if (ch == ' ') { @@ -437,26 +435,26 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi continue; } - if (textBuffer[text_index] == '=' && kInStruct) + if (text[text_index] == '=' && kInStruct) { continue; } - if (textBuffer[text_index + 1] == '=' || - textBuffer[text_index - 1] == '!' || - textBuffer[text_index - 1] == '<' || - textBuffer[text_index - 1] == '>') + if (text[text_index + 1] == '=' || + text[text_index - 1] == '!' || + text[text_index - 1] == '<' || + text[text_index - 1] == '>') { continue; } std::string substr; - if (textBuffer.find('=') != std::string::npos && kInBraces && !kIfFound) + if (text.find('=') != std::string::npos && kInBraces && !kIfFound) { - if (textBuffer.find("*") != std::string::npos) + if (text.find("*") != std::string::npos) { - if (textBuffer.find("=") > textBuffer.find("*")) + if (text.find("=") > text.find("*")) substr += "\tlda "; else substr += "\tldw "; @@ -466,7 +464,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi substr += "\tldw "; } } - else if (textBuffer.find('=') != std::string::npos && !kInBraces) + else if (text.find('=') != std::string::npos && !kInBraces) { substr += "stw public_segment .data64 "; } @@ -475,10 +473,10 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi std::string str_name; - for (size_t text_index_2 = 0; text_index_2 < textBuffer.size(); + for (size_t text_index_2 = 0; text_index_2 < text.size(); ++text_index_2) { - if (textBuffer[text_index_2] == '\"') + if (text[text_index_2] == '\"') { ++text_index_2; @@ -486,29 +484,29 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi // string. substr += '"'; - for (; text_index_2 < textBuffer.size(); ++text_index_2) + for (; text_index_2 < text.size(); ++text_index_2) { - if (textBuffer[text_index_2] == '\"') + if (text[text_index_2] == '\"') break; - substr += textBuffer[text_index_2]; + substr += text[text_index_2]; } } - if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}') + if (text[text_index_2] == '{' || text[text_index_2] == '}') continue; - if (textBuffer[text_index_2] == ';') + if (text[text_index_2] == ';') { break; } - if (textBuffer[text_index_2] == ' ' || - textBuffer[text_index_2] == '\t') + if (text[text_index_2] == ' ' || + text[text_index_2] == '\t') { if (first_encountered != 2) { - if (textBuffer[text_index] != '=' && + if (text[text_index] != '=' && substr.find("public_segment .data64") == std::string::npos && !kInStruct) substr += "public_segment .data64 "; @@ -519,7 +517,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi continue; } - if (textBuffer[text_index_2] == '=') + if (text[text_index_2] == '=') { if (!kInBraces) { @@ -531,7 +529,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi continue; } - substr += textBuffer[text_index_2]; + substr += text[text_index_2]; } for (auto& clType : kCompilerTypes) @@ -586,13 +584,13 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi syntaxLeaf.fUserValue += substr; kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf); - if (textBuffer[text_index] == '=') + if (text[text_index] == '=') break; } // function handler. - if (textBuffer[text_index] == '(' && !fnFound && !kIfFound) + if (text[text_index] == '(' && !fnFound && !kIfFound) { std::string substr; std::string args_buffer; @@ -600,20 +598,20 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi bool type_crossed = false; - for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size(); + for (size_t idx = text.find('(') + 1; idx < text.size(); ++idx) { - if (textBuffer[idx] == ',') + if (text[idx] == ',') continue; - if (textBuffer[idx] == ' ') + if (text[idx] == ' ') continue; - if (textBuffer[idx] == ')') + if (text[idx] == ')') break; } - for (char substr_first_index : textBuffer) + for (char substr_first_index : text) { if (substr_first_index != ',') args_buffer += substr_first_index; @@ -649,7 +647,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi } } - for (char _text_i : textBuffer) + for (char _text_i : text) { if (_text_i == '\t' || _text_i == ' ') { @@ -692,27 +690,27 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi fnFound = true; } - kCompilerFunctions.push_back(textBuffer); + kCompilerFunctions.push_back(text); } - if (textBuffer[text_index] == '-' && textBuffer[text_index + 1] == '-') + if (text[text_index] == '-' && text[text_index + 1] == '-') { - textBuffer = textBuffer.replace(textBuffer.find("--"), strlen("--"), ""); + text = text.replace(text.find("--"), strlen("--"), ""); - for (int _text_i = 0; _text_i < textBuffer.size(); ++_text_i) + for (int _text_i = 0; _text_i < text.size(); ++_text_i) { - if (textBuffer[_text_i] == '\t' || textBuffer[_text_i] == ' ') - textBuffer.erase(_text_i, 1); + if (text[_text_i] == '\t' || text[_text_i] == ' ') + text.erase(_text_i, 1); } syntaxLeaf.fUserValue += "sub "; - syntaxLeaf.fUserValue += textBuffer; + syntaxLeaf.fUserValue += text; kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf); break; } - if (textBuffer[text_index] == '}') + if (text[text_index] == '}') { kRegisterCounter = kStartUsable; @@ -1300,11 +1298,11 @@ public: explicit AssemblyCCInterface() = default; ~AssemblyCCInterface() override = default; - TOOLCHAINKIT_COPY_DEFAULT(AssemblyCCInterface); + LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface); [[maybe_unused]] static Int32 Arch() noexcept { - return LibCompiler::AssemblyFactory::kArchARM64; + return LibCompiler::AssemblyFactory::kArchAARCH64; } Int32 CompileToFormat(std::string& src, Int32 arch) override @@ -1478,7 +1476,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////////// -#include <Version.h> +#include <LibCompiler/Version.h> #define kPrintF printf #define kSplashCxx() \ @@ -1493,7 +1491,7 @@ static void cc_print_help() #define kCExtension ".c" -TOOLCHAINKIT_MODULE(ZkaOSCompilerCLangARM64) +LIBCOMPILER_MODULE(ZkaOSCompilerCLangARM64) { kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); @@ -1505,7 +1503,7 @@ TOOLCHAINKIT_MODULE(ZkaOSCompilerCLangARM64) bool skip = false; kFactory.Mount(new AssemblyCCInterface()); - kMachine = LibCompiler::AssemblyFactory::kArchARM64; + kMachine = LibCompiler::AssemblyFactory::kArchAARCH64; kCompilerFrontend = new CompilerFrontendARM64(); for (auto index = 1UL; index < argc; ++index) diff --git a/dev/LibCompiler/src/CCompilerPower64.cc b/dev/LibCompiler/src/CCompilerPower64.cc index 328c429..4338c68 100644 --- a/dev/LibCompiler/src/CCompilerPower64.cc +++ b/dev/LibCompiler/src/CCompilerPower64.cc @@ -70,20 +70,20 @@ namespace Details struct CompilerState final { std::vector<LibCompiler::SyntaxLeafList> fSyntaxTreeList; - std::vector<CompilerRegisterMap> kStackFrame; - std::vector<CompilerStructMap> kStructMap; + std::vector<CompilerRegisterMap> kStackFrame; + std::vector<CompilerStructMap> kStructMap; LibCompiler::SyntaxLeafList* fSyntaxTree{nullptr}; - std::unique_ptr<std::ofstream> fOutputAssembly; - std::string fLastFile; - std::string fLastError; - bool fVerbose; + std::unique_ptr<std::ofstream> fOutputAssembly; + std::string fLastFile; + std::string fLastError; + bool fVerbose; }; } // namespace Details static Details::CompilerState kState; -static SizeType kErrorLimit = 100; -static std::string kIfFunction = ""; -static Int32 kAcceptableErrors = 0; +static SizeType kErrorLimit = 100; +static std::string kIfFunction = ""; +static Int32 kAcceptableErrors = 0; namespace Details { @@ -122,14 +122,14 @@ static std::string kRegisterPrefix = kAsmRegisterPrefix; ///////////////////////////////////////// -static std::vector<std::string> kFileList; -static LibCompiler::AssemblyFactory kFactory; -static bool kInStruct = false; -static bool kOnWhileLoop = false; -static bool kOnForLoop = false; -static bool kInBraces = false; -static bool kIfFound = false; -static size_t kBracesCount = 0UL; +static std::vector<std::string> kFileList; +static LibCompiler::AssemblyFactory kFactory; +static bool kInStruct = false; +static bool kOnWhileLoop = false; +static bool kOnForLoop = false; +static bool kInBraces = false; +static bool kIfFound = false; +static size_t kBracesCount = 0UL; /* @brief C compiler backend for C */ class CompilerFrontendPower64 final : public LibCompiler::ICompilerFrontend @@ -138,10 +138,10 @@ public: explicit CompilerFrontendPower64() = default; ~CompilerFrontendPower64() override = default; - TOOLCHAINKIT_COPY_DEFAULT(CompilerFrontendPower64); + LIBCOMPILER_COPY_DEFAULT(CompilerFrontendPower64); std::string Check(const char* text, const char* file); - bool Compile(const std::string text, const std::string file) override; + bool Compile(std::string text, const std::string file) override; const char* Language() override { @@ -149,9 +149,9 @@ public: } }; -static CompilerFrontendPower64* kCompilerFrontend = nullptr; +static CompilerFrontendPower64* kCompilerFrontend = nullptr; static std::vector<Details::CompilerType> kCompilerVariables; -static std::vector<std::string> kCompilerFunctions; +static std::vector<std::string> kCompilerFunctions; static std::vector<Details::CompilerType> kCompilerTypes; namespace Details @@ -191,9 +191,9 @@ namespace Details ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerFrontendPower64::Compile(const std::string text, const std::string file) +bool CompilerFrontendPower64::Compile(std::string text_, const std::string file) { - std::string textBuffer = text; + std::string text = text_; bool typeFound = false; bool fnFound = false; @@ -207,7 +207,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string std::mt19937 generator(seq); // start parsing - for (size_t text_index = 0; text_index < textBuffer.size(); ++text_index) + for (size_t text_index = 0; text_index < text.size(); ++text_index) { auto syntaxLeaf = LibCompiler::SyntaxLeafList::SyntaxLeaf(); @@ -218,7 +218,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string if (!typeFound) { - auto substr = textBuffer.substr(text_index); + auto substr = text.substr(text_index); std::string match_type; for (size_t y = 0; y < substr.size(); ++y) @@ -246,7 +246,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string break; } - if (textBuffer.find('(') != std::string::npos) + if (text.find('(') != std::string::npos) { syntaxLeaf.fUserValue = buf; @@ -265,7 +265,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string } } - if (textBuffer[text_index] == '{') + if (text[text_index] == '{') { if (kInStruct) { @@ -279,7 +279,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string } // return keyword handler - if (textBuffer[text_index] == 'r') + if (text[text_index] == 'r') { std::string return_keyword; return_keyword += "return"; @@ -288,18 +288,18 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string std::string value; - for (size_t return_index = text_index; return_index < textBuffer.size(); + for (size_t return_index = text_index; return_index < text.size(); ++return_index) { - if (textBuffer[return_index] != return_keyword[index]) + if (text[return_index] != return_keyword[index]) { for (size_t value_index = return_index; - value_index < textBuffer.size(); ++value_index) + value_index < text.size(); ++value_index) { - if (textBuffer[value_index] == ';') + if (text[value_index] == ';') break; - value += textBuffer[value_index]; + value += text[value_index]; } break; @@ -364,10 +364,10 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string } } - if (textBuffer[text_index] == 'i' && textBuffer[text_index + 1] == 'f') + if (text[text_index] == 'i' && text[text_index + 1] == 'f') { - auto expr = textBuffer.substr(text_index + 2); - textBuffer.erase(text_index, 2); + auto expr = text.substr(text_index + 2); + text.erase(text_index, 2); if (expr.find("{") != std::string::npos) { @@ -380,7 +380,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string if (expr.find(")") != std::string::npos) expr.erase(expr.find(")")); - kIfFunction = "__TOOLCHAINKIT_IF_PROC_"; + kIfFunction = "__LIBCOMPILER_IF_PROC_"; kIfFunction += std::to_string(time_off._Raw); syntaxLeaf.fUserValue = @@ -398,32 +398,32 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string // Parse expressions and instructions here. // what does this mean? // we encounter an assignment, or we reached the end of an expression. - if (textBuffer[text_index] == '=' || textBuffer[text_index] == ';') + if (text[text_index] == '=' || text[text_index] == ';') { if (fnFound) continue; if (kIfFound) continue; - if (textBuffer[text_index] == ';' && kInStruct) + if (text[text_index] == ';' && kInStruct) continue; - if (textBuffer.find("typedef ") != std::string::npos) + if (text.find("typedef ") != std::string::npos) continue; - if (textBuffer[text_index] == '=' && kInStruct) + if (text[text_index] == '=' && kInStruct) { Details::print_error( - "assignement of value inside a struct " + textBuffer, file); + "assignement of value inside a struct " + text, file); continue; } - if (textBuffer[text_index] == ';' && kInStruct) + if (text[text_index] == ';' && kInStruct) { bool space_found_ = false; std::string sym; - for (auto& ch : textBuffer) + for (auto& ch : text) { if (ch == ' ') { @@ -448,26 +448,26 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string continue; } - if (textBuffer[text_index] == '=' && kInStruct) + if (text[text_index] == '=' && kInStruct) { continue; } - if (textBuffer[text_index + 1] == '=' || - textBuffer[text_index - 1] == '!' || - textBuffer[text_index - 1] == '<' || - textBuffer[text_index - 1] == '>') + if (text[text_index + 1] == '=' || + text[text_index - 1] == '!' || + text[text_index - 1] == '<' || + text[text_index - 1] == '>') { continue; } std::string substr; - if (textBuffer.find('=') != std::string::npos && kInBraces && !kIfFound) + if (text.find('=') != std::string::npos && kInBraces && !kIfFound) { - if (textBuffer.find("*") != std::string::npos) + if (text.find("*") != std::string::npos) { - if (textBuffer.find("=") > textBuffer.find("*")) + if (text.find("=") > text.find("*")) substr += "\tli "; else substr += "\tli "; @@ -477,7 +477,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string substr += "\tli "; } } - else if (textBuffer.find('=') != std::string::npos && !kInBraces) + else if (text.find('=') != std::string::npos && !kInBraces) { substr += "stw public_segment .data64 "; } @@ -486,10 +486,10 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string std::string str_name; - for (size_t text_index_2 = 0; text_index_2 < textBuffer.size(); + for (size_t text_index_2 = 0; text_index_2 < text.size(); ++text_index_2) { - if (textBuffer[text_index_2] == '\"') + if (text[text_index_2] == '\"') { ++text_index_2; @@ -497,29 +497,29 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string // string. substr += '"'; - for (; text_index_2 < textBuffer.size(); ++text_index_2) + for (; text_index_2 < text.size(); ++text_index_2) { - if (textBuffer[text_index_2] == '\"') + if (text[text_index_2] == '\"') break; - substr += textBuffer[text_index_2]; + substr += text[text_index_2]; } } - if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}') + if (text[text_index_2] == '{' || text[text_index_2] == '}') continue; - if (textBuffer[text_index_2] == ';') + if (text[text_index_2] == ';') { break; } - if (textBuffer[text_index_2] == ' ' || - textBuffer[text_index_2] == '\t') + if (text[text_index_2] == ' ' || + text[text_index_2] == '\t') { if (first_encountered != 2) { - if (textBuffer[text_index] != '=' && + if (text[text_index] != '=' && substr.find("public_segment .data64") == std::string::npos && !kInStruct) substr += "public_segment .data64 "; @@ -530,7 +530,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string continue; } - if (textBuffer[text_index_2] == '=') + if (text[text_index_2] == '=') { if (!kInBraces) { @@ -542,7 +542,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string continue; } - substr += textBuffer[text_index_2]; + substr += text[text_index_2]; } for (auto& clType : kCompilerTypes) @@ -582,7 +582,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string kCompilerVariables.push_back({.fName = substr}); - if (textBuffer[text_index] == ';') + if (text[text_index] == ';') break; std::string reg = kAsmRegisterPrefix; @@ -614,7 +614,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string // function handler. - if (textBuffer[text_index] == '(' && !fnFound && !kIfFound) + if (text[text_index] == '(' && !fnFound && !kIfFound) { std::string substr; std::string args_buffer; @@ -622,20 +622,20 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string bool type_crossed = false; - for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size(); + for (size_t idx = text.find('(') + 1; idx < text.size(); ++idx) { - if (textBuffer[idx] == ',') + if (text[idx] == ',') continue; - if (textBuffer[idx] == ' ') + if (text[idx] == ' ') continue; - if (textBuffer[idx] == ')') + if (text[idx] == ')') break; } - for (char substr_first_index : textBuffer) + for (char substr_first_index : text) { if (substr_first_index != ',') args_buffer += substr_first_index; @@ -671,7 +671,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string } } - for (char _text_i : textBuffer) + for (char _text_i : text) { if (_text_i == '\t' || _text_i == ' ') { @@ -715,27 +715,27 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string fnFound = true; } - kCompilerFunctions.push_back(textBuffer); + kCompilerFunctions.push_back(text); } - if (textBuffer[text_index] == '-' && textBuffer[text_index + 1] == '-') + if (text[text_index] == '-' && text[text_index + 1] == '-') { - textBuffer = textBuffer.replace(textBuffer.find("--"), strlen("--"), ""); + text = text.replace(text.find("--"), strlen("--"), ""); - for (int _text_i = 0; _text_i < textBuffer.size(); ++_text_i) + for (int _text_i = 0; _text_i < text.size(); ++_text_i) { - if (textBuffer[_text_i] == '\t' || textBuffer[_text_i] == ' ') - textBuffer.erase(_text_i, 1); + if (text[_text_i] == '\t' || text[_text_i] == ' ') + text.erase(_text_i, 1); } syntaxLeaf.fUserValue += "dec "; - syntaxLeaf.fUserValue += textBuffer; + syntaxLeaf.fUserValue += text; kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf); break; } - if (textBuffer[text_index] == '}') + if (text[text_index] == '}') { kRegisterCounter = kStartUsable; @@ -1323,7 +1323,7 @@ public: explicit AssemblyMountpointCLang() = default; ~AssemblyMountpointCLang() override = default; - TOOLCHAINKIT_COPY_DEFAULT(AssemblyMountpointCLang); + LIBCOMPILER_COPY_DEFAULT(AssemblyMountpointCLang); [[maybe_unused]] static Int32 Arch() noexcept { @@ -1498,7 +1498,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////////// -#include <Version.h> +#include <LibCompiler/Version.h> #define kPrintF printf #define kSplashCxx() \ @@ -1513,7 +1513,7 @@ static void cc_print_help() #define kExt ".c" -TOOLCHAINKIT_MODULE(ZkaOSCompilerCLangPowerPC) +LIBCOMPILER_MODULE(ZkaOSCompilerCLangPowerPC) { kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc index ba805bd..e2b66f2 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc @@ -9,7 +9,6 @@ /// BUGS: 1 -#include <cstdio> #define kPrintF printf #define kExitOK (EXIT_SUCCESS) @@ -24,6 +23,8 @@ #include <LibCompiler/Parser.h> #include <LibCompiler/UUID.h> +#include <cstdio> + /* ZKA C++ Compiler */ /* This is part of the LibCompiler. */ /* (c) Theater Quality Corp. */ @@ -162,7 +163,7 @@ public: explicit CompilerFrontendCPlusPlus() = default; ~CompilerFrontendCPlusPlus() override = default; - TOOLCHAINKIT_COPY_DEFAULT(CompilerFrontendCPlusPlus); + LIBCOMPILER_COPY_DEFAULT(CompilerFrontendCPlusPlus); bool Compile(const std::string text, const std::string file) override; @@ -431,13 +432,13 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text, ++indexFnName; } - syntax_tree.fUserValue = "public_segment .code64 __TOOLCHAINKIT_" + fnName + "\n"; + syntax_tree.fUserValue = "public_segment .code64 __LIBCOMPILER_" + fnName + "\n"; ++kFunctionEmbedLevel; break; tk_write_assembly: - syntax_tree.fUserValue = "jmp __TOOLCHAINKIT_" + fnName + "\n"; + syntax_tree.fUserValue = "jmp __LIBCOMPILER_" + fnName + "\n"; } case LibCompiler::KeywordKind::kKeywordKindFunctionEnd: { if (text.ends_with(";")) @@ -572,8 +573,8 @@ tk_write_assembly: if (valueOfVar[0] == '\"') { - syntax_tree.fUserValue = "segment .data64 __TOOLCHAINKIT_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n\n"; - syntax_tree.fUserValue += instr + kRegisterList[kRegisterMap.size() - 1] + ", " + "__TOOLCHAINKIT_LOCAL_VAR_" + varName + "\n"; + syntax_tree.fUserValue = "segment .data64 __LIBCOMPILER_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n\n"; + syntax_tree.fUserValue += instr + kRegisterList[kRegisterMap.size() - 1] + ", " + "__LIBCOMPILER_LOCAL_VAR_" + varName + "\n"; } else { @@ -589,8 +590,8 @@ tk_write_assembly: if (valueOfVar[0] == '\"') { - syntax_tree.fUserValue = "segment .data64 __TOOLCHAINKIT_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n"; - syntax_tree.fUserValue += instr + kRegisterList[kRegisterMap.size()] + ", " + "__TOOLCHAINKIT_LOCAL_VAR_" + varName + "\n"; + syntax_tree.fUserValue = "segment .data64 __LIBCOMPILER_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n"; + syntax_tree.fUserValue += instr + kRegisterList[kRegisterMap.size()] + ", " + "__LIBCOMPILER_LOCAL_VAR_" + varName + "\n"; } else { @@ -767,7 +768,7 @@ tk_write_assembly: } else { - syntax_tree.fUserValue = "__TOOLCHAINKIT_LOCAL_RETURN_STRING: db " + subText + ", 0\nmov rcx, __TOOLCHAINKIT_LOCAL_RETURN_STRING\n"; + syntax_tree.fUserValue = "__LIBCOMPILER_LOCAL_RETURN_STRING: db " + subText + ", 0\nmov rcx, __LIBCOMPILER_LOCAL_RETURN_STRING\n"; syntax_tree.fUserValue += "mov rax, rcx\r\nret\n"; } @@ -806,7 +807,7 @@ public: explicit AssemblyCPlusPlusInterface() = default; ~AssemblyCPlusPlusInterface() override = default; - TOOLCHAINKIT_COPY_DEFAULT(AssemblyCPlusPlusInterface); + LIBCOMPILER_COPY_DEFAULT(AssemblyCPlusPlusInterface); [[maybe_unused]] static Int32 Arch() noexcept { @@ -924,7 +925,7 @@ static void cxx_print_help() ".cpp", ".cxx", ".cc", ".c++", ".cp" \ } -TOOLCHAINKIT_MODULE(CompilerCPlusPlusX8664) +LIBCOMPILER_MODULE(CompilerCPlusPlusX8664) { bool skip = false; diff --git a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc b/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc index 906ba4e..90db68b 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc @@ -74,7 +74,7 @@ namespace Details explicit bpp_pragma() = default; ~bpp_pragma() = default; - TOOLCHAINKIT_COPY_DEFAULT(bpp_pragma); + LIBCOMPILER_COPY_DEFAULT(bpp_pragma); std::string fMacroName; bpp_parser_fn_t fParse; @@ -908,7 +908,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) ///////////////////////////////////////////////////////////////////////////////////////// -TOOLCHAINKIT_MODULE(CPlusPlusPreprocessorMain) +LIBCOMPILER_MODULE(CPlusPlusPreprocessorMain) { try { @@ -931,7 +931,7 @@ TOOLCHAINKIT_MODULE(CPlusPlusPreprocessorMain) Details::bpp_macro macro_zka; - macro_zka.fName = "__TOOLCHAINKIT__"; + macro_zka.fName = "__LIBCOMPILER__"; macro_zka.fValue = "1"; kMacros.push_back(macro_zka); @@ -1044,7 +1044,7 @@ TOOLCHAINKIT_MODULE(CPlusPlusPreprocessorMain) } if (kFiles.empty()) - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; for (auto& file : kFiles) { diff --git a/dev/LibCompiler/src/CPlusPlusLinkerELF.cc b/dev/LibCompiler/src/CPlusPlusLinkerELF.cc index 5b85afc..de7cbd1 100644 --- a/dev/LibCompiler/src/CPlusPlusLinkerELF.cc +++ b/dev/LibCompiler/src/CPlusPlusLinkerELF.cc @@ -55,8 +55,8 @@ namespace Details { struct DynamicLinkerBlob final { - std::vector<CharType> fPefBlob; // PEF code/bss/data blob. - std::uintptr_t fAEOffset; // the offset of the PEF container header.. + std::vector<CharType> mBlob; // PEF code/bss/data blob. + std::uintptr_t mObjOffset; // the offset of the PEF container header.. }; } @@ -84,7 +84,7 @@ static uintptr_t kByteCount = 1024; /// @brief ZKA 64-bit Linker. /// @note This linker is made for XCOFF executable, thus ZKA based OSes. -TOOLCHAINKIT_MODULE(DynamicLinker64XCOFF) +LIBCOMPILER_MODULE(DynamicLinker64XCOFF) { return EXIT_SUCCESS; } diff --git a/dev/LibCompiler/src/CPlusPlusLinkerPEF.cc b/dev/LibCompiler/src/CPlusPlusLinkerPEF.cc index 2e5e40b..a7abc92 100644 --- a/dev/LibCompiler/src/CPlusPlusLinkerPEF.cc +++ b/dev/LibCompiler/src/CPlusPlusLinkerPEF.cc @@ -55,8 +55,8 @@ namespace Details { struct DynamicLinkerBlob final { - std::vector<CharType> fPefBlob; // PEF code/bss/data blob. - std::uintptr_t fAEOffset; // the offset of the PEF container header.. + std::vector<CharType> mBlob; // PEF code/bss/data blob. + std::uintptr_t mObjOffset; // the offset of the PEF container header.. }; } @@ -92,7 +92,7 @@ static uintptr_t kByteCount = 1024; /// @brief ZKA 64-bit Linker. /// @note This linker is made for PEF executable, thus ZKA based OSes. -TOOLCHAINKIT_MODULE(DynamicLinker64PEF) +LIBCOMPILER_MODULE(DynamicLinker64PEF) { bool is_executable = true; @@ -213,14 +213,14 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) if (kOutput.empty()) { kStdOut << "no output filename set." << std::endl; - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } // sanity check. if (kObjectList.empty()) { kStdOut << "no input files." << std::endl; - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } else { @@ -234,7 +234,7 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) // if filesystem doesn't find file // -> throw error. kStdOut << "no such file: " << obj << std::endl; - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } } } @@ -243,7 +243,7 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) if (kArch == 0) { kStdOut << "no target architecture set, can't continue." << std::endl; - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } LibCompiler::PEFContainer pef_container{}; @@ -274,7 +274,7 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) kStdOut << "error: " << strerror(errno) << "\n"; } - return TOOLCHAINKIT_FILE_NOT_FOUND; + return LIBCOMPILER_FILE_NOT_FOUND; } //! Read AE to convert as PEF. @@ -312,7 +312,7 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) "treated as a FAT binary." << std::endl; - return TOOLCHAINKIT_FAT_ERROR; + return LIBCOMPILER_FAT_ERROR; } else { @@ -412,7 +412,7 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) for (auto& byte : bytes) { - kObjectBytes.push_back({ .fPefBlob = bytes, .fAEOffset = ae_header.fStartCode }); + kObjectBytes.push_back({ .mBlob = bytes, .mObjOffset = ae_header.fStartCode }); } reader_protocol.FP.close(); @@ -422,7 +422,7 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) kStdOut << "Not an object container: " << objectFile << std::endl; // don't continue, it is a fatal error. - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } pef_container.Cpu = archs; @@ -723,14 +723,14 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) kStdOut << "Multiple symbols of " << symbol << ".\n"; } - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } // step 2.5: write program bytes. for (auto& struct_of_blob : kObjectBytes) { - output_fc.write(struct_of_blob.fPefBlob.data(), struct_of_blob.fPefBlob.size()); + output_fc.write(struct_of_blob.mBlob.data(), struct_of_blob.mBlob.size()); } if (kVerbose) @@ -765,7 +765,7 @@ TOOLCHAINKIT_MODULE(DynamicLinker64PEF) kStdOut << "file: " << kOutput << ", is corrupt, removing file...\n"; - return TOOLCHAINKIT_EXEC_ERROR; + return LIBCOMPILER_EXEC_ERROR; } return EXIT_SUCCESS; |
