diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-26 22:58:28 -0500 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-26 22:58:28 -0500 |
| commit | e63063f6556aaa759b8af2cc5bf88b89e2dbcfab (patch) | |
| tree | 551705f187dc1129ef0835ac7d1a56bbb6856340 /src/CompilerKit/utils/AsmUtils.h | |
| parent | ad4deb740e8a358542d59dae2b3c1225c02b50db (diff) | |
| parent | eb7f40b000b0b1dc4605113f3f2d7e87003d6d43 (diff) | |
Merge branch 'dev' of github.com:nekernel-org/necti into dev
Diffstat (limited to 'src/CompilerKit/utils/AsmUtils.h')
| -rw-r--r-- | src/CompilerKit/utils/AsmUtils.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/CompilerKit/utils/AsmUtils.h b/src/CompilerKit/utils/AsmUtils.h new file mode 100644 index 0000000..897fcbe --- /dev/null +++ b/src/CompilerKit/utils/AsmUtils.h @@ -0,0 +1,92 @@ +/* ======================================== + + Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license + +======================================== */ + +#pragma once + +#include <CompilerKit/Compiler.h> +#include <CompilerKit/Frontend.h> +#include <CompilerKit/utils/CompilerUtils.h> + +using namespace CompilerKit; + +/// @brief Get Number from lineBuffer. +/// @param lineBuffer the lineBuffer to fetch from. +/// @param numberKey where to seek that number. +/// @return +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) { + 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) { + 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) { + 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) { + 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; + } + } +} |
