231 lines
8.8 KiB
Plaintext
231 lines
8.8 KiB
Plaintext
#! @file abi.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains what the runtime of an executable shares with the code the compiler
|
||
|
|
#! generates: the intrinsics it calls the kernel and stops with, and the records
|
||
|
|
#! the compiler writes the program's layouts, functions, and globals in.
|
||
|
|
#!
|
||
|
|
#! The runtime of executables made by `sakura build`, which is compiled into
|
||
|
|
#! each of them. It is a library of its own, found beside the compiler as
|
||
|
|
#! `core` is, and reads `core` alone. Each of its files says
|
||
|
|
#! `when backend == native`, so it is no part of a program a machine runs,
|
||
|
|
#! which does what it does itself. It formats no text itself, since that is
|
||
|
|
#! what it does for the program, and works through pointers. Values may be
|
||
|
|
#! allocated before `<globals>` runs, so the runtime's `val`s are numbers known
|
||
|
|
#! while compiling, which globals begin with, and its `var`s begin as zeros and
|
||
|
|
#! are set up on first use.
|
||
|
|
|
||
|
|
module runtime when backend == native;
|
||
|
|
|
||
|
|
#! The version of what the runtime shares with the code the compiler
|
||
|
|
#! generates: the helpers it calls, the words of `state`, and the records it
|
||
|
|
#! writes. A compiler builds only with a runtime of the ABI it calls, which
|
||
|
|
#! changes whenever any of those does, so one of another release is refused
|
||
|
|
#! rather than misread.
|
||
|
|
pub const abi: u32 = 1;
|
||
|
|
|
||
|
|
#! Calls the kernel, giving the negated error number when the call fails.
|
||
|
|
#! @param n The number of the system call.
|
||
|
|
#! @param a Its first argument.
|
||
|
|
#! @param b Its second argument.
|
||
|
|
#! @param c Its third argument.
|
||
|
|
#! @param d Its fourth argument.
|
||
|
|
#! @param e Its fifth argument.
|
||
|
|
#! @param f Its sixth argument.
|
||
|
|
#! @returns What the kernel gives, which is the negated error number where the
|
||
|
|
#! call fails.
|
||
|
|
@intrinsic def syscall(n: i64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64): i64;
|
||
|
|
|
||
|
|
#! The frame of the calling function.
|
||
|
|
#! @returns The frame's first byte.
|
||
|
|
@intrinsic def frame(): *mut u8;
|
||
|
|
|
||
|
|
# The record of the running thread, which `gs` points at.
|
||
|
|
@intrinsic def current(): *mut state;
|
||
|
|
|
||
|
|
# Starts a thread of the system on the record's stack, which runs `thread_main`
|
||
|
|
# there, giving its number, or a negative error number.
|
||
|
|
@intrinsic def clone_thread(record: *mut state): i64;
|
||
|
|
|
||
|
|
#! Stops the program with the error, reporting where it happened.
|
||
|
|
#! @param code The error, as the machine numbers them.
|
||
|
|
#! @remarks It does not return.
|
||
|
|
@intrinsic def trap(code: i64): void;
|
||
|
|
|
||
|
|
#! An array of its parts, without copying them.
|
||
|
|
|
||
|
|
#! The image the executable was built with.
|
||
|
|
#! @returns The image, which lives as long as the program does.
|
||
|
|
@intrinsic def executable(): *image;
|
||
|
|
|
||
|
|
# The records an executable is built with, which `native/native.sakura` writes.
|
||
|
|
# Every field is a word, so the compiler places them where the builder does, but
|
||
|
|
# for those of the line tables, which are as narrow as what they hold allows.
|
||
|
|
|
||
|
|
struct layout {
|
||
|
|
var kind: u64; #! A `layout_kind` of `sources/program.h`.
|
||
|
|
var access: u64; #! An `access` of `sources/program.h`.
|
||
|
|
var bits: u64;
|
||
|
|
var size: u64;
|
||
|
|
var align: u64;
|
||
|
|
var frame: u64;
|
||
|
|
var element: *layout; #! Of an array.
|
||
|
|
var fields: *field;
|
||
|
|
var count: u64;
|
||
|
|
var flags: u64; #! `layout_niche`, `layout_texts`, and `layout_views`.
|
||
|
|
var name: *u8;
|
||
|
|
var name_length: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Set where a union is laid out without a tag: it holds its field of bytes where
|
||
|
|
#! its first word is not null, and its empty one where it is.
|
||
|
|
val layout_niche := 2u;
|
||
|
|
|
||
|
|
#! Set where the value is text, written as the bytes it holds rather than as its
|
||
|
|
#! fields. The field saying where they begin is at `flags >> 8`, and the one
|
||
|
|
#! saying how many there are is the field after it.
|
||
|
|
val layout_texts := 4u;
|
||
|
|
val layout_views := 8u;
|
||
|
|
val layout_text_at := 8u;
|
||
|
|
|
||
|
|
struct field {
|
||
|
|
var layout: *layout;
|
||
|
|
var offset: u64;
|
||
|
|
var name: *u8;
|
||
|
|
var name_length: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Where the code of a function from an offset on was written: the line and
|
||
|
|
# column, the path among the image's, and one more than the site of the function
|
||
|
|
# inlined there it was the code of, or 0.
|
||
|
|
struct line {
|
||
|
|
var offset: u32;
|
||
|
|
var line: u32;
|
||
|
|
var column: u16;
|
||
|
|
var path: u16;
|
||
|
|
var site: u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Where a function's code was inlined within another's: the function inlined,
|
||
|
|
# where it was called, and one more than the site that call is within, or 0 for
|
||
|
|
# the function's own code.
|
||
|
|
struct site {
|
||
|
|
var function: u32;
|
||
|
|
var line: u32;
|
||
|
|
var column: u16;
|
||
|
|
var path: u16;
|
||
|
|
var parent: u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
struct function {
|
||
|
|
var start: u64;
|
||
|
|
var end: u64;
|
||
|
|
var name: *u8;
|
||
|
|
var name_length: u64;
|
||
|
|
var lines: *line;
|
||
|
|
var line_count: u64;
|
||
|
|
var runtime: u64; #! Whether it belongs to the runtime, which traces leave out.
|
||
|
|
}
|
||
|
|
|
||
|
|
struct text {
|
||
|
|
var bytes: *u8;
|
||
|
|
var length: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A thread's record, which `gs` points at in the thread, as `native/native.sakura`
|
||
|
|
# lays it out: the first thread's is the executable's state, written by its start
|
||
|
|
# before the runtime starts, which also holds what every thread shares, and every
|
||
|
|
# other is mapped where the thread starts. The records are a list from the first.
|
||
|
|
struct state {
|
||
|
|
var shadow_base: *mut u8; #! The top of the stack, where frames begin.
|
||
|
|
var shadow_limit: *mut u8; #! The lowest a frame may reach.
|
||
|
|
var trap_frame: *mut u8; #! The bottom of the stack, past the limit, which a trap's report may use.
|
||
|
|
var itself: *mut state; #! The record itself.
|
||
|
|
var argc: u64; #! The arguments the program started with, in the first.
|
||
|
|
var argv: *mut u64;
|
||
|
|
var reporting: u64; #! Whether a trap is being reported, in the first.
|
||
|
|
var c_top: u64;
|
||
|
|
var c_frame: u64; #! Where the program called C last.
|
||
|
|
var c_return: u64;
|
||
|
|
var features: u64; #! The vector instructions the program uses, in the first.
|
||
|
|
var signals: u64; #! The signals noted, in the first, and none in the others.
|
||
|
|
var spare_word: u64;
|
||
|
|
var next: *mut state; #! The next thread's record.
|
||
|
|
var tid: u64; #! The system's number for the thread, cleared by the system as it ends.
|
||
|
|
var cache: *mut block_cache; #! The blocks it takes and gives back without the lock, once there are threads.
|
||
|
|
var builders: *mut builder; #! The text being formatted, innermost last.
|
||
|
|
var builder_count: u64;
|
||
|
|
var builder_capacity: u64;
|
||
|
|
var body: def(*mut u8): void; #! The function the thread runs, and the context it is given.
|
||
|
|
var context: *mut u8;
|
||
|
|
var map: *mut u8; #! Where its stack is mapped, and its bytes.
|
||
|
|
var map_size: u64;
|
||
|
|
var spare_map: u64[2];
|
||
|
|
var running: u64; #! 1 while it runs, and 0 once its function has returned.
|
||
|
|
var spare: u64[5];
|
||
|
|
}
|
||
|
|
|
||
|
|
struct image {
|
||
|
|
var state: *mut state;
|
||
|
|
var layouts: *layout;
|
||
|
|
var layout_count: u64;
|
||
|
|
var functions: *function;
|
||
|
|
var function_count: u64;
|
||
|
|
var texts: *text;
|
||
|
|
var text_count: u64;
|
||
|
|
var globals: *mut u8;
|
||
|
|
var globals_size: u64;
|
||
|
|
var spare_image: u64;
|
||
|
|
var vtables: **layout; #! Of what each vtable's trait references refer to.
|
||
|
|
var vtable_count: u64;
|
||
|
|
var paths: *text; #! The paths the line tables name.
|
||
|
|
var sites: *site; #! Where the functions inlined were, which the line tables name.
|
||
|
|
}
|
||
|
|
|
||
|
|
# The texts of an image, where those before `text_out_of_memory` describe the
|
||
|
|
# errors of `machine_error` in `include/malunal/sakura.h` by their codes.
|
||
|
|
val error_aborted: i64 = 21; # `MACHINE_ERROR_ABORTED`, what `abort` reports with.
|
||
|
|
val error_faulted: i64 = 26; # What `fault` reports with, before the text it was given.
|
||
|
|
val text_out_of_memory := 31u;
|
||
|
|
val text_prefix := 32u; # `sakura: runtime error: `
|
||
|
|
val text_at := 33u; # ` at `
|
||
|
|
val text_open := 34u; # ` (`
|
||
|
|
val text_colon := 35u; # `:`
|
||
|
|
val text_close := 36u; # `)` and a new line
|
||
|
|
val text_newline := 37u;
|
||
|
|
|
||
|
|
# Errors the runtime traps with, the codes of `machine_error`.
|
||
|
|
val error_out_of_bounds: i64 = 4;
|
||
|
|
val error_null_reference: i64 = 5;
|
||
|
|
val error_division: i64 = 6;
|
||
|
|
val error_invalid_release: i64 = 11;
|
||
|
|
val error_invalid_alignment: i64 = 14;
|
||
|
|
val error_out_of_memory: i64 = 31;
|
||
|
|
|
||
|
|
# The layout kinds of `sources/program.h`.
|
||
|
|
val kind_void := 0u;
|
||
|
|
val kind_int := 1u;
|
||
|
|
val kind_uint := 2u;
|
||
|
|
val kind_float := 3u;
|
||
|
|
val kind_bool := 4u;
|
||
|
|
val kind_reference := 5u;
|
||
|
|
val kind_trait := 6u;
|
||
|
|
val kind_fixed := 7u;
|
||
|
|
val kind_struct := 8u;
|
||
|
|
val kind_union := 9u;
|
||
|
|
val kind_function := 10u;
|
||
|
|
val kind_pointer := 11u;
|
||
|
|
|
||
|
|
# The accesses of `sources/program.h`.
|
||
|
|
val access_i8 := 0u;
|
||
|
|
val access_u8 := 1u;
|
||
|
|
val access_i16 := 2u;
|
||
|
|
val access_u16 := 3u;
|
||
|
|
val access_i32 := 4u;
|
||
|
|
val access_u32 := 5u;
|
||
|
|
val access_i64 := 6u;
|
||
|
|
val access_f32 := 7u;
|
||
|
|
val access_f64 := 8u;
|
||
|
|
val access_bytes := 9u;
|