summaryrefslogtreecommitdiffhomepage
path: root/dev/LibCompiler/Util
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-05-29 10:48:12 +0200
committerGitHub <noreply@github.com>2025-05-29 10:48:12 +0200
commit433bb5ef102b2bfa0049468be00d63011da8b973 (patch)
treee0893a30710477045a5bb085cb7a27aada425c14 /dev/LibCompiler/Util
parent1ddeab9a4426abd781a5066ba79af2ba64de11d9 (diff)
parent756ee7f8dc954e27350fe5bdfbaa83b9f69780c8 (diff)
Merge pull request #6 from nekernel-org/dev
0.0.2e3
Diffstat (limited to 'dev/LibCompiler/Util')
-rw-r--r--dev/LibCompiler/Util/AsmUtils.h93
-rw-r--r--dev/LibCompiler/Util/CompilerUtils.h109
-rw-r--r--dev/LibCompiler/Util/DylibHelpers.h13
3 files changed, 215 insertions, 0 deletions
diff --git a/dev/LibCompiler/Util/AsmUtils.h b/dev/LibCompiler/Util/AsmUtils.h
new file mode 100644
index 0000000..f176eda
--- /dev/null
+++ b/dev/LibCompiler/Util/AsmUtils.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/CompilerUtils.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/CompilerUtils.h b/dev/LibCompiler/Util/CompilerUtils.h
new file mode 100644
index 0000000..19e247d
--- /dev/null
+++ b/dev/LibCompiler/Util/CompilerUtils.h
@@ -0,0 +1,109 @@
+/* -------------------------------------------
+
+ 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 <LibCompiler/Version.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 drvi_crash_handler(std::int32_t id) {
+ LibCompiler::STLString verbose_header = "LIBCOMPILER CRASH REPORT - ";
+ verbose_header += kDistVersion;
+ verbose_header += " - ";
+ verbose_header += LibCompiler::current_date();
+
+ for (auto& ch : verbose_header) {
+ std::cout << '=';
+ }
+
+ std::cout << std::endl;
+
+ std::cout << verbose_header << std::endl;
+
+ for (auto& ch : verbose_header) {
+ std::cout << '=';
+ }
+
+ std::cout << std::endl;
+
+ kStdOut << "DATE: " << LibCompiler::current_date() << std::endl;
+ kStdOut << "VERSION: " << kDistVersion << std::endl;
+ kStdOut << "ERRNO: " << errno << std::endl;
+ kStdOut << "ERRNO(STRING): " << strerror(errno) << std::endl;
+
+ switch (id) {
+ case SIGSEGV: {
+ kStdOut << "SIGNAL: Segmentation Fault." << kBlank << std::endl;
+ break;
+ }
+ case SIGABRT: {
+ kStdOut << "SIGNAL: Aborted." << kBlank << std::endl;
+ break;
+ }
+ }
+
+ std::cout << kWhite;
+
+ for (auto& ch : verbose_header) {
+ std::cout << '=';
+ }
+
+ std::cout << std::endl;
+
+ std::cout << verbose_header << std::endl;
+
+ for (auto& ch : verbose_header) {
+ std::cout << '=';
+ }
+
+ std::cout << std::endl;
+
+ std::exit(LIBCOMPILER_EXEC_ERROR);
+}
+} // namespace Detail
diff --git a/dev/LibCompiler/Util/DylibHelpers.h b/dev/LibCompiler/Util/DylibHelpers.h
new file mode 100644
index 0000000..be8119f
--- /dev/null
+++ b/dev/LibCompiler/Util/DylibHelpers.h
@@ -0,0 +1,13 @@
+/* -------------------------------------------
+
+ Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved
+
+------------------------------------------- */
+
+#pragma once
+
+#include <LibCompiler/Defines.h>
+#include <dlfcn.h>
+
+typedef Int32 (*LibCompilerEntrypoint)(Int32 argc, Char const* argv[]);
+typedef VoidPtr DylibHandle;