Files
sakurac/compiler/report.sakura
T

113 lines
4.3 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file report.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what `check --checks-report` lists: the checks a checked program
#! makes that the optimizer could not leave out, in the code of the program's
#! own functions, each at the line of the function's own source it comes from.
#! A check inlined from std, as `vector::at`'s, is at the line of the call, so
#! the list says where an unchecked build trusts the program, and where a `get`
#! may be better than `[]`.
import sakura::analyzer;
import std::containers;
module sakura::compiler;
#! What a check stops the program for.
pub enum checked : u8 {
index, #! An index or a slice past the end, of a view, a fixed array, or a container.
field, #! The field a union does not hold, read.
member, #! An integer converted to an enum none of whose members has its value, or a `match` finding no arm.
fault, #! A contract, or a check std makes itself, which faults where it fails.
count, #! How many kinds there are; not a kind itself.
}
#! The checks of one kind a function's code makes at one line of its own
#! source.
pub struct check_site {
pub var function: u32; #! The function.
pub var at: u64; #! Where in its own source: one more than the file, then the byte of the code's own instruction or of the call it was inlined from.
pub var kind: checked; #! What they stop the program for.
pub var count: u32; #! How many there are.
}
#! The checks the code of the program's own functions makes, once lowered and
#! optimized, in the order of the functions and of where each is.
#! @param this The compiler.
#! @returns The checks, those of each place and kind together.
pub def lowerer::checks_left(&mut this): vector<check_site> {
var made := vector::<check_site>::new();
for f in 0..this.program.functions.length {
val held := &this.program.functions[f];
continue if !held.lowered;
val decl := held.decl != 0 ? held.decl : held.outer;
continue if decl == 0 || this.in_std(decl);
var found := dict::<u64, u32>::new();
var places := vector::<u64>::new();
var walk := held.body.walk_all();
while walk.more() {
val each: inst = held.body.insts[walk.next()];
val kind := this.check_kind(&each);
continue if kind == checked::count;
val at := own_place(&held.body, &each);
val key := (at << 3) | kind as u64;
if found.contains(&key) {
found[key] += 1;
} else {
found.set(key, 1);
places.append(key);
}
}
places.sort();
for &key in places {
made.append(check_site { function = f as u32, at = key >> 3, kind = (key & 7) as checked, count = found[key] });
}
}
return made;
}
#! What an instruction checks, or `checked::count` where it checks nothing.
#! @param this The compiler.
#! @param held The instruction.
#! @returns The kind.
def lowerer::check_kind(&mut this, held: &inst): checked {
match held.kind {
op::check => {
return match held.aux {
fault::out_of_bounds as u32 => checked::index,
fault::not_held as u32 => checked::field,
fault::not_member as u32 => checked::member,
else => checked::count
};
}
op::trap => return held.aux == fault::not_member as u32 ? checked::member : checked::count;
op::fault => return checked::fault;
op::call => {
# A container's own check calls `past_end` only where it fails.
val decl := this.program.functions[held.aux].decl;
return checked::count if decl == 0 || !this.in_std(decl);
return this.checker.names.text(this.checker.decls.names.p[decl]) == "past_end" ? checked::index : checked::count;
}
else => return checked::count;
}
}
#! Where an instruction is in the source of the function whose code holds it:
#! its own place, or, inlined, that of the outermost call it was inlined from.
#! @param body The code.
#! @param held The instruction.
#! @returns The place: one more than the file, then the byte.
def own_place(body: &code, held: &inst): u64 {
var site := held.site;
var at := held.at;
var steps := 0;
while site != 0 && steps < 256 {
val from: inline_site = body.sites[site - 1];
at = from.at;
site = from.parent;
steps++;
}
return at;
}