Files
R8Forge/README.md
T
dsh 43390efe8a Byte level add, one bit per cell: 65536 combinations ok
The rotation based byte layer never ran.  Measured with r8forge_derive -2:

    raw_add   A=0,B=0 / 1+1 / 3+5 / 1+0   out empty (step limit hit)
    raw_add   A=127,B=130                 out 0 0 0 0 0, everything destroyed
    raw_and   0xF0 and 0xCC, 1 and 1, 0 and 5   out empty
    copy      A=5                         out empty (A=0 returns fine)

Not "only operand preservation is missing": set_0 (= [-]) cannot clear a cell
that holds more than one bit, and biz only answers "is this whole cell zero",
because ror does not change zero-ness.  So the bits of a packed byte are
unreadable, and the whole ror + set_0 layer has no future.  The docs now say
that instead of blaming operand preservation.

What replaces it is one bit per cell:

  asm/byte_add.r8asm   8 bit adder.  Bit i owns four adjacent cells at base+4i:
                       A, B, carry (in -> out), scratch.  The synthesised four
                       cell slice bit_raw_add_nc does the bit, clb_bit_mov moves
                       the carry into the next quartet.  A doubles as the
                       accumulator, so calling byte_add again continues the sum.
  asm/pack_byte.r8asm  eight flags -> one byte, the only direction that works.
                       bit_raw_flip_if consumes a flag into flipping bit 0 of an
                       accumulator that may already hold bits (no set_0 there).
  examples/add8.r8asm  a+b, 1308 characters.  A shared carry cell layout needs
                       only 25 cells but was measured at 2517: paying .tp travel
                       eight times beats saving seven carry cells.
  examples/add8_inc.r8asm   add 1 three times, proving the block composes.
  tests/byte_add_test.cc    r8forge_byte_add_test: runs the tapes on the ROT-8
                       machine over every input pair, 0.3 s.

Also: AGENTS.md gains the measurement table, the third instruction set
constraint (packed bytes are unreadable), the new files, and the note that
build/ has to be rebuilt with the VS bundled cmake -- scoop's 4.4 reconfigures
it and dies with "No preprocessor test for PellesC".
2026-11-06 23:40:14 +08:00

