Audited all 30 %macro in asm/. The format was already followed almost
everywhere -- the doc block sits above the %macro, or above the %synth block
that generates its body -- but four things were wrong:
raw_and.r8asm the two doc blocks still carried the old names (bit_and, and)
raw_xor.r8asm raw_xor's first two parameters were typed Unit, not Cell
lib.r8asm step / back / output had no type at all ("%1: step_count")
and two files still repeated the retracted claim that set_0 only converges on 0
and 1 (raw_add_nc.r8asm, raw_add_nc4.r8asm). Those now say what set_0 really
does -- it clears the low bit -- and stay on the point that matters for a bit
level slice: A, B and the carry must be single bits because the searched
program only tracks the low K bits.
Parameter types are now a closed set: Cell, CellBit, Unit, UnitBit, Count
(Count is for step/back's repeat count). It is written down in AGENTS.md
together with the format, so the next macro has something to copy.
New r8forge_macro_docs_test checks the format instead of trusting review: a doc
block must exist, its "; Macro <name>" line must match the macro, the number
of documented parameters must equal argc, and every type must be one of the
five. Negative tested by deleting a parameter line and by renaming a block --
it reports both and exits 1.
Also fixes a real annoyance found while running the checks: %synth writes its
generated macro to the system temp directory and imports it. That write fails
intermittently on this machine (same directory, cmd can write, r8forge cannot;
cause unknown), which turned every assembly into "cannot open ...synth.r8asm".
The body is now built in memory and written to the temp directory or, if that
open fails, to the working directory, with one stderr line saying so.
Full suite green: test.r8asm 1666 characters, full_add 8/8, verify_raw_add_nc,
byte_op_test 65536x4 + 512, macro_docs_test 30/30, fibonacci prints its ten
numbers.
Four interpreters here (tests/synth_test.cc, src/derive.cc and the two I added
earlier: tests/tape_run.cc, tests/byte_add_test.cc) implemented '[' and ']' as
a Brainfuck style "is this whole cell zero" test. The reference implementation
does not:
ROT-8-viz src/rot8.c
rot8_biz: if ((*mem & 1) == 0) skip to just past the matching ']'
rot8_rnz: if ((*mem & 1) == 1) jump back to the matching '['
That one difference is why every byte level program in the library looked like
an endless loop. It is invisible in the bit level world -- for a cell holding 0
or 1 the low bit test and the zero test agree -- which is why the synth tables,
asm/test.r8asm and full_add never noticed anything.
Cross checked against the reference before touching anything: the tape our
assembler produces for "A = 128, set_0, B = 3, raw_add" prints 128 3 0 131 0 on
ROT-8-viz/build/rot8_test.exe and hung in our derive.
With the fix, measured:
raw_add 65536/65536 pairs ok 1647 chars
raw_and 65536/65536 639
raw_or 65536/65536 575
raw_xor 65536/65536 463
raw_inc 512/512 1214
asm/fibonacci.r8asm prints 1 1 2 3 5 8 13 21 34 55, exactly what its
comment claims
So the byte level layer was never broken. Two commits ago it was declared dead
-- set_0 "only converges on 0 and 1", the bits of a packed byte "unreadable",
raw_add "only survives 0+0" -- entirely on the strength of the broken
interpreters. AGENTS.md and README.md now say the opposite and keep the wrong
diagnosis as a worked example: when byte level code looks hung, check the
interpreter first.
New:
tests/byte_op_test.cc + drv/raw_{add,and,or,xor,inc}_byte.r8asm
exhaustive runtime proof for the byte level library, 0.5 s
tests/tape_run.cc + r8forge_tape_run
a second interpreter that runs a .rot8 file directly -- cross checking
derive is now one command
verify_set_0.r8asm now demonstrates what set_0 really does instead of claiming
it hangs: 255 -> 254, 5 -> 4, 128 -> 128 (it clears the low bit, not the cell).
The one bit per cell layer (byte_add / pack_byte) stays as a documented
alternative: it is correct under the real semantics too, just redundant.
Writing the sum back into A's cell would drop the separate S cells and cost no
extra instructions -- the driver would even get simpler. So it was worth
trying, and it does not work: %synth reports
no program up to 5 layers
and raising the layer bound to 8 does not help either:
no program up to 8 layers
So the obstacle is not runtime cost, it is feasibility: three cells cannot do
' two inputs plus a carry in -> a sum plus a new carry' in this instruction
model, the same class of limit that stopped clb_bit_and. Keeping the four cell
slice (and the 25 cell bit-per-cell adder) is the right call, and the file is
committed with that conclusion written into it so nobody retries it blind.
The layer bound is now 8 rather than 5 (src/synth.cc), since 5 was low enough to
call some feasible tables impossible; a feasible table still costs the same,
only a hopeless search takes 60% longer before giving up.
Cache untouched: restored from git after the experiments, asm/test.r8asm still
assembles from it in a fraction of a second, 7/7 hits, 1666 characters.
Asked why the slice still needs 5 cells. It does not: the scratch cell was
doing nothing the carry could not do, so the 4 cell version is
p1..p4 = A, B, carry (in -> out), S
and the table says S = A^B^Ci, carry = AB|AC|BC, A and B consumed. %synth
finds cost 27 in 5 blocks for it -- the same tape length as the 5 cell version
(cell count is not part of the cost metric, only the instructions are) but one
cell cheaper, 73 characters for a single slice.
All eight input combinations verified through the built in machine.
Diagnosis of the chained hang, from dumping the cells after one slice plus the
ror rotation:
A=1 B=0 -> cells: 4=80 (only the rotated sum bit survives)
A=1 B=1 -> cells: 1=1 (only the carry)
so a slice leaves A and B fully clear, and the carry is a clean 0/1. The dirty
one is S: after the rotation its bit 7 is set, and set_0 in the next slice does
not remove it -- the dump after two slices shows 4=81 and, worse, the tape then
runs off the end instead of reaching 'output S'.
So the fix is not in the slice but in the driver: it must not rotate the sum
into a cell that the next slice clears, or must clear the whole cell (set_cell,
not set_0 over the modelled bits). That is the next step, together with
re-checking whether set_0 is the right primitive here at all.
The idea: for an adder whose operand sum is known not to overflow, there is no
need for a separate carry in cell or a carry out cell -- the carry can live in
one cell and be overwritten each bit, and the final carry is simply dropped.
That takes the slice from 6 cells (A, B, Ci, S, Co, tmp) down to 5 (A, B,
carry, S, tmp).
The slice was not hand written: the table was derived from S = A^B^Ci and
carry = AB|AC|BC, and %synth searched it with metric flat:
bit_add_nc_synth: tape cost 27 in 5 blocks (1891406 states)
bit_raw_add_synth (the 6 cell full adder): tape cost 38 in 5 blocks
so 11 units of the metric cheaper, about 29%, plus one cell less per slice.
Verified: all eight input combinations of a single slice are correct through the
built in machine (A, B, Ci -> S), including the all zero one.
Known limitation, found rather than guessed: the synthesised program only
touches the low K bits of each cell, the bits the truth table knows about. The
caller therefore has to hand it cells whose high bits are zero. Chaining slices
with ror breaks that: the rotated cell keeps bit 7, and a block that loops until
its cell reaches zero can never get there. So the 8 bit raw_add_nc hangs; the
slice itself is fine, the driver needs to move each bit into a clean cell first.
Also added: r8forge_derive <file> -1 addr=value ... runs a tape with chosen cells
and prints OUT, which is how the slice was checked.
r8asm::ast::CELL_SIZE machine word width in bits, run time value
(import_file's second argument, 8 in the CLI)
r8veritas<CELL_ARGC> how many parameter cells a synth block has,
compile time, selected by 'params N'
set_cell_bits went back to set_cell_size for the same reason, and the .r8asm
visible names (_CELL_SIZE, _CELL_MAX, _ROL_STEP) are unchanged, so lib.r8asm
keeps working as written.
This time the string literal used by %rep and by the .xor masking is the one
the C++ member is now called too, which removes the trap the previous rename
walked into.
Regressions after the rename:
asm/test.r8asm exit 0, 1666 characters, 7/7 cached
examples/full_add_layers.r8asm eight combinations ok
derive bit_raw_add same table as the hand written version
clang-cl build of all five targets, and the clang built binaries produce the
same 1666 character tape and the same table.
There were two unrelated things under one name:
r8asm::ast::CELL_BITS the machine word width in bits, a run time number
(import_file(path, 8)), used by %rep and the .xor
argument masking
r8veritas<PARAMS> how many parameter cells a synth block has, a
compile time template argument selected by 'params N'
The first is now CELL_BITS, the second PARAMS. What a .r8asm program sees does
not change: _CELL_SIZE / _CELL_MAX / _ROL_STEP keep their names, because
lib.r8asm uses them.
Answering the actual question along the way: the machine word width is a run
time parameter (import_file's second argument, hardcoded to 8 by the CLI), not a
template argument -- the comment in r8asm.hh even apologises for that. The
synth block's 'params N' is the other one, and only that goes through the
template switch (1..8).
The first attempt at the rename also rewrote the _CELL_SIZE string used by %rep
into _CELL_BITS, so the lookup threw and the assembler died with a fastfail on
any file containing .xor or %rep. Caught by regressing test.r8asm (exit
0xC0000409, no tape); the string is restored and asm/test.r8asm is back to 1666
characters with all seven blocks cached.
Asked whether the cache survives a change of CELL_SIZE, and looking at the key
turned up a real problem.
The key used to be params | mode | metric | the stat line's values | layout.
Every block in asm/ uses a stat line, which is why nothing had gone wrong yet,
but a block written with map leaves stat empty -- so two map blocks with the
same params/mode/metric hashed to the same entry, and the second one would have
been served the first one's program.
The key is now the resolved target table itself (every entry, per plane), which
is exactly what the search is given:
* params / CELL_SIZE is part of it, and the table covers all 2^params entries,
so blocks of different sizes cannot collide, and changing params in a block
simply misses and recomputes
* two blocks that describe the same table (map or stat, either spelling) now
share one entry
Also added kModelVersion to the key. Without it, changing the instruction
model later (say adding conjunctions) would keep serving programs that were
optimal for the old model. Bump it whenever the block set changes.
Cache files now start with a readable header, e.g.
# params 3 mode 0 metric 1
0 2 0
1 4 0
Re-warmed after the change: 7 entries, 885s (bit_raw_add again, 990M states).
asm/test.r8asm still 1666 characters, all seven derived tables still match the
old implementations bit for bit, and a second assembly hits the cache for all
seven and produces an identical tape.
The flat metric goes deeper than the exact one did (415M states for the full
adder), and 700M was not enough for bit_raw_add: it ran out, fell back to the
layer answer and cached that. The box has 64 GB, not the 32 I assumed, so the
ceiling is now 1.5B states (~22 GB) and the search completes:
bit_raw_add_synth: tape cost 38 in 5 blocks, 990,636,137 states, 680s
Same tape length as the fallback happened to give, but now it is actually the
minimum of the metric rather than whatever the layer search reached.
All seven cached bodies were re-derived against the old implementations after
the switch and still match bit for bit (bit_raw_add 0 9 10 19 12 21 22 31
repeated, bit_raw_inc 0 5 6 11, clb_bit_or 0 4 4 4, clb_bit_xor 0 4 4 0, ...),
so the shorter tapes are a free win. asm/test.r8asm: 1674 -> 1666 characters,
and assembling it from the committed cache takes 0.5s.
Every synth block in asm/ now says 'metric flat', so the seven block bodies are
optimised without the .tp walk and therefore do not depend on where the caller
puts its cells. The resulting bodies are cached in .r8forge_cache/ and the
cache is committed, so a fresh clone assembles the library without waiting for
a single search.
asm/test.r8asm: 1674 -> 1666 characters.
The seven tables were re-derived after the switch and all match the hand written
implementations bit for bit (bit_raw_add still 0 9 10 19 12 21 22 31 repeated,
bit_raw_inc 0 5 6 11, and so on), so this is a pure length win.
One caveat: bit_raw_add's flat search still runs out of state budget (700M, the
table stops fitting in a laptop) and falls back to the layer answer, which is
what got cached for it. The other six converge in milliseconds; the whole warm
up took 396 seconds, almost all of it that one block.
%synth could optimise the real tape characters, but that price depends on where
the cells sit, so the answer was only good for one layout and the cache could
only be shared between call sites with the same addresses.
metric chars tape characters including the .tp walk layout dependent
metric flat characters without the walk layout independent
metric blocks block count layout independent
'flat' is the step-free mode: it counts biz/flb/rnz and the ops, ignores how
far apart the cells are, so one search result and one cache entry serve every
layout. The price is a slightly longer tape when the layout happens to suit a
different program: 90 characters for chars, 95 for flat on the full adder.
Checked: examples/full_add_flat.r8asm hits the cache on the second run, and the
same block with 'layout 0 2 4 6 8 10' added hits the *same* entry and emits a
byte-identical tape (the key drops the layout for the layout-free metrics).
Both pass the eight combination check.
The sequential search is memory bound on its hash table, so the split that
works is inside a Dial bucket, not across the graph:
* the table is read only for the whole bucket, so threads probe it with no
locking -- 99.7% of the work, since only about one edge in four hundred
lands on an unseen state
* misses and dist improvements are buffered per thread and applied by one
serial pass after the bucket
Cost order is untouched, so the answer is identical. Measured at limit 60:
threads wall states
1 12.40s 7,671,626
8 2.68s 7,671,626
16 2.20s 7,671,626
32 2.14s 7,671,626
and at the full limit 82 (the run that used to take 1350s): 162s, 414,631,673
states, cost 75 in 6 blocks -- the same program.
%synth now uses this by default. examples/full_add.r8asm assembles in 166s
instead of ~23 minutes and still produces the 90 character tape, checked on the
machine for all eight combinations.
The earlier attempt -- one worker per possible first block -- stays in
r8veritas.hh behind opt.threads but off by default; it duplicated the middle of
the graph and cost three to four times the work.
The eight bit-level macros in asm/ are now synthesised instead of hand written.
Their tables were not typed in by hand: r8forge_derive (new, src/derive.cc)
runs the OLD implementation over every input combination and reads the cells
back, so the replacement is bit-for-bit identical, quirks included. big two
findings:
* a synth block assumes the parameters that do not take part in the
computation start at 0 (that is what compress_stat_space means). These
macros are called repeatedly inside a loop, so S/Co/tmp arrive dirty; each
one now has a thin %macro wrapper that clears them and then calls the
synthesised body. With that, bit_raw_add derives to exactly the old table
(0 9 10 19 12 21 22 31 repeated) and bit_raw_and to 0 1 2 7 repeated.
* clb_bit_and stays hand written. Three parameters leave no scratch bit and
"and" needs both inputs alive at once, so it needs nested biz; the searcher
exhausts nine layers over three planes without finding anything. That is
the model's expressive limit, not a shallow search.
asm/test.r8asm assembles to 1674 characters, down from 1714, with identical
block semantics.
%synth results are cached in .r8forge_cache/ keyed by the table, so the
twenty minute tape search happens once per table rather than once per build.
Note on verification: r8forge_derive validates one block at a time, which is
what the tables above are. The little VM inside it is not solid enough to
vouch for whole programs (fibonacci prints two numbers instead of ten), so
"the whole library still computes the right answer end to end" is NOT verified
here -- that needs the real ROT-8 virtual machine.
A synth block used to spell out which parameter is an input and which is an
output. That was redundant: with map, 2^K entries already mean %1..%K are the
inputs and the value's bits land on the parameters after them; with a raw
table, nothing needs saying at all. So 'inputs' and 'outputs' now fail with a
message pointing at map/stat, and the map form derives both.
Added 'stat' as the fully explicit form: 2^params values, one per input
combination, which is exactly the state table the search consumes.
examples/full_add_stat.r8asm is the same adder written that way and produces a
byte-identical tape to the map version (95 characters), both pass the eight
combination check.
%synth used to always run both searches, so every assembly of the full adder
sat there for twenty minutes. A block can now say 'mode layers' to stop after
the layer search (7.7 seconds) or 'mode tape' (the default) to pay for the
shortest tape. examples/full_add_layers.r8asm is the fast one, 95 characters
and still checked on the machine.
The lexer also no longer chokes on a UTF-8 BOM. Every Windows editor writes
one, and the first token came out as a garbage prefix, which crashed the
assembler with an access violation rather than saying anything useful.
README grew a getting-started walkthrough: build, a complete adder.r8asm, what
the two stderr lines mean, how to drop a synth block into a library, and the
three things that bite (inputs first, don't call them in/out, map length).
The layer search still runs first (7.7 seconds) but only to get a bound; the
cost search then takes over and its answer is what goes on the tape. A caller
that wants its own layout can say 'layout 0 2 4 6 8 10' inside the block.
examples/full_add.r8asm now assembles to 90 characters of tape instead of 95:
cost 75 in 6 blocks after 415M states and about 21 minutes, against the 82 the
five layer program needs. Assembly getting slower is the whole point -- it is
the price of the shortest tape.
For scale: one bit_raw_add slice is 179 characters with the cells adjacent, and
the synthesised slice is 82. A whole raw_add call is 1650 characters today, so
eight synthesised slices would land near 700.
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.
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.