Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,151 @@
|
||||
# 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].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());
|
||||
}
|
||||
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.span == 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. Simplifying folds constants, grouping those applied one after another
|
||||
and adding them last, so an element after another is read at an offset from
|
||||
its address. Numbering reads a word as the constant a branch or switch found
|
||||
it equal to. Bit tracking takes out narrowings and masks of bits already zero
|
||||
or never read, and decides comparisons by what is known of a word, as LLVM's
|
||||
known and demanded bits do. Lifting works out what both ways out of a branch
|
||||
work out alike before the branch. Loops move out what they work out the same
|
||||
each time round, regrouping `(i + a) + b` as `i + (a + b)` so `a + b` moves too.
|
||||
- `-O2` also inlines and promotes cells to values. A function too large to
|
||||
inline everywhere is inlined at a call where its copy would cost little,
|
||||
`worth.sakura` working that out for each call as LLVM's inliner does: what
|
||||
the call's constants fold away, and what promotes once copied, costs nothing.
|
||||
Promoting first gives each
|
||||
value a cell holds in turn, as a statement's temporary does, a cell of its own,
|
||||
and in an executable's code reads a vector of `f32`s written by fields, or
|
||||
fields of one written whole, as the cell was written, so it holds one width.
|
||||
It runs once more after numbering.
|
||||
- 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.
|
||||
Reference in new issue
Block a user