summaryrefslogtreecommitdiffhomepage
path: root/dev
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 /dev
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>
Diffstat (limited to 'dev')
-rw-r--r--dev/ndkdll/Sources/AssemblerAMD64.cxx24
-rw-r--r--dev/ndkdll/Sources/CPlusPlusCompilerAMD64.cxx55
2 files changed, 52 insertions, 27 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;