83 lines
2.9 KiB
Plaintext
83 lines
2.9 KiB
Plaintext
#! @file moves.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the making of moves all at once, as the moves along an edge into the
|
|
#! places of what its block takes are made: a move whose place another still
|
|
#! reads waits for that one, and moves going round in a cycle save one place
|
|
#! first, which those reading it read from where it was saved. The machine's
|
|
#! assembler moves words of the frame, and the native backend's selector
|
|
#! registers, slots, and the stack, each making the moves itself.
|
|
|
|
module sakura::compiler;
|
|
|
|
|
|
#! What making moves all at once needs of the moves and of what makes them.
|
|
#! @tparam M The type of a move.
|
|
pub trait move_maker<M> {
|
|
#! Whether a move reads the place another writes.
|
|
def move_reads(&this, reader: &M, writer: &M): bool;
|
|
|
|
#! Makes a move.
|
|
def move_one(&mut this, made: &M): void;
|
|
|
|
#! The move saving what a move would write over, to a place no move reads or
|
|
#! writes.
|
|
def move_saving(&mut this, written: &M, pending: span<M>): M;
|
|
|
|
#! Has a move read what another saved from where it was saved.
|
|
def move_reread(&this, reader: &mut M, saved: &M): void;
|
|
}
|
|
|
|
#! Makes moves all at once: the first whose place no other reads, again and
|
|
#! again, and where every place written is still read, the first move's place
|
|
#! saved first, to be read from where it was saved.
|
|
#! @param pending The moves, no two writing one place, and none reading the
|
|
#! place it writes, which are taken as they are made.
|
|
#! @param maker What makes them.
|
|
#! @param swap Whether a move made gives its place among those left to the
|
|
#! last, rather than those after it each moving up one, which
|
|
#! is the order the rest are tried in.
|
|
pub def make_moves<M, K>(pending: &mut vector<M>, maker: &mut K, swap: bool): void where K: move_maker<M> {
|
|
# The moves are read through a view, without a call, taken again as one goes.
|
|
var moves := pending.slice();
|
|
while moves.length != 0 {
|
|
var ready := moves.length;
|
|
for i in 0..moves.length {
|
|
var read := false;
|
|
for j in 0..moves.length {
|
|
if j != i && maker.move_reads(&moves[j], &moves[i]) {
|
|
read = true;
|
|
break;
|
|
}
|
|
}
|
|
if !read {
|
|
ready = i;
|
|
break;
|
|
}
|
|
}
|
|
if ready != moves.length {
|
|
maker.move_one(&moves[ready]);
|
|
if swap {
|
|
moves[ready] = pending.last();
|
|
pending.truncate();
|
|
} else {
|
|
pending.remove_at(ready);
|
|
}
|
|
moves = pending.slice();
|
|
continue;
|
|
}
|
|
|
|
# Every place written is still read: the first is saved, and read from
|
|
# where it was saved.
|
|
val saved := maker.move_saving(&moves[0], moves.view());
|
|
maker.move_one(&saved);
|
|
for j in 0..moves.length {
|
|
if maker.move_reads(&moves[j], &moves[0]) {
|
|
maker.move_reread(&mut moves[j], &saved);
|
|
}
|
|
}
|
|
}
|
|
}
|