Files

123 lines
4.8 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
#! @file orders.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the order of the blocks and their dominators, kept with the code.
#! Working them out reads the last instruction of every block, which is where
#! most of the time goes, and many passes ask for them in turn, most of which
#! leave the blocks as they were. So code keeps what was last worked out, and
#! forgets it where what leads where changes: in each edit that changes an
#! edge, and where a pass changes edges itself, as merging blocks does, or
#! where pruning works the order out anew after inlining. Finding the blocks
#! the entry reaches works it out and keeps it, as what follows asks for it.
#! The verifier checks that what is kept is what would be worked out, after
#! each pass.
module sakura::compiler;
#! Writes the blocks the entry reaches in reverse postorder, and each block's
#! place in it, as `reverse_postorder` works them out, keeping them for the next
#! pass to ask.
#! @param this The code.
#! @param order Where the blocks are written, in that order.
#! @param number Where each block's place is written, by its index, or
#! `0xFFFFFFFF` for one the entry does not reach.
pub def code::block_order(&mut this, order: &mut vector<u32>, number: &mut vector<u32>): void {
this.keep_order();
order.clear();
order.extend(this.kept_order.span);
number.clear();
number.extend(this.kept_number.span);
}
#! Writes the blocks' order as `block_order` does, from what the code keeps
#! where it keeps it, for those that cannot keep it, as the backends.
#! @param this The code.
#! @param order Where the blocks are written.
#! @param number Where each block's place is written.
pub def code::copy_order(&this, order: &mut vector<u32>, number: &mut vector<u32>): void {
if !this.order_kept {
this.reverse_postorder(order, number);
return;
}
order.clear();
order.extend(this.kept_order.span);
number.clear();
number.extend(this.kept_number.span);
}
#! The immediate dominator of each block, as `dominators` works them out,
#! keeping them for the next pass to ask.
#! @param this The code.
#! @returns The dominators, by block, in scratch memory.
pub def code::block_dominators(&mut this): vector<u32> {
if !this.idom_kept {
this.keep_order();
val found := this.dominators(this.kept_order.span, this.kept_number.span);
this.kept_idom.clear();
this.kept_idom.extend(found.span);
this.idom_kept = true;
}
var made := vector::<u32>::new(scratch());
made.extend(this.kept_idom.span);
return made;
}
#! Works out the blocks' order, where the code does not keep it, or keeps it
#! for fewer blocks than it has, as where blocks were added since.
#! @param this The code.
def code::keep_order(&mut this): void {
return if this.order_kept && this.kept_number.length == this.blocks.length;
this.reverse_postorder(&mut this.kept_order, &mut this.kept_number);
this.order_kept = true;
}
#! Forgets the order and dominators the code keeps, as what changes where its
#! blocks lead does.
#! @param this The code.
pub def code::forget_order(&mut this): void {
this.order_kept = false;
this.idom_kept = false;
}
#! Keeps an order worked out elsewhere: the blocks numbered in reverse
#! postorder, as compacting numbers them, the first `reached` of `count` being
#! those the entry reaches, with their dominators where given.
#! @param this The code.
#! @param reached How many blocks the entry reaches.
#! @param count How many blocks there are.
#! @param idom The immediate dominator of each, by its new number, or an
#! empty view where they are not known.
pub def code::keep_numbered_order(&mut this, reached: u32, count: u32, idom: span<u32>): void {
this.kept_order.clear();
this.kept_number.clear();
this.kept_number.resize(count as u64, 0xFFFFFFFF);
for i in 0..reached {
this.kept_order.append(i as u32);
this.kept_number[i as u64] = i as u32;
}
this.order_kept = true;
this.idom_kept = idom.length == count as u64;
if this.idom_kept {
this.kept_idom.clear();
this.kept_idom.extend(idom);
}
}
#! Notes where what the code keeps of its blocks' order or dominators is not
#! what would be worked out now, as where an edit changed where blocks lead
#! without forgetting them.
#! @param this The code.
#! @param order The blocks' order, worked out now.
#! @param idom Their dominators, worked out now.
#! @param found Where what is wrong is noted.
pub def code::check_kept_order(&this, order: span<u32>, idom: span<u32>, found: &mut vector<string>): void {
if this.order_kept && this.kept_order.span != order {
found.append("The order of the blocks kept is not theirs now.");
}
if this.idom_kept && this.kept_idom.span != idom {
found.append("The dominators of the blocks kept are not theirs now.");
}
}