Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+250
View File
@@ -0,0 +1,250 @@
#! @file compact.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the compacting of code. A pass taking out an instruction, a block,
#! or an edge leaves its slot behind, and inlining leaves about half of a
#! function's slots so, yet every pass after and both backends size their
#! tables by the slots and walk past the empty ones. Compacting numbers again
#! what is left so they cost only what the code holds. The instructions keep
#! the order they were in, which keeps what goes by it: the instructions a
#! variable's scope covers, as how many the code had where it begins and ends.
#! The blocks are numbered in reverse postorder, so the order the code keeps
#! of them is known without working it out again.
module sakura::compiler;
#! What compacting gives a slot that goes.
const compacted_out: u32 = 0xFFFFFFFF;
#! Compacts the code, as the file says. The blocks not removed are kept, with
#! the instructions in them, the edges those instructions leave by, and the
#! values they give and the blocks take, and so are the values the bindings
#! name; the rest goes. The blocks the entry reaches are numbered in reverse
#! postorder, then those it does not, and everything else in the order it was.
#! A value a binding names whose instruction went stays, given by nothing, so
#! the variable is shown as nowhere from there, as it was; but where that
#! instruction made a constant, the binding keeps the constant in its place.
#! @param this The code.
pub def code::compact(&mut this): void {
# The blocks the entry reaches, in reverse postorder, then the others not
# removed.
var order := vector::<u32>::new(scratch());
var number := vector::<u32>::new(scratch());
this.block_order(&mut order, &mut number);
var block_to := vector::<u32>::new(scratch());
block_to.resize(this.blocks.length, compacted_out);
var block_from := vector::<u32>::new(scratch());
block_from.reserve(this.blocks.length);
for &b in order {
block_to[b] = block_from.length as u32;
block_from.append(b);
}
val reached := block_from.length as u32;
for b in 0..this.blocks.length {
continue if this.blocks[b].gone || block_to[b] != compacted_out;
block_to[b] = block_from.length as u32;
block_from.append(b as u32);
}
# The instructions in those blocks, marked, then numbered in the order they
# were, with how many are kept below each slot, as a variable's scope is
# numbered.
var inst_to := vector::<u32>::new(scratch());
inst_to.resize(this.insts.length, compacted_out);
inst_to[0] = 0;
var live: u64 = 1;
for &b in block_from {
var walk := this.walk(b);
while walk.more() {
inst_to[walk.next()] = 0;
live++;
}
}
var kept := vector::<u32>::new(scratch());
kept.reserve(live);
var below := vector::<u32>::new(scratch());
below.resize(this.insts.length + 1);
for i in 0..this.insts.length {
below[i] = kept.length as u32;
continue if inst_to[i] == compacted_out;
inst_to[i] = kept.length as u32;
kept.append(i as u32);
}
below[this.insts.length] = kept.length as u32;
# The values the instructions kept give, the blocks kept take, and the
# bindings name, marked, then numbered in the order they were, each moving
# down to its number where it is. The operands and edges kept are counted.
val values := this.origins.length;
var value_to := vector::<u32>::new(scratch());
value_to.resize(values, compacted_out);
value_to[0] = 0;
var operand_count: u64 = 0;
var edge_count: u64 = 0;
for &i in kept {
val held := &this.insts[i];
for r in 0..held.count {
value_to[held.result as u64 + r as u64] = 0;
}
operand_count += held.argc as u64;
edge_count += held.edge_count() as u64;
}
for &b in block_from {
for &param in this.blocks[b].params {
value_to[param] = 0;
}
}
# A binding naming a constant whose instruction went keeps the constant in
# the value's place.
for i in 0..this.bindings.length {
val bound := &mut this.bindings[i];
continue if bound.value == 0 || bound.value as u64 >= values;
val held: origin = this.origins[bound.value];
if !held.param && inst_to[held.from] == compacted_out {
bound.known = this.constant_kind(bound.value, &mut bound.bits);
if bound.known != vt::none {
bound.value = 0;
continue;
}
}
value_to[bound.value] = 0;
}
var origins_kept: u64 = 0;
val origins := this.origins.slice().data;
for v in 0..values {
continue if value_to[v] == compacted_out;
value_to[v] = origins_kept as u32;
var held: origin = origins[v];
if v != 0 {
val home := held.param ? block_to[held.from] : inst_to[held.from];
if home == compacted_out {
held.param = false;
held.from = 0;
held.index = 0;
} else {
held.from = home;
}
}
origins[origins_kept] = held;
origins_kept++;
}
# Copied into memory of their size, so the code holds no more than it needs.
var made_origins := vector::<origin>::new();
made_origins.extend(span::<origin>::over(this.origins.view().data, origins_kept));
this.origins = move(&mut made_origins);
# What the bindings and variables name. A binding whose instruction went
# takes effect where its block begins, or where the block of that
# instruction does where its own went, as the backends read it. This reads
# the instructions where they are, before they move.
for i in 0..this.bindings.length {
val bound := &mut this.bindings[i];
val after := bound.after;
var block := bound.block;
if block == no_block || block_to[block] == compacted_out {
block = after != 0 ? this.insts[after].block : no_block;
}
bound.block = block == no_block || block_to[block] == compacted_out ? no_block : block_to[block];
bound.after = inst_to[after] == compacted_out ? 0 : inst_to[after];
bound.value = bound.value as u64 >= values ? 0 : value_to[bound.value];
}
for i in 0..this.variables.length {
val held := &mut this.variables[i];
if held.first != 0 {
held.first = below[held.first];
}
if held.end != 0 {
held.end = below[held.end];
}
}
# The instructions, each moving down to its number, their operands each in
# a run of its own, and the edges they leave by, each instruction's in a run
# as before. Read and written where the elements are, as this runs over
# every operand.
var made_operands := vector::<u32>::new();
made_operands.resize(operand_count);
val operands_to := made_operands.slice().data;
val operands_from := this.operands.view().data;
val values_to := value_to.view().data;
var made_edges := vector::<edge>::new();
made_edges.reserve(edge_count);
var edge_to := vector::<u32>::new(scratch());
edge_to.resize(this.edges.length, compacted_out);
val insts := this.insts.slice().data;
var placed: u64 = 0;
for n in 0..kept.length {
val i: u32 = kept[n];
var held: inst = insts[i];
val args := held.args as u64;
held.args = placed as u32;
for k in 0..held.argc as u64 {
operands_to[placed] = values_to[operands_from[args + k]];
placed++;
}
held.result = held.count == 0 ? 0 : values_to[held.result];
if i != 0 {
held.block = block_to[held.block];
held.next = inst_to[held.next];
}
val count := held.edge_count();
if count != 0 {
val first: u64 = held.aux;
held.aux = made_edges.length as u32;
for e in 0..count {
val old := first + e as u64;
edge_to[old] = made_edges.length as u32;
var args := move(&mut this.edges[old].args);
for k in 0..args.length {
args[k as u64] = values_to[args[k as u64]];
}
made_edges.append(edge {
from = n as u32, target = block_to[this.edges[old].target], args = move(&mut args), case = this.edges[old].case
});
}
}
insts[n] = held;
}
var made_insts := vector::<inst>::new();
made_insts.extend(span::<inst>::over(this.insts.view().data, kept.length));
this.insts = move(&mut made_insts);
# The blocks, their parameters and the edges into them numbered as they are
# now.
var made_blocks := vector::<block>::new();
made_blocks.reserve(block_from.length);
for &b in block_from {
var params := move(&mut this.blocks[b].params);
for k in 0..params.length {
params[k as u64] = values_to[params[k as u64]];
}
var preds := ids {};
for &pred in this.blocks[b].preds {
if edge_to[pred] != compacted_out {
preds.append(edge_to[pred]);
}
}
made_blocks.append(block {
first = inst_to[this.blocks[b].first], last = inst_to[this.blocks[b].last],
params = move(&mut params), preds = move(&mut preds)
});
}
# The dominators the code kept, where it kept them, numbered as the blocks.
var idom := vector::<u32>::new(scratch());
if this.idom_kept {
idom.resize(block_from.length, no_block);
for i in 0..block_from.length {
val dominator: u32 = this.kept_idom[block_from[i]];
idom[i] = dominator == no_block ? no_block : block_to[dominator];
}
}
this.operands = move(&mut made_operands);
this.blocks = move(&mut made_blocks);
this.edges = move(&mut made_edges);
this.keep_numbered_order(reached, block_from.length as u32, idom.view());
}