Files
R8Forge/tests/asm_test.cc
T
dsh eacf6c01da r8forge: add -q / --quiet for a tape-only stdout
Assembling a library file prints one `%synth ...: cached, N blocks` line per
block (21 of them for asm/test.r8asm), plus search timings and the occasional
temp-directory notice.  That is useful when working on the searcher and noise
when the tape is the output:

    r8forge -q examples/full_add.r8asm > full_add.rot8

-q keeps stdout to the tape alone: cache hits, the layer/tape search lines and
the "temp dir is not writable, using ..." fallback are silenced.  Errors are
not: a missing file, an unknown macro, no program up to N layers, a tape search
that fell back to the layer answer and "cannot write the synthesised macro
anywhere" all still go to stderr with the same exit code.  Quiet means "don't
natter", not "hide the failure".

Implementation follows the existing per-run switches: ast::quiet, the
r8asm::set_quiet(bool) setter, one more parameter on r8asm_synth_program (the
prints live in synth.cc) and the temp-directory notice in r8asm.cc.  The CLI
parses -q and --quiet anywhere among the arguments.

Verified: -q examples/full_add.r8asm and -q asm/test.r8asm produce byte
identical tapes to the normal run (95 / 58722 characters), stderr drops from 1
line / 21 lines to 0 bytes, and -q on a missing file or an unknown macro still
prints the error and returns 1.

SPEC 3.7, the README cost-model section and the AGENTS %synth list document it.
2026-11-08 00:24:51 +08:00

101 lines
3.0 KiB
C++

// The assembler CLI.
//
// r8forge [--synth-chars] [-j N] [-q] <file.r8asm>
//
// %synth charges a block for a layout free tape by default: the block is
// copied to many call sites, so the .tp walk is not part of the search and one
// cache entry serves every layout. --synth-chars charges the real characters
// for the layout written in the source instead (much slower, and the result is
// only optimal for that layout). It is a property of the run, not of a block,
// which is why there is no `metric` line inside %synth any more.
//
// -j N (or --synth-threads N) caps the workers of the %synth tape search; 0
// (the default) means one per hardware thread. Only the tape search is
// parallel -- the layer search is serial whatever this says.
//
// -q (or --quiet) keeps stdout to the tape alone: no cache hit lines, no
// search timing, no temp-directory notices. Errors still go to stderr, so a
// broken program is not silent.
#include "r8forge/r8asm.hh"
#include <cerrno>
#include <cstdlib>
#include <iostream>
#include <string>
namespace {
void usage() {
std::cerr << "usage: r8forge [--synth-chars] [-j N] [-q] <file.r8asm>\n";
}
// 0 is allowed (one worker per hardware thread); absurd counts are not.
bool parse_count(const std::string &text, unsigned &out) {
if (text.empty())
return false;
char *end = nullptr;
errno = 0;
const unsigned long value = std::strtoul(text.c_str(), &end, 10);
if (end == nullptr || *end != '\0' || errno != 0 || value > 4096)
return false;
out = unsigned(value);
return true;
}
} // namespace
int main(int argc, char **argv) {
const char *file = nullptr;
int metric = 1; // layout free
unsigned threads = 0; // one worker per hardware thread
bool quiet = false;
for (int i = 1; i < argc; i++) {
const std::string arg = argv[i];
if (arg == "--synth-chars") {
metric = 0;
} else if (arg == "--synth-flat") {
metric = 1;
} else if (arg == "-q" || arg == "--quiet") {
quiet = true;
} else if (arg == "-h" || arg == "--help") {
usage();
return (0);
} else if (arg == "-j" || arg == "--synth-threads") {
if (i + 1 >= argc || !parse_count(argv[++i], threads)) {
std::cerr << "'" << arg << "' wants a thread count\n";
usage();
return (2);
}
} else if (arg.rfind("--synth-threads=", 0) == 0) {
if (!parse_count(arg.substr(16), threads)) {
std::cerr << "bad thread count in '" << arg << "'\n";
usage();
return (2);
}
} else if (arg.size() > 1 && arg[0] == '-') {
std::cerr << "unknown option '" << arg << "'\n";
usage();
return (2);
} else if (file == nullptr) {
file = argv[i];
} else {
usage();
return (2);
}
}
if (file == nullptr) {
usage();
return (2);
}
r8asm a;
a.set_synth_metric(metric);
a.set_synth_threads(threads);
a.set_quiet(quiet);
if (!a.import_file(file, kDefaultCellSize))
return (1);
std::string tape = a.export_rot8();
if (a.failed())
return (1);
std::cout << tape;
return (0);
}