diff --git a/.gitignore b/.gitignore index 92a8751..52fb822 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -build/ +build*/ .cache/ .continue/ .vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 535c02d..9e0ee96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) include_directories(include) +# The width of a cell. 11 is the project default; the tests and the VMs follow +# it, so `-DR8FORGE_CELL_SIZE=16` builds an entirely 16 bit toolchain. +set(R8FORGE_CELL_SIZE 11 CACHE STRING "bits per cell (1..32)") +add_compile_definitions(R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE}) + find_package(Threads REQUIRED) add_library(r8forge_core src/r8asm.cc src/synth.cc) diff --git a/SPEC.md b/SPEC.md index 3f07b03..a3a8957 100644 --- a/SPEC.md +++ b/SPEC.md @@ -37,7 +37,7 @@ inline constexpr size_t kDefaultMemoryCells = 259; inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u; ``` -汇编器把三个名字注入 `.r8asm` 程序的作用域,宏库靠它们与位宽解耦: +位宽可以用构建选项换:`cmake -S . -B build16 -DR8FORGE_CELL_SIZE=16` 会造出一整套 16 位的工具链(汇编器、库、五份 VM 都跟着走);实测 11 / 16 / 32 位下 `r8forge_synth_test` 都是 8/8 通过、`byte_op_test` 在 16 位下也全过(32 位的穷举是 2^64 组,跑不了)。\n\n汇编器把三个名字注入 `.r8asm` 程序的作用域,宏库靠它们与位宽解耦: | 名字 | 值(11 位) | 用途 | |---|---|---| diff --git a/include/r8forge/geometry.hh b/include/r8forge/geometry.hh index fba67d3..dd0f609 100644 --- a/include/r8forge/geometry.hh +++ b/include/r8forge/geometry.hh @@ -9,4 +9,7 @@ // these are the only places the old 8 bit / 256 cell assumptions live. inline constexpr size_t kDefaultCellSize = 11; inline constexpr size_t kDefaultMemoryCells = 259; -inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u; +// The VMs store a cell in uint32_t, so 32 bits is the ceiling -- and `1u << 32` +// would be undefined, hence the special case instead of a plain shift. +inline constexpr uint32_t kDefaultCellMask = + kDefaultCellSize >= 32 ? 0xFFFFFFFFu : (1u << kDefaultCellSize) - 1u; diff --git a/src/derive.cc b/src/derive.cc index 478b189..277d2c3 100644 --- a/src/derive.cc +++ b/src/derive.cc @@ -19,15 +19,15 @@ namespace { // Characters handed to INP (','); -i/--input fills it once per process, which is // what a scripted run of an interactive program needs. The live terminal case // lives in ROT-8-viz, not here. -std::deque g_input; +std::deque g_input; struct machine { - std::vector cells = std::vector(kDefaultMemoryCells, 0); - std::vector out; + std::vector cells = std::vector(kDefaultMemoryCells, 0); + std::vector out; size_t dp = 0; - static uint16_t ror(uint16_t v) { - return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); + static uint32_t ror(uint32_t v) { + return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); } void run(const std::string &tape, size_t limit = 5'000'000) { @@ -121,7 +121,7 @@ int main(int argc, char **argv) { return 2; } for (const char *p = argv[++i]; *p; p++) - g_input.push_back(uint16_t((unsigned char)*p & 0x7f)); + g_input.push_back(uint32_t((unsigned char)*p & 0x7f)); continue; } argv[++keep] = argv[i]; @@ -141,7 +141,7 @@ int main(int argc, char **argv) { machine m; m.run(tape); std::cout << "out"; - for (uint16_t v : m.out) + for (uint32_t v : m.out) std::cout << " " << int(v); std::cout << "\n"; return 0; @@ -159,11 +159,11 @@ int main(int argc, char **argv) { continue; } const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str()); - m.cells[size_t(at)] = uint16_t(std::atoi(eq + 1) & kDefaultCellMask); + m.cells[size_t(at)] = uint32_t(std::atoi(eq + 1) & kDefaultCellMask); } m.run(tape); std::cout << "out"; - for (uint16_t v : m.out) { + for (uint32_t v : m.out) { std::cout << " " << int(v); } std::cout << "\ncells"; @@ -188,11 +188,11 @@ int main(int argc, char **argv) { continue; } const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str()); - m.cells[size_t(at)] = uint16_t(std::atoi(eq + 1) & kDefaultCellMask); + m.cells[size_t(at)] = uint32_t(std::atoi(eq + 1) & kDefaultCellMask); } m.run(tape); std::cout << "out"; - for (uint16_t v : m.out) { + for (uint32_t v : m.out) { std::cout << " " << int(v); } std::cout << "\n"; @@ -216,7 +216,7 @@ int main(int argc, char **argv) { for (int combo = 0; combo < (1 << params); combo++) { machine m; for (int b = 0; b < params; b++) - m.cells[static_cast(addr[static_cast(b)])] = uint16_t((combo >> b) & 1); + m.cells[static_cast(addr[static_cast(b)])] = uint32_t((combo >> b) & 1); m.run(tape); unsigned val = 0; for (int b = 0; b < params; b++) diff --git a/src/r8asm.cc b/src/r8asm.cc index 3c38668..8d832fb 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -69,7 +69,10 @@ void r8asm::ast::set_cell_size(size_t cell_size) { // the names a .r8asm program sees stay as they were: _CELL_SIZE is the // 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)}, + // 32-bit cells are the ceiling (the VMs store a cell in uint32_t), and +// `1 << 32` is undefined -- spell the largest width out. +{"_CELL_MAX", static_cast(cell_size >= 32 ? 0xFFFFFFFFu + : (1u << cell_size) - 1u)}, {"_ROL_STEP", static_cast(cell_size - 1)}}}; } diff --git a/tests/byte_op_test.cc b/tests/byte_op_test.cc index c590a4e..5a476f4 100644 --- a/tests/byte_op_test.cc +++ b/tests/byte_op_test.cc @@ -26,14 +26,14 @@ constexpr int kCellMod = 1 << kCellSize; constexpr int kCellMask = int(kDefaultCellMask); struct machine { - std::vector cells = std::vector(kDefaultMemoryCells, 0); - std::vector out; + std::vector cells = std::vector(kDefaultMemoryCells, 0); + std::vector out; size_t dp = 0; size_t steps = 0; bool finished = true; // false: the step limit ran out - static uint16_t ror(uint16_t v) { - return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); + static uint32_t ror(uint32_t v) { + return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); } void run(const std::string &tape, size_t limit) { @@ -112,7 +112,7 @@ void report(const std::string &name, int a, int b, const machine &m, const std::string &want) { if (failures++ < 5) { std::cerr << " WRONG " << name << " a=" << a << " b=" << b << " out"; - for (uint16_t v : m.out) + for (uint32_t v : m.out) std::cerr << " " << int(v); std::cerr << " want " << want << " finished=" << m.finished << " steps=" << m.steps << "\n"; @@ -127,8 +127,8 @@ int test_bitop(const std::string &file, const char *name, int op) { for (int a = 0; a < 256; a++) for (int b = 0; b < 256; b++) { machine m; - m.cells[0] = uint16_t(a); // A - m.cells[2] = uint16_t(b); // B + m.cells[0] = uint32_t(a); // A + m.cells[2] = uint32_t(b); // B m.run(tape, 100'000); const int want = op == 0 ? (a & b) : op == 1 ? (a | b) : (a ^ b); const bool ok = m.finished && m.out.size() == 3 && m.out[0] == a && @@ -154,8 +154,8 @@ int test_add(const std::string &file) { for (int a = 0; a < 256; a++) for (int b = 0; b < 256; b++) { machine m; - m.cells[0] = uint16_t(a); - m.cells[2] = uint16_t(b); + m.cells[0] = uint32_t(a); + m.cells[2] = uint32_t(b); m.run(tape, 100'000); const int sum = a + b; const bool ok = m.finished && m.out.size() == 5 && m.out[0] == a && @@ -183,8 +183,8 @@ int test_inc(const std::string &file) { for (int a = 0; a < 256; a++) for (int ci = 0; ci < 2; ci++) { machine m; - m.cells[0] = uint16_t(a); - m.cells[2] = uint16_t(ci); + m.cells[0] = uint32_t(a); + m.cells[2] = uint32_t(ci); m.run(tape, 100'000); const int sum = a + ci; const bool ok = m.finished && m.out.size() == 4 && m.out[0] == a && @@ -213,8 +213,8 @@ int test_add_nc(const std::string &file) { for (int a = 0; a < 256; a++) for (int b = 0; b < 256; b++) { machine m; - m.cells[0] = uint16_t(a); // A - m.cells[2] = uint16_t(b); // B + m.cells[0] = uint32_t(a); // A + m.cells[2] = uint32_t(b); // B m.run(tape, 100'000); const int sum = a + b; const bool ok = m.finished && m.out.size() == 4 && m.out[0] == 0 && @@ -240,7 +240,7 @@ int test_inc_nc(const std::string &file) { int bad = 0; for (int a = 0; a < 256; a++) { machine m; - m.cells[0] = uint16_t(a); // A + m.cells[0] = uint32_t(a); // A m.run(tape, 100'000); const int sum = a + 1; const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 && @@ -265,7 +265,7 @@ int test_neg_nc(const std::string &file) { int bad = 0; for (int a = 0; a < 256; a++) { machine m; - m.cells[0] = uint16_t(a); // A + m.cells[0] = uint32_t(a); // A m.run(tape, 100'000); const int neg = (kCellMod - a) & kCellMask; const int carry = a == 0 ? 1 : 0; // -0 is 0 but it does carry out @@ -291,8 +291,8 @@ int test_sub_nc(const std::string &file) { for (int a = 0; a < 256; a++) for (int b = 0; b < 256; b++) { machine m; - m.cells[0] = uint16_t(a); // A - m.cells[2] = uint16_t(b); // B + m.cells[0] = uint32_t(a); // A + m.cells[2] = uint32_t(b); // B m.run(tape, 100'000); const int diff = (a - b) & kDefaultCellMask; const int no_borrow = a >= b ? 1 : 0; @@ -318,7 +318,7 @@ int test_not_nc(const std::string &file) { int bad = 0; for (int a = 0; a < 256; a++) { machine m; - m.cells[0] = uint16_t(a); // A + m.cells[0] = uint32_t(a); // A m.run(tape, 100'000); const int inv = (~a) & kDefaultCellMask; const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 && @@ -341,7 +341,7 @@ int test_not_ip(const std::string &file) { int bad = 0; for (int a = 0; a < 256; a++) { machine m; - m.cells[0] = uint16_t(a); // A + m.cells[0] = uint32_t(a); // A m.run(tape, 100'000); const int inv = (~a) & kDefaultCellMask; const bool ok = @@ -364,7 +364,7 @@ int test_inc_ip(const std::string &file) { int bad = 0; for (int a = 0; a < 256; a++) { machine m; - m.cells[0] = uint16_t(a); // A + m.cells[0] = uint32_t(a); // A m.run(tape, 100'000); const int want = (a + 1) & kDefaultCellMask; const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want && @@ -387,7 +387,7 @@ int test_neg_ip(const std::string &file) { int bad = 0; for (int a = 0; a < 256; a++) { machine m; - m.cells[0] = uint16_t(a); // A + m.cells[0] = uint32_t(a); // A m.run(tape, 100'000); const int want = (kCellMod - a) & kCellMask; const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want; @@ -409,7 +409,7 @@ int test_dec_ip(const std::string &file) { int bad = 0; for (int a = 0; a < 256; a++) { machine m; - m.cells[0] = uint16_t(a); + m.cells[0] = uint32_t(a); m.run(tape, 100'000); const int want = (a - 1) & kDefaultCellMask; const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want; @@ -431,7 +431,7 @@ int test_nonzero(const std::string &file) { int bad = 0; for (int b = 0; b < 256; b++) { machine m; - m.cells[0] = uint16_t(b); // B + m.cells[0] = uint32_t(b); // B m.run(tape, 100'000); const bool ok = m.finished && m.out.size() == 2 && m.out[0] == b && m.out[1] == (b != 0 ? 1 : 0); @@ -455,7 +455,7 @@ int test_ones(const std::string &file) { int bad = 0, n = 0; for (int a = 0; a <= kCellSize; a++) { machine m; - m.cells[0] = uint16_t(a); // A, the count + m.cells[0] = uint32_t(a); // A, the count m.run(tape, 5'000'000); n++; const int want = (1 << a) - 1; @@ -487,8 +487,8 @@ int test_divmod(const std::string &file) { for (int d : divs) for (int a = 0; a < kCellMod; a++) { machine m; - m.cells[0] = uint16_t(a); // N - m.cells[1] = uint16_t(d); // D + m.cells[0] = uint32_t(a); // N + m.cells[1] = uint32_t(d); // D m.run(tape, 5'000'000); n++; if (m.steps > worst) worst = m.steps; @@ -517,7 +517,7 @@ int test_digits(const std::string &file) { size_t worst = 0; for (int v = 0; v < kCellMod; v++) { machine m; - m.cells[0] = uint16_t(v); + m.cells[0] = uint32_t(v); m.run(tape, 5'000'000); n++; if (m.steps > worst) worst = m.steps; @@ -525,7 +525,7 @@ int test_digits(const std::string &file) { while (want.size() < 4) want.insert(want.begin(), '0'); bool ok = m.finished && m.out.size() == 4; for (int i = 0; ok && i < 4; i++) { - ok = m.out[size_t(i)] == (uint16_t)(unsigned char)want[size_t(i)]; + ok = m.out[size_t(i)] == (uint32_t)(unsigned char)want[size_t(i)]; } if (!ok) { bad++; @@ -558,7 +558,7 @@ int test_undigits(const std::string &file) { rest /= 10; if (garbage) digit |= 5 << 7; // bits the macro must ignore - m.cells[size_t(i)] = uint16_t('0' + digit); + m.cells[size_t(i)] = uint32_t('0' + digit); } m.run(tape, 500'000); n++; @@ -587,8 +587,8 @@ int test_add_ip(const std::string &file) { for (int a : accs) for (int b = 0; b < 256; b++) { machine m; - m.cells[0] = uint16_t(a); // A - m.cells[1] = uint16_t(b); // B + m.cells[0] = uint32_t(a); // A + m.cells[1] = uint32_t(b); // B m.run(tape, 5'000'000); n++; const int want = (a + b) & kDefaultCellMask; @@ -617,8 +617,8 @@ int test_mul(const std::string &file) { for (int mc = 0; mc < 256; mc++) for (int mu = 0; mu < 256; mu++) { machine m; - m.cells[0] = uint16_t(mc); // MC - m.cells[1] = uint16_t(mu); // MUL + m.cells[0] = uint32_t(mc); // MC + m.cells[1] = uint32_t(mu); // MUL m.run(tape, 5'000'000); n++; const int p = mc * mu; diff --git a/tests/examples_test.cc b/tests/examples_test.cc index c5a0802..e1daef1 100644 --- a/tests/examples_test.cc +++ b/tests/examples_test.cc @@ -28,15 +28,15 @@ namespace { struct machine { - std::vector cells = std::vector(kDefaultMemoryCells, 0); - std::vector out; - std::deque in; // characters for INP, from the header's `; in` + std::vector cells = std::vector(kDefaultMemoryCells, 0); + std::vector out; + std::deque in; // characters for INP, from the header's `; in` size_t dp = 0; size_t steps = 0; bool finished = true; // false: the step limit ran out - static uint16_t ror(uint16_t v) { - return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); + static uint32_t ror(uint32_t v) { + return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); } void run(const std::string &tape, size_t limit = 5'000'000) { @@ -213,10 +213,10 @@ int main(int argc, char **argv) { expected_in(file, in_text); machine m; for (char ch : in_text) - m.in.push_back(uint16_t((unsigned char)ch & 0x7f)); + m.in.push_back(uint32_t((unsigned char)ch & 0x7f)); m.run(tape); std::vector got; - for (uint16_t v : m.out) + for (uint32_t v : m.out) got.push_back(int(v)); const bool ok = m.finished && got == want; std::cout << name << ": " << tape.size() << " characters, " << m.steps diff --git a/tests/synth_test.cc b/tests/synth_test.cc index 104c7bf..267b12b 100644 --- a/tests/synth_test.cc +++ b/tests/synth_test.cc @@ -15,12 +15,12 @@ const char *const INSTRUCTIONS = "><-+[].r"; // r = our ror alias // note "-" is ror, and there is no separate 'r' struct machine { - std::vector cells = std::vector(kDefaultMemoryCells, 0); - std::vector out; + std::vector cells = std::vector(kDefaultMemoryCells, 0); + std::vector out; size_t dp = 0; - static uint16_t ror(uint16_t v) { - return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); + static uint32_t ror(uint32_t v) { + return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1))); } void run(const std::string &tape) { @@ -97,9 +97,9 @@ int main(int argc, char **argv) { for (int combo = 0; combo < 8; combo++) { const int a = combo & 1, b = (combo >> 1) & 1, c = (combo >> 2) & 1; machine m; - m.cells[0] = uint16_t(a); // A - m.cells[1] = uint16_t(b); // B - m.cells[2] = uint16_t(c); // Ci + m.cells[0] = uint32_t(a); // A + m.cells[1] = uint32_t(b); // B + m.cells[2] = uint32_t(c); // Ci m.run(tape); const int sum = a ^ b ^ c; const int carry = (a & b) | (a & c) | (b & c); diff --git a/tests/tape_run.cc b/tests/tape_run.cc index 55aa62f..fdfe3f6 100644 --- a/tests/tape_run.cc +++ b/tests/tape_run.cc @@ -34,8 +34,8 @@ int main(int argc, char **argv) { const std::string tape((std::istreambuf_iterator(in)), std::istreambuf_iterator()); - std::vector cells(kDefaultMemoryCells, 0); - std::deque input; + std::vector cells(kDefaultMemoryCells, 0); + std::deque input; bool trace = false; for (int i = 2; i < argc; i++) { const std::string a = argv[i]; @@ -49,17 +49,17 @@ int main(int argc, char **argv) { return 2; } for (const char *p = argv[++i]; *p; p++) - input.push_back(uint16_t((unsigned char)*p & 0x7f)); + input.push_back(uint32_t((unsigned char)*p & 0x7f)); continue; } const size_t eq = a.find('='); if (eq == std::string::npos) continue; const size_t at = size_t(std::stoul(a.substr(0, eq))); - cells.at(at) = uint16_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask); + cells.at(at) = uint32_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask); } - std::vector out; + std::vector out; size_t dp = 0; const size_t limit = 5'000'000; // same as r8forge_derive size_t pc = 0; @@ -89,7 +89,7 @@ int main(int argc, char **argv) { cells[dp] ^= 1; break; case '+': // ror - cells[dp] = uint16_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1))); + cells[dp] = uint32_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1))); 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 // byte level program in the library look like an endless loop. @@ -137,7 +137,7 @@ int main(int argc, char **argv) { } std::cout << "out"; - for (uint16_t v : out) + for (uint32_t v : out) std::cout << " " << int(v); std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex << int(cells[0]) << std::dec << ")\n";