Files
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

273 lines
8.8 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include "r8forge/geometry.hh"
#include "r8forge/r8veritas.hh"
// %synth: run the searcher on a truth table and return the block list as
// {sacrifice, mask, or_mode} triples. Either `map` (2^K output values, the
// inputs implied to be %1..%K and the outputs the ones after them) or `stat`
// (the whole 2^params state table) says what to compute. `layout` is the cell
// address of each parameter (empty means adjacent, 0..n-1). `metric` picks the
// tape cost model: 1 = layout free (the .tp walk is not counted, one cache
// entry serves every caller), 0 = real characters for this layout. `threads`
// is how many workers the tape search may use; 0 = one per hardware thread.
// `quiet` drops the progress lines (cache hits, search timing); failures are
// still reported.
std::vector<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map,
const std::vector<int> &stat, const std::string &name,
const std::vector<int> &layout, int mode, int metric,
unsigned threads, bool quiet);
class r8asm {
using data = int16_t;
using parameter_index = uint8_t;
using addr = uint32_t;
using general_data = int32_t;
using line_number = unsigned long long;
using depth_t = unsigned long long;
using precedence_t = uint8_t;
private:
enum class token_t {
STRING,
OPERATOR,
MACROCALL,
ROT8,
DIRECTIVE,
PARAMETER,
BUILTIN,
LPAREN,
RPAREN,
LQUOTE,
RQUOTE,
ENDL,
};
struct token {
token_t type;
std::string value;
};
struct token_line {
line_number number;
std::vector<token> tokens;
};
struct token_stream {
std::string filename;
std::vector<token_line> lines;
const std::set<std::string> raw_rot8_ins = {"stp", "btp", "ror", "flb",
"biz", "rnz", "out", "inp"};
token_stream(const std::string &filename) {
this->filename = filename;
std::ifstream file(filename);
if (!file.is_open()) {
ok = false;
return;
}
std::stringstream buf;
buf << file.rdbuf();
lines = lex(buf.str());
}
std::vector<token_line> lex(const std::string &char_stream);
bool ok = true; // false when the file could not be read
};
enum class ast_node_t {
CONSTANT,
PARAMETER,
VARIABLE,
OPERATOR,
BUILTIN,
MACROCALL,
REPEAT,
FOR,
ROT8,
CODEBLOCK,
STRING,
};
struct ast_node {
ast_node_t type;
std::string value;
std::vector<ast_node> children;
};
class ast {
public:
ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}};
size_t cell_size;
// I know template class is the most elegant solution of cell_size
// but I can't be bother to add that.
// How %synth charges a block: 1 = layout free (default), 0 = real tape
// characters for the layout in the source. A library block is called from
// many places, so the default ignores the layout and one cache entry serves
// every call site; `r8forge --synth-chars` flips it for the whole run.
int synth_metric = 1;
// Workers the tape search may use: 0 = one per hardware thread. Only the
// tape search splits (it splits by first block); the layer search is serial.
unsigned synth_threads = 0;
// Stay silent about progress: %synth cache hits, search timing, the
// temp-directory fallback. Errors are still reported -- quiet means "only
// the tape on stdout", not "hide the failure".
bool quiet = false;
// The assembler has no exceptions: anything that cannot be assembled
// sets this flag and says why on stderr.
bool error = false;
void fail(const std::string &message) {
std::cerr << "r8asm: " << message << std::endl;
error = true;
}
// ---- %synth ---------------------------------------------------------
// A synth block is a macro whose body the assembler synthesises from a
// truth table. Parameters are %1..%params; the input ones come first
// (they carry the input value), the rest are outputs and scratch cells.
struct synth_spec {
int params = 0;
std::vector<int> in; // param index per input bit, bit 0 first
std::vector<int> out; // param index per output bit, bit 0 first
std::vector<int> map; // 2^K entries: output bits of that input
std::vector<int> stat; // or the whole table: 2^params values
std::vector<int> layout; // optional: cell address of each parameter
int mode = 0; // 0 = shortest tape, 1 = fewest layers only
};
private:
addr dataptr = 0;
std::vector<std::map<std::string, general_data>> scopes;
std::map<std::string, ast_node> macros;
std::set<std::string> reported; // keep a typo from being printed n times
enum class builtin_t {
ANCHOR,
TP,
XOR,
LOOP,
ENDLOOP,
VAR,
};
const std::map<std::string, builtin_t> builtin_map = {
{"builtin_anchor", builtin_t::ANCHOR},
{"builtin_tp", builtin_t::TP},
{"builtin_xor", builtin_t::XOR},
{"builtin_loop", builtin_t::LOOP},
{"builtin_endloop", builtin_t::ENDLOOP},
{"builtin_var", builtin_t::VAR},
};
enum class rot8_t {
STP, // Step Tape Pointer,'>'
BTP, // Back Tape Pointer,'<'
ROR, // ROtate Right,'-'
FLB, // Flip Last Bit,'+'
BIZ, // Branch If Zero,'['
RNZ, // Return Not Zero,']'
OUT, // OUTput byte,'.'
INP, // INPut byte,','
};
const std::map<std::string, rot8_t> rot8_map = {
{"rot8_stp", rot8_t::STP}, {"rot8_btp", rot8_t::BTP},
{"rot8_ror", rot8_t::ROR}, {"rot8_flb", rot8_t::FLB},
{"rot8_biz", rot8_t::BIZ}, {"rot8_rnz", rot8_t::RNZ},
{"rot8_out", rot8_t::OUT}, {"rot8_inp", rot8_t::INP},
};
general_data expand_node_to_data(const ast_node &node);
std::string expand_node_to_string(const ast_node &node);
public:
enum class operator_t {
ADD,
SUB,
MUL,
DIV,
XOR,
AND,
OR,
NOT,
};
const std::map<std::string, operator_t> operator_map = {
{"op_add", operator_t::ADD}, {"op_sub", operator_t::SUB},
{"op_mul", operator_t::MUL}, {"op_div", operator_t::DIV},
{"op_xor", operator_t::XOR}, {"op_and", operator_t::AND},
{"op_or", operator_t::OR}, {"op_not", operator_t::NOT},
};
const std::map<operator_t, precedence_t> op_precedence_map = {
{operator_t::NOT, 0}, {operator_t::MUL, 1}, {operator_t::DIV, 1},
{operator_t::ADD, 2}, {operator_t::SUB, 2}, {operator_t::AND, 3},
{operator_t::XOR, 4}, {operator_t::OR, 5},
};
ast_node expand_node_to_ins(const ast_node &node);
std::string expand_root();
bool import_macro(const std::string &macro_name, const ast_node &node);
void set_cell_size(size_t cell_size);
};
ast main_tree;
enum class directive_t {
SYNTH,
ENDSYNTH,
MACRO,
ENDMACRO,
REP,
ENDREP,
FOR,
ENDFOR,
INCLUDE,
};
std::map<std::string, directive_t> directive_keywords = {
{"macro", directive_t::MACRO},
{"endmacro", directive_t::ENDMACRO},
{"rep", directive_t::REP}, {"synth", directive_t::SYNTH},
{"endsynth", directive_t::ENDSYNTH},
{"endrep", directive_t::ENDREP},
{"for", directive_t::FOR}, {"endfor", directive_t::ENDFOR},
{"include", directive_t::INCLUDE}};
ast_node parse(const token_stream &stream);
ast_node parse_args(const token_line &line);
ast_node parse_single_arg(const token_line &line);
public:
std::string export_rot8();
bool import_file(const std::string &filename, size_t cell_size);
// %synth cost model for the whole run: 1 (default) = layout free, 0 = real
// tape characters for the layout written in the source. Set it before
// import_file; `r8forge --synth-chars` is the command line spelling.
void set_synth_metric(int metric) { main_tree.synth_metric = metric; }
// Workers the %synth tape search may use; 0 (default) = one per hardware
// thread. `r8forge -j N` is the command line spelling. The layer search is
// serial either way.
void set_synth_threads(unsigned threads) {
main_tree.synth_threads = threads;
}
// Drop the progress chatter (cache hits, search timing, the temp-directory
// fallback) and keep only the tape on stdout. Errors are still reported;
// `r8forge --quiet` is the command line spelling.
void set_quiet(bool quiet) { main_tree.quiet = quiet; }
// True once anything (a missing file, an unknown name, ...) has failed.
bool failed() const { return main_tree.error; }
};