diff --git a/include/r8asm/r8asm_core.hh b/include/r8asm/r8asm_core.hh index 7f83083..bfa2d7a 100644 --- a/include/r8asm/r8asm_core.hh +++ b/include/r8asm/r8asm_core.hh @@ -8,15 +8,16 @@ #include typedef uint32_t r8asm_label; -typedef uint32_t r8asm_data; +typedef int32_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; +typedef uint32_t argc_type; -#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast(bc)]) +#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast(bc)]) -enum class rot8_bytecode { STP, +enum class rot8_bytecode { + STP, BTP, ROR, FLB, @@ -25,7 +26,7 @@ enum class rot8_bytecode { STP, OUT, INP, }; -enum class r8asm_macro { +enum class r8asm_builtin { LOOP, ENDLOOP, TP, @@ -34,28 +35,31 @@ enum class r8asm_macro { MACRO, ENDMACRO, }; - using R8Operand = std::variant; -using R8MetaOp = std::variant; + +struct r8asm_macro { + std::string name; + std::vector args; +}; + +using R8MetaOp = std::variant; struct R8Instruction { R8MetaOp op; - R8Operand arg = std::monostate {}; -}; - -class R8Macro { - argc_type argc; - std::vector ins; + R8Operand arg; + R8Instruction(R8MetaOp in_op, R8Operand in_arg = std::monostate {}) + : op(in_op) + , arg(in_arg) { + }; }; 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 DEFAULT_UNIT_SIZE 8 // 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 index 6a5edb0..3b4b06f 100644 --- a/include/r8asm/r8asm_macro.hh +++ b/include/r8asm/r8asm_macro.hh @@ -2,6 +2,11 @@ #define R8ASM_MACRO #include "r8asm/r8asm_core.hh" +struct R8Macro { + argc_type argc; + std::vector ins; +}; +extern std::map macros; std::vector expand_macro(std::string macro_name, std::vector argv); diff --git a/src/r8asm_asm.cc b/src/r8asm_asm.cc index 9f8d7d8..2714d37 100644 --- a/src/r8asm_asm.cc +++ b/src/r8asm_asm.cc @@ -1,11 +1,11 @@ #include "r8asm/r8asm_asm.hh" +#include "r8asm/r8asm_core.hh" #include #include #include -static r8asm_data -read_opr(R8Operand arg, r8asm_data default_opr = 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,7 +16,7 @@ read_opr(R8Operand arg, r8asm_data default_opr = 0) return (default_opr); } -static void expand_xor(std::vector &tape, R8Operand arg) +void expand_xor(std::vector &tape, R8Operand arg) { r8asm_data opr = read_opr(arg); @@ -28,7 +28,7 @@ static void expand_xor(std::vector &tape, R8Operand arg) } } -static void expand_loop(std::vector &tape, R8Operand arg) +void expand_loop(std::vector &tape, R8Operand arg) { r8asm_data opr = read_opr(arg, UNIT_MAX); @@ -38,17 +38,15 @@ static void expand_loop(std::vector &tape, R8Operand arg) tape.push_back(rot8_bytecode::BTP); // >(xor)[< } -static void expand_endloop(std::vector &tape, R8Operand arg) +void expand_endloop(std::vector &tape, R8Operand arg) { - std::vector tmp = { rot8_bytecode::STP, - rot8_bytecode::FLB, - rot8_bytecode::ROR, - rot8_bytecode::RNZ, + 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) +void expand_tp(std::vector &tape, R8Operand arg, bool count_offset = true) { r8asm_data opr = read_opr(arg); @@ -58,10 +56,11 @@ static void expand_tp(std::vector &tape, R8Operand arg) for (unit_offset_type i = 0; i < offset; i++) { tape.push_back(stepforward ? rot8_bytecode::STP : rot8_bytecode::BTP); } - dataptr = opr; + if (count_offset) + dataptr = opr; } -static 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 }); @@ -69,35 +68,41 @@ static void expand_anchor(std::vector &tape, R8Operand arg) datas.insert({ std::to_string(*name), dataptr }); } -std::vector -r8asm_tape_out(std::span ops) +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); + if (count_offset) { + if (op == rot8_bytecode::STP) + dataptr++; + else if (op == rot8_bytecode::BTP) + 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_bytecode(tape, it.arg, *bc); + } else if (const r8asm_builtin *builtin = std::get_if(&it.op)) + switch (*builtin) { + case r8asm_builtin::LOOP: expand_loop(tape, it.arg); break; - case r8asm_macro::ENDLOOP: + case r8asm_builtin::ENDLOOP: expand_endloop(tape, it.arg); break; - case r8asm_macro::TP:; + case r8asm_builtin::TP:; expand_tp(tape, it.arg); break; - case r8asm_macro::XOR: + case r8asm_builtin::XOR: expand_xor(tape, it.arg); break; - case r8asm_macro::ANCHOR: + case r8asm_builtin::ANCHOR: expand_anchor(tape, it.arg); break; default:; diff --git a/src/r8asm_core.cc b/src/r8asm_core.cc index e2c6dcc..603683d 100644 --- a/src/r8asm_core.cc +++ b/src/r8asm_core.cc @@ -3,5 +3,6 @@ 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 } }; +std::map datas = { + { std::string("_UNIT_MAX"), UNIT_MAX } +}; \ No newline at end of file diff --git a/src/r8asm_macro.cc b/src/r8asm_macro.cc index a2101d3..489063d 100644 --- a/src/r8asm_macro.cc +++ b/src/r8asm_macro.cc @@ -1,7 +1,22 @@ #include "r8asm/r8asm_macro.hh" -std::vector expand_macro(std::string macro_name, std::vector argv) +std::map macros; + +std::vector expand_macro(std::string macro_name, + std::vector argv) { - std::vector tape; - return (tape); + 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); + 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()); + } + } + + return (tmp_tape); } \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index e44b9a0..eaae839 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,14 +1,19 @@ #include #include "r8asm/r8asm.hh" +#include "r8asm/r8asm_core.hh" 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 } + R8Instruction { rot8_bytecode::INP }, // To make clang-format distribute all + // instructions into diffirent lines. + { r8asm_builtin::LOOP }, + { rot8_bytecode::BIZ }, + { rot8_bytecode::FLB }, + { rot8_bytecode::RNZ }, + { rot8_bytecode::ROR }, + { r8asm_builtin::ENDLOOP }, }; std::vector tape = r8asm_tape_out(ins);