Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,244 @@
|
||||
# sakura::machine
|
||||
|
||||
`sakura::machine` is the virtual machine Sakura programs run on with
|
||||
`sakura run`. It runs the code `sakura::compiler` assembles for each function.
|
||||
It has its own stack of frames of words, the program's globals and data, and a
|
||||
heap of the blocks the program allocates and releases. Addresses are real ones,
|
||||
so a pointer reaches memory as it is, and the machine calls the system and C
|
||||
as an executable does. The compiler also runs it to compute constants while
|
||||
compiling.
|
||||
|
||||
## Using it
|
||||
|
||||
Require this repository, which exports each of the compiler's libraries, and name this
|
||||
one among a target's `deps` with the analyzer and the compiler, which it reads itself:
|
||||
|
||||
```yaml
|
||||
requires:
|
||||
- https://git.malunal.studio/sakura/std@v1.0.0
|
||||
- https://git.malunal.studio/sakura/sakurac@v1.0.0
|
||||
|
||||
targets:
|
||||
- name: app
|
||||
type: program
|
||||
deps: [sakura.analyzer, sakura.compiler, sakura.machine, std.io]
|
||||
```
|
||||
|
||||
Running a program as `sakura run` does:
|
||||
|
||||
```sakura
|
||||
import std::io;
|
||||
import sakura::analyzer;
|
||||
import sakura::compiler;
|
||||
import sakura::machine;
|
||||
|
||||
# Runs a program as `sakura run` does, and gives the status it ended with. Its
|
||||
# files are read, then `core` whole, then the modules of `system` and `std` that
|
||||
# they refer to, a round at a time. The program is checked, its constants
|
||||
# evaluated, and it is lowered, optimized, and run on the machine.
|
||||
def run(files: span<strview>, core: strview, system: strview, library: strview, args: span<strview>): i32 {
|
||||
var finder := resolver::new();
|
||||
finder.whole("core", core);
|
||||
finder.add_library("system", system);
|
||||
finder.add_library("std", library);
|
||||
var paths := vector::<string>::new();
|
||||
for each in files {
|
||||
paths.append(string::from(each, nothing {}));
|
||||
}
|
||||
paths.append(string::from(builtin_path, nothing {}));
|
||||
every_file(core, &mut paths);
|
||||
for &each in paths {
|
||||
finder.mark(each.span);
|
||||
}
|
||||
|
||||
var names := names::new();
|
||||
var notes := vector::<note>::new();
|
||||
var sources := vector::<source>::new();
|
||||
var syntaxes := vector::<syntax>::new();
|
||||
var roots := vector::<u32>::new();
|
||||
# The checker refers to each source and syntax, so there is room for every
|
||||
# file the libraries hold before any is read, and none moves.
|
||||
val room := paths.length + finder.files_held();
|
||||
sources.reserve(room);
|
||||
syntaxes.reserve(room);
|
||||
for i in 0..room {
|
||||
break if i == paths.length;
|
||||
val at := paths[i].span;
|
||||
sources.append(at == builtin_path ? source::from(at, builtin_text) : source::open(at));
|
||||
syntaxes.append(syntax::new());
|
||||
var lexing := lexer::new(sources.at(i), &mut names, &mut notes, i as u32);
|
||||
lexing.tokenize_all(syntaxes.at(i));
|
||||
var parsing := parser::new(syntaxes.at(i), &names, &mut notes, i as u32);
|
||||
roots.append(parsing.parse());
|
||||
finder.refer(syntaxes.at(i), &names, &mut paths); # Adds the files it refers to.
|
||||
}
|
||||
|
||||
var checker := checker::new(&mut names, &mut notes);
|
||||
for i in 0..paths.length {
|
||||
checker.add(sources.at(i), syntaxes.at(i), roots.at(i));
|
||||
}
|
||||
checker.check();
|
||||
var results := outcomes::new();
|
||||
if notes.length == 0 {
|
||||
results = evaluate(&mut checker);
|
||||
}
|
||||
for &each in notes {
|
||||
val held := &sources.at(each.file);
|
||||
val at := held.lines.place_of(each.offset);
|
||||
println($"{held.path}:{at.line}:{at.column}: {each.code}: {each.message(held)}");
|
||||
}
|
||||
return 2 if notes.length != 0;
|
||||
|
||||
var lowered := lowerer::new(&mut checker, lowering { level = 2 });
|
||||
lowered.evaluated = move(&mut results);
|
||||
lowered.compile();
|
||||
for &each in lowered.problems {
|
||||
println(each);
|
||||
}
|
||||
return 2 if lowered.problems.length != 0;
|
||||
lowered.drop_unreached_functions();
|
||||
|
||||
var running := interpreter::new(&mut lowered, interpreting { checks = false });
|
||||
val why := running.run(args);
|
||||
return running.status if why == halt::exited;
|
||||
if why != halt::none {
|
||||
var report := string::new(nothing {});
|
||||
running.report(why, &mut report);
|
||||
print(report);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
`core` and `system` are the folders beside the `sakura` executable, and `std`
|
||||
is its standard library. The program is checked for Linux unless the checker's
|
||||
`target` is given another system.
|
||||
|
||||
## Embedding it
|
||||
|
||||
A program embeds the machine to run code it compiles as it runs, as a game runs
|
||||
its scripts. It compiles as `run` above does, lowering what it calls besides
|
||||
`main`: every function, with `lowering::everything`, or those `main` gives the
|
||||
host as values, which are kept. It then begins the program and calls its
|
||||
functions itself, which call the host's own functions in turn:
|
||||
|
||||
```sakura
|
||||
# What the program calls the host by: a foreign declaration of a name, as C's
|
||||
# are declared, which the host provides.
|
||||
# extern module game when os == windows;
|
||||
# pub def spawn_at(x: f64, y: f64): u64;
|
||||
|
||||
def spawn_at(x: f64, y: f64): u64 { ... } # the host's own
|
||||
|
||||
var running := interpreter::new(&mut lowered, interpreting{ capture = true, steps = 10000000 });
|
||||
val given: cfn<def(f64, f64): u64> = spawn_at;
|
||||
running.provide("spawn_at", given as u64); # before `begin`, or after, which replaces it
|
||||
if running.begin() != halt::none { ... } # the C it calls found, the globals given their values
|
||||
|
||||
var args: u64[2] = [state, f64_bits(0.016)]; # a word each: integers, pointers, references, a float's f64 bits
|
||||
val why := running.call(update, args[:]); # or `call_value`, given a function value's two words
|
||||
if why != halt::none {
|
||||
var report := string::new(nothing {});
|
||||
running.report(why, &mut report); # the fault, and where each function was
|
||||
}
|
||||
val got := running.result(0); # the first word it returned
|
||||
val said := running.take_output(); # what it printed
|
||||
```
|
||||
|
||||
- **Calls**: `call` calls a function by its index among the program's, given
|
||||
the words of its parameters as a call passes them: a word for each integer,
|
||||
bool, pointer, and reference, an `f64` for each float, `f32`s too, the words
|
||||
of a value held in words, and the address of the bytes of one passed in
|
||||
memory, which is copied. A result held in memory is written where `call` is
|
||||
told; one held in words is `result`'s. A function value's first word is one
|
||||
more than its function, and its second what a closure uses, which
|
||||
`call_value` takes, so a program gives its host a function as a value.
|
||||
- **The host's functions**: `provide` gives a foreign declaration's name an
|
||||
address of the host's, found before any library's, which the program calls as
|
||||
it calls C, so a `cfn` of a function of the host's own is one. A host function
|
||||
may call back into the program: that call runs above the calls under way, and
|
||||
where it faults they stop too, as the host returns to them, the trace naming
|
||||
each. The machine calls C only where the system running it is Windows.
|
||||
- **Steps**: `interpreting::steps` stops `begin`, `run`, and each call the host
|
||||
makes once it takes that many steps, jumps and calls, with `step_limit`, as a
|
||||
program looping forever would; the next call has as many again.
|
||||
- **Output**: with `interpreting::capture`, what the program writes to its
|
||||
output and error is kept for `take_output`, on Windows through `WriteFile`
|
||||
and `WriteConsoleW` too.
|
||||
- **Memory**: the program and its host share their memory, so a host may lend
|
||||
the program its own data, as a pointer, and read what the program made. What
|
||||
the program allocates the machine releases as it closes, so the host keeps no
|
||||
block of the program's past it, and the program releases none of the host's.
|
||||
|
||||
`hearth::scripting`, of hearth, embeds the machine this way.
|
||||
|
||||
## What it holds
|
||||
|
||||
- **The machine**: `interpreter`, made of a lowered program and its
|
||||
`interpreting` options. `run` calls `main` with the arguments given, and
|
||||
`begin`, `call`, and `call_value` let a host call the program's functions
|
||||
itself. The result is a `halt`: how the program stopped, as by `exit` or a
|
||||
fault, with `status` holding its exit status.
|
||||
- `report` writes a fault and where each running function was, in the same
|
||||
form as an executable's fault report.
|
||||
- With `checks`, as `run --checks` asks, each read and write through a
|
||||
reference or pointer is checked, and `report_leaks` lists the blocks
|
||||
nothing reaches. Once the program starts a thread, `races.sakura` checks
|
||||
those reads and writes for races too, ordering the threads by vector
|
||||
clocks that starting a thread and atomics' orders move, as
|
||||
ThreadSanitizer does.
|
||||
- **Compiled code**: where the machine runs as an executable on Windows or
|
||||
Linux, as `sakurac` and a host embedding it do, `jit.sakura` compiles each
|
||||
function to x86-64 the first time it is called, through the encoder in
|
||||
`x86.sakura`, and runs that rather than interpreting it. It is interpreted instead while
|
||||
constants are evaluated, where memory is checked, and where
|
||||
`interpreting::interpret` asks, as `run --interpret` does.
|
||||
- Compiled code keeps the interpreter's frames, call records, and halts, so
|
||||
compiled and interpreted functions call each other, a fault's trace is the
|
||||
same, and a host's calls, C calling back, threads, and `steps` work as they
|
||||
do interpreted.
|
||||
- Each operation becomes the instructions doing its work. The words a
|
||||
function reads and writes most, loops weighing most, are held in registers
|
||||
over the code from where each is first used to where it is last: integers
|
||||
in `rsi`, `rdi`, `r13`, and `rbp`, and reals in `xmm6` to `xmm15`, a word
|
||||
every value of which is a single's held as the single, so `f32` arithmetic
|
||||
is the processor's arithmetic of singles.
|
||||
- Calls of compiled functions are calls of the processor. A call of C whose
|
||||
arguments and result are each a word calls C directly; other calls of C,
|
||||
and what the interpreter runs rarely, as formatting, allocating, and the
|
||||
system, call the machine's own `slow_op`, as the interpreter does, through
|
||||
code keeping `xmm6` to `xmm15`, which the machine's own code does not keep.
|
||||
- Steps are counted in a register, settled with what the host allows and a
|
||||
thread's turn as they run out and around calls of the machine's own code.
|
||||
- Where the system's stack of the thread running is within 128 KiB of its
|
||||
end, calls are interpreted, which take only the machine's stack.
|
||||
- On the benchmarks of `docs/performance.md`, compiled code runs integer and
|
||||
memory work within 1.0 to 1.5 times built code's time, and `f32` work
|
||||
within 2 to 4 times, where the interpreter takes 4 to 27 times.
|
||||
- **The heap**: the blocks the program allocates and releases. With checks,
|
||||
released blocks are held back a while, so a late use still finds them
|
||||
released.
|
||||
- **Threads**: each thread of the program is a thread of the system. They take
|
||||
turns running code, so they run at the same time but not in parallel.
|
||||
- **The system and C**: the program's system calls, its environment, and its
|
||||
signals, whose handlers run at the program's safe points. On Windows, it
|
||||
calls C through code it makes that follows Microsoft's x86-64 ABI.
|
||||
- **Constants**: `evaluate` runs the program that computes a checked
|
||||
program's constants, each constant once, before the ones that read it, and
|
||||
gives the `outcomes` that lowering takes.
|
||||
|
||||
## Testing
|
||||
|
||||
From the repository's root, `blossom test` runs them as `sakura.machine.tests`, or:
|
||||
|
||||
```sh
|
||||
sakurac run --lib sakura::analyzer=analyzer --lib sakura::compiler=compiler \
|
||||
--lib sakura::machine=machine tests/main.sakura tests/machine/*.sakura
|
||||
```
|
||||
|
||||
The tests are declared within the module. Each runs a small program, written
|
||||
over a small `core` of its own, and compares what it wrote and how it stopped
|
||||
with what is expected. `sakura.machine.native` runs the same tests built as an
|
||||
executable, where the machine compiles what it runs, and the corpus,
|
||||
`sakurac.corpus`, runs every program of `tests/programs` compiled so too.
|
||||
Reference in new issue
Block a user