diff --git a/AGENTS.md b/AGENTS.md index b834b6f..6cf728e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -34,7 +34,8 @@ r8forge asm/test.r8asm # 64513 字符(除 raw_puts_dyn r8forge_byte_op_test # 21 项(divmod 16384 组、digits 2048、undigits 4096) r8forge_examples_test # 18 个 example + asm/test.r8asm,核对头部 `; out`/`; in` r8forge_macro_docs_test # 53 macros in 23 files -r8forge_tape_opt in.rot8 out.rot8 # 纸带窥孔优化:>< 净位移、+ mod W、- mod 2(全家桶 −10%) +r8forge --opt asm/test.r8asm # 纸带窥孔优化 64513 -> 58060(>< 净位移、+ mod W、- mod 2) +r8forge_tape_opt in.rot8 out.rot8 # 同一优化,但作用在已有纸带上 r8forge_synth_test examples/full_add.r8asm # all eight combinations ok r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0 powershell -File tests\widths.ps1 # 位宽矩阵:每个位宽一套工具链 + 断言(约 20 分钟) diff --git a/CMakeLists.txt b/CMakeLists.txt index 10c7508..b7727dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ add_compile_definitions(R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE}) find_package(Threads REQUIRED) -add_library(r8forge_core src/r8asm.cc src/synth.cc) +add_library(r8forge_core src/r8asm.cc src/synth.cc src/tape_opt.cc) target_link_libraries(r8forge_core PUBLIC Threads::Threads) # 别再 set(CMAKE_CXX_FLAGS ...) 了:那会把默认 flag 整个换掉,MSVC 下丢 /EHsc、 @@ -50,6 +50,7 @@ target_link_libraries(r8forge_examples_test r8forge_core) # The tape level peephole pass: three run-local rules, no bracket bookkeeping. add_executable(r8forge_tape_opt tests/tape_opt.cc) +target_link_libraries(r8forge_tape_opt r8forge_core) add_executable(r8forge_cost_par tests/cost_par.cc) target_link_libraries(r8forge_cost_par r8forge_core) diff --git a/README.md b/README.md index 342ec77..543347d 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ r8forge asm/test.r8asm # 汇编,stdout 是纸带 r8forge --synth-chars examples/full_add.r8asm # 按真实布局重新搜 r8forge -j 4 examples/full_add.r8asm # 纸带搜索最多 4 个 worker(默认每硬件线程一个) r8forge -q examples/full_add.r8asm > full_add.rot8 # stdout 只剩纸带 +r8forge --opt asm/test.r8asm > t.rot8 # 纸带窥孔优化:64513 -> 58060 字符 ``` `-j` 只影响纸带搜索(层数搜索是串行的);`-q` 只压 stdout,错误照旧进 stderr、退出码不变。 diff --git a/TODO.md b/TODO.md index 355aada..faec8ff 100644 --- a/TODO.md +++ b/TODO.md @@ -22,7 +22,7 @@ ## 工具 -* ~~`r8forge --opt`~~ 已实现为 **`r8forge_tape_opt`**(见下),原条目保留说明:`><`、`<>`、`--`、11 个 `++`、 + 重复清位…… 要能重算跳转偏移。配套 `r8forge_tape_diff`(比较两条纸带的 OUT 流和步数) 证明等价 —— 一次改动全库受益。 * **`--profile`**:统计每段宏/每个 cell 的步数占比和 `.tp` 行走距离,用数据决定下一步优化谁 diff --git a/include/r8forge/tape_opt.hh b/include/r8forge/tape_opt.hh new file mode 100644 index 0000000..1a7feb5 --- /dev/null +++ b/include/r8forge/tape_opt.hh @@ -0,0 +1,24 @@ +#ifndef R8FORGE_TAPE_OPT_HPP +#define R8FORGE_TAPE_OPT_HPP + +#include +#include + +/* The tape level peephole pass. Three run-local rules: + * + * > < a maximal run of pointer moves becomes its net displacement + * + a run becomes (length % cell_size): a whole turn is a no-op + * - a run becomes (length % 2): two flips cancel + * + * It needs no bracket bookkeeping: '[' and ']' are not in any of the three + * character classes, so they always end a run, and keeping each run's net keeps + * the data pointer identical at every surviving character -- brackets included -- + * so the pairing the machine finds by scanning cannot change. Bracket + * characters are never added or removed either, so a run can never be merged + * across one. + * + * Non-instruction characters are dropped (they do nothing at run time). + */ +std::string rot8_optimize(const std::string &tape, size_t cell_size); + +#endif diff --git a/src/tape_opt.cc b/src/tape_opt.cc new file mode 100644 index 0000000..85b0257 --- /dev/null +++ b/src/tape_opt.cc @@ -0,0 +1,62 @@ +#include "r8forge/tape_opt.hh" + +#include + +namespace { + +constexpr char kGt = '>'; +constexpr char kLt = '<'; +constexpr char kRor = '+'; +constexpr char kFlb = '-'; + +bool is_ins(char c) { return c != 0 && std::strchr("><+-[].,", c) != nullptr; } + +} // namespace + +std::string rot8_optimize(const std::string &tape, size_t cell_size) { + const size_t width = cell_size == 0 ? 1 : cell_size; + const size_t n = tape.size(); + std::string out; + out.reserve(n); + for (size_t i = 0; i < n;) { + const char c = tape[i]; + if (!is_ins(c)) { + i++; // a stray byte (newline, comment) does nothing; drop it + continue; + } + if (c == kGt || c == kLt) { + long net = 0; + while (i < n && (tape[i] == kGt || tape[i] == kLt)) { + net += tape[i] == kGt ? 1 : -1; + i++; + } + const char dir = net >= 0 ? kGt : kLt; + const long count = net >= 0 ? net : -net; + for (long k = 0; k < count; k++) { + out += dir; + } + continue; + } + if (c == kRor) { + size_t run = 0; + while (i < n && tape[i] == kRor) { + run++; + i++; + } + out.append(run % width, kRor); + continue; + } + if (c == kFlb) { + size_t run = 0; + while (i < n && tape[i] == kFlb) { + run++; + i++; + } + out.append(run % 2, kFlb); + continue; + } + out += c; + i++; + } + return out; +} diff --git a/tests/asm_test.cc b/tests/asm_test.cc index 864c193..b0f5c8d 100644 --- a/tests/asm_test.cc +++ b/tests/asm_test.cc @@ -16,7 +16,9 @@ // -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 #include #include @@ -25,7 +27,7 @@ namespace { void usage() { - std::cerr << "usage: r8forge [--synth-chars] [-j N] [-q] \n"; + std::cerr << "usage: r8forge [--synth-chars] [-j N] [-q] [--opt] \n"; } // 0 is allowed (one worker per hardware thread); absurd counts are not. @@ -44,6 +46,19 @@ bool parse_count(const std::string &text, unsigned &out) { } // 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 @@ -93,6 +108,9 @@ int main(int argc, char **argv) { 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; diff --git a/tests/tape_opt.cc b/tests/tape_opt.cc index c11c682..8ef2968 100644 --- a/tests/tape_opt.cc +++ b/tests/tape_opt.cc @@ -1,20 +1,11 @@ -// r8forge_tape_opt -- the tape level peephole pass. +// r8forge_tape_opt -- the peephole pass as a filter for an existing .rot8 file. // // r8forge_tape_opt [out.rot8] // -// Three run-local rules. None of them needs to know where the brackets are, -// because '[' and ']' are not in any of the three character classes, so they -// always end a run: -// -// > < a maximal run of pointer moves becomes its net displacement -// + a run becomes (length % cell_size): a whole turn is a no-op -// - a run becomes (length % 2): two flips cancel -// -// A run never crosses another character: `> + >` is not `>> +`, the rotation -// would land on a different cell. Preserving the net of every run means the -// data pointer is unchanged at every surviving character (brackets included), -// so the pairing found by scanning at run time cannot change either. +// `r8forge --opt file.r8asm` does the same thing one step earlier; this exists +// for tapes that were assembled before, or by something else. #include "r8forge/geometry.hh" +#include "r8forge/tape_opt.hh" #include #include @@ -22,65 +13,6 @@ #include #include -namespace { - -constexpr char kGt = '>'; -constexpr char kLt = '<'; -constexpr char kRor = '+'; -constexpr char kFlb = '-'; - -bool is_ins(char c) { return c != 0 && std::strchr("><+-[].,", c) != nullptr; } - -std::string optimize(const std::string &tape) { - const size_t width = kDefaultCellSize; - const size_t n = tape.size(); - std::string out; - out.reserve(n); - for (size_t i = 0; i < n;) { - const char c = tape[i]; - if (!is_ins(c)) { - i++; // a stray byte (newline, comment) does nothing; drop it - continue; - } - if (c == kGt || c == kLt) { - long net = 0; - while (i < n && (tape[i] == kGt || tape[i] == kLt)) { - net += tape[i] == kGt ? 1 : -1; - i++; - } - const char dir = net >= 0 ? kGt : kLt; - const long count = net >= 0 ? net : -net; - for (long k = 0; k < count; k++) { - out += dir; - } - continue; - } - if (c == kRor) { - size_t run = 0; - while (i < n && tape[i] == kRor) { - run++; - i++; - } - out.append(run % width, kRor); - continue; - } - if (c == kFlb) { - size_t run = 0; - while (i < n && tape[i] == kFlb) { - run++; - i++; - } - out.append(run % 2, kFlb); - continue; - } - out += c; - i++; - } - return out; -} - -} // namespace - int main(int argc, char **argv) { if (argc < 2) { std::fprintf(stderr, "usage: r8forge_tape_opt [out.rot8]\n"); @@ -93,18 +25,11 @@ int main(int argc, char **argv) { } const std::string tape((std::istreambuf_iterator(in)), std::istreambuf_iterator()); - std::string filtered; - filtered.reserve(tape.size()); - for (char c : tape) { - if (is_ins(c)) { - filtered += c; - } - } - if (filtered.empty()) { + const std::string out = rot8_optimize(tape, kDefaultCellSize); + if (out.empty()) { std::fprintf(stderr, "no instructions in %s\n", argv[1]); return 1; } - const std::string out = optimize(filtered); if (argc > 2) { std::ofstream f(argv[2], std::ios::binary); if (!f) { @@ -115,11 +40,8 @@ int main(int argc, char **argv) { } else { std::fwrite(out.data(), 1, out.size(), stdout); } - const size_t before = filtered.size(); - const size_t after = out.size(); - std::fprintf(stderr, - "r8forge_tape_opt: %zu -> %zu characters (%ld saved, %.1f%%)\n", - before, after, (long)(before - after), - 100.0 * (double)(before - after) / (double)before); + std::fprintf(stderr, "r8forge_tape_opt: %zu -> %zu characters\n", + tape.size(), out.size()); return 0; } +