Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,388 @@
|
||||
#! @file format.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what the machine does with values as its layouts describe them:
|
||||
#! writes them as text, as interpolated strings ask, and compares them part by
|
||||
#! part, as `==` of a struct, union, or fixed array asks, each as
|
||||
#! `sources/machine.c` does.
|
||||
|
||||
import sakura::compiler;
|
||||
module sakura::machine;
|
||||
|
||||
|
||||
#! How deeply a value within values is written before `...` stands for the rest.
|
||||
const deepest: u64 = 8;
|
||||
|
||||
#! Adds bytes to the text being formatted.
|
||||
#! @param this The machine.
|
||||
#! @param address The first byte, or 0.
|
||||
#! @param length How many bytes.
|
||||
pub def interpreter::add_text(&mut this, address: u64, length: u64): void {
|
||||
return if this.depth == 0 || address == 0;
|
||||
val bytes := address as *u8;
|
||||
val top := this.depth - 1;
|
||||
for i in 0..length {
|
||||
this.texts[top].append(bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#! Adds text the machine wrote itself to the text being formatted.
|
||||
#! @param this The machine.
|
||||
#! @param text The text.
|
||||
pub def interpreter::add_view(&mut this, text: strview): void {
|
||||
return if this.depth == 0;
|
||||
val top := this.depth - 1;
|
||||
for &each in text {
|
||||
this.texts[top].append(each);
|
||||
}
|
||||
}
|
||||
|
||||
#! Begins text, which the texts made before keep the memory of.
|
||||
#! @param this The machine.
|
||||
pub def interpreter::begin_text(&mut this): void {
|
||||
if this.depth == this.texts.length {
|
||||
this.texts.append(vector::<u8>::new());
|
||||
} else {
|
||||
this.texts[this.depth].clear();
|
||||
}
|
||||
this.depth++;
|
||||
}
|
||||
|
||||
#! Ends the text being formatted, giving it in a block the program gives back.
|
||||
#! @param this The machine.
|
||||
#! @returns The block's first byte, or 0 where the text is empty.
|
||||
pub def interpreter::owned_text(&mut this): u64 {
|
||||
return 0 if this.depth == 0;
|
||||
this.depth--;
|
||||
val length := this.texts[this.depth].length;
|
||||
return 0 if length == 0;
|
||||
val made := this.allocate(length, false);
|
||||
memcpy(made as *mut u8, this.texts[this.depth].span.data, length);
|
||||
this.wrote(made, length);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Adds a value an instruction formats: the words it is, or the address of the
|
||||
#! memory holding it.
|
||||
#! @param this The machine.
|
||||
#! @param layout The value's layout.
|
||||
#! @param fp The frame.
|
||||
#! @param code The code.
|
||||
#! @param at Where the instruction's operand words begin.
|
||||
#! @param count How many there are.
|
||||
pub def interpreter::format_operands(&mut this, layout: u32, fp: *mut u64, code: *u32, at: u64, count: u64): void {
|
||||
val shape := this.c.layout_at(layout);
|
||||
if in_memory(&shape) {
|
||||
this.format(fp[code[at]], layout, false, 0);
|
||||
return;
|
||||
}
|
||||
var words: u64[3] = [0, 0, 0];
|
||||
for i in 0..count {
|
||||
break if i >= 3;
|
||||
words[i] = fp[code[at + i]];
|
||||
}
|
||||
this.format((&mut words) as *mut u64 as u64, layout, true, 0);
|
||||
}
|
||||
|
||||
#! Reads a scalar from memory as a frame holds it, widened to a word.
|
||||
#! @param at The address.
|
||||
#! @param how How it is read.
|
||||
#! @returns The word.
|
||||
pub def read_scalar(at: u64, how: access): u64 {
|
||||
return match how {
|
||||
access::i8 => ((at as *i8)[0] as i64) as u64,
|
||||
access::u8 => (at as *u8)[0] as u64,
|
||||
access::i16 => ((at as *i16)[0] as i64) as u64,
|
||||
access::u16 => (at as *u16)[0] as u64,
|
||||
access::i32 => ((at as *i32)[0] as i64) as u64,
|
||||
access::u32 => (at as *u32)[0] as u64,
|
||||
access::f32 => bits_of_real((at as *f32)[0] as f64),
|
||||
else => (at as *u64)[0]
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether the machine's own code may read some of the program's memory for it,
|
||||
#! as formatting a value or comparing arrays does, running with its memory
|
||||
#! checked: the bytes are memory the program holds, and, where asked, were
|
||||
#! written. Where they are not, why the program stops is kept in `read_stop`,
|
||||
#! which the operation reading them stops with, and nothing more is read.
|
||||
#! @param this The machine.
|
||||
#! @param at The first byte.
|
||||
#! @param size How many.
|
||||
#! @param written Whether they must have been written too.
|
||||
#! @returns Whether they may be read.
|
||||
def interpreter::reads(&mut this, at: u64, size: u64, written: bool = true): bool {
|
||||
return true if !this.options.checks;
|
||||
return false if this.read_stop != halt::none;
|
||||
if at >= null_page && !this.checked_reach(at, 1, size) {
|
||||
this.read_stop = halt::invalid_access;
|
||||
} else if written && at >= null_page && this.unwritten_in(at, size) {
|
||||
this.read_stop = halt::unwritten;
|
||||
}
|
||||
return this.read_stop == halt::none;
|
||||
}
|
||||
|
||||
#! Why reading the program's memory for an operation stopped it, which is then
|
||||
#! forgotten, or `none`.
|
||||
#! @param this The machine.
|
||||
#! @returns Why.
|
||||
pub def interpreter::read_stopped(&mut this): halt {
|
||||
val why := this.read_stop;
|
||||
this.read_stop = halt::none;
|
||||
return why;
|
||||
}
|
||||
|
||||
#! Adds a value to the text being formatted, as its layout says it is written.
|
||||
#! @param this The machine.
|
||||
#! @param at Where the value is.
|
||||
#! @param layout Its layout.
|
||||
#! @param frame Whether it is held as a frame holds it, a scalar widened to a
|
||||
#! word, rather than as memory holds it.
|
||||
#! @param depth How deeply within values it is.
|
||||
pub def interpreter::format(&mut this, at: u64, layout: u32, frame: bool, depth: u64): void {
|
||||
if depth > deepest {
|
||||
this.add_view("...");
|
||||
return;
|
||||
}
|
||||
|
||||
val shape := this.c.layout_at(layout);
|
||||
# Each scalar is read where it is, so what is never written between fields
|
||||
# stops nothing.
|
||||
val scalar := shape.kind == lk::signed || shape.kind == lk::unsigned || shape.kind == lk::boolean
|
||||
|| shape.kind == lk::floating || shape.kind == lk::reference || shape.kind == lk::pointer
|
||||
|| shape.kind == lk::function || shape.kind == lk::trait_reference;
|
||||
return if !frame && scalar && !this.reads(at, shape.size);
|
||||
match shape.kind {
|
||||
lk::signed, lk::unsigned => {
|
||||
val value := frame ? (at as *u64)[0] : read_scalar(at, shape.how);
|
||||
# An enum is the name of the member of the value.
|
||||
for i in 0..shape.count {
|
||||
val member := this.c.field_at(layout, i as u32);
|
||||
if member.offset == value {
|
||||
this.add_view(this.c.checker.names.text(member.name));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if shape.kind == lk::signed {
|
||||
this.add_view($"{value as i64}");
|
||||
} else {
|
||||
this.add_view($"{value}");
|
||||
}
|
||||
}
|
||||
lk::boolean => {
|
||||
val value := frame ? (at as *u64)[0] : read_scalar(at, shape.how);
|
||||
this.add_view(value != 0 ? "true" : "false");
|
||||
}
|
||||
lk::floating => {
|
||||
val value := real_of(frame ? (at as *u64)[0] : read_scalar(at, shape.how));
|
||||
if shape.bits <= 32 {
|
||||
this.add_view($"{value as f32}");
|
||||
} else {
|
||||
this.add_view($"{value}");
|
||||
}
|
||||
}
|
||||
lk::fixed => {
|
||||
val element := shape.element - 1;
|
||||
val inner := this.c.layout_at(element);
|
||||
# A fixed array of bytes is the text it holds.
|
||||
if (inner.kind == lk::signed || inner.kind == lk::unsigned) && inner.size == 1 && inner.count == 0 {
|
||||
return if !frame && !this.reads(at, shape.count as u64);
|
||||
this.add_text(at, shape.count as u64);
|
||||
return;
|
||||
}
|
||||
this.add_view("[");
|
||||
for i in 0..shape.count {
|
||||
if i != 0 {
|
||||
this.add_view(", ");
|
||||
}
|
||||
this.format(at + i as u64 * inner.size, element, false, depth + 1);
|
||||
}
|
||||
this.add_view("]");
|
||||
}
|
||||
lk::reference => {
|
||||
val pointer := (at as *u64)[0];
|
||||
if pointer == 0 || shape.element == 0 {
|
||||
this.add_view("null");
|
||||
return;
|
||||
}
|
||||
this.format(pointer, shape.element - 1, false, depth + 1);
|
||||
}
|
||||
lk::trait_reference => {
|
||||
val pointer := (at as *u64)[0];
|
||||
val vtable := (at as *u64)[1];
|
||||
if pointer == 0 || vtable >= this.c.program.vtables.length {
|
||||
this.add_view("null");
|
||||
return;
|
||||
}
|
||||
this.format(pointer, this.c.program.vtables[vtable].layout, false, depth + 1);
|
||||
}
|
||||
lk::record => this.format_record(at, layout, &shape, depth);
|
||||
lk::pointer => {
|
||||
val address := (at as *u64)[0];
|
||||
if address == 0 {
|
||||
this.add_view("null");
|
||||
return;
|
||||
}
|
||||
this.add_view("0x");
|
||||
this.add_view(hex(address).span);
|
||||
}
|
||||
lk::function => {
|
||||
val index := (at as *u64)[0];
|
||||
if index == 0 {
|
||||
this.add_view("null");
|
||||
return;
|
||||
}
|
||||
if index - 1 >= this.c.program.functions.length {
|
||||
this.add_view("def ?");
|
||||
return;
|
||||
}
|
||||
this.add_view("def ");
|
||||
this.add_view(this.c.program.functions[index - 1].name.span);
|
||||
}
|
||||
lk::variant => {
|
||||
# A union is written as a struct holding the one field it holds.
|
||||
# Its first word, which says which field it holds, may hold bytes no
|
||||
# field wrote, so only that it is held is asked.
|
||||
return if !frame && !this.reads(at, 8, false);
|
||||
val tag := held_field(&shape, (at as *u64)[0]);
|
||||
if tag >= shape.count as u64 {
|
||||
this.add_view("?");
|
||||
return;
|
||||
}
|
||||
val field := this.c.field_at(layout, tag as u32);
|
||||
this.add_view(this.c.checker.names.text(shape.name));
|
||||
this.add_view("{");
|
||||
this.add_view(this.c.checker.names.text(field.name));
|
||||
this.add_view(" = ");
|
||||
this.format(at + field.offset, field.layout, false, depth + 1);
|
||||
this.add_view("}");
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
|
||||
#! Adds a struct to the text being formatted: text as the bytes it holds, any
|
||||
#! other view as the elements it views, and the rest as its name and fields.
|
||||
#! @param this The machine.
|
||||
#! @param at Where it is.
|
||||
#! @param layout Its layout.
|
||||
#! @param shape The layout itself.
|
||||
#! @param depth How deeply within values it is.
|
||||
def interpreter::format_record(&mut this, at: u64, layout: u32, shape: &layout, depth: u64): void {
|
||||
if shape.texts || (shape.views && shape.viewed != 0) {
|
||||
val begins := this.c.field_at(layout, shape.text);
|
||||
val many := this.c.field_at(layout, shape.text + 1);
|
||||
return if !this.reads(at + begins.offset, 8) || !this.reads(at + many.offset, 8);
|
||||
val data := ((at + begins.offset) as *u64)[0];
|
||||
val length := data != 0 ? ((at + many.offset) as *u64)[0] : 0;
|
||||
if shape.texts {
|
||||
return if data != 0 && !this.reads(data, length);
|
||||
this.add_text(data, length);
|
||||
return;
|
||||
}
|
||||
val element := shape.viewed - 1;
|
||||
val size := this.c.layout_at(element).size;
|
||||
this.add_view("[");
|
||||
for i in 0..length {
|
||||
if i != 0 {
|
||||
this.add_view(", ");
|
||||
}
|
||||
this.format(data + i * size, element, false, depth + 1);
|
||||
}
|
||||
this.add_view("]");
|
||||
return;
|
||||
}
|
||||
|
||||
this.add_view(this.c.checker.names.text(shape.name));
|
||||
this.add_view("{");
|
||||
for i in 0..shape.count {
|
||||
val field := this.c.field_at(layout, i as u32);
|
||||
if i != 0 {
|
||||
this.add_view(", ");
|
||||
}
|
||||
val name := this.c.checker.names.text(field.name);
|
||||
if name.length != 0 {
|
||||
this.add_view(name);
|
||||
this.add_view(" = ");
|
||||
}
|
||||
this.format(at + field.offset, field.layout, false, depth + 1);
|
||||
}
|
||||
this.add_view("}");
|
||||
}
|
||||
|
||||
#! The hexadecimal digits of a word, without leading zeros.
|
||||
#! @param value The word.
|
||||
#! @returns The digits.
|
||||
def hex(value: u64): string {
|
||||
var made := string::new(nothing {});
|
||||
var shift: u64 = 64;
|
||||
var started := false;
|
||||
while shift > 0 {
|
||||
shift -= 4;
|
||||
val digit := (value >> shift) & 0xF;
|
||||
started = started || digit != 0 || shift == 0;
|
||||
continue if !started;
|
||||
made.push(digit < 10 ? ('0' + digit as u8) : ('a' + (digit - 10) as u8));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether the values at two addresses are equal: fixed arrays of what holds
|
||||
#! no object by their bytes, and of anything else element by element, structs
|
||||
#! field by field, unions by the field held and then its value, floats as
|
||||
#! numbers, so `-0` equals `0` and `nan` equals nothing, and anything else by
|
||||
#! its bytes.
|
||||
#! @param this The machine.
|
||||
#! @param a Where one is.
|
||||
#! @param b Where the other is.
|
||||
#! @param layout Their layout.
|
||||
#! @param depth How deeply within values they are.
|
||||
#! @returns Whether they are equal.
|
||||
pub def interpreter::equal(&mut this, a: u64, b: u64, layout: u32, depth: u64): bool {
|
||||
val shape := this.c.layout_at(layout);
|
||||
# What is compared a byte at a time is read whole; a struct, union, or array
|
||||
# of them a field or element at a time, as below.
|
||||
if shape.kind != lk::record && shape.kind != lk::variant {
|
||||
val inner := shape.kind == lk::fixed ? this.c.layout_at(shape.element - 1) : shape;
|
||||
val whole := shape.kind != lk::fixed || inner.kind == lk::signed || inner.kind == lk::unsigned
|
||||
|| inner.kind == lk::boolean || inner.kind == lk::pointer;
|
||||
return false if whole && (!this.reads(a, shape.size) || !this.reads(b, shape.size));
|
||||
}
|
||||
match shape.kind {
|
||||
lk::floating => {
|
||||
val x := real_of(read_scalar(a, shape.how));
|
||||
val y := real_of(read_scalar(b, shape.how));
|
||||
return x == y;
|
||||
}
|
||||
lk::fixed => {
|
||||
val element := shape.element - 1;
|
||||
val inner := this.c.layout_at(element);
|
||||
# Elements that are integers, `bool`, or pointers are equal where their
|
||||
# bytes are; any other is compared as its type is.
|
||||
if inner.kind == lk::signed || inner.kind == lk::unsigned || inner.kind == lk::boolean || inner.kind == lk::pointer {
|
||||
return memcmp(a as *u8, b as *u8, shape.size) == 0;
|
||||
}
|
||||
for i in 0..shape.count {
|
||||
return false if !this.equal(a + i as u64 * inner.size, b + i as u64 * inner.size, element, depth + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
lk::record => {
|
||||
for i in 0..shape.count {
|
||||
val field := this.c.field_at(layout, i as u32);
|
||||
return false if !this.equal(a + field.offset, b + field.offset, field.layout, depth + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
lk::variant => {
|
||||
return false if !this.reads(a, 8, false) || !this.reads(b, 8, false);
|
||||
val tag := held_field(&shape, (a as *u64)[0]);
|
||||
return false if tag != held_field(&shape, (b as *u64)[0]) || tag >= shape.count as u64;
|
||||
val field := this.c.field_at(layout, tag as u32);
|
||||
return this.equal(a + field.offset, b + field.offset, field.layout, depth + 1);
|
||||
}
|
||||
else => return memcmp(a as *u8, b as *u8, shape.size) == 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user