Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,253 @@
|
||||
#! @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);
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
#! @file builtins.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the methods the compiler gives the built-in types, which make
|
||||
#! numbers, `bool`, enums, and pointers `equatable`, `hashable`, `ordered`, and
|
||||
#! `numeric` as far as each allows: written where they are called, and made
|
||||
#! functions of their own where a vtable names them.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! A call of a method the compiler gives a built-in type, on the value it is
|
||||
#! called on and the other operand.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::builtin_call(&mut this, node: u32): operand {
|
||||
val callee := this.c.checker.left_of(node);
|
||||
val object := this.c.checker.left_of(callee);
|
||||
val name := this.c.checker.name_of(callee);
|
||||
|
||||
# `f.invoke(args)` calls the function, or the C function, the value is.
|
||||
if name == word::invoke {
|
||||
val made := this.value_of_type(object);
|
||||
val function := this.through_references(&made);
|
||||
var given := gathered { values = vector::<operand>::new() };
|
||||
this.gather(node, 0, 0, this.called_type(function.kind), 0, &mut given);
|
||||
return this.call_function_value(&function, &given);
|
||||
}
|
||||
|
||||
# A fixed array is compared and hashed element by element, where it is.
|
||||
val reached := this.c.checker.unalias(this.c.checker.reached(this.c.checker.type_at(object)));
|
||||
if this.c.checker.types.kind_of(reached) == ty::fixed {
|
||||
val whole := this.object(object);
|
||||
val first := this.address_of(&whole);
|
||||
val result := this.c.checker.type_at(node);
|
||||
if name == word::hash {
|
||||
return operand::one(result, this.hash_elements(first, reached));
|
||||
}
|
||||
val run := this.c.checker.right_of(node);
|
||||
val other := this.object(this.c.checker.run_at(run, 0));
|
||||
return operand::one(result, this.equal_elements(first, this.address_of(&other), reached));
|
||||
}
|
||||
|
||||
val made := this.value_of_type(object);
|
||||
val subject := this.through_references(&made);
|
||||
var other := operand {};
|
||||
val run := this.c.checker.right_of(node);
|
||||
if this.c.checker.run_length(run) != 0 {
|
||||
val given := this.value_of_type(this.c.checker.run_at(run, 0));
|
||||
other = this.through_references(&given);
|
||||
other = this.convert(&other, subject.kind);
|
||||
}
|
||||
return this.builtin_method(name, &subject, &other, this.c.checker.type_at(node));
|
||||
}
|
||||
|
||||
#! What a method the compiler gives a built-in type gives: `equals` compares,
|
||||
#! `hash` gives the bits, as 0 for both zeros of a float, `compare` gives -1, 0,
|
||||
#! or 1, and the methods of `numeric` operate.
|
||||
#! @param this The emitter.
|
||||
#! @param name The ID of the method's name.
|
||||
#! @param a The value it is called on.
|
||||
#! @param b The other operand, where it takes one.
|
||||
#! @param result The type it gives.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::builtin_method(&mut this, name: u32, a: &operand, b: &operand, result: u32): operand {
|
||||
val kind := a.kind;
|
||||
val floating := this.is_float(kind);
|
||||
val signed := this.is_signed(kind);
|
||||
if name == word::equals {
|
||||
return operand::one(result, this.build.binary(floating ? op::feq : op::eq, a.words[0], b.words[0]));
|
||||
}
|
||||
if name == word::compare {
|
||||
val less := floating ? op::flt : (signed ? op::lts : op::ltu);
|
||||
val below := this.build.binary(less, a.words[0], b.words[0]);
|
||||
val above := this.build.binary(less, b.words[0], a.words[0]);
|
||||
return operand::one(result, this.build.binary(op::sub, above, below));
|
||||
}
|
||||
if name == word::hash {
|
||||
if !floating {
|
||||
return operand::one(result, a.words[0]);
|
||||
}
|
||||
# Both zeros hash as 0, and anything else as its bits, an `f32`'s as one.
|
||||
val shape := this.c.shape_of(kind);
|
||||
val cell := this.temporary(word_size, word_size);
|
||||
val address := this.build.cell_address(cell);
|
||||
this.build.store(shape.bits <= 32 ? access::f32 : access::f64, address, 0, a.words[0]);
|
||||
val bits := this.build.load(shape.bits <= 32 ? access::u32 : access::i64, address, 0);
|
||||
val zero := this.build.binary(op::feq, a.words[0], this.build.real(0));
|
||||
val nonzero := this.build.binary(op::eq, zero, this.build.word(0));
|
||||
val mask := this.build.unary(op::neg, nonzero);
|
||||
return operand::one(result, this.build.binary(op::band, bits, mask));
|
||||
}
|
||||
if name == word::negate {
|
||||
val made := this.build.unary(floating ? op::fneg : op::neg, a.words[0]);
|
||||
return operand::one(result, this.normalize(made, kind));
|
||||
}
|
||||
if name == word::bit_not {
|
||||
return operand::one(result, this.normalize(this.build.unary(op::bnot, a.words[0]), kind));
|
||||
}
|
||||
|
||||
var chosen := op::add;
|
||||
if name == word::add {
|
||||
chosen = floating ? op::fadd : op::add;
|
||||
} else if name == word::sub {
|
||||
chosen = floating ? op::fsub : op::sub;
|
||||
} else if name == word::mul {
|
||||
chosen = floating ? op::fmul : op::mul;
|
||||
} else if name == word::div {
|
||||
chosen = floating ? op::fdiv : (signed ? op::divs : op::divu);
|
||||
} else if name == word::rem {
|
||||
chosen = floating ? op::frem : (signed ? op::rems : op::remu);
|
||||
} else if name == word::bit_and {
|
||||
chosen = op::band;
|
||||
} else if name == word::bit_or {
|
||||
chosen = op::bor;
|
||||
} else if name == word::bit_xor {
|
||||
chosen = op::bxor;
|
||||
} else if name == word::shift_left {
|
||||
chosen = op::shl;
|
||||
} else if name == word::shift_right {
|
||||
chosen = signed ? op::shrs : op::shru;
|
||||
}
|
||||
return operand::one(result, this.normalize(this.build.binary(chosen, a.words[0], b.words[0]), kind));
|
||||
}
|
||||
|
||||
#! The function giving a built-in type a method, as a vtable names it, made
|
||||
#! where it is new: it takes `this` and the other operand by reference, as the
|
||||
#! method's type says.
|
||||
#! @param this The compiler.
|
||||
#! @param kind The built-in type.
|
||||
#! @param name The ID of the method's name.
|
||||
#! @returns The function's index, or `no_function` where the type has no such
|
||||
#! method.
|
||||
pub def lowerer::builtin_function(&mut this, kind: u32, name: u32): u32 {
|
||||
val type_of := this.checker.builtin_method(kind, name);
|
||||
return no_function if type_of == 0;
|
||||
val key := pair_key(kind, name);
|
||||
if this.builtin_ids.get(&key) is some(got) {
|
||||
return got;
|
||||
}
|
||||
var text := string::from(this.checker.names.text(name), nothing {});
|
||||
val index := this.add_function(move(&mut text), this.signature_of(this.call_type(type_of)));
|
||||
this.builtin_ids.set(key, index);
|
||||
this.program.functions[index].builtin = name;
|
||||
this.program.functions[index].owner_type = kind;
|
||||
this.want(index);
|
||||
return index;
|
||||
}
|
||||
|
||||
#! Lowers a function giving a built-in type a method.
|
||||
#! @param this The emitter.
|
||||
pub def emitter::lower_builtin(&mut this): void {
|
||||
val name: u32 = this.c.program.functions[this.function].builtin;
|
||||
val kind: u32 = this.c.program.functions[this.function].owner_type;
|
||||
|
||||
# `invoke` calls the function, or the C function, its `this` refers to, with
|
||||
# the arguments it is given, as they are.
|
||||
if name == word::invoke {
|
||||
val signature: u32 = this.c.program.functions[this.function].signature;
|
||||
val total := this.c.program.signatures[signature].params.length as u32;
|
||||
var next: u32 = 0;
|
||||
val taken := this.take_param(signature, 0, &mut next);
|
||||
val place := location { kind = kind, address = taken.words[0] };
|
||||
val function := this.load(&place);
|
||||
var given := gathered { values = vector::<operand>::new() };
|
||||
for p in 1..total {
|
||||
given.values.append(this.take_param(signature, p as u32, &mut next));
|
||||
}
|
||||
val made := this.call_function_value(&function, &given);
|
||||
var args := vector::<u32>::new();
|
||||
made.args(&mut args);
|
||||
this.give_back(args.view());
|
||||
return;
|
||||
}
|
||||
if this.c.checker.types.kind_of(this.c.checker.unalias(kind)) == ty::fixed {
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
var words: u32[1] = [0];
|
||||
if name == word::hash {
|
||||
words[0] = this.hash_elements(this.build.param(0), plain);
|
||||
} else {
|
||||
words[0] = this.equal_elements(this.build.param(0), this.build.param(2), plain);
|
||||
}
|
||||
this.build.ret(words[0:1]);
|
||||
return;
|
||||
}
|
||||
val first := location { kind = kind, address = this.build.param(0) };
|
||||
val a := this.load(&first);
|
||||
var b := operand {};
|
||||
if this.c.program.signatures[this.c.program.functions[this.function].signature].params.length > 1 {
|
||||
val second := location { kind = kind, address = this.build.param(2) };
|
||||
b = this.load(&second);
|
||||
}
|
||||
val result := this.c.checker.types.first_of(this.c.checker.builtin_method(kind, name));
|
||||
val made := this.builtin_method(name, &a, &b, result);
|
||||
var args := vector::<u32>::new();
|
||||
made.args(&mut args);
|
||||
this.give_back(args.view());
|
||||
}
|
||||
|
||||
#! Whether the elements of two fixed arrays are equal, one by one, as each
|
||||
#! says it is equal: a number by its value, and a struct by its `equals`.
|
||||
#! @param this The emitter.
|
||||
#! @param a The address of one array.
|
||||
#! @param b The address of the other.
|
||||
#! @param kind The arrays' type.
|
||||
#! @returns A word of 1 where they are, and 0 where not.
|
||||
pub def emitter::equal_elements(&mut this, a: u32, b: u32, kind: u32): u32 {
|
||||
val element := this.c.checker.types.first_of(kind);
|
||||
val count := this.c.checker.types.second_of(kind) as u64;
|
||||
val size := this.c.shape_of(element).size;
|
||||
val same := this.build.variable(vt::word);
|
||||
val index := this.build.variable(vt::word);
|
||||
this.build.assign(same, this.build.word(1));
|
||||
this.build.assign(index, this.build.word(0));
|
||||
|
||||
val head := this.build.new_block();
|
||||
val body := this.build.new_block();
|
||||
val differs := this.build.new_block();
|
||||
val next := this.build.new_block();
|
||||
val done := this.build.new_block();
|
||||
val at := this.count_up(index, count, head, body, done);
|
||||
val offset := this.scaled(at, size);
|
||||
val equal := this.element_equals(this.build.binary(op::add, a, offset), this.build.binary(op::add, b, offset), element);
|
||||
this.build.branch(equal, next, differs);
|
||||
this.build.seal(differs);
|
||||
this.build.switch_to(differs);
|
||||
this.build.assign(same, this.build.word(0));
|
||||
this.build.jump(done);
|
||||
this.build.seal(next);
|
||||
this.build.switch_to(next);
|
||||
this.count_on(index, head, done);
|
||||
return this.build.read(same);
|
||||
}
|
||||
|
||||
#! Whether two values of a type, at their addresses, are equal, as the type
|
||||
#! says it is.
|
||||
#! @param this The emitter.
|
||||
#! @param a The address of one.
|
||||
#! @param b The address of the other.
|
||||
#! @param kind The type.
|
||||
#! @returns A word of 1 where they are, and 0 where not.
|
||||
def emitter::element_equals(&mut this, a: u32, b: u32, kind: u32): u32 {
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
if this.c.checker.types.kind_of(plain) == ty::fixed {
|
||||
return this.equal_elements(a, b, plain);
|
||||
}
|
||||
val first := location { kind = kind, address = a };
|
||||
val second := location { kind = kind, address = b };
|
||||
if this.c.checker.find_member(kind, word::equals).decl == 0 {
|
||||
val x := this.load(&first);
|
||||
val y := this.load(&second);
|
||||
return this.builtin_method(word::equals, &x, &y, bt::boolean).words[0];
|
||||
}
|
||||
val method := this.c.checker.find_member(kind, word::equals).decl;
|
||||
val reference := this.c.checker.types.reference(kind, false);
|
||||
val subject := this.reference_to(&first, reference);
|
||||
var others := vector::<operand>::new();
|
||||
others.append(this.reference_to(&second, reference));
|
||||
return this.method_call(method, kind, &subject, others.view()).words[0];
|
||||
}
|
||||
|
||||
#! The hash of a fixed array: of its length, then each element's in turn,
|
||||
#! mixed as the C compiler mixes them.
|
||||
#! @param this The emitter.
|
||||
#! @param a The array's address.
|
||||
#! @param kind Its type.
|
||||
#! @returns The hash.
|
||||
pub def emitter::hash_elements(&mut this, a: u32, kind: u32): u32 {
|
||||
val element := this.c.checker.types.first_of(kind);
|
||||
val count := this.c.checker.types.second_of(kind) as u64;
|
||||
val size := this.c.shape_of(element).size;
|
||||
val code := this.build.variable(vt::word);
|
||||
val index := this.build.variable(vt::word);
|
||||
this.build.assign(code, this.build.word(count));
|
||||
this.build.assign(index, this.build.word(0));
|
||||
|
||||
val head := this.build.new_block();
|
||||
val body := this.build.new_block();
|
||||
val done := this.build.new_block();
|
||||
val at := this.count_up(index, count, head, body, done);
|
||||
val each := this.element_hash(this.build.binary(op::add, a, this.scaled(at, size)), element);
|
||||
val mixed := this.build.binary(op::mul, this.build.binary(op::bxor, this.build.read(code), each), this.build.word(0x9E3779B97F4A7C15));
|
||||
this.build.assign(code, this.build.binary(op::bxor, mixed, this.build.binary(op::shru, mixed, this.build.word(29))));
|
||||
this.count_on(index, head, done);
|
||||
return this.build.read(code);
|
||||
}
|
||||
|
||||
#! The hash of a value of a type, at its address, as the type gives it.
|
||||
#! @param this The emitter.
|
||||
#! @param a The value's address.
|
||||
#! @param kind The type.
|
||||
#! @returns The hash.
|
||||
def emitter::element_hash(&mut this, a: u32, kind: u32): u32 {
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
if this.c.checker.types.kind_of(plain) == ty::fixed {
|
||||
return this.hash_elements(a, plain);
|
||||
}
|
||||
val place := location { kind = kind, address = a };
|
||||
val method := this.c.checker.find_member(kind, word::hash).decl;
|
||||
if method == 0 {
|
||||
val x := this.load(&place);
|
||||
return this.builtin_method(word::hash, &x, &operand {}, bt::uint64).words[0];
|
||||
}
|
||||
val subject := this.reference_to(&place, this.c.checker.types.reference(kind, false));
|
||||
return this.method_call(method, kind, &subject, span::<operand>::empty()).words[0];
|
||||
}
|
||||
@@ -0,0 +1,702 @@
|
||||
#! @file calls.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lowering of calls: of functions and the instances of generics,
|
||||
#! of methods given the value they are called on, of traits' methods through a
|
||||
#! vtable, of function values, and of C functions, with the arguments a call
|
||||
#! leaves to defaults, those a variadic parameter gathers, and those a pack
|
||||
#! spreads. Also contains the calls the compiler makes itself, for reading and
|
||||
#! writing a property and indexing what implements `indexable`, and the
|
||||
#! intrinsics, which are code of their own rather than calls.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! The arguments of a call as lowering gathers them, each the value of a
|
||||
#! parameter, in order.
|
||||
pub struct gathered {
|
||||
var values: vector<operand>; #! The value given each parameter.
|
||||
}
|
||||
|
||||
#! The instance of a generic a node calls, as the checker recorded it.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @returns One more than the instance, or 0.
|
||||
pub def emitter::called(&mut this, node: u32): u32 {
|
||||
return this.c.called(node);
|
||||
}
|
||||
|
||||
#! Lowers a call.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::call(&mut this, node: u32): operand {
|
||||
val callee := this.c.checker.left_of(node);
|
||||
var decl := this.c.checker.ref_at(node);
|
||||
# A value of no function type is called through its `invoke`, given the value
|
||||
# as `this`, which the callee is however it is reached, as `obj.f` of `obj.f(x)`.
|
||||
if this.c.checker.has_flag(node, rf::invokes) && decl != 0 {
|
||||
return this.call_decl(node, decl, callee);
|
||||
}
|
||||
# `x.(d)()` calls the method a description names, which the callee names.
|
||||
if decl == 0 && this.c.checker.kind_of(callee) == nk::member_of {
|
||||
decl = this.c.checker.ref_at(callee);
|
||||
}
|
||||
|
||||
# An intrinsic of reflection records what it reaches as the call's own, as
|
||||
# `value_of(d)` does the function `d` describes, so its callee says what it is.
|
||||
val named := this.c.checker.kind_of(callee) == nk::name || this.c.checker.kind_of(callee) == nk::path
|
||||
? this.c.checker.ref_at(callee) : 0;
|
||||
if named != 0 && (named & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[named] == dk::function
|
||||
&& this.c.checker.decls.has(named, df::intrinsic) {
|
||||
return this.intrinsic(node, named);
|
||||
}
|
||||
if decl != 0 && (decl & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[decl] == dk::function {
|
||||
if this.c.checker.decls.has(decl, df::intrinsic) {
|
||||
return this.intrinsic(node, decl);
|
||||
}
|
||||
var object: u32 = 0;
|
||||
val callee_kind := this.c.checker.kind_of(callee);
|
||||
if (callee_kind == nk::member || callee_kind == nk::member_of) && this.c.checker.decls.has(decl, df::method) {
|
||||
object = this.c.checker.left_of(callee);
|
||||
}
|
||||
return this.call_decl(node, decl, object);
|
||||
}
|
||||
|
||||
# A method the compiler gives a built-in type, as `equals` of a number, but
|
||||
# not a field holding a function.
|
||||
if this.c.checker.kind_of(callee) == nk::member && decl == 0 {
|
||||
val field := this.c.checker.ref_at(callee);
|
||||
val is_field := field != 0 && (field & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[field] == dk::field;
|
||||
if !is_field {
|
||||
return this.builtin_call(node);
|
||||
}
|
||||
}
|
||||
return this.call_value(node);
|
||||
}
|
||||
|
||||
#! Lowers a call of a declared function, given the value a method is called on.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @param decl The function's declaration.
|
||||
#! @param object The node of the value a method is called on, or 0.
|
||||
#! @returns What it gives.
|
||||
def emitter::call_decl(&mut this, node: u32, decl: u32, object: u32): operand {
|
||||
val instance := this.called(node);
|
||||
val holder := this.c.trait_of_method(decl);
|
||||
var kind := this.c.type_of_function(decl, instance);
|
||||
if holder != 0 && object != 0 && instance == 0 {
|
||||
kind = this.c.checker.trait_method_type(decl, this.c.checker.reached(this.c.checker.type_at(object)));
|
||||
}
|
||||
|
||||
var given := gathered { values = vector::<operand>::new() };
|
||||
if object != 0 {
|
||||
given.values.append(this.receiver(object, this.c.checker.types.part_at(kind, 0), decl));
|
||||
}
|
||||
this.gather(node, decl, instance, kind, object != 0 ? 1 : 0, &mut given);
|
||||
return this.call_gathered(decl, instance, holder, kind, object, &given);
|
||||
}
|
||||
|
||||
#! Calls a declared function with its arguments gathered: through a vtable, as
|
||||
#! a trait's default, through its address where it is C's, or as it is.
|
||||
#! @param this The emitter.
|
||||
#! @param decl The function's declaration.
|
||||
#! @param instance One more than the instance called, or 0.
|
||||
#! @param holder The trait it is a member of, or 0.
|
||||
#! @param kind The function type called.
|
||||
#! @param object The node of the value a method is called on, or 0.
|
||||
#! @param given The arguments.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::call_gathered(
|
||||
&mut this,
|
||||
decl: u32,
|
||||
instance: u32,
|
||||
holder: u32,
|
||||
kind: u32,
|
||||
object: u32,
|
||||
given: &gathered
|
||||
): operand {
|
||||
# A trait's method is called through the vtable of the value's trait
|
||||
# reference, but a default it writes a body for, called on a value of a type
|
||||
# that takes it, is that body: the type's own instance of it, which the
|
||||
# checker made where the call gives the default's type arguments, or one
|
||||
# given the value as a reference to the trait.
|
||||
if holder != 0 {
|
||||
val receiver_type := object != 0 ? this.c.checker.unalias(this.c.checker.reached(this.c.checker.type_at(object))) : 0;
|
||||
val dynamic := object != 0 && (this.c.checker.named_kind(receiver_type) == dk::trait_type
|
||||
|| this.c.checker.types.kind_of(receiver_type) == ty::param);
|
||||
if dynamic || !this.c.checker.has_body(decl) {
|
||||
return this.call_trait(decl, holder, given, this.c.checker.types.first_of(kind));
|
||||
}
|
||||
if instance != 0 {
|
||||
return this.call_function(this.c.function_of(decl, instance), given, this.c.checker.types.first_of(kind));
|
||||
}
|
||||
val implemented := this.c.checker.implemented(receiver_type, this.c.checker.enclosing_trait(decl));
|
||||
val own := span::<u32>::empty();
|
||||
return this.call_function(this.c.default_of(decl, implemented, receiver_type, own), given, this.c.checker.types.first_of(kind));
|
||||
}
|
||||
|
||||
# A C function without a body is called through its address.
|
||||
if this.c.checker.decls.has(decl, df::foreign) && !this.c.checker.has_body(decl) {
|
||||
val address := this.build.value(op::c_address, span::<u32>::empty(), vt::word, this.c.linked_of(decl), 0);
|
||||
return this.call_address(address, this.c.call_type(kind), given, this.c.checker.types.first_of(kind));
|
||||
}
|
||||
return this.call_function(this.c.function_of(decl, instance), given, this.c.checker.types.first_of(kind));
|
||||
}
|
||||
|
||||
#! The reference a method's `this` takes to the value it is called on: the
|
||||
#! reference the value already is, read through to the one `this` takes, or one
|
||||
#! to the place it is, or to a copy of it where it is no place. A trait's
|
||||
#! default method takes it as a reference to the trait.
|
||||
#! @param this The emitter.
|
||||
#! @param object The node of the value.
|
||||
#! @param param The type of `this`.
|
||||
#! @param decl The method's declaration.
|
||||
#! @returns The reference.
|
||||
pub def emitter::receiver(&mut this, object: u32, param: u32, decl: u32): operand {
|
||||
var made := this.receiver_of(object, this.c.checker.decls.has(decl, df::mut_this));
|
||||
|
||||
# A default takes the value as a reference to its trait, unless it is compiled
|
||||
# for each type.
|
||||
val holder := this.c.trait_of_method(decl);
|
||||
if holder != 0 && made.count == 1 && !this.c.checker.typed_default(decl) {
|
||||
val given := this.c.checker.types.first_of(this.c.checker.unalias(made.kind));
|
||||
val referent := this.c.checker.unalias(given);
|
||||
if this.c.checker.named_kind(referent) != dk::trait_type {
|
||||
val implemented := this.c.checker.implemented(referent, this.c.checker.enclosing_trait(decl));
|
||||
val vtable := this.c.vtable_of(given, implemented);
|
||||
return operand::two(this.c.checker.types.reference(implemented, false),
|
||||
made.words[0], this.build.word(vtable as u64));
|
||||
}
|
||||
}
|
||||
return this.convert(&made, param);
|
||||
}
|
||||
|
||||
#! A reference to what a node gives, which a parameter taking a reference is
|
||||
#! given where the value given is no reference: one to its place, or to a copy
|
||||
#! of it the statement holds until it ends.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @param kind The reference's type.
|
||||
#! @returns The reference.
|
||||
pub def emitter::reference_arg(&mut this, node: u32, kind: u32): operand {
|
||||
if this.c.checker.is_place(node) {
|
||||
val place := this.place_of(node);
|
||||
return this.reference_to(&place, kind) if this.lends_as_is(place.kind, kind);
|
||||
}
|
||||
|
||||
# A value is referred to where the statement holds it, which closes it where
|
||||
# the statement ends. A value referred to as a trait it implements is held as
|
||||
# its own type.
|
||||
val referent := this.c.checker.types.first_of(this.c.checker.unalias(kind));
|
||||
var value := this.value_of_type(node);
|
||||
if this.c.checker.named_kind(this.c.checker.unalias(referent)) != dk::trait_type {
|
||||
value = this.convert(&value, referent);
|
||||
}
|
||||
val place := this.hold(&value);
|
||||
return this.reference_to(&place, kind);
|
||||
}
|
||||
|
||||
#! A reference to an operand an operator's method is given, which never keeps
|
||||
#! it: to the place it names, or to where the statement holds its value, which
|
||||
#! the statement closes where it ends.
|
||||
#! @param this The emitter.
|
||||
#! @param node The operand's node.
|
||||
#! @param kind The reference's type.
|
||||
#! @returns The reference.
|
||||
pub def emitter::lent_arg(&mut this, node: u32, kind: u32): operand {
|
||||
if this.c.checker.is_place(node) {
|
||||
val place := this.place_of(node);
|
||||
return this.reference_to(&place, kind) if this.lends_as_is(place.kind, kind);
|
||||
}
|
||||
val referent := this.c.checker.types.first_of(this.c.checker.unalias(kind));
|
||||
var value := this.value(node);
|
||||
value = this.convert(&value, referent);
|
||||
val place := this.hold(&value);
|
||||
if value.address == 0 {
|
||||
var held := operand::at(value.kind, place.address);
|
||||
this.keep(&held);
|
||||
}
|
||||
return this.reference_to(&place, kind);
|
||||
}
|
||||
|
||||
#! Whether a place is referred to as it is where a reference is wanted: one of
|
||||
#! its own type, of a trait it implements, of a reference it holds, or one
|
||||
#! written through. A place of another type is converted first, as a view where
|
||||
#! a `string` is wanted is copied into one, and the copy referred to.
|
||||
#! @param this The emitter.
|
||||
#! @param held The place's type.
|
||||
#! @param kind The reference's type.
|
||||
#! @returns Whether it is.
|
||||
def emitter::lends_as_is(&mut this, held: u32, kind: u32): bool {
|
||||
val wanted := this.c.checker.unalias(kind);
|
||||
return true if this.c.checker.types.kind_of(wanted) != ty::reference || this.c.checker.types.second_of(wanted) != 0;
|
||||
val referent := this.c.checker.unalias(this.c.checker.types.first_of(wanted));
|
||||
val own := this.c.checker.unalias(held);
|
||||
return referent == own || this.c.checker.named_kind(referent) == dk::trait_type
|
||||
|| this.c.checker.types.kind_of(own) == ty::reference;
|
||||
}
|
||||
|
||||
#! Gathers the arguments of a call: those written, each converted to its
|
||||
#! parameter or referred to where the parameter takes a reference, those a
|
||||
#! variadic parameter takes as a view of them, those a pack spreads, and the
|
||||
#! defaults of the parameters the call leaves out.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @param decl The function's declaration.
|
||||
#! @param instance One more than the instance called, or 0.
|
||||
#! @param kind The function type called.
|
||||
#! @param skip How many parameters come before the first argument, as
|
||||
#! `this`.
|
||||
#! @param into Where the arguments are gathered, after any already there.
|
||||
pub def emitter::gather(
|
||||
&mut this,
|
||||
node: u32,
|
||||
decl: u32,
|
||||
instance: u32,
|
||||
kind: u32,
|
||||
skip: u32,
|
||||
into: &mut gathered
|
||||
): void {
|
||||
val run := this.c.checker.right_of(node);
|
||||
val count := this.c.checker.types.part_count(kind);
|
||||
val variadic := this.c.checker.types.second_of(kind) != 0;
|
||||
val stop: u32 = variadic ? count - 1 : count;
|
||||
var p := skip;
|
||||
var rest_given := false;
|
||||
|
||||
for i in 0..this.c.checker.run_length(run) {
|
||||
val arg := this.c.checker.run_at(run, i as u32);
|
||||
val packs := this.is_spread(arg)
|
||||
&& this.c.checker.types.kind_of(this.c.checker.unalias(this.c.checker.reached(this.c.checker.type_at(this.c.checker.left_of(arg))))) == ty::pack;
|
||||
# A variadic function's pack ends in a view of the rest, which is the view a
|
||||
# variadic parameter takes as it is.
|
||||
val ends_in_rest := packs && this.c.checker.types.second_of(this.c.checker.reached(this.c.checker.type_at(this.c.checker.left_of(arg)))) != 0;
|
||||
break if p >= stop && variadic && !ends_in_rest;
|
||||
break if p >= count;
|
||||
|
||||
# `...args` spreads the values of a pack, one to a parameter.
|
||||
if packs {
|
||||
val pack := this.c.checker.left_of(arg);
|
||||
val ref := this.c.checker.ref_at(pack);
|
||||
val local := (ref & ref_local) != 0 ? this.local_of(ref & ref_mask) : 0;
|
||||
if local != 0 {
|
||||
val held: local_info = this.locals[local - 1];
|
||||
for j in 0..held.count {
|
||||
val place: location = this.packs[held.pack as u64 + j as u64];
|
||||
val value := this.load(&place);
|
||||
if ends_in_rest && variadic && j + 1 == held.count && p + 1 == count {
|
||||
into.values.append(value);
|
||||
p = count;
|
||||
rest_given = true;
|
||||
break;
|
||||
}
|
||||
# A value that closes is moved to the parameter taking it, as the
|
||||
# function then owns it: the pack is left empty, so it isn't closed
|
||||
# twice. The value was copied out of the pack's place as it was read.
|
||||
val param := this.c.checker.types.part_at(kind, p);
|
||||
if this.c.checker.closes(place.kind)
|
||||
&& this.c.checker.types.kind_of(this.c.checker.unalias(param)) != ty::reference {
|
||||
this.zero(&place);
|
||||
}
|
||||
into.values.append(this.convert(&value, param));
|
||||
p++;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
into.values.append(this.argument(arg, this.c.checker.types.part_at(kind, p)));
|
||||
p++;
|
||||
}
|
||||
|
||||
# A variadic parameter takes a view of the rest, which `...xs` gives as it is,
|
||||
# after the defaults of those before it the call leaves out.
|
||||
if variadic && !rest_given {
|
||||
if p < stop {
|
||||
this.defaults_of(decl, instance, kind, p, stop, into, node, skip);
|
||||
p = stop;
|
||||
}
|
||||
val element := this.c.checker.types.part_at(kind, count - 1);
|
||||
val view := this.c.checker.span_of(element);
|
||||
var rest := vector::<u32>::new();
|
||||
for i in (p - skip) as u64..this.c.checker.run_length(run) as u64 {
|
||||
rest.append(this.c.checker.run_at(run, i as u32));
|
||||
}
|
||||
if rest.length == 1 && this.is_spread(rest[0]) {
|
||||
into.values.append(this.spread_view(this.c.checker.left_of(rest[0]), view));
|
||||
} else {
|
||||
into.values.append(this.gathered_view(rest.view(), element, view));
|
||||
}
|
||||
p = count;
|
||||
}
|
||||
|
||||
# The parameters left out take their defaults, made where the function is.
|
||||
if p < count {
|
||||
this.defaults_of(decl, instance, kind, p, count, into, node, skip);
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a node is `...xs`.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @returns Whether it is.
|
||||
pub def emitter::is_spread(&mut this, node: u32): bool {
|
||||
return this.c.checker.kind_of(node) == nk::unary && this.c.checker.token_kind(node) == tk::spread;
|
||||
}
|
||||
|
||||
#! An argument as its parameter takes it: converted to its type, or referred to
|
||||
#! where it takes a reference and the argument is none.
|
||||
#! @param this The emitter.
|
||||
#! @param node The argument's node.
|
||||
#! @param param The parameter's type.
|
||||
#! @returns The value.
|
||||
pub def emitter::argument(&mut this, node: u32, param: u32) : operand {
|
||||
if this.c.checker.has_flag(node, rf::borrowed) {
|
||||
val made := this.reference_arg(node, param);
|
||||
return this.convert(&made, param);
|
||||
}
|
||||
return this.value_as(node, param);
|
||||
}
|
||||
|
||||
#! A view of the arguments a variadic parameter gathers, held in an array the
|
||||
#! statement holds.
|
||||
#! @param this The emitter.
|
||||
#! @param args The arguments' nodes.
|
||||
#! @param element The type of each.
|
||||
#! @param view The view's type.
|
||||
#! @returns The view.
|
||||
def emitter::gathered_view(&mut this, args: span<u32>, element: u32, view: u32): operand {
|
||||
val address := this.temporary_of(view);
|
||||
if args.length == 0 {
|
||||
this.build.clear(address, 2 * word_size);
|
||||
return operand::at(view, address);
|
||||
}
|
||||
|
||||
val array := this.c.checker.types.fixed(element, args.length as u32);
|
||||
val held := this.c.shape_of(element);
|
||||
val first := this.temporary_of(array);
|
||||
for i in 0..args.length {
|
||||
val value := this.value_as(args[i], element);
|
||||
this.adopt(&value);
|
||||
val place := location { kind = element, address = first, offset = i as u64 * held.size, framed = true };
|
||||
this.store(&place, &value);
|
||||
}
|
||||
this.build.store(access::i64, address, 0, first);
|
||||
this.build.store(access::i64, address, word_size, this.build.word(args.length));
|
||||
# What the array holds closes where the statement ends, as an array literal's does.
|
||||
var whole := operand::at(array, first);
|
||||
this.keep(&whole);
|
||||
return operand::at(view, address);
|
||||
}
|
||||
|
||||
#! The view `...xs` gives a variadic parameter: a view as it is, or one of every
|
||||
#! element of a fixed array.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node of what is spread.
|
||||
#! @param view The view's type.
|
||||
#! @returns The view.
|
||||
def emitter::spread_view(&mut this, node: u32, view: u32): operand {
|
||||
val through := this.c.checker.unalias(this.c.checker.reached(this.c.checker.type_at(node)));
|
||||
if this.c.checker.types.kind_of(through) == ty::fixed {
|
||||
val whole := this.object(node);
|
||||
val shape := this.c.shape_of(through);
|
||||
val address := this.temporary_of(view);
|
||||
this.build.store(access::i64, address, 0, this.address_of(&whole));
|
||||
this.build.store(access::i64, address, word_size, this.build.word(shape.count as u64));
|
||||
return operand::at(view, address);
|
||||
}
|
||||
val made := this.value(node);
|
||||
val read := this.through_references(&made);
|
||||
return this.convert(&read, view);
|
||||
}
|
||||
|
||||
#! The defaults of the parameters a call leaves out, each made where the
|
||||
#! function is declared, with the parameters before it named as the arguments
|
||||
#! the call gave them.
|
||||
#! @param this The emitter.
|
||||
#! @param decl The function's declaration.
|
||||
#! @param instance One more than the instance called, or 0.
|
||||
#! @param kind The function type called.
|
||||
#! @param from The first parameter left out.
|
||||
#! @param to One past the last.
|
||||
#! @param into The arguments gathered so far, which the defaults follow.
|
||||
def emitter::defaults_of(
|
||||
&mut this,
|
||||
decl: u32,
|
||||
instance: u32,
|
||||
kind: u32,
|
||||
from: u32,
|
||||
to: u32,
|
||||
into: &mut gathered,
|
||||
call: u32,
|
||||
skip: u32
|
||||
): void {
|
||||
# What a default asks of its call, as where it is or the text of an argument,
|
||||
# is of the call written, not of a call a default makes.
|
||||
val outer := this.defaulting;
|
||||
if outer.node == 0 {
|
||||
this.defaulting = default_site {
|
||||
node = call,
|
||||
file = this.c.checker.at.file,
|
||||
table = this.c.checker.at.table,
|
||||
args = this.c.checker.right_of(call),
|
||||
skip = skip
|
||||
};
|
||||
}
|
||||
val table := this.c.body_table(decl, instance);
|
||||
val saved := this.c.checker.at;
|
||||
this.c.checker.use_table(table);
|
||||
val node: u32 = this.c.checker.decls.nodes.p[decl];
|
||||
val params := this.c.checker.record(node, function_slot::params);
|
||||
val mark := this.scope_mark();
|
||||
if outer.node == 0 {
|
||||
this.defaulting.params = params;
|
||||
}
|
||||
|
||||
# The parameters given are named as what they were given.
|
||||
for i in 0..from {
|
||||
break if i as u64 >= into.values.length || i >= this.c.checker.run_length(params);
|
||||
val param := this.c.checker.run_at(params, i as u32);
|
||||
val place := this.hold(&into.values[i]);
|
||||
this.declare(param, place, false);
|
||||
}
|
||||
for i in from..to {
|
||||
break if i >= this.c.checker.run_length(params);
|
||||
val param := this.c.checker.run_at(params, i as u32);
|
||||
val value := this.c.checker.record(param, parameter_slot::value);
|
||||
val wanted := this.c.checker.types.part_at(kind, i as u32);
|
||||
if value == 0 {
|
||||
this.problem(param, "The parameter is left out and has no default.");
|
||||
into.values.append(this.zero_value(wanted));
|
||||
continue;
|
||||
}
|
||||
val made := this.value_as(value, wanted);
|
||||
into.values.append(made);
|
||||
val place := this.hold(&made);
|
||||
this.declare(param, place, false);
|
||||
}
|
||||
|
||||
this.forget_locals(mark);
|
||||
this.c.checker.at = saved;
|
||||
this.defaulting = outer;
|
||||
}
|
||||
|
||||
#! The argument a call wrote for a parameter a default names, where the default
|
||||
#! being made names one: the node, in the call's own table.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node naming the parameter, in the default.
|
||||
#! @returns The argument's node, or 0.
|
||||
pub def emitter::written_argument(&mut this, node: u32): u32 {
|
||||
return 0 if this.defaulting.node == 0 || this.c.checker.kind_of(node) != nk::name;
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
return 0 if (ref & ref_local) == 0;
|
||||
val param := ref & ref_mask;
|
||||
val params := this.defaulting.params;
|
||||
for i in 0..this.c.checker.run_length(params) {
|
||||
continue if this.c.checker.run_at(params, i as u32) != param;
|
||||
return 0 if (i as u32) < this.defaulting.skip;
|
||||
val at := i as u32 - this.defaulting.skip;
|
||||
val saved := this.c.checker.at;
|
||||
this.c.checker.use_table(this.defaulting.table);
|
||||
val arg := at < this.c.checker.run_length(this.defaulting.args) ? this.c.checker.run_at(this.defaulting.args, at) : 0;
|
||||
this.c.checker.at = saved;
|
||||
return arg;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Calls a function of the program with the arguments gathered.
|
||||
#! @param this The emitter.
|
||||
#! @param index The function.
|
||||
#! @param given The arguments.
|
||||
#! @param result The type it gives.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::call_function(&mut this, index: u32, given: &gathered, result: u32): operand {
|
||||
var args := vector::<u32>::new();
|
||||
this.pass_args(given, &mut args);
|
||||
return this.call_with(op::call, index, 0, &mut args, result);
|
||||
}
|
||||
|
||||
#! Adds the words of the arguments gathered to a call's operands, which the call
|
||||
#! takes as its own.
|
||||
#! @param this The emitter.
|
||||
#! @param given The arguments.
|
||||
#! @param args The operands.
|
||||
def emitter::pass_args(&mut this, given: &gathered, args: &mut vector<u32>): void {
|
||||
for &each in given.values {
|
||||
this.adopt(&each);
|
||||
each.args(args);
|
||||
}
|
||||
}
|
||||
|
||||
#! Adds a call, writing what it gives to a cell the statement holds where it is
|
||||
#! held in memory, whose address the call is given last.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The kind of call.
|
||||
#! @param aux The function or signature called.
|
||||
#! @param imm The slot of a vtable called through, or 0.
|
||||
#! @param args The operands.
|
||||
#! @param result The type it gives.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::call_with(&mut this, kind: op, aux: u32, imm: u64, args: &mut vector<u32>, result: u32): operand {
|
||||
val held := this.c.shape_of(result);
|
||||
if in_memory(&held) {
|
||||
# A value of no bytes, as a struct of no fields, is returned as nothing, so
|
||||
# the call is given nowhere to write it.
|
||||
val address := this.temporary_of(result);
|
||||
if returned_in_memory(&held) {
|
||||
args.append(address);
|
||||
}
|
||||
this.build.effect(kind, args.view(), aux, imm);
|
||||
return operand::at(result, address);
|
||||
}
|
||||
|
||||
var gives := vector::<vt>::new();
|
||||
for _ in 0..word_count(&held) {
|
||||
gives.append(word_type(&held));
|
||||
}
|
||||
val index := this.build.emit(kind, args.view(), gives.view(), aux, imm);
|
||||
var made := operand { kind = result, count = word_count(&held) };
|
||||
val first: u32 = this.build.code.insts[index].result;
|
||||
for i in 0..made.count {
|
||||
made.words[i] = first + i as u32;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Calls a trait's method through the vtable of the reference it is given.
|
||||
#! @param this The emitter.
|
||||
#! @param decl The method's declaration.
|
||||
#! @param holder The trait's declaration.
|
||||
#! @param given The arguments, the trait reference first.
|
||||
#! @param result The type it gives.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::call_trait(&mut this, decl: u32, holder: u32, given: &gathered, result: u32): operand {
|
||||
var args := vector::<u32>::new();
|
||||
this.pass_args(given, &mut args);
|
||||
# The reference's own trait numbers the slots its vtables hold; within a
|
||||
# default, `this` holds the vtable of the trait declaring it.
|
||||
val reached := given.values.length != 0 ? this.c.checker.types.first_of(this.c.checker.unalias(given.values[0].kind)) : 0;
|
||||
val plain := this.c.checker.unalias(reached);
|
||||
var owner := this.c.checker.named_kind(plain) == dk::trait_type ? this.c.checker.types.first_of(plain) : holder;
|
||||
if this.c.checker.types.kind_of(plain) == ty::param {
|
||||
val param := this.c.checker.types.first_of(plain);
|
||||
val declarer: u32 = this.c.checker.decls.owners.p[param];
|
||||
if this.c.checker.decls.names.p[param] == names::self_id && declarer != 0
|
||||
&& this.c.checker.decls.kinds.p[declarer] == dk::trait_type {
|
||||
owner = declarer;
|
||||
}
|
||||
}
|
||||
val slot := this.c.dispatch_index(owner, decl);
|
||||
val signature := this.c.trait_signature(decl, owner, result,
|
||||
this.c.checker.named_kind(plain) == dk::trait_type ? plain : 0);
|
||||
return this.call_with(op::call_trait, signature, slot as u64, &mut args, result);
|
||||
}
|
||||
|
||||
#! Calls a C function through its address.
|
||||
#! @param this The emitter.
|
||||
#! @param address The function's address.
|
||||
#! @param kind Its function type.
|
||||
#! @param given The arguments.
|
||||
#! @param result The type it gives.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::call_address(&mut this, address: u32, kind: u32, given: &gathered, result: u32): operand {
|
||||
var args := vector::<u32>::new();
|
||||
args.append(address);
|
||||
this.pass_args(given, &mut args);
|
||||
return this.call_with(op::call_c, this.c.signature_of(kind), 0, &mut args, result);
|
||||
}
|
||||
|
||||
#! Lowers a call of a function value: a local, a field, or anything giving a
|
||||
#! function, or the address of a C function.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @returns What it gives.
|
||||
def emitter::call_value(&mut this, node: u32): operand {
|
||||
val callee := this.c.checker.left_of(node);
|
||||
val made := this.value_of_type(callee);
|
||||
val function := this.through_references(&made);
|
||||
|
||||
# A function value has no defaults, but takes a view of the rest where its last
|
||||
# parameter is variadic, and a value of each type of a pack spread into it.
|
||||
var given := gathered { values = vector::<operand>::new() };
|
||||
this.gather(node, 0, 0, this.called_type(function.kind), 0, &mut given);
|
||||
return this.call_function_value(&function, &given);
|
||||
}
|
||||
|
||||
#! The function type a function value, or the address of a C function, is called
|
||||
#! as.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The value's type.
|
||||
#! @returns The function type.
|
||||
pub def emitter::called_type(&mut this, kind: u32): u32 {
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
return this.c.checker.is_std(plain, word::cfn)
|
||||
? this.c.checker.unalias(this.c.checker.types.part_at(plain, 0))
|
||||
: plain;
|
||||
}
|
||||
|
||||
#! Calls a function value, or the address of a C function, given its arguments.
|
||||
#! @param this The emitter.
|
||||
#! @param function The value.
|
||||
#! @param given The arguments, as its parameters take them.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::call_function_value(&mut this, function: &operand, given: &gathered): operand {
|
||||
val plain := this.called_type(function.kind);
|
||||
val result := this.c.checker.types.first_of(plain);
|
||||
if this.c.checker.is_std(this.c.checker.unalias(function.kind), word::cfn) {
|
||||
return this.call_address(function.words[0], plain, given, result);
|
||||
}
|
||||
|
||||
var args := vector::<u32>::new();
|
||||
function.args(&mut args);
|
||||
this.pass_args(given, &mut args);
|
||||
return this.call_with(op::call_value, this.c.signature_of(this.c.call_type(plain)), 0, &mut args, result);
|
||||
}
|
||||
|
||||
#! Calls an accessor the compiler calls itself, as `at` indexing what implements
|
||||
#! `indexable`, given the value it is called on and its other argument.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node the call stands for.
|
||||
#! @param decl The accessor's declaration.
|
||||
#! @param object The node of the value it is called on.
|
||||
#! @param arg The node of its other argument, or 0.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::accessor_call(&mut this, node: u32, decl: u32, object: u32, arg: u32): operand {
|
||||
val owner := this.c.checker.reached(this.object_type(object));
|
||||
val target := this.c.method_target(decl, owner);
|
||||
var given := gathered { values = vector::<operand>::new() };
|
||||
given.values.append(this.receiver(object, this.c.checker.types.part_at(target.kind, 0), decl));
|
||||
if arg != 0 && this.c.checker.types.part_count(target.kind) > 1 {
|
||||
given.values.append(this.value_as(arg, this.c.checker.types.part_at(target.kind, 1)));
|
||||
}
|
||||
val result := this.c.checker.types.first_of(target.kind);
|
||||
if target.holder != 0 {
|
||||
return this.call_trait(decl, target.holder, &given, result);
|
||||
}
|
||||
return this.call_function(target.function, &given, result);
|
||||
}
|
||||
|
||||
#! Calls a method on a value already lowered, as the compiler does where a loop
|
||||
#! asks an iterator for its next element, or a value is closed: given a
|
||||
#! reference to it, and the other arguments.
|
||||
#! @param this The emitter.
|
||||
#! @param decl The method's declaration.
|
||||
#! @param owner The type it is called on.
|
||||
#! @param receiver The reference `this` takes.
|
||||
#! @param others The other arguments.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::method_call(&mut this, decl: u32, owner: u32, receiver: &operand, others: span<operand>): operand {
|
||||
val target := this.c.method_target(decl, owner);
|
||||
var given := gathered { values = vector::<operand>::new() };
|
||||
given.values.append(this.convert(receiver, this.c.checker.types.part_at(target.kind, 0)));
|
||||
given.values.extend(others);
|
||||
val result := this.c.checker.types.first_of(target.kind);
|
||||
if target.holder != 0 {
|
||||
return this.call_trait(decl, target.holder, &given, result);
|
||||
}
|
||||
return this.call_function(target.function, &given, result);
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
#! @file closures.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains functions as values: a function named, the instance of a generic
|
||||
#! one, the address of a C function, and anonymous functions, which refer to
|
||||
#! the locals of the function around them they name. An anonymous function
|
||||
#! naming any is a closure, given an environment of references to them after
|
||||
#! its parameters, which is a cell of the frame making it, as the locals are.
|
||||
#! So a closure is good while the locals it refers to are, and one outliving
|
||||
#! them is the program's mistake, as a reference outliving what it refers to
|
||||
#! is.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! A function named as a value: its index plus one, with no environment, a C
|
||||
#! function's address, or the address of the entry C calls one of the program
|
||||
#! back through, where it is given as a `cfn`.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node naming it.
|
||||
#! @param decl The function's declaration.
|
||||
#! @returns The value.
|
||||
pub def emitter::function_value(&mut this, node: u32, decl: u32): operand {
|
||||
val kind := this.c.checker.type_at(node);
|
||||
if this.c.checker.decls.has(decl, df::foreign) {
|
||||
return operand::one(kind, this.build.value(op::c_address, span::<u32>::empty(), vt::word, this.c.linked_of(decl), 0));
|
||||
}
|
||||
if this.c.checker.decls.has(decl, df::callback) && this.c.checker.is_std(this.c.checker.unalias(kind), word::cfn) {
|
||||
val function := this.c.function_of(decl, 0);
|
||||
return operand::one(kind, this.build.value(op::callback, span::<u32>::empty(), vt::word, function, 0));
|
||||
}
|
||||
|
||||
var instance := this.called(node);
|
||||
# A generic type's function named through it is the instance its arguments give.
|
||||
if instance == 0 && this.c.checker.decls.has(decl, df::generic) && this.c.checker.kind_of(node) == nk::path {
|
||||
val target := this.c.checker.path_target(node);
|
||||
if target.owner != bt::error && this.c.checker.types.kind_of(target.owner) == ty::named {
|
||||
var args := this.c.checker.types.parts_of(target.owner);
|
||||
instance = this.c.checker.instance_of(decl, args.view(), node);
|
||||
}
|
||||
}
|
||||
val index := this.c.function_of(decl, instance);
|
||||
|
||||
# Named where another function type is expected, it is called through a
|
||||
# function of that type made for it.
|
||||
if this.c.checker.has_flag(node, rf::adapted) {
|
||||
val wanted := this.c.checker.converted(node);
|
||||
val adapter := this.c.adapter_of(this.c.checker.at.table, node, this.c.checker.at.file, wanted, index);
|
||||
this.c.names(this.function, adapter);
|
||||
return operand::two(wanted, this.build.word(adapter as u64 + 1), this.build.word(0));
|
||||
}
|
||||
this.c.program.functions[index].taken = true;
|
||||
this.c.names(this.function, index);
|
||||
return operand::two(kind, this.build.word(index as u64 + 1), this.build.word(0));
|
||||
}
|
||||
|
||||
#! An anonymous function as a value: its index plus one, and where it names
|
||||
#! locals of the functions around it, the environment holding a reference to
|
||||
#! each, in a cell of the frame.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `lambda` node.
|
||||
#! @returns The value.
|
||||
pub def emitter::lambda_value(&mut this, node: u32): operand {
|
||||
val kind := this.c.checker.type_at(node);
|
||||
var captures := this.captures_of(node);
|
||||
val index := this.c.lambda_of(this.c.checker.at.table, node, this.c.checker.at.file, kind, captures.length != 0);
|
||||
val outer: u32 = this.c.program.functions[this.function].decl != 0
|
||||
? this.c.program.functions[this.function].decl
|
||||
: this.c.program.functions[this.function].outer;
|
||||
this.c.program.functions[index].outer = outer;
|
||||
this.c.program.functions[index].taken = true;
|
||||
this.c.names(this.function, index);
|
||||
val function := this.build.word(index as u64 + 1);
|
||||
if captures.length == 0 {
|
||||
return operand::two(kind, function, this.build.word(0));
|
||||
}
|
||||
|
||||
# The environment is a fixed array of references, one to each local captured.
|
||||
val reference := this.c.checker.types.reference(bt::int64, false);
|
||||
val holder := this.c.checker.types.fixed(reference, captures.length as u32);
|
||||
val shape := this.c.shape_of(holder);
|
||||
val cell := this.build.code.add_cell(round_up(shape.size, word_size) as u32, word_size as u32, ck::local, 0);
|
||||
val environment := this.build.cell_address(cell);
|
||||
for i in 0..captures.length {
|
||||
val local := this.local_of(captures[i]);
|
||||
if local == 0 {
|
||||
this.problem(node, "A local the function captures is not where it is named.");
|
||||
continue;
|
||||
}
|
||||
val held: local_info = this.locals[local - 1];
|
||||
val made := this.reference_to(&held.place, reference);
|
||||
val at := location { kind = reference, address = environment, offset = i as u64 * word_size };
|
||||
this.store(&at, &made);
|
||||
}
|
||||
return operand::two(kind, function, environment);
|
||||
}
|
||||
|
||||
#! The locals of the functions around an anonymous function that it names, or
|
||||
#! that one within it names, in the order it first names them.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `lambda` node.
|
||||
#! @returns The nodes declaring them.
|
||||
pub def emitter::captures_of(&mut this, node: u32): vector<u32> {
|
||||
var made := vector::<u32>::new();
|
||||
val first := this.lowest_node(node);
|
||||
var walk := node_walk::new(node);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
# A contract left out captures nothing.
|
||||
continue if this.c.checker.contracts_off && this.c.checker.kind_of(at) == nk::contract;
|
||||
if this.c.checker.kind_of(at) == nk::name {
|
||||
val ref := this.c.checker.ref_at(at);
|
||||
if (ref & ref_local) != 0 {
|
||||
val local := ref & ref_mask;
|
||||
if (local < first || local > node) && !made.view().contains(&local) {
|
||||
made.append(local);
|
||||
}
|
||||
}
|
||||
}
|
||||
walk.enter(this.c.checker.syn(), at);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The first node of what a node was parsed from, which the parser made before
|
||||
#! the node itself, one after another.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @returns The lowest node under it.
|
||||
pub def emitter::lowest_node(&mut this, node: u32): u32 {
|
||||
var lowest := node;
|
||||
var walk := node_walk::new(node);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
lowest = at < lowest ? at : lowest;
|
||||
walk.enter(this.c.checker.syn(), at);
|
||||
}
|
||||
return lowest;
|
||||
}
|
||||
|
||||
#! The function an anonymous function is lowered to, made where it is new.
|
||||
#! @param this The compiler.
|
||||
#! @param table The result table its nodes' results are in.
|
||||
#! @param node Its `lambda` node.
|
||||
#! @param file The file it is in.
|
||||
#! @param kind Its function type.
|
||||
#! @param closure Whether it captures locals.
|
||||
#! @returns The function's index.
|
||||
pub def lowerer::lambda_of(&mut this, table: u32, node: u32, file: u32, kind: u32, closure: bool): u32 {
|
||||
val key := pair_key(table, node);
|
||||
if this.lambda_ids.get(&key) is some(got) {
|
||||
return got;
|
||||
}
|
||||
val index := this.add_function(string::from("<lambda>", nothing {}), this.signature_of(kind));
|
||||
this.lambda_ids.set(key, index);
|
||||
this.program.functions[index].node = node;
|
||||
this.program.functions[index].table = table;
|
||||
this.program.functions[index].file = file;
|
||||
this.program.functions[index].offset = this.node_offset(file, node);
|
||||
this.program.functions[index].closure = closure;
|
||||
this.want(index);
|
||||
return index;
|
||||
}
|
||||
|
||||
#! The adapter of a function named as a value of another function type: a
|
||||
#! function of that type, made where it is first named, which calls it as a call
|
||||
#! would, with the defaults of the parameters the type leaves out.
|
||||
#! @param this The compiler.
|
||||
#! @param table The result table holding the node.
|
||||
#! @param node The node naming the function.
|
||||
#! @param file The file the node is in.
|
||||
#! @param kind The function type the adapter is of.
|
||||
#! @param callee The function it calls.
|
||||
#! @returns The adapter.
|
||||
pub def lowerer::adapter_of(&mut this, table: u32, node: u32, file: u32, kind: u32, callee: u32): u32 {
|
||||
val key := pair_key(table, node);
|
||||
if this.adapter_ids.get(&key) is some(got) {
|
||||
return got;
|
||||
}
|
||||
var name := string::from(this.program.functions[callee].name.view(), nothing {});
|
||||
name.append(" (adapted)");
|
||||
val index := this.add_function(move(&mut name), this.signature_of(kind));
|
||||
this.adapter_ids.set(key, index);
|
||||
this.program.functions[index].adapted = callee + 1;
|
||||
this.program.functions[index].node = node;
|
||||
this.program.functions[index].table = table;
|
||||
this.program.functions[index].file = file;
|
||||
this.program.functions[index].offset = this.node_offset(file, node);
|
||||
this.program.functions[index].taken = true;
|
||||
this.want(index);
|
||||
return index;
|
||||
}
|
||||
|
||||
#! Lowers an adapter: each parameter of the type it is of is given to the
|
||||
#! function it calls as a call gives an argument: a value lent where it takes a
|
||||
#! reference, closed once the call returns, a reference read through where it
|
||||
#! takes what it reaches, and a value converted or moved where it takes a value.
|
||||
#! The parameters the type leaves out take their defaults, and what the function
|
||||
#! gives is converted to what the type gives.
|
||||
#! @param this The emitter.
|
||||
pub def emitter::lower_adapter(&mut this): void {
|
||||
val entry := this.function;
|
||||
val callee: u32 = this.c.program.functions[entry].adapted - 1;
|
||||
val node: u32 = this.c.program.functions[entry].node;
|
||||
this.table = this.c.program.functions[entry].table;
|
||||
this.use_own_table();
|
||||
this.at_node(node);
|
||||
val wanted := this.c.checker.unalias(this.c.checker.converted(node));
|
||||
val kind := this.c.checker.unalias(this.c.checker.type_at(node));
|
||||
this.result = this.c.checker.types.first_of(wanted);
|
||||
val signature: u32 = this.c.program.functions[entry].signature;
|
||||
val count := this.c.checker.types.part_count(wanted);
|
||||
|
||||
var next: u32 = 0;
|
||||
var given := gathered { values = vector::<operand>::new() };
|
||||
var lent := vector::<location>::new();
|
||||
for i in 0..count {
|
||||
val held := this.take_param(signature, i as u32, &mut next);
|
||||
val param := this.c.checker.types.part_at(kind, i as u32);
|
||||
val place := this.param_place(0, held);
|
||||
val takes_reference := this.c.is_reference_type(this.c.checker.unalias(param));
|
||||
if takes_reference && !this.c.is_reference_type(this.c.checker.unalias(held.kind)) {
|
||||
given.values.append(this.reference_to(&place, param));
|
||||
if this.c.checker.closes(held.kind) {
|
||||
lent.append(place);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
val value := this.load(&place);
|
||||
given.values.append(this.convert(&value, param));
|
||||
# What converts lends from what the adapter holds, as a string a view of its
|
||||
# text, and what doesn't is moved into the call.
|
||||
if !takes_reference && this.c.checker.closes(held.kind)
|
||||
&& this.c.checker.unalias(held.kind) != this.c.checker.unalias(param) {
|
||||
lent.append(place);
|
||||
}
|
||||
}
|
||||
|
||||
val decl: u32 = this.c.program.functions[callee].decl;
|
||||
val instance: u32 = this.c.program.functions[callee].instance;
|
||||
val total := this.c.checker.types.part_count(kind);
|
||||
if count < total {
|
||||
this.defaults_of(decl, instance, kind, count as u32, total, &mut given, node, 0);
|
||||
}
|
||||
val made := this.call_function(callee, &given, this.c.checker.types.first_of(kind));
|
||||
val result := this.convert(&made, this.result);
|
||||
for &each in lent.view() {
|
||||
this.close(&each);
|
||||
}
|
||||
var args := vector::<u32>::new();
|
||||
result.args(&mut args);
|
||||
this.give_back(args.view());
|
||||
}
|
||||
|
||||
#! Lowers an anonymous function: the locals it captures are reached through the
|
||||
#! references its environment holds, which it never closes, since the function
|
||||
#! declaring them owns them.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `lambda` node.
|
||||
pub def emitter::lower_lambda(&mut this, node: u32): void {
|
||||
this.table = this.c.program.functions[this.function].table;
|
||||
this.use_own_table();
|
||||
this.at_node(node);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
this.result = this.c.checker.types.first_of(kind);
|
||||
val params := this.c.checker.left_of(node);
|
||||
val body := this.c.checker.record(node, lambda_slot::body);
|
||||
val signature: u32 = this.c.program.functions[this.function].signature;
|
||||
val total := this.c.program.signatures[signature].params.length as u32;
|
||||
|
||||
var next: u32 = 0;
|
||||
var taken := vector::<operand>::new();
|
||||
for i in 0..total {
|
||||
taken.append(this.take_param(signature, i as u32, &mut next));
|
||||
}
|
||||
|
||||
# The environment follows the parameters.
|
||||
if this.c.program.functions[this.function].closure {
|
||||
val environment := this.build.param(next);
|
||||
var captures := this.captures_of(node);
|
||||
val reference := this.c.checker.types.reference(bt::int64, false);
|
||||
for i in 0..captures.length {
|
||||
val at := location { kind = reference, address = environment, offset = i as u64 * word_size };
|
||||
val made := this.load(&at);
|
||||
val captured := captures[i];
|
||||
val place := location { kind = this.c.checker.type_at(captured), address = made.words[0] };
|
||||
this.declare(captured, place, false);
|
||||
}
|
||||
}
|
||||
|
||||
this.plan_lambda(node);
|
||||
for i in 0..this.c.checker.run_length(params) {
|
||||
break if i as u64 >= taken.length;
|
||||
val param := this.c.checker.run_at(params, i as u32);
|
||||
val place := this.param_place(param, taken[i]);
|
||||
this.declare(param, place, this.c.checker.closes(taken[i].kind));
|
||||
}
|
||||
this.safepoint();
|
||||
this.lower_body(body);
|
||||
}
|
||||
@@ -0,0 +1,553 @@
|
||||
#! @file composite.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lowering of values built from others, and of what reads into
|
||||
#! them: struct and union literals, with the defaults of the fields they leave
|
||||
#! out, array literals, the members of a value, its properties through their
|
||||
#! accessors, and slices.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! A struct or union literal, built in a cell the statement holds.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `construct` node.
|
||||
#! @returns The value.
|
||||
pub def emitter::construct(&mut this, node: u32): operand {
|
||||
return this.construct_of(this.c.checker.type_at(node), this.c.checker.right_of(node));
|
||||
}
|
||||
|
||||
#! A struct or union literal of a type, given its field inits, built in a cell
|
||||
#! the statement holds, as an attribute's value is too.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The type.
|
||||
#! @param inits The run of `field_init` nodes.
|
||||
#! @returns The value.
|
||||
pub def emitter::construct_of(&mut this, kind: u32, inits: u32): operand {
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
return operand::none(kind) if plain == bt::void_type;
|
||||
val index := this.c.layout_of(plain);
|
||||
val shape := this.c.layout_at(index);
|
||||
val address := this.temporary_of(kind);
|
||||
val decl := this.c.checker.types.first_of(plain);
|
||||
# A struct's fields given write themselves, so what is cleared is the rest:
|
||||
# the padding, and the fields left out, which take their defaults over zero.
|
||||
if shape.kind != lk::variant && this.c.checker.decls.kinds.p[decl] != dk::union_type {
|
||||
this.clear_unwritten(address, index, inits);
|
||||
} else {
|
||||
this.build.clear(address, shape.size);
|
||||
}
|
||||
val whole := location { kind = kind, address = address, framed = true };
|
||||
|
||||
# A union holds the one field it is given, whose index is its tag. A C union
|
||||
# holds it over the others, without a tag.
|
||||
if shape.kind == lk::variant {
|
||||
if this.c.checker.run_length(inits) == 0 {
|
||||
this.defaults(&whole);
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
val init := this.c.checker.run_at(inits, 0);
|
||||
val field := this.c.checker.ref_at(init);
|
||||
for i in 0..shape.count {
|
||||
val each := this.c.field_at(index, i as u32);
|
||||
continue if each.decl != field;
|
||||
if shape.niche == 0 {
|
||||
this.build.store(access::i64, address, 0, this.build.word(i));
|
||||
}
|
||||
val value := this.value_as(this.c.checker.left_of(init), each.type_id);
|
||||
this.adopt(&value);
|
||||
val at := this.within(&whole, each.offset, each.type_id);
|
||||
this.store(&at, &value);
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
|
||||
var given := vector::<u32>::new();
|
||||
for i in 0..this.c.checker.run_length(inits) {
|
||||
val init := this.c.checker.run_at(inits, i as u32);
|
||||
val field := this.c.checker.ref_at(init);
|
||||
given.append(field);
|
||||
val at := this.field_in(&whole, plain, field, 0);
|
||||
val value := this.value_as(this.c.checker.left_of(init), at.kind);
|
||||
this.adopt(&value);
|
||||
this.store(&at, &value);
|
||||
}
|
||||
if this.c.checker.decls.kinds.p[decl] == dk::union_type {
|
||||
if given.length == 0 {
|
||||
this.defaults(&whole);
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
|
||||
# The fields left out take their defaults, or the defaults of what they hold.
|
||||
for i in 0..shape.count {
|
||||
val each := this.c.field_at(index, i as u32);
|
||||
var was_given := false;
|
||||
for &field in given {
|
||||
was_given = was_given || field == each.decl;
|
||||
}
|
||||
continue if was_given;
|
||||
val at := this.within(&whole, each.offset, each.type_id);
|
||||
this.default_of(&at, each.decl, each.spread);
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
|
||||
#! Clears the bytes of a struct literal's memory that none of the fields it
|
||||
#! gives writes, where each is written whole: those of its own fields, and not
|
||||
#! those a spread brings in, whose spread field is cleared whole.
|
||||
#! @param this The emitter.
|
||||
#! @param address Where the struct is built.
|
||||
#! @param index Its layout.
|
||||
#! @param inits The run of `field_init` nodes.
|
||||
def emitter::clear_unwritten(&mut this, address: u32, index: u32, inits: u32): void {
|
||||
val shape := this.c.layout_at(index);
|
||||
var written := vector::<bool>::new();
|
||||
written.resize(shape.size);
|
||||
for i in 0..this.c.checker.run_length(inits) {
|
||||
val field := this.c.checker.ref_at(this.c.checker.run_at(inits, i as u32));
|
||||
for k in 0..shape.count {
|
||||
val each := this.c.field_at(index, k as u32);
|
||||
continue if each.decl != field || each.spread;
|
||||
val end := each.offset + this.c.shape_of(each.type_id).size;
|
||||
for at in each.offset..end {
|
||||
written[at] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
var at: u64 = 0;
|
||||
while at < shape.size {
|
||||
if written[at] {
|
||||
at++;
|
||||
continue;
|
||||
}
|
||||
var end := at;
|
||||
while end < shape.size && !written[end] {
|
||||
end++;
|
||||
}
|
||||
this.clear_bytes(address, at, end);
|
||||
at = end;
|
||||
}
|
||||
}
|
||||
|
||||
#! Clears a run of bytes at an address: as stores of zero where it is a few
|
||||
#! words, as padding is, and otherwise at once.
|
||||
#! @param this The emitter.
|
||||
#! @param address The address.
|
||||
#! @param from The first byte.
|
||||
#! @param to Past the last.
|
||||
def emitter::clear_bytes(&mut this, address: u32, from: u64, to: u64): void {
|
||||
if to - from > 4 * word_size {
|
||||
val first := from == 0 ? address : this.build.binary(op::add, address, this.build.word(from));
|
||||
this.build.clear(first, to - from);
|
||||
return;
|
||||
}
|
||||
val zero := this.build.word(0);
|
||||
var at := from;
|
||||
while at < to {
|
||||
val left := to - at;
|
||||
val how := left >= 8 && at % 8 == 0 ? access::i64
|
||||
: left >= 4 && at % 4 == 0 ? access::u32
|
||||
: left >= 2 && at % 2 == 0 ? access::u16 : access::u8;
|
||||
this.build.store(how, address, at, zero);
|
||||
at += how.width();
|
||||
}
|
||||
}
|
||||
|
||||
#! `a..b`, or `a..=b`, as a value: std's `range<T>`, built in a cell the
|
||||
#! statement holds from its fields in the order std declares them, its start,
|
||||
#! its end, and whether it is still to give its end.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `range` node.
|
||||
#! @returns The value.
|
||||
pub def emitter::range_value(&mut this, node: u32): operand {
|
||||
val kind := this.c.checker.type_at(node);
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
val index := this.c.layout_of(plain);
|
||||
val shape := this.c.layout_at(index);
|
||||
val address := this.temporary_of(kind);
|
||||
this.build.clear(address, shape.size);
|
||||
val whole := location { kind = kind, address = address, framed = true };
|
||||
|
||||
val start := this.c.field_at(index, 0);
|
||||
val first := this.value_as(this.c.checker.left_of(node), start.type_id);
|
||||
val at_start := this.within(&whole, start.offset, start.type_id);
|
||||
this.store(&at_start, &first);
|
||||
|
||||
val end := this.c.field_at(index, 1);
|
||||
val last := this.value_as(this.c.checker.right_of(node), end.type_id);
|
||||
val at_end := this.within(&whole, end.offset, end.type_id);
|
||||
this.store(&at_end, &last);
|
||||
|
||||
if this.c.checker.token_kind(node) == tk::cascadeeq {
|
||||
val held := this.c.field_at(index, 2);
|
||||
this.build.store(access::u8, address, held.offset, this.build.word(1));
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
|
||||
#! Sets the defaults of the fields of a zeroed struct, and of the first field a
|
||||
#! zeroed union holds, as a variable without a value is given them.
|
||||
#! @param this The emitter.
|
||||
#! @param place The place, zeroed.
|
||||
pub def emitter::defaults(&mut this, place: &location): void {
|
||||
val plain := this.c.checker.unalias(place.kind);
|
||||
return if this.c.checker.types.kind_of(plain) != ty::named;
|
||||
val index := this.c.layout_of(plain);
|
||||
val shape := this.c.layout_at(index);
|
||||
val decl := this.c.checker.types.first_of(plain);
|
||||
val named := this.c.checker.decls.kinds.p[decl];
|
||||
return if named != dk::struct_type && named != dk::union_type;
|
||||
|
||||
# A union is zeroed as its first field, and so is a C union.
|
||||
if named == dk::union_type {
|
||||
if shape.count != 0 {
|
||||
val first := this.c.field_at(index, 0);
|
||||
val inner := this.c.layout_at(first.layout);
|
||||
if inner.kind == lk::record {
|
||||
val at := this.within(place, first.offset, first.type_id);
|
||||
this.defaults(&at);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
for i in 0..shape.count {
|
||||
val each := this.c.field_at(index, i as u32);
|
||||
val at := this.within(place, each.offset, each.type_id);
|
||||
this.default_of(&at, each.decl, each.spread);
|
||||
}
|
||||
}
|
||||
|
||||
#! Gives a field of a zeroed struct its default: the value it is declared with,
|
||||
#! made where the struct is declared, or the defaults of a struct it holds.
|
||||
#! @param this The emitter.
|
||||
#! @param place The field's place, zeroed.
|
||||
#! @param field The field's declaration.
|
||||
#! @param spread Whether it spreads another struct's fields.
|
||||
def emitter::default_of(&mut this, place: &location, field: u32, spread: bool): void {
|
||||
val value := spread ? 0 : this.c.checker.field_default(field);
|
||||
if value == 0 {
|
||||
val inner := this.c.shape_of(place.kind);
|
||||
if inner.kind == lk::record {
|
||||
this.defaults(place);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
val file: u32 = this.c.checker.decls.files.p[field];
|
||||
val saved := this.c.checker.at;
|
||||
this.c.checker.use_table(this.c.checker.units[file].table);
|
||||
val made := this.value_as(value, place.kind);
|
||||
this.c.checker.at = saved;
|
||||
this.adopt(&made);
|
||||
this.store(place, &made);
|
||||
}
|
||||
|
||||
#! An array literal: a fixed array holding its elements, or, where a view is
|
||||
#! wanted, a view of such an array, which lives as long as the function does,
|
||||
#! since the view may outlive the statement.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `array` node.
|
||||
#! @param kind The type it is used as.
|
||||
#! @returns The value.
|
||||
pub def emitter::array_literal(&mut this, node: u32, kind: u32): operand {
|
||||
val run := this.c.checker.left_of(node);
|
||||
val count := this.c.checker.run_length(run);
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
val viewed := this.c.viewed(plain);
|
||||
val element := viewed != 0 ? viewed : this.c.checker.types.first_of(plain);
|
||||
val inner := this.c.shape_of(element);
|
||||
|
||||
if viewed != 0 {
|
||||
val view := this.temporary_of(kind);
|
||||
if count == 0 {
|
||||
this.build.clear(view, 2 * word_size);
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
|
||||
# A read-only view of what is known while compiling views data the program
|
||||
# holds, made once.
|
||||
if this.c.checker.is_std(plain, word::span) {
|
||||
val held := this.static_datum(node, element);
|
||||
if held != no_block {
|
||||
val data := this.build.value(op::datum, span::<u32>::empty(), vt::word, held, 0);
|
||||
this.build.store(access::i64, view, 0, data);
|
||||
this.build.store(access::i64, view, word_size, this.build.word(count as u64));
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
}
|
||||
val array := this.c.checker.types.fixed(element, count);
|
||||
val held := this.c.shape_of(array);
|
||||
val cell := this.build.code.add_cell(round_up(held.size, word_size) as u32,
|
||||
held.align < word_size ? word_size as u32 : held.align as u32, ck::local, 0);
|
||||
val first := this.build.cell_address(cell);
|
||||
this.fill_array(run, element, inner.size, first);
|
||||
this.build.store(access::i64, view, 0, first);
|
||||
this.build.store(access::i64, view, word_size, this.build.word(count as u64));
|
||||
# What the array holds closes where the statement ends, since a view owns
|
||||
# nothing.
|
||||
var whole := operand::at(array, first);
|
||||
this.keep(&whole);
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
|
||||
val address := this.temporary_of(kind);
|
||||
this.fill_array(run, element, inner.size, address);
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
|
||||
#! The datum holding the elements of an array literal, where each is known
|
||||
#! while compiling: a number, text, or an array literal viewed of the same.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `array` node.
|
||||
#! @param element The elements' type.
|
||||
#! @returns The datum, or `no_block` where an element is not known.
|
||||
def emitter::static_datum(&mut this, node: u32, element: u32): u32 {
|
||||
val run := this.c.checker.left_of(node);
|
||||
val count := this.c.checker.run_length(run);
|
||||
val inner := this.c.shape_of(element);
|
||||
var bytes := vector::<u8>::new();
|
||||
bytes.resize(count as u64 * inner.size + 1);
|
||||
var moved := vector::<relocation>::new();
|
||||
for i in 0..count {
|
||||
val each := this.c.checker.run_at(run, i as u32);
|
||||
return no_block if !this.write_static(each, element, &mut bytes, i as u64 * inner.size, &mut moved);
|
||||
}
|
||||
val index := this.c.program.data.length as u32;
|
||||
this.c.program.data.append(datum {
|
||||
bytes = move(&mut bytes),
|
||||
align = inner.align < word_size ? word_size : inner.align,
|
||||
relocations = move(&mut moved)
|
||||
});
|
||||
return index;
|
||||
}
|
||||
|
||||
#! Writes a value known while compiling into the bytes of a datum, laid out as
|
||||
#! its type is.
|
||||
#! @param this The emitter.
|
||||
#! @param node The value's node.
|
||||
#! @param kind Its type.
|
||||
#! @param bytes The datum's bytes.
|
||||
#! @param at Where it is written.
|
||||
#! @param moved Where the addresses it holds are noted.
|
||||
#! @returns Whether it is known.
|
||||
def emitter::write_static(&mut this, node: u32, kind: u32, bytes: &mut vector<u8>, at: u64, moved: &mut vector<relocation>): bool {
|
||||
val shape := this.c.shape_of(kind);
|
||||
if this.c.is_scalar_type(kind) && this.c.checker.is_known(node) {
|
||||
val value := this.c.checker.value_at(node);
|
||||
var word: u64 = 0;
|
||||
if shape.kind == lk::floating {
|
||||
val real := value.as_real();
|
||||
word = shape.bits <= 32 ? bits_of_single(real as f32) as u64 : bits_of_real(real);
|
||||
} else {
|
||||
word = value.floating ? (value.real as i64) as u64 : value.bits();
|
||||
}
|
||||
write_number(bytes, at, shape.size, word);
|
||||
return true;
|
||||
}
|
||||
|
||||
val viewed := this.c.viewed(kind);
|
||||
return false if viewed == 0 || !this.c.checker.is_std(this.c.checker.unalias(kind), word::span);
|
||||
var target: u32 = no_block;
|
||||
var length: u64 = 0;
|
||||
if this.c.checker.kind_of(node) == nk::string {
|
||||
var text := string::new(nothing {});
|
||||
this.decode_literal(this.c.checker.text_of(node), &mut text);
|
||||
target = this.c.text_datum(text.view());
|
||||
length = text.length;
|
||||
} else if this.c.checker.kind_of(node) == nk::array {
|
||||
length = this.c.checker.run_length(this.c.checker.left_of(node)) as u64;
|
||||
target = length == 0 ? no_block : this.static_datum(node, viewed);
|
||||
return false if length != 0 && target == no_block;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if length != 0 {
|
||||
moved.append(relocation { at = at, target = target, addend = 0 });
|
||||
}
|
||||
write_number(bytes, at + word_size, word_size, length);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The bits of an `f32`.
|
||||
#! @param value The value.
|
||||
#! @returns Its bits.
|
||||
def bits_of_single(value: f32): u32 {
|
||||
var held := value;
|
||||
return ((&mut held) as *mut u32)[0];
|
||||
}
|
||||
|
||||
#! Writes the elements of an array literal one after another from an address.
|
||||
#! @param this The emitter.
|
||||
#! @param run The run of the elements' nodes.
|
||||
#! @param element Their type.
|
||||
#! @param size The size of each.
|
||||
#! @param first Where the first goes.
|
||||
def emitter::fill_array(&mut this, run: u32, element: u32, size: u64, first: u32): void {
|
||||
for i in 0..this.c.checker.run_length(run) {
|
||||
val value := this.value_as(this.c.checker.run_at(run, i as u32), element);
|
||||
this.adopt(&value);
|
||||
val at := location { kind = element, address = first, offset = i as u64 * size, framed = true };
|
||||
this.store(&at, &value);
|
||||
}
|
||||
}
|
||||
|
||||
#! `x.name`: a field, read where it is, or a property, read through its `get`.
|
||||
#! The length of a fixed array is known while compiling, which its value gives.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `member` node.
|
||||
#! @returns The value.
|
||||
pub def emitter::member_value(&mut this, node: u32): operand {
|
||||
val property := this.property_of(node);
|
||||
if property != 0 {
|
||||
val object := this.c.checker.left_of(node);
|
||||
val receiver := this.receiver_of(object, false);
|
||||
return this.property_get(property, &receiver, node);
|
||||
}
|
||||
val place := this.place_of(node);
|
||||
return this.load(&place);
|
||||
}
|
||||
|
||||
#! The property a member names, or 0.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @returns The property's declaration, or 0.
|
||||
pub def emitter::property_of(&mut this, node: u32): u32 {
|
||||
val kind := this.c.checker.kind_of(node);
|
||||
return 0 if kind != nk::member && kind != nk::member_of;
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0;
|
||||
# `x.(d)` may name the accessor of the property.
|
||||
if this.c.checker.decls.kinds.p[ref] == dk::function && this.c.checker.decls.has(ref, df::accessor) {
|
||||
return this.c.checker.decls.owners.p[ref];
|
||||
}
|
||||
return this.c.checker.decls.kinds.p[ref] == dk::property ? ref : 0;
|
||||
}
|
||||
|
||||
#! The reference a method or a property's accessors take to the value they are
|
||||
#! of: the reference the value already is, read through to the last, or one to
|
||||
#! the place it is, or to a copy of it where it is no place.
|
||||
#! @param this The emitter.
|
||||
#! @param object The node of the value.
|
||||
#! @param writes Whether it is written.
|
||||
#! @returns The reference.
|
||||
pub def emitter::receiver_of(&mut this, object: u32, writes: bool): operand {
|
||||
val kind := this.c.checker.type_at(object);
|
||||
return this.through_one(object) if this.c.is_reference_type(kind);
|
||||
return this.reference_arg(object, this.c.checker.types.reference(kind, writes));
|
||||
}
|
||||
|
||||
#! The accessor of a property: its `set`, or its `get`.
|
||||
#! @param this The emitter.
|
||||
#! @param property The property's declaration.
|
||||
#! @param setter Whether the `set` is wanted.
|
||||
#! @returns The accessor's declaration, or 0.
|
||||
pub def emitter::accessor_of(&mut this, property: u32, setter: bool): u32 {
|
||||
val run: u32 = this.c.checker.decls.children.p[property];
|
||||
for i in 0..this.c.checker.decls.list_length(run) {
|
||||
val each := this.c.checker.decls.list_at(run, i as u32);
|
||||
return each if this.c.checker.decls.has(each, df::setter) == setter;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Reads a property through its `get`.
|
||||
#! @param this The emitter.
|
||||
#! @param property The property's declaration.
|
||||
#! @param receiver The reference to the value it is of.
|
||||
#! @param node The node reading it.
|
||||
#! @returns What its `get` gives.
|
||||
pub def emitter::property_get(&mut this, property: u32, receiver: &operand, node: u32): operand {
|
||||
val getter := this.accessor_of(property, false);
|
||||
if getter == 0 {
|
||||
this.problem(node, "The property has no `get`.");
|
||||
return this.zero_value(this.c.checker.type_at(node));
|
||||
}
|
||||
val owner := this.c.checker.types.first_of(this.c.checker.unalias(receiver.kind));
|
||||
val made := this.method_call(getter, owner, receiver, span::<operand>::empty());
|
||||
this.keep(&made);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Writes a property through its `set`.
|
||||
#! @param this The emitter.
|
||||
#! @param property The property's declaration.
|
||||
#! @param receiver The reference to the value it is of.
|
||||
#! @param value The value written.
|
||||
#! @param node The node writing it.
|
||||
pub def emitter::property_set(&mut this, property: u32, receiver: &operand, value: &operand, node: u32): void {
|
||||
val setter := this.accessor_of(property, true);
|
||||
if setter == 0 {
|
||||
this.problem(node, "The property has no `set`.");
|
||||
return;
|
||||
}
|
||||
val owner := this.c.checker.types.first_of(this.c.checker.unalias(receiver.kind));
|
||||
var others: operand[1] = [value];
|
||||
this.method_call(setter, owner, receiver, others[0:1]);
|
||||
}
|
||||
|
||||
#! `x[start:count]`: a view of the elements of a fixed array, of what a pointer
|
||||
#! points at, or of another view, checked against how many there are where that
|
||||
#! is known. Either bound may be left out but a pointer's count.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `slice` node.
|
||||
#! @returns The view.
|
||||
pub def emitter::slice(&mut this, node: u32): operand {
|
||||
val object := this.c.checker.left_of(node);
|
||||
val start := this.c.checker.record(node, slice_slot::start);
|
||||
val count := this.c.checker.record(node, slice_slot::count);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
val through := this.c.checker.unalias(this.c.checker.reached(this.c.checker.type_at(object)));
|
||||
var data: u32 = 0;
|
||||
var held: u32 = 0;
|
||||
var element: u32 = 0;
|
||||
|
||||
match this.c.checker.types.kind_of(through) {
|
||||
ty::fixed => {
|
||||
val whole := this.object(object);
|
||||
val shape := this.c.shape_of(through);
|
||||
data = this.address_of(&whole);
|
||||
held = this.build.word(shape.count as u64);
|
||||
element = this.c.checker.types.first_of(through);
|
||||
}
|
||||
ty::pointer => {
|
||||
val pointer := this.value_of_type(object);
|
||||
data = this.through_references(&pointer).words[0];
|
||||
element = this.c.checker.types.first_of(through);
|
||||
}
|
||||
else => {
|
||||
val whole := this.object(object);
|
||||
data = this.build.load(access::i64, whole.address, whole.offset);
|
||||
held = this.build.load(access::i64, whole.address, whole.offset + word_size);
|
||||
element = this.c.viewed(through);
|
||||
}
|
||||
}
|
||||
|
||||
val size := this.c.shape_of(element).size;
|
||||
var from := this.build.word(0);
|
||||
if start != 0 {
|
||||
from = this.value_as(start, bt::uint64).words[0];
|
||||
if held != 0 && this.checking() {
|
||||
this.build.check(this.build.binary(op::leu, from, held), fault::out_of_bounds as u32);
|
||||
}
|
||||
}
|
||||
var many: u32 = 0;
|
||||
if count != 0 {
|
||||
many = this.value_as(count, bt::uint64).words[0];
|
||||
if held != 0 && this.checking() {
|
||||
val rest := this.build.binary(op::sub, held, from);
|
||||
this.build.check(this.build.binary(op::leu, many, rest), fault::out_of_bounds as u32);
|
||||
}
|
||||
} else {
|
||||
many = held != 0 ? this.build.binary(op::sub, held, from) : this.build.word(0);
|
||||
}
|
||||
|
||||
val first := this.build.binary(op::add, data, this.scaled(from, size));
|
||||
val view := this.temporary_of(kind);
|
||||
this.build.store(access::i64, view, 0, first);
|
||||
this.build.store(access::i64, view, word_size, many);
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
#! @file formatting.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lowering of text a program writes while it runs: interpolated
|
||||
#! strings, whose holes are written as the values they hold, and the text of
|
||||
#! literals, decoded from how they are written.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! An interpolated string: its text and the values of its holes, written in
|
||||
#! order into text a `string` then owns. A view of bytes, and a string, are
|
||||
#! written as the text they hold.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `istring` node.
|
||||
#! @returns The string.
|
||||
pub def emitter::interpolated(&mut this, node: u32): operand {
|
||||
this.build.effect(op::format, span::<u32>::empty(), 0, 0);
|
||||
val run := this.c.checker.left_of(node);
|
||||
val count := this.c.checker.run_length(run);
|
||||
|
||||
# The lines of a block are written without the indentation its closing
|
||||
# delimiter stands at, as a block of plain text is.
|
||||
var indent: u64 = 0;
|
||||
var block := false;
|
||||
if count != 0 && this.c.checker.kind_of(this.c.checker.run_at(run, 0)) == nk::string {
|
||||
val opened := this.c.checker.text_of(this.c.checker.run_at(run, 0));
|
||||
block = opened.length >= 4 && opened.substr(0, 4) == "$\"\"\"";
|
||||
}
|
||||
if block && this.c.checker.kind_of(this.c.checker.run_at(run, count - 1)) == nk::string {
|
||||
val closed := this.c.checker.text_of(this.c.checker.run_at(run, count - 1));
|
||||
var line := closed.length;
|
||||
while line > 0 && closed[line - 1] != '\n' {
|
||||
line--;
|
||||
}
|
||||
if line != 0 && closed.length >= 3 {
|
||||
indent = closed.length - 3 - line;
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..count {
|
||||
val piece := this.c.checker.run_at(run, i as u32);
|
||||
if this.c.checker.kind_of(piece) == nk::string {
|
||||
var text := string::new(nothing {});
|
||||
if block {
|
||||
this.block_piece(piece, i == 0, indent, &mut text);
|
||||
} else {
|
||||
this.piece_text(piece, &mut text);
|
||||
}
|
||||
if text.length != 0 {
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(text.view()), 0);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
val mark := this.cell_mark();
|
||||
val value := this.value(piece);
|
||||
this.format_value(&value);
|
||||
this.release(mark);
|
||||
}
|
||||
|
||||
# Text written where a view is wanted is made as a string all the same, which
|
||||
# the statement closes, so the view is only good while it runs.
|
||||
val made := this.c.checker.type_at(node);
|
||||
val text := this.owned_text(made);
|
||||
val wanted := this.c.checker.converted(node);
|
||||
return text if wanted == made;
|
||||
return this.convert(&text, wanted);
|
||||
}
|
||||
|
||||
#! Writes a value into the text being made: the bytes a view of them or a
|
||||
#! string holds, or the value as its layout says it is written.
|
||||
#! @param this The emitter.
|
||||
#! @param value The value.
|
||||
pub def emitter::format_value(&mut this, value: &operand): void {
|
||||
val kind := this.c.checker.unalias(value.kind);
|
||||
val viewed := this.c.is_reference_type(kind) ? 0 : this.c.viewed(kind);
|
||||
if viewed != 0 {
|
||||
val element := this.c.shape_of(viewed);
|
||||
if element.size == 1 && element.kind != lk::record {
|
||||
var args: u32[2] = [this.build.load(access::i64, value.address, 0), this.build.load(access::i64, value.address, word_size)];
|
||||
this.build.effect(op::format_bytes, args[0:2], 0, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if !this.c.is_reference_type(kind) && this.c.is_string(kind) {
|
||||
val index := this.c.layout_of(kind);
|
||||
val data := this.c.field_at(index, 1);
|
||||
val count := this.c.field_at(index, 2);
|
||||
var args: u32[2] = [this.build.load(access::i64, value.address, data.offset), this.build.load(access::i64, value.address, count.offset)];
|
||||
this.build.effect(op::format_bytes, args[0:2], 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
var args := vector::<u32>::new();
|
||||
value.args(&mut args);
|
||||
this.build.effect(op::format_value, args.view(), this.c.layout_of(kind), 0);
|
||||
}
|
||||
|
||||
#! The text of a piece of an interpolated block, decoded: each of its lines
|
||||
#! without the spaces trailing it and followed by a line break, and each line
|
||||
#! after one without the indentation the block's closing delimiter stands at.
|
||||
#! The first piece begins below the line the block opens.
|
||||
#! @param this The emitter.
|
||||
#! @param piece The `string` node of the piece.
|
||||
#! @param first Whether it is the first piece.
|
||||
#! @param indent How far the block's lines are indented.
|
||||
#! @param out Where the bytes are written.
|
||||
def emitter::block_piece(&mut this, piece: u32, first: bool, indent: u64, out: &mut string): void {
|
||||
val written := this.c.checker.text_of(piece);
|
||||
var from := 0u;
|
||||
var to := written.length;
|
||||
if written.length >= 4 && written.substr(0, 4) == "$\"\"\"" {
|
||||
from = 4;
|
||||
} else if written.length >= 1 && written[0] == '}' {
|
||||
from = 1;
|
||||
}
|
||||
if to >= from + 3 && written.substr(to - 3, 3) == "\"\"\"" {
|
||||
to -= 3;
|
||||
} else if to > from && written[to - 1] == '{' {
|
||||
to -= 1;
|
||||
}
|
||||
|
||||
var at := from;
|
||||
if first {
|
||||
while at < to && written[at] != '\n' {
|
||||
at++;
|
||||
}
|
||||
return if at >= to;
|
||||
at = past_indent(written, at + 1, to, indent);
|
||||
}
|
||||
var start := at;
|
||||
while at < to {
|
||||
if written[at] != '\n' {
|
||||
at++;
|
||||
continue;
|
||||
}
|
||||
var stop := at;
|
||||
while stop > start && (written[stop - 1] == ' ' || written[stop - 1] == '\t') {
|
||||
stop--;
|
||||
}
|
||||
this.c.checker.decode_text(written.substr(start, stop - start), out);
|
||||
out.push('\n');
|
||||
at = past_indent(written, at + 1, to, indent);
|
||||
start = at;
|
||||
}
|
||||
this.c.checker.decode_text(written.substr(start, to - start), out);
|
||||
}
|
||||
|
||||
#! Where a line begins past the indentation of a block.
|
||||
#! @param text The text.
|
||||
#! @param at Where the line begins.
|
||||
#! @param end Where the text ends.
|
||||
#! @param indent How far the block's lines are indented.
|
||||
#! @returns Where the line begins past it.
|
||||
def past_indent(text: strview, at: u64, end: u64, indent: u64): u64 {
|
||||
var made := at;
|
||||
var skipped: u64 = 0;
|
||||
while skipped < indent && made < end && (text[made] == ' ' || text[made] == '\t') {
|
||||
made++;
|
||||
skipped++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The text of a piece of an interpolated string, without what opens and
|
||||
#! closes it, decoded.
|
||||
#! @param this The emitter.
|
||||
#! @param piece The `string` node of the piece.
|
||||
#! @param out Where the bytes are written.
|
||||
def emitter::piece_text(&mut this, piece: u32, out: &mut string): void {
|
||||
val written := this.c.checker.text_of(piece);
|
||||
var from := 0u;
|
||||
var to := written.length;
|
||||
if written.length >= 4 && written.substr(0, 4) == "$\"\"\"" {
|
||||
from = 4;
|
||||
} else if written.length >= 2 && written.substr(0, 2) == "$\"" {
|
||||
from = 2;
|
||||
} else if written.length >= 1 && written[0] == '}' {
|
||||
from = 1;
|
||||
}
|
||||
if to >= from + 3 && written.substr(to - 3, 3) == "\"\"\"" {
|
||||
to -= 3;
|
||||
} else if to > from && (written[to - 1] == '"' || written[to - 1] == '{') {
|
||||
to -= 1;
|
||||
}
|
||||
this.c.checker.decode_text(written.substr(from, to - from), out);
|
||||
}
|
||||
|
||||
#! Decodes the text a literal is written as: between its quotes, with its
|
||||
#! escapes, or of text spanning lines, each of its lines without the
|
||||
#! indentation the closing quotes stand at, and each followed by a line break.
|
||||
#! @param this The emitter.
|
||||
#! @param written The literal, with its quotes.
|
||||
#! @param out Where the bytes are written.
|
||||
pub def emitter::decode_literal(&mut this, written: strview, out: &mut string): void {
|
||||
if written.length < 6 || written.substr(0, 3) != "\"\"\"" {
|
||||
this.c.checker.decode_text(written.substr(1, written.length < 2 ? 0 : written.length - 2), out);
|
||||
return;
|
||||
}
|
||||
|
||||
# The text begins below the line the block opens, and the closing quotes' own
|
||||
# line says how far every line of it is indented.
|
||||
val inner := written.substr(3, written.length - 6);
|
||||
var first := 0u;
|
||||
while first < inner.length && inner[first] != '\n' {
|
||||
first++;
|
||||
}
|
||||
var last := inner.length;
|
||||
while last > 0 && inner[last - 1] != '\n' {
|
||||
last--;
|
||||
}
|
||||
return if first >= inner.length || last <= first;
|
||||
|
||||
val indent := inner.length - last;
|
||||
var line := first + 1;
|
||||
while line < last {
|
||||
var stop := line;
|
||||
while stop < last && inner[stop] != '\n' {
|
||||
stop++;
|
||||
}
|
||||
var at := line;
|
||||
var skipped := 0u;
|
||||
while skipped < indent && at < stop && (inner[at] == ' ' || inner[at] == '\t') {
|
||||
at++;
|
||||
skipped++;
|
||||
}
|
||||
var text := stop;
|
||||
while text > at && (inner[text - 1] == ' ' || inner[text - 1] == '\t') {
|
||||
text--;
|
||||
}
|
||||
this.c.checker.decode_text(inner.substr(at, text - at), out);
|
||||
out.push('\n');
|
||||
line = stop + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,546 @@
|
||||
#! @file intrinsics.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the intrinsics, the functions std declares `@intrinsic` that the
|
||||
#! compiler gives: an operation of their own, or a value worked out while
|
||||
#! compiling, rather than a call.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Lowers a call of an intrinsic.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @param decl The intrinsic's declaration.
|
||||
#! @returns What it gives.
|
||||
pub def emitter::intrinsic(&mut this, node: u32, decl: u32): operand {
|
||||
val name := this.c.checker.names.text(this.c.checker.decls.names.p[decl]);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
val run := this.c.checker.right_of(node);
|
||||
|
||||
# A type's layout is known while compiling.
|
||||
if name == "size_of" || name == "align_of" {
|
||||
val instance := this.called(node);
|
||||
var size: u64 = 0;
|
||||
if instance != 0 {
|
||||
val args: u32 = this.c.checker.instances[instance - 1].args;
|
||||
val held := this.c.shape_of(this.c.checker.types.part_at(args, 0));
|
||||
size = name == "size_of" ? held.size : held.align;
|
||||
}
|
||||
return operand::one(kind, this.build.word(size));
|
||||
}
|
||||
if name == "os" || name == "arch" || name == "backend" {
|
||||
val value := name == "os" ? this.c.checker.target.os : name == "arch" ? this.c.checker.target.arch : this.c.checker.target.backend;
|
||||
return this.text_value(this.c.checker.names.text(value), kind);
|
||||
}
|
||||
if name == "call_site" {
|
||||
return this.call_site(node, kind);
|
||||
}
|
||||
if name == "contracts" || name == "checking" {
|
||||
val off := name == "contracts" ? this.c.checker.contracts_off : this.c.checker.checks_off;
|
||||
return operand::one(kind, this.build.word(off ? 0 : 1));
|
||||
}
|
||||
# These read the argument as it is written: in a default, the argument the
|
||||
# call wrote for the parameter it names.
|
||||
if name == "source_of" || name == "values_of" {
|
||||
val arg := this.c.checker.run_length(run) != 0 ? this.c.checker.run_at(run, 0) : 0;
|
||||
val written := arg != 0 ? this.written_argument(arg) : 0;
|
||||
val saved := this.c.checker.at;
|
||||
if written != 0 {
|
||||
this.c.checker.use_table(this.defaulting.table);
|
||||
}
|
||||
val chosen := written != 0 ? written : arg;
|
||||
var made := operand {};
|
||||
if name == "source_of" {
|
||||
var text := string::new(nothing {});
|
||||
if chosen != 0 {
|
||||
this.source_text(chosen, &mut text);
|
||||
}
|
||||
this.c.checker.at = saved;
|
||||
made = this.text_value(text.view(), kind);
|
||||
} else {
|
||||
val outer := this.defaulting;
|
||||
this.defaulting = default_site {};
|
||||
made = this.values_of(chosen, kind);
|
||||
this.defaulting = outer;
|
||||
this.c.checker.at = saved;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
if name == "name_of" || name == "path_of" {
|
||||
# The checker keeps the type named as the callee's.
|
||||
val named := this.c.checker.type_at(this.c.checker.left_of(node));
|
||||
var text := string::new(nothing {});
|
||||
if named != 0 && named != bt::error {
|
||||
text = name == "path_of" ? this.c.checker.describe_path(named) : this.c.checker.describe(named);
|
||||
}
|
||||
return this.text_value(text.view(), kind);
|
||||
}
|
||||
if name == "value_of" {
|
||||
if this.c.checker.is_known(node) && this.c.is_scalar_type(kind) {
|
||||
return this.known(&this.c.checker.value_at(node), kind);
|
||||
}
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::enum_member {
|
||||
var value := constant {};
|
||||
this.c.checker.decl_value(ref, &mut value);
|
||||
return this.known(&value, kind);
|
||||
}
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::variable {
|
||||
val place := this.global_place(ref);
|
||||
return this.load(&place);
|
||||
}
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::function {
|
||||
return this.function_value(node, ref);
|
||||
}
|
||||
# An attribute is the value it writes, made where it is read.
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::attribute {
|
||||
val saved := this.c.checker.at;
|
||||
this.c.checker.use_table(this.c.checker.units[this.c.checker.decls.files.p[ref]].table);
|
||||
val made := this.construct_of(this.c.checker.decls.types.p[ref], this.c.checker.right_of(this.c.checker.decls.nodes.p[ref]));
|
||||
this.c.checker.at = saved;
|
||||
return made;
|
||||
}
|
||||
this.problem(node, "`value_of` reaches nothing lowering knows.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
if name == "attached" {
|
||||
return this.attached(node, kind);
|
||||
}
|
||||
if name == "compile_error" {
|
||||
this.build.unreachable();
|
||||
return operand::none(kind);
|
||||
}
|
||||
if name == "lanes_have" || name == "lanes_apply" {
|
||||
return this.lanes(node, name == "lanes_have", kind);
|
||||
}
|
||||
# Vectors are values held in registers between operations in an executable
|
||||
# optimized with inlining and promotion, and nowhere else.
|
||||
if name == "lanes_held" {
|
||||
val native := !this.c.options.evaluating && this.c.checker.names.text(this.c.checker.target.backend) == "native";
|
||||
return operand::one(kind, this.build.word(native && this.c.options.level >= 2 ? 1 : 0));
|
||||
}
|
||||
|
||||
if name.starts_with("atomic_") {
|
||||
return this.atomic(node, name, kind);
|
||||
}
|
||||
|
||||
# `fault` takes its text's two words, wherever the view is.
|
||||
if name == "fault" {
|
||||
val text := this.value(this.c.checker.run_at(run, 0));
|
||||
var words: u32[2] = [0, 0];
|
||||
if text.address != 0 {
|
||||
words[0] = this.build.load(access::i64, text.address, 0);
|
||||
words[1] = this.build.load(access::i64, text.address, word_size);
|
||||
} else {
|
||||
words[0] = text.words[0];
|
||||
words[1] = text.words[1];
|
||||
}
|
||||
this.build.effect(op::fault, words[0:2], 0, 0);
|
||||
return operand::none(kind);
|
||||
}
|
||||
|
||||
# The rest are operations, given their arguments' values.
|
||||
var args := vector::<u32>::new();
|
||||
for i in 0..this.c.checker.run_length(run) {
|
||||
val made := this.value(this.c.checker.run_at(run, i as u32));
|
||||
made.args(&mut args);
|
||||
}
|
||||
# A member of an enum's place among the values of its members, and the member
|
||||
# at a place, by which a range of its members counts.
|
||||
if name == "rank_of" || name == "member_at" {
|
||||
val instance := this.called(node);
|
||||
val members := instance != 0 ? this.c.checker.types.part_at(this.c.checker.instances[instance - 1].args, 0) : bt::error;
|
||||
if members == bt::error || this.c.checker.named_kind(this.c.checker.unalias(members)) != dk::enum_type {
|
||||
this.problem(node, "`rank_of` and `member_at` count the members of an enum.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
val made := name == "rank_of" ? this.member_rank(args[0], members) : this.member_of_rank(args[0], members);
|
||||
return operand::one(kind, made);
|
||||
}
|
||||
if name == "raw_allocate" || name == "raw_allocate_zeroed" {
|
||||
val uncleared: u32 = name == "raw_allocate" ? 1 : 0;
|
||||
return operand::one(kind, this.build.value(op::allocate, args.view(), vt::word, uncleared, 0));
|
||||
}
|
||||
if name == "raw_release" {
|
||||
this.build.effect(op::release, args.view(), 0, 0);
|
||||
return operand::none(kind);
|
||||
}
|
||||
# `core::close` closes a value where it is, as its scope ending would: its own
|
||||
# `close`, then what it holds that closes.
|
||||
if name == "close_where_it_is" {
|
||||
val instance := this.called(node);
|
||||
if instance != 0 && args.length > 0 {
|
||||
val closed := this.c.checker.types.part_at(this.c.checker.instances[instance - 1].args, 0);
|
||||
val place := location { kind = closed, address = args[0] };
|
||||
this.close(&place);
|
||||
}
|
||||
return operand::none(kind);
|
||||
}
|
||||
if name == "raw_resize" {
|
||||
return operand::one(kind, this.build.value(op::resize, args.view(), vt::word, 0, 0));
|
||||
}
|
||||
if name == "syscall" {
|
||||
return operand::one(kind, this.build.value(op::syscall, args.view(), vt::word, 0, 0));
|
||||
}
|
||||
if name == "frame" || name == "executable" || name == "environ" || name == "take_signals" || name == "current" {
|
||||
val chosen := name == "frame" ? op::frame : name == "executable" ? op::image : name == "environ" ? op::environ
|
||||
: name == "current" ? op::current : op::signals;
|
||||
return operand::one(kind, this.build.value(chosen, span::<u32>::empty(), vt::word, 0, 0));
|
||||
}
|
||||
# A thread runs the function value, of two words, given the context, a word;
|
||||
# the runtime starts one of the system on a record's stacks.
|
||||
if name == "start" || name == "clone_thread" {
|
||||
return operand::one(kind, this.build.value(name == "start" ? op::start : op::clone, args.view(), vt::word, 0, 0));
|
||||
}
|
||||
if name == "catch_signal" {
|
||||
return operand::one(kind, this.build.value(op::catch, args.view(), vt::word, 0, 0));
|
||||
}
|
||||
if name == "exit" {
|
||||
this.build.effect(op::exit, args.view(), 0, 0);
|
||||
return operand::none(kind);
|
||||
}
|
||||
if name == "abort" {
|
||||
this.build.effect(op::abort, span::<u32>::empty(), 0, 0);
|
||||
return operand::none(kind);
|
||||
}
|
||||
if name == "trap" {
|
||||
this.build.effect(op::trap, args.view(), 0, 0);
|
||||
return operand::none(kind);
|
||||
}
|
||||
# Operations of the machine on a whole real or word, which `std::math` narrows
|
||||
# for the other types.
|
||||
if name == "square_root" {
|
||||
return operand::one(kind, this.build.unary(op::fsqrt, args[0]));
|
||||
}
|
||||
if name == "ones_in" || name == "zeros_leading" || name == "zeros_trailing" || name == "bytes_reversed" {
|
||||
val chosen := name == "ones_in" ? op::ones : name == "zeros_leading" ? op::clz : name == "zeros_trailing" ? op::ctz : op::bswap;
|
||||
return operand::one(kind, this.build.unary(chosen, args[0]));
|
||||
}
|
||||
this.problem(node, "The intrinsic is not lowered.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
|
||||
#! `lanes_have` and `lanes_apply`, of an operation on lanes: in an executable,
|
||||
#! whether the processor it runs on has the vector instructions working it, as
|
||||
#! it found when it started, and the lanes worked with them. The virtual machine
|
||||
#! and constants have none, so never reach the second.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @param have Whether it is `lanes_have`.
|
||||
#! @param kind What it gives.
|
||||
#! @returns What it gives.
|
||||
def emitter::lanes(&mut this, node: u32, have: bool, kind: u32): operand {
|
||||
# Its type arguments are the type of lane, the operation, and how many lanes.
|
||||
var lane := lane_none;
|
||||
var operation: u32 = 0;
|
||||
var count: u64 = 0;
|
||||
val instance := this.called(node);
|
||||
if instance != 0 {
|
||||
val args: u32 = this.c.checker.instances[instance - 1].args;
|
||||
lane = this.lane_of(this.c.checker.types.part_at(args, 0));
|
||||
operation = this.constant_argument(this.c.checker.types.part_at(args, 1)) as u32;
|
||||
count = this.constant_argument(this.c.checker.types.part_at(args, 2));
|
||||
}
|
||||
val native := !this.c.options.evaluating && this.c.checker.names.text(this.c.checker.target.backend) == "native";
|
||||
val need := native && lane != lane_none ? lanes_needs(operation, lane, count * lane_bits(lane) / 8) : cpu_never;
|
||||
|
||||
if have {
|
||||
return operand::one(kind, this.build.word(0)) if need == cpu_never;
|
||||
return operand::one(kind, this.build.word(1)) if need == cpu_sse2;
|
||||
val found := this.build.value(op::cpu, span::<u32>::empty(), vt::word, 0, 0);
|
||||
val held := this.build.binary(op::band, found, this.build.word(need));
|
||||
return operand::one(kind, this.build.binary(op::eq, held, this.build.word(need)));
|
||||
}
|
||||
if need == cpu_never {
|
||||
this.build.unreachable();
|
||||
return operand::none(kind);
|
||||
}
|
||||
# The pointers: where the result goes, then the operands.
|
||||
var pointers := vector::<u32>::new();
|
||||
val run := this.c.checker.right_of(node);
|
||||
for i in 0..this.c.checker.run_length(run) {
|
||||
val made := this.value(this.c.checker.run_at(run, i as u32));
|
||||
made.args(&mut pointers);
|
||||
}
|
||||
|
||||
# The operands are read, worked as values, and the result written, so a
|
||||
# vector a cell holds is a value wherever the cell is promoted.
|
||||
val bytes := count * lane_bits(lane) / 8;
|
||||
var args := vector::<u32>::new();
|
||||
for k in 0..lanes_operands(operation) {
|
||||
val how := lanes_operand_access(operation, lane, bytes, k);
|
||||
args.append(this.build.load(how, pointers[1 + k as u64], 0));
|
||||
}
|
||||
val result := lanes_result_access(operation, lane, bytes);
|
||||
val made := operation == lanes_copy ? args[0]
|
||||
: this.build.value(op::lanes, args.view(), result.value_type(), operation, lane as u64 | bytes << 8);
|
||||
this.build.store(result, pointers[0], 0, made);
|
||||
return operand::none(kind);
|
||||
}
|
||||
|
||||
#! The atomics of `core::atomic`: an operation on the `T` at an address, in one
|
||||
#! step, which is the first type argument, and of `atomic_fetch` the operation
|
||||
#! the second. A compare-exchange gives whether it wrote, and writes what it
|
||||
#! found where a pointer it is given points.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @param name The intrinsic's name.
|
||||
#! @param kind What it gives.
|
||||
#! @returns What it gives.
|
||||
def emitter::atomic(&mut this, node: u32, name: strview, kind: u32): operand {
|
||||
var operation := name == "atomic_load" ? atomic_load : name == "atomic_store" ? atomic_store
|
||||
: name == "atomic_exchange" ? atomic_exchange : name == "atomic_compare_exchange" ? atomic_compare_exchange
|
||||
: name == "atomic_fence" ? atomic_fence : atomic_add;
|
||||
var how := access::i64;
|
||||
var unsigned: u64 = 0;
|
||||
val instance := this.called(node);
|
||||
if instance != 0 && operation != atomic_fence {
|
||||
val args: u32 = this.c.checker.instances[instance - 1].args;
|
||||
val held := this.c.checker.types.part_at(args, 0);
|
||||
how = this.c.shape_of(held).how;
|
||||
if this.c.checker.types.kind_of(this.c.checker.unalias(held)) == ty::unsigned {
|
||||
unsigned = atomic_unsigned;
|
||||
}
|
||||
if name == "atomic_fetch" {
|
||||
operation = this.constant_argument(this.c.checker.types.part_at(args, 1)) as u32;
|
||||
}
|
||||
}
|
||||
|
||||
var values := vector::<u32>::new();
|
||||
val run := this.c.checker.right_of(node);
|
||||
for i in 0..this.c.checker.run_length(run) {
|
||||
val made := this.value(this.c.checker.run_at(run, i as u32));
|
||||
made.args(&mut values);
|
||||
}
|
||||
if operation == atomic_fence {
|
||||
this.build.effect(op::atomic, values.view(), operation, 0);
|
||||
return operand::none(kind);
|
||||
}
|
||||
|
||||
# While compiling, what it reaches is checked as a read through a pointer is.
|
||||
this.reaches(values[0], this.build.word(1), how.width());
|
||||
if operation == atomic_compare_exchange {
|
||||
this.reaches(values[3], this.build.word(1), how.width());
|
||||
var args: u32[5] = [values[0], values[1], values[2], values[4], values[5]];
|
||||
val found := this.build.value(op::atomic, args[0:5], vt::word, operation, how as u64 | unsigned);
|
||||
this.build.store(how, values[3], 0, found);
|
||||
return operand::one(kind, this.build.binary(op::eq, found, values[1]));
|
||||
}
|
||||
if atomic_gives(operation) {
|
||||
return operand::one(kind, this.build.value(op::atomic, values.view(), vt::word, operation, how as u64 | unsigned));
|
||||
}
|
||||
this.build.effect(op::atomic, values.view(), operation, how as u64 | unsigned);
|
||||
return operand::none(kind);
|
||||
}
|
||||
|
||||
#! The type of lane a number is, as vector instructions take it.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The type.
|
||||
#! @returns The type of lane, or `lane_none` where it is no number.
|
||||
def emitter::lane_of(&mut this, kind: u32): u32 {
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
val of := this.c.checker.types.kind_of(plain);
|
||||
val bits := this.c.checker.types.first_of(plain);
|
||||
if of == ty::floating {
|
||||
return bits == 32 ? lane_f32 : lane_f64;
|
||||
}
|
||||
return lane_none if of != ty::signed && of != ty::unsigned;
|
||||
val width: u32 = bits == 8 ? 0 : bits == 16 ? 2 : bits == 32 ? 4 : 6;
|
||||
return width + (of == ty::unsigned ? 1 : 0);
|
||||
}
|
||||
|
||||
#! The number a constant type argument is.
|
||||
#! @param this The emitter.
|
||||
#! @param arg The type argument.
|
||||
#! @returns The number, or 0 where it is none.
|
||||
def emitter::constant_argument(&mut this, arg: u32): u64 {
|
||||
return 0 if this.c.checker.types.kind_of(arg) != ty::value;
|
||||
return this.c.checker.known_at(this.c.checker.types.second_of(arg)).value.magnitude;
|
||||
}
|
||||
|
||||
#! Where a call is written, as `core::site`: its file, line, and column, and the
|
||||
#! signature of the function holding it.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @param kind The `site` type.
|
||||
#! @returns The value.
|
||||
def emitter::call_site(&mut this, node: u32, kind: u32): operand {
|
||||
# In a default, it is where the call leaving the parameter out is.
|
||||
val file := this.defaulting.node != 0 ? this.defaulting.file : this.c.checker.at.file;
|
||||
val site := this.defaulting.node != 0 ? this.defaulting.node : node;
|
||||
val held := &this.c.checker.units[file].source;
|
||||
val offset := this.c.node_offset(file, site);
|
||||
val at := held.lines.place_of(offset);
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
val index := this.c.layout_of(plain);
|
||||
val shape := this.c.layout_at(index);
|
||||
val address := this.temporary_of(kind);
|
||||
this.build.clear(address, shape.size);
|
||||
val whole := location { kind = kind, address = address, framed = true };
|
||||
|
||||
var signature := string::new(nothing {});
|
||||
val decl: u32 = this.c.program.functions[this.function].decl;
|
||||
if decl != 0 {
|
||||
this.c.signature_text(decl, &mut signature);
|
||||
}
|
||||
for i in 0..shape.count {
|
||||
val field := this.c.field_at(index, i as u32);
|
||||
val name := this.c.checker.names.text(field.name);
|
||||
val place := this.within(&whole, field.offset, field.type_id);
|
||||
var value := operand {};
|
||||
if name == "path" {
|
||||
value = this.text_value(held.path, field.type_id);
|
||||
} else if name == "line" {
|
||||
value = operand::one(field.type_id, this.build.word(at.line as u64));
|
||||
} else if name == "column" {
|
||||
value = operand::one(field.type_id, this.build.word(at.column as u64));
|
||||
} else if name == "signature" {
|
||||
value = this.text_value(signature.view(), field.type_id);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
this.store(&place, &value);
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
|
||||
#! The text an expression is written as, without the spaces around it.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @param out Where the text is written.
|
||||
pub def emitter::source_text(&mut this, node: u32, out: &mut string): void {
|
||||
val file := this.c.checker.at.file;
|
||||
val syntax := &this.c.checker.units[file].syntax;
|
||||
val held := &this.c.checker.units[file].source;
|
||||
# A node's own token may come after those of what it was parsed from, as an
|
||||
# operator's comes after its left operand, so the first is the least of all.
|
||||
var first: u32 = syntax.node_tokens[node];
|
||||
var walk := node_walk::new(node);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val token := syntax.node_tokens[at];
|
||||
first = token < first ? token : first;
|
||||
walk.enter(&syntax, at);
|
||||
}
|
||||
val last := syntax.node_end(node);
|
||||
val begin := syntax.token_offsets[first];
|
||||
val end := syntax.token_offsets[last] + syntax.token_length(last, this.c.checker.names);
|
||||
return if end <= begin;
|
||||
out.append(held.make_lexeme(begin, end - begin));
|
||||
}
|
||||
|
||||
#! What the operands of an expression held, as `values_of` gives it: both
|
||||
#! operands of a comparison, or the expression itself, each as `text = value`,
|
||||
#! but for literals, which say what they hold already.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression, or 0.
|
||||
#! @param kind The `string` type.
|
||||
#! @returns The text.
|
||||
def emitter::values_of(&mut this, node: u32, kind: u32): operand {
|
||||
this.build.effect(op::format, span::<u32>::empty(), 0, 0);
|
||||
if node != 0 {
|
||||
this.write_values(node, "");
|
||||
}
|
||||
return this.owned_text(kind);
|
||||
}
|
||||
|
||||
#! Writes what the operands of an expression held into the text being made, as
|
||||
#! `values_of` gives it, after a lead where there are any.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @param lead What comes before the first, as `, with `, or nothing.
|
||||
pub def emitter::write_values(&mut this, node: u32, lead: strview): void {
|
||||
var named := vector::<u32>::new();
|
||||
val shape := this.c.checker.kind_of(node);
|
||||
if shape == nk::binary {
|
||||
val operator := this.c.checker.token_kind(node);
|
||||
if operator.compares() {
|
||||
named.append(this.c.checker.left_of(node));
|
||||
named.append(this.c.checker.right_of(node));
|
||||
}
|
||||
} else if shape != nk::unary {
|
||||
named.append(node);
|
||||
}
|
||||
|
||||
var first := true;
|
||||
for &each in named {
|
||||
val literal := this.c.checker.kind_of(each);
|
||||
continue if literal == nk::number || literal == nk::string || literal == nk::character || literal == nk::boolean
|
||||
|| literal == nk::null_lit;
|
||||
if !first {
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(", "), 0);
|
||||
} else if lead.length != 0 {
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(lead), 0);
|
||||
}
|
||||
first = false;
|
||||
var text := string::new(nothing {});
|
||||
this.source_text(each, &mut text);
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(text.view()), 0);
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(" = "), 0);
|
||||
val mark := this.cell_mark();
|
||||
val value := this.value(each);
|
||||
this.format_value(&value);
|
||||
this.release(mark);
|
||||
}
|
||||
}
|
||||
|
||||
#! `attached::<A>(d)`: a view of the values of the attributes of the type `A`
|
||||
#! written on the declaration `d` describes, in the order they are written, each
|
||||
#! made as the struct literal it is, where the declaration is. They are held as
|
||||
#! long as the function runs, since the view may outlive the statement.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `call` node.
|
||||
#! @param kind The view's type.
|
||||
#! @returns The view.
|
||||
def emitter::attached(&mut this, node: u32, kind: u32): operand {
|
||||
val view := this.temporary_of(kind);
|
||||
this.build.clear(view, 2 * word_size);
|
||||
val run := this.c.checker.right_of(node);
|
||||
val wanted := this.c.viewed(kind);
|
||||
return operand::at(kind, view) if wanted == 0 || this.c.checker.run_length(run) == 0;
|
||||
val one := this.c.checker.reflected_one(this.c.checker.run_at(run, 0));
|
||||
return operand::at(kind, view) if one == checker::no_reflection;
|
||||
val held: desc = this.c.checker.descs[one];
|
||||
return operand::at(kind, view) if held.param != 0;
|
||||
|
||||
# The attributes of the type, in the order they are written.
|
||||
val decl := held.decl;
|
||||
var types := this.c.checker.attribute_types(decl);
|
||||
val saved := this.c.checker.at;
|
||||
this.c.checker.use_table(this.c.checker.units[this.c.checker.decls.files.p[decl]].table);
|
||||
val attrs := this.c.checker.attributes_of(this.c.checker.decls.nodes.p[decl]);
|
||||
var chosen := vector::<u32>::new();
|
||||
var at: u64 = 0;
|
||||
for i in 0..this.c.checker.run_length(attrs) {
|
||||
val attr := this.c.checker.run_at(attrs, i as u32);
|
||||
continue if this.c.checker.kind_of(attr) != nk::attribute;
|
||||
if at < types.length && this.c.checker.strip(types[at], 0) == this.c.checker.strip(wanted, 0) {
|
||||
chosen.append(attr);
|
||||
}
|
||||
at++;
|
||||
}
|
||||
if chosen.length == 0 {
|
||||
this.c.checker.at = saved;
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
|
||||
val shape := this.c.shape_of(wanted);
|
||||
val array := this.c.checker.types.fixed(wanted, chosen.length as u32);
|
||||
val whole := this.c.shape_of(array);
|
||||
val cell := this.build.code.add_cell(round_up(whole.size, word_size) as u32,
|
||||
whole.align < word_size ? word_size as u32 : whole.align as u32, ck::local, 0);
|
||||
val first := this.build.cell_address(cell);
|
||||
for i in 0..chosen.length {
|
||||
val made := this.construct_of(wanted, this.c.checker.right_of(chosen[i]));
|
||||
val place := location { kind = wanted, address = first, offset = i * shape.size, framed = true };
|
||||
this.store(&place, &made);
|
||||
}
|
||||
this.c.checker.at = saved;
|
||||
this.build.store(access::i64, view, 0, first);
|
||||
this.build.store(access::i64, view, word_size, this.build.word(chosen.length));
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
@@ -0,0 +1,410 @@
|
||||
#! @file loops.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lowering of `for`: counting through a range, walking the
|
||||
#! elements of a fixed array or a view, each bound by reference, asking an
|
||||
#! iterator for each element until it gives none, and `for const`, whose body
|
||||
#! the checker checked once for each value of a pack.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Lowers `for`.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `for_stmt` node.
|
||||
pub def emitter::for_statement(&mut this, node: u32): void {
|
||||
val iterated := this.c.checker.record(node, for_slot::iterated);
|
||||
val end := this.c.checker.record(node, for_slot::end);
|
||||
if this.c.checker.node_has(node, nf::constant) {
|
||||
this.unrolled(node);
|
||||
return;
|
||||
}
|
||||
if end != 0 {
|
||||
this.for_range(node);
|
||||
return;
|
||||
}
|
||||
|
||||
# What's walked is of the type it's converted to, as a literal of text is a
|
||||
# `strview`.
|
||||
val through := this.c.checker.unalias(this.c.checker.reached(this.c.checker.converted(iterated)));
|
||||
if this.c.checker.types.kind_of(through) == ty::fixed || this.c.viewed(through) != 0 {
|
||||
this.for_elements(node, through);
|
||||
return;
|
||||
}
|
||||
this.for_iterator(node, through);
|
||||
}
|
||||
|
||||
#! A loop's blocks: where each pass begins, its body, where `continue` goes, and
|
||||
#! where it ends.
|
||||
struct loop_blocks {
|
||||
var head: u32; #! Where each pass asks whether there is another.
|
||||
var body: u32; #! The body.
|
||||
var next: u32; #! Where `continue` goes, which steps to the next pass.
|
||||
var exit: u32; #! Where it ends.
|
||||
}
|
||||
|
||||
#! Makes the blocks of a loop, and goes to its head.
|
||||
#! @param this The emitter.
|
||||
#! @returns The blocks.
|
||||
def emitter::begin_loop(&mut this): loop_blocks {
|
||||
val made := loop_blocks {
|
||||
head = this.build.new_block(),
|
||||
body = this.build.new_block(),
|
||||
next = this.build.new_block(),
|
||||
exit = this.build.new_block()
|
||||
};
|
||||
this.build.jump(made.head);
|
||||
this.build.switch_to(made.head);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Lowers a loop's body, with `break` and `continue` going where the loop says,
|
||||
#! then goes to the step to the next pass.
|
||||
#! @param this The emitter.
|
||||
#! @param blocks The loop's blocks.
|
||||
#! @param body The body's node.
|
||||
#! @param locals How many locals there were before the loop's variables,
|
||||
#! which each pass closes past.
|
||||
def emitter::loop_body(&mut this, blocks: &loop_blocks, body: u32, locals: u64): void {
|
||||
this.safepoint();
|
||||
this.loops.append(loop_info { exit = blocks.exit, next = blocks.next, locals = locals, kept = this.kept.length });
|
||||
this.lower_block(body);
|
||||
this.loops.truncate();
|
||||
this.close_locals(locals, 0);
|
||||
this.build.jump(blocks.next);
|
||||
this.build.seal(blocks.next);
|
||||
this.build.switch_to(blocks.next);
|
||||
}
|
||||
|
||||
#! Goes into a loop's body where there is another pass, and leaves it where not.
|
||||
#! @param this The emitter.
|
||||
#! @param blocks The loop's blocks.
|
||||
#! @param more Whether there is another pass.
|
||||
#! @returns How many locals there were before the pass's own.
|
||||
def emitter::enter_pass(&mut this, blocks: &loop_blocks, more: u32): u64 {
|
||||
this.build.branch(more, blocks.body, blocks.exit);
|
||||
this.build.seal(blocks.body);
|
||||
this.build.switch_to(blocks.body);
|
||||
return this.scope_mark();
|
||||
}
|
||||
|
||||
#! Lowers the body of a counted pass, then counts it and ends the loop.
|
||||
#! @param this The emitter.
|
||||
#! @param blocks The loop's blocks.
|
||||
#! @param node The `for_stmt` node.
|
||||
#! @param locals How many locals there were before the pass's own.
|
||||
#! @param counter The variable counting the passes.
|
||||
def emitter::end_pass(&mut this, blocks: &loop_blocks, node: u32, locals: u64, counter: u32): void {
|
||||
this.loop_body(blocks, this.c.checker.right_of(node), locals);
|
||||
this.forget_locals(locals);
|
||||
this.count_on(counter, blocks.head, blocks.exit);
|
||||
}
|
||||
|
||||
#! Ends a loop: the step jumps back to its head, and what follows goes on from
|
||||
#! its exit.
|
||||
#! @param this The emitter.
|
||||
#! @param blocks The loop's blocks.
|
||||
def emitter::end_loop(&mut this, blocks: &loop_blocks): void {
|
||||
this.close_loop(blocks.head, blocks.exit);
|
||||
}
|
||||
|
||||
#! Ends a loop of the blocks given: where the code is jumps back to its head,
|
||||
#! every way into which is known then, and what follows goes on from its exit.
|
||||
#! @param this The emitter.
|
||||
#! @param head The loop's head.
|
||||
#! @param exit The block after it.
|
||||
def emitter::close_loop(&mut this, head: u32, exit: u32): void {
|
||||
this.build.jump(head);
|
||||
this.build.seal(head);
|
||||
this.build.seal(exit);
|
||||
this.build.switch_to(exit);
|
||||
}
|
||||
|
||||
#! Goes into a loop counting a variable up to a count, as one over the elements
|
||||
#! of a fixed array does: its head goes into its body while the variable is
|
||||
#! below the count, and out of the loop otherwise, and what follows is the body.
|
||||
#! @param this The emitter.
|
||||
#! @param index The variable, which holds where the count begins.
|
||||
#! @param count The count, which the variable stays below.
|
||||
#! @param head The loop's head.
|
||||
#! @param body Its body.
|
||||
#! @param exit The block after it.
|
||||
#! @returns What the variable holds in the body.
|
||||
def emitter::count_up(&mut this, index: u32, count: u64, head: u32, body: u32, exit: u32): u32 {
|
||||
this.build.jump(head);
|
||||
this.build.switch_to(head);
|
||||
val at := this.build.read(index);
|
||||
this.build.branch(this.build.binary(op::ltu, at, this.build.word(count)), body, exit);
|
||||
this.build.seal(body);
|
||||
this.build.switch_to(body);
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Ends the pass of a loop counting a variable up: counts it, and ends the loop.
|
||||
#! @param this The emitter.
|
||||
#! @param index The variable.
|
||||
#! @param head The loop's head.
|
||||
#! @param exit The block after it.
|
||||
def emitter::count_on(&mut this, index: u32, head: u32, exit: u32): void {
|
||||
this.build.assign(index, this.build.binary(op::add, this.build.read(index), this.build.word(1)));
|
||||
this.close_loop(head, exit);
|
||||
}
|
||||
|
||||
#! Declares the variable of a loop's pass, and its index, where it has one.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `for_stmt` node.
|
||||
#! @param value The value the variable is given.
|
||||
#! @param counter The index of the pass, a word.
|
||||
#! @param closes Whether the variable is the loop's own, closed where each
|
||||
#! pass ends.
|
||||
def emitter::bind_pass(&mut this, node: u32, value: &operand, counter: u32, closes: bool): void {
|
||||
val variable := this.c.checker.record(node, for_slot::variable);
|
||||
val index := this.c.checker.record(node, for_slot::index);
|
||||
if variable != 0 {
|
||||
val kind := this.c.checker.type_at(variable);
|
||||
val place := this.new_place(variable, kind);
|
||||
val made := this.convert(value, kind);
|
||||
this.store(&place, &made);
|
||||
this.declare(variable, place, closes);
|
||||
}
|
||||
if index != 0 {
|
||||
val place := this.new_place(index, bt::uint64);
|
||||
val made := operand::one(bt::uint64, counter);
|
||||
this.store(&place, &made);
|
||||
this.declare(index, place, false);
|
||||
}
|
||||
}
|
||||
|
||||
#! `for i in a..b`: counts from the start to just before the end, or through it
|
||||
#! where the loop is `a..=b`, each count a variable of the body's own. A range
|
||||
#! of an enum's members counts their places among the values of its members,
|
||||
#! each pass's variable the member at its place.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `for_stmt` node.
|
||||
def emitter::for_range(&mut this, node: u32): void {
|
||||
val mark := this.begin_statement();
|
||||
val variable := this.c.checker.record(node, for_slot::variable);
|
||||
val kind := variable != 0 ? this.c.checker.type_at(variable) : this.c.checker.converted(this.c.checker.record(node, for_slot::iterated));
|
||||
val start := this.value_as(this.c.checker.record(node, for_slot::iterated), kind);
|
||||
val limit := this.value_as(this.c.checker.record(node, for_slot::end), kind);
|
||||
val members := this.c.checker.named_kind(this.c.checker.unalias(kind)) == dk::enum_type;
|
||||
val first := members ? this.member_rank(start.words[0], kind) : start.words[0];
|
||||
val last := members ? this.member_rank(limit.words[0], kind) : limit.words[0];
|
||||
val count := this.build.variable(vt::word);
|
||||
this.build.assign(count, first);
|
||||
|
||||
# `a..=b` gives `b` too, so whether another pass follows is kept apart from the
|
||||
# count, which may pass the end of its type after `b`.
|
||||
val inclusive := this.c.checker.node_has(node, nf::inclusive);
|
||||
val signed := !members && this.is_signed(kind);
|
||||
var keep: u32 = 0;
|
||||
if inclusive {
|
||||
keep = this.build.variable(vt::word);
|
||||
this.build.assign(keep, this.build.binary(signed ? op::les : op::leu, first, last));
|
||||
}
|
||||
|
||||
val blocks := this.begin_loop();
|
||||
val more := inclusive
|
||||
? this.build.read(keep)
|
||||
: this.build.binary(signed ? op::lts : op::ltu, this.build.read(count), last);
|
||||
val locals := this.enter_pass(&blocks, more);
|
||||
if variable != 0 {
|
||||
val place := this.new_place(variable, kind);
|
||||
val counted := this.build.read(count);
|
||||
val made := operand::one(kind, members ? this.member_of_rank(counted, kind) : counted);
|
||||
this.store(&place, &made);
|
||||
this.declare(variable, place, false);
|
||||
}
|
||||
if inclusive {
|
||||
this.loop_body(&blocks, this.c.checker.right_of(node), locals);
|
||||
this.forget_locals(locals);
|
||||
this.build.assign(keep, this.build.binary(op::ne, this.build.read(count), last));
|
||||
this.count_on(count, blocks.head, blocks.exit);
|
||||
} else {
|
||||
this.end_pass(&blocks, node, locals, count);
|
||||
}
|
||||
this.end_statement(mark);
|
||||
}
|
||||
|
||||
#! Walks the elements of a fixed array or a view, which lie one after another,
|
||||
#! binding a reference to each.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `for_stmt` node.
|
||||
#! @param through The type walked, read through its references.
|
||||
def emitter::for_elements(&mut this, node: u32, through: u32): void {
|
||||
val mark := this.begin_statement();
|
||||
val whole := this.object(this.c.checker.record(node, for_slot::iterated));
|
||||
var first: u32 = 0;
|
||||
var limit: u32 = 0;
|
||||
var element: u32 = 0;
|
||||
if this.c.checker.types.kind_of(through) == ty::fixed {
|
||||
val shape := this.c.shape_of(through);
|
||||
first = this.address_of(&whole);
|
||||
limit = this.build.word(shape.count as u64);
|
||||
element = this.c.checker.types.first_of(through);
|
||||
} else {
|
||||
first = this.build.load(access::i64, whole.address, whole.offset);
|
||||
limit = this.build.load(access::i64, whole.address, whole.offset + word_size);
|
||||
element = this.c.viewed(through);
|
||||
}
|
||||
val size := this.c.shape_of(element).size;
|
||||
val counter := this.build.variable(vt::word);
|
||||
this.build.assign(counter, this.build.word(0));
|
||||
|
||||
val blocks := this.begin_loop();
|
||||
val locals := this.enter_pass(&blocks, this.build.binary(op::ltu, this.build.read(counter), limit));
|
||||
val address := this.build.binary(op::add, first, this.scaled(this.build.read(counter), size));
|
||||
val reference := operand::one(this.c.checker.types.reference(element, true), address);
|
||||
this.bind_pass(node, &reference, this.build.read(counter), false);
|
||||
this.end_pass(&blocks, node, locals, counter);
|
||||
this.end_statement(mark);
|
||||
}
|
||||
|
||||
#! Asks an iterator for each element until it gives `none`. The iterator is the
|
||||
#! one the loop is given, advanced where it is, or the one what it walks lends
|
||||
#! through `iter`, which the loop holds and closes where it ends. An element
|
||||
#! given by value is the loop's own, closed where each pass ends.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `for_stmt` node.
|
||||
#! @param through The type walked, read through its references.
|
||||
def emitter::for_iterator(&mut this, node: u32, through: u32): void {
|
||||
val mark := this.begin_statement();
|
||||
val iterated := this.c.checker.record(node, for_slot::iterated);
|
||||
val mutable := this.c.checker.node_has(node, nf::mutable);
|
||||
val locals := this.scope_mark();
|
||||
|
||||
# The iterator, where it is: what the loop is given, or what `iter` lends.
|
||||
var cursor_place := location {};
|
||||
var cursor_type := through;
|
||||
if !this.c.checker.has_method(through, word::next) {
|
||||
val lends := this.c.checker.pick_iter(through, mutable);
|
||||
val receiver := this.c.is_reference_type(this.c.checker.type_at(iterated))
|
||||
? this.through_one(iterated)
|
||||
: this.reference_arg(iterated, this.c.checker.types.reference(through, mutable));
|
||||
val made := this.method_call(lends, through, &receiver, span::<operand>::empty());
|
||||
this.adopt(&made);
|
||||
cursor_type = this.c.checker.unalias(made.kind);
|
||||
cursor_place = this.fresh_place(made.kind);
|
||||
this.store(&cursor_place, &made);
|
||||
this.declare(this.c.checker.right_of(node), cursor_place, this.c.checker.closes(made.kind));
|
||||
} else if this.c.is_reference_type(this.c.checker.type_at(iterated)) {
|
||||
val made := this.through_one(iterated);
|
||||
cursor_place = this.referent(&made);
|
||||
} else if this.c.checker.is_place(iterated) {
|
||||
cursor_place = this.place_of(iterated);
|
||||
} else {
|
||||
val made := this.value_of_type(iterated);
|
||||
this.adopt(&made);
|
||||
cursor_place = this.fresh_place(made.kind);
|
||||
this.store(&cursor_place, &made);
|
||||
this.declare(this.c.checker.right_of(node), cursor_place, this.c.checker.closes(made.kind));
|
||||
}
|
||||
|
||||
val next := this.c.checker.find_member(cursor_type, word::next);
|
||||
val counter := this.build.variable(vt::word);
|
||||
this.build.assign(counter, this.build.word(0));
|
||||
val blocks := this.begin_loop();
|
||||
|
||||
# `next` gives `some` of the element while there are more, and `none` after.
|
||||
val receiver := this.reference_to(&cursor_place, this.c.checker.types.reference(cursor_type, true));
|
||||
val given := this.method_call(next.decl, cursor_type, &receiver, span::<operand>::empty());
|
||||
val maybe := this.hold(&given);
|
||||
val plain := this.c.checker.unalias(given.kind);
|
||||
val index := this.c.layout_of(plain);
|
||||
var some: u32 = 0;
|
||||
for i in 0..this.c.layout_at(index).count {
|
||||
if this.c.field_at(index, i as u32).name == word::some {
|
||||
some = i as u32;
|
||||
}
|
||||
}
|
||||
val passes := this.enter_pass(&blocks, this.tag_is(&maybe, some));
|
||||
val field := this.c.field_at(index, some);
|
||||
val payload := this.within(&maybe, field.offset, field.type_id);
|
||||
val element := this.load(&payload);
|
||||
this.bind_pass(node, &element, this.build.read(counter), !this.c.is_reference_type(field.type_id) && this.c.checker.closes(field.type_id));
|
||||
this.end_pass(&blocks, node, passes, counter);
|
||||
|
||||
# Leaving the loop, by its end or a `break`, closes the iterator it holds.
|
||||
this.end_scope(locals, 0);
|
||||
this.end_statement(mark);
|
||||
}
|
||||
|
||||
#! The reference a node is, read through the references it refers to until one
|
||||
#! refers to what is no reference.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node, of a reference type.
|
||||
#! @returns The reference.
|
||||
def emitter::through_one(&mut this, node: u32): operand {
|
||||
var made := this.value_of_type(node);
|
||||
while this.c.is_reference_type(this.c.checker.types.first_of(this.c.checker.unalias(made.kind))) {
|
||||
val place := this.referent(&made);
|
||||
made = this.load(&place);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! `for const`: the body the checker checked once for each value of a pack, or
|
||||
#! each declaration reflection gives, each with a result table of its own, run
|
||||
#! one after another. `continue` goes on to the next, and `break` past the last.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `for_stmt` node.
|
||||
def emitter::unrolled(&mut this, node: u32): void {
|
||||
val variable := this.c.checker.record(node, for_slot::variable);
|
||||
val iterated := this.c.checker.record(node, for_slot::iterated);
|
||||
val body := this.c.checker.right_of(node);
|
||||
val outer := this.c.checker.at.table;
|
||||
val pack := this.c.checker.unalias(this.c.checker.reached(this.c.checker.type_at(iterated)));
|
||||
val is_pack := this.c.checker.types.kind_of(pack) == ty::pack;
|
||||
|
||||
# The tables the checker made for each pass, in order.
|
||||
var tables := vector::<u32>::new();
|
||||
for t in 0..this.c.checker.tables.length {
|
||||
val held: rtable = this.c.checker.tables.p[t];
|
||||
if held.parent == outer + 1 && held.base == variable && held.count == body - variable + 1 {
|
||||
tables.append(t as u32);
|
||||
}
|
||||
}
|
||||
|
||||
# The pack is named in the table around the passes, which theirs do not hold.
|
||||
var values := vector::<location>::new();
|
||||
if is_pack {
|
||||
for i in 0..this.c.checker.types.part_count(pack) {
|
||||
values.append(this.pack_place(iterated, i as u32));
|
||||
}
|
||||
}
|
||||
|
||||
val exit := this.build.new_block();
|
||||
for i in 0..tables.length {
|
||||
val next := this.build.new_block();
|
||||
this.c.checker.use_table(tables[i]);
|
||||
val locals := this.scope_mark();
|
||||
if is_pack {
|
||||
# The variable is the pack's value itself, so writing it writes the pack.
|
||||
val place: location = i < values.length ? values[i] : location { kind = bt::error };
|
||||
this.declare(variable, place, false);
|
||||
} else if this.c.checker.record(node, for_slot::end) != 0 {
|
||||
# The variable is the number this pass counts.
|
||||
val kind := this.c.checker.type_at(variable);
|
||||
val made := this.known(&this.c.checker.value_at(variable), kind);
|
||||
val place := this.new_place(variable, kind);
|
||||
this.store(&place, &made);
|
||||
this.declare(variable, place, false);
|
||||
} else {
|
||||
this.reflected_pass(variable);
|
||||
}
|
||||
this.loops.append(loop_info { exit = exit, next = next, locals = locals, kept = this.kept.length });
|
||||
this.lower_block(body);
|
||||
this.loops.truncate();
|
||||
this.end_scope(locals, 0);
|
||||
this.build.jump(next);
|
||||
this.build.seal(next);
|
||||
this.build.switch_to(next);
|
||||
this.c.checker.use_table(outer);
|
||||
}
|
||||
this.build.jump(exit);
|
||||
this.build.seal(exit);
|
||||
this.build.switch_to(exit);
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
#! @file match.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lowering of `match`: the first arm naming the field a union
|
||||
#! holds, or the member an enum is, or a value equal to the subject, runs, or
|
||||
#! else the `else` arm. A union's arm may name the value its field holds, a
|
||||
#! copy of it, taken out where it closes, or with `&` or `&mut` a reference to
|
||||
#! it. A match used as a value gives what its arm gives.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! A `match` used as a value.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `match_value` node.
|
||||
#! @returns What the arm taken gives.
|
||||
pub def emitter::match_value(&mut this, node: u32): operand {
|
||||
return this.lower_match(node, true);
|
||||
}
|
||||
|
||||
#! Lowers a match, as a statement or as a value.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `match_stmt` or `match_value` node.
|
||||
#! @param value Whether it is used as a value.
|
||||
#! @returns What the arm taken gives, where it is used as a value.
|
||||
pub def emitter::lower_match(&mut this, node: u32, value: bool): operand {
|
||||
val subject := this.c.checker.left_of(node);
|
||||
val arms := this.c.checker.right_of(node);
|
||||
val kind := value ? this.c.checker.type_at(node) : bt::void_type;
|
||||
val whole := this.object(subject);
|
||||
val plain := this.c.checker.unalias(whole.kind);
|
||||
val union_like := this.c.checker.named_kind(plain) == dk::union_type;
|
||||
|
||||
# Values are compared with the subject as read once, before any arm runs.
|
||||
var held := operand {};
|
||||
if !union_like {
|
||||
held = this.load(&whole);
|
||||
}
|
||||
|
||||
val result := value ? this.result_place(kind) : location {};
|
||||
val join := this.build.new_block();
|
||||
var otherwise := false;
|
||||
for i in 0..this.c.checker.run_length(arms) {
|
||||
val arm := this.c.checker.run_at(arms, i as u32);
|
||||
val patterns := this.c.checker.left_of(arm);
|
||||
val body := this.c.checker.right_of(arm);
|
||||
if patterns == 0 {
|
||||
this.lower_arm(arm, body, value, kind, &result, &whole, 0, 0);
|
||||
this.build.jump(join);
|
||||
otherwise = true;
|
||||
break;
|
||||
}
|
||||
|
||||
# The arm runs where any of its patterns does.
|
||||
val taken := this.build.new_block();
|
||||
val next := this.build.new_block();
|
||||
var field: u32 = 0;
|
||||
var binding: u32 = 0;
|
||||
for j in 0..this.c.checker.run_length(patterns) {
|
||||
val pattern := this.c.checker.run_at(patterns, j as u32);
|
||||
val failed := j + 1 == this.c.checker.run_length(patterns) as u64 ? next : this.build.new_block();
|
||||
var test: u32 = 0;
|
||||
if union_like {
|
||||
field = this.c.checker.ref_at(pattern);
|
||||
if this.c.checker.kind_of(pattern) == nk::call {
|
||||
val given := this.c.checker.right_of(pattern);
|
||||
if this.c.checker.run_length(given) == 1 {
|
||||
binding = this.c.checker.named_binding(this.c.checker.run_at(given, 0));
|
||||
}
|
||||
}
|
||||
test = this.tag_is(&whole, this.field_index(plain, field));
|
||||
} else {
|
||||
test = this.equals_value(&held, pattern, plain);
|
||||
}
|
||||
this.build.branch(test, taken, failed);
|
||||
if failed != next {
|
||||
this.build.seal(failed);
|
||||
this.build.switch_to(failed);
|
||||
}
|
||||
}
|
||||
this.build.seal(taken);
|
||||
this.build.seal(next);
|
||||
|
||||
this.build.switch_to(taken);
|
||||
this.lower_arm(arm, body, value, kind, &result, &whole, field, binding);
|
||||
this.build.jump(join);
|
||||
this.build.switch_to(next);
|
||||
}
|
||||
|
||||
# A match used as a value with no `else` finds no arm only for a value no enum
|
||||
# member has, which only memory written through pointers can hold, and which
|
||||
# only code that checks looks for.
|
||||
if !otherwise {
|
||||
if value && this.checking() {
|
||||
this.build.trap(fault::not_member as u32);
|
||||
} else if value {
|
||||
this.build.unreachable();
|
||||
} else {
|
||||
this.build.jump(join);
|
||||
}
|
||||
}
|
||||
this.build.seal(join);
|
||||
this.build.switch_to(join);
|
||||
return operand::none(bt::void_type) if !value;
|
||||
val made := this.load_result(&result);
|
||||
this.keep(&made);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The index of a union's field among its fields.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The union's type, without aliases.
|
||||
#! @param field The field's declaration.
|
||||
#! @returns Its index.
|
||||
pub def emitter::field_index(&mut this, kind: u32, field: u32): u32 {
|
||||
val index := this.c.layout_of(kind);
|
||||
for i in 0..this.c.layout_at(index).count {
|
||||
return i as u32 if this.c.field_at(index, i as u32).decl == field;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Whether a value equals a pattern's: as words, or through `equals`, or part by
|
||||
#! part for what the language compares that way.
|
||||
#! @param this The emitter.
|
||||
#! @param held The value.
|
||||
#! @param pattern The pattern's node.
|
||||
#! @param kind The type compared, without aliases.
|
||||
#! @returns A word of 1 where they are equal.
|
||||
def emitter::equals_value(&mut this, held: &operand, pattern: u32, kind: u32): u32 {
|
||||
val given := this.value_as(pattern, kind);
|
||||
val shape := this.c.shape_of(kind);
|
||||
if !in_memory(&shape) {
|
||||
if this.c.checker.has_method(kind, word::equals) && this.c.checker.named_kind(kind) != dk::enum_type
|
||||
&& !this.c.checker.types.is_numeric(kind) && this.c.checker.types.kind_of(kind) != ty::pointer
|
||||
&& !this.c.checker.types.is_boolean(kind) {
|
||||
return this.equals_call(held, &given, kind);
|
||||
}
|
||||
val floating := shape.kind == lk::floating;
|
||||
return this.build.binary(floating ? op::feq : op::eq, held.words[0], given.words[0]);
|
||||
}
|
||||
if this.c.checker.has_method(kind, word::equals) {
|
||||
return this.equals_call(held, &given, kind);
|
||||
}
|
||||
var args: u32[2] = [held.address, given.address];
|
||||
return this.build.value(op::equal, args[0:2], vt::word, this.c.layout_of(kind), 0);
|
||||
}
|
||||
|
||||
#! Calls a type's `equals` on two values, given references to each.
|
||||
#! @param this The emitter.
|
||||
#! @param a One value.
|
||||
#! @param b The other.
|
||||
#! @param kind Their type, without aliases.
|
||||
#! @returns A word of 1 where they are equal.
|
||||
def emitter::equals_call(&mut this, a: &operand, b: &operand, kind: u32): u32 {
|
||||
val method := this.pick_method(kind, word::equals, true);
|
||||
val reference := this.c.checker.types.reference(kind, false);
|
||||
val left := this.hold(a);
|
||||
val right := this.hold(b);
|
||||
val subject := this.reference_to(&left, reference);
|
||||
var others: operand[1] = [this.reference_to(&right, reference)];
|
||||
val made := this.method_call(method, kind, &subject, others[0:1]);
|
||||
return made.words[0];
|
||||
}
|
||||
|
||||
#! Lowers an arm: the value its pattern names, where it names one, and its body,
|
||||
#! whose value the match gives where it is used as one.
|
||||
#! @param this The emitter.
|
||||
#! @param arm The arm's node.
|
||||
#! @param body Its body's node.
|
||||
#! @param value Whether the match is used as a value.
|
||||
#! @param kind The match's type.
|
||||
#! @param result Where its value goes.
|
||||
#! @param whole The place of the subject.
|
||||
#! @param field The field the arm names of a union, or 0.
|
||||
#! @param binding The node naming the field's value, or 0.
|
||||
def emitter::lower_arm(
|
||||
&mut this,
|
||||
arm: u32,
|
||||
body: u32,
|
||||
value: bool,
|
||||
kind: u32,
|
||||
result: &location,
|
||||
whole: &location,
|
||||
field: u32,
|
||||
binding: u32
|
||||
): void {
|
||||
val locals := this.scope_mark();
|
||||
var named: u32 = 0;
|
||||
if binding != 0 && field != 0 {
|
||||
val refers := this.c.checker.kind_of(binding) == nk::reference;
|
||||
named = refers ? this.c.checker.left_of(binding) : binding;
|
||||
val plain := this.c.checker.unalias(whole.kind);
|
||||
val held := this.c.field_at(this.c.layout_of(plain), this.field_index(plain, field));
|
||||
val payload := this.within(whole, held.offset, held.type_id);
|
||||
val bound := this.c.checker.type_at(named);
|
||||
val place := this.new_place(named, bound);
|
||||
if refers {
|
||||
# A field holding a reference gives that reference, since references
|
||||
# never stack.
|
||||
var made := operand {};
|
||||
if this.c.checker.has_flag(binding, rf::relent) {
|
||||
val inner := this.load(&payload);
|
||||
made = this.convert(&inner, bound);
|
||||
} else {
|
||||
made = this.reference_to(&payload, bound);
|
||||
}
|
||||
this.store(&place, &made);
|
||||
this.declare(named, place, false);
|
||||
} else {
|
||||
val made := this.load(&payload);
|
||||
val converted := this.convert(&made, bound);
|
||||
this.store(&place, &converted);
|
||||
val closes := this.c.checker.closes(held.type_id);
|
||||
if closes {
|
||||
this.zero(&payload);
|
||||
}
|
||||
this.declare(named, place, closes);
|
||||
}
|
||||
}
|
||||
|
||||
# An arm giving the value its name took hands it over to the match.
|
||||
var given: u32 = 0;
|
||||
if value {
|
||||
if this.c.checker.kind_of(body) == nk::name && named != 0 && (this.c.checker.ref_at(body) & ref_mask) == named {
|
||||
given = named;
|
||||
}
|
||||
val made := this.value_as(body, kind);
|
||||
this.adopt(&made);
|
||||
this.store(result, &made);
|
||||
} else {
|
||||
this.lower_block(body);
|
||||
}
|
||||
this.end_scope(locals, given);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,146 @@
|
||||
#! @file ownership.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains closing values: a value of a type naming `owning` is closed by its
|
||||
#! own `close`, and then what it holds is closed, in the reverse of the order
|
||||
#! it was written, as a scope closes its locals. Also contains the text a
|
||||
#! format made, which a `string` holding no allocator owns.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Closes a value where it is: its own `close` first, then the fields it holds
|
||||
#! that close, the last first, or of a union the one it holds, or of a fixed
|
||||
#! array each element, the last first. Closing a zeroed value closes nothing.
|
||||
#! @param this The emitter.
|
||||
#! @param place The value's place.
|
||||
pub def emitter::close(&mut this, place: &location): void {
|
||||
return if place.held || !this.c.checker.closes(place.kind);
|
||||
val plain := this.c.checker.unalias(place.kind);
|
||||
val index := this.c.layout_of(plain);
|
||||
val shape := this.c.layout_at(index);
|
||||
|
||||
if shape.kind == lk::fixed {
|
||||
this.close_elements(place, plain, &shape);
|
||||
return;
|
||||
}
|
||||
|
||||
# Its own `close`, where it names `owning`.
|
||||
val owning := this.c.checker.std_decl(word::owning);
|
||||
if owning != 0 && this.c.checker.satisfies(plain, this.c.checker.decls.types.p[owning]) {
|
||||
val closer := this.closer_of(plain);
|
||||
if closer != 0 {
|
||||
val receiver := this.reference_to(place, this.c.checker.types.reference(plain, true));
|
||||
this.method_call(closer, plain, &receiver, span::<operand>::empty());
|
||||
}
|
||||
}
|
||||
|
||||
# A union holds one of its fields, so only that one is closed.
|
||||
if shape.kind == lk::variant {
|
||||
var at := shape.count;
|
||||
while at > 0 {
|
||||
at--;
|
||||
val each := this.c.field_at(index, at as u32);
|
||||
continue if !this.c.checker.closes(each.type_id);
|
||||
val holding := this.build.new_block();
|
||||
val after := this.build.new_block();
|
||||
this.build.branch(this.tag_is(place, at as u32), holding, after);
|
||||
this.build.seal(holding);
|
||||
this.build.switch_to(holding);
|
||||
val inner := this.within(place, each.offset, each.type_id);
|
||||
this.close(&inner);
|
||||
this.build.jump(after);
|
||||
this.build.seal(after);
|
||||
this.build.switch_to(after);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var at := shape.count;
|
||||
while at > 0 {
|
||||
at--;
|
||||
val each := this.c.field_at(index, at as u32);
|
||||
continue if !this.c.checker.closes(each.type_id);
|
||||
val inner := this.within(place, each.offset, each.type_id);
|
||||
this.close(&inner);
|
||||
}
|
||||
}
|
||||
|
||||
#! The `close(&mut this): void` of `owning` a type gives, or 0.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The type.
|
||||
#! @returns The method's declaration, or 0.
|
||||
pub def emitter::closer_of(&mut this, kind: u32): u32 {
|
||||
val found := this.c.checker.find_member(kind, word::close);
|
||||
return 0 if found.decl == 0;
|
||||
var walk := this.c.checker.decls.walk_overloads(found.decl);
|
||||
while true {
|
||||
val at := walk.next();
|
||||
break if at == 0;
|
||||
this.c.checker.resolve(at);
|
||||
if this.c.checker.decls.kinds.p[at] == dk::function && this.c.checker.decls.has(at, df::mut_this)
|
||||
&& this.c.checker.types.part_count(this.c.checker.decls.types.p[at]) == 1 {
|
||||
return at;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Closes the elements of a fixed array, the last first, by a loop.
|
||||
#! @param this The emitter.
|
||||
#! @param place The array's place.
|
||||
#! @param kind Its type, without aliases.
|
||||
#! @param shape Its layout.
|
||||
def emitter::close_elements(&mut this, place: &location, kind: u32, shape: &layout): void {
|
||||
return if shape.count == 0;
|
||||
val element := this.c.checker.types.first_of(kind);
|
||||
val size := this.c.layout_at(shape.element - 1).size;
|
||||
val first := this.address_of(place);
|
||||
val left := this.build.variable(vt::word);
|
||||
this.build.assign(left, this.build.word(shape.count as u64));
|
||||
|
||||
val head := this.build.new_block();
|
||||
val body := this.build.new_block();
|
||||
val exit := this.build.new_block();
|
||||
this.build.jump(head);
|
||||
this.build.switch_to(head);
|
||||
val count := this.build.read(left);
|
||||
this.build.branch(this.build.binary(op::ne, count, this.build.word(0)), body, exit);
|
||||
this.build.seal(body);
|
||||
this.build.switch_to(body);
|
||||
val index := this.build.binary(op::sub, this.build.read(left), this.build.word(1));
|
||||
this.build.assign(left, index);
|
||||
val address := this.build.binary(op::add, first, this.scaled(index, size));
|
||||
val inner := location { kind = element, address = address, framed = place.framed };
|
||||
this.close(&inner);
|
||||
this.close_loop(head, exit);
|
||||
}
|
||||
|
||||
#! The text a format made, taken from what the program manages itself and
|
||||
#! handed to a `string` holding no allocator, which gives it back with
|
||||
#! `raw_release` where it closes. The statement keeps it until a place takes it.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The `string` type.
|
||||
#! @returns The string.
|
||||
pub def emitter::owned_text(&mut this, kind: u32): operand {
|
||||
var gives: vt[2] = [vt::word, vt::word];
|
||||
val made := this.build.emit(op::format_owned, span::<u32>::empty(), gives[0:2], 0, 0);
|
||||
val pointer: u32 = this.build.code.insts[made].result;
|
||||
val length := pointer + 1;
|
||||
|
||||
val index := this.c.layout_of(this.c.checker.unalias(kind));
|
||||
val shape := this.c.layout_at(index);
|
||||
val address := this.temporary_of(kind);
|
||||
this.build.clear(address, shape.size);
|
||||
for i in 1..shape.count {
|
||||
break if i >= 4;
|
||||
val field := this.c.field_at(index, i as u32);
|
||||
this.build.store(access::i64, address, field.offset, i == 1 ? pointer : length);
|
||||
}
|
||||
var text := operand::at(kind, address);
|
||||
this.keep(&text);
|
||||
return text;
|
||||
}
|
||||
@@ -0,0 +1,617 @@
|
||||
#! @file places.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains places: where what a name, a field, an element, or what a pointer
|
||||
#! or reference reaches is, how what is there is read and written, and the
|
||||
#! references made to it. A reference is read through to what it refers to,
|
||||
#! as many as it takes, wherever a place is reached through it.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! The address a place's memory begins at.
|
||||
#! @param this The emitter.
|
||||
#! @param place The place, in memory.
|
||||
#! @returns The address.
|
||||
pub def emitter::address_of(&mut this, place: &location): u32 {
|
||||
return place.address if place.offset == 0;
|
||||
return this.build.binary(op::add, place.address, this.build.word(place.offset));
|
||||
}
|
||||
|
||||
#! Reads what a place holds. What is held in memory is copied to a cell of the
|
||||
#! statement's own, so writing the place afterwards leaves the value as it was.
|
||||
#! @param this The emitter.
|
||||
#! @param place The place.
|
||||
#! @returns The value.
|
||||
pub def emitter::load(&mut this, place: &location): operand {
|
||||
val held := this.c.shape_of(place.kind);
|
||||
var made := operand { kind = place.kind, count = word_count(&held) };
|
||||
if place.held {
|
||||
for i in 0..made.count {
|
||||
made.words[i] = this.build.read(place.vars[i]);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
if in_memory(&held) {
|
||||
val copy := this.temporary_of(place.kind);
|
||||
this.build.copy(copy, this.address_of(place), held.size);
|
||||
return operand::at(place.kind, copy);
|
||||
}
|
||||
if made.count == 1 {
|
||||
made.words[0] = this.build.load(held.how, place.address, place.offset);
|
||||
return made;
|
||||
}
|
||||
for i in 0..made.count {
|
||||
made.words[i] = this.build.load(access::i64, place.address, place.offset + i as u64 * word_size);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Writes a value to a place, which is of the value's type.
|
||||
#! @param this The emitter.
|
||||
#! @param place The place.
|
||||
#! @param value The value.
|
||||
pub def emitter::store(&mut this, place: &location, value: &operand): void {
|
||||
val held := this.c.shape_of(place.kind);
|
||||
if place.held {
|
||||
for i in 0..word_count(&held) {
|
||||
this.build.assign(place.vars[i], value.words[i]);
|
||||
this.bind(place.vars[i], value.words[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if in_memory(&held) {
|
||||
return if value.address == 0;
|
||||
this.build.copy(this.address_of(place), value.address, held.size);
|
||||
return;
|
||||
}
|
||||
if word_count(&held) == 1 {
|
||||
this.build.store(held.how, place.address, place.offset, value.words[0]);
|
||||
return;
|
||||
}
|
||||
for i in 0..word_count(&held) {
|
||||
this.build.store(access::i64, place.address, place.offset + i as u64 * word_size, value.words[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#! Zeroes what a place holds.
|
||||
#! @param this The emitter.
|
||||
#! @param place The place.
|
||||
pub def emitter::zero(&mut this, place: &location): void {
|
||||
val held := this.c.shape_of(place.kind);
|
||||
if place.held {
|
||||
for i in 0..word_count(&held) {
|
||||
val zero := word_type(&held) == vt::real ? this.build.real(0) : this.build.word(0);
|
||||
this.build.assign(place.vars[i], zero);
|
||||
this.bind(place.vars[i], zero);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.build.clear(this.address_of(place), held.size);
|
||||
}
|
||||
|
||||
#! A reference to a place, as the word a reference is: where the value is. A
|
||||
#! place held in variables has no address, so a copy of its value is referred
|
||||
#! to instead, which the storage worked out before lowering avoids for what a
|
||||
#! reference must reach.
|
||||
#! @param this The emitter.
|
||||
#! @param place The place.
|
||||
#! @param kind The reference's type.
|
||||
#! @returns The reference.
|
||||
pub def emitter::reference_to(&mut this, place: &location, kind: u32): operand {
|
||||
if place.held {
|
||||
val value := this.load(place);
|
||||
val copy := location { kind = place.kind, address = this.temporary_of(place.kind), framed = true };
|
||||
this.store(©, &value);
|
||||
return this.reference_to(©, kind);
|
||||
}
|
||||
|
||||
# A reference to a trait the value implements is given its type's vtable.
|
||||
val wanted := this.c.checker.unalias(kind);
|
||||
if this.c.checker.types.kind_of(wanted) == ty::reference {
|
||||
val referent := this.c.checker.unalias(this.c.checker.types.first_of(wanted));
|
||||
if this.c.checker.named_kind(referent) == dk::trait_type && this.c.checker.named_kind(this.c.checker.unalias(place.kind)) != dk::trait_type {
|
||||
val own := this.c.checker.types.reference(place.kind, this.c.checker.types.second_of(wanted) != 0);
|
||||
val made := operand::one(own, this.address_of(place));
|
||||
return this.convert(&made, kind);
|
||||
}
|
||||
}
|
||||
return operand::one(kind, this.address_of(place));
|
||||
}
|
||||
|
||||
#! The place a reference refers to.
|
||||
#! @param this The emitter.
|
||||
#! @param value The reference.
|
||||
#! @returns The place.
|
||||
pub def emitter::referent(&mut this, value: &operand): location {
|
||||
val kind := this.c.checker.types.first_of(this.c.checker.unalias(value.kind));
|
||||
# Running with its memory checked, what a reference refers to is where the
|
||||
# program holds memory, as what a pointer points at is.
|
||||
if this.c.options.checks && this.c.checker.named_kind(this.c.checker.unalias(kind)) != dk::trait_type {
|
||||
this.reaches(value.words[0], this.build.word(1), this.c.shape_of(kind).size);
|
||||
}
|
||||
return location { kind = kind, address = value.words[0] };
|
||||
}
|
||||
|
||||
#! A place within another, at an offset, as a field is.
|
||||
#! @param this The emitter.
|
||||
#! @param place The place holding it.
|
||||
#! @param offset How many bytes in it is.
|
||||
#! @param kind Its type.
|
||||
#! @returns The place.
|
||||
pub def emitter::within(&mut this, place: &location, offset: u64, kind: u32): location {
|
||||
var made: location = place;
|
||||
if place.held {
|
||||
# A value held in variables is no struct, so this is never reached; it is
|
||||
# copied to memory where it is.
|
||||
val value := this.load(place);
|
||||
made = location { kind = place.kind, address = this.temporary_of(place.kind), framed = true };
|
||||
this.store(&made, &value);
|
||||
}
|
||||
made.kind = kind;
|
||||
made.offset = made.offset + offset;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The place a node's value is at, read through as many references as it holds
|
||||
#! until what they refer to is no reference, or as many as `levels` says.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @param levels How many references to read through at most.
|
||||
#! @returns The place.
|
||||
pub def emitter::object_by(&mut this, node: u32, levels: u32): location {
|
||||
var kind := this.c.checker.type_at(node);
|
||||
if !this.c.is_reference_type(kind) || levels == 0 {
|
||||
return this.place_of(node);
|
||||
}
|
||||
|
||||
var value := this.value_of_type(node);
|
||||
var place := this.referent(&value);
|
||||
var steps: u32 = 1;
|
||||
while steps < levels && this.c.is_reference_type(place.kind) && steps < 16 {
|
||||
value = this.load(&place);
|
||||
place = this.referent(&value);
|
||||
steps++;
|
||||
}
|
||||
return place;
|
||||
}
|
||||
|
||||
#! The place a node's value is at, read through every reference it holds.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @returns The place.
|
||||
pub def emitter::object(&mut this, node: u32): location {
|
||||
return this.object_by(node, 16);
|
||||
}
|
||||
|
||||
#! The place a node names: a local, a global, a field, an element, or what a
|
||||
#! pointer points at; or else a cell of the statement's own holding its value.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @returns The place.
|
||||
pub def emitter::place_of(&mut this, node: u32): location {
|
||||
val saved := this.build.pos;
|
||||
this.at_node(node);
|
||||
val made := this.place_node(node);
|
||||
this.build.pos = saved;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The place a node names, by the kind of the node.
|
||||
#! @param this The emitter.
|
||||
#! @param node The node.
|
||||
#! @returns The place.
|
||||
def emitter::place_node(&mut this, node: u32): location {
|
||||
match this.c.checker.kind_of(node) {
|
||||
nk::name, nk::path => {
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
if (ref & ref_local) != 0 {
|
||||
val index := this.local_of(ref & ref_mask);
|
||||
if index != 0 {
|
||||
val held: local_info = this.locals[index - 1];
|
||||
return held.place;
|
||||
}
|
||||
if this.pass_variable(ref & ref_mask) {
|
||||
val held: local_info = this.locals[this.local_of(ref & ref_mask) - 1];
|
||||
return held.place;
|
||||
}
|
||||
this.problem(node, "The local is not where it is named.");
|
||||
} else if ref != 0 && (ref & ref_type) == 0 && this.c.checker.decls.kinds.p[ref] == dk::variable
|
||||
&& !this.c.checker.is_known(node) {
|
||||
return this.global_place(ref);
|
||||
}
|
||||
}
|
||||
nk::member => {
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::field {
|
||||
return this.field_place(node, ref);
|
||||
}
|
||||
}
|
||||
nk::member_of => {
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::field {
|
||||
return this.field_place(node, ref);
|
||||
}
|
||||
# What else a description names is no place.
|
||||
if this.property_of(node) == 0 {
|
||||
this.problem(node, "The member a description names is not lowered.");
|
||||
return location { kind = this.c.checker.type_at(node), address = this.build.word(0) };
|
||||
}
|
||||
}
|
||||
nk::index => {
|
||||
# Through `at`, it is the reference `at` gives, held as any value is.
|
||||
if !this.indexes_through_at(node) {
|
||||
return this.element_place(node);
|
||||
}
|
||||
}
|
||||
nk::call => {
|
||||
# `value_of(d)` of a variable is the variable.
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::variable
|
||||
&& !this.c.checker.is_known(node) {
|
||||
return this.global_place(ref);
|
||||
}
|
||||
}
|
||||
nk::unary => {
|
||||
if this.c.checker.token_kind(node) == tk::star {
|
||||
# A reference to a pointer is read through to the pointer.
|
||||
val given := this.value_of_type(this.c.checker.left_of(node));
|
||||
val pointer := this.through_references(&given);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
this.reaches(pointer.words[0], this.build.word(1), this.c.shape_of(kind).size);
|
||||
return location { kind = kind, address = pointer.words[0] };
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
|
||||
# Anything else is a value, held where the statement holds what it makes.
|
||||
val value := this.value_of_type(node);
|
||||
return this.hold(&value);
|
||||
}
|
||||
|
||||
#! A place holding a value: its own memory, where it is in memory, or a cell of
|
||||
#! the statement's own the value is written to.
|
||||
#! @param this The emitter.
|
||||
#! @param value The value.
|
||||
#! @returns The place.
|
||||
pub def emitter::hold(&mut this, value: &operand): location {
|
||||
if value.address != 0 {
|
||||
return location { kind = value.kind, address = value.address, framed = true };
|
||||
}
|
||||
val place := location { kind = value.kind, address = this.temporary_of(value.kind), framed = true };
|
||||
this.store(&place, value);
|
||||
return place;
|
||||
}
|
||||
|
||||
#! The place of a global, where it is placed among the globals.
|
||||
#! @param this The emitter.
|
||||
#! @param decl The global's declaration.
|
||||
#! @returns The place.
|
||||
pub def emitter::global_place(&mut this, decl: u32): location {
|
||||
val kind: u32 = this.c.checker.decls.types.p[decl];
|
||||
val constant := this.c.checker.decls.has(decl, df::constant);
|
||||
|
||||
# While compiling, a constant is read once it is evaluated, and a variable
|
||||
# given its value once the program runs is not read at all.
|
||||
if this.c.options.evaluating {
|
||||
if !constant {
|
||||
this.build.trap(fault::not_constant as u32);
|
||||
val nowhere := this.build.word(0);
|
||||
return location { kind = kind, address = nowhere };
|
||||
}
|
||||
val index := this.c.evaluation_of(decl_key(decl), decl, 0, 0, false);
|
||||
this.build.effect(op::ready, span::<u32>::empty(), index, 0);
|
||||
val offset: u64 = this.c.evaluations[index].offset;
|
||||
val address := this.build.value(op::global, span::<u32>::empty(), vt::word, 0, offset);
|
||||
return location { kind = kind, address = address };
|
||||
}
|
||||
|
||||
# A constant evaluated is read where the program holds its value as data.
|
||||
if constant {
|
||||
val datum := this.c.evaluated_datum(decl_key(decl), kind);
|
||||
if datum != no_block {
|
||||
val address := this.build.value(op::datum, span::<u32>::empty(), vt::word, datum, 0);
|
||||
return location { kind = kind, address = address };
|
||||
}
|
||||
}
|
||||
|
||||
val offset := this.c.global_of(decl);
|
||||
val address := this.build.value(op::global, span::<u32>::empty(), vt::word, 0, offset);
|
||||
return location { kind = kind, address = address };
|
||||
}
|
||||
|
||||
#! Whether the code checks what only a checked build does: indexes and slices,
|
||||
#! the field a union holds, an integer converted to an enum, and a `match`
|
||||
#! finding no arm. A constant is evaluated checked in every build.
|
||||
#! @param this The emitter.
|
||||
#! @returns Whether it does.
|
||||
pub def emitter::checking(&this): bool {
|
||||
return !this.c.checker.checks_off || this.c.options.evaluating;
|
||||
}
|
||||
|
||||
#! Stops the program where a pointer is null, which only the code of a checked
|
||||
#! program reaching an element past the first page asks. Elsewhere reading or
|
||||
#! writing through null stops the program where it does, in every build: the
|
||||
#! machine compares each address it reads or writes through with the first
|
||||
#! page, and an executable faults there, which its runtime reports as the same.
|
||||
#! @param this The emitter.
|
||||
#! @param pointer The pointer.
|
||||
def emitter::compare_null(&mut this, pointer: u32): void {
|
||||
val zero := this.build.word(0);
|
||||
this.build.check(this.build.binary(op::ne, pointer, zero), fault::null_reference as u32);
|
||||
}
|
||||
|
||||
#! The place of a field of a struct or union, read through the references the
|
||||
#! object is reached by. A field a struct's spread brings in is reached through
|
||||
#! the spread field holding it, and a field of a union only where the union
|
||||
#! holds it.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `member` node.
|
||||
#! @param field The field's declaration.
|
||||
#! @returns The place.
|
||||
pub def emitter::field_place(&mut this, node: u32, field: u32): location {
|
||||
val object := this.c.checker.left_of(node);
|
||||
val whole := this.object(object);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
val owner := this.c.checker.unalias(whole.kind);
|
||||
return this.field_in(&whole, owner, field, kind);
|
||||
}
|
||||
|
||||
#! The place of a field within a place holding a struct or union of a type.
|
||||
#! @param this The emitter.
|
||||
#! @param whole The place of the struct or union.
|
||||
#! @param owner Its type, without aliases.
|
||||
#! @param field The field's declaration.
|
||||
#! @param kind The field's type, as checked.
|
||||
#! @returns The place.
|
||||
pub def emitter::field_in(&mut this, whole: &location, owner: u32, field: u32, kind: u32): location {
|
||||
val index := this.c.layout_of(owner);
|
||||
val shape := this.c.layout_at(index);
|
||||
for i in 0..shape.count {
|
||||
val each := this.c.field_at(index, i as u32);
|
||||
if each.decl == field {
|
||||
if shape.kind == lk::variant {
|
||||
this.check_held(whole, i as u32);
|
||||
}
|
||||
return this.within(whole, each.offset, each.type_id);
|
||||
}
|
||||
}
|
||||
|
||||
# Reached through a spread field, which holds the struct declaring it.
|
||||
for i in 0..shape.count {
|
||||
val each := this.c.field_at(index, i as u32);
|
||||
continue if !each.spread;
|
||||
val inner := this.within(whole, each.offset, each.type_id);
|
||||
val plain := this.c.checker.unalias(each.type_id);
|
||||
if this.holds_field(plain, field, 0) {
|
||||
return this.field_in(&inner, plain, field, kind);
|
||||
}
|
||||
}
|
||||
this.problem(0, "A field is not in the layout of what holds it.");
|
||||
return this.within(whole, 0, kind);
|
||||
}
|
||||
|
||||
#! Whether a struct holds a field, its own or one a spread brings in.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The struct's type.
|
||||
#! @param field The field's declaration.
|
||||
#! @param depth How deeply spreads have been followed.
|
||||
#! @returns Whether it holds it.
|
||||
def emitter::holds_field(&mut this, kind: u32, field: u32, depth: u32): bool {
|
||||
return false if depth > 16;
|
||||
val index := this.c.layout_of(kind);
|
||||
val shape := this.c.layout_at(index);
|
||||
for i in 0..shape.count {
|
||||
val each := this.c.field_at(index, i as u32);
|
||||
return true if each.decl == field;
|
||||
if each.spread && this.holds_field(this.c.checker.unalias(each.type_id), field, depth + 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Stops the program unless a union holds its field of an index, where the code
|
||||
#! checks.
|
||||
#! @param this The emitter.
|
||||
#! @param whole The place of the union.
|
||||
#! @param index The field's index.
|
||||
pub def emitter::check_held(&mut this, whole: &location, index: u32): void {
|
||||
return if !this.checking();
|
||||
val holds := this.tag_is(whole, index);
|
||||
this.build.check(holds, fault::not_held as u32);
|
||||
}
|
||||
|
||||
#! Whether a union holds its field of an index, as a word of 0 or 1.
|
||||
#! @param this The emitter.
|
||||
#! @param whole The place of the union.
|
||||
#! @param index The field's index.
|
||||
#! @returns The word.
|
||||
pub def emitter::tag_is(&mut this, whole: &location, index: u32): u32 {
|
||||
var tag: u32 = 0;
|
||||
if whole.held {
|
||||
tag = this.build.read(whole.vars[0]);
|
||||
} else {
|
||||
tag = this.build.load(access::i64, whole.address, whole.offset);
|
||||
}
|
||||
# A union without a tag holds the field it is laid out for where its first word
|
||||
# is not null, and the empty one where it is.
|
||||
val shape := this.c.shape_of(whole.kind);
|
||||
if shape.niche != 0 {
|
||||
return this.build.binary(index + 1 == shape.niche ? op::ne : op::eq, tag, this.build.word(0));
|
||||
}
|
||||
return this.build.binary(op::eq, tag, this.build.word(index as u64));
|
||||
}
|
||||
|
||||
#! The place of an element: of a fixed array, at an index checked against its
|
||||
#! length; of what a pointer points at, which is not checked; of a view, checked
|
||||
#! against how many it views; of a pack, at an index known while compiling; or
|
||||
#! of what implements `indexable`, the place its `at` refers to.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `index` node.
|
||||
#! @returns The place.
|
||||
pub def emitter::element_place(&mut this, node: u32): location {
|
||||
val object := this.c.checker.left_of(node);
|
||||
val at := this.c.checker.right_of(node);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
val whole_type := this.object_type(object);
|
||||
val through := this.c.checker.unalias(this.c.checker.reached(whole_type));
|
||||
|
||||
match this.c.checker.types.kind_of(through) {
|
||||
ty::fixed => {
|
||||
val whole := this.object(object);
|
||||
val shape := this.c.shape_of(through);
|
||||
val element := this.c.layout_at(shape.element - 1);
|
||||
if this.c.checker.is_known(at) {
|
||||
val index := this.c.checker.value_at(at).magnitude;
|
||||
if index < shape.count as u64 {
|
||||
return this.within(&whole, index * element.size, this.c.checker.types.first_of(through));
|
||||
}
|
||||
}
|
||||
val index := this.value_as(at, bt::uint64);
|
||||
if this.checking() {
|
||||
val within := this.build.binary(op::ltu, index.words[0], this.build.word(shape.count as u64));
|
||||
this.build.check(within, fault::out_of_bounds as u32);
|
||||
}
|
||||
val first := this.address_of(&whole);
|
||||
val address := this.build.binary(op::add, first, this.scaled(index.words[0], element.size));
|
||||
return location { kind = this.c.checker.types.first_of(through), address = address, framed = whole.framed };
|
||||
}
|
||||
ty::pointer => {
|
||||
val pointer := this.value_of_type(object);
|
||||
val reached := this.through_references(&pointer);
|
||||
val element := this.c.shape_of(this.c.checker.types.first_of(through));
|
||||
# An element past the first page is no fault there, so a checked program
|
||||
# compares the pointer with null first, unless the index is known to
|
||||
# stay within it.
|
||||
val near := this.c.checker.is_known(at) && (element.size == 0 || this.c.checker.value_at(at).magnitude < null_page / element.size);
|
||||
if this.checking() && !near {
|
||||
this.compare_null(reached.words[0]);
|
||||
}
|
||||
val index := this.value_as(at, bt::uint64);
|
||||
val address := this.build.binary(op::add, reached.words[0], this.scaled(index.words[0], element.size));
|
||||
this.reaches(address, this.build.word(1), element.size);
|
||||
return location { kind = this.c.checker.types.first_of(through), address = address };
|
||||
}
|
||||
ty::pack => {
|
||||
val index := this.c.checker.value_at(at).magnitude;
|
||||
return this.pack_place(object, index as u32);
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
|
||||
# A view holds where its elements are and how many, so an element of one is the
|
||||
# place that many along, once the index is checked against the count. A view
|
||||
# holding any element is never over null, as std makes every view, so the
|
||||
# index's check is all it needs.
|
||||
val viewed := this.c.viewed(through);
|
||||
if viewed != 0 {
|
||||
val whole := this.object(object);
|
||||
val data := this.build.load(access::i64, whole.address, whole.offset);
|
||||
val count := this.build.load(access::i64, whole.address, whole.offset + word_size);
|
||||
val index := this.value_as(at, bt::uint64);
|
||||
if this.checking() {
|
||||
this.build.check(this.build.binary(op::ltu, index.words[0], count), fault::out_of_bounds as u32);
|
||||
}
|
||||
val element := this.c.shape_of(viewed);
|
||||
val address := this.build.binary(op::add, data, this.scaled(index.words[0], element.size));
|
||||
return location { kind = viewed, address = address };
|
||||
}
|
||||
|
||||
# What implements `indexable` gives a reference to the element through `at`.
|
||||
val method := this.c.checker.ref_at(node);
|
||||
if method != 0 && (method & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[method] == dk::function {
|
||||
val made := this.accessor_call(node, method, object, at);
|
||||
if this.c.is_reference_type(made.kind) {
|
||||
return this.referent(&made);
|
||||
}
|
||||
return this.hold(&made);
|
||||
}
|
||||
this.problem(node, "Nothing is indexed here.");
|
||||
return location { kind = kind, address = this.temporary_of(kind) };
|
||||
}
|
||||
|
||||
#! The type of what an element or a member is reached through: its own, or,
|
||||
#! for a constant, the one it settles as, so `"text"[0]` is a `strview`'s.
|
||||
#! @param this The emitter.
|
||||
#! @param object The node of the value reached through.
|
||||
#! @returns The type.
|
||||
pub def emitter::object_type(&mut this, object: u32): u32 {
|
||||
val own := this.c.checker.type_at(object);
|
||||
return this.c.checker.types.is_untyped(own) ? this.c.checker.converted(object) : own;
|
||||
}
|
||||
|
||||
#! Whether `x[k]` is indexed through `at`, and so is the reference it gives
|
||||
#! rather than a place of its own.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `index` node.
|
||||
#! @returns Whether it is.
|
||||
pub def emitter::indexes_through_at(&mut this, node: u32): bool {
|
||||
val method := this.c.checker.ref_at(node);
|
||||
return method != 0 && (method & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[method] == dk::function
|
||||
&& this.c.is_reference_type(this.c.checker.type_at(node));
|
||||
}
|
||||
|
||||
#! The reference `x[k]` gives through `at`: the one `at` gives, or, for a view,
|
||||
#! which is indexed without calling it, one to the element it covers.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `index` node.
|
||||
#! @returns The reference.
|
||||
pub def emitter::indexed_reference(&mut this, node: u32): operand {
|
||||
val object := this.c.checker.left_of(node);
|
||||
val through := this.c.checker.unalias(this.c.checker.reached(this.object_type(object)));
|
||||
if this.c.viewed(through) != 0 {
|
||||
val place := this.element_place(node);
|
||||
return this.reference_to(&place, this.c.checker.type_at(node));
|
||||
}
|
||||
return this.accessor_call(node, this.c.checker.ref_at(node), object, this.c.checker.right_of(node));
|
||||
}
|
||||
|
||||
#! A count of elements of a size, in bytes.
|
||||
#! @param this The emitter.
|
||||
#! @param count The count, a word.
|
||||
#! @param size The size of each.
|
||||
#! @returns The bytes.
|
||||
pub def emitter::scaled(&mut this, count: u32, size: u64): u32 {
|
||||
return count if size == 1;
|
||||
return this.build.binary(op::mul, count, this.build.word(size));
|
||||
}
|
||||
|
||||
#! A value read through every reference it is, until it is no reference.
|
||||
#! @param this The emitter.
|
||||
#! @param value The value.
|
||||
#! @returns What it reaches.
|
||||
pub def emitter::through_references(&mut this, value: &operand): operand {
|
||||
var at: operand = value;
|
||||
var steps := 0;
|
||||
while this.c.is_reference_type(at.kind) && steps < 16 {
|
||||
val place := this.referent(&at);
|
||||
at = this.load(&place);
|
||||
steps++;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! The place of a value of a pack, by its index.
|
||||
#! @param this The emitter.
|
||||
#! @param object The node naming the pack.
|
||||
#! @param index The value's index.
|
||||
#! @returns The place.
|
||||
pub def emitter::pack_place(&mut this, object: u32, index: u32): location {
|
||||
val ref := this.c.checker.ref_at(object);
|
||||
val local := (ref & ref_local) != 0 ? this.local_of(ref & ref_mask) : 0;
|
||||
if local != 0 {
|
||||
val held: local_info = this.locals[local - 1];
|
||||
if index < held.count {
|
||||
return this.packs[held.pack as u64 + index as u64];
|
||||
}
|
||||
}
|
||||
this.problem(object, "The pack holds no value at the index.");
|
||||
return location { kind = bt::error };
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
#! @file reflection.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what reflection gives a program: descriptions of its declarations,
|
||||
#! as `core::decl`s, which the checker found while compiling. They are written
|
||||
#! as data the program holds as long as it runs, the text they name among it,
|
||||
#! and read where the program stands.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! What reflection gives an expression: one description, read from the data
|
||||
#! holding it, or a view of a list of them.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @returns The value.
|
||||
pub def emitter::reflected_value(&mut this, node: u32): operand {
|
||||
val kind := this.c.checker.type_at(node);
|
||||
val at := this.c.checker.reflected_at(node);
|
||||
if at == checker::no_reflection {
|
||||
this.problem(node, "Reflection gives nothing here.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
|
||||
var items := vector::<u32>::new();
|
||||
for i in 0..this.c.checker.reflected_count(at) {
|
||||
items.append(this.c.checker.reflected_item(at, i as u32));
|
||||
}
|
||||
if this.c.checker.reflected_kind(at) == rk::one as u32 {
|
||||
val datum := this.c.descriptions(items.view(), kind);
|
||||
val address := this.build.value(op::datum, span::<u32>::empty(), vt::word, datum, 0);
|
||||
val place := location { kind = kind, address = address };
|
||||
return this.load(&place);
|
||||
}
|
||||
|
||||
val view := this.temporary_of(kind);
|
||||
if items.length == 0 {
|
||||
this.build.clear(view, 2 * word_size);
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
val element := this.c.viewed(kind);
|
||||
val datum := this.c.descriptions(items.view(), element);
|
||||
val first := this.build.value(op::datum, span::<u32>::empty(), vt::word, datum, 0);
|
||||
this.build.store(access::i64, view, 0, first);
|
||||
this.build.store(access::i64, view, word_size, this.build.word(items.length));
|
||||
return operand::at(kind, view);
|
||||
}
|
||||
|
||||
#! Declares the variable of a pass of `for const` over a list reflection gives:
|
||||
#! the description the pass is of.
|
||||
#! @param this The emitter.
|
||||
#! @param variable The node of the loop's variable.
|
||||
pub def emitter::reflected_pass(&mut this, variable: u32): void {
|
||||
val kind := this.c.checker.type_at(variable);
|
||||
val made := this.reflected_value(variable);
|
||||
val place := this.new_place(variable, kind);
|
||||
this.store(&place, &made);
|
||||
this.declare(variable, place, false);
|
||||
}
|
||||
|
||||
#! Declares the variable of a pass of `for const` over a list reflection gives
|
||||
#! where a function the pass doesn't run in names it, as the one evaluating the
|
||||
#! condition of an `if const` within the pass does: the description the pass is
|
||||
#! of, as it is in the pass.
|
||||
#! @param this The emitter.
|
||||
#! @param variable The node of the loop's variable.
|
||||
#! @returns Whether it is such a variable, and was declared.
|
||||
pub def emitter::pass_variable(&mut this, variable: u32): bool {
|
||||
val at := this.c.checker.reflected_at(variable);
|
||||
return false if at == checker::no_reflection || this.c.checker.reflected_kind(at) != rk::one as u32;
|
||||
this.reflected_pass(variable);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The datum holding descriptions as `core::decl`s one after another, made where
|
||||
#! it is new, with the text each names among the program's data.
|
||||
#! @param this The compiler.
|
||||
#! @param items The descriptions' indices among the checker's.
|
||||
#! @param kind The type of each, `core::decl`.
|
||||
#! @returns The datum's index.
|
||||
pub def lowerer::descriptions(&mut this, items: span<u32>, kind: u32): u32 {
|
||||
val plain := this.checker.unalias(kind);
|
||||
val index := this.layout_of(plain);
|
||||
val shape := this.layout_at(index);
|
||||
var bytes := vector::<u8>::new();
|
||||
bytes.resize(shape.size * items.length);
|
||||
var relocations := vector::<relocation>::new();
|
||||
|
||||
for i in 0..items.length {
|
||||
val held: desc = this.checker.descs[items[i]];
|
||||
val base := i * shape.size;
|
||||
var name := string::new(nothing {});
|
||||
var path := string::new(nothing {});
|
||||
var signature := string::new(nothing {});
|
||||
val decl := held.decl;
|
||||
if held.param != 0 {
|
||||
this.param_name(decl, held.param - 1, &mut name);
|
||||
} else {
|
||||
name.append(this.checker.names.text(this.checker.decls.names.p[decl]));
|
||||
}
|
||||
this.qualified_name(decl, &mut path);
|
||||
this.signature_text(decl, &mut signature);
|
||||
|
||||
val file: u32 = this.checker.decls.files.p[decl];
|
||||
val source := &this.checker.units[file].source;
|
||||
val place := source.lines.place_of(this.node_offset(file, this.checker.decls.nodes.p[decl]));
|
||||
for f in 0..shape.count {
|
||||
val field := this.field_at(index, f as u32);
|
||||
val field_name := this.checker.names.text(field.name);
|
||||
val at := base + field.offset;
|
||||
if field_name == "id" {
|
||||
write_number(&mut bytes, at, this.layout_at(field.layout).size, decl as u64);
|
||||
} else if field_name == "kind" {
|
||||
val bit := held.param != 0 ? 0x2000u : this.checker.target_bit(decl);
|
||||
write_number(&mut bytes, at, this.layout_at(field.layout).size, bit);
|
||||
} else if field_name == "exported" {
|
||||
write_number(&mut bytes, at, 1, held.param == 0 && this.checker.decls.has(decl, df::exported) ? 1 : 0);
|
||||
} else if field_name == "generic" {
|
||||
write_number(&mut bytes, at, 1, held.param == 0 && this.checker.own_param_count(decl) != 0 ? 1 : 0);
|
||||
} else if field_name == "name" {
|
||||
this.write_text(&mut bytes, &mut relocations, at, name.view());
|
||||
} else if field_name == "path" {
|
||||
this.write_text(&mut bytes, &mut relocations, at, path.view());
|
||||
} else if field_name == "at" {
|
||||
val inner := this.layout_at(field.layout);
|
||||
for s in 0..inner.count {
|
||||
val part := this.field_at(field.layout, s as u32);
|
||||
val part_name := this.checker.names.text(part.name);
|
||||
val within := at + part.offset;
|
||||
if part_name == "path" {
|
||||
this.write_text(&mut bytes, &mut relocations, within, source.path);
|
||||
} else if part_name == "line" {
|
||||
write_number(&mut bytes, within, this.layout_at(part.layout).size, place.line as u64);
|
||||
} else if part_name == "column" {
|
||||
write_number(&mut bytes, within, this.layout_at(part.layout).size, place.column as u64);
|
||||
} else if part_name == "signature" {
|
||||
this.write_text(&mut bytes, &mut relocations, within, signature.view());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val datum := this.program.data.length as u32;
|
||||
bytes.append(0);
|
||||
this.program.data.append(datum {
|
||||
bytes = move(&mut bytes),
|
||||
align = shape.align < word_size ? word_size : shape.align,
|
||||
relocations = move(&mut relocations)
|
||||
});
|
||||
return datum;
|
||||
}
|
||||
|
||||
#! Writes a number into bytes, as wide as it says, least significant first.
|
||||
#! @param bytes Where it is written.
|
||||
#! @param at The first byte.
|
||||
#! @param size How many bytes it takes.
|
||||
#! @param value The number.
|
||||
pub def write_number(bytes: &mut vector<u8>, at: u64, size: u64, value: u64): void {
|
||||
for i in 0..size {
|
||||
bytes[at + i] = ((value >> (i * 8)) & 0xFF) as u8;
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes a view of text into bytes: where the text is, as the address of a
|
||||
#! datum holding it, and how many bytes it holds. Empty text points nowhere.
|
||||
#! @param this The compiler.
|
||||
#! @param bytes Where the view is written.
|
||||
#! @param relocations Where the address is noted.
|
||||
#! @param at The first byte of the view.
|
||||
#! @param text The text.
|
||||
def lowerer::write_text(&mut this, bytes: &mut vector<u8>, relocations: &mut vector<relocation>, at: u64, text: strview): void {
|
||||
if text.length != 0 {
|
||||
relocations.append(relocation { at = at, target = this.text_datum(text), addend = 0 });
|
||||
}
|
||||
write_number(bytes, at + word_size, word_size, text.length);
|
||||
}
|
||||
|
||||
#! The name a function's parameter is declared with.
|
||||
#! @param this The compiler.
|
||||
#! @param decl The function's declaration.
|
||||
#! @param index The parameter's position.
|
||||
#! @param out Where the name is written.
|
||||
def lowerer::param_name(&mut this, decl: u32, index: u32, out: &mut string): void {
|
||||
val saved := this.checker.at;
|
||||
this.checker.use_table(this.checker.units[this.checker.decls.files.p[decl]].table);
|
||||
val node: u32 = this.checker.decls.nodes.p[decl];
|
||||
val params := this.checker.decls.has(decl, df::accessor) ? this.checker.left_of(node) : this.checker.record(node, function_slot::params);
|
||||
if index < this.checker.run_length(params) {
|
||||
out.append(this.checker.names.text(this.checker.param_name(this.checker.run_at(params, index))));
|
||||
}
|
||||
this.checker.at = saved;
|
||||
}
|
||||
|
||||
#! The name a declaration is reached by from outside its module, as
|
||||
#! `sakura::analyzer::interns_keywords`.
|
||||
#! @param this The compiler.
|
||||
#! @param decl The declaration.
|
||||
#! @param out Where the name is written.
|
||||
pub def lowerer::qualified_name(&mut this, decl: u32, out: &mut string): void {
|
||||
var modules := vector::<u32>::new();
|
||||
var at: u32 = this.checker.decls.modules.p[decl];
|
||||
var steps := 0;
|
||||
while at != 0 && steps < 64 {
|
||||
modules.append(at);
|
||||
at = this.checker.modules.parents[at];
|
||||
steps++;
|
||||
}
|
||||
var i := modules.length;
|
||||
while i > 0 {
|
||||
i--;
|
||||
out.append(this.checker.names.text(this.checker.modules.names[modules[i]]));
|
||||
out.append("::");
|
||||
}
|
||||
var holder: u32 = this.checker.decls.owners.p[decl];
|
||||
if holder != 0 && this.checker.decls.kinds.p[holder] == dk::property {
|
||||
holder = this.checker.decls.owners.p[holder];
|
||||
}
|
||||
if holder != 0 && this.checker.decls.kinds.p[holder] != dk::type_param {
|
||||
out.append(this.checker.names.text(this.checker.decls.names.p[holder]));
|
||||
out.append("::");
|
||||
}
|
||||
out.append(this.checker.names.text(this.checker.decls.names.p[decl]));
|
||||
}
|
||||
|
||||
#! The signature a declaration is written with: its text from where it begins
|
||||
#! to its body, or its first line.
|
||||
#! @param this The compiler.
|
||||
#! @param decl The declaration.
|
||||
#! @param out Where the text is written.
|
||||
pub def lowerer::signature_text(&mut this, decl: u32, out: &mut string): void {
|
||||
val file: u32 = this.checker.decls.files.p[decl];
|
||||
val node: u32 = this.checker.decls.nodes.p[decl];
|
||||
return if node == 0;
|
||||
val syntax := &this.checker.units[file].syntax;
|
||||
val source := &this.checker.units[file].source;
|
||||
val begin := syntax.token_offsets[syntax.start_of(node)];
|
||||
val end_token := syntax.node_end(node);
|
||||
val end := syntax.token_offsets[end_token] + syntax.token_length(end_token, this.checker.names);
|
||||
return if end <= begin;
|
||||
val whole := source.make_lexeme(begin, end - begin);
|
||||
var stop := 0u;
|
||||
while stop < whole.length && whole[stop] != '{' && whole[stop] != '\n' {
|
||||
stop++;
|
||||
}
|
||||
while stop > 0 && (whole[stop - 1] == ' ' || whole[stop - 1] == '\t') {
|
||||
stop--;
|
||||
}
|
||||
out.append(whole.substr(0, stop));
|
||||
}
|
||||
@@ -0,0 +1,653 @@
|
||||
#! @file statements.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lowering of statements: blocks and the locals declared in
|
||||
#! them, branches, `if const` and `when`, loops, contracts, and leaving by
|
||||
#! `return`, `break`, and `continue`, each closing what it leaves behind.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Lowers a block, whose locals close where it ends. Statements after one the
|
||||
#! checker found always leaves, as decided while checking, were never checked,
|
||||
#! so they are not lowered either.
|
||||
#! @param this The emitter.
|
||||
#! @param node The block, or a statement standing for one.
|
||||
pub def emitter::lower_block(&mut this, node: u32): void {
|
||||
return if node == 0;
|
||||
if this.c.checker.kind_of(node) != nk::block {
|
||||
this.lower_statement(node);
|
||||
return;
|
||||
}
|
||||
|
||||
val mark := this.scope_mark();
|
||||
val run := this.c.checker.left_of(node);
|
||||
for i in 0..this.c.checker.run_length(run) {
|
||||
val each := this.c.checker.run_at(run, i as u32);
|
||||
this.lower_statement(each);
|
||||
break if this.skips(each);
|
||||
}
|
||||
this.end_scope(mark, 0);
|
||||
}
|
||||
|
||||
#! Whether a statement leaves where it was decided while checking, so the
|
||||
#! checker checked nothing after it: an `if const` whose branch taken leaves,
|
||||
#! with no `else`, or a block or `when` holding one.
|
||||
#! @param this The emitter.
|
||||
#! @param node The statement.
|
||||
#! @returns Whether it does.
|
||||
pub def emitter::skips(&mut this, node: u32): bool {
|
||||
match this.c.checker.kind_of(node) {
|
||||
nk::block => {
|
||||
val run := this.c.checker.left_of(node);
|
||||
for i in 0..this.c.checker.run_length(run) {
|
||||
return true if this.skips(this.c.checker.run_at(run, i as u32));
|
||||
}
|
||||
}
|
||||
nk::when_block => return this.skips(this.c.checker.when_branch(node));
|
||||
nk::if_const => {
|
||||
val condition := this.c.checker.left_of(node);
|
||||
return false if !this.c.checker.is_known(condition);
|
||||
val then := this.c.checker.record(node, if_slot::then);
|
||||
val other := this.c.checker.record(node, if_slot::other);
|
||||
if this.c.checker.value_at(condition).magnitude != 0 {
|
||||
return this.skips(then) || (other == 0 && this.c.checker.has_flag(then, rf::leaves));
|
||||
}
|
||||
return other != 0 && this.skips(other);
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Lowers a statement.
|
||||
#! @param this The emitter.
|
||||
#! @param node The statement.
|
||||
pub def emitter::lower_statement(&mut this, node: u32): void {
|
||||
# A contract left out is no statement at all, so what follows takes no
|
||||
# place of it.
|
||||
return if this.c.checker.contracts_off && this.c.checker.kind_of(node) == nk::contract;
|
||||
this.at_node(node);
|
||||
match this.c.checker.kind_of(node) {
|
||||
nk::block => this.lower_block(node);
|
||||
nk::variable => this.local_statement(node);
|
||||
nk::if_stmt => this.if_statement(node);
|
||||
nk::if_const => this.if_const(node);
|
||||
nk::when_block => this.lower_block(this.c.checker.when_branch(node));
|
||||
nk::while_stmt => this.while_statement(node);
|
||||
nk::for_stmt => this.for_statement(node);
|
||||
nk::match_stmt => {
|
||||
val mark := this.begin_statement();
|
||||
this.lower_match(node, false);
|
||||
this.end_statement(mark);
|
||||
}
|
||||
nk::return_stmt => {
|
||||
val mark := this.begin_statement();
|
||||
val value := this.c.checker.left_of(node);
|
||||
if value != 0 && this.result != bt::void_type {
|
||||
this.return_value(value, mark);
|
||||
return;
|
||||
}
|
||||
# `return;` from a function giving more than nothing gives `void`, as a
|
||||
# `result<void, E>` holds it.
|
||||
if value == 0 && this.c.checker.unalias(this.result) != bt::void_type {
|
||||
val none := operand::none(bt::void_type);
|
||||
var made := this.convert(&none, this.result);
|
||||
this.return_made(&mut made, 0, mark);
|
||||
return;
|
||||
}
|
||||
# What gives nothing, returned from a function giving nothing, is done first.
|
||||
if value != 0 {
|
||||
this.value(value);
|
||||
}
|
||||
this.close_kept(mark.kept);
|
||||
this.leave_kept(0);
|
||||
this.close_locals(0, 0);
|
||||
this.build.ret(span::<u32>::empty());
|
||||
}
|
||||
nk::break_stmt, nk::continue_stmt => this.leave_loop(this.c.checker.kind_of(node) == nk::break_stmt);
|
||||
nk::contract => this.contract_statement(node);
|
||||
nk::expr_stmt => {
|
||||
val mark := this.begin_statement();
|
||||
val inner := this.c.checker.left_of(node);
|
||||
this.value(inner);
|
||||
this.end_statement(mark);
|
||||
# A call that ends the program, as `exit`, is never returned from.
|
||||
if this.ends_program(inner) {
|
||||
this.build.unreachable();
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
|
||||
#! Lowers a contract, as only a program checking contracts does: `expects` and
|
||||
#! `asserts` are checked where they are, and an `ensures` is an entry among the
|
||||
#! locals, which closing checks on each way out of its block, once what each
|
||||
#! `old` within it reads is kept.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `contract` node.
|
||||
def emitter::contract_statement(&mut this, node: u32): void {
|
||||
if this.c.checker.clause_of(node) != clause::ensures {
|
||||
val mark := this.begin_statement();
|
||||
this.check_contract(node);
|
||||
this.end_statement(mark);
|
||||
return;
|
||||
}
|
||||
this.keep_earlier(this.c.checker.left_of(node));
|
||||
val index := this.declare(node, location {}, true);
|
||||
this.locals[index].checks = true;
|
||||
}
|
||||
|
||||
#! Keeps what each `old(e)` in an `ensures`'s condition reads, where the
|
||||
#! `ensures` is reached, as a local the `old_value` node declares.
|
||||
#! @param this The emitter.
|
||||
#! @param condition The condition.
|
||||
def emitter::keep_earlier(&mut this, condition: u32): void {
|
||||
var walk := node_walk::new(condition);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
if this.c.checker.kind_of(at) == nk::old_value {
|
||||
val held := this.c.checker.type_at(at);
|
||||
val mark := this.begin_statement();
|
||||
val made := this.value_as(this.c.checker.left_of(at), held);
|
||||
val place := this.fresh_place(held);
|
||||
this.store(&place, &made);
|
||||
this.end_statement(mark);
|
||||
this.declare(at, place, false);
|
||||
continue;
|
||||
}
|
||||
this.c.scan_children(at, &mut walk);
|
||||
}
|
||||
}
|
||||
|
||||
#! Checks an `ensures` where its block is left, the name it gives what the
|
||||
#! function returns standing for that. Where the way out returns nothing, as
|
||||
#! where every way through the block returned before its end, it is never
|
||||
#! reached.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `contract` node.
|
||||
pub def emitter::check_ensures(&mut this, node: u32): void {
|
||||
return if this.build.ended();
|
||||
val bound := this.c.checker.record(node, contract_slot::bound);
|
||||
return if bound != 0 && !this.returning;
|
||||
val locals := this.scope_mark();
|
||||
if bound != 0 {
|
||||
val place := this.new_place(bound, this.returned.kind);
|
||||
this.store(&place, &this.returned);
|
||||
this.declare(bound, place, false);
|
||||
}
|
||||
val mark := this.begin_statement();
|
||||
this.check_contract(node);
|
||||
this.end_statement(mark);
|
||||
this.forget_locals(locals);
|
||||
}
|
||||
|
||||
#! Lowers a contract's check: where its condition holds, the program goes on,
|
||||
#! and where it does not, a block of its own, which nothing comes back from,
|
||||
#! makes the report and stops the program with it, so a contract that holds
|
||||
#! costs its condition and a branch.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `contract` node.
|
||||
def emitter::check_contract(&mut this, node: u32): void {
|
||||
val saved := this.build.pos;
|
||||
this.at_node(node);
|
||||
val fine := this.build.new_block();
|
||||
val failing := this.build.new_block();
|
||||
this.branch_on(this.c.checker.left_of(node), fine, failing);
|
||||
this.build.seal(failing);
|
||||
this.build.switch_to(failing);
|
||||
this.report_contract(node);
|
||||
this.build.seal(fine);
|
||||
this.build.switch_to(fine);
|
||||
this.build.pos = saved;
|
||||
}
|
||||
|
||||
#! Stops the program where a contract failed, saying which kind failed, what it
|
||||
#! checked, as written or as its text says, and what the condition's operands
|
||||
#! held, as `A pre-condition failed: b != 0, with b = 0.`
|
||||
#! @param this The emitter.
|
||||
#! @param node The `contract` node.
|
||||
def emitter::report_contract(&mut this, node: u32): void {
|
||||
val mark := this.begin_statement();
|
||||
val condition := this.c.checker.left_of(node);
|
||||
val text := this.c.checker.record(node, contract_slot::text);
|
||||
val lead := match this.c.checker.clause_of(node) {
|
||||
expects => "A pre-condition failed: ",
|
||||
asserts => "An assertion failed: ",
|
||||
ensures => "A post-condition failed: "
|
||||
};
|
||||
this.build.effect(op::format, span::<u32>::empty(), 0, 0);
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(lead), 0);
|
||||
|
||||
# Text written as a literal ends where the report's own full stop does.
|
||||
if text != 0 && this.c.checker.kind_of(text) == nk::string {
|
||||
var said := string::new(nothing {});
|
||||
this.decode_literal(this.c.checker.text_of(text), &mut said);
|
||||
if said.length != 0 && said[said.length - 1] == '.' {
|
||||
said.resize(said.length - 1);
|
||||
}
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(said.view()), 0);
|
||||
} else if text != 0 {
|
||||
val said := this.value(text);
|
||||
this.format_sentence(&said);
|
||||
} else {
|
||||
var written := string::new(nothing {});
|
||||
this.source_text(condition, &mut written);
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum(written.view()), 0);
|
||||
}
|
||||
this.write_values(condition, ", with ");
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), this.c.text_datum("."), 0);
|
||||
|
||||
# The program stops with the report, so nothing made here is closed.
|
||||
var report := this.owned_text(this.c.checker.type_at(node));
|
||||
this.adopt(&report);
|
||||
val index := this.c.layout_of(this.c.checker.unalias(report.kind));
|
||||
val data := this.c.field_at(index, 1);
|
||||
val count := this.c.field_at(index, 2);
|
||||
var words: u32[2] = [this.build.load(access::i64, report.address, data.offset), this.build.load(access::i64, report.address, count.offset)];
|
||||
this.build.effect(op::fault, words[0:2], 0, 0);
|
||||
this.build.unreachable();
|
||||
this.drop_kept(mark.kept);
|
||||
this.release(mark.cells);
|
||||
}
|
||||
|
||||
#! Writes a view of text into the text being made without a full stop it ends
|
||||
#! with, where the report's own ends it.
|
||||
#! @param this The emitter.
|
||||
#! @param text The view.
|
||||
def emitter::format_sentence(&mut this, text: &operand): void {
|
||||
var data := text.words[0];
|
||||
var length := text.words[1];
|
||||
if text.address != 0 {
|
||||
data = this.build.load(access::i64, text.address, 0);
|
||||
length = this.build.load(access::i64, text.address, word_size);
|
||||
}
|
||||
val held := this.build.variable(vt::word);
|
||||
this.build.assign(held, length);
|
||||
val looking := this.build.new_block();
|
||||
val dropping := this.build.new_block();
|
||||
val after := this.build.new_block();
|
||||
this.build.branch(this.build.binary(op::ne, length, this.build.word(0)), looking, after);
|
||||
this.build.seal(looking);
|
||||
this.build.switch_to(looking);
|
||||
val last := this.build.load(access::u8, this.build.binary(op::add, data, this.build.binary(op::sub, length, this.build.word(1))), 0);
|
||||
this.build.branch(this.build.binary(op::eq, last, this.build.word('.' as u64)), dropping, after);
|
||||
this.build.seal(dropping);
|
||||
this.build.switch_to(dropping);
|
||||
this.build.assign(held, this.build.binary(op::sub, length, this.build.word(1)));
|
||||
this.build.jump(after);
|
||||
this.build.seal(after);
|
||||
this.build.switch_to(after);
|
||||
var args: u32[2] = [data, this.build.read(held)];
|
||||
this.build.effect(op::format_bytes, args[0:2], 0, 0);
|
||||
}
|
||||
|
||||
#! Whether an expression ends the program: a call of `exit` or `abort`, which
|
||||
#! the checker treats as leaving.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @returns Whether it does.
|
||||
def emitter::ends_program(&mut this, node: u32): bool {
|
||||
return false if this.c.checker.kind_of(node) != nk::call;
|
||||
val decl := this.c.checker.ref_at(node);
|
||||
return false if decl == 0 || (decl & (ref_local | ref_type)) != 0;
|
||||
return false if !this.c.checker.decls.has(decl, df::intrinsic);
|
||||
val name := this.c.checker.names.text(this.c.checker.decls.names.p[decl]);
|
||||
return name == "exit" || name == "abort" || name == "fault";
|
||||
}
|
||||
|
||||
#! Lowers a local variable or constant. A constant known while compiling is no
|
||||
#! local at all, being written wherever it is named.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `variable` node.
|
||||
def emitter::local_statement(&mut this, node: u32): void {
|
||||
val value := this.c.checker.record(node, variable_slot::value);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
if this.c.checker.node_has(node, nf::constant) && this.c.checker.is_known(node) {
|
||||
return;
|
||||
}
|
||||
|
||||
# A constant the checker did not work out is evaluated: read where it is
|
||||
# once it is, while compiling, and where the program holds it as data after.
|
||||
if this.c.checker.node_has(node, nf::constant) {
|
||||
val key := node_key(this.table, node);
|
||||
if this.c.options.evaluating {
|
||||
val index := this.c.evaluation_of(key, 0, this.table, node, false);
|
||||
this.build.effect(op::ready, span::<u32>::empty(), index, 0);
|
||||
val offset: u64 = this.c.evaluations[index].offset;
|
||||
val address := this.build.value(op::global, span::<u32>::empty(), vt::word, 0, offset);
|
||||
this.declare(node, location { kind = kind, address = address }, false);
|
||||
return;
|
||||
}
|
||||
val datum := this.c.evaluated_datum(key, kind);
|
||||
if datum != no_block {
|
||||
val address := this.build.value(op::datum, span::<u32>::empty(), vt::word, datum, 0);
|
||||
this.declare(node, location { kind = kind, address = address }, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# A function declared within a block is named in its own body, so its local
|
||||
# is declared before its value is made.
|
||||
if this.c.checker.node_has(node, nf::function) {
|
||||
val place := this.new_place(node, kind);
|
||||
this.declare(node, place, false);
|
||||
val mark := this.begin_statement();
|
||||
val made := this.value(value);
|
||||
this.store(&place, &made);
|
||||
this.end_statement(mark);
|
||||
return;
|
||||
}
|
||||
|
||||
val mark := this.begin_statement();
|
||||
if value != 0 && this.refers_to_temporary(value) {
|
||||
this.bound_temporary(node, value, kind, mark);
|
||||
return;
|
||||
}
|
||||
# `undefined` leaves a cell as it is, and words, which cost nothing to give
|
||||
# a value, are zero. Running with its memory checked, the cell is marked as
|
||||
# never written, so reading it first stops the program.
|
||||
if value != 0 && this.c.checker.kind_of(value) == nk::undefined {
|
||||
val left := this.new_place(node, kind, ck::undefined);
|
||||
if left.held {
|
||||
this.zero(&left);
|
||||
} else if this.c.options.checks {
|
||||
this.build.forget(left.address, this.c.shape_of(kind).size);
|
||||
}
|
||||
this.end_statement(mark);
|
||||
this.declare(node, left, false);
|
||||
return;
|
||||
}
|
||||
val place := this.new_place(node, kind);
|
||||
if value == 0 {
|
||||
this.zero(&place);
|
||||
this.defaults(&place);
|
||||
} else {
|
||||
val made := this.value_as(value, kind);
|
||||
this.adopt(&made);
|
||||
this.store(&place, &made);
|
||||
}
|
||||
this.end_statement(mark);
|
||||
this.declare(node, place, this.c.checker.closes(kind));
|
||||
}
|
||||
|
||||
#! Whether a value is a reference to a value that is no place, as
|
||||
#! `&point { x = 1 }` or `&make()`, which the statement would hold.
|
||||
#! @param this The emitter.
|
||||
#! @param node The value's node.
|
||||
#! @returns Whether it is.
|
||||
def emitter::refers_to_temporary(&mut this, node: u32): bool {
|
||||
return false if this.c.checker.kind_of(node) != nk::reference;
|
||||
val inner := this.c.checker.left_of(node);
|
||||
return false if this.c.checker.has_flag(node, rf::relent);
|
||||
return !(this.c.checker.is_place(inner) && (!this.c.checker.is_known(inner) || this.stored(inner)));
|
||||
}
|
||||
|
||||
#! Lowers a local whose value is a reference to a value that is no place, which
|
||||
#! lives as long as the name does, as C++ has a temporary bound to a reference:
|
||||
#! held in a cell of its own, as a local declared just before the name is, and
|
||||
#! closed just after the name is.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `variable` node.
|
||||
#! @param value The `reference` node.
|
||||
#! @param kind The local's type.
|
||||
#! @param mark What the statement held before.
|
||||
def emitter::bound_temporary(&mut this, node: u32, value: u32, kind: u32, mark: statement_mark): void {
|
||||
val taken := this.c.checker.type_at(value);
|
||||
val referent := this.c.checker.types.first_of(this.c.checker.unalias(taken));
|
||||
val made := this.value_as(this.c.checker.left_of(value), referent);
|
||||
this.adopt(&made);
|
||||
val shape := this.c.shape_of(referent);
|
||||
val cell := this.build.code.add_cell(round_up(shape.size == 0 ? word_size : shape.size, word_size) as u32,
|
||||
shape.align < word_size ? word_size as u32 : shape.align as u32, ck::local, 0);
|
||||
val held := location { kind = referent, address = this.build.cell_address(cell), framed = true };
|
||||
this.store(&held, &made);
|
||||
val referred := this.reference_to(&held, taken);
|
||||
val converted := this.convert(&referred, kind);
|
||||
val place := this.new_place(node, kind);
|
||||
this.store(&place, &converted);
|
||||
this.end_statement(mark);
|
||||
this.declare(value, held, this.c.checker.closes(referent));
|
||||
this.declare(node, place, this.c.checker.closes(kind));
|
||||
}
|
||||
|
||||
#! Lowers `if`: the names its condition's tests give are declared before it runs,
|
||||
#! and close where it ends, but those an `is not` test gives the rest of the
|
||||
#! block where its branch always leaves.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `if_stmt` node.
|
||||
def emitter::if_statement(&mut this, node: u32): void {
|
||||
val condition := this.c.checker.left_of(node);
|
||||
val then := this.c.checker.record(node, if_slot::then);
|
||||
val other := this.c.checker.record(node, if_slot::other);
|
||||
val mark := this.begin_statement();
|
||||
val locals := this.scope_mark();
|
||||
this.declare_tests(condition);
|
||||
|
||||
val then_block := this.build.new_block();
|
||||
val join := this.build.new_block();
|
||||
val else_block := other != 0 ? this.build.new_block() : join;
|
||||
this.branch_on(condition, then_block, else_block);
|
||||
this.lower_branches(then, other, then_block, else_block, join);
|
||||
|
||||
# The names an `is not` test gives stay for the rest of the block, where the
|
||||
# branch it guards always leaves.
|
||||
val gives_rest := other == 0 && this.c.checker.has_flag(then, rf::leaves) && this.binds_where(condition, false);
|
||||
if !gives_rest {
|
||||
this.end_scope(locals, 0);
|
||||
}
|
||||
this.end_statement(mark);
|
||||
}
|
||||
|
||||
#! Lowers `if const`: the branch the checker decided it takes, or where only the
|
||||
#! program running decides it, as in a generic's instance whose condition the
|
||||
#! checker left unknown, both, by the condition's value.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `if_const` node.
|
||||
def emitter::if_const(&mut this, node: u32): void {
|
||||
val condition := this.c.checker.left_of(node);
|
||||
val then := this.c.checker.record(node, if_slot::then);
|
||||
val other := this.c.checker.record(node, if_slot::other);
|
||||
if this.c.checker.is_known(condition) {
|
||||
val taken := this.c.checker.value_at(condition).magnitude != 0 ? then : other;
|
||||
this.lower_block(taken);
|
||||
return;
|
||||
}
|
||||
|
||||
# A condition evaluated decides the branch as one the checker decided does.
|
||||
if this.c.evaluated.decisions.get(&node_key(this.table, node)) is some(holds) {
|
||||
val taken := holds ? then : other;
|
||||
this.lower_block(taken);
|
||||
return;
|
||||
}
|
||||
|
||||
val mark := this.begin_statement();
|
||||
val then_block := this.build.new_block();
|
||||
val join := this.build.new_block();
|
||||
val else_block := other != 0 ? this.build.new_block() : join;
|
||||
val cond := this.value_as(condition, bt::boolean);
|
||||
this.build.branch(cond.words[0], then_block, else_block);
|
||||
this.lower_branches(then, other, then_block, else_block, join);
|
||||
this.end_statement(mark);
|
||||
}
|
||||
|
||||
#! Lowers the branches of an `if` its condition branched to, each going on
|
||||
#! where they join, and goes on there.
|
||||
#! @param this The emitter.
|
||||
#! @param then The block taken where the condition holds.
|
||||
#! @param other The one taken where it does not, or 0.
|
||||
#! @param then_block Where the first begins.
|
||||
#! @param else_block Where the second begins, or `join`.
|
||||
#! @param join Where they join.
|
||||
def emitter::lower_branches(&mut this, then: u32, other: u32, then_block: u32, else_block: u32, join: u32): void {
|
||||
this.build.seal(then_block);
|
||||
if other != 0 {
|
||||
this.build.seal(else_block);
|
||||
}
|
||||
this.build.switch_to(then_block);
|
||||
this.lower_block(then);
|
||||
this.build.jump(join);
|
||||
if other != 0 {
|
||||
this.build.switch_to(else_block);
|
||||
this.lower_block(other);
|
||||
this.build.jump(join);
|
||||
}
|
||||
this.build.seal(join);
|
||||
this.build.switch_to(join);
|
||||
}
|
||||
|
||||
#! Lowers `while`. Each pass, and each `continue`, closes what the condition
|
||||
#! made, and the names its tests gave, before making them again.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `while_stmt` node.
|
||||
def emitter::while_statement(&mut this, node: u32): void {
|
||||
val condition := this.c.checker.left_of(node);
|
||||
val body := this.c.checker.right_of(node);
|
||||
val head := this.build.new_block();
|
||||
val exit := this.build.new_block();
|
||||
val next := this.build.new_block();
|
||||
this.build.jump(head);
|
||||
this.build.switch_to(head);
|
||||
|
||||
val mark := this.begin_statement();
|
||||
val locals := this.scope_mark();
|
||||
this.declare_tests(condition);
|
||||
val inside := this.build.new_block();
|
||||
this.branch_on(condition, inside, exit);
|
||||
this.build.seal(inside);
|
||||
|
||||
this.build.switch_to(inside);
|
||||
this.safepoint();
|
||||
this.loops.append(loop_info { exit = exit, next = next, locals = this.locals.length, kept = this.kept.length });
|
||||
this.lower_block(body);
|
||||
this.loops.truncate();
|
||||
this.build.jump(next);
|
||||
this.build.seal(next);
|
||||
|
||||
this.build.switch_to(next);
|
||||
this.close_locals(locals, 0);
|
||||
this.leave_kept(mark.kept);
|
||||
|
||||
# A `break` leaves with the names the test gave, which close here, and what the
|
||||
# condition made closes where the statement ends.
|
||||
this.close_loop(head, exit);
|
||||
this.end_scope(locals, 0);
|
||||
this.end_statement(mark);
|
||||
}
|
||||
|
||||
#! Lowers `break` or `continue`: what the statements and scopes of the loop's
|
||||
#! body hold is closed, and control goes to the loop's exit or its next pass.
|
||||
#! @param this The emitter.
|
||||
#! @param leaves Whether it is `break`.
|
||||
def emitter::leave_loop(&mut this, leaves: bool): void {
|
||||
return if this.loops.length == 0;
|
||||
val held: loop_info = this.loops.last();
|
||||
this.leave_kept(held.kept);
|
||||
this.close_locals(held.locals, 0);
|
||||
this.build.jump(leaves ? held.exit : held.next);
|
||||
}
|
||||
|
||||
#! Lowers a condition as a branch: where it holds, to one block, and to another
|
||||
#! where it does not. `&&` and `||` go to the second only where the first does
|
||||
#! not decide.
|
||||
#! @param this The emitter.
|
||||
#! @param node The condition.
|
||||
#! @param then Where it goes where the condition holds.
|
||||
#! @param other Where it goes where it does not.
|
||||
pub def emitter::branch_on(&mut this, node: u32, then: u32, other: u32): void {
|
||||
if this.c.checker.is_known(node) {
|
||||
this.build.jump(this.c.checker.value_at(node).magnitude != 0 ? then : other);
|
||||
return;
|
||||
}
|
||||
if this.c.checker.kind_of(node) == nk::binary {
|
||||
val operator := this.c.checker.token_kind(node);
|
||||
if operator == tk::logand || operator == tk::logor {
|
||||
val middle := this.build.new_block();
|
||||
if operator == tk::logand {
|
||||
this.branch_on(this.c.checker.left_of(node), middle, other);
|
||||
} else {
|
||||
this.branch_on(this.c.checker.left_of(node), then, middle);
|
||||
}
|
||||
this.build.seal(middle);
|
||||
this.build.switch_to(middle);
|
||||
this.branch_on(this.c.checker.right_of(node), then, other);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if this.c.checker.kind_of(node) == nk::unary && this.c.checker.token_kind(node) == tk::negate {
|
||||
this.branch_on(this.c.checker.left_of(node), other, then);
|
||||
return;
|
||||
}
|
||||
|
||||
val made := this.value_as(node, bt::boolean);
|
||||
this.build.branch(made.words[0], then, other);
|
||||
}
|
||||
|
||||
#! Declares the locals the tests of a condition name their values in, before
|
||||
#! any of it runs, since `&&` and `||` may leave a test that would set one
|
||||
#! unrun. One that closes starts zeroed, so closing it where it was never given
|
||||
#! closes nothing.
|
||||
#! @param this The emitter.
|
||||
#! @param node The condition.
|
||||
pub def emitter::declare_tests(&mut this, node: u32): void {
|
||||
return if node == 0;
|
||||
match this.c.checker.kind_of(node) {
|
||||
nk::is_test => {
|
||||
val binding := this.c.checker.named_binding(this.c.checker.record(node, is_slot::binding));
|
||||
return if binding == 0;
|
||||
val named := this.c.checker.kind_of(binding) == nk::reference ? this.c.checker.left_of(binding) : binding;
|
||||
return if !this.c.checker.has_flag(named, rf::narrowed) || this.local_of(named) != 0;
|
||||
val kind := this.c.checker.type_at(named);
|
||||
val place := this.new_place(named, kind);
|
||||
val closes := this.c.checker.closes(kind);
|
||||
if closes {
|
||||
this.zero(&place);
|
||||
}
|
||||
this.declare(named, place, closes);
|
||||
}
|
||||
nk::binary => {
|
||||
val operator := this.c.checker.token_kind(node);
|
||||
if operator == tk::logand || operator == tk::logor {
|
||||
this.declare_tests(this.c.checker.left_of(node));
|
||||
this.declare_tests(this.c.checker.right_of(node));
|
||||
}
|
||||
}
|
||||
nk::unary => {
|
||||
if this.c.checker.token_kind(node) == tk::negate {
|
||||
this.declare_tests(this.c.checker.left_of(node));
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a condition's tests name a value where it holds as `positive` says,
|
||||
#! through `&&`, `||`, and `!`.
|
||||
#! @param this The emitter.
|
||||
#! @param node The condition.
|
||||
#! @param positive Whether the condition holds where the value is named.
|
||||
#! @returns Whether they do.
|
||||
pub def emitter::binds_where(&mut this, node: u32, positive: bool): bool {
|
||||
return false if node == 0;
|
||||
match this.c.checker.kind_of(node) {
|
||||
nk::is_test => {
|
||||
return this.c.checker.named_binding(this.c.checker.record(node, is_slot::binding)) != 0
|
||||
&& this.c.checker.node_has(node, nf::negated) != positive;
|
||||
}
|
||||
nk::binary => {
|
||||
val operator := this.c.checker.token_kind(node);
|
||||
return false if !((operator == tk::logand && positive) || (operator == tk::logor && !positive));
|
||||
return this.binds_where(this.c.checker.left_of(node), positive)
|
||||
|| this.binds_where(this.c.checker.right_of(node), positive);
|
||||
}
|
||||
nk::unary => {
|
||||
return this.c.checker.token_kind(node) == tk::negate && this.binds_where(this.c.checker.left_of(node), !positive);
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
#! @file storage.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains where a function's locals are held, worked out before its body is
|
||||
#! lowered from where its references are made, as `escapes.sakura` finds it. A
|
||||
#! local is held in variables of the builder unless a reference to it is made,
|
||||
#! and then in a cell of the frame, which the reference refers to while the
|
||||
#! local lives. Nothing is moved to the heap: a reference that outlives what it
|
||||
#! refers to is the program's mistake, which the checker warns of where it is
|
||||
#! sure of it.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! Works out where the locals of a function's body are held, given what its
|
||||
#! parameters are.
|
||||
#! @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.
|
||||
#! @param body The body's node.
|
||||
pub def emitter::plan_function(&mut this, decl: u32, params: u32, kind: u32, body: u32): void {
|
||||
var s := escape_scan::new(scratch());
|
||||
s.result = this.c.checker.types.first_of(kind);
|
||||
this.c.scan_params(&mut s, params, kind, this.c.packs_last(decl));
|
||||
this.c.scan_body(&mut s, body);
|
||||
this.apply_plan(&mut s, body);
|
||||
}
|
||||
|
||||
#! Works out where the locals of an anonymous function's body are held. What it
|
||||
#! captures is the function's around it, reached through its environment, as
|
||||
#! what a parameter refers to is the caller's.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `lambda` node.
|
||||
pub def emitter::plan_lambda(&mut this, node: u32): void {
|
||||
var s := escape_scan::new(scratch());
|
||||
val kind := this.c.checker.type_at(node);
|
||||
s.result = this.c.checker.types.first_of(kind);
|
||||
var captures := this.captures_of(node);
|
||||
for &each in captures {
|
||||
val outer := s.graph.new_spot(spot_kind::outer, each, 0);
|
||||
val inner := s.graph.new_spot(spot_kind::inner, each, 0);
|
||||
s.graph.add(s.graph.held_by(outer), inner);
|
||||
s.graph.add(s.graph.held_by(inner), inner);
|
||||
s.slots.set(each, outer);
|
||||
}
|
||||
this.c.scan_params(&mut s, this.c.checker.left_of(node), kind, false);
|
||||
val body := this.c.checker.record(node, lambda_slot::body);
|
||||
this.c.scan_body(&mut s, body);
|
||||
this.apply_plan(&mut s, body);
|
||||
}
|
||||
|
||||
#! Works out where the locals of a value evaluated into a global are held,
|
||||
#! which keeps what it holds.
|
||||
#! @param this The emitter.
|
||||
#! @param value The value's node.
|
||||
pub def emitter::plan_storage(&mut this, value: u32): void {
|
||||
var s := escape_scan::new(scratch());
|
||||
s.graph.keep(this.c.value_flow(&mut s, value));
|
||||
this.apply_plan(&mut s, value);
|
||||
}
|
||||
|
||||
#! Holds in a cell each local a reference is made to. What is within an
|
||||
#! anonymous function is left to its own plan.
|
||||
#! @param this The emitter.
|
||||
#! @param s The scan.
|
||||
#! @param body The body scanned.
|
||||
def emitter::apply_plan(&mut this, s: &mut escape_scan, body: u32): void {
|
||||
for at in 0..s.graph.spots.length {
|
||||
val held: spot = s.graph.spots[at];
|
||||
continue if held.depth != 0;
|
||||
if held.kind == spot_kind::local && held.taken {
|
||||
this.raise(held.node, held_as::cell);
|
||||
}
|
||||
}
|
||||
this.check_plan(s, body);
|
||||
}
|
||||
|
||||
#! Holds in a cell any local a reference is made to that the scan did not see
|
||||
#! made, which it always sees, so no reference refers to a local held in
|
||||
#! variables: the operand of `&`, what a parameter taking a reference is given,
|
||||
#! a method's receiver, the operands of an operator's method, what a loop
|
||||
#! walks, a union a match names a field of by `&`, and what an anonymous
|
||||
#! function captures.
|
||||
#! @param this The emitter.
|
||||
#! @param s The scan.
|
||||
#! @param body The body scanned.
|
||||
def emitter::check_plan(&mut this, s: &mut escape_scan, body: u32): void {
|
||||
var walk := node_walk::new(body);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
# A contract left out refers to nothing.
|
||||
continue if this.c.checker.contracts_off && this.c.checker.kind_of(at) == nk::contract;
|
||||
match this.c.checker.kind_of(at) {
|
||||
nk::reference => {
|
||||
val inner := this.c.checker.left_of(at);
|
||||
if this.c.checker.is_place(inner) && (!this.c.checker.is_known(inner) || this.stored(inner)) {
|
||||
this.check_taken(s, inner);
|
||||
}
|
||||
}
|
||||
nk::lambda => {
|
||||
var captures := this.captures_of(at);
|
||||
for &each in captures {
|
||||
this.check_local(s, each);
|
||||
}
|
||||
}
|
||||
nk::member => {
|
||||
# A property's accessors are given a reference to the value.
|
||||
if this.property_of(at) != 0 {
|
||||
this.check_taken(s, this.c.checker.left_of(at));
|
||||
}
|
||||
}
|
||||
nk::index => {
|
||||
# So is `at`, but for what the language indexes itself.
|
||||
val ref := this.c.checker.ref_at(at);
|
||||
val through := this.c.checker.unalias(this.c.checker.reached(this.object_type(this.c.checker.left_of(at))));
|
||||
val kind := this.c.checker.types.kind_of(through);
|
||||
if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::function
|
||||
&& kind != ty::fixed && kind != ty::pointer && kind != ty::pack && this.c.viewed(through) == 0 {
|
||||
this.check_taken(s, this.c.checker.left_of(at));
|
||||
}
|
||||
}
|
||||
nk::binary, nk::assign => {
|
||||
if this.operator_method(at) != 0 {
|
||||
this.check_taken(s, this.c.checker.left_of(at));
|
||||
this.check_taken(s, this.c.checker.right_of(at));
|
||||
}
|
||||
}
|
||||
nk::unary => {
|
||||
val token := this.c.checker.token_kind(at);
|
||||
if (token == tk::minus || token == tk::bitnot) && this.operator_decl(at) != 0 {
|
||||
this.check_taken(s, this.c.checker.left_of(at));
|
||||
}
|
||||
}
|
||||
nk::for_stmt => {
|
||||
val iterated := this.c.checker.record(at, for_slot::iterated);
|
||||
val through := this.c.checker.unalias(this.c.checker.type_at(iterated));
|
||||
if this.c.checker.record(at, for_slot::end) == 0 && !this.c.checker.node_has(at, nf::constant)
|
||||
&& this.c.viewed(through) == 0 && this.c.checker.types.kind_of(through) != ty::pointer {
|
||||
this.check_taken(s, iterated);
|
||||
}
|
||||
}
|
||||
nk::is_test => {
|
||||
val binding := this.c.checker.record(at, is_slot::binding);
|
||||
if binding != 0 && this.c.checker.kind_of(binding) == nk::reference {
|
||||
this.check_taken(s, this.c.checker.left_of(at));
|
||||
}
|
||||
}
|
||||
nk::match_stmt, nk::match_value => {
|
||||
if this.c.refers_in_arm(at) {
|
||||
this.check_taken(s, this.c.checker.left_of(at));
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
if this.c.checker.has_flag(at, rf::borrowed) {
|
||||
this.check_taken(s, at);
|
||||
}
|
||||
if this.c.checker.kind_of(at) != nk::lambda {
|
||||
this.c.scan_children(at, &mut walk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Holds in a cell the local whose own storage a place is within, past fields
|
||||
#! and the elements of fixed arrays, where a reference to it is made that the
|
||||
#! scan did not see.
|
||||
#! @param this The emitter.
|
||||
#! @param s The scan.
|
||||
#! @param node The place's node.
|
||||
def emitter::check_taken(&mut this, s: &mut escape_scan, node: u32): void {
|
||||
var at := node;
|
||||
var steps := 0;
|
||||
while at != 0 && steps < 64 {
|
||||
steps++;
|
||||
val kind := this.c.checker.kind_of(at);
|
||||
if kind == nk::member {
|
||||
val ref := this.c.checker.ref_at(at);
|
||||
return if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.c.checker.decls.kinds.p[ref] != dk::field;
|
||||
val inner := this.c.checker.left_of(at);
|
||||
return if this.c.is_reference_type(this.c.checker.type_at(inner));
|
||||
at = inner;
|
||||
continue;
|
||||
}
|
||||
if kind == nk::index {
|
||||
val inner := this.c.checker.left_of(at);
|
||||
return if this.c.checker.types.kind_of(this.c.checker.unalias(this.c.checker.type_at(inner))) != ty::fixed;
|
||||
at = inner;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return if at == 0 || this.c.checker.kind_of(at) != nk::name;
|
||||
val ref := this.c.checker.ref_at(at);
|
||||
return if (ref & ref_local) == 0 || this.c.is_reference_type(this.c.checker.type_at(at));
|
||||
this.check_local(s, ref & ref_mask);
|
||||
}
|
||||
|
||||
#! Holds a local in a cell where the scan did not see a reference to it made.
|
||||
#! @param this The emitter.
|
||||
#! @param s The scan.
|
||||
#! @param local The node declaring the local.
|
||||
def emitter::check_local(&mut this, s: &mut escape_scan, local: u32): void {
|
||||
if s.slots.get(&local) is some(at) {
|
||||
return if s.graph.spots[at as u64].taken || s.graph.spots[at as u64].kind != spot_kind::local;
|
||||
}
|
||||
this.raise(local, held_as::cell);
|
||||
}
|
||||
|
||||
#! Holds a local at least as the way given: a cell over variables.
|
||||
#! @param this The emitter.
|
||||
#! @param local The node declaring the local.
|
||||
#! @param how How it must be held.
|
||||
def emitter::raise(&mut this, local: u32, how: held_as): void {
|
||||
val held := this.storage.get_or(&local, held_as::words);
|
||||
if (how as u8) > (held as u8) {
|
||||
this.storage.set(local, how);
|
||||
}
|
||||
}
|
||||
|
||||
#! The method an operator calls, where the language does not operate on its
|
||||
#! operands itself: the one the checker chose for arithmetic, or the `equals`
|
||||
#! or `compare` of what implements `equatable` or `ordered`, or 0.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `binary` or `assign` node.
|
||||
#! @returns The method's declaration, or 0.
|
||||
pub def emitter::operator_method(&mut this, node: u32): u32 {
|
||||
return 0 if this.c.checker.kind_of(node) == nk::unary;
|
||||
val operator := this.c.checker.token_kind(node);
|
||||
return 0 if operator == tk::logand || operator == tk::logor || operator == tk::assign;
|
||||
val chosen := this.operator_decl(node);
|
||||
return chosen if chosen != 0;
|
||||
val kind := this.c.checker.type_at(this.c.checker.left_of(node));
|
||||
val plain := this.c.checker.unalias(this.c.layout_type(this.c.checker.read_type(kind)));
|
||||
val named := this.c.checker.named_kind(plain);
|
||||
return 0 if named != dk::struct_type && named != dk::union_type;
|
||||
|
||||
if operator.compares() {
|
||||
val equal := operator == tk::equals || operator == tk::noteq;
|
||||
return this.pick_method(plain, equal ? word::equals : word::compare, true);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,487 @@
|
||||
#! @file values.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the lowering of expressions to their values: what each kind of
|
||||
#! node gives, the constants the checker worked out, and the conversions the
|
||||
#! checker recorded, by which a value of one type is taken where another is
|
||||
#! expected.
|
||||
|
||||
import sakura::analyzer;
|
||||
module sakura::compiler;
|
||||
|
||||
|
||||
#! The value of an expression as the type it is used as: the type the checker
|
||||
#! converts it to, or its own.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @returns The value.
|
||||
pub def emitter::value(&mut this, node: u32): operand {
|
||||
var to := this.c.checker.converted(node);
|
||||
if this.c.checker.is_known(node) && this.c.is_scalar_type(to) {
|
||||
# An integer constant past what an `i64` holds is unsigned, as `1 << 63` is.
|
||||
val held := this.c.checker.value_at(node);
|
||||
if this.c.checker.types.kind_of(to) == ty::untyped_int && !held.floating && !held.negative
|
||||
&& held.magnitude > 0x7FFFFFFFFFFFFFFF {
|
||||
to = bt::uint64;
|
||||
}
|
||||
return this.known(&held, to);
|
||||
}
|
||||
val own := this.raw(node);
|
||||
return this.convert(&own, to);
|
||||
}
|
||||
|
||||
#! The value of an expression as its own type, before any conversion.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @returns The value.
|
||||
pub def emitter::value_of_type(&mut this, node: u32): operand {
|
||||
val own := this.c.checker.type_at(node);
|
||||
if this.c.checker.is_known(node) && this.c.is_scalar_type(own) {
|
||||
return this.known(&this.c.checker.value_at(node), own);
|
||||
}
|
||||
return this.raw(node);
|
||||
}
|
||||
|
||||
#! The value of an expression converted to a type.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @param to The type.
|
||||
#! @returns The value.
|
||||
pub def emitter::value_as(&mut this, node: u32, to: u32): operand {
|
||||
if this.c.checker.is_known(node) && this.c.is_scalar_type(to) {
|
||||
return this.known(&this.c.checker.value_at(node), to);
|
||||
}
|
||||
val made := this.value(node);
|
||||
return this.convert(&made, to);
|
||||
}
|
||||
|
||||
#! A constant the checker worked out, as a value of a type.
|
||||
#! @param this The emitter.
|
||||
#! @param value The constant.
|
||||
#! @param kind The type, a number, `bool`, enum, or pointer.
|
||||
#! @returns The value.
|
||||
pub def emitter::known(&mut this, value: &constant, kind: u32): operand {
|
||||
val held := this.c.shape_of(kind);
|
||||
if held.kind == lk::floating {
|
||||
var real := value.as_real();
|
||||
if held.bits <= 32 {
|
||||
real = (real as f32) as f64;
|
||||
}
|
||||
return operand::one(kind, this.build.real(bits_of_real(real)));
|
||||
}
|
||||
var bits := value.floating ? (value.real < 0.0 ? 0 - ((0.0 - value.real) as u64) : value.real as u64) : value.bits();
|
||||
return operand::one(kind, this.build.word(bits));
|
||||
}
|
||||
|
||||
#! The value of an expression as its own type, of each kind of node. What it
|
||||
#! lowers is from the node, as what its operands lower is from theirs, and
|
||||
#! what is lowered after it is from where it was before.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @returns The value.
|
||||
pub def emitter::raw(&mut this, node: u32): operand {
|
||||
val saved := this.build.pos;
|
||||
this.at_node(node);
|
||||
val made := this.raw_node(node);
|
||||
this.build.pos = saved;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The value of an expression as its own type, by the kind of its node.
|
||||
#! @param this The emitter.
|
||||
#! @param node The expression.
|
||||
#! @returns The value.
|
||||
def emitter::raw_node(&mut this, node: u32): operand {
|
||||
val kind := this.c.checker.type_at(node);
|
||||
if this.c.checker.has_flag(node, rf::reflected) {
|
||||
return this.reflected_value(node);
|
||||
}
|
||||
|
||||
match this.c.checker.kind_of(node) {
|
||||
nk::number, nk::character, nk::boolean => return this.known(&this.c.checker.value_at(node), kind);
|
||||
nk::string => {
|
||||
# Text given to what is not text, as a union holding a view, is made as the
|
||||
# view it settles as, which is then converted.
|
||||
val to := this.c.checker.converted(node);
|
||||
val own := this.c.is_string(to) || this.c.viewed(to) != 0 ? to : this.c.checker.settle(kind);
|
||||
return this.text_literal(node, own);
|
||||
}
|
||||
nk::istring => return this.interpolated(node);
|
||||
nk::null_lit => return this.zero_value(this.c.checker.converted(node));
|
||||
nk::name, nk::path => return this.name_value(node);
|
||||
nk::unary => return this.unary(node);
|
||||
nk::postfix => return this.step(node, this.c.checker.left_of(node), false);
|
||||
nk::reference => return this.reference(node);
|
||||
nk::binary => return this.binary(node);
|
||||
nk::assign => return this.assign(node);
|
||||
nk::ternary => return this.ternary(node);
|
||||
nk::fallback => return this.fallback(node);
|
||||
nk::call => {
|
||||
val made := this.call(node);
|
||||
this.keep(&made);
|
||||
return made;
|
||||
}
|
||||
nk::index => {
|
||||
if this.indexes_through_at(node) {
|
||||
return this.indexed_reference(node);
|
||||
}
|
||||
val place := this.place_of(node);
|
||||
return this.load(&place);
|
||||
}
|
||||
# `x.(d)` reads the field or property a description names.
|
||||
nk::member_of => return this.member_value(node);
|
||||
# `value_of(d)::<i64>` is the instance its type arguments make.
|
||||
nk::type_args => return this.function_value(node, this.c.checker.ref_at(node));
|
||||
nk::slice => return this.slice(node);
|
||||
nk::member => return this.member_value(node);
|
||||
nk::cast => return this.cast(node);
|
||||
nk::is_test => return this.is_test(node);
|
||||
nk::construct => {
|
||||
val made := this.construct(node);
|
||||
this.keep(&made);
|
||||
return made;
|
||||
}
|
||||
nk::array => {
|
||||
val made := this.array_literal(node, this.c.checker.converted(node));
|
||||
this.keep(&made);
|
||||
return made;
|
||||
}
|
||||
nk::lambda => return this.lambda_value(node);
|
||||
nk::match_value => return this.match_value(node);
|
||||
nk::range => return this.range_value(node);
|
||||
# What `e` was is kept where its `ensures` was reached.
|
||||
nk::old_value => {
|
||||
val local := this.local_of(node);
|
||||
if local != 0 {
|
||||
val held: location = this.locals[local as u64 - 1].place;
|
||||
return this.load(&held);
|
||||
}
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
this.problem(node, "The expression is not lowered.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
|
||||
#! The zero value of a type: zero words, or a zeroed cell.
|
||||
#! @param this The emitter.
|
||||
#! @param kind The type.
|
||||
#! @returns The value.
|
||||
pub def emitter::zero_value(&mut this, kind: u32): operand {
|
||||
val held := this.c.shape_of(kind);
|
||||
if in_memory(&held) {
|
||||
val address := this.temporary_of(kind);
|
||||
this.build.clear(address, held.size);
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
var made := operand { kind = kind, count = word_count(&held) };
|
||||
for i in 0..made.count {
|
||||
made.words[i] = word_type(&held) == vt::real ? this.build.real(0) : this.build.word(0);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The value a name or path gives: a local, a global, a constant, an enum
|
||||
#! member, a constant parameter, or a function.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `name` or `path` node.
|
||||
#! @returns The value.
|
||||
pub def emitter::name_value(&mut this, node: u32): operand {
|
||||
val ref := this.c.checker.ref_at(node);
|
||||
val kind := this.c.checker.type_at(node);
|
||||
if (ref & ref_local) != 0 {
|
||||
val place := this.place_of(node);
|
||||
return this.load(&place);
|
||||
}
|
||||
if (ref & ref_type) != 0 {
|
||||
return this.known_argument(ref & ref_mask, kind);
|
||||
}
|
||||
if ref == 0 {
|
||||
this.problem(node, "The name refers to nothing.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
|
||||
match this.c.checker.decls.kinds.p[ref] {
|
||||
dk::function => return this.function_value(node, ref);
|
||||
dk::variable => {
|
||||
val place := this.global_place(ref);
|
||||
return this.load(&place);
|
||||
}
|
||||
dk::enum_member => {
|
||||
var value := constant {};
|
||||
this.c.checker.decl_value(ref, &mut value);
|
||||
return this.known(&value, kind);
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
this.problem(node, "The name is no value.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
|
||||
#! The value of a constant type argument a constant parameter names.
|
||||
#! @param this The emitter.
|
||||
#! @param arg The type argument, a constant.
|
||||
#! @param kind The type of its value.
|
||||
#! @returns The value.
|
||||
pub def emitter::known_argument(&mut this, arg: u32, kind: u32): operand {
|
||||
if this.c.checker.types.kind_of(arg) != ty::value {
|
||||
this.problem(0, "A constant parameter is given no value.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
return this.known_object(this.c.checker.types.second_of(arg), kind);
|
||||
}
|
||||
|
||||
#! A value the checker worked out, as a value of a type: a number, text, a
|
||||
#! struct, union, or array of them, `null`, a function, or the address of a
|
||||
#! constant.
|
||||
#! @param this The emitter.
|
||||
#! @param id The value's ID among the checker's known values.
|
||||
#! @param kind The type.
|
||||
#! @returns The value.
|
||||
pub def emitter::known_object(&mut this, id: u32, kind: u32): operand {
|
||||
val held: known = this.c.checker.known_at(id);
|
||||
val shape := this.c.shape_of(kind);
|
||||
match held.kind {
|
||||
kk::number, kk::real => return this.known(&held.value, kind);
|
||||
kk::null_lit => return this.zero_value(kind);
|
||||
kk::text => return this.text_value(this.c.checker.known_text(id), kind);
|
||||
kk::function => {
|
||||
val index := this.c.function_of(held.decl, 0);
|
||||
this.c.program.functions[index].taken = true;
|
||||
this.c.names(this.function, index);
|
||||
return operand::two(kind, this.build.word(index as u64 + 1), this.build.word(0));
|
||||
}
|
||||
kk::address => {
|
||||
val place := this.global_place(held.decl);
|
||||
return this.reference_to(&place, kind);
|
||||
}
|
||||
kk::fields => {
|
||||
val address := this.temporary_of(kind);
|
||||
this.build.clear(address, shape.size);
|
||||
val whole := location { kind = kind, address = address, framed = true };
|
||||
val plain := this.c.checker.unalias(kind);
|
||||
if shape.kind == lk::fixed {
|
||||
val element := this.c.checker.types.first_of(plain);
|
||||
val inner := this.c.shape_of(element);
|
||||
for i in 0..this.c.checker.known_count(id) {
|
||||
val part := this.known_object(this.c.checker.known_part(id, i as u32), element);
|
||||
val at := this.within(&whole, i as u64 * inner.size, element);
|
||||
this.store(&at, &part);
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
val index := this.c.layout_of(plain);
|
||||
for i in 0..this.c.checker.known_count(id) {
|
||||
break if i >= shape.count as u64;
|
||||
val field := this.c.field_at(index, i as u32);
|
||||
val part := this.known_object(this.c.checker.known_part(id, i as u32), field.type_id);
|
||||
val at := this.within(&whole, field.offset, field.type_id);
|
||||
this.store(&at, &part);
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
kk::held => {
|
||||
val address := this.temporary_of(kind);
|
||||
this.build.clear(address, shape.size);
|
||||
val tag := held.value.magnitude as u32;
|
||||
if shape.niche == 0 {
|
||||
this.build.store(access::i64, address, 0, this.build.word(tag as u64));
|
||||
}
|
||||
val index := this.c.layout_of(this.c.checker.unalias(kind));
|
||||
if tag < shape.count && this.c.checker.known_count(id) != 0 {
|
||||
val field := this.c.field_at(index, tag);
|
||||
val part := this.known_object(this.c.checker.known_part(id, 0), field.type_id);
|
||||
val at := location { kind = field.type_id, address = address, offset = field.offset, framed = true };
|
||||
this.store(&at, &part);
|
||||
}
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
this.problem(0, "A constant argument is not worked out.");
|
||||
return this.zero_value(kind);
|
||||
}
|
||||
|
||||
#! The value text written as a literal is, as the type it is used as: a view of
|
||||
#! the bytes it was written with, which stand as long as the program runs, or a
|
||||
#! `string` holding a copy of them it owns.
|
||||
#! @param this The emitter.
|
||||
#! @param node The `string` node.
|
||||
#! @param kind The type it is used as.
|
||||
#! @returns The value.
|
||||
pub def emitter::text_literal(&mut this, node: u32, kind: u32): operand {
|
||||
var text := string::new(nothing {});
|
||||
this.decode_literal(this.c.checker.text_of(node), &mut text);
|
||||
return this.text_value(text.view(), kind);
|
||||
}
|
||||
|
||||
#! Text as a value of a type: a view of a datum holding its bytes, or a
|
||||
#! `string` holding a copy of them.
|
||||
#! @param this The emitter.
|
||||
#! @param text The bytes.
|
||||
#! @param kind The type: a view of bytes, or `string`.
|
||||
#! @returns The value.
|
||||
pub def emitter::text_value(&mut this, text: strview, kind: u32): operand {
|
||||
val datum := this.c.text_datum(text);
|
||||
if this.c.is_string(kind) {
|
||||
this.build.effect(op::format, span::<u32>::empty(), 0, 0);
|
||||
this.build.effect(op::format_text, span::<u32>::empty(), datum, 0);
|
||||
return this.owned_text(kind);
|
||||
}
|
||||
|
||||
val address := this.temporary_of(kind);
|
||||
val data := text.length == 0 ? this.build.word(0) : this.build.value(op::datum, span::<u32>::empty(), vt::word, datum, 0);
|
||||
this.build.store(access::i64, address, 0, data);
|
||||
this.build.store(access::i64, address, word_size, this.build.word(text.length));
|
||||
return operand::at(kind, address);
|
||||
}
|
||||
|
||||
#! A value of a type taken as another, as the checker converts it: widened,
|
||||
#! read through references, given to a union, referred to as a trait, or lent
|
||||
#! as a view.
|
||||
#! @param this The emitter.
|
||||
#! @param value The value.
|
||||
#! @param to The type it is taken as.
|
||||
#! @returns The value converted.
|
||||
pub def emitter::convert(&mut this, value: &operand, to: u32): operand {
|
||||
var made: operand = value;
|
||||
return made if value.kind == to || to == bt::error || to == 0 || value.kind == bt::error;
|
||||
|
||||
val from := this.c.layout_type(value.kind);
|
||||
val target := this.c.layout_type(to);
|
||||
made.kind = to;
|
||||
return made if from == target || this.c.checker.strip(from, 0) == this.c.checker.strip(target, 0);
|
||||
|
||||
val fk := this.c.checker.types.kind_of(from);
|
||||
val tok := this.c.checker.types.kind_of(target);
|
||||
|
||||
# Numbers widen within their kind, which their words already hold, and an
|
||||
# integer operating with a float operates as the float.
|
||||
if this.c.checker.types.is_numeric(from) && this.c.checker.types.is_numeric(target) {
|
||||
if this.c.checker.types.is_float(target) && !this.c.checker.types.is_float(from) {
|
||||
val signed := this.c.checker.types.is_signed(from);
|
||||
var real := this.build.unary(signed ? op::itof : op::utof, value.words[0]);
|
||||
if this.c.checker.types.first_of(target) <= 32 {
|
||||
real = this.build.unary(op::fsingle, real);
|
||||
}
|
||||
return operand::one(to, real);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# An enum is its integer.
|
||||
if this.c.checker.named_kind(from) == dk::enum_type && this.c.checker.types.is_integer(target) {
|
||||
return made;
|
||||
}
|
||||
|
||||
if fk == ty::reference && tok == ty::reference {
|
||||
val inner_from := this.c.checker.unalias(this.c.checker.types.first_of(from));
|
||||
val inner_to := this.c.checker.unalias(this.c.checker.types.first_of(target));
|
||||
val from_trait := this.c.checker.named_kind(inner_from) == dk::trait_type;
|
||||
val to_trait := this.c.checker.named_kind(inner_to) == dk::trait_type;
|
||||
|
||||
# A reference to a value becomes one to a trait it implements, given the
|
||||
# vtable of its type for the trait.
|
||||
if to_trait && !from_trait && this.c.checker.types.kind_of(inner_from) != ty::reference {
|
||||
val vtable := this.c.vtable_of(this.c.checker.types.first_of(from), inner_to);
|
||||
return operand::two(to, value.words[0], this.build.word(vtable as u64));
|
||||
}
|
||||
# A reference to a trait becomes one to another the value also implements.
|
||||
if to_trait && from_trait {
|
||||
return made if inner_from == inner_to;
|
||||
var args: u32[1] = [value.words[1]];
|
||||
val map := this.c.retrait_of(inner_from, inner_to);
|
||||
val vtable := this.build.value(op::retrait, args[0:1], vt::word, map, 0);
|
||||
return operand::two(to, value.words[0], vtable);
|
||||
}
|
||||
# A reference to a reference is read through to the one it holds.
|
||||
if this.c.checker.types.kind_of(inner_from) == ty::reference && this.c.checker.types.kind_of(inner_to) != ty::reference {
|
||||
val place := this.referent(value);
|
||||
val inner := this.load(&place);
|
||||
return this.convert(&inner, to);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# A union holds a value as the field of its type, another union's too, and
|
||||
# a reference where one of its fields is one; and otherwise a reference is
|
||||
# read through first.
|
||||
if this.c.checker.named_kind(target) == dk::union_type && this.c.checker.united_field(value.kind, target) != 0 {
|
||||
return this.unite(value, target, to);
|
||||
}
|
||||
|
||||
# A reference written as one becomes a pointer at what it refers to, which
|
||||
# holds the same word.
|
||||
if fk == ty::reference && tok == ty::pointer
|
||||
&& this.c.checker.unalias(this.c.checker.types.first_of(from)) == this.c.checker.unalias(this.c.checker.types.first_of(target)) {
|
||||
return made;
|
||||
}
|
||||
|
||||
# A plain reference is read through where its value is expected.
|
||||
if fk == ty::reference {
|
||||
val place := this.referent(value);
|
||||
val inner := this.load(&place);
|
||||
return this.convert(&inner, to);
|
||||
}
|
||||
|
||||
# Bytes become a string by being copied, which the statement keeps, from the
|
||||
# view's two words wherever they are.
|
||||
if this.c.is_string(target) && this.c.checker.is_byte_view(from) && (value.address != 0 || value.count == 2) {
|
||||
var args: u32[2] = [0, 0];
|
||||
if value.address != 0 {
|
||||
args[0] = this.build.load(access::i64, value.address, 0);
|
||||
args[1] = this.build.load(access::i64, value.address, word_size);
|
||||
} else {
|
||||
args[0] = value.words[0];
|
||||
args[1] = value.words[1];
|
||||
}
|
||||
this.build.effect(op::format, span::<u32>::empty(), 0, 0);
|
||||
this.build.effect(op::format_bytes, args[0:2], 0, 0);
|
||||
return this.owned_text(to);
|
||||
}
|
||||
|
||||
# A string lends a view of its text: where its bytes begin, and how many.
|
||||
if this.c.is_string(from) && this.c.viewed(target) != 0 {
|
||||
val index := this.c.layout_of(from);
|
||||
val data := this.c.field_at(index, 1);
|
||||
val count := this.c.field_at(index, 2);
|
||||
val address := this.temporary_of(to);
|
||||
this.build.store(access::i64, address, 0, this.build.load(access::i64, value.address, data.offset));
|
||||
this.build.store(access::i64, address, word_size, this.build.load(access::i64, value.address, count.offset));
|
||||
return operand::at(to, address);
|
||||
}
|
||||
|
||||
# What else converts holds the same words: a writable view lending a
|
||||
# read-only one, a function by another name, `null` becoming a pointer.
|
||||
return made;
|
||||
}
|
||||
|
||||
#! A union holding a value, as the field the checker finds of its type.
|
||||
#! @param this The emitter.
|
||||
#! @param value The value.
|
||||
#! @param target The union's type, without aliases.
|
||||
#! @param to The type it is taken as.
|
||||
#! @returns The union.
|
||||
pub def emitter::unite(&mut this, value: &operand, target: u32, to: u32): operand {
|
||||
val field := this.c.checker.united_field(value.kind, target);
|
||||
val index := this.c.layout_of(target);
|
||||
val shape := this.c.layout_at(index);
|
||||
val address := this.temporary_of(to);
|
||||
this.build.clear(address, shape.size);
|
||||
for i in 0..shape.count {
|
||||
val each := this.c.field_at(index, i as u32);
|
||||
continue if each.decl != field;
|
||||
if shape.niche == 0 {
|
||||
this.build.store(access::i64, address, 0, this.build.word(i));
|
||||
}
|
||||
val held := this.convert(value, each.type_id);
|
||||
val at := location { kind = each.type_id, address = address, offset = each.offset, framed = true };
|
||||
this.store(&at, &held);
|
||||
this.adopt(value);
|
||||
return operand::at(to, address);
|
||||
}
|
||||
this.problem(0, "A union holds no field of the value's type.");
|
||||
return operand::at(to, address);
|
||||
}
|
||||
Reference in new issue
Block a user