310 lines
13 KiB
Plaintext
310 lines
13 KiB
Plaintext
#! @file builtins.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the methods the compiler gives the built-in types, which make
|
||
|
|
#! numbers, `bool`, enums, and pointers `equatable`, `hashable`, `ordered`, and
|
||
|
|
#! `numeric` as far as each allows: written where they are called, and made
|
||
|
|
#! functions of their own where a vtable names them.
|
||
|
|
|
||
|
|
import sakura::analyzer;
|
||
|
|
module sakura::compiler;
|
||
|
|
|
||
|
|
|
||
|
|
#! A call of a method the compiler gives a built-in type, on the value it is
|
||
|
|
#! called on and the other operand.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param node The `call` node.
|
||
|
|
#! @returns What it gives.
|
||
|
|
pub def emitter::builtin_call(&mut this, node: u32): operand {
|
||
|
|
val callee := this.c.checker.left_of(node);
|
||
|
|
val object := this.c.checker.left_of(callee);
|
||
|
|
val name := this.c.checker.name_of(callee);
|
||
|
|
|
||
|
|
# `f.invoke(args)` calls the function, or the C function, the value is.
|
||
|
|
if name == word::invoke {
|
||
|
|
val made := this.value_of_type(object);
|
||
|
|
val function := this.through_references(&made);
|
||
|
|
var given := gathered { values = vector::<operand>::new() };
|
||
|
|
this.gather(node, 0, 0, this.called_type(function.kind), 0, &mut given);
|
||
|
|
return this.call_function_value(&function, &given);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A fixed array is compared and hashed element by element, where it is.
|
||
|
|
val reached := this.c.checker.unalias(this.c.checker.reached(this.c.checker.type_at(object)));
|
||
|
|
if this.c.checker.types.kind_of(reached) == ty::fixed {
|
||
|
|
val whole := this.object(object);
|
||
|
|
val first := this.address_of(&whole);
|
||
|
|
val result := this.c.checker.type_at(node);
|
||
|
|
if name == word::hash {
|
||
|
|
return operand::one(result, this.hash_elements(first, reached));
|
||
|
|
}
|
||
|
|
val run := this.c.checker.right_of(node);
|
||
|
|
val other := this.object(this.c.checker.run_at(run, 0));
|
||
|
|
return operand::one(result, this.equal_elements(first, this.address_of(&other), reached));
|
||
|
|
}
|
||
|
|
|
||
|
|
val made := this.value_of_type(object);
|
||
|
|
val subject := this.through_references(&made);
|
||
|
|
var other := operand {};
|
||
|
|
val run := this.c.checker.right_of(node);
|
||
|
|
if this.c.checker.run_length(run) != 0 {
|
||
|
|
val given := this.value_of_type(this.c.checker.run_at(run, 0));
|
||
|
|
other = this.through_references(&given);
|
||
|
|
other = this.convert(&other, subject.kind);
|
||
|
|
}
|
||
|
|
return this.builtin_method(name, &subject, &other, this.c.checker.type_at(node));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What a method the compiler gives a built-in type gives: `equals` compares,
|
||
|
|
#! `hash` gives the bits, as 0 for both zeros of a float, `compare` gives -1, 0,
|
||
|
|
#! or 1, and the methods of `numeric` operate.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param name The ID of the method's name.
|
||
|
|
#! @param a The value it is called on.
|
||
|
|
#! @param b The other operand, where it takes one.
|
||
|
|
#! @param result The type it gives.
|
||
|
|
#! @returns What it gives.
|
||
|
|
pub def emitter::builtin_method(&mut this, name: u32, a: &operand, b: &operand, result: u32): operand {
|
||
|
|
val kind := a.kind;
|
||
|
|
val floating := this.is_float(kind);
|
||
|
|
val signed := this.is_signed(kind);
|
||
|
|
if name == word::equals {
|
||
|
|
return operand::one(result, this.build.binary(floating ? op::feq : op::eq, a.words[0], b.words[0]));
|
||
|
|
}
|
||
|
|
if name == word::compare {
|
||
|
|
val less := floating ? op::flt : (signed ? op::lts : op::ltu);
|
||
|
|
val below := this.build.binary(less, a.words[0], b.words[0]);
|
||
|
|
val above := this.build.binary(less, b.words[0], a.words[0]);
|
||
|
|
return operand::one(result, this.build.binary(op::sub, above, below));
|
||
|
|
}
|
||
|
|
if name == word::hash {
|
||
|
|
if !floating {
|
||
|
|
return operand::one(result, a.words[0]);
|
||
|
|
}
|
||
|
|
# Both zeros hash as 0, and anything else as its bits, an `f32`'s as one.
|
||
|
|
val shape := this.c.shape_of(kind);
|
||
|
|
val cell := this.temporary(word_size, word_size);
|
||
|
|
val address := this.build.cell_address(cell);
|
||
|
|
this.build.store(shape.bits <= 32 ? access::f32 : access::f64, address, 0, a.words[0]);
|
||
|
|
val bits := this.build.load(shape.bits <= 32 ? access::u32 : access::i64, address, 0);
|
||
|
|
val zero := this.build.binary(op::feq, a.words[0], this.build.real(0));
|
||
|
|
val nonzero := this.build.binary(op::eq, zero, this.build.word(0));
|
||
|
|
val mask := this.build.unary(op::neg, nonzero);
|
||
|
|
return operand::one(result, this.build.binary(op::band, bits, mask));
|
||
|
|
}
|
||
|
|
if name == word::negate {
|
||
|
|
val made := this.build.unary(floating ? op::fneg : op::neg, a.words[0]);
|
||
|
|
return operand::one(result, this.normalize(made, kind));
|
||
|
|
}
|
||
|
|
if name == word::bit_not {
|
||
|
|
return operand::one(result, this.normalize(this.build.unary(op::bnot, a.words[0]), kind));
|
||
|
|
}
|
||
|
|
|
||
|
|
var chosen := op::add;
|
||
|
|
if name == word::add {
|
||
|
|
chosen = floating ? op::fadd : op::add;
|
||
|
|
} else if name == word::sub {
|
||
|
|
chosen = floating ? op::fsub : op::sub;
|
||
|
|
} else if name == word::mul {
|
||
|
|
chosen = floating ? op::fmul : op::mul;
|
||
|
|
} else if name == word::div {
|
||
|
|
chosen = floating ? op::fdiv : (signed ? op::divs : op::divu);
|
||
|
|
} else if name == word::rem {
|
||
|
|
chosen = floating ? op::frem : (signed ? op::rems : op::remu);
|
||
|
|
} else if name == word::bit_and {
|
||
|
|
chosen = op::band;
|
||
|
|
} else if name == word::bit_or {
|
||
|
|
chosen = op::bor;
|
||
|
|
} else if name == word::bit_xor {
|
||
|
|
chosen = op::bxor;
|
||
|
|
} else if name == word::shift_left {
|
||
|
|
chosen = op::shl;
|
||
|
|
} else if name == word::shift_right {
|
||
|
|
chosen = signed ? op::shrs : op::shru;
|
||
|
|
}
|
||
|
|
return operand::one(result, this.normalize(this.build.binary(chosen, a.words[0], b.words[0]), kind));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The function giving a built-in type a method, as a vtable names it, made
|
||
|
|
#! where it is new: it takes `this` and the other operand by reference, as the
|
||
|
|
#! method's type says.
|
||
|
|
#! @param this The compiler.
|
||
|
|
#! @param kind The built-in type.
|
||
|
|
#! @param name The ID of the method's name.
|
||
|
|
#! @returns The function's index, or `no_function` where the type has no such
|
||
|
|
#! method.
|
||
|
|
pub def lowerer::builtin_function(&mut this, kind: u32, name: u32): u32 {
|
||
|
|
val type_of := this.checker.builtin_method(kind, name);
|
||
|
|
return no_function if type_of == 0;
|
||
|
|
val key := pair_key(kind, name);
|
||
|
|
if this.builtin_ids.get(&key) is some(got) {
|
||
|
|
return got;
|
||
|
|
}
|
||
|
|
var text := string::from(this.checker.names.text(name), nothing {});
|
||
|
|
val index := this.add_function(move(&mut text), this.signature_of(this.call_type(type_of)));
|
||
|
|
this.builtin_ids.set(key, index);
|
||
|
|
this.program.functions[index].builtin = name;
|
||
|
|
this.program.functions[index].owner_type = kind;
|
||
|
|
this.want(index);
|
||
|
|
return index;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lowers a function giving a built-in type a method.
|
||
|
|
#! @param this The emitter.
|
||
|
|
pub def emitter::lower_builtin(&mut this): void {
|
||
|
|
val name: u32 = this.c.program.functions[this.function].builtin;
|
||
|
|
val kind: u32 = this.c.program.functions[this.function].owner_type;
|
||
|
|
|
||
|
|
# `invoke` calls the function, or the C function, its `this` refers to, with
|
||
|
|
# the arguments it is given, as they are.
|
||
|
|
if name == word::invoke {
|
||
|
|
val signature: u32 = this.c.program.functions[this.function].signature;
|
||
|
|
val total := this.c.program.signatures[signature].params.length as u32;
|
||
|
|
var next: u32 = 0;
|
||
|
|
val taken := this.take_param(signature, 0, &mut next);
|
||
|
|
val place := location { kind = kind, address = taken.words[0] };
|
||
|
|
val function := this.load(&place);
|
||
|
|
var given := gathered { values = vector::<operand>::new() };
|
||
|
|
for p in 1..total {
|
||
|
|
given.values.append(this.take_param(signature, p as u32, &mut next));
|
||
|
|
}
|
||
|
|
val made := this.call_function_value(&function, &given);
|
||
|
|
var args := vector::<u32>::new();
|
||
|
|
made.args(&mut args);
|
||
|
|
this.give_back(args.span);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if this.c.checker.types.kind_of(this.c.checker.unalias(kind)) == ty::fixed {
|
||
|
|
val plain := this.c.checker.unalias(kind);
|
||
|
|
var words: u32[1] = [0];
|
||
|
|
if name == word::hash {
|
||
|
|
words[0] = this.hash_elements(this.build.param(0), plain);
|
||
|
|
} else {
|
||
|
|
words[0] = this.equal_elements(this.build.param(0), this.build.param(2), plain);
|
||
|
|
}
|
||
|
|
this.build.ret(words[0:1]);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val first := location { kind = kind, address = this.build.param(0) };
|
||
|
|
val a := this.load(&first);
|
||
|
|
var b := operand {};
|
||
|
|
if this.c.program.signatures[this.c.program.functions[this.function].signature].params.length > 1 {
|
||
|
|
val second := location { kind = kind, address = this.build.param(2) };
|
||
|
|
b = this.load(&second);
|
||
|
|
}
|
||
|
|
val result := this.c.checker.types.first_of(this.c.checker.builtin_method(kind, name));
|
||
|
|
val made := this.builtin_method(name, &a, &b, result);
|
||
|
|
var args := vector::<u32>::new();
|
||
|
|
made.args(&mut args);
|
||
|
|
this.give_back(args.span);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether the elements of two fixed arrays are equal, one by one, as each
|
||
|
|
#! says it is equal: a number by its value, and a struct by its `equals`.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param a The address of one array.
|
||
|
|
#! @param b The address of the other.
|
||
|
|
#! @param kind The arrays' type.
|
||
|
|
#! @returns A word of 1 where they are, and 0 where not.
|
||
|
|
pub def emitter::equal_elements(&mut this, a: u32, b: u32, kind: u32): u32 {
|
||
|
|
val element := this.c.checker.types.first_of(kind);
|
||
|
|
val count := this.c.checker.types.second_of(kind) as u64;
|
||
|
|
val size := this.c.shape_of(element).size;
|
||
|
|
val same := this.build.variable(vt::word);
|
||
|
|
val index := this.build.variable(vt::word);
|
||
|
|
this.build.assign(same, this.build.word(1));
|
||
|
|
this.build.assign(index, this.build.word(0));
|
||
|
|
|
||
|
|
val head := this.build.new_block();
|
||
|
|
val body := this.build.new_block();
|
||
|
|
val differs := this.build.new_block();
|
||
|
|
val next := this.build.new_block();
|
||
|
|
val done := this.build.new_block();
|
||
|
|
val at := this.count_up(index, count, head, body, done);
|
||
|
|
val offset := this.scaled(at, size);
|
||
|
|
val equal := this.element_equals(this.build.binary(op::add, a, offset), this.build.binary(op::add, b, offset), element);
|
||
|
|
this.build.branch(equal, next, differs);
|
||
|
|
this.build.seal(differs);
|
||
|
|
this.build.switch_to(differs);
|
||
|
|
this.build.assign(same, this.build.word(0));
|
||
|
|
this.build.jump(done);
|
||
|
|
this.build.seal(next);
|
||
|
|
this.build.switch_to(next);
|
||
|
|
this.count_on(index, head, done);
|
||
|
|
return this.build.read(same);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether two values of a type, at their addresses, are equal, as the type
|
||
|
|
#! says it is.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param a The address of one.
|
||
|
|
#! @param b The address of the other.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @returns A word of 1 where they are, and 0 where not.
|
||
|
|
def emitter::element_equals(&mut this, a: u32, b: u32, kind: u32): u32 {
|
||
|
|
val plain := this.c.checker.unalias(kind);
|
||
|
|
if this.c.checker.types.kind_of(plain) == ty::fixed {
|
||
|
|
return this.equal_elements(a, b, plain);
|
||
|
|
}
|
||
|
|
val first := location { kind = kind, address = a };
|
||
|
|
val second := location { kind = kind, address = b };
|
||
|
|
if this.c.checker.find_member(kind, word::equals).decl == 0 {
|
||
|
|
val x := this.load(&first);
|
||
|
|
val y := this.load(&second);
|
||
|
|
return this.builtin_method(word::equals, &x, &y, bt::boolean).words[0];
|
||
|
|
}
|
||
|
|
val method := this.c.checker.find_member(kind, word::equals).decl;
|
||
|
|
val reference := this.c.checker.types.reference(kind, false);
|
||
|
|
val subject := this.reference_to(&first, reference);
|
||
|
|
var others := vector::<operand>::new();
|
||
|
|
others.append(this.reference_to(&second, reference));
|
||
|
|
return this.method_call(method, kind, &subject, others.span).words[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The hash of a fixed array: of its length, then each element's in turn,
|
||
|
|
#! mixed as the C compiler mixes them.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param a The array's address.
|
||
|
|
#! @param kind Its type.
|
||
|
|
#! @returns The hash.
|
||
|
|
pub def emitter::hash_elements(&mut this, a: u32, kind: u32): u32 {
|
||
|
|
val element := this.c.checker.types.first_of(kind);
|
||
|
|
val count := this.c.checker.types.second_of(kind) as u64;
|
||
|
|
val size := this.c.shape_of(element).size;
|
||
|
|
val code := this.build.variable(vt::word);
|
||
|
|
val index := this.build.variable(vt::word);
|
||
|
|
this.build.assign(code, this.build.word(count));
|
||
|
|
this.build.assign(index, this.build.word(0));
|
||
|
|
|
||
|
|
val head := this.build.new_block();
|
||
|
|
val body := this.build.new_block();
|
||
|
|
val done := this.build.new_block();
|
||
|
|
val at := this.count_up(index, count, head, body, done);
|
||
|
|
val each := this.element_hash(this.build.binary(op::add, a, this.scaled(at, size)), element);
|
||
|
|
val mixed := this.build.binary(op::mul, this.build.binary(op::bxor, this.build.read(code), each), this.build.word(0x9E3779B97F4A7C15));
|
||
|
|
this.build.assign(code, this.build.binary(op::bxor, mixed, this.build.binary(op::shru, mixed, this.build.word(29))));
|
||
|
|
this.count_on(index, head, done);
|
||
|
|
return this.build.read(code);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The hash of a value of a type, at its address, as the type gives it.
|
||
|
|
#! @param this The emitter.
|
||
|
|
#! @param a The value's address.
|
||
|
|
#! @param kind The type.
|
||
|
|
#! @returns The hash.
|
||
|
|
def emitter::element_hash(&mut this, a: u32, kind: u32): u32 {
|
||
|
|
val plain := this.c.checker.unalias(kind);
|
||
|
|
if this.c.checker.types.kind_of(plain) == ty::fixed {
|
||
|
|
return this.hash_elements(a, plain);
|
||
|
|
}
|
||
|
|
val place := location { kind = kind, address = a };
|
||
|
|
val method := this.c.checker.find_member(kind, word::hash).decl;
|
||
|
|
if method == 0 {
|
||
|
|
val x := this.load(&place);
|
||
|
|
return this.builtin_method(word::hash, &x, &operand {}, bt::uint64).words[0];
|
||
|
|
}
|
||
|
|
val subject := this.reference_to(&place, this.c.checker.types.reference(kind, false));
|
||
|
|
return this.method_call(method, kind, &subject, span::<operand>::blank()).words[0];
|
||
|
|
}
|