%synth: the cost model is a run option, not a per block metric line

`metric chars|flat|blocks` inside %synth meant the same truth table could be
searched twice for two call sites, and every library block had to spell out
`metric flat` to avoid the default.  The cost model belongs to the whole
assembly, so it is one now:

    r8forge <file.r8asm>                  flat (layout free), the default
    r8forge --synth-chars <file.r8asm>    charge the real .tp walk

flat stays the default because a library block is expanded at dozens of call
sites with different layouts: only a layout free result can share one cache
entry, and it is usually within a few percent (full_add: 95 vs 90 characters).
The chars search is far more expensive (full_add: 415M states, ~21 minutes,
~10 GB), which is exactly why it should be asked for on the command line.

`metric blocks` goes with it: block count is a poor proxy for tape length (and
it was only another way to ask for mode layers).

Implementation: the metric lives in the assembler (ast::synth_metric, default
1) and is set through r8asm::set_synth_metric() before import_file; the CLI in
tests/asm_test.cc parses --synth-chars / --synth-flat.  The cache key already
carried metric, so the committed entries still hit: assembling the library and
asm/test.r8asm gives byte identical tapes (28729 characters, byte_op_test all
green, all 13 examples green, synth_test still 8/8).

A `metric` line now fails with a message that names the flag instead of being
silently ignored.  examples/full_add_flat.r8asm and full_add_flat_spaced.r8asm
are removed: they only existed to demonstrate the per block metric (the flat
default makes them copies of full_add.r8asm).

Also repairs the mojibake in README's metric section: an earlier tool wrote
\x0c / \x08 / \x0d where \f, \b and \r belonged, which ate characters out of
"flat", "blocks", "biz/flb/rnz" and "rnz".  The section is rewritten for the
flag, and the %synth numbers in README/SPEC/AGENTS now match what the tree
prints.

