diff --git a/AGENTS.md b/AGENTS.md index 89d0941..62c7190 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -104,6 +104,12 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } } 2. **合成的程序只碰每个 cell 的低 K 位**(K = 参与运算的输入位数),高位不追踪。 所以 `%synth` 出来的块只适用于"每格一位"的世界。 +**写手写宏还有第三条**:`dp` 是**汇编期**算的常量,而 `[...]` 的 body 在运行期可能被跳过。 +所以**每个条件块的身体必须回到它自己的条件 cell**(净位移 0),否则跳过路径和进入路径的 +`dp` 会不一致,之后所有 `.tp` 全部错位 —— 症状是 `dp` 跑到 1023 这种地方( +`r8forge_tape_run --trace` 能直接看到)。库里的宏都遵守:`set_0` 的身体不移动、`clb_bit_mov` +的身体最后 `.tp before` 再 `flb`(顺便把条件吃掉)。 + 曾经这里还写过第三条("多字节 cell 的位读不出来")—— 那是把 `biz` 当成 Brainfuck 的 整格零判断推出来的,**错的**,已删。 diff --git a/tests/tape_run.cc b/tests/tape_run.cc index b6896af..fc81ace 100644 --- a/tests/tape_run.cc +++ b/tests/tape_run.cc @@ -30,8 +30,13 @@ int main(int argc, char **argv) { std::istreambuf_iterator()); std::vector cells(1024, 0); + bool trace = false; for (int i = 2; i < argc; i++) { const std::string a = argv[i]; + if (a == "--trace" || a == "-t") { + trace = true; + continue; + } const size_t eq = a.find('='); if (eq == std::string::npos) continue; @@ -51,6 +56,13 @@ int main(int argc, char **argv) { << ")\n"; return 3; } + if (trace) { + // cells 0..3 are the interesting ones when debugging a library macro + std::cout << "pc " << pc << " " << tape[pc] << " dp " << dp << " ["; + for (size_t i = 0; i < 4; i++) + std::cout << (i ? " " : "") << int(cells[i]); + std::cout << "]\n"; + } switch (tape[pc]) { case '>': dp = (dp + 1) % cells.size(); @@ -64,8 +76,7 @@ int main(int argc, char **argv) { case '+': // ror cells[dp] = uint8_t((cells[dp] >> 1) | (cells[dp] << 7)); break; - // biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz - // src/rot8.c. Reading them as a Brainfuck style zero test makes every + // biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz // src/rot8.c. Reading them as a Brainfuck style zero test makes every // byte level program in the library look like an endless loop. case '[': if ((cells[dp] & 1) == 0) {