summaryrefslogtreecommitdiffhomepage
path: root/dev/LibCompiler/Detail
diff options
context:
space:
mode:
authorAmlal <amlal@nekernel.org>2025-04-25 19:21:22 +0200
committerAmlal <amlal@nekernel.org>2025-04-25 19:21:51 +0200
commitf6b400b80efc64b918c03352e93ec9de4e2369a1 (patch)
tree251a82ee3653568ef33ffdc48c83b7f54e370bc1 /dev/LibCompiler/Detail
parent20042235d1f53ae428aa154e64afdbae5d8d91ad (diff)
dev, general: codebase needed refactors and tweaks, for NeKernel's 0.0.2 release.
details: - things needed to be cleared off, short sighted decisions fixed. - the inconsistency of certain files have been fixed too. Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'dev/LibCompiler/Detail')
-rw-r--r--dev/LibCompiler/Detail/AsmUtils.h93
-rw-r--r--dev/LibCompiler/Detail/ClUtils.h57
2 files changed, 150 insertions, 0 deletions
diff --git a/dev/LibCompiler/Detail/AsmUtils.h b/dev/LibCompiler/Detail/AsmUtils.h
new file mode 100644
index 0000000..559df5f
--- /dev/null
+++ b/dev/LibCompiler/Detail/AsmUtils.h
@@ -0,0 +1,93 @@
+/* -------------------------------------------
+
+ Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved
+
+------------------------------------------- */
+
+#pragma once
+
+#include <LibCompiler/AssemblyInterface.h>
+#include <LibCompiler/Parser.h>
+
+#include <LibCompiler/Detail/ClUtils.h>
+
+using namespace LibCompiler;
+
+/// @brief Get Number from lineBuffer.
+/// @param lineBuffer the lineBuffer to fetch from.
+/// @param numberKey where to seek that number.
+/// @return
+static NumberCast32 GetNumber32(std::string lineBuffer, std::string numberKey) {
+ auto pos = lineBuffer.find(numberKey) + numberKey.size();
+
+ while (lineBuffer[pos] == ' ') {
+ ++pos;
+ }
+
+ switch (lineBuffer[pos + 1]) {
+ case 'x': {
+ if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 16); !res) {
+ if (errno != 0) {
+ Detail::print_error("invalid hex number: " + lineBuffer, "LibCompiler");
+ throw std::runtime_error("invalid_hex");
+ }
+ }
+
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 16));
+
+ if (kVerbose) {
+ kStdOut << "asm: found a base 16 number here: " << lineBuffer.substr(pos) << "\n";
+ }
+
+ return numOffset;
+ }
+ case 'b': {
+ if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 2); !res) {
+ if (errno != 0) {
+ Detail::print_error("invalid binary number:" + lineBuffer, "LibCompiler");
+ throw std::runtime_error("invalid_bin");
+ }
+ }
+
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 2));
+
+ if (kVerbose) {
+ kStdOut << "asm: found a base 2 number here:" << lineBuffer.substr(pos) << "\n";
+ }
+
+ return numOffset;
+ }
+ case 'o': {
+ if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 7); !res) {
+ if (errno != 0) {
+ Detail::print_error("invalid octal number: " + lineBuffer, "LibCompiler");
+ throw std::runtime_error("invalid_octal");
+ }
+ }
+
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 7));
+
+ if (kVerbose) {
+ kStdOut << "asm: found a base 8 number here:" << lineBuffer.substr(pos) << "\n";
+ }
+
+ return numOffset;
+ }
+ default: {
+ if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 10); !res) {
+ if (errno != 0) {
+ Detail::print_error("invalid hex number: " + lineBuffer, "LibCompiler");
+ throw std::runtime_error("invalid_hex");
+ }
+ }
+
+ NumberCast32 numOffset(strtol(lineBuffer.substr(pos).c_str(), nullptr, 10));
+
+ if (kVerbose) {
+ kStdOut << "asm: found a base 10 number here:" << lineBuffer.substr(pos) << "\n";
+ }
+
+ return numOffset;
+ }
+ }
+}
diff --git a/dev/LibCompiler/Detail/ClUtils.h b/dev/LibCompiler/Detail/ClUtils.h
new file mode 100644
index 0000000..b14d472
--- /dev/null
+++ b/dev/LibCompiler/Detail/ClUtils.h
@@ -0,0 +1,57 @@
+/* -------------------------------------------
+
+ Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved
+
+------------------------------------------- */
+
+#pragma once
+
+#include <LibCompiler/AssemblyInterface.h>
+#include <LibCompiler/Parser.h>
+
+#define kZero64Section ".zero64"
+#define kCode64Section ".code64"
+#define kData64Section ".data64"
+
+#define kZero128Section ".zero128"
+#define kCode128Section ".code128"
+#define kData128Section ".data128"
+
+#define kBlank "\e[0;30m"
+#define kRed "\e[0;31m"
+#define kWhite "\e[0;97m"
+#define kYellow "\e[0;33m"
+
+#define kStdOut (std::cout << kWhite)
+#define kStdErr (std::cout << kRed)
+
+inline static UInt32 kErrorLimit = 10;
+inline static UInt32 kAcceptableErrors = 0;
+inline static bool kVerbose = false;
+inline static bool kOutputAsBinary = false;
+
+namespace Detail {
+inline void print_error(std::string reason, std::string file) noexcept {
+ if (reason[0] == '\n') reason.erase(0, 1);
+
+ kStdErr << kRed << "[ asm ] " << kWhite
+ << ((file == "LibCompiler") ? "InternalErrorException: "
+ : ("FileException{ " + file + " }: "))
+ << kBlank << std::endl;
+ kStdErr << kRed << "[ asm ] " << kWhite << reason << kBlank << std::endl;
+
+ if (kAcceptableErrors > kErrorLimit) std::exit(3);
+
+ ++kAcceptableErrors;
+}
+
+inline void print_warning(std::string reason, std::string file) noexcept {
+ if (reason[0] == '\n') reason.erase(0, 1);
+
+ if (!file.empty()) {
+ kStdOut << kYellow << "[ asm ] " << kWhite << file << kBlank << std::endl;
+ }
+
+ kStdOut << kYellow << "[ asm ] " << kWhite << reason << kBlank << std::endl;
+}
+} // namespace Detail \ No newline at end of file