238 lines
8.6 KiB
Plaintext
238 lines
8.6 KiB
Plaintext
#! @file match.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the lowering of `match`: the first arm naming the field a union
|
||
|
|
#! holds, or the member an enum is, or a value equal to the subject, runs, or
|
||
|
|
#! else the `else` arm. A union's arm may name the value its field holds, a
|
||
|
|
#! copy of it, taken out where it closes, or with `&` or `&mut` a reference to
|
||
|
|
#! it. A match used as a value gives what its arm gives.
|
||
|
|
|
||
|
|
import sakura::analyzer;
|
||
|
|
module sakura::compiler;
|
||
|
|
|
||
|
|
|
||
|
|
#! A `match` used as a value.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param node The `match_value` node.
|
||
|
|
#! @returns What the arm taken gives.
|
||
|
|
pub def emitter::match_value(&mut this, node: u32): operand {
|
||
|
|
return this.lower_match(node, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lowers a match, as a statement or as a value.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param node The `match_stmt` or `match_value` node.
|
||
|
|
#! @param value Whether it is used as a value.
|
||
|
|
#! @returns What the arm taken gives, where it is used as a value.
|
||
|
|
pub def emitter::lower_match(&mut this, node: u32, value: bool): operand {
|
||
|
|
val subject := this.c.checker.left_of(node);
|
||
|
|
val arms := this.c.checker.right_of(node);
|
||
|
|
val kind := value ? this.c.checker.type_at(node) : bt::void_type;
|
||
|
|
val whole := this.object(subject);
|
||
|
|
val plain := this.c.checker.unalias(whole.kind);
|
||
|
|
val union_like := this.c.checker.named_kind(plain) == dk::union_type;
|
||
|
|
|
||
|
|
# Values are compared with the subject as read once, before any arm runs.
|
||
|
|
var held := operand {};
|
||
|
|
if !union_like {
|
||
|
|
held = this.load(&whole);
|
||
|
|
}
|
||
|
|
|
||
|
|
val result := value ? this.result_place(kind) : location {};
|
||
|
|
val join := this.build.new_block();
|
||
|
|
var otherwise := false;
|
||
|
|
for i in 0..this.c.checker.run_length(arms) {
|
||
|
|
val arm := this.c.checker.run_at(arms, i as u32);
|
||
|
|
val patterns := this.c.checker.left_of(arm);
|
||
|
|
val body := this.c.checker.right_of(arm);
|
||
|
|
if patterns == 0 {
|
||
|
|
this.lower_arm(arm, body, value, kind, &result, &whole, 0, 0);
|
||
|
|
this.build.jump(join);
|
||
|
|
otherwise = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The arm runs where any of its patterns does.
|
||
|
|
val taken := this.build.new_block();
|
||
|
|
val next := this.build.new_block();
|
||
|
|
var field: u32 = 0;
|
||
|
|
var binding: u32 = 0;
|
||
|
|
for j in 0..this.c.checker.run_length(patterns) {
|
||
|
|
val pattern := this.c.checker.run_at(patterns, j as u32);
|
||
|
|
val failed := j + 1 == this.c.checker.run_length(patterns) as u64 ? next : this.build.new_block();
|
||
|
|
var test: u32 = 0;
|
||
|
|
if union_like {
|
||
|
|
field = this.c.checker.ref_at(pattern);
|
||
|
|
if this.c.checker.kind_of(pattern) == nk::call {
|
||
|
|
val given := this.c.checker.right_of(pattern);
|
||
|
|
if this.c.checker.run_length(given) == 1 {
|
||
|
|
binding = this.c.checker.named_binding(this.c.checker.run_at(given, 0));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
test = this.tag_is(&whole, this.field_index(plain, field));
|
||
|
|
} else {
|
||
|
|
test = this.equals_value(&held, pattern, plain);
|
||
|
|
}
|
||
|
|
this.build.branch(test, taken, failed);
|
||
|
|
if failed != next {
|
||
|
|
this.build.seal(failed);
|
||
|
|
this.build.switch_to(failed);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.build.seal(taken);
|
||
|
|
this.build.seal(next);
|
||
|
|
|
||
|
|
this.build.switch_to(taken);
|
||
|
|
this.lower_arm(arm, body, value, kind, &result, &whole, field, binding);
|
||
|
|
this.build.jump(join);
|
||
|
|
this.build.switch_to(next);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A match used as a value with no `else` finds no arm only for a value no enum
|
||
|
|
# member has, which only memory written through pointers can hold, and which
|
||
|
|
# only code that checks looks for.
|
||
|
|
if !otherwise {
|
||
|
|
if value && this.checking() {
|
||
|
|
this.build.trap(fault::not_member as u32);
|
||
|
|
} else if value {
|
||
|
|
this.build.unreachable();
|
||
|
|
} else {
|
||
|
|
this.build.jump(join);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.build.seal(join);
|
||
|
|
this.build.switch_to(join);
|
||
|
|
return operand::none(bt::void_type) if !value;
|
||
|
|
val made := this.load_result(&result);
|
||
|
|
this.keep(&made);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The index of a union's field among its fields.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param kind The union's type, without aliases.
|
||
|
|
#! @param field The field's declaration.
|
||
|
|
#! @returns Its index.
|
||
|
|
pub def emitter::field_index(&mut this, kind: u32, field: u32): u32 {
|
||
|
|
val index := this.c.layout_of(kind);
|
||
|
|
for i in 0..this.c.layout_at(index).count {
|
||
|
|
return i as u32 if this.c.field_at(index, i as u32).decl == field;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a value equals a pattern's: as words, or through `equals`, or part by
|
||
|
|
#! part for what the language compares that way.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param held The value.
|
||
|
|
#! @param pattern The pattern's node.
|
||
|
|
#! @param kind The type compared, without aliases.
|
||
|
|
#! @returns A word of 1 where they are equal.
|
||
|
|
def emitter::equals_value(&mut this, held: &operand, pattern: u32, kind: u32): u32 {
|
||
|
|
val given := this.value_as(pattern, kind);
|
||
|
|
val shape := this.c.shape_of(kind);
|
||
|
|
if !in_memory(&shape) {
|
||
|
|
if this.c.checker.has_method(kind, word::equals) && this.c.checker.named_kind(kind) != dk::enum_type
|
||
|
|
&& !this.c.checker.types.is_numeric(kind) && this.c.checker.types.kind_of(kind) != ty::pointer
|
||
|
|
&& !this.c.checker.types.is_boolean(kind) {
|
||
|
|
return this.equals_call(held, &given, kind);
|
||
|
|
}
|
||
|
|
val floating := shape.kind == lk::floating;
|
||
|
|
return this.build.binary(floating ? op::feq : op::eq, held.words[0], given.words[0]);
|
||
|
|
}
|
||
|
|
if this.c.checker.has_method(kind, word::equals) {
|
||
|
|
return this.equals_call(held, &given, kind);
|
||
|
|
}
|
||
|
|
var args: u32[2] = [held.address, given.address];
|
||
|
|
return this.build.value(op::equal, args[0:2], vt::word, this.c.layout_of(kind), 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Calls a type's `equals` on two values, given references to each.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param a One value.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @param kind Their type, without aliases.
|
||
|
|
#! @returns A word of 1 where they are equal.
|
||
|
|
def emitter::equals_call(&mut this, a: &operand, b: &operand, kind: u32): u32 {
|
||
|
|
val method := this.pick_method(kind, word::equals, true);
|
||
|
|
val reference := this.c.checker.types.reference(kind, false);
|
||
|
|
val left := this.hold(a);
|
||
|
|
val right := this.hold(b);
|
||
|
|
val subject := this.reference_to(&left, reference);
|
||
|
|
var others: operand[1] = [this.reference_to(&right, reference)];
|
||
|
|
val made := this.method_call(method, kind, &subject, others[0:1]);
|
||
|
|
return made.words[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lowers an arm: the value its pattern names, where it names one, and its body,
|
||
|
|
#! whose value the match gives where it is used as one.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param arm The arm's node.
|
||
|
|
#! @param body Its body's node.
|
||
|
|
#! @param value Whether the match is used as a value.
|
||
|
|
#! @param kind The match's type.
|
||
|
|
#! @param result Where its value goes.
|
||
|
|
#! @param whole The place of the subject.
|
||
|
|
#! @param field The field the arm names of a union, or 0.
|
||
|
|
#! @param binding The node naming the field's value, or 0.
|
||
|
|
def emitter::lower_arm(
|
||
|
|
&mut this,
|
||
|
|
arm: u32,
|
||
|
|
body: u32,
|
||
|
|
value: bool,
|
||
|
|
kind: u32,
|
||
|
|
result: &location,
|
||
|
|
whole: &location,
|
||
|
|
field: u32,
|
||
|
|
binding: u32
|
||
|
|
): void {
|
||
|
|
val locals := this.scope_mark();
|
||
|
|
var named: u32 = 0;
|
||
|
|
if binding != 0 && field != 0 {
|
||
|
|
val refers := this.c.checker.kind_of(binding) == nk::reference;
|
||
|
|
named = refers ? this.c.checker.left_of(binding) : binding;
|
||
|
|
val plain := this.c.checker.unalias(whole.kind);
|
||
|
|
val held := this.c.field_at(this.c.layout_of(plain), this.field_index(plain, field));
|
||
|
|
val payload := this.within(whole, held.offset, held.type_id);
|
||
|
|
val bound := this.c.checker.type_at(named);
|
||
|
|
val place := this.new_place(named, bound);
|
||
|
|
if refers {
|
||
|
|
# A field holding a reference gives that reference, since references
|
||
|
|
# never stack.
|
||
|
|
var made := operand {};
|
||
|
|
if this.c.checker.has_flag(binding, rf::relent) {
|
||
|
|
val inner := this.load(&payload);
|
||
|
|
made = this.convert(&inner, bound);
|
||
|
|
} else {
|
||
|
|
made = this.reference_to(&payload, bound);
|
||
|
|
}
|
||
|
|
this.store(&place, &made);
|
||
|
|
this.declare(named, place, false);
|
||
|
|
} else {
|
||
|
|
val made := this.load(&payload);
|
||
|
|
val converted := this.convert(&made, bound);
|
||
|
|
this.store(&place, &converted);
|
||
|
|
val closes := this.c.checker.closes(held.type_id);
|
||
|
|
if closes {
|
||
|
|
this.zero(&payload);
|
||
|
|
}
|
||
|
|
this.declare(named, place, closes);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# An arm giving the value its name took hands it over to the match.
|
||
|
|
var given: u32 = 0;
|
||
|
|
if value {
|
||
|
|
if this.c.checker.kind_of(body) == nk::name && named != 0 && (this.c.checker.ref_at(body) & ref_mask) == named {
|
||
|
|
given = named;
|
||
|
|
}
|
||
|
|
val made := this.value_as(body, kind);
|
||
|
|
this.adopt(&made);
|
||
|
|
this.store(result, &made);
|
||
|
|
} else {
|
||
|
|
this.lower_block(body);
|
||
|
|
}
|
||
|
|
this.end_scope(locals, given);
|
||
|
|
}
|