dsh 19cbfb36f6 R8Forge: assembler and searcher in one repo, %synth directive
The searcher stops being a separate program: %synth is an assembly-time
directive.  It describes a truth table, the assembler runs the search while
parsing, and the block list comes back as ready-to-paste r8asm.

Merging is what makes the cost model honest.  The price of a layer is
  cost = 2 (biz + rnz) + sum(move distance + op cost)
and the move distance depends on the call site's cell layout, not on anything
the assembler knows in advance: the same five-layer adder is 87 characters
with the cells adjacent and 136 with them two apart.  Only the side that sees
the layout can price it, which is now the assembler itself.

Working: the assembler (asm/test.r8asm still assembles to the same 1714
characters), the searcher (bit planes + backward one-step lookup, full_add in
5 layers, proven shortest in 7.2s), and %synth parsing plus the search itself.
Not working yet: splicing the synthesised body into the tape -- a synth call
fails loudly with 'unknown macro' instead of emitting anything, so nothing can
silently assemble wrong.  The two ways to finish it are in the README.

Also carries over the CMake fix: no set(CMAKE_CXX_FLAGS ...), which used to
drop /EHsc, push -O3 into Debug and fight /RTC1.
2026-11-05 23:59:15 +08:00

R8Forge

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

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

状态

部分 状态
汇编器(原 r8asm) 可用,asm/test.r8asm 纸带不变(1714 字符)
搜索器(原 R8Veritas) 可用,位平面 + 反向查表,full_add 5 层最优
%synth 解析 + 搜索 可用,汇编期跑,结果打印成可直接粘贴的 r8asm
%synth 结果拼接进纸带 未接线,现在调用点会明确报 unknown macro,不会悄悄生成错纸带

真值表描述:

%synth fulladd
	params 6
	inputs  %1 %2 %3        ; 输入位,必须是最前面的参数
	outputs %4 %5           ; 要算出来的输出位
	map  0 1 1 2 1 2 2 3    ; 每个输入组合对应的输出位取值(bit0 = %4)
%endsynth

inputs/outputs 不能叫 in/out:out 和 ROT-8 的指令助记符撞了,词法会先认成 rot8_out。

汇编时的输出:

r8asm: %synth fulladd: 5 layers (shortest), 20388515 states, 7.16s
r8asm: %synth fulladd body:
	.tp %1
	biz
		.tp %2
		flb
	...

为什么合在一起

代价 = 汇编出来多少条指令,而这只有发射器知道:

cost(layer) = 2                   # biz + rnz
            + Σ (|Δ| 移动 + op)   # flb=1, set_0=3, set_1=4

其中 |Δ| 来自调用点的 cell 布局,不是汇编器的内部知识 —— 同一份 5 层程序, cell 紧邻时 87 字符,间隔 2 格时 136 字符。所以这个数只能由「知道布局的那一方」算, 也就是汇编期。

搜索器

  • 状态 = 位平面真值表,只留影响答案的输入(加法器类问题通常 3 个,48 bit 一个 uint64_t)
  • 前向 BFS 到 d−1 层,最后一层不展开:枚举目标的一步前驱反查表
  • proven_optimal 为真表示长度已被穷尽证明

full_add 结果(r8veritas<6>):5 层,2038 万状态,7.2 s,5 层是穷尽搜索下的最短。 >=4 层被排除,而且换更宽松的指令语义(每目的地独立 OR/XOR、set_0、条件位≠牺牲位、 8 个参数 cell)也没有 4 层解。

下一次要做的事

  1. 把 %synth 的结果接进纸带。两条路:
    • 生成宏体 → import_macro(内存版,我在 parse() 递归里踩了 bug,还没追完)
    • 生成一个临时 .r8asm 再 %include(绕开内存 token 流,最稳)
  2. 搜索的代价从「1/层」换成真实指令数(Dijkstra + 每层目的地访问顺序的 TSP), 这样嵌套条件块(A AND B 门控)也能公平地参与竞争。
  3. 缓存:同一个 %synth 已经在汇编期只跑一次(按名字),但要跨进程缓存还得落盘。

构建

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

CMakeLists.txt 里没有 set(CMAKE_CXX_FLAGS ...):那会把默认 flag 整个换掉,MSVC 下丢 /EHsc、还把 -O3 塞进 Debug 和 /RTC1 打架。

布局

include/r8forge/r8asm.hh      汇编器
include/r8forge/r8veritas.hh  搜索器
src/r8asm.cc                  汇编器 + %synth 指令
src/synth.cc                  %synth 的真值表 -> 搜索
example(s)/                   用法示例
asm/                          标准库与既有实现
S
Description
R8ASM assembler with the searcher built in
Readme
2 MiB
0 Stars 1 Watchers 0 Forks
Languages
C++ 86.5%
CMake 5.8%
Verilog 5.4%
PowerShell 2.3%