From 725b2ae2ce0a55f206eccfe8eba9e8934327735c Mon Sep 17 00:00:00 2001 From: starcells Date: Wed, 13 May 2026 17:49:06 +0800 Subject: [PATCH] First workable version with preprocessing ability! --- asm/asm.r8asm | 12 +++++ include/r8asm/r8asm_core.hh | 15 +++--- include/r8asm/r8asm_macro.hh | 6 +-- include/r8asm/r8asm_parse.hh | 21 ++++---- src/r8asm_asm.cc | 14 +++--- src/r8asm_core.cc | 15 ++++-- src/r8asm_macro.cc | 43 ++++++++-------- src/r8asm_parse.cc | 98 ++++++++++++++++++++++++++++++++---- tests/r8_test.cc | 21 ++------ 9 files changed, 165 insertions(+), 80 deletions(-) create mode 100644 asm/asm.r8asm diff --git a/asm/asm.r8asm b/asm/asm.r8asm new file mode 100644 index 0000000..dc7ab11 --- /dev/null +++ b/asm/asm.r8asm @@ -0,0 +1,12 @@ +%macro test_erase 1 +.tp %1 +.loop _UNIT_MAX +biz +flb +rnz +ror +.endloop +%endmacro + +.xor 15 +test_erase $ \ No newline at end of file diff --git a/include/r8asm/r8asm_core.hh b/include/r8asm/r8asm_core.hh index f6775d5..229007c 100644 --- a/include/r8asm/r8asm_core.hh +++ b/include/r8asm/r8asm_core.hh @@ -35,27 +35,30 @@ enum class r8asm_builtin { ERASE, MACRO, ENDMACRO, + REPEAT, + ENDREPEAT, }; -using R8Operand = std::variant>; +using R8Operand = std::variant; -struct r8asm_macro { +struct r8asm_macrocall { std::string name; - std::vector args; + std::vector actual_args; }; -using R8MetaOp = std::variant; +using R8MetaOp = std::variant; struct R8Instruction { R8MetaOp op; - R8Operand arg; + R8Operand formal_para; }; extern std::map labels; extern std::map datas; extern mem_size_type dataptr; +extern std::map r8asm_insmap; + extern unit_size_type UNIT_SIZE; // #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8) #define DEFAULT_UNIT_SIZE 8 // Just for test. diff --git a/include/r8asm/r8asm_macro.hh b/include/r8asm/r8asm_macro.hh index 83e1ef1..7876198 100644 --- a/include/r8asm/r8asm_macro.hh +++ b/include/r8asm/r8asm_macro.hh @@ -7,11 +7,11 @@ struct R8Macro { argc_type argc = 0; std::vector ins; }; -extern std::map macros; +extern std::map r8asm_macromap; std::vector expand_single_macro(std::string macro_name, - std::vector argv); + std::vector argv); -std::vector expand_macros(std::vector &ins); +std::vector expand_macros(std::vector ins); #endif \ No newline at end of file diff --git a/include/r8asm/r8asm_parse.hh b/include/r8asm/r8asm_parse.hh index ac83e4d..7f7930e 100644 --- a/include/r8asm/r8asm_parse.hh +++ b/include/r8asm/r8asm_parse.hh @@ -2,24 +2,25 @@ #define R8ASM_PARSE #include "r8asm/r8asm_core.hh" -#include +#include +#include +#include #include #include #include #define EMPTY_CHARS (" \t\n\r\f\v") -typedef size_t ins_type_sign; +typedef std::list>::iterator r8asm_src_point; -struct r8asm_src { - std::vector> raw_src; - std::vector> codeblocks; +struct R8Src { + std::list> raw_src; + std::stack> codeblocks; + void preprocess(); }; -r8asm_src read_src(std::string filename); - -void include_src(r8asm_src); - -std::vector r8asm_preprocess(r8asm_src &src); +R8Src read_src(std::string filename); +std::vector r8asm_preprocess(R8Src &src); +extern std::map r8asm_parsemap; #endif \ No newline at end of file diff --git a/src/r8asm_asm.cc b/src/r8asm_asm.cc index be433ce..02ef2e8 100644 --- a/src/r8asm_asm.cc +++ b/src/r8asm_asm.cc @@ -90,27 +90,27 @@ 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); + expand_bytecode(tape, it.formal_para, *bc); } else if (const r8asm_builtin *builtin = std::get_if(&it.op)) switch (*builtin) { case r8asm_builtin::LOOP: - expand_loop(tape, it.arg); + expand_loop(tape, it.formal_para); break; case r8asm_builtin::ENDLOOP: - expand_endloop(tape, it.arg); + expand_endloop(tape, it.formal_para); break; case r8asm_builtin::TP:; - expand_tp(tape, it.arg); + expand_tp(tape, it.formal_para); break; case r8asm_builtin::XOR: - expand_xor(tape, it.arg); + expand_xor(tape, it.formal_para); break; case r8asm_builtin::ANCHOR: - expand_anchor(tape, it.arg); + expand_anchor(tape, it.formal_para); case r8asm_builtin::ERASE: break; - expand_erase(tape, it.arg); + expand_erase(tape, it.formal_para); break; default:; } diff --git a/src/r8asm_core.cc b/src/r8asm_core.cc index 603683d..cb28e1c 100644 --- a/src/r8asm_core.cc +++ b/src/r8asm_core.cc @@ -2,7 +2,16 @@ unit_size_type UNIT_SIZE = DEFAULT_UNIT_SIZE; mem_size_type dataptr = 0; -std::map labels = { { std::string("main"), 0 } }; +std::map labels = {{std::string("main"), 0}}; std::map datas = { - { std::string("_UNIT_MAX"), UNIT_MAX } -}; \ No newline at end of file + {std::string("_UNIT_MAX"), UNIT_MAX}}; + +std::map r8asm_insmap = { + {"stp", rot8_bytecode::STP}, {"btp", rot8_bytecode::BTP}, + {"ror", rot8_bytecode::ROR}, {"flb", rot8_bytecode::FLB}, + {"biz", rot8_bytecode::BIZ}, {"rnz", rot8_bytecode::RNZ}, + {"out", rot8_bytecode::OUT}, {"inp", rot8_bytecode::INP}, + {".loop", r8asm_builtin::LOOP}, {".endloop", r8asm_builtin::ENDLOOP}, + {".anchor", r8asm_builtin::ANCHOR}, {".tp", r8asm_builtin::TP}, + {".xor", r8asm_builtin::XOR}, {".erase", r8asm_builtin::ERASE}, +}; diff --git a/src/r8asm_macro.cc b/src/r8asm_macro.cc index 4986477..a89b24e 100644 --- a/src/r8asm_macro.cc +++ b/src/r8asm_macro.cc @@ -1,9 +1,8 @@ #include "r8asm/r8asm_macro.hh" -#include "r8asm/r8asm_core.hh" #include #include -std::map macros = { +std::map r8asm_macromap = { {std::string("STEP"), {1, {{rot8_bytecode::STP, std::string("%1")}, @@ -13,35 +12,36 @@ std::map macros = { {{rot8_bytecode::BTP, std::string("%1")}, {rot8_bytecode::BTP, std::string("%1")}}}}}; -R8Operand read_arg(std::vector &argv, R8Operand argf, +R8Operand read_arg(std::vector &actual_args, R8Operand formal_para, argc_type argc) { - if (std::string *str = std::get_if(&argf)) { + if (std::string *str = std::get_if(&formal_para)) { if ((*str) == "%0") return ((r8asm_data)argc); else if ((*str)[0] == '%') - return (argv[std::stoi(str->substr(1)) - 1]); + return (actual_args[std::stoi(str->substr(1)) - 1]); // e.g. "%15" -> "15" -> 14 -> argv[14] } - return (argf); + return (formal_para); } -std::vector expand_single_macro(std::string macro_name, - std::vector argv) { +std::vector +expand_single_macro(std::string macro_name, + std::vector actual_args) { std::vector tmp_tape; - std::vector macro_args; + R8Macro this_macro = r8asm_macromap[macro_name]; - for (auto i : macros[macro_name].ins) { + for (auto i : this_macro.ins) { 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)) { - macro_args.clear(); - for (auto j : macro->args) - macro_args.push_back( - read_arg(argv, j, macros[macro_name].argc)); + {i.op, read_arg(actual_args, i.formal_para, this_macro.argc)}); + else if (r8asm_macrocall *tmp_macro = + std::get_if(&i.op)) { + std::vector macro_args; + for (auto j : tmp_macro->actual_args) + macro_args.push_back(read_arg(actual_args, j, this_macro.argc)); std::vector macro_body = - expand_single_macro(macro->name, macro_args); + expand_single_macro(tmp_macro->name, macro_args); tmp_tape.insert(tmp_tape.end(), macro_body.begin(), macro_body.end()); } @@ -50,13 +50,12 @@ std::vector expand_single_macro(std::string macro_name, return (tmp_tape); } -std::vector expand_macros(std::vector &ins) { +std::vector expand_macros(std::vector ins) { std::vector tape; - std::vector tmp_tape; for (auto i : ins) { - tmp_tape.clear(); - if (r8asm_macro *macro = std::get_if(&i.op)) { - tmp_tape = (expand_single_macro(macro->name, macro->args)); + if (r8asm_macrocall *tmp_macro = std::get_if(&i.op)) { + std::vector tmp_tape = + (expand_single_macro(tmp_macro->name, tmp_macro->actual_args)); tape.insert(tape.end(), tmp_tape.begin(), tmp_tape.end()); } else tape.push_back(i); diff --git a/src/r8asm_parse.cc b/src/r8asm_parse.cc index cae3c51..f95a257 100644 --- a/src/r8asm_parse.cc +++ b/src/r8asm_parse.cc @@ -1,11 +1,22 @@ #include "r8asm/r8asm_parse.hh" -#include "r8asm/r8asm_core.hh" +#include "r8asm/r8asm_macro.hh" + #include +#include +#include #include +#include #include #include #include +std::map r8asm_parsemap = { + {"%macro", r8asm_builtin::MACRO}, + {"%endmacro", r8asm_builtin::ENDMACRO}, + {"%repeat", r8asm_builtin::REPEAT}, + {"%endrepeat", r8asm_builtin::ENDREPEAT}, +}; + void r8asm_trim(std::string &str) { auto start = str.find_first_not_of(EMPTY_CHARS); @@ -33,15 +44,15 @@ std::string r8asm_peel(std::string &str) { } } -r8asm_src read_src(std::string filename) { - r8asm_src src; +R8Src read_src(std::string filename) { + R8Src src; - ins_type_sign line_counter = 0; std::ifstream ifile; - std::string line_buffer; ifile.open(filename, std::ios::in); + + std::string line_buffer; std::vector ins_buffer; - std::stack matcher; + std::stack matcher; while (std::getline(ifile, line_buffer)) { ins_buffer.clear(); auto comment = line_buffer.find_first_of(';'); @@ -49,22 +60,87 @@ r8asm_src read_src(std::string filename) { line_buffer = line_buffer.substr(0, comment); if (line_buffer.empty()) continue; - line_counter++; while (!line_buffer.empty()) ins_buffer.push_back(r8asm_peel(line_buffer)); src.raw_src.push_back({ins_buffer}); if (ins_buffer[0][0] == '%') { if (ins_buffer[0].substr(1, 3) == "end") { - src.codeblocks.push_back({matcher.top(), line_counter}); + src.codeblocks.push( + {matcher.top(), std::prev(src.raw_src.end())}); matcher.pop(); } else - matcher.push(line_counter); + matcher.push(--src.raw_src.end()); } } ifile.close(); return (src); } -std::vector r8asm_preprocess(r8asm_src &src) { - std::vector ins; +R8Operand r8asm_arg_stoi(std::string raw_arg) { + try { + r8asm_data arg = std::stoi(raw_arg); + return (arg); + } catch (std::invalid_argument) { + if (datas.find(raw_arg) != datas.end()) + return (datas[raw_arg]); + return (raw_arg); + } +} + +R8Instruction +r8asm_translate_single_ins(std::vector const raw_ins) { + R8MetaOp tmp_op; + if (r8asm_insmap.find(raw_ins[0]) != r8asm_insmap.end()) + if (raw_ins.size() == 1) + return (R8Instruction{r8asm_insmap[raw_ins[0]]}); + else + return (R8Instruction{r8asm_insmap[raw_ins[0]], + r8asm_arg_stoi(raw_ins[1])}); + else if (r8asm_macromap.find(raw_ins[0]) != r8asm_macromap.end()) { + std::vector tmp_operands; + for (auto i : raw_ins | std::views::drop(1)) + tmp_operands.push_back(r8asm_arg_stoi(i)); + return (R8Instruction{r8asm_macrocall{raw_ins[0], tmp_operands}}); + } + return (R8Instruction{}); +} + +void R8Src::preprocess() { + std::vector codeblock; + while (!this->codeblocks.empty()) { + auto i = this->codeblocks.top(); + this->codeblocks.pop(); + codeblock.clear(); + auto raw_codeblock = + std::ranges::subrange(std::next(i.first), i.second); + /* + src.raw_src: 0 1 2 3(%macro) 4 5 6 7(%endmacro) 8 + i:-----------------^first----------^second------- + subrange:--------------------4 5 6--------------- + */ + for (auto j : raw_codeblock) + codeblock.push_back(r8asm_translate_single_ins(j)); + if ((*i.first)[0] == "%macro") { + r8asm_macromap.insert_or_assign( + (*i.first)[1], R8Macro{(argc_type)std::get( + r8asm_arg_stoi((*i.first)[2])), + codeblock}); + } else if ((*i.first)[0] == "%repeat") { + for (auto count = + std::get(r8asm_arg_stoi((*i.first)[1])); + count > 0; count--) + this->raw_src.insert(std::next(i.second), std::next(i.first), + i.second); + } + this->raw_src.erase(i.first, std::next(i.second)); + } +} + +std::vector r8asm_preprocess(R8Src &src) { + std::vector tmp_ins; + src.preprocess(); + for (auto i : src.raw_src) { + tmp_ins.push_back(r8asm_translate_single_ins(i)); + } + return (tmp_ins); } \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index cd9b264..6755924 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -2,24 +2,9 @@ #include "r8asm/r8asm.hh" -int main() { - std::vector ins = { - R8Instruction{rot8_bytecode::INP}, - // To make clang-format distribute all instructions into diffirent - // lines. - {rot8_bytecode::STP}, - {r8asm_builtin::XOR, 255}, - {rot8_bytecode::BTP}, - {r8asm_builtin::LOOP}, - {rot8_bytecode::BIZ}, - {rot8_bytecode::FLB}, - {rot8_bytecode::RNZ}, - {rot8_bytecode::ROR}, - {rot8_bytecode::OUT}, - {r8asm_builtin::ENDLOOP}, - }; - auto out_tape = r8asm_tape_out(expand_macros(ins)); - +int main(int argc, char *argv[]) { + auto src = read_src(argc > 1 ? argv[1] : "asm/asm.r8asm"); + auto out_tape = r8asm_tape_out(expand_macros(r8asm_preprocess(src))); for (auto i : out_tape) std::cout << ROT8_BC_TO_CHAR(i); return (0);