Files
sakurac/analyzer/checker/constants.sakura
T
2026-12-06 13:52:57 -06:00

342 lines
12 KiB
Plaintext

#! @file constants.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the constants the checker works out while checking: the values of
#! literals, and of operators applied to constants, which the length of a
#! fixed array, the value of an enum member, and whether a constant fits its
#! type need. An integer is kept as its magnitude and sign, so every value of
#! both `i64` and `u64` is one.
module sakura::analyzer;
#! A constant integer of the magnitude and sign, where zero is never negative.
#! @param magnitude Its magnitude.
#! @param negative Whether it is below zero.
#! @returns The constant.
pub def constant::integer(magnitude: u64, negative: bool): constant {
return constant { word = magnitude, negative = negative && magnitude != 0 };
}
#! A constant float.
#! @param real Its value.
#! @returns The constant.
pub def constant::real_of(real: f64): constant {
var held := real;
return constant { word = ((&mut held) as *mut u64)[0], floating = true };
}
#! A constant `bool`, as a magnitude of 0 or 1.
#! @param value Its value.
#! @returns The constant.
pub def constant::truth(value: bool): constant {
return constant { word = value ? 1 : 0 };
}
#! An integer's magnitude, or a `bool` as 0 or 1, or 0 for a float.
#! @param this The constant.
#! @returns The magnitude.
pub val constant::magnitude: u64 { get => this.floating ? 0 : this.word; }
#! A float's value, or 0 for an integer.
#! @param this The constant.
#! @returns The value.
pub val constant::real: f64 {
get {
return 0.0 if !this.floating;
var held := this.word;
return ((&mut held) as *mut f64)[0];
}
}
#! Whether two constants are the same value, a float's bits and all, as
#! interning them asks.
#! @param this The one constant.
#! @param other The other.
#! @returns Whether they are.
pub def constant::same(&this, other: &constant): bool {
return this.word == other.word && this.negative == other.negative && this.floating == other.floating;
}
#! The constant as a float.
#! @param this The constant.
#! @returns The float, which an integer converts to.
pub def constant::as_real(&this): f64 {
return this.real if this.floating;
val magnitude := this.magnitude as f64;
return this.negative ? -magnitude : magnitude;
}
#! The constant's 64 bits, as two's complement where it is negative.
#! @param this The constant.
#! @returns The bits.
pub def constant::bits(&this): u64 {
return this.negative ? ~this.magnitude + 1 : this.magnitude;
}
#! The constant of the 64 bits, read as signed where `signed` says.
#! @param bits The bits.
#! @param signed Whether they are two's complement.
#! @returns The constant.
pub def constant::from_bits(bits: u64, signed: bool): constant {
if signed && (bits >> 63) != 0 {
return constant::integer(~bits + 1, true);
}
return constant::integer(bits, false);
}
#! The integer an integer literal writes, in decimal or with `0x`, `0o`, or
#! `0b`, without its `u`, or false where it is past 64 bits.
#! @param this The checker.
#! @param text The literal as written.
#! @param out Where the integer is written.
#! @returns Whether it fits in 64 bits.
pub def checker::read_integer(&this, text: strview, out: &mut constant): bool {
var at := 0u;
var base := 10u;
var limit := text.length;
if limit > 0 && text[limit - 1] == 'u' {
limit--;
}
if limit > 2 && text[0] == '0' {
val prefix := text[1];
if prefix == 'x' { base = 16; at = 2; }
if prefix == 'o' { base = 8; at = 2; }
if prefix == 'b' { base = 2; at = 2; }
}
var magnitude := 0u;
while at < limit {
val c := text[at];
var digit := 0u;
if c >= '0' && c <= '9' {
digit = (c - '0') as u64;
} else if c >= 'a' && c <= 'f' {
digit = (c - 'a') as u64 + 10;
} else if c >= 'A' && c <= 'F' {
digit = (c - 'A') as u64 + 10;
} else {
at++;
continue;
}
if magnitude > (0xFFFFFFFFFFFFFFFFu - digit) / base {
return false;
}
magnitude = magnitude * base + digit;
at++;
}
out.word = magnitude;
out.negative = false;
out.floating = false;
return true;
}
#! The float a float literal writes, without its `f`.
#! @param this The checker.
#! @param text The literal as written.
#! @returns The float.
pub def checker::read_real(&this, text: strview): f64 {
var limit := text.length;
if limit > 0 && text[limit - 1] == 'f' {
limit--;
}
return match real::parse::<f64>(text.substr(0, limit)) {
value(read) => read,
else => 0.0
};
}
#! The code point a character literal writes, from its escape where it is one.
#! @param this The checker.
#! @param text The literal as written, with its quotes.
#! @returns The code point.
pub def checker::read_character(&this, text: strview): u64 {
return 0 if text.length < 3;
val inner := text.substr(1, text.length - 2);
if inner[0] == '\\' && inner.length >= 2 {
return match inner[1] {
'n' => 10,
't' => 9,
'r' => 13,
'0' => 0,
'x' => this.read_hex(inner.substr(2, 2)),
else => inner[1] as u64
};
}
# UTF-8: the lead byte's bits, then six of each byte continuing it.
val lead := inner[0];
return lead as u64 if lead < 0x80;
val count: u64 = lead >= 0xF0 ? 3 : lead >= 0xE0 ? 2 : 1;
var code: u64 = (lead & (0x3F >> count as u8)) as u64;
for i in 1..(count + 1) {
break if i >= inner.length;
code = (code << 6) | (inner[i] & 0x3F) as u64;
}
return code;
}
def checker::read_hex(&this, text: strview): u64 {
var value := 0u;
for &c in text {
var digit := 0u;
if c >= '0' && c <= '9' { digit = (c - '0') as u64; }
else if c >= 'a' && c <= 'f' { digit = (c - 'a') as u64 + 10; }
else if c >= 'A' && c <= 'F' { digit = (c - 'A') as u64 + 10; }
value = value * 16 + digit;
}
return value;
}
#! Whether the constant is within the range of the type, as a signed or
#! unsigned integer of its bits, a float, or a constant of no type.
#! @param this The checker.
#! @param kind The type.
#! @param value The constant.
#! @returns Whether it is.
pub def checker::in_range(&this, kind: u32, value: &constant): bool {
val bits := this.types.first_of(kind);
match this.types.kind_of(kind) {
ty::signed => {
return true if value.floating;
val top: u64 = 1u << (bits - 1) as u64;
return value.negative ? value.magnitude <= top : value.magnitude < top;
}
ty::unsigned => {
return true if value.floating;
return false if value.negative;
return bits == 64 || value.magnitude < (1u << bits as u64);
}
ty::untyped_uint => return !value.negative;
else => return true;
}
return true;
}
#! The constant an operator gives of two constants, where it gives one: false
#! where it overflows 64 bits, divides by zero, or is no operator on constants.
#! @param this The checker.
#! @param operator The operator.
#! @param a The left constant.
#! @param b The right constant.
#! @param signed Whether they are signed, which division, remainder,
#! comparisons, and shifts care about.
#! @param out Where the constant given is written.
#! @returns Whether it gives one.
pub def checker::fold_binary(&this, operator: tk, a: &constant, b: &constant, signed: bool, out: &mut constant): bool {
if a.floating || b.floating {
val x := a.as_real();
val y := b.as_real();
match operator {
tk::plus => { out = constant::real_of(x + y); return true; }
tk::minus => { out = constant::real_of(x - y); return true; }
tk::star => { out = constant::real_of(x * y); return true; }
tk::slash => { out = constant::real_of(x / y); return true; }
tk::modulo => { out = constant::real_of(x % y); return true; }
tk::equals => { out = constant::truth(x == y); return true; }
tk::noteq => { out = constant::truth(x != y); return true; }
tk::less => { out = constant::truth(x < y); return true; }
tk::lesseq => { out = constant::truth(x <= y); return true; }
tk::more => { out = constant::truth(x > y); return true; }
tk::moreeq => { out = constant::truth(x >= y); return true; }
else => return false;
}
return false;
}
match operator {
tk::plus => return this.fold_add(a, b.magnitude, b.negative, out);
tk::minus => return this.fold_add(a, b.magnitude, !b.negative, out);
tk::star => {
if a.magnitude != 0 && b.magnitude > 0xFFFFFFFFFFFFFFFFu / a.magnitude {
return false;
}
out = constant::integer(a.magnitude * b.magnitude, a.negative != b.negative);
return true;
}
tk::slash => {
return false if b.magnitude == 0;
out = constant::integer(a.magnitude / b.magnitude, a.negative != b.negative);
return true;
}
tk::modulo => {
return false if b.magnitude == 0;
out = constant::integer(a.magnitude % b.magnitude, a.negative);
return true;
}
tk::bitand => { out = constant::from_bits(a.bits() & b.bits(), a.negative || b.negative); return true; }
tk::bitor => { out = constant::from_bits(a.bits() | b.bits(), a.negative || b.negative); return true; }
tk::bitxor => { out = constant::from_bits(a.bits() ^ b.bits(), a.negative != b.negative); return true; }
tk::lshift => {
return false if b.negative || b.magnitude >= 64;
val shifted := a.magnitude << b.magnitude;
return false if (shifted >> b.magnitude) != a.magnitude;
out = constant::integer(shifted, a.negative);
return true;
}
tk::rshift => {
return false if b.negative;
if b.magnitude >= 64 {
out = constant::integer(a.negative ? 1 : 0, a.negative);
return true;
}
out = a.negative
? constant::from_bits(((a.bits() as i64) >> b.magnitude as i64) as u64, true)
: constant::integer(a.magnitude >> b.magnitude, false);
return true;
}
tk::equals => { out = constant::truth(this.compare_constants(a, b) == 0); return true; }
tk::noteq => { out = constant::truth(this.compare_constants(a, b) != 0); return true; }
tk::less => { out = constant::truth(this.compare_constants(a, b) < 0); return true; }
tk::lesseq => { out = constant::truth(this.compare_constants(a, b) <= 0); return true; }
tk::more => { out = constant::truth(this.compare_constants(a, b) > 0); return true; }
tk::moreeq => { out = constant::truth(this.compare_constants(a, b) >= 0); return true; }
tk::logand => { out = constant::truth(a.magnitude != 0 && b.magnitude != 0); return true; }
tk::logor => { out = constant::truth(a.magnitude != 0 || b.magnitude != 0); return true; }
else => return false;
}
return false;
}
def checker::fold_add(&this, a: &constant, magnitude: u64, negative: bool, out: &mut constant): bool {
if a.negative == negative {
return false if a.magnitude > 0xFFFFFFFFFFFFFFFFu - magnitude;
out = constant::integer(a.magnitude + magnitude, negative);
return true;
}
out = a.magnitude >= magnitude
? constant::integer(a.magnitude - magnitude, a.negative)
: constant::integer(magnitude - a.magnitude, negative);
return true;
}
#! Below zero where `a` is less than `b`, zero where they are equal, and above
#! zero where `a` is more.
#! @param this The checker.
#! @param a One constant.
#! @param b The other.
#! @returns Their order.
pub def checker::compare_constants(&this, a: &constant, b: &constant): i32 {
if a.floating || b.floating {
val x := a.as_real();
val y := b.as_real();
return x < y ? -1 : x > y ? 1 : 0;
}
return -1 if a.negative && !b.negative;
return 1 if !a.negative && b.negative;
if a.magnitude == b.magnitude {
return 0;
}
val less := a.magnitude < b.magnitude;
return (less != a.negative) ? -1 : 1;
}