Files
sakurac/runtime/start.sakura
T
2026-12-06 13:52:57 -06:00

319 lines
11 KiB
Plaintext

#! @file start.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how an executable starts and stops: the arguments it gives `main`,
#! exiting, and the report a fault gives.
import runtime::win32;
module runtime when backend == native;
#! Writes a view of the text of each argument into a slot, for `main`, leaving
#! out the path of the executable, as the machine gives only those after it.
#!
#! A view owns nothing, so each one views the text the system left on the stack
#! rather than a copy of it, and the run of views is a block the program keeps
#! for as long as it runs.
#! @param out The slot the view of the views is written to.
def arguments(out: *mut u8): void {
val info := executable();
val argc := (*(*info).state).argc > 0 ? (*(*info).state).argc - 1 : 0;
val argv := (*(*info).state).argv + 1;
val views := argc > 0 ? acquire(argc * 16) : null;
for i in 0..argc {
val text := argv[i] as *mut u8;
var length := 0u;
while text[length] != 0 {
length++;
}
val view := (views + i * 16) as *mut u64;
view[0] = text as u64;
view[1] = length;
}
val slot := out as *mut u64;
slot[0] = views as u64;
slot[1] = argc;
}
#! The vector instructions the program uses, of those the processor running it
#! has, which the entry found by asking it as it started, one bit each: all of
#! them, but where the variable `SAKURA_CPU` names fewer, only those, so the
#! program runs as it would on a processor without the rest. It names them
#! joined by `+`: `sse2`, `ssse3`, `sse4.1`, `sse4.2`, `avx`, `avx2`, `fma`,
#! `popcnt`, `lzcnt`, `bmi1`, and `avx512`, or a level of x86-64, `x86-64`,
#! `x86-64-v2`, `x86-64-v3`, or `x86-64-v4`, for those it has. SSE2 is every x86-64 processor's, so always
#! kept.
#! @param found The instruction sets the processor has.
#! @returns Those the program uses.
def processor(found: u64): u64 {
val info := executable();
val argc := (*(*info).state).argc;
var at := (*(*info).state).argv + argc + 1;
while at[0] != 0 {
val text := at[0] as *u8;
if named_at(text, 11, "SAKURA_CPU=") {
return found & processor_named(text + 11);
}
at = at + 1;
}
return found;
}
# The instruction sets `SAKURA_CPU` names, from its value.
def processor_named(text: *u8): u64 {
var allowed: u64 = 0x01;
var start: u64 = 0;
var i: u64 = 0;
while true {
val c := text[i];
if c == 0 || c == '+' as u8 {
val name := text + start;
val length := i - start;
if named_at(name, length, "ssse3") { allowed = allowed | 0x02; }
if named_at(name, length, "sse4.1") { allowed = allowed | 0x04; }
if named_at(name, length, "sse4.2") { allowed = allowed | 0x08; }
if named_at(name, length, "avx") { allowed = allowed | 0x10; }
if named_at(name, length, "avx2") { allowed = allowed | 0x20; }
if named_at(name, length, "fma") { allowed = allowed | 0x40; }
if named_at(name, length, "popcnt") { allowed = allowed | 0x80; }
if named_at(name, length, "lzcnt") { allowed = allowed | 0x100; }
if named_at(name, length, "bmi1") { allowed = allowed | 0x200; }
if named_at(name, length, "avx512") { allowed = allowed | 0x400; }
if named_at(name, length, "x86-64-v2") { allowed = allowed | 0x8F; }
if named_at(name, length, "x86-64-v3") { allowed = allowed | 0x3FF; }
if named_at(name, length, "x86-64-v4") { allowed = allowed | 0x7FF; }
break if c == 0;
start = i + 1;
}
i++;
}
return allowed;
}
# Whether bytes are a name.
def named_at(bytes: *u8, length: u64, name: strview): bool {
return false if length != name.length;
for k in 0..length {
return false if bytes[k] != name[k];
}
return true;
}
module runtime when backend == native && os == linux;
# Retries a system call interrupted by a signal.
def retried(n: i64, a: u64, b: u64, c: u64): i64 {
while true {
val result := syscall(n, a, b, c, 0, 0, 0);
if result != -4 {
return result;
}
}
return 0;
}
def write_all(file: u64, data: *u8, length: u64): void {
var written := 0u;
while written < length {
val count := retried(call_write, file, (data + written) as u64, length - written);
if count <= 0 {
return;
}
written = written + count as u64;
}
}
module runtime when backend == native;
# The function whose code holds an address, or null.
def function_at(address: u64): *function {
val info := executable();
for i in 0..(*info).function_count {
val each := (*info).functions + i;
if address >= (*each).start && address < (*each).end {
return each;
}
}
return null;
}
# Writes ` at name (path:line:column)` for an address within a function, unless
# the function belongs to the runtime.
def put_frame(into: *mut builder, address: u64): void {
val found := function_at(address);
if found == null || (*found).runtime != 0 || (*found).line_count == 0 {
return;
}
var line := (*found).lines;
for i in 1..(*found).line_count {
val next := (*found).lines + i;
if (*next).offset as u64 > address - (*found).start {
break;
}
line = next;
}
# Code inlined names the function it came from, where it is, then each function
# it was called within, where it was called, out to the function itself.
val info := executable();
val path := (*info).paths + (*line).path as u64;
if (*line).site == 0 {
put_place(into, (*found).name, (*found).name_length, (*path).bytes, (*path).length, (*line).line as u64, (*line).column as u64);
return;
}
var site := (*info).sites + ((*line).site - 1) as u64;
val inlined := (*info).functions + (*site).function as u64;
put_place(into, (*inlined).name, (*inlined).name_length, (*path).bytes, (*path).length, (*line).line as u64, (*line).column as u64);
while true {
val called := (*info).paths + (*site).path as u64;
var within := found;
val parent := (*site).parent;
if parent != 0 {
within = (*info).functions + (*((*info).sites + (parent - 1) as u64)).function as u64;
}
put_place(into, (*within).name, (*within).name_length, (*called).bytes, (*called).length, (*site).line as u64, (*site).column as u64);
break if parent == 0;
site = (*info).sites + (parent - 1) as u64;
}
}
# Writes one frame of a trace: ` at name (path:line:column)`.
def put_place(into: *mut builder, name: *u8, name_length: u64, path: *u8, path_length: u64, line: u64, column: u64): void {
put_text(into, text_at);
append(into, name, name_length);
put_text(into, text_open);
append(into, path, path_length);
put_text(into, text_colon);
put_unsigned(into, line);
put_text(into, text_colon);
put_unsigned(into, column);
put_text(into, text_close);
}
module runtime when backend == native && os == linux;
#! Stops the program with a status, having run everything asked of it.
#! @param code The status to stop with.
def exit(code: i64): void {
syscall(call_exit_group, code as u64, 0, 0, 0, 0, 0);
}
module runtime when backend == native;
#! Stops the program at once, reporting the functions that were running.
#!
#! The report is the one every fault gives, so an abort reads like any other way
#! the program stops, and exits with the same status.
def abort(): void {
trap(error_aborted);
}
# The text `fault` was given, which the report of its trap gives.
var fault_data: *u8;
var fault_length: u64;
#! Stops the program at once, reporting the text given, then the functions that
#! were running, as every fault does.
#! @param data The text's bytes.
#! @param length How many there are.
def fault(data: *u8, length: u64): void {
# The lock is kept to the end, so a thread faulting beside it waits rather
# than changing the text.
hold();
fault_data = data;
fault_length = length;
trap(error_faulted);
}
module runtime when backend == native && os == linux;
#! Reports a trap and exits with 1: its error, then the functions it happened
#! in, from the address the trap was called from and the chain of frame
#! pointers.
#! @param code The error, as the machine numbers them.
#! @param address The address the trap was called from, in the function that
#! faulted.
#! @param chain The frame pointer of that function, whose saved frame
#! pointers and return addresses lead to each caller.
#! @remarks It does not return.
def fail(code: i64, address: u64, chain: *mut u64): void {
hold();
val report := acquire(size_of::<builder>()) as *mut builder;
put_text(report, text_prefix);
if code == error_faulted && fault_data != null {
append(report, fault_data, fault_length);
} else {
put_text(report, code >= 0 && code < 31 ? code as u64 : text_out_of_memory);
}
put_text(report, text_newline);
# A stack overflow is found entering the function called, so its caller is first.
if code != 8 {
put_frame(report, address - 1);
}
var each := chain;
while each != null {
put_frame(report, each[1] - 1);
each = each[0] as *mut u64;
}
write_all(2, (*report).data, (*report).length);
syscall(call_exit_group, 1, 0, 0, 0, 0, 0);
}
module runtime when backend == native && os == windows;
# Writes to the standard output or error, a file `file` numbers as Linux does:
# to a console as the text it is, and elsewhere as its bytes.
def write_all(file: u64, data: *u8, length: u64): void {
val handle := GetStdHandle((0xFFFFFFF6 - file) as u32);
var mode: u32 = 0;
if GetConsoleMode(handle, &mut mode) != 0 && write_console(handle, data, length) {
return;
}
var written := 0u;
while written < length {
val left := length - written;
val asked: u32 = left > 0x40000000 ? 0x40000000 : left as u32;
var count: u32 = 0;
if WriteFile(handle, data + written, asked, &mut count, null) == 0 || count == 0 {
return;
}
written = written + count as u64;
}
}
#! Stops the program with a status, having run everything asked of it.
#! @param code The status to stop with.
def exit(code: i64): void {
ExitProcess(code as u32);
}
#! Reports a trap and exits with 1, as on Linux.
#! @param code The error, as the machine numbers them.
#! @param address The address the trap was called from, in the function that
#! faulted.
#! @param chain The frame pointer of that function, whose saved frame
#! pointers and return addresses lead to each caller.
#! @remarks It does not return.
def fail(code: i64, address: u64, chain: *mut u64): void {
hold();
val report := acquire(size_of::<builder>()) as *mut builder;
put_text(report, text_prefix);
if code == error_faulted && fault_data != null {
append(report, fault_data, fault_length);
} else {
put_text(report, code >= 0 && code < 31 ? code as u64 : text_out_of_memory);
}
put_text(report, text_newline);
# A stack overflow is found entering the function called, so its caller is first.
if code != 8 {
put_frame(report, address - 1);
}
var each := chain;
while each != null {
put_frame(report, each[1] - 1);
each = each[0] as *mut u64;
}
write_all(2, (*report).data, (*report).length);
ExitProcess(1);
}