std::strcmp needs it; MSVC pulls it in transitively so the Windows build never noticed, but GCC 16 rejects the file. Found while trying to build a 32 bit toolchain on a Linux box to time the %synth cache.
120 lines
3.6 KiB
C++
120 lines
3.6 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/geometry.hh"
|
|
#include "r8forge/r8asm.hh"
|
|
#include "r8forge/tape_opt.hh"
|
|
#include <cerrno>
|
|
#include <cstdlib>
|
|
#include <cstring> // std::strcmp -- MSVC pulls it in transitively, GCC does not
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
namespace {
|
|
|
|
void usage() {
|
|
std::cerr << "usage: r8forge [--synth-chars] [-j N] [-q] [--opt] <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) {
|
|
// --opt may appear anywhere: peel it off, leave the rest of the parsing alone
|
|
bool opt = false;
|
|
{
|
|
int keep = 0;
|
|
for (int i = 1; i < argc; i++) {
|
|
if (std::strcmp(argv[i], "--opt") == 0) {
|
|
opt = true;
|
|
continue;
|
|
}
|
|
argv[++keep] = argv[i];
|
|
}
|
|
argc = keep + 1;
|
|
}
|
|
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 (opt && !a.failed()) {
|
|
tape = rot8_optimize(tape, kDefaultCellSize);
|
|
}
|
|
if (a.failed())
|
|
return (1);
|
|
std::cout << tape;
|
|
return (0);
|
|
}
|