diff --git a/SPEC.md b/SPEC.md index 56e97f9..7481341 100644 --- a/SPEC.md +++ b/SPEC.md @@ -143,6 +143,24 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列 | 表达不出"无条件翻转" | 模型只会"牺牲一个 cell → 条件翻转别的 cell",输入为 0 时整块被跳过 | | 原地写回的表(`A←~A^Ci` 等四个)无解 | 一个合成块只有一个条件 cell,区分不了 "A=1 且 Ci=0" 和 "A=1 且 Ci=1"(手写可以,见 §8) | +### 3.5 `%for`:带循环变量的重复(本仓库给 r8asm 加的) + +`%rep` 只会把**同一段文本**抄 N 遍,所以"每轮换一个偏移"写不出来。`%for` 补上这个: + + %for k 3 ; k = 0, 1, 2 + step k + %endfor ; -> 第 0 轮没有 stp,第 1 轮两个,第 2 轮四个 + +* `%for `:`` 在 body 里是**汇编期常量**(0..count-1),可以出现在 + 任何表达式里 —— `%rep` 的次数、`.tp` 的目标、宏实参都行。 +* body 里的 `%2` 之类宏形参照常可见(循环作用域是"复制当前作用域再加一个名字")。 +* 和 `%rep` / `%synth` 可以互相嵌套(`%endfor` 和 `%endrep` 都算配对的收口)。 +* 和 `%synth` 一样是**本仓库的扩展**,不在上游语言规格里。`%rep` 保持原样。 + +用处就在这里:`clb_digits` 的挪位链原来是三行手写(`REG+2→REG+3`、`REG+1→REG+2`、 +`REG+0→REG+1`),位宽一改就静默算错;现在是 `%for k L-1` 生成,**位数、链长、 +常数构建的尾部转数全部跟着 `_CELL_SIZE` 走**。 + --- ## 4. 标准库 `asm/` —— 总则 diff --git a/asm/digits.r8asm b/asm/digits.r8asm index ed3ec67..97d4b18 100644 --- a/asm/digits.r8asm +++ b/asm/digits.r8asm @@ -20,11 +20,10 @@ ; 否则先把低位搬上去会盖掉还没搬的高位。这样最后 REG+0..3 正好是千、百、十、个, ; 也就是"高位在前"的字符串,不用倒序、不用 4 个分支。 ; -; **约束:挪位链是 L-1 = 3 步,写死的。** 轮数(上面 K 的个数)会跟着位宽自动算, -; 但挪位链不行 —— 每一步要写*不同*的地址(REG+1、REG+2、REG+3),而 r8asm 没有 -; `%if`、`%rep` 也没有循环变量,没法"按 L 生成 L-1 段不同的代码"。所以本宏 -; **只在 `_CELL_SIZE == 11`(L=4)下正确**;位宽改 16 时要加一步 `REG+3 -> REG+4` -; (并多给一格),否则静默出错。彻底的解法是给汇编器加一个带下标的 `%for`。 +; **位数、挪位链、常数构建三者都跟着 `_CELL_SIZE` 走**:位数和链长用 +; `%for k `(见 SPEC §3.5),常数构建的尾部转数写成 `_CELL_SIZE - 5` / +; `_CELL_SIZE - 3`。11 位 -> 4 位十进制 / 3 轮挪位;16 位 -> 5 位 / 4 轮 +; (实测 16 位下 `4660 -> "04660"`、`7 -> "00007"`)。 ; ; (第一版是 4 个分支 + one-hot flag 分派,只跑对了第一支 —— 甩掉分派之后不仅 ; 正确,还少了两层嵌套和 4 个 cell。) @@ -71,25 +70,25 @@ loop %3+6 ror %endrep set_1 - %rep 8 + %rep _CELL_SIZE - 3 ror %endrep ; N = 这位数字(0..9),Q = 商 clb_divmod %1 %3+0 %3+1 %3+2 - ; ---- 已有数字整体上移一格:从高到低搬 - erase %3+7 - .tp %2+2 - copy %2+3 %3+7 - erase %3+7 - .tp %2+1 - copy %2+2 %3+7 - erase %3+7 - .tp %2+0 - copy %2+1 %3+7 + ; ---- 已有数字整体上移一格:从高到低搬。 + ; 轮数 = L-1,用 %for 跟着位宽自动来(11 位 3 轮、16 位 4 轮)。 + %for k _CELL_SIZE * 30103 / 100000 + erase %3+7 + .tp %2 + _CELL_SIZE * 30103 / 100000 - 1 - k + copy %2 + _CELL_SIZE * 30103 / 100000 - k %3+7 + %endfor - ; ---- REG+0 = '0' + 数字(位 4、5) + ; ---- REG+0 = '0' + 数字(位 4、5)。 + ; 走一圈:转 4 格置位、再转 1 格置位,最后转回原点。 + ; **尾部的转数必须写成 `_CELL_SIZE - 5`**,不能写死 6 —— 16 位下 + ; 写死会让 0x30 建错(实测 16 位时得到 0x600)。 erase %2+0 .tp %2+0 %rep 4 @@ -98,7 +97,7 @@ loop %3+6 set_1 ror set_1 - %rep 6 + %rep _CELL_SIZE - 5 ror %endrep clb_add_ip %2+0 %1 %3+2 %3+3 diff --git a/include/r8forge/r8asm.hh b/include/r8forge/r8asm.hh index 97a84d9..7fc8345 100644 --- a/include/r8forge/r8asm.hh +++ b/include/r8forge/r8asm.hh @@ -91,6 +91,7 @@ private: BUILTIN, MACROCALL, REPEAT, + FOR, ROT8, CODEBLOCK, STRING, @@ -212,15 +213,17 @@ private: ENDMACRO, REP, ENDREP, + FOR, + ENDFOR, INCLUDE, }; std::map directive_keywords = { {"macro", directive_t::MACRO}, {"endmacro", directive_t::ENDMACRO}, - {"rep", directive_t::REP}, - {"synth", directive_t::SYNTH}, + {"rep", directive_t::REP}, {"synth", directive_t::SYNTH}, {"endsynth", directive_t::ENDSYNTH}, {"endrep", directive_t::ENDREP}, + {"for", directive_t::FOR}, {"endfor", directive_t::ENDFOR}, {"include", directive_t::INCLUDE}}; ast_node parse(const token_stream &stream); diff --git a/src/r8asm.cc b/src/r8asm.cc index a822c16..a25f373 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -186,6 +186,30 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) { for (const auto &i : expand_node_to_ins(node.children[1]).children) to_return.children.push_back(i); } break; + case ast_node_t::FOR: { + /* + FOR + value: loop variable name + children[0]: count + children[1]: codeblock + %for expands the body times with bound to + 0, 1, ... count-1. Unlike %rep the body is not identical every round -- + the name is a compile time constant, so expressions inside the body can + depend on which round it is (that is the whole point: naming cell +k for + a different k every round). + */ + data count = expand_node_to_data(node.children[0]); + for (data k = 0; k < count; k++) { + /* Copy the innermost scope rather than starting an empty one: inside a + macro body a %for loop still has to see that macro's %1, %2, ... */ + std::map loop_scope = scopes.back(); + loop_scope.insert_or_assign(node.value, k); + scopes.push_back(loop_scope); + for (const auto &i : expand_node_to_ins(node.children[1]).children) + to_return.children.push_back(i); + scopes.pop_back(); + } + } break; case ast_node_t::MACROCALL: { /* MACROCALL @@ -417,9 +441,10 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { // now it points at %rep line for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDREP) && (layer == 0)); it++) { - if (POINTS_AT_DIRECTIVE(it, REP)) + if (POINTS_AT_DIRECTIVE(it, REP) || POINTS_AT_DIRECTIVE(it, FOR)) layer++; - else if (POINTS_AT_DIRECTIVE(it, ENDREP)) + else if (POINTS_AT_DIRECTIVE(it, ENDREP) || + POINTS_AT_DIRECTIVE(it, ENDFOR)) layer--; tmp_codeblock_stream.lines.push_back(*it); } @@ -429,6 +454,45 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { parse(tmp_codeblock_stream)); // codeblock will be pushed here. } break; + case directive_t::FOR: { + /* + FOR + value: loop variable name + children[0]: count + children[1]: codeblock + */ + if (it->tokens.size() < 3) { + main_tree.fail("%for needs a name and a count"); + break; + } + const std::string loop_name = it->tokens[1].value; + + token_line buffer = {it->number, {}}; + for (auto token_it = it->tokens.begin() + 2; // name, then the count + token_it < it->tokens.end(); token_it++) + buffer.tokens.push_back(*token_it); + + to_return.children.push_back({r8asm::ast_node_t::FOR, loop_name, {}}); + to_return.children.back().children.push_back(parse_single_arg(buffer)); + tmp_codeblock_stream.lines = {}; + + depth_t for_layer = 0; + for (it += 1; + !((POINTS_AT_DIRECTIVE(it, ENDFOR) || + POINTS_AT_DIRECTIVE(it, ENDREP)) && + (for_layer == 0)); + it++) { + if (POINTS_AT_DIRECTIVE(it, FOR) || POINTS_AT_DIRECTIVE(it, REP)) + for_layer++; + else if (POINTS_AT_DIRECTIVE(it, ENDFOR) || + POINTS_AT_DIRECTIVE(it, ENDREP)) + for_layer--; + tmp_codeblock_stream.lines.push_back(*it); + } + + to_return.children.back().children.push_back( + parse(tmp_codeblock_stream)); + } break; case directive_t::INCLUDE: { std::string filename = ""; auto tp = it->tokens.begin() + 1;