415 lines
11 KiB
Plaintext
415 lines
11 KiB
Plaintext
#! @file format.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains how an executable builds the text of an interpolation.
|
||
|
|
#!
|
||
|
|
#! Interpolation builds text in builders of memory the runtime manages, as
|
||
|
|
#! `machine__format` of `sources/machine.c` does, then makes a string of it.
|
||
|
|
|
||
|
|
module runtime when backend == native;
|
||
|
|
|
||
|
|
struct builder {
|
||
|
|
var data: *mut u8;
|
||
|
|
var length: u64;
|
||
|
|
var capacity: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
val format_depth := 8u;
|
||
|
|
|
||
|
|
|
||
|
|
def append(into: *mut builder, bytes: *u8, length: u64): void {
|
||
|
|
if (*into).length + length > (*into).capacity {
|
||
|
|
var capacity := (*into).capacity == 0 ? 64 : (*into).capacity;
|
||
|
|
while capacity < (*into).length + length {
|
||
|
|
capacity = capacity * 2;
|
||
|
|
}
|
||
|
|
val grown := acquire(capacity);
|
||
|
|
copy_bytes(grown, (*into).data, (*into).length);
|
||
|
|
dispose((*into).data, (*into).capacity);
|
||
|
|
(*into).data = grown;
|
||
|
|
(*into).capacity = capacity;
|
||
|
|
}
|
||
|
|
copy_bytes((*into).data + (*into).length, bytes, length);
|
||
|
|
(*into).length = (*into).length + length;
|
||
|
|
}
|
||
|
|
|
||
|
|
def put(into: *mut builder, byte: u8): void {
|
||
|
|
if (*into).length == (*into).capacity {
|
||
|
|
val capacity := (*into).capacity == 0 ? 64 : (*into).capacity * 2;
|
||
|
|
val grown := acquire(capacity);
|
||
|
|
copy_bytes(grown, (*into).data, (*into).length);
|
||
|
|
dispose((*into).data, (*into).capacity);
|
||
|
|
(*into).data = grown;
|
||
|
|
(*into).capacity = capacity;
|
||
|
|
}
|
||
|
|
(*into).data[(*into).length] = byte;
|
||
|
|
(*into).length = (*into).length + 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_text(into: *mut builder, index: u64): void {
|
||
|
|
val text := (*executable()).texts + index;
|
||
|
|
append(into, (*text).bytes, (*text).length);
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_unsigned(into: *mut builder, value: u64): void {
|
||
|
|
var power := 1u;
|
||
|
|
while value / power >= 10 {
|
||
|
|
power = power * 10;
|
||
|
|
}
|
||
|
|
while power > 0 {
|
||
|
|
put(into, ('0' + value / power % 10) as u8);
|
||
|
|
power = power / 10;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_signed(into: *mut builder, value: i64): void {
|
||
|
|
if value < 0 {
|
||
|
|
put(into, '-');
|
||
|
|
put_unsigned(into, 0 - (value as u64));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
put_unsigned(into, value as u64);
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_hex(into: *mut builder, value: u64): void {
|
||
|
|
put(into, '0');
|
||
|
|
put(into, 'x');
|
||
|
|
var shift := 60u;
|
||
|
|
while shift > 0 && (value >> shift) == 0 {
|
||
|
|
shift = shift - 4;
|
||
|
|
}
|
||
|
|
while true {
|
||
|
|
val digit := (value >> shift) & 15;
|
||
|
|
put(into, (digit < 10 ? '0' + digit : 'a' + digit - 10) as u8);
|
||
|
|
if shift == 0 {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
shift = shift - 4;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_word(into: *mut builder, text: *u8, length: u64): void {
|
||
|
|
append(into, text, length);
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_name(into: *mut builder, name: *u8, length: u64): void {
|
||
|
|
append(into, name, length);
|
||
|
|
}
|
||
|
|
|
||
|
|
# Writes `true`, `false`, `null`, and the other words formatting uses, a byte at a time.
|
||
|
|
def put_true(into: *mut builder): void {
|
||
|
|
put(into, 't');
|
||
|
|
put(into, 'r');
|
||
|
|
put(into, 'u');
|
||
|
|
put(into, 'e');
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_false(into: *mut builder): void {
|
||
|
|
put(into, 'f');
|
||
|
|
put(into, 'a');
|
||
|
|
put(into, 'l');
|
||
|
|
put(into, 's');
|
||
|
|
put(into, 'e');
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_null(into: *mut builder): void {
|
||
|
|
put(into, 'n');
|
||
|
|
put(into, 'u');
|
||
|
|
put(into, 'l');
|
||
|
|
put(into, 'l');
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_separator(into: *mut builder): void {
|
||
|
|
put(into, ',');
|
||
|
|
put(into, ' ');
|
||
|
|
}
|
||
|
|
|
||
|
|
def put_value(into: *mut builder, at: *mut u8, kind: *layout, frame: bool, depth: u64): void {
|
||
|
|
if depth > format_depth {
|
||
|
|
put(into, '.');
|
||
|
|
put(into, '.');
|
||
|
|
put(into, '.');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
val info := executable();
|
||
|
|
if (*kind).kind == kind_int || (*kind).kind == kind_uint || (*kind).kind == kind_bool {
|
||
|
|
# A scalar in memory is read as wide as it is there, as a whole word past
|
||
|
|
# it may not be memory at all.
|
||
|
|
var value: u64 = 0;
|
||
|
|
if frame {
|
||
|
|
value = word(at);
|
||
|
|
} else {
|
||
|
|
read_scalar((&mut scalar_scratch) as *mut u8, at, (*kind).access);
|
||
|
|
value = scalar_scratch;
|
||
|
|
}
|
||
|
|
# An enum is the name of the member of the value.
|
||
|
|
for i in 0..(*kind).count {
|
||
|
|
val member := (*kind).fields + i;
|
||
|
|
if (*member).offset == value {
|
||
|
|
append(into, (*member).name, (*member).name_length);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (*kind).kind == kind_bool {
|
||
|
|
if value != 0 {
|
||
|
|
put_true(into);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
put_false(into);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (*kind).kind == kind_int {
|
||
|
|
put_signed(into, value as i64);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
put_unsigned(into, value);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (*kind).kind == kind_float {
|
||
|
|
var value := (at as *mut f64)[0];
|
||
|
|
if !frame {
|
||
|
|
read_scalar((&mut scalar_scratch) as *mut u8, at, (*kind).access);
|
||
|
|
value = ((&mut scalar_scratch) as *mut f64)[0];
|
||
|
|
}
|
||
|
|
put_float(into, value, (*kind).bits <= 32);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A fixed array holds its elements where it is, and prints as they do.
|
||
|
|
if (*kind).kind == kind_fixed {
|
||
|
|
val element := (*kind).element;
|
||
|
|
if ((*element).kind == kind_int || (*element).kind == kind_uint) && (*element).size == 1 && (*element).count == 0 {
|
||
|
|
append(into, at, (*kind).count);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
put(into, '[');
|
||
|
|
for i in 0..(*kind).count {
|
||
|
|
if i > 0 {
|
||
|
|
put_separator(into);
|
||
|
|
}
|
||
|
|
put_value(into, at + i * (*element).size, element, false, depth + 1);
|
||
|
|
}
|
||
|
|
put(into, ']');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (*kind).kind == kind_reference || (*kind).kind == kind_trait {
|
||
|
|
val pointer := resolved_pointer(at);
|
||
|
|
if (*kind).kind == kind_trait {
|
||
|
|
val vtable := word(at + 8);
|
||
|
|
if pointer == null || vtable >= (*info).vtable_count {
|
||
|
|
put_null(into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
put_value(into, pointer, (*info).vtables[vtable], false, depth + 1);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if pointer == null || (*kind).element == null {
|
||
|
|
put_null(into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
put_value(into, pointer, (*kind).element, false, depth + 1);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (*kind).kind == kind_struct {
|
||
|
|
# Text is written as the bytes it holds, not as where they are and how many.
|
||
|
|
if ((*kind).flags & layout_texts) != 0 {
|
||
|
|
val index := (*kind).flags >> layout_text_at;
|
||
|
|
val begins := (*kind).fields + index;
|
||
|
|
val counts := (*kind).fields + index + 1;
|
||
|
|
val data := word(at + (*begins).offset) as *u8;
|
||
|
|
val length := word(at + (*counts).offset);
|
||
|
|
if data != null && length > 0 {
|
||
|
|
append(into, data, length);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Any other view is written as the elements it views, as an array of them is.
|
||
|
|
if ((*kind).flags & layout_views) != 0 {
|
||
|
|
val index := (*kind).flags >> layout_text_at;
|
||
|
|
val begins := (*kind).fields + index;
|
||
|
|
val counts := (*kind).fields + index + 1;
|
||
|
|
val data := word(at + (*begins).offset) as *mut u8;
|
||
|
|
val length := data != null ? word(at + (*counts).offset) : 0;
|
||
|
|
val element := (*kind).element;
|
||
|
|
put(into, '[');
|
||
|
|
for i in 0..length {
|
||
|
|
if i > 0 {
|
||
|
|
put_separator(into);
|
||
|
|
}
|
||
|
|
put_value(into, data + i * (*element).size, element, false, depth + 1);
|
||
|
|
}
|
||
|
|
put(into, ']');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
append(into, (*kind).name, (*kind).name_length);
|
||
|
|
put(into, '{');
|
||
|
|
for i in 0..(*kind).count {
|
||
|
|
val field := (*kind).fields + i;
|
||
|
|
if i > 0 {
|
||
|
|
put_separator(into);
|
||
|
|
}
|
||
|
|
if (*field).name_length > 0 {
|
||
|
|
append(into, (*field).name, (*field).name_length);
|
||
|
|
put(into, ' ');
|
||
|
|
put(into, '=');
|
||
|
|
put(into, ' ');
|
||
|
|
}
|
||
|
|
put_value(into, at + (*field).offset, (*field).layout, false, depth + 1);
|
||
|
|
}
|
||
|
|
put(into, '}');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (*kind).kind == kind_function {
|
||
|
|
val index := word(at);
|
||
|
|
if index == 0 {
|
||
|
|
put_null(into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
put(into, 'd');
|
||
|
|
put(into, 'e');
|
||
|
|
put(into, 'f');
|
||
|
|
put(into, ' ');
|
||
|
|
if index - 1 >= (*info).function_count {
|
||
|
|
put(into, '?');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val function := (*info).functions + (index - 1);
|
||
|
|
append(into, (*function).name, (*function).name_length);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A union is written as a struct holding the one field it holds.
|
||
|
|
if (*kind).kind == kind_union {
|
||
|
|
val tag := held_field(at, kind);
|
||
|
|
if tag >= (*kind).count {
|
||
|
|
put(into, '?');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val field := (*kind).fields + tag;
|
||
|
|
append(into, (*kind).name, (*kind).name_length);
|
||
|
|
put(into, '{');
|
||
|
|
append(into, (*field).name, (*field).name_length);
|
||
|
|
put(into, ' ');
|
||
|
|
put(into, '=');
|
||
|
|
put(into, ' ');
|
||
|
|
put_value(into, at + (*field).offset, (*field).layout, false, depth + 1);
|
||
|
|
put(into, '}');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (*kind).kind == kind_pointer {
|
||
|
|
val address := word(at);
|
||
|
|
if address == 0 {
|
||
|
|
put_null(into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
put_hex(into, address);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Room to read a scalar or a reference into, since the runtime takes no references to its locals.
|
||
|
|
struct scratch {
|
||
|
|
var first: u64;
|
||
|
|
var second: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
var scalar_scratch: u64;
|
||
|
|
var reference_scratch: scratch;
|
||
|
|
|
||
|
|
# The pointer a reference holds.
|
||
|
|
def resolved_pointer(at: *mut u8): *mut u8 {
|
||
|
|
return word(at) as *mut u8;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `OP_FORMAT`: begins a string, in the running thread's builders.
|
||
|
|
def format_begin(): void {
|
||
|
|
if threaded {
|
||
|
|
hold();
|
||
|
|
}
|
||
|
|
val me := current();
|
||
|
|
if (*me).builder_count == (*me).builder_capacity {
|
||
|
|
val capacity := (*me).builder_capacity == 0 ? 8 : (*me).builder_capacity * 2;
|
||
|
|
val grown := acquire(capacity * size_of_builder()) as *mut builder;
|
||
|
|
copy_bytes(grown as *mut u8, (*me).builders as *mut u8, (*me).builder_count * size_of_builder());
|
||
|
|
dispose((*me).builders as *mut u8, (*me).builder_capacity * size_of_builder());
|
||
|
|
(*me).builders = grown;
|
||
|
|
(*me).builder_capacity = capacity;
|
||
|
|
}
|
||
|
|
(*((*me).builders + (*me).builder_count)).length = 0;
|
||
|
|
(*me).builder_count++;
|
||
|
|
if threaded {
|
||
|
|
let_go();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
def size_of_builder(): u64 {
|
||
|
|
return size_of::<builder>();
|
||
|
|
}
|
||
|
|
|
||
|
|
# The builder the running thread appends to.
|
||
|
|
def innermost(): *mut builder {
|
||
|
|
val me := current();
|
||
|
|
return (*me).builders + ((*me).builder_count - 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `OP_FORMAT_TEXT`: appends constant text.
|
||
|
|
#! @param bytes The first byte of the text.
|
||
|
|
#! @param length How many bytes it holds.
|
||
|
|
def format_text(bytes: *mut u8, length: u64): void {
|
||
|
|
if threaded {
|
||
|
|
hold();
|
||
|
|
}
|
||
|
|
append(innermost(), bytes, length);
|
||
|
|
if threaded {
|
||
|
|
let_go();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `OP_FORMAT_VALUE`: appends a value in its frame representation.
|
||
|
|
#! @param at The value, in its frame representation.
|
||
|
|
#! @param kind The layout of its type, which says how it is written.
|
||
|
|
def format_value(at: *mut u8, kind: *layout): void {
|
||
|
|
if threaded {
|
||
|
|
hold();
|
||
|
|
}
|
||
|
|
put_value(innermost(), at, kind, true, 0);
|
||
|
|
if threaded {
|
||
|
|
let_go();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `OP_FORMAT_OWNED`: hands the text over in memory the program manages itself,
|
||
|
|
#! as where it begins and how many bytes it holds, which is what a `string` owns
|
||
|
|
#! and gives back where its scope ends. Its block is not cleared, as every byte
|
||
|
|
#! of it is written.
|
||
|
|
#! @param dst The slot of the `string` to fill: its first word takes where the
|
||
|
|
#! text begins, and its second how many bytes it holds.
|
||
|
|
def format_owned(dst: *mut u8): void {
|
||
|
|
if threaded {
|
||
|
|
hold();
|
||
|
|
}
|
||
|
|
val me := current();
|
||
|
|
(*me).builder_count--;
|
||
|
|
val done := (*me).builders + (*me).builder_count;
|
||
|
|
val data := (*done).length > 0 ? allocate((*done).length) : null;
|
||
|
|
copy_bytes(data, (*done).data, (*done).length);
|
||
|
|
|
||
|
|
val words := dst as *mut u64;
|
||
|
|
words[0] = data as u64;
|
||
|
|
words[1] = (*done).length;
|
||
|
|
if threaded {
|
||
|
|
let_go();
|
||
|
|
}
|
||
|
|
}
|