137 lines
5.0 KiB
Markdown
137 lines
5.0 KiB
Markdown
# sakura::compiler
|
|
|
|
`sakura::compiler` is the middle of the Sakura compiler. It lowers a program
|
|
that `sakura::analyzer` has checked to intermediate code: values each assigned
|
|
once, in blocks that take parameters where control flow joins. It optimizes
|
|
that code, and assembles each function into the code `sakura::machine` runs.
|
|
The compiler's back end builds executables from the same intermediate code.
|
|
|
|
## 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, 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, std.io]
|
|
```
|
|
|
|
A program is lowered from the checker that checked it, as `sakura::analyzer`'s
|
|
README checks one:
|
|
|
|
```sakura
|
|
import std::io;
|
|
import sakura::analyzer;
|
|
import sakura::compiler;
|
|
|
|
# Checks a file importing nothing, with `core`, whose folder is given, then
|
|
# lowers it, optimizes it at a level, and prints the code of the function of a
|
|
# name. Gives whether it could.
|
|
def lower(path: strview, core: strview, level: u32, name: strview): bool {
|
|
var paths := vector::<string>::new();
|
|
paths.append(string::from(path, nothing {}));
|
|
paths.append(string::from(builtin_path, nothing {}));
|
|
every_file(core, &mut paths);
|
|
|
|
var names := names::new();
|
|
var notes := vector::<note>::new();
|
|
var sources := vector::<source>::new();
|
|
var syntaxes := vector::<syntax>::new();
|
|
var roots := vector::<u32>::new();
|
|
sources.reserve(paths.length);
|
|
syntaxes.reserve(paths.length);
|
|
for i in 0..paths.length {
|
|
val at := paths[i].view();
|
|
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());
|
|
}
|
|
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();
|
|
return false if notes.length != 0;
|
|
|
|
var lowered := lowerer::new(&mut checker, lowering { verify = true, level = level });
|
|
lowered.compile();
|
|
for &each in lowered.problems {
|
|
println(each);
|
|
}
|
|
return false if lowered.problems.length != 0;
|
|
lowered.drop_unreached_functions();
|
|
for &function in lowered.program.functions {
|
|
if function.lowered && function.name.view() == name {
|
|
print(function.body.print());
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
```
|
|
|
|
For `def square(x: i32): i32 { return x * x; }` at level 0, it prints:
|
|
|
|
```
|
|
b0(v0: i64):
|
|
v1 = mul v0, v0
|
|
v2 = wrap v1, 32, signed
|
|
ret v2
|
|
```
|
|
|
|
The values of constants are computed by running code on `sakura::machine`.
|
|
Its `evaluate` gives the `outcomes`, which are set as the lowerer's `evaluated`
|
|
before `compile`.
|
|
|
|
## What it holds
|
|
|
|
- **Lowering**: `lowerer` lowers each function once something that runs names
|
|
it, by `lowering`'s options. `emitter/` has a file for each part of the
|
|
language.
|
|
- `layouts` lays out types as C lays out the same fields.
|
|
- `dispatch` holds the vtables of traits.
|
|
- `globals` gives the globals their values before `main` runs.
|
|
- `escapes` and `flows` find where each reference may go, so a local whose
|
|
address is kept lives in a cell of the frame.
|
|
- **Intermediate code** (`intermediate/`): `code`, a function's blocks,
|
|
instructions and cells. `builder` makes it in one pass. `verifier` checks it
|
|
is well formed. `printer` writes it as text.
|
|
- **Optimizing** (`optimizer/`):
|
|
- `-O1` simplifies, cleans up control flow, numbers values, and removes dead
|
|
code.
|
|
- `-O2` also inlines and promotes cells to values.
|
|
- Below `-O2`, calls of `@inline` functions are inlined all the same, and at
|
|
`-O0` that is all that changes.
|
|
- At `-O1` and above, a function asking which vector instructions the
|
|
processor has, or counting a word's bits, gets a version for x86-64-v3's,
|
|
and an executable's stores filling 16 bytes of a struct are merged into one
|
|
of a vector once the rest is done.
|
|
- Each pass leaves the code well formed.
|
|
- **The machine's code**: `bytecode` assembles each function into the words
|
|
`sakura::machine` runs.
|
|
- **Constants**: the program that computes the constants and the `if const`
|
|
conditions the checker left undecided.
|
|
- **Threads**: `workers` shares functions among threads, which optimize and
|
|
assemble them into the same bytes however many there are.
|
|
|
|
## Testing
|
|
|
|
From the repository's root, `blossom test` runs them as `sakura.compiler.tests`, or:
|
|
|
|
```sh
|
|
sakurac run --lib sakura::analyzer=analyzer --lib sakura::compiler=compiler \
|
|
tests/main.sakura tests/compiler/*.sakura
|
|
```
|
|
|
|
The tests are declared within the module. Most check and lower a small program
|
|
and compare its printed code with what is expected, after the verifier has
|
|
found nothing wrong with it.
|