201 lines
12 KiB
Plaintext
201 lines
12 KiB
Plaintext
#! @file program.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the program lowering makes: the layouts of its types, the
|
||
|
|
#! signatures its calls pass arguments by, its functions, each with its
|
||
|
|
#! intermediate code, the C functions it calls, the vtables of the traits it
|
||
|
|
#! refers to values through, the bytes it holds for as long as it runs, and
|
||
|
|
#! the size of its globals. Layouts and signatures are those the C compiler's
|
||
|
|
#! `sources/program.h` describes, so the runtime reads the image an executable
|
||
|
|
#! is built with as it reads the C compiler's.
|
||
|
|
|
||
|
|
module sakura::compiler;
|
||
|
|
|
||
|
|
|
||
|
|
#! Every frame word, and every scalar in a frame, is this many bytes.
|
||
|
|
pub const word_size: u64 = 8;
|
||
|
|
|
||
|
|
#! The kinds of layout, numbered as `sources/program.h` and the runtime number
|
||
|
|
#! them.
|
||
|
|
pub enum lk : u8 {
|
||
|
|
empty, #! `void`, which holds nothing.
|
||
|
|
signed, #! A signed integer.
|
||
|
|
unsigned, #! An unsigned integer.
|
||
|
|
floating, #! A float.
|
||
|
|
boolean, #! `bool`.
|
||
|
|
reference, #! A reference: the address of what it refers to.
|
||
|
|
trait_reference, #! A reference to a trait: a reference, then its vtable.
|
||
|
|
fixed, #! A fixed array: its elements, one after another.
|
||
|
|
record, #! A struct: its fields where `offset` says.
|
||
|
|
variant, #! A union: its tag, which field it holds, then that field.
|
||
|
|
function, #! A function value: its function plus one, then its environment.
|
||
|
|
pointer, #! A pointer: an address the program follows itself.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The layout of a type: how its values are held in memory and in frames.
|
||
|
|
pub struct layout {
|
||
|
|
pub var kind: lk; #! What kind of value it holds.
|
||
|
|
pub var how: access; #! How a scalar is read and written, or `bytes`.
|
||
|
|
pub var bits: u32; #! The bits of a number.
|
||
|
|
pub var size: u64; #! The bytes it takes in memory.
|
||
|
|
pub var align: u64; #! What its address in memory is a multiple of.
|
||
|
|
pub var frame: u64; #! The bytes it takes in a frame, where a scalar is widened to a word.
|
||
|
|
pub var type_id: u32; #! The type laid out.
|
||
|
|
pub var name: u32; #! The name of a struct, union, or enum, or 0.
|
||
|
|
pub var element: u32; #! One more than the layout of a fixed array's elements, or of what a reference or pointer reaches, or 0.
|
||
|
|
pub var fields: u32; #! The first of its fields in the program's `fields`.
|
||
|
|
pub var count: u32; #! How many fields it has, or elements a fixed array holds.
|
||
|
|
pub var pointers: bool; #! Whether it may hold a reference, a trait reference, or a function value.
|
||
|
|
pub var texts: bool; #! Whether it is text, written as the bytes it holds rather than as its fields.
|
||
|
|
pub var views: bool; #! Whether it is a view, written as the elements it views.
|
||
|
|
pub var viewed: u32; #! One more than the layout of the elements a view views, or 0.
|
||
|
|
pub var text: u32; #! The field saying where the bytes or elements begin, the next saying how many.
|
||
|
|
pub var niche: u32; #! Of a union laid out without a tag: one more than the field it holds wherever its first word is not zero, the other field, which is empty, held where it is.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A field of a struct or union, or a member of an enum, which its layout
|
||
|
|
#! names as a field whose offset is its value.
|
||
|
|
pub struct slot_field {
|
||
|
|
pub var name: u32; #! The ID of its name.
|
||
|
|
pub var layout: u32; #! Its layout, or 0 for a member of an enum.
|
||
|
|
var type_id: u32; #! Its type.
|
||
|
|
pub var offset: u64; #! Where it begins, or a member's value.
|
||
|
|
var decl: u32; #! The field's declaration.
|
||
|
|
var spread: bool; #! Whether it spreads another struct's fields into its own.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How a call passes arguments and takes a result: each parameter where it is
|
||
|
|
#! in the frame of the function called, and the result's layout.
|
||
|
|
pub struct signature {
|
||
|
|
pub var params: vector<u32>; #! The layout of each parameter.
|
||
|
|
var offsets: vector<u64>; #! Where each begins among the parameters.
|
||
|
|
var size: u64; #! The bytes of every parameter, in a frame.
|
||
|
|
pub var result: u32; #! The result's layout.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A function of the program.
|
||
|
|
pub struct function {
|
||
|
|
pub var name: string; #! Its name, as a trace writes it, as `vec2::normalize`.
|
||
|
|
pub var decl: u32; #! Its declaration, or 0 for what lowering makes.
|
||
|
|
pub var instance: u32; #! The instance of a generic it is, or 0.
|
||
|
|
pub var node: u32; #! The node of an anonymous function, or of a function named as a value of another function type, which an adapter calls; or 0.
|
||
|
|
pub var adapted: u32; #! Of an adapter, one more than the function it calls as the function type it is of asks, or 0.
|
||
|
|
pub var outer: u32; #! Of an anonymous function, the declaration of the function it is written in.
|
||
|
|
pub var table: u32; #! The result table of an anonymous function, which holds its nodes' results.
|
||
|
|
pub var file: u32; #! The file it is declared in.
|
||
|
|
pub var offset: u32; #! The byte of the file it is declared at.
|
||
|
|
pub var signature: u32; #! How it is called.
|
||
|
|
pub var body: code; #! Its code, once lowered.
|
||
|
|
pub var compiled: bytecode; #! Its code for the machine, once assembled.
|
||
|
|
pub var lowered: bool; #! Whether its code is lowered, and kept: what nothing reaches once the program is optimized is dropped, as though never lowered.
|
||
|
|
pub var finished: bool; #! Whether lowering its code ended, which lowering another function within it, as evaluating while checking does, may not have.
|
||
|
|
var optimized: bool; #! Whether its code is optimized, which is done once.
|
||
|
|
var unfinished: bool; #! Whether only the passes up to inlining ran on it, as the function alone calling it copies it whole, and optimizes it there.
|
||
|
|
var inlining: inline_facts; #! What inlining knows of it.
|
||
|
|
var order: vector<u32>; #! Its blocks in reverse postorder, made once it is first inlined.
|
||
|
|
var named: vector<u32>; #! The functions its code names as values, and the code inlined into it named, which optimizing it waits for as for those it calls, so a call through one may become a call of it.
|
||
|
|
var queued: bool; #! Whether it waits to be lowered.
|
||
|
|
pub var closure: bool; #! Whether its environment follows its parameters.
|
||
|
|
pub var callback: bool; #! Whether C calls it back, as it was given where a `cfn` is expected.
|
||
|
|
pub var stepping: bool; #! Whether it is a method an iterator gives its elements by, `next` or `next_back`, or a functor's `invoke`, which a loop calling it inlines though it is larger.
|
||
|
|
pub var inline: bool; #! Whether it is written `@inline`, so each call of it is copied wherever inlining can copy one, at every level, whatever its size.
|
||
|
|
pub var taken: bool; #! Whether it is reached other than by the program's calls: as a value code takes or a constant holds, or by the backend calling it by name.
|
||
|
|
var evaluation: u32; #! One more than the evaluation it computes, of a program evaluating constants, or 0.
|
||
|
|
var builtin: u32; #! The name of the method it gives a built-in type, or 0.
|
||
|
|
var owner_type: u32; #! The built-in type it gives the method.
|
||
|
|
pub var version: u32; #! One more than its version for processors with x86-64-v3's instructions, which it goes to where the processor has them, or 0.
|
||
|
|
var unoptimized: code; #! Of an executable's function, its code as lowered, before optimizing, which its version is made from, until the versions are made; otherwise empty.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A C function the program calls by name, which the system finds in the
|
||
|
|
#! libraries its file's `extern import`s name, or among what the running program
|
||
|
|
#! has where it names none.
|
||
|
|
pub struct linked {
|
||
|
|
pub var name: u32; #! The ID of its name.
|
||
|
|
pub var library: string; #! The first of its libraries, which is its only one on Windows, or empty.
|
||
|
|
pub var libraries: vector<string>; #! Every library its file names for the target, which a dynamically linked executable needs.
|
||
|
|
pub var decl: u32; #! Its declaration.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The functions a type gives for a trait's methods, in the order the trait's
|
||
|
|
#! dispatch numbers them.
|
||
|
|
pub struct vtable {
|
||
|
|
var type_id: u32; #! The type referred to.
|
||
|
|
var trait_id: u32; #! The trait's type, given its arguments.
|
||
|
|
pub var layout: u32; #! The layout of what the trait reference refers to.
|
||
|
|
pub var functions: vector<u32>; #! The function of each method, or `no_function` for an `@optional` one left out.
|
||
|
|
pub var owners: vector<u32>; #! The vtable each method's code addresses: this, or a composed trait's whose default it is.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What stands where a vtable has no function, as for an `@optional` method a
|
||
|
|
#! type leaves out.
|
||
|
|
pub const no_function: u32 = 0xFFFFFFFF;
|
||
|
|
|
||
|
|
#! Where each vtable of one trait goes as a reference to it converts to a
|
||
|
|
#! reference to another, once every vtable is made.
|
||
|
|
pub struct retrait {
|
||
|
|
var from: u32; #! The trait's type converted from.
|
||
|
|
var to: u32; #! The trait's type converted to.
|
||
|
|
pub var vtables: vector<u32>; #! The vtable each becomes, by the vtable it was.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where bytes held for as long as the program runs hold the address of other
|
||
|
|
#! such bytes: `addend` bytes into datum `target`, written at byte `at`.
|
||
|
|
pub struct relocation {
|
||
|
|
pub var at: u64; #! The byte the address is written at.
|
||
|
|
pub var target: u32; #! The datum it points into.
|
||
|
|
pub var addend: u64; #! The byte within that datum.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Bytes the program holds for as long as it runs, as text and the elements of
|
||
|
|
#! a literal viewed, which are never written. Each is followed by a zero the
|
||
|
|
#! length does not count, so text is also what C reads.
|
||
|
|
pub struct datum {
|
||
|
|
pub var bytes: vector<u8>; #! Its bytes.
|
||
|
|
pub var align: u64; #! What its address is a multiple of.
|
||
|
|
pub var relocations: vector<relocation>; #! Where its bytes hold the address of a datum.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The program.
|
||
|
|
pub struct program {
|
||
|
|
pub var layouts: vector<layout>; #! Every layout, by index.
|
||
|
|
pub var fields: vector<slot_field>; #! The fields of every layout, as runs each layout's `fields` begins.
|
||
|
|
pub var signatures: vector<signature>; #! Every signature, by index.
|
||
|
|
pub var functions: vector<function>; #! Every function, by index.
|
||
|
|
pub var linked: vector<linked>; #! The C functions it calls by name.
|
||
|
|
pub var vtables: vector<vtable>; #! Its vtables.
|
||
|
|
pub var retraits: vector<retrait>; #! The conversions between references to traits it makes.
|
||
|
|
pub var data: vector<datum>; #! What it holds for as long as it runs.
|
||
|
|
pub var globals: u64; #! The bytes of its globals.
|
||
|
|
pub var preset: vector<u8>; #! The bytes its globals begin with, as far as values known while compiling give them; the rest begin as zeros.
|
||
|
|
pub var init: u32; #! The function initializing the globals.
|
||
|
|
pub var main: u32; #! The function `main`, or `no_function`.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Makes an empty program.
|
||
|
|
#! @returns The program.
|
||
|
|
pub def program::new(): program {
|
||
|
|
return program {
|
||
|
|
layouts = vector::<layout>::new(),
|
||
|
|
fields = vector::<slot_field>::new(),
|
||
|
|
signatures = vector::<signature>::new(),
|
||
|
|
functions = vector::<function>::new(),
|
||
|
|
linked = vector::<linked>::new(),
|
||
|
|
vtables = vector::<vtable>::new(),
|
||
|
|
retraits = vector::<retrait>::new(),
|
||
|
|
data = vector::<datum>::new(),
|
||
|
|
preset = vector::<u8>::new(),
|
||
|
|
init = no_function,
|
||
|
|
main = no_function
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Rounds a size up to a multiple of an alignment.
|
||
|
|
#! @param size The size.
|
||
|
|
#! @param align The alignment, which is not 0.
|
||
|
|
#! @returns The size rounded.
|
||
|
|
pub def round_up(size: u64, align: u64): u64 {
|
||
|
|
return (size + align - 1) / align * align;
|
||
|
|
}
|