summaryrefslogtreecommitdiffhomepage
path: root/include/CompilerKit/Utilities/Assembler.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-12-05 11:49:28 -0500
committerAmlal El Mahrouss <amlal@nekernel.org>2025-12-05 11:50:39 -0500
commit037ac38824623c13070384e8fc0e70c4770dcdbd (patch)
tree19d7286c5d226b33f10743c76436dace0cf42112 /include/CompilerKit/Utilities/Assembler.h
parent5535f22998bf991eeb75a56c9e147f0fd4bd23b2 (diff)
chore! new project filesystem structure.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'include/CompilerKit/Utilities/Assembler.h')
-rw-r--r--include/CompilerKit/Utilities/Assembler.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/include/CompilerKit/Utilities/Assembler.h b/include/CompilerKit/Utilities/Assembler.h
new file mode 100644
index 0000000..fc965f0
--- /dev/null
+++ b/include/CompilerKit/Utilities/Assembler.h
@@ -0,0 +1,92 @@
+/* ========================================
+
+ Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license
+
+======================================== */
+
+#pragma once
+
+#include <CompilerKit/AST.h>
+#include <CompilerKit/CodeGenerator.h>
+#include <CompilerKit/Utilities/Compiler.h>
+
+using namespace CompilerKit;
+
+/// @brief Get Number from lineBuffer.
+/// @param lineBuffer the lineBuffer to fetch from.
+/// @param numberKey where to seek that number.
+/// @return A numbercast of 32-bit width.
+static NumberCast32 GetNumber32(STLString lineBuffer, STLString 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) {
+ CompilerKit::Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit");
+ 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) {
+ CompilerKit::Detail::print_error("invalid binary number:" + lineBuffer, "CompilerKit");
+ 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) {
+ CompilerKit::Detail::print_error("invalid octal number: " + lineBuffer, "CompilerKit");
+ 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) {
+ CompilerKit::Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit");
+ 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;
+ }
+ }
+}