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.
127 lines
3.2 KiB
C++
127 lines
3.2 KiB
C++
// Derives the state table of a library macro: assembles a program that calls
|
|
// it on cells 0..P-1, then runs the tape once per input combination and reads
|
|
// the resulting cell values back.
|
|
//
|
|
// r8forge_derive <file.r8asm> <P> [cells...]
|
|
//
|
|
// Prints one line per input combination: the P-bit value of the parameters
|
|
// afterwards. That table is exactly what %synth's `stat` line wants.
|
|
#include "r8forge/r8asm.hh"
|
|
#include <cstddef>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
struct machine {
|
|
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
|
|
std::vector<uint8_t> out;
|
|
size_t dp = 0;
|
|
|
|
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
|
|
|
|
void run(const std::string &tape, size_t limit = 5'000'000) {
|
|
for (size_t pc = 0; pc < tape.size() && limit; pc++, limit--) {
|
|
switch (tape[pc]) {
|
|
case '>':
|
|
dp++;
|
|
break;
|
|
case '<':
|
|
dp--;
|
|
break;
|
|
case '-': // flb
|
|
cells[dp] ^= 1;
|
|
break;
|
|
case '+': // ror
|
|
cells[dp] = ror(cells[dp]);
|
|
break;
|
|
case '[':
|
|
if (cells[dp] == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']':
|
|
if (cells[dp] != 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc--;
|
|
if (tape[pc] == ']')
|
|
depth++;
|
|
else if (tape[pc] == '[')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case '.':
|
|
out.push_back(cells[dp]);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 3) {
|
|
std::cerr << "usage: r8forge_derive <file.r8asm> <params> [cells...]\n";
|
|
return 2;
|
|
}
|
|
const char *file = argv[1];
|
|
const int params = std::atoi(argv[2]);
|
|
if (params == 0) { // just run the program and dump what it printed
|
|
r8asm a;
|
|
if (!a.import_file(file, 8)) {
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
return 1;
|
|
}
|
|
machine m;
|
|
m.run(a.export_rot8());
|
|
std::cout << "out";
|
|
for (uint8_t v : m.out)
|
|
std::cout << " " << int(v);
|
|
std::cout << "\n";
|
|
return 0;
|
|
}
|
|
if (params < 1 || params > 8) {
|
|
std::cerr << "params must be 1..8\n";
|
|
return 2;
|
|
}
|
|
std::vector<int> addr(static_cast<size_t>(params));
|
|
for (int i = 0; i < params; i++)
|
|
addr[static_cast<size_t>(i)] = argc > 3 + i ? std::atoi(argv[3 + i]) : i;
|
|
|
|
r8asm assembler;
|
|
if (!assembler.import_file(file, 8)) {
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
return 1;
|
|
}
|
|
const std::string tape = assembler.export_rot8();
|
|
std::cerr << "tape: " << tape.size() << " characters\n";
|
|
|
|
std::cout << "stat";
|
|
for (int combo = 0; combo < (1 << params); combo++) {
|
|
machine m;
|
|
for (int b = 0; b < params; b++)
|
|
m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] = uint8_t((combo >> b) & 1);
|
|
m.run(tape);
|
|
unsigned val = 0;
|
|
for (int b = 0; b < params; b++)
|
|
if (m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] & 1)
|
|
val |= 1u << b;
|
|
std::cout << " " << val;
|
|
}
|
|
std::cout << "\n";
|
|
return 0;
|
|
}
|