summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2026-01-14 13:57:47 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2026-01-14 14:06:30 +0100
commit955ddf513eed8ee925022e88d5c10fb879b57953 (patch)
treed13713b23fcadc2c85c798d6f1372145c73b6f59
parent9cf3a4835418d4de6470dbfa6bf51b4d8b262602 (diff)
chore: Assembler and Frontend improvements.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--include/CompilerKit/AST.h1
-rw-r--r--src/CompilerKit/src/Assemblers/Assembler+AMD64.cc60
-rw-r--r--src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc47
-rw-r--r--test/test_samples/test_struct.ncpp8
4 files changed, 63 insertions, 53 deletions
diff --git a/include/CompilerKit/AST.h b/include/CompilerKit/AST.h
index d586bbc..0e869da 100644
--- a/include/CompilerKit/AST.h
+++ b/include/CompilerKit/AST.h
@@ -37,7 +37,6 @@ enum struct KeywordKind {
kKeywordKindImpl,
kKeywordKindNew,
kKeywordKindDelete,
- kKeywordKindPtrAccess,
kKeywordKindAccess,
kKeywordKindIf,
kKeywordKindElse,
diff --git a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc
index 007b03f..d90db7a 100644
--- a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc
+++ b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cc
@@ -578,7 +578,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber(const std::size_t& pos, std::string&
case 'x': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid hex number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -602,7 +602,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber(const std::size_t& pos, std::string&
case 'b': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid binary number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -625,7 +625,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber(const std::size_t& pos, std::string&
case 'o': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid octal number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -778,7 +778,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber16(const std::size_t& pos, std::strin
case 'x': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid hex number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -802,7 +802,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber16(const std::size_t& pos, std::strin
case 'b': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid binary number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -825,7 +825,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber16(const std::size_t& pos, std::strin
case 'o': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid octal number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -880,7 +880,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string
case 'x': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 16); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid hex number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid hex number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -900,7 +900,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string
case 'b': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 2); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid binary number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid binary number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -919,7 +919,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string
case 'o': {
if (auto res = strtol(jump_label.substr(pos + 2).c_str(), nullptr, 7); !res) {
if (errno != 0) {
- CompilerKit::Detail::print_error("invalid octal number: " + jump_label, "CompilerKit");
+ CompilerKit::Detail::print_error("Invalid octal number: " + jump_label, "CompilerKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -1006,30 +1006,34 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string line, std::string file) {
std::vector<RegMapAMD64> currentRegList;
for (auto& reg : kRegisterList) {
- std::vector<char> regExt = {'e', 'r'};
-
- for (auto& ext : regExt) {
- std::string registerName;
-
- if (bits > 16) registerName.push_back(ext);
+ std::string registerName;
+
+ if (bits == 32) registerName.push_back('e');
+ else if (bits == 64) registerName.push_back('r');
+ else {
+ CompilerKit::Detail::print_error(
+ "Invalid size for register, current bit width is: " +
+ std::to_string(kRegisterBitWidth),
+ file);
+ throw std::runtime_error("invalid_reg_size");
+ }
- registerName += reg.fName;
+ registerName += reg.fName;
- while (line.find(registerName) != std::string::npos) {
- line.erase(line.find(registerName), registerName.size());
+ while (line.find(registerName) != std::string::npos) {
+ line.erase(line.find(registerName), registerName.size());
- if (bits == 16) {
- if (registerName[0] == 'r') {
- CompilerKit::Detail::print_error(
- "invalid size for register, current bit width is: " +
- std::to_string(kRegisterBitWidth),
- file);
- throw std::runtime_error("invalid_reg_size");
- }
+ if (bits == 16) {
+ if (registerName[0] == 'r') {
+ CompilerKit::Detail::print_error(
+ "Invalid size for register, current bit width is: " +
+ std::to_string(kRegisterBitWidth),
+ file);
+ throw std::runtime_error("invalid_reg_size");
}
-
- currentRegList.push_back({.fName = registerName, .fModRM = reg.fModRM});
}
+
+ currentRegList.push_back({.fName = registerName, .fModRM = reg.fModRM});
}
}
diff --git a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc b/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc
index 0e21e58..1169ea9 100644
--- a/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc
+++ b/src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc
@@ -26,6 +26,7 @@
#include <cstdlib>
#include <filesystem>
#include <ios>
+#include "CompilerKit/Detail/Config.h"
/* NeKernel NECTAR Compiler Driver. */
/* This is part of the CompilerKit. */
@@ -375,7 +376,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile(
dont_accept_func:
break;
- accept_func: {
+ accept_func : {
CompilerKit::STLString symbol_name_fn = text;
size_t indexFnName = 0;
@@ -480,17 +481,20 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile(
break;
}
case CompilerKit::KeywordKind::kKeywordKindDelete: {
- if (auto pos = syntax_tree.fUserValue.find(keyword.first.fKeywordName); pos != CompilerKit::STLString::npos)
- syntax_tree.fUserValue.replace(pos, keyword.first.fKeywordName.size(), "__operator_release");
+ if (auto pos = syntax_tree.fUserValue.find(keyword.first.fKeywordName);
+ pos != CompilerKit::STLString::npos)
+ syntax_tree.fUserValue.replace(pos, keyword.first.fKeywordName.size(),
+ "__operator_delete");
continue;
}
case CompilerKit::KeywordKind::kKeywordKindNew: {
- if (auto pos = syntax_tree.fUserValue.find(keyword.first.fKeywordName); pos != CompilerKit::STLString::npos)
- syntax_tree.fUserValue.replace(pos, keyword.first.fKeywordName.size(), "__operator_retain");
+ if (auto pos = syntax_tree.fUserValue.find(keyword.first.fKeywordName);
+ pos != CompilerKit::STLString::npos)
+ syntax_tree.fUserValue.replace(pos, keyword.first.fKeywordName.size(),
+ "__operator_new");
continue;
}
- case CompilerKit::KeywordKind::kKeywordKindAccess:
- case CompilerKit::KeywordKind::kKeywordKindPtrAccess: {
+ case CompilerKit::KeywordKind::kKeywordKindAccess: {
CompilerKit::STLString valueOfVar =
text.substr(text.find(keyword.first.fKeywordName) + keyword.first.fKeywordName.size());
@@ -532,7 +536,7 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile(
}
if (!isnumber(val[0])) {
- val = "0";
+ val = "0x0";
}
}
@@ -552,7 +556,9 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile(
if (!nectar_get_variable_ref(nameVar).empty()) {
syntax_tree.fUserValue +=
- "call " + nectar_get_variable_ref(nameVar) + (keyword.first.fKeywordName.ends_with('>') ? " ptr offset " : " offset ") + method + "\n";
+ "call " + nectar_get_variable_ref(nameVar) +
+ (keyword.first.fKeywordName.ends_with('>') ? " ptr offset " : " offset ") + method +
+ "\n";
}
break;
@@ -570,7 +576,8 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile(
valueOfVar = text.substr(text.find("-=") + 2);
} else if (keyword.first.fKeywordKind ==
CompilerKit::KeywordKind::kKeywordKindVariableAssign) {
- valueOfVar = text.substr(text.find(keyword.first.fKeywordName) + keyword.first.fKeywordName.size());
+ valueOfVar = text.substr(text.find(keyword.first.fKeywordName) +
+ keyword.first.fKeywordName.size());
} else if (keyword.first.fKeywordKind == CompilerKit::KeywordKind::kKeywordKindEndLine) {
break;
}
@@ -673,15 +680,17 @@ CompilerKit::SyntaxLeafList::SyntaxLeaf CompilerFrontendNectarAMD64::Compile(
nectar_allocate_stack_variable(varName);
if (valueOfVar.find(".") != CompilerKit::STLString::npos) {
- auto value = "offset ";
+ CompilerKit::STLString value = "offset ";
valueOfVar.replace(0, valueOfVar.find(".") + 1, value);
- } else if (valueOfVar.find("->") != CompilerKit::STLString::npos) {
- auto value = "ptr offset ";
- valueOfVar.replace(0, valueOfVar.find("->") + 2, value);
}
- if (valueOfVar.find("(") != CompilerKit::STLString::npos)
- valueOfVar.erase(valueOfVar.find("("));
+ if (valueOfVar.find(")") != CompilerKit::STLString::npos) {
+ if (valueOfVar.find("(") != CompilerKit::STLString::npos)
+ valueOfVar.erase(valueOfVar.find("("));
+
+ syntax_tree.fUserValue += "call " + valueOfVar + "\nmov rcx, rax\n";
+ valueOfVar = "rcx";
+ }
syntax_tree.fUserValue +=
instr + nectar_get_variable_ref(varName) + ", " + valueOfVar + "\n";
@@ -1171,7 +1180,8 @@ static void nectar_process_function_parameters(const std::vector<CompilerKit::ST
/////////////////////////////////////////////////////////////////////////////////////////
-#define kExtListCxx {".ncpp"}
+#define kExtListCxx \
+ { ".ncpp" }
class AssemblyNectarInterfaceAMD64 final CK_ASSEMBLY_INTERFACE {
public:
@@ -1229,11 +1239,8 @@ NECTAR_MODULE(CompilerNectarAMD64) {
kKeywords.emplace_back("-=", CompilerKit::KeywordKind::kKeywordKindVariableDec);
kKeywords.emplace_back("const", CompilerKit::KeywordKind::kKeywordKindConstant);
kKeywords.emplace_back("let", CompilerKit::KeywordKind::kKeywordKindVariable);
- kKeywords.emplace_back("retain", CompilerKit::KeywordKind::kKeywordKindNew);
- kKeywords.emplace_back("release", CompilerKit::KeywordKind::kKeywordKindDelete);
kKeywords.emplace_back("new", CompilerKit::KeywordKind::kKeywordKindNew);
kKeywords.emplace_back("delete", CompilerKit::KeywordKind::kKeywordKindDelete);
- kKeywords.emplace_back("->", CompilerKit::KeywordKind::kKeywordKindPtrAccess);
kKeywords.emplace_back(".", CompilerKit::KeywordKind::kKeywordKindAccess);
kKeywords.emplace_back(",", CompilerKit::KeywordKind::kKeywordKindArgSeparator);
kKeywords.emplace_back(";", CompilerKit::KeywordKind::kKeywordKindEndLine);
diff --git a/test/test_samples/test_struct.ncpp b/test/test_samples/test_struct.ncpp
index 24fb79c..f1893d0 100644
--- a/test/test_samples/test_struct.ncpp
+++ b/test/test_samples/test_struct.ncpp
@@ -7,7 +7,7 @@ impl foo
let noop()
{
- return 0;
+ return 0x0;
}
};
@@ -21,11 +21,11 @@ let construct_foo()
let main()
{
- let io := new;
+ let io := 0x0;
io := construct_foo();
- let first_number := io->noop();
- io := delete;
+ let first_number := io.noop();
+ let status := delete(io);
return first_number;
} \ No newline at end of file