From 4d842917c2fc0855dd8d68347ee554af8363633c Mon Sep 17 00:00:00 2001 From: starcells Date: Thu, 23 Apr 2026 01:04:38 +0800 Subject: [PATCH] 202604230104 --- .clang-format | 2 +- include/r8asm/r8asm_core.hh | 4 ++- src/r8asm_asm.cc | 62 ++++++++++++++++++++----------------- src/r8asm_macro.cc | 34 +++++++++++++++----- 4 files changed, 64 insertions(+), 38 deletions(-) diff --git a/.clang-format b/.clang-format index 8479648..edbe08b 100644 --- a/.clang-format +++ b/.clang-format @@ -1,4 +1,4 @@ -BasedOnStyle: Webkit +BasedOnStyle: LLVM IndentWidth: 4 UseTab: Always PointerAlignment: Right \ No newline at end of file diff --git a/include/r8asm/r8asm_core.hh b/include/r8asm/r8asm_core.hh index bfa2d7a..a5ddf9c 100644 --- a/include/r8asm/r8asm_core.hh +++ b/include/r8asm/r8asm_core.hh @@ -32,10 +32,12 @@ enum class r8asm_builtin { TP, XOR, ANCHOR, + ERASE, MACRO, ENDMACRO, }; -using R8Operand = std::variant; + +using R8Operand = std::variant>; struct r8asm_macro { std::string name; diff --git a/src/r8asm_asm.cc b/src/r8asm_asm.cc index 2714d37..0959e7b 100644 --- a/src/r8asm_asm.cc +++ b/src/r8asm_asm.cc @@ -5,7 +5,8 @@ #include #include -r8asm_data read_opr(R8Operand arg, r8asm_data default_opr = 0) // Attention,default_opr is 0 +r8asm_data read_opr(R8Operand arg, + r8asm_data default_opr = 0) // Attention,default_opr is 0 { if (const std::string *operand = std::get_if(&arg)) { if ((*operand) == "$") @@ -16,8 +17,7 @@ r8asm_data read_opr(R8Operand arg, r8asm_data default_opr = 0) // Attention,defa return (default_opr); } -void expand_xor(std::vector &tape, R8Operand arg) -{ +void expand_xor(std::vector &tape, R8Operand arg) { r8asm_data opr = read_opr(arg); for (unit_size_type i = 0; i < UNIT_SIZE; i++) { @@ -28,26 +28,22 @@ void expand_xor(std::vector &tape, R8Operand arg) } } -void expand_loop(std::vector &tape, R8Operand arg) -{ - r8asm_data opr = read_opr(arg, UNIT_MAX); +void expand_loop(std::vector &tape, R8Operand arg) { + std::vector tmp = {rot8_bytecode::STP, rot8_bytecode::BIZ, + rot8_bytecode::BTP}; // >[< - 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()); } -void expand_tp(std::vector &tape, R8Operand arg, bool count_offset = true) -{ +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()); +} + +void expand_tp(std::vector &tape, R8Operand arg, + bool count_offset = true) { r8asm_data opr = read_opr(arg); unit_offset_type offset = (unit_offset_type)opr - (unit_offset_type)dataptr; @@ -60,16 +56,24 @@ void expand_tp(std::vector &tape, R8Operand arg, bool count_offse dataptr = opr; } -void expand_anchor(std::vector &tape, R8Operand arg) -{ +void expand_anchor(std::vector &tape, R8Operand arg) { if (const std::string *name = std::get_if(&arg)) - datas.insert({ *name, dataptr }); + datas.insert({*name, dataptr}); else if (const r8asm_data *name = std::get_if(&arg)) - datas.insert({ std::to_string(*name), dataptr }); + datas.insert({std::to_string(*name), dataptr}); } -void expand_bytecode(std::vector &tape, R8Operand arg, rot8_bytecode op, bool count_offset = true) -{ +void expand_erase(std::vector &tape, R8Operand arg) { + std::vector tmp = {rot8_bytecode::BIZ, rot8_bytecode::FLB, + rot8_bytecode::ROR, + rot8_bytecode::RNZ}; // [-+] + for (unit_size_type i = 0; i < UNIT_SIZE; i++) + tape.insert(tape.end(), tmp.begin(), tmp.end()); + return; +} + +void expand_bytecode(std::vector &tape, R8Operand arg, + rot8_bytecode op, bool count_offset = true) { r8asm_data repeat = read_opr(arg, 1); for (r8asm_data i = 0; i < repeat; i++) { tape.push_back(op); @@ -82,13 +86,13 @@ void expand_bytecode(std::vector &tape, R8Operand arg, rot8_bytec } } -std::vector r8asm_tape_out(std::span ops) -{ +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)) { expand_bytecode(tape, it.arg, *bc); - } else if (const r8asm_builtin *builtin = std::get_if(&it.op)) + } else if (const r8asm_builtin *builtin = + std::get_if(&it.op)) switch (*builtin) { case r8asm_builtin::LOOP: expand_loop(tape, it.arg); @@ -104,6 +108,8 @@ std::vector r8asm_tape_out(std::span ops) break; case r8asm_builtin::ANCHOR: expand_anchor(tape, it.arg); + case r8asm_builtin::ERASE: + expand_erase(tape, it.arg); break; default:; } diff --git a/src/r8asm_macro.cc b/src/r8asm_macro.cc index 489063d..b177ea0 100644 --- a/src/r8asm_macro.cc +++ b/src/r8asm_macro.cc @@ -1,20 +1,38 @@ #include "r8asm/r8asm_macro.hh" +#include "r8asm/r8asm_core.hh" std::map macros; +R8Operand read_arg(std::vector &argv, R8Operand argf, + argc_type argc) { + if (std::string *str = std::get_if(&argf)) { + if ((*str) == "%0") + return ((r8asm_data)argc); + else if ((*str)[0] == '%') + return (argv[std::stoi(str->substr(1)) - 1]); + // e.g. "%15" -> "15" -> 14 -> argv[14] + } + return (argf); +} + std::vector expand_macro(std::string macro_name, - std::vector argv) -{ + std::vector argv) { std::vector tmp_tape; - // TODO:explain args - for (auto i : macros[macro_name].ins) { - if (std::get_if(&i.op) || std::get_if(&i.op)) - tmp_tape.push_back(i); + if (std::get_if(&i.op) || + std::get_if(&i.op)) + tmp_tape.push_back( + {i.op, read_arg(argv, i.arg, macros[macro_name].argc)}); else if (r8asm_macro *macro = std::get_if(&i.op)) { - std::vector expanded_macro = expand_macro(macro->name, macro->args); - tmp_tape.insert(tmp_tape.end(), expanded_macro.begin(), expanded_macro.end()); + std::vector macro_args; + for (auto j : macro->args) + macro_args.push_back( + read_arg(argv, j, macros[macro_name].argc)); + std::vector macro_body = + expand_macro(macro->name, macro_args); + tmp_tape.insert(tmp_tape.end(), macro_body.begin(), + macro_body.end()); } }