253 lines
9.3 KiB
Plaintext
253 lines
9.3 KiB
Plaintext
#! @file bodies.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the lowering of a function's body: where its parameters are, its
|
||
|
|
#! statements, and how it returns, closing its parameters as it does.
|
||
|
|
|
||
|
|
import sakura::analyzer;
|
||
|
|
module sakura::compiler;
|
||
|
|
|
||
|
|
|
||
|
|
#! Lowers the function the emitter is of.
|
||
|
|
#! @param this The emitter.
|
||
|
|
pub def emitter::lower(&mut this): void {
|
||
|
|
val decl: u32 = this.c.program.functions[this.function].decl;
|
||
|
|
val instance: u32 = this.c.program.functions[this.function].instance;
|
||
|
|
val lambda: u32 = this.c.program.functions[this.function].node;
|
||
|
|
if this.c.program.functions[this.function].adapted != 0 {
|
||
|
|
this.lower_adapter();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if lambda != 0 {
|
||
|
|
this.lower_lambda(lambda);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if this.c.program.functions[this.function].builtin != 0 {
|
||
|
|
this.lower_builtin();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val evaluation: u32 = this.c.program.functions[this.function].evaluation;
|
||
|
|
if evaluation != 0 {
|
||
|
|
this.lower_evaluation(evaluation - 1);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if decl == 0 {
|
||
|
|
this.build.unreachable();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
val table := this.c.body_table(decl, instance);
|
||
|
|
if instance != 0 && this.c.checker.instances[instance - 1].table == 0 {
|
||
|
|
this.c.checker.use_table(table);
|
||
|
|
this.problem(this.c.checker.decls.nodes.p[decl], "The instance's body was never checked.");
|
||
|
|
this.build.unreachable();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.table = table;
|
||
|
|
this.use_own_table();
|
||
|
|
|
||
|
|
val node: u32 = this.c.checker.decls.nodes.p[decl];
|
||
|
|
val accessor := this.c.checker.decls.has(decl, df::accessor);
|
||
|
|
val body := accessor ? this.c.checker.record(node, accessor_slot::body) : this.c.checker.record(node, function_slot::body);
|
||
|
|
val params := accessor ? this.c.checker.left_of(node) : this.c.checker.record(node, function_slot::params);
|
||
|
|
val kind := this.c.type_of_function(decl, instance);
|
||
|
|
this.at_node(node);
|
||
|
|
this.result = this.c.checker.types.first_of(kind);
|
||
|
|
|
||
|
|
this.plan_function(decl, params, kind, body);
|
||
|
|
this.take_params(decl, params, kind);
|
||
|
|
this.safepoint();
|
||
|
|
this.lower_body(body);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lowers a body: a block, which ends returning nothing where control falls off
|
||
|
|
#! its end, or an expression, whose value is returned.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param body The body's node.
|
||
|
|
pub def emitter::lower_body(&mut this, body: u32): void {
|
||
|
|
if this.c.checker.kind_of(body) != nk::block {
|
||
|
|
val mark := this.begin_statement();
|
||
|
|
this.at_node(body);
|
||
|
|
if this.c.checker.types.kind_of(this.c.layout_type(this.result)) == ty::void_type {
|
||
|
|
this.value(body);
|
||
|
|
this.end_statement(mark);
|
||
|
|
this.close_locals(0, 0);
|
||
|
|
this.build.ret(span::<u32>::empty());
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.return_value(body, mark);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.lower_block(body);
|
||
|
|
return if this.build.ended();
|
||
|
|
|
||
|
|
# Running off the end returns, which a function giving a value never does.
|
||
|
|
if this.c.checker.types.kind_of(this.c.layout_type(this.result)) == ty::void_type {
|
||
|
|
this.close_locals(0, 0);
|
||
|
|
this.build.ret(span::<u32>::empty());
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.build.trap(fault::unreachable as u32);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Takes the function's parameters where its code is given them: each as a
|
||
|
|
#! local, held as its storage says, and a pack's as one local holding each.
|
||
|
|
#! @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.
|
||
|
|
def emitter::take_params(&mut this, decl: u32, params: u32, kind: u32): void {
|
||
|
|
val signature: u32 = this.c.program.functions[this.function].signature;
|
||
|
|
val count := this.c.checker.run_length(params);
|
||
|
|
var next: u32 = 0;
|
||
|
|
|
||
|
|
# A pack parameter takes every parameter from its own on, one value of each.
|
||
|
|
val written: u32 = this.c.checker.decls.types.p[decl];
|
||
|
|
val wcount := this.c.checker.types.part_count(written);
|
||
|
|
val packed := this.c.checker.decls.has(decl, df::variadic) && wcount != 0
|
||
|
|
&& this.c.checker.types.is_open_pack(this.c.checker.types.part_at(written, wcount - 1));
|
||
|
|
val total := this.c.program.signatures[signature].params.length as u32;
|
||
|
|
|
||
|
|
for i in 0..count {
|
||
|
|
val node := this.c.checker.run_at(params, i as u32);
|
||
|
|
if packed && i as u32 + 1 == count {
|
||
|
|
val first := this.packs.length as u32;
|
||
|
|
for p in i as u32..total {
|
||
|
|
val held := this.take_param(signature, p, &mut next);
|
||
|
|
val place := this.param_place(0, held);
|
||
|
|
this.packs.append(place);
|
||
|
|
}
|
||
|
|
val index := this.declare(node, location { kind = this.c.checker.type_at(node) }, true);
|
||
|
|
this.locals[index].pack = first;
|
||
|
|
this.locals[index].count = this.packs.length as u32 - first;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
break if i as u32 >= total;
|
||
|
|
|
||
|
|
val held := this.take_param(signature, i as u32, &mut next);
|
||
|
|
val place := this.param_place(node, held);
|
||
|
|
this.declare(node, place, this.c.checker.closes(held.kind));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A parameter as the code is given it: its words, or the address of the
|
||
|
|
#! function's own copy of it.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param signature The function's signature.
|
||
|
|
#! @param index The parameter's position.
|
||
|
|
#! @param next The first of the code's parameters not yet taken, moved
|
||
|
|
#! past those of this one.
|
||
|
|
#! @returns The parameter's value.
|
||
|
|
def emitter::take_param(&mut this, signature: u32, index: u32, next: &mut u32): operand {
|
||
|
|
val layout_index: u32 = this.c.program.signatures[signature].params[index];
|
||
|
|
val shape := this.c.layout_at(layout_index);
|
||
|
|
val kind := shape.type_id;
|
||
|
|
if in_memory(&shape) {
|
||
|
|
val address := this.build.param(next);
|
||
|
|
next++;
|
||
|
|
return operand::at(kind, address);
|
||
|
|
}
|
||
|
|
|
||
|
|
var made := operand { kind = kind, count = word_count(&shape) };
|
||
|
|
for i in 0..made.count {
|
||
|
|
made.words[i] = this.build.param(next);
|
||
|
|
next++;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where a parameter is held: in memory it was given, or its own copy, as its
|
||
|
|
#! storage says where it is referred to.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param node The parameter's node, or 0 for a value of a pack.
|
||
|
|
#! @param held Its value.
|
||
|
|
#! @returns Where it is.
|
||
|
|
def emitter::param_place(&mut this, node: u32, held: operand): location {
|
||
|
|
if held.address != 0 {
|
||
|
|
return location { kind = held.kind, address = held.address, framed = true };
|
||
|
|
}
|
||
|
|
|
||
|
|
val place := node != 0 ? this.new_place(node, held.kind) : this.fresh_place(held.kind);
|
||
|
|
this.store(&place, &held);
|
||
|
|
return place;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A place for a value of a type that no node declares, as each value of a
|
||
|
|
#! pack: in variables, or a cell where it is in memory.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @returns Where it is.
|
||
|
|
pub def emitter::fresh_place(&mut this, kind: u32): location {
|
||
|
|
val held := this.c.shape_of(kind);
|
||
|
|
if in_memory(&held) {
|
||
|
|
val made := this.build.code.add_cell(round_up(held.size == 0 ? word_size : held.size, word_size) as u32,
|
||
|
|
held.align < word_size ? word_size as u32 : held.align as u32, ck::local, 0);
|
||
|
|
return location { kind = kind, address = this.build.cell_address(made), framed = true };
|
||
|
|
}
|
||
|
|
var place := location { kind = kind, held = true };
|
||
|
|
for i in 0..word_count(&held) {
|
||
|
|
place.vars[i] = this.build.variable(word_type(&held));
|
||
|
|
}
|
||
|
|
return place;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lowers `return value;`: the value is made, or the local it names handed
|
||
|
|
#! over, everything the statements and scopes around it hold is closed, and the
|
||
|
|
#! function returns.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param node The value's node.
|
||
|
|
#! @param mark What the statement held before the value.
|
||
|
|
pub def emitter::return_value(&mut this, node: u32, mark: statement_mark): void {
|
||
|
|
var made := this.value_as(node, this.result);
|
||
|
|
|
||
|
|
# Returning a local by name hands it over, so it is not closed.
|
||
|
|
var given: u32 = 0;
|
||
|
|
if this.c.checker.kind_of(node) == nk::name {
|
||
|
|
val ref := this.c.checker.ref_at(node);
|
||
|
|
if (ref & ref_local) != 0 {
|
||
|
|
given = ref & ref_mask;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.return_made(&mut made, given, mark);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Ends the function returning a value made, closing what the statement made and
|
||
|
|
#! what the function's locals hold, all but the local handed over.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param made The value returned.
|
||
|
|
#! @param given The local the value is, handed over rather than closed, or 0.
|
||
|
|
#! @param mark Where the statement began.
|
||
|
|
pub def emitter::return_made(&mut this, made: &mut operand, given: u32, mark: statement_mark): void {
|
||
|
|
this.adopt(made);
|
||
|
|
|
||
|
|
# What the statement made closes, and what the statements around it made too,
|
||
|
|
# which they still close down the paths that do not return. The cells it took
|
||
|
|
# are freed only once the closing is lowered, which might take them otherwise.
|
||
|
|
var args := vector::<u32>::new();
|
||
|
|
made.args(&mut args);
|
||
|
|
this.close_kept(mark.kept);
|
||
|
|
this.leave_kept(0);
|
||
|
|
# An `ensures` naming what is returned reads it as the locals close.
|
||
|
|
this.returned = made;
|
||
|
|
this.returning = true;
|
||
|
|
this.close_locals(0, given);
|
||
|
|
this.returning = false;
|
||
|
|
this.release(mark.cells);
|
||
|
|
this.give_back(args.view());
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Ends the block returning what the function gives: its words, or the address
|
||
|
|
#! of what it gives in memory, and nothing where that has no bytes, as a struct
|
||
|
|
#! of no fields.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param args The words, or the address.
|
||
|
|
pub def emitter::give_back(&mut this, args: span<u32>): void {
|
||
|
|
if this.build.code.results.length == 0 && this.build.code.returned == 0 {
|
||
|
|
this.build.ret(span::<u32>::empty());
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.build.ret(args);
|
||
|
|
}
|