diff --git a/include/r8forge/r8asm.hh b/include/r8forge/r8asm.hh index a13e4c4..2e8e708 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_BITS; - // I know template class is the most elegant solution of CELL_BITS + size_t CELL_SIZE; + // I know template class is the most elegant solution of CELL_SIZE // 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_bits(size_t cell_bits); + void set_cell_size(size_t cell_size); }; ast main_tree; diff --git a/include/r8forge/r8veritas.hh b/include/r8forge/r8veritas.hh index 1f34b6e..737d69f 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^PARAMS, and the whole state is PARAMS * 2^K bits +// 2^CELL_ARGC, and the whole state is CELL_ARGC * 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 < PARAMS; i++) + for (int i = 0; i < CELL_ARGC; i++) nicknames[i] = "%" + std::to_string(i + 1); } void initialize_tp_paths() { - for (int i = 0; i < PARAMS; i++) + for (int i = 0; i < CELL_ARGC; 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) << PARAMS); i++) { + for (size_t i = 0; i < (size_t(1) << CELL_ARGC); 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 < PARAMS; i++) { + for (int i = 0; i < CELL_ARGC; i++) { bool influences = false; - for (size_t j = 0; j < (size_t(1) << PARAMS); j++) + for (size_t j = 0; j < (size_t(1) << CELL_ARGC); 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) << PARAMS); j++) + for (size_t j = 0; j < (size_t(1) << CELL_ARGC); 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 < PARAMS; b++) + for (int b = 0; b < CELL_ARGC; 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 < PARAMS; b++) + for (int b = 0; b < CELL_ARGC; 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 < PARAMS; b++) + for (int b = 0; b < CELL_ARGC; 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 < PARAMS; i++) - for (size_t j = 0; j < (size_t(1) << PARAMS); j++) + for (int i = 0; i < CELL_ARGC; i++) + for (size_t j = 0; j < (size_t(1) << CELL_ARGC); 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 < PARAMS; i++) { - for (size_t j = 0; j < (size_t(1) << PARAMS); j++) + for (int i = 0; i < CELL_ARGC; i++) { + for (size_t j = 0; j < (size_t(1) << CELL_ARGC); 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 PARAMS * 2^K bits packed into KEYW words. + // is CELL_ARGC * 2^K bits packed into KEYW words. template struct engine { static constexpr int M = 1 << K; // bits per plane - static constexpr int KEYW = (PARAMS * M + 63) / 64; + static constexpr int KEYW = (CELL_ARGC * 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 < PARAMS; b++) + for (int b = 0; b < CELL_ARGC; 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 < PARAMS; b++) + for (int b = 0; b < CELL_ARGC; 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 < PARAMS; b++) { + for (int b = 0; b < CELL_ARGC; 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 < PARAMS; b++) { + for (int b = 0; b < CELL_ARGC; 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 < PARAMS; b++) + for (; b < CELL_ARGC; b++) if (ins.mask & (1 << b)) break; - if (b == PARAMS) + if (b == CELL_ARGC) 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(PARAMS * (2 * (1 << (PARAMS - 1)) + 1)); - for (int s = 0; s < PARAMS; s++) { + out.reserve(CELL_ARGC * (2 * (1 << (CELL_ARGC - 1)) + 1)); + for (int s = 0; s < CELL_ARGC; s++) { out.push_back({uint8_t(s), 0, false}); // pure sacrifice: clear plane s - for (int sub = 1; sub < (1 << (PARAMS - 1)); sub++) { + for (int sub = 1; sub < (1 << (CELL_ARGC - 1)); sub++) { uint8_t m = 0; int bit = 0; - for (int b = 0; b < PARAMS; b++) + for (int b = 0; b < CELL_ARGC; 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 fa936e4..52a4f5c 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -64,13 +64,13 @@ std::string peel(std::string &str) { // Peel the first word from string. #undef EMPTY_CHARS #undef OPERATORS -void r8asm::ast::set_cell_bits(size_t cell_bits) { - CELL_BITS = cell_bits; +void r8asm::ast::set_cell_size(size_t cell_size) { + CELL_SIZE = cell_size; // 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)}}}; + // machine word width in bits (CELL_SIZE on the C++ side) + scopes = {{{"_CELL_SIZE", static_cast(cell_size)}, + {"_CELL_MAX", static_cast((1 << cell_size) - 1)}, + {"_ROL_STEP", static_cast(cell_size - 1)}}}; } r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) { @@ -259,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_BITS 2, only the last 2 bit will be noticed, + when define CELL_SIZE 2, only the last 2 bit will be noticed, and 5 means 0b01(1) here. then the node will be expanded like this: @@ -438,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_BITS); + import_file(filename, main_tree.CELL_SIZE); // import_file has already said why if it failed; the error flag is // set, so this file will not be reported as assembled. } break; @@ -552,7 +552,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { } file << "%endmacro\n"; file.close(); - import_file(path, main_tree.CELL_BITS); + import_file(path, main_tree.CELL_SIZE); std::remove(path.c_str()); } } break; @@ -857,10 +857,10 @@ bool r8asm::import_file(const std::string &filename, size_t cell_size) { return (false); } - // Before parsing: %include asks main_tree.CELL_BITS for the width to hand + // Before parsing: %include asks main_tree.CELL_SIZE 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_bits(cell_size); + main_tree.set_cell_size(cell_size); for (const auto &token_it : parse(stream).children) { main_tree.root.children.push_back(token_it);