summaryrefslogtreecommitdiffhomepage
path: root/src/CompilerKit
diff options
context:
space:
mode:
Diffstat (limited to 'src/CompilerKit')
-rw-r--r--src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp14
-rw-r--r--src/CompilerKit/test/Compilers/.gitkeep0
-rw-r--r--src/CompilerKit/test/Compilers/CMakeLists.txt23
-rw-r--r--src/CompilerKit/test/Compilers/NectarCodegen.test.cpp20
4 files changed, 52 insertions, 5 deletions
diff --git a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp
index 0acbdb5..536ac27 100644
--- a/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp
+++ b/src/CompilerKit/src/Assemblers/Assembler+AMD64.cpp
@@ -900,7 +900,7 @@ bool CompilerKit::EncoderAMD64::WriteNumber8(const std::size_t& pos, std::string
/////////////////////////////////////////////////////////////////////////////////////////
-bool CompilerKit::EncoderAMD64::WriteLine(std::string line, std::string file) {
+bool CompilerKit::EncoderAMD64::WriteLine(CompilerKit::STLString line, CompilerKit::STLString file) {
if (CompilerKit::ast_find_needle(line, "public_segment ")) return true;
struct RegMapAMD64 {
@@ -926,7 +926,9 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string line, std::string file) {
/// Move instruction handler.
if (line.find(name) != std::string::npos) {
- if (name == "mov" || name == "xor") {
+ if ((line.find(name) + name.size()) > line.size()) continue;
+
+ if (name == "mov" || name == "xor") {
std::string substr = line.substr(line.find(name) + name.size());
uint64_t bits = kRegisterBitWidth;
@@ -1165,9 +1167,7 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string line, std::string file) {
bool hasRBasedRegs = false;
- if (!onlyOneReg) {
- /// very tricky to understand.
- /// but this checks for a r8 through r15 register.
+ if (!onlyOneReg && currentRegList.size() == 2) {
if (currentRegList[0].fName[0] == 'r' || currentRegList[1].fName[0] == 'r') {
if (isdigit(currentRegList[0].fName[1]) && isdigit(currentRegList[1].fName[1])) {
kAppBytes.emplace_back(0x4d);
@@ -1796,10 +1796,13 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string line, std::string file) {
if (line.find("bits 64") != std::string::npos) {
kRegisterBitWidth = 64U;
+ return true;
} else if (line.find("bits 32") != std::string::npos) {
kRegisterBitWidth = 32U;
+ return true;
} else if (line.find("bits 16") != std::string::npos) {
kRegisterBitWidth = 16U;
+ return true;
}
if (auto org_pos = line.find("org"); org_pos != std::string::npos) {
@@ -1827,6 +1830,7 @@ bool CompilerKit::EncoderAMD64::WriteLine(std::string line, std::string file) {
}
}
}
+
/// write a dword
else if (auto pos = line.find(".dword"); pos != std::string::npos) {
this->WriteNumber32(pos + strlen(".dword") + 1, line);
diff --git a/src/CompilerKit/test/Compilers/.gitkeep b/src/CompilerKit/test/Compilers/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/CompilerKit/test/Compilers/.gitkeep
diff --git a/src/CompilerKit/test/Compilers/CMakeLists.txt b/src/CompilerKit/test/Compilers/CMakeLists.txt
new file mode 100644
index 0000000..2d1a2f2
--- /dev/null
+++ b/src/CompilerKit/test/Compilers/CMakeLists.txt
@@ -0,0 +1,23 @@
+cmake_minimum_required(VERSION 3.10)
+project(NeCTICodeGen)
+
+include(FetchContent)
+FetchContent_Declare(
+ googletest
+ URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
+)
+
+# For Windows: Prevent overriding the parent project's compiler/linker settings
+set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
+FetchContent_MakeAvailable(googletest)
+
+enable_testing()
+
+add_executable(CodegenTestBasic NectarCodegen.test.cpp)
+target_link_libraries(CodegenTestBasic gtest_main)
+
+set_property(TARGET CodegenTestBasic PROPERTY CXX_STANDARD 20)
+target_include_directories(CodegenTestBasic PUBLIC ../../)
+
+include(GoogleTest)
+gtest_discover_tests(CodegenTestBasic)
diff --git a/src/CompilerKit/test/Compilers/NectarCodegen.test.cpp b/src/CompilerKit/test/Compilers/NectarCodegen.test.cpp
new file mode 100644
index 0000000..27a908e
--- /dev/null
+++ b/src/CompilerKit/test/Compilers/NectarCodegen.test.cpp
@@ -0,0 +1,20 @@
+// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org)
+// Licensed under the Apache License, Version 2.0 (See accompanying
+// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
+// Official repository: https://github.com/ne-foss-org/nectar
+
+/// @brief Codegen Unit test, from the C++ unit to the final executable.
+/// @author Amlal El Mahrouss
+
+#include <gtest/gtest.h>
+
+TEST(CodegenTest, BasicCodegenTestGrep) {
+ // Compile C++ source to assembly
+ auto compile_result = std::system("pef-amd64-necdrv ../../../../snippets/test_snippets/inner.nc > /dev/null 2>&1");
+ EXPECT_TRUE(compile_result == 0) << "C++ compiler driver failed to compile sample.cc";
+}
+
+TEST(CodegenTest, BasicCodegenTestAssemble) {
+ auto expr = std::system("pef-amd64-asm ../../../../snippets/test_snippets/inner.masm > /dev/null 2>&1");
+ EXPECT_TRUE(expr == 0) << "ASM Driver did not compile the easy ASM unit.";
+}