summaryrefslogtreecommitdiffhomepage
path: root/dev
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-11-10 11:16:53 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-11-10 11:16:53 +0100
commita0a06a8defaf1669c122594c13c5bb9278508a88 (patch)
tree965664a5e506698d1e53375601d7c9f25e22a6e0 /dev
parent54167881459a4c188da488d900174eb3e903e17d (diff)
IMP: Auto detect git repository when writing assembly file.
WIP: Functions support needed too. WIP: C++ Rule checker using XML files.
Diffstat (limited to 'dev')
-rw-r--r--dev/ToolchainKit/src/AssemblerAMD64.cc25
-rw-r--r--dev/ToolchainKit/src/CCompilerPower64.cc2
-rw-r--r--dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc127
-rw-r--r--dev/ToolchainKit/src/CPlusPlusRuleChecker.cc8
4 files changed, 106 insertions, 56 deletions
diff --git a/dev/ToolchainKit/src/AssemblerAMD64.cc b/dev/ToolchainKit/src/AssemblerAMD64.cc
index 80d37a0..9867ccd 100644
--- a/dev/ToolchainKit/src/AssemblerAMD64.cc
+++ b/dev/ToolchainKit/src/AssemblerAMD64.cc
@@ -79,7 +79,6 @@ static std::vector<std::string> kDefinedSymbols;
static std::vector<std::string> kUndefinedSymbols;
static const std::string kUndefinedSymbol = ":UndefinedSymbol:";
-static const std::string kRelocSymbol = ":RuntimeSymbol:";
// \brief forward decl.
static bool asm_read_attributes(std::string& line);
@@ -190,6 +189,8 @@ TOOLCHAINKIT_MODULE(AssemblerAMD64)
std::ifstream file_ptr(argv[i]);
std::ofstream file_ptr_out(object_output, std::ofstream::binary);
+ kStdOut << "AssemblerAMD64: assembling: " << argv[i] << "\n";
+
if (file_ptr_out.bad())
{
if (kVerbose)
@@ -222,12 +223,11 @@ TOOLCHAINKIT_MODULE(AssemblerAMD64)
if (kVerbose)
{
kStdOut << "Compiling: " + asm_input << "\n";
+ kStdOut << "From: " + line << "\n";
}
while (std::getline(file_ptr, line))
{
- kStdOut << "Compiling: " + line << "\n";
-
if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty())
{
detail::print_error_asm(ln, argv[i]);
@@ -546,17 +546,20 @@ static bool asm_read_attributes(std::string& line)
namespace detail::algorithm
{
// \brief authorize a brief set of characters.
- static inline bool is_not_alnum_space(char c)
+ static inline bool is_not_valid(char c)
{
- return !(isalpha(c) || isdigit(c) || (c == ' ') || (c == '\t') ||
+ if ((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 == ';')))
+ return false;
+
+ return true;
}
bool is_valid_amd64(const std::string& str)
{
- return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end();
+ return std::find_if(str.begin(), str.end(), is_not_valid) == str.end();
}
} // namespace detail::algorithm
@@ -593,14 +596,6 @@ std::string ToolchainKit::EncoderAMD64::CheckLine(std::string& line,
return err_str;
}
- if (!detail::algorithm::is_valid_amd64(line))
- {
- err_str = "Line contains non alphanumeric characters.\nHere -> ";
- err_str += line;
-
- return err_str;
- }
-
// check for a valid instruction format.
if (line.find(',') != std::string::npos)
diff --git a/dev/ToolchainKit/src/CCompilerPower64.cc b/dev/ToolchainKit/src/CCompilerPower64.cc
index c91bd38..72d0b40 100644
--- a/dev/ToolchainKit/src/CCompilerPower64.cc
+++ b/dev/ToolchainKit/src/CCompilerPower64.cc
@@ -1,7 +1,7 @@
/*
* ========================================================
*
- * cc
+ * CCompilerPower64
* Copyright (C) 2024, EL Mahrouss Logic, all rights reserved.
*
* ========================================================
diff --git a/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc b/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc
index 85f2114..6dacdc3 100644
--- a/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc
+++ b/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc
@@ -9,10 +9,11 @@
/// BUGS: 1
+#include <cstdio>
#define kPrintF printf
-#define kExitOK (EXIT_SUCCESS)
-#define kExitFail (EXIT_FAILURE)
+#define kExitOK (EXIT_SUCCESS)
+#define kExitNO (EXIT_FAILURE)
#define kSplashCxx() \
kPrintF(kWhite "%s\n", "ELMH C++ Compiler Driver, (c) 2024 EL Mahrouss Logic, all rights reserved.")
@@ -52,6 +53,27 @@
/// @internal
namespace detail
{
+ std::filesystem::path expand_home(const std::filesystem::path& p)
+ {
+ if (!p.empty() && p.string()[0] == '~')
+ {
+ const char* home = std::getenv("HOME"); // For Unix-like systems
+ if (!home)
+ {
+ home = std::getenv("USERPROFILE"); // For Windows
+ }
+ if (home)
+ {
+ return std::filesystem::path(home) / p.relative_path().string().substr(1);
+ }
+ else
+ {
+ throw std::runtime_error("Home directory not found in environment variables");
+ }
+ }
+ return p;
+ }
+
struct CompilerRegisterMap final
{
std::string fName;
@@ -75,7 +97,7 @@ namespace detail
{
std::vector<CompilerRegisterMap> fStackMapVector;
std::vector<CompilerStructMap> fStructMapVector;
- ToolchainKit::SyntaxLeafList* fSyntaxTree{nullptr};
+ ToolchainKit::SyntaxLeafList* fSyntaxTree{nullptr};
std::unique_ptr<std::ofstream> fOutputAssembly;
std::string fLastFile;
std::string fLastError;
@@ -113,10 +135,10 @@ static int kMachine = ToolchainKit::AssemblyFactory::kArchAMD64;
/////////////////////////////////////////
-static size_t kRegisterCnt = kAsmRegisterLimit;
-static size_t kStartUsable = 8;
-static size_t kUsableLimit = 15;
-static size_t kRegisterCounter = kStartUsable;
+static size_t kRegisterCnt = kAsmRegisterLimit;
+static size_t kStartUsable = 8;
+static size_t kUsableLimit = 15;
+static size_t kRegisterCounter = kStartUsable;
static std::vector<ToolchainKit::CompilerKeyword> kKeywords;
/////////////////////////////////////////
@@ -125,13 +147,13 @@ static std::vector<ToolchainKit::CompilerKeyword> kKeywords;
/////////////////////////////////////////
-static std::vector<std::string> kFileList;
-static ToolchainKit::AssemblyFactory kFactory;
-static bool kInStruct = false;
-static bool kOnWhileLoop = false;
-static bool kOnForLoop = false;
-static bool kInBraces = false;
-static size_t kBracesCount = 0UL;
+static std::vector<std::string> kFileList;
+static ToolchainKit::AssemblyFactory kFactory;
+static bool kInStruct = false;
+static bool kOnWhileLoop = false;
+static bool kOnForLoop = false;
+static bool kInBraces = false;
+static size_t kBracesCount = 0UL;
/* @brief C++ compiler backend for the ZKA C++ driver */
class CompilerFrontendCPlusPlus final : public ToolchainKit::ICompilerFrontend
@@ -203,7 +225,7 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
if (text.empty())
return false;
- std::size_t index = 0UL;
+ std::size_t index = 0UL;
std::vector<std::pair<ToolchainKit::CompilerKeyword, std::size_t>> keywords_list;
bool found = false;
@@ -680,44 +702,51 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
break;
}
case ToolchainKit::KeywordKind::eKeywordKindReturn: {
- auto pos = text.find("return") + strlen("return") + 1;
- std::string subText = text.substr(pos);
- subText = subText.erase(subText.find(";"));
- size_t indxReg = 0UL;
-
- if (subText[0] != '\"' &&
- subText[0] != '\'')
+ try
{
- if (!isdigit(subText[0]))
+ auto pos = text.find("return") + strlen("return") + 1;
+ std::string subText = text.substr(pos);
+ subText = subText.erase(subText.find(";"));
+ size_t indxReg = 0UL;
+
+ if (subText[0] != '\"' &&
+ subText[0] != '\'')
{
- for (auto pair : kRegisterMap)
+ if (!isdigit(subText[0]))
{
- ++indxReg;
+ for (auto pair : kRegisterMap)
+ {
+ ++indxReg;
- if (pair != subText)
- continue;
+ if (pair != subText)
+ continue;
- syntax_tree.fUserValue = "mov rax, " + kRegisterList[indxReg - 1] + "\r\nret\n";
- break;
- }
+ syntax_tree.fUserValue = "mov rax," + kRegisterList[indxReg - 1] + "\r\nret\n";
+ break;
+ }
- if (syntax_tree.fUserValue.empty())
+ if (syntax_tree.fUserValue.empty())
+ {
+ detail::print_error_asm("Variable not declared: " + subText, file);
+ }
+ }
+ else
{
- detail::print_error_asm("Variable not declared: " + subText, file);
+ syntax_tree.fUserValue = "mov rax, " + subText + "\r\nret\n";
}
}
else
{
- syntax_tree.fUserValue = "mov rax, " + subText + "\r\nret\n";
+ syntax_tree.fUserValue = "__TOOLCHAINKIT_LOCAL_RETURN_STRING: db " + subText + ", 0\nmov rcx, __TOOLCHAINKIT_LOCAL_RETURN_STRING\n";
+ syntax_tree.fUserValue += "mov rax, rcx\r\nret\n";
}
+
+ break;
}
- else
+ catch (...)
{
- syntax_tree.fUserValue = "__TOOLCHAINKIT_LOCAL_RETURN_STRING: db " + subText + ", 0\nmov rcx, __TOOLCHAINKIT_LOCAL_RETURN_STRING\n";
- syntax_tree.fUserValue += "mov rax, rcx\r\nret\n";
+ syntax_tree.fUserValue = "ret\n";
}
-
- break;
}
default:
break;
@@ -791,9 +820,27 @@ public:
auto fmt = ToolchainKit::current_date();
- (*kState.fOutputAssembly) << "; Path: " << src_file << "\n";
+ (*kState.fOutputAssembly) << "; Repository Path: /" << src_file << "\n";
+
+ std::filesystem::path path = std::filesystem::path("./");
+
+ while (path != detail::expand_home(std::filesystem::path("~")))
+ {
+ for (auto const& dir_entry : std::filesystem::recursive_directory_iterator{path})
+ {
+ if (dir_entry.is_directory() &&
+ dir_entry.path().string().find(".git") != std::string::npos)
+ goto break_loop;
+ }
+
+ path = path.parent_path();
+ break_loop:
+ (*kState.fOutputAssembly) << "; Repository Style: Git\n";
+ break;
+ }
+
(*kState.fOutputAssembly)
- << "; Language: AMD64 assembly. (Generated from C++)\n";
+ << "; Assembler Dialect: AMD64 ToolchainKit Assembler. (Generated from C++)\n";
(*kState.fOutputAssembly) << "; Date: " << fmt << "\n";
(*kState.fOutputAssembly) << "#bits 64\n#org 0x1000000"
<< "\n";
diff --git a/dev/ToolchainKit/src/CPlusPlusRuleChecker.cc b/dev/ToolchainKit/src/CPlusPlusRuleChecker.cc
new file mode 100644
index 0000000..0751ec4
--- /dev/null
+++ b/dev/ToolchainKit/src/CPlusPlusRuleChecker.cc
@@ -0,0 +1,8 @@
+/*
+ * ========================================================
+ *
+ * CPlusPlusRuleChecker
+ * Copyright (C) 2024, EL Mahrouss Logic, all rights reserved.
+ *
+ * ========================================================
+ */