Files
sakurac/compiler/emitter/composite.sakura
T

638 lines
25 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
#! @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);
if this.c.checker.types.kind_of(this.c.checker.unalias(at.kind)) == ty::pack {
this.give_pack(&at, this.c.checker.left_of(init));
continue;
}
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);
}
#! Gives a field holding a pack the values of a pack of its types, each where
#! the field holds it. A value that closes is moved, leaving the pack's place
#! empty, as `...` moves it to a call, so it isn't closed twice.
#! @param this The emitter.
#! @param at The field's place.
#! @param node The pack's node.
def emitter::give_pack(&mut this, at: &location, node: u32): void {
val places := this.pack_places(node);
val held := this.c.layout_of(this.c.checker.unalias(at.kind));
for j in 0..places.length {
val from: location = places[j];
val part := this.c.field_at(held, j as u32);
val value := this.load(&from);
val into := this.within(at, part.offset, part.type_id);
this.store(&into, &value);
if this.c.checker.closes(from.kind) {
this.zero(&from);
}
}
}
#! 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;
# A pack is written a value at a time, so what lies between them is
# cleared.
if this.c.checker.types.kind_of(this.c.checker.unalias(each.type_id)) == ty::pack {
val held := this.c.layout_of(each.type_id);
for v in 0..this.c.layout_at(held).count {
val part := this.c.field_at(held, v as u32);
val from := each.offset + part.offset;
for at in from..from + this.c.shape_of(part.type_id).size {
written[at] = true;
}
}
continue;
}
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>::blank(), 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.span);
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, this.writer_of(node) != 0);
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 `get mut` a member reads its property through, which the checker chose
#! where the value can be written, or 0. What it gives is a reference, written
#! through as any other is, rather than by the property's `set`.
#! @param this The emitter.
#! @param node The node.
#! @returns The `get mut`'s declaration, or 0.
pub def emitter::writer_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;
return 0 if this.c.checker.decls.kinds.p[ref] != dk::function || !this.c.checker.decls.has(ref, df::accessor);
return this.c.checker.decls.has(ref, df::mut_this) && !this.c.checker.decls.has(ref, df::setter) ? 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 {
return setter ? this.c.checker.property_setter(property) : this.c.checker.property_reader(property, false);
}
#! Reads a property through its `get`, or the `get mut` the node reads it
#! through.
#! @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 getter gives.
pub def emitter::property_get(&mut this, property: u32, receiver: &operand, node: u32): operand {
val writer := this.writer_of(node);
val getter := writer != 0 ? writer : 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>::blank());
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. What is no view
#! is sliced through the `span_of` or `slice_of` the checker chose.
#! @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 chosen := this.c.checker.ref_at(node);
if chosen != 0 && (chosen & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[chosen] == dk::function {
return this.sliced_through(chosen, object, start, 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);
}
#! `x[start:count]` of what is no view: `T::span_of(&x, start, count)`, or its
#! `slice_of` given `&mut x`, which checks the bounds itself. A start left out
#! is 0, and a count left out is `~0u`, everything after the start.
#! @param this The emitter.
#! @param decl The `span_of` or `slice_of` the checker chose.
#! @param object The node of what is sliced.
#! @param start The start's node, or 0.
#! @param count The count's node, or 0.
#! @returns The view it gives.
def emitter::sliced_through(&mut this, decl: u32, object: u32, start: u32, count: u32): operand {
val owner := this.c.checker.reached(this.object_type(object));
val target := this.c.method_target(decl, owner);
val taken := this.c.checker.unalias(this.c.checker.types.part_at(target.kind, 0));
val writes := this.c.checker.types.kind_of(taken) == ty::reference && this.c.checker.types.second_of(taken) != 0;
var given := gathered { values = vector::<operand>::new() };
val receiver := this.receiver_of(object, writes);
given.values.append(this.convert(&receiver, this.c.checker.types.part_at(target.kind, 0)));
val from_type := this.c.checker.types.part_at(target.kind, 1);
val many_type := this.c.checker.types.part_at(target.kind, 2);
given.values.append(start != 0 ? this.value_as(start, from_type) : operand::one(from_type, this.build.word(0)));
given.values.append(count != 0 ? this.value_as(count, many_type) : operand::one(many_type, this.build.word(~0u)));
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);
}