r8asm %for: repetition with a loop variable
clb_digits needed to name a different cell every round, which %rep cannot do: it
copies identical text N times and has no loop variable, and there is no %if
either, so the three shift steps were spelled out by hand and the macro was
only correct for 11 bit cells.
%for k 3 ; k is 0, 1, 2 inside the body
step k
%endfor
%for <name> <count> binds <name> to 0..count-1 as a compile time constant that
any expression can use: %rep counts, .tp targets, macro arguments. The loop
scope is a copy of the current scope plus the name, so a %for inside a macro
body still sees that macro's %1, %2, ... %rep and %for nest in each other
(both closers count as a closer). %rep itself is untouched.
With it, clb_digits lost its hardcoded chain: the digit count, the shift count
and now the constant builds (tails written as _CELL_SIZE - 5 / _CELL_SIZE - 3)
all follow _CELL_SIZE. Checked at 16 bits with a five cell region: 4660 gave
'0' '4' '6' '6' '0' and 7 gave '0' '0' '0' '0' '7', so the count really did
become five. 65535 hangs in clb_divmod (6553 rounds, over the 5M step budget)
-- that is the divider's O(N/D), not digits. Back at 11 bits everything is
unchanged: all eleven values, hash 28729, byte_op and docs green.
SPEC 3.5 documents the directive; it is a local extension like %synth.
This commit is contained in:
1 parent
b962c345c8
commit
91e3fed537
4 files changed
+106
-22
No files matched your search
@@ -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 <name> <count>`:`<name>` 在 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/` —— 总则
|
||||
|
||||
+17
-18
@@ -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 <count>`(见 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
|
||||
|
||||
@@ -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<std::string, directive_t> 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);
|
||||
|
||||
+66
-2
@@ -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 <name> <count> expands the body <count> times with <name> 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<std::string, general_data> 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;
|
||||
|
||||
Reference in new issue
Block a user