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