#! @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::::blank(), 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::::blank(), 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.span, 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::environment, 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 { var made := vector::::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.span.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("", 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.span, 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::::new() }; var lent := vector::::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.span { this.close(&each); } var args := vector::::new(); result.args(&mut args); this.give_back(args.span); } #! 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::::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); }