#! @file places.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains places: where what a name, a field, an element, or what a pointer #! or reference reaches is, how what is there is read and written, and the #! references made to it. A reference is read through to what it refers to, #! as many as it takes, wherever a place is reached through it. import sakura::analyzer; module sakura::compiler; #! The address a place's memory begins at. #! @param this The emitter. #! @param place The place, in memory. #! @returns The address. pub def emitter::address_of(&mut this, place: &location): u32 { return place.address if place.offset == 0; return this.build.binary(op::add, place.address, this.build.word(place.offset)); } #! Reads what a place holds. What is held in memory is copied to a cell of the #! statement's own, so writing the place afterwards leaves the value as it was. #! @param this The emitter. #! @param place The place. #! @returns The value. pub def emitter::load(&mut this, place: &location): operand { val held := this.c.shape_of(place.kind); var made := operand { kind = place.kind, count = word_count(&held) }; if place.held { for i in 0..made.count { made.words[i] = this.build.read(place.vars[i]); } return made; } if in_memory(&held) { val copy := this.temporary_of(place.kind); this.build.copy(copy, this.address_of(place), held.size); return operand::at(place.kind, copy); } if made.count == 1 { made.words[0] = this.build.load(held.how, place.address, place.offset); return made; } for i in 0..made.count { made.words[i] = this.build.load(access::i64, place.address, place.offset + i as u64 * word_size); } return made; } #! Writes a value to a place, which is of the value's type. #! @param this The emitter. #! @param place The place. #! @param value The value. pub def emitter::store(&mut this, place: &location, value: &operand): void { val held := this.c.shape_of(place.kind); if place.held { for i in 0..word_count(&held) { this.build.assign(place.vars[i], value.words[i]); this.bind(place.vars[i], value.words[i]); } return; } if in_memory(&held) { return if value.address == 0; this.build.copy(this.address_of(place), value.address, held.size); return; } if word_count(&held) == 1 { this.build.store(held.how, place.address, place.offset, value.words[0]); return; } for i in 0..word_count(&held) { this.build.store(access::i64, place.address, place.offset + i as u64 * word_size, value.words[i]); } } #! Zeroes what a place holds. #! @param this The emitter. #! @param place The place. pub def emitter::zero(&mut this, place: &location): void { val held := this.c.shape_of(place.kind); if place.held { for i in 0..word_count(&held) { val zero := word_type(&held) == vt::real ? this.build.real(0) : this.build.word(0); this.build.assign(place.vars[i], zero); this.bind(place.vars[i], zero); } return; } this.build.clear(this.address_of(place), held.size); } #! A reference to a place, as the word a reference is: where the value is. A #! place held in variables has no address, so a copy of its value is referred #! to instead, which the storage worked out before lowering avoids for what a #! reference must reach. #! @param this The emitter. #! @param place The place. #! @param kind The reference's type. #! @returns The reference. pub def emitter::reference_to(&mut this, place: &location, kind: u32): operand { if place.held { val value := this.load(place); val copy := location { kind = place.kind, address = this.temporary_of(place.kind), framed = true }; this.store(©, &value); return this.reference_to(©, kind); } # A reference to a trait the value implements is given its type's vtable. val wanted := this.c.checker.unalias(kind); if this.c.checker.types.kind_of(wanted) == ty::reference { val referent := this.c.checker.unalias(this.c.checker.types.first_of(wanted)); if this.c.checker.named_kind(referent) == dk::trait_type && this.c.checker.named_kind(this.c.checker.unalias(place.kind)) != dk::trait_type { val own := this.c.checker.types.reference(place.kind, this.c.checker.types.second_of(wanted) != 0); val made := operand::one(own, this.address_of(place)); return this.convert(&made, kind); } } return operand::one(kind, this.address_of(place)); } #! The place a reference refers to. #! @param this The emitter. #! @param value The reference. #! @returns The place. pub def emitter::referent(&mut this, value: &operand): location { val kind := this.c.checker.types.first_of(this.c.checker.unalias(value.kind)); # Running with its memory checked, what a reference refers to is where the # program holds memory, as what a pointer points at is. if this.c.options.checks && this.c.checker.named_kind(this.c.checker.unalias(kind)) != dk::trait_type { this.reaches(value.words[0], this.build.word(1), this.c.shape_of(kind).size); } return location { kind = kind, address = value.words[0] }; } #! A place within another, at an offset, as a field is. #! @param this The emitter. #! @param place The place holding it. #! @param offset How many bytes in it is. #! @param kind Its type. #! @returns The place. pub def emitter::within(&mut this, place: &location, offset: u64, kind: u32): location { var made: location = place; if place.held { # A value held in variables is no struct, so this is never reached; it is # copied to memory where it is. val value := this.load(place); made = location { kind = place.kind, address = this.temporary_of(place.kind), framed = true }; this.store(&made, &value); } made.kind = kind; made.offset = made.offset + offset; return made; } #! The place a node's value is at, read through as many references as it holds #! until what they refer to is no reference, or as many as `levels` says. #! @param this The emitter. #! @param node The node. #! @param levels How many references to read through at most. #! @returns The place. pub def emitter::object_by(&mut this, node: u32, levels: u32): location { var kind := this.c.checker.type_at(node); if !this.c.is_reference_type(kind) || levels == 0 { return this.place_of(node); } var value := this.value_of_type(node); var place := this.referent(&value); var steps: u32 = 1; while steps < levels && this.c.is_reference_type(place.kind) && steps < 16 { value = this.load(&place); place = this.referent(&value); steps++; } return place; } #! The place a node's value is at, read through every reference it holds. #! @param this The emitter. #! @param node The node. #! @returns The place. pub def emitter::object(&mut this, node: u32): location { return this.object_by(node, 16); } #! The place a node names: a local, a global, a field, an element, or what a #! pointer points at; or else a cell of the statement's own holding its value. #! @param this The emitter. #! @param node The node. #! @returns The place. pub def emitter::place_of(&mut this, node: u32): location { val saved := this.build.pos; this.at_node(node); val made := this.place_node(node); this.build.pos = saved; return made; } #! The place a node names, by the kind of the node. #! @param this The emitter. #! @param node The node. #! @returns The place. def emitter::place_node(&mut this, node: u32): location { match this.c.checker.kind_of(node) { nk::name, nk::path => { val ref := this.c.checker.ref_at(node); if (ref & ref_local) != 0 { val index := this.local_of(ref & ref_mask); if index != 0 { val held: local_info = this.locals[index - 1]; return held.place; } if this.pass_variable(ref & ref_mask) { val held: local_info = this.locals[this.local_of(ref & ref_mask) - 1]; return held.place; } # Evaluating, a local constant another reads is evaluated first, and # read where its value is held. val local := ref & ref_mask; if this.c.options.evaluating && this.c.checker.kind_of(local) == nk::variable && this.c.checker.node_has(local, nf::constant) { val index := this.c.evaluation_of(node_key(this.table, local), 0, this.table, local, false); this.build.effect(op::ready, span::::blank(), index, 0); val offset: u64 = this.c.evaluations[index].offset; val address := this.build.value(op::global, span::::blank(), vt::word, 0, offset); return location { kind = this.c.checker.type_at(local), address = address }; } this.problem(node, "The local is not where it is named."); } else if ref != 0 && (ref & ref_type) == 0 && this.c.checker.decls.kinds.p[ref] == dk::variable && !this.c.checker.is_known(node) { return this.global_place(ref, this.called(node)); } } nk::member => { val ref := this.c.checker.ref_at(node); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::field { return this.field_place(node, ref); } } nk::member_of => { val ref := this.c.checker.ref_at(node); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::field { return this.field_place(node, ref); } # What else a description names is no place. if this.property_of(node) == 0 { this.problem(node, "The member a description names is not lowered."); return location { kind = this.c.checker.type_at(node), address = this.build.word(0) }; } } nk::index => { # Through `at`, it is the reference `at` gives, held as any value is. if !this.indexes_through_at(node) { return this.element_place(node); } } nk::call => { # `value_of(d)` of a variable is the variable. val ref := this.c.checker.ref_at(node); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::variable && !this.c.checker.is_known(node) { return this.global_place(ref); } } nk::unary => { if this.c.checker.token_kind(node) == tk::star { # A reference to a pointer is read through to the pointer. val given := this.value_of_type(this.c.checker.left_of(node)); val pointer := this.through_references(&given); val kind := this.c.checker.type_at(node); this.reaches(pointer.words[0], this.build.word(1), this.c.shape_of(kind).size); return location { kind = kind, address = pointer.words[0] }; } } else => {} } # Anything else is a value, held where the statement holds what it makes. val value := this.value_of_type(node); return this.hold(&value); } #! A place holding a value: its own memory, where it is in memory, or a cell of #! the statement's own the value is written to. #! @param this The emitter. #! @param value The value. #! @returns The place. pub def emitter::hold(&mut this, value: &operand): location { if value.address != 0 { return location { kind = value.kind, address = value.address, framed = true }; } val place := location { kind = value.kind, address = this.temporary_of(value.kind), framed = true }; this.store(&place, value); return place; } #! The place of a global, where it is placed among the globals. #! @param this The emitter. #! @param decl The global's declaration. #! @param instance Of a variable of a generic type, the instance read, or 0. #! @returns The place. pub def emitter::global_place(&mut this, decl: u32, instance: u32 = 0): location { return this.instance_place(decl, instance) if instance != 0; val kind: u32 = this.c.checker.decls.types.p[decl]; val constant := this.c.checker.decls.has(decl, df::constant); # While compiling, a constant is read once it is evaluated, and a variable # given its value once the program runs is not read at all. if this.c.options.evaluating { if !constant { this.build.trap(fault::not_constant as u32); val nowhere := this.build.word(0); return location { kind = kind, address = nowhere }; } val index := this.c.evaluation_of(decl_key(decl), decl, 0, 0, false); this.build.effect(op::ready, span::::blank(), index, 0); val offset: u64 = this.c.evaluations[index].offset; val address := this.build.value(op::global, span::::blank(), vt::word, 0, offset); return location { kind = kind, address = address }; } # A constant evaluated is read where the program holds its value as data. if constant { val datum := this.c.evaluated_datum(decl_key(decl), kind); if datum != no_block { val address := this.build.value(op::datum, span::::blank(), vt::word, datum, 0); return location { kind = kind, address = address }; } } val offset := this.c.global_of(decl); val address := this.build.value(op::global, span::::blank(), vt::word, 0, offset); return location { kind = kind, address = address }; } #! The place of the instance of a variable or constant of a generic type, as #! `global_place` gives a global's: a constant's instance is evaluated as a #! local constant is, by its node in the instance's table. #! @param this The emitter. #! @param decl The variable's declaration. #! @param instance The instance. #! @returns The place. def emitter::instance_place(&mut this, decl: u32, instance: u32): location { val held: instance = this.c.checker.instances[instance]; val kind := held.kind; val node: u32 = this.c.checker.decls.nodes.p[decl]; val constant := this.c.checker.decls.has(decl, df::constant); if this.c.options.evaluating { if !constant { this.build.trap(fault::not_constant as u32); val nowhere := this.build.word(0); return location { kind = kind, address = nowhere }; } val index := this.c.evaluation_of(node_key(held.table, node), 0, held.table, node, false); this.build.effect(op::ready, span::::blank(), index, 0); val offset: u64 = this.c.evaluations[index].offset; val address := this.build.value(op::global, span::::blank(), vt::word, 0, offset); return location { kind = kind, address = address }; } if constant { val datum := this.c.evaluated_datum(node_key(held.table, node), kind); if datum != no_block { val address := this.build.value(op::datum, span::::blank(), vt::word, datum, 0); return location { kind = kind, address = address }; } } val offset := this.c.instance_global(instance); val address := this.build.value(op::global, span::::blank(), vt::word, 0, offset); return location { kind = kind, address = address }; } #! Whether the code checks what only a checked build does: indexes and slices, #! the field a union holds, an integer converted to an enum, and a `match` #! finding no arm. A constant is evaluated checked in every build. #! @param this The emitter. #! @returns Whether it does. pub def emitter::checking(&this): bool { return !this.c.checker.checks_off || this.c.options.evaluating; } #! Stops the program where a pointer is null, which only the code of a checked #! program reaching an element past the first page asks. Elsewhere reading or #! writing through null stops the program where it does, in every build: the #! machine compares each address it reads or writes through with the first #! page, and an executable faults there, which its runtime reports as the same. #! @param this The emitter. #! @param pointer The pointer. def emitter::compare_null(&mut this, pointer: u32): void { val zero := this.build.word(0); this.build.check(this.build.binary(op::ne, pointer, zero), fault::null_reference as u32); } #! The place of a field of a struct or union, read through the references the #! object is reached by. A field a struct's spread brings in is reached through #! the spread field holding it, and a field of a union only where the union #! holds it. #! @param this The emitter. #! @param node The `member` node. #! @param field The field's declaration. #! @returns The place. pub def emitter::field_place(&mut this, node: u32, field: u32): location { val object := this.c.checker.left_of(node); val whole := this.object(object); val kind := this.c.checker.type_at(node); val owner := this.c.checker.unalias(whole.kind); return this.field_in(&whole, owner, field, kind); } #! The place of a field within a place holding a struct or union of a type. #! @param this The emitter. #! @param whole The place of the struct or union. #! @param owner Its type, without aliases. #! @param field The field's declaration. #! @param kind The field's type, as checked. #! @returns The place. pub def emitter::field_in(&mut this, whole: &location, owner: u32, field: u32, kind: u32): location { val index := this.c.layout_of(owner); val shape := this.c.layout_at(index); for i in 0..shape.count { val each := this.c.field_at(index, i as u32); if each.decl == field { if shape.kind == lk::variant { this.check_held(whole, i as u32); } return this.within(whole, each.offset, each.type_id); } } # Reached through a spread field, which holds the struct declaring it. for i in 0..shape.count { val each := this.c.field_at(index, i as u32); continue if !each.spread; val inner := this.within(whole, each.offset, each.type_id); val plain := this.c.checker.unalias(each.type_id); if this.holds_field(plain, field, 0) { return this.field_in(&inner, plain, field, kind); } } this.problem(0, "A field is not in the layout of what holds it."); return this.within(whole, 0, kind); } #! Whether a struct holds a field, its own or one a spread brings in. #! @param this The emitter. #! @param kind The struct's type. #! @param field The field's declaration. #! @param depth How deeply spreads have been followed. #! @returns Whether it holds it. def emitter::holds_field(&mut this, kind: u32, field: u32, depth: u32): bool { return false if depth > 16; val index := this.c.layout_of(kind); val shape := this.c.layout_at(index); for i in 0..shape.count { val each := this.c.field_at(index, i as u32); return true if each.decl == field; if each.spread && this.holds_field(this.c.checker.unalias(each.type_id), field, depth + 1) { return true; } } return false; } #! Stops the program unless a union holds its field of an index, where the code #! checks. #! @param this The emitter. #! @param whole The place of the union. #! @param index The field's index. pub def emitter::check_held(&mut this, whole: &location, index: u32): void { return if !this.checking(); val holds := this.tag_is(whole, index); this.build.check(holds, fault::not_held as u32); } #! Whether a union holds its field of an index, as a word of 0 or 1. #! @param this The emitter. #! @param whole The place of the union. #! @param index The field's index. #! @returns The word. pub def emitter::tag_is(&mut this, whole: &location, index: u32): u32 { var tag: u32 = 0; if whole.held { tag = this.build.read(whole.vars[0]); } else { tag = this.build.load(access::i64, whole.address, whole.offset); } # A union without a tag holds the field it is laid out for where its first word # is not null, and the empty one where it is. val shape := this.c.shape_of(whole.kind); if shape.niche != 0 { return this.build.binary(index + 1 == shape.niche ? op::ne : op::eq, tag, this.build.word(0)); } return this.build.binary(op::eq, tag, this.build.word(index as u64)); } #! The place of an element: of a fixed array, at an index checked against its #! length; of what a pointer points at, which is not checked; of a view, checked #! against how many it views; of a pack, at an index known while compiling; or #! of what implements `indexable`, the place its `at` refers to. #! @param this The emitter. #! @param node The `index` node. #! @returns The place. pub def emitter::element_place(&mut this, node: u32): location { val object := this.c.checker.left_of(node); val at := this.c.checker.right_of(node); val kind := this.c.checker.type_at(node); val whole_type := this.object_type(object); val through := this.c.checker.unalias(this.c.checker.reached(whole_type)); match this.c.checker.types.kind_of(through) { ty::fixed => { val whole := this.object(object); val shape := this.c.shape_of(through); val element := this.c.layout_at(shape.element - 1); if this.c.checker.is_known(at) { val index := this.c.checker.value_at(at).magnitude; if index < shape.count as u64 { return this.within(&whole, index * element.size, this.c.checker.types.first_of(through)); } } val index := this.value_as(at, bt::uint64); if this.checking() { val within := this.build.binary(op::ltu, index.words[0], this.build.word(shape.count as u64)); this.build.check(within, fault::out_of_bounds as u32); } val first := this.address_of(&whole); val address := this.build.binary(op::add, first, this.scaled(index.words[0], element.size)); return location { kind = this.c.checker.types.first_of(through), address = address, framed = whole.framed }; } ty::pointer => { val pointer := this.value_of_type(object); val reached := this.through_references(&pointer); val element := this.c.shape_of(this.c.checker.types.first_of(through)); # An element past the first page is no fault there, so a checked program # compares the pointer with null first, unless the index is known to # stay within it. val near := this.c.checker.is_known(at) && (element.size == 0 || this.c.checker.value_at(at).magnitude < null_page / element.size); if this.checking() && !near { this.compare_null(reached.words[0]); } val index := this.value_as(at, bt::uint64); val address := this.build.binary(op::add, reached.words[0], this.scaled(index.words[0], element.size)); this.reaches(address, this.build.word(1), element.size); return location { kind = this.c.checker.types.first_of(through), address = address }; } ty::pack => { val index := this.c.checker.value_at(at).magnitude; return this.pack_place(object, index as u32); } else => {} } # A view holds where its elements are and how many, so an element of one is the # place that many along, once the index is checked against the count. A view # holding any element is never over null, as std makes every view, so the # index's check is all it needs. val viewed := this.c.viewed(through); if viewed != 0 { val whole := this.object(object); val data := this.build.load(access::i64, whole.address, whole.offset); val count := this.build.load(access::i64, whole.address, whole.offset + word_size); val index := this.value_as(at, bt::uint64); if this.checking() { this.build.check(this.build.binary(op::ltu, index.words[0], count), fault::out_of_bounds as u32); } val element := this.c.shape_of(viewed); val address := this.build.binary(op::add, data, this.scaled(index.words[0], element.size)); return location { kind = viewed, address = address }; } # What implements `indexable` gives a reference to the element through `at`. val method := this.c.checker.ref_at(node); if method != 0 && (method & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[method] == dk::function { val made := this.accessor_call(node, method, object, at); if this.c.is_reference_type(made.kind) { return this.referent(&made); } return this.hold(&made); } this.problem(node, "Nothing is indexed here."); return location { kind = kind, address = this.temporary_of(kind) }; } #! The type of what an element or a member is reached through: its own, or, #! for a constant, the one it settles as, so `"text"[0]` is a `strview`'s. #! @param this The emitter. #! @param object The node of the value reached through. #! @returns The type. pub def emitter::object_type(&mut this, object: u32): u32 { val own := this.c.checker.type_at(object); return this.c.checker.types.is_untyped(own) ? this.c.checker.converted(object) : own; } #! Whether `x[k]` is indexed through `at`, and so is the reference it gives #! rather than a place of its own. #! @param this The emitter. #! @param node The `index` node. #! @returns Whether it is. pub def emitter::indexes_through_at(&mut this, node: u32): bool { val method := this.c.checker.ref_at(node); return method != 0 && (method & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[method] == dk::function && this.c.is_reference_type(this.c.checker.type_at(node)); } #! The reference `x[k]` gives through `at`: the one `at` gives, or, for a view, #! which is indexed without calling it, one to the element it covers. #! @param this The emitter. #! @param node The `index` node. #! @returns The reference. pub def emitter::indexed_reference(&mut this, node: u32): operand { val object := this.c.checker.left_of(node); val through := this.c.checker.unalias(this.c.checker.reached(this.object_type(object))); if this.c.viewed(through) != 0 { val place := this.element_place(node); return this.reference_to(&place, this.c.checker.type_at(node)); } return this.accessor_call(node, this.c.checker.ref_at(node), object, this.c.checker.right_of(node)); } #! A count of elements of a size, in bytes. #! @param this The emitter. #! @param count The count, a word. #! @param size The size of each. #! @returns The bytes. pub def emitter::scaled(&mut this, count: u32, size: u64): u32 { return count if size == 1; return this.build.binary(op::mul, count, this.build.word(size)); } #! A value read through every reference it is, until it is no reference. #! @param this The emitter. #! @param value The value. #! @returns What it reaches. pub def emitter::through_references(&mut this, value: &operand): operand { var at: operand = value; var steps := 0; while this.c.is_reference_type(at.kind) && steps < 16 { val place := this.referent(&at); at = this.load(&place); steps++; } return at; } #! The place of a value of a pack, by its index: of a variadic parameter's, #! the local holding it, and of a field's, where the field holds it. #! @param this The emitter. #! @param object The node naming the pack. #! @param index The value's index. #! @returns The place. pub def emitter::pack_place(&mut this, object: u32, index: u32): location { val ref := this.c.checker.ref_at(object); val local := (ref & ref_local) != 0 ? this.local_of(ref & ref_mask) : 0; if local != 0 { val held: local_info = this.locals[local - 1]; if index < held.count { return this.packs[held.pack as u64 + index as u64]; } } if local == 0 && this.c.checker.kind_of(object) != nk::name { val whole := this.object(object); val pack := this.c.checker.unalias(whole.kind); if this.c.checker.types.kind_of(pack) == ty::pack && index < this.c.checker.types.part_count(pack) { val field := this.c.field_at(this.c.layout_of(pack), index); return this.within(&whole, field.offset, field.type_id); } } this.problem(object, "The pack holds no value at the index."); return location { kind = bt::error }; } #! The places of each value of a pack, in order. #! @param this The emitter. #! @param object The node naming the pack. #! @returns The places. pub def emitter::pack_places(&mut this, object: u32): vector { var made := vector::::new(); val ref := this.c.checker.ref_at(object); 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 i in 0..held.count { made.append(this.packs[held.pack as u64 + i as u64]); } return made; } # A field's pack is reached once, and each value within it. val whole := this.object(object); val pack := this.c.checker.unalias(whole.kind); if this.c.checker.types.kind_of(pack) != ty::pack { this.problem(object, "The value is no pack."); return made; } val index := this.c.layout_of(pack); for i in 0..this.c.checker.types.part_count(pack) { val field := this.c.field_at(index, i as u32); made.append(this.within(&whole, field.offset, field.type_id)); } return made; }