diff --git a/AGENTS.md b/AGENTS.md index e916390..3af8fdb 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -461,6 +461,10 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL (`r8veritas_phase.hh` 的 `search_tt`),所以多 worker 安全;**层数搜索是串行的**。 搜完在 stderr 那行末尾会打 `, N threads`,可以直接确认参数生效(实测 `-j 1` → 1、 `-j 4` → 4、`-j 0` → 32(本机硬件线程数))。 +* **`-q` / `--quiet`**:stdout 只剩纸带,cache 命中 / 搜索计时 / 临时目录回退都不打; + **错误照旧进 stderr**、退出码不变(quiet 不藏故障)。C++ 侧是 `r8asm::set_quiet(bool)`。 + 实测 `-q asm/test.r8asm`:21 行 stderr → 0,纸带逐字节相同;`-q does_not_exist.r8asm` + 仍然打 `cannot open` 并返回 1。 * 缓存代价参考:`bit_raw_add` 的 flat 搜索 990,636,137 状态 / 680 秒 / ~22 GB 内存。 层数上限现在是 8(原来是 5,5 会把一些可行的表误判成不可能)。 diff --git a/README.md b/README.md index 60b107c..e1581c3 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,13 @@ r8forge -j 4 examples/full_add.r8asm # 纸带搜索最多 4 个 worker 层数搜索是串行的,这个参数对它没用(它本来就只有秒级)。 +`-q`(`--quiet`)把 stdout 缩到只剩纸带:cache 命中、搜索计时、临时目录提示都不打, +错误还是照旧进 stderr: + +```sh +r8forge -q examples/full_add.r8asm > full_add.rot8 # 管道里只有纸带 +``` + ## 从零开始 diff --git a/SPEC.md b/SPEC.md index 2c3cc5a..d9f25a9 100644 --- a/SPEC.md +++ b/SPEC.md @@ -206,6 +206,11 @@ worker 数,`0`(默认)是每个硬件线程一个。纸带搜索按"第一 `r8veritas_phase.hh` 的 `search_tt`),所以多 worker 是安全的;**层数搜索是串行的**,这个 参数对它没用(它本来就只有秒级)。`r8asm::set_synth_threads(unsigned)` 是 C++ 侧的开关。 +**`-q` / `--quiet`**:stdout 只剩纸带 —— `%synth` 的 cache 命中、层数/纸带搜索的计时、 +临时目录回退的提示都不打。**错误照旧打到 stderr**(缺文件、未知宏、搜不出解、写不了临时 +文件),退出码也不变:quiet 是"别啰嗦",不是"藏故障"。C++ 侧是 +`r8asm::set_quiet(bool)`。 + --- ## 4. 标准库 `asm/` —— 总则 diff --git a/include/r8forge/r8asm.hh b/include/r8forge/r8asm.hh index 81d4b06..3211692 100644 --- a/include/r8forge/r8asm.hh +++ b/include/r8forge/r8asm.hh @@ -21,12 +21,14 @@ // 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> r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, const std::vector &layout, int mode, int metric, - unsigned threads); + unsigned threads, bool quiet); class r8asm { using data = int16_t; @@ -123,6 +125,11 @@ private: // 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; @@ -255,6 +262,10 @@ public: 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; } }; diff --git a/src/r8asm.cc b/src/r8asm.cc index 1a653f2..3c38668 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -587,7 +587,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { auto prog = r8asm_synth_program(spec.params, spec.in, spec.out, spec.map, spec.stat, name, spec.layout, spec.mode, main_tree.synth_metric, - main_tree.synth_threads); + main_tree.synth_threads, main_tree.quiet); if (prog.empty()) { main_tree.fail("%synth '" + name + "': nothing emitted"); } else { @@ -637,7 +637,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { file.close(); if (!file) continue; - if (d > 0) + if (d > 0 && !main_tree.quiet) std::cerr << "r8asm: %synth " << name << ": " << dirs.front().string() << " is not writable, using " << dirs[d].string() diff --git a/src/synth.cc b/src/synth.cc index 8e780bc..b8151ed 100644 --- a/src/synth.cc +++ b/src/synth.cc @@ -17,7 +17,7 @@ std::vector> r8asm_synth_run(const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, const std::vector &layout, - int mode, int metric, unsigned threads) { + int mode, int metric, unsigned threads, bool quiet) { using V = r8veritas

; V v; v.initialize_nicknames(); @@ -112,8 +112,9 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, std::vector> cached; if (read_cache(cached)) { - std::cerr << "r8asm: %synth " << name << ": cached, " - << cached.size() << " blocks\n"; + if (!quiet) + std::cerr << "r8asm: %synth " << name << ": cached, " + << cached.size() << " blocks\n"; return cached; } @@ -128,9 +129,11 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, << opt.max_depth << " layers\n"; return {}; } - std::cerr << "r8asm: %synth " << name << ": " << layers.program.size() - << " layers" << (layers.proven_optimal ? " (shortest)" : "") - << ", " << layers.states << " states, " << layers.seconds << "s\n"; + if (!quiet) + std::cerr << "r8asm: %synth " << name << ": " << layers.program.size() + << " layers" << (layers.proven_optimal ? " (shortest)" : "") + << ", " << layers.states << " states, " << layers.seconds + << "s\n"; if (mode == 1) { // fewest layers only: skip the tape search for (const auto &i : layers.program) @@ -190,10 +193,11 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, write_cache(out_ins); return out_ins; } - std::cerr << "r8asm: %synth " << name << ": tape cost " << tape.cost << " in " - << tape.prog.size() << " blocks (layer answer was " << bound << "), " - << tape.states << " states, " << tape_seconds << "s, " << workers - << " threads\n"; + if (!quiet) + std::cerr << "r8asm: %synth " << name << ": tape cost " << tape.cost + << " in " << tape.prog.size() << " blocks (layer answer was " + << bound << "), " << tape.states << " states, " << tape_seconds + << "s, " << workers << " threads\n"; for (const auto &i : tape.prog) out_ins.push_back({i[0], i[1], i[2]}); write_cache(out_ins); @@ -205,7 +209,7 @@ r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, const std::vector &layout, int mode, int metric, - unsigned threads) { + unsigned threads, bool quiet) { if (params < 1 || params > 8) { std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n"; return {}; @@ -225,21 +229,21 @@ r8asm_synth_program(int params, const std::vector &in, } switch (params) { case 1: - return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode, metric, threads, quiet); case 2: - return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode, metric, threads, quiet); case 3: - return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode, metric, threads, quiet); case 4: - return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode, metric, threads, quiet); case 5: - return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode, metric, threads, quiet); case 6: - return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode, metric, threads, quiet); case 7: - return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode, metric, threads, quiet); default: - return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode, metric, threads); + return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode, metric, threads, quiet); } } diff --git a/tests/asm_test.cc b/tests/asm_test.cc index 8755f3b..864c193 100644 --- a/tests/asm_test.cc +++ b/tests/asm_test.cc @@ -1,6 +1,6 @@ // The assembler CLI. // -// r8forge [--synth-chars] [-j N] +// r8forge [--synth-chars] [-j N] [-q] // // %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 @@ -12,6 +12,10 @@ // -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 #include @@ -21,7 +25,7 @@ namespace { void usage() { - std::cerr << "usage: r8forge [--synth-chars] [-j N] \n"; + std::cerr << "usage: r8forge [--synth-chars] [-j N] [-q] \n"; } // 0 is allowed (one worker per hardware thread); absurd counts are not. @@ -43,12 +47,15 @@ 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); @@ -82,6 +89,7 @@ int main(int argc, char **argv) { 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();