From e0c75cc3dc9a55632c21ea2dc7bab1dfc44e2657 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 19 Jan 2026 10:37:08 +0100 Subject: feat: asm&frontend: codegen and assembler improvements. Signed-off-by: Amlal El Mahrouss --- src/CompilerKit/src/Assemblers/Assembler+AMD64.cc | 12 +++++----- .../src/Compilers/NectarCompiler+AMD64.cc | 26 +++++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc index d90791c..88bb78c 100644 --- a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc +++ b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc @@ -614,7 +614,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber(const std::size_t& pos, std::string& return true; } case 'o': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); res += kOrigin; if (errno != 0) { @@ -712,7 +712,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber32(const std::size_t& pos, std::strin return true; } case 'o': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); res += kOrigin; if (errno != 0) { @@ -812,7 +812,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber16(const std::size_t& pos, std::strin return true; } case 'o': { - if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) { + if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); !res) { if (errno != 0) { CompilerKit::Detail::print_error("Invalid octal number: " + jump_label, "CompilerKit"); throw std::runtime_error("invalid_octal"); @@ -820,7 +820,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber16(const std::size_t& pos, std::strin } CompilerKit::NumberCast16 num = - CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7)); + CompilerKit::NumberCast16(strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8)); if (kVerbose) { kStdOut << "AssemblerAMD64: Found a base 8 number here: " << jump_label.substr(pos) << "\n"; @@ -904,7 +904,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string return true; } case 'o': { - auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); + auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 8); res += kOrigin; if (errno != 0) { @@ -1691,7 +1691,7 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string line, std::string file) { throw std::runtime_error("invalid_org"); } - size_t base[] = {10, 16, 2, 7}; + size_t base[] = {10, 16, 2, 8}; for (size_t i = 0; i < 4; i++) { if (kOrigin = strtol(line.substr(value_pos).c_str(), nullptr, base[i]); kOrigin) { diff --git a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc b/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc index 1889035..9536864 100644 --- a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc +++ b/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc @@ -249,8 +249,9 @@ static std::vector kRegisterConventionCallList = { }; static std::size_t kFunctionEmbedLevel{}; -static CompilerKit::STLString kCurrentFunctionName{}; +static CompilerKit::STLString kCurrentIfSymbol{}; static CompilerKit::STLString kCurrentReturnAddress{}; +static bool kCurrentIfCondition{false}; /// detail namespaces @@ -328,6 +329,8 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile( break; } case CompilerKit::KeywordKind::kKeywordKindIf: { + kCurrentIfCondition = true; + std::size_t keywordPos = text.find(keyword.first.fKeywordName); std::size_t openParen = text.find("(", keywordPos); std::size_t closeParen = text.find(")", openParen); @@ -392,9 +395,9 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile( syntax_tree.fUserValue += "cmp rdi, rsi\n"; syntax_tree.fUserValue += - op.second + " __ret_" + std::to_string(kOrigin) + "_" + kCurrentFunctionName + "\n"; + op.second + " __ret_" + std::to_string(kOrigin) + "_" + kCurrentIfSymbol + "\n"; - kCurrentFunctionName = std::to_string(kOrigin) + "_" + kCurrentFunctionName; + kCurrentIfSymbol = std::to_string(kOrigin) + "_" + kCurrentIfSymbol; ++kOrigin; } @@ -521,8 +524,6 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile( ++kFunctionEmbedLevel; - kCurrentFunctionName = mangled_name; - kOriginMap.push_back({mangled_name, kOrigin}); kOrigin += 2UL; // Account for prologue instructions @@ -870,13 +871,16 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile( kOrigin += 2UL; } - if (!kNasmOutput) - syntax_tree.fUserValue += - "public_segment .code64 __ret_" + kCurrentFunctionName + "\nnop\n"; - else - syntax_tree.fUserValue += "__ret_" + kCurrentFunctionName + ":\n"; + if (kCurrentIfCondition) { + if (!kNasmOutput) + syntax_tree.fUserValue += + "public_segment .code64 __ret_" + kCurrentIfSymbol + "\nnop\n"; + else + syntax_tree.fUserValue += "__ret_" + kCurrentIfSymbol + ":\n"; - kCurrentFunctionName.clear(); + kCurrentIfSymbol.clear(); + kCurrentIfCondition = false; + } } default: { continue; -- cgit v1.2.3