246 lines
9.8 KiB
Plaintext
246 lines
9.8 KiB
Plaintext
#! @file storage.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains where a function's locals are held, worked out before its body is
|
||
|
|
#! lowered from where its references are made, as `escapes.sakura` finds it. A
|
||
|
|
#! local is held in variables of the builder unless a reference to it is made,
|
||
|
|
#! and then in a cell of the frame, which the reference refers to while the
|
||
|
|
#! local lives. Nothing is moved to the heap: a reference that outlives what it
|
||
|
|
#! refers to is the program's mistake, which the checker warns of where it is
|
||
|
|
#! sure of it.
|
||
|
|
|
||
|
|
import sakura::analyzer;
|
||
|
|
module sakura::compiler;
|
||
|
|
|
||
|
|
|
||
|
|
#! Works out where the locals of a function's body are held, given what its
|
||
|
|
#! parameters are.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param decl The function's declaration.
|
||
|
|
#! @param params The run of its parameters' nodes.
|
||
|
|
#! @param kind Its function type, given its type arguments.
|
||
|
|
#! @param body The body's node.
|
||
|
|
pub def emitter::plan_function(&mut this, decl: u32, params: u32, kind: u32, body: u32): void {
|
||
|
|
var s := escape_scan::new(scratch());
|
||
|
|
s.result = this.c.checker.types.first_of(kind);
|
||
|
|
this.c.scan_params(&mut s, params, kind, this.c.packs_last(decl));
|
||
|
|
this.c.scan_body(&mut s, body);
|
||
|
|
this.apply_plan(&mut s, body);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Works out where the locals of an anonymous function's body are held. What it
|
||
|
|
#! captures is the function's around it, reached through its environment, as
|
||
|
|
#! what a parameter refers to is the caller's.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param node The `lambda` node.
|
||
|
|
pub def emitter::plan_lambda(&mut this, node: u32): void {
|
||
|
|
var s := escape_scan::new(scratch());
|
||
|
|
val kind := this.c.checker.type_at(node);
|
||
|
|
s.result = this.c.checker.types.first_of(kind);
|
||
|
|
var captures := this.captures_of(node);
|
||
|
|
for &each in captures {
|
||
|
|
val outer := s.graph.new_spot(spot_kind::outer, each, 0);
|
||
|
|
val inner := s.graph.new_spot(spot_kind::inner, each, 0);
|
||
|
|
s.graph.add(s.graph.held_by(outer), inner);
|
||
|
|
s.graph.add(s.graph.held_by(inner), inner);
|
||
|
|
s.slots.set(each, outer);
|
||
|
|
}
|
||
|
|
this.c.scan_params(&mut s, this.c.checker.left_of(node), kind, false);
|
||
|
|
val body := this.c.checker.record(node, lambda_slot::body);
|
||
|
|
this.c.scan_body(&mut s, body);
|
||
|
|
this.apply_plan(&mut s, body);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Works out where the locals of a value evaluated into a global are held,
|
||
|
|
#! which keeps what it holds.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param value The value's node.
|
||
|
|
pub def emitter::plan_storage(&mut this, value: u32): void {
|
||
|
|
var s := escape_scan::new(scratch());
|
||
|
|
s.graph.keep(this.c.value_flow(&mut s, value));
|
||
|
|
this.apply_plan(&mut s, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Holds in a cell each local a reference is made to. What is within an
|
||
|
|
#! anonymous function is left to its own plan.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param s The scan.
|
||
|
|
#! @param body The body scanned.
|
||
|
|
def emitter::apply_plan(&mut this, s: &mut escape_scan, body: u32): void {
|
||
|
|
for at in 0..s.graph.spots.length {
|
||
|
|
val held: spot = s.graph.spots[at];
|
||
|
|
continue if held.depth != 0;
|
||
|
|
if held.kind == spot_kind::local && held.taken {
|
||
|
|
this.raise(held.node, held_as::cell);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.check_plan(s, body);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Holds in a cell any local a reference is made to that the scan did not see
|
||
|
|
#! made, which it always sees, so no reference refers to a local held in
|
||
|
|
#! variables: the operand of `&`, what a parameter taking a reference is given,
|
||
|
|
#! a method's receiver, the operands of an operator's method, what a loop
|
||
|
|
#! walks, a union a match names a field of by `&`, and what an anonymous
|
||
|
|
#! function captures.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param s The scan.
|
||
|
|
#! @param body The body scanned.
|
||
|
|
def emitter::check_plan(&mut this, s: &mut escape_scan, body: u32): void {
|
||
|
|
var walk := node_walk::new(body);
|
||
|
|
while walk.more() {
|
||
|
|
val at := walk.next();
|
||
|
|
# A contract left out refers to nothing.
|
||
|
|
continue if this.c.checker.contracts_off && this.c.checker.kind_of(at) == nk::contract;
|
||
|
|
match this.c.checker.kind_of(at) {
|
||
|
|
nk::reference => {
|
||
|
|
val inner := this.c.checker.left_of(at);
|
||
|
|
if this.c.checker.is_place(inner) && (!this.c.checker.is_known(inner) || this.stored(inner)) {
|
||
|
|
this.check_taken(s, inner);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::lambda => {
|
||
|
|
var captures := this.captures_of(at);
|
||
|
|
for &each in captures {
|
||
|
|
this.check_local(s, each);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::member => {
|
||
|
|
# A property's accessors are given a reference to the value.
|
||
|
|
if this.property_of(at) != 0 {
|
||
|
|
this.check_taken(s, this.c.checker.left_of(at));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::index => {
|
||
|
|
# So is `at`, but for what the language indexes itself.
|
||
|
|
val ref := this.c.checker.ref_at(at);
|
||
|
|
val through := this.c.checker.unalias(this.c.checker.reached(this.object_type(this.c.checker.left_of(at))));
|
||
|
|
val kind := this.c.checker.types.kind_of(through);
|
||
|
|
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::function
|
||
|
|
&& kind != ty::fixed && kind != ty::pointer && kind != ty::pack && this.c.viewed(through) == 0 {
|
||
|
|
this.check_taken(s, this.c.checker.left_of(at));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::binary, nk::assign => {
|
||
|
|
if this.operator_method(at) != 0 {
|
||
|
|
this.check_taken(s, this.c.checker.left_of(at));
|
||
|
|
this.check_taken(s, this.c.checker.right_of(at));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::unary => {
|
||
|
|
val token := this.c.checker.token_kind(at);
|
||
|
|
if (token == tk::minus || token == tk::bitnot) && this.operator_decl(at) != 0 {
|
||
|
|
this.check_taken(s, this.c.checker.left_of(at));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::for_stmt => {
|
||
|
|
val iterated := this.c.checker.record(at, for_slot::iterated);
|
||
|
|
val through := this.c.checker.unalias(this.c.checker.type_at(iterated));
|
||
|
|
if this.c.checker.record(at, for_slot::end) == 0 && !this.c.checker.node_has(at, nf::constant)
|
||
|
|
&& this.c.viewed(through) == 0 && this.c.checker.types.kind_of(through) != ty::pointer {
|
||
|
|
this.check_taken(s, iterated);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::is_test => {
|
||
|
|
val binding := this.c.checker.record(at, is_slot::binding);
|
||
|
|
if binding != 0 && this.c.checker.kind_of(binding) == nk::reference {
|
||
|
|
this.check_taken(s, this.c.checker.left_of(at));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
nk::match_stmt, nk::match_value => {
|
||
|
|
if this.c.refers_in_arm(at) {
|
||
|
|
this.check_taken(s, this.c.checker.left_of(at));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else => {}
|
||
|
|
}
|
||
|
|
if this.c.checker.has_flag(at, rf::borrowed) {
|
||
|
|
this.check_taken(s, at);
|
||
|
|
}
|
||
|
|
if this.c.checker.kind_of(at) != nk::lambda {
|
||
|
|
this.c.scan_children(at, &mut walk);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Holds in a cell the local whose own storage a place is within, past fields
|
||
|
|
#! and the elements of fixed arrays, where a reference to it is made that the
|
||
|
|
#! scan did not see.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param s The scan.
|
||
|
|
#! @param node The place's node.
|
||
|
|
def emitter::check_taken(&mut this, s: &mut escape_scan, node: u32): void {
|
||
|
|
var at := node;
|
||
|
|
var steps := 0;
|
||
|
|
while at != 0 && steps < 64 {
|
||
|
|
steps++;
|
||
|
|
val kind := this.c.checker.kind_of(at);
|
||
|
|
if kind == nk::member {
|
||
|
|
val ref := this.c.checker.ref_at(at);
|
||
|
|
return if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.c.checker.decls.kinds.p[ref] != dk::field;
|
||
|
|
val inner := this.c.checker.left_of(at);
|
||
|
|
return if this.c.is_reference_type(this.c.checker.type_at(inner));
|
||
|
|
at = inner;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if kind == nk::index {
|
||
|
|
val inner := this.c.checker.left_of(at);
|
||
|
|
return if this.c.checker.types.kind_of(this.c.checker.unalias(this.c.checker.type_at(inner))) != ty::fixed;
|
||
|
|
at = inner;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
return if at == 0 || this.c.checker.kind_of(at) != nk::name;
|
||
|
|
val ref := this.c.checker.ref_at(at);
|
||
|
|
return if (ref & ref_local) == 0 || this.c.is_reference_type(this.c.checker.type_at(at));
|
||
|
|
this.check_local(s, ref & ref_mask);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Holds a local in a cell where the scan did not see a reference to it made.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param s The scan.
|
||
|
|
#! @param local The node declaring the local.
|
||
|
|
def emitter::check_local(&mut this, s: &mut escape_scan, local: u32): void {
|
||
|
|
if s.slots.get(&local) is some(at) {
|
||
|
|
return if s.graph.spots[at as u64].taken || s.graph.spots[at as u64].kind != spot_kind::local;
|
||
|
|
}
|
||
|
|
this.raise(local, held_as::cell);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Holds a local at least as the way given: a cell over variables.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param local The node declaring the local.
|
||
|
|
#! @param how How it must be held.
|
||
|
|
def emitter::raise(&mut this, local: u32, how: held_as): void {
|
||
|
|
val held := this.storage.get_or(&local, held_as::words);
|
||
|
|
if (how as u8) > (held as u8) {
|
||
|
|
this.storage.set(local, how);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The method an operator calls, where the language does not operate on its
|
||
|
|
#! operands itself: the one the checker chose for arithmetic, or the `equals`
|
||
|
|
#! or `compare` of what implements `equatable` or `ordered`, or 0.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param node The `binary` or `assign` node.
|
||
|
|
#! @returns The method's declaration, or 0.
|
||
|
|
pub def emitter::operator_method(&mut this, node: u32): u32 {
|
||
|
|
return 0 if this.c.checker.kind_of(node) == nk::unary;
|
||
|
|
val operator := this.c.checker.token_kind(node);
|
||
|
|
return 0 if operator == tk::logand || operator == tk::logor || operator == tk::assign;
|
||
|
|
val chosen := this.operator_decl(node);
|
||
|
|
return chosen if chosen != 0;
|
||
|
|
val kind := this.c.checker.type_at(this.c.checker.left_of(node));
|
||
|
|
val plain := this.c.checker.unalias(this.c.layout_type(this.c.checker.read_type(kind)));
|
||
|
|
val named := this.c.checker.named_kind(plain);
|
||
|
|
return 0 if named != dk::struct_type && named != dk::union_type;
|
||
|
|
|
||
|
|
if operator.compares() {
|
||
|
|
val equal := operator == tk::equals || operator == tk::noteq;
|
||
|
|
return this.pick_method(plain, equal ? word::equals : word::compare, true);
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|