#! @file flows.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains where the references of a body may go, as `escapes.sakura` finds #! it: the spots a reference may refer to, and the sets of spots the values of #! the body may hold references to. Each spot holds a set of its own, of what #! is stored in it. A set flows into another as a value is copied; reading #! through a set, what each of its spots holds flows into another; and writing #! through one, a set flows into what each of its spots holds. Two sets stand #! for where values leave the body: what may be kept past it, and what it gives #! back. #! #! Solving carries each spot along the flows until no set grows, as Andersen's #! analysis does, without telling the fields of a value apart. A spot then in #! the set of what is kept is kept, one in the set given back is given back, #! and so is every spot held by one that is, since whoever holds it may reach #! them. A spot of the frame held by one that may outlive it is kept too: a #! value a statement holds held by anything but another such value, or a local #! held by a local declared before it, which may be given another value later, #! as each pass of a loop gives one declared within it. import std::containers; module sakura::compiler; #! What a spot stands for. pub enum spot_kind : u8 { local, #! The storage of a local, a parameter, or a name a test or a loop gives. temp, #! A value a statement holds where a reference to it is taken. env, #! The environment of an anonymous function, which holds references to what it captures. outer, #! What a parameter taking a reference refers to, the caller's. inner, #! What the caller's references reach through a parameter, the caller's. shared, #! What any code may reach: the globals, the heap, and what pointers point at. } #! How far a spot may outlive the body, as solving finds. pub enum reach : u8 { stays, #! Not past the body. given, #! Given back, in what the body returns. kept, #! Kept past the body, as where it is stored. } #! A spot a reference may refer to. pub struct spot { var kind: spot_kind; #! What it stands for. var node: u32; #! The node declaring it, or making it. var held: u32; #! The set of what is stored in it. var alone: u32; #! A set of it alone, or 0 until one is asked for. var depth: u32; #! How many anonymous functions it is declared within. var reach: reach; #! How far it may outlive the body. var taken: bool; #! Whether a reference to it is made. } #! The spots and sets of a body, and the flows between them. pub struct flow { var spots: vector; #! The spots. var heads: vector; #! Of each set, one more than its first member's link, or 0. var member: vector; #! Of each link of a set's members, the spot. var next: vector; #! Of each link, one more than the next link of the same list, or 0. var copies: vector; #! Of each set, one more than the first link of the sets it flows into. var reads: vector; #! Of each set, one more than the first link of the sets what its spots hold flows into. var writes: vector; #! Of each set, one more than the first link of the sets that flow into what its spots hold. var known: hashset; #! The spots each set holds, by `pair_key(set, spot)`. var edges: hashset; #! The flows from set to set, by `pair_key(from, to)`. var work: vector; #! The spots added to sets not yet carried on, by `pair_key(set, spot)`. var kept: u32; #! The set of what may be kept past the body. var given: u32; #! The set of what the body may give back. var shared: u32; #! The spot of what any code may reach. } #! Makes the flows of a body, with nothing in them but the sets of what is kept #! and given back, and the spot any code may reach, which holds itself. #! @param alloc The optional allocator its memory is taken from. #! @returns The flows. pub def flow::new(alloc: maybe<&mut allocator>): flow { var made := flow { spots = vector::::new(alloc), heads = vector::::new(alloc), member = vector::::new(alloc), next = vector::::new(alloc), copies = vector::::new(alloc), reads = vector::::new(alloc), writes = vector::::new(alloc), known = hashset::::new(alloc), edges = hashset::::new(alloc), work = vector::::new(alloc) }; # Set 0 stands for none. made.heads.append(0); made.copies.append(0); made.reads.append(0); made.writes.append(0); made.kept = made.new_set(); made.given = made.new_set(); made.shared = made.new_spot(spot_kind::shared, 0, 0); made.add(made.spots[made.shared as u64].held, made.shared); return made; } #! Makes an empty set. #! @param this The flows. #! @returns The set. pub def flow::new_set(&mut this): u32 { val made := this.heads.length as u32; this.heads.append(0); this.copies.append(0); this.reads.append(0); this.writes.append(0); return made; } #! Makes a spot, holding nothing yet. #! @param this The flows. #! @param kind What it stands for. #! @param node The node declaring or making it. #! @param depth How many anonymous functions it is declared within. #! @returns The spot. pub def flow::new_spot(&mut this, kind: spot_kind, node: u32, depth: u32): u32 { val held := this.new_set(); val made := this.spots.length as u32; this.spots.append(spot { kind = kind, node = node, held = held, depth = depth }); return made; } #! The set of a spot alone, as a reference to it is. #! @param this The flows. #! @param at The spot. #! @returns The set. pub def flow::alone(&mut this, at: u32): u32 { val had: u32 = this.spots[at as u64].alone; return had if had != 0; val made := this.new_set(); this.spots[at as u64].alone = made; this.add(made, at); return made; } #! The set of what a spot holds. #! @param this The flows. #! @param at The spot. #! @returns The set. pub def flow::held_by(&this, at: u32): u32 { return this.spots[at as u64].held; } #! Whether what is written to a spot leaves the body: the caller's, or what any #! code may reach. #! @param this The flows. #! @param at The spot. #! @returns Whether it does. pub def flow::outside(&this, at: u32): bool { val kind: spot_kind = this.spots[at as u64].kind; return kind == spot_kind::outer || kind == spot_kind::inner || kind == spot_kind::shared; } #! Adds a spot to a set, to be carried along its flows. #! @param this The flows. #! @param set The set, or 0 for none. #! @param at The spot. pub def flow::add(&mut this, set: u32, at: u32): void { return if set == 0; val key := pair_key(set, at); return if !this.known.insert(key); this.member.append(at); this.next.append(this.heads[set as u64]); this.heads[set as u64] = this.member.length as u32; this.work.append(key); } #! Links a set into one of the lists a set has of where it flows. #! @param this The flows. #! @param head The first link of the list, which becomes the new one. #! @param set The set linked. def flow::link(&mut this, head: &mut u32, set: u32): void { this.member.append(set); this.next.append(head); head = this.member.length as u32; } #! Has a set flow into another: every spot one holds, the other does. #! @param this The flows. #! @param from The set flowing, or 0 for none. #! @param to The set it flows into, or 0 for none. pub def flow::into(&mut this, from: u32, to: u32): void { return if from == 0 || to == 0 || from == to; return if !this.edges.insert(pair_key(from, to)); var head: u32 = this.copies[from as u64]; this.link(&mut head, to); this.copies[from as u64] = head; var at: u32 = this.heads[from as u64]; while at != 0 { this.add(to, this.member[at as u64 - 1]); at = this.next[at as u64 - 1]; } } #! Has what each spot of a set holds flow into another set, as reading through #! the references the set stands for does. #! @param this The flows. #! @param from The set read through, or 0 for none. #! @param to The set what is read flows into, or 0 for none. pub def flow::read(&mut this, from: u32, to: u32): void { return if from == 0 || to == 0; var head: u32 = this.reads[from as u64]; this.link(&mut head, to); this.reads[from as u64] = head; var at: u32 = this.heads[from as u64]; while at != 0 { this.into(this.held_by(this.member[at as u64 - 1]), to); at = this.next[at as u64 - 1]; } } #! Has a set flow into what each spot of another holds, as writing through the #! references that set stands for does. What is written to a spot outside the #! body is kept. #! @param this The flows. #! @param at The set written through, or 0 for none. #! @param from The set written, or 0 for none. pub def flow::write(&mut this, at: u32, from: u32): void { return if at == 0 || from == 0; var head: u32 = this.writes[at as u64]; this.link(&mut head, from); this.writes[at as u64] = head; var each: u32 = this.heads[at as u64]; while each != 0 { val target: u32 = this.member[each as u64 - 1]; this.into(from, this.outside(target) ? this.kept : this.held_by(target)); each = this.next[each as u64 - 1]; } } #! A set of what reading through a set gives. #! @param this The flows. #! @param from The set, or 0 for none. #! @returns The set read, or 0 for none. pub def flow::read_of(&mut this, from: u32): u32 { return 0 if from == 0; val made := this.new_set(); this.read(from, made); return made; } #! A set of what either of two sets holds. #! @param this The flows. #! @param a One set, or 0 for none. #! @param b The other, or 0 for none. #! @returns The set, or 0 for none. pub def flow::either(&mut this, a: u32, b: u32): u32 { return b if a == 0; return a if b == 0; val made := this.new_set(); this.into(a, made); this.into(b, made); return made; } #! Has a set's spots be kept past the body. #! @param this The flows. #! @param set The set, or 0 for none. pub def flow::keep(&mut this, set: u32): void { this.into(set, this.kept); } #! Carries every spot added along the flows of the sets it was added to, until #! no set grows. #! @param this The flows. pub def flow::solve(&mut this): void { while this.work.length != 0 { val key: u64 = this.work.last(); this.work.truncate(); val set := pair_high(key); val at := pair_low(key); var link: u32 = this.copies[set as u64]; while link != 0 { this.add(this.member[link as u64 - 1], at); link = this.next[link as u64 - 1]; } link = this.reads[set as u64]; while link != 0 { this.into(this.held_by(at), this.member[link as u64 - 1]); link = this.next[link as u64 - 1]; } link = this.writes[set as u64]; while link != 0 { this.into(this.member[link as u64 - 1], this.outside(at) ? this.kept : this.held_by(at)); link = this.next[link as u64 - 1]; } } } #! Works out how far each spot may outlive the body, once solved: those kept or #! given back, those held by one that is, and those of the frame held by one #! that may outlive them. #! @param this The flows. pub def flow::settle(&mut this): void { var todo := vector::::new(); this.mark_all(this.given, reach::given, &mut todo); this.mark_all(this.kept, reach::kept, &mut todo); # A value a statement holds lives only while it runs, and a local only from # where it is declared on, so one held by what may outlive it is kept. for h in 0..this.spots.length { val holder: spot = this.spots[h]; continue if holder.kind != spot_kind::local && holder.kind != spot_kind::env && holder.kind != spot_kind::temp; var at: u32 = this.heads[holder.held as u64]; while at != 0 { val held: u32 = this.member[at as u64 - 1]; at = this.next[at as u64 - 1]; val kind: spot_kind = this.spots[held as u64].kind; val later := (kind == spot_kind::local || kind == spot_kind::env) && holder.kind != spot_kind::temp && this.spots[held as u64].node > holder.node; if (kind == spot_kind::temp && holder.kind != spot_kind::temp) || later { this.mark(held, reach::kept, &mut todo); } } } # Whoever holds a spot reaches what it holds. while todo.length != 0 { val at: u32 = todo.last(); todo.truncate(); val how: reach = this.spots[at as u64].reach; var link: u32 = this.heads[this.spots[at as u64].held as u64]; while link != 0 { this.mark(this.member[link as u64 - 1], how, &mut todo); link = this.next[link as u64 - 1]; } } } #! Marks each spot of a set as outliving the body at least as far as given. #! @param this The flows. #! @param set The set. #! @param how How far. #! @param todo The spots whose reach grew, whose held spots are marked next. def flow::mark_all(&mut this, set: u32, how: reach, todo: &mut vector): void { var at: u32 = this.heads[set as u64]; while at != 0 { this.mark(this.member[at as u64 - 1], how, todo); at = this.next[at as u64 - 1]; } } #! Marks a spot as outliving the body at least as far as given. #! @param this The flows. #! @param at The spot. #! @param how How far. #! @param todo The spots whose reach grew, whose held spots are marked next. def flow::mark(&mut this, at: u32, how: reach, todo: &mut vector): void { val had: reach = this.spots[at as u64].reach; return if (had as u8) >= (how as u8); this.spots[at as u64].reach = how; todo.append(at); }