summaryrefslogtreecommitdiffhomepage
path: root/dev/LibCompiler/src/CCompilerARM64.cc
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2025-01-08 10:28:10 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2025-01-08 10:28:10 +0100
commitf5a153c3f888f82edaf5038e5762f9bd70356db4 (patch)
tree02d3a8f71796105a7e6780eb3327b2c2724e70d5 /dev/LibCompiler/src/CCompilerARM64.cc
parentc3b10ee1e28737375d65c3811f390d77a84fc165 (diff)
KAN-8: Compiler tweaks and AARCH64 in progress.
- Refactor C compilers. - Add Encoder for ARM64. - Add and working on assembler for AARCH64. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/LibCompiler/src/CCompilerARM64.cc')
-rw-r--r--dev/LibCompiler/src/CCompilerARM64.cc134
1 files changed, 66 insertions, 68 deletions
diff --git a/dev/LibCompiler/src/CCompilerARM64.cc b/dev/LibCompiler/src/CCompilerARM64.cc
index d985b2e..476596d 100644
--- a/dev/LibCompiler/src/CCompilerARM64.cc
+++ b/dev/LibCompiler/src/CCompilerARM64.cc
@@ -147,10 +147,10 @@ public:
explicit CompilerFrontendARM64() = default;
~CompilerFrontendARM64() override = default;
- TOOLCHAINKIT_COPY_DEFAULT(CompilerFrontendARM64);
+ LIBCOMPILER_COPY_DEFAULT(CompilerFrontendARM64);
std::string Check(const char* text, const char* file);
- bool Compile(const std::string text, const std::string file) override;
+ bool Compile(std::string text, const std::string file) override;
const char* Language() override
{
@@ -200,10 +200,8 @@ namespace Details
/////////////////////////////////////////////////////////////////////////////////////////
-bool CompilerFrontendARM64::Compile(const std::string text, const std::string file)
+bool CompilerFrontendARM64::Compile(std::string text, const std::string file)
{
- std::string textBuffer = text;
-
bool typeFound = false;
bool fnFound = false;
@@ -216,7 +214,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
std::mt19937 generator(seq);
// start parsing
- for (size_t text_index = 0; text_index < textBuffer.size(); ++text_index)
+ for (size_t text_index = 0; text_index < text.size(); ++text_index)
{
auto syntaxLeaf = LibCompiler::SyntaxLeafList::SyntaxLeaf();
@@ -227,7 +225,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
if (!typeFound)
{
- auto substr = textBuffer.substr(text_index);
+ auto substr = text.substr(text_index);
std::string match_type;
for (size_t y = 0; y < substr.size(); ++y)
@@ -255,7 +253,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
break;
}
- if (textBuffer.find('(') != std::string::npos)
+ if (text.find('(') != std::string::npos)
{
syntaxLeaf.fUserValue = buf;
@@ -274,7 +272,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
}
}
- if (textBuffer[text_index] == '{')
+ if (text[text_index] == '{')
{
if (kInStruct)
{
@@ -288,7 +286,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
}
// return keyword handler
- if (textBuffer[text_index] == 'r')
+ if (text[text_index] == 'r')
{
std::string return_keyword;
return_keyword += "return";
@@ -297,18 +295,18 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
std::string value;
- for (size_t return_index = text_index; return_index < textBuffer.size();
+ for (size_t return_index = text_index; return_index < text.size();
++return_index)
{
- if (textBuffer[return_index] != return_keyword[index])
+ if (text[return_index] != return_keyword[index])
{
for (size_t value_index = return_index;
- value_index < textBuffer.size(); ++value_index)
+ value_index < text.size(); ++value_index)
{
- if (textBuffer[value_index] == ';')
+ if (text[value_index] == ';')
break;
- value += textBuffer[value_index];
+ value += text[value_index];
}
break;
@@ -354,10 +352,10 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
}
}
- if (textBuffer[text_index] == 'i' && textBuffer[text_index + 1] == 'f')
+ if (text[text_index] == 'i' && text[text_index + 1] == 'f')
{
- auto expr = textBuffer.substr(text_index + 2);
- textBuffer.erase(text_index, 2);
+ auto expr = text.substr(text_index + 2);
+ text.erase(text_index, 2);
if (expr.find("{") != std::string::npos)
{
@@ -370,7 +368,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
if (expr.find(")") != std::string::npos)
expr.erase(expr.find(")"));
- kIfFunction = "__TOOLCHAINKIT_IF_PROC_";
+ kIfFunction = "__LIBCOMPILER_IF_PROC_";
kIfFunction += std::to_string(time_off._Raw);
syntaxLeaf.fUserValue = "\tlda r12, extern_segment ";
@@ -387,32 +385,32 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
// Parse expressions and instructions here.
// what does this mean?
// we encounter an assignment, or we reached the end of an expression.
- if (textBuffer[text_index] == '=' || textBuffer[text_index] == ';')
+ if (text[text_index] == '=' || text[text_index] == ';')
{
if (fnFound)
continue;
if (kIfFound)
continue;
- if (textBuffer[text_index] == ';' && kInStruct)
+ if (text[text_index] == ';' && kInStruct)
continue;
- if (textBuffer.find("typedef ") != std::string::npos)
+ if (text.find("typedef ") != std::string::npos)
continue;
- if (textBuffer[text_index] == '=' && kInStruct)
+ if (text[text_index] == '=' && kInStruct)
{
- Details::print_error("assignement of value in struct " + textBuffer,
+ Details::print_error("assignement of value in struct " + text,
file);
continue;
}
- if (textBuffer[text_index] == ';' && kInStruct)
+ if (text[text_index] == ';' && kInStruct)
{
bool space_found_ = false;
std::string sym;
- for (auto& ch : textBuffer)
+ for (auto& ch : text)
{
if (ch == ' ')
{
@@ -437,26 +435,26 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
continue;
}
- if (textBuffer[text_index] == '=' && kInStruct)
+ if (text[text_index] == '=' && kInStruct)
{
continue;
}
- if (textBuffer[text_index + 1] == '=' ||
- textBuffer[text_index - 1] == '!' ||
- textBuffer[text_index - 1] == '<' ||
- textBuffer[text_index - 1] == '>')
+ if (text[text_index + 1] == '=' ||
+ text[text_index - 1] == '!' ||
+ text[text_index - 1] == '<' ||
+ text[text_index - 1] == '>')
{
continue;
}
std::string substr;
- if (textBuffer.find('=') != std::string::npos && kInBraces && !kIfFound)
+ if (text.find('=') != std::string::npos && kInBraces && !kIfFound)
{
- if (textBuffer.find("*") != std::string::npos)
+ if (text.find("*") != std::string::npos)
{
- if (textBuffer.find("=") > textBuffer.find("*"))
+ if (text.find("=") > text.find("*"))
substr += "\tlda ";
else
substr += "\tldw ";
@@ -466,7 +464,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
substr += "\tldw ";
}
}
- else if (textBuffer.find('=') != std::string::npos && !kInBraces)
+ else if (text.find('=') != std::string::npos && !kInBraces)
{
substr += "stw public_segment .data64 ";
}
@@ -475,10 +473,10 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
std::string str_name;
- for (size_t text_index_2 = 0; text_index_2 < textBuffer.size();
+ for (size_t text_index_2 = 0; text_index_2 < text.size();
++text_index_2)
{
- if (textBuffer[text_index_2] == '\"')
+ if (text[text_index_2] == '\"')
{
++text_index_2;
@@ -486,29 +484,29 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
// string.
substr += '"';
- for (; text_index_2 < textBuffer.size(); ++text_index_2)
+ for (; text_index_2 < text.size(); ++text_index_2)
{
- if (textBuffer[text_index_2] == '\"')
+ if (text[text_index_2] == '\"')
break;
- substr += textBuffer[text_index_2];
+ substr += text[text_index_2];
}
}
- if (textBuffer[text_index_2] == '{' || textBuffer[text_index_2] == '}')
+ if (text[text_index_2] == '{' || text[text_index_2] == '}')
continue;
- if (textBuffer[text_index_2] == ';')
+ if (text[text_index_2] == ';')
{
break;
}
- if (textBuffer[text_index_2] == ' ' ||
- textBuffer[text_index_2] == '\t')
+ if (text[text_index_2] == ' ' ||
+ text[text_index_2] == '\t')
{
if (first_encountered != 2)
{
- if (textBuffer[text_index] != '=' &&
+ if (text[text_index] != '=' &&
substr.find("public_segment .data64") == std::string::npos &&
!kInStruct)
substr += "public_segment .data64 ";
@@ -519,7 +517,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
continue;
}
- if (textBuffer[text_index_2] == '=')
+ if (text[text_index_2] == '=')
{
if (!kInBraces)
{
@@ -531,7 +529,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
continue;
}
- substr += textBuffer[text_index_2];
+ substr += text[text_index_2];
}
for (auto& clType : kCompilerTypes)
@@ -586,13 +584,13 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
syntaxLeaf.fUserValue += substr;
kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf);
- if (textBuffer[text_index] == '=')
+ if (text[text_index] == '=')
break;
}
// function handler.
- if (textBuffer[text_index] == '(' && !fnFound && !kIfFound)
+ if (text[text_index] == '(' && !fnFound && !kIfFound)
{
std::string substr;
std::string args_buffer;
@@ -600,20 +598,20 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
bool type_crossed = false;
- for (size_t idx = textBuffer.find('(') + 1; idx < textBuffer.size();
+ for (size_t idx = text.find('(') + 1; idx < text.size();
++idx)
{
- if (textBuffer[idx] == ',')
+ if (text[idx] == ',')
continue;
- if (textBuffer[idx] == ' ')
+ if (text[idx] == ' ')
continue;
- if (textBuffer[idx] == ')')
+ if (text[idx] == ')')
break;
}
- for (char substr_first_index : textBuffer)
+ for (char substr_first_index : text)
{
if (substr_first_index != ',')
args_buffer += substr_first_index;
@@ -649,7 +647,7 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
}
}
- for (char _text_i : textBuffer)
+ for (char _text_i : text)
{
if (_text_i == '\t' || _text_i == ' ')
{
@@ -692,27 +690,27 @@ bool CompilerFrontendARM64::Compile(const std::string text, const std::string fi
fnFound = true;
}
- kCompilerFunctions.push_back(textBuffer);
+ kCompilerFunctions.push_back(text);
}
- if (textBuffer[text_index] == '-' && textBuffer[text_index + 1] == '-')
+ if (text[text_index] == '-' && text[text_index + 1] == '-')
{
- textBuffer = textBuffer.replace(textBuffer.find("--"), strlen("--"), "");
+ text = text.replace(text.find("--"), strlen("--"), "");
- for (int _text_i = 0; _text_i < textBuffer.size(); ++_text_i)
+ for (int _text_i = 0; _text_i < text.size(); ++_text_i)
{
- if (textBuffer[_text_i] == '\t' || textBuffer[_text_i] == ' ')
- textBuffer.erase(_text_i, 1);
+ if (text[_text_i] == '\t' || text[_text_i] == ' ')
+ text.erase(_text_i, 1);
}
syntaxLeaf.fUserValue += "sub ";
- syntaxLeaf.fUserValue += textBuffer;
+ syntaxLeaf.fUserValue += text;
kState.fSyntaxTree->fLeafList.push_back(syntaxLeaf);
break;
}
- if (textBuffer[text_index] == '}')
+ if (text[text_index] == '}')
{
kRegisterCounter = kStartUsable;
@@ -1300,11 +1298,11 @@ public:
explicit AssemblyCCInterface() = default;
~AssemblyCCInterface() override = default;
- TOOLCHAINKIT_COPY_DEFAULT(AssemblyCCInterface);
+ LIBCOMPILER_COPY_DEFAULT(AssemblyCCInterface);
[[maybe_unused]] static Int32 Arch() noexcept
{
- return LibCompiler::AssemblyFactory::kArchARM64;
+ return LibCompiler::AssemblyFactory::kArchAARCH64;
}
Int32 CompileToFormat(std::string& src, Int32 arch) override
@@ -1478,7 +1476,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////
-#include <Version.h>
+#include <LibCompiler/Version.h>
#define kPrintF printf
#define kSplashCxx() \
@@ -1493,7 +1491,7 @@ static void cc_print_help()
#define kCExtension ".c"
-TOOLCHAINKIT_MODULE(ZkaOSCompilerCLangARM64)
+LIBCOMPILER_MODULE(ZkaOSCompilerCLangARM64)
{
kCompilerTypes.push_back({.fName = "void", .fValue = "void"});
kCompilerTypes.push_back({.fName = "char", .fValue = "byte"});
@@ -1505,7 +1503,7 @@ TOOLCHAINKIT_MODULE(ZkaOSCompilerCLangARM64)
bool skip = false;
kFactory.Mount(new AssemblyCCInterface());
- kMachine = LibCompiler::AssemblyFactory::kArchARM64;
+ kMachine = LibCompiler::AssemblyFactory::kArchAARCH64;
kCompilerFrontend = new CompilerFrontendARM64();
for (auto index = 1UL; index < argc; ++index)