From 6189b15370dc04276692684a1cbbd455c26a3285 Mon Sep 17 00:00:00 2001 From: starcells Date: Sun, 19 Apr 2026 10:49:51 +0800 Subject: [PATCH] TODO:Macro expand --- .clang-format | 2 +- CMakeLists.txt | 5 +- include/r8asm/r8asm.hh | 46 +-------------- include/r8asm/r8asm_asm.hh | 9 +++ include/r8asm/r8asm_core.hh | 61 ++++++++++++++++++++ include/r8asm/r8asm_macro.hh | 8 +++ src/r8asm.cc | 69 ---------------------- src/r8asm_asm.cc | 107 +++++++++++++++++++++++++++++++++++ src/r8asm_core.cc | 7 +++ src/r8asm_macro.cc | 7 +++ tests/r8_test.cc | 16 ++++-- 11 files changed, 215 insertions(+), 122 deletions(-) create mode 100644 include/r8asm/r8asm_asm.hh create mode 100644 include/r8asm/r8asm_core.hh create mode 100644 include/r8asm/r8asm_macro.hh delete mode 100644 src/r8asm.cc create mode 100644 src/r8asm_asm.cc create mode 100644 src/r8asm_core.cc create mode 100644 src/r8asm_macro.cc diff --git a/.clang-format b/.clang-format index 67d549f..8479648 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,4 @@ -BasedOnStyle: Google +BasedOnStyle: Webkit IndentWidth: 4 UseTab: Always PointerAlignment: Right \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a924c1..6673f11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,9 +5,8 @@ set(CMAKE_CXX_STANDARD 20) include_directories(include) -file(GLOB_RECURSE SOURCES "src/*.cc") - -add_library(r8asm_core STATIC ${SOURCES}) +add_library(r8asm_core src/r8asm_core.cc src/r8asm_asm.cc + src/r8asm_macro.cc) add_executable(r8asm_test tests/r8_test.cc) target_link_libraries(r8asm_test r8asm_core) diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 5fb05e4..9400301 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -1,49 +1,7 @@ #ifndef R8ASM #define R8ASM -#include -#include -#include -#include -#include -#include - -typedef uint32_t r8asm_label; -typedef uint32_t r8asm_data; -typedef uint32_t unit_size_type; - -#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast(bc)]) - -enum class rot8_bytecode { STP, BTP, ROR, FLB, BIZ, RNZ, OUT, INP }; -enum class r8asm_macro { - LOOP, - ENDLOOP, - TP, - XOR, -}; - -using R8Operand = std::variant; -using R8MetaOp = std::variant; - -struct R8Instruction { - R8MetaOp op; - R8Operand arg = std::monostate{}; -}; - -class R8Macro { - std::vector args = {std::monostate{}}; - std::vector ins; -}; - -std::vector r8asm_tape_out(std::span ops); - -extern std::map labels; -extern std::map datas; -extern std::map> macros; - -extern unit_size_type UNIT_SIZE; -// #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8) -#define DEFAULT_UNIT_SIZE 17 // Just for test. -#define UNIT_MAX (((uint64_t)1 << UNIT_SIZE) - 1) +#include "r8asm/r8asm_asm.hh" // IWYU pragma: export +#include "r8asm_macro.hh" // IWYU pragma: export #endif \ No newline at end of file diff --git a/include/r8asm/r8asm_asm.hh b/include/r8asm/r8asm_asm.hh new file mode 100644 index 0000000..3a6973b --- /dev/null +++ b/include/r8asm/r8asm_asm.hh @@ -0,0 +1,9 @@ +#ifndef R8ASM_ASM +#define R8ASM_ASM + +#include "r8asm/r8asm_core.hh" +#include + +std::vector r8asm_tape_out(std::span ops); + +#endif \ No newline at end of file diff --git a/include/r8asm/r8asm_core.hh b/include/r8asm/r8asm_core.hh new file mode 100644 index 0000000..7f83083 --- /dev/null +++ b/include/r8asm/r8asm_core.hh @@ -0,0 +1,61 @@ +#ifndef R8ASM_CORE +#define R8ASM_CORE + +#include +#include +#include +#include +#include + +typedef uint32_t r8asm_label; +typedef uint32_t r8asm_data; +typedef uint32_t unit_size_type; +typedef int32_t unit_offset_type; +typedef uint32_t mem_size_type; +typedef uint8_t argc_type; + +#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast(bc)]) + +enum class rot8_bytecode { STP, + BTP, + ROR, + FLB, + BIZ, + RNZ, + OUT, + INP, +}; +enum class r8asm_macro { + LOOP, + ENDLOOP, + TP, + XOR, + ANCHOR, + MACRO, + ENDMACRO, +}; + +using R8Operand = std::variant; +using R8MetaOp = std::variant; + +struct R8Instruction { + R8MetaOp op; + R8Operand arg = std::monostate {}; +}; + +class R8Macro { + argc_type argc; + std::vector ins; +}; + +extern std::map labels; +extern std::map datas; +extern std::map macros; +extern mem_size_type dataptr; + +extern unit_size_type UNIT_SIZE; +// #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8) +#define DEFAULT_UNIT_SIZE 17 // Just for test. +#define UNIT_MAX (((uint64_t)1 << UNIT_SIZE) - 1) + +#endif \ No newline at end of file diff --git a/include/r8asm/r8asm_macro.hh b/include/r8asm/r8asm_macro.hh new file mode 100644 index 0000000..6a5edb0 --- /dev/null +++ b/include/r8asm/r8asm_macro.hh @@ -0,0 +1,8 @@ +#ifndef R8ASM_MACRO +#define R8ASM_MACRO + +#include "r8asm/r8asm_core.hh" + +std::vector expand_macro(std::string macro_name, std::vector argv); + +#endif \ No newline at end of file diff --git a/src/r8asm.cc b/src/r8asm.cc deleted file mode 100644 index 7a3e208..0000000 --- a/src/r8asm.cc +++ /dev/null @@ -1,69 +0,0 @@ -#include "r8asm/r8asm.hh" - -#include -#include - -unit_size_type UNIT_SIZE = DEFAULT_UNIT_SIZE; - -std::map labels = {{std::string("main"), 0}}; -std::map datas = { - {std::string("_UNIT_MAX"), UNIT_MAX}}; - -r8asm_data read_opr(R8Operand arg, r8asm_data default_opr = 1) { - if (const std::string *operand = std::get_if(&arg)) - return (datas[*operand]); - else if (const r8asm_data *operand = std::get_if(&arg)) - return (*operand); - return (default_opr); -} - -void expand_xor(std::vector &tape, R8Operand arg) { - r8asm_data opr = read_opr(arg); - - for (unit_size_type i = 0; i < UNIT_SIZE; i++) { - if ((opr & 1) == 1) tape.push_back(rot8_bytecode::FLB); - tape.push_back(rot8_bytecode::ROR); - opr >>= 1; - } -} - -void expand_loop(std::vector &tape, R8Operand arg) { - r8asm_data opr = read_opr(arg, UNIT_MAX); - - tape.push_back(rot8_bytecode::STP); - expand_xor(tape, opr); - tape.push_back(rot8_bytecode::BIZ); - tape.push_back(rot8_bytecode::BTP); // >(xor)[< -} - -void expand_endloop(std::vector &tape, R8Operand arg) { - std::vector tmp = {rot8_bytecode::STP, rot8_bytecode::FLB, - rot8_bytecode::ROR, rot8_bytecode::RNZ, - rot8_bytecode::BTP}; // >-+]< - tape.insert(tape.end(), tmp.begin(), tmp.end()); -} - -std::vector r8asm_tape_out(std::span ops) { - std::vector tape; - for (const auto it : ops) { - if (const rot8_bytecode *bc = std::get_if(&it.op)) { - r8asm_data repeat = read_opr(it.arg); - for (r8asm_data i = 0; i < repeat; i++) tape.push_back(*bc); - } else if (const r8asm_macro *macro = std::get_if(&it.op)) - switch (*macro) { - case r8asm_macro::LOOP: - expand_loop(tape, it.arg); - break; - case r8asm_macro::ENDLOOP:; - expand_endloop(tape, it.arg); - break; - case r8asm_macro::TP:; - break; - case r8asm_macro::XOR:; - expand_xor(tape, it.arg); - break; - default:; - } - } - return (tape); -} diff --git a/src/r8asm_asm.cc b/src/r8asm_asm.cc new file mode 100644 index 0000000..9f8d7d8 --- /dev/null +++ b/src/r8asm_asm.cc @@ -0,0 +1,107 @@ +#include "r8asm/r8asm_asm.hh" + +#include +#include +#include + +static r8asm_data +read_opr(R8Operand arg, r8asm_data default_opr = 0) +{ + if (const std::string *operand = std::get_if(&arg)) { + if ((*operand) == "$") + return dataptr; + return (datas[*operand]); + } else if (const r8asm_data *operand = std::get_if(&arg)) + return (*operand); + return (default_opr); +} + +static void expand_xor(std::vector &tape, R8Operand arg) +{ + r8asm_data opr = read_opr(arg); + + for (unit_size_type i = 0; i < UNIT_SIZE; i++) { + if ((opr & 1) == 1) + tape.push_back(rot8_bytecode::FLB); + tape.push_back(rot8_bytecode::ROR); + opr >>= 1; + } +} + +static void expand_loop(std::vector &tape, R8Operand arg) +{ + r8asm_data opr = read_opr(arg, UNIT_MAX); + + tape.push_back(rot8_bytecode::STP); + expand_xor(tape, opr); + tape.push_back(rot8_bytecode::BIZ); + tape.push_back(rot8_bytecode::BTP); // >(xor)[< +} + +static void expand_endloop(std::vector &tape, R8Operand arg) +{ + std::vector tmp = { rot8_bytecode::STP, + rot8_bytecode::FLB, + rot8_bytecode::ROR, + rot8_bytecode::RNZ, + rot8_bytecode::BTP }; // >-+]< + tape.insert(tape.end(), tmp.begin(), tmp.end()); +} + +static void expand_tp(std::vector &tape, R8Operand arg) +{ + r8asm_data opr = read_opr(arg); + + unit_offset_type offset = (unit_offset_type)opr - (unit_offset_type)dataptr; + bool stepforward = (offset > 0); + offset = abs(offset); + for (unit_offset_type i = 0; i < offset; i++) { + tape.push_back(stepforward ? rot8_bytecode::STP : rot8_bytecode::BTP); + } + dataptr = opr; +} + +static void expand_anchor(std::vector &tape, R8Operand arg) +{ + if (const std::string *name = std::get_if(&arg)) + datas.insert({ *name, dataptr }); + else if (const r8asm_data *name = std::get_if(&arg)) + datas.insert({ std::to_string(*name), dataptr }); +} + +std::vector +r8asm_tape_out(std::span ops) +{ + std::vector tape; + for (const auto it : ops) { + if (const rot8_bytecode *bc = std::get_if(&it.op)) { + r8asm_data repeat = read_opr(it.arg); + for (r8asm_data i = 0; i < repeat; i++) { + tape.push_back(*bc); + if (*bc == rot8_bytecode::STP) + dataptr++; + else if (*bc == rot8_bytecode::BTP) + dataptr--; + } + } else if (const r8asm_macro *macro = std::get_if(&it.op)) + switch (*macro) { + case r8asm_macro::LOOP: + expand_loop(tape, it.arg); + break; + case r8asm_macro::ENDLOOP: + expand_endloop(tape, it.arg); + break; + case r8asm_macro::TP:; + expand_tp(tape, it.arg); + break; + case r8asm_macro::XOR: + expand_xor(tape, it.arg); + break; + case r8asm_macro::ANCHOR: + expand_anchor(tape, it.arg); + break; + default:; + } + } + return (tape); +} diff --git a/src/r8asm_core.cc b/src/r8asm_core.cc new file mode 100644 index 0000000..e2c6dcc --- /dev/null +++ b/src/r8asm_core.cc @@ -0,0 +1,7 @@ +#include "r8asm/r8asm_core.hh" +unit_size_type UNIT_SIZE = DEFAULT_UNIT_SIZE; +mem_size_type dataptr = 0; + +std::map labels = { { std::string("main"), 0 } }; +std::map datas = { { std::string("_UNIT_MAX"), + UNIT_MAX } }; diff --git a/src/r8asm_macro.cc b/src/r8asm_macro.cc new file mode 100644 index 0000000..a2101d3 --- /dev/null +++ b/src/r8asm_macro.cc @@ -0,0 +1,7 @@ +#include "r8asm/r8asm_macro.hh" + +std::vector expand_macro(std::string macro_name, std::vector argv) +{ + std::vector tape; + return (tape); +} \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 3eedca3..e44b9a0 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,12 +1,18 @@ #include #include "r8asm/r8asm.hh" -int main() { + +int main() +{ std::vector ins = { - {rot8_bytecode::INP}, {r8asm_macro::LOOP}, {rot8_bytecode::BIZ}, - {rot8_bytecode::FLB}, {rot8_bytecode::RNZ}, {rot8_bytecode::ROR}, - {r8asm_macro::ENDLOOP}, {rot8_bytecode::OUT}}; + { rot8_bytecode::INP }, { r8asm_macro::LOOP }, + { rot8_bytecode::BIZ }, { rot8_bytecode::FLB }, + { rot8_bytecode::RNZ }, { rot8_bytecode::ROR }, + { r8asm_macro::ENDLOOP }, { rot8_bytecode::OUT } + }; std::vector tape = r8asm_tape_out(ins); - for (const auto i : tape) std::cout << ROT8_BC_TO_CHAR(i); + + for (const auto i : tape) + std::cout << ROT8_BC_TO_CHAR(i); return (0); }