Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,298 @@
|
||||
#! @file intermediate.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the tests of the intermediate code: building it with variables,
|
||||
#! which become the parameters of blocks where control flow joins, taking out
|
||||
#! what stands for one value, verifying it, printing it, and walking it, and
|
||||
#! the lists of indices its blocks and edges hold.
|
||||
|
||||
import std::io;
|
||||
import std::testing;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Whether two texts are the same but for the line breaks ending them, which
|
||||
#! printed code ends with and a block of text does not.
|
||||
#! @param a One text.
|
||||
#! @param b The other.
|
||||
#! @returns Whether they are.
|
||||
def same_text(a: strview, b: strview): bool {
|
||||
var x := a.length;
|
||||
var y := b.length;
|
||||
while x > 0 && a.at(x - 1) == '\n' { x--; }
|
||||
while y > 0 && b.at(y - 1) == '\n' { y--; }
|
||||
val same := a.substr(0, x) == b.substr(0, y);
|
||||
if !same {
|
||||
println(a);
|
||||
}
|
||||
return same;
|
||||
}
|
||||
|
||||
#! Builds the loop of `steps`, which counts the steps of the Collatz sequence:
|
||||
#!
|
||||
#! ```
|
||||
#! def steps(start: u64): u64 {
|
||||
#! var n := start;
|
||||
#! var count: u64 = 0;
|
||||
#! while n != 1 {
|
||||
#! if n % 2 == 0 { n = n / 2; } else { n = 3 * n + 1; }
|
||||
#! count += 1;
|
||||
#! }
|
||||
#! return count;
|
||||
#! }
|
||||
#! ```
|
||||
#! @returns Its code.
|
||||
def build_steps(): code {
|
||||
var params: vt[1] = [vt::word];
|
||||
var made := builder::new(params[0:1]);
|
||||
val n := made.variable(vt::word);
|
||||
val count := made.variable(vt::word);
|
||||
made.assign(n, made.param(0));
|
||||
made.assign(count, made.word(0));
|
||||
|
||||
val head := made.new_block();
|
||||
val body := made.new_block();
|
||||
val exit := made.new_block();
|
||||
made.jump(head);
|
||||
made.switch_to(head);
|
||||
val done := made.binary(op::ne, made.read(n), made.word(1));
|
||||
made.branch(done, body, exit);
|
||||
made.seal(body);
|
||||
made.seal(exit);
|
||||
|
||||
made.switch_to(body);
|
||||
val even := made.binary(op::eq, made.binary(op::remu, made.read(n), made.word(2)), made.word(0));
|
||||
val half := made.new_block();
|
||||
val odd := made.new_block();
|
||||
val next := made.new_block();
|
||||
made.branch(even, half, odd);
|
||||
made.seal(half);
|
||||
made.seal(odd);
|
||||
|
||||
made.switch_to(half);
|
||||
made.assign(n, made.binary(op::divu, made.read(n), made.word(2)));
|
||||
made.jump(next);
|
||||
made.switch_to(odd);
|
||||
made.assign(n, made.binary(op::add, made.binary(op::mul, made.word(3), made.read(n)), made.word(1)));
|
||||
made.jump(next);
|
||||
made.seal(next);
|
||||
|
||||
made.switch_to(next);
|
||||
made.assign(count, made.binary(op::add, made.read(count), made.word(1)));
|
||||
made.jump(head);
|
||||
made.seal(head);
|
||||
|
||||
made.switch_to(exit);
|
||||
var result: u32[1] = [made.read(count)];
|
||||
made.ret(result[0:1]);
|
||||
return made.finish();
|
||||
}
|
||||
|
||||
@test def loops_take_their_variables_as_parameters(t: &mut tester): void {
|
||||
var made := build_steps();
|
||||
made.results.append(vt::word);
|
||||
val verdict := made.verify();
|
||||
t.expects(verdict.ok());
|
||||
for &each in verdict.problems {
|
||||
println(each);
|
||||
}
|
||||
|
||||
val printed := made.print();
|
||||
val want := """
|
||||
b0(v0: i64):
|
||||
v1 = iconst 0
|
||||
jump b1(v0, v1)
|
||||
b1(v2: i64, v3: i64):
|
||||
v4 = iconst 1
|
||||
v5 = ne v2, v4
|
||||
branch v5, b3, b2
|
||||
b2:
|
||||
ret v3
|
||||
b3:
|
||||
v6 = iconst 2
|
||||
v7 = remu v2, v6
|
||||
v8 = iconst 0
|
||||
v9 = eq v7, v8
|
||||
branch v9, b5, b4
|
||||
b4:
|
||||
v10 = iconst 3
|
||||
v11 = mul v10, v2
|
||||
v12 = iconst 1
|
||||
v13 = add v11, v12
|
||||
jump b6(v13)
|
||||
b5:
|
||||
v14 = iconst 2
|
||||
v15 = divu v2, v14
|
||||
jump b6(v15)
|
||||
b6(v16: i64):
|
||||
v17 = iconst 1
|
||||
v18 = add v3, v17
|
||||
jump b1(v16, v18)
|
||||
""";
|
||||
t.expects(same_text(printed.view(), want));
|
||||
}
|
||||
|
||||
@test def parameters_standing_for_one_value_are_taken_out(t: &mut tester): void {
|
||||
# `x` is given before the branch, and neither side changes it, so the join
|
||||
# takes nothing.
|
||||
var params: vt[1] = [vt::word];
|
||||
var made := builder::new(params[0:1]);
|
||||
val x := made.variable(vt::word);
|
||||
made.assign(x, made.param(0));
|
||||
val then := made.new_block();
|
||||
val other := made.new_block();
|
||||
val join := made.new_block();
|
||||
made.branch(made.param(0), then, other);
|
||||
made.seal(then);
|
||||
made.seal(other);
|
||||
made.switch_to(then);
|
||||
made.jump(join);
|
||||
made.switch_to(other);
|
||||
made.jump(join);
|
||||
made.seal(join);
|
||||
made.switch_to(join);
|
||||
var result: u32[1] = [made.read(x)];
|
||||
made.ret(result[0:1]);
|
||||
|
||||
var code := made.finish();
|
||||
code.results.append(vt::word);
|
||||
t.expects(code.verify().ok());
|
||||
t.expects(same_text(code.print().view(), """
|
||||
b0(v0: i64):
|
||||
branch v0, b2, b1
|
||||
b1:
|
||||
jump b3
|
||||
b2:
|
||||
jump b3
|
||||
b3:
|
||||
ret v0
|
||||
"""));
|
||||
}
|
||||
|
||||
@test def what_nothing_reaches_is_taken_out(t: &mut tester): void {
|
||||
var made := builder::new(span::<vt>::empty());
|
||||
var none: u32[1] = [0];
|
||||
made.ret(none[0:0]);
|
||||
# Code after a `return` goes in a block nothing reaches.
|
||||
made.word(5);
|
||||
made.ret(none[0:0]);
|
||||
|
||||
var code := made.finish();
|
||||
t.expects(code.verify().ok());
|
||||
t.expects(same_text(code.print().view(), "b0:\n ret"));
|
||||
}
|
||||
|
||||
@test def the_verifier_finds_uses_not_dominated(t: &mut tester): void {
|
||||
var params: vt[1] = [vt::word];
|
||||
var made := builder::new(params[0:1]);
|
||||
val then := made.new_block();
|
||||
val other := made.new_block();
|
||||
val join := made.new_block();
|
||||
made.branch(made.param(0), then, other);
|
||||
made.seal(then);
|
||||
made.seal(other);
|
||||
made.switch_to(then);
|
||||
val only_here := made.word(1);
|
||||
made.jump(join);
|
||||
made.switch_to(other);
|
||||
made.jump(join);
|
||||
made.seal(join);
|
||||
made.switch_to(join);
|
||||
var result: u32[1] = [only_here];
|
||||
made.ret(result[0:1]);
|
||||
|
||||
var code := made.finish();
|
||||
code.results.append(vt::word);
|
||||
val verdict := code.verify();
|
||||
t.expects(!verdict.ok());
|
||||
}
|
||||
|
||||
|
||||
@test def lists_of_indices_hold_four_themselves_and_more_in_memory(t: &mut tester): void {
|
||||
var list := ids {};
|
||||
for i in 0..4u {
|
||||
list.append(i as u32 * 3);
|
||||
}
|
||||
t.expects(list.length == 4 && list.at(3) == 9);
|
||||
|
||||
# Past four the indices move to memory of the list's own, and stay in order.
|
||||
for i in 4..100u {
|
||||
list.append(i as u32 * 3);
|
||||
}
|
||||
var total: u64 = 0;
|
||||
for &each in list {
|
||||
total += each as u64;
|
||||
}
|
||||
t.expects(list.length == 100 && list.at(0) == 0 && list.at(99) == 297 && total == 14850);
|
||||
|
||||
list.at(5) = 1;
|
||||
var other := list.copied(nothing {});
|
||||
t.expects(other.at(5) == 1 && other.length == 100);
|
||||
var same := ids {};
|
||||
for &each in other {
|
||||
same.append(each);
|
||||
}
|
||||
t.expects(same.same(&list));
|
||||
same.clear();
|
||||
t.expects(same.length == 0 && !same.same(&list));
|
||||
}
|
||||
|
||||
@test def walks_give_each_instruction_and_what_each_reads(t: &mut tester): void {
|
||||
var made := build_steps();
|
||||
|
||||
# Every instruction of the blocks not removed, each once.
|
||||
var count: u32 = 0;
|
||||
var all := made.walk_all();
|
||||
while all.more() {
|
||||
all.next();
|
||||
count++;
|
||||
}
|
||||
t.expects(count != 0 && count == made.size());
|
||||
|
||||
# The loop's head, block 1, compares and branches.
|
||||
var found := vector::<u32>::new();
|
||||
var head := made.walk(1);
|
||||
while head.more() {
|
||||
found.append(head.next());
|
||||
}
|
||||
t.expects(found.length == 3 && made.insts[found[0]].kind == op::iconst
|
||||
&& made.insts[found[1]].kind == op::ne && made.insts[found[2]].kind == op::branch);
|
||||
|
||||
# The comparison reads its operands, in order.
|
||||
var operands := made.reads_of(found[1]);
|
||||
var places: u32 = 0;
|
||||
while operands.more() {
|
||||
operands.next();
|
||||
t.expects(operands.operand() == places);
|
||||
places++;
|
||||
}
|
||||
t.expects(places == 2);
|
||||
|
||||
# The branch leaves by two edges, one after another.
|
||||
var outs := made.edges_of(found[2]);
|
||||
t.expects(outs.more() && outs.next() == made.insts[found[2]].aux);
|
||||
t.expects(outs.more() && outs.next() == made.insts[found[2]].aux + 1 && !outs.more());
|
||||
|
||||
# The jump back to the head reads only what its edge gives.
|
||||
val jump: u32 = made.blocks[6].last;
|
||||
val first: u32 = made.edges[made.insts[jump].aux].args[0];
|
||||
var given := made.reads_of(jump);
|
||||
var values := vector::<u32>::new();
|
||||
while given.more() {
|
||||
values.append(given.next());
|
||||
t.expects(given.operand() == no_operand);
|
||||
}
|
||||
t.expects(values.length == 2 && values[0] == first);
|
||||
|
||||
# A walk reads where each instruction goes on to as it gives it, so taking
|
||||
# the one given out of its block's order does not end the walk.
|
||||
var cut: u32 = 0;
|
||||
var again := made.walk(1);
|
||||
while again.more() {
|
||||
made.insts[again.next()].next = 0;
|
||||
cut++;
|
||||
}
|
||||
t.expects(cut == 3);
|
||||
}
|
||||
Reference in new issue
Block a user