The synthesised body is written out as a macro file and imported, so the
generated code goes through the ordinary expansion path instead of being
spliced by hand. set_1 is spelled out as set_0 plus flb, so the generated file
needs nothing from lib.r8asm.
examples/full_add.r8asm now assembles: 7.7 seconds of search at assembly time,
then a 95 character tape. tests/synth_test.cc runs that tape on a small ROT-8
machine for all eight input combinations and checks S and Co -- all eight pass.
Two things that cost me time and are worth writing down:
* the opcode map is "><+-[].,", i.e. '-' is flb and '+' is ror. Guess it the
other way round and a perfectly good tape looks like an infinite loop.
* synth names must not collide with mnemonics: 'out' is lexed as rot8_out
before it can be a block keyword.
103 lines
2.7 KiB
C++
103 lines
2.7 KiB
C++
// Assembles examples/full_add.r8asm (whose body is synthesised at assembly
|
|
// time) and runs the resulting tape on a tiny ROT-8 machine for all eight
|
|
// input combinations.
|
|
#include "r8forge/r8asm.hh"
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
// the eight ROT-8 instructions
|
|
const char *const INSTRUCTIONS = "><-+[].r"; // r = our ror alias
|
|
// real ROT-8: '>' '<' '-' '+' '[' ']' '.' ','
|
|
// note "-" is ror, and there is no separate 'r'
|
|
|
|
struct machine {
|
|
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
|
|
std::vector<uint8_t> out;
|
|
size_t dp = 0;
|
|
|
|
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
|
|
|
|
void run(const std::string &tape) {
|
|
for (size_t pc = 0; pc < tape.size(); pc++) {
|
|
switch (tape[pc]) {
|
|
case '>':
|
|
dp++;
|
|
break;
|
|
case '<':
|
|
dp--;
|
|
break;
|
|
case '-': // flb: flip last bit
|
|
cells[dp] ^= 1;
|
|
break;
|
|
case '+': // ror: rotate right
|
|
cells[dp] = ror(cells[dp]);
|
|
break;
|
|
case '[':
|
|
if (cells[dp] == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']':
|
|
if (cells[dp] != 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc--;
|
|
if (tape[pc] == ']')
|
|
depth++;
|
|
else if (tape[pc] == '[')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case '.':
|
|
out.push_back(cells[dp]);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
r8asm assembler;
|
|
if (!assembler.import_file("examples/full_add.r8asm", 8)) {
|
|
std::cerr << "could not assemble examples/full_add.r8asm\n";
|
|
return 1;
|
|
}
|
|
const std::string tape = assembler.export_rot8();
|
|
std::cout << "tape: " << tape.size() << " characters\n";
|
|
|
|
int failures = 0;
|
|
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] = uint8_t(a); // A
|
|
m.cells[1] = uint8_t(b); // B
|
|
m.cells[2] = uint8_t(c); // Ci
|
|
m.run(tape);
|
|
const int sum = a ^ b ^ c;
|
|
const int carry = (a & b) | (a & c) | (b & c);
|
|
const bool ok = m.out.size() == 2 && m.out[0] == sum && m.out[1] == carry;
|
|
std::cout << " " << a << b << c << " -> " << (m.out.size() > 0 ? int(m.out[0]) : -1)
|
|
<< (m.out.size() > 1 ? int(m.out[1]) : -1) << " expect " << sum
|
|
<< carry << (ok ? " ok" : " WRONG") << "\n";
|
|
if (!ok)
|
|
failures++;
|
|
}
|
|
std::cout << (failures ? "FAILED\n" : "all eight combinations ok\n");
|
|
return failures ? 1 : 0;
|
|
}
|