128 lines
3.9 KiB
Plaintext
128 lines
3.9 KiB
Plaintext
#! @file values.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the operations the code an executable is compiled to calls on
|
|
#! values.
|
|
#!
|
|
#! The operations generated code calls on values, ports of those of
|
|
#! `sources/machine.c`. Values are in their frame representation in slots, and
|
|
#! in their memory representation within fixed arrays, structs, and objects.
|
|
|
|
module runtime when backend == native;
|
|
|
|
# Reads a scalar in memory as its frame representation.
|
|
def read_scalar(dst: *mut u8, at: *mut u8, access: u64): void {
|
|
if access == access_i8 {
|
|
set_word(dst, (at as *mut i8)[0] as u64);
|
|
}
|
|
else if access == access_u8 {
|
|
set_word(dst, at[0] as u64);
|
|
}
|
|
else if access == access_i16 {
|
|
set_word(dst, (at as *mut i16)[0] as u64);
|
|
}
|
|
else if access == access_u16 {
|
|
set_word(dst, (at as *mut u16)[0] as u64);
|
|
}
|
|
else if access == access_i32 {
|
|
set_word(dst, (at as *mut i32)[0] as u64);
|
|
}
|
|
else if access == access_u32 {
|
|
set_word(dst, (at as *mut u32)[0] as u64);
|
|
}
|
|
else if access == access_f32 {
|
|
(dst as *mut f64)[0] = (at as *mut f32)[0] as f64;
|
|
}
|
|
else {
|
|
set_word(dst, word(at));
|
|
}
|
|
}
|
|
|
|
# Writes a scalar in its frame representation to memory.
|
|
def write_scalar(at: *mut u8, src: *mut u8, access: u64): void {
|
|
if access == access_i8 || access == access_u8 {
|
|
at[0] = word(src) as u8;
|
|
}
|
|
else if access == access_i16 || access == access_u16 {
|
|
(at as *mut u16)[0] = word(src) as u16;
|
|
}
|
|
else if access == access_i32 || access == access_u32 {
|
|
(at as *mut u32)[0] = word(src) as u32;
|
|
}
|
|
else if access == access_f32 {
|
|
(at as *mut f32)[0] = (src as *mut f64)[0] as f32;
|
|
}
|
|
else {
|
|
set_word(at, word(src));
|
|
}
|
|
}
|
|
|
|
# The field a union holds, by its index: its tag, or of one laid out without a
|
|
# tag, its field of bytes where its first word is not null, and its empty one
|
|
# where it is.
|
|
def held_field(at: *mut u8, kind: *layout): u64 {
|
|
val first := word(at);
|
|
return first if ((*kind).flags & layout_niche) == 0;
|
|
val first_empty := (*(*(*kind).fields).layout).size == 0;
|
|
return (first != 0) == first_empty ? 1 : 0;
|
|
}
|
|
|
|
# Whether values are equal: fixed arrays element by element, structs field by
|
|
# field, and unions by variant and then value.
|
|
def equal_values(a: *mut u8, b: *mut u8, kind: *layout): bool {
|
|
if (*kind).kind == kind_float {
|
|
if (*kind).access == access_f32 {
|
|
return (a as *mut f32)[0] == (b as *mut f32)[0];
|
|
}
|
|
return (a as *mut f64)[0] == (b as *mut f64)[0];
|
|
}
|
|
|
|
if (*kind).kind == kind_fixed {
|
|
val element := (*kind).element;
|
|
val plain := (*element).kind;
|
|
if plain == kind_int || plain == kind_uint || plain == kind_bool || plain == kind_pointer {
|
|
return compare_bytes(a, b, (*kind).size) == 0;
|
|
}
|
|
for i in 0..(*kind).count {
|
|
if !equal_values(a + i * (*element).size, b + i * (*element).size, element) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (*kind).kind == kind_struct {
|
|
for i in 0..(*kind).count {
|
|
val field := (*kind).fields + i;
|
|
if !equal_values(a + (*field).offset, b + (*field).offset, (*field).layout) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (*kind).kind == kind_union {
|
|
val tag := held_field(a, kind);
|
|
if tag != held_field(b, kind) || tag >= (*kind).count {
|
|
return false;
|
|
}
|
|
val field := (*kind).fields + tag;
|
|
return equal_values(a + (*field).offset, b + (*field).offset, (*field).layout);
|
|
}
|
|
|
|
return compare_bytes(a, b, (*kind).size) == 0;
|
|
}
|
|
|
|
# The helpers generated code calls.
|
|
|
|
#! `OP_EQUAL`: whether the values are equal.
|
|
#! @param a One value, in its frame representation.
|
|
#! @param b The other.
|
|
#! @param kind The layout of their type.
|
|
#! @returns Whether they are equal, as `==` compares them.
|
|
def equal(a: *mut u8, b: *mut u8, kind: *layout): bool {
|
|
return equal_values(a, b, kind);
|
|
}
|