summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2026-01-19 10:37:08 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2026-01-19 10:37:08 +0100
commite0c75cc3dc9a55632c21ea2dc7bab1dfc44e2657 (patch)
tree5c5ec9377e68aa4baee00864af34a4d56c546f82
parent300eb04e2567a284f23edff7a8eef96bcd267254 (diff)
feat: asm&frontend: codegen and assembler improvements.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--doc/specs/GENERAL_SPECIFICATION.md6
-rw-r--r--example/example_02_nectar/example.ncpp2
-rw-r--r--src/CompilerKit/src/Assemblers/Assembler+AMD64.cc12
-rw-r--r--src/CompilerKit/src/Compilers/NectarCompiler+AMD64.cc26
4 files changed, 25 insertions, 21 deletions
diff --git a/doc/specs/GENERAL_SPECIFICATION.md b/doc/specs/GENERAL_SPECIFICATION.md
index 045f2ff..2a0cccf 100644
--- a/doc/specs/GENERAL_SPECIFICATION.md
+++ b/doc/specs/GENERAL_SPECIFICATION.md
@@ -25,14 +25,14 @@
===================================
-# 2: LibGL
+# 2: GenericsLibrary
===================================
- Shall support Nectar runtime and ABI of NeKernel.
- Shall support a basic subset of the Nectar library.
-- Shall be written in Nectar
-
+- Shall be written in Nectar.
+- Shall provide Nectar with the required implementation to write programs.
===================================
diff --git a/example/example_02_nectar/example.ncpp b/example/example_02_nectar/example.ncpp
index d7ec86c..b0668a8 100644
--- a/example/example_02_nectar/example.ncpp
+++ b/example/example_02_nectar/example.ncpp
@@ -3,5 +3,5 @@ const main() {
return 0x137;
}
- return 0;
+ return 0x0;
}
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<CompilerKit::STLString> 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;