diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2026-01-25 18:18:28 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2026-01-25 18:18:56 +0100 |
| commit | 8769270744d26e8884cc819bc86c5f5514be5c78 (patch) | |
| tree | 0b0ae4fed20051d41b928d423aa57e460975d7fb | |
| parent | 4ac738d6f88aba0a3222334422e4360b5d25b8ed (diff) | |
chore: frontend improvements, fixing bug in if condition generation.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | include/CompilerKit/Utilities/Compiler.h | 8 | ||||
| -rw-r--r-- | src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc | 96 | ||||
| -rw-r--r-- | test/test_samples/inner.nc | 2 |
3 files changed, 61 insertions, 45 deletions
diff --git a/include/CompilerKit/Utilities/Compiler.h b/include/CompilerKit/Utilities/Compiler.h index 621f229..cd28567 100644 --- a/include/CompilerKit/Utilities/Compiler.h +++ b/include/CompilerKit/Utilities/Compiler.h @@ -26,8 +26,8 @@ #define kWhite "\e[0;97m" #define kYellow "\e[0;33m" -#define kStdOut (std::cout << kRed << "drv: " << kWhite) -#define kStdErr (std::cerr << kYellow << "drv: " << kWhite) +#define kStdOut (std::cout << kRed << "Nectar: " << kWhite) +#define kStdErr (std::cerr << kRed << "Nectar: " << kWhite) #define kStdEndl std::endl #define kPrintF kStdOut #define kPrintErr kStdErr @@ -50,7 +50,7 @@ struct Blob final { inline void print_error(STLString reason, STLString file) noexcept { if (reason[0] == '\n') reason.erase(0, 1); - kStdErr << reason << kBlank << std::endl; + kStdErr << file << ": " << reason << kBlank << std::endl; ++kAcceptableErrors; if (kAcceptableErrors > kErrorLimit) std::exit(NECTAR_EXEC_ERROR); @@ -59,7 +59,7 @@ inline void print_error(STLString reason, STLString file) noexcept { inline void print_warning(STLString reason, STLString file) noexcept { if (reason[0] == '\n') reason.erase(0, 1); - kStdOut << kYellow << reason << kBlank << std::endl; + kStdOut << file << ": " << kYellow << reason << kBlank << std::endl; } /// @internal diff --git a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc b/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc index 81eec4b..63deddc 100644 --- a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc +++ b/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc @@ -334,8 +334,8 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile( 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); + std::size_t openParen = text.find("("); + std::size_t closeParen = text.find("):"); if (keywordPos == CompilerKit::STLString::npos || openParen == CompilerKit::STLString::npos || @@ -359,40 +359,37 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile( auto right = left.substr(left.find(op.first) + op.first.size()); + if (auto res = right.find(":"); res != CompilerKit::STLString::npos) right.erase(res); + auto tmp = left.substr(0, left.find(op.first)); - left = std::move(tmp); - - if (!nectar_get_variable_ref(left).empty()) - syntax_tree.fUserValue += "mov rdi, " + - (isnumber(left[0]) ? left - : (!nectar_get_variable_ref(left).empty() - ? left - : nectar_get_variable_ref(left))) + - "\n"; - else - syntax_tree.fUserValue += "mov rdi, " + - (isnumber(left[0]) ? left - : (nectar_get_variable_ref(left).empty() - ? "0x0" - : nectar_get_variable_ref(left))) + - "\n"; - - if (!nectar_get_variable_ref(right).empty()) - syntax_tree.fUserValue += - "lea rsi, " + - (isnumber(right[0]) - ? right - : (!nectar_get_variable_ref(right).empty() ? right - : nectar_get_variable_ref(right))) + - "\n"; - else - syntax_tree.fUserValue += - "mov rsi, " + - (isnumber(right[0]) - ? right - : (nectar_get_variable_ref(right).empty() ? "0x0" - : nectar_get_variable_ref(right))) + - "\n"; + + kStdOut << tmp << "\n"; + + if (auto var = nectar_find_variable(tmp); var) { + syntax_tree.fUserValue += "mov rdi, qword " + var->fRegister + "\n"; + delete var; + } else { + if (!isnumber(tmp[0])) { + CompilerKit::Detail::print_warning("Variable not found, treating as symbol: " + tmp, file); + } + + syntax_tree.fUserValue += "mov rdi, " + tmp + "\n"; + } + + kStdOut << right << "\n"; + + if (auto var = nectar_find_variable(right); var) { + syntax_tree.fUserValue += "mov rsi, qword " + var->fRegister + "\n"; + delete var; + } + + else { + if (!isnumber(right[0])) { + CompilerKit::Detail::print_warning("Variable not found, treating as symbol: " + right, file); + } + + syntax_tree.fUserValue += "mov rsi, " + right + "\n"; + } syntax_tree.fUserValue += "cmp rdi, rsi\n"; @@ -1233,6 +1230,8 @@ static Int32 nectar_allocate_stack_variable(const CompilerKit::STLString& var_na if (var->fStackOffset > 0) CompilerKit::Detail::print_error("Variable " + var_name + " is already defined.", "CompilerKit"); + + delete var; } VariableInfo varInfo; @@ -1251,7 +1250,7 @@ static Int32 nectar_allocate_stack_variable(const CompilerKit::STLString& var_na static VariableInfo* nectar_find_variable(const CompilerKit::STLString& var_name) { for (auto& var : kContext.fVariables) { if (var.fName == var_name) { - return &var; + return new VariableInfo(var); } } return nullptr; @@ -1282,17 +1281,30 @@ static CompilerKit::STLString nectar_get_variable_ref(const CompilerKit::STLStri varInfo->fLastUsed = kContext.fInstructionCounter; if (varInfo->fLocation == VarLocation::kRegister) { - return varInfo->fRegister; + auto reg = varInfo->fRegister; + delete varInfo; + return reg; } else { // Stack or spilled - return "qword [rbp+" + std::to_string(-varInfo->fStackOffset) + "]"; + auto reg = "qword [rbp+" + std::to_string(-varInfo->fStackOffset) + "]"; + delete varInfo; + return reg; } + + return ""; } /// \brief Allocate a register for a variable static CompilerKit::STLString nectar_allocate_register(const CompilerKit::STLString& var_name) { // Check if variable already has a register - auto* existing = nectar_find_variable(var_name); + VariableInfo* existing = nullptr; + + for (auto& var : kContext.fVariables) { + if (var.fName == var_name) { + existing = &var; + break; + } + } if (existing && existing->fLocation == VarLocation::kRegister) { return existing->fRegister; @@ -1442,6 +1454,7 @@ static CompilerKit::STLString nectar_generate_constructor_call( static CompilerKit::STLString nectar_generate_destructor_call( const CompilerKit::STLString& class_name, const CompilerKit::STLString& obj_name) { auto* varInfo = nectar_find_variable(obj_name); + if (!varInfo) { return ""; } @@ -1456,6 +1469,9 @@ static CompilerKit::STLString nectar_generate_destructor_call( } else { code += "mov r8, " + varInfo->fRegister + "\n"; } + + delete varInfo; + code += "call " + dtor_mangled + "\n"; return code; } @@ -1661,7 +1677,7 @@ NECTAR_MODULE(CompilerNectarAMD64) { CompilerKit::STLString err = "Unknown option: "; err += argv[index]; - CompilerKit::Detail::print_error(err, "necfront"); + CompilerKit::Detail::print_error(err, "necdrv"); continue; } diff --git a/test/test_samples/inner.nc b/test/test_samples/inner.nc index 05657d1..048cd53 100644 --- a/test/test_samples/inner.nc +++ b/test/test_samples/inner.nc @@ -2,7 +2,7 @@ let main() { let foo := 42; - if (foo == 42) + if (foo == 42): { const return_stub(): foo := 0; |
