Files
R8Forge/tests/tape_opt.cc
T
dsh ca72e19b0b tape_opt: the peephole pass -- 10% off the flagship tape with identical output
r8forge_tape_opt rewrites a tape with three run-local rules:

  > <   a maximal run of pointer moves becomes its net displacement
  +     a run becomes (length % _CELL_SIZE), a whole turn is a no-op
  -     a run becomes (length % 2), two flips cancel

The nice part is what it does NOT do: it never looks at brackets.  '[' and ']' are
not in any of the three character classes, so they always end a run, and within a
run only the net matters (the pointer moves are pure additions and nothing in
between is observable).  Keeping every run's net keeps the pointer identical at
every surviving character, brackets included, so the pairing that the machine
finds by scanning cannot change; and since bracket characters are never added or
removed, a run can never be merged across one.  No stack, no offset table, one
linear pass.

Measured, with tape_run's out stream compared before and after (and the reference
machine on the flagship's -n stream, same 93 values):

  asm/test.r8asm      64513 -> 58060   (-6453, 10.0%)
  examples/calc.r8asm 62585 -> 54863   (-7722, 12.3%)   "12 34 + ." identical
  examples/term.r8asm  3467 ->  3402   (  -65,  1.9%)   "hi" identical

Step counts unchanged (the removed instructions did nothing).  Not yet wired into
`r8forge` itself: the pipeline is `r8forge f.r8asm > t.rot8 && r8forge_tape_opt
t.rot8 t2.rot8`, and a --opt flag can wrap that later.
2026-11-18 10:29:53 +08:00

126 lines
3.2 KiB
C++

// r8forge_tape_opt -- the tape level peephole pass.
//
// r8forge_tape_opt <in.rot8> [out.rot8]
//
// Three run-local rules. None of them needs to know where the brackets are,
// because '[' and ']' are not in any of the three character classes, so they
// always end a run:
//
// > < a maximal run of pointer moves becomes its net displacement
// + a run becomes (length % cell_size): a whole turn is a no-op
// - a run becomes (length % 2): two flips cancel
//
// A run never crosses another character: `> + >` is not `>> +`, the rotation
// would land on a different cell. Preserving the net of every run means the
// data pointer is unchanged at every surviving character (brackets included),
// so the pairing found by scanning at run time cannot change either.
#include "r8forge/geometry.hh"
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iterator>
#include <string>
namespace {
constexpr char kGt = '>';
constexpr char kLt = '<';
constexpr char kRor = '+';
constexpr char kFlb = '-';
bool is_ins(char c) { return c != 0 && std::strchr("><+-[].,", c) != nullptr; }
std::string optimize(const std::string &tape) {
const size_t width = kDefaultCellSize;
const size_t n = tape.size();
std::string out;
out.reserve(n);
for (size_t i = 0; i < n;) {
const char c = tape[i];
if (!is_ins(c)) {
i++; // a stray byte (newline, comment) does nothing; drop it
continue;
}
if (c == kGt || c == kLt) {
long net = 0;
while (i < n && (tape[i] == kGt || tape[i] == kLt)) {
net += tape[i] == kGt ? 1 : -1;
i++;
}
const char dir = net >= 0 ? kGt : kLt;
const long count = net >= 0 ? net : -net;
for (long k = 0; k < count; k++) {
out += dir;
}
continue;
}
if (c == kRor) {
size_t run = 0;
while (i < n && tape[i] == kRor) {
run++;
i++;
}
out.append(run % width, kRor);
continue;
}
if (c == kFlb) {
size_t run = 0;
while (i < n && tape[i] == kFlb) {
run++;
i++;
}
out.append(run % 2, kFlb);
continue;
}
out += c;
i++;
}
return out;
}
} // namespace
int main(int argc, char **argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: r8forge_tape_opt <in.rot8> [out.rot8]\n");
return 2;
}
std::ifstream in(argv[1], std::ios::binary);
if (!in) {
std::fprintf(stderr, "could not open %s\n", argv[1]);
return 1;
}
const std::string tape((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
std::string filtered;
filtered.reserve(tape.size());
for (char c : tape) {
if (is_ins(c)) {
filtered += c;
}
}
if (filtered.empty()) {
std::fprintf(stderr, "no instructions in %s\n", argv[1]);
return 1;
}
const std::string out = optimize(filtered);
if (argc > 2) {
std::ofstream f(argv[2], std::ios::binary);
if (!f) {
std::fprintf(stderr, "could not write %s\n", argv[2]);
return 1;
}
f << out;
} else {
std::fwrite(out.data(), 1, out.size(), stdout);
}
const size_t before = filtered.size();
const size_t after = out.size();
std::fprintf(stderr,
"r8forge_tape_opt: %zu -> %zu characters (%ld saved, %.1f%%)\n",
before, after, (long)(before - after),
100.0 * (double)(before - after) / (double)before);
return 0;
}