summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-08-25 10:52:15 +0200
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-08-25 10:52:15 +0200
commite751e12b02cc1f1b9a5eb9d091b0a6f4e8dde944 (patch)
tree4410c715cbbe90f69309ed8e6dc06a533cceb0d4
parent6b5b3a22e85d99d28265227e1fe56628a6332610 (diff)
[IMP] A set of fixes and improvements regarding the assembler and C++
compiler. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
-rw-r--r--dev/ndkdll/Sources/AssemblerAMD64.cxx24
-rw-r--r--dev/ndkdll/Sources/CPlusPlusCompilerAMD64.cxx55
-rw-r--r--sdk/zka.h2
-rw-r--r--tests/example.cxx17
-rw-r--r--tools/cl.cxx35
5 files changed, 92 insertions, 41 deletions
diff --git a/dev/ndkdll/Sources/AssemblerAMD64.cxx b/dev/ndkdll/Sources/AssemblerAMD64.cxx
index cc9f0f2..598e6d0 100644
--- a/dev/ndkdll/Sources/AssemblerAMD64.cxx
+++ b/dev/ndkdll/Sources/AssemblerAMD64.cxx
@@ -540,9 +540,9 @@ namespace detail::algorithm
static inline bool is_not_alnum_space(char c)
{
return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') ||
- (c == ',') || (c == '(') || (c == ')') || (c == '"') ||
+ (c == ',') || (c == '(') || (c == ')') || (c == '"') || (c == '*') ||
(c == '\'') || (c == '[') || (c == ']') || (c == '+') ||
- (c == '_') || (c == ':') || (c == '@') || (c == '.') || (c == '#'));
+ (c == '_') || (c == ':') || (c == '@') || (c == '.') || (c == '#') || (c == ';'));
}
bool is_valid_amd64(const std::string& str)
@@ -1284,16 +1284,18 @@ bool NDK::EncoderAMD64::WriteLine(std::string& line,
"Invalid combination of operands and registers.", "i64asm");
throw std::runtime_error("comb_op_reg");
}
-
- kAppBytes.emplace_back(0x66);
- kAppBytes.emplace_back(0x89);
+ else
+ {
+ kAppBytes.emplace_back(0x66);
+ kAppBytes.emplace_back(0x89);
+ }
}
if (currentRegList[1].fName[0] == 'r' &&
currentRegList[0].fName[0] == 'e')
{
detail::print_error_asm("Invalid combination of operands and registers.",
- "i64asm");
+ "i64asm");
throw std::runtime_error("comb_op_reg");
}
@@ -1301,7 +1303,7 @@ bool NDK::EncoderAMD64::WriteLine(std::string& line,
currentRegList[1].fName[0] == 'e')
{
detail::print_error_asm("Invalid combination of operands and registers.",
- "i64asm");
+ "i64asm");
throw std::runtime_error("comb_op_reg");
}
@@ -1311,7 +1313,7 @@ bool NDK::EncoderAMD64::WriteLine(std::string& line,
currentRegList[0].fName[0] == 'e')
{
detail::print_error_asm("Invalid combination of operands and registers.",
- "i64asm");
+ "i64asm");
throw std::runtime_error("comb_op_reg");
}
@@ -1319,7 +1321,7 @@ bool NDK::EncoderAMD64::WriteLine(std::string& line,
currentRegList[1].fName[0] == 'e')
{
detail::print_error_asm("Invalid combination of operands and registers.",
- "i64asm");
+ "i64asm");
throw std::runtime_error("comb_op_reg");
}
}
@@ -1329,7 +1331,7 @@ bool NDK::EncoderAMD64::WriteLine(std::string& line,
currentRegList[0].fName[0] == 'e')
{
detail::print_error_asm("Invalid combination of operands and registers.",
- "i64asm");
+ "i64asm");
throw std::runtime_error("comb_op_reg");
}
@@ -1337,7 +1339,7 @@ bool NDK::EncoderAMD64::WriteLine(std::string& line,
currentRegList[1].fName[0] == 'e')
{
detail::print_error_asm("Invalid combination of operands and registers.",
- "i64asm");
+ "i64asm");
throw std::runtime_error("comb_op_reg");
}
}
diff --git a/dev/ndkdll/Sources/CPlusPlusCompilerAMD64.cxx b/dev/ndkdll/Sources/CPlusPlusCompilerAMD64.cxx
index 7485ad8..325e2da 100644
--- a/dev/ndkdll/Sources/CPlusPlusCompilerAMD64.cxx
+++ b/dev/ndkdll/Sources/CPlusPlusCompilerAMD64.cxx
@@ -159,9 +159,7 @@ public:
/// @internal compiler variables
-static CompilerBackendCPlusPlus* kCompilerBackend = nullptr;
-static std::vector<detail::CompilerType> kCompilerVariables;
-static std::vector<std::string> kCompilerFunctions;
+static CompilerBackendCPlusPlus* kCompilerBackend = nullptr;
static std::vector<std::string> kRegisterMap;
@@ -337,8 +335,6 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text,
auto& valueOfVarOpposite = isdigit(left[0]) ? left : right;
syntax_tree.fUserValue += "mov " + cRegisters[indexRight + 1] + ", " + valueOfVarOpposite + "\n";
-
- syntax_tree.fUserValue += "mov " + cRegisters[kRegisterMap.size() - 1] + ", " + valueOfVar + "\n";
syntax_tree.fUserValue += "cmp " + cRegisters[kRegisterMap.size() - 1] + "," + cRegisters[indexRight + 1] + "\n";
goto done_iterarting_on_if;
@@ -347,7 +343,6 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text,
auto& valueOfVarOpposite = isdigit(left[0]) ? left : right;
syntax_tree.fUserValue += "mov " + cRegisters[indexRight + 1] + ", " + valueOfVarOpposite + "\n";
- syntax_tree.fUserValue += "mov " + cRegisters[kRegisterMap.size() - 1] + ", " + valueOfVar + "\n";
syntax_tree.fUserValue += "cmp " + cRegisters[kRegisterMap.size() - 1] + ", " + cRegisters[indexRight + 1] + "\n";
break;
@@ -365,7 +360,7 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text,
ch = '_';
}
- syntax_tree.fUserValue += "jge offset [rsp + 4]\n";
+ syntax_tree.fUserValue += "jge __OFFSET_ON_TRUE_NDK\n__OFFSET_ON_TRUE_NDK:\n";
}
break;
@@ -540,17 +535,35 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text,
if (pairRight != valueOfVar)
{
- syntax_tree.fUserValue = instr + cRegisters[kRegisterMap.size() - 1] + ", " + valueOfVar + "\n";
+ if (valueOfVar[0] == '\"')
+ {
+
+ syntax_tree.fUserValue = "__NDK_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n\n";
+ syntax_tree.fUserValue += instr + cRegisters[kRegisterMap.size() - 1] + ", " + "__NDK_LOCAL_VAR_" + varName + "\n";
+ }
+ else
+ {
+ syntax_tree.fUserValue = instr + cRegisters[kRegisterMap.size() - 1] + ", " + valueOfVar + "\n";
+ }
+
goto done;
}
-
- syntax_tree.fUserValue = instr + cRegisters[kRegisterMap.size() - 1] + ", " + valueOfVar + "\n";
- break;
}
if (((int)indexRight - 1) < 0)
{
- syntax_tree.fUserValue = instr + cRegisters[kRegisterMap.size()] + ", " + valueOfVar + "\n";
+ if (valueOfVar[0] == '\"')
+ {
+
+ syntax_tree.fUserValue = "__NDK_LOCAL_VAR_" + varName + ": db " + valueOfVar + ", 0\n";
+ syntax_tree.fUserValue += instr + cRegisters[kRegisterMap.size()] + ", " + "__NDK_LOCAL_VAR_" + varName + "\n";
+ }
+ else
+ {
+ syntax_tree.fUserValue = instr + cRegisters[kRegisterMap.size()] + ", " + valueOfVar + "\n";
+ }
+
+ goto done;
}
if (valueOfVar[0] != '\"' &&
@@ -563,11 +576,21 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text,
goto done;
}
- detail::print_error_asm("Variable not declared: " + valueOfVar, file);
+ detail::print_error_asm("Variable not declared: " + varName, file);
return false;
}
done:
+ for (auto& keyword : kKeywords)
+ {
+ if (keyword.keyword_kind == NDK::eKeywordKindType &&
+ varName.find(keyword.keyword_name) != std::string::npos)
+ {
+ varName.erase(varName.find(keyword.keyword_name), keyword.keyword_name.size());
+ break;
+ }
+ }
+
kRegisterMap.push_back(varName);
}
else
@@ -692,7 +715,7 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text,
if (pair != subText)
continue;
- syntax_tree.fUserValue = "mov rax, " + cRegisters[indxReg] + "\r\nret\n";
+ syntax_tree.fUserValue = "mov rax, " + cRegisters[indxReg - 1] + "\r\nret\n";
break;
}
@@ -708,8 +731,8 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text,
}
else
{
- syntax_tree.fUserValue = "mov rcx, " + subText + "\n";
- syntax_tree.fUserValue = "mov rax, rcx\r\nret\n";
+ syntax_tree.fUserValue = "__NDK_LOCAL_RETURN_STRING: db " + subText + ", 0\nmov rcx, __NDK_LOCAL_RETURN_STRING\n";
+ syntax_tree.fUserValue += "mov rax, rcx\r\nret\n";
}
break;
diff --git a/sdk/zka.h b/sdk/zka.h
index d9724de..2b0641d 100644
--- a/sdk/zka.h
+++ b/sdk/zka.h
@@ -10,6 +10,6 @@
#ifndef __SDK_ZKA_H__
#define __SDK_ZKA_H__
-#warning this header is incomplete.
+#warning Giving a main symbol for the app.
#endif // ifdef __SDK_ZKA_H__
diff --git a/tests/example.cxx b/tests/example.cxx
index dd91b68..ac0991d 100644
--- a/tests/example.cxx
+++ b/tests/example.cxx
@@ -1 +1,16 @@
-#include "sdk/zka.h"
+#include "../sdk/zka.h"
+const int main()
+{
+ // help, i'm not a int!
+ int test_me = 0;
+
+ if (test_me >= 0)
+ {
+ return 0;
+ }
+ else
+ {
+ return 1;
+ }
+
+}
diff --git a/tools/cl.cxx b/tools/cl.cxx
index d4b982e..43d058a 100644
--- a/tools/cl.cxx
+++ b/tools/cl.cxx
@@ -17,10 +17,22 @@ extern "C" int ZKAAssemblerMainAMD64(int argc, char const* argv[]);
int main(int argc, char const* argv[])
{
+ for (size_t index_arg = 0; index_arg < argc; ++index_arg)
+ {
+ if (strstr(argv[index_arg], "/?"))
+ {
+ std::printf("cl.exe: Frontend C++ Compiler.\n");
+ std::printf("cl.exe: Designed by Amlal El Mahrouss, Copyright ZKA Technologies.\n");
+ std::printf("libndk.dylib/ndk.dll: Designed by Amlal El Mahrouss, Copyright ZKA Technologies.\n");
+
+ return 0;
+ }
+ }
+
if (auto code = CPlusPlusPreprocessorMain(argc, argv); code)
{
std::printf("cl.exe: frontend exited with code %i.\n", code);
- return 1;
+ return 1;
}
else
{
@@ -35,22 +47,21 @@ int main(int argc, char const* argv[])
strstr(argv[index_arg], ".c++") ||
strstr(argv[index_arg], ".C"))
{
- std::string arg = argv[index_arg];
-
- arg += ".masm";
- args_list_asm.push_back(arg);
+ std::string arg = argv[index_arg];
+ arg += ".pp.masm";
+ args_list_asm.push_back(arg);
- arg = argv[index_arg];
- arg += ".pp";
+ arg = argv[index_arg];
+ arg += ".pp";
- args_list_cxx.push_back(arg);
+ args_list_cxx.push_back(arg);
}
}
- for (auto &cli : args_list_cxx)
+ for (auto& cli : args_list_cxx)
{
- const char* arr_cli[] = { argv[0], cli.data() };
+ const char* arr_cli[] = {argv[0], cli.data()};
if (auto code = CompilerCPlusPlusX8664(2, arr_cli); code)
{
@@ -58,9 +69,9 @@ int main(int argc, char const* argv[])
}
}
- for (auto &cli : args_list_cxx)
+ for (auto& cli : args_list_asm)
{
- const char* arr_cli[] = { argv[0], cli.data() };
+ const char* arr_cli[] = {argv[0], cli.data()};
if (auto code = ZKAAssemblerMainAMD64(2, arr_cli); code)
{