#! @file builder.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the builder, which makes the intermediate code of a function in one #! pass, as Braun et al.'s "Simple and Efficient Construction of Static Single #! Assignment Form" does. What lowering calls a variable is written and read #! by the builder, which finds the value it holds in the block where it is #! read, or else along the edges into that block, making it a parameter of a #! block where they join. A block is sealed once every edge into it is known, #! and a parameter made before then is given its arguments as it is sealed. #! Parameters that turn out to stand for one value, and blocks nothing reaches, #! are taken out once the code is finished. import std::containers; module sakura::compiler; #! Builds the code of one function. pub struct builder { var code: code; #! What is built. var at: u32; #! The block instructions are added to. var pos: u64; #! Where in the source what is added is from: one more than the file, then the byte. var kinds: vector; #! The type of each variable. var defs: dict; #! The value of each variable in each block, by `pair_key(variable, block)`. var sealed: vector; #! Whether each block is sealed. var waiting: vector; #! Parameters of blocks not sealed yet, which wait for their arguments. var held_for: vector; #! The variable each waiting parameter holds. var zeros: u32[vt::count as u64]; #! The value of each type a variable has where nothing gave it one, made once. } #! Makes a builder of code whose entry takes parameters of the types. #! @param params The types of the parameters. #! @returns The builder, adding to the entry, which is sealed, since nothing #! goes back into it. pub def builder::new(params: span): builder { var made := builder { code = code::new(), kinds = vector::::new(scratch()), defs = dict::::new(scratch()), sealed = vector::::new(scratch()), waiting = vector::::new(scratch()), held_for = vector::::new(scratch()) }; made.sealed.append(true); for &each in params { made.code.add_param(0, each); } return made; } #! The value of a parameter of the entry: one the function takes. #! @param this The builder. #! @param index Its position. #! @returns The value. pub def builder::param(&this, index: u32): u32 { return this.code.blocks[0].params[index]; } #! Adds a block, which takes nothing yet and is not sealed. #! @param this The builder. #! @returns The block. pub def builder::new_block(&mut this): u32 { this.sealed.append(false); return this.code.add_block(); } #! Makes instructions be added to the end of a block. #! @param this The builder. #! @param block The block. pub def builder::switch_to(&mut this, block: u32): void { this.at = block; } #! Whether the block instructions are added to has ended, as after a `return`, #! so what is added now is never reached. #! @param this The builder. #! @returns Whether it has. pub def builder::ended(&this): bool { val last: u32 = this.code.blocks[this.at].last; return last != 0 && this.code.insts[last].kind.ends(); } #! Says every edge into a block is known, giving the parameters it waits with #! their arguments. #! @param this The builder. #! @param block The block. pub def builder::seal(&mut this, block: u32): void { return if this.sealed[block]; # Giving one its arguments may make more, which are given theirs in turn, in # the order the block takes them. var at: u64 = 0; while at < this.waiting.length { val param: u32 = this.waiting[at]; if this.code.origins[param].from == block { this.fill(this.held_for[at], param, block); this.waiting.remove_at(at); this.held_for.remove_at(at); continue; } at++; } this.sealed[block] = true; } #! Makes a variable of the type, which holds nothing yet. #! @param this The builder. #! @param kind Its type. #! @returns The variable. pub def builder::variable(&mut this, kind: vt): u32 { this.kinds.append(kind); return (this.kinds.length - 1) as u32; } #! Gives a variable a value, from where instructions are added on. #! @param this The builder. #! @param variable The variable. #! @param value The value. pub def builder::assign(&mut this, variable: u32, value: u32): void { this.defs.set(pair_key(variable, this.at), value); } #! The value a variable holds where instructions are added. #! @param this The builder. #! @param variable The variable. #! @returns The value. pub def builder::read(&mut this, variable: u32): u32 { return this.read_in(variable, this.at); } #! The value a variable holds at the end of a block. #! @param this The builder. #! @param variable The variable. #! @param block The block. #! @returns The value. def builder::read_in(&mut this, variable: u32, block: u32): u32 { val key := pair_key(variable, block); if this.defs.get(&key) is some(got) { return got; } val kind: vt = this.kinds[variable]; var value: u32 = 0; val count := this.code.blocks[block].preds.length; if !this.sealed[block] { # Edges into it may yet be added, so it takes the value, given later. value = this.code.add_param(block, kind); this.waiting.append(value); this.held_for.append(variable); } else if count == 0 { value = this.zero(kind); } else if count == 1 { val edge: u32 = this.code.blocks[block].preds[0]; value = this.read_in(variable, this.code.edge_source(edge)); } else { # Recorded before its arguments are found, which may come back to it. value = this.code.add_param(block, kind); this.defs.set(key, value); this.fill(variable, value, block); } this.defs.set(key, value); return value; } #! Gives a parameter of a block the value the variable holds along each edge #! into the block. #! @param this The builder. #! @param variable The variable it holds. #! @param param The parameter. #! @param block The block. def builder::fill(&mut this, variable: u32, param: u32, block: u32): void { var preds := this.code.blocks[block].preds.copied(scratch()); for &edge in preds { val given := this.read_in(variable, this.code.edge_source(edge)); this.code.edges[edge].args.append(given); } } #! The value of the type a variable holds where nothing gave it one: zero, made #! first in the entry, which every block follows. #! @param this The builder. #! @param kind The type. #! @returns The value. def builder::zero(&mut this, kind: vt): u32 { val held: u32 = this.zeros[kind as u64]; return held if held != 0; # Made in the entry, then moved first, so it stands before everything there. val saved := this.at; this.at = 0; val made := kind == vt::real ? op::fconst : kind.lanes() ? op::vzero : op::iconst; val value := this.value(made, span::::blank(), kind, 0, 0); this.at = saved; val index: u32 = this.code.origins[value].from; this.unlink(index); this.code.put_first(0, index); this.zeros[kind as u64] = value; return value; } #! Takes an instruction out of its block's order, where it is last. #! @param this The builder. #! @param index The instruction. def builder::unlink(&mut this, index: u32): void { val block: u32 = this.code.insts[index].block; val before := this.code.before(index); val after: u32 = this.code.insts[index].next; if before == 0 { this.code.blocks[block].first = after; } else { this.code.insts[before].next = after; } if this.code.blocks[block].last == index { this.code.blocks[block].last = before; } this.code.insts[index].next = 0; } #! Adds an instruction, in a block of its own where the block it would be added #! to has ended, since nothing reaches it there. #! @param this The builder. #! @param kind What it does. #! @param args Its operands. #! @param gives The types of its results. #! @param aux What its kind says `aux` holds. #! @param imm What its kind says `imm` holds. #! @returns The instruction. pub def builder::emit(&mut this, kind: op, args: span, gives: span, aux: u32, imm: u64): u32 { if this.ended() { val unreached := this.new_block(); this.seal(unreached); this.at = unreached; } return this.code.append(this.at, kind, args, gives, aux, imm, this.pos); } #! Adds an instruction giving one value. #! @param this The builder. #! @param kind What it does. #! @param args Its operands. #! @param give The type of what it gives. #! @param aux What its kind says `aux` holds. #! @param imm What its kind says `imm` holds. #! @returns The value it gives. pub def builder::value(&mut this, kind: op, args: span, give: vt, aux: u32, imm: u64): u32 { var gives: vt[1] = [give]; val index := this.emit(kind, args, gives[0:1], aux, imm); return this.code.insts[index].result; } #! Adds an instruction giving nothing. #! @param this The builder. #! @param kind What it does. #! @param args Its operands. #! @param aux What its kind says `aux` holds. #! @param imm What its kind says `imm` holds. #! @returns The instruction. pub def builder::effect(&mut this, kind: op, args: span, aux: u32, imm: u64): u32 { return this.emit(kind, args, span::::blank(), aux, imm); } #! A word. #! @param this The builder. #! @param bits Its bits. #! @returns The value. pub def builder::word(&mut this, bits: u64): u32 { return this.value(op::iconst, span::::blank(), vt::word, 0, bits); } #! A real. #! @param this The builder. #! @param bits The bits of the `f64` it is. #! @returns The value. pub def builder::real(&mut this, bits: u64): u32 { return this.value(op::fconst, span::::blank(), vt::real, 0, bits); } #! An operator of two values. #! @param this The builder. #! @param kind The operator. #! @param a The first operand. #! @param b The second operand. #! @returns What it gives: a real for arithmetic of reals, and otherwise a word. pub def builder::binary(&mut this, kind: op, a: u32, b: u32): u32 { var args: u32[2] = [a, b]; val gives := kind == op::fadd || kind == op::fsub || kind == op::fmul || kind == op::fdiv || kind == op::frem ? vt::real : vt::word; return this.value(kind, args[0:2], gives, 0, 0); } #! An operator of one value. #! @param this The builder. #! @param kind The operator. #! @param a The operand. #! @returns What it gives: a real where it gives one, and otherwise a word. pub def builder::unary(&mut this, kind: op, a: u32): u32 { var args: u32[1] = [a]; val gives := kind == op::fneg || kind == op::itof || kind == op::utof || kind == op::fsingle || kind == op::fsqrt ? vt::real : vt::word; return this.value(kind, args[0:1], gives, 0, 0); } #! A word narrowed to a number of bits. #! @param this The builder. #! @param a The word. #! @param bits How many bits it keeps. #! @param signed Whether it is sign-extended rather than zero-extended. #! @returns The word narrowed. pub def builder::wrap(&mut this, a: u32, bits: u32, signed: bool): u32 { var args: u32[1] = [a]; return this.value(op::wrap, args[0:1], vt::word, bits, signed ? 1 : 0); } #! The address of a cell. #! @param this The builder. #! @param index The cell. #! @returns The address. pub def builder::cell_address(&mut this, index: u32): u32 { return this.value(op::cell, span::::blank(), vt::word, index, 0); } #! Reads a value from memory. #! @param this The builder. #! @param how How it is read. #! @param address The address. #! @param offset How many bytes past the address it is. #! @returns The value, a real for a float, a vector for lanes, and a word #! otherwise. pub def builder::load(&mut this, how: access, address: u32, offset: u64): u32 { var args: u32[1] = [address]; return this.value(op::load, args[0:1], how.value_type(), how as u32, offset); } #! Writes a value to memory. #! @param this The builder. #! @param how How it is written. #! @param address The address. #! @param offset How many bytes past the address it goes. #! @param value The value. pub def builder::store(&mut this, how: access, address: u32, offset: u64, value: u32): void { var args: u32[2] = [address, value]; this.effect(op::store, args[0:2], how as u32, offset); } #! Copies bytes from one address to another. #! @param this The builder. #! @param to Where they go. #! @param from Where they come from. #! @param size How many there are. pub def builder::copy(&mut this, to: u32, from: u32, size: u64): void { return if size == 0; var args: u32[2] = [to, from]; this.effect(op::copy, args[0:2], 0, size); } #! Zeroes bytes from an address. #! @param this The builder. #! @param address The address. #! @param size How many there are. pub def builder::clear(&mut this, address: u32, size: u64): void { return if size == 0; var args: u32[1] = [address]; this.effect(op::clear, args[0:1], 0, size); } #! Marks bytes at an address as never written, running with memory checked. #! @param this The builder. #! @param address The address. #! @param size How many bytes. pub def builder::forget(&mut this, address: u32, size: u64): void { return if size == 0; var args: u32[1] = [address]; this.effect(op::forget, args[0:1], 0, size); } #! Stops the program with an error where a word is zero. #! @param this The builder. #! @param cond The word. #! @param error The error, as the machine numbers them. pub def builder::check(&mut this, cond: u32, error: u32): void { var args: u32[1] = [cond]; this.effect(op::check, args[0:1], error, 0); } #! Ends the block going to another. #! @param this The builder. #! @param target The block. pub def builder::jump(&mut this, target: u32): void { return if this.ended(); val index := this.effect(op::jump, span::::blank(), 0, 0); this.code.insts[index].aux = this.code.add_edge(index, target, 0); } #! Ends the block going to one block where a word is not zero, and another #! where it is. #! @param this The builder. #! @param cond The word. #! @param then Where it goes where the word is not zero. #! @param other Where it goes where it is. pub def builder::branch(&mut this, cond: u32, then: u32, other: u32): void { return if this.ended(); var args: u32[1] = [cond]; val index := this.effect(op::branch, args[0:1], 0, 0); this.code.insts[index].aux = this.code.add_edge(index, then, 0); this.code.add_edge(index, other, 0); } #! Ends the block returning words, or the memory at an address. #! @param this The builder. #! @param args The words, or the address. pub def builder::ret(&mut this, args: span): void { return if this.ended(); this.effect(op::ret, args, 0, 0); } #! Ends the block stopping the program with an error. #! @param this The builder. #! @param error The error, as the machine numbers them. pub def builder::trap(&mut this, error: u32): void { return if this.ended(); this.effect(op::trap, span::::blank(), error, 0); } #! Ends the block as one nothing goes on from, as after a call that never #! returns. #! @param this The builder. pub def builder::unreachable(&mut this): void { return if this.ended(); this.effect(op::unreachable, span::::blank(), 0, 0); } #! Finishes the code: seals what is not sealed, takes out the blocks nothing #! reaches and the parameters standing for one value, and gives the code. #! @param this The builder. #! @returns The code. pub def builder::finish(&mut this): code { # A block left open, as one control falls off the end of, ends unreached. for i in 0..this.code.blocks.length { this.at = i as u32; if !this.ended() { this.unreachable(); } this.seal(i as u32); } this.code.prune(); return move(&mut this.code); } #! Takes out the blocks the entry does not reach, and the parameters that stand #! for one value, being given it, or themselves, along every edge, so each use #! of one uses that value instead. #! @param this The code. pub def code::prune(&mut this): void { this.forget_order(); this.drop_unreached(); this.drop_lone_params(); } #! Takes out the parameters that stand for one value, being given it, or #! themselves, along every edge, so each use of one uses that value instead. #! @param this The code. pub def code::drop_lone_params(&mut this): void { return if !this.has_lone_param(); # What each parameter taken out stands for, followed until one that is not. var aliases := vector::::new(scratch()); aliases.resize(this.origins.length); val links := aliases.span.data; val count := aliases.length; var changed := true; while changed { changed = false; for b in 1..this.blocks.length { continue if this.blocks[b].gone; for p in 0..this.blocks[b].params.length { val param: u32 = this.blocks[b].params[p]; continue if aliases[param] != 0; var only: u32 = 0; var many := false; for &edge in this.blocks[b].preds { val given := follow_replaced(links, count, this.edges[edge].args[p]); continue if given == param || given == only; if only != 0 { many = true; break; } only = given; } continue if many || only == 0; aliases[param] = only; changed = true; } } } # Each use reads what it stands for, and each edge gives only the parameters # left. for i in 0..this.operands.length { this.operands[i] = follow_replaced(links, count, this.operands[i]); } var keeps := vector::::new(scratch()); for b in 0..this.blocks.length { continue if this.blocks[b].gone; for &edge in this.blocks[b].preds.span { var args := this.edges[edge].args.slice; for p in 0..args.length { args[p] = follow_replaced(links, count, args[p]); } } keeps.clear(); for ¶m in this.blocks[b].params.span { keeps.append(aliases[param] == 0); } if keeps.span.contains(&false) { this.drop_params(b as u32, keeps.span); } } } #! Whether a block takes a parameter every edge into it gives one value, or #! itself, which pruning takes out. #! @param this The code. #! @returns Whether one does. def code::has_lone_param(&this): bool { for b in 1..this.blocks.length { continue if this.blocks[b].gone; for p in 0..this.blocks[b].params.length { val param: u32 = this.blocks[b].params[p]; var only: u32 = 0; var many := false; for &edge in this.blocks[b].preds { val given: u32 = this.edges[edge].args[p]; continue if given == param || given == only; if only != 0 { many = true; break; } only = given; } return true if !many; } } return false; } #! Marks the blocks the entry does not reach as gone, and takes their edges out #! of the blocks they went to. Those it reaches are the blocks of their order, #! worked out where the code does not keep it, and kept, as what comes after #! asks it. #! @param this The code. pub def code::drop_unreached(&mut this): void { this.keep_order(); # Only the blocks a block newly gone went to lose edges. var touched := vector::::new(scratch()); for b in 0..this.blocks.length { continue if this.kept_number[b] != 0xFFFFFFFF || this.blocks[b].gone; this.blocks[b].gone = true; val last: u32 = this.blocks[b].last; continue if last == 0; val first: u32 = this.insts[last].aux; for e in 0..this.edge_count(last) { touched.append(this.edges[first + e as u32].target); } } for &b in touched { continue if this.kept_number[b] == 0xFFFFFFFF; var kept := ids {}; for &edge in this.blocks[b].preds { if this.kept_number[this.edge_source(edge)] != 0xFFFFFFFF { kept.append(edge); } } this.blocks[b].preds = move(&mut kept); } }