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".
171 lines
9.1 KiB
Markdown
171 lines
9.1 KiB
Markdown
# 给接手的 agent 的话
|
||
|
||
这个文件是给下一个会话(或者下一个 agent)看的交接说明。内容都是这个仓库里**实测过**的结论,
|
||
不是推测;每条后面基本都跟着一次失败的尝试,别再踩。
|
||
|
||
## 工作方式(上一轮踩到的)
|
||
|
||
* **超过 ~30 秒的命令一律丢后台**(`run_in_background`),前台只做短轮询。
|
||
一次搜索可以跑 680 秒、缓存预热 885 秒,前台干等会撞上客户端和 API 的传输超时。
|
||
* **别让大段 stderr 进上下文**。PowerShell 遇到原生命令出错会吐一整套 `NativeCommandError`
|
||
堆栈;用 `2>$null` 收干净,判断结果靠 `$LASTEXITCODE` 和 stdout。
|
||
* 写含中文的文件用编辑工具(UTF-8),**不要用 `Set-Content -Encoding ascii`** ——
|
||
汉字会变成 `?`(`examples/full_add_flat*.r8asm` 就这么坏过一次)。
|
||
* **构建要用 VS 自带的 cmake**:`build/` 是 `Visual Studio 18 2026` 生成器配的,
|
||
直接敲 PATH 上的 scoop cmake(4.4)会因为版本不一致重新配置,然后死在
|
||
`No preprocessor test for "PellesC"`。用这个:
|
||
|
||
```sh
|
||
"C:/Program Files/Microsoft Visual Studio/18/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe" \
|
||
--build build --config Release
|
||
```
|
||
* 会话很长时传输容易失败,重要结论要落到 **commit message 和文件注释**里,别只留在对话里。
|
||
|
||
## 构建与自检
|
||
|
||
```sh
|
||
# MSVC Release
|
||
cmake -S . -B build -G "Visual Studio 18 2026" -A x64
|
||
cmake --build build --config Release
|
||
|
||
# clang-cl(scoop 的 llvm,比 MSVC 严格,能抓出模板推导问题)
|
||
clang-cl /nologo /std:c++20 /EHsc /O2 /I include src/r8asm.cc src/synth.cc tests/asm_test.cc /Fe:r8forge.exe
|
||
```
|
||
|
||
自检三件套:
|
||
|
||
```sh
|
||
r8forge asm/test.r8asm # 应 exit 0,1666 字符,7/7 命中缓存
|
||
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
|
||
r8forge verify_raw_add_nc.r8asm # 4 cell 加法切片驱动
|
||
```
|
||
|
||
跑纸带的第四件(**唯一一个真的执行纸带、按返回值判断对错的**):
|
||
|
||
```sh
|
||
r8forge_byte_add_test # add8: 65536/65536,add8_inc: 256/256
|
||
```
|
||
|
||
它汇编 `examples/add8*.r8asm` 并在内置的 ROT-8 虚拟机里穷举输入。约 0.3 秒。
|
||
`asm/test.r8asm` 那三件只证明"汇编得出来",不证明"跑得对" —— 这个区别踩过坑,见下面
|
||
「字节级运算」。
|
||
|
||
`r8forge_derive` 是验证工具:
|
||
|
||
```sh
|
||
r8forge_derive <file.r8asm> 4 0 2 # 按输入组合跑,打印 stat
|
||
r8forge_derive <file.r8asm> -1 0=100 1=50 # 指定 cell 初值跑一遍,打印 out
|
||
r8forge_derive <file.r8asm> -2 0=100 # 同上,另外倒出所有非零 cell
|
||
```
|
||
|
||
注意 `-1/-2` 是拿**汇编源码**跑的(内部会先 `import_file`),不能直接喂 `.rot8` 纸带。
|
||
|
||
## 指令集语义(这些搞错会浪费一整轮)
|
||
|
||
```
|
||
'-' flb 翻转最低位 cells[dp] ^= 1
|
||
'+' ror 右旋整个 cell
|
||
'[' biz 当前 cell 为零 -> 跳到匹配的 ']' 之后
|
||
']' rnz 当前 cell 非零 -> 跳回匹配的 '['
|
||
```
|
||
|
||
由此推出三条**关键约束**:
|
||
|
||
1. **`set_0`(= `[-]`)只对 0 和 1 收敛。**
|
||
`5` 会 `5->4->5->4` 死循环,`0x80` 永远不变,而且 tape 走不到下一条指令、不报错。
|
||
`erase`(`%rep _CELL_SIZE { set_0; ror }`)卡在同一个 `set_0` 上,也清不掉任意 cell。
|
||
→ **清必须发生在 cell 还只放一位的时候。**
|
||
2. **合成的程序只碰每个 cell 的低 K 位**(K = 参与运算的输入位数),高位不追踪。
|
||
所以 `%synth` 出来的块只适用于"每格一位"的世界。
|
||
3. **一个 cell 里放了多个位,它的位在程序里根本读不出来。**
|
||
没有移位指令,只有 `ror`,而 `ror` 不改变"这个 cell 是不是 0",所以 `biz` 对整字节
|
||
永远只能回答"是不是 0";`flb` 只动最低位(写,不是读)。
|
||
→ **字节级运算只能一位一 cell**;"把一个字节放进一个 cell 再逐位处理"这条路在
|
||
这个 ISA 里不存在。详见下面「字节级运算」。
|
||
|
||
## 字节级运算(一位一 cell)
|
||
|
||
先记结论:**库里原来那层"整字节放一个 cell + ror 轮转"的宏,在运行时全是坏的。**
|
||
全是实测,不是推的:
|
||
|
||
| 调用 | 结果 |
|
||
|---|---|
|
||
| `raw_add` A=0,B=0 / 1+1 / 3+5 / 1+0 | 都跑不完(5M 步上限内 `out` 空) |
|
||
| `raw_add` A=127,B=130 | 跑完了,但 `out 0 0 0 0 0`,A/B/Ci/S/Co 全被毁 |
|
||
| `raw_and` 0xF0 & 0xCC、1 & 1、0 & 5 | 都跑不完 |
|
||
| `copy`(`lib.r8asm`)A=5 | 跑不完;只有 A=0 才正常返回 |
|
||
|
||
原因就是约束 1 + 3:轮转驱动每轮都用 `set_0` 清一个已经装了 7 位的累加器,而切片里的
|
||
`biz` 判的是整字节。**别去修它们**,那层的前提在 ROT-8 里不成立。
|
||
|
||
踩在这层上的还有两个现成文件,都别信:
|
||
|
||
* `asm/test.r8asm`(自检里那 1666 字符)只证明**汇编**得出来;它跑的 `raw_add` 会卡住。
|
||
* `asm/fibonacci.r8asm` 汇编出 8761 字符,跑起来只输出 `1 1` 就卡住。文件顶上那串
|
||
`1 1 2 3 5 8 13 21 34 55` 是旧实现时代的记录,现在对不上。
|
||
|
||
能用的是一层新写的、一位一 cell 的东西:
|
||
|
||
| 文件 | 内容 |
|
||
|---|---|
|
||
| `asm/byte_add.r8asm` | `byte_add %1`:8 位加法/累加。第 i 位占 `%1+4i`(A) `+4i+1`(B) `+4i+2`(进位) `+4i+3`(草稿);b7 的进位留在 `%1+30` |
|
||
| `asm/pack_byte.r8asm` | `pack_byte %1 %2 %3`:八个 flag(base、步长、目标)压成一个字节;`bit_raw_flip_if` 是它的积木 |
|
||
| `examples/add8.r8asm` | a+b,**1308 字符** |
|
||
| `examples/add8_inc.r8asm` | 连加三次 1,3142 字符,演示"可拼" |
|
||
| `tests/byte_add_test.cc` | `r8forge_byte_add_test`:65536 + 256 组合,0.3 秒 |
|
||
|
||
四条实测属性:
|
||
|
||
* **A 是累加器**:`byte_add` 之后 B 的八个 cell 全是 0,把下一个操作数摆进去再叫一次就接着加。
|
||
每次都清进位输入,所以是 mod 256(`add8_inc` 的进位只反映最后一次加的进位)。
|
||
* **压字节是单向的**:`pack_byte` 之后 flag 全被吃掉;压出来的字节能 `out`,但**不能再参与运算**
|
||
(约束 3)。所以"字节级加法"的输入只能是 flag,输出才能是字节。
|
||
* **cell 数 32**(8 组 quartet)。最早的共用进位版本只要 25 个 cell,但 8 个 bit 共用一个远端
|
||
进位 cell 让每片都付几十次 `.tp` 跳跃,实测 **2517 字符** —— 每 bit 自带进位 cell 换来
|
||
1308 字符,是划算的。
|
||
* **拼接靠 A**:想算 a+b+c,就 `byte_add` 两次(第二次前把 c 摆进 B)。
|
||
|
||
## %synth 相关
|
||
|
||
* 语法:`params N` + `mode tape|layers` + `metric chars|flat|blocks` + `layout ...`(可选)
|
||
+ 目标表 `stat ...` 或 `map ...`。`stat` 的项数必须是 `2^params`(3 参数要 8 项)。
|
||
参数是输入位 `%1..%K`、输出位、剩余草稿位(草稿位必须结束为 0,语义见 `compress_stat_space`)。
|
||
* `metric flat`:不计 `.tp` 行走代价 → 结果与 cell 布局无关、缓存可跨调用点复用。
|
||
`metric chars` 才是按实际布局最优。库里的块统一用 `flat`。
|
||
* 缓存:`.r8forge_cache/`(**已入库**,克隆下来直接命中)。键 = 模型版本 + params + mode +
|
||
metric + **解析后的整张目标表**(+ layout,仅 chars)。改指令模型时要 bump
|
||
`kModelVersion`(`src/synth.cc`)。
|
||
* 缓存代价参考:`bit_raw_add` 的 flat 搜索 990,636,137 状态 / 680 秒 / ~22 GB 内存。
|
||
层数上限现在是 8(原来是 5,5 会把一些可行的表误判成不可能)。
|
||
|
||
## 已经验证可行的东西
|
||
|
||
| 东西 | 结论 |
|
||
|---|---|
|
||
| 库里的 7 个位运算块(and/or/xor/inc/add) | 合成版比手写短:`asm/test.r8asm` 1714 → 1666 字符 |
|
||
| `bit_raw_add_nc`(4 cell 忽略进位切片) | flat 代价 **27**,完整全加器切片是 38;8/8 组合正确 |
|
||
| `bit_raw_add`(6 cell 全加器切片) | 保留 A、B,配合轮转使用(原版库的做法) |
|
||
| `byte_add`(8 位加法器,一位一 cell) | 32 cell / 1308 字符;**65536 组合穷举全对** |
|
||
| `pack_byte`(八个 flag -> 一个字节) | 单向可写;`bit_raw_flip_if` 是唯一"条件翻转、不清目标"的积木 |
|
||
|
||
## 已经确认走不通的(别重试)
|
||
|
||
* **3 个 cell 做加法切片**:层数上限 5 和 8 都报 `no program up to N layers`。
|
||
* **轮转 + 合成切片做字节级运算**:`raw_add`/`raw_and`/`raw_or`/`raw_xor`/`raw_inc`/`copy`/
|
||
`erase` 这一整层运行时是坏的(实测见上面「字节级运算」)。不是"只差操作数保留":
|
||
`set_0` 清不动带多个位的 cell,切片里的 `biz` 又只判整字节。
|
||
* **把一个字节放进一个 cell 再逐位处理**:约束 3 —— 那些位读不出来,做不到。
|
||
* 字节级(2^16 项真值表)交给 `%synth`:搜索空间不可行。
|
||
|
||
## 命名与注释格式(`asm/lib.r8asm` 的约定)
|
||
|
||
```asm
|
||
; Macro bit_raw_add_nc
|
||
; %1: CellBit A
|
||
; %2: CellBit B
|
||
```
|
||
|
||
* 暴露 cell 作为参数的宏,名字里要有 `raw`;块级别的宏(`Cell` 参数)不带。
|
||
* `%synth` 块名 = `<宏名>_synth`,包装的 `%macro` 用不带 `_synth` 的名字。
|
||
* 宏里的注释沿用上面那种 `; Macro` / `; %N: <类型> <名字>`。
|