Files
R8Forge/README.md
T
dsh 230cf92159 Fix the Dial queue: two ways it silently dropped states
First version scanned ahead for the next nonempty bucket but stopped one max
edge weight out, so a sparse frontier ended the search early and claimed
"nothing under the limit" while the queue still had work.  Scanning further is
also wrong: the ring reuses a slot every ring.size steps, so a far bucket sits
where a near one would be.  Plain Dial now: step one cost unit at a time.

The growth table in the README was measured with the broken version and was
wildly optimistic.  Real numbers from the fixed build, full adder with
adjacent cells: cost 58 -> 80M states, 66 -> 220M, 70 -> 280M, about 27 bytes
per state and 650k states/s, so cost 75 / 450M states / 12GB is roughly the
ceiling on this machine.

Validated solve_cost on the small problems where the answer is known by hand:
copy comes out at cost 17 (sacrifice 0 into {2}, then sacrifice 2 into {0,1}),
which is the two block program, and it replays ok.
2026-11-06 09:10:00 +08:00

6.1 KiB
Raw Blame History

R8Forge

R8ASM 汇编器 + R8Veritas 搜索器,合成一个仓库。

搜索器不再是独立程序:%synth 是汇编期指令,描述一张真值表,汇编器现场搜出最短的块 序列直接拼进纸带。代价模型和发射器从此是同一份代码,不用再维护两套「这个块要花多少条 指令」的估算。

用法

%include "asm/lib.r8asm"

%synth fulladd
	params 6
	inputs  %1 %2 %3        ; 输入位,必须排在最前面
	outputs %4 %5           ; 要算出来的输出位
	map  0 1 1 2 1 2 2 3    ; 每个输入组合对应的输出位取值(bit0 = %4)
%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       ; 这一行展开成汇编期合成的 5 个块
output S
output Co

汇编:

$ r8forge examples/full_add.r8asm
r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.7s
> [-]-...    (95 字符纸带)

tests/synth_test.cc 把这条纸带丢进一台小 ROT-8 虚拟机,8 种输入组合全对:

tape: 95 characters
  000 -> 00  expect 00  ok
  ...
  111 -> 11  expect 11  ok
all eight combinations ok

注意 inputs / outputs 不能写成 in / out:out 和 ROT-8 助记符撞了,词法会先认成 rot8_out,整个 block 解析错位。

实现方式

%synth 在 parse() 里被读成一张真值表,随即调用搜索器;得到的块序列写成

%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)

层数最少不等于纸带最短。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 位字里)。

构建

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    # 合成 + 跑虚拟机自检

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 虚拟机自检
asm/                          标准库与既有实现
examples/full_add.r8asm       汇编期合成全加器