#! @file keeps.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what a function may do with what it is given past a call, its #! summary: of each parameter, whether the reference it is given may be kept, #! as where the function stores it, or given back in what the function returns; #! and whether what that reference reaches, or what a value given holds, may #! be. A local a reference is taken of lives in a cell of the frame, which #! keeps its address for as long as the function runs, where the reference may #! be kept past the statement taking it, and in its slots otherwise, so the #! summaries decide where the locals of every caller are held. #! #! A function's summary comes from scanning its body, as `escapes.sakura` does, #! with what each parameter is given standing as spots of the caller's. A #! reference held in a value is followed where the value goes, so a function #! giving back a value holding a reference to what it was given only gives the #! reference back, which its caller follows in turn. Anything the scan does not #! recognize keeps what it is given. #! #! Functions calling one another are summarized together, as Tarjan's algorithm #! finds them: a function whose scan is not done is taken to do what was found #! of it so far, nothing at first, and each function reading that is noted as #! its reader. Once the first function of such a group is scanned, each of the #! group whose summary grew after a reader read it has that reader scanned #! again, until none grows, and each is then summarized for good. A function is #! so scanned again only for what it read, never for what it calls being #! scanned again. import sakura::analyzer; module sakura::compiler; #! What a function may do with what each parameter gives it, one bit for each #! parameter, `this` first. A parameter at 62 or past is taken to keep all. pub struct keeping { var kept: u64; #! Whose reference may be kept past the call. var given: u64; #! Whose reference may be given back, in what the call gives. var reached: u64; #! Of whose reference what it reaches, or of whose value what it holds, may be kept. var passed: u64; #! Of whose reference what it reaches, or of whose value what it holds, may be given back. } #! The summary of a function that may do anything with what it is given. #! @returns The summary. pub def keeping::all(): keeping { return keeping { kept = 0xFFFFFFFFFFFFFFFF, given = 0xFFFFFFFFFFFFFFFF, reached = 0xFFFFFFFFFFFFFFFF, passed = 0xFFFFFFFFFFFFFFFF }; } #! Whether a summary does all another does. #! @param this The summary. #! @param other The other. #! @returns Whether it does. pub def keeping::covers(&this, other: &keeping): bool { return (this.kept | other.kept) == this.kept && (this.given | other.given) == this.given && (this.reached | other.reached) == this.reached && (this.passed | other.passed) == this.passed; } #! A function whose summary is being found. pub struct pending_scan { var key: u64; #! The function, as `pair_key(decl, instance)`. var decl: u32; #! Its declaration. var instance: u32; #! One more than the instance scanned, or 0. var found: keeping; #! What was found of it so far. var low: u32; #! The earliest pending function it reaches, by its place. var stale: bool; #! Whether a summary it read grew since it read it. var readers: u32; #! One more than the first link of the functions that read its summary, or 0. } #! What `scanning` holds where no summary is being scanned. const no_scan: u32 = 0xFFFFFFFF; #! The summary of a function, or of an instance of a generic one, found where #! it is new. A C function is given only an address, good until it returns, #! and nothing is taken as kept. #! What has no body to scan, as an intrinsic or a trait's method, may do #! anything, but for an instance of a default compiled for each type, which is #! scanned as the type's own method is. #! @param this The compiler. #! @param decl The function's declaration. #! @param instance One more than the instance called, or 0. #! @returns The summary. pub def lowerer::keeps_of(&mut this, decl: u32, instance: u32): keeping { return keeping::all() if decl == 0 || (decl & (ref_local | ref_type)) != 0 || this.checker.decls.kinds.p[decl] != dk::function; this.checker.resolve(decl); return keeping {} if this.checker.decls.has(decl, df::foreign) && !this.checker.has_body(decl); return keeping::all() if this.checker.decls.has(decl, df::intrinsic) || !this.checker.has_body(decl) || (this.trait_of_method(decl) != 0 && (instance == 0 || !this.checker.typed_default(decl))); val key := pair_key(decl, instance); if this.summaries.get(&key) is some(got) { return got; } if this.pending_ids.get(&key) is some(at) { this.lean_on(at); return this.pending[at as u64].found; } return this.summarize(decl, instance, key); } #! The summary of the function a call calls: a trait's method without a body, #! called on a value whose type is known, is the method of that type the #! vtable holds. #! @param this The compiler. #! @param decl The function's declaration. #! @param instance One more than the instance called, or 0. #! @param object The node of the value a method is called on, or 0. #! @returns The summary. pub def lowerer::keeps_of_call(&mut this, decl: u32, instance: u32, object: u32): keeping { if object != 0 && instance == 0 && decl != 0 && (decl & (ref_local | ref_type)) == 0 && this.checker.typed_default(decl) { return this.keeps_of_method(decl, this.checker.reached(this.checker.type_at(object))); } if object != 0 && decl != 0 && (decl & (ref_local | ref_type)) == 0 && this.trait_of_method(decl) != 0 && !this.checker.has_body(decl) { val callee := this.trait_callee(decl, this.checker.reached(this.checker.type_at(object))); return keeping::all() if callee == 0; return this.keeps_of(pair_high(callee), pair_low(callee)); } return this.keeps_of(decl, instance); } #! The summary of a method the compiler calls itself on a value of a type, as a #! loop's `next`, the instance the type's arguments give. #! @param this The compiler. #! @param decl The method's declaration, or 0. #! @param owner The type it is called on. #! @returns The summary. pub def lowerer::keeps_of_method(&mut this, decl: u32, owner: u32): keeping { return keeping::all() if decl == 0; this.checker.resolve(decl); if this.checker.typed_default(decl) { val plain := this.checker.unalias(owner); val holder := this.checker.enclosing_trait(decl); val made := this.checker.default_instance(decl, this.checker.implemented(plain, holder), plain, span::::empty()); return made == 0 ? keeping::all() : this.keeps_of(decl, made); } if this.trait_of_method(decl) != 0 { return keeping::all() if this.checker.has_body(decl); val callee := this.trait_callee(decl, owner); return keeping::all() if callee == 0; return this.keeps_of(pair_high(callee), pair_low(callee)); } return this.keeps_of(decl, this.method_instance(decl, owner)); } #! The method a trait's method without a body calls on a value of a known type, #! as its vtable holds it. #! @param this The compiler. #! @param decl The trait's method. #! @param receiver The type of the value, read through its references. #! @returns The method and its instance, as `pair_key(decl, instance)`, or 0 #! where the type is not known, or the method is a default. def lowerer::trait_callee(&mut this, decl: u32, receiver: u32): u64 { val plain := this.checker.unalias(receiver); return 0 if this.checker.types.kind_of(plain) != ty::named || this.checker.named_kind(plain) == dk::trait_type; val implemented := this.checker.implemented(plain, this.checker.enclosing_trait(decl)); val found := this.implementation(plain, decl, implemented); return 0 if found == 0 || this.trait_of_method(found) != 0; return pair_key(found, this.method_instance(found, plain)); } #! The instance of a method a type's arguments give, as `method_target` finds #! it. #! @param this The compiler. #! @param decl The method's declaration. #! @param owner The type it is called on. #! @returns One more than the instance, or 0. pub def lowerer::method_instance(&mut this, decl: u32, owner: u32): u32 { return 0 if decl == 0; this.checker.resolve(decl); return 0 if this.trait_of_method(decl) != 0 || !this.checker.decls.has(decl, df::generic); val plain := this.checker.unalias(owner); var args := vector::::new(); if this.checker.types.kind_of(plain) == ty::named { for i in 0..this.checker.types.part_count(plain) { args.append(this.checker.types.part_at(plain, i as u32)); } } return this.checker.instance_of(decl, args.view(), 0); } #! The type of a method called on a value of a type, as `method_target` gives #! it. #! @param this The compiler. #! @param decl The method's declaration. #! @param instance One more than its instance, as `method_instance` gives it. #! @param owner The type it is called on. #! @returns The function type. pub def lowerer::method_kind(&mut this, decl: u32, instance: u32, owner: u32): u32 { return bt::error if decl == 0; val plain := this.checker.unalias(owner); return this.checker.trait_method_type(decl, plain) if this.trait_of_method(decl) != 0; return this.checker.instances[instance - 1].kind if instance != 0; return this.checker.method_type(decl, plain) if this.checker.decls.has(decl, df::generic); return this.checker.decls.types.p[decl]; } #! Notes that the scan being made read the summary of a pending function, which #! it then reaches, and must be scanned again where that summary grows. #! @param this The compiler. #! @param at The pending function, by its place. def lowerer::lean_on(&mut this, at: u32): void { # A function calling itself reads its own summary too. val reader := this.scanning; return if reader == no_scan; if at < this.pending[reader as u64].low { this.pending[reader as u64].low = at; } return if !this.read_pairs.insert(pair_key(at, reader)); this.reader_of.append(reader); this.reader_next.append(this.pending[at as u64].readers); this.pending[at as u64].readers = this.reader_of.length as u32; } #! Finds the summary of a function not yet scanned: scans it, and where it is #! the first of a group calling back into one another, settles the group. #! @param this The compiler. #! @param decl The function's declaration. #! @param instance One more than the instance, or 0. #! @param key The function, as `pair_key(decl, instance)`. #! @returns What was found of it, for good where its group is settled. def lowerer::summarize(&mut this, decl: u32, instance: u32, key: u64): keeping { val at := this.pending.length as u32; this.pending.append(pending_scan { key = key, decl = decl, instance = instance, low = at }); this.pending_ids.set(key, at); val outer := this.scanning; this.scanning = at; val found := this.summary_scan(decl, instance); this.scanning = outer; this.grow(at, &found); if this.pending[at as u64].low == at { this.settle_group(at); return this.summaries.get_or(&key, keeping::all()); } # Still pending, as the group it is in is: the scan around it reaches what it # reaches, and read what it found. if outer != no_scan && this.pending[at as u64].low < this.pending[outer as u64].low { this.pending[outer as u64].low = this.pending[at as u64].low; } this.lean_on(at); return this.pending[at as u64].found; } #! Adds to what was found of a pending function, and has each function that read #! it scanned again where that grew. #! @param this The compiler. #! @param at The pending function, by its place. #! @param found What a scan found of it. def lowerer::grow(&mut this, at: u32, found: &keeping): void { val had: keeping = this.pending[at as u64].found; return if had.covers(found); this.pending[at as u64].found = keeping { kept = had.kept | found.kept, given = had.given | found.given, reached = had.reached | found.reached, passed = had.passed | found.passed }; var link: u32 = this.pending[at as u64].readers; while link != 0 { this.pending[this.reader_of[link as u64 - 1] as u64].stale = true; link = this.reader_next[link as u64 - 1]; } } #! Settles a group of functions calling back into one another, from its first, #! once that is scanned: each that read a summary which grew since is scanned #! again, until none grows, and each is then summarized for good. A group that #! will not settle, which never happens, is taken to do anything. #! @param this The compiler. #! @param root The group's first function, by its place. def lowerer::settle_group(&mut this, root: u32): void { var rounds: u64 = 0; var more := true; while more { more = false; var m := root as u64; while m < this.pending.length { if this.pending[m].stale { this.pending[m].stale = false; more = true; val outer := this.scanning; this.scanning = m as u32; val found := this.summary_scan(this.pending[m].decl, this.pending[m].instance); this.scanning = outer; this.grow(m as u32, &found); rounds++; } m++; } if rounds > 64 * (this.pending.length - root as u64) + 1024 { for k in root as u64..this.pending.length { this.pending[k].found = keeping::all(); } break; } } for k in root as u64..this.pending.length { val held: pending_scan = this.pending[k]; this.summaries.set(held.key, held.found); this.pending_ids.remove(&held.key); } this.pending.resize(root as u64); if this.pending.length == 0 { this.reader_of.clear(); this.reader_next.clear(); this.read_pairs.clear(); } } #! Scans a function's body for what it does with what it is given, each #! parameter given spots of the caller's, and the scan's memory freed once the #! summary is read from it. #! @param this The compiler. #! @param decl The function's declaration. #! @param instance One more than the instance scanned, or 0. #! @returns What it was found to do. def lowerer::summary_scan(&mut this, decl: u32, instance: u32): keeping { val saved := this.checker.at; this.checker.use_table(this.body_table(decl, instance)); val node: u32 = this.checker.decls.nodes.p[decl]; val accessor := this.checker.decls.has(decl, df::accessor); val body := accessor ? this.checker.record(node, accessor_slot::body) : this.checker.record(node, function_slot::body); val params := accessor ? this.checker.left_of(node) : this.checker.record(node, function_slot::params); val kind := this.type_of_function(decl, instance); val point := mark_scratch(); var made := keeping {}; { var s := escape_scan::new(scratch()); s.result = this.checker.types.first_of(kind); this.scan_params(&mut s, params, kind, this.packs_last(decl)); this.scan_body(&mut s, body); s.graph.solve(); s.graph.settle(); val count := this.checker.types.part_count(kind) as u64; for i in 0..(count < 62 ? count : 62) { val bit := 1u << i; val outer: u32 = i < s.outers.length ? s.outers[i] : 0; val inner: u32 = i < s.inners.length ? s.inners[i] : 0; # What a pack takes, or what no parameter names, may be anything. if inner == 0 { made.kept |= bit; made.given |= bit; made.reached |= bit; made.passed |= bit; continue; } if outer != 0 { val went: reach = s.graph.spots[outer as u64].reach; if went == reach::kept { made.kept |= bit; } else if went == reach::given { made.given |= bit; } } val within: reach = s.graph.spots[inner as u64].reach; if within == reach::kept { made.reached |= bit; } else if within == reach::given { made.passed |= bit; } } } release_scratch(point); this.checker.at = saved; return made; } #! Whether a function's last parameter is a pack, which takes every parameter #! from its own on. #! @param this The compiler. #! @param decl The function's declaration. #! @returns Whether it is. pub def lowerer::packs_last(&mut this, decl: u32): bool { val written: u32 = this.checker.decls.types.p[decl]; val count := this.checker.types.part_count(written); return this.checker.decls.has(decl, df::variadic) && count != 0 && this.checker.types.is_open_pack(this.checker.types.part_at(written, count - 1)); } #! The method an arithmetic operator calls, as the checker chose it, or 0 where #! the language operates on its operands itself. #! @param this The compiler. #! @param node The operator's node. #! @returns The method's declaration, or 0. def lowerer::operator_called(&mut this, node: u32): u32 { val kind := this.checker.kind_of(node); return 0 if kind == nk::unary && this.checker.token_kind(node) != tk::minus && this.checker.token_kind(node) != tk::bitnot; val ref := this.checker.ref_at(node); return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.checker.decls.kinds.p[ref] != dk::function; return ref; } #! The instance of a generic a call or an operator's method uses, as the #! checker chose it. #! @param this The compiler. #! @param node The call's node. #! @returns One more than the instance, or 0. pub def lowerer::called(&mut this, node: u32): u32 { val key := this.checker.result_key(node); return this.checker.called.get_or(&key, 0); } #! Whether a type is a reference. #! @param this The compiler. #! @param kind The type. #! @returns Whether it is. pub def lowerer::is_reference_type(&mut this, kind: u32): bool { return kind != 0 && this.checker.types.kind_of(this.checker.unalias(kind)) == ty::reference; }