The pass moved out of tests/tape_opt.cc into src/tape_opt.cc with a header (rot8_optimize(tape, cell_size)), so both entry points share one implementation: r8forge --opt file.r8asm > t.rot8 assemble, then optimise in memory r8forge_tape_opt t.rot8 t2.rot8 optimise a tape assembled earlier --opt is peeled off argv before the rest of the parsing, so it can appear anywhere and the existing flags are untouched; without it the output is byte for byte what it was (flagship 64513). Measured: flagship 64513 -> 58060 (-10.0%), calculator 62585 -> 54863 (-12.3%), and "12 34 + ." still prints the same out stream from the optimised calculator. ctest 8/8 still green.
25 lines
921 B
C++
25 lines
921 B
C++
#ifndef R8FORGE_TAPE_OPT_HPP
|
|
#define R8FORGE_TAPE_OPT_HPP
|
|
|
|
#include <cstddef>
|
|
#include <string>
|
|
|
|
/* 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
|