Files
sakurac/compiler/intermediate/walks.sakura
T

246 lines
8.5 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file walks.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the walks over code that the passes and the backends share: of the
#! instructions of a block, or of every block not removed, of the edges an
#! instruction leaves by, and of the values an instruction reads. Each is asked
#! `more` and then gives the next with `next`, so a `continue` goes on to the
#! next rather than round again, and a walk gives nothing once it is done. What
#! each is asked is small enough for the optimizer to inline, so a pass walking
#! with one costs what a loop written out by hand does.
module sakura::compiler;
#! What `read_walk::operand` gives for a value an edge gives, which is no
#! operand.
pub const no_operand: u32 = 0xFFFFFFFF;
#! A walk of the instructions of one block, in order. It reads where each
#! instruction goes on to as it gives it, so the instruction given may be
#! rewritten, moved, or taken out, and what is put after it is not given.
#! Asking it for each is small enough to be inlined, and nothing takes its
#! address, so what it holds stays in registers as it goes.
pub struct inst_walk {
var body: &code; #! The code.
var at: u32; #! The next instruction it gives, or 0 where none is left.
}
#! Walks the instructions of a block, in order.
#! @param this The code.
#! @param block The block.
#! @returns The walk.
pub def code::walk(&this, block: u32): inst_walk {
return inst_walk { body = this, at = this.blocks[block].first };
}
#! Whether the walk has another instruction to give.
#! @param this The walk.
#! @returns Whether it has.
pub def inst_walk::more(&this): bool {
return this.at != 0;
}
#! Gives the next instruction, once `more` says there is one.
#! @param this The walk.
#! @returns The instruction.
pub def inst_walk::next(&mut this): u32 {
val at := this.at;
this.at = this.body.insts[at].next;
return at;
}
#! A walk of the instructions of every block not removed: the blocks in order,
#! and each one's instructions in order, as `inst_walk` gives them. It goes
#! into each block by a call that takes the walk's values, not its address, so
#! what it holds stays in registers too.
pub struct code_walk {
var body: &code; #! The code.
var at: u32; #! The next instruction it gives, or 0 where the block it is in has no more.
var block: u32; #! The next block it goes into once that one has no more.
var end: u32; #! The block after the last it goes into.
}
#! Walks the instructions of every block not removed. A block is asked whether
#! it is removed as the walk goes into it, and the blocks are those the code
#! has now.
#! @param this The code.
#! @returns The walk.
pub def code::walk_all(&this): code_walk {
return code_walk { body = this, end = this.blocks.length as u32 };
}
#! Whether the walk has another instruction to give.
#! @param this The walk.
#! @returns Whether it has.
pub def code_walk::more(&mut this): bool {
return true if this.at != 0;
val found := this.body.next_filled(this.block, this.end);
this.block = pair_high(found);
this.at = pair_low(found);
return this.at != 0;
}
#! Gives the next instruction, once `more` says there is one.
#! @param this The walk.
#! @returns The instruction.
pub def code_walk::next(&mut this): u32 {
val at := this.at;
this.at = this.body.insts[at].next;
return at;
}
#! Finds the first block from one on, and before another, that is not removed
#! and holds an instruction.
#! @param this The code.
#! @param block The block to look from.
#! @param end The block to look before.
#! @returns The block after the one found, and its first instruction, as
#! `pair_key` packs them, or `end` and 0 where none is.
pub def code::next_filled(&this, block: u32, end: u32): u64 {
var at := block;
while at < end {
val held := &this.blocks[at];
at++;
return pair_key(at, held.first) if !held.gone && held.first != 0;
}
return pair_key(end, 0);
}
#! A walk of the edges an instruction ending a block leaves by, which follow one
#! another from its `aux`.
pub struct edge_walk {
var at: u32; #! The next edge it gives.
var end: u32; #! The edge after the last.
}
#! Walks the edges an instruction leaves by, in order: none for one that does
#! not end its block, or returns or stops.
#! @param this The code.
#! @param index The instruction.
#! @returns The walk.
pub def code::edges_of(&this, index: u32): edge_walk {
val first: u32 = this.insts[index].aux;
return edge_walk { at = first, end = first + this.edge_count(index) };
}
#! Whether the walk has another edge to give.
#! @param this The walk.
#! @returns Whether it has.
pub def edge_walk::more(&this): bool {
return this.at < this.end;
}
#! Gives the next edge, once `more` says there is one.
#! @param this The walk.
#! @returns The edge.
pub def edge_walk::next(&mut this): u32 {
val at := this.at;
this.at = at + 1;
return at;
}
#! A walk of every value instructions read: an instruction's operands, in
#! order, then the arguments of each edge it leaves by, edge after edge. A
#! walk is made once for the code with `reader`, then started at each
#! instruction in turn, or made for one with `reads_of`. It reads the values
#! where they are held, so nothing may add instructions, operands, or edges to
#! the code while it walks. Starting it is small enough to be inlined, so a
#! pass walking what every instruction reads makes no call for each.
pub struct read_walk {
var insts: *inst; #! The code's instructions.
var values: *u32; #! The code's operands.
var edges: *edge; #! The code's edges.
var held: *inst; #! The instruction while its operands are given, or null once its edges' arguments are.
var data: *u32; #! Where the values it is giving are held: its operands, or an edge's arguments.
var at: u64; #! The place there of the next it gives.
var end: u64; #! How many are held there.
var ends: bool; #! Whether the instruction ends its block, so the arguments of its edges follow its operands.
var edge: u32; #! The next edge whose arguments it gives.
var last: u32; #! The edge after the instruction's last.
}
#! Makes a walk of what the code's instructions read, to be started at each.
#! @param this The code.
#! @returns The walk, which gives nothing until it is started.
pub def code::reader(&this): read_walk {
return read_walk {
insts = this.insts.span.data,
values = this.operands.span.data,
edges = this.edges.span.data
};
}
#! Walks every value an instruction reads.
#! @param this The code.
#! @param index The instruction.
#! @returns The walk.
pub def code::reads_of(&this, index: u32): read_walk {
var made := this.reader();
made.start(index);
return made;
}
#! Starts the walk at an instruction, from its first operand.
#! @param this The walk.
#! @param index The instruction.
pub def read_walk::start(&mut this, index: u32): void {
val held := this.insts + index as u64;
this.held = held;
this.data = this.values + held[0].args as u64;
this.at = 0;
this.end = held[0].argc as u64;
# The operations from `jump` on are those ending a block, as `op` orders
# them, which a comparison asks more cheaply than `op::ends`.
this.ends = held[0].kind as u32 >= op::jump as u32;
}
#! Whether the walk has another value to give.
#! @param this The walk.
#! @returns Whether it has.
pub def read_walk::more(&mut this): bool {
return true if this.at < this.end;
return this.ends && this.next_edge();
}
#! Gives the next value, once `more` says there is one.
#! @param this The walk.
#! @returns The value.
pub def read_walk::next(&mut this): u32 {
val value := this.data[this.at];
this.at++;
return value;
}
#! Which operand the value the walk gave last is.
#! @param this The walk.
#! @returns Its position among the operands, or `no_operand` where an edge
#! gives it.
pub def read_walk::operand(&this): u32 {
return this.held != null ? (this.at - 1) as u32 : no_operand;
}
#! Goes on to the arguments of the next edge of the instruction that gives
#! any, its operands given.
#! @param this The walk.
#! @returns Whether one does.
def read_walk::next_edge(&mut this): bool {
if this.held != null {
this.edge = this.held[0].aux;
this.last = this.edge + this.held[0].edge_count();
this.held = null;
}
while this.edge < this.last {
val args := &this.edges[this.edge as u64].args;
this.edge++;
if args.length != 0 {
this.data = args.data();
this.at = 0;
this.end = args.length;
return true;
}
}
return false;
}