diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-02-27 14:13:10 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-02-27 14:13:10 +0100 |
| commit | 11dc3ca142df08d9c1bc73f96a2eb93ead4f6924 (patch) | |
| tree | 86421bc4ad67f3bbac3a5a9d5a88926eaa9fff45 | |
| parent | 9e1a18a02c4aebac768d38cffc43c3c91015d85e (diff) | |
Encoder (AMD64): Add support for 'mov' (64-bit)
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
| -rw-r--r-- | Private/CompilerKit/AsmKit/AsmKit.hpp | 32 | ||||
| -rw-r--r-- | Private/Toolchain/64asm.cc | 8 | ||||
| -rw-r--r-- | Private/Toolchain/bin/Source/hello_amd64.masm | 2 | ||||
| -rw-r--r-- | Private/Toolchain/i64asm.cc | 49 |
4 files changed, 54 insertions, 37 deletions
diff --git a/Private/CompilerKit/AsmKit/AsmKit.hpp b/Private/CompilerKit/AsmKit/AsmKit.hpp index 88a66e7..5ad90ce 100644 --- a/Private/CompilerKit/AsmKit/AsmKit.hpp +++ b/Private/CompilerKit/AsmKit/AsmKit.hpp @@ -96,12 +96,12 @@ union NumberCast8 final { UInt8 raw; }; -class PlatformAssembler { +class Encoder { public: - explicit PlatformAssembler() = default; - ~PlatformAssembler() = default; + explicit Encoder() = default; + ~Encoder() = default; - MPCC_COPY_DEFAULT(PlatformAssembler); + MPCC_COPY_DEFAULT(Encoder); virtual std::string CheckLine(std::string& line, const std::string& file) = 0; virtual bool WriteLine(std::string& line, const std::string& file) = 0; @@ -110,12 +110,12 @@ class PlatformAssembler { #ifdef __ASM_NEED_AMD64__ -class PlatformAssemblerAMD64 final : public PlatformAssembler { +class EncoderAMD64 final : public Encoder { public: - explicit PlatformAssemblerAMD64() = default; - ~PlatformAssemblerAMD64() = default; + explicit EncoderAMD64() = default; + ~EncoderAMD64() = default; - MPCC_COPY_DEFAULT(PlatformAssemblerAMD64); + MPCC_COPY_DEFAULT(EncoderAMD64); virtual std::string CheckLine(std::string& line, const std::string& file) override; @@ -132,12 +132,12 @@ class PlatformAssemblerAMD64 final : public PlatformAssembler { #ifdef __ASM_NEED_64x0__ -class PlatformAssembler64x0 final : public PlatformAssembler { +class Encoder64x0 final : public Encoder { public: - explicit PlatformAssembler64x0() = default; - ~PlatformAssembler64x0() = default; + explicit Encoder64x0() = default; + ~Encoder64x0() = default; - MPCC_COPY_DEFAULT(PlatformAssembler64x0); + MPCC_COPY_DEFAULT(Encoder64x0); virtual std::string CheckLine(std::string& line, const std::string& file) override; @@ -150,12 +150,12 @@ class PlatformAssembler64x0 final : public PlatformAssembler { #ifdef __ASM_NEED_32x0__ -class PlatformAssembler32x0 final : public PlatformAssembler { +class Encoder32x0 final : public Encoder { public: - explicit PlatformAssembler32x0() = default; - ~PlatformAssembler32x0() = default; + explicit Encoder32x0() = default; + ~Encoder32x0() = default; - MPCC_COPY_DEFAULT(PlatformAssembler32x0); + MPCC_COPY_DEFAULT(Encoder32x0); virtual std::string CheckLine(std::string& line, const std::string& file) override; diff --git a/Private/Toolchain/64asm.cc b/Private/Toolchain/64asm.cc index 15dc568..982de95 100644 --- a/Private/Toolchain/64asm.cc +++ b/Private/Toolchain/64asm.cc @@ -172,7 +172,7 @@ MPCC_MODULE(HCoreAssembler64000) { ///////////////////////////////////////////////////////////////////////////////////////// - CompilerKit::PlatformAssembler64x0 asm64; + CompilerKit::Encoder64x0 asm64; while (std::getline(file_ptr, line)) { if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { @@ -442,7 +442,7 @@ bool is_valid(const std::string &str) { ///////////////////////////////////////////////////////////////////////////////////////// -std::string CompilerKit::PlatformAssembler64x0::CheckLine( +std::string CompilerKit::Encoder64x0::CheckLine( std::string &line, const std::string &file) { std::string err_str; @@ -551,7 +551,7 @@ std::string CompilerKit::PlatformAssembler64x0::CheckLine( return err_str; } -bool CompilerKit::PlatformAssembler64x0::WriteNumber(const std::size_t &pos, +bool CompilerKit::Encoder64x0::WriteNumber(const std::size_t &pos, std::string &jump_label) { if (!isdigit(jump_label[pos])) return false; @@ -658,7 +658,7 @@ bool CompilerKit::PlatformAssembler64x0::WriteNumber(const std::size_t &pos, ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerKit::PlatformAssembler64x0::WriteLine(std::string &line, +bool CompilerKit::Encoder64x0::WriteLine(std::string &line, const std::string &file) { if (ParserKit::find_word(line, "export ")) return true; diff --git a/Private/Toolchain/bin/Source/hello_amd64.masm b/Private/Toolchain/bin/Source/hello_amd64.masm index da77b1b..dd50377 100644 --- a/Private/Toolchain/bin/Source/hello_amd64.masm +++ b/Private/Toolchain/bin/Source/hello_amd64.masm @@ -11,7 +11,7 @@ retf export .text __start cli -mov rdx, rsi +mov rax, rdx int 0x80 jmp 0x100000 sti diff --git a/Private/Toolchain/i64asm.cc b/Private/Toolchain/i64asm.cc index cd441a0..95e722b 100644 --- a/Private/Toolchain/i64asm.cc +++ b/Private/Toolchain/i64asm.cc @@ -208,7 +208,7 @@ MPCC_MODULE(HCoreAssemblerAMD64) { ///////////////////////////////////////////////////////////////////////////////////////// - CompilerKit::PlatformAssemblerAMD64 asm64; + CompilerKit::EncoderAMD64 asm64; while (std::getline(file_ptr, line)) { if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty()) { @@ -484,7 +484,7 @@ bool is_valid(const std::string &str) { ///////////////////////////////////////////////////////////////////////////////////////// -std::string CompilerKit::PlatformAssemblerAMD64::CheckLine( +std::string CompilerKit::EncoderAMD64::CheckLine( std::string &line, const std::string &file) { std::string err_str; @@ -552,7 +552,7 @@ std::string CompilerKit::PlatformAssemblerAMD64::CheckLine( return err_str; } -bool CompilerKit::PlatformAssemblerAMD64::WriteNumber(const std::size_t &pos, +bool CompilerKit::EncoderAMD64::WriteNumber(const std::size_t &pos, std::string &jump_label) { if (!isdigit(jump_label[pos])) return false; @@ -661,7 +661,7 @@ bool CompilerKit::PlatformAssemblerAMD64::WriteNumber(const std::size_t &pos, return true; } -bool CompilerKit::PlatformAssemblerAMD64::WriteNumber32( +bool CompilerKit::EncoderAMD64::WriteNumber32( const std::size_t &pos, std::string &jump_label) { if (!isdigit(jump_label[pos])) return false; @@ -770,7 +770,7 @@ bool CompilerKit::PlatformAssemblerAMD64::WriteNumber32( return true; } -bool CompilerKit::PlatformAssemblerAMD64::WriteNumber16( +bool CompilerKit::EncoderAMD64::WriteNumber16( const std::size_t &pos, std::string &jump_label) { if (!isdigit(jump_label[pos])) return false; @@ -879,7 +879,7 @@ bool CompilerKit::PlatformAssemblerAMD64::WriteNumber16( return true; } -bool CompilerKit::PlatformAssemblerAMD64::WriteNumber8( +bool CompilerKit::EncoderAMD64::WriteNumber8( const std::size_t &pos, std::string &jump_label) { if (!isdigit(jump_label[pos])) return false; @@ -978,7 +978,7 @@ bool CompilerKit::PlatformAssemblerAMD64::WriteNumber8( ///////////////////////////////////////////////////////////////////////////////////////// -bool CompilerKit::PlatformAssemblerAMD64::WriteLine(std::string &line, +bool CompilerKit::EncoderAMD64::WriteLine(std::string &line, const std::string &file) { if (ParserKit::find_word(line, "export ")) return true; @@ -988,10 +988,10 @@ bool CompilerKit::PlatformAssemblerAMD64::WriteLine(std::string &line, }; std::vector<RegMapAMD64> regs{ - {.fName = "ax", .fModRM = 0b000}, {.fName = "cx", .fModRM = 0b001}, - {.fName = "dx", .fModRM = 0b010}, {.fName = "bx", .fModRM = 0b011}, - {.fName = "sp", .fModRM = 0b100}, {.fName = "bp", .fModRM = 0b101}, - {.fName = "si", .fModRM = 0b110}, {.fName = "di", .fModRM = 0b111}, + {.fName = "ax", .fModRM = 0x0}, {.fName = "cx", .fModRM = 0x8}, + {.fName = "dx", .fModRM = 0x2}, {.fName = "bx", .fModRM = 0x10}, + {.fName = "sp", .fModRM = 0x4}, {.fName = "bp", .fModRM = 0x12}, + {.fName = "si", .fModRM = 0x6}, {.fName = "di", .fModRM = 0x14}, }; for (auto &opcodeAMD64 : kOpcodesAMD64) { @@ -1044,16 +1044,33 @@ bool CompilerKit::PlatformAssemblerAMD64::WriteLine(std::string &line, if (bits == 64 || bits == 32) { - if (bits != 32) + if (bits >= 32) kBytes.emplace_back(opcodeAMD64.fOpcode); kBytes.emplace_back(0x89); - auto byte = 0xe0; + if (currentRegList[1].fName.find("sp") != std::string::npos) { + auto byte = 0xe0; + byte += currentRegList[0].fModRM; + + kBytes.push_back(byte); + } else if (currentRegList[1].fName.find("dx") != std::string::npos) { + auto byte = 0xd0; + byte += currentRegList[0].fModRM; + + kBytes.push_back(byte); + } else if (currentRegList[1].fName.find("ax") != std::string::npos) { + auto byte = 0xc0; + byte += currentRegList[0].fModRM; + + kBytes.push_back(byte); + } else { + auto byte = 0xf0; + byte += currentRegList[0].fModRM; + + kBytes.push_back(byte); + } - - - kBytes.push_back(byte); } else if (bits == 16) { kBytes.emplace_back(0x66); |
