dsh 79bc0a4068 Splice %synth into the tape, and check it on a ROT-8 machine
The synthesised body is written out as a macro file and imported, so the
generated code goes through the ordinary expansion path instead of being
spliced by hand.  set_1 is spelled out as set_0 plus flb, so the generated file
needs nothing from lib.r8asm.

examples/full_add.r8asm now assembles: 7.7 seconds of search at assembly time,
then a 95 character tape.  tests/synth_test.cc runs that tape on a small ROT-8
machine for all eight input combinations and checks S and Co -- all eight pass.

Two things that cost me time and are worth writing down:

  * the opcode map is "><+-[].,", i.e. '-' is flb and '+' is ror.  Guess it the
    other way round and a perfectly good tape looks like an infinite loop.
  * synth names must not collide with mnemonics: 'out' is lexed as rot8_out
    before it can be a block keyword.
2026-11-06 00:08:29 +08:00

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 无

为什么合在一起

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

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       汇编期合成全加器
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%