467 lines
17 KiB
Plaintext
467 lines
17 KiB
Plaintext
#! @file variables.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains where the variables debugging information shows are in a
|
|
#! function's code once it is selected. A variable held in memory is where its
|
|
#! cell, or its copy of a parameter, is in the frame, over the code its scope
|
|
#! covers. One held in values, as optimized code holds a local of words, is
|
|
#! where the values it took are: each binding takes effect where it was made,
|
|
#! or where its block begins where optimizing took that out; a block begins
|
|
#! with what every block before it ended with where they agree, or with the
|
|
#! parameter they each give that; and where each value is at each instruction
|
|
#! is where the registers' placing put it, a register or its slot, while it
|
|
#! lives, or a constant, which is known anywhere, as is one a binding keeps
|
|
#! once its instruction went.
|
|
|
|
import sakura::compiler;
|
|
import binary::amd64;
|
|
module sakura::native;
|
|
|
|
|
|
#! The most words a variable held in values has.
|
|
const held_words: u64 = 3;
|
|
|
|
#! Notes where each variable of the function is, as the file says.
|
|
#! @param this The selector.
|
|
#! @param spans Where each instruction's code begins, then ends.
|
|
def selector::place_variables(&mut this, spans: span<u32>): void {
|
|
val size := (this.b.text.length - this.start) as u32;
|
|
var held := false;
|
|
for v in 0..this.body.variables.length {
|
|
val each: variable_info = this.body.variables[v];
|
|
if each.held {
|
|
held = true;
|
|
continue;
|
|
}
|
|
# One whose cell went, as optimizing takes out a cell nothing reads, is
|
|
# nowhere.
|
|
var below: u64 = 0;
|
|
if each.cell != 0 {
|
|
below = this.cells[each.cell as u64 - 1];
|
|
} else if each.given != 0 {
|
|
below = this.copies[each.given as u64 - 1];
|
|
}
|
|
continue if below == 0;
|
|
var from: u32 = 0;
|
|
var to: u32 = size;
|
|
if !each.param && (each.first != 0 || each.end != 0) {
|
|
from = size;
|
|
to = 0;
|
|
val end := each.end != 0 ? each.end : this.body.insts.length as u32;
|
|
for i in each.first..end {
|
|
break if 2 * i as u64 + 1 >= spans.length;
|
|
val begins := spans[2 * i as u64];
|
|
val ends := spans[2 * i as u64 + 1];
|
|
continue if ends <= begins;
|
|
from = begins < from ? begins : from;
|
|
to = ends > to ? ends : to;
|
|
}
|
|
}
|
|
# The frame's canonical address is past the saved `rbp` and the return
|
|
# address.
|
|
this.b.debug.variables.append(placed_variable {
|
|
name = each.name,
|
|
at = each.at,
|
|
layout = each.layout,
|
|
offset = -(below as i64) - 16,
|
|
param = each.param,
|
|
boxed = each.boxed,
|
|
site = each.site,
|
|
from = from,
|
|
to = to
|
|
});
|
|
}
|
|
if held {
|
|
this.place_held(spans);
|
|
}
|
|
}
|
|
|
|
#! Where a value is while an instruction runs, as a word of a variable: where
|
|
#! a call is, where it is past the call, as what lives across a call is in a
|
|
#! register the call keeps, which the frames below say where they saved, or in
|
|
#! its slot.
|
|
#! @param this The selector.
|
|
#! @param value The value.
|
|
#! @param index The instruction.
|
|
#! @param kind Where the kind of place is written.
|
|
#! @returns The register, the offset from the frame's canonical address, or the
|
|
#! bits.
|
|
def selector::word_spot_of(&this, value: u32, index: u32, kind: &mut u8): u64 {
|
|
kind = word_spot::nowhere as u8;
|
|
|
|
# A constant is known, made where it is read or not at all, as is one a
|
|
# binding keeps, which the binding, numbered past the values, stands for.
|
|
val values := this.body.origins.length;
|
|
if value as u64 >= values {
|
|
val bound: binding = this.body.bindings[value as u64 - values];
|
|
kind = (bound.known == vt::real ? word_spot::real : word_spot::word) as u8;
|
|
return bound.bits;
|
|
}
|
|
val maker := this.body.defining(value);
|
|
if maker != 0 {
|
|
val made: inst = this.body.insts[maker];
|
|
if made.kind == op::iconst {
|
|
kind = word_spot::word as u8;
|
|
return made.imm;
|
|
}
|
|
if made.kind == op::fconst {
|
|
kind = word_spot::real as u8;
|
|
return made.imm;
|
|
}
|
|
}
|
|
|
|
# Elsewhere, it is where its part there is, and nowhere outside its life.
|
|
val position := 4 * this.p.slots[index] + (calls(this.body.insts[index].kind) ? 3 : 1);
|
|
val first: u32 = this.p.child_first[value];
|
|
val past: u32 = this.p.child_first[value + 1];
|
|
var part: u32 = past;
|
|
for c in first..past {
|
|
if this.p.child_starts[c as u64] <= position && position < this.p.child_ends[c as u64] {
|
|
part = c;
|
|
}
|
|
}
|
|
return 0 if part == past;
|
|
val held: loc = this.p.child_locs[part as u64];
|
|
match held.kind {
|
|
place::gpr => {
|
|
kind = word_spot::gpr as u8;
|
|
return held.number as u64;
|
|
}
|
|
place::xmm => {
|
|
kind = word_spot::xmm as u8;
|
|
return held.number as u64;
|
|
}
|
|
place::slot => {
|
|
kind = word_spot::frame as u8;
|
|
return (this.slot(value).disp as i64 - 16) as u64;
|
|
}
|
|
else => {}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#! Notes the ranges of code each variable held in values is somewhere in, and
|
|
#! where each of its words is over each.
|
|
#! @param this The selector.
|
|
#! @param spans Where each instruction's code begins, then ends.
|
|
def selector::place_held(&mut this, spans: span<u32>): void {
|
|
val body := &this.body;
|
|
val program := &this.b.c.program;
|
|
|
|
# Each word followed: those of each variable held in values, by slot.
|
|
var slot_first := vector::<u32>::new(scratch());
|
|
var words := vector::<u32>::new(scratch());
|
|
var slots: u32 = 0;
|
|
for v in 0..body.variables.length {
|
|
val each: variable_info = body.variables[v];
|
|
var count: u64 = 0;
|
|
if each.held {
|
|
count = each.boxed ? 1 : word_count(&program.layouts[each.layout]);
|
|
}
|
|
if count > held_words {
|
|
count = 0;
|
|
}
|
|
slot_first.append(slots);
|
|
words.append(count as u32);
|
|
slots += count as u32;
|
|
}
|
|
return if slots == 0;
|
|
|
|
# Of each `f32` held in values, whether it is described as one: where more of
|
|
# the values bound to it are held as `f32`s than as `f64`s, as `singles.sakura`
|
|
# says. Where it is held the other way, it is described as nowhere.
|
|
var narrow := vector::<bool>::new(scratch());
|
|
var floats := vector::<bool>::new(scratch());
|
|
var votes := vector::<i64>::new(scratch());
|
|
narrow.resize(body.variables.length);
|
|
floats.resize(body.variables.length);
|
|
votes.resize(body.variables.length);
|
|
for v in 0..body.variables.length {
|
|
val each: variable_info = body.variables[v];
|
|
val shape: layout = program.layouts[each.layout];
|
|
floats[v] = each.held && !each.boxed && shape.kind == lk::floating && shape.size == 4;
|
|
}
|
|
for &bound in body.bindings {
|
|
continue if bound.variable as u64 >= floats.length || !floats[bound.variable as u64];
|
|
continue if bound.value == 0 || bound.value as u64 >= body.origins.length || this.p.remade[bound.value];
|
|
votes[bound.variable as u64] = votes[bound.variable as u64] + (this.single[bound.value] ? 1 : -1);
|
|
}
|
|
for v in 0..body.variables.length {
|
|
narrow[v] = floats[v] && votes[v] > 0;
|
|
}
|
|
|
|
# What binds each slot, where: after the instruction it was bound after,
|
|
# where that is still in the code, or else where the block it was bound in
|
|
# begins, in the order bound.
|
|
var placed := vector::<bool>::new(scratch());
|
|
placed.resize(body.insts.length);
|
|
for k in 0..this.p.order.length {
|
|
var walk := body.walk(this.p.order[k]);
|
|
while walk.more() {
|
|
placed[walk.next()] = true;
|
|
}
|
|
}
|
|
var at_inst := vector::<u32>::new(scratch());
|
|
at_inst.resize(body.insts.length);
|
|
var at_block := vector::<u32>::new(scratch());
|
|
at_block.resize(body.blocks.length);
|
|
var after := vector::<u32>::new(scratch());
|
|
after.resize(body.bindings.length);
|
|
var b := body.bindings.length;
|
|
while b > 0 {
|
|
b--;
|
|
val bound: binding = body.bindings[b];
|
|
continue if bound.variable as u64 >= words.length || bound.word >= words[bound.variable as u64];
|
|
continue if (bound.value == 0 && bound.known == vt::none) || bound.value as u64 >= body.origins.length;
|
|
if bound.after != 0 && placed[bound.after] {
|
|
after[b] = at_inst[bound.after];
|
|
at_inst[bound.after] = b as u32 + 1;
|
|
continue;
|
|
}
|
|
var block := bound.block;
|
|
if block == 0xFFFFFFFF || body.blocks[block].gone || this.p.number[block] == never {
|
|
block = bound.after != 0 ? body.insts[bound.after].block : 0xFFFFFFFF;
|
|
}
|
|
continue if block == 0xFFFFFFFF || body.blocks[block].gone || this.p.number[block] == never;
|
|
after[b] = at_block[block];
|
|
at_block[block] = b as u32 + 1;
|
|
}
|
|
|
|
# What each block begins with, by slot: what every block laid out before it
|
|
# ended with, where they agree, and nothing otherwise, until none changes.
|
|
# `never` stands for a block not yet worked out.
|
|
val blocks := body.blocks.length;
|
|
var begins := vector::<u32>::new(scratch());
|
|
var ends := vector::<u32>::new(scratch());
|
|
begins.resize(blocks * slots as u64);
|
|
ends.resize(blocks * slots as u64);
|
|
for i in 0..blocks * slots as u64 {
|
|
ends[i] = never;
|
|
}
|
|
var state := vector::<u32>::new(scratch());
|
|
state.resize(slots as u64);
|
|
var split := vector::<bool>::new(scratch());
|
|
split.resize(slots as u64);
|
|
var changed := true;
|
|
var rounds := 0u;
|
|
while changed && rounds < 32 {
|
|
changed = false;
|
|
rounds++;
|
|
for k in 0..this.p.order.length {
|
|
val block: u32 = this.p.order[k];
|
|
for s in 0..slots as u64 {
|
|
state[s] = block == 0 ? 0 : never;
|
|
split[s] = false;
|
|
}
|
|
for &edge in body.blocks[block].preds.view() {
|
|
val from := body.edge_source(edge);
|
|
continue if this.p.number[from] == never;
|
|
for s in 0..slots as u64 {
|
|
val given: u32 = ends[from as u64 * slots as u64 + s];
|
|
continue if given == never;
|
|
val now: u32 = state[s];
|
|
if now != never && now != given {
|
|
split[s] = true;
|
|
}
|
|
state[s] = now == never || now == given ? given : 0;
|
|
}
|
|
}
|
|
# Where the blocks before differ, the variable is the parameter each
|
|
# gives what it ended with, where one is.
|
|
for s in 0..slots as u64 {
|
|
continue if !split[s];
|
|
val params := &body.blocks[block].params;
|
|
for i in 0..params.length {
|
|
var all := true;
|
|
for &edge in body.blocks[block].preds.view() {
|
|
val from := body.edge_source(edge);
|
|
continue if this.p.number[from] == never;
|
|
val given: u32 = ends[from as u64 * slots as u64 + s];
|
|
all = all && (given == never || body.edges[edge].args[i] == given);
|
|
}
|
|
if all {
|
|
state[s] = params[i];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
for s in 0..slots as u64 {
|
|
if state[s] == never {
|
|
state[s] = 0;
|
|
}
|
|
begins[block as u64 * slots as u64 + s] = state[s];
|
|
}
|
|
this.apply_bindings(at_block[block], &after, &slot_first, &mut state);
|
|
var walk := body.walk(block);
|
|
while walk.more() {
|
|
this.apply_bindings(at_inst[walk.next()], &after, &slot_first, &mut state);
|
|
}
|
|
for s in 0..slots as u64 {
|
|
val index := block as u64 * slots as u64 + s;
|
|
if ends[index] != state[s] {
|
|
ends[index] = state[s];
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# The ranges: each instruction where a variable's words are somewhere other
|
|
# than before begins one.
|
|
val variables := body.variables.length;
|
|
var open := vector::<located_range>::new(scratch());
|
|
open.resize(variables);
|
|
var opened := vector::<bool>::new(scratch());
|
|
opened.resize(variables);
|
|
var found := vector::<vector<located_range>>::new(scratch());
|
|
for _ in 0..variables {
|
|
found.append(vector::<located_range>::new(scratch()));
|
|
}
|
|
# The stubs of each instruction, written after the body, as a list through
|
|
# `stub_after`.
|
|
val stubs := this.stub_spans.length / 3;
|
|
var stub_head := vector::<u32>::new(scratch());
|
|
var stub_after := vector::<u32>::new(scratch());
|
|
stub_after.resize(stubs);
|
|
if stubs != 0 {
|
|
stub_head.resize(body.insts.length);
|
|
}
|
|
for t in 0..stubs {
|
|
val index: u32 = this.stub_spans[3 * t + 2];
|
|
stub_after[t] = stub_head[index];
|
|
stub_head[index] = t as u32 + 1;
|
|
}
|
|
|
|
var last_end: u32 = 0;
|
|
for k in 0..this.p.order.length {
|
|
val block: u32 = this.p.order[k];
|
|
for s in 0..slots as u64 {
|
|
state[s] = begins[block as u64 * slots as u64 + s];
|
|
}
|
|
this.apply_bindings(at_block[block], &after, &slot_first, &mut state);
|
|
var walk := body.walk(block);
|
|
while walk.more() {
|
|
val at := walk.next();
|
|
val from := spans[2 * at as u64];
|
|
val to := spans[2 * at as u64 + 1];
|
|
if to > from {
|
|
for v in 0..variables {
|
|
continue if words[v] == 0;
|
|
var here := located_range { from = from, to = to };
|
|
var known := false;
|
|
for w in 0..words[v] as u64 {
|
|
val value: u32 = state[slot_first[v] as u64 + w];
|
|
continue if value == 0;
|
|
var kind: u8 = 0;
|
|
here.spots[w] = this.word_spot_of(value, at, &mut kind);
|
|
if floats[v] && (kind == word_spot::real as u8 ? narrow[v] : this.single[value] != narrow[v]) {
|
|
kind = word_spot::nowhere as u8;
|
|
}
|
|
here.kinds[w] = kind;
|
|
known = known || kind != word_spot::nowhere as u8;
|
|
}
|
|
val same := opened[v] && same_spots(&open[v], &here);
|
|
continue if same;
|
|
if opened[v] {
|
|
open[v].to = from;
|
|
if open[v].to > open[v].from {
|
|
found[v].append(open[v]);
|
|
}
|
|
}
|
|
opened[v] = known;
|
|
open[v] = here;
|
|
}
|
|
last_end = to > last_end ? to : last_end;
|
|
}
|
|
|
|
# A stub of the instruction has the variables where the instruction does.
|
|
var t: u32 = stubs != 0 ? stub_head[at] : 0;
|
|
while t != 0 {
|
|
val stub_from: u32 = this.stub_spans[3 * (t as u64 - 1)];
|
|
val stub_to: u32 = this.stub_spans[3 * (t as u64 - 1) + 1];
|
|
for v in 0..variables {
|
|
continue if words[v] == 0;
|
|
var here := located_range { from = stub_from, to = stub_to };
|
|
var known := false;
|
|
for w in 0..words[v] as u64 {
|
|
val value: u32 = state[slot_first[v] as u64 + w];
|
|
continue if value == 0;
|
|
var kind: u8 = 0;
|
|
here.spots[w] = this.word_spot_of(value, at, &mut kind);
|
|
if floats[v] && (kind == word_spot::real as u8 ? narrow[v] : this.single[value] != narrow[v]) {
|
|
kind = word_spot::nowhere as u8;
|
|
}
|
|
here.kinds[w] = kind;
|
|
known = known || kind != word_spot::nowhere as u8;
|
|
}
|
|
if known && stub_to > stub_from {
|
|
found[v].append(here);
|
|
}
|
|
}
|
|
t = stub_after[t as u64 - 1];
|
|
}
|
|
this.apply_bindings(at_inst[at], &after, &slot_first, &mut state);
|
|
}
|
|
}
|
|
|
|
for v in 0..variables {
|
|
continue if words[v] == 0;
|
|
if opened[v] {
|
|
open[v].to = last_end;
|
|
if open[v].to > open[v].from {
|
|
found[v].append(open[v]);
|
|
}
|
|
}
|
|
continue if found[v].length == 0;
|
|
val each: variable_info = body.variables[v];
|
|
val first := this.b.debug.ranges.length as u32;
|
|
for &range in found[v] {
|
|
this.b.debug.ranges.append(range);
|
|
}
|
|
this.b.debug.variables.append(placed_variable {
|
|
name = each.name,
|
|
at = each.at,
|
|
layout = each.layout,
|
|
param = each.param,
|
|
boxed = each.boxed,
|
|
held = true,
|
|
narrow = narrow[v],
|
|
site = each.site,
|
|
ranges = first,
|
|
count = found[v].length as u32
|
|
});
|
|
}
|
|
}
|
|
|
|
#! Gives each slot the value each binding of a list gives it, in order. One
|
|
#! keeping a constant gives what stands for it: the binding, numbered past the
|
|
#! values.
|
|
#! @param this The selector.
|
|
#! @param first One more than the first binding, or 0.
|
|
#! @param after One more than the binding after each, or 0.
|
|
#! @param slot_first The first slot of each variable.
|
|
#! @param state The value each slot holds.
|
|
def selector::apply_bindings(&this, first: u32, after: &vector<u32>, slot_first: &vector<u32>, state: &mut vector<u32>): void {
|
|
val values := this.body.origins.length;
|
|
var b := first;
|
|
while b != 0 {
|
|
val bound: binding = this.body.bindings[b as u64 - 1];
|
|
val given := bound.known != vt::none ? (values + b as u64 - 1) as u32 : bound.value;
|
|
state[slot_first[bound.variable as u64] as u64 + bound.word as u64] = given;
|
|
b = after[b as u64 - 1];
|
|
}
|
|
}
|
|
|
|
#! Whether two ranges say each word is in the same place.
|
|
#! @param a The one.
|
|
#! @param b The other.
|
|
#! @returns Whether they do.
|
|
def same_spots(a: &located_range, b: &located_range): bool {
|
|
for w in 0..held_words {
|
|
return false if a.kinds[w] != b.kinds[w] || a.spots[w] != b.spots[w];
|
|
}
|
|
return true;
|
|
}
|