Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+487
View File
@@ -0,0 +1,487 @@
#! @file values.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the lowering of expressions to their values: what each kind of
#! node gives, the constants the checker worked out, and the conversions the
#! checker recorded, by which a value of one type is taken where another is
#! expected.
import sakura::analyzer;
module sakura::compiler;
#! The value of an expression as the type it is used as: the type the checker
#! converts it to, or its own.
#! @param this The emitter.
#! @param node The expression.
#! @returns The value.
pub def emitter::value(&mut this, node: u32): operand {
var to := this.c.checker.converted(node);
if this.c.checker.is_known(node) && this.c.is_scalar_type(to) {
# An integer constant past what an `i64` holds is unsigned, as `1 << 63` is.
val held := this.c.checker.value_at(node);
if this.c.checker.types.kind_of(to) == ty::untyped_int && !held.floating && !held.negative
&& held.magnitude > 0x7FFFFFFFFFFFFFFF {
to = bt::uint64;
}
return this.known(&held, to);
}
val own := this.raw(node);
return this.convert(&own, to);
}
#! The value of an expression as its own type, before any conversion.
#! @param this The emitter.
#! @param node The expression.
#! @returns The value.
pub def emitter::value_of_type(&mut this, node: u32): operand {
val own := this.c.checker.type_at(node);
if this.c.checker.is_known(node) && this.c.is_scalar_type(own) {
return this.known(&this.c.checker.value_at(node), own);
}
return this.raw(node);
}
#! The value of an expression converted to a type.
#! @param this The emitter.
#! @param node The expression.
#! @param to The type.
#! @returns The value.
pub def emitter::value_as(&mut this, node: u32, to: u32): operand {
if this.c.checker.is_known(node) && this.c.is_scalar_type(to) {
return this.known(&this.c.checker.value_at(node), to);
}
val made := this.value(node);
return this.convert(&made, to);
}
#! A constant the checker worked out, as a value of a type.
#! @param this The emitter.
#! @param value The constant.
#! @param kind The type, a number, `bool`, enum, or pointer.
#! @returns The value.
pub def emitter::known(&mut this, value: &constant, kind: u32): operand {
val held := this.c.shape_of(kind);
if held.kind == lk::floating {
var real := value.as_real();
if held.bits <= 32 {
real = (real as f32) as f64;
}
return operand::one(kind, this.build.real(bits_of_real(real)));
}
var bits := value.floating ? (value.real < 0.0 ? 0 - ((0.0 - value.real) as u64) : value.real as u64) : value.bits();
return operand::one(kind, this.build.word(bits));
}
#! The value of an expression as its own type, of each kind of node. What it
#! lowers is from the node, as what its operands lower is from theirs, and
#! what is lowered after it is from where it was before.
#! @param this The emitter.
#! @param node The expression.
#! @returns The value.
pub def emitter::raw(&mut this, node: u32): operand {
val saved := this.build.pos;
this.at_node(node);
val made := this.raw_node(node);
this.build.pos = saved;
return made;
}
#! The value of an expression as its own type, by the kind of its node.
#! @param this The emitter.
#! @param node The expression.
#! @returns The value.
def emitter::raw_node(&mut this, node: u32): operand {
val kind := this.c.checker.type_at(node);
if this.c.checker.has_flag(node, rf::reflected) {
return this.reflected_value(node);
}
match this.c.checker.kind_of(node) {
nk::number, nk::character, nk::boolean => return this.known(&this.c.checker.value_at(node), kind);
nk::string => {
# Text given to what is not text, as a union holding a view, is made as the
# view it settles as, which is then converted.
val to := this.c.checker.converted(node);
val own := this.c.is_string(to) || this.c.viewed(to) != 0 ? to : this.c.checker.settle(kind);
return this.text_literal(node, own);
}
nk::istring => return this.interpolated(node);
nk::null_lit => return this.zero_value(this.c.checker.converted(node));
nk::name, nk::path => return this.name_value(node);
nk::unary => return this.unary(node);
nk::postfix => return this.step(node, this.c.checker.left_of(node), false);
nk::reference => return this.reference(node);
nk::binary => return this.binary(node);
nk::assign => return this.assign(node);
nk::ternary => return this.ternary(node);
nk::fallback => return this.fallback(node);
nk::call => {
val made := this.call(node);
this.keep(&made);
return made;
}
nk::index => {
if this.indexes_through_at(node) {
return this.indexed_reference(node);
}
val place := this.place_of(node);
return this.load(&place);
}
# `x.(d)` reads the field or property a description names.
nk::member_of => return this.member_value(node);
# `value_of(d)::<i64>` is the instance its type arguments make.
nk::type_args => return this.function_value(node, this.c.checker.ref_at(node));
nk::slice => return this.slice(node);
nk::member => return this.member_value(node);
nk::cast => return this.cast(node);
nk::is_test => return this.is_test(node);
nk::construct => {
val made := this.construct(node);
this.keep(&made);
return made;
}
nk::array => {
val made := this.array_literal(node, this.c.checker.converted(node));
this.keep(&made);
return made;
}
nk::lambda => return this.lambda_value(node);
nk::match_value => return this.match_value(node);
nk::range => return this.range_value(node);
# What `e` was is kept where its `ensures` was reached.
nk::old_value => {
val local := this.local_of(node);
if local != 0 {
val held: location = this.locals[local as u64 - 1].place;
return this.load(&held);
}
}
else => {}
}
this.problem(node, "The expression is not lowered.");
return this.zero_value(kind);
}
#! The zero value of a type: zero words, or a zeroed cell.
#! @param this The emitter.
#! @param kind The type.
#! @returns The value.
pub def emitter::zero_value(&mut this, kind: u32): operand {
val held := this.c.shape_of(kind);
if in_memory(&held) {
val address := this.temporary_of(kind);
this.build.clear(address, held.size);
return operand::at(kind, address);
}
var made := operand { kind = kind, count = word_count(&held) };
for i in 0..made.count {
made.words[i] = word_type(&held) == vt::real ? this.build.real(0) : this.build.word(0);
}
return made;
}
#! The value a name or path gives: a local, a global, a constant, an enum
#! member, a constant parameter, or a function.
#! @param this The emitter.
#! @param node The `name` or `path` node.
#! @returns The value.
pub def emitter::name_value(&mut this, node: u32): operand {
val ref := this.c.checker.ref_at(node);
val kind := this.c.checker.type_at(node);
if (ref & ref_local) != 0 {
val place := this.place_of(node);
return this.load(&place);
}
if (ref & ref_type) != 0 {
return this.known_argument(ref & ref_mask, kind);
}
if ref == 0 {
this.problem(node, "The name refers to nothing.");
return this.zero_value(kind);
}
match this.c.checker.decls.kinds.p[ref] {
dk::function => return this.function_value(node, ref);
dk::variable => {
val place := this.global_place(ref, this.called(node));
return this.load(&place);
}
dk::enum_member => {
var value := constant {};
this.c.checker.decl_value(ref, &mut value);
return this.known(&value, kind);
}
else => {}
}
this.problem(node, "The name is no value.");
return this.zero_value(kind);
}
#! The value of a constant type argument a constant parameter names.
#! @param this The emitter.
#! @param arg The type argument, a constant.
#! @param kind The type of its value.
#! @returns The value.
pub def emitter::known_argument(&mut this, arg: u32, kind: u32): operand {
if this.c.checker.types.kind_of(arg) != ty::value {
this.problem(0, "A constant parameter is given no value.");
return this.zero_value(kind);
}
return this.known_object(this.c.checker.types.second_of(arg), kind);
}
#! A value the checker worked out, as a value of a type: a number, text, a
#! struct, union, or array of them, `null`, a function, or the address of a
#! constant.
#! @param this The emitter.
#! @param id The value's ID among the checker's known values.
#! @param kind The type.
#! @returns The value.
pub def emitter::known_object(&mut this, id: u32, kind: u32): operand {
val held: known = this.c.checker.known_at(id);
val shape := this.c.shape_of(kind);
match held.kind {
kk::number, kk::real => return this.known(&held.value, kind);
kk::null_lit => return this.zero_value(kind);
kk::text => return this.text_value(this.c.checker.known_text(id), kind);
kk::function => {
val index := this.c.function_of(held.decl, 0);
this.c.program.functions[index].taken = true;
this.c.names(this.function, index);
return operand::two(kind, this.build.word(index as u64 + 1), this.build.word(0));
}
kk::address => {
val place := this.global_place(held.decl);
return this.reference_to(&place, kind);
}
kk::fields => {
val address := this.temporary_of(kind);
this.build.clear(address, shape.size);
val whole := location { kind = kind, address = address, framed = true };
val plain := this.c.checker.unalias(kind);
if shape.kind == lk::fixed {
val element := this.c.checker.types.first_of(plain);
val inner := this.c.shape_of(element);
for i in 0..this.c.checker.known_count(id) {
val part := this.known_object(this.c.checker.known_part(id, i as u32), element);
val at := this.within(&whole, i as u64 * inner.size, element);
this.store(&at, &part);
}
return operand::at(kind, address);
}
val index := this.c.layout_of(plain);
for i in 0..this.c.checker.known_count(id) {
break if i >= shape.count as u64;
val field := this.c.field_at(index, i as u32);
val part := this.known_object(this.c.checker.known_part(id, i as u32), field.type_id);
val at := this.within(&whole, field.offset, field.type_id);
this.store(&at, &part);
}
return operand::at(kind, address);
}
kk::held => {
val address := this.temporary_of(kind);
this.build.clear(address, shape.size);
val tag := held.value.magnitude as u32;
if shape.niche == 0 {
this.build.store(access::i64, address, 0, this.build.word(tag as u64));
}
val index := this.c.layout_of(this.c.checker.unalias(kind));
if tag < shape.count && this.c.checker.known_count(id) != 0 {
val field := this.c.field_at(index, tag);
val part := this.known_object(this.c.checker.known_part(id, 0), field.type_id);
val at := location { kind = field.type_id, address = address, offset = field.offset, framed = true };
this.store(&at, &part);
}
return operand::at(kind, address);
}
else => {}
}
this.problem(0, "A constant argument is not worked out.");
return this.zero_value(kind);
}
#! The value text written as a literal is, as the type it is used as: a view of
#! the bytes it was written with, which stand as long as the program runs, or a
#! `string` holding a copy of them it owns.
#! @param this The emitter.
#! @param node The `string` node.
#! @param kind The type it is used as.
#! @returns The value.
pub def emitter::text_literal(&mut this, node: u32, kind: u32): operand {
var text := string::new(nothing {});
this.decode_literal(this.c.checker.text_of(node), &mut text);
return this.text_value(text.span, kind);
}
#! Text as a value of a type: a view of a datum holding its bytes, or a
#! `string` holding a copy of them.
#! @param this The emitter.
#! @param text The bytes.
#! @param kind The type: a view of bytes, or `string`.
#! @returns The value.
pub def emitter::text_value(&mut this, text: strview, kind: u32): operand {
val datum := this.c.text_datum(text);
if this.c.is_string(kind) {
this.build.effect(op::format, span::<u32>::blank(), 0, 0);
this.build.effect(op::format_text, span::<u32>::blank(), datum, 0);
return this.owned_text(kind);
}
val address := this.temporary_of(kind);
val data := text.length == 0 ? this.build.word(0) : this.build.value(op::datum, span::<u32>::blank(), vt::word, datum, 0);
this.build.store(access::i64, address, 0, data);
this.build.store(access::i64, address, word_size, this.build.word(text.length));
return operand::at(kind, address);
}
#! A value of a type taken as another, as the checker converts it: widened,
#! read through references, given to a union, referred to as a trait, or lent
#! as a view.
#! @param this The emitter.
#! @param value The value.
#! @param to The type it is taken as.
#! @returns The value converted.
pub def emitter::convert(&mut this, value: &operand, to: u32): operand {
var made: operand = value;
return made if value.kind == to || to == bt::error || to == 0 || value.kind == bt::error;
val from := this.c.layout_type(value.kind);
val target := this.c.layout_type(to);
made.kind = to;
return made if from == target || this.c.checker.strip(from, 0) == this.c.checker.strip(target, 0);
val fk := this.c.checker.types.kind_of(from);
val tok := this.c.checker.types.kind_of(target);
# Numbers widen within their kind, which their words already hold, and an
# integer operating with a float operates as the float.
if this.c.checker.types.is_numeric(from) && this.c.checker.types.is_numeric(target) {
if this.c.checker.types.is_float(target) && !this.c.checker.types.is_float(from) {
val signed := this.c.checker.types.is_signed(from);
var real := this.build.unary(signed ? op::itof : op::utof, value.words[0]);
if this.c.checker.types.first_of(target) <= 32 {
real = this.build.unary(op::fsingle, real);
}
return operand::one(to, real);
}
return made;
}
# An enum is its integer.
if this.c.checker.named_kind(from) == dk::enum_type && this.c.checker.types.is_integer(target) {
return made;
}
if fk == ty::reference && tok == ty::reference {
val inner_from := this.c.checker.unalias(this.c.checker.types.first_of(from));
val inner_to := this.c.checker.unalias(this.c.checker.types.first_of(target));
val from_trait := this.c.checker.named_kind(inner_from) == dk::trait_type;
val to_trait := this.c.checker.named_kind(inner_to) == dk::trait_type;
# A reference to a value becomes one to a trait it implements, given the
# vtable of its type for the trait.
if to_trait && !from_trait && this.c.checker.types.kind_of(inner_from) != ty::reference {
val vtable := this.c.vtable_of(this.c.checker.types.first_of(from), inner_to);
return operand::two(to, value.words[0], this.build.word(vtable as u64));
}
# A reference to a trait becomes one to another the value also implements.
if to_trait && from_trait {
return made if inner_from == inner_to;
var args: u32[1] = [value.words[1]];
val map := this.c.retrait_of(inner_from, inner_to);
val vtable := this.build.value(op::retrait, args[0:1], vt::word, map, 0);
return operand::two(to, value.words[0], vtable);
}
# A reference to a reference is read through to the one it holds.
if this.c.checker.types.kind_of(inner_from) == ty::reference && this.c.checker.types.kind_of(inner_to) != ty::reference {
val place := this.referent(value);
val inner := this.load(&place);
return this.convert(&inner, to);
}
return made;
}
# A union holds a value as the field of its type, another union's too, and
# a reference where one of its fields is one; and otherwise a reference is
# read through first.
if this.c.checker.named_kind(target) == dk::union_type && this.c.checker.united_field(value.kind, target) != 0 {
return this.unite(value, target, to);
}
# A reference written as one becomes a pointer at what it refers to, which
# holds the same word.
if fk == ty::reference && tok == ty::pointer
&& this.c.checker.unalias(this.c.checker.types.first_of(from)) == this.c.checker.unalias(this.c.checker.types.first_of(target)) {
return made;
}
# A plain reference is read through where its value is expected.
if fk == ty::reference {
val place := this.referent(value);
val inner := this.load(&place);
return this.convert(&inner, to);
}
# Bytes become a string by being copied, which the statement keeps, from the
# view's two words wherever they are.
if this.c.is_string(target) && this.c.checker.is_byte_view(from) && (value.address != 0 || value.count == 2) {
var args: u32[2] = [0, 0];
if value.address != 0 {
args[0] = this.build.load(access::i64, value.address, 0);
args[1] = this.build.load(access::i64, value.address, word_size);
} else {
args[0] = value.words[0];
args[1] = value.words[1];
}
this.build.effect(op::format, span::<u32>::blank(), 0, 0);
this.build.effect(op::format_bytes, args[0:2], 0, 0);
return this.owned_text(to);
}
# A string lends a view of its text: where its bytes begin, and how many.
if this.c.is_string(from) && this.c.viewed(target) != 0 {
val index := this.c.layout_of(from);
val data := this.c.field_at(index, 1);
val count := this.c.field_at(index, 2);
val address := this.temporary_of(to);
this.build.store(access::i64, address, 0, this.build.load(access::i64, value.address, data.offset));
this.build.store(access::i64, address, word_size, this.build.load(access::i64, value.address, count.offset));
return operand::at(to, address);
}
# What else converts holds the same words: a writable view lending a
# read-only one, a function by another name, `null` becoming a pointer.
return made;
}
#! A union holding a value, as the field the checker finds of its type.
#! @param this The emitter.
#! @param value The value.
#! @param target The union's type, without aliases.
#! @param to The type it is taken as.
#! @returns The union.
pub def emitter::unite(&mut this, value: &operand, target: u32, to: u32): operand {
val field := this.c.checker.united_field(value.kind, target);
val index := this.c.layout_of(target);
val shape := this.c.layout_at(index);
val address := this.temporary_of(to);
this.build.clear(address, shape.size);
for i in 0..shape.count {
val each := this.c.field_at(index, i as u32);
continue if each.decl != field;
if shape.niche == 0 {
this.build.store(access::i64, address, 0, this.build.word(i));
}
val held := this.convert(value, each.type_id);
val at := location { kind = each.type_id, address = address, offset = each.offset, framed = true };
this.store(&at, &held);
this.adopt(value);
return operand::at(to, address);
}
this.problem(0, "A union holds no field of the value's type.");
return operand::at(to, address);
}