diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b296ef --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# R8ASM + +基于[ROT-8](https://gitea.starcells.ltd/starcells/ROT-8.git)的汇编语言\ +和用于汇编汇编的汇编器类 + +## 作为语言的R8ASM + +### 简介 + +rot8指令集是图灵完备的(假如tape无限长),因此就像大多数计算工具一样,rot8纸带"能表示任何计算任务" \ +但这显然不是个令人满意的答案,理论上的成立从来不意味着工程上的可行,而实践表明用rot8本身编写处理复杂任务的程序在工程上确实几乎是不可行的 \ +因此rot8也需要自己的汇编--r8asm,r8asm汇编的存在使得全加器纸带这类规模动辄成千上万条指令的复杂计算得以实现,且具有不错的复用能力,同时提供了抽象层以供[别的怪东西](https://gitea.starcells.ltd/starcells/R8VeritasCC)发挥威力 \ +r8asm的语法很大程度上参考了nasm,因为这样在vscode里把.r8asm文件的语言模式绑定到nasm就可以享受语法高亮了,仅此而已 + +### 语法规范 + +见[SPEC](SPEC.md) + +## 作为汇编器的R8ASM + +### 简介 + +一个简单的汇编器实现,以class的形式封装 \ +因为是重构,着急拿来干活,所以一些细节实现得比较糟糕 \ +当然SPEC里提到的功能肯定是已实现的 + +### 标准宏 + +在[asm](asm)里提供了一些标准宏实现可供取用.标准宏命名规范,定义和功能同样见[SPEC](SPEC.md) + +### 示例 + +``` CPP +// 若以库的形式调用r8asm +#include "r8asm/r8asm.hh" +#include +int main() { + r8asm a; + size_t cell_size = 8; + a.import_file("asm/test.r8asm", cell_size); + std::cout << a.export_rot8(); + return (0); +} +``` + +``` SH +# 若执行r8asm_test以汇编asm/test.r8asm +mkdir build +cd build +cmake .. +make -j$(nproc) +r8asm_test > test.rot8 +``` + +### TODO + +1. 各种Warning/Error提示 +2. r8asm类的分割(架构调整) diff --git a/SPEC.md b/SPEC.md new file mode 100644 index 0000000..f472141 --- /dev/null +++ b/SPEC.md @@ -0,0 +1,32 @@ +# R8ASM Specification + +## 作用域规则 + + 当调用宏时,宏体内会被视为新的作用域 \ + 在子作用域内,父作用域的符号是可读的, + +### TODO + +## 作为变量的编译期常量 + +由于rot8本身的限制,r8asm在运行时是几乎没有自由度可言的.内存中的数据难以参与控制流,r8asm自然也就无法实现灵活的各类取值运算 \ +因此,r8asm采用 + +### TODO + +## 运算符优先级 + +r8asm的运算符优先级与nasm一致,具体来讲: + +|运算符|含义|优先级| +|---|---|---| +|~|按位取反|0| +|*|乘|1| +|/|除|1| +|+|加|2| +|-|减|2| +|&|按位与|3| +|^|按位异或|4| +|\||按位或|5| + +## 标准宏 diff --git a/asm/lib.r8asm b/asm/lib.r8asm index 265932b..3d7bea4 100644 --- a/asm/lib.r8asm +++ b/asm/lib.r8asm @@ -46,7 +46,7 @@ flb ; Macro rol ; %this: Cell operand %macro rol 0 -%rep _ROL_STEP +%rep _CELL_SIZE - 1 ror %endrep %endmacro diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 4a36446..2a20b77 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -9,12 +9,6 @@ #include #include -constexpr size_t CELL_SIZE = 8; -constexpr size_t CELL_MAX(((uint64_t)1 << CELL_SIZE) - 1); -constexpr size_t ROL_STEP(CELL_SIZE - 1); - -namespace r8asm_maps {} - class r8asm { using data = int16_t; using parameter_index = uint8_t; @@ -89,13 +83,13 @@ private: class ast { public: ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}}; + size_t CELL_SIZE; + // I know template class is the most elegant solution of CELL_SIZE + // but I can't be bother to add that. private: addr dataptr = 0; - std::vector> scopes = { - {{"_CELL_SIZE", CELL_SIZE}, - {"_CELL_MAX", CELL_MAX}, - {"_ROL_STEP", ROL_STEP}}}; + std::vector> scopes; std::map macros; enum class builtin_t { @@ -159,6 +153,7 @@ private: ast_node expand_node_to_ins(const ast_node &node); std::string expand_root(); bool import_macro(const std::string ¯o_name, const ast_node &node); + void set_cell_size(size_t cell_size); }; ast main_tree; @@ -183,5 +178,5 @@ private: public: std::string export_rot8(); - bool import_file(const std::string &filename); + bool import_file(const std::string &filename, size_t cell_size); }; \ No newline at end of file diff --git a/src/r8asm.cc b/src/r8asm.cc index 9c0bbb7..32e1798 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -1,5 +1,6 @@ #include "r8asm/r8asm.hh" #include +#include #include #include #include @@ -60,6 +61,13 @@ std::string peel(std::string &str) { // Peel the first word from string. #undef EMPTY_CHARS #undef OPERATORS +void r8asm::ast::set_cell_size(size_t cell_size) { + CELL_SIZE = cell_size; + scopes = {{{"_CELL_SIZE", cell_size}, + {"_CELL_MAX", (1 << cell_size) - 1}, + {"_ROL_STEP", cell_size - 1}}}; +} + r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) { general_data to_return = 0; switch (node.type) { @@ -248,7 +256,7 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) { ror */ data d = expand_node_to_data(node.children[0]); - for (auto i = 0; i < CELL_SIZE; i++) { + for (auto i = 0; i < scopes[0].at("_CELL_SIZE"); i++) { if (d & 1) to_return.children.push_back(ROT8_NODE("flb")); to_return.children.push_back(ROT8_NODE("ror")); @@ -404,8 +412,8 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { filename += tp->value; // now tp points at RQUOTE - import_file( - filename); // TODO: check whether the file is open successfully + import_file(filename, main_tree.CELL_SIZE); // TODO: check whether the + // file is open successfully } break; default: break; @@ -692,9 +700,10 @@ r8asm::token_stream::lex(const std::string &char_stream) { } #undef OPERATORS -bool r8asm::import_file(const std::string &filename) { +bool r8asm::import_file(const std::string &filename, size_t cell_size) { for (const auto &token_it : parse(token_stream(filename)).children) { main_tree.root.children.push_back(token_it); } + main_tree.set_cell_size(cell_size); return true; // TODO: ERROR CHECK } \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 34d9612..1dcbde5 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -3,7 +3,7 @@ #include int main() { r8asm a; - a.import_file("asm/test.r8asm"); + a.import_file("asm/test.r8asm", 8); std::cout << a.export_rot8(); return (0); } \ No newline at end of file