tape_run --trace, and the dp rule that killed the two cell increment
r8forge_tape_run grew a `--trace` flag: it prints pc, instruction, dp and the first four cells before every step. Without it a wrong hand written macro is just "the answer is wrong"; with it the failure is one line, e.g. dp jumping to 1023 because a backwards move was emitted where the runtime was already there. That is what happened to the two cell in place increment I tried after the in place complement. It assembled to 155 characters (against 430 for the slice version) and got 129 of 256 inputs right -- every input whose low bit is 0, i.e. every input that never propagates a carry. The algorithm was fine; the macros were not. The rule it broke: dp is a compile time constant in r8asm while a `[ ... ]` body may be skipped at run time, so **every conditional body has to come back to its own condition cell** (net zero moves). Otherwise the taken and the skipped path leave dp in different places and every later `.tp` is off by one. The library already obeys this: set_0's body never moves, clb_bit_mov ends with `.tp before` and a `flb` that eats the condition on the way out. For a two cell in place add both candidate condition cells hold live data (A's bit is the result, the carry cell's bit is the carry), so each test first has to make its condition consumable, which means moving a bit through the spare bits of the carry cell and rotating it into place -- about eight characters of detour per bit. A correct version should land near 200-220 characters, still about half of the 430 character slice version, but it stays a hand written loop with subtle invariants. Not added to the library yet; the scratch files are gone. AGENTS.md now carries the dp rule under the instruction semantics section, next to the two `set_0` / `%synth` constraints it belongs with.
This commit is contained in:
1 parent
eedd9a33a6
commit
349b6d2748
2 files changed
+19
-2
No files matched your search
@@ -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 的
|
||||
整格零判断推出来的,**错的**,已删。
|
||||
|
||||
|
||||
+13
-2
@@ -30,8 +30,13 @@ int main(int argc, char **argv) {
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
std::vector<uint8_t> 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) {
|
||||
|
||||
Reference in new issue
Block a user