Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,175 @@
|
||||
# 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:
|
||||
|
||||
```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, sakura.machine, sakura.native, std.io]
|
||||
```
|
||||
|
||||
Building a program as `sakura build` does:
|
||||
|
||||
```sakura
|
||||
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`, `system`, and `runtime` are the folders beside the `sakurac` executable, and `std`
|
||||
is its standard library. 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 its `build_options`, writes
|
||||
the executable and gives what it couldn't build.
|
||||
- `debug` writes the debugging information too, as `-g` asks;
|
||||
- `windows` builds for Windows rather than Linux;
|
||||
- `threads` places the values of that many functions at once;
|
||||
- `verify`, as `--verify` asks, 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.
|
||||
`f32`s are worked, compared, negated, and chosen as `f32`s where every reader rounds
|
||||
or takes them so; a choice of reals by `<` is `maxss`, `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` and `binary` beside the repository:
|
||||
|
||||
```sh
|
||||
sakurac run --lib std=../std --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`, `system`, and
|
||||
the runtime beside `sakura`, which they find as the language server does, and over the
|
||||
standard library's repository beside this one, else the `std` 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.
|
||||
@@ -0,0 +1,881 @@
|
||||
#! @file abi.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how values cross to C on x86-64, as the System V ABI passes them,
|
||||
#! and the code of calls of C and of the functions C calls. A value is split
|
||||
#! into eightbytes, each passed in an integer or an SSE register, unless it is
|
||||
#! larger than two of them, or too few registers are left for all of its
|
||||
#! eightbytes, in which case the whole value is passed on the stack. A result
|
||||
#! too large for two registers is written by C to memory whose address the
|
||||
#! caller passes as a hidden first argument, and which C gives back.
|
||||
#!
|
||||
#! A frame holds a scalar widened to a word, and an `f32` as a double, where C
|
||||
#! holds it as it is, so each crossing narrows or widens it. C is given a
|
||||
#! reference as its address alone.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! The registers C takes integers in, in order.
|
||||
pub const c_integers: reg[6] = [reg::rdi, reg::rsi, reg::rdx, reg::rcx, reg::r8, reg::r9];
|
||||
|
||||
#! The class of an eightbyte.
|
||||
pub enum abi_class : u8 { none, integer, sse }
|
||||
|
||||
#! How one value crosses.
|
||||
pub struct abi_value {
|
||||
var parts: abi_class[2]; #! The class of each eightbyte.
|
||||
var count: u64; #! Eightbytes, or 0 for `void`.
|
||||
var memory: bool; #! Whether it is passed on the stack, or returned through memory.
|
||||
var reference: bool; #! Whether it is a `&T`, of which C is given the address alone.
|
||||
}
|
||||
|
||||
#! Where an argument is placed.
|
||||
pub struct abi_place {
|
||||
var value: abi_value;
|
||||
var registers: u8[2]; #! The register of each eightbyte, numbered within its class.
|
||||
var stacked: bool; #! Whether it is on the stack.
|
||||
var word: u64; #! The word of the stack it begins at, where it is.
|
||||
}
|
||||
|
||||
#! How a call passes each argument and takes back its result.
|
||||
pub struct abi_call {
|
||||
var args: vector<abi_place>;
|
||||
var result: abi_value;
|
||||
var integers: u64; #! Integer registers taken, the address of a result in memory included.
|
||||
var floats: u64; #! SSE registers taken.
|
||||
var words: u64; #! Words of the stack taken.
|
||||
}
|
||||
|
||||
#! Marks the class of what lies at an offset in its eightbyte: one holding
|
||||
#! anything of an integer's class is passed as an integer.
|
||||
#! @param parts The classes.
|
||||
#! @param offset The offset.
|
||||
#! @param class The class of what lies there.
|
||||
def mark_part(parts: &mut abi_class[2], offset: u64, class: abi_class): void {
|
||||
val index := offset / 8;
|
||||
return if index >= 2;
|
||||
parts[index] = parts[index] == abi_class::integer || class == abi_class::integer ? abi_class::integer : abi_class::sse;
|
||||
}
|
||||
|
||||
#! Marks the classes of what a layout holds, from an offset.
|
||||
#! @param program The program.
|
||||
#! @param index The layout.
|
||||
#! @param offset Where it begins.
|
||||
#! @param parts The classes.
|
||||
#! @param depth How deeply structs nest so far.
|
||||
def mark_leaves(program: &program, index: u32, offset: u64, parts: &mut abi_class[2], depth: u32): void {
|
||||
return if depth > 64;
|
||||
val shape: layout = program.layouts[index];
|
||||
match shape.kind {
|
||||
lk::floating => mark_part(parts, offset, abi_class::sse);
|
||||
lk::fixed => {
|
||||
val element := shape.element - 1;
|
||||
val size: u64 = program.layouts[element].size;
|
||||
for i in 0..shape.count {
|
||||
mark_leaves(program, element, offset + i as u64 * size, parts, depth + 1);
|
||||
}
|
||||
}
|
||||
lk::record => {
|
||||
for f in 0..shape.count {
|
||||
val field: slot_field = program.fields[shape.fields as u64 + f as u64];
|
||||
mark_leaves(program, field.layout, offset + field.offset, parts, depth + 1);
|
||||
}
|
||||
}
|
||||
else => mark_part(parts, offset, abi_class::integer);
|
||||
}
|
||||
}
|
||||
|
||||
#! How a value of a layout crosses.
|
||||
#! @param program The program.
|
||||
#! @param index The layout.
|
||||
#! @returns How.
|
||||
pub def abi_classify(program: &program, index: u32): abi_value {
|
||||
val shape: layout = program.layouts[index];
|
||||
var value := abi_value {};
|
||||
match shape.kind {
|
||||
lk::empty => return value;
|
||||
lk::signed, lk::unsigned, lk::boolean, lk::pointer => {
|
||||
value.parts[0] = abi_class::integer;
|
||||
value.count = 1;
|
||||
return value;
|
||||
}
|
||||
lk::floating => {
|
||||
value.parts[0] = abi_class::sse;
|
||||
value.count = 1;
|
||||
return value;
|
||||
}
|
||||
lk::reference => {
|
||||
value.parts[0] = abi_class::integer;
|
||||
value.count = 1;
|
||||
value.reference = true;
|
||||
return value;
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
# A struct or an array is in memory where larger than two eightbytes, and
|
||||
# else in an eightbyte of the class of what it holds there.
|
||||
value.count = (shape.size + 7) / 8;
|
||||
value.memory = shape.size > 16;
|
||||
return value if value.memory;
|
||||
mark_leaves(program, index, 0, &mut value.parts, 0);
|
||||
for i in 0..value.count {
|
||||
if value.parts[i] == abi_class::none {
|
||||
value.parts[i] = abi_class::sse;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
#! Places the arguments of a call by a signature.
|
||||
#! @param program The program.
|
||||
#! @param signature The signature.
|
||||
#! @returns How the call passes them.
|
||||
pub def abi_place_call(program: &program, signature: u32): abi_call {
|
||||
val held := &program.signatures[signature];
|
||||
var call := abi_call { args = vector::<abi_place>::new(scratch()) };
|
||||
call.result = abi_classify(program, held.result);
|
||||
call.integers = call.result.memory ? 1 : 0;
|
||||
for &each in held.params {
|
||||
var place := abi_place { value = abi_classify(program, each) };
|
||||
var integers: u64 = 0;
|
||||
var floats: u64 = 0;
|
||||
if !place.value.memory {
|
||||
for k in 0..place.value.count {
|
||||
if place.value.parts[k] == abi_class::integer {
|
||||
integers++;
|
||||
} else {
|
||||
floats++;
|
||||
}
|
||||
}
|
||||
}
|
||||
# A value is passed in registers only where all of its eightbytes fit.
|
||||
if !place.value.memory && call.integers + integers <= 6 && call.floats + floats <= 8 {
|
||||
for k in 0..place.value.count {
|
||||
if place.value.parts[k] == abi_class::integer {
|
||||
place.registers[k] = call.integers as u8;
|
||||
call.integers++;
|
||||
} else {
|
||||
place.registers[k] = call.floats as u8;
|
||||
call.floats++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
place.stacked = true;
|
||||
place.word = call.words;
|
||||
call.words += place.value.count;
|
||||
}
|
||||
call.args.append(place);
|
||||
}
|
||||
return call;
|
||||
}
|
||||
|
||||
#! How many words a frame takes for a value of a layout passed to a function
|
||||
#! of the program: the address of one held in memory, or its words.
|
||||
#! @param program The program.
|
||||
#! @param index The layout.
|
||||
#! @returns How many.
|
||||
def frame_words(program: &program, index: u32): u64 {
|
||||
val shape: layout = program.layouts[index];
|
||||
return argument_words(&shape);
|
||||
}
|
||||
|
||||
|
||||
# Calls of C.
|
||||
|
||||
#! Loads an eightbyte of an argument into `rax`, as C is given it: the word a
|
||||
#! frame holds, narrowed from a double where it is an `f32`, or the eightbyte
|
||||
#! of what is held in memory at the address the word is, read through `r10`
|
||||
#! without reading past it.
|
||||
#! @param this The selector.
|
||||
#! @param index The argument's layout.
|
||||
#! @param operand Where the argument's word was kept on the stack.
|
||||
#! @param e The eightbyte.
|
||||
def selector::c_eightbyte(&mut this, index: u32, operand: mem, e: u64): void {
|
||||
val shape: layout = this.b.c.program.layouts[index];
|
||||
if !in_memory(&shape) {
|
||||
this.b.text.load(8, false, reg::rax, operand);
|
||||
# Converted in a register C takes no argument in.
|
||||
if shape.kind == lk::floating && shape.size == 4 {
|
||||
this.b.text.movq_to_xmm(15, reg::rax);
|
||||
this.b.text.cvtsd2ss(15, 15);
|
||||
this.b.text.movq_from_xmm(reg::rax, 15);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.b.text.load(8, false, reg::r10, operand);
|
||||
val left := shape.size - 8 * e;
|
||||
load_bytes(&mut this.b.text, reg::rax, at(reg::r10, (8 * e) as i32), left < 8 ? left : 8, reg::r11);
|
||||
}
|
||||
|
||||
#! Loads up to eight bytes into a register, as many as there are, without
|
||||
#! reading past them, the bytes past them zero.
|
||||
#! @param text The code.
|
||||
#! @param into The register.
|
||||
#! @param from The memory.
|
||||
#! @param count How many bytes.
|
||||
#! @param temp A register to put the pieces together in.
|
||||
def load_bytes(text: &mut assembly, into: reg, from: mem, count: u64, temp: reg): void {
|
||||
if count >= 8 {
|
||||
text.load(8, false, into, from);
|
||||
return;
|
||||
}
|
||||
text.mov_imm(into, 0);
|
||||
var done: u64 = 0;
|
||||
while done < count {
|
||||
val left := count - done;
|
||||
val width: u64 = left >= 4 ? 4 : left >= 2 ? 2 : 1;
|
||||
text.load(width, false, temp, offset_by(from, done));
|
||||
if done != 0 {
|
||||
text.shift_imm(shift::shl, temp, (8 * done) as u8);
|
||||
}
|
||||
text.alu(alu::bor, into, temp);
|
||||
done += width;
|
||||
}
|
||||
}
|
||||
|
||||
#! Stores up to eight bytes of a register to memory, as many as the value
|
||||
#! there holds, without writing past them; the register is shifted away.
|
||||
#! @param text The code.
|
||||
#! @param to The memory.
|
||||
#! @param from The register.
|
||||
#! @param count How many bytes.
|
||||
def store_bytes(text: &mut assembly, to: mem, from: reg, count: u64): void {
|
||||
if count >= 8 {
|
||||
text.store(8, to, from);
|
||||
return;
|
||||
}
|
||||
var done: u64 = 0;
|
||||
while done < count {
|
||||
val left := count - done;
|
||||
val width: u64 = left >= 4 ? 4 : left >= 2 ? 2 : 1;
|
||||
text.store(width, offset_by(to, done), from);
|
||||
if done + width < count {
|
||||
text.shift_imm(shift::shr, from, (8 * width) as u8);
|
||||
}
|
||||
done += width;
|
||||
}
|
||||
}
|
||||
|
||||
#! Widens a scalar C gave in `rax` as a frame holds it: an integer extended
|
||||
#! from its bits, which C leaves unspecified past them, and an `f32` widened to
|
||||
#! a double.
|
||||
#! @param text The code.
|
||||
#! @param program The program.
|
||||
#! @param index The layout.
|
||||
def widen_c(text: &mut assembly, program: &program, index: u32): void {
|
||||
val shape: layout = program.layouts[index];
|
||||
if shape.kind == lk::floating {
|
||||
if shape.size == 4 {
|
||||
text.movq_to_xmm(0, reg::rax);
|
||||
text.cvtss2sd(0, 0);
|
||||
text.movq_from_xmm(reg::rax, 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if shape.kind == lk::boolean {
|
||||
text.extend(1, false, reg::rax, reg::rax);
|
||||
return;
|
||||
}
|
||||
return if shape.size >= 8 || shape.kind == lk::pointer || shape.kind == lk::reference;
|
||||
text.extend(shape.size, shape.kind == lk::signed, reg::rax, reg::rax);
|
||||
}
|
||||
|
||||
#! The code of a call of a C function through its address. Every operand is
|
||||
#! first written to the stack, so placing the arguments in C's registers
|
||||
#! overwrites none still to be read; then the arguments C takes on the stack
|
||||
#! and in SSE registers are placed, and those it takes in integer registers
|
||||
#! last, each read through `r10` and `r11`, which C takes none in.
|
||||
#! @param this The selector.
|
||||
#! @param index The call.
|
||||
#! @param held The call.
|
||||
def selector::call_c(&mut this, index: u32, held: &inst): void {
|
||||
if this.b.options.windows {
|
||||
this.call_microsoft(index, held);
|
||||
return;
|
||||
}
|
||||
val program := &this.b.c.program;
|
||||
val call := abi_place_call(program, held.aux);
|
||||
val signature := &program.signatures[held.aux];
|
||||
val result_shape: layout = program.layouts[signature.result];
|
||||
|
||||
# The operands, at the stack's bottom past the words C takes there.
|
||||
val operands := held.argc as u64;
|
||||
val stack_words := call.words;
|
||||
val kept := 8 * stack_words;
|
||||
val area := round_up(8 * (stack_words + operands), 16);
|
||||
this.b.text.alu_imm(alu::sub, reg::rsp, area as i32);
|
||||
for a in 0..operands {
|
||||
val from := this.read(this.body.arg(index, a as u32), index);
|
||||
this.move_one(&transfer { from = from, to = loc { kind = place::out, value = (stack_words + a) as u32 } });
|
||||
}
|
||||
|
||||
for pass in 0..2u {
|
||||
var k: u64 = 1;
|
||||
for i in 0..call.args.length {
|
||||
val placed: abi_place = call.args[i];
|
||||
val param: u32 = signature.params[i];
|
||||
val operand := at(reg::rsp, (kept + 8 * k) as i32);
|
||||
for e in 0..placed.value.count {
|
||||
val integer := !placed.stacked && placed.value.parts[e] == abi_class::integer;
|
||||
continue if integer != (pass == 1);
|
||||
this.c_eightbyte(param, operand, e);
|
||||
if placed.stacked {
|
||||
this.b.text.store(8, at(reg::rsp, (8 * (placed.word + e)) as i32), reg::rax);
|
||||
} else if integer {
|
||||
this.b.text.mov(c_integers[placed.registers[e] as u64], reg::rax);
|
||||
} else {
|
||||
this.b.text.movq_to_xmm(placed.registers[e], reg::rax);
|
||||
}
|
||||
}
|
||||
k += frame_words(program, param);
|
||||
}
|
||||
}
|
||||
|
||||
this.c_function(kept, index);
|
||||
val returned := this.mark_c_call();
|
||||
val last := kept + 8 * (operands - 1);
|
||||
if call.result.memory {
|
||||
this.b.text.load(8, false, reg::rdi, at(reg::rsp, last as i32));
|
||||
}
|
||||
this.b.text.mov_imm(reg::rax, call.floats);
|
||||
this.call_c_function(&call, returned, &result_shape, last);
|
||||
this.b.text.alu_imm(alu::add, reg::rsp, area as i32);
|
||||
this.take_c_result(&call, held);
|
||||
}
|
||||
|
||||
#! Loads the C function a call calls into `r11`, from its first operand, with a
|
||||
#! trap where it is null.
|
||||
#! @param this The selector.
|
||||
#! @param first Where the first operand is above `rsp`.
|
||||
#! @param index The call.
|
||||
def selector::c_function(&mut this, first: u64, index: u32): void {
|
||||
this.b.text.load(8, false, reg::r11, at(reg::rsp, first as i32));
|
||||
this.b.text.test(reg::r11, reg::r11);
|
||||
this.trap_if(cc::equal, fault::null_reference as u32, index);
|
||||
}
|
||||
|
||||
#! Marks where the program calls C, for a function C calls back to find, to
|
||||
#! walk a trace back through: the frame, and the address the call returns to,
|
||||
#! which `call_c_function` writes once the call is.
|
||||
#! @param this The selector.
|
||||
#! @returns Where the address the call returns to is in the text.
|
||||
def selector::mark_c_call(&mut this): u64 {
|
||||
val c_frame := this.b.own_word(state_c_frame);
|
||||
this.b.text.store(8, c_frame, reg::rbp);
|
||||
this.b.text.rex(true, 0, reg::none, reg::rax, false);
|
||||
this.b.text.byte(0xB8);
|
||||
val returned := this.b.text.length;
|
||||
this.addresses.append(returned);
|
||||
this.b.text.u64(0);
|
||||
val c_return := this.b.own_word(state_c_return);
|
||||
this.b.text.store(8, c_return, reg::rax);
|
||||
return returned;
|
||||
}
|
||||
|
||||
#! Calls the C function in `r11`, where `mark_c_call` marked the call, after
|
||||
#! which the program is no longer calling C. A result held in memory C gives in
|
||||
#! registers is written where the call asked, whose address its last operand
|
||||
#! kept; one C writes itself is there already.
|
||||
#! @param this The selector.
|
||||
#! @param call How the call passes its arguments and takes its result.
|
||||
#! @param returned Where the address the call returns to is in the text.
|
||||
#! @param result_shape The result's layout.
|
||||
#! @param last Where the last operand is above `rsp`.
|
||||
def selector::call_c_function(&mut this, call: &abi_call, returned: u64, result_shape: &layout, last: u64): void {
|
||||
this.b.text.call_reg(reg::r11);
|
||||
this.b.text.set_u64(returned, this.b.text_base + this.b.text.length);
|
||||
val c_frame_clear := this.b.own_word(state_c_frame);
|
||||
this.b.text.store_imm(c_frame_clear, 0);
|
||||
return if !returned_in_memory(result_shape) || call.result.memory;
|
||||
|
||||
this.b.text.load(8, false, reg::r10, at(reg::rsp, last as i32));
|
||||
var integers: u64 = 0;
|
||||
var floats: u64 = 0;
|
||||
for e in 0..call.result.count {
|
||||
val left := result_shape.size - 8 * e;
|
||||
var from := reg::r11;
|
||||
if call.result.parts[e] == abi_class::sse {
|
||||
this.b.text.movq_from_xmm(reg::r11, floats as u8);
|
||||
floats++;
|
||||
} else {
|
||||
from = integers == 0 ? reg::rax : reg::rdx;
|
||||
integers++;
|
||||
}
|
||||
store_bytes(&mut this.b.text, at(reg::r10, (8 * e) as i32), from, left < 8 ? left : 8);
|
||||
}
|
||||
}
|
||||
|
||||
#! Takes a scalar result C gave in `rax` or `xmm0` as the frame holds it, once
|
||||
#! the stack is as it was before the call.
|
||||
#! @param this The selector.
|
||||
#! @param call How the call passes its arguments and takes its result.
|
||||
#! @param held The call.
|
||||
def selector::take_c_result(&mut this, call: &abi_call, held: &inst): void {
|
||||
val program := &this.b.c.program;
|
||||
val result: u32 = program.signatures[held.aux].result;
|
||||
val result_shape: layout = program.layouts[result];
|
||||
return if returned_in_memory(&result_shape) || call.result.count == 0;
|
||||
if call.result.parts[0] == abi_class::sse {
|
||||
this.b.text.movq_from_xmm(reg::rax, 0);
|
||||
}
|
||||
widen_c(&mut this.b.text, program, result);
|
||||
this.results(held);
|
||||
}
|
||||
|
||||
|
||||
# Functions C calls.
|
||||
|
||||
#! How C passes arguments and what it keeps, as far as the code C calls a
|
||||
#! function of the program through differs by the ABI: System V's, or
|
||||
#! Microsoft's on Windows.
|
||||
struct c_convention {
|
||||
var microsoft: bool; #! Whether it is Microsoft's ABI.
|
||||
var kept: reg[7]; #! The registers C keeps that the program does not, in the order they are pushed.
|
||||
var keeps: u64; #! How many of them there are.
|
||||
var integers: u64; #! How many integer registers C passes arguments in.
|
||||
var floats: u64; #! How many SSE registers C passes arguments in.
|
||||
var home: u64; #! The bytes a caller leaves past the address C returns to, before the arguments on the stack.
|
||||
}
|
||||
|
||||
#! The convention C calls by on a target.
|
||||
#! @param windows Whether the target is Windows.
|
||||
#! @returns The convention.
|
||||
def c_convention_of(windows: bool): c_convention {
|
||||
if windows {
|
||||
return c_convention {
|
||||
microsoft = true,
|
||||
kept = [reg::rbx, reg::rsi, reg::rdi, reg::r12, reg::r13, reg::r14, reg::r15],
|
||||
keeps = 7,
|
||||
integers = 4,
|
||||
floats = 4,
|
||||
home = 32
|
||||
};
|
||||
}
|
||||
return c_convention {
|
||||
microsoft = false,
|
||||
kept = [reg::rbx, reg::r12, reg::r13, reg::r14, reg::r15, reg::none, reg::none],
|
||||
keeps = 5,
|
||||
integers = 6,
|
||||
floats = 8,
|
||||
home = 0
|
||||
};
|
||||
}
|
||||
|
||||
#! Places the arguments of a call by a signature as the convention does.
|
||||
#! @param this The convention.
|
||||
#! @param program The program.
|
||||
#! @param signature The signature.
|
||||
#! @returns How the call passes them.
|
||||
def c_convention::place(&this, program: &program, signature: u32): abi_call {
|
||||
if this.microsoft {
|
||||
return abi_place_microsoft(program, signature);
|
||||
}
|
||||
return abi_place_call(program, signature);
|
||||
}
|
||||
|
||||
#! Writes the code C calls a function of the program through: it keeps what C
|
||||
#! keeps, `rsi` and `rdi` among them on Windows, takes each argument as the
|
||||
#! program passes it, and makes a frame that leads back to where the program
|
||||
#! called C, so a trace walks through the program alone. A function C calls
|
||||
#! while the program is not calling C stops it. On Windows, a function C calls
|
||||
#! on a thread the program never started has no record, and is reported as the
|
||||
#! machine reports it.
|
||||
#! @param this The building.
|
||||
#! @param function The function.
|
||||
#! @returns Where the code is in the text.
|
||||
pub def building::callback_entry(&mut this, function: u32): u64 {
|
||||
val conv := c_convention_of(this.options.windows);
|
||||
val program := &this.c.program;
|
||||
val signature_index: u32 = program.functions[function].signature;
|
||||
val call := conv.place(program, signature_index);
|
||||
val signature := &program.signatures[signature_index];
|
||||
val result_shape: layout = program.layouts[signature.result];
|
||||
val memory := returned_in_memory(&result_shape);
|
||||
|
||||
# The frame, from the stack's bottom: the words passed on the stack, C's
|
||||
# registers, the state kept, the frame made for traces, a buffer for each
|
||||
# parameter held in memory C passed in registers, and one for the result.
|
||||
var words: u64 = 0;
|
||||
for &each in signature.params {
|
||||
words += frame_words(program, each);
|
||||
}
|
||||
val out := words > arg_count ? words - arg_count : 0;
|
||||
val saved := 8 * out;
|
||||
val state := saved + 8 * (conv.integers + conv.floats);
|
||||
val fake := state + 16;
|
||||
val buffers := fake + 16;
|
||||
val result_buffer := buffers + 16 * signature.params.length;
|
||||
val size := round_up(result_buffer + 16 + 8, 16) - 8;
|
||||
|
||||
val start := this.text.length;
|
||||
this.text.push(reg::rbp);
|
||||
this.text.mov(reg::rbp, reg::rsp);
|
||||
for k in 0..conv.keeps {
|
||||
this.text.push(conv.kept[k]);
|
||||
}
|
||||
this.text.alu_imm(alu::sub, reg::rsp, size as i32);
|
||||
if conv.microsoft {
|
||||
# C's rcx, rdx, r8, r9, then xmm0 to xmm3.
|
||||
for k in 0..4u {
|
||||
this.text.store(8, at(reg::rsp, (saved + 8 * k) as i32), micro_positions[k]);
|
||||
this.text.movsd_store(at(reg::rsp, (saved + 32 + 8 * k) as i32), k as u8);
|
||||
}
|
||||
|
||||
# A thread the program never started has no record, so it cannot be run.
|
||||
this.own_record(reg::rax);
|
||||
this.text.test(reg::rax, reg::rax);
|
||||
this.text.patch(this.text.jcc(cc::equal), this.stranger);
|
||||
} else {
|
||||
for k in 0..6u {
|
||||
this.text.store(8, at(reg::rsp, (saved + 8 * k) as i32), c_integers[k]);
|
||||
}
|
||||
for k in 0..8u {
|
||||
this.text.movsd_store(at(reg::rsp, (saved + 48 + 8 * k) as i32), k as u8);
|
||||
}
|
||||
}
|
||||
|
||||
# C calls it only while the program is calling C.
|
||||
val got_frame := this.own_word(state_c_frame);
|
||||
this.text.load(8, false, reg::rax, got_frame);
|
||||
this.text.test(reg::rax, reg::rax);
|
||||
val calling := this.text.jcc(cc::not_equal);
|
||||
this.text.mov_imm(reg::rbp, 0);
|
||||
this.text.mov_imm(reg::rdi, 23);
|
||||
this.text.patch(this.text.call(), this.trap_stub);
|
||||
this.text.land(calling);
|
||||
this.text.store(8, at(reg::rsp, state as i32), reg::rax);
|
||||
this.text.store(8, at(reg::rsp, fake as i32), reg::rax);
|
||||
val got_return := this.own_word(state_c_return);
|
||||
this.text.load(8, false, reg::rax, got_return);
|
||||
this.text.store(8, at(reg::rsp, (state + 8) as i32), reg::rax);
|
||||
this.text.store(8, at(reg::rsp, (fake + 8) as i32), reg::rax);
|
||||
|
||||
# Each argument, as the program passes it: an aggregate as the address of its
|
||||
# bytes, and a scalar widened as a frame holds it. System V passes the bytes
|
||||
# of an aggregate on the stack or in registers, copied to a buffer; Microsoft's
|
||||
# ABI passes a large aggregate as the pointer C gave, and a small one's bytes,
|
||||
# copied to a buffer.
|
||||
var k: u64 = 0;
|
||||
for i in 0..call.args.length {
|
||||
val placed: abi_place = call.args[i];
|
||||
val param: u32 = signature.params[i];
|
||||
val shape: layout = program.layouts[param];
|
||||
if in_memory(&shape) {
|
||||
val buffer := buffers + 16 * i;
|
||||
if conv.microsoft {
|
||||
this.c_part(&conv, &placed, 0, saved, reg::rax);
|
||||
if !placed.value.memory {
|
||||
store_bytes(&mut this.text, at(reg::rsp, buffer as i32), reg::rax, shape.size);
|
||||
this.text.lea(reg::rax, at(reg::rsp, buffer as i32));
|
||||
}
|
||||
} else if placed.stacked {
|
||||
this.text.lea(reg::rax, at(reg::rbp, (16 + 8 * placed.word) as i32));
|
||||
} else {
|
||||
for e in 0..placed.value.count {
|
||||
this.c_part(&conv, &placed, e, saved, reg::rax);
|
||||
this.text.store(8, at(reg::rsp, (buffer + 8 * e) as i32), reg::rax);
|
||||
}
|
||||
this.text.lea(reg::rax, at(reg::rsp, buffer as i32));
|
||||
}
|
||||
this.pass_word(k, reg::rax);
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
this.c_part(&conv, &placed, 0, saved, reg::rax);
|
||||
if placed.value.reference {
|
||||
this.pass_word(k, reg::rax);
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
widen_c(&mut this.text, program, param);
|
||||
this.pass_word(k, reg::rax);
|
||||
k += frame_words(program, param);
|
||||
}
|
||||
if memory {
|
||||
if call.result.memory {
|
||||
this.text.load(8, false, reg::rdx, at(reg::rsp, saved as i32));
|
||||
} else {
|
||||
this.text.lea(reg::rdx, at(reg::rsp, result_buffer as i32));
|
||||
}
|
||||
}
|
||||
this.text.lea(reg::rbp, at(reg::rsp, fake as i32));
|
||||
this.call_to(function);
|
||||
|
||||
# The result, as C takes it: the pointer back for one it wrote itself, the
|
||||
# eightbytes of one held in memory it takes in registers, or a scalar, a float
|
||||
# in `xmm0`.
|
||||
if memory && call.result.memory {
|
||||
this.text.load(8, false, reg::rax, at(reg::rsp, saved as i32));
|
||||
} else if memory {
|
||||
var integers: u64 = 0;
|
||||
var floats: u64 = 0;
|
||||
for e in 0..call.result.count {
|
||||
val left := result_shape.size - 8 * e;
|
||||
val from := at(reg::rsp, (result_buffer + 8 * e) as i32);
|
||||
if call.result.parts[e] == abi_class::sse {
|
||||
load_bytes(&mut this.text, reg::r11, from, left < 8 ? left : 8, reg::r10);
|
||||
this.text.movq_to_xmm(floats as u8, reg::r11);
|
||||
floats++;
|
||||
} else {
|
||||
load_bytes(&mut this.text, integers == 0 ? reg::rax : reg::rdx, from, left < 8 ? left : 8, reg::r10);
|
||||
integers++;
|
||||
}
|
||||
}
|
||||
} else if call.result.count != 0 && call.result.parts[0] == abi_class::sse {
|
||||
this.text.movq_to_xmm(0, reg::rax);
|
||||
if result_shape.size == 4 {
|
||||
this.text.cvtsd2ss(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
# What the program was calling C with is as it was, for the program, or
|
||||
# another function C calls.
|
||||
this.text.load(8, false, reg::rcx, at(reg::rsp, state as i32));
|
||||
val back_frame := this.own_word(state_c_frame);
|
||||
this.text.store(8, back_frame, reg::rcx);
|
||||
this.text.load(8, false, reg::rcx, at(reg::rsp, (state + 8) as i32));
|
||||
val back_return := this.own_word(state_c_return);
|
||||
this.text.store(8, back_return, reg::rcx);
|
||||
this.text.alu_imm(alu::add, reg::rsp, size as i32);
|
||||
for j in 0..conv.keeps {
|
||||
this.text.pop(conv.kept[conv.keeps - 1 - j]);
|
||||
}
|
||||
this.text.pop(reg::rbp);
|
||||
this.text.ret();
|
||||
return start;
|
||||
}
|
||||
|
||||
#! Loads an eightbyte C gave of an argument into a register: from the
|
||||
#! registers kept, or the stack C passed it on, past the caller's home area.
|
||||
#! @param this The building.
|
||||
#! @param conv The convention C called by.
|
||||
#! @param placed Where C placed it.
|
||||
#! @param e The eightbyte.
|
||||
#! @param saved Where C's registers were kept.
|
||||
#! @param into The register.
|
||||
def building::c_part(&mut this, conv: &c_convention, placed: &abi_place, e: u64, saved: u64, into: reg): void {
|
||||
if placed.stacked {
|
||||
this.text.load(8, false, into, at(reg::rbp, (16 + conv.home + 8 * (placed.word + e)) as i32));
|
||||
return;
|
||||
}
|
||||
val number := placed.registers[e] as u64;
|
||||
val offset := placed.value.parts[e] == abi_class::sse ? saved + 8 * conv.integers + 8 * number : saved + 8 * number;
|
||||
this.text.load(8, false, into, at(reg::rsp, offset as i32));
|
||||
}
|
||||
|
||||
#! Passes a word as the program passes a call's words: in a register, or on
|
||||
#! the stack's bottom.
|
||||
#! @param this The building.
|
||||
#! @param k The word.
|
||||
#! @param from The register holding it.
|
||||
def building::pass_word(&mut this, k: u64, from: reg): void {
|
||||
if k < arg_count {
|
||||
this.text.mov(gprs[k], from);
|
||||
return;
|
||||
}
|
||||
this.text.store(8, at(reg::rsp, (8 * (k - arg_count)) as i32), from);
|
||||
}
|
||||
|
||||
|
||||
# Calls of C with Microsoft's ABI, on Windows.
|
||||
|
||||
#! The registers C takes the first four arguments in on Windows, by position.
|
||||
pub const micro_positions: reg[4] = [reg::rcx, reg::rdx, reg::r8, reg::r9];
|
||||
|
||||
#! How a value of a layout crosses to C on Windows, where each value is one
|
||||
#! eightbyte: a scalar, a reference, or `void` as the System V ABI has it, an
|
||||
#! aggregate of 1, 2, 4, or 8 bytes as an integer, and any other aggregate as
|
||||
#! the address of a copy, which the loader of the call makes.
|
||||
#! @param program The program.
|
||||
#! @param index The layout.
|
||||
#! @returns How.
|
||||
pub def abi_microsoft(program: &program, index: u32): abi_value {
|
||||
val shape: layout = program.layouts[index];
|
||||
if !in_memory(&shape) {
|
||||
return abi_classify(program, index);
|
||||
}
|
||||
var value := abi_value {};
|
||||
return value if shape.size == 0;
|
||||
value.parts[0] = abi_class::integer;
|
||||
value.count = 1;
|
||||
value.memory = !micro_by_value(shape.size);
|
||||
return value;
|
||||
}
|
||||
|
||||
#! Places the arguments of a call by a signature as Microsoft's ABI does: each
|
||||
#! takes a position, the first four in registers and the rest on the stack, and
|
||||
#! a result in memory takes the first position for its address.
|
||||
#! @param program The program.
|
||||
#! @param signature The signature.
|
||||
#! @returns How the call passes them.
|
||||
pub def abi_place_microsoft(program: &program, signature: u32): abi_call {
|
||||
val held := &program.signatures[signature];
|
||||
var call := abi_call { args = vector::<abi_place>::new(scratch()) };
|
||||
call.result = abi_microsoft(program, held.result);
|
||||
call.integers = call.result.memory ? 1 : 0;
|
||||
var position := call.integers;
|
||||
for &each in held.params {
|
||||
var place := abi_place { value = abi_microsoft(program, each) };
|
||||
if position < 4 {
|
||||
place.registers[0] = position as u8;
|
||||
if place.value.parts[0] == abi_class::sse {
|
||||
call.floats++;
|
||||
} else {
|
||||
call.integers++;
|
||||
}
|
||||
} else {
|
||||
place.stacked = true;
|
||||
place.word = call.words;
|
||||
call.words++;
|
||||
}
|
||||
position++;
|
||||
call.args.append(place);
|
||||
}
|
||||
return call;
|
||||
}
|
||||
|
||||
#! Copies an aggregate C is given by reference into a copy below the stack,
|
||||
#! from the address in `r10`.
|
||||
#! @param this The selector.
|
||||
#! @param at Where the copy is, below `rsp`.
|
||||
#! @param size How many bytes.
|
||||
def selector::copy_aggregate(&mut this, below: u64, size: u64): void {
|
||||
this.b.text.mov(reg::rsi, reg::r10);
|
||||
this.b.text.lea(reg::rdi, at(reg::rsp, below as i32));
|
||||
this.b.text.mov_imm(reg::rcx, size);
|
||||
this.b.text.rep_movsb();
|
||||
}
|
||||
|
||||
#! Loads the eightbyte of an argument C is given into a register: a scalar's
|
||||
#! word, narrowed from a double where it is an `f32`, or the bytes of a small
|
||||
#! aggregate, read from the address an operand holds.
|
||||
#! @param this The selector.
|
||||
#! @param param The argument's layout.
|
||||
#! @param operand Where the operand's word is below `rsp`.
|
||||
#! @param into The register, which no scratch of this is.
|
||||
def selector::micro_eightbyte(&mut this, param: u32, operand: u64, into: reg): void {
|
||||
val shape: layout = this.b.c.program.layouts[param];
|
||||
if in_memory(&shape) {
|
||||
this.b.text.load(8, false, reg::r10, at(reg::rsp, operand as i32));
|
||||
load_bytes(&mut this.b.text, into, at(reg::r10, 0), shape.size, reg::r11);
|
||||
return;
|
||||
}
|
||||
this.b.text.load(8, false, into, at(reg::rsp, operand as i32));
|
||||
if shape.kind == lk::floating && shape.size == 4 {
|
||||
this.b.text.movq_to_xmm(5, into);
|
||||
this.b.text.cvtsd2ss(5, 5);
|
||||
this.b.text.movq_from_xmm(into, 5);
|
||||
}
|
||||
}
|
||||
|
||||
#! The code of a call of a C function on Windows, with Microsoft's ABI. Every
|
||||
#! operand is first written to a scratch below the stack, so placing the
|
||||
#! arguments in C's registers overwrites none still to be read; then the
|
||||
#! aggregates C is given by reference are copied, and the arguments placed on
|
||||
#! the stack and in registers, each one eightbyte. `rsp` is kept in the
|
||||
#! frame's scratch around it.
|
||||
#! @param this The selector.
|
||||
#! @param index The call.
|
||||
#! @param held The call.
|
||||
def selector::call_microsoft(&mut this, index: u32, held: &inst): void {
|
||||
val program := &this.b.c.program;
|
||||
val call := abi_place_microsoft(program, held.aux);
|
||||
val signature := &program.signatures[held.aux];
|
||||
val result_shape: layout = program.layouts[signature.result];
|
||||
|
||||
# The home area and the stacked words at the bottom, then a copy of each
|
||||
# aggregate passed by reference, then the operands, one word each.
|
||||
val operands := held.argc as u64;
|
||||
val base_area := round_up(32 + 8 * call.words, 16);
|
||||
var copy_at := vector::<u64>::new(scratch());
|
||||
copy_at.resize(call.args.length);
|
||||
var copies: u64 = 0;
|
||||
for i in 0..call.args.length {
|
||||
continue if !call.args[i].value.memory;
|
||||
copy_at[i] = base_area + copies;
|
||||
copies += round_up(program.layouts[signature.params[i]].size, 16);
|
||||
}
|
||||
val operand_base := base_area + copies;
|
||||
val area := round_up(operand_base + 8 * operands, 16);
|
||||
|
||||
this.b.text.store(8, this.scratch_word(0), reg::rsp);
|
||||
this.b.text.alu_imm(alu::band, reg::rsp, -16);
|
||||
this.b.descend(area);
|
||||
for a in 0..operands {
|
||||
val from := this.read(this.body.arg(index, a as u32), index);
|
||||
this.move_one(&transfer { from = from, to = loc { kind = place::out, value = (operand_base / 8 + a) as u32 } });
|
||||
}
|
||||
|
||||
# The function, the first operand, and a trap where it is null.
|
||||
this.c_function(operand_base, index);
|
||||
val returned := this.mark_c_call();
|
||||
|
||||
# Copies of aggregates passed by reference, from the address each operand holds.
|
||||
var k: u64 = 1;
|
||||
for i in 0..call.args.length {
|
||||
val param: u32 = signature.params[i];
|
||||
if call.args[i].value.memory {
|
||||
this.b.text.load(8, false, reg::r10, at(reg::rsp, (operand_base + 8 * k) as i32));
|
||||
this.copy_aggregate(copy_at[i], program.layouts[param].size);
|
||||
}
|
||||
k += frame_words(program, param);
|
||||
}
|
||||
|
||||
# The words of the stack, then floats, then integers, so `rax`, which each
|
||||
# word passes through, is never an argument's register.
|
||||
k = 1;
|
||||
for i in 0..call.args.length {
|
||||
val placed: abi_place = call.args[i];
|
||||
val param: u32 = signature.params[i];
|
||||
if placed.stacked {
|
||||
if placed.value.memory {
|
||||
this.b.text.lea(reg::rax, at(reg::rsp, copy_at[i] as i32));
|
||||
} else {
|
||||
this.micro_eightbyte(param, operand_base + 8 * k, reg::rax);
|
||||
}
|
||||
this.b.text.store(8, at(reg::rsp, (32 + 8 * placed.word) as i32), reg::rax);
|
||||
}
|
||||
k += frame_words(program, param);
|
||||
}
|
||||
k = 1;
|
||||
for i in 0..call.args.length {
|
||||
val placed: abi_place = call.args[i];
|
||||
val param: u32 = signature.params[i];
|
||||
if !placed.stacked && placed.value.parts[0] == abi_class::sse {
|
||||
this.micro_eightbyte(param, operand_base + 8 * k, reg::rax);
|
||||
this.b.text.movq_to_xmm(placed.registers[0], reg::rax);
|
||||
this.b.text.mov(micro_positions[placed.registers[0] as u64], reg::rax);
|
||||
}
|
||||
k += frame_words(program, param);
|
||||
}
|
||||
k = 1;
|
||||
for i in 0..call.args.length {
|
||||
val placed: abi_place = call.args[i];
|
||||
val param: u32 = signature.params[i];
|
||||
if !placed.stacked && placed.value.parts[0] != abi_class::sse {
|
||||
val reg := micro_positions[placed.registers[0] as u64];
|
||||
if placed.value.memory {
|
||||
this.b.text.lea(reg, at(reg::rsp, copy_at[i] as i32));
|
||||
} else {
|
||||
this.micro_eightbyte(param, operand_base + 8 * k, reg);
|
||||
}
|
||||
}
|
||||
k += frame_words(program, param);
|
||||
}
|
||||
|
||||
# The hidden result pointer, where C writes a result in memory itself. A small
|
||||
# aggregate result C gives in `rax` is written where the call asked.
|
||||
val last := operand_base + 8 * (operands - 1);
|
||||
if call.result.memory {
|
||||
this.b.text.load(8, false, reg::rcx, at(reg::rsp, last as i32));
|
||||
}
|
||||
this.b.text.load(8, false, reg::r11, at(reg::rsp, operand_base as i32));
|
||||
this.call_c_function(&call, returned, &result_shape, last);
|
||||
this.b.text.load(8, false, reg::rsp, this.scratch_word(0));
|
||||
this.take_c_result(&call, held);
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
#! @file atomics.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the code of atomics on x86-64, whose ordinary reads and writes are
|
||||
#! already ordered as `acquire` and `release` ask. So a load is a `mov` at every
|
||||
#! order, a store a `mov` but of `seq_cst`, which is an `xchg`, and a fence is
|
||||
#! an `mfence` of `seq_cst` and nothing otherwise. An exchange is an `xchg`,
|
||||
#! which locks by itself, a compare-exchange a `lock cmpxchg`, adding and
|
||||
#! subtracting a `lock xadd`, and the rest a loop of `lock cmpxchg`, every one of
|
||||
#! which orders as `seq_cst` does. An order not known while compiling is tested
|
||||
#! where it matters, which costs a compare.
|
||||
#!
|
||||
#! They work in `rax`, `rcx`, and `rdx`, which hold no value between
|
||||
#! instructions: `rdx` holds an address in none of its own, `rax` what the
|
||||
#! memory held, as `cmpxchg` has it, and `rcx` what is written.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! `atomic`: an operation on the integer at an address, or a fence, in one step.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param held The instruction.
|
||||
def selector::atomic(&mut this, index: u32, held: &inst): void {
|
||||
val operation := held.aux;
|
||||
val order := this.body.arg(index, held.argc - 1);
|
||||
if operation == atomic_fence {
|
||||
val skip := this.unless_seq_cst(order, index);
|
||||
if skip != never_patched {
|
||||
this.b.text.mfence();
|
||||
this.end_unless(skip);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
val how := atomic_access(held.imm);
|
||||
val width := how.width();
|
||||
val sign := how == access::i8 || how == access::i16 || how == access::i32;
|
||||
var direct := false;
|
||||
val at := this.address(this.body.arg(index, 0), index, 0, reg::rdx, reg::rcx, &mut direct);
|
||||
if !direct {
|
||||
this.null_check(at.base, width, index);
|
||||
}
|
||||
val result := held.result;
|
||||
|
||||
if operation == atomic_load {
|
||||
val into := this.target(result, reg::rax);
|
||||
this.b.text.load(width, sign, into, at);
|
||||
this.put(result, into);
|
||||
return;
|
||||
}
|
||||
if operation == atomic_store {
|
||||
val value := this.word_reg(this.body.arg(index, 1), index, reg::rcx);
|
||||
var bits: u64 = 0;
|
||||
val known := this.constant_bits(order, &mut bits);
|
||||
if known && bits != order_seq_cst {
|
||||
this.b.text.store(width, at, value);
|
||||
return;
|
||||
}
|
||||
var plain: u64 = never_patched;
|
||||
if !known {
|
||||
this.load_into(order, index, reg::rax);
|
||||
this.b.text.alu_imm(alu::cmp, reg::rax, order_seq_cst as i32);
|
||||
plain = this.b.text.jcc(cc::not_equal);
|
||||
}
|
||||
# `xchg` writes what the memory held into the register, which may hold the
|
||||
# value past here.
|
||||
if value != reg::rcx {
|
||||
this.b.text.mov(reg::rcx, value);
|
||||
}
|
||||
this.b.text.xchg(width, at, reg::rcx);
|
||||
if !known {
|
||||
val done := this.b.text.jmp();
|
||||
this.b.text.land(plain);
|
||||
this.b.text.store(width, at, value);
|
||||
this.b.text.land(done);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if operation == atomic_exchange || operation == atomic_add || operation == atomic_sub {
|
||||
this.load_into(this.body.arg(index, 1), index, reg::rax);
|
||||
if operation == atomic_exchange {
|
||||
this.b.text.xchg(width, at, reg::rax);
|
||||
} else {
|
||||
if operation == atomic_sub {
|
||||
this.b.text.unary(unary::neg, reg::rax);
|
||||
}
|
||||
this.b.text.lock_xadd(width, at, reg::rax);
|
||||
}
|
||||
this.b.text.extend(width, sign, reg::rax, reg::rax);
|
||||
this.put(result, reg::rax);
|
||||
return;
|
||||
}
|
||||
if operation == atomic_compare_exchange {
|
||||
this.load_into(this.body.arg(index, 1), index, reg::rax);
|
||||
val desired := this.word_reg(this.body.arg(index, 2), index, reg::rcx);
|
||||
this.b.text.lock_cmpxchg(width, at, desired);
|
||||
this.b.text.extend(width, sign, reg::rax, reg::rax);
|
||||
this.put(result, reg::rax);
|
||||
return;
|
||||
}
|
||||
this.combine(index, held, at, width, sign);
|
||||
}
|
||||
|
||||
#! The operations combining what the memory holds with an operand, as a loop
|
||||
#! working out what to write from what it held, and writing it where nothing
|
||||
#! wrote it since. A `cmpxchg` failing loads only the bytes it compares into
|
||||
#! `rax`, so what it holds is widened again before the next turn.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param held The instruction.
|
||||
#! @param at The memory.
|
||||
#! @param width The bytes of the integer.
|
||||
#! @param sign Whether it is widened by its sign.
|
||||
def selector::combine(&mut this, index: u32, held: &inst, at: mem, width: u64, sign: bool): void {
|
||||
val operation := held.aux;
|
||||
# The operand stays where it is, or where it is in no register of its own, in
|
||||
# the scratch of the frame, so `rcx` is free.
|
||||
var operand := this.word_operand(this.body.arg(index, 1), index, reg::rcx);
|
||||
val extreme := operation == atomic_min || operation == atomic_max;
|
||||
if (operand.kind == 0 && operand.r == reg::rcx) || (operand.kind == 2 && extreme) {
|
||||
if operand.kind == 2 {
|
||||
this.b.text.mov_imm(reg::rcx, operand.imm as i64 as u64);
|
||||
}
|
||||
this.b.text.store(8, this.scratch_word(0), reg::rcx);
|
||||
operand = operand_of { kind = 1, m = this.scratch_word(0) };
|
||||
}
|
||||
|
||||
this.b.text.load(width, sign, reg::rax, at);
|
||||
val top := this.b.text.length;
|
||||
if extreme {
|
||||
if operand.kind == 0 {
|
||||
this.b.text.mov(reg::rcx, operand.r);
|
||||
} else {
|
||||
this.b.text.load(8, false, reg::rcx, operand.m);
|
||||
}
|
||||
this.b.text.extend(width, sign, reg::rcx, reg::rcx);
|
||||
this.b.text.alu(alu::cmp, reg::rax, reg::rcx);
|
||||
val signed := atomic_signed(held.imm);
|
||||
val keeps := operation == atomic_min ? (signed ? cc::less : cc::below) : (signed ? cc::greater : cc::above);
|
||||
this.b.text.cmov(keeps, reg::rcx, reg::rax);
|
||||
} else {
|
||||
this.b.text.mov(reg::rcx, reg::rax);
|
||||
val kind := operation == atomic_and ? alu::band : operation == atomic_or ? alu::bor : alu::bxor;
|
||||
this.apply(kind, reg::rcx, &operand);
|
||||
}
|
||||
this.b.text.lock_cmpxchg(width, at, reg::rcx);
|
||||
val done := this.b.text.jcc(cc::equal);
|
||||
this.b.text.extend(width, sign, reg::rax, reg::rax);
|
||||
val again := this.b.text.jmp();
|
||||
this.b.text.patch(again, top);
|
||||
this.b.text.land(done);
|
||||
this.put(held.result, reg::rax);
|
||||
}
|
||||
|
||||
#! What stands for no jump to patch.
|
||||
const never_patched: u64 = 0xFFFFFFFFFFFFFFFF;
|
||||
|
||||
#! Begins code working only where an order is `seq_cst`: where it is known
|
||||
#! while compiling to be another, none; where it is known to be `seq_cst`, all of
|
||||
#! it, with nothing to skip; and otherwise a test of it, jumping past the code.
|
||||
#! @param this The selector.
|
||||
#! @param order The order.
|
||||
#! @param index The instruction.
|
||||
#! @returns The jump past it to patch, 0 where there is none, or
|
||||
#! `never_patched` where the code is not written at all.
|
||||
def selector::unless_seq_cst(&mut this, order: u32, index: u32): u64 {
|
||||
var bits: u64 = 0;
|
||||
if this.constant_bits(order, &mut bits) {
|
||||
return bits == order_seq_cst ? 0 : never_patched;
|
||||
}
|
||||
this.load_into(order, index, reg::rax);
|
||||
this.b.text.alu_imm(alu::cmp, reg::rax, order_seq_cst as i32);
|
||||
return this.b.text.jcc(cc::not_equal);
|
||||
}
|
||||
|
||||
#! Ends code `unless_seq_cst` began.
|
||||
#! @param this The selector.
|
||||
#! @param skip The jump it gave.
|
||||
def selector::end_unless(&mut this, skip: u64): void {
|
||||
if skip != 0 {
|
||||
this.b.text.land(skip);
|
||||
}
|
||||
}
|
||||
+1221
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,181 @@
|
||||
#! @file check.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the checker of the register allocator, which replays what it chose
|
||||
#! of a function, symbolically: which value each register and slot holds as
|
||||
#! each move, call, and result changes them, block by block, confirming each
|
||||
#! operand is read where its value is, and that each edge leaves every value
|
||||
#! the block it goes to holds where that block expects it. The allocator and
|
||||
#! the moves it finds are where such mistakes hide, so tests and `--verify`
|
||||
#! run this on every function they build.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
import std::containers;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! What a register or slot holds as the replay goes, by place.
|
||||
struct holding {
|
||||
var gprs: u32[16]; #! The value each general register holds, or 0.
|
||||
var xmms: u32[16]; #! The value each SSE register holds, or 0.
|
||||
var slots: dict<u32, u32>; #! The value each value's slot holds, where it holds one.
|
||||
}
|
||||
|
||||
#! A copy of what is held.
|
||||
#! @param this What is held.
|
||||
#! @returns The copy.
|
||||
def holding::copied(&this): holding {
|
||||
var made := holding { gprs = this.gprs, xmms = this.xmms, slots = dict::<u32, u32>::new(scratch()) };
|
||||
for &each in this.slots {
|
||||
made.slots.set(each.key, each.value);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The value a place holds, or 0 where it holds none known.
|
||||
#! @param this What is held.
|
||||
#! @param spot The place.
|
||||
#! @returns The value.
|
||||
def holding::at(&this, spot: &loc): u32 {
|
||||
if spot.kind == place::slot {
|
||||
return this.slots.get_or(&spot.value, 0);
|
||||
}
|
||||
return match spot.kind {
|
||||
place::gpr => this.gprs[spot.number as u64],
|
||||
place::xmm => this.xmms[spot.number as u64],
|
||||
place::remade => spot.value,
|
||||
else => 0
|
||||
};
|
||||
}
|
||||
|
||||
#! Says a place holds a value.
|
||||
#! @param this What is held.
|
||||
#! @param spot The place.
|
||||
#! @param value The value.
|
||||
def holding::set(&mut this, spot: &loc, value: u32): void {
|
||||
match spot.kind {
|
||||
place::gpr => this.gprs[spot.number as u64] = value;
|
||||
place::xmm => this.xmms[spot.number as u64] = value;
|
||||
place::slot => this.slots.set(spot.value, value);
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
|
||||
#! Forgets what every register a call clobbers holds, as a call leaves them:
|
||||
#! all but the general registers a call keeps.
|
||||
#! @param this What is held.
|
||||
def holding::clobber(&mut this): void {
|
||||
for r in 0..16u {
|
||||
if !keeps(r as u8) {
|
||||
this.gprs[r] = 0;
|
||||
}
|
||||
this.xmms[r] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#! Makes moves all at once: each reads what its place held before any wrote.
|
||||
#! @param this What is held.
|
||||
#! @param moves The moves.
|
||||
def holding::parallel(&mut this, moves: span<transfer>): void {
|
||||
var values := vector::<u32>::new(scratch());
|
||||
for &each in moves {
|
||||
values.append(this.at(&each.from));
|
||||
}
|
||||
for i in 0..moves.length {
|
||||
this.set(&moves[i].to, values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#! Replays where a function's values were placed, giving what it finds wrong.
|
||||
#! @param body The code.
|
||||
#! @param p Where its values are.
|
||||
#! @returns What is wrong, one problem each.
|
||||
pub def check_placing(body: &code, p: &placing): vector<string> {
|
||||
var problems := vector::<string>::new();
|
||||
for k in 0..p.order.length {
|
||||
val block: u32 = p.order[k];
|
||||
var held := holding { slots = dict::<u32, u32>::new(scratch()) };
|
||||
# What the block holds where it begins, which each edge into it is checked
|
||||
# to leave.
|
||||
for ¶m in body.blocks[block].params {
|
||||
held.set(&p.made_at(param), param);
|
||||
}
|
||||
for &value in p.liveins.of(block) {
|
||||
held.set(&p.located(value, 4 * p.start_slots[block]), value);
|
||||
if p.slot_of[value] != never {
|
||||
held.set(&in_slot(value), value);
|
||||
}
|
||||
}
|
||||
replay_block(body, p, block, &mut held, &mut problems);
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
|
||||
#! Replays a block from what it holds where it begins, then each edge leaving
|
||||
#! it.
|
||||
#! @param body The code.
|
||||
#! @param p Where its values are.
|
||||
#! @param block The block.
|
||||
#! @param held What is held where it begins.
|
||||
#! @param problems Where what is wrong is written.
|
||||
def replay_block(body: &code, p: &placing, block: u32, held: &mut holding, problems: &mut vector<string>): void {
|
||||
var operands := vector::<u32>::new(scratch());
|
||||
var walk := body.walk(block);
|
||||
while walk.more() {
|
||||
val inst_at := walk.next();
|
||||
val instruction: inst = body.insts[inst_at];
|
||||
val slot: u32 = p.slots[inst_at];
|
||||
held.parallel(p.gap_moves.of(slot));
|
||||
continue if p.folded[inst_at];
|
||||
|
||||
# Each operand is where its value is, and what a select a comparison is
|
||||
# folded into chooses between.
|
||||
p.read_by(body, inst_at, &mut operands);
|
||||
for &value in operands {
|
||||
val spot := p.read_at(value, inst_at);
|
||||
if held.at(&spot) != value && spot.kind != place::none {
|
||||
problems.append($"b{block}: v{value}, read by i{inst_at}, is not spot it was placed");
|
||||
}
|
||||
}
|
||||
if calls(instruction.kind) {
|
||||
held.clobber();
|
||||
}
|
||||
for r in 0..instruction.count {
|
||||
val result := instruction.result + r as u32;
|
||||
held.set(&p.made_at(result), result);
|
||||
}
|
||||
}
|
||||
|
||||
val end: u32 = p.end_slots[block];
|
||||
held.parallel(p.gap_moves.of(end));
|
||||
val last: u32 = body.blocks[block].last;
|
||||
return if last == 0;
|
||||
var outs := body.edges_of(last);
|
||||
while outs.more() {
|
||||
val edge := outs.next();
|
||||
val target: u32 = body.edges[edge].target;
|
||||
continue if p.number[target] == never;
|
||||
var after := held.copied();
|
||||
after.parallel(p.edge_moves_of(edge));
|
||||
# Each parameter's place holds the argument the edge gives it.
|
||||
val params := &body.blocks[target].params;
|
||||
for i in 0..params.length {
|
||||
break if i >= body.edges[edge].args.length;
|
||||
val param: u32 = params[i];
|
||||
val arg: u32 = body.edges[edge].args[i];
|
||||
val spot := p.made_at(param);
|
||||
if spot.kind != place::none && after.at(&spot) != arg {
|
||||
problems.append($"b{block}: edge {edge} leaves v{arg}, for parameter v{param} of b{target}, out of place");
|
||||
}
|
||||
}
|
||||
for &value in p.liveins.of(target) {
|
||||
val spot := p.located(value, 4 * p.start_slots[target]);
|
||||
if after.at(&spot) != value {
|
||||
problems.append($"b{block}: edge {edge} leaves v{value}, live into b{target}, out of place");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,753 @@
|
||||
#! @file codeview.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the writer of an executable's debugging information for Windows'
|
||||
#! debuggers, as a PDB file beside it holding CodeView: the types of the
|
||||
#! variables, each function's code with its frame, its variables and where
|
||||
#! each is over which code, the calls inlined into it with their lines, the
|
||||
#! lines of its code, and the names a debugger finds each function by. It
|
||||
#! describes what the DWARF in the executable does, which other debuggers read.
|
||||
#!
|
||||
#! Every function makes its frame of `rbp` as it begins, so each variable in
|
||||
#! its frame is at an offset from `rbp`, sixteen bytes below where the DWARF
|
||||
#! names it from, the caller's frame. The executable names the file, which is
|
||||
#! named by a GUID made from what it holds once it is written.
|
||||
|
||||
import std::io;
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
import binary::elf;
|
||||
import binary::pdb;
|
||||
import binary::pe;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! How far `rbp` is below the caller's frame, which the DWARF names variables
|
||||
#! from: past the return address and the saved `rbp`.
|
||||
const rbp_below_frame: i64 = 16;
|
||||
|
||||
#! The most bytes of code one range of a variable holds.
|
||||
const range_most: u64 = 0xFFFF;
|
||||
|
||||
#! What writing the PDB file keeps.
|
||||
struct codeview_writer {
|
||||
var b: &building;
|
||||
var file: pdb::writer;
|
||||
var symbols: pdb::symbol_stream;
|
||||
var checksums: pdb::file_checksums;
|
||||
var inlinees: pdb::inlinee_lines;
|
||||
var lines: byte_writer; #! The subsections of each function's lines.
|
||||
var section: u16; #! The section holding the code, from 1.
|
||||
var directory: strview; #! The directory the build was in.
|
||||
var paths: vector<string>; #! The path of each file named, as a debugger finds it.
|
||||
var file_at: vector<u32>; #! Where each source's entry is among the checksums, or `never`.
|
||||
var ids: vector<u32>; #! The ID of each function inlined somewhere, or 0.
|
||||
var complete: vector<u32>; #! The index of each type, by key, or 0.
|
||||
var declared: vector<u32>; #! The index of each struct's declaration, by key, or 0.
|
||||
var procedure: u32; #! The type every function is given.
|
||||
}
|
||||
|
||||
#! The path of the PDB file an executable's debugging information is written
|
||||
#! to: the executable's, with `.pdb` in place of `.exe`, within the directory
|
||||
#! the build was in where it is relative.
|
||||
#! @param output The executable's path.
|
||||
#! @param directory The directory the build was in.
|
||||
#! @returns The path.
|
||||
pub def pdb_path_of(output: strview, directory: strview): string {
|
||||
var stem := output;
|
||||
if output.length >= 4 {
|
||||
val ending := output.substr(output.length - 4, 4).lower();
|
||||
if ending.span == ".exe" {
|
||||
stem = output.substr(0, output.length - 4);
|
||||
}
|
||||
}
|
||||
return $"{stem}.pdb" if is_absolute(stem);
|
||||
return $"{directory}/{stem}.pdb";
|
||||
}
|
||||
|
||||
#! Whether a path is absolute, on Linux or Windows.
|
||||
#! @param path The path.
|
||||
#! @returns Whether it is.
|
||||
def is_absolute(path: strview): bool {
|
||||
return false if path.length == 0;
|
||||
return true if path[0] == '/' as u8 || path[0] == '\\' as u8;
|
||||
return path.length > 1 && path[1] == ':' as u8;
|
||||
}
|
||||
|
||||
#! The key of the type of a word, which no layout is, of an address of nothing
|
||||
#! the program says, and of an `f32` held as a real, as DWARF's are.
|
||||
#! @param this The writer.
|
||||
#! @returns The key.
|
||||
def codeview_writer::word_key(&this): u64 {
|
||||
return this.b.c.program.layouts.length;
|
||||
}
|
||||
|
||||
#! @copydoc codeview_writer::word_key
|
||||
def codeview_writer::address_key(&this): u64 {
|
||||
return this.b.c.program.layouts.length + 1;
|
||||
}
|
||||
|
||||
#! @copydoc codeview_writer::word_key
|
||||
def codeview_writer::widened_key(&this): u64 {
|
||||
return this.b.c.program.layouts.length + 2;
|
||||
}
|
||||
|
||||
#! Where a source's entry is among the checksums, naming it the first time:
|
||||
#! its path, within the directory the build was in where it is relative.
|
||||
#! @param this The writer.
|
||||
#! @param source The source.
|
||||
#! @returns Where its entry is.
|
||||
def codeview_writer::checksum(&mut this, source: u32): u32 {
|
||||
if this.file_at[source] != never {
|
||||
return this.file_at[source];
|
||||
}
|
||||
val written := this.b.c.checker.units[source].source.path;
|
||||
var path := is_absolute(written) ? string::from(written, nothing {}) : $"{this.directory}/{written}";
|
||||
val name := this.file.names.offset_of(path.span);
|
||||
this.paths.append(move(&mut path));
|
||||
val at := this.checksums.file(name);
|
||||
this.file_at[source] = at;
|
||||
return at;
|
||||
}
|
||||
|
||||
|
||||
# Types.
|
||||
|
||||
#! The type of an integer of a size.
|
||||
#! @param size Its bytes.
|
||||
#! @param signed Whether it is signed.
|
||||
#! @returns The type.
|
||||
def integer_type(size: u64, signed: bool): u32 {
|
||||
if size == 1 { return signed ? pdb::t_int1 : pdb::t_uint1; }
|
||||
if size == 2 { return signed ? pdb::t_int2 : pdb::t_uint2; }
|
||||
if size == 4 { return signed ? pdb::t_int4 : pdb::t_uint4; }
|
||||
return signed ? pdb::t_int8 : pdb::t_uint8;
|
||||
}
|
||||
|
||||
#! The name of a layout's type, as problems write it.
|
||||
#! @param this The writer.
|
||||
#! @param shape The layout.
|
||||
#! @returns The name.
|
||||
def codeview_writer::type_name(&this, shape: &layout): string {
|
||||
if shape.type_id != 0 {
|
||||
var text := this.b.c.checker.describe(shape.type_id);
|
||||
if text.length != 0 {
|
||||
return move(&mut text);
|
||||
}
|
||||
}
|
||||
if shape.name != 0 {
|
||||
return string::from(this.b.c.checker.names.text(shape.name), nothing {});
|
||||
}
|
||||
return string::from("?", nothing {});
|
||||
}
|
||||
|
||||
#! The unique name of a layout's type, which tells it apart from another of the
|
||||
#! same name, as one generic's two instances.
|
||||
#! @param key The type's key.
|
||||
#! @returns The name.
|
||||
def unique_name(key: u64): string {
|
||||
return $"sakura@{key}";
|
||||
}
|
||||
|
||||
#! Whether a layout is written as a struct, which a pointer names by its
|
||||
#! declaration, so a struct reaching itself through one is written.
|
||||
#! @param shape The layout.
|
||||
#! @returns Whether it is.
|
||||
def written_as_struct(shape: &layout): bool {
|
||||
return shape.kind == lk::record || shape.kind == lk::variant || shape.kind == lk::trait_reference || shape.kind == lk::function;
|
||||
}
|
||||
|
||||
#! The index of a type, writing it and those it names the first time.
|
||||
#! @param this The writer.
|
||||
#! @param key The type's key: its layout, or one past the layouts or two or three.
|
||||
#! @returns The index.
|
||||
def codeview_writer::type_of(&mut this, key: u64): u32 {
|
||||
return this.complete[key] if this.complete[key] != 0;
|
||||
val made := this.make_type(key);
|
||||
this.complete[key] = made;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The index of what a pointer or reference reaches: a struct's declaration,
|
||||
#! or the type itself.
|
||||
#! @param this The writer.
|
||||
#! @param key The key of what it reaches.
|
||||
#! @returns The index.
|
||||
def codeview_writer::reached(&mut this, key: u64): u32 {
|
||||
val shape: layout = this.b.c.program.layouts[key];
|
||||
return this.type_of(key) if !written_as_struct(&shape);
|
||||
if this.declared[key] == 0 {
|
||||
val name := this.type_name(&shape);
|
||||
val unique := unique_name(key);
|
||||
this.declared[key] = this.file.types.structure(name.span, unique.span, 0, 0, 0, true);
|
||||
}
|
||||
return this.declared[key];
|
||||
}
|
||||
|
||||
#! Adds the members of a struct or union: each field but those holding nothing,
|
||||
#! at its offset.
|
||||
#! @param this The writer.
|
||||
#! @param shape The layout.
|
||||
#! @param list The members.
|
||||
def codeview_writer::fields(&mut this, shape: &layout, list: &mut pdb::field_list): void {
|
||||
for f in 0..shape.count {
|
||||
val field: slot_field = this.b.c.program.fields[shape.fields as u64 + f as u64];
|
||||
continue if field.layout == never || this.b.c.program.layouts[field.layout].kind == lk::empty;
|
||||
val type_index := this.type_of(field.layout as u64);
|
||||
list.member(type_index, field.offset, this.b.c.checker.names.text(field.name));
|
||||
}
|
||||
}
|
||||
|
||||
#! Adds a struct's record, its members given.
|
||||
#! @param this The writer.
|
||||
#! @param key The type's key.
|
||||
#! @param shape The layout.
|
||||
#! @param list The members.
|
||||
#! @returns Its index.
|
||||
def codeview_writer::structure(&mut this, key: u64, shape: &layout, list: &pdb::field_list): u32 {
|
||||
val name := this.type_name(shape);
|
||||
val unique := unique_name(key);
|
||||
val members := this.file.types.fields(list);
|
||||
return this.file.types.structure(name.span, unique.span, members, list.count, shape.size, false);
|
||||
}
|
||||
|
||||
#! Writes a type's record, and those it names.
|
||||
#! @param this The writer.
|
||||
#! @param key The type's key.
|
||||
#! @returns Its index.
|
||||
def codeview_writer::make_type(&mut this, key: u64): u32 {
|
||||
return pdb::t_uint8 if key == this.word_key();
|
||||
return pdb::t_64p | pdb::t_void if key == this.address_key();
|
||||
return pdb::t_real64 if key == this.widened_key();
|
||||
|
||||
val program := &this.b.c.program;
|
||||
val shape: layout = program.layouts[key];
|
||||
var list := pdb::field_list::new();
|
||||
match shape.kind {
|
||||
# An enum is laid out as its integer, with a field for each member whose
|
||||
# offset is the member's value.
|
||||
lk::signed, lk::unsigned => {
|
||||
val underlying := integer_type(shape.size, shape.kind == lk::signed);
|
||||
return underlying if shape.name == 0 || shape.count == 0;
|
||||
for f in 0..shape.count {
|
||||
val field: slot_field = program.fields[shape.fields as u64 + f as u64];
|
||||
list.enumerate(field.offset as i64, this.b.c.checker.names.text(field.name));
|
||||
}
|
||||
val name := this.type_name(&shape);
|
||||
val unique := unique_name(key);
|
||||
val members := this.file.types.fields(&list);
|
||||
return this.file.types.enumeration(name.span, unique.span, underlying, members, list.count, false);
|
||||
}
|
||||
lk::floating => return shape.size == 4 ? pdb::t_real32 : pdb::t_real64;
|
||||
lk::boolean => return pdb::t_bool08;
|
||||
|
||||
# A reference is the address of what it refers to.
|
||||
lk::pointer, lk::reference => {
|
||||
if shape.element == 0 || program.layouts[shape.element - 1].kind == lk::empty {
|
||||
return pdb::t_64p | pdb::t_void;
|
||||
}
|
||||
val reached := this.reached(shape.element as u64 - 1);
|
||||
return this.file.types.pointer(reached, shape.kind == lk::reference);
|
||||
}
|
||||
|
||||
# A reference to a trait is a reference, then the index of the vtable.
|
||||
lk::trait_reference => {
|
||||
list.member(pdb::t_64p | pdb::t_void, 0, "address");
|
||||
list.member(pdb::t_uint8, 8, "vtable");
|
||||
return this.structure(key, &shape, &list);
|
||||
}
|
||||
|
||||
# A function value is its function's index plus one, then its environment.
|
||||
lk::function => {
|
||||
list.member(pdb::t_uint8, 0, "function");
|
||||
list.member(pdb::t_64p | pdb::t_void, 8, "environment");
|
||||
return this.structure(key, &shape, &list);
|
||||
}
|
||||
lk::fixed => {
|
||||
val element := this.type_of(shape.element as u64 - 1);
|
||||
return this.file.types.array(element, shape.size);
|
||||
}
|
||||
lk::record => {
|
||||
this.fields(&shape, &mut list);
|
||||
return this.structure(key, &shape, &list);
|
||||
}
|
||||
|
||||
# A union laid out without a tag is its fields over one another, the empty
|
||||
# one held where the first word is null. One with a tag is the tag, then each
|
||||
# field over one another past it, as a struct holding an unnamed union is.
|
||||
lk::variant => {
|
||||
if shape.niche == 0 {
|
||||
var tags := pdb::field_list::new();
|
||||
for f in 0..shape.count {
|
||||
val field: slot_field = program.fields[shape.fields as u64 + f as u64];
|
||||
tags.enumerate(f as i64, this.b.c.checker.names.text(field.name));
|
||||
}
|
||||
val name := this.type_name(&shape);
|
||||
val tag_name := $"{name}::tag";
|
||||
val tag_unique := $"sakura@{key}.tag";
|
||||
val members := this.file.types.fields(&tags);
|
||||
val tag := this.file.types.enumeration(tag_name.span, tag_unique.span, pdb::t_uint8, members, tags.count, false);
|
||||
list.member(tag, 0, "tag");
|
||||
}
|
||||
this.fields(&shape, &mut list);
|
||||
return this.structure(key, &shape, &list);
|
||||
}
|
||||
else => {
|
||||
return this.structure(key, &shape, &list);
|
||||
}
|
||||
}
|
||||
return pdb::t_notype;
|
||||
}
|
||||
|
||||
#! Writes the definition of each struct declared only, so each declaration is
|
||||
#! found to one, and those their definitions declare in turn.
|
||||
#! @param this The writer.
|
||||
def codeview_writer::define_declared(&mut this): void {
|
||||
var again := true;
|
||||
while again {
|
||||
again = false;
|
||||
for key in 0..this.declared.length {
|
||||
if this.declared[key] != 0 && this.complete[key] == 0 {
|
||||
_ = this.type_of(key);
|
||||
again = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Variables.
|
||||
|
||||
#! The code of a function from one offset to another, in ranges of at most
|
||||
#! `range_most` bytes, each given to `each`.
|
||||
#! @param this The writer.
|
||||
#! @param index The function.
|
||||
#! @param from Where it begins, within the function's code.
|
||||
#! @param to Where it ends.
|
||||
#! @param out The ranges, added.
|
||||
def codeview_writer::ranges_of(&this, index: u32, from: u64, to: u64, out: &mut vector<pdb::code_range>): void {
|
||||
var at := from;
|
||||
while at < to {
|
||||
val length := to - at > range_most ? range_most : to - at;
|
||||
out.append(pdb::code_range { section = this.section, offset = (this.b.starts[index] + at) as u32, length = length as u16 });
|
||||
at += length;
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes a variable its frame holds: its name and type, and where it is from
|
||||
#! `rbp`, wherever its scope is, or over the code its scope covers. One of an
|
||||
#! object of the heap is the address of that object, which the frame holds.
|
||||
#! @param this The writer.
|
||||
#! @param index The function.
|
||||
#! @param each The variable.
|
||||
#! @param scoped Whether it is where its scope's code is, rather than wherever
|
||||
#! its scope is, as a parameter is.
|
||||
def codeview_writer::framed_variable(&mut this, index: u32, each: &placed_variable, scoped: bool): void {
|
||||
var type_index := this.type_of(each.layout as u64);
|
||||
if each.boxed {
|
||||
type_index = this.file.types.pointer(type_index, false);
|
||||
}
|
||||
this.symbols.local(type_index, each.param ? pdb::local_parameter : 0, this.b.c.checker.names.text(each.name));
|
||||
val offset := (rbp_below_frame + each.offset) as i32;
|
||||
if !scoped {
|
||||
this.symbols.in_frame_throughout(offset);
|
||||
return;
|
||||
}
|
||||
var ranges := vector::<pdb::code_range>::new();
|
||||
this.ranges_of(index, each.from as u64, each.to as u64, &mut ranges);
|
||||
for &over in ranges {
|
||||
this.symbols.in_frame(offset, over);
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a word of a variable held in values can be named where it is over a
|
||||
#! range: in a register, or, but for the address of an object of the heap, in
|
||||
#! the frame. A word known, as a constant, can't be.
|
||||
#! @param kind Where the word is, as a `word_spot`.
|
||||
#! @param boxed Whether the word is the address of the object the variable is in.
|
||||
#! @returns Whether it can.
|
||||
def nameable(kind: u8, boxed: bool): bool {
|
||||
return kind == word_spot::gpr as u8 || kind == word_spot::xmm as u8 || (kind == word_spot::frame as u8 && !boxed);
|
||||
}
|
||||
|
||||
#! Writes a variable held in values: its name and type, then where each of its
|
||||
#! words is over each range of code it is somewhere in, where that can be
|
||||
#! named. One named nowhere is said to be optimized out.
|
||||
#! @param this The writer.
|
||||
#! @param index The function.
|
||||
#! @param each The variable.
|
||||
def codeview_writer::listed_variable(&mut this, index: u32, each: &placed_variable): void {
|
||||
val shape: layout = this.b.c.program.layouts[each.layout];
|
||||
val widened := !each.boxed && !each.narrow && shape.kind == lk::floating && shape.size == 4;
|
||||
val type_index := this.type_of(widened ? this.widened_key() : each.layout as u64);
|
||||
val words := each.boxed ? 1u : word_count(&shape) as u64;
|
||||
|
||||
var named := false;
|
||||
for r in each.ranges..each.ranges + each.count {
|
||||
val range: located_range = this.b.debug.ranges[r as u64];
|
||||
for w in 0..words {
|
||||
named = named || nameable(range.kinds[w], each.boxed);
|
||||
}
|
||||
}
|
||||
var flags: u16 = each.param ? pdb::local_parameter : 0;
|
||||
if !named {
|
||||
flags = flags | 0x0100; # Optimized out.
|
||||
}
|
||||
this.symbols.local(type_index, flags, this.b.c.checker.names.text(each.name));
|
||||
|
||||
var ranges := vector::<pdb::code_range>::new();
|
||||
for r in each.ranges..each.ranges + each.count {
|
||||
val range: located_range = this.b.debug.ranges[r as u64];
|
||||
ranges.clear();
|
||||
this.ranges_of(index, range.from as u64, range.to as u64, &mut ranges);
|
||||
for &over in ranges {
|
||||
for w in 0..words {
|
||||
val kind := range.kinds[w];
|
||||
val spot := range.spots[w];
|
||||
continue if !nameable(kind, each.boxed);
|
||||
if words > 1 {
|
||||
# Each word of a variable of several is a part of it, where it is in
|
||||
# a register; the frame holds none of a part's.
|
||||
if kind == word_spot::gpr as u8 {
|
||||
this.symbols.part_in_register(pdb::amd64_register(spot as u8, 8), (8 * w) as u32, over);
|
||||
} else if kind == word_spot::xmm as u8 {
|
||||
this.symbols.part_in_register(pdb::amd64_xmm(spot as u8), (8 * w) as u32, over);
|
||||
}
|
||||
} else if kind == word_spot::gpr as u8 {
|
||||
if each.boxed {
|
||||
this.symbols.through_register(pdb::amd64_register(spot as u8, 8), 0, over);
|
||||
} else {
|
||||
val width := shape.size == 1 || shape.size == 2 || shape.size == 4 ? shape.size : 8;
|
||||
this.symbols.in_register(pdb::amd64_register(spot as u8, width), over);
|
||||
}
|
||||
} else if kind == word_spot::xmm as u8 {
|
||||
this.symbols.in_register(pdb::amd64_xmm(spot as u8), over);
|
||||
} else {
|
||||
this.symbols.in_frame((rbp_below_frame + spot as i64) as i32, over);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes the variables of a function's own code, or of a site inlined into it:
|
||||
#! its parameters, then its locals.
|
||||
#! @param this The writer.
|
||||
#! @param index The function.
|
||||
#! @param site One more than the site, or 0 for the function's own code.
|
||||
def codeview_writer::variables(&mut this, index: u32, site: u32): void {
|
||||
val debug := &this.b.debug;
|
||||
for v in debug.variable_first[index]..debug.variable_first[index + 1] {
|
||||
val each: placed_variable = debug.variables[v as u64];
|
||||
continue if each.site != site || this.b.c.program.layouts[each.layout].kind == lk::empty;
|
||||
if each.held {
|
||||
this.listed_variable(index, &each);
|
||||
} else if each.param || site != 0 {
|
||||
this.framed_variable(index, &each, false);
|
||||
} else if each.to > each.from {
|
||||
this.framed_variable(index, &each, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Inline sites.
|
||||
|
||||
#! The ID of a function inlined somewhere, naming where it begins among the
|
||||
#! inlinees' lines the first time.
|
||||
#! @param this The writer.
|
||||
#! @param function The function.
|
||||
#! @returns Its ID.
|
||||
def codeview_writer::inlinee(&mut this, function: u32): u32 {
|
||||
return this.ids[function] if this.ids[function] != 0;
|
||||
val inlined := &this.b.c.program.functions[function];
|
||||
val id := this.file.ids.function_id(0, this.procedure, inlined.name.span);
|
||||
var source: u32 = 0;
|
||||
val located := this.b.source_of(function, 0, &mut source);
|
||||
this.inlinees.add(id, this.checksum(source), located.line as u32);
|
||||
this.ids[function] = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
#! The site inlined directly within another that a site is within, or the site
|
||||
#! itself where it is directly within.
|
||||
#! @param sites The function's inline sites.
|
||||
#! @param site One more than the site.
|
||||
#! @param outer One more than the other, or 0 for the function's own code.
|
||||
#! @returns One more than the site directly within.
|
||||
def directly_within(sites: &vector<inline_site>, site: u32, outer: u32): u32 {
|
||||
var at := site;
|
||||
var depth: u64 = 0;
|
||||
while at != 0 && depth <= sites.length {
|
||||
val parent := sites[at as u64 - 1].parent;
|
||||
return at if parent == outer;
|
||||
at = parent;
|
||||
depth++;
|
||||
}
|
||||
return site;
|
||||
}
|
||||
|
||||
#! Whether a row of a function's line table holds code: no later one begins
|
||||
#! where it does, so it is the line of the code there, as DWARF's readers take
|
||||
#! the last row at an offset.
|
||||
#! @param this The writer.
|
||||
#! @param row The row.
|
||||
#! @param last One past the function's last row.
|
||||
#! @returns Whether it does.
|
||||
def codeview_writer::holds_code(&this, row: u64, last: u64): bool {
|
||||
return row + 1 >= last || this.b.rows[row + 1].offset != this.b.rows[row].offset;
|
||||
}
|
||||
|
||||
#! Writes the sites inlined directly within a site of a function, or within its
|
||||
#! own code, each with the annotations of the code it is: each run of its code,
|
||||
#! and the line of each, where the code of a site within it is at the line it
|
||||
#! was called at. Each holds its variables and the sites within it.
|
||||
#! @param this The writer.
|
||||
#! @param index The function.
|
||||
#! @param parent One more than the site, or 0.
|
||||
def codeview_writer::sites(&mut this, index: u32, parent: u32): void {
|
||||
val sites := &this.b.c.program.functions[index].body.sites;
|
||||
val first := this.b.row_first[index] as u64;
|
||||
val last := this.b.row_first[index + 1] as u64;
|
||||
val size := this.b.ends[index] - this.b.starts[index];
|
||||
for k in 0..sites.length {
|
||||
val site: inline_site = sites[k];
|
||||
continue if site.parent != parent;
|
||||
val number := k as u32 + 1;
|
||||
|
||||
# The rows holding code from the site's first to its last. A site with
|
||||
# none is no code's, as the DWARF says too.
|
||||
var begins := last;
|
||||
var ends := last;
|
||||
for r in first..last {
|
||||
continue if !this.holds_code(r, last) || !within(sites, this.b.rows[r].site, number);
|
||||
if begins == last {
|
||||
begins = r;
|
||||
}
|
||||
ends = r + 1;
|
||||
}
|
||||
continue if begins == last;
|
||||
|
||||
var file: u32 = 0;
|
||||
val start := this.b.source_of(site.function, 0, &mut file);
|
||||
val inlinee := this.inlinee(site.function);
|
||||
var notes := pdb::annotations::new(this.checksum(file), start.line as u32);
|
||||
for r in begins..ends {
|
||||
continue if !this.holds_code(r, last);
|
||||
val row: line_row = this.b.rows[r];
|
||||
if !within(sites, row.site, number) {
|
||||
notes.away(row.offset as u32);
|
||||
continue;
|
||||
}
|
||||
# Code of a site within this one is at the line that site was called at.
|
||||
var at := row.at;
|
||||
if row.site != number {
|
||||
at = sites[directly_within(sites, row.site, number) as u64 - 1].at;
|
||||
}
|
||||
var source: u32 = 0;
|
||||
val located := this.b.source_of(index, at, &mut source);
|
||||
notes.at_line(row.offset as u32, this.checksum(source), located.line as u32);
|
||||
}
|
||||
val end := ends < last ? this.b.rows[ends].offset : size;
|
||||
notes.finish(end as u32);
|
||||
|
||||
this.symbols.inline_site(inlinee, notes.bytes.span);
|
||||
this.variables(index, number);
|
||||
this.sites(index, number);
|
||||
this.symbols.end();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Functions.
|
||||
|
||||
#! A line of a function's code.
|
||||
struct code_line {
|
||||
var offset: u64; #! Where its code begins, within the function's.
|
||||
var file: u32; #! Its file, as its entry among the checksums.
|
||||
var line: u32;
|
||||
var column: u32;
|
||||
}
|
||||
|
||||
#! Writes the lines of a function's code: each row of its own code at its own
|
||||
#! line, and the code of each site inlined into it at the line it was called
|
||||
#! at, once for each run. Each run of lines of one file is a part of the code
|
||||
#! of its own, so a reader taking one file's lines alone finds each where the
|
||||
#! code is, as a function whose globals are from several files has.
|
||||
#! @param this The writer.
|
||||
#! @param index The function.
|
||||
def codeview_writer::function_lines(&mut this, index: u32): void {
|
||||
val sites := &this.b.c.program.functions[index].body.sites;
|
||||
val size := this.b.ends[index] - this.b.starts[index];
|
||||
var found := vector::<code_line>::new();
|
||||
val rows_end := this.b.row_first[index + 1] as u64;
|
||||
for r in this.b.row_first[index]..this.b.row_first[index + 1] {
|
||||
continue if !this.holds_code(r as u64, rows_end);
|
||||
val row: line_row = this.b.rows[r as u64];
|
||||
var at := row.at;
|
||||
if row.site != 0 {
|
||||
at = sites[directly_within(sites, row.site, 0) as u64 - 1].at;
|
||||
}
|
||||
var source: u32 = 0;
|
||||
val located := this.b.source_of(index, at, &mut source);
|
||||
val made := code_line { offset = row.offset, file = this.checksum(source), line = located.line as u32, column = located.column as u32 };
|
||||
if row.site != 0 && found.length != 0 {
|
||||
val last := found.last;
|
||||
continue if made.file == last.file && made.line == last.line && made.column == last.column;
|
||||
}
|
||||
found.append(made);
|
||||
}
|
||||
|
||||
var first: u64 = 0;
|
||||
while first < found.length {
|
||||
var end := first + 1;
|
||||
while end < found.length && found[end].file == found[first].file {
|
||||
end++;
|
||||
}
|
||||
val begins := found[first].offset;
|
||||
val ends := end < found.length ? found[end].offset : size;
|
||||
var lines := pdb::function_lines::new(this.section, (this.b.starts[index] + begins) as u32, (ends - begins) as u32);
|
||||
for k in first..end {
|
||||
val each: code_line = found[k];
|
||||
lines.line(each.file, (each.offset - begins) as u32, each.line, each.column as u16);
|
||||
}
|
||||
lines.finish();
|
||||
pdb::subsection(&mut this.lines, pdb::debug_s_lines, lines.bytes.span);
|
||||
first = end;
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes a function: its procedure, with its frame, its variables, and the
|
||||
#! sites inlined into it, the global naming it, and the lines of its code.
|
||||
#! @param this The writer.
|
||||
#! @param index The function.
|
||||
def codeview_writer::function(&mut this, index: u32): void {
|
||||
val function := &this.b.c.program.functions[index];
|
||||
val debug := &this.b.debug;
|
||||
val size := this.b.ends[index] - this.b.starts[index];
|
||||
var leaving := size;
|
||||
val returns := debug.return_first[index + 1];
|
||||
if returns > debug.return_first[index] {
|
||||
leaving = debug.returns[returns as u64 - 1] as u64;
|
||||
}
|
||||
var saved: u32 = 0;
|
||||
for &r in kept_gprs {
|
||||
if (debug.saves[index] & (1u << r as u64) as u32) != 0 {
|
||||
saved += 8;
|
||||
}
|
||||
}
|
||||
val at := this.symbols.procedure(
|
||||
true, function.name.span, this.procedure, this.section, this.b.starts[index] as u32, size as u32,
|
||||
debug.bodies[index], leaving as u32, pdb::proc_has_frame_pointer
|
||||
);
|
||||
this.symbols.frame(debug.frames[index], saved, pdb::frame_locals_rbp | pdb::frame_params_rbp);
|
||||
this.variables(index, 0);
|
||||
this.sites(index, 0);
|
||||
this.symbols.end();
|
||||
this.file.procedure(function.name.span, 1, at, true);
|
||||
this.function_lines(index);
|
||||
}
|
||||
|
||||
|
||||
# The file.
|
||||
|
||||
#! Writes the PDB file of an executable's debugging information, then stamps
|
||||
#! the executable, which names the file by its path already, with its GUID.
|
||||
#! @param b The building.
|
||||
#! @param exe The executable's file.
|
||||
#! @param path The PDB file's path.
|
||||
#! @param directory The directory the build was in.
|
||||
#! @returns Whether the file was written.
|
||||
pub def describe_pdb(b: &building, exe: &mut byte_writer, path: strview, directory: strview): bool {
|
||||
val program := &b.c.program;
|
||||
val keys := program.layouts.length + 3;
|
||||
var w := codeview_writer {
|
||||
b = b,
|
||||
file = pdb::writer::new(),
|
||||
symbols = pdb::symbol_stream::new(),
|
||||
checksums = pdb::file_checksums::new(),
|
||||
inlinees = pdb::inlinee_lines::new(),
|
||||
lines = byte_writer::new(),
|
||||
section = 0,
|
||||
directory = directory,
|
||||
paths = vector::<string>::new(),
|
||||
file_at = vector::<u32>::new(),
|
||||
ids = vector::<u32>::new(),
|
||||
complete = vector::<u32>::new(),
|
||||
declared = vector::<u32>::new(),
|
||||
procedure = 0
|
||||
};
|
||||
w.file_at.resize(b.c.checker.units.length);
|
||||
for i in 0..w.file_at.length {
|
||||
w.file_at[i] = never;
|
||||
}
|
||||
w.ids.resize(program.functions.length);
|
||||
w.complete.resize(keys);
|
||||
w.declared.resize(keys);
|
||||
|
||||
# The sections, as the executable's headers say, and the one of the code.
|
||||
w.file.sections = pe::section_headers(exe.span);
|
||||
for k in 0..w.file.sections.length / 40 {
|
||||
if w.file.sections.get_u32(40 * k + 12) as u64 == b.text_base - base_address {
|
||||
w.section = (k + 1) as u16;
|
||||
}
|
||||
}
|
||||
return false if w.section == 0;
|
||||
|
||||
# The module, compiled from the main file, its functions' type that of a
|
||||
# function taking nothing, as a debugger can't call them anyway.
|
||||
val main_file := program.functions[program.main].file;
|
||||
_ = w.checksum(main_file);
|
||||
val module_name := string::from(w.paths[0].span, nothing {});
|
||||
w.symbols.object_name(module_name.span);
|
||||
val version: u16[3] = [1, 0, 0];
|
||||
w.symbols.compiler(pdb::language_cxx, pdb::cv_machine_x64, version, "sakura");
|
||||
val arguments := w.file.types.arguments(span::<u32>::blank());
|
||||
w.procedure = w.file.types.procedure(pdb::t_void, arguments, 0);
|
||||
|
||||
for i in 0..program.functions.length {
|
||||
continue if b.starts[i] == b.missing || b.ends[i] <= b.starts[i];
|
||||
w.function(i as u32);
|
||||
}
|
||||
w.define_declared();
|
||||
|
||||
# The module's lines: each function's, then the files they name, then where
|
||||
# each function inlined begins.
|
||||
pdb::subsection(&mut w.lines, pdb::debug_s_file_checksums, w.checksums.bytes.span);
|
||||
if w.inlinees.count != 0 {
|
||||
pdb::subsection(&mut w.lines, pdb::debug_s_inlinee_lines, w.inlinees.bytes.span);
|
||||
}
|
||||
var files := vector::<strview>::new();
|
||||
for &each in w.paths {
|
||||
files.append(each.span);
|
||||
}
|
||||
val ours := w.file.add_module(module_name.span, module_name.span, &mut w.symbols.bytes, &mut w.lines, files.span);
|
||||
|
||||
# The linker's module, naming each section.
|
||||
var linker := pdb::symbol_stream::new();
|
||||
linker.object_name("* Linker *");
|
||||
linker.compiler(pdb::language_link, pdb::cv_machine_x64, version, "sakura");
|
||||
linker.sections(&w.file.sections);
|
||||
var no_lines := byte_writer::new();
|
||||
_ = w.file.add_module("* Linker *", "", &mut linker.bytes, &mut no_lines, span::<strview>::blank());
|
||||
|
||||
# Every section the system keeps once loaded is the module's.
|
||||
for k in 0..w.file.sections.length / 40 {
|
||||
val characteristics := w.file.sections.get_u32(40 * k + 36);
|
||||
continue if (characteristics & 0x02000000) != 0;
|
||||
w.file.contribute(ours, (k + 1) as u16, 0, w.file.sections.get_u32(40 * k + 8), characteristics);
|
||||
}
|
||||
|
||||
# The names a debugger finds the code by, as disassemblers show them.
|
||||
var named := vector::<elf::symbol>::new();
|
||||
b.name_code(&mut named);
|
||||
for &each in named {
|
||||
w.file.public(each.name, w.section, (each.address - b.text_base) as u32, pdb::public_function | pdb::public_code);
|
||||
}
|
||||
|
||||
val bytes := w.file.file();
|
||||
return false if !write_bytes(path, bytes.span, 0o644);
|
||||
return pe::stamp_codeview(exe, w.file.guid, w.file.signature);
|
||||
}
|
||||
+1159
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,370 @@
|
||||
#! @file evex.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how an executable works 64 bytes of lanes with AVX-512, where
|
||||
#! `lanes_needs` said the processor running it has it: AVX-512F, BW, DQ, and VL,
|
||||
#! with the system keeping the registers' upper 384 bits and the mask
|
||||
#! registers. Such a vector is held in its slot, never a register, as only code
|
||||
#! that asked the processor may touch `zmm` registers. Each operation loads its
|
||||
#! operands from their slots into `zmm16` and those after it, works them with
|
||||
#! EVEX's encodings, and stores its result in its slot; comparing lanes gives a
|
||||
#! mask in `k1`, which becomes the mask's lanes, and choosing lanes or taking a
|
||||
#! mask's bits takes them back into `k1`. VEX names no register past `zmm15`,
|
||||
#! so neither `vzeroupper` nor SSE's instructions meet those, and code working
|
||||
#! 64 bytes clears nothing after.
|
||||
#!
|
||||
#! An instruction is a word as `binary::amd64` writes vector instructions: its
|
||||
#! opcode, its map above it, its mandatory prefix above that, and `W` at bit 24.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
# Moving 64 bytes, and 16 into every quarter of a register, or into one.
|
||||
const z_movdqu64_load: u32 = 0x102016F;
|
||||
const z_movdqu64_store: u32 = 0x102017F;
|
||||
const z_broadcasti32x4: u32 = 0x01025A;
|
||||
const z_inserti32x4: u32 = 0x010338;
|
||||
# Integer arithmetic, by width: 16, 32, and 64 bits.
|
||||
const z_paddw: u32 = 0x0101FD;
|
||||
const z_paddd: u32 = 0x0101FE;
|
||||
const z_paddq: u32 = 0x10101D4;
|
||||
const z_psubw: u32 = 0x0101F9;
|
||||
const z_psubd: u32 = 0x0101FA;
|
||||
const z_psubq: u32 = 0x10101FB;
|
||||
const z_pmullw: u32 = 0x0101D5;
|
||||
const z_pmulld: u32 = 0x010240;
|
||||
const z_pmullq: u32 = 0x1010240;
|
||||
# Bitwise operations of 32-bit lanes, and of 64 with `W`; and AVX-512's of
|
||||
# three operands by a table of eight bits.
|
||||
const z_pandd: u32 = 0x0101DB;
|
||||
const z_pord: u32 = 0x0101EB;
|
||||
const z_pxord: u32 = 0x0101EF;
|
||||
const z_pternlogd: u32 = 0x010325;
|
||||
# The least, greatest, and absolute values of integers, by sign and width; `W`
|
||||
# makes the 32-bit ones' 64-bit.
|
||||
const z_pminsw: u32 = 0x0101EA;
|
||||
const z_pmaxsw: u32 = 0x0101EE;
|
||||
const z_pminuw: u32 = 0x01023A;
|
||||
const z_pmaxuw: u32 = 0x01023E;
|
||||
const z_pminsd: u32 = 0x010239;
|
||||
const z_pmaxsd: u32 = 0x01023D;
|
||||
const z_pminud: u32 = 0x01023B;
|
||||
const z_pmaxud: u32 = 0x01023F;
|
||||
const z_pabsw: u32 = 0x01021D;
|
||||
const z_pabsd: u32 = 0x01021E;
|
||||
const z_pabsq: u32 = 0x101021F;
|
||||
# Rounding reals with how they round, and comparing lanes into a mask, by a
|
||||
# predicate.
|
||||
const z_rndscaleps: u32 = 0x010308;
|
||||
const z_rndscalepd: u32 = 0x1010309;
|
||||
const z_pcmpd: u32 = 0x01031F;
|
||||
const z_pcmpud: u32 = 0x01031E;
|
||||
const z_pcmpw: u32 = 0x101033F;
|
||||
const z_pcmpuw: u32 = 0x101033E;
|
||||
const z_cmpps: u32 = 0x0001C2;
|
||||
# A mask's lanes from `k1`, and `k1` from a mask's lanes, of 16, 32, and 64-bit
|
||||
# lanes; and lanes chosen by `k1`.
|
||||
const z_pmovm2w: u32 = 0x1020228;
|
||||
const z_pmovm2d: u32 = 0x020238;
|
||||
const z_pmovm2q: u32 = 0x1020238;
|
||||
const z_pmovw2m: u32 = 0x1020229;
|
||||
const z_pmovd2m: u32 = 0x020239;
|
||||
const z_pmovq2m: u32 = 0x1020239;
|
||||
const z_pblendmw: u32 = 0x1010266;
|
||||
const z_pblendmd: u32 = 0x010264;
|
||||
const z_pblendmq: u32 = 0x1010264;
|
||||
const z_blendmps: u32 = 0x010265;
|
||||
const z_blendmpd: u32 = 0x1010265;
|
||||
# Splatting a general register's lane, or a real's low lane.
|
||||
const z_pbroadcastw_r: u32 = 0x01027B;
|
||||
const z_pbroadcastd_r: u32 = 0x01027C;
|
||||
const z_pbroadcastq_r: u32 = 0x101027C;
|
||||
const z_broadcastss: u32 = 0x010218;
|
||||
const z_broadcastsd: u32 = 0x1010219;
|
||||
# A mask register's bits into a general register, written with VEX: 16 bits,
|
||||
# and 32 with `F2`.
|
||||
const k_movw: u32 = 0x000193;
|
||||
const k_movd: u32 = 0x030193;
|
||||
|
||||
# The registers 64 bytes are worked in.
|
||||
const z16: u32 = 16;
|
||||
const z17: u32 = 17;
|
||||
const z18: u32 = 18;
|
||||
|
||||
#! Writes EVEX's four bytes and an instruction's opcode, on 64 bytes: `r` the
|
||||
#! register in ModRM's reg, `v` the one EVEX names, or 0 where the instruction
|
||||
#! names none, `b` the base or register in ModRM's rm, and `k` the mask
|
||||
#! register writing is masked by, or 0. A vector register is any of the 32, and
|
||||
#! a general one, or the base of `memory`, any of the 16.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param r ModRM's reg.
|
||||
#! @param v The register EVEX names.
|
||||
#! @param b ModRM's rm.
|
||||
#! @param k The mask register.
|
||||
#! @param memory Whether `b` is the base of memory, which names no index.
|
||||
def selector::evex_opcode(&mut this, op: u32, r: u32, v: u32, b: u32, k: u32, memory: bool): void {
|
||||
val pp := (op >> 16) & 3;
|
||||
val map := (op >> 8) & 3;
|
||||
val w: u32 = (op & vec_wide) != 0 ? 1 : 0;
|
||||
# R, X, B, and R' inverted: the fourth and fifth bits of `r` and `b`, where X
|
||||
# is the fifth of a register `b`.
|
||||
val x: u32 = memory ? 1 : ((b >> 4) & 1) ^ 1;
|
||||
this.b.text.byte(0x62);
|
||||
this.b.text.byte(((((r >> 3) & 1) ^ 1) << 7 | x << 6 | (((b >> 3) & 1) ^ 1) << 5 | (((r >> 4) & 1) ^ 1) << 4 | map) as u8);
|
||||
this.b.text.byte((w << 7 | ((~v & 15) << 3) | 1 << 2 | pp) as u8);
|
||||
# Of 512 bits, V' inverted, and the mask.
|
||||
this.b.text.byte((2 << 5 | (((v >> 4) & 1) ^ 1) << 3 | (k & 7)) as u8);
|
||||
this.b.text.byte((op & 0xFF) as u8);
|
||||
}
|
||||
|
||||
#! An instruction of registers on 64 bytes: `op dst, v, src`.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param dst ModRM's reg.
|
||||
#! @param v The register EVEX names, or 0.
|
||||
#! @param src ModRM's rm.
|
||||
#! @param k The mask register, or 0.
|
||||
def selector::evex(&mut this, op: u32, dst: u32, v: u32, src: u32, k: u32): void {
|
||||
this.evex_opcode(op, dst, v, src, k, false);
|
||||
this.b.text.byte((0xC0 | ((dst & 7) << 3) | (src & 7)) as u8);
|
||||
}
|
||||
|
||||
#! An instruction of registers on 64 bytes, with a byte after it.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param dst ModRM's reg.
|
||||
#! @param v The register EVEX names, or 0.
|
||||
#! @param src ModRM's rm.
|
||||
#! @param imm The byte.
|
||||
def selector::evex_imm(&mut this, op: u32, dst: u32, v: u32, src: u32, imm: u8): void {
|
||||
this.evex(op, dst, v, src, 0);
|
||||
this.b.text.byte(imm);
|
||||
}
|
||||
|
||||
#! An instruction of a register and memory on 64 bytes, as a load or store: the
|
||||
#! memory a base register and a displacement, written whole, which EVEX does
|
||||
#! not scale.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param r The register.
|
||||
#! @param m The memory.
|
||||
def selector::evex_mem(&mut this, op: u32, r: u32, m: mem): void {
|
||||
val base := m.base as u32;
|
||||
this.evex_opcode(op, r, 0, base, 0, true);
|
||||
this.b.text.byte((0x80 | ((r & 7) << 3) | (base & 7)) as u8);
|
||||
if (base & 7) == 4 {
|
||||
this.b.text.byte(0x24);
|
||||
}
|
||||
this.b.text.u32(m.disp as u64 & 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
#! An instruction of a register and memory on 64 bytes, with a byte after it.
|
||||
#! @param this The selector.
|
||||
#! @param op The instruction.
|
||||
#! @param r The register, which EVEX also names.
|
||||
#! @param m The memory.
|
||||
#! @param imm The byte.
|
||||
def selector::evex_mem_imm(&mut this, op: u32, r: u32, m: mem, imm: u8): void {
|
||||
val base := m.base as u32;
|
||||
this.evex_opcode(op, r, r, base, 0, true);
|
||||
this.b.text.byte((0x80 | ((r & 7) << 3) | (base & 7)) as u8);
|
||||
if (base & 7) == 4 {
|
||||
this.b.text.byte(0x24);
|
||||
}
|
||||
this.b.text.u32(m.disp as u64 & 0xFFFFFFFF);
|
||||
this.b.text.byte(imm);
|
||||
}
|
||||
|
||||
#! Loads an operand of the instruction, 64 bytes in its slot, into a register,
|
||||
#! a quarter at a time, as `read_halves` reads 32: a value is copied into its
|
||||
#! slot 16 bytes at a time, and one load reading four stores would wait for
|
||||
#! them all to reach the cache.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param k Which operand.
|
||||
#! @param z The register.
|
||||
def selector::fetch_wide(&mut this, index: u32, k: u32, z: u32): void {
|
||||
val held := this.read(this.body.arg(index, k), index);
|
||||
if held.kind == place::none {
|
||||
this.evex(z_pxord, z, z, z, 0);
|
||||
return;
|
||||
}
|
||||
val from := this.memory_of(&held);
|
||||
this.evex_mem(z_broadcasti32x4, z, from);
|
||||
for q in 1..4u {
|
||||
var part := from;
|
||||
part.disp += (16 * q) as i32;
|
||||
this.evex_mem_imm(z_inserti32x4, z, part, q as u8);
|
||||
}
|
||||
}
|
||||
|
||||
#! Splats a word across a register of 64 bytes, from a general register.
|
||||
#! @param this The selector.
|
||||
#! @param z The register.
|
||||
#! @param word The word.
|
||||
def selector::splat_wide(&mut this, z: u32, word: u64): void {
|
||||
this.b.text.mov_imm(reg::rax, word);
|
||||
this.evex(z_pbroadcastq_r, z, 0, reg::rax as u32, 0);
|
||||
}
|
||||
|
||||
#! `lanes` of 64 bytes, as the file says.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param held The instruction.
|
||||
def selector::lanes_wide(&mut this, index: u32, held: &inst): void {
|
||||
val operation := held.aux;
|
||||
val lane := (held.imm & 0xFF) as u32;
|
||||
val result := held.result;
|
||||
val real := lane >= lane_f32;
|
||||
val bits := lane_bits(lane);
|
||||
val doubles: u32 = lane == lane_f64 ? 0x10000 | vec_wide : 0;
|
||||
val q: u32 = bits == 64 ? vec_wide : 0;
|
||||
val to_mask := by_width(lane, 0, z_pmovw2m, z_pmovd2m, z_pmovq2m);
|
||||
if operation == lanes_bits {
|
||||
# `vpmov?2m` clears the bits of `k1` past the lanes.
|
||||
this.fetch_wide(index, 0, z16);
|
||||
this.evex(to_mask, 1, 0, z16, 0);
|
||||
val into := this.target(result, reg::rax);
|
||||
this.b.text.vex(bits == 16 ? k_movd : k_movw, false, into as u32, 0, 1);
|
||||
this.put(result, into);
|
||||
return;
|
||||
}
|
||||
if operation == lanes_splat {
|
||||
val value := this.body.arg(index, 0);
|
||||
if lane == lane_f32 {
|
||||
this.evex(z_broadcastss, z16, 0, this.single_reg(value, index, 0) as u32, 0);
|
||||
} else if lane == lane_f64 {
|
||||
this.load_real_into(value, index, 0);
|
||||
this.evex(z_broadcastsd, z16, 0, 0, 0);
|
||||
} else {
|
||||
val r := this.word_reg(value, index, reg::rax);
|
||||
this.evex(by_width(lane, 0, z_pbroadcastw_r, z_pbroadcastd_r, z_pbroadcastq_r), z16, 0, r as u32, 0);
|
||||
}
|
||||
} else if operation == lanes_select {
|
||||
# `k1` set where the mask is; each lane then the second operand's there, and
|
||||
# the third's elsewhere.
|
||||
this.fetch_wide(index, 0, z16);
|
||||
this.fetch_wide(index, 1, z17);
|
||||
this.fetch_wide(index, 2, z18);
|
||||
this.evex(to_mask, 1, 0, z16, 0);
|
||||
val blend := real ? (lane == lane_f64 ? z_blendmpd : z_blendmps) : by_width(lane, 0, z_pblendmw, z_pblendmd, z_pblendmq);
|
||||
this.evex(blend, z16, z18, z17, 1);
|
||||
} else if operation == lanes_fma {
|
||||
# `zmm16 = zmm17 * zmm18 + zmm16`, as `lanes.sakura`'s is.
|
||||
this.fetch_wide(index, 2, z16);
|
||||
this.fetch_wide(index, 0, z17);
|
||||
this.fetch_wide(index, 1, z18);
|
||||
this.evex(vec_vfmadd231ps | (lane == lane_f64 ? vec_wide : 0), z16, z17, z18, 0);
|
||||
} else if operation == lanes_shift_left || operation == lanes_shift_right {
|
||||
# The count in the low word of `zmm17`, the same for every lane; a signed
|
||||
# shift right of 64-bit lanes is AVX-512's own.
|
||||
val count := this.word_reg(this.body.arg(index, 1), index, reg::rax);
|
||||
this.evex(z_pbroadcastq_r, z17, 0, count as u32, 0);
|
||||
this.fetch_wide(index, 0, z16);
|
||||
val op := operation == lanes_shift_left ? by_width(lane, 0, vec_psllw, vec_pslld, vec_psllq)
|
||||
: lane_signed(lane) ? by_width(lane, 0, vec_psraw, vec_psrad, vec_psrad) : by_width(lane, 0, vec_psrlw, vec_psrld, vec_psrlq);
|
||||
this.evex(op | q, z16, z16, z17, 0);
|
||||
} else if operation >= lanes_equal && operation <= lanes_greater_equal {
|
||||
this.compare_wide(index, operation, lane);
|
||||
} else {
|
||||
this.arithmetic_wide(index, operation, lane, doubles, q);
|
||||
}
|
||||
val made := this.p.made_at(result);
|
||||
if made.kind != place::none {
|
||||
this.evex_mem(z_movdqu64_store, z16, this.memory_of(&made));
|
||||
}
|
||||
}
|
||||
|
||||
#! Compares 64 bytes of lanes into a mask: into `k1` by a predicate, of reals
|
||||
#! as SSE's are, the greater ones with their operands swapped, then its lanes.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param operation The comparison.
|
||||
#! @param lane The type of lane.
|
||||
def selector::compare_wide(&mut this, index: u32, operation: u32, lane: u32): void {
|
||||
val bits := lane_bits(lane);
|
||||
this.fetch_wide(index, 0, z16);
|
||||
this.fetch_wide(index, 1, z17);
|
||||
if lane >= lane_f32 {
|
||||
val swapped := operation == lanes_greater || operation == lanes_greater_equal;
|
||||
val predicate: u8 = operation == lanes_equal ? 0 : operation == lanes_not_equal ? 4
|
||||
: operation == lanes_less || operation == lanes_greater ? 1 : 2;
|
||||
val op := z_cmpps | (lane == lane_f64 ? 0x10000 | vec_wide : 0);
|
||||
if swapped {
|
||||
this.evex_imm(op, 1, z17, z16, predicate);
|
||||
} else {
|
||||
this.evex_imm(op, 1, z16, z17, predicate);
|
||||
}
|
||||
} else {
|
||||
# 0 equal, 1 less, 2 at most, 4 not equal, 5 at least, 6 greater.
|
||||
val predicate: u8 = operation == lanes_equal ? 0 : operation == lanes_not_equal ? 4 : operation == lanes_less ? 1
|
||||
: operation == lanes_less_equal ? 2 : operation == lanes_greater ? 6 : 5;
|
||||
val signed := lane_signed(lane);
|
||||
val op := bits == 16 ? (signed ? z_pcmpw : z_pcmpuw) : (signed ? z_pcmpd : z_pcmpud) | (bits == 64 ? vec_wide : 0);
|
||||
this.evex_imm(op, 1, z16, z17, predicate);
|
||||
}
|
||||
this.evex(by_width(lane, 0, z_pmovm2w, z_pmovm2d, z_pmovm2q), z16, 0, 1, 0);
|
||||
}
|
||||
|
||||
#! The arithmetic, bitwise operations, and rounding of 64 bytes of lanes, into
|
||||
#! `zmm16`.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param operation The operation.
|
||||
#! @param lane The type of lane.
|
||||
#! @param doubles What makes an instruction of `f32`s one of `f64`s.
|
||||
#! @param q What makes one of 32-bit integers one of 64.
|
||||
def selector::arithmetic_wide(&mut this, index: u32, operation: u32, lane: u32, doubles: u32, q: u32): void {
|
||||
val real := lane >= lane_f32;
|
||||
this.fetch_wide(index, 0, z16);
|
||||
if operation == lanes_negate {
|
||||
if real {
|
||||
this.splat_wide(z17, sign_bits(lane));
|
||||
this.evex(z_pxord | vec_wide, z16, z16, z17, 0);
|
||||
} else {
|
||||
this.evex(z_pxord, z17, z17, z17, 0);
|
||||
this.evex(by_width(lane, 0, z_psubw, z_psubd, z_psubq), z16, z17, z16, 0);
|
||||
}
|
||||
} else if operation == lanes_not {
|
||||
this.evex_imm(z_pternlogd, z16, z16, z16, 0x0F);
|
||||
} else if operation == lanes_abs {
|
||||
if real {
|
||||
this.splat_wide(z17, ~sign_bits(lane));
|
||||
this.evex(z_pandd | vec_wide, z16, z16, z17, 0);
|
||||
} else if lane_signed(lane) {
|
||||
this.evex(by_width(lane, 0, z_pabsw, z_pabsd, z_pabsq), z16, 0, z16, 0);
|
||||
}
|
||||
} else if operation == lanes_sqrt {
|
||||
this.evex(vec_sqrtps | doubles, z16, 0, z16, 0);
|
||||
} else if operation >= lanes_floor && operation <= lanes_round_even {
|
||||
# Toward -∞, +∞, zero, or the nearest, even on a tie, as a whole number,
|
||||
# with no exception for what is not exact.
|
||||
val mode: u8 = operation == lanes_floor ? 9 : operation == lanes_ceil ? 10 : operation == lanes_trunc ? 11 : 8;
|
||||
this.evex_imm(lane == lane_f64 ? z_rndscalepd : z_rndscaleps, z16, 0, z16, mode);
|
||||
} else {
|
||||
this.fetch_wide(index, 1, z17);
|
||||
var op: u32 = 0;
|
||||
if operation == lanes_add {
|
||||
op = real ? vec_addps | doubles : by_width(lane, 0, z_paddw, z_paddd, z_paddq);
|
||||
} else if operation == lanes_sub {
|
||||
op = real ? vec_subps | doubles : by_width(lane, 0, z_psubw, z_psubd, z_psubq);
|
||||
} else if operation == lanes_mul {
|
||||
op = real ? vec_mulps | doubles : by_width(lane, 0, z_pmullw, z_pmulld, z_pmullq);
|
||||
} else if operation == lanes_div {
|
||||
op = vec_divps | doubles;
|
||||
} else if operation == lanes_and || operation == lanes_or || operation == lanes_xor {
|
||||
op = (operation == lanes_and ? z_pandd : operation == lanes_or ? z_pord : z_pxord) | vec_wide;
|
||||
} else {
|
||||
val least := operation == lanes_min;
|
||||
val signed := lane_signed(lane);
|
||||
op = lane_bits(lane) == 16 ? (signed ? (least ? z_pminsw : z_pmaxsw) : (least ? z_pminuw : z_pmaxuw))
|
||||
: (signed ? (least ? z_pminsd : z_pmaxsd) : (least ? z_pminud : z_pmaxud)) | q;
|
||||
}
|
||||
this.evex(op, z16, z16, z17, 0);
|
||||
}
|
||||
}
|
||||
+1297
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,52 @@
|
||||
#! @file output.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how a build writes its files: each in place of any file there, as a
|
||||
#! C compiler replaces what it writes over, even an executable a program is
|
||||
#! running from.
|
||||
|
||||
import std::io;
|
||||
import system;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! Writes bytes to a file, made with permissions, in place of any file there,
|
||||
#! as `create_replacing` replaces it.
|
||||
#! @param path The file.
|
||||
#! @param bytes The bytes.
|
||||
#! @param mode The permissions.
|
||||
#! @returns Whether it was written.
|
||||
pub def write_bytes(path: strview, bytes: span<u8>, mode: u32): bool {
|
||||
var at_path := path::parse(path) else (_) { return false; };
|
||||
var made := create_replacing(&at_path, mode) else (_) { return false; };
|
||||
made.write(bytes) else (_) { return false; };
|
||||
made.close();
|
||||
set_permissions(&at_path, mode) else (_) { return false; };
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Opens a file to write in place of any file at a path, as a C compiler
|
||||
#! replaces what it writes over. One that cannot be opened where it is, as an
|
||||
#! executable a program runs from, is moved aside first, to its path with
|
||||
#! `.old` added, then removed from there. A program running from it keeps
|
||||
#! running: Linux removes the file once the program ends, and Windows keeps it
|
||||
#! until a file is next written in its place, which moves this one over it.
|
||||
#! @param at The path of the file.
|
||||
#! @param mode The permissions a file this makes is given, less the mask.
|
||||
#! @returns The open file, or why it could not be opened.
|
||||
pub def create_replacing(at: &path, mode: u32): result<file, io_error> {
|
||||
var made := file::create(at, mode) else (why) {
|
||||
var aside := path::parse($"{at.span}.old".span) else (_) { return why; };
|
||||
io::rename(at, &aside) else (_) { return why; };
|
||||
var fresh := file::create(at, mode) else (again) {
|
||||
_ = io::rename(&aside, at);
|
||||
return again;
|
||||
};
|
||||
_ = io::remove(&aside);
|
||||
return move(&mut fresh);
|
||||
};
|
||||
return move(&mut made);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,337 @@
|
||||
#! @file relax.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the shortening of a function's jumps. Selection writes every jump
|
||||
#! with a 32-bit displacement, since where it goes is often not known yet. Once
|
||||
#! the function's code is written, each jump within it whose target is near
|
||||
#! enough becomes one of a byte's displacement, 3 bytes shorter for a `jmp` and
|
||||
#! 4 for a `jcc`. Shortening one brings others nearer, and no jump moves
|
||||
#! farther, so every jump is first taken to be short and those that turn out
|
||||
#! too far are made long again until none is. Then the code is written again,
|
||||
#! and everything that named a place within it is moved with it.
|
||||
#!
|
||||
#! A loop whose head ends in a jump through a table, as a machine's loop over
|
||||
#! its operations does, comes back to that block from each case, and runs as
|
||||
#! fast as the processor fetches it, which is slower where it lies across two
|
||||
#! lines of 64 bytes: the rebuild's own machine took 15% longer so. Selection
|
||||
#! writes 63 bytes of `nop`s before such a head, and once the jumps are settled
|
||||
#! each is cut to what starts the head at a multiple of 64. Cutting only brings
|
||||
#! jumps nearer, so none settled is too far.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! A displacement written within the function, and the jump holding it.
|
||||
struct fixup {
|
||||
var at: u64; #! Where the displacement is in the text.
|
||||
var target: u64; #! Where it goes in the text.
|
||||
var before: u64; #! How many displacements end at or before its target.
|
||||
var kind: u8; #! 1 for a `jmp`, 2 for a `jcc`, 3 for the padding before a loop's head, ending where a displacement would, or 0 for what is not shortened.
|
||||
var short: bool; #! Whether it is shortened.
|
||||
var cut: u64; #! Of padding, how many of its bytes go.
|
||||
}
|
||||
|
||||
#! The bytes of padding before a loop's head.
|
||||
const pad_bytes: u64 = 63;
|
||||
|
||||
#! The `nop`s of each length up to 11 bytes, as Intel writes them, one after
|
||||
#! another, from 1 byte.
|
||||
const nops: u8[66] = [
|
||||
0x90,
|
||||
0x66, 0x90,
|
||||
0x0F, 0x1F, 0x00,
|
||||
0x0F, 0x1F, 0x40, 0x00,
|
||||
0x0F, 0x1F, 0x44, 0x00, 0x00,
|
||||
0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00,
|
||||
0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00,
|
||||
0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x66, 0x2E, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x66, 0x66, 0x2E, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
|
||||
#! Writes `nop`s of so many bytes, of 11 bytes each but the last.
|
||||
#! @param into Where.
|
||||
#! @param count How many bytes.
|
||||
def write_nops(into: *mut u8, count: u64): void {
|
||||
var left := count;
|
||||
var at: u64 = 0;
|
||||
while left > 0 {
|
||||
val length := left > 11 ? 11 : left;
|
||||
val first := length * (length - 1) / 2;
|
||||
for i in 0..length {
|
||||
into[at + i] = nops[first + i];
|
||||
}
|
||||
at += length;
|
||||
left -= length;
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a block heads a loop and ends in a jump through a table, whose
|
||||
#! padding `relax` aligns.
|
||||
#! @param this The selector.
|
||||
#! @param block The block.
|
||||
#! @returns Whether it does.
|
||||
def selector::dispatches(&this, block: u32): bool {
|
||||
val last: u32 = this.body.blocks[block].last;
|
||||
return false if last == 0 || this.body.insts[last].kind != op::switch;
|
||||
val held: inst = this.body.insts[last];
|
||||
var low: i64 = 0;
|
||||
return this.table_size(&held, &mut low) != 0 && this.p.heads_loop(block);
|
||||
}
|
||||
|
||||
#! Writes the padding before a loop's head, for `relax` to cut.
|
||||
#! @param this The selector.
|
||||
def selector::pad_head(&mut this): void {
|
||||
var made: u8[63];
|
||||
write_nops((&mut made) as *mut u8, pad_bytes);
|
||||
this.pads.append(this.b.text.length);
|
||||
for i in 0..pad_bytes {
|
||||
this.b.text.byte(made[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#! The bytes of a jump's opcode before its displacement.
|
||||
#! @param kind The jump's kind.
|
||||
#! @returns How many.
|
||||
def opcode_bytes(kind: u8): u64 {
|
||||
return kind == 2 ? 2 : 1;
|
||||
}
|
||||
|
||||
#! Where a place of the text moves to once the jumps shortened before it are.
|
||||
#! @param ends Where every displacement ends, in order of place.
|
||||
#! @param saved How many bytes the jumps before each saved, and every jump
|
||||
#! after the last.
|
||||
#! @param place The place.
|
||||
#! @returns Where it moves to.
|
||||
def moved(ends: span<u64>, saved: &vector<u64>, place: u64): u64 {
|
||||
return place - saved[ends.upper_bound(&place)];
|
||||
}
|
||||
|
||||
#! Works out how many bytes the jumps before each saved, as they stand.
|
||||
#! @param fixes Every displacement, in order of place.
|
||||
#! @param saved Where how many is written for each, and for after the last.
|
||||
def count_saved(fixes: &vector<fixup>, saved: &mut vector<u64>): void {
|
||||
var total: u64 = 0;
|
||||
for i in 0..fixes.length {
|
||||
saved[i] = total;
|
||||
val each: fixup = fixes[i];
|
||||
if each.kind == 3 {
|
||||
total += each.cut;
|
||||
} else if each.short {
|
||||
total += each.kind == 2 ? 4 : 3;
|
||||
}
|
||||
}
|
||||
saved[fixes.length] = total;
|
||||
}
|
||||
|
||||
#! Shortens the function's jumps whose targets are near enough, then writes its
|
||||
#! code again and moves everything naming a place within it.
|
||||
#! @param this The selector.
|
||||
#! @param spans Where each instruction's code begins and ends, moved too.
|
||||
def selector::relax(&mut this, spans: &mut vector<u32>): void {
|
||||
val start := this.start;
|
||||
val end := this.b.text.length;
|
||||
return if end == start;
|
||||
|
||||
# The last displacement written at each place, in order of place: each
|
||||
# written is sorted by its place, then by when it was written.
|
||||
var keys := vector::<u64>::new(scratch());
|
||||
val logged := this.b.text.fixups.length / 2;
|
||||
for k in 0..logged {
|
||||
val at: u64 = this.b.text.fixups[2 * k];
|
||||
if at >= start && at + 4 <= end {
|
||||
keys.append(pair_key((at - start) as u32, k as u32));
|
||||
}
|
||||
}
|
||||
# Each padding, its displacement taken to end where it does.
|
||||
for p in 0..this.pads.length {
|
||||
keys.append(pair_key((this.pads[p] + pad_bytes - 4 - start) as u32, (logged + p) as u32));
|
||||
}
|
||||
this.pads.clear();
|
||||
keys.sort();
|
||||
var fixes := vector::<fixup>::new(scratch());
|
||||
val bytes := this.b.text.bytes.span;
|
||||
for i in 0..keys.length {
|
||||
continue if i + 1 < keys.length && pair_high(keys[i + 1]) == pair_high(keys[i]);
|
||||
val k := pair_low(keys[i]) as u64;
|
||||
val at := start + pair_high(keys[i]);
|
||||
if k >= logged {
|
||||
fixes.append(fixup { at = at, kind = 3, short = true });
|
||||
continue;
|
||||
}
|
||||
val target: u64 = this.b.text.fixups[2 * k + 1];
|
||||
var kind: u8 = 0;
|
||||
if at >= start + 1 && bytes[at - 1] == 0xE9 {
|
||||
kind = 1;
|
||||
} else if at >= start + 2 && bytes[at - 2] == 0x0F && (bytes[at - 1] & 0xF0) == 0x80 {
|
||||
kind = 2;
|
||||
}
|
||||
if target < start || target > end {
|
||||
kind = 0;
|
||||
}
|
||||
fixes.append(fixup { at = at, target = target, kind = kind, short = kind != 0 });
|
||||
}
|
||||
this.b.text.fixups.clear();
|
||||
return if fixes.length == 0;
|
||||
|
||||
# How many displacements end at or before a place is how many of their ends
|
||||
# are not past it.
|
||||
var ending := vector::<u64>::new(scratch());
|
||||
for &each in fixes {
|
||||
ending.append(each.at + 4);
|
||||
}
|
||||
val ends := ending.span;
|
||||
for i in 0..fixes.length {
|
||||
fixes[i].before = ends.upper_bound(&fixes[i].target);
|
||||
}
|
||||
|
||||
# Every jump is taken to be short, and those too far made long, until none
|
||||
# is.
|
||||
var saved := vector::<u64>::new(scratch());
|
||||
saved.resize(fixes.length + 1);
|
||||
var changed := true;
|
||||
while changed {
|
||||
changed = false;
|
||||
count_saved(&fixes, &mut saved);
|
||||
for i in 0..fixes.length {
|
||||
val each: fixup = fixes[i];
|
||||
continue if !each.short || each.kind == 3;
|
||||
# Every displacement before this one ends before its jump begins.
|
||||
val from := each.at - opcode_bytes(each.kind) - saved[i] + 2;
|
||||
val distance := (each.target - saved[each.before]) as i64 - from as i64;
|
||||
if distance < -128 || distance > 127 {
|
||||
fixes[i].short = false;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
# Each padding is cut to what starts its head at a multiple of 64, once what
|
||||
# comes before it is cut.
|
||||
var total: u64 = 0;
|
||||
for i in 0..fixes.length {
|
||||
val each: fixup = fixes[i];
|
||||
if each.kind == 3 {
|
||||
val opening := each.at + 4 - pad_bytes - total;
|
||||
val kept := (64 - opening % 64) % 64;
|
||||
fixes[i].cut = pad_bytes - kept;
|
||||
total += pad_bytes - kept;
|
||||
} else if each.short {
|
||||
total += each.kind == 2 ? 4 : 3;
|
||||
}
|
||||
}
|
||||
count_saved(&fixes, &mut saved);
|
||||
return if saved[fixes.length] == 0;
|
||||
|
||||
# The code again, each short jump written as one, what lies between copied
|
||||
# whole.
|
||||
var code := vector::<u8>::new(scratch());
|
||||
code.resize(end - start - saved[fixes.length]);
|
||||
val into := code.slice.data;
|
||||
var from := start;
|
||||
var written: u64 = 0;
|
||||
for i in 0..fixes.length {
|
||||
val each: fixup = fixes[i];
|
||||
continue if !each.short;
|
||||
if each.kind == 3 {
|
||||
val opening := each.at + 4 - pad_bytes;
|
||||
memcpy(into + written, bytes.data + from, opening - from);
|
||||
written += opening - from;
|
||||
write_nops(into + written, pad_bytes - each.cut);
|
||||
written += pad_bytes - each.cut;
|
||||
from = each.at + 4;
|
||||
continue;
|
||||
}
|
||||
val opening := each.at - opcode_bytes(each.kind);
|
||||
memcpy(into + written, bytes.data + from, opening - from);
|
||||
written += opening - from;
|
||||
code[written] = each.kind == 1 ? 0xEB : (0x70 | (bytes[each.at - 1] & 0x0F));
|
||||
written += 2;
|
||||
from = each.at + 4;
|
||||
}
|
||||
memcpy(into + written, bytes.data + from, end - from);
|
||||
|
||||
# Each displacement, from where its jump is now to where its target is.
|
||||
for i in 0..fixes.length {
|
||||
val each: fixup = fixes[i];
|
||||
continue if each.kind == 3;
|
||||
val target := each.target >= start && each.target <= end ? moved(ends, &saved, each.target) : each.target;
|
||||
if each.short {
|
||||
val opening := moved(ends, &saved, each.at - opcode_bytes(each.kind));
|
||||
code[opening - start + 1] = ((target as i64 - (opening as i64 + 2)) & 0xFF) as u8;
|
||||
continue;
|
||||
}
|
||||
val at := moved(ends, &saved, each.at);
|
||||
val relative := (target as i64 - (at as i64 + 4)) as u64;
|
||||
for b in 0..4u {
|
||||
code[at - start + b] = ((relative >> (8 * b)) & 0xFF) as u8;
|
||||
}
|
||||
}
|
||||
this.b.text.bytes.rewind(start);
|
||||
this.b.text.put(code.span);
|
||||
|
||||
# What named places within the code names where they moved.
|
||||
for i in 0..this.labels.length {
|
||||
val label: u64 = this.labels[i];
|
||||
if label >= start && label <= end {
|
||||
this.labels[i] = moved(ends, &saved, label);
|
||||
}
|
||||
}
|
||||
for i in 0..this.entries.length {
|
||||
if this.entries[i].block == never {
|
||||
this.entries[i].at = moved(ends, &saved, this.entries[i].at);
|
||||
}
|
||||
}
|
||||
for i in 0..this.tables.length {
|
||||
this.tables[i].patch = moved(ends, &saved, this.tables[i].patch);
|
||||
}
|
||||
for i in 0..this.constant_uses.length {
|
||||
val use: u64 = this.constant_uses[i];
|
||||
this.constant_uses[i] = pair_key(moved(ends, &saved, pair_high(use) as u64) as u32, pair_low(use));
|
||||
}
|
||||
for i in 0..this.addresses.length {
|
||||
val at := moved(ends, &saved, this.addresses[i]);
|
||||
val address := this.b.text.get_u64(at) - this.b.text_base;
|
||||
this.b.text.set_u64(at, this.b.text_base + moved(ends, &saved, address));
|
||||
this.addresses[i] = at;
|
||||
}
|
||||
# The calls and the rows come in order of place, so each is moved past the
|
||||
# jumps before it in one walk.
|
||||
var j: u64 = 0;
|
||||
for i in this.calls_first..this.b.calls.length {
|
||||
val call: u64 = this.b.calls.at[i];
|
||||
while j < fixes.length && fixes[j].at + 4 <= call {
|
||||
j++;
|
||||
}
|
||||
this.b.calls.at[i] = call - saved[j];
|
||||
}
|
||||
j = 0;
|
||||
for r in this.b.row_first[this.index] as u64..this.b.rows.length {
|
||||
val place := start + this.b.rows[r].offset;
|
||||
while j < fixes.length && fixes[j].at + 4 <= place {
|
||||
j++;
|
||||
}
|
||||
this.b.rows[r].offset = place - saved[j] - start;
|
||||
}
|
||||
for i in 0..spans.length {
|
||||
spans[i] = (moved(ends, &saved, start + spans[i] as u64) - start) as u32;
|
||||
}
|
||||
var k: u64 = 0;
|
||||
while k < this.stub_spans.length {
|
||||
for j in 0..2u {
|
||||
this.stub_spans[k + j] = (moved(ends, &saved, start + this.stub_spans[k + j] as u64) - start) as u32;
|
||||
}
|
||||
k += 3;
|
||||
}
|
||||
if this.b.options.debug || this.b.options.windows {
|
||||
this.b.debug.bodies[this.index] = (moved(ends, &saved, start + this.b.debug.bodies[this.index] as u64) - start) as u32;
|
||||
this.b.debug.saved_at[this.index] = (moved(ends, &saved, start + this.b.debug.saved_at[this.index] as u64) - start) as u32;
|
||||
for r in this.b.debug.return_first[this.index] as u64..this.b.debug.returns.length {
|
||||
this.b.debug.returns[r] = (moved(ends, &saved, start + this.b.debug.returns[r] as u64) - start) as u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
#! @file reshape.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the operations on lanes giving lanes of another type or size, as
|
||||
#! `lanes_needs` numbers them past `lanes_convert`: converting each lane,
|
||||
#! widening half a vector's lanes and narrowing two vectors' into one, taking
|
||||
#! 32 bytes apart into halves and joining two halves, and gathering lanes from
|
||||
#! memory. Each is worked in `xmm0` and those after it, as `lanes.sakura`'s
|
||||
#! operations are, the result put where it goes from `xmm0`.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
# The instructions, written as `binary::amd64` writes vector instructions.
|
||||
# Widening integers, each the signed form and the unsigned 16 on: bytes to 16,
|
||||
# 32, and 64 bits, 16 to 32 and 64, and 32 to 64.
|
||||
const vec_pmovsxbw: u32 = 0x010220;
|
||||
const vec_pmovsxbd: u32 = 0x010221;
|
||||
const vec_pmovsxbq: u32 = 0x010222;
|
||||
const vec_pmovsxwd: u32 = 0x010223;
|
||||
const vec_pmovsxwq: u32 = 0x010224;
|
||||
const vec_pmovsxdq: u32 = 0x010225;
|
||||
const vec_movzx_from_sx: u32 = 0x10;
|
||||
# Packing two registers' lanes into one of half the width, saturating.
|
||||
const vec_packuswb: u32 = 0x010167;
|
||||
const vec_packssdw: u32 = 0x01016B;
|
||||
const vec_packusdw: u32 = 0x01022B;
|
||||
# Converting: `i32` to `f32`, `f32` to `i32` toward zero, `f32` to `f64` and
|
||||
# back, and `i32` to `f64`.
|
||||
const vec_cvtdq2ps: u32 = 0x00015B;
|
||||
const vec_cvttps2dq: u32 = 0x02015B;
|
||||
const vec_cvtps2pd: u32 = 0x00015A;
|
||||
const vec_cvtpd2ps: u32 = 0x01015A;
|
||||
const vec_cvtdq2pd: u32 = 0x0201E6;
|
||||
const vec_cvttpd2dq: u32 = 0x0101E6;
|
||||
# A register's high 8 bytes into another's low, and 64-bit lanes put in an
|
||||
# order across 32 bytes, AVX2's.
|
||||
const vec_movhlps: u32 = 0x000112;
|
||||
const vec_vpermq: u32 = 0x1010300;
|
||||
|
||||
#! An operation giving lanes of another type or size, as the file says.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param held The instruction.
|
||||
#! @param lane The type of lane it is given.
|
||||
#! @param long Whether what it is given is 32 bytes.
|
||||
def selector::reshape_lanes(&mut this, index: u32, held: &inst, lane: u32, long: bool): void {
|
||||
val operation := held.aux & 0xFF;
|
||||
val target := lanes_target(held.aux);
|
||||
val result := held.result;
|
||||
val out_long := this.body.type_of(result) == vt::v256;
|
||||
if operation == lanes_low {
|
||||
this.fetch_lanes(index, 0, 0, true);
|
||||
} else if operation == lanes_high {
|
||||
this.fetch_lanes(index, 0, 0, true);
|
||||
this.b.text.vex_imm(vec_vextractf128, true, 0, 0, 0, 1);
|
||||
} else if operation == lanes_join {
|
||||
this.fetch_lanes(index, 0, 0, false);
|
||||
this.fetch_lanes(index, 1, 1, false);
|
||||
this.b.text.vex_imm(vec_vinsertf128, true, 0, 0, 1, 1);
|
||||
} else if operation == lanes_widen || operation == lanes_widen_high {
|
||||
this.widen_lanes(index, operation == lanes_widen_high, lane, target, long);
|
||||
} else if operation == lanes_narrow {
|
||||
this.narrow_lanes(index, lane, target, long);
|
||||
} else if operation == lanes_gather {
|
||||
this.gather_lanes(index, lane, long || out_long);
|
||||
} else {
|
||||
this.convert_lanes(index, lane, target, long, out_long);
|
||||
}
|
||||
this.put_lanes(result, 0, out_long);
|
||||
this.settle_upper(index, long || out_long);
|
||||
}
|
||||
|
||||
# An instruction reading only its second register, as a conversion: with VEX's
|
||||
# form naming no other where the code is written with VEX.
|
||||
def selector::vunary(&mut this, op: u32, long: bool, x: u8, y: u8): void {
|
||||
if long || this.b.text.avx {
|
||||
this.b.text.vex(op, long, x as u32, 0, y as u32);
|
||||
} else {
|
||||
this.b.text.packed(op, x as u32, y as u32);
|
||||
}
|
||||
}
|
||||
|
||||
# The instruction widening integer lanes of `from` bits to `into` bits, by the
|
||||
# sign or not.
|
||||
def widening(from: u64, into: u64, signed: bool): u32 {
|
||||
val sx := from == 8 ? (into == 16 ? vec_pmovsxbw : into == 32 ? vec_pmovsxbd : vec_pmovsxbq)
|
||||
: from == 16 ? (into == 32 ? vec_pmovsxwd : vec_pmovsxwq) : vec_pmovsxdq;
|
||||
return signed ? sx : sx + vec_movzx_from_sx;
|
||||
}
|
||||
|
||||
#! Half the lanes of `xmm0` widened to twice their width into `xmm0`: of 16
|
||||
#! bytes, the first half, or the second moved down; of 32, either half, into
|
||||
#! all 32. Integers keep their value by their sign, as `as` does, and `f32`s
|
||||
#! become `f64`s.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param high Whether it is the second half.
|
||||
#! @param lane The type of lane.
|
||||
#! @param target The type of lane it gives.
|
||||
#! @param long Whether it is 32 bytes.
|
||||
def selector::widen_lanes(&mut this, index: u32, high: bool, lane: u32, target: u32, long: bool): void {
|
||||
this.fetch_lanes(index, 0, 0, long);
|
||||
if long {
|
||||
if high {
|
||||
this.b.text.vex_imm(vec_vextractf128, true, 0, 0, 0, 1);
|
||||
}
|
||||
} else if high {
|
||||
this.b.text.packed(vec_movhlps, 0, 0);
|
||||
}
|
||||
val op := lane >= lane_f32 ? vec_cvtps2pd : widening(lane_bits(lane), lane_bits(target), lane_signed(lane));
|
||||
this.vunary(op, long, 0, 0);
|
||||
}
|
||||
|
||||
#! Two vectors' lanes narrowed to half their width into `xmm0`, the first's
|
||||
#! first: integers packed, saturating, those of unsigned lanes held at the
|
||||
#! type's greatest first, and `f64`s rounded to `f32`s. 32 bytes are packed
|
||||
#! within each half, then their 8-byte quarters put back in order.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param lane The type of lane.
|
||||
#! @param target The type of lane it gives.
|
||||
#! @param long Whether each is 32 bytes.
|
||||
def selector::narrow_lanes(&mut this, index: u32, lane: u32, target: u32, long: bool): void {
|
||||
this.fetch_lanes(index, 0, 0, long);
|
||||
this.fetch_lanes(index, 1, 1, long);
|
||||
if lane == lane_f64 {
|
||||
this.vunary(vec_cvtpd2ps, long, 0, 0);
|
||||
this.vunary(vec_cvtpd2ps, long, 1, 1);
|
||||
if long {
|
||||
this.b.text.vex_imm(vec_vinsertf128, true, 0, 0, 1, 1);
|
||||
} else {
|
||||
this.b.text.packed(vec_movlhps, 0, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
val bits := lane_bits(lane);
|
||||
val signed := lane_signed(target);
|
||||
if !lane_signed(lane) {
|
||||
# Held at the target's greatest, which packing then keeps.
|
||||
val most: u64 = bits == 16 ? (signed ? 0x007F007F007F007F : 0x00FF00FF00FF00FF) : (signed ? 0x00007FFF00007FFF : 0x0000FFFF0000FFFF);
|
||||
this.splat_bits(2, most, long);
|
||||
this.vop(bits == 16 ? vec_pminuw : vec_pminud, long, 0, 2);
|
||||
this.vop(bits == 16 ? vec_pminuw : vec_pminud, long, 1, 2);
|
||||
}
|
||||
val op := bits == 16 ? (lane_signed(lane) && signed ? vec_packsswb : vec_packuswb)
|
||||
: (lane_signed(lane) && signed ? vec_packssdw : vec_packusdw);
|
||||
this.vop(op, long, 0, 1);
|
||||
if long {
|
||||
this.b.text.vex_imm(vec_vpermq, true, 0, 0, 0, 0xD8);
|
||||
}
|
||||
}
|
||||
|
||||
#! Each lane converted to another type of as many lanes into `xmm0`, as `as`
|
||||
#! converts it: integers widened as `widen_lanes` widens them, or narrowed, by
|
||||
#! the low bytes of each kept, from 32 bytes to 16; `i32`s to reals by the
|
||||
#! instruction, and `u32`s as their high and low 16 bits, each exact, added
|
||||
#! once; smaller integers to `f32` widened to `i32` first; `f32`s and `f64`s
|
||||
#! to 32-bit integers toward zero, which `simd`'s `convert` gives only lanes
|
||||
#! within `i32`'s range, where the instruction is exact, and an unsigned one
|
||||
#! held at 0 below zero; and `f32`s to `f64`s and back.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param lane The type of lane.
|
||||
#! @param target The type of lane it gives.
|
||||
#! @param long Whether what it is given is 32 bytes.
|
||||
#! @param out_long Whether what it gives is 32 bytes.
|
||||
def selector::convert_lanes(&mut this, index: u32, lane: u32, target: u32, long: bool, out_long: bool): void {
|
||||
val bits := lane_bits(lane);
|
||||
val into := lane_bits(target);
|
||||
val real := lane >= lane_f32;
|
||||
val into_real := target >= lane_f32;
|
||||
this.fetch_lanes(index, 0, 0, long);
|
||||
if !real && !into_real {
|
||||
if into > bits {
|
||||
this.vunary(widening(bits, into, lane_signed(lane)), out_long, 0, 0);
|
||||
} else if into < bits {
|
||||
# Each lane's low bytes, of each half, into that half's low 8 bytes, then
|
||||
# the halves' together.
|
||||
var low: u8[32];
|
||||
val width := bits / 8;
|
||||
val kept := into / 8;
|
||||
for i in 0..32u {
|
||||
val at := i % 16;
|
||||
low[i] = at < 16 / width * kept ? ((at / kept) * width + at % kept) as u8 : 0x80;
|
||||
}
|
||||
this.b.text.vex_imm(vec_vextracti128, true, 0, 0, 1, 1);
|
||||
this.load_bytes(2, low, false);
|
||||
this.b.text.vex(vec_pshufb, false, 0, 0, 2);
|
||||
this.b.text.vex(vec_pshufb, false, 1, 1, 2);
|
||||
this.b.text.vex(vec_punpcklqdq, false, 0, 0, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if !real && into_real {
|
||||
if target == lane_f64 {
|
||||
this.vunary(vec_cvtdq2pd, true, 0, 0);
|
||||
return;
|
||||
}
|
||||
if bits < 32 {
|
||||
this.vunary(widening(bits, 32, lane_signed(lane)), out_long, 0, 0);
|
||||
}
|
||||
if lane == lane_u32 {
|
||||
# The high 16 bits times 2^16, and the low 16, each exact, added once.
|
||||
this.vmove(out_long, 1, 0);
|
||||
this.shift_by(vec_psrld, out_long, 1, 16);
|
||||
this.splat_bits(2, 0x0000FFFF0000FFFF, out_long);
|
||||
this.vop(vec_pand, out_long, 0, 2);
|
||||
this.vunary(vec_cvtdq2ps, out_long, 0, 0);
|
||||
this.vunary(vec_cvtdq2ps, out_long, 1, 1);
|
||||
this.splat_bits(2, 0x4780000047800000, out_long);
|
||||
this.vop(vec_mulps, out_long, 1, 2);
|
||||
this.vop(vec_addps, out_long, 0, 1);
|
||||
return;
|
||||
}
|
||||
this.vunary(vec_cvtdq2ps, out_long, 0, 0);
|
||||
return;
|
||||
}
|
||||
if real && !into_real {
|
||||
if lane == lane_f32 {
|
||||
this.vunary(vec_cvttps2dq, long, 0, 0);
|
||||
} else {
|
||||
this.b.text.vex(vec_cvttpd2dq, true, 0, 0, 0);
|
||||
}
|
||||
# An unsigned integer holds what is below zero at 0, as `as` does.
|
||||
if !lane_signed(target) {
|
||||
this.vop(vec_pcmpeqd, out_long, 1, 1);
|
||||
this.vmove(out_long, 2, 0);
|
||||
this.vop(vec_pcmpgtd, out_long, 2, 1);
|
||||
this.vop(vec_pand, out_long, 0, 2);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if lane == lane_f32 {
|
||||
this.vunary(vec_cvtps2pd, true, 0, 0);
|
||||
} else {
|
||||
this.b.text.vex(vec_cvtpd2ps, true, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#! Lanes gathered from memory into `xmm0`, by AVX2's `vpgatherdd` and the rest:
|
||||
#! the address in `rax`, the 32-bit places in `xmm1`, and a mask of every lane
|
||||
#! in `xmm2`, which the instruction clears as it goes. `simd`'s `gather` checks
|
||||
#! each place is within the view first.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param lane The type of lane.
|
||||
#! @param long Whether the lanes gathered are 32 bytes.
|
||||
def selector::gather_lanes(&mut this, index: u32, lane: u32, long: bool): void {
|
||||
val bits := lane_bits(lane);
|
||||
this.load_into(this.body.arg(index, 0), index, reg::rax);
|
||||
this.fetch_lanes(index, 1, 1, bits == 32 && long);
|
||||
this.b.text.vex(vec_pcmpeqd, long, 2, 2, 2);
|
||||
this.b.text.vex(vec_pxor, long, 0, 0, 0);
|
||||
# VEX's three bytes, of the map `0F 38`, the prefix `66`, `W` for 64-bit
|
||||
# lanes, and `xmm2` the mask; then the opcode, `ModRM` naming a `SIB`, and the
|
||||
# `SIB` of the lanes' scale, `xmm1` the index, and `rax` the base.
|
||||
this.b.text.byte(0xC4);
|
||||
this.b.text.byte(0xE2);
|
||||
this.b.text.byte(((bits == 64 ? 0x80 : 0) | ((~2u & 15) << 3) | (long ? 4 : 0) | 1) as u8);
|
||||
this.b.text.byte(lane >= lane_f32 ? 0x92 : 0x90);
|
||||
this.b.text.byte(0x04);
|
||||
this.b.text.byte(((bits == 64 ? 3 : 2) << 6 | 1 << 3) as u8);
|
||||
}
|
||||
@@ -0,0 +1,759 @@
|
||||
#! @file select.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the selection of instructions: the x86-64 code of a function, made
|
||||
#! from its intermediate code and where the register allocator put each value.
|
||||
#! Each operation is written as a few instructions over the registers and
|
||||
#! slots its operands are in, computing in `rax`, `rcx`, and `rdx`, and `xmm0`
|
||||
#! and `xmm1`, which hold no value between instructions. Constants and the
|
||||
#! addresses the code knows become immediates and addressing modes, and a
|
||||
#! comparison only a branch reads is the branch's own test.
|
||||
#!
|
||||
#! A function's frame is on the stack, below the caller's frame pointer and
|
||||
#! return address: the registers a call keeps that it saved, the address its
|
||||
#! memory result goes to, the values' slots, words the runtime's helpers write,
|
||||
#! where it calls anything, the copies of the parameters it takes in memory,
|
||||
#! its cells, and the arguments it passes on the stack. Where a check
|
||||
#! fails, the code jumps to a stub after the function's body, which calls the
|
||||
#! trap stub with the error, so the check costs a compare and a jump never
|
||||
#! taken.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! A check's failure, written after the function's body.
|
||||
struct trap_site {
|
||||
var patch: u64; #! The jump to it.
|
||||
var code: u32; #! The error it stops the program with.
|
||||
var at: u64; #! Where in the source the check is.
|
||||
var site: u32; #! The inline site the check is in.
|
||||
var index: u32; #! The instruction checking.
|
||||
}
|
||||
|
||||
#! A safe point's call of the function running the handlers, written after
|
||||
#! the function's body, where a signal the program handles came.
|
||||
struct safe_site {
|
||||
var patch: u64; #! The jump to it.
|
||||
var back: u64; #! Where the code goes on.
|
||||
var at: u64; #! Where in the source the safe point is.
|
||||
var site: u32; #! The inline site it is in.
|
||||
var index: u32; #! The safe point.
|
||||
}
|
||||
|
||||
#! An entry of a switch's table: where a case goes, a block or code leading to
|
||||
#! one.
|
||||
struct table_entry {
|
||||
var block: u32; #! The block, or `never` where it is code.
|
||||
var at: u64; #! Where the code is in the text.
|
||||
}
|
||||
|
||||
#! A switch's table of addresses, written after the function's body.
|
||||
struct switch_table {
|
||||
var patch: u64; #! Where the jump through it holds its address.
|
||||
var first: u32; #! Its first entry in `entries`.
|
||||
var count: u32; #! How many entries it has.
|
||||
}
|
||||
|
||||
#! A word operand: a register, memory, or an immediate.
|
||||
struct operand_of {
|
||||
var kind: u8; #! 0 for a register, 1 for memory, 2 for an immediate.
|
||||
var r: reg;
|
||||
var m: mem;
|
||||
var imm: i32;
|
||||
}
|
||||
|
||||
#! What selecting the code of one function keeps, which makes the moves between
|
||||
#! the places of its values.
|
||||
pub struct selector : move_maker<transfer> {
|
||||
var b: &mut building; #! The executable being built.
|
||||
var body: &code; #! The function's code.
|
||||
var p: placing; #! Where its values are.
|
||||
var index: u32; #! The function.
|
||||
var start: u64; #! Where its code begins in the text.
|
||||
|
||||
# The frame, as offsets below the frame pointer.
|
||||
var frame: u64; #! The bytes of the frame below the frame pointer.
|
||||
var dst_at: u64; #! Of the address a result held in memory is written to.
|
||||
var slot_base: u64; #! Of the slots: slot `k` is at `slot_base + 8 * k + 8`, and a vector's of `n` words at that of its last.
|
||||
var scratch_at: u64; #! Of three words the runtime's helpers write.
|
||||
var cells: vector<u64>; #! Of each cell.
|
||||
var copies: vector<u64>; #! Of the copy of each parameter of the entry held in memory, or 0.
|
||||
var zero_from: u64; #! The cells zeroed as the function begins, from this offset
|
||||
var zero_to: u64; #! up to this one.
|
||||
var out_words: u64; #! How many words the calls it makes pass on the stack.
|
||||
|
||||
var labels: vector<u64>; #! Where each block begins in the text.
|
||||
var jumps: fixup_list; #! The jumps to blocks, and the block each goes to, to patch once every block is placed.
|
||||
var traps: vector<trap_site>;
|
||||
var safe_points: vector<safe_site>;
|
||||
var tables: vector<switch_table>;
|
||||
var entries: vector<table_entry>; #! The entries of every table, one table's after another's.
|
||||
var addresses: vector<u64>; #! Where the code holds an address within itself, as a word.
|
||||
var calls_first: u64; #! The first of the building's calls the function makes.
|
||||
var stub_spans: vector<u32>; #! Of each stub written after the body, where its code begins and ends within the function's, then the instruction it is of.
|
||||
var pads: vector<u64>; #! Where the padding before each loop's head that `relax` aligns begins.
|
||||
var constants: vector<u8>; #! The vectors of constant lanes the code loads, 32 bytes each, written after its tables.
|
||||
var constant_uses: vector<u64>; #! Where the code holds the address of each it loads, and which, by `pair_key(place, constant)`.
|
||||
var last_at: u64;
|
||||
var last_site: u32;
|
||||
var returned: u64; #! The bytes of the memory it returns, or 0.
|
||||
var leaf: bool; #! Whether it calls nothing.
|
||||
var saved: reg[5]; #! The registers a call keeps that it saves below the frame pointer, in order, from the highest.
|
||||
var save_count: u64; #! How many.
|
||||
var aligned: bool; #! Whether its frame keeps `rsp` a multiple of 16, as calls, safe points, and taking signals need.
|
||||
var tails: bool; #! Whether a call a return comes straight after may be a jump, made once the frame is left: in code optimized with inlining, where no pointer into the frame is kept where the code cannot follow it.
|
||||
var framed: vector<bool>; #! Where tail calls may be made, whether each value may point into the frame.
|
||||
var tailed: u32; #! The return a call before it took the place of, jumping, or 0.
|
||||
var upper_parts: vector<u64>; #! Where registers hold 32 bytes of lanes, each its start and end as a pair's key, in code that may run without AVX.
|
||||
var uses_upper: bool; #! Whether that code works on 32 bytes of lanes at all.
|
||||
var single: vector<bool>; #! Whether each value is a real held as an `f32`, in its low four bytes, as `singles.sakura` says.
|
||||
var works: vector<bool>; #! Whether each instruction works an arithmetic of `f32`s in single precision.
|
||||
}
|
||||
|
||||
|
||||
# Frames.
|
||||
|
||||
#! Finds the registers a call keeps that the function holds values in, which
|
||||
#! it saves as it begins and gives back as it returns. One reading `frame`
|
||||
#! saves every one of them, so what its callers hold in them is in its frame,
|
||||
#! where a reader of the stack from there up finds it. Finds too
|
||||
#! whether its frame keeps `rsp` a multiple of 16: where it calls anything, as
|
||||
#! C needs, or makes a safe point or takes signals, which call what does.
|
||||
#! @param this The selector.
|
||||
def selector::find_saved(&mut this): void {
|
||||
var used: u64 = 0;
|
||||
for &each in this.p.child_locs {
|
||||
if each.kind == place::gpr && keeps(each.number) {
|
||||
used = used | (1u << each.number as u64);
|
||||
}
|
||||
}
|
||||
this.aligned = !this.leaf;
|
||||
for k in 0..this.p.order.length {
|
||||
var walk := this.body.walk(this.p.order[k]);
|
||||
while walk.more() {
|
||||
val kind: op = this.body.insts[walk.next()].kind;
|
||||
if kind == op::frame {
|
||||
used = 0xFFFF;
|
||||
}
|
||||
if kind == op::safepoint || kind == op::signals {
|
||||
this.aligned = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
for &r in kept_gprs {
|
||||
if (used & (1u << r as u64)) != 0 {
|
||||
this.saved[this.save_count] = r;
|
||||
this.save_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! The registers a function saves, one bit each by number.
|
||||
#! @param this The selector.
|
||||
#! @returns The bits.
|
||||
def selector::saved_mask(&this): u32 {
|
||||
var mask: u64 = 0;
|
||||
for k in 0..this.save_count {
|
||||
mask = mask | (1u << this.saved[k] as u64);
|
||||
}
|
||||
return mask as u32;
|
||||
}
|
||||
|
||||
#! Lays out the frame of the function. The scratch the runtime's helpers write
|
||||
#! is only that of one calling anything.
|
||||
#! @param this The selector.
|
||||
#! @param memory Whether each parameter of the entry is held in memory.
|
||||
#! @param sizes The bytes of each one held in memory.
|
||||
def selector::lay_frame(&mut this, memory: span<bool>, sizes: span<u64>): void {
|
||||
var below: u64 = 8 * this.save_count;
|
||||
if this.returned != 0 {
|
||||
below += 8;
|
||||
this.dst_at = below;
|
||||
}
|
||||
this.slot_base = below;
|
||||
below += 8 * this.p.slot_count as u64;
|
||||
if !this.leaf {
|
||||
below += 24;
|
||||
}
|
||||
this.scratch_at = below;
|
||||
|
||||
this.copies.resize(memory.length);
|
||||
for i in 0..memory.length {
|
||||
continue if !memory[i];
|
||||
below = round_up(below + round_up(sizes[i], 8), 16);
|
||||
this.copies[i] = below;
|
||||
}
|
||||
|
||||
# The cells the frame zeroes come together, then those it need not: those
|
||||
# of functions inlined, whose code clears them, and those every read of
|
||||
# which some write comes before.
|
||||
val writes := this.b.c.result_writes(this.index);
|
||||
val zeroed := zeroed_cells(this.body, this.p.order.span, this.p.number.span, writes.span);
|
||||
this.cells.resize(this.body.cells.length);
|
||||
below = round_up(below, 8);
|
||||
this.zero_from = below;
|
||||
for pass in 0..2u {
|
||||
for i in 0..this.body.cells.length {
|
||||
val each: cell = this.body.cells[i];
|
||||
val zeroes := each.kind != ck::inlined && each.kind != ck::undefined && zeroed[i];
|
||||
continue if zeroes != (pass == 0);
|
||||
val align := each.align < 8 ? 8 : (each.align > 16 ? 16 : each.align as u64);
|
||||
below = round_up(below + round_up(each.size as u64, 8), align);
|
||||
this.cells[i] = below;
|
||||
}
|
||||
if pass == 0 {
|
||||
this.zero_to = below;
|
||||
}
|
||||
}
|
||||
|
||||
# The arguments passed on the stack are at its bottom.
|
||||
this.frame = round_up(below + 8 * this.out_words, this.aligned ? 16 : 8);
|
||||
}
|
||||
|
||||
#! The memory of a value's slot: the words it takes, from the lowest.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @returns The memory.
|
||||
def selector::slot(&this, value: u32): mem {
|
||||
val k := this.p.slot_of[value] as u64 + (this.p.bytes[value] / 8) as u64 - 1;
|
||||
return at(reg::rbp, -((this.slot_base + 8 * k + 8) as i64) as i32);
|
||||
}
|
||||
|
||||
#! The memory of a word the scratch of the frame holds.
|
||||
#! @param this The selector.
|
||||
#! @param k The word: 0, 1, or 2.
|
||||
#! @returns The memory.
|
||||
def selector::scratch_word(&this, k: u64): mem {
|
||||
return at(reg::rbp, (8 * k as i64 - this.scratch_at as i64) as i32);
|
||||
}
|
||||
|
||||
#! The memory of a place a move reads or writes that is no register.
|
||||
#! @param this The selector.
|
||||
#! @param held The place.
|
||||
#! @returns The memory.
|
||||
def selector::memory_of(&this, held: &loc): mem {
|
||||
if held.kind == place::slot {
|
||||
return this.slot(held.value);
|
||||
}
|
||||
if held.kind == place::out {
|
||||
return at(reg::rsp, (8 * held.value as u64) as i32);
|
||||
}
|
||||
# A word the function was called with past those in registers, above the
|
||||
# return address and the caller's frame pointer.
|
||||
return at(reg::rbp, (16 + 8 * (held.value as u64 - arg_count)) as i32);
|
||||
}
|
||||
|
||||
|
||||
# Values made again.
|
||||
|
||||
#! The address a value made again is, where it is known while compiling: of the
|
||||
#! globals, a datum, or the image.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param address Where the address is written.
|
||||
#! @returns Whether it is known.
|
||||
def selector::known_address(&this, value: u32, address: &mut u64): bool {
|
||||
val made := this.body.defining(value);
|
||||
return false if made == 0;
|
||||
val held: inst = this.body.insts[made];
|
||||
if held.kind == op::global {
|
||||
address = this.b.globals + held.imm;
|
||||
return true;
|
||||
}
|
||||
if held.kind == op::datum {
|
||||
address = this.b.datum_address(held.aux) + held.imm;
|
||||
return true;
|
||||
}
|
||||
if held.kind == op::image {
|
||||
address = this.b.image_address;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Where in the frame a value made again points, where it is an address of the
|
||||
#! frame: a cell, or the copy of a parameter held in memory.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param offset Where the offset below the frame pointer is written.
|
||||
#! @returns Whether it is.
|
||||
def selector::frame_address(&this, value: u32, offset: &mut u64): bool {
|
||||
if this.p.memory_params[value] {
|
||||
offset = this.copies[this.body.origins[value].index];
|
||||
return true;
|
||||
}
|
||||
val made := this.body.defining(value);
|
||||
return false if made == 0;
|
||||
val held: inst = this.body.insts[made];
|
||||
return false if held.kind != op::cell;
|
||||
offset = this.cells[held.aux];
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The bits of a value made again that is a constant.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param bits Where the bits are written.
|
||||
#! @returns Whether it is a constant.
|
||||
def selector::constant_bits(&this, value: u32, bits: &mut u64): bool {
|
||||
val made := this.body.defining(value);
|
||||
return false if made == 0 || this.p.memory_params[value];
|
||||
val held: inst = this.body.insts[made];
|
||||
return false if held.kind != op::iconst && held.kind != op::fconst;
|
||||
bits = held.imm;
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Makes a value made again in a general register.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param into The register.
|
||||
def selector::remake(&mut this, value: u32, into: reg): void {
|
||||
var offset: u64 = 0;
|
||||
if this.frame_address(value, &mut offset) {
|
||||
this.b.text.lea(into, at(reg::rbp, -(offset as i64) as i32));
|
||||
return;
|
||||
}
|
||||
var address: u64 = 0;
|
||||
if this.known_address(value, &mut address) {
|
||||
this.b.text.mov_imm(into, address);
|
||||
return;
|
||||
}
|
||||
val held: inst = this.body.insts[this.body.defining(value)];
|
||||
match held.kind {
|
||||
op::iconst, op::fconst => this.b.text.mov_imm(into, held.imm);
|
||||
op::callback => this.b.text.load(8, false, into, absolute(this.b.callback_word(held.aux)));
|
||||
op::c_address => this.b.text.load(8, false, into, absolute(this.b.got_word(held.aux)));
|
||||
op::frame => this.b.text.mov(into, reg::rsp);
|
||||
else => this.b.text.mov_imm(into, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Moves.
|
||||
|
||||
#! Makes a move: from a register, a slot, the stack, or a value made again, to
|
||||
#! a register, a slot, or the stack. `rcx` carries a word between memory, and
|
||||
#! `xmm0` a vector.
|
||||
#! @param this The selector.
|
||||
#! @param made The move.
|
||||
pub def selector::move_one(&mut this, made: &transfer): void {
|
||||
val from := made.from;
|
||||
val to := made.to;
|
||||
return if to.kind == place::none || from.same(&to);
|
||||
if made.bytes == 64 {
|
||||
this.move_wide(this.memory_of(&from), this.memory_of(&to));
|
||||
return;
|
||||
}
|
||||
if made.bytes > 8 {
|
||||
this.move_lanes(from, to, made.bytes == 32);
|
||||
return;
|
||||
}
|
||||
if to.kind == place::gpr {
|
||||
val r := to.number as reg;
|
||||
match from.kind {
|
||||
place::gpr => this.b.text.mov(r, from.number as reg);
|
||||
place::xmm => this.b.text.movq_from_xmm(r, from.number);
|
||||
place::remade => this.remake(from.value, r);
|
||||
else => this.b.text.load(8, false, r, this.memory_of(&from));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if to.kind == place::xmm {
|
||||
match from.kind {
|
||||
place::xmm => this.b.text.movaps(to.number, from.number);
|
||||
place::gpr => this.b.text.movq_to_xmm(to.number, from.number as reg);
|
||||
place::remade => {
|
||||
this.remake(from.value, reg::rcx);
|
||||
this.b.text.movq_to_xmm(to.number, reg::rcx);
|
||||
}
|
||||
else => this.b.text.movsd_load(to.number, this.memory_of(&from));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
# To memory.
|
||||
val dst := this.memory_of(&to);
|
||||
match from.kind {
|
||||
place::gpr => this.b.text.store(8, dst, from.number as reg);
|
||||
place::xmm => this.b.text.movsd_store(dst, from.number);
|
||||
place::remade => {
|
||||
var bits: u64 = 0;
|
||||
if this.constant_bits(from.value, &mut bits) && fits_i32(bits) {
|
||||
this.b.text.store_imm(dst, bits as i32);
|
||||
return;
|
||||
}
|
||||
this.remake(from.value, reg::rcx);
|
||||
this.b.text.store(8, dst, reg::rcx);
|
||||
}
|
||||
else => {
|
||||
this.b.text.load(8, false, reg::rcx, this.memory_of(&from));
|
||||
this.b.text.store(8, dst, reg::rcx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Makes moves all at once: each place written is read first by any move
|
||||
#! reading it, and a cycle saves one place in `rax`, or in `xmm0` for an SSE
|
||||
#! register, before writing it. Each move made gives its place among those
|
||||
#! waiting to the last.
|
||||
#! @param this The selector.
|
||||
#! @param given The moves, whose places written all differ.
|
||||
def selector::parallel(&mut this, given: span<transfer>): void {
|
||||
var pending := vector::<transfer>::new(scratch());
|
||||
for &each in given {
|
||||
continue if each.to.kind == place::none || each.from.same(&each.to);
|
||||
pending.append(each);
|
||||
}
|
||||
make_moves::<transfer, selector>(&mut pending, this, true);
|
||||
}
|
||||
|
||||
#! Whether a move reads the place another writes.
|
||||
#! @param this The selector.
|
||||
#! @param reader The move that may read it.
|
||||
#! @param writer The move writing it.
|
||||
#! @returns Whether it does.
|
||||
pub def selector::move_reads(&this, reader: &transfer, writer: &transfer): bool {
|
||||
return reader.from.same(&writer.to);
|
||||
}
|
||||
|
||||
#! The move saving what a move would write over, which a cycle of moves still
|
||||
#! reads, in `rax`, or in `xmm0` for an SSE register.
|
||||
#! @param this The selector.
|
||||
#! @param written The move.
|
||||
#! @param pending The moves waiting, among them those reading the place.
|
||||
#! @returns The move saving it.
|
||||
pub def selector::move_saving(&mut this, written: &transfer, pending: span<transfer>): transfer {
|
||||
# It holds the value a move reading it moves, which is as wide as that is.
|
||||
val saved: loc = written.to;
|
||||
var bytes: u8 = 8;
|
||||
for &each in pending {
|
||||
if each.from.same(&saved) {
|
||||
bytes = each.bytes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
val keep := saved.kind == place::xmm ? in_xmm(0) : in_gpr(reg::rax);
|
||||
return transfer { from = saved, to = keep, bytes = bytes };
|
||||
}
|
||||
|
||||
#! Has a move read what another saved from where it was saved.
|
||||
#! @param this The selector.
|
||||
#! @param reader The move.
|
||||
#! @param saved The move saving what it reads.
|
||||
pub def selector::move_reread(&this, reader: &mut transfer, saved: &transfer): void {
|
||||
reader.from = saved.to;
|
||||
}
|
||||
|
||||
#! Makes the moves of the gap before a place.
|
||||
#! @param this The selector.
|
||||
#! @param at The place.
|
||||
def selector::gap(&mut this, at: u32): void {
|
||||
val moves := this.p.gap_moves.of(at);
|
||||
return if moves.length == 0;
|
||||
this.parallel(moves);
|
||||
}
|
||||
|
||||
#! Makes the moves along an edge.
|
||||
#! @param this The selector.
|
||||
#! @param edge The edge.
|
||||
def selector::edge(&mut this, edge: u32): void {
|
||||
val moves := this.p.edge_moves_of(edge);
|
||||
return if moves.length == 0;
|
||||
this.parallel(moves);
|
||||
}
|
||||
|
||||
#! Whether an edge moves anything.
|
||||
#! @param this The selector.
|
||||
#! @param edge The edge.
|
||||
#! @returns Whether it does.
|
||||
def selector::moves_on(&this, edge: u32): bool {
|
||||
return this.p.edge_first[edge as u64] != this.p.edge_first[edge as u64 + 1];
|
||||
}
|
||||
|
||||
|
||||
# Operands.
|
||||
|
||||
#! Where a value is read by an instruction.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param index The instruction reading it.
|
||||
#! @returns Where.
|
||||
def selector::read(&this, value: u32, index: u32): loc {
|
||||
return this.p.read_at(value, index);
|
||||
}
|
||||
|
||||
#! A general register holding a word an instruction reads: its own, or the
|
||||
#! scratch register given, loaded with it.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param index The instruction reading it.
|
||||
#! @param scratch The register it is loaded into where it is in none.
|
||||
#! @returns The register.
|
||||
def selector::word_reg(&mut this, value: u32, index: u32, scratch: reg): reg {
|
||||
val held := this.read(value, index);
|
||||
match held.kind {
|
||||
place::gpr => return held.number as reg;
|
||||
place::xmm => this.b.text.movq_from_xmm(scratch, held.number);
|
||||
place::remade => this.remake(value, scratch);
|
||||
place::none => this.b.text.mov_imm(scratch, 0);
|
||||
else => this.b.text.load(8, false, scratch, this.memory_of(&held));
|
||||
}
|
||||
return scratch;
|
||||
}
|
||||
|
||||
#! An SSE register holding a real an instruction reads: its own, or the
|
||||
#! scratch register given, loaded with it.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param index The instruction reading it.
|
||||
#! @param scratch The register it is loaded into where it is in none.
|
||||
#! @returns The register's number.
|
||||
def selector::real_reg(&mut this, value: u32, index: u32, scratch: u8): u8 {
|
||||
val held := this.read(value, index);
|
||||
return this.widened(&held, scratch) if this.single[value];
|
||||
match held.kind {
|
||||
place::xmm => return held.number;
|
||||
place::gpr => this.b.text.movq_to_xmm(scratch, held.number as reg);
|
||||
place::remade => {
|
||||
this.remake(value, reg::rdx);
|
||||
this.b.text.movq_to_xmm(scratch, reg::rdx);
|
||||
}
|
||||
place::none => {
|
||||
this.b.text.mov_imm(reg::rdx, 0);
|
||||
this.b.text.movq_to_xmm(scratch, reg::rdx);
|
||||
}
|
||||
else => this.b.text.movsd_load(scratch, this.memory_of(&held));
|
||||
}
|
||||
return scratch;
|
||||
}
|
||||
|
||||
#! A word an instruction reads as an operand of arithmetic: an immediate where
|
||||
#! it is a constant a 32-bit immediate holds, memory where it is in its slot,
|
||||
#! and a register otherwise.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param index The instruction reading it.
|
||||
#! @param scratch The register it is loaded into where it is in none.
|
||||
#! @returns The operand.
|
||||
def selector::word_operand(&mut this, value: u32, index: u32, scratch: reg): operand_of {
|
||||
var imm: i32 = 0;
|
||||
if this.immediate(value, &mut imm) {
|
||||
return operand_of { kind = 2, imm = imm };
|
||||
}
|
||||
if this.read(value, index).kind == place::slot {
|
||||
return operand_of { kind = 1, m = this.slot(value) };
|
||||
}
|
||||
return operand_of { kind = 0, r = this.word_reg(value, index, scratch) };
|
||||
}
|
||||
|
||||
#! Whether a word an instruction reads is a constant a 32-bit immediate holds.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param imm Where the constant is written.
|
||||
#! @returns Whether it is.
|
||||
def selector::immediate(&this, value: u32, imm: &mut i32): bool {
|
||||
return false if !this.p.remade[value];
|
||||
var bits: u64 = 0;
|
||||
return false if !this.constant_bits(value, &mut bits) && !this.known_address(value, &mut bits);
|
||||
return false if !fits_i32(bits);
|
||||
imm = bits as i32;
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The general register a word result is computed into: its own, or the
|
||||
#! fallback where it goes to its slot or nowhere.
|
||||
#! @param this The selector.
|
||||
#! @param value The result.
|
||||
#! @param fallback The register otherwise.
|
||||
#! @returns The register.
|
||||
def selector::target(&this, value: u32, fallback: reg): reg {
|
||||
val held := this.p.made_at(value);
|
||||
return held.kind == place::gpr ? held.number as reg : fallback;
|
||||
}
|
||||
|
||||
#! Puts a word result computed in a register where it goes.
|
||||
#! @param this The selector.
|
||||
#! @param value The result.
|
||||
#! @param r The register.
|
||||
def selector::put(&mut this, value: u32, r: reg): void {
|
||||
val held := this.p.made_at(value);
|
||||
match held.kind {
|
||||
place::gpr => {
|
||||
if held.number as reg != r {
|
||||
this.b.text.mov(held.number as reg, r);
|
||||
}
|
||||
}
|
||||
place::xmm => this.b.text.movq_to_xmm(held.number, r);
|
||||
place::slot => this.b.text.store(8, this.slot(value), r);
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
|
||||
#! The SSE register a real result is computed into: its own, or `xmm0`.
|
||||
#! @param this The selector.
|
||||
#! @param value The result.
|
||||
#! @returns The register's number.
|
||||
def selector::real_target(&this, value: u32): u8 {
|
||||
val held := this.p.made_at(value);
|
||||
return held.kind == place::xmm ? held.number : 0;
|
||||
}
|
||||
|
||||
#! Puts a real result computed in an SSE register where it goes.
|
||||
#! @param this The selector.
|
||||
#! @param value The result.
|
||||
#! @param x The register.
|
||||
def selector::put_real(&mut this, value: u32, x: u8): void {
|
||||
val held := this.p.made_at(value);
|
||||
match held.kind {
|
||||
place::xmm => {
|
||||
if held.number != x {
|
||||
this.b.text.movaps(held.number, x);
|
||||
}
|
||||
}
|
||||
place::gpr => this.b.text.movq_from_xmm(held.number as reg, x);
|
||||
place::slot => this.b.text.movsd_store(this.slot(value), x);
|
||||
else => {}
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a result goes anywhere, so it is worth computing.
|
||||
#! @param this The selector.
|
||||
#! @param value The result.
|
||||
#! @returns Whether it does.
|
||||
def selector::wanted(&this, value: u32): bool {
|
||||
return this.p.made_at(value).kind != place::none;
|
||||
}
|
||||
|
||||
#! The memory an address an instruction reads points at, from an offset:
|
||||
#! addressing the frame or the image directly where the address is one of
|
||||
#! theirs, through the base and index a sum folded into it is made of, and
|
||||
#! otherwise through a register holding it.
|
||||
#! @param this The selector.
|
||||
#! @param value The address.
|
||||
#! @param index The instruction reading it.
|
||||
#! @param offset The bytes from the address.
|
||||
#! @param scratch The register it is loaded into where it is in none.
|
||||
#! @param second The register the index of a sum folded into it is loaded
|
||||
#! into where it is in none.
|
||||
#! @param direct Where whether it points into the frame or the image is
|
||||
#! written, which is never null.
|
||||
#! @returns The memory.
|
||||
def selector::address(&mut this, value: u32, index: u32, offset: u64, scratch: reg, second: reg, direct: &mut bool): mem {
|
||||
direct = false;
|
||||
val made := this.body.defining(value);
|
||||
if made != 0 && this.p.folded[made] && this.body.insts[made].kind == op::add {
|
||||
return this.sum_address(made, index, offset, scratch, second, direct);
|
||||
}
|
||||
if this.p.remade[value] {
|
||||
var below: u64 = 0;
|
||||
if this.frame_address(value, &mut below) {
|
||||
direct = true;
|
||||
return at(reg::rbp, (offset as i64 - below as i64) as i32);
|
||||
}
|
||||
var known: u64 = 0;
|
||||
if this.known_address(value, &mut known) {
|
||||
direct = true;
|
||||
return absolute(known + offset);
|
||||
}
|
||||
}
|
||||
return at(this.word_reg(value, index, scratch), offset as i32);
|
||||
}
|
||||
|
||||
#! The base, index, and scale of a sum: the word a shift folded into it scales
|
||||
#! as its index, or else the operand held rather than made again, the other
|
||||
#! its base.
|
||||
#! @param this The selector.
|
||||
#! @param sum The sum.
|
||||
#! @param base Where its base is written.
|
||||
#! @param scale Where its index's scale is written.
|
||||
#! @returns Its index.
|
||||
def selector::sum_parts(&this, sum: u32, base: &mut u32, scale: &mut u8): u32 {
|
||||
val a := this.body.arg(sum, 0);
|
||||
val b := this.body.arg(sum, 1);
|
||||
scale = 1;
|
||||
for k in 0..2u {
|
||||
val operand := k == 0 ? a : b;
|
||||
val made := this.body.defining(operand);
|
||||
continue if made == 0 || !this.p.folded[made] || this.body.insts[made].kind != op::shl;
|
||||
base = k == 0 ? b : a;
|
||||
scale = (1u << this.body.insts[this.body.defining(this.body.arg(made, 1))].imm) as u8;
|
||||
return this.body.arg(made, 0);
|
||||
}
|
||||
if this.p.remade[b] {
|
||||
base = b;
|
||||
return a;
|
||||
}
|
||||
base = a;
|
||||
return b;
|
||||
}
|
||||
|
||||
#! The memory a sum folded into the instruction reading it points at, from an
|
||||
#! offset: its base and its index scaled, the base the frame's or the image's
|
||||
#! own where it is an address of theirs.
|
||||
#! @param this The selector.
|
||||
#! @param sum The sum.
|
||||
#! @param index The instruction reading it.
|
||||
#! @param offset The bytes from the address.
|
||||
#! @param scratch The register the base is loaded into where it is in none.
|
||||
#! @param second The register the index is loaded into where it is in none.
|
||||
#! @param direct Where whether it points into the frame or the image is
|
||||
#! written.
|
||||
#! @returns The memory.
|
||||
def selector::sum_address(&mut this, sum: u32, index: u32, offset: u64, scratch: reg, second: reg, direct: &mut bool): mem {
|
||||
var base: u32 = 0;
|
||||
var scale: u8 = 1;
|
||||
val scaled := this.sum_parts(sum, &mut base, &mut scale);
|
||||
val by := this.word_reg(scaled, index, second);
|
||||
if this.p.remade[base] {
|
||||
var below: u64 = 0;
|
||||
if this.frame_address(base, &mut below) {
|
||||
direct = true;
|
||||
return indexed(reg::rbp, by, scale, (offset as i64 - below as i64) as i32);
|
||||
}
|
||||
var known: u64 = 0;
|
||||
if this.known_address(base, &mut known) {
|
||||
direct = true;
|
||||
return indexed(reg::none, by, scale, (known + offset) as i32);
|
||||
}
|
||||
}
|
||||
return indexed(this.word_reg(base, index, scratch), by, scale, offset as i32);
|
||||
}
|
||||
|
||||
#! Stops the program with an error where a condition holds, jumping to a stub
|
||||
#! after the function's body that reports it.
|
||||
#! @param this The selector.
|
||||
#! @param condition The condition.
|
||||
#! @param code The error.
|
||||
#! @param index The instruction checking, whose place the report names.
|
||||
def selector::trap_if(&mut this, condition: cc, code: u32, index: u32): void {
|
||||
val held: inst = this.body.insts[index];
|
||||
this.traps.append(trap_site { patch = this.b.text.jcc(condition), code = code, at = held.at, site = held.site, index = index });
|
||||
}
|
||||
|
||||
#! Stops the program with an error where a register holding an address is
|
||||
#! null, where reading at the offset would not fault on its own and the program
|
||||
#! checks.
|
||||
#! @param this The selector.
|
||||
#! @param base The register.
|
||||
#! @param reach How far past the address the access reaches.
|
||||
#! @param index The instruction.
|
||||
def selector::null_check(&mut this, base: reg, reach: u64, index: u32): void {
|
||||
return if reach <= null_page || !this.checking();
|
||||
this.b.text.test(base, base);
|
||||
this.trap_if(cc::equal, fault::null_reference as u32, index);
|
||||
}
|
||||
|
||||
#! Whether the program checks what only a checked build does, as `--checks`
|
||||
#! asks: here, an address reached far past null, a function value's index, and
|
||||
#! a trait's vtable, each compared with what it may be.
|
||||
#! @param this The selector.
|
||||
#! @returns Whether it does.
|
||||
def selector::checking(&this): bool {
|
||||
return !this.b.c.checker.checks_off;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,622 @@
|
||||
#! @file singles.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains which `f32`s an executable works in single precision. The code
|
||||
#! holds every real as an `f64`, and an `f32`'s arithmetic as the `f64`'s,
|
||||
#! rounded to an `f32` where it is read: `fsingle`, or a store of four bytes.
|
||||
#! `addss`, `subss`, `mulss`, `divss`, and `sqrtss` of `f32`s round exactly as
|
||||
#! the `f64`'s, rounded again, do, since an `f64` holds more than twice an
|
||||
#! `f32`'s bits, so the code gives the same bits worked either way. Worked in
|
||||
#! single precision, it is one instruction for three, and the conversions
|
||||
#! around each leave every chain of them.
|
||||
#!
|
||||
#! An operation is worked so where its operands are `f32`s exactly, and every
|
||||
#! reader rounds what it gives. A value is held as an `f32`, its low four bytes,
|
||||
#! where it is one, and every reader takes it so: those, comparisons, and the
|
||||
#! edges giving it to a block whose parameter is held so too. Any other reader
|
||||
#! reads it widened, as `real_reg` gives it; a splat of `f32` lanes takes it
|
||||
#! as it is. What is held so is the same with debugging information as
|
||||
#! without, which describes a variable as an `f32` where most of its values are
|
||||
#! held so, and as the `f64` they are held as otherwise, leaving out where it is
|
||||
#! held the other way.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
# The instructions of `f32`s, written as the vector instructions are, with the
|
||||
# prefix `F3`, and of `f64`s, with `F2`.
|
||||
const ss_add: u32 = 0x020158;
|
||||
const ss_sub: u32 = 0x02015C;
|
||||
const ss_mul: u32 = 0x020159;
|
||||
const ss_div: u32 = 0x02015E;
|
||||
const ss_sqrt: u32 = 0x020151;
|
||||
const ss_min: u32 = 0x02015D;
|
||||
const ss_max: u32 = 0x02015F;
|
||||
const ss_cmp: u32 = 0x0201C2;
|
||||
const ss_widen: u32 = 0x02015A;
|
||||
const sd_min: u32 = 0x03015D;
|
||||
const sd_max: u32 = 0x03015F;
|
||||
const sd_cmp: u32 = 0x0301C2;
|
||||
const sd_narrow: u32 = 0x03015A;
|
||||
const ss_compare: u32 = 0x00012E;
|
||||
|
||||
# The predicates of `cmpss` and `cmpsd`, each false where either is not a
|
||||
# number, as `<` and `<=` of reals are.
|
||||
const predicate_lt: u8 = 1;
|
||||
const predicate_le: u8 = 2;
|
||||
|
||||
#! Works out which operations are worked in single precision, and which values
|
||||
#! are held as `f32`s, as the file says.
|
||||
#! @param this The selector.
|
||||
def selector::find_singles(&mut this): void {
|
||||
val values := this.body.origins.length;
|
||||
this.single.resize(values, false);
|
||||
this.works.resize(this.body.insts.length, false);
|
||||
|
||||
# Of each real, whether every reader takes it as an `f32`, and whether every
|
||||
# reader rounds it to one, which passing it on does not.
|
||||
var handled := vector::<bool>::new(scratch());
|
||||
var rounded := vector::<bool>::new(scratch());
|
||||
handled.resize(values, true);
|
||||
rounded.resize(values, true);
|
||||
# A negation and a choice pass on what they read as it is, so what they read
|
||||
# is rounded where every reader of what they give rounds it.
|
||||
var passing := vector::<u32>::new(scratch());
|
||||
var any := false;
|
||||
for k in 0..this.p.order.length {
|
||||
var walk := this.body.walk(this.p.order[k]);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.body.insts[at];
|
||||
val passes := passes_on(held.kind) && held.count != 0 && this.body.type_of(held.result) == vt::real;
|
||||
if passes {
|
||||
passing.append(at);
|
||||
}
|
||||
for a in 0..held.argc {
|
||||
val value := this.body.arg(at, a);
|
||||
continue if this.body.type_of(value) != vt::real;
|
||||
# A splat or a pack of `f32` lanes rounds what it reads to an `f32`, as a
|
||||
# store of one does.
|
||||
val splats := held.kind == op::lanes && (held.aux == lanes_splat || held.aux == lanes_pack) && (held.imm & 0xFF) as u32 == lane_f32;
|
||||
val rounds := held.kind == op::fsingle || (held.kind == op::store && held.aux as access == access::f32 && a == 1) || splats;
|
||||
val alone := held.kind == op::lanes && (held.imm >> 8) == 4;
|
||||
any = any || rounds || alone;
|
||||
if !rounds && !passes {
|
||||
rounded[value] = false;
|
||||
}
|
||||
if !rounds && !splats && !alone && !takes_single(held.kind) {
|
||||
handled[value] = false;
|
||||
}
|
||||
}
|
||||
for e in 0..this.body.edge_count(at) {
|
||||
for &value in this.body.edges[held.aux + e as u32].args {
|
||||
rounded[value] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return if !any;
|
||||
var unrounding := true;
|
||||
while unrounding {
|
||||
unrounding = false;
|
||||
for &at in passing {
|
||||
continue if rounded[this.body.insts[at].result];
|
||||
val first: u32 = this.body.insts[at].kind == op::select ? 1 : 0;
|
||||
for a in first..this.body.insts[at].argc {
|
||||
val value := this.body.arg(at, a);
|
||||
if rounded[value] {
|
||||
rounded[value] = false;
|
||||
unrounding = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Which reals are `f32`s exactly: what is read or rounded as one, an
|
||||
# operation worked as one, a constant one is, and a block's parameter every
|
||||
# edge gives one, which each is taken to be until an edge gives another. The
|
||||
# function's own parameters are passed widened, whichever they are.
|
||||
var exact := vector::<bool>::new(scratch());
|
||||
exact.resize(values, false);
|
||||
for k in 0..this.p.order.length {
|
||||
val block: u32 = this.p.order[k];
|
||||
continue if block == 0;
|
||||
for ¶m in this.body.blocks[block].params {
|
||||
exact[param] = this.body.type_of(param) == vt::real;
|
||||
}
|
||||
}
|
||||
var changed := true;
|
||||
while changed {
|
||||
changed = false;
|
||||
for k in 0..this.p.order.length {
|
||||
var walk := this.body.walk(this.p.order[k]);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val held: inst = this.body.insts[at];
|
||||
continue if held.count == 0 || this.body.type_of(held.result) != vt::real;
|
||||
val result := held.result;
|
||||
var one := false;
|
||||
match held.kind {
|
||||
op::load => one = held.aux as access == access::f32;
|
||||
op::fsingle => one = true;
|
||||
op::fconst => one = single_constant(held.imm);
|
||||
op::fadd, op::fsub, op::fmul, op::fdiv => {
|
||||
one = rounded[result] && exact[this.body.arg(at, 0)] && exact[this.body.arg(at, 1)];
|
||||
}
|
||||
op::fsqrt => one = rounded[result] && exact[this.body.arg(at, 0)];
|
||||
# Negating and choosing round nothing, so what they give is an `f32`
|
||||
# wherever what they read is.
|
||||
op::fneg => one = exact[this.body.arg(at, 0)];
|
||||
op::select => one = exact[this.body.arg(at, 1)] && exact[this.body.arg(at, 2)];
|
||||
# One `f32` lane alone, or `f32` lanes added or multiplied together,
|
||||
# worked as `f32`s.
|
||||
op::lanes => {
|
||||
one = (held.imm >> 8) == 4
|
||||
|| ((held.imm & 0xFF) as u32 == lane_f32 && (held.aux == lanes_sum || held.aux == lanes_product || (held.aux & 0xFF) == lanes_lane));
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
exact[result] = one;
|
||||
this.works[at] = one && held.kind != op::load && held.kind != op::fsingle && held.kind != op::fconst;
|
||||
}
|
||||
}
|
||||
for k in 0..this.p.order.length {
|
||||
val block: u32 = this.p.order[k];
|
||||
val last: u32 = this.body.blocks[block].last;
|
||||
continue if last == 0;
|
||||
val ending: inst = this.body.insts[last];
|
||||
for e in 0..this.body.edge_count(last) {
|
||||
val edge := ending.aux + e as u32;
|
||||
val params := &this.body.blocks[this.body.edges[edge].target].params;
|
||||
val args := &this.body.edges[edge].args;
|
||||
for i in 0..args.length {
|
||||
val param: u32 = params[i];
|
||||
if exact[param] && !exact[args[i] as u64] {
|
||||
exact[param] = false;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Which are held as `f32`s: those every reader takes so, but for constants,
|
||||
# which are made again where read, and the function's own parameters. A
|
||||
# parameter and what each edge gives it are held alike, a constant of zero's
|
||||
# bits being either's.
|
||||
for v in 1..values {
|
||||
continue if !handled[v] || !exact[v] || this.p.remade[v] || this.body.type_of(v as u32) != vt::real;
|
||||
val made := this.body.defining(v as u32);
|
||||
if made == 0 {
|
||||
this.single[v] = this.body.origins[v].from != 0;
|
||||
} else {
|
||||
val kind := this.body.insts[made].kind;
|
||||
this.single[v] = kind == op::load || kind == op::fsingle || kind == op::lanes || this.works[made];
|
||||
}
|
||||
}
|
||||
changed = true;
|
||||
while changed {
|
||||
changed = false;
|
||||
for k in 0..this.p.order.length {
|
||||
val block: u32 = this.p.order[k];
|
||||
val last: u32 = this.body.blocks[block].last;
|
||||
continue if last == 0;
|
||||
val ending: inst = this.body.insts[last];
|
||||
for e in 0..this.body.edge_count(last) {
|
||||
val edge := ending.aux + e as u32;
|
||||
val params := &this.body.blocks[this.body.edges[edge].target].params;
|
||||
val args := &this.body.edges[edge].args;
|
||||
for i in 0..args.length {
|
||||
val param: u32 = params[i];
|
||||
val arg: u32 = args[i];
|
||||
continue if this.body.type_of(param) != vt::real;
|
||||
val given := this.single[arg as u64] || this.zero_constant(arg);
|
||||
if this.single[param] && !given {
|
||||
this.single[param] = false;
|
||||
changed = true;
|
||||
}
|
||||
if !this.single[param] && this.single[arg as u64] {
|
||||
this.single[arg as u64] = false;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether an operation takes a real it reads as an `f32` where it is held so:
|
||||
#! the arithmetic and comparisons of reals, negation, and choosing, which widen
|
||||
#! it where they work in double precision.
|
||||
#! @param kind The operation.
|
||||
#! @returns Whether it does.
|
||||
def takes_single(kind: op): bool {
|
||||
return match kind {
|
||||
op::fadd, op::fsub, op::fmul, op::fdiv, op::fsqrt, op::feq, op::fne, op::flt, op::fle, op::fneg, op::select => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether an operation gives a real it reads as it is, rounding nothing:
|
||||
#! negation, and choosing between two.
|
||||
#! @param kind The operation.
|
||||
#! @returns Whether it does.
|
||||
def passes_on(kind: op): bool {
|
||||
return kind == op::fneg || kind == op::select;
|
||||
}
|
||||
|
||||
#! Whether a real an instruction reads may be read as an `f32`: one held so,
|
||||
#! or a constant an `f32` holds exactly.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @returns Whether it may.
|
||||
def selector::single_operand(&this, value: u32): bool {
|
||||
return true if this.single[value];
|
||||
var bits: u64 = 0;
|
||||
return this.p.remade[value] && this.constant_bits(value, &mut bits) && single_constant(bits);
|
||||
}
|
||||
|
||||
#! Whether a comparison of two reals is made of `f32`s: both may be read so, and
|
||||
#! one is held so, which reading it widened would convert.
|
||||
#! @param this The selector.
|
||||
#! @param a One operand.
|
||||
#! @param b The other.
|
||||
#! @returns Whether it is.
|
||||
def selector::singles_compared(&this, a: u32, b: u32): bool {
|
||||
return (this.single[a] || this.single[b]) && this.single_operand(a) && this.single_operand(b);
|
||||
}
|
||||
|
||||
#! Zeroes an SSE register by `xorps` of itself, which the processor knows reads
|
||||
#! nothing: before an instruction writing only its low lanes, as a conversion
|
||||
#! does, which would otherwise wait on what the register held last, chaining
|
||||
#! each use of a scratch register to the one before.
|
||||
#! @param this The selector.
|
||||
#! @param x The register's number.
|
||||
def selector::fresh(&mut this, x: u8): void {
|
||||
this.b.text.packed(vec_xorps, x as u32, x as u32);
|
||||
}
|
||||
|
||||
#! Widens an `f32` in one SSE register into an `f64` in another, which waits
|
||||
#! on nothing the other held: with VEX, merging the source's own upper lanes.
|
||||
#! @param this The selector.
|
||||
#! @param dst The register written.
|
||||
#! @param src The register read.
|
||||
def selector::widen_into(&mut this, dst: u8, src: u8): void {
|
||||
if this.b.text.avx {
|
||||
this.b.text.vex(ss_widen, false, dst as u32, src as u32, src as u32);
|
||||
return;
|
||||
}
|
||||
if dst != src {
|
||||
this.fresh(dst);
|
||||
}
|
||||
this.b.text.cvtss2sd(dst, src);
|
||||
}
|
||||
|
||||
#! Rounds an `f64` in one SSE register to an `f32` in another, as `widen_into`
|
||||
#! widens one.
|
||||
#! @param this The selector.
|
||||
#! @param dst The register written.
|
||||
#! @param src The register read.
|
||||
def selector::narrow_into(&mut this, dst: u8, src: u8): void {
|
||||
if this.b.text.avx {
|
||||
this.b.text.vex(sd_narrow, false, dst as u32, src as u32, src as u32);
|
||||
return;
|
||||
}
|
||||
if dst != src {
|
||||
this.fresh(dst);
|
||||
}
|
||||
this.b.text.cvtsd2ss(dst, src);
|
||||
}
|
||||
|
||||
#! A square root of the low lane of one SSE register into another, as
|
||||
#! `widen_into` converts one.
|
||||
#! @param this The selector.
|
||||
#! @param single Whether it is of an `f32`, or an `f64`.
|
||||
#! @param dst The register written.
|
||||
#! @param src The register read.
|
||||
def selector::root_into(&mut this, single: bool, dst: u8, src: u8): void {
|
||||
val op := single ? ss_sqrt : vec_sqrtsd;
|
||||
if this.b.text.avx {
|
||||
this.b.text.vex(op, false, dst as u32, src as u32, src as u32);
|
||||
return;
|
||||
}
|
||||
if dst != src {
|
||||
this.fresh(dst);
|
||||
}
|
||||
this.b.text.packed(op, dst as u32, src as u32);
|
||||
}
|
||||
|
||||
#! `ucomiss` of two `f32`s, setting the flags as `ucomisd` does for `f64`s.
|
||||
#! @param this The selector.
|
||||
#! @param x One register.
|
||||
#! @param y The other.
|
||||
def selector::compare_singles(&mut this, x: u8, y: u8): void {
|
||||
if this.b.text.avx {
|
||||
this.b.text.vex(ss_compare, false, x as u32, 0, y as u32);
|
||||
return;
|
||||
}
|
||||
this.b.text.packed(ss_compare, x as u32, y as u32);
|
||||
}
|
||||
|
||||
#! Whether the bits of an `f64` are an `f32`'s exactly.
|
||||
#! @param bits The bits.
|
||||
#! @returns Whether they are.
|
||||
def single_constant(bits: u64): bool {
|
||||
val real := ((&bits) as *f64)[0];
|
||||
return (real as f32) as f64 == real;
|
||||
}
|
||||
|
||||
#! Whether a value is the constant `0`, whose bits are the same as an `f32`'s
|
||||
#! and an `f64`'s.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @returns Whether it is.
|
||||
def selector::zero_constant(&this, value: u32): bool {
|
||||
var bits: u64 = 1;
|
||||
return this.constant_bits(value, &mut bits) && bits == 0;
|
||||
}
|
||||
|
||||
#! The SSE register an instruction reads a real from as an `f32`: its own where
|
||||
#! it is held as one in a register, and otherwise the scratch, which it is
|
||||
#! loaded or rounded into, being one exactly.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param index The instruction reading it.
|
||||
#! @param scratch The register it is put in where it is in none.
|
||||
#! @returns The register's number.
|
||||
def selector::single_reg(&mut this, value: u32, index: u32, scratch: u8): u8 {
|
||||
var bits: u64 = 0;
|
||||
if this.p.remade[value] && this.constant_bits(value, &mut bits) {
|
||||
if bits == 0 {
|
||||
this.b.text.packed(vec_xorps, scratch as u32, scratch as u32);
|
||||
return scratch;
|
||||
}
|
||||
val real := ((&bits) as *f64)[0];
|
||||
val narrow: f32 = real as f32;
|
||||
this.b.text.mov_imm(reg::rdx, ((&narrow) as *u32)[0] as u64);
|
||||
this.b.text.movq_to_xmm(scratch, reg::rdx);
|
||||
return scratch;
|
||||
}
|
||||
if !this.single[value] {
|
||||
val wide := this.real_reg(value, index, scratch);
|
||||
this.narrow_into(scratch, wide);
|
||||
return scratch;
|
||||
}
|
||||
val held := this.read(value, index);
|
||||
match held.kind {
|
||||
place::xmm => return held.number;
|
||||
place::gpr => this.b.text.movq_to_xmm(scratch, held.number as reg);
|
||||
place::none => {
|
||||
this.b.text.mov_imm(reg::rdx, 0);
|
||||
this.b.text.movq_to_xmm(scratch, reg::rdx);
|
||||
}
|
||||
else => this.b.text.movss_load(scratch, this.memory_of(&held));
|
||||
}
|
||||
return scratch;
|
||||
}
|
||||
|
||||
#! Widens a real held as an `f32` into a register, as `real_reg` gives every
|
||||
#! real.
|
||||
#! @param this The selector.
|
||||
#! @param held Where it is.
|
||||
#! @param scratch The register.
|
||||
#! @returns The register's number.
|
||||
def selector::widened(&mut this, held: &loc, scratch: u8): u8 {
|
||||
match held.kind {
|
||||
place::xmm => this.widen_into(scratch, held.number);
|
||||
place::gpr => {
|
||||
this.b.text.movq_to_xmm(scratch, held.number as reg);
|
||||
this.b.text.cvtss2sd(scratch, scratch);
|
||||
}
|
||||
place::none => {
|
||||
this.b.text.mov_imm(reg::rdx, 0);
|
||||
this.b.text.movq_to_xmm(scratch, reg::rdx);
|
||||
}
|
||||
else => {
|
||||
this.b.text.movss_load(scratch, this.memory_of(held));
|
||||
this.b.text.cvtss2sd(scratch, scratch);
|
||||
}
|
||||
}
|
||||
return scratch;
|
||||
}
|
||||
|
||||
#! The instruction of `f32`s an arithmetic of `f64`s is worked as.
|
||||
#! @param kind The arithmetic.
|
||||
#! @returns The instruction.
|
||||
def single_of(kind: sse): u32 {
|
||||
return match kind {
|
||||
sse::addsd => ss_add,
|
||||
sse::subsd => ss_sub,
|
||||
sse::mulsd => ss_mul,
|
||||
sse::divsd => ss_div,
|
||||
else => ss_sqrt
|
||||
};
|
||||
}
|
||||
|
||||
#! An arithmetic of two reals worked in single precision, into the result's
|
||||
#! register where it is held as an `f32` there, and otherwise into `xmm0`,
|
||||
#! widened where it is held as an `f64`.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param kind The arithmetic, as its `f64`s' instruction.
|
||||
#! @param a The first operand.
|
||||
#! @param b The second.
|
||||
#! @param result The result.
|
||||
#! @param commutes Whether the operands may be swapped.
|
||||
def selector::single_arithmetic(&mut this, index: u32, kind: sse, a: u32, b: u32, result: u32, commutes: bool): void {
|
||||
val instruction := single_of(kind);
|
||||
val narrow := this.single[result];
|
||||
val into := narrow ? this.real_target(result) : 0;
|
||||
if into != 0 && this.is_single_in(a, index, into) {
|
||||
this.b.text.packed(instruction, into as u32, this.single_reg(b, index, 1) as u32);
|
||||
} else if into != 0 && commutes && this.is_single_in(b, index, into) {
|
||||
this.b.text.packed(instruction, into as u32, this.single_reg(a, index, 1) as u32);
|
||||
} else {
|
||||
val second := this.single_reg(b, index, 1);
|
||||
val first := this.single_reg(a, index, 0);
|
||||
if first != 0 {
|
||||
this.b.text.movaps(0, first);
|
||||
}
|
||||
this.b.text.packed(instruction, 0, second as u32);
|
||||
if into != 0 {
|
||||
this.b.text.movaps(into, 0);
|
||||
}
|
||||
}
|
||||
this.put_single(result, into);
|
||||
}
|
||||
|
||||
#! Puts an `f32` worked in an SSE register where its result goes: as it is
|
||||
#! where it is held so, and widened otherwise.
|
||||
#! @param this The selector.
|
||||
#! @param result The result.
|
||||
#! @param x The register.
|
||||
def selector::put_single(&mut this, result: u32, x: u8): void {
|
||||
if this.single[result] {
|
||||
this.put_real(result, x);
|
||||
return;
|
||||
}
|
||||
val wide := this.real_target(result);
|
||||
this.widen_into(wide, x);
|
||||
this.put_real(result, wide);
|
||||
}
|
||||
|
||||
#! A square root worked in single precision, as `single_arithmetic` works the
|
||||
#! others.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param a The operand.
|
||||
#! @param result The result.
|
||||
def selector::single_root(&mut this, index: u32, a: u32, result: u32): void {
|
||||
val into := this.single[result] ? this.real_target(result) : 0;
|
||||
this.root_into(true, into, this.single_reg(a, index, 1));
|
||||
this.put_single(result, into);
|
||||
}
|
||||
|
||||
#! `fneg` worked on an `f32`: its sign bit flipped by `xorps`.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param a The operand.
|
||||
#! @param result The result.
|
||||
def selector::single_negation(&mut this, index: u32, a: u32, result: u32): void {
|
||||
val into := this.single[result] ? this.real_target(result) : 0;
|
||||
val got := this.single_reg(a, index, into);
|
||||
this.b.text.mov_imm(reg::rax, 0x80000000);
|
||||
this.b.text.movq_to_xmm(1, reg::rax);
|
||||
if got != into {
|
||||
this.b.text.movaps(into, got);
|
||||
}
|
||||
this.b.text.packed(vec_xorps, into as u32, 1);
|
||||
this.put_single(result, into);
|
||||
}
|
||||
|
||||
#! `fsingle` of a real: a copy of one held as an `f32` already, and otherwise
|
||||
#! the real rounded; where the result is held as an `f64`, the rounded real
|
||||
#! widened again.
|
||||
#! @param this The selector.
|
||||
#! @param index The instruction.
|
||||
#! @param a The operand.
|
||||
#! @param result The result.
|
||||
def selector::round_single(&mut this, index: u32, a: u32, result: u32): void {
|
||||
if this.single[result] {
|
||||
val into := this.real_target(result);
|
||||
val got := this.single_reg(a, index, into);
|
||||
if got != into {
|
||||
this.b.text.movaps(into, got);
|
||||
}
|
||||
this.put_real(result, into);
|
||||
return;
|
||||
}
|
||||
val into := this.real_target(result);
|
||||
if this.single[a] {
|
||||
this.widen_into(into, this.single_reg(a, index, 0));
|
||||
} else {
|
||||
this.narrow_into(0, this.real_reg(a, index, 0));
|
||||
this.widen_into(into, 0);
|
||||
}
|
||||
this.put_real(result, into);
|
||||
}
|
||||
|
||||
#! `select` of reals by a comparison of reals folded into it, with no branch:
|
||||
#! where it chooses the lesser or the greater of what it compares, as `a < b ? b
|
||||
#! : a` does, by `maxss` or `minss`, which give their second operand where
|
||||
#! either is not a number, as the comparison does; otherwise by a mask of
|
||||
#! `cmpss`, all ones where the comparison holds, choosing each by `and`, `andn`,
|
||||
#! and `or`. It works in single precision where both it chooses between are
|
||||
#! `f32`s, and otherwise in double.
|
||||
#! @param this The selector.
|
||||
#! @param index The `select`.
|
||||
#! @param compare The comparison, `flt` or `fle`.
|
||||
#! @param result The result.
|
||||
def selector::choose_real(&mut this, index: u32, compare: u32, result: u32): void {
|
||||
val single := this.works[index];
|
||||
val x := this.body.arg(compare, 0);
|
||||
val y := this.body.arg(compare, 1);
|
||||
val a := this.body.arg(index, 1);
|
||||
val b := this.body.arg(index, 2);
|
||||
var into := single && !this.single[result] ? 0 : this.real_target(result);
|
||||
val lt := this.body.insts[compare].kind == op::flt;
|
||||
|
||||
# `x < y ? y : x` is the greater, `maxss` of `y` and `x`, and `x < y ? x : y`
|
||||
# the lesser, `minss` of `x` and `y`: each gives its first where the
|
||||
# comparison holds, and its second otherwise.
|
||||
if lt && ((a == y && b == x) || (a == x && b == y)) {
|
||||
val greater := a == y;
|
||||
val second := single ? this.single_reg(b, index, 1) : this.real_reg(b, index, 1);
|
||||
# The second is read after the first is put where the result goes, so not
|
||||
# there.
|
||||
if second == into {
|
||||
into = 0;
|
||||
}
|
||||
val first := single ? this.single_reg(a, index, into) : this.real_reg(a, index, into);
|
||||
if first != into {
|
||||
this.b.text.movaps(into, first);
|
||||
}
|
||||
val op := single ? (greater ? ss_max : ss_min) : (greater ? sd_max : sd_min);
|
||||
this.b.text.packed(op, into as u32, second as u32);
|
||||
if single {
|
||||
this.put_single(result, into);
|
||||
} else {
|
||||
this.put_real(result, into);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
# The mask, in `xmm1`, of `f32`s where both compared are, and of `f64`s
|
||||
# otherwise, but where an `f64` is chosen, whose mask is 8 bytes.
|
||||
val narrow := single && this.single_operand(x) && this.single_operand(y);
|
||||
val left := narrow ? this.single_reg(x, index, 1) : this.real_reg(x, index, 1);
|
||||
if left != 1 {
|
||||
this.b.text.movaps(1, left);
|
||||
}
|
||||
val right := narrow ? this.single_reg(y, index, 2) : this.real_reg(y, index, 2);
|
||||
this.b.text.packed_imm(narrow ? ss_cmp : sd_cmp, 1, right as u32, lt ? predicate_lt : predicate_le);
|
||||
val chosen := single ? this.single_reg(a, index, 0) : this.real_reg(a, index, 0);
|
||||
if chosen != 0 {
|
||||
this.b.text.movaps(0, chosen);
|
||||
}
|
||||
this.b.text.packed(vec_andps, 0, 1);
|
||||
val other := single ? this.single_reg(b, index, 2) : this.real_reg(b, index, 2);
|
||||
this.b.text.packed(vec_andnps, 1, other as u32);
|
||||
this.b.text.packed(vec_orps, 0, 1);
|
||||
if single {
|
||||
this.put_single(result, 0);
|
||||
} else {
|
||||
this.put_real(result, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether a value an instruction reads is held as an `f32` in an SSE register.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param index The instruction reading it.
|
||||
#! @param x The register's number.
|
||||
#! @returns Whether it is.
|
||||
def selector::is_single_in(&this, value: u32, index: u32, x: u8): bool {
|
||||
return false if !this.single[value];
|
||||
val held := this.read(value, index);
|
||||
return held.kind == place::xmm && held.number == x;
|
||||
}
|
||||
@@ -0,0 +1,466 @@
|
||||
#! @file variables.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains where the variables debugging information shows are in a
|
||||
#! function's code once it is selected. A variable held in memory is where its
|
||||
#! cell, or its copy of a parameter, is in the frame, over the code its scope
|
||||
#! covers. One held in values, as optimized code holds a local of words, is
|
||||
#! where the values it took are: each binding takes effect where it was made,
|
||||
#! or where its block begins where optimizing took that out; a block begins
|
||||
#! with what every block before it ended with where they agree, or with the
|
||||
#! parameter they each give that; and where each value is at each instruction
|
||||
#! is where the registers' placing put it, a register or its slot, while it
|
||||
#! lives, or a constant, which is known anywhere, as is one a binding keeps
|
||||
#! once its instruction went.
|
||||
|
||||
import sakura::compiler;
|
||||
import binary::amd64;
|
||||
module sakura::native;
|
||||
|
||||
|
||||
#! The most words a variable held in values has.
|
||||
const held_words: u64 = 3;
|
||||
|
||||
#! Notes where each variable of the function is, as the file says.
|
||||
#! @param this The selector.
|
||||
#! @param spans Where each instruction's code begins, then ends.
|
||||
def selector::place_variables(&mut this, spans: span<u32>): void {
|
||||
val size := (this.b.text.length - this.start) as u32;
|
||||
var held := false;
|
||||
for v in 0..this.body.variables.length {
|
||||
val each: variable_info = this.body.variables[v];
|
||||
if each.held {
|
||||
held = true;
|
||||
continue;
|
||||
}
|
||||
# One whose cell went, as optimizing takes out a cell nothing reads, is
|
||||
# nowhere.
|
||||
var below: u64 = 0;
|
||||
if each.cell != 0 {
|
||||
below = this.cells[each.cell as u64 - 1];
|
||||
} else if each.given != 0 {
|
||||
below = this.copies[each.given as u64 - 1];
|
||||
}
|
||||
continue if below == 0;
|
||||
var from: u32 = 0;
|
||||
var to: u32 = size;
|
||||
if !each.param && (each.first != 0 || each.end != 0) {
|
||||
from = size;
|
||||
to = 0;
|
||||
val end := each.end != 0 ? each.end : this.body.insts.length as u32;
|
||||
for i in each.first..end {
|
||||
break if 2 * i as u64 + 1 >= spans.length;
|
||||
val begins := spans[2 * i as u64];
|
||||
val ends := spans[2 * i as u64 + 1];
|
||||
continue if ends <= begins;
|
||||
from = begins < from ? begins : from;
|
||||
to = ends > to ? ends : to;
|
||||
}
|
||||
}
|
||||
# The frame's canonical address is past the saved `rbp` and the return
|
||||
# address.
|
||||
this.b.debug.variables.append(placed_variable {
|
||||
name = each.name,
|
||||
at = each.at,
|
||||
layout = each.layout,
|
||||
offset = -(below as i64) - 16,
|
||||
param = each.param,
|
||||
boxed = each.boxed,
|
||||
site = each.site,
|
||||
from = from,
|
||||
to = to
|
||||
});
|
||||
}
|
||||
if held {
|
||||
this.place_held(spans);
|
||||
}
|
||||
}
|
||||
|
||||
#! Where a value is while an instruction runs, as a word of a variable: where
|
||||
#! a call is, where it is past the call, as what lives across a call is in a
|
||||
#! register the call keeps, which the frames below say where they saved, or in
|
||||
#! its slot.
|
||||
#! @param this The selector.
|
||||
#! @param value The value.
|
||||
#! @param index The instruction.
|
||||
#! @param kind Where the kind of place is written.
|
||||
#! @returns The register, the offset from the frame's canonical address, or the
|
||||
#! bits.
|
||||
def selector::word_spot_of(&this, value: u32, index: u32, kind: &mut u8): u64 {
|
||||
kind = word_spot::nowhere as u8;
|
||||
|
||||
# A constant is known, made where it is read or not at all, as is one a
|
||||
# binding keeps, which the binding, numbered past the values, stands for.
|
||||
val values := this.body.origins.length;
|
||||
if value as u64 >= values {
|
||||
val bound: binding = this.body.bindings[value as u64 - values];
|
||||
kind = (bound.known == vt::real ? word_spot::real : word_spot::word) as u8;
|
||||
return bound.bits;
|
||||
}
|
||||
val maker := this.body.defining(value);
|
||||
if maker != 0 {
|
||||
val made: inst = this.body.insts[maker];
|
||||
if made.kind == op::iconst {
|
||||
kind = word_spot::word as u8;
|
||||
return made.imm;
|
||||
}
|
||||
if made.kind == op::fconst {
|
||||
kind = word_spot::real as u8;
|
||||
return made.imm;
|
||||
}
|
||||
}
|
||||
|
||||
# Elsewhere, it is where its part there is, and nowhere outside its life.
|
||||
val position := 4 * this.p.slots[index] + (calls(this.body.insts[index].kind) ? 3 : 1);
|
||||
val first: u32 = this.p.child_first[value];
|
||||
val past: u32 = this.p.child_first[value + 1];
|
||||
var part: u32 = past;
|
||||
for c in first..past {
|
||||
if this.p.child_starts[c as u64] <= position && position < this.p.child_ends[c as u64] {
|
||||
part = c;
|
||||
}
|
||||
}
|
||||
return 0 if part == past;
|
||||
val held: loc = this.p.child_locs[part as u64];
|
||||
match held.kind {
|
||||
place::gpr => {
|
||||
kind = word_spot::gpr as u8;
|
||||
return held.number as u64;
|
||||
}
|
||||
place::xmm => {
|
||||
kind = word_spot::xmm as u8;
|
||||
return held.number as u64;
|
||||
}
|
||||
place::slot => {
|
||||
kind = word_spot::frame as u8;
|
||||
return (this.slot(value).disp as i64 - 16) as u64;
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Notes the ranges of code each variable held in values is somewhere in, and
|
||||
#! where each of its words is over each.
|
||||
#! @param this The selector.
|
||||
#! @param spans Where each instruction's code begins, then ends.
|
||||
def selector::place_held(&mut this, spans: span<u32>): void {
|
||||
val body := &this.body;
|
||||
val program := &this.b.c.program;
|
||||
|
||||
# Each word followed: those of each variable held in values, by slot.
|
||||
var slot_first := vector::<u32>::new(scratch());
|
||||
var words := vector::<u32>::new(scratch());
|
||||
var slots: u32 = 0;
|
||||
for v in 0..body.variables.length {
|
||||
val each: variable_info = body.variables[v];
|
||||
var count: u64 = 0;
|
||||
if each.held {
|
||||
count = each.boxed ? 1 : word_count(&program.layouts[each.layout]);
|
||||
}
|
||||
if count > held_words {
|
||||
count = 0;
|
||||
}
|
||||
slot_first.append(slots);
|
||||
words.append(count as u32);
|
||||
slots += count as u32;
|
||||
}
|
||||
return if slots == 0;
|
||||
|
||||
# Of each `f32` held in values, whether it is described as one: where more of
|
||||
# the values bound to it are held as `f32`s than as `f64`s, as `singles.sakura`
|
||||
# says. Where it is held the other way, it is described as nowhere.
|
||||
var narrow := vector::<bool>::new(scratch());
|
||||
var floats := vector::<bool>::new(scratch());
|
||||
var votes := vector::<i64>::new(scratch());
|
||||
narrow.resize(body.variables.length);
|
||||
floats.resize(body.variables.length);
|
||||
votes.resize(body.variables.length);
|
||||
for v in 0..body.variables.length {
|
||||
val each: variable_info = body.variables[v];
|
||||
val shape: layout = program.layouts[each.layout];
|
||||
floats[v] = each.held && !each.boxed && shape.kind == lk::floating && shape.size == 4;
|
||||
}
|
||||
for &bound in body.bindings {
|
||||
continue if bound.variable as u64 >= floats.length || !floats[bound.variable as u64];
|
||||
continue if bound.value == 0 || bound.value as u64 >= body.origins.length || this.p.remade[bound.value];
|
||||
votes[bound.variable as u64] = votes[bound.variable as u64] + (this.single[bound.value] ? 1 : -1);
|
||||
}
|
||||
for v in 0..body.variables.length {
|
||||
narrow[v] = floats[v] && votes[v] > 0;
|
||||
}
|
||||
|
||||
# What binds each slot, where: after the instruction it was bound after,
|
||||
# where that is still in the code, or else where the block it was bound in
|
||||
# begins, in the order bound.
|
||||
var placed := vector::<bool>::new(scratch());
|
||||
placed.resize(body.insts.length);
|
||||
for k in 0..this.p.order.length {
|
||||
var walk := body.walk(this.p.order[k]);
|
||||
while walk.more() {
|
||||
placed[walk.next()] = true;
|
||||
}
|
||||
}
|
||||
var at_inst := vector::<u32>::new(scratch());
|
||||
at_inst.resize(body.insts.length);
|
||||
var at_block := vector::<u32>::new(scratch());
|
||||
at_block.resize(body.blocks.length);
|
||||
var after := vector::<u32>::new(scratch());
|
||||
after.resize(body.bindings.length);
|
||||
var b := body.bindings.length;
|
||||
while b > 0 {
|
||||
b--;
|
||||
val bound: binding = body.bindings[b];
|
||||
continue if bound.variable as u64 >= words.length || bound.word >= words[bound.variable as u64];
|
||||
continue if (bound.value == 0 && bound.known == vt::none) || bound.value as u64 >= body.origins.length;
|
||||
if bound.after != 0 && placed[bound.after] {
|
||||
after[b] = at_inst[bound.after];
|
||||
at_inst[bound.after] = b as u32 + 1;
|
||||
continue;
|
||||
}
|
||||
var block := bound.block;
|
||||
if block == 0xFFFFFFFF || body.blocks[block].gone || this.p.number[block] == never {
|
||||
block = bound.after != 0 ? body.insts[bound.after].block : 0xFFFFFFFF;
|
||||
}
|
||||
continue if block == 0xFFFFFFFF || body.blocks[block].gone || this.p.number[block] == never;
|
||||
after[b] = at_block[block];
|
||||
at_block[block] = b as u32 + 1;
|
||||
}
|
||||
|
||||
# What each block begins with, by slot: what every block laid out before it
|
||||
# ended with, where they agree, and nothing otherwise, until none changes.
|
||||
# `never` stands for a block not yet worked out.
|
||||
val blocks := body.blocks.length;
|
||||
var begins := vector::<u32>::new(scratch());
|
||||
var ends := vector::<u32>::new(scratch());
|
||||
begins.resize(blocks * slots as u64);
|
||||
ends.resize(blocks * slots as u64);
|
||||
for i in 0..blocks * slots as u64 {
|
||||
ends[i] = never;
|
||||
}
|
||||
var state := vector::<u32>::new(scratch());
|
||||
state.resize(slots as u64);
|
||||
var split := vector::<bool>::new(scratch());
|
||||
split.resize(slots as u64);
|
||||
var changed := true;
|
||||
var rounds := 0u;
|
||||
while changed && rounds < 32 {
|
||||
changed = false;
|
||||
rounds++;
|
||||
for k in 0..this.p.order.length {
|
||||
val block: u32 = this.p.order[k];
|
||||
for s in 0..slots as u64 {
|
||||
state[s] = block == 0 ? 0 : never;
|
||||
split[s] = false;
|
||||
}
|
||||
for &edge in body.blocks[block].preds.span {
|
||||
val from := body.edge_source(edge);
|
||||
continue if this.p.number[from] == never;
|
||||
for s in 0..slots as u64 {
|
||||
val given: u32 = ends[from as u64 * slots as u64 + s];
|
||||
continue if given == never;
|
||||
val now: u32 = state[s];
|
||||
if now != never && now != given {
|
||||
split[s] = true;
|
||||
}
|
||||
state[s] = now == never || now == given ? given : 0;
|
||||
}
|
||||
}
|
||||
# Where the blocks before differ, the variable is the parameter each
|
||||
# gives what it ended with, where one is.
|
||||
for s in 0..slots as u64 {
|
||||
continue if !split[s];
|
||||
val params := &body.blocks[block].params;
|
||||
for i in 0..params.length {
|
||||
var all := true;
|
||||
for &edge in body.blocks[block].preds.span {
|
||||
val from := body.edge_source(edge);
|
||||
continue if this.p.number[from] == never;
|
||||
val given: u32 = ends[from as u64 * slots as u64 + s];
|
||||
all = all && (given == never || body.edges[edge].args[i] == given);
|
||||
}
|
||||
if all {
|
||||
state[s] = params[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for s in 0..slots as u64 {
|
||||
if state[s] == never {
|
||||
state[s] = 0;
|
||||
}
|
||||
begins[block as u64 * slots as u64 + s] = state[s];
|
||||
}
|
||||
this.apply_bindings(at_block[block], &after, &slot_first, &mut state);
|
||||
var walk := body.walk(block);
|
||||
while walk.more() {
|
||||
this.apply_bindings(at_inst[walk.next()], &after, &slot_first, &mut state);
|
||||
}
|
||||
for s in 0..slots as u64 {
|
||||
val index := block as u64 * slots as u64 + s;
|
||||
if ends[index] != state[s] {
|
||||
ends[index] = state[s];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# The ranges: each instruction where a variable's words are somewhere other
|
||||
# than before begins one.
|
||||
val variables := body.variables.length;
|
||||
var open := vector::<located_range>::new(scratch());
|
||||
open.resize(variables);
|
||||
var opened := vector::<bool>::new(scratch());
|
||||
opened.resize(variables);
|
||||
var found := vector::<vector<located_range>>::new(scratch());
|
||||
for _ in 0..variables {
|
||||
found.append(vector::<located_range>::new(scratch()));
|
||||
}
|
||||
# The stubs of each instruction, written after the body, as a list through
|
||||
# `stub_after`.
|
||||
val stubs := this.stub_spans.length / 3;
|
||||
var stub_head := vector::<u32>::new(scratch());
|
||||
var stub_after := vector::<u32>::new(scratch());
|
||||
stub_after.resize(stubs);
|
||||
if stubs != 0 {
|
||||
stub_head.resize(body.insts.length);
|
||||
}
|
||||
for t in 0..stubs {
|
||||
val index: u32 = this.stub_spans[3 * t + 2];
|
||||
stub_after[t] = stub_head[index];
|
||||
stub_head[index] = t as u32 + 1;
|
||||
}
|
||||
|
||||
var last_end: u32 = 0;
|
||||
for k in 0..this.p.order.length {
|
||||
val block: u32 = this.p.order[k];
|
||||
for s in 0..slots as u64 {
|
||||
state[s] = begins[block as u64 * slots as u64 + s];
|
||||
}
|
||||
this.apply_bindings(at_block[block], &after, &slot_first, &mut state);
|
||||
var walk := body.walk(block);
|
||||
while walk.more() {
|
||||
val at := walk.next();
|
||||
val from := spans[2 * at as u64];
|
||||
val to := spans[2 * at as u64 + 1];
|
||||
if to > from {
|
||||
for v in 0..variables {
|
||||
continue if words[v] == 0;
|
||||
var here := located_range { from = from, to = to };
|
||||
var known := false;
|
||||
for w in 0..words[v] as u64 {
|
||||
val value: u32 = state[slot_first[v] as u64 + w];
|
||||
continue if value == 0;
|
||||
var kind: u8 = 0;
|
||||
here.spots[w] = this.word_spot_of(value, at, &mut kind);
|
||||
if floats[v] && (kind == word_spot::real as u8 ? narrow[v] : this.single[value] != narrow[v]) {
|
||||
kind = word_spot::nowhere as u8;
|
||||
}
|
||||
here.kinds[w] = kind;
|
||||
known = known || kind != word_spot::nowhere as u8;
|
||||
}
|
||||
val same := opened[v] && same_spots(&open[v], &here);
|
||||
continue if same;
|
||||
if opened[v] {
|
||||
open[v].to = from;
|
||||
if open[v].to > open[v].from {
|
||||
found[v].append(open[v]);
|
||||
}
|
||||
}
|
||||
opened[v] = known;
|
||||
open[v] = here;
|
||||
}
|
||||
last_end = to > last_end ? to : last_end;
|
||||
}
|
||||
|
||||
# A stub of the instruction has the variables where the instruction does.
|
||||
var t: u32 = stubs != 0 ? stub_head[at] : 0;
|
||||
while t != 0 {
|
||||
val stub_from: u32 = this.stub_spans[3 * (t as u64 - 1)];
|
||||
val stub_to: u32 = this.stub_spans[3 * (t as u64 - 1) + 1];
|
||||
for v in 0..variables {
|
||||
continue if words[v] == 0;
|
||||
var here := located_range { from = stub_from, to = stub_to };
|
||||
var known := false;
|
||||
for w in 0..words[v] as u64 {
|
||||
val value: u32 = state[slot_first[v] as u64 + w];
|
||||
continue if value == 0;
|
||||
var kind: u8 = 0;
|
||||
here.spots[w] = this.word_spot_of(value, at, &mut kind);
|
||||
if floats[v] && (kind == word_spot::real as u8 ? narrow[v] : this.single[value] != narrow[v]) {
|
||||
kind = word_spot::nowhere as u8;
|
||||
}
|
||||
here.kinds[w] = kind;
|
||||
known = known || kind != word_spot::nowhere as u8;
|
||||
}
|
||||
if known && stub_to > stub_from {
|
||||
found[v].append(here);
|
||||
}
|
||||
}
|
||||
t = stub_after[t as u64 - 1];
|
||||
}
|
||||
this.apply_bindings(at_inst[at], &after, &slot_first, &mut state);
|
||||
}
|
||||
}
|
||||
|
||||
for v in 0..variables {
|
||||
continue if words[v] == 0;
|
||||
if opened[v] {
|
||||
open[v].to = last_end;
|
||||
if open[v].to > open[v].from {
|
||||
found[v].append(open[v]);
|
||||
}
|
||||
}
|
||||
continue if found[v].length == 0;
|
||||
val each: variable_info = body.variables[v];
|
||||
val first := this.b.debug.ranges.length as u32;
|
||||
for &range in found[v] {
|
||||
this.b.debug.ranges.append(range);
|
||||
}
|
||||
this.b.debug.variables.append(placed_variable {
|
||||
name = each.name,
|
||||
at = each.at,
|
||||
layout = each.layout,
|
||||
param = each.param,
|
||||
boxed = each.boxed,
|
||||
held = true,
|
||||
narrow = narrow[v],
|
||||
site = each.site,
|
||||
ranges = first,
|
||||
count = found[v].length as u32
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#! Gives each slot the value each binding of a list gives it, in order. One
|
||||
#! keeping a constant gives what stands for it: the binding, numbered past the
|
||||
#! values.
|
||||
#! @param this The selector.
|
||||
#! @param first One more than the first binding, or 0.
|
||||
#! @param after One more than the binding after each, or 0.
|
||||
#! @param slot_first The first slot of each variable.
|
||||
#! @param state The value each slot holds.
|
||||
def selector::apply_bindings(&this, first: u32, after: &vector<u32>, slot_first: &vector<u32>, state: &mut vector<u32>): void {
|
||||
val values := this.body.origins.length;
|
||||
var b := first;
|
||||
while b != 0 {
|
||||
val bound: binding = this.body.bindings[b as u64 - 1];
|
||||
val given := bound.known != vt::none ? (values + b as u64 - 1) as u32 : bound.value;
|
||||
state[slot_first[bound.variable as u64] as u64 + bound.word as u64] = given;
|
||||
b = after[b as u64 - 1];
|
||||
}
|
||||
}
|
||||
|
||||
#! Whether two ranges say each word is in the same place.
|
||||
#! @param a The one.
|
||||
#! @param b The other.
|
||||
#! @returns Whether they do.
|
||||
def same_spots(a: &located_range, b: &located_range): bool {
|
||||
for w in 0..held_words {
|
||||
return false if a.kinds[w] != b.kinds[w] || a.spots[w] != b.spots[w];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user