#! @file globals.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the globals: where each is among them, and ``, 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 ``, 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::::blank(), bt::void_type, false); val index := this.add_function(string::from("", nothing {}), this.signature_of(void_type)); this.program.functions[index].lowered = true; this.program.functions[index].queued = true; # A library's globals nothing reached are no part of the program. var made := emitter::new(this, index); for id in 1..this.checker.decls.count { continue if !this.checker.decls.has(id as u32, df::checked); made.lower_global(id as u32, 0); } # Each instance of a variable of a generic type is a global of its own. for i in 1..this.checker.instances.length { val decl: u32 = this.checker.instances[i].decl; continue if this.checker.decls.kinds.p[decl] != dk::variable || this.checker.instances[i].table == 0; made.lower_global(decl, i as u32); } made.build.ret(span::::blank()); made.finish(); this.program.functions[index].finished = true; 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. One of a #! generic type is none, but each of its instances is. A constant a trait asks #! for is the type's, or its default each type's instance, and a sealed trait's #! each instance, but where the trait has no parameters, of which it is one. #! @param this The compiler. #! @param decl The declaration. #! @returns Whether it is. pub def lowerer::is_global(&this, decl: u32): bool { return false if this.checker.decls.kinds.p[decl] != dk::variable || this.checker.decls.has(decl, df::failed) || this.checker.decls.nodes.p[decl] == 0 || this.checker.decls.has(decl, df::generic); return !this.checker.trait_asks(decl) || this.checker.sealed_alone(decl); } #! Gives a global its value in ``, after those its value reads. #! @param this The emitter of ``. #! @param decl The global's declaration. #! @param instance Of a variable of a generic type, its instance, whose #! value is checked in the instance's table, or 0. pub def emitter::lower_global(&mut this, decl: u32, instance: u32): void { return if instance == 0 && !this.c.is_global(decl); val file: u32 = this.c.checker.decls.files.p[decl]; val node: u32 = this.c.checker.decls.nodes.p[decl]; val table: u32 = instance != 0 ? this.c.checker.instances[instance].table : this.c.checker.units[file].table; # A constant evaluated is held as data instead. val key := instance != 0 ? node_key(table, node) : decl_key(decl); return if this.c.checker.decls.has(decl, df::constant) && this.c.evaluated.values.contains(&key); return if !this.c.begun_globals.insert(instance != 0 ? instance | 0x80000000 : decl); val saved := this.c.checker.at; this.c.checker.use_table(table); this.table = table; 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::::new(); this.globals_read(value, &mut reads); for &each in reads { this.lower_global((each & 0xFFFFFFFF) as u32, (each >> 32) as u32); this.c.checker.use_table(this.table); } this.c.checker.use_table(table); this.table = table; } this.at_node(node); val kind: u32 = instance != 0 ? this.c.checker.instances[instance].kind : this.c.checker.decls.types.p[decl]; val mark := this.begin_statement(); val place := this.global_place(decl, instance); # 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 `` 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::::blank(), 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 ``. #! @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, each as its declaration, #! and of a variable of a generic type, its instance above it. #! @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): 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.checker.decls.kinds.p[ref] == dk::variable { val instance := this.called(at); if instance != 0 || this.c.is_global(ref) { reads.append((instance as u64 << 32) | ref as u64); } } } continue if kind == nk::lambda; walk.enter(this.c.checker.syn(), at); } }