391 lines
14 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# R8Forge
R8ASM 汇编器 + R8Veritas 搜索器,合成一个仓库。
搜索器不再是独立程序:`%synth` 是**汇编期指令**,描述一张真值表,汇编器现场搜出最短的块
序列直接拼进纸带。代价模型和发射器从此是同一份代码,不用再维护两套「这个块要花多少条
指令」的估算。
## 用法
```asm
%include "asm/lib.r8asm"
%synth fulladd
params 6
map 0 1 1 2 1 2 2 3 ; 8 个输入组合各自的输出位取值,%1..%3 是输入
layout 0 1 2 3 4 5 ; 可选:各参数所在 cell 的地址(默认 0..n-1)
%endsynth
.var A 0
.var B 1
.var Ci 2
.var S 3
.var Co 4
.var T 5
fulladd A B Ci S Co T ; 这一行展开成汇编期合成的最短纸带
output S
output Co
```
`%synth` 会跑两遍:先用层数搜索拿到一个解(7 秒),再拿它的代价当上界跑纸带长度搜索
(Dijkstra)。第二遍是精确的,弹出目标即停,所以 full_add 大约 12 分钟 —— 汇编时间换最短纸带。
`layout` 写的是真实地址时,搜索按真实 `.tp` 距离算代价;不写就假设参数 cell 紧邻。
汇编:
```
$ r8forge examples/full_add.r8asm
r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.7s
r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596 states, 1280s
> [-]-... (90 字符纸带)
```
第一行是层数搜索(7.7 秒),第二行是按纸带长度的精确搜索(约 21 分钟,4.15 亿状态,峰值约
10 GB),最后发射的是第二行的解。`tests/synth_test.cc` 把纸带丢进一台小 ROT-8 虚拟机,
8 种组合全对:
```
tape: 90 characters
000 -> 00 expect 00 ok
...
111 -> 11 expect 11 ok
all eight combinations ok
```
`map` 的项数决定输入个数:2^K 项就是 `%1..%K` 为输入,值的第 j 位落到 `%(K+j+1)`,剩下
的参数是必须结束为 0 的草稿。想要完全显式(或输入不在最前面)就改用 `stat`,那是整张状态表。
## 三种代价口径
标准库现在统一用 metric flat(步长无关),结果全部预先生成并提交在 .r8forge_cache/ 里,
所以第一次构建不用等搜索。
%synth 里用 metric 选优化目标:
| metric | 优化什么 | 依赖 cell 布局? | 缓存能否跨调用点复用 |
|---|---|---|---|
| chars(默认) | 真实纸带字符数,含 .tp 行走 | 是(可用 layout 指定) | 同一个布局才能 |
| lat | 字符数,但**不计 .tp 行走** | 否 | **能** |
| locks | 块数 | 否 | 能 |
lat 就是"步长无关模式":它只数 iz/ lb/
nz 和 op 本身,不看 cell 之间隔多远,
所以同一张真值表在任何布局下都用同一份结果、同一份缓存。代价是纸带会比按实际布局最优的
长一点 —— 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 再汇编,会直接命中同一份缓存并且
发射出**完全相同**的纸带(已验证)。
## 从零开始
```sh
cmake -S . -B build -G "Visual Studio 18 2026" -A x64 # 或者用 Ninja / Makefile
cmake --build build --config Release
```
写一个 `adder.r8asm`:
```asm
%include "asm/lib.r8asm"
%synth fulladd
params 6
map 0 1 1 2 1 2 2 3 ; 8 个输入组合各自的输出位取值
mode tape ; tape = 最短纸带(默认),layers = 只求层数最少(快)
%endsynth
.var A 0
.var B 1
.var Ci 2
.var S 3
.var Co 4
.var T 5
fulladd A B Ci S Co T
output S
output Co
```
`map` 一项就说明了谁是谁:**项数是 2^K,就意味着 `%1..%K` 是输入**;值里的第 j 位
落到 `%(K+j+1)`;剩下的参数都是必须结束为 0 的草稿。所以不需要再声明 inputs/outputs。
要完全显式也可以,直接给整张状态表(每个输入组合下 6 个参数的取值),一个声明都不用:
```asm
%synth fulladd_stat
params 6
stat 0 1 2 3 4 5 6 ...
mode layers
%endsynth
```
`examples/full_add.r8asm` 是 `map` 版,`examples/full_add_stat.r8asm` 是 `stat` 版,
两者合成的纸带逐字节相同(95 字符)。
汇编:
```sh
build/Release/r8forge adder.r8asm > adder.rot8
```
stderr 上会告诉你它查到了什么,stdout 就是纸带:
```
r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.7s
r8asm: %synth fulladd: tape cost 75 in 6 blocks (layer answer was 82), 415041596 states, 1280s
```
`mode layers` 只跑第一行(7.7 秒),`mode tape`(默认)会接着跑第二行。结果按真值表缓存在
`.r8forge_cache/` 里,所以同一张表只贵一次;要重算就删掉那个目录。
## 标准库现在也是合成的
`asm/` 里的 8 个 bit 宏(`clb_bit_and`、`bit_raw_and`、`clb_bit_or`、`bit_raw_or`、
`clb_bit_xor`、`bit_raw_xor`、`bit_raw_inc`、`bit_raw_add`)全部换成了 `%synth` 块,宏名、
参数顺序、调用方式都没动,上层的 `%rep` 驱动照旧。
`stat` 那张表不是手写的,是 `r8forge_derive` 从**旧实现本身**跑出来的:
```sh
build/Release/r8forge_derive drv/bit_raw_add.r8asm 6
# stat 0 9 10 19 12 21 22 31 0 9 10 ...
```
它把 6 个参数摆到 cell 0..5,穷举 64 个输入组合各跑一遍旧纸带,读回 6 个 bit 的终值。
所以合成结果与旧实现**逐位一致**(连旧实现里"某个输入没被清掉"这类怪癖都保住了),只是更短。
要改语义,改那张表就行,不用碰代码。
### 放进自己的库里
`%synth` 块定义完就是一个普通宏,所以在库里写一次即可:
```asm
; asm/my_adder.r8asm
%synth bit_add
params 6
map 0 1 1 2 1 2 2 3
%endsynth
```
调用点和普通宏调用一样:`bit_add A B Ci S Co tmp` —— 替换掉现有实现就是改这一行。
### 三条要注意的
1. **输入永远是排在最前面的参数**(`%1..%K`,K 由 `map` 项数得出)。搜索器把真值表索引直接
当输入值,靠的就是这个顺序;输入不在最前面就用 `stat`。
2. `map` 一项里的 bit0 落在 `%(K+1)`、bit1 落在 `%(K+2)`……超出 `params` 会直接报错。
3. `stat` 必须给 `2^params` 项,每项是那个输入组合下所有参数的取值。
## 字节级运算:只能一位一 cell
```asm
%include "asm/byte_add.r8asm"
%include "asm/pack_byte.r8asm"
.var BITS 0
.var P 15
byte_add BITS ; A 的八位在 0,4,...,28;B 的八位在 1,5,...,29
pack_byte BITS 4 P ; 八位 -> 一个字节
output P
output 30 ; b7 的进位
```
`byte_add` 是 8 位加法器:第 i 位占 `BITS + 4i`(A)、`+4i+1`(B)、`+4i+2`(进位)、`+4i+3`(草稿)。
四个 cell 紧邻,所以切片内部来回跳跃只有 1~2 步;进位到下一块用 `clb_bit_mov` 搬一格。
A 同时是累加器 —— 算完 B 的八个 cell 归零,把下一个操作数摆进去再叫一次就接着加,
`examples/add8_inc.r8asm` 连加三次 1 就是这么写的。
**为什么不能"一个 cell 放一个字节再轮转"**:ROT-8 没有移位,只有 `ror`,而 `ror` 不改变
"这个 cell 是不是 0",所以 `biz` 对整字节永远只能回答"是不是 0";`flb` 只写最低位,不读。
一个带多个位的 cell,它的位在程序里根本读不出来 —— 再加上 `set_0` 对多字节 cell 不收敛。
库里原来那层 `raw_add`/`raw_and`/`raw_or`/`raw_xor`/`raw_inc`/`copy` 全踩在这上面,
实测跑不完(连 `raw_add` 的 `0+0` 都卡死,`127+130` 能跑完但输出全 0)。所以字节级
运算只能一位一 cell。
`r8forge_byte_add_test` 汇编 `examples/add8*.r8asm`,在 rot8 虚拟机里把每种输入都跑一遍,
0.3 秒:
```
add8: 1308 characters
all 65536 combinations ok
add8_inc: 3142 characters
all 256 combinations ok
byte level adders ok
```
`pack_byte` 是**单向**的:八个 flag 压成字节,压完 flag 全被吃掉;压出来的字节能 `out`,
但不能再参与运算。反过来把字节拆成八位 —— 做不到(同上)。
## 实现方式
`%synth` 在 `parse()` 里被读成一张真值表,随即调用搜索器;得到的块序列写成
```asm
%macro fulladd 6
.tp %1
biz
...
%endmacro
```
这样一个临时文件(`<名字>.synth.r8asm`),再 `import_file` 进来、删掉。于是合成出来的代码
走的是**正常宏展开**路径,不是手工拼 AST —— 少一套容易写错的代码。
生成的块体里 `set_1` 展开成 `biz/flb/rnz` + `flb`(即 `set_0` + 翻转),所以生成文件不依赖
`lib.r8asm`。
## 搜索器
- 状态 = 位平面真值表,只留影响答案的输入(加法器类问题通常 3 个,48 bit 一个 `uint64_t`)
- 前向 BFS 到 d−1 层,最后一层不展开:枚举目标的一步前驱反查表
- `proven_optimal` 为真表示长度已被穷尽证明
full_add(`r8veritas<6>`):**5 层,2038 万状态,7.7 秒,穷尽搜索下最短**。
比它更宽松的指令语义也没有 4 层解(都做过穷尽搜索):
| 放宽 | 指令数 | 搜索规模 | 4 层? |
|---|---|---|---|
| 每目的地独立 OR/XOR | 1,458 | 前向 3 层 173 万 + 反向 20,096 前驱 | 无 |
| 再加 `set_0` | 6,144 | 前向 3 层 956 万 + 反向 40,928 | 无 |
| 参数 6 → 7 → 8 个 | 2,040 | 穷尽 540 万 / 4410 万状态 | 无 |
| 条件位 ≠ 牺牲位 | 2,304 | 前向 3 层 317 万 + 反向 1,280 | 无 |
## 按纸带长度搜(`solve_cost`)
**结论先说**:full_add 上 5 层那版是 95 字符,代价搜索找到的 6 块版是 **90 字符**。两个都在
`examples/` 里,都跑过那台 ROT-8 虚拟机,8 种输入组合全对:
| 版本 | 层数 | 模型代价 | 真实纸带 |
|---|---|---|---|
| 层数最优(`full_add.r8asm`) | 5 | 82 | 95 字符 |
| 代价最优(`full_add_cost.r8asm`) | 6 | 75 | **90 字符** |
代价最优解:
```
sacrifice B, FLIP {S,T}
sacrifice A, FLIP {B}
sacrifice B, FLIP {A,Ci,S}
sacrifice Ci, FLIP {B,Co,T}
sacrifice S, SET1 {Ci,Co}
sacrifice T, FLIP {B,Ci,S,Co}
```
层数最优不等于纸带最优:多一层反而少 5 个字符。Dijkstra 在弹出代价 75 之前已经把 < 75 的
区域穷尽,所以 75 是模型内的最小值。
层数最少不等于纸带最短。`solve_cost` 用同一张状态图跑 Dijkstra,边权就是这个块汇编出来
多少字符:
```
w(block) = 3 (biz + flb + rnz) + 2 × span(cell 地址) + Σ op # flb=1, set_1=4
```
桶队列(Dial),按代价递增弹出,第一次弹出目标态就是最短纸带。给一个 `max_cost` 上界就变成
分支限界,超出上界的状态不入队。
full_add、6 个 cell 紧邻时,搜索推进到某个代价时已经攒下的状态数:
| 代价 | 状态数 |
|---|---|
| 20 | 1,086 |
| 40 | 128,528 |
| 50 | 999,448 |
| 58 | 80,000,000 |
| 62 | 140,000,000 |
| 66 | 220,000,000 |
| 70 | 280,000,000 |
每状态约 27 字节(8 key + 2 距离 + 4 父 + 2 指令 + 哈希表),到代价 70 约 7.5 GB,速度约
65 万状态/秒。所以这台机器上能跑到的上限大约就是「代价 75 左右 / 4.5 亿状态 / 12 GB」,
再往上就超出内存了 —— 这正是当初估的 13 GB 量级。
因为 Dijkstra 按代价递增弹出,**停在哪儿就证明了「比它更短的纸带不存在」**:跑到代价 d 就
停,等于给出「没有短于 d 字符的程序」这个下界。
注意:模型里的 `w` 不含「上一块的牺牲位跳到这一块」的那几步(`|s_i − s_{i−1}|`),因为那让
代价依赖历史、Dijkstra 就不能用了。发射器会把这部分加上再报真实长度,5 层程序是模型 82 /
实际 87。
## 为什么合在一起
代价 = 汇编出来多少条指令,而这只有发射器知道:
```
cost(layer) = 2 # biz + rnz
+ Σ (|Δ| 移动 + op) # flb=1, set_0=3, set_1=4
```
`|Δ|` 来自**调用点的 cell 布局**:同一份 5 层程序,cell 紧邻是 87 字符,隔两格是 136 字符。
这是「程序 × 布局」的属性,只有看得见调用点的那一方算得出来 —— 也就是汇编期。
## 坑
- ROT-8 的字符映射是 `"><+-[].,"`,也就是 **`-` 是 flb,`+` 是 ror**。写仿真器时按常识
猜会反(我第一次就反了,纸带看起来「死循环」)。
- `%synth` 的名字不能和 ROT-8 助记符重名(`out`、`flb`、`biz`…),否则词法先认成指令。
## 下一次要做的事
1. 搜索的代价从「1/层」换成真实指令数(Dijkstra + 每层目的地访问顺序的 TSP),这样嵌套条件
块(`A AND B` 门控)也能公平参与竞争。注意嵌套会降低层数但拉长纸带 —— 层数不是长度
的好代理。
2. 缓存:现在每次汇编都要重跑搜索(full_add 是 7.7 秒)。按真值表 + 布局落盘缓存。
3. `%synth` 只支持 8 位以内的参数(状态要塞进 64 位字里)。
## 给接手的 agent / 新会话
先读 [`AGENTS.md`](AGENTS.md)。那里有这个仓库实测过的指令集语义、`%synth` 用法、缓存机制,
以及一列**已经确认走不通**的方案(3 cell 加法切片、轮转配合成切片、字节级真值表交给搜索……)。
那些都是失败换来的结论,别重试。
## 构建
```powershell
cmake -S . -B build -G "Visual Studio 18 2026" -A x64
cmake --build build --config Release
build\Release\r8forge.exe asm\test.r8asm > test.rot8
build\Release\r8forge_synth_test.exe # 合成 + 跑虚拟机自检
build\Release\r8forge_byte_add_test.exe # 8 位加法器:65536 组合全对
```
`build/` 已经有生成器了,重编译时**别用 PATH 上的另一个 cmake**(版本不一致会重新配置,
然后死在 `No preprocessor test for "PellesC"`);用配这个目录的那个:
```powershell
& "C:\Program Files\Microsoft Visual Studio\18\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --build build --config Release
```
`CMakeLists.txt` 里没有 `set(CMAKE_CXX_FLAGS ...)`:那会把默认 flag 整个换掉,MSVC 下丢
`/EHsc`、还把 `-O3` 塞进 Debug 和 `/RTC1` 打架。
## 布局
```
include/r8forge/r8asm.hh 汇编器 + %synth 指令
include/r8forge/r8veritas.hh 搜索器
src/r8asm.cc 汇编器实现
src/synth.cc 真值表 -> 搜索
tests/asm_test.cc 汇编器自检(纸带不变)
tests/synth_test.cc 合成 + ROT-8 虚拟机自检
tests/byte_add_test.cc 8 位加法器穷举自检(65536 组合)
asm/ 标准库与既有实现
asm/byte_add.r8asm 8 位加法器(一位一 cell)
asm/pack_byte.r8asm 八个 flag -> 一个字节
examples/full_add.r8asm 汇编期合成全加器
examples/add8.r8asm a+b,结果压成一个字节省出来
```