New cache entry c16bf083bb0ccbc8.txt is full_add's stat table with mode layers
+ flat, which full_add_stat.r8asm now produces.
This commit is contained in:
dsh committed 2026-11-07 23:44:01 +08:00
1 parent 66ed7bc263
commit b4ea79ba91
17 files changed
+142 -129

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
# params 6 mode 1 metric 1
0 38 0
1 41 0
2 51 0
3 20 1
5 31 0
+9 -7
View File
@@ -40,13 +40,13 @@
```sh ```sh
r8forge asm/test.r8asm # 28729 字符 r8forge asm/test.r8asm # 28729 字符
r8forge_byte_op_test # 20 项(clb_divmod 16384 组、clb_digits 2048 个值) r8forge_byte_op_test # 20 项(clb_divmod 16384 组、clb_digits 2048 个值)
r8forge_examples_test # examples/ 里 15 个程序,头部 `; out` 逐个核对 r8forge_examples_test # examples/ 里 13 个程序,头部 `; out` 逐个核对
r8forge_macro_docs_test # 47 macros in 21 files r8forge_macro_docs_test # 47 macros in 21 files
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0 r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0
``` ```
**另一个仓库**:`../ROT-8-viz`(fork `dsh/ROT-8-viz`,`main` = `754f885`)有串行 7E1 **另一个仓库**:`../ROT-8-viz`(fork `dsh/ROT-8-viz`,`main` = `6e51df5`)有串行 7E1
(`-s` / `--parity` / `--stop` / `--7e1`),`clb_digits` 产出的 ASCII 正好能直接喂它的 (`-s` / `--parity` / `--stop` / `--7e1`),`clb_digits` 产出的 ASCII 正好能直接喂它的
`OUT`。那边的工作树也是干净的。 `OUT`。那边的工作树也是干净的。
@@ -74,7 +74,7 @@ r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0
`Get-Content` 默认按 GBK 读 UTF-8 文件,再 `Set-Content -Encoding utf8` 写回去就是 `Get-Content` 默认按 GBK 读 UTF-8 文件,再 `Set-Content -Encoding utf8` 写回去就是
二次转码、整篇变乱码(`AGENTS.md` 就这么被我毁过一次,靠 `git checkout -- AGENTS.md` 二次转码、整篇变乱码(`AGENTS.md` 就这么被我毁过一次,靠 `git checkout -- AGENTS.md`
救回来)。`Set-Content -Encoding ascii` 更糟,汉字直接变 `?` 救回来)。`Set-Content -Encoding ascii` 更糟,汉字直接变 `?`
(`examples/full_add_flat*.r8asm` 就这么坏过一次)。 (`examples/` 里那些带中文注释的 `.r8asm` 就这么坏过一次)。
* **构建要用 VS 自带的 cmake**:`build/` 是 `Visual Studio 18 2026` 生成器配的, * **构建要用 VS 自带的 cmake**:`build/` 是 `Visual Studio 18 2026` 生成器配的,
直接敲 PATH 上的 scoop cmake(4.4)会因为版本不一致重新配置,然后死在 直接敲 PATH 上的 scoop cmake(4.4)会因为版本不一致重新配置,然后死在
`No preprocessor test for "PellesC"`。用这个: `No preprocessor test for "PellesC"`。用这个:
@@ -446,11 +446,13 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL
## %synth 相关 ## %synth 相关
* 语法:`params N` + `mode tape|layers` + `metric chars|flat|blocks` + `layout ...`(可选) * 语法:`params N` + `mode tape|layers` + `layout ...`(可选,只在 `--synth-chars` 下有用)
+ 目标表 `stat ...` 或 `map ...`。`stat` 的项数必须是 `2^params`(3 参数要 8 项)。 + 目标表 `stat ...` 或 `map ...`。`stat` 的项数必须是 `2^params`(3 参数要 8 项)。
参数是输入位 `%1..%K`、输出位、剩余草稿位(草稿位必须结束为 0,语义见 `compress_stat_space`)。 参数是输入位 `%1..%K`、输出位、剩余草稿位(草稿位必须结束为 0,语义见 `compress_stat_space`)。
* `metric flat`:不计 `.tp` 行走代价 → 结果与 cell 布局无关、缓存可跨调用点复用。 * **代价口径是隐式的**(源码里没有 `metric` 行了,写了会报错):默认 flat —— 不计 `.tp`
`metric chars` 才是按实际布局最优。库里的块统一用 `flat`。 行走、结果与 cell 布局无关、缓存可跨调用点复用;`r8forge --synth-chars <file>` 才对这次
汇编里的**所有**块按真实布局算(`r8asm::set_synth_metric(0)`,CLI 在 `tests/asm_test.cc`)。
full_add 上两个口径分别是 95 / 90 字符。`metric blocks` 已删(块数不是纸带长度的好代理)。
* 缓存:`.r8forge_cache/`(**已入库**,克隆下来直接命中)。键 = 模型版本 + params + mode + * 缓存:`.r8forge_cache/`(**已入库**,克隆下来直接命中)。键 = 模型版本 + params + mode +
metric + **解析后的整张目标表**(+ layout,仅 chars)。改指令模型时要 bump metric + **解析后的整张目标表**(+ layout,仅 chars)。改指令模型时要 bump
`kModelVersion`(`src/synth.cc`)。 `kModelVersion`(`src/synth.cc`)。
@@ -481,7 +483,7 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL
| `bit_raw_add`(6 cell 全加器切片) | 保留 A、B;字节级 `raw_add` 2199 字符,65536 组合全对 | | `bit_raw_add`(6 cell 全加器切片) | 保留 A、B;字节级 `raw_add` 2199 字符,65536 组合全对 |
| `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 870 / 782 / 628 / 1619 字符,各 65536(inc 512)全对 | | `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 870 / 782 / 628 / 1619 字符,各 65536(inc 512)全对 |
| `asm/fibonacci.r8asm` | 11959 字符,跑出 13 项(`… 89 144 233`),比 8 位时代多 3 项 | | `asm/fibonacci.r8asm` | 11959 字符,跑出 13 项(`… 89 144 233`),比 8 位时代多 3 项 |
| `examples/` 里的例程 | **每组宏一份**,9 个程序(§10.3 的表)+ 6 个 `%synth` 例子。每个都是能跑的程序,头部 `; out ...` 是它打印什么;`r8forge_examples_test` 全部跑一遍逐项核对(缺那行也算失败),顺带打印纸带长度和步数 | | `examples/` 里的例程 | **每组宏一份**,9 个程序(§10.3 的表)+ 4 个 `%synth` 例子。每个都是能跑的程序,头部 `; out ...` 是它打印什么;`r8forge_examples_test` 全部跑一遍逐项核对(缺那行也算失败),顺带打印纸带长度和步数 |
## 已经确认走不通的(别重试) ## 已经确认走不通的(别重试)
+36 -41
View File
@@ -30,25 +30,26 @@ output Co
``` ```
`%synth` 会跑两遍:先用层数搜索拿到一个解(7 秒),再拿它的代价当上界跑纸带长度搜索 `%synth` 会跑两遍:先用层数搜索拿到一个解(7 秒),再拿它的代价当上界跑纸带长度搜索
(Dijkstra)。第二遍是精确的,弹出目标即停,所以 full_add 大约 12 分钟 —— 汇编时间换最短纸带。 (Dijkstra),第二遍弹出目标即停。**默认按"布局无关"口径搜**(见下一节),所以库里那些块
的结果早就缓存在 `.r8forge_cache/` 里;想按真实 `.tp` 行走重新搜,加 `--synth-chars`。
`layout` 写的是真实地址时,搜索按真实 `.tp` 距离算代价;不写就假设参数 cell 紧邻。 `layout` 只在 `--synth-chars` 下有用:写真实地址就按真实 `.tp` 距离算,不写就假设参数
cell 紧邻。
汇编: 汇编:
``` ```
$ r8forge examples/full_add.r8asm $ r8forge examples/full_add.r8asm # 默认 flat:95 字符
r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.7s $ r8forge --synth-chars examples/full_add.r8asm # 按这份布局最优:90 字符
r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596 states, 1280s
> [-]-... (90 字符纸带)
``` ```
第一行是层数搜索(7.7 秒),第二行是按纸带长度的精确搜索(约 21 分钟,4.15 亿状态,峰值约 两个结果都已在 `.r8forge_cache/` 里,所以这两条现在都是秒出;第一次搜的时候
10 GB),最后发射的是第二行的解。`tests/synth_test.cc` 把纸带丢进一台小 ROT-8 虚拟机, `--synth-chars` 那版是 4.15 亿状态、约 21 分钟、峰值约 10 GB。
8 种组合全对:
`tests/synth_test.cc` 把纸带丢进一台小 ROT-8 虚拟机,8 种组合全对:
``` ```
tape: 90 characters tape: 95 characters
000 -> 00 expect 00 ok 000 -> 00 expect 00 ok
... ...
111 -> 11 expect 11 ok 111 -> 11 expect 11 ok
@@ -58,34 +59,25 @@ all eight combinations ok
`map` 的项数决定输入个数:2^K 项就是 `%1..%K` 为输入,值的第 j 位落到 `%(K+j+1)`,剩下 `map` 的项数决定输入个数:2^K 项就是 `%1..%K` 为输入,值的第 j 位落到 `%(K+j+1)`,剩下
的参数是必须结束为 0 的草稿。想要完全显式(或输入不在最前面)就改用 `stat`,那是整张状态表。 的参数是必须结束为 0 的草稿。想要完全显式(或输入不在最前面)就改用 `stat`,那是整张状态表。
## 三种代价口径 ## 代价口径:默认 flat,`--synth-chars` 切到真实布局
标准库现在统一用 metric flat(步长无关),结果全部预先生成并提交在 .r8forge_cache/ 里, `%synth` 里**没有** `metric` 了 —— 这是整次汇编的属性,不是某个块的属性:
所以第一次构建不用等搜索。
| 口径 | 怎么开 | 计什么 | 依赖 cell 布局? | 缓存能否跨调用点复用 |
|---|---|---|---|---|
| flat(默认) | 什么都不用加 | `biz` / `flb` / `rnz` 和 op 本身 | 否 | **能** |
| chars | `r8forge --synth-chars <file>` | 真实纸带字符数,含 `.tp` 行走 | 是(`layout`,不写按相邻) | 只有同一份布局 |
%synth 里用 metric 选优化目标: 默认 flat 的理由:库里的块会被几十个调用点展开,布局到处不同,只有"步长无关"的结果能
一份缓存复用。代价是纸带比按实际布局最优的长一点 —— full_add 上 flat 是 95 字符,
`--synth-chars` 是 90 字符。
| metric | 优化什么 | 依赖 cell 布局? | 缓存能否跨调用点复用 | 两个口径的缓存分开存(键里带 metric),所以第一次跑 `--synth-chars` 会真的去搜。库里的块
|---|---|---|---| 全是 flat,仓库里已经带好了缓存,所以 `cmake --build` 之后第一次汇编不用等。
| chars(默认) | 真实纸带字符数,含 .tp 行走 | 是(可用 layout 指定) | 同一个布局才能 |
| lat | 字符数,但**不计 .tp 行走** | 否 | **能** |
| locks | 块数 | 否 | 能 |
lat 就是"步长无关模式":它只数 iz/ lb/␍nz 和 op 本身,不看 cell 之间隔多远, 早先那个 `metric blocks`(只按块数)**删掉了**:块数不是纸带长度的好代理 —— 层数最少不等
所以同一张真值表在任何布局下都用同一份结果、同一份缓存。代价是纸带会比按实际布局最优的 于纸带最短,块数最少也不是。
长一点 —— full_add 上 chars 是 90 字符, lat 是 95 字符。
`
%synth fulladd_flat
params 6
map 0 1 1 2 1 2 2 3
metric flat
%endsynth
`
同一个 metric flat 的块配上 layout 0 2 4 6 8 10 再汇编,会直接命中同一份缓存并且
发射出**完全相同**的纸带(已验证)。
## 从零开始 ## 从零开始
@@ -142,12 +134,13 @@ build/Release/r8forge adder.r8asm > adder.rot8
stderr 上会告诉你它查到了什么,stdout 就是纸带: stderr 上会告诉你它查到了什么,stdout 就是纸带:
``` ```
r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.7s r8asm: %synth fulladd: cached, 5 blocks
r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596 states, 1280s
``` ```
`mode layers` 只跑第一行(7.7 秒),`mode tape`(默认)会接着跑第二行。结果按真值表缓存在 第一次搜(缓存里没有这张表 + 这个口径)会先打一行层数搜索,再打一行纸带搜索;之后再编
`.r8forge_cache/` 里,所以同一张表只贵一次;要重算就删掉那个目录。 同一张表就直接命中缓存。`mode layers` 只跑到层数为止,`mode tape`(默认)才是纸带长度
搜索。结果按真值表 + 口径缓存在 `.r8forge_cache/` 里,所以同一张表只贵一次;要重算就删掉
那个目录。
## 标准库现在也是合成的 ## 标准库现在也是合成的
@@ -362,7 +355,7 @@ build/Release/r8forge_tape_run t.rot8 # 打印 out 和步数
| `mul_demo.r8asm` | 82×141、2047×2047 的 `(PH:PL)` | | `mul_demo.r8asm` | 82×141、2047×2047 的 `(PH:PL)` |
| `divmod_demo.r8asm` | 2047/10、2047/1(旧版会 HANG) | | `divmod_demo.r8asm` | 2047/10、2047/1(旧版会 HANG) |
| `digits_demo.r8asm` | 1234 和 7 的 4 位 ASCII | | `digits_demo.r8asm` | 1234 和 7 的 4 位 ASCII |
| `full_add*.r8asm` | `%synth` 的六种写法(`map` / `stat` / `flat` / `layout` / `layers` / 手写) | | `full_add*.r8asm` | `%synth` 的四种写法(`map` / `stat` / `layers` / 手写);`--synth-chars` 那版见上一节 |
`r8forge_examples_test` 会把它们全跑一遍,实跑的 `out` 和头部那行逐项比 —— 例程不会悄悄腐掉。 `r8forge_examples_test` 会把它们全跑一遍,实跑的 `out` 和头部那行逐项比 —— 例程不会悄悄腐掉。
宏的契约(吃什么、留什么、几个 cell、纸带多长)看 `SPEC.md`。 宏的契约(吃什么、留什么、几个 cell、纸带多长)看 `SPEC.md`。
@@ -404,13 +397,14 @@ full_add(`r8veritas<6>`):**5 层,2038 万状态,7.7 秒,穷尽搜索
## 按纸带长度搜(`solve_cost`) ## 按纸带长度搜(`solve_cost`)
**结论先说**:full_add 上 5 层那版是 95 字符,代价搜索找到的 6 块版是 **90 字符**。两个都在 **结论先说**:full_add 上 5 层那版是 95 字符,按纸带长度搜出来的 6 块版是 **90 字符**
(后者就是 `--synth-chars` 的结果;缓存里已经有,所以不用再跑十几分钟)。两个都在
`examples/` 里,都跑过那台 ROT-8 虚拟机,8 种输入组合全对: `examples/` 里,都跑过那台 ROT-8 虚拟机,8 种输入组合全对:
| 版本 | 层数 | 模型代价 | 真实纸带 | | 版本 | 层数 | 模型代价 | 真实纸带 |
|---|---|---|---| |---|---|---|---|
| 层数最优(`full_add.r8asm`) | 5 | 82 | 95 字符 | | 层数最优(`full_add_layers.r8asm` / `full_add_stat.r8asm`) | 5 | 82 | 95 字符 |
| 代价最优(`full_add_cost.r8asm`) | 6 | 75 | **90 字符** | | 代价最优(`full_add_cost.r8asm`;`--synth-chars` 搜出来的是同一份) | 6 | 75 | **90 字符** |
代价最优解: 代价最优解:
@@ -469,7 +463,8 @@ cost(layer) = 2 # biz + rnz
``` ```
`|Δ|` 来自**调用点的 cell 布局**:同一份 5 层程序,cell 紧邻是 87 字符,隔两格是 136 字符。 `|Δ|` 来自**调用点的 cell 布局**:同一份 5 层程序,cell 紧邻是 87 字符,隔两格是 136 字符。
这是「程序 × 布局」的属性,只有看得见调用点的那一方算得出来 —— 也就是汇编期。 这是「程序 × 布局」的属性,只有看得见调用点的那一方算得出来 —— 也就是汇编期。所以默认
不把它算进代价(flat);`--synth-chars` 才是把这部分算进去的那一版。
## 坑 ## 坑
+25 -7
View File
@@ -96,7 +96,6 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列
%synth <名字> %synth <名字>
params <N> ; 1..8 params <N> ; 1..8
mode tape|layers ; 默认 tape mode tape|layers ; 默认 tape
metric chars|flat|blocks ; 默认 chars
layout <addr> ... ; 可选,按 %1..%N 顺序给 cell 地址 layout <addr> ... ; 可选,按 %1..%N 顺序给 cell 地址
map <2^K 项> ; 或者 stat <2^params 项> map <2^K 项> ; 或者 stat <2^params 项>
%endsynth %endsynth
@@ -105,8 +104,10 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列
- `stat` = **整张状态表**:每项是该输入组合下所有参数的取值(按 `%1..%N` 的位拼成一个数)。 - `stat` = **整张状态表**:每项是该输入组合下所有参数的取值(按 `%1..%N` 的位拼成一个数)。
- `map` = 只写输出位:项数 `2^K`(必须是 2 的幂)就表示 `%1..%K` 是输入,值里 bit j 落到 - `map` = 只写输出位:项数 `2^K`(必须是 2 的幂)就表示 `%1..%K` 是输入,值里 bit j 落到
`%(K+j+1)`,剩下的参数是**必须结束为 0 的草稿位**。 `%(K+j+1)`,剩下的参数是**必须结束为 0 的草稿位**。
- `layout` 只影响 `metric chars`。**给错个数不会报错**:数量不等于 `params` 时静默退回 - **代价口径不在源码里选**(早先的 `metric` 行已经删掉):默认按"布局无关"(flat)搜,
`0..N-1`(想按真实布局算就得数对,`metric flat` 下本来也无所谓布局)。 `r8forge --synth-chars <file>` 才对这次汇编里的**所有**块按真实 `.tp` 行走搜。见 §3.7。
- `layout` 只在 `--synth-chars` 下有用。**给错个数不会报错**:数量不等于 `params` 时静默
退回 `0..N-1`。
- 定义完就是一个普通宏:`<名字> a1 a2 ... aN`。 - 定义完就是一个普通宏:`<名字> a1 a2 ... aN`。
### 3.2 语义 ### 3.2 语义
@@ -119,8 +120,6 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列
高位不追踪。 高位不追踪。
- `mode layers`:只求层数最少(快)。`mode tape`:接着按纸带长度做精确 Dijkstra 搜索, - `mode layers`:只求层数最少(快)。`mode tape`:接着按纸带长度做精确 Dijkstra 搜索,
用层数解的代价做上界。 用层数解的代价做上界。
- `metric`:`chars`(默认,按 layout 算真实字符数)、`flat`(不计 `.tp` 行走,结果与布局
无关、缓存可跨调用点复用;**库里的块统一用 flat**)、`blocks`(只数块)。
- 生成的代码走正常宏展开:写成临时文件 `<名字>.synth.r8asm`(系统临时目录,写不进去就退到 - 生成的代码走正常宏展开:写成临时文件 `<名字>.synth.r8asm`(系统临时目录,写不进去就退到
当前目录并提示),再 `import_file`,然后删掉。因此生成物不依赖 `lib.r8asm` 当前目录并提示),再 `import_file`,然后删掉。因此生成物不依赖 `lib.r8asm`
(`set_1` 展开成 `biz/flb/rnz` + `flb`)。 (`set_1` 展开成 `biz/flb/rnz` + `flb`)。
@@ -182,6 +181,25 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列
要省掉那 3 个字符,只能让搜索器不压缩这些位(搜索空间 ×2^3、块还会更长),不划算。 要省掉那 3 个字符,只能让搜索器不压缩这些位(搜索空间 ×2^3、块还会更长),不划算。
### 3.7 代价口径:默认 flat,`--synth-chars` 切到真实布局
`%synth` 里**没有** `metric` 行(早先的语法有,已删):这是**整次汇编**的属性,不是某个块:
| 口径 | 开启方式 | 计什么 | 依赖布局? | 缓存可跨调用点复用 |
|---|---|---|---|---|
| flat(默认,metric=1) | 什么都不加 | `biz`/`flb`/`rnz` 和 op 本身 | 否 | **能** |
| chars(metric=0) | `r8forge --synth-chars <file>` | 真实字符数,含 `.tp` 行走 | 是(`layout`,缺省按相邻) | 只有同一份布局 |
默认 flat 的理由:库里的块会被几十个调用点展开,布局各不相同,只有"步长无关"的结果能一份
缓存复用;对整条纸带来说它通常只长几个百分点(full_add:flat 95 字符、chars 90 字符)。
两个口径的缓存分开存(键里带 metric),所以第一次跑 `--synth-chars` 会真的去搜。
**`blocks` 删掉了**:块数不是纸带长度的好代理(层数最少 ≠ 纸带最短,块数最少也不是),
而且它只是 `mode layers` 的一个变体。
实现:`r8asm::set_synth_metric(int)`(`include/r8forge/r8asm.hh`),在 `import_file` 之前
调用;CLI 在 `tests/asm_test.cc`(`r8forge`)里解析 `--synth-chars` / `--synth-flat`。
其它入口(`r8forge_derive`、几个测试)不传就一律 flat。
--- ---
@@ -659,7 +677,7 @@ r8forge_tape_run t.rot8 # 打印 out 和步数
| `mul_demo.r8asm` | §9 | 82×141、2047×2047 的 `(PH:PL)` | | `mul_demo.r8asm` | §9 | 82×141、2047×2047 的 `(PH:PL)` |
| `divmod_demo.r8asm` | §7.6 | 2047/10、2047/1(旧版第二个会 HANG),顺带证明 N、D 保留、草稿可以反复用 | | `divmod_demo.r8asm` | §7.6 | 2047/10、2047/1(旧版第二个会 HANG),顺带证明 N、D 保留、草稿可以反复用 |
| `digits_demo.r8asm` | §7.7 | 1234 和 7 的 4 位 ASCII | | `digits_demo.r8asm` | §7.7 | 1234 和 7 的 4 位 ASCII |
| `full_add*.r8asm` | §3 `%synth` | 六种写法(`map` / `stat` / `flat` / `layout` / `layers` / 手写);输入全 0,所以 `out 0 0` | | `full_add*.r8asm` | §3 `%synth` | 四种写法(`map` / `stat` / `layers` / 手写);输入全 0,所以 `out 0 0` |
`r8forge_examples_test` 会把 `examples/` 里所有 `.r8asm` 跑一遍,实跑的 `out` 和头部那行 `r8forge_examples_test` 会把 `examples/` 里所有 `.r8asm` 跑一遍,实跑的 `out` 和头部那行
逐项比(顺带打印纸带长度和步数)。它保证例程既**汇编得出来**、又**还是那样输出**; 逐项比(顺带打印纸带长度和步数)。它保证例程既**汇编得出来**、又**还是那样输出**;
@@ -673,7 +691,7 @@ r8forge_tape_run t.rot8 # 打印 out 和步数
|---|---|---| |---|---|---|
| `r8forge_byte_op_test` | 20 个测试项 = 19 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§7.6、§7.7、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举、`clb_divmod` 8 个除数 × 全部 2048 个被除数 = 16384 组全穷举、`clb_digits` 全部 2048 个值全穷举),任一不符即退出码非 0 | | `r8forge_byte_op_test` | 20 个测试项 = 19 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§7.6、§7.7、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举、`clb_divmod` 8 个除数 × 全部 2048 个被除数 = 16384 组全穷举、`clb_digits` 全部 2048 个值全穷举),任一不符即退出码非 0 |
| `r8forge_synth_test examples/full_add.r8asm` | `%synth` 端到端:汇编 + 跑 8 种输入 | `all eight combinations ok` | | `r8forge_synth_test examples/full_add.r8asm` | `%synth` 端到端:汇编 + 跑 8 种输入 | `all eight combinations ok` |
| `r8forge_examples_test` | `examples/` 里全部 15 个程序(9 组宏例程 + 6 个 `%synth` 例子,见 §10.3) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败 | | `r8forge_examples_test` | `examples/` 里全部 13 个程序(9 组宏例程 + 4 个 `%synth` 例子,见 §10.3) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败 |
| `r8forge verify_clb_bit_add_nc.r8asm` | 4 cell 加法切片(进位进→出) | 单点:`0+0+Ci` | | `r8forge verify_clb_bit_add_nc.r8asm` | 4 cell 加法切片(进位进→出) | 单点:`0+0+Ci` |
| `r8forge verify_clb_bit_inc_nc.r8asm` | 3 cell 自增切片 | 单点 | | `r8forge verify_clb_bit_inc_nc.r8asm` | 3 cell 自增切片 | 单点 |
| `r8forge_derive verify_loop.r8asm -1 0=N` | 动态循环:`N=0..11` 时循环体跑 `N` 次(`A = N`、`C = 0`) | 逐值 `out` | | `r8forge_derive verify_loop.r8asm -1 0=N` | 动态循环:`N=0..11` 时循环体跑 `N` 次(`A = N`、`C = 0`) | 逐值 `out` |
-1
View File
@@ -14,7 +14,6 @@
%synth bit_raw_add_synth %synth bit_raw_add_synth
params 6 params 6
stat 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 stat 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31
metric flat
mode tape mode tape
%endsynth %endsynth
-1
View File
@@ -28,7 +28,6 @@
%synth clb_bit_add_nc_synth %synth clb_bit_add_nc_synth
params 4 params 4
stat 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12 stat 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12
metric flat
mode tape mode tape
%endsynth %endsynth
-1
View File
@@ -61,7 +61,6 @@ rnz
%synth bit_raw_and_synth %synth bit_raw_and_synth
params 4 params 4
stat 0 1 2 7 0 1 2 7 0 1 2 7 0 1 2 7 stat 0 1 2 7 0 1 2 7 0 1 2 7 0 1 2 7
metric flat
mode tape mode tape
%endsynth %endsynth
-1
View File
@@ -11,7 +11,6 @@
%synth bit_raw_inc_synth %synth bit_raw_inc_synth
params 5 params 5
stat 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 stat 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11 0 5 6 11
metric flat
mode tape mode tape
%endsynth %endsynth
-1
View File
@@ -24,7 +24,6 @@
%synth clb_bit_inc_nc_synth %synth clb_bit_inc_nc_synth
params 3 params 3
stat 0 4 4 2 0 4 4 2 stat 0 4 4 2 0 4 4 2
metric flat
mode tape mode tape
%endsynth %endsynth
-2
View File
@@ -9,7 +9,6 @@
%synth clb_bit_or_synth %synth clb_bit_or_synth
params 3 params 3
stat 0 4 4 4 0 4 4 4 stat 0 4 4 4 0 4 4 4
metric flat
mode tape mode tape
%endsynth %endsynth
@@ -47,7 +46,6 @@
%synth bit_raw_or_synth %synth bit_raw_or_synth
params 4 params 4
stat 0 5 6 7 0 5 6 7 0 5 6 7 0 5 6 7 stat 0 5 6 7 0 5 6 7 0 5 6 7 0 5 6 7
metric flat
mode tape mode tape
%endsynth %endsynth
-2
View File
@@ -9,7 +9,6 @@
%synth clb_bit_xor_synth %synth clb_bit_xor_synth
params 3 params 3
stat 0 4 4 0 0 4 4 0 stat 0 4 4 0 0 4 4 0
metric flat
mode tape mode tape
%endsynth %endsynth
@@ -47,7 +46,6 @@
%synth bit_raw_xor_synth %synth bit_raw_xor_synth
params 4 params 4
stat 0 5 6 3 0 5 6 3 0 5 6 3 0 5 6 3 stat 0 5 6 3 0 5 6 3 0 5 6 3 0 5 6 3
metric flat
mode tape mode tape
%endsynth %endsynth
-22
View File
@@ -1,22 +0,0 @@
%include "asm/lib.r8asm"
; 步长无关:metric flat 不计 .tp 的行走代价,结果与 cell 布局无关,
; 所以一份缓存能用在任何调用点(代价是纸带比按实际布局最优的长一点)。
; 输入全 0,所以 S=0、Co=0(组合全查靠 r8forge_synth_test)。
; out 0 0
%synth fulladd_flat
params 6
map 0 1 1 2 1 2 2 3
metric flat
%endsynth
.var A 0
.var B 1
.var Ci 2
.var S 3
.var Co 4
.var T 5
fulladd_flat A B Ci S Co T
output S
output Co
-24
View File
@@ -1,24 +0,0 @@
%include "asm/lib.r8asm"
; 同一个 metric flat 块,只是额外写了 layout 0 2 4 6 8 10。
; flat 不看布局,所以它命中 full_add_flat.r8asm 的同一份缓存,发射出完全相同的纸带
; —— 这正是"步长无关、可跨调用点复用"的证据。
; 输入全 0,所以 S=0、Co=0。
; out 0 0
%synth fulladd_flat
params 6
map 0 1 1 2 1 2 2 3
layout 0 2 4 6 8 10
metric flat
%endsynth
.var A 0
.var B 1
.var Ci 2
.var S 3
.var Co 4
.var T 5
fulladd_flat A B Ci S Co T
output S
output Co
+13 -3
View File
@@ -17,7 +17,9 @@
// {sacrifice, mask, or_mode} triples. Either `map` (2^K output values, the // {sacrifice, mask, or_mode} triples. Either `map` (2^K output values, the
// inputs implied to be %1..%K and the outputs the ones after them) or `stat` // inputs implied to be %1..%K and the outputs the ones after them) or `stat`
// (the whole 2^params state table) says what to compute. `layout` is the cell // (the whole 2^params state table) says what to compute. `layout` is the cell
// address of each parameter (empty means adjacent, 0..n-1). // address of each parameter (empty means adjacent, 0..n-1). `metric` picks the
// tape cost model: 1 = layout free (the .tp walk is not counted, one cache
// entry serves every caller), 0 = real characters for this layout.
std::vector<std::array<int, 3>> std::vector<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in, r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map, const std::vector<int> &out, const std::vector<int> &map,
@@ -109,6 +111,12 @@ private:
// I know template class is the most elegant solution of cell_size // I know template class is the most elegant solution of cell_size
// but I can't be bother to add that. // but I can't be bother to add that.
// How %synth charges a block: 1 = layout free (default), 0 = real tape
// characters for the layout in the source. A library block is called from
// many places, so the default ignores the layout and one cache entry serves
// every call site; `r8forge --synth-chars` flips it for the whole run.
int synth_metric = 1;
// The assembler has no exceptions: anything that cannot be assembled // The assembler has no exceptions: anything that cannot be assembled
// sets this flag and says why on stderr. // sets this flag and says why on stderr.
bool error = false; bool error = false;
@@ -130,8 +138,6 @@ private:
std::vector<int> stat; // or the whole table: 2^params values std::vector<int> stat; // or the whole table: 2^params values
std::vector<int> layout; // optional: cell address of each parameter std::vector<int> layout; // optional: cell address of each parameter
int mode = 0; // 0 = shortest tape, 1 = fewest layers only int mode = 0; // 0 = shortest tape, 1 = fewest layers only
int metric = 0; // 0 = tape chars (needs the layout), 1 = chars without
// the .tp walk (any layout), 2 = blocks
}; };
private: private:
@@ -233,6 +239,10 @@ private:
public: public:
std::string export_rot8(); std::string export_rot8();
bool import_file(const std::string &filename, size_t cell_size); bool import_file(const std::string &filename, size_t cell_size);
// %synth cost model for the whole run: 1 (default) = layout free, 0 = real
// tape characters for the layout written in the source. Set it before
// import_file; `r8forge --synth-chars` is the command line spelling.
void set_synth_metric(int metric) { main_tree.synth_metric = metric; }
// True once anything (a missing file, an unknown name, ...) has failed. // True once anything (a missing file, an unknown name, ...) has failed.
bool failed() const { return main_tree.error; } bool failed() const { return main_tree.error; }
}; };
+7 -11
View File
@@ -533,16 +533,12 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
"' is gone: map already says which parameter is " "' is gone: map already says which parameter is "
"which, use stat for the raw table"); "which, use stat for the raw table");
} else if (kw == "metric") { } else if (kw == "metric") {
const std::string &m = it->tokens[1].value; // it is a property of the run, not of the block: the assembler
if (m == "chars") // optimises for a layout free block by default, and --synth-chars
spec.metric = 0; // switches every block in the file to the real layout
else if (m == "flat") main_tree.fail("'metric' is gone: blocks are layout free by "
spec.metric = 1; "default, run r8forge --synth-chars to charge the "
else if (m == "blocks") "real .tp walk instead");
spec.metric = 2;
else
main_tree.fail("unknown metric '" + m +
"' (chars, flat or blocks)");
} else if (kw == "mode") { } else if (kw == "mode") {
const std::string &m = it->tokens[1].value; const std::string &m = it->tokens[1].value;
if (m == "layers") if (m == "layers")
@@ -590,7 +586,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
} }
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out, auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
spec.map, spec.stat, name, spec.layout, spec.map, spec.stat, name, spec.layout,
spec.mode, spec.metric); spec.mode, main_tree.synth_metric);
if (prog.empty()) { if (prog.empty()) {
main_tree.fail("%synth '" + name + "': nothing emitted"); main_tree.fail("%synth '" + name + "': nothing emitted");
} else { } else {
+1 -1
View File
@@ -132,7 +132,7 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
<< " layers" << (layers.proven_optimal ? " (shortest)" : "") << " layers" << (layers.proven_optimal ? " (shortest)" : "")
<< ", " << layers.states << " states, " << layers.seconds << "s\n"; << ", " << layers.states << " states, " << layers.seconds << "s\n";
if (mode == 1 || metric == 2) { // blocks only: skip the tape search if (mode == 1) { // fewest layers only: skip the tape search
for (const auto &i : layers.program) for (const auto &i : layers.program)
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)}); out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
write_cache(out_ins); write_cache(out_ins);
+45 -4
View File
@@ -1,14 +1,55 @@
// The assembler CLI.
//
// r8forge [--synth-chars] <file.r8asm>
//
// %synth charges a block for a layout free tape by default: the block is
// copied to many call sites, so the .tp walk is not part of the search and one
// cache entry serves every layout. --synth-chars charges the real characters
// for the layout written in the source instead (much slower, and the result is
// only optimal for that layout). It is a property of the run, not of a block,
// which is why there is no `metric` line inside %synth any more.
#include "r8forge/r8asm.hh" #include "r8forge/r8asm.hh"
#include <iostream> #include <iostream>
#include <string>
namespace {
void usage() {
std::cerr << "usage: r8forge [--synth-chars] <file.r8asm>\n";
}
} // namespace
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 2) { const char *file = nullptr;
std::cerr << "usage: r8asm_test <file.r8asm>" << std::endl; int metric = 1; // layout free
for (int i = 1; i < argc; i++) {
const std::string arg = argv[i];
if (arg == "--synth-chars") {
metric = 0;
} else if (arg == "--synth-flat") {
metric = 1;
} else if (arg == "-h" || arg == "--help") {
usage();
return (0);
} else if (arg.size() > 1 && arg[0] == '-') {
std::cerr << "unknown option '" << arg << "'\n";
usage();
return (2);
} else if (file == nullptr) {
file = argv[i];
} else {
usage();
return (2);
}
}
if (file == nullptr) {
usage();
return (2); return (2);
} }
r8asm a; r8asm a;
if (!a.import_file(argv[1], kDefaultCellSize)) a.set_synth_metric(metric);
if (!a.import_file(file, kDefaultCellSize))
return (1); return (1);
std::string tape = a.export_rot8(); std::string tape = a.export_rot8();
if (a.failed()) if (a.failed())