#! @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::::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::::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::::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::::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); }