From 7d259b1c6cd47f424e8caeb5b500f4a7f9da64c7 Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 20:14:49 +0800 Subject: [PATCH] Rename the two different 'CELL_SIZE's so they stop being confused There were two unrelated things under one name: r8asm::ast::CELL_BITS the machine word width in bits, a run time number (import_file(path, 8)), used by %rep and the .xor argument masking r8veritas how many parameter cells a synth block has, a compile time template argument selected by 'params N' The first is now CELL_BITS, the second PARAMS. What a .r8asm program sees does not change: _CELL_SIZE / _CELL_MAX / _ROL_STEP keep their names, because lib.r8asm uses them. Answering the actual question along the way: the machine word width is a run time parameter (import_file's second argument, hardcoded to 8 by the CLI), not a template argument -- the comment in r8asm.hh even apologises for that. The synth block's 'params N' is the other one, and only that goes through the template switch (1..8). The first attempt at the rename also rewrote the _CELL_SIZE string used by %rep into _CELL_BITS, so the lookup threw and the assembler died with a fastfail on any file containing .xor or %rep. Caught by regressing test.r8asm (exit 0xC0000409, no tape); the string is restored and asm/test.r8asm is back to 1666 characters with all seven blocks cached. --- .r8forge_cache/aa1afee0772e05b5.txt | 6 +++ .r8forge_cache/fcf6d47fd7645238.txt | 7 +++ include/r8forge/r8asm.hh | 6 +-- include/r8forge/r8veritas.hh | 68 ++++++++++++++--------------- src/r8asm.cc | 24 +++++----- 5 files changed, 63 insertions(+), 48 deletions(-) create mode 100644 .r8forge_cache/aa1afee0772e05b5.txt create mode 100644 .r8forge_cache/fcf6d47fd7645238.txt diff --git a/.r8forge_cache/aa1afee0772e05b5.txt b/.r8forge_cache/aa1afee0772e05b5.txt new file mode 100644 index 0000000..da59429 --- /dev/null +++ b/.r8forge_cache/aa1afee0772e05b5.txt @@ -0,0 +1,6 @@ +# params 6 mode 1 metric 0 +0 38 0 +1 41 0 +2 51 0 +3 20 1 +5 31 0 diff --git a/.r8forge_cache/fcf6d47fd7645238.txt b/.r8forge_cache/fcf6d47fd7645238.txt new file mode 100644 index 0000000..9c00fc3 --- /dev/null +++ b/.r8forge_cache/fcf6d47fd7645238.txt @@ -0,0 +1,7 @@ +# params 6 mode 0 metric 0 +1 40 0 +0 2 0 +1 13 0 +2 50 0 +3 20 1 +5 30 0 diff --git a/include/r8forge/r8asm.hh b/include/r8forge/r8asm.hh index 2e8e708..a13e4c4 100644 --- a/include/r8forge/r8asm.hh +++ b/include/r8forge/r8asm.hh @@ -103,8 +103,8 @@ private: class ast { public: ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}}; - size_t CELL_SIZE; - // I know template class is the most elegant solution of CELL_SIZE + size_t CELL_BITS; + // I know template class is the most elegant solution of CELL_BITS // but I can't be bother to add that. // The assembler has no exceptions: anything that cannot be assembled @@ -199,7 +199,7 @@ private: ast_node expand_node_to_ins(const ast_node &node); std::string expand_root(); bool import_macro(const std::string ¯o_name, const ast_node &node); - void set_cell_size(size_t cell_size); + void set_cell_bits(size_t cell_bits); }; ast main_tree; diff --git a/include/r8forge/r8veritas.hh b/include/r8forge/r8veritas.hh index 14c983d..1f34b6e 100644 --- a/include/r8forge/r8veritas.hh +++ b/include/r8forge/r8veritas.hh @@ -23,7 +23,7 @@ // Search strategy: // * only the input bits that influence the answer survive (the old // compress_stat_space), so a plane is a 2^K bit truth table instead of -// 2^CELL_SIZE, and the whole state is CELL_SIZE * 2^K bits +// 2^PARAMS, and the whole state is PARAMS * 2^K bits // * forward BFS keeps every reachable state of depth < d plus the block // that produced it // * the last block is not searched forward. A block clears its own @@ -56,10 +56,10 @@ #include #include -template class r8veritas { +template class r8veritas { public: using cell = uint8_t; - using stat = std::array; + using stat = std::array; using stat_ref = const stat *; using truthtable = std::pair; using op = std::function; @@ -85,7 +85,7 @@ public: // set_1. The inter-block hop from the previous sacrifice back to this one // is not in the model -- the emitter charges it afterwards. struct cost_options { - std::array addr{}; // cell address of each plane + std::array addr{}; // cell address of each plane int max_cost = 4096; // give up above this tape length size_t max_states = 600'000'000; unsigned threads = 0; // 0 = one worker per hardware thread @@ -113,16 +113,16 @@ public: std::string message; }; - std::array nicknames; - std::array tp_paths; + std::array nicknames; + std::array tp_paths; void initialize_nicknames() { - for (int i = 0; i < CELL_SIZE; i++) + for (int i = 0; i < PARAMS; i++) nicknames[i] = "%" + std::to_string(i + 1); } void initialize_tp_paths() { - for (int i = 0; i < CELL_SIZE; i++) + for (int i = 0; i < PARAMS; i++) tp_paths[i] = ".tp " + nicknames[i]; } @@ -135,7 +135,7 @@ public: truthtable generate_truthtable(const op &operation) { stat in{}, out{}; - for (size_t i = 0; i < (size_t(1) << CELL_SIZE); i++) { + for (size_t i = 0; i < (size_t(1) << PARAMS); i++) { in[i] = cell(i); out[i] = operation(cell(i)); } @@ -152,13 +152,13 @@ public: // starts from a canonical state. truthtable compress_stat_space(const truthtable &raw) { stat start = *raw.first; - for (int i = 0; i < CELL_SIZE; i++) { + for (int i = 0; i < PARAMS; i++) { bool influences = false; - for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++) + for (size_t j = 0; j < (size_t(1) << PARAMS); j++) if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)]) influences = true; if (!influences) - for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++) + for (size_t j = 0; j < (size_t(1) << PARAMS); j++) start[j] = cell(start[j] & ~(1u << i)); } return {keep(start), raw.second}; @@ -173,7 +173,7 @@ public: for (size_t j = 0; j < cur.size(); j++) if (cur[j] & (1u << i.sacrifice)) { cur[j] = cell(cur[j] & ~(1u << i.sacrifice)); - for (int b = 0; b < CELL_SIZE; b++) + for (int b = 0; b < PARAMS; b++) if (i.mask & (1 << b)) cur[j] = i.or_mode ? cell(cur[j] | (1u << b)) : cell(cur[j] ^ (1u << b)); @@ -189,7 +189,7 @@ public: // ---------------------------------------------------------------- output void output_stat(const stat &s) const { for (auto v : s) - std::cout << std::bitset(v).to_string() << " "; + std::cout << std::bitset(v).to_string() << " "; } void output_ins(const instruction &ins, std::ostream &os, @@ -199,7 +199,7 @@ public: if (ins.mask) { os << (ins.or_mode ? ", SET1: {" : ", FLIP: {"); bool first = true; - for (int b = 0; b < CELL_SIZE; b++) + for (int b = 0; b < PARAMS; b++) if (ins.mask & (1 << b)) { if (!first) os << ','; @@ -213,7 +213,7 @@ public: } os << tp_paths[ins.sacrifice] << "\n"; os << "biz\n"; - for (int b = 0; b < CELL_SIZE; b++) + for (int b = 0; b < PARAMS; b++) if (ins.mask & (1 << b)) { os << '\t' << tp_paths[b] << "\n"; os << '\t' << (ins.or_mode ? "set_1" : "flb") << "\n"; @@ -242,10 +242,10 @@ public: return r; }; - std::array bit_of{}; + std::array bit_of{}; int k = 0; - for (int i = 0; i < CELL_SIZE; i++) - for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++) + for (int i = 0; i < PARAMS; i++) + for (size_t j = 0; j < (size_t(1) << PARAMS); j++) if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)]) { bit_of[k++] = i; break; @@ -289,10 +289,10 @@ public: }; // which input bits matter, in compression order - std::array bit_of{}; + std::array bit_of{}; int k = 0; - for (int i = 0; i < CELL_SIZE; i++) { - for (size_t j = 0; j < (size_t(1) << CELL_SIZE); j++) + for (int i = 0; i < PARAMS; i++) { + for (size_t j = 0; j < (size_t(1) << PARAMS); j++) if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)]) { bit_of[k++] = i; break; @@ -336,10 +336,10 @@ private: // ---------------------------------------------------------------- engine // K relevant inputs: every plane is a 2^K bit truth table, the whole state - // is CELL_SIZE * 2^K bits packed into KEYW words. + // is PARAMS * 2^K bits packed into KEYW words. template struct engine { static constexpr int M = 1 << K; // bits per plane - static constexpr int KEYW = (CELL_SIZE * M + 63) / 64; + static constexpr int KEYW = (PARAMS * M + 63) / 64; static constexpr uint64_t PMASK = (M >= 64) ? ~uint64_t(0) : ((uint64_t(1) << (M & 63)) - 1); using key = std::array; @@ -364,7 +364,7 @@ private: key n = k; const uint64_t c = get(k, i.sacrifice); put(n, i.sacrifice, 0); - for (int b = 0; b < CELL_SIZE; b++) + for (int b = 0; b < PARAMS; b++) if (i.mask & (1 << b)) put(n, b, i.or_mode ? (get(k, b) | c) : (get(k, b) ^ c)); return n; @@ -456,7 +456,7 @@ private: for (size_t i = 0; i < insns.size(); i++) { const instruction &in = insns[i]; int lo = opt.addr[in.sacrifice], hi = lo, cnt = 0; - for (int b = 0; b < CELL_SIZE; b++) + for (int b = 0; b < PARAMS; b++) if (in.mask & (1 << b)) { cnt++; lo = std::min(lo, opt.addr[b]); @@ -470,7 +470,7 @@ private: // start = identity over the compressed inputs, goal = the target key start = E::zero(), goal = E::zero(); for (size_t j = 0; j < full.size(); j++) - for (int b = 0; b < CELL_SIZE; b++) { + for (int b = 0; b < PARAMS; b++) { if (j & (size_t(1) << b)) E::put(start, b, E::get(start, b) | (uint64_t(1) << j)); if ((*raw.second)[full[j]] & (1u << b)) @@ -675,7 +675,7 @@ private: // start = identity over the compressed inputs, goal = the target key start = E::zero(), goal = E::zero(); for (size_t j = 0; j < full.size(); j++) - for (int b = 0; b < CELL_SIZE; b++) { + for (int b = 0; b < PARAMS; b++) { if (j & (size_t(1) << b)) E::put(start, b, E::get(start, b) | (uint64_t(1) << j)); if ((*raw.second)[full[j]] & (1u << b)) @@ -794,10 +794,10 @@ private: const instruction &ins, uint64_t c, typename E::key pre, int b, F &f) { using key = typename E::key; - for (; b < CELL_SIZE; b++) + for (; b < PARAMS; b++) if (ins.mask & (1 << b)) break; - if (b == CELL_SIZE) + if (b == PARAMS) return f(pre); const uint64_t T = E::get(post, b); @@ -821,13 +821,13 @@ private: // ---------------------------------------------------------------- syntax std::vector build_instructions() const { std::vector out; - out.reserve(CELL_SIZE * (2 * (1 << (CELL_SIZE - 1)) + 1)); - for (int s = 0; s < CELL_SIZE; s++) { + out.reserve(PARAMS * (2 * (1 << (PARAMS - 1)) + 1)); + for (int s = 0; s < PARAMS; s++) { out.push_back({uint8_t(s), 0, false}); // pure sacrifice: clear plane s - for (int sub = 1; sub < (1 << (CELL_SIZE - 1)); sub++) { + for (int sub = 1; sub < (1 << (PARAMS - 1)); sub++) { uint8_t m = 0; int bit = 0; - for (int b = 0; b < CELL_SIZE; b++) + for (int b = 0; b < PARAMS; b++) if (b != s) m |= uint8_t(((sub >> bit++) & 1) << b); out.push_back({uint8_t(s), m, false}); diff --git a/src/r8asm.cc b/src/r8asm.cc index 858aaab..fa936e4 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -64,11 +64,13 @@ std::string peel(std::string &str) { // Peel the first word from string. #undef EMPTY_CHARS #undef OPERATORS -void r8asm::ast::set_cell_size(size_t cell_size) { - CELL_SIZE = cell_size; - scopes = {{{"_CELL_SIZE", static_cast(cell_size)}, - {"_CELL_MAX", static_cast((1 << cell_size) - 1)}, - {"_ROL_STEP", static_cast(cell_size - 1)}}}; +void r8asm::ast::set_cell_bits(size_t cell_bits) { + CELL_BITS = cell_bits; + // the names a .r8asm program sees stay as they were: _CELL_SIZE is the + // machine word width in bits (CELL_BITS on the C++ side) + scopes = {{{"_CELL_SIZE", static_cast(cell_bits)}, + {"_CELL_MAX", static_cast((1 << cell_bits) - 1)}, + {"_ROL_STEP", static_cast(cell_bits - 1)}}}; } r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) { @@ -257,10 +259,10 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) { /* XOR children[0]: argument - WARNING: argument bigger than ((1< 5(0b101) - when define CELL_SIZE 2, only the last 2 bit will be noticed, + when define CELL_BITS 2, only the last 2 bit will be noticed, and 5 means 0b01(1) here. then the node will be expanded like this: @@ -436,7 +438,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { filename += tp->value; // now tp points at RQUOTE - import_file(filename, main_tree.CELL_SIZE); + import_file(filename, main_tree.CELL_BITS); // import_file has already said why if it failed; the error flag is // set, so this file will not be reported as assembled. } break; @@ -550,7 +552,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { } file << "%endmacro\n"; file.close(); - import_file(path, main_tree.CELL_SIZE); + import_file(path, main_tree.CELL_BITS); std::remove(path.c_str()); } } break; @@ -855,10 +857,10 @@ bool r8asm::import_file(const std::string &filename, size_t cell_size) { return (false); } - // Before parsing: %include asks main_tree.CELL_SIZE for the width to hand + // Before parsing: %include asks main_tree.CELL_BITS for the width to hand // to the included file, and that used to answer 0 because the size was // only set at the end of this function. - main_tree.set_cell_size(cell_size); + main_tree.set_cell_bits(cell_size); for (const auto &token_it : parse(stream).children) { main_tree.root.children.push_back(token_it);