Files
sakurac/compiler/tracking.sakura
T

211 lines
7.0 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file tracking.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the small containers the compiler's passes keep track of what they
#! find with: a worklist, which takes each index once, a set emptied at once
#! each round, and a dictionary whose changes are taken back as a scope ends.
import std::containers;
module sakura::compiler;
#! A worklist of indices below a count, as of the blocks a walk reaches or the
#! values a pass finds needed: each is pushed once, and is known to have been
#! pushed after it is popped.
pub struct worklist {
var seen: vector<bool>; #! Whether each index was pushed.
var todo: vector<u32>; #! The indices pushed and not popped yet, the next last.
}
#! Makes an empty worklist.
#! @param count How many indices there are.
#! @param alloc The optional allocator its memory is taken from.
#! @returns The worklist.
pub def worklist::new(count: u64, alloc: maybe<&mut allocator>): worklist {
var made := worklist { seen = vector::<bool>::new(alloc), todo = vector::<u32>::new(alloc) };
made.seen.resize(count);
return made;
}
#! Whether no index pushed is left to pop.
#! @param this The worklist.
#! @returns Whether none is.
pub val worklist::is_empty: bool {
get => this.todo.length == 0;
}
#! Pushes an index, where it was never pushed before.
#! @param this The worklist.
#! @param index The index.
#! @returns Whether it was new.
pub def worklist::push_new(&mut this, index: u32): bool {
return false if this.seen[index];
this.push(index);
return true;
}
# Pushes an index never pushed before, apart from `push_new`, so that what
# asks whether it was is small enough to be inlined.
def worklist::push(&mut this, index: u32): void {
this.seen[index] = true;
this.todo.append(index);
}
#! Pops the index pushed last of those left.
#! @param this The worklist, which holds one, or else the program stops.
#! @returns The index.
pub def worklist::pop(&mut this): u32 {
val index: u32 = this.todo.last();
this.todo.truncate();
return index;
}
#! Whether an index was ever pushed.
#! @param this The worklist.
#! @param index The index.
#! @returns Whether it was.
pub def worklist::has(&this, index: u32): bool {
return this.seen[index];
}
#! A set of indices below a count, emptied at once as each round begins, as
#! what one function, or one block, of many counted: an index is in the set
#! where it was put in during the round.
pub struct stamped_set {
var stamps: vector<u32>; #! The round each index was last put in during, or 0.
var round: u32; #! The round now, from 1.
}
#! Makes an empty set, in its first round.
#! @param count How many indices there are.
#! @param alloc The optional allocator its memory is taken from.
#! @returns The set.
pub def stamped_set::new(count: u64, alloc: maybe<&mut allocator>): stamped_set {
var made := stamped_set { stamps = vector::<u32>::new(alloc), round = 1 };
made.stamps.resize(count);
return made;
}
#! Begins the next round, which empties the set.
#! @param this The set.
pub def stamped_set::next_round(&mut this): void {
this.round++;
}
#! Puts an index in the set, whether or not it is in it already.
#! @param this The set.
#! @param index The index.
pub def stamped_set::put(&mut this, index: u32): void {
this.stamps[index] = this.round;
}
#! Puts an index in the set.
#! @param this The set.
#! @param index The index.
#! @returns Whether it was new to the set this round.
pub def stamped_set::insert(&mut this, index: u32): bool {
return false if this.stamps[index] == this.round;
this.stamps[index] = this.round;
return true;
}
#! Whether an index was put in the set this round.
#! @param this The set.
#! @param index The index.
#! @returns Whether it was.
pub def stamped_set::contains(&this, index: u32): bool {
return this.stamps[index] == this.round;
}
#! A dictionary of plain keys and values, as indices, whose changes are taken
#! back as a scope ends: each value set is noted with what its key held before,
#! and rewinding to a mark puts back what each key held then, the last change
#! first, as a walk down a tree takes back what each node added as it leaves.
#! @tparam K The type of its keys, which a `dict` takes.
#! @tparam V The type of its values.
pub struct scoped_dict<K, V> {
var map: dict<K, V>; #! What each key holds now.
var changes: vector<scoped_dict<K, V>::change>; #! Each change made, in order.
}
#! A change a scoped dictionary made: the key set, and what it held before.
pub struct scoped_dict<K, V>::change {
var key: K; #! The key.
var old: V; #! The value it held before, where it held one.
var had: bool; #! Whether it held one.
}
#! Makes an empty dictionary.
#! @param alloc The optional allocator its memory is taken from.
#! @returns The dictionary.
pub def scoped_dict<K, V>::new(alloc: maybe<&mut allocator>): scoped_dict<K, V> {
return scoped_dict::<K, V> {
map = dict::<K, V>::new(alloc),
changes = vector::<scoped_dict<K, V>::change>::new(alloc)
};
}
#! Reads the value of a key.
#! @param this The dictionary.
#! @param key The key.
#! @returns Maybe the value; otherwise, `nothing`.
pub def scoped_dict<K, V>::get(&this, key: &K): maybe<&V> {
return this.map.get(key);
}
#! The value of a key, or another value where it holds none.
#! @param this The dictionary.
#! @param key The key.
#! @param fallback What is given where the key holds no value.
#! @returns The value, or `fallback`.
pub def scoped_dict<K, V>::get_or(&this, key: &K, fallback: V): V {
return this.map.get_or(key, fallback);
}
#! Whether a key holds a value.
#! @param this The dictionary.
#! @param key The key.
#! @returns Whether it does.
pub def scoped_dict<K, V>::contains(&this, key: &K): bool {
return this.map.contains(key);
}
#! Sets the value of a key, noting what it held before.
#! @param this The dictionary.
#! @param key The key.
#! @param value The value.
pub def scoped_dict<K, V>::set(&mut this, key: K, value: V): void {
if this.map.insert(key, value) {
this.changes.append(scoped_dict::<K, V>::change { key = key, old = value, had = false });
return;
}
this.changes.append(scoped_dict::<K, V>::change { key = key, old = this.map.get_or(&key, value), had = true });
this.map.set(key, value);
}
#! How many changes were made, which `rewind` takes back those made since.
#! @param this The dictionary.
#! @returns The mark.
pub def scoped_dict<K, V>::mark(&this): u64 {
return this.changes.length;
}
#! Takes back the changes made since a mark, the last first.
#! @param this The dictionary.
#! @param mark How many changes were made before.
pub def scoped_dict<K, V>::rewind(&mut this, mark: u64): void {
return if this.changes.length <= mark;
var at := this.changes.length;
while at > mark {
at--;
val each: scoped_dict<K, V>::change = this.changes[at];
if each.had {
this.map.set(each.key, each.old);
} else {
this.map.remove(&each.key);
}
}
this.changes.resize(mark);
}