sakura::native
sakura::native is the Sakura compiler's native backend: what a program's when backend == native names, and what sakura build builds with. It takes a program sakura::compiler
has lowered and optimized and builds an x86-64 executable of it, for Linux as ELF or for
Windows as PE. It chooses the instructions of each function's intermediate code, places
its values in registers and its frame, and writes the file with its runtime, its start,
and the handlers of faults. With -g, it also writes what debuggers read: DWARF in a
.petal file beside a Linux executable, and a PDB file beside a Windows one.
The encodings are those of the libraries it writes through, all of them binary's:
binary::amd64 for the instructions, and binary::elf, binary::dwarf, binary::pe,
and binary::pdb for the files.
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. A
build evaluates the program's constants on the machine, so name the machine too:
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, sakura.native, std.io]
Building a program as sakura build does:
import std::io;
import sakura::analyzer;
import sakura::compiler;
import sakura::machine;
import sakura::native;
# Builds a program into a Linux executable, as `sakura build` does, and gives
# whether it could. Its files are read, then `core` whole, then the modules of
# `system`, `std`, and the runtime that they refer to, a round at a time. The
# program is checked for an executable, its constants are evaluated on the
# machine, and it is lowered, optimized, and built.
def build(
files: span<strview>, core: strview, system: strview, runtime: strview, library: strview, output: strview
): bool {
var finder := resolver::new();
finder.whole("core", core);
finder.add_library("system", system);
finder.add_library("runtime", runtime);
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);
}
finder.want("runtime", &mut paths); # Every executable has the runtime.
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);
checker.target.os = checker.names.intern(&"linux");
checker.target.backend = checker.names.intern(&"native");
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 false 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 false if lowered.problems.length != 0;
val made := build_executable(&mut lowered, build_options { output = output });
for &each in made {
println(each);
}
return made.length == 0;
}
core and runtime are the folders beside the sakurac executable, and system and
std, the layer over the operating system and the standard library, are found as
sakurac finds them. For Windows, the checker's os is windows and the options'
windows is set.
What it holds
- Building:
build_executable, given a lowered program and itsbuild_options, writes the executable and gives what it couldn't build.debugwrites the debugging information too, as-gasks;windowsbuilds for Windows rather than Linux;threadsplaces the values of that many functions at once;verify, as--verifyasks, replays where each function's values were placed.
- Instructions: each operation of the intermediate code as a few instructions over
the registers and slots its operands are in. A
simd's lanes use SSE, AVX, or for 64 bytes AVX-512 where the processor running the program has them, with the constants they read written after each function, and atomics use the locked instructions.f32s are worked, compared, negated, and chosen asf32s where every reader rounds or takes them so; a choice of reals by<ismaxss,minss, or a mask, with no branch; and an instruction writing a register's low lanes alone, as a conversion does, zeroes it first, so it waits on nothing the register held. - Registers: a linear scan over each function's values, in SSA form, which keeps a
value live across a call in a register the call keeps or in its slot. Where more
values live than registers hold, it spills the one read furthest ahead, or within a
loop the one read least often. A checker replays what it chose, symbolically, in the
tests and with
verify. - Calls to C: values cross as the System V ABI passes them on Linux, and as Microsoft's x86-64 ABI does on Windows, both ways, for the functions C calls back.
- Executables: the image the runtime reads, the stubs reporting traps and faults, the start the system runs, and jumps shortened once a function's code is written, with the head of a loop jumping through a table begun at a multiple of 64 bytes. For Windows, the functions imported from kernel32 and the program's libraries, the handlers of faults and of the console, and how each function unwinds.
- Debugging information: each function's lines, the calls inlined into it, how its frame unwinds, and where each variable is over its code, held in memory or, in optimized code, in registers, slots, or as a constant.
Testing
From the repository's root, blossom test runs them as sakura.native.tests, or, with
std, system, and binary beside the repository:
sakurac run --lib std=../std --lib system=../system/system --lib sakura::analyzer=analyzer \
--lib sakura::compiler=compiler --lib sakura::machine=machine --lib sakura::server=server \
--lib binary::amd64=../binary/amd64 --lib binary::elf=../binary/elf \
--lib binary::dwarf=../binary/dwarf --lib binary::pe=../binary/pe \
--lib binary::pdb=../binary/pdb --lib sakura::native=native tests/main.sakura tests/native/*.sakura
The tests are declared within the module. They build programs over core and the
runtime beside sakura, which they find as the language server does, and over the
repositories of system and the standard library beside this one, else those beside
sakura. They run
what they built and read the files written. They also call C and check what gdb and
valgrind make of the debugging information, where those are installed, which takes a few
minutes; where no standard library is found, those tests pass over what they test.