#! @file operators.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the lowering of operators: prefix and postfix operators, taking a #! reference, the binary operators, assignment, ternaries, `else`, casts with #! `as`, and tests with `is`. Arithmetic on words wraps at 64 bits, so a result #! narrower than a word is narrowed again to its type, as the machine keeps it. import sakura::analyzer; module sakura::compiler; #! Narrows the result of arithmetic to its type: an integer to its bits, and a #! real to the precision of an `f32`. #! @param this The emitter. #! @param value The result. #! @param kind Its type. #! @returns The result narrowed. pub def emitter::normalize(&mut this, value: u32, kind: u32): u32 { val held := this.c.shape_of(kind); if held.kind == lk::floating && held.bits <= 32 { return this.build.unary(op::fsingle, value); } if (held.kind == lk::signed || held.kind == lk::unsigned) && held.bits < 64 { return this.build.wrap(value, held.bits, held.kind == lk::signed); } return value; } #! Whether a type is a signed integer, read as the machine reads it. #! @param this The emitter. #! @param kind The type. #! @returns Whether it is. pub def emitter::is_signed(&mut this, kind: u32): bool { val held := this.c.shape_of(kind); return held.kind == lk::signed; } #! Whether a type is a float. #! @param this The emitter. #! @param kind The type. #! @returns Whether it is. pub def emitter::is_float(&mut this, kind: u32): bool { val held := this.c.shape_of(kind); return held.kind == lk::floating; } #! A prefix operator: `-x`, `!x`, `~x`, `++x`, `--x`, or `*p`. #! @param this The emitter. #! @param node The operator's node. #! @returns Its value. pub def emitter::unary(&mut this, node: u32): operand { val operand_node := this.c.checker.left_of(node); val kind := this.c.checker.type_at(node); match this.c.checker.token_kind(node) { tk::minus => { val method := this.operator_decl(node); if method != 0 { return this.method_operator(node, method, operand_node, 0); } val value := this.value_as(operand_node, kind); val made := this.is_float(kind) ? this.build.unary(op::fneg, value.words[0]) : this.build.unary(op::neg, value.words[0]); return operand::one(kind, this.normalize(made, kind)); } tk::negate => { val value := this.value_as(operand_node, kind); return operand::one(kind, this.build.unary(op::lnot, value.words[0])); } tk::bitnot => { val method := this.operator_decl(node); if method != 0 { return this.method_operator(node, method, operand_node, 0); } val value := this.value_as(operand_node, kind); return operand::one(kind, this.normalize(this.build.unary(op::bnot, value.words[0]), kind)); } tk::increment, tk::decrement => return this.step(node, operand_node, true); tk::star => { val place := this.place_of(node); return this.load(&place); } else => {} } this.problem(node, "The operator is not lowered."); return this.zero_value(kind); } #! The method an arithmetic operator calls, as `+` calls `add`, which the #! checker chose by its operands, or 0 where the language operates on them #! itself, as it does on numbers. #! @param this The emitter. #! @param node The operator's node: a `binary`, a prefix `-`, or a compound #! assignment. #! @returns The method's declaration, or 0. pub def emitter::operator_decl(&mut this, node: u32): u32 { val ref := this.c.checker.ref_at(node); return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.c.checker.decls.kinds.p[ref] != dk::function; return ref; } #! Calls the method an arithmetic operator is, given the operand it is called #! on as its `this` and the other as its argument, each as a call gives them: #! referred to where the method takes a reference, and converted to what it #! takes otherwise. What it gives is the statement's, as what a call gives is. #! @param this The emitter. #! @param node The operator's node, which the checker recorded the call at. #! @param decl The method's declaration. #! @param left The node of the operand it is called on. #! @param right The node of the other operand, or 0. #! @returns What the method gives. pub def emitter::method_operator(&mut this, node: u32, decl: u32, left: u32, right: 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 && instance == 0 { kind = this.c.checker.trait_method_type(decl, this.c.checker.reached(this.c.checker.type_at(left))); } var given := gathered { values = vector::::new() }; given.values.append(this.receiver(left, this.c.checker.types.part_at(kind, 0), decl)); if right != 0 && this.c.checker.types.part_count(kind) > 1 { given.values.append(this.argument(right, this.c.checker.types.part_at(kind, 1))); } val made := this.call_gathered(decl, instance, holder, kind, left, &given); this.keep(&made); return made; } #! Whether the method a compound assignment calls changes its target in place, #! as `add_assign` does, giving nothing, rather than giving what is written over #! it, as `add` does. #! @param this The emitter. #! @param decl The method's declaration. #! @returns Whether it does. def emitter::in_place(&mut this, decl: u32): bool { val kind := this.c.checker.decls.types.p[decl]; return this.c.checker.types.kind_of(kind) == ty::function && this.c.checker.unalias(this.c.checker.types.first_of(kind)) == bt::void_type; } #! Calls the method a compound assignment's operator is, as `+=` calls `add`, #! on what its target holds, given the value as its argument. What it gives is #! the caller's to store. #! @param this The emitter. #! @param node The `assign` node, which the checker recorded the call at. #! @param decl The method's declaration. #! @param place Where what the target holds is. #! @param value The node of the value. #! @returns What the method gives. def emitter::method_applied(&mut this, node: u32, decl: u32, place: &location, value: u32): operand { val instance := this.called(node); val holder := this.c.trait_of_method(decl); val target := this.c.checker.left_of(node); var kind := this.c.type_of_function(decl, instance); if holder != 0 { kind = this.c.checker.trait_method_type(decl, this.c.checker.reached(this.c.checker.type_at(target))); } var given := gathered { values = vector::::new() }; val subject := this.reference_to(place, this.c.checker.types.reference(place.kind, this.c.checker.decls.has(decl, df::mut_this))); given.values.append(this.convert(&subject, this.c.checker.types.part_at(kind, 0))); if this.c.checker.types.part_count(kind) > 1 { given.values.append(this.argument(value, this.c.checker.types.part_at(kind, 1))); } return this.call_gathered(decl, instance, holder, kind, target, &given); } #! Calls the method of a comparison, as `==` calls `equals`, given references to #! its operands. #! @param this The emitter. #! @param node The operator's node. #! @param kind The type of the operand the method is called on. #! @param word The ID of the method's name. #! @param left The node of that operand. #! @param right The node of the other operand, or 0. #! @returns What the method gives. pub def emitter::operator_call(&mut this, node: u32, kind: u32, word: u32, left: u32, right: u32): operand { val plain := this.c.checker.unalias(this.c.checker.read_type(kind)); val method := this.pick_method(plain, word, right != 0); if method == 0 { this.problem(node, "The operator's method is not found."); return this.zero_value(kind); } val target := this.c.method_target(method, plain); var others := vector::::new(); if right != 0 && this.c.checker.types.part_count(target.kind) > 1 { val param := this.c.checker.types.part_at(target.kind, 1); if this.c.is_reference_type(this.c.checker.type_at(right)) { val given := this.value_of_type(right); others.append(this.convert(&given, param)); } else { others.append(this.lent_arg(right, param)); } } val subject := this.c.is_reference_type(this.c.checker.type_at(left)) ? this.value_of_type(left) : this.lent_arg(left, this.c.checker.types.reference(plain, false)); val made := this.method_call(method, plain, &subject, others.view()); this.keep(&made); return made; } #! The method of a name a type has, of the overloads the one taking `this` and, #! where `binary` says, another reference to the type. #! @param this The emitter. #! @param kind The type. #! @param word The ID of the method's name. #! @param binary Whether it takes another operand. #! @returns The method's declaration, or 0. pub def emitter::pick_method(&mut this, kind: u32, word: u32, binary: bool): u32 { val found := this.c.checker.find_member(kind, word); return 0 if found.decl == 0; var first: u32 = 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::method) { val held := this.c.checker.method_type(at, kind); val count := this.c.checker.types.part_count(held); if (binary && count == 2) || (!binary && count == 1) { if first == 0 { first = at; } if !binary { return at; } val other := this.c.checker.types.part_at(held, 1); if this.c.is_reference_type(other) && this.c.checker.strip(this.c.checker.types.first_of(this.c.checker.unalias(other)), 0) == this.c.checker.strip(kind, 0) { return at; } } } } return first; } #! `x++`, `x--`, `++x`, or `--x`, which change a number or pointer in place, or a #! property through its accessors. #! @param this The emitter. #! @param node The operator's node. #! @param target The node of what it changes. #! @param prefix Whether it gives the value after the change rather than before. #! @returns The value it gives. pub def emitter::step(&mut this, node: u32, target: u32, prefix: bool): operand { val kind := this.c.checker.type_at(node); val increments := this.c.checker.token_kind(node) == tk::increment; # A property is read by its `get` and written by its `set`, on one receiver. val property := this.property_of(target); if property != 0 { val object := this.c.checker.left_of(target); val receiver := this.receiver_of(object, true); val before := this.property_get(property, &receiver, target); val after := this.stepped(&before, kind, increments); this.property_set(property, &receiver, &after, target); return prefix ? after : before; } val place := this.writable_place(target); val before := this.load(&place); val after := this.stepped(&before, place.kind, increments); this.store(&place, &after); var made := prefix ? after : before; made.kind = kind; return made; } #! A number or pointer one more or one less. #! @param this The emitter. #! @param value The number or pointer. #! @param kind Its type. #! @param increments Whether it is one more. #! @returns The value. def emitter::stepped(&mut this, value: &operand, kind: u32, increments: bool): operand { val plain := this.c.layout_type(kind); if this.c.checker.types.kind_of(plain) == ty::pointer { val element := this.c.shape_of(this.c.checker.types.first_of(plain)); val made := this.build.binary(increments ? op::add : op::sub, value.words[0], this.build.word(element.size)); return operand::one(kind, made); } if this.is_float(kind) { val one := this.build.real(bits_of_real(1.0)); val made := this.build.binary(increments ? op::fadd : op::fsub, value.words[0], one); return operand::one(kind, this.normalize(made, kind)); } val made := this.build.binary(increments ? op::add : op::sub, value.words[0], this.build.word(1)); return operand::one(kind, this.normalize(made, kind)); } #! The place an assignment or step writes: a place, read through references #! where the node is a reference whose referent is written. #! @param this The emitter. #! @param node The node written. #! @returns The place. def emitter::writable_place(&mut this, node: u32): location { return this.c.is_reference_type(this.c.checker.type_at(node)) ? this.object(node) : this.place_of(node); } #! Whether a place whose value is known while compiling is still in memory: an #! element or field of a variable or constant whose whole value is not known, as #! one of a constant's array, which the program holds as data. A reference to it #! is to the place, as one to the whole is. #! @param this The emitter. #! @param node The place's node. #! @returns Whether it is. def emitter::stored(&mut this, node: u32): bool { var at := node; while this.c.checker.kind_of(at) == nk::index || this.c.checker.kind_of(at) == nk::member { at = this.c.checker.left_of(at); } val named := this.c.checker.kind_of(at) == nk::name || this.c.checker.kind_of(at) == nk::path; return named && at != node && !this.c.checker.is_known(at); } #! `&x` or `&mut x`: a reference to a place, or to a value that is no place, #! held where the statement holds it, which closes it where the statement ends. #! One bound to a name lives as long as the name, as `local_statement` holds #! it. #! @param this The emitter. #! @param node The `reference` node. #! @returns The reference. pub def emitter::reference(&mut this, node: u32): operand { val inner := this.c.checker.left_of(node); val kind := this.c.checker.type_at(node); # A reference, as a variable or a call gives one, is lent as it is, since # references never stack. if this.c.checker.has_flag(node, rf::relent) { val same := this.value(inner); return this.convert(&same, kind); } if this.c.checker.is_place(inner) && (!this.c.checker.is_known(inner) || this.stored(inner)) { val place := this.place_of(inner); return this.reference_to(&place, kind); } val referent := this.c.checker.types.first_of(this.c.checker.unalias(kind)); val value := this.value_as(inner, referent); val place := this.hold(&value); return this.reference_to(&place, kind); } #! A binary operator. #! @param this The emitter. #! @param node The operator's node. #! @returns Its value. pub def emitter::binary(&mut this, node: u32): operand { val operator := this.c.checker.token_kind(node); val left := this.c.checker.left_of(node); val right := this.c.checker.right_of(node); val kind := this.c.checker.type_at(node); if operator == tk::logand || operator == tk::logor { return this.logical(node); } if operator.compares() { return this.comparison(node); } # Pointers move by elements, and two count the elements between them. val left_type := this.c.layout_type(this.c.checker.read_type(this.c.checker.type_at(left))); if (operator == tk::plus || operator == tk::minus) && this.c.checker.types.kind_of(left_type) == ty::pointer { val a := this.value_of_type(left); val pointer := this.through_references(&a); val element := this.c.shape_of(this.c.checker.types.first_of(left_type)); val right_type := this.c.layout_type(this.c.checker.read_type(this.c.checker.type_at(right))); if this.c.checker.types.kind_of(right_type) == ty::pointer { val b := this.value_of_type(right); val other := this.through_references(&b); if this.c.options.evaluating { this.same_memory(pointer.words[0], other.words[0]); } var bytes := this.build.binary(op::sub, pointer.words[0], other.words[0]); if element.size > 1 { bytes = this.build.binary(op::divs, bytes, this.build.word(element.size)); } return operand::one(kind, bytes); } val count := this.value_as(right, bt::int64); val step := this.scaled(count.words[0], element.size); return operand::one(kind, this.build.binary(operator == tk::plus ? op::add : op::sub, pointer.words[0], step)); } # Arithmetic on what is no number calls the method the checker chose. val method := this.operator_decl(node); if method != 0 { return this.method_operator(node, method, left, right); } # A shift's amount is of its own type, read through a reference. val a := this.value_as(left, kind); val b := operator == tk::lshift || operator == tk::rshift ? this.value_as(right, this.read_kind(right)) : this.value_as(right, kind); return this.arithmetic(operator, a.words[0], b.words[0], kind); } #! The type of a node's value, read through the references it is. #! @param this The emitter. #! @param node The node. #! @returns The type. def emitter::read_kind(&mut this, node: u32): u32 { return this.c.checker.read_type(this.c.checker.type_at(node)); } #! Arithmetic the language does itself, on numbers or bits. #! @param this The emitter. #! @param operator The operator, as `+`. #! @param a The left operand's word. #! @param b The right operand's word. #! @param kind The type of the result. #! @returns Its value. def emitter::arithmetic(&mut this, operator: tk, a: u32, b: u32, kind: u32): operand { val floating := this.is_float(kind); val signed := this.is_signed(kind); val made := match operator { tk::plus => floating ? op::fadd : op::add, tk::minus => floating ? op::fsub : op::sub, tk::star => floating ? op::fmul : op::mul, tk::slash => floating ? op::fdiv : (signed ? op::divs : op::divu), tk::modulo => floating ? op::frem : (signed ? op::rems : op::remu), tk::bitand => op::band, tk::bitor => op::bor, tk::bitxor => op::bxor, tk::lshift => op::shl, tk::rshift => signed ? op::shrs : op::shru, else => op::add }; return operand::one(kind, this.normalize(this.build.binary(made, a, b), kind)); } #! `&&` or `||` as a value: the right is only evaluated where the left does not #! decide. #! @param this The emitter. #! @param node The operator's node. #! @returns Its value. def emitter::logical(&mut this, node: u32): operand { val locals := this.scope_mark(); this.declare_tests(node); val result := this.build.variable(vt::word); val yes := this.build.new_block(); val no := this.build.new_block(); val join := this.build.new_block(); this.branch_on(node, yes, no); this.build.seal(yes); this.build.seal(no); this.build.switch_to(yes); this.build.assign(result, this.build.word(1)); this.build.jump(join); this.build.switch_to(no); this.build.assign(result, this.build.word(0)); this.build.jump(join); this.build.seal(join); this.build.switch_to(join); val made := operand::one(this.c.checker.type_at(node), this.build.read(result)); this.end_scope(locals, 0); return made; } #! A comparison: of numbers, enums, `bool`, and pointers as words, of integers #! of different signs exactly, of floats as floats, of what says how it compares #! through its `equals` or `compare`, and of fixed arrays, structs, and unions #! the language compares part by part. #! @param this The emitter. #! @param node The comparison's node. #! @returns Its value, a `bool`. def emitter::comparison(&mut this, node: u32): operand { val operator := this.c.checker.token_kind(node); val left := this.c.checker.left_of(node); val right := this.c.checker.right_of(node); val kind := this.c.checker.type_at(node); val shared := this.c.checker.converted(left); val plain := this.c.layout_type(this.c.checker.read_type(shared)); val equal := operator == tk::equals || operator == tk::noteq; val swap := operator == tk::more || operator == tk::moreeq; # Integers of different signs compare as the numbers they are. val ra := this.c.checker.unalias(this.c.checker.read_type(this.c.checker.type_at(left))); val rb := this.c.checker.unalias(this.c.checker.read_type(this.c.checker.type_at(right))); if this.c.checker.types.is_integer(ra) && this.c.checker.types.is_integer(rb) && !this.c.checker.types.is_untyped(ra) && !this.c.checker.types.is_untyped(rb) && this.c.checker.types.is_signed(ra) != this.c.checker.types.is_signed(rb) { return this.mixed(node, ra); } # What says how it compares is compared by its method. # A fixed array compares element by element, which the runtime does by its # layout, as the machine does. val named := this.c.checker.named_kind(plain); if named == dk::struct_type || named == dk::union_type { if !equal || this.c.checker.has_method(plain, word::equals) { return this.method_comparison(node, plain); } } if this.c.checker.types.kind_of(plain) == ty::fixed { val a := this.value_as(left, plain); val b := this.value_as(right, plain); var args: u32[2] = [a.address, b.address]; var made := this.build.value(op::equal, args[0:2], vt::word, this.c.layout_of(plain), 0); if operator == tk::noteq { made = this.build.unary(op::lnot, made); } return operand::one(kind, made); } val a := this.value_as(left, plain); val b := this.value_as(right, plain); val x := swap ? b.words[0] : a.words[0]; val y := swap ? a.words[0] : b.words[0]; # While compiling, pointers are ordered only within one memory, since where # different memory is changes from one build to the next. if !equal && this.c.options.evaluating && this.c.checker.types.kind_of(plain) == ty::pointer { this.same_memory(x, y); } val floating := this.is_float(plain); val signed := this.is_signed(plain); val compared := match operator { tk::equals => floating ? op::feq : op::eq, tk::noteq => floating ? op::fne : op::ne, tk::less, tk::more => floating ? op::flt : (signed ? op::lts : op::ltu), else => floating ? op::fle : (signed ? op::les : op::leu) }; return operand::one(kind, this.build.binary(compared, x, y)); } #! A comparison through `equals` or `compare`, given references to both sides: #! `!=` turns `equals` around, and `<` and the others test the sign of what #! `compare` gives. #! @param this The emitter. #! @param node The comparison's node. #! @param kind The type compared. #! @returns Its value. def emitter::method_comparison(&mut this, node: u32, kind: u32): operand { val operator := this.c.checker.token_kind(node); val equal := operator == tk::equals || operator == tk::noteq; val word := equal ? word::equals : word::compare; val left := this.c.checker.left_of(node); val right := this.c.checker.right_of(node); val result := this.operator_call(node, kind, word, left, right); val bool_type := this.c.checker.type_at(node); if equal { return operand::one(bool_type, operator == tk::noteq ? this.build.unary(op::lnot, result.words[0]) : result.words[0]); } val zero := this.build.word(0); val made := match operator { tk::less => this.build.binary(op::lts, result.words[0], zero), tk::lesseq => this.build.binary(op::les, result.words[0], zero), tk::more => this.build.binary(op::lts, zero, result.words[0]), else => this.build.binary(op::les, zero, result.words[0]) }; return operand::one(bool_type, made); } #! Compares a signed and an unsigned integer exactly, as the numbers they are. #! @param this The emitter. #! @param node The comparison's node. #! @param ra The left operand's type, read through. #! @returns Its value. def emitter::mixed(&mut this, node: u32, ra: u32): operand { val operator := this.c.checker.token_kind(node); val a := this.value(this.c.checker.left_of(node)); val b := this.value(this.c.checker.right_of(node)); var x := this.through_references(&a).words[0]; var y := this.through_references(&b).words[0]; var signed_left := this.c.checker.types.is_signed(ra); # `a > b` is `b < a`, and `a >= b` is `b <= a`. if operator == tk::more || operator == tk::moreeq { val held := x; x = y; y = held; signed_left = !signed_left; } var made: u32 = 0; if operator == tk::equals || operator == tk::noteq { made = signed_left ? this.build.binary(op::eqsu, x, y) : this.build.binary(op::eqsu, y, x); if operator == tk::noteq { made = this.build.unary(op::lnot, made); } } else if operator == tk::less || operator == tk::more { made = this.build.binary(signed_left ? op::ltsu : op::ltus, x, y); } else { made = this.build.binary(signed_left ? op::lesu : op::leus, x, y); } return operand::one(this.c.checker.type_at(node), made); } #! An assignment, `=` or a compound one as `+=`, which gives the value assigned. #! @param this The emitter. #! @param node The `assign` node. #! @returns The value assigned. pub def emitter::assign(&mut this, node: u32): operand { val operator := this.c.checker.token_kind(node); val target := this.c.checker.left_of(node); val value := this.c.checker.right_of(node); val kind := this.c.checker.type_at(node); # `_ = x` evaluates `x` and lets go of it, which the statement closes. if operator == tk::assign && this.c.checker.kind_of(target) == nk::name && this.c.checker.name_of(target) == names::underscore_id { return this.value(value); } # A property is written by its `set`, on the receiver its `get` reads. val property := this.property_of(target); if property != 0 { val object := this.c.checker.left_of(target); val receiver := this.receiver_of(object, true); val held := this.c.checker.decls.types.p[property]; var stored := operand {}; if operator == tk::assign { stored = this.value_as(value, kind); } else { val current := this.property_get(property, &receiver, target); stored = this.compound(node, ¤t, value, kind); # Changed in place, what `get` gave is what `set` takes, so the statement # no longer closes it. val method := this.operator_decl(node); if method != 0 && this.in_place(method) { this.adopt(¤t); } } this.property_set(property, &receiver, &stored, target); return stored; } # A variable holding a reference is pointed elsewhere by a reference written # as one, and anything else writes what it refers to. val target_type := this.c.checker.type_at(target); if operator == tk::assign && this.c.is_reference_type(target_type) { if !this.c.checker.has_flag(node, rf::valued) { val place := this.place_of(target); val made := this.value_as(value, target_type); this.store(&place, &made); return made; } val place := this.object_by(target, 1); val made := this.value_as(value, place.kind); this.adopt(&made); this.replace(&place, &made); return made; } # Writing a union's field makes the union hold it, as `u.(d)` names one too. val target_kind := this.c.checker.kind_of(target); if target_kind == nk::member || target_kind == nk::member_of { val ref := this.c.checker.ref_at(target); if ref != 0 && (ref & (ref_local | ref_type)) == 0 && this.c.checker.decls.kinds.p[ref] == dk::field && this.c.checker.decls.kinds.p[this.c.checker.decls.owners.p[ref]] == dk::union_type && !this.c.checker.decls.has(this.c.checker.decls.owners.p[ref], df::foreign) && operator == tk::assign { val whole := this.object(this.c.checker.left_of(target)); val made := this.value_as(value, this.c.checker.type_at(target)); val united := this.unite(&made, this.c.checker.unalias(whole.kind), whole.kind); this.replace(&whole, &united); return made; } } val place := this.writable_place(target); if operator == tk::assign { val made := this.value_as(value, place.kind); this.adopt(&made); this.replace(&place, &made); return made; } # A compound assignment reads the target after the value, so it holds whatever # the value made of it. Where its operator is a method's call, what the method # gives is written over what the target held, which is closed, and one that # changes the target in place, as `add_assign`, is given it to change. val method := this.operator_decl(node); if method != 0 { val made := this.method_applied(node, method, &place, value); if this.in_place(method) { return this.load(&place); } this.replace(&place, &made); return made; } val current := this.load(&place); val made := this.compound(node, ¤t, value, place.kind); this.store(&place, &made); return made; } #! Writes a value over what a place holds, closing what it held first, since #! nothing holds it afterwards. The value is made before, in case it reads the #! old one. #! @param this The emitter. #! @param place The place. #! @param value The value. pub def emitter::replace(&mut this, place: &location, value: &operand): void { if this.c.checker.closes(place.kind) { this.close(place); } this.store(place, value); } #! The value a compound assignment stores: the operator applied to what the #! target held and the value. #! @param this The emitter. #! @param node The `assign` node. #! @param current What the target held. #! @param value The node of the value. #! @param kind The target's type. #! @returns The value to store. def emitter::compound(&mut this, node: u32, current: &operand, value: u32, kind: u32): operand { val operator := this.c.checker.token_kind(node); # Where the operator is a method's call, it is called on what the target held, # and one changing it in place, as `add_assign`, changes that. val method := this.operator_decl(node); if method != 0 { val place := this.hold(current); val made := this.method_applied(node, method, &place, value); return this.in_place(method) ? this.load(&place) : made; } val plain := this.c.layout_type(kind); if this.c.checker.types.kind_of(plain) == ty::pointer { val count := this.value_as(value, bt::int64); val element := this.c.shape_of(this.c.checker.types.first_of(plain)); val step := this.scaled(count.words[0], element.size); return operand::one(kind, this.build.binary(operator == tk::pluseq ? op::add : op::sub, current.words[0], step)); } val shifting := operator == tk::lshifteq || operator == tk::rshifteq; val right := shifting ? this.value_as(value, this.read_kind(value)) : this.value_as(value, kind); return this.arithmetic(operator.applied(), current.words[0], right.words[0], kind); } #! `cond ? a : b`, whose branches give the ternary's type. #! @param this The emitter. #! @param node The `ternary` node. #! @returns Its value. pub def emitter::ternary(&mut this, node: u32): operand { val kind := this.c.checker.type_at(node); val then := this.c.checker.record(node, if_slot::then); val other := this.c.checker.record(node, if_slot::other); val yes := this.build.new_block(); val no := this.build.new_block(); val join := this.build.new_block(); val result := this.result_place(kind); # The names the condition's tests give are declared before it runs, as an # `if`'s are, and close once the branches join. val condition := this.c.checker.left_of(node); val locals := this.scope_mark(); this.declare_tests(condition); this.branch_on(condition, yes, no); this.build.seal(yes); this.build.seal(no); this.build.switch_to(yes); val a := this.value_as(then, kind); this.adopt(&a); this.store(&result, &a); this.build.jump(join); this.build.switch_to(no); val b := this.value_as(other, kind); this.adopt(&b); this.store(&result, &b); this.build.jump(join); this.build.seal(join); this.build.switch_to(join); this.end_scope(locals, 0); val made := this.load_result(&result); this.keep(&made); return made; } #! A place branches each give their value to, which the value is read from where #! they join: variables, or a cell the statement holds. #! @param this The emitter. #! @param kind The value's type. #! @returns The place. pub def emitter::result_place(&mut this, kind: u32): location { val held := this.c.shape_of(kind); if in_memory(&held) { return location { kind = kind, address = this.temporary_of(kind), framed = true }; } var place := location { kind = kind, held = true }; for i in 0..word_count(&held) { place.vars[i] = this.build.variable(word_type(&held)); } return place; } #! The value a place branches gave their values to holds, as its own memory #! where it is in memory, rather than a copy. #! @param this The emitter. #! @param place The place. #! @returns The value. pub def emitter::load_result(&mut this, place: &location): operand { return operand::at(place.kind, place.address) if !place.held; return this.load(place); } #! `value else fallback`: what a `result` or `maybe` holds, or else what stands #! in for it, a value, or a block that leaves, with the error a result holds #! named for it. #! @param this The emitter. #! @param node The `fallback` node. #! @returns The value held. pub def emitter::fallback(&mut this, node: u32): operand { val kind := this.c.checker.type_at(node); val error := this.c.checker.record(node, fallback_slot::error); val other := this.c.checker.record(node, fallback_slot::other); val whole := this.object(this.c.checker.left_of(node)); val plain := this.c.checker.unalias(whole.kind); val index := this.c.layout_of(plain); # A result holds its value as `value`, and a maybe as `some`, the other field # being what holds otherwise. val wanted := this.c.checker.is_std(plain, word::maybe) ? word::some : names::value_id; var held: u32 = 0; var fault: u32 = 0xFFFFFFFF; 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.name == wanted { held = i as u32; } else if each.name == word::error { fault = i as u32; } } val value_field := this.c.field_at(index, held); val result := this.result_place(kind); val yes := this.build.new_block(); val no := this.build.new_block(); val join := this.build.new_block(); this.build.branch(this.tag_is(&whole, held), yes, no); this.build.seal(yes); this.build.seal(no); # The value is taken out, which a value that closes leaves zeroed, as `move` # does, so closing what holds it closes nothing twice. this.build.switch_to(yes); val payload := this.within(&whole, value_field.offset, value_field.type_id); val taken := this.load(&payload); val converted := this.convert(&taken, kind); this.store(&result, &converted); if this.c.checker.closes(value_field.type_id) { this.zero(&payload); } this.build.jump(join); this.build.switch_to(no); val mark := this.scope_mark(); if error != 0 && fault != 0xFFFFFFFF { val error_field := this.c.field_at(index, fault); val reason := this.within(&whole, error_field.offset, error_field.type_id); val copied := this.load(&reason); val place := this.new_place(error, this.c.checker.type_at(error)); this.store(&place, &copied); this.declare(error, place, false); } if this.c.checker.kind_of(other) == nk::block { this.lower_block(other); } else { val given := this.value_as(other, kind); this.adopt(&given); this.store(&result, &given); } this.end_scope(mark, 0); this.build.jump(join); this.build.seal(join); this.build.switch_to(join); val made := this.load_result(&result); this.keep(&made); return made; } #! `x as T`: between numbers, between integers and enums, which checks an #! integer is a member's value, between pointers and integers, a reference to #! the address it holds, and whatever converts without `as`. #! @param this The emitter. #! @param node The `cast` node. #! @returns The value converted. pub def emitter::cast(&mut this, node: u32): operand { val inner := this.c.checker.left_of(node); val to := this.c.checker.type_at(node); val target := this.c.layout_type(to); val value := this.value(inner); val from_type := this.c.layout_type(this.c.checker.read_type(value.kind)); val reads_through := this.c.is_reference_type(value.kind) && this.c.checker.types.kind_of(target) != ty::pointer && !this.c.checker.is_std(target, word::cfn) && !this.c.is_reference_type(to); val source := reads_through ? this.through_references(&value) : value; val from := this.c.layout_type(source.kind); val fk := this.c.checker.types.kind_of(from); val tok := this.c.checker.types.kind_of(target); # An integer, or another enum, becomes an enum only as the value of a member. if this.c.checker.named_kind(target) == dk::enum_type && this.c.checker.named_kind(from) != dk::enum_type || this.c.checker.named_kind(target) == dk::enum_type && from != target { val wide := this.numeric(source.words[0], from, this.is_signed(from) ? bt::int64 : bt::uint64); this.check_member(wide, this.is_signed(from), target); return operand::one(to, this.numeric(source.words[0], from, to)); } # A reference is the address it holds. if (tok == ty::pointer || this.c.checker.is_std(target, word::cfn)) && fk == ty::reference { return operand::one(to, source.words[0]); } if tok == ty::pointer || this.c.checker.is_std(target, word::cfn) || fk == ty::pointer || this.c.checker.is_std(from, word::cfn) { # While compiling, an address is no number, since where memory is changes from # one build to the next, so what turns one into a number does not run then. if this.c.options.evaluating && tok != ty::pointer && !this.c.checker.is_std(target, word::cfn) { this.build.trap(fault::not_constant as u32); } var made := source; made.count = 1; made.kind = to; return made; } if (this.c.checker.types.is_numeric(from) || this.c.checker.types.is_boolean(from) || this.c.checker.named_kind(from) == dk::enum_type) && (this.c.checker.types.is_numeric(target) || this.c.checker.types.is_boolean(target)) { if this.c.checker.types.is_boolean(target) && !this.c.checker.types.is_boolean(from) { if this.is_float(from) { return operand::one(to, this.build.binary(op::fne, source.words[0], this.build.real(0))); } return operand::one(to, this.build.binary(op::ne, source.words[0], this.build.word(0))); } return operand::one(to, this.numeric(source.words[0], from, to)); } return this.convert(&source, to); } #! A number converted to another numeric type, as the machine converts it: #! integers wrap to their bits, floats round toward zero and saturate to #! integers, and an `f32` is rounded to its precision. #! @param this The emitter. #! @param value The number. #! @param from Its type. #! @param to The type converted to. #! @returns The number converted. pub def emitter::numeric(&mut this, value: u32, from: u32, to: u32): u32 { val source := this.c.shape_of(from); val target := this.c.shape_of(to); val floating := source.kind == lk::floating; if target.kind == lk::floating { var made := floating ? value : this.build.unary(source.kind == lk::signed ? op::itof : op::utof, value); if target.bits <= 32 { made = this.build.unary(op::fsingle, made); } return made; } var made := floating ? this.build.unary(target.kind == lk::signed ? op::ftoi : op::ftou, value) : value; if (target.kind == lk::signed || target.kind == lk::unsigned) && target.bits < 64 { made = this.build.wrap(made, target.bits, target.kind == lk::signed); } if target.kind == lk::boolean { made = this.build.binary(op::ne, made, this.build.word(0)); } return made; } #! Stops the program unless an integer is the value of a member of the enum, #! where the code checks: it is where it lies within one of the runs of #! consecutive values the members have, which `value - first <= last - first` #! asks of each without its sign. #! @param this The emitter. #! @param value The integer, widened to 64 bits. #! @param signed Whether it is signed. #! @param kind The enum's type. def emitter::check_member(&mut this, value: u32, signed: bool, kind: u32): void { return if !this.checking(); val decl := this.c.checker.types.first_of(this.c.checker.unalias(kind)); var values := vector::::new(); val flip: u64 = signed ? 0x8000000000000000u : 0; val run: u32 = this.c.checker.decls.children.p[decl]; for i in 0..this.c.checker.decls.list_length(run) { var held := constant {}; continue if !this.c.checker.decl_value(this.c.checker.decls.list_at(run, i as u32), &mut held); continue if held.negative && !signed; continue if !held.negative && signed && held.magnitude > 0x7FFFFFFFFFFFFFFFu; values.append(held.bits() ^ flip); } values.sort(); var member: u32 = this.build.word(0); var i: u64 = 0; while i < values.length { var end := i + 1; while end < values.length && values[end] - values[end - 1] <= 1 { end++; } val offset := this.build.binary(op::sub, value, this.build.word(values[i] ^ flip)); val within := this.build.binary(op::leu, offset, this.build.word(values[end - 1] - values[i])); member = this.build.binary(op::bor, member, within); i = end; } this.build.check(member, fault::not_member as u32); } #! The runs of consecutive values an enum's members have, from the least, as #! the first and last value of each, flipped as `checker::member_order` gives #! them. #! @param this The emitter. #! @param kind The enum's type. #! @returns The first and last of each run, one after the other. def emitter::member_runs(&mut this, kind: u32): vector { val values := this.c.checker.member_order(kind); var made := vector::::new(); var i: u64 = 0; while i < values.length { var end := i + 1; while end < values.length && values[end] - values[end - 1] == 1 { end++; } made.append(values[i]); made.append(values[end - 1]); i = end; } return made; } #! The place of a member of an enum among the values of its members, from 0 #! for the least, as a range of its members counts: how far it is past the #! least, less the values missing between the runs before it. #! @param this The emitter. #! @param value The member's value, a word. #! @param kind The enum's type. #! @returns Its place, a word. pub def emitter::member_rank(&mut this, value: u32, kind: u32): u32 { val runs := this.member_runs(kind); return this.build.word(0) if runs.length == 0; val flip := this.c.checker.member_flip(kind); val key := flip == 0 ? value : this.build.binary(op::bxor, value, this.build.word(flip)); var made := this.build.binary(op::sub, key, this.build.word(runs[0])); var k: u64 = 2; while k < runs.length { val past := this.build.binary(op::leu, this.build.word(runs[k]), key); val missing := this.build.binary(op::mul, past, this.build.word(runs[k] - runs[k - 1] - 1)); made = this.build.binary(op::sub, made, missing); k += 2; } return made; } #! The member of an enum at a place among the values of its members, which #! `member_rank` gives: the least, past it by the place, and past the values #! missing between the runs before it. #! @param this The emitter. #! @param rank The place, a word, before the count of the members' values. #! @param kind The enum's type. #! @returns The member's value, a word. pub def emitter::member_of_rank(&mut this, rank: u32, kind: u32): u32 { val runs := this.member_runs(kind); return this.build.word(0) if runs.length == 0; var made := this.build.binary(op::add, rank, this.build.word(runs[0])); var before: u64 = runs[1] - runs[0] + 1; var k: u64 = 2; while k < runs.length { val past := this.build.binary(op::leu, this.build.word(before), rank); val missing := this.build.binary(op::mul, past, this.build.word(runs[k] - runs[k - 1] - 1)); made = this.build.binary(op::add, made, missing); before += runs[k + 1] - runs[k] + 1; k += 2; } val flip := this.c.checker.member_flip(kind); return flip == 0 ? made : this.build.binary(op::bxor, made, this.build.word(flip)); } #! `x is field`, `x is field(v)`, or `x is (d)`: whether a union holds a field, #! with the value it holds named where it does, a copy of it, or with `&` or #! `&mut` a reference to it. A test of what a type satisfies was decided while #! checking, which its value gives. #! @param this The emitter. #! @param node The `is_test` node. #! @returns Whether it holds, a `bool`. pub def emitter::is_test(&mut this, node: u32): operand { val kind := this.c.checker.type_at(node); val field := this.c.checker.ref_at(node); if field == 0 || (field & (ref_local | ref_type)) != 0 || this.c.checker.decls.kinds.p[field] != dk::field { this.problem(node, "The test is not decided."); return operand::one(kind, this.build.word(0)); } val whole := this.object(this.c.checker.left_of(node)); val plain := this.c.checker.unalias(whole.kind); val index := this.c.layout_of(plain); val shape := this.c.layout_at(index); var at: u32 = 0; for i in 0..shape.count { if this.c.field_at(index, i as u32).decl == field { at = i as u32; } } val held := this.c.field_at(index, at); val holds := this.tag_is(&whole, at); val negated := this.c.checker.node_has(node, nf::negated); # The value it holds is named only where it holds it, and taken out where it # closes, as a value a call made is; `_` names nothing, so takes nothing. val binding := this.c.checker.named_binding(this.c.checker.record(node, is_slot::binding)); if binding != 0 { val refers := this.c.checker.kind_of(binding) == nk::reference; val named := refers ? this.c.checker.left_of(binding) : binding; if this.local_of(named) == 0 { val place := this.new_place(named, this.c.checker.type_at(named)); val closes := this.c.checker.closes(this.c.checker.type_at(named)); if closes { this.zero(&place); } this.declare(named, place, closes); } val local: local_info = this.locals[this.local_of(named) - 1]; val taking := this.build.new_block(); val after := this.build.new_block(); this.build.branch(holds, taking, after); this.build.seal(taking); this.build.switch_to(taking); val payload := this.within(&whole, held.offset, held.type_id); val bound := this.c.checker.type_at(named); if refers && !this.c.checker.has_flag(binding, rf::relent) { val made := this.reference_to(&payload, bound); this.store(&local.place, &made); } else { # A field holding a reference gives that reference, or what it reaches. val loaded := this.load(&payload); val made := this.convert(&loaded, bound); this.store(&local.place, &made); if this.c.checker.closes(held.type_id) { this.zero(&payload); } } this.build.jump(after); this.build.seal(after); this.build.switch_to(after); } return operand::one(kind, negated ? this.build.binary(op::eq, holds, this.build.word(0)) : holds); } #! While compiling, stops unless two pointers are within the same memory. #! @param this The emitter. #! @param a One pointer. #! @param b The other. pub def emitter::same_memory(&mut this, a: u32, b: u32): void { var args: u32[2] = [a, b]; this.build.effect(op::same, args[0:2], 0, 0); } #! While compiling, or running with its memory checked, stops unless elements of #! a type from a pointer lie in memory the program holds. #! @param this The emitter. #! @param pointer The first element. #! @param count How many there are. #! @param size The bytes of each. pub def emitter::reaches(&mut this, pointer: u32, count: u32, size: u64): void { return if !this.c.options.evaluating && !this.c.options.checks; var args: u32[2] = [pointer, count]; this.build.effect(op::reach, args[0:2], 0, size); }