678 lines
30 KiB
Plaintext
678 lines
30 KiB
Plaintext
#! @file code.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the intermediate code of a function: values each assigned once,
|
|
#! the instructions giving them, the blocks holding the instructions, and the
|
|
#! cells of the frame that what is too large to be a value, or whose address is
|
|
#! taken, lives in. Where control flow joins, values arrive as the parameters
|
|
#! of a block, which each jump into it gives arguments for.
|
|
#!
|
|
#! A value is a word: an integer, which every integer type, `bool`, enum, and
|
|
#! pointer is held as, widened as a frame holds it, or a float, which an `f32`
|
|
#! is held as rounded to its precision. A reference is one value, its address,
|
|
#! and a reference to a trait or a function two. Structs,
|
|
#! unions, and fixed arrays live in cells or on the heap, and are read and
|
|
#! written through their addresses. An executable also has vectors, 16 or 32
|
|
#! bytes of lanes, which a cell holding a `simd` becomes where promoted.
|
|
|
|
module sakura::compiler;
|
|
|
|
|
|
#! The type of a value.
|
|
pub enum vt : u8 {
|
|
none, #! No value.
|
|
word, #! A 64-bit integer, which every integer type, `bool`, enum, and pointer is held as.
|
|
real, #! A 64-bit float, which an `f32` is held as.
|
|
v128, #! 16 bytes of lanes, which only an executable holds.
|
|
v256, #! 32 bytes of lanes, which only code for processors with AVX holds.
|
|
v512, #! 64 bytes of lanes, which only an executable holds, in memory, working them with AVX-512 where the processor has it.
|
|
count, #! How many types there are, which sizes the tables of them; not a type itself.
|
|
}
|
|
|
|
#! What an instruction does. Each says what its operands are, what its `aux`
|
|
#! and `imm` hold, and what it gives. Operators of integers take and give words,
|
|
#! and those of floats reals, but for comparisons, which give a word of 0 or 1.
|
|
pub enum op : u8 {
|
|
# Constants.
|
|
iconst, #! A word: `imm` is its bits.
|
|
fconst, #! A real: `imm` is its bits.
|
|
|
|
# Operators of two words, giving a word.
|
|
add, #! `a + b`, wrapping at 64 bits.
|
|
sub, #! `a - b`.
|
|
mul, #! `a * b`.
|
|
divs, #! `a / b` of signed words, stopping where `b` is zero.
|
|
divu, #! `a / b` of unsigned words, stopping where `b` is zero.
|
|
rems, #! What is left dividing signed words, stopping where `b` is zero.
|
|
remu, #! What is left dividing unsigned words, stopping where `b` is zero.
|
|
band, #! `a & b`.
|
|
bor, #! `a | b`.
|
|
bxor, #! `a ^ b`.
|
|
shl, #! `a << b`.
|
|
shrs, #! `a >> b`, keeping the sign.
|
|
shru, #! `a >> b`, filling with zeros.
|
|
eq, #! `a == b`.
|
|
ne, #! `a != b`.
|
|
lts, #! `a < b`, signed.
|
|
ltu, #! `a < b`, unsigned.
|
|
les, #! `a <= b`, signed.
|
|
leu, #! `a <= b`, unsigned.
|
|
eqsu, #! `a == b`, where `a` is signed and `b` unsigned, compared as the numbers they are.
|
|
ltsu, #! `a < b`, where `a` is signed and `b` unsigned.
|
|
lesu, #! `a <= b`, where `a` is signed and `b` unsigned.
|
|
ltus, #! `a < b`, where `a` is unsigned and `b` signed.
|
|
leus, #! `a <= b`, where `a` is unsigned and `b` signed.
|
|
|
|
# Operators of two reals.
|
|
fadd, #! `a + b`, giving a real.
|
|
fsub, #! `a - b`, giving a real.
|
|
fmul, #! `a * b`, giving a real.
|
|
fdiv, #! `a / b`, giving a real.
|
|
frem, #! What is left dividing, whose sign is the dividend's, giving a real.
|
|
feq, #! `a == b`, giving a word.
|
|
fne, #! `a != b`, giving a word.
|
|
flt, #! `a < b`, giving a word.
|
|
fle, #! `a <= b`, giving a word.
|
|
|
|
# Operators of one value.
|
|
neg, #! `-a` of a word.
|
|
bnot, #! `~a` of a word.
|
|
lnot, #! 1 where the word is zero, and 0 otherwise.
|
|
fneg, #! `-a` of a real.
|
|
itof, #! The real a signed word is.
|
|
utof, #! The real an unsigned word is.
|
|
ftoi, #! The signed word a real is, toward zero, saturating.
|
|
ftou, #! The unsigned word a real is, toward zero, saturating.
|
|
fsingle, #! The real rounded to the precision of an `f32`.
|
|
fsqrt, #! The square root of a real, rounded once.
|
|
ones, #! How many bits of the word are 1.
|
|
clz, #! How many 0 bits lead the word from its top, 64 where it is zero.
|
|
ctz, #! How many 0 bits trail the word from its bottom, 64 where it is zero.
|
|
bswap, #! The word with its bytes in the reverse order.
|
|
wrap, #! The word narrowed to `aux` bits, sign-extended where `imm` is 1 and zero-extended otherwise.
|
|
|
|
# Choosing.
|
|
select, #! (condition, a, b): `a` where the word is not zero, and `b` where it is, both words or both reals.
|
|
|
|
# Addresses.
|
|
cell, #! The address of the cell `aux`.
|
|
global, #! The address `imm` bytes into the globals.
|
|
datum, #! The address `imm` bytes into the bytes of datum `aux`, which are never written.
|
|
|
|
# Memory, `imm` bytes from an address.
|
|
load, #! (address): the value there, read as access `aux` says.
|
|
store, #! (address, value): writes the value there, as access `aux` says.
|
|
copy, #! (to, from): copies `imm` bytes.
|
|
clear, #! (address): zeroes `imm` bytes.
|
|
forget, #! (address): running with its memory checked, marks `imm` bytes as never written, so reading one before writing it stops the program; elsewhere nothing.
|
|
|
|
# Memory the program holds.
|
|
allocate, #! (size): zeroed memory the program gives back itself, or with aux 1 memory not cleared.
|
|
release, #! (address): gives memory back.
|
|
resize, #! (address, size): memory holding what the address's did.
|
|
|
|
# Calls, whose results are the words the callee returns, followed, where it
|
|
# returns memory, by an operand giving the address it is written to.
|
|
call, #! (arguments...): calls function `aux`.
|
|
call_trait, #! (pointer, vtable, arguments...): calls slot `imm` of the vtable, by signature `aux`.
|
|
call_value, #! (function, environment, arguments...): calls a function value, by signature `aux`.
|
|
call_c, #! (address, arguments...): calls a C function, by signature `aux`.
|
|
c_address, #! The address of the C function `aux` names.
|
|
callback, #! The address C calls function `aux` through.
|
|
|
|
# Values the runtime works with.
|
|
equal, #! (a, b): 1 where the values at the addresses of layout `aux` are equal.
|
|
retrait, #! (vtable): the vtable map `aux` gives, as a trait reference becomes another.
|
|
check, #! (condition): stops the program with error `aux` where the word is zero.
|
|
|
|
# The system.
|
|
syscall, #! (number, a, b, c, d, e, f): what the kernel gives.
|
|
frame, #! The frame pointer, in an executable.
|
|
image, #! The image an executable was built with.
|
|
environ, #! Where the system put the variables the program started with.
|
|
signals, #! The signals that came since last asked, one bit each.
|
|
catch, #! (number): has the backend note the signal, giving 0 or the negated error.
|
|
safepoint, #! Calls function `aux` where a signal the program handles has come.
|
|
exit, #! (code): ends the program with the status, having run everything asked of it.
|
|
abort, #! Ends the program at once, reporting where it was.
|
|
fault, #! (pointer, length): ends the program at once, reporting the text and where it was.
|
|
|
|
# Formatting text, which `format` begins and `format_owned` takes.
|
|
format, #! Begins text.
|
|
format_text, #! Adds the bytes of datum `aux`.
|
|
format_value, #! (value...): adds a value of layout `aux`, its words, or the address of its memory.
|
|
format_bytes, #! (pointer, length): adds the bytes.
|
|
format_owned, #! The text made, as two words: its pointer and length, in memory the program gives back.
|
|
|
|
# Checks only a program evaluating constants makes.
|
|
ready, #! Stops unless the constant evaluation `aux` computes is evaluated.
|
|
reach, #! (pointer, count): stops unless that many elements of `imm` bytes from the pointer are memory the program holds.
|
|
same, #! (a, b): stops unless the pointers are within the same memory.
|
|
|
|
# Vector instructions, which only an executable works.
|
|
cpu, #! The instruction sets of the processor running the executable, one bit each, as it found them when it started; `imm` those known to be among them.
|
|
lanes, #! (a, b, c...): operation `aux` on lanes, of type `imm & 0xFF` and `imm >> 8` bytes in all, giving a vector, or the word of a mask's bits.
|
|
vzero, #! A vector of zero bytes.
|
|
|
|
# Atomics, which several threads, or a thread and a signal handler, read and
|
|
# write without a lock. Each touches all memory, as a call does, so no load or
|
|
# store moves across one.
|
|
atomic, #! (address, operands..., orders...): operation `aux`, as `atomic_` numbers them, of access `imm`, giving what the address held.
|
|
|
|
# Threads.
|
|
start, #! (function, environment): starts a thread running the function value, giving its number or the negated error.
|
|
current, #! The running thread's record, in an executable.
|
|
clone, #! (record): starts a thread of the system on the record's stacks, running `runtime::thread_main`, in an executable.
|
|
|
|
# Ends of blocks.
|
|
jump, #! Goes to edge `aux`.
|
|
branch, #! (condition): goes to edge `aux` where the word is not zero, and edge `aux + 1` where it is.
|
|
switch, #! (word): goes to the edge from `aux` whose case is the word, of `imm` edges, the last being the default.
|
|
ret, #! (words..., address): returns the words, or the memory at the address.
|
|
trap, #! Stops the program with error `aux`, or with the error a word it takes says.
|
|
unreachable, #! Is never reached.
|
|
|
|
count, #! How many operations there are, which sizes the tables of them; not an operation itself.
|
|
}
|
|
|
|
#! How a value is read from memory or written to it, as the program's layouts
|
|
#! give it, in the order of the C machine's `access`.
|
|
pub enum access : u8 {
|
|
i8, #! A signed byte.
|
|
u8, #! An unsigned byte.
|
|
i16, #! A signed 16-bit integer.
|
|
u16, #! An unsigned 16-bit integer.
|
|
i32, #! A signed 32-bit integer.
|
|
u32, #! An unsigned 32-bit integer.
|
|
i64, #! A 64-bit integer.
|
|
f32, #! An `f32`, read as a real.
|
|
f64, #! An `f64`.
|
|
bytes, #! No word: what is copied as bytes.
|
|
v128, #! 16 bytes of lanes, read as a vector.
|
|
v256, #! 32 bytes of lanes, read as a vector.
|
|
v512, #! 64 bytes of lanes, read as a vector.
|
|
}
|
|
|
|
#! The errors code stops a program with, numbered as the machine numbers them.
|
|
pub enum fault : u32 {
|
|
out_of_bounds = 4, #! An index past the end, or a slice outside what it slices.
|
|
null_reference = 5, #! Reaching through a null pointer.
|
|
division = 6, #! Dividing by zero.
|
|
not_held = 7, #! Reading a field of a union holding another.
|
|
unreachable = 9, #! Reaching the end of a function that returns a value.
|
|
not_member = 13, #! Converting an integer to an enum none of whose members has its value.
|
|
not_constant = 15, #! Running, while compiling, what only the program running can.
|
|
}
|
|
|
|
#! The addresses below which a program holds no memory: the first page, which no
|
|
#! system maps. The machine stops at a read or a write there as at null, and an
|
|
#! executable faults there, whether the address is null or some way past it, as
|
|
#! an element of a null pointer is, so neither compares a pointer with null
|
|
#! before reaching through it where what it reaches stays within the page.
|
|
pub const null_page: u64 = 4096;
|
|
|
|
#! An instruction.
|
|
pub struct inst {
|
|
pub var kind: op; #! What it does.
|
|
pub var count: u8; #! How many results it gives.
|
|
pub var aux: u32; #! What its kind says: a cell, a function, a layout, an edge.
|
|
pub var imm: u64; #! What its kind says: bits, an offset, a size.
|
|
pub var args: u32; #! Where its operands begin in `operands`.
|
|
pub var argc: u32; #! How many operands it takes.
|
|
pub var result: u32; #! Its first result, the others following it.
|
|
pub var block: u32; #! The block holding it.
|
|
pub var next: u32; #! The next instruction of its block, or 0.
|
|
pub var at: u64; #! Where in the source it is from: one more than the file, then the byte.
|
|
pub var site: u32; #! One more than the site of the function it was inlined from, or 0 for the function's own.
|
|
}
|
|
|
|
#! Where a function's code was put within another's, which called it there.
|
|
#! A trace names it as a frame of its own, as though it were called.
|
|
pub struct inline_site {
|
|
pub var function: u32; #! The function inlined.
|
|
pub var at: u64; #! Where it was called: one more than the file, then the byte.
|
|
pub var parent: u32; #! One more than the site the call was within, or 0 for the function's own code.
|
|
}
|
|
|
|
#! Where a value comes from: the instruction giving it, as one of its results,
|
|
#! or the block taking it, as one of its parameters.
|
|
pub struct origin {
|
|
pub var kind: vt; #! Its type.
|
|
pub var param: bool; #! Whether a block takes it.
|
|
pub var from: u32; #! The instruction or block.
|
|
pub var index: u32; #! Which of its results or parameters it is.
|
|
}
|
|
|
|
#! A block: instructions in order, the last of which ends it.
|
|
pub struct block {
|
|
pub var first: u32; #! Its first instruction, or 0.
|
|
pub var last: u32; #! Its last instruction, or 0.
|
|
pub var params: ids; #! The values it takes.
|
|
pub var preds: ids; #! The edges into it.
|
|
pub var gone: bool; #! Whether it was removed, as unreachable.
|
|
}
|
|
|
|
#! A way out of a block into another, with the arguments it gives the other's
|
|
#! parameters.
|
|
pub struct edge {
|
|
var from: u32; #! The instruction it leaves by.
|
|
pub var target: u32; #! The block it goes to.
|
|
pub var args: ids; #! The values it gives the block's parameters.
|
|
pub var case: u64; #! Of a `switch`, the word it goes on.
|
|
}
|
|
|
|
#! The kinds of cell.
|
|
pub enum ck : u8 {
|
|
temporary, #! What a statement holds while it runs.
|
|
local, #! A variable, for the scope it is declared in.
|
|
param, #! A parameter taken as memory, whose address the entry block takes.
|
|
inlined, #! A cell of a function inlined, which its code clears or copies into where it begins, so the frame does not zero it.
|
|
undefined, #! A variable declared `= undefined`, which nothing zeroes, where it is declared or where its function is inlined.
|
|
}
|
|
|
|
#! A cell of the frame.
|
|
pub struct cell {
|
|
pub var size: u32; #! How many bytes it holds.
|
|
pub var align: u32; #! What its address is a multiple of.
|
|
pub var kind: ck; #! What it holds.
|
|
var name: u32; #! The ID of the name of the variable it holds, or 0.
|
|
}
|
|
|
|
#! A variable debugging information shows: a named local or parameter, and
|
|
#! where it is. Code lowered with its locals kept in cells holds each in
|
|
#! memory, with the instructions its scope covers. Optimized code holds a local
|
|
#! of words in values instead, which the code's bindings name.
|
|
pub struct variable_info {
|
|
pub var name: u32; #! The ID of its name.
|
|
pub var at: u64; #! Where it is declared: one more than the file, then the byte.
|
|
pub var kind: u32; #! Its type.
|
|
pub var layout: u32; #! Its layout, worked out once the program's are written, so noting it makes none.
|
|
pub var cell: u32; #! One more than the cell holding it, or 0 where it is a parameter given as memory.
|
|
pub var given: u32; #! Of a parameter given as memory, one more than which of the code's parameters is its address, or 0.
|
|
pub var param: bool; #! Whether it is a parameter.
|
|
pub var boxed: bool; #! Whether it is in an object of the heap: whose address its cell holds, or its binding names.
|
|
pub var held: bool; #! Whether it is held in values, which the code's bindings name.
|
|
pub var site: u32; #! One more than the inline site it was declared in, or 0 for the function's own.
|
|
pub var first: u32; #! How many instructions the code had where its scope begins.
|
|
pub var end: u32; #! How many it had where its scope ends, or 0 where it lasts to the end.
|
|
}
|
|
|
|
#! A value a variable held in values takes: from where it was given the value
|
|
#! on, it is that word of the variable, until another value is. Where the value
|
|
#! was a constant whose instruction went, the binding keeps the constant in its
|
|
#! place, as a constant is known anywhere.
|
|
pub struct binding {
|
|
pub var variable: u32; #! The variable, among the code's.
|
|
pub var word: u32; #! Which of its words.
|
|
pub var value: u32; #! The value, or 0 where it keeps a constant.
|
|
pub var after: u32; #! The instruction it was given the value after, or 0 where it was where its block begins.
|
|
pub var block: u32; #! The block it was given the value in, or `no_block` where that went.
|
|
pub var known: vt; #! The type of the constant it keeps, or `none` where it names a value.
|
|
pub var bits: u64; #! The constant's bits.
|
|
}
|
|
|
|
#! The intermediate code of one function. Instruction 0 and value 0 stand for
|
|
#! none, and block 0 is the entry, whose parameters are the function's.
|
|
pub struct code {
|
|
pub var insts: vector<inst>; #! The instructions, by index.
|
|
pub var operands: vector<u32>; #! The values the instructions take, as runs `args` and `argc` give.
|
|
pub var origins: vector<origin>; #! Where each value comes from, by index.
|
|
pub var blocks: vector<block>; #! The blocks, by index.
|
|
pub var edges: vector<edge>; #! The ways between them, by index.
|
|
pub var cells: vector<cell>; #! The cells of the frame, by index.
|
|
var results: vector<vt>; #! The words it returns.
|
|
pub var returned: u32; #! The bytes of the memory it returns, or 0 where it returns words.
|
|
pub var sites: vector<inline_site>; #! Where the functions inlined into it were called, which instructions name.
|
|
pub var variables: vector<variable_info>; #! Its variables, which debugging information shows.
|
|
pub var bindings: vector<binding>; #! The values its variables held in values take.
|
|
pub var vectors: bool; #! Whether it is an executable's, which may hold 16 bytes of lanes as values.
|
|
pub var wide: bool; #! Whether it runs only where the processor has AVX, so 32 bytes of lanes may be held as values.
|
|
var kept_order: vector<u32>; #! The blocks in reverse postorder, as last worked out, where `order_kept` says it holds.
|
|
var kept_number: vector<u32>; #! Each block's place in `kept_order`, or `0xFFFFFFFF` for one the entry does not reach.
|
|
var kept_idom: vector<u32>; #! Each block's immediate dominator, as last worked out, where `idom_kept` says it holds.
|
|
var order_kept: bool; #! Whether `kept_order` and `kept_number` hold for the blocks as they are.
|
|
var idom_kept: bool; #! Whether `kept_idom` holds for the blocks as they are.
|
|
}
|
|
|
|
#! Makes code of one empty block, the entry.
|
|
#! @returns The code.
|
|
pub def code::new(): code {
|
|
var made := code {
|
|
insts = vector::<inst>::new(),
|
|
operands = vector::<u32>::new(),
|
|
origins = vector::<origin>::new(),
|
|
blocks = vector::<block>::new(),
|
|
edges = vector::<edge>::new(),
|
|
cells = vector::<cell>::new(),
|
|
results = vector::<vt>::new(),
|
|
sites = vector::<inline_site>::new(),
|
|
variables = vector::<variable_info>::new(),
|
|
bindings = vector::<binding>::new(),
|
|
kept_order = vector::<u32>::new(),
|
|
kept_number = vector::<u32>::new(),
|
|
kept_idom = vector::<u32>::new()
|
|
};
|
|
made.insts.append(inst {});
|
|
made.origins.append(origin {});
|
|
made.add_block();
|
|
return made;
|
|
}
|
|
|
|
#! Adds a block, taking nothing yet.
|
|
#! @param this The code.
|
|
#! @returns Its index.
|
|
pub def code::add_block(&mut this): u32 {
|
|
this.blocks.append(block {});
|
|
return (this.blocks.length - 1) as u32;
|
|
}
|
|
|
|
#! Adds a parameter to a block.
|
|
#! @param this The code.
|
|
#! @param at The block.
|
|
#! @param kind The parameter's type.
|
|
#! @returns The value it is.
|
|
pub def code::add_param(&mut this, at: u32, kind: vt): u32 {
|
|
val value := this.origins.length as u32;
|
|
val index := this.blocks[at].params.length as u32;
|
|
this.origins.append(origin { kind = kind, param = true, from = at, index = index });
|
|
this.blocks[at].params.append(value);
|
|
return value;
|
|
}
|
|
|
|
#! Adds a cell to the frame.
|
|
#! @param this The code.
|
|
#! @param size How many bytes it holds.
|
|
#! @param align What its address is a multiple of.
|
|
#! @param kind What it holds.
|
|
#! @param name The ID of its variable's name, or 0.
|
|
#! @returns Its index.
|
|
pub def code::add_cell(&mut this, size: u32, align: u32, kind: ck, name: u32): u32 {
|
|
this.cells.append(cell { size = size, align = align == 0 ? 1 : align, kind = kind, name = name });
|
|
return (this.cells.length - 1) as u32;
|
|
}
|
|
|
|
#! Appends an instruction to the end of a block, with its operands and the
|
|
#! values it gives.
|
|
#! @param this The code.
|
|
#! @param at The block.
|
|
#! @param kind What it does.
|
|
#! @param args Its operands.
|
|
#! @param gives The types of its results, in order.
|
|
#! @param aux What its kind says `aux` holds.
|
|
#! @param imm What its kind says `imm` holds.
|
|
#! @param pos Where in the source it is from: one more than the file, then
|
|
#! the byte.
|
|
#! @returns Its index.
|
|
pub def code::append(
|
|
&mut this,
|
|
at: u32,
|
|
kind: op,
|
|
args: span<u32>,
|
|
gives: span<vt>,
|
|
aux: u32,
|
|
imm: u64,
|
|
pos: u64
|
|
): u32 {
|
|
val index := this.insts.length as u32;
|
|
val first := this.operands.length as u32;
|
|
# Appended one by one, which is inlined, where `extend` is a call for each
|
|
# instruction.
|
|
for &each in args {
|
|
this.operands.append(each);
|
|
}
|
|
|
|
val result := this.origins.length as u32;
|
|
for i in 0..gives.length {
|
|
this.origins.append(origin { kind = gives[i], from = index, index = i as u32 });
|
|
}
|
|
|
|
this.insts.append(inst {
|
|
kind = kind,
|
|
count = gives.length as u8,
|
|
aux = aux,
|
|
imm = imm,
|
|
args = first,
|
|
argc = args.length as u32,
|
|
result = gives.length == 0 ? 0 : result,
|
|
block = at,
|
|
at = pos
|
|
});
|
|
|
|
val last: u32 = this.blocks[at].last;
|
|
if last == 0 {
|
|
this.blocks[at].first = index;
|
|
} else {
|
|
this.insts[last].next = index;
|
|
}
|
|
this.blocks[at].last = index;
|
|
return index;
|
|
}
|
|
|
|
#! Puts an instruction first in a block, as a value a variable has nowhere is
|
|
#! put first in the entry.
|
|
#! @param this The code.
|
|
#! @param at The block.
|
|
#! @param index The instruction, appended to no block yet.
|
|
pub def code::put_first(&mut this, at: u32, index: u32): void {
|
|
this.insts[index].block = at;
|
|
this.insts[index].next = this.blocks[at].first;
|
|
this.blocks[at].first = index;
|
|
if this.blocks[at].last == 0 {
|
|
this.blocks[at].last = index;
|
|
}
|
|
}
|
|
|
|
#! Adds an edge from an instruction ending a block into another block, without
|
|
#! arguments yet.
|
|
#! @param this The code.
|
|
#! @param from The instruction.
|
|
#! @param target The block it goes to.
|
|
#! @param case Of a `switch`, the word it goes on.
|
|
#! @returns Its index.
|
|
pub def code::add_edge(&mut this, from: u32, target: u32, case: u64): u32 {
|
|
val index := this.edges.length as u32;
|
|
this.edges.append(edge { from = from, target = target, case = case });
|
|
this.blocks[target].preds.append(index);
|
|
return index;
|
|
}
|
|
|
|
#! The operand of an instruction, by its position.
|
|
#! @param this The code.
|
|
#! @param index The instruction.
|
|
#! @param at The operand's position, below its `argc`.
|
|
#! @returns The value.
|
|
pub def code::arg(&this, index: u32, at: u32): u32 {
|
|
return this.operands[this.insts[index].args as u64 + at as u64];
|
|
}
|
|
|
|
#! The type of a value.
|
|
#! @param this The code.
|
|
#! @param value The value.
|
|
#! @returns Its type.
|
|
pub def code::type_of(&this, value: u32): vt {
|
|
return this.origins[value].kind;
|
|
}
|
|
|
|
#! The block an instruction ending a block leaves from: the block holding it.
|
|
#! @param this The code.
|
|
#! @param index The edge.
|
|
#! @returns The block.
|
|
pub def code::edge_source(&this, index: u32): u32 {
|
|
return this.insts[this.edges[index].from].block;
|
|
}
|
|
|
|
#! How many edges an instruction ending a block leaves by.
|
|
#! @param this The code.
|
|
#! @param index The instruction.
|
|
#! @returns How many: none for one that returns or stops.
|
|
pub def code::edge_count(&this, index: u32): u32 {
|
|
return this.insts[index].edge_count();
|
|
}
|
|
|
|
#! How many edges an instruction leaves by, which follow one another from its
|
|
#! `aux`.
|
|
#! @param this The instruction.
|
|
#! @returns How many: none for one that does not end its block, or returns or
|
|
#! stops.
|
|
pub def inst::edge_count(&this): u32 {
|
|
return match this.kind {
|
|
op::jump => 1,
|
|
op::branch => 2,
|
|
op::switch => this.imm as u32,
|
|
else => 0
|
|
};
|
|
}
|
|
|
|
#! Whether an operation ends its block.
|
|
#! @param this The operation.
|
|
#! @returns Whether it does.
|
|
pub def op::ends(&this): bool {
|
|
return (op_traits[this as u64] & op_ends) != 0;
|
|
}
|
|
|
|
#! What an operation is like, as the bits `op_pure` and the others give.
|
|
#! @param this The operation.
|
|
#! @returns Its bits.
|
|
pub def op::traits(&this): u8 {
|
|
return op_traits[this as u64];
|
|
}
|
|
|
|
#! The name of an operation, as the printed code writes it.
|
|
#! @param this The operation.
|
|
#! @returns The name.
|
|
pub def op::name(&this): strview {
|
|
return op_names[this as u64];
|
|
}
|
|
|
|
# What an operation is like, which the passes over the code respect.
|
|
pub const op_pure: u8 = 0x01; #! It only computes its result from its operands, so it may move or go.
|
|
pub const op_traps: u8 = 0x02; #! It may stop the program.
|
|
pub const op_reads: u8 = 0x04; #! It reads memory.
|
|
pub const op_write: u8 = 0x08; #! It writes memory.
|
|
pub const op_calls: u8 = 0x1E; #! It calls code, which may read, write, and stop the program.
|
|
pub const op_ends: u8 = 0x20; #! It ends its block.
|
|
|
|
#! What each operation is like, in the order `op` declares them.
|
|
const op_traits: u8[op::count as u64] = [
|
|
op_pure, op_pure, # iconst, fconst
|
|
op_pure, op_pure, op_pure, op_traps, op_traps, op_traps, op_traps, # add .. remu
|
|
op_pure, op_pure, op_pure, op_pure, op_pure, op_pure, # band .. shru
|
|
op_pure, op_pure, op_pure, op_pure, op_pure, op_pure, # eq .. leu
|
|
op_pure, op_pure, op_pure, op_pure, op_pure, # eqsu .. leus
|
|
op_pure, op_pure, op_pure, op_pure, op_pure, # fadd .. frem
|
|
op_pure, op_pure, op_pure, op_pure, # feq .. fle
|
|
op_pure, op_pure, op_pure, op_pure, # neg, bnot, lnot, fneg
|
|
op_pure, op_pure, op_pure, op_pure, op_pure, # itof .. fsingle
|
|
op_pure, op_pure, op_pure, op_pure, op_pure, op_pure, # fsqrt .. wrap
|
|
op_pure, # select
|
|
op_pure, op_pure, op_pure, # cell, global, datum
|
|
op_reads | op_traps, op_write | op_traps, # load, store
|
|
op_reads | op_write | op_traps, op_write | op_traps, # copy, clear
|
|
op_calls, # forget
|
|
op_calls, op_calls, op_calls, # allocate .. resize
|
|
op_calls, op_calls, op_calls, op_calls, # call .. call_c
|
|
op_pure, op_pure, # c_address, callback
|
|
op_reads | op_calls, op_pure, op_traps, # equal, retrait, check
|
|
op_calls, op_reads, op_pure, op_pure, op_calls, op_calls, # syscall .. catch
|
|
op_calls, op_calls, op_calls, op_calls, # safepoint, exit, abort, fault
|
|
op_calls, op_calls, op_calls | op_reads, op_calls | op_reads, op_calls, # format ..
|
|
op_traps, op_traps, op_traps, # ready, reach, same
|
|
op_pure, op_pure, op_pure, # cpu, lanes, vzero
|
|
op_calls, # atomic
|
|
op_calls, op_pure, op_calls, # start, current, clone
|
|
op_ends, op_ends, op_ends, op_ends | op_reads, op_ends | op_traps, op_ends, # jump .. unreachable
|
|
];
|
|
|
|
#! The name of each operation, in the order `op` declares them.
|
|
const op_names: strview[op::count as u64] = [
|
|
"iconst", "fconst",
|
|
"add", "sub", "mul", "divs", "divu", "rems", "remu",
|
|
"band", "bor", "bxor", "shl", "shrs", "shru",
|
|
"eq", "ne", "lts", "ltu", "les", "leu",
|
|
"eqsu", "ltsu", "lesu", "ltus", "leus",
|
|
"fadd", "fsub", "fmul", "fdiv", "frem",
|
|
"feq", "fne", "flt", "fle",
|
|
"neg", "bnot", "lnot", "fneg",
|
|
"itof", "utof", "ftoi", "ftou", "fsingle",
|
|
"fsqrt", "ones", "clz", "ctz", "bswap", "wrap",
|
|
"select",
|
|
"cell", "global", "datum",
|
|
"load", "store", "copy", "clear", "forget",
|
|
"allocate", "release", "resize",
|
|
"call", "call_trait", "call_value", "call_c",
|
|
"c_address", "callback",
|
|
"equal", "retrait", "check",
|
|
"syscall", "frame", "image", "environ", "signals", "catch",
|
|
"safepoint", "exit", "abort", "fault",
|
|
"format", "format_text", "format_value", "format_bytes", "format_owned",
|
|
"ready", "reach", "same",
|
|
"cpu", "lanes", "vzero",
|
|
"atomic",
|
|
"start", "current", "clone",
|
|
"jump", "branch", "switch", "ret", "trap", "unreachable",
|
|
];
|
|
|
|
#! The name of each access, as the printed code writes it.
|
|
const access_names: strview[13] = ["i8", "u8", "i16", "u16", "i32", "u32", "i64", "f32", "f64", "bytes", "v128", "v256", "v512"];
|
|
|
|
#! The name of an access.
|
|
#! @param this The access.
|
|
#! @returns Its name.
|
|
pub def access::name(&this): strview {
|
|
return access_names[this as u64];
|
|
}
|
|
|
|
#! The type of value an access reads and writes.
|
|
#! @param how The access.
|
|
#! @returns The type: a real for a float, a vector for lanes, and otherwise a
|
|
#! word.
|
|
pub def access::value_type(&this): vt {
|
|
return match this {
|
|
access::f32, access::f64 => vt::real,
|
|
access::v128 => vt::v128,
|
|
access::v256 => vt::v256,
|
|
access::v512 => vt::v512,
|
|
else => vt::word
|
|
};
|
|
}
|
|
|
|
#! The bytes an access reads or writes.
|
|
#! @param this The access.
|
|
#! @returns How many: 8 for `bytes`, whose size the instruction gives.
|
|
pub def access::width(&this): u64 {
|
|
return match this {
|
|
access::i8, access::u8 => 1,
|
|
access::i16, access::u16 => 2,
|
|
access::i32, access::u32, access::f32 => 4,
|
|
access::v128 => 16,
|
|
access::v256 => 32,
|
|
access::v512 => 64,
|
|
else => 8
|
|
};
|
|
}
|
|
|
|
#! Whether a type of value is a vector's.
|
|
#! @param this The type.
|
|
#! @returns Whether it is.
|
|
pub def vt::lanes(&this): bool {
|
|
return this == vt::v128 || this == vt::v256 || this == vt::v512;
|
|
}
|
|
|
|
#! The real whose bits a word holds.
|
|
#! @param bits The bits of an `f64`.
|
|
#! @returns The real.
|
|
pub def real_of(bits: u64): f64 {
|
|
var held := bits;
|
|
return ((&mut held) as *mut f64)[0];
|
|
}
|
|
|
|
#! The bits of a real, as a word holds them.
|
|
#! @param value The real.
|
|
#! @returns Its bits.
|
|
pub def bits_of_real(value: f64): u64 {
|
|
var held := value;
|
|
return ((&mut held) as *mut u64)[0];
|
|
}
|