Files
sakurac/compiler/globals.sakura
T
2026-10-28 22:13:49 -05:00

159 lines
6.4 KiB
Plaintext

#! @file globals.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the globals: where each is among them, and `<globals>`, the
#! function the program runs before `main`, which gives each its value in the
#! order they are declared, after those its value reads. A constant is given
#! the value its declaration computes, as the program starts, until constants
#! are evaluated while compiling.
import sakura::analyzer;
module sakura::compiler;
#! Lowers `<globals>`, which gives every global of the program its value.
#! @param this The compiler.
#! @returns The function's index.
pub def lowerer::lower_globals(&mut this): u32 {
val void_type := this.checker.types.function(span::<u32>::empty(), bt::void_type, false);
val index := this.add_function(string::from("<globals>", nothing {}), this.signature_of(void_type));
this.program.functions[index].lowered = true;
this.program.functions[index].queued = true;
var made := emitter::new(this, index);
for id in 1..this.checker.decls.count {
made.lower_global(id as u32);
}
made.build.ret(span::<u32>::empty());
made.finish();
return index;
}
#! Whether a declaration is a global the program holds: a variable or constant
#! outside any function, or declared through a type, that checked.
#! @param this The compiler.
#! @param decl The declaration.
#! @returns Whether it is.
pub def lowerer::is_global(&this, decl: u32): bool {
return this.checker.decls.kinds.p[decl] == dk::variable && !this.checker.decls.has(decl, df::failed)
&& this.checker.decls.nodes.p[decl] != 0;
}
#! Gives a global its value in `<globals>`, after those its value reads.
#! @param this The emitter of `<globals>`.
#! @param decl The global's declaration.
pub def emitter::lower_global(&mut this, decl: u32): void {
return if !this.c.is_global(decl);
# A constant evaluated is held as data instead.
return if this.c.checker.decls.has(decl, df::constant) && this.c.evaluated.values.contains(&decl_key(decl));
return if !this.c.begun_globals.insert(decl);
val file: u32 = this.c.checker.decls.files.p[decl];
val saved := this.c.checker.at;
this.c.checker.use_table(this.c.checker.units[file].table);
this.table = this.c.checker.units[file].table;
val node: u32 = this.c.checker.decls.nodes.p[decl];
val value := this.c.checker.record(node, variable_slot::value);
# The globals its value reads are given theirs first.
if value != 0 {
var reads := vector::<u32>::new();
this.globals_read(value, &mut reads);
for &each in reads {
this.lower_global(each);
this.c.checker.use_table(this.table);
}
this.c.checker.use_table(this.c.checker.units[file].table);
this.table = this.c.checker.units[file].table;
}
this.at_node(node);
val kind: u32 = this.c.checker.decls.types.p[decl];
val mark := this.begin_statement();
val place := this.global_place(decl);
# The globals begin as zeros, so one without a value is only given the
# defaults of its fields, and a `val` of a value known while compiling
# begins with it, so code that runs before `<globals>` gives it, as the
# runtime's does where values are allocated, reads it.
if value == 0 {
this.defaults(&place);
} else if this.refers_to_temporary(value) {
# A value referred to where it is made lives as long as the global bound
# to it, as a global of its own.
val taken := this.c.checker.type_at(value);
val referent := this.c.checker.types.first_of(this.c.checker.unalias(taken));
val made := this.value_as(this.c.checker.left_of(value), referent);
this.adopt(&made);
val offset := this.c.unnamed_global(referent);
val address := this.build.value(op::global, span::<u32>::empty(), vt::word, 0, offset);
val held := location { kind = referent, address = address };
this.store(&held, &made);
val referred := this.reference_to(&held, taken);
val converted := this.convert(&referred, kind);
this.store(&place, &converted);
} else {
val made := this.value_as(value, kind);
this.adopt(&made);
val fixed := !this.c.checker.decls.has(decl, df::mutable);
if !fixed || !this.preset(&place, &made) {
this.store(&place, &made);
}
}
this.end_statement(mark);
this.c.checker.at = saved;
}
#! Gives a global the value it begins with, where that is a number known while
#! compiling, which it then has before any code runs.
#! @param this The emitter of `<globals>`.
#! @param place The global.
#! @param made Its value.
#! @returns Whether it was given, which it is not where the value is not known,
#! or not a number.
def emitter::preset(&mut this, place: &location, made: &operand): bool {
return false if made.count != 1 || made.address != 0 || place.address == 0 || place.offset != 0;
val at: origin = this.build.code.origins[place.address];
val value: origin = this.build.code.origins[made.words[0]];
return false if at.param || value.param || this.build.code.insts[at.from].kind != op::global;
val kind := this.build.code.insts[value.from].kind;
return false if kind != op::iconst && kind != op::fconst;
val bits: u64 = this.build.code.insts[value.from].imm;
val offset: u64 = this.build.code.insts[at.from].imm;
# Only an integer or an `f64` is written as its bytes: an `f32` would be
# narrowed first, so it is left to the store.
val how := this.c.shape_of(place.kind).how;
return false if how == access::bytes || how == access::f32 || how.value_type().lanes();
return false if kind == op::fconst && how != access::f64;
val width := how.width();
if this.c.program.preset.length < offset + width {
this.c.program.preset.resize(offset + width);
}
for i in 0..width {
this.c.program.preset[offset + i] = ((bits >> (8 * i)) & 0xFF) as u8;
}
return true;
}
#! Collects the globals an expression reads, by name.
#! @param this The emitter.
#! @param node The expression.
#! @param reads Where they are collected.
def emitter::globals_read(&mut this, node: u32, reads: &mut vector<u32>): void {
var walk := node_walk::new(node);
while walk.more() {
val at := walk.next();
val kind := this.c.checker.kind_of(at);
if kind == nk::name || kind == nk::path {
val ref := this.c.checker.ref_at(at);
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.is_global(ref) {
reads.append(ref);
}
}
continue if kind == nk::lambda;
walk.enter(this.c.checker.syn(), at);
}
}