Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user