summaryrefslogtreecommitdiffhomepage
path: root/dev/LibCompiler/Util
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-05-23 03:48:06 +0200
committerAmlal El Mahrouss <amlal@nekernel.org>2025-05-23 03:48:06 +0200
commit2eed4954c762bb8050e40798c3d9f1d3998324d1 (patch)
tree8848d4345fca4d62c23d1e7136eeff2978c9e6c5 /dev/LibCompiler/Util
parent8ad58a91a11380203c4a81fe4dc93e7734631b32 (diff)
feat!(LibCompiler): Codebase and diagram has been improved.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/LibCompiler/Util')
-rw-r--r--dev/LibCompiler/Util/LCAsmUtils.h93
-rw-r--r--dev/LibCompiler/Util/LCClUtils.h68
2 files changed, 161 insertions, 0 deletions
diff --git a/dev/LibCompiler/Util/LCAsmUtils.h b/dev/LibCompiler/Util/LCAsmUtils.h
new file mode 100644
index 0000000..7728444
--- /dev/null
+++ b/dev/LibCompiler/Util/LCAsmUtils.h
@@ -0,0 +1,93 @@
+/* -------------------------------------------
+
+ Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved
+
+------------------------------------------- */
+
+#pragma once
+
+#include <LibCompiler/CodeGen.h>
+#include <LibCompiler/Frontend.h>
+
+#include <LibCompiler/Util/LCClUtils.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/Util/LCClUtils.h b/dev/LibCompiler/Util/LCClUtils.h
new file mode 100644
index 0000000..c88339d
--- /dev/null
+++ b/dev/LibCompiler/Util/LCClUtils.h
@@ -0,0 +1,68 @@
+/* -------------------------------------------
+
+ Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved
+
+------------------------------------------- */
+
+#pragma once
+
+#include <LibCompiler/CodeGen.h>
+#include <LibCompiler/ErrorID.h>
+#include <LibCompiler/Frontend.h>
+#include <Vendor/Dialogs.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 << kRed << "drv: " << kWhite)
+#define kStdErr (std::cout << kYellow << "drv: " << kWhite)
+
+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 << reason << kBlank << std::endl;
+
+ if (kAcceptableErrors > kErrorLimit) std::exit(LIBCOMPILER_EXEC_ERROR);
+
+ ++kAcceptableErrors;
+}
+
+inline void print_warning(std::string reason, std::string file) noexcept {
+ if (reason[0] == '\n') reason.erase(0, 1);
+
+ kStdOut << kYellow << reason << kBlank << std::endl;
+}
+
+/// @internal
+/// @brief Handler for SIGSEGV signal.
+inline void drv_segfault_handler(std::int32_t id) {
+ switch (id) {
+ case SIGSEGV: {
+ kStdErr << "SIGSEGV: Please report this on the GitHub issues page." << kBlank << std::endl;
+ break;
+ }
+ case SIGABRT: {
+ kStdErr << "SIGABRT: Please report this on the GitHub issues page." << kBlank << std::endl;
+ break;
+ }
+ }
+
+ std::exit(LIBCOMPILER_EXEC_ERROR);
+}
+} // namespace Detail