Files
R8Forge/tests/tape_run.cc
T
dsh 01aa13d0f0 tape_run --profile: the walking share is measured, not estimated
A tape run now reports, on stderr (stdout keeps the out line):

    profile: 314779 steps, 250112 of them pointer moves (79.5%)
      > 125073 39.7%   < 125039 39.7%   + 22178 7.0%   - 12395 3.9%
      [ 25121 8.0%     ] 4954 1.6%      . 7  , 10
      hottest cells (steps spent there): 11:30471 10:26446 9:25733 12:24879 ...

The flagship tape: 236441 steps, 180665 pointer moves (76.4%), hottest cells
91, 90, 92, 89, 87, 93, 88, 86, 85 -- which is exactly clb_digits' scratch block.

So the "walking is about seventy percent" line in AGENTS turns out to be right, and
now it is measured: this machine has no random access, every access costs
|delta address|, so layout is the performance knob -- which is also why the
peephole pass (that only cancels round trips) buys 17% of the steps.

TODO drops the --profile item; AGENTS gets the number and the conclusion, with the
two commands next to the other baseline entries.
2026-11-18 15:19:57 +08:00

186 lines
5.8 KiB
C++

#include "r8forge/geometry.hh"
// Independent ROT-8 interpreter. Reads a .rot8 tape straight from a file --
// no macro, no assembler, no r8asm -- and prints what `out` ('.') produced.
//
// r8forge_tape_run <file.rot8> [-i text] [cells...]
//
// cells are addr=value (decimal), e.g. 0=128
// -i/--input gives INP (",") its characters, low 7 bits each; when the text
// runs out INP reads 0. That is enough to drive an interactive program from
// a script -- the ROT-8-viz CLI is the one that prompts a live terminal.
//
// This exists as a second opinion for r8forge_derive: if the two disagree, the
// difference is in derive; if they agree, the disagreement is in the program.
//
// Note the character map: "><+-[].," -- '-' is flb, '+' is ror, and ror does
// not change whether a cell is zero (that is what makes [-] a two value loop).
#include <cstdint>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "usage: r8forge_tape_run <file.rot8> [addr=value ...]\n";
return 2;
}
std::ifstream in(argv[1], std::ios::binary);
if (!in) {
std::cerr << "could not open " << argv[1] << "\n";
return 1;
}
const std::string tape((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
std::vector<uint32_t> cells(kDefaultMemoryCells, 0);
std::deque<uint32_t> input;
bool trace = false;
bool profile = false;
for (int i = 2; i < argc; i++) {
const std::string a = argv[i];
if (a == "--trace" || a == "-t") {
trace = true;
continue;
}
if (a == "--profile" || a == "-p") {
profile = true;
continue;
}
if (a == "-i" || a == "--input") {
if (i + 1 >= argc) {
std::cerr << "-i needs a string of input characters\n";
return 2;
}
for (const char *p = argv[++i]; *p; p++)
input.push_back(uint32_t((unsigned char)*p & 0x7f));
continue;
}
const size_t eq = a.find('=');
if (eq == std::string::npos)
continue;
const size_t at = size_t(std::stoul(a.substr(0, eq)));
cells.at(at) = uint32_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask);
}
std::vector<uint32_t> out;
size_t dp = 0;
const size_t limit = 20'000'000; // same as r8forge_derive
size_t pc = 0;
size_t steps = 0;
size_t ins_count[256] = {};
std::vector<size_t> cell_hits(cells.size(), 0);
size_t walking = 0;
for (; pc < tape.size(); pc++) {
if (++steps > limit) {
std::cout << "HANG after " << limit << " steps (pc " << pc << ", dp " << dp
<< ", cell=0x" << std::hex << int(cells[dp]) << std::dec
<< ")\n";
return 3;
}
if (profile) {
ins_count[(unsigned char)tape[pc]]++;
cell_hits[dp]++;
if (tape[pc] == 0x3e || tape[pc] == 0x3c)
walking++;
}
if (trace) {
// cells 0..3 are the interesting ones when debugging a library macro
std::cout << "pc " << pc << " " << tape[pc] << " dp " << dp << " [";
for (size_t i = 0; i < 4; i++)
std::cout << (i ? " " : "") << int(cells[i]);
std::cout << "]\n";
}
switch (tape[pc]) {
case '>':
dp = (dp + 1) % cells.size();
break;
case '<':
dp = (dp + cells.size() - 1) % cells.size();
break;
case '-': // flb
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = uint32_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1)));
break;
// biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz // src/rot8.c. Reading them as a Brainfuck style zero test makes every
// byte level program in the library look like an endless loop.
case '[':
if ((cells[dp] & 1) == 0) {
for (int depth = 1; depth;) {
pc++;
if (pc >= tape.size()) {
std::cout << "unbalanced '['\n";
return 4;
}
if (tape[pc] == '[')
depth++;
else if (tape[pc] == ']')
depth--;
}
}
break;
case ']':
if ((cells[dp] & 1) == 1) {
for (int depth = 1; depth;) {
if (pc == 0) {
std::cout << "unbalanced ']'\n";
return 4;
}
pc--;
if (tape[pc] == ']')
depth++;
else if (tape[pc] == '[')
depth--;
}
}
break;
case '.':
out.push_back(cells[dp]);
break;
case ',': // INP: the low 7 bits of the next scripted character
cells[dp] = input.empty() ? 0 : input.front();
if (!input.empty())
input.pop_front();
break;
default: // anything else: not modelled
break;
}
}
if (profile) {
const char *kinds = "><+-[].,";
std::fprintf(stderr, "profile: %zu steps, %zu of them pointer moves (%.1f%%)\n",
steps, walking, 100.0 * double(walking) / double(steps));
for (const char *k = kinds; *k; k++) {
const size_t n = ins_count[(unsigned char)*k];
std::fprintf(stderr, " %c %8zu %5.1f%%\n", *k, n,
100.0 * double(n) / double(steps));
}
std::vector<size_t> order(cell_hits.size());
for (size_t i = 0; i < order.size(); i++)
order[i] = i;
std::sort(order.begin(), order.end(), [&](size_t a, size_t b) {
return cell_hits[a] > cell_hits[b];
});
std::fprintf(stderr, " hottest cells (steps spent there):");
for (size_t i = 0; i < order.size() && i < 10; i++) {
if (cell_hits[order[i]] == 0)
break;
std::fprintf(stderr, " %zu:%zu", order[i], cell_hits[order[i]]);
}
std::fprintf(stderr, "\n");
}
std::cout << "out";
for (uint32_t v : out)
std::cout << " " << int(v);
std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex
<< int(cells[0]) << std::dec << ")\n";
return 0;
}