// r8forge_tape_opt -- the peephole pass as a filter for an existing .rot8 file. // // r8forge_tape_opt [out.rot8] // // `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 #include #include #include int main(int argc, char **argv) { if (argc < 2) { std::fprintf(stderr, "usage: r8forge_tape_opt [out.rot8]\n"); return 2; } std::ifstream in(argv[1], std::ios::binary); if (!in) { std::fprintf(stderr, "could not open %s\n", argv[1]); return 1; } const std::string tape((std::istreambuf_iterator(in)), std::istreambuf_iterator()); const std::string out = rot8_optimize(tape, kDefaultCellSize); if (out.empty()) { std::fprintf(stderr, "no instructions in %s\n", argv[1]); return 1; } if (argc > 2) { std::ofstream f(argv[2], std::ios::binary); if (!f) { std::fprintf(stderr, "could not write %s\n", argv[2]); return 1; } f << out; } else { std::fwrite(out.data(), 1, out.size(), stdout); } std::fprintf(stderr, "r8forge_tape_opt: %zu -> %zu characters\n", tape.size(), out.size()); return 0; }