Layers are not characters. solve_cost runs Dijkstra over the same state graph
with an edge weight of what the block actually assembles to:
w = 3 (biz + flb + rnz) + 2 * span(cell addresses) + sum(op)
op = 1 for flb, 4 for set_1
Dial buckets, pop in cost order, the first pop of the goal is the shortest
tape. Give it max_cost and it becomes branch and bound: anything past the
bound never enters the queue.
Exhausted regions on the full adder with adjacent cells:
max_cost 20 1,086 states 0.0s
max_cost 40 128,528 states 0.1s
max_cost 50 999,448 states 0.7s
max_cost 60 7,671,626 states 12.8s
max_cost 65 20,153,459 states 47.6s
About 2.6x states per 5 cost, about 32 bytes per state, so max_cost 80 is
~350M states / 11GB and 82 would be ~600M / 19GB. The block weight leaves out
the hop from the previous sacrifice (that would make the cost depend on
history and Dijkstra would stop working); the emitter adds it when it reports
the real length -- the five layer program is 82 in the model, 87 on the tape.
The first version of the bucket ring only looked one max-edge-weight ahead for
work, so it declared 'nothing under the limit' while the queue still had
states further out. That made the small limits look sound when they were not;
advancing to the next nonempty bucket fixed it.
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 上界就变成
分支限界,超出上界的状态不入队。
穷尽得到的数据(6 个 cell 紧邻,full_add):
| max_cost | 状态数 | 用时 |
|---|---|---|
| 20 | 1,086 | 0.0 s |
| 40 | 128,528 | 0.1 s |
| 50 | 999,448 | 0.7 s |
| 60 | 7,671,626 | 12.8 s |
| 65 | 20,153,459 | 47.6 s |
每加 5 点代价状态数约 ×2.6,每状态约 32 字节(8 字节 key + 2 距离 + 4 父 + 2 指令 + 哈希表)。
按这个斜率,max_cost = 80 约 3.5 亿状态 / 11 GB,82 约 6 亿 / 19 GB —— 后者超出机器
内存,所以默认跑 80。
注意:模型里的 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/层」换成真实指令数(Dijkstra + 每层目的地访问顺序的 TSP),这样嵌套条件
块(
A AND B门控)也能公平参与竞争。注意嵌套会降低层数但拉长纸带 —— 层数不是长度 的好代理。 - 缓存:现在每次汇编都要重跑搜索(full_add 是 7.7 秒)。按真值表 + 布局落盘缓存。
%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 汇编期合成全加器