1193 lines
46 KiB
Plaintext
1193 lines
46 KiB
Plaintext
#! @file calls.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the code of calls, of the operations the runtime's helpers do, and
|
|
#! of the ends of blocks, and selects the code of a whole function: its
|
|
#! prologue, its blocks in the order the register allocator laid them out, and
|
|
#! the stubs reporting the checks that fail.
|
|
#!
|
|
#! A call passes the first words of its arguments in the registers `gprs`
|
|
#! names, `arg_count` of them, in order, and the rest on the stack, the first
|
|
#! at the bottom of the caller's frame; a parameter held in memory is passed as
|
|
#! the address of the caller's copy, and the function called copies it. A
|
|
#! function returning memory is given the address to write it to in `rdx`.
|
|
#! Words come back in `rax`, `rdx`, and `rcx`. A call keeps `rbx` and `r12` to
|
|
#! `r15`, which a function saves as it begins where it uses them, and gives
|
|
#! back before it returns. A call through a vtable passes the vtable as the
|
|
#! third word, which a method taking a plain reference never sees, since its
|
|
#! vtable's slot names code moving the words after it down one.
|
|
|
|
import sakura::compiler;
|
|
import binary::amd64;
|
|
module sakura::native;
|
|
|
|
|
|
# Calls.
|
|
|
|
#! Whether a signature's result is returned in memory, which a call writes to an
|
|
#! address its last operand gives.
|
|
#! @param this The selector.
|
|
#! @param signature The signature.
|
|
#! @returns Whether it is.
|
|
def selector::returns_memory(&this, signature: u32): bool {
|
|
val result: u32 = this.b.c.program.signatures[signature].result;
|
|
val shape: layout = this.b.c.program.layouts[result];
|
|
return returned_in_memory(&shape);
|
|
}
|
|
|
|
#! The place a word of a call's arguments is passed in.
|
|
#! @param k The word.
|
|
#! @returns The place.
|
|
def argument_place(k: u64): loc {
|
|
if k < arg_count {
|
|
return in_gpr(gprs[k]);
|
|
}
|
|
return loc { kind = place::out, value = (k - arg_count) as u32 };
|
|
}
|
|
|
|
#! How many words a call passes, and how many of them on the stack.
|
|
#! @param this The selector.
|
|
#! @param index The call.
|
|
#! @returns How many on the stack.
|
|
def selector::stacked_words(&this, index: u32): u64 {
|
|
val held: inst = this.body.insts[index];
|
|
var words: u64 = held.argc as u64;
|
|
match held.kind {
|
|
op::call => {
|
|
val signature: u32 = this.b.c.program.functions[held.aux].signature;
|
|
if this.returns_memory(signature) {
|
|
words--;
|
|
}
|
|
}
|
|
op::call_trait, op::call_value => {
|
|
if this.returns_memory(held.aux) {
|
|
words--;
|
|
}
|
|
if held.kind == op::call_value {
|
|
words--;
|
|
}
|
|
}
|
|
else => return 0;
|
|
}
|
|
return words > arg_count ? words - arg_count : 0;
|
|
}
|
|
|
|
#! The code of a call of a function of the program: directly, through a
|
|
#! vtable, or through a function value.
|
|
#! @param this The selector.
|
|
#! @param index The call.
|
|
#! @param held The call.
|
|
def selector::call(&mut this, index: u32, held: &inst): void {
|
|
var signature: u32 = held.aux;
|
|
if held.kind == op::call {
|
|
signature = this.b.c.program.functions[held.aux].signature;
|
|
}
|
|
val memory := this.returns_memory(signature);
|
|
var given := held.argc as u64;
|
|
if memory {
|
|
given--;
|
|
}
|
|
val tail := this.tail_call(index, held, memory);
|
|
|
|
# The address called, found before any argument is moved, and kept in the
|
|
# frame's scratch.
|
|
# A program that checks compares the index with the table's size first, so a
|
|
# function value or vtable made from memory written by hand stops it.
|
|
if held.kind == op::call_value {
|
|
this.load_into(this.body.arg(index, 0), index, reg::rax);
|
|
if this.checking() {
|
|
this.b.text.alu_imm(alu::sub, reg::rax, 1);
|
|
this.b.text.mov_imm(reg::rcx, this.b.c.program.functions.length);
|
|
this.b.text.alu(alu::cmp, reg::rax, reg::rcx);
|
|
this.trap_if(cc::above_equal, fault::null_reference as u32, index);
|
|
this.b.text.load(8, false, reg::rax, indexed(reg::none, reg::rax, 8, this.b.function_table as i32));
|
|
} else {
|
|
this.b.text.load(8, false, reg::rax, indexed(reg::none, reg::rax, 8, this.b.function_table as i32 - 8));
|
|
}
|
|
this.b.text.store(8, this.scratch_word(2), reg::rax);
|
|
} else if held.kind == op::call_trait {
|
|
this.load_into(this.body.arg(index, 1), index, reg::rax);
|
|
if this.checking() {
|
|
this.b.text.mov_imm(reg::rcx, this.b.c.program.vtables.length);
|
|
this.b.text.alu(alu::cmp, reg::rax, reg::rcx);
|
|
this.trap_if(cc::above_equal, fault::null_reference as u32, index);
|
|
}
|
|
this.b.text.load(8, false, reg::rax, indexed(reg::none, reg::rax, 8, this.b.vtable_table as i32));
|
|
this.b.text.load(8, false, reg::rax, at(reg::rax, (8 * held.imm) as i32));
|
|
this.b.text.store(8, this.scratch_word(2), reg::rax);
|
|
}
|
|
|
|
# The words: a function value's environment comes after the rest.
|
|
var moves := vector::<transfer>::new(scratch());
|
|
if held.kind == op::call_value {
|
|
for a in 2..given {
|
|
moves.append(transfer { from = this.read(this.body.arg(index, a as u32), index), to = argument_place(a - 2) });
|
|
}
|
|
moves.append(transfer { from = this.read(this.body.arg(index, 1), index), to = argument_place(given - 2) });
|
|
} else {
|
|
for a in 0..given {
|
|
moves.append(transfer { from = this.read(this.body.arg(index, a as u32), index), to = argument_place(a) });
|
|
}
|
|
}
|
|
if memory && !tail {
|
|
moves.append(transfer { from = this.read(this.body.arg(index, held.argc - 1), index), to = in_gpr(reg::rdx) });
|
|
}
|
|
this.parallel(moves.view());
|
|
if tail {
|
|
this.tail_jump(held, memory);
|
|
return;
|
|
}
|
|
|
|
if held.kind == op::call {
|
|
this.b.call_to(held.aux);
|
|
} else {
|
|
this.b.text.call_mem(this.scratch_word(2));
|
|
}
|
|
this.results(held);
|
|
}
|
|
|
|
#! Whether a call is made as a jump, once the frame is left, so the function
|
|
#! called returns to this one's caller: where the return straight after it
|
|
#! returns just what it gives, its words are all passed in registers, and none
|
|
#! of them points into the frame, which is gone by the time the function
|
|
#! jumped to runs. A result in memory is written straight to where this
|
|
#! function's caller asked for its own, which the return would copy it to.
|
|
#! @param this The selector.
|
|
#! @param index The call.
|
|
#! @param held The call.
|
|
#! @param memory Whether its result is returned in memory.
|
|
#! @returns Whether it is.
|
|
def selector::tail_call(&this, index: u32, held: &inst, memory: bool): bool {
|
|
return false if !this.tails || held.next == 0 || this.stacked_words(index) != 0;
|
|
val after: inst = this.body.insts[held.next];
|
|
return false if after.kind != op::ret;
|
|
var passed := held.argc;
|
|
if memory {
|
|
var signature: u32 = held.aux;
|
|
if held.kind == op::call {
|
|
signature = this.b.c.program.functions[held.aux].signature;
|
|
}
|
|
val result: u32 = this.b.c.program.signatures[signature].result;
|
|
return false if this.returned == 0 || this.b.c.program.layouts[result].size != this.returned;
|
|
return false if held.count != 0 || after.argc != 1 || this.body.arg(held.next, 0) != this.body.arg(index, held.argc - 1);
|
|
passed--;
|
|
} else {
|
|
return false if this.returned != 0 || after.argc != held.count as u32;
|
|
for r in 0..held.count as u32 {
|
|
return false if this.body.arg(held.next, r) != held.result + r;
|
|
}
|
|
}
|
|
for a in 0..passed {
|
|
return false if this.framed[this.body.arg(index, a)];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#! Leaves the frame and jumps to the function a call calls, its words passed:
|
|
#! given the address this function's caller asked for a result in memory at,
|
|
#! and through the address a call through a vtable or a function value found,
|
|
#! which `rax` holds while the frame is left.
|
|
#! @param this The selector.
|
|
#! @param held The call.
|
|
#! @param memory Whether its result is returned in memory.
|
|
def selector::tail_jump(&mut this, held: &inst, memory: bool): void {
|
|
if memory {
|
|
this.b.text.load(8, false, reg::rdx, at(reg::rbp, -(this.dst_at as i64) as i32));
|
|
}
|
|
if held.kind == op::call {
|
|
this.epilogue(5);
|
|
this.b.jump_to_function(held.aux);
|
|
} else {
|
|
this.b.text.load(8, false, reg::rax, this.scratch_word(2));
|
|
this.epilogue(2);
|
|
this.b.text.jmp_reg(reg::rax);
|
|
}
|
|
this.tailed = held.next;
|
|
}
|
|
|
|
#! Whether no pointer into the frame is kept anywhere a function jumped to, its
|
|
#! frame gone, could find it, as far as the code says, and so which values the
|
|
#! words a jump passes must not be: following which may point into the frame,
|
|
#! the addresses of cells and of parameters held in memory and what is
|
|
#! computed from them, and the parameters edges give them as, none is written
|
|
#! to memory, given to C, or given to a call as anything it might keep. A call
|
|
#! is lent them only as references, which a function never keeps, as its
|
|
#! referent would be on the heap rather than in a cell, as memory it only
|
|
#! copies or writes its result to, or as the environment of a function value
|
|
#! it calls, and what it gives may point into the frame too. The function
|
|
#! never reads `frame`.
|
|
#! @param this The selector.
|
|
#! @returns Whether it is.
|
|
def selector::frame_held(&mut this): bool {
|
|
val values := this.body.origins.length;
|
|
this.framed.resize(values);
|
|
for v in 0..values {
|
|
var below: u64 = 0;
|
|
this.framed[v] = this.p.remade[v] && this.frame_address(v as u32, &mut below);
|
|
}
|
|
var reads := this.body.reader();
|
|
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];
|
|
return false if held.kind == op::frame;
|
|
var gives := false;
|
|
reads.start(at);
|
|
while reads.more() {
|
|
val value := reads.next();
|
|
val a := reads.operand();
|
|
continue if !this.framed[value] || a == no_operand;
|
|
val use := this.framed_use(at, &held, a);
|
|
return false if use == 2;
|
|
gives = gives || use == 1;
|
|
}
|
|
if gives {
|
|
for r in 0..held.count as u32 {
|
|
if !this.framed[held.result + r] {
|
|
this.framed[held.result + r] = true;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
for e in 0..this.body.edge_count(at) {
|
|
val edge := held.aux + e;
|
|
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 this.framed[args[i]] && !this.framed[param] {
|
|
this.framed[param] = true;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#! What an instruction does with an operand that may point into the frame: 0
|
|
#! where it only reads or writes through it, copies to or from it, clears it,
|
|
#! tests it, compares it, or formats what it points at, 1 where what it gives
|
|
#! may point into the frame too, as arithmetic's and a call's lent it does, and
|
|
#! 2 where it may keep it where the code cannot follow it.
|
|
#! @param this The selector.
|
|
#! @param index The instruction.
|
|
#! @param held The instruction.
|
|
#! @param a The operand.
|
|
#! @returns What.
|
|
def selector::framed_use(&this, index: u32, held: &inst, a: u32): u8 {
|
|
return match held.kind {
|
|
op::load, op::clear, op::copy, op::check, op::branch, op::switch, op::equal, op::format_value => 0,
|
|
op::store, op::atomic => a == 0 ? 0 : 2,
|
|
op::ret => this.returned != 0 ? 0 : 2,
|
|
op::eq, op::ne, op::lts, op::ltu, op::les, op::leu, op::eqsu, op::ltsu, op::lesu, op::ltus, op::leus, op::lnot => 0,
|
|
op::add, op::sub, op::band, op::bor, op::bxor, op::mul, op::shl, op::shrs, op::shru, op::neg, op::bnot, op::wrap => 1,
|
|
op::call, op::call_trait, op::call_value => this.lent_operand(index, a) ? 1 : 2,
|
|
else => 2
|
|
};
|
|
}
|
|
|
|
#! Whether an operand of a call is lent to the function called, which keeps no
|
|
#! pointer to what it points at: a word of a reference, which a function never
|
|
#! keeps, as its referent would be on the heap rather than in a cell, the
|
|
#! address of a parameter held in memory, which it copies, or of where its
|
|
#! result goes, which it writes, or the environment of the function value
|
|
#! called, which it reads.
|
|
#! @param this The selector.
|
|
#! @param index The call.
|
|
#! @param a The operand.
|
|
#! @returns Whether it is.
|
|
def selector::lent_operand(&this, index: u32, a: u32): bool {
|
|
val held: inst = this.body.insts[index];
|
|
var signature: u32 = held.aux;
|
|
if held.kind == op::call {
|
|
signature = this.b.c.program.functions[held.aux].signature;
|
|
}
|
|
return true if this.returns_memory(signature) && a == held.argc - 1;
|
|
# A function value's two words come before the arguments: what it is, then
|
|
# its environment.
|
|
var word: u32 = 0;
|
|
if held.kind == op::call_value {
|
|
return a != 0 if a < 2;
|
|
word = 2;
|
|
}
|
|
for &each in this.b.c.program.signatures[signature].params {
|
|
val shape: layout = this.b.c.program.layouts[each];
|
|
val words: u32 = in_memory(&shape) ? 1 : word_count(&shape);
|
|
if a < word + words {
|
|
return in_memory(&shape) || shape.kind == lk::reference || shape.kind == lk::trait_reference;
|
|
}
|
|
word += words;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#! Puts the words a call gave in `rax`, `rdx`, and `rcx` where its results go.
|
|
#! @param this The selector.
|
|
#! @param held The call.
|
|
def selector::results(&mut this, held: &inst): void {
|
|
val given: reg[3] = [reg::rax, reg::rdx, reg::rcx];
|
|
for r in 0..held.count {
|
|
break if r >= 3;
|
|
val result := held.result + r as u32;
|
|
val to := this.p.made_at(result);
|
|
continue if to.kind == place::none;
|
|
this.move_one(&transfer { from = in_gpr(given[r as u64]), to = to });
|
|
}
|
|
}
|
|
|
|
#! Calls one of the runtime's helpers, given its arguments.
|
|
#! @param this The selector.
|
|
#! @param which The helper.
|
|
#! @param moves The moves of its arguments into the registers they are passed in.
|
|
def selector::helper_call(&mut this, which: helper, moves: span<transfer>): void {
|
|
this.parallel(moves);
|
|
this.b.call_to(this.b.helper_of(which));
|
|
}
|
|
|
|
#! The move of an operand of an instruction into the register a helper's
|
|
#! argument is passed in.
|
|
#! @param this The selector.
|
|
#! @param index The instruction.
|
|
#! @param at The operand.
|
|
#! @param k The argument.
|
|
#! @returns The move.
|
|
def selector::passing(&this, index: u32, at: u32, k: u64): transfer {
|
|
return transfer { from = this.read(this.body.arg(index, at), index), to = in_gpr(gprs[k]) };
|
|
}
|
|
|
|
#! The move of a constant into the register a helper's argument is passed in.
|
|
#! @param value The constant.
|
|
#! @param k The argument.
|
|
#! @param this The selector.
|
|
#! @returns The move.
|
|
def selector::constant_arg(&mut this, value: u64, k: u64): void {
|
|
this.b.text.mov_imm(gprs[k], value);
|
|
}
|
|
|
|
#! The code of the operations the runtime's helpers do.
|
|
#! @param this The selector.
|
|
#! @param index The instruction.
|
|
#! @param held The instruction.
|
|
def selector::runtime_op(&mut this, index: u32, held: &inst): void {
|
|
match held.kind {
|
|
op::allocate => {
|
|
var moves: transfer[1] = [this.passing(index, 0, 0)];
|
|
this.helper_call(held.aux == 0 ? helper::allocate_zeroed : helper::allocate, moves[:]);
|
|
this.results(held);
|
|
}
|
|
op::release => {
|
|
var moves: transfer[1] = [this.passing(index, 0, 0)];
|
|
this.helper_call(helper::release, moves[:]);
|
|
}
|
|
op::resize => {
|
|
var moves: transfer[2] = [this.passing(index, 0, 0), this.passing(index, 1, 1)];
|
|
this.helper_call(helper::resize, moves[:]);
|
|
this.results(held);
|
|
}
|
|
op::equal => {
|
|
var moves: transfer[2] = [this.passing(index, 0, 0), this.passing(index, 1, 1)];
|
|
this.parallel(moves[:]);
|
|
this.constant_arg(this.b.layout_address(held.aux), 2);
|
|
this.b.call_to(this.b.helper_of(helper::equal));
|
|
this.results(held);
|
|
}
|
|
op::format => this.b.call_to(this.b.helper_of(helper::format_begin));
|
|
op::format_text => {
|
|
this.constant_arg(this.b.datum_address(held.aux), 0);
|
|
this.constant_arg(this.b.datum_length(held.aux), 1);
|
|
this.b.call_to(this.b.helper_of(helper::format_text));
|
|
}
|
|
op::format_bytes => {
|
|
var moves: transfer[2] = [this.passing(index, 0, 0), this.passing(index, 1, 1)];
|
|
this.helper_call(helper::format_text, moves[:]);
|
|
}
|
|
op::format_value => {
|
|
val shape: layout = this.b.c.program.layouts[held.aux];
|
|
if in_memory(&shape) {
|
|
var moves: transfer[1] = [this.passing(index, 0, 0)];
|
|
this.parallel(moves[:]);
|
|
} else {
|
|
# The words, as a frame holds the value, go to the scratch of the frame.
|
|
for a in 0..held.argc {
|
|
break if a >= 3;
|
|
val from := this.read(this.body.arg(index, a), index);
|
|
this.move_one(&transfer { from = from, to = loc { kind = place::none } });
|
|
this.store_scratch(from, a as u64);
|
|
}
|
|
this.b.text.lea(gprs[0], this.scratch_word(0));
|
|
}
|
|
this.constant_arg(this.b.layout_address(held.aux), 1);
|
|
this.b.call_to(this.b.helper_of(helper::format_value));
|
|
}
|
|
op::format_owned => {
|
|
this.b.text.lea(gprs[0], this.scratch_word(0));
|
|
this.b.call_to(this.b.helper_of(helper::format_owned));
|
|
this.b.text.load(8, false, reg::rax, this.scratch_word(0));
|
|
this.b.text.load(8, false, reg::rdx, this.scratch_word(1));
|
|
this.results(held);
|
|
}
|
|
op::exit => {
|
|
var moves: transfer[1] = [this.passing(index, 0, 0)];
|
|
this.helper_call(helper::exit, moves[:]);
|
|
}
|
|
op::abort => this.b.call_to(this.b.helper_of(helper::abort));
|
|
op::fault => {
|
|
var moves: transfer[2] = [this.passing(index, 0, 0), this.passing(index, 1, 1)];
|
|
this.helper_call(helper::fault, moves[:]);
|
|
}
|
|
else => {}
|
|
}
|
|
}
|
|
|
|
#! Writes where a value is to a word of the frame's scratch.
|
|
#! @param this The selector.
|
|
#! @param from Where the value is.
|
|
#! @param k The word.
|
|
def selector::store_scratch(&mut this, from: loc, k: u64): void {
|
|
val to := this.scratch_word(k);
|
|
match from.kind {
|
|
place::gpr => this.b.text.store(8, to, from.number as reg);
|
|
place::xmm => this.b.text.movsd_store(to, from.number);
|
|
place::remade => {
|
|
this.remake(from.value, reg::rax);
|
|
this.b.text.store(8, to, reg::rax);
|
|
}
|
|
place::none => this.b.text.store_imm(to, 0);
|
|
else => {
|
|
this.b.text.load(8, false, reg::rax, this.memory_of(&from));
|
|
this.b.text.store(8, to, reg::rax);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
# The system.
|
|
|
|
#! The code of the operations of the system: system calls, signals, the
|
|
#! environment, and safe points.
|
|
#! @param this The selector.
|
|
#! @param index The instruction.
|
|
#! @param held The instruction.
|
|
def selector::system_op(&mut this, index: u32, held: &inst): void {
|
|
match held.kind {
|
|
op::syscall => {
|
|
if this.b.options.windows {
|
|
val decl := this.b.c.program.functions[this.index].decl;
|
|
this.b.problem_at(decl, "System calls are Linux's: an executable for Windows calls the system through C functions, as an `extern module` and `extern import \"kernel32.dll\"` declare them.");
|
|
return;
|
|
}
|
|
# Each operand is pushed, then popped into the register the kernel reads
|
|
# it from, so none is overwritten before it is read.
|
|
for a in 0..7u {
|
|
if a < held.argc as u64 {
|
|
this.push_operand(this.body.arg(index, a as u32), index);
|
|
} else {
|
|
this.b.text.mov_imm(reg::rax, 0);
|
|
this.b.text.push(reg::rax);
|
|
}
|
|
}
|
|
val kernel: reg[7] = [reg::rax, reg::rdi, reg::rsi, reg::rdx, reg::r10, reg::r8, reg::r9];
|
|
for k in 0..7u {
|
|
this.b.text.pop(kernel[6 - k]);
|
|
}
|
|
this.b.text.syscall();
|
|
this.results(held);
|
|
}
|
|
op::catch => {
|
|
# On Windows the console's events stand for signals, and what the program
|
|
# asks of `catch_signal` is done by code of its own, called as C is.
|
|
# `rsp` is kept in the frame's scratch around the call.
|
|
if this.b.options.windows {
|
|
this.load_into(this.body.arg(index, 0), index, reg::rcx);
|
|
this.b.text.store(8, this.scratch_word(0), reg::rsp);
|
|
this.b.text.alu_imm(alu::band, reg::rsp, -16);
|
|
this.b.text.patch(this.b.text.call(), this.b.catching);
|
|
this.b.text.load(8, false, reg::rsp, this.scratch_word(0));
|
|
this.results(held);
|
|
return;
|
|
}
|
|
# `rt_sigaction`, giving the signal the handler noting it, without
|
|
# `SA_RESTART`, so a wait it interrupts ends.
|
|
this.load_into(this.body.arg(index, 0), index, reg::rdi);
|
|
this.b.text.alu_imm(alu::sub, reg::rsp, 32);
|
|
this.b.text.mov_imm(reg::rax, this.b.text_base + this.b.noting);
|
|
this.b.text.store(8, at(reg::rsp, 0), reg::rax);
|
|
this.b.text.mov_imm(reg::rax, 0x04000000);
|
|
this.b.text.store(8, at(reg::rsp, 8), reg::rax);
|
|
this.b.text.mov_imm(reg::rax, this.b.text_base + this.b.restorer);
|
|
this.b.text.store(8, at(reg::rsp, 16), reg::rax);
|
|
this.b.text.store_imm(at(reg::rsp, 24), 0);
|
|
this.b.text.mov_imm(reg::rax, 13);
|
|
this.b.text.mov(reg::rsi, reg::rsp);
|
|
this.b.text.mov_imm(reg::rdx, 0);
|
|
this.b.text.mov_imm(reg::r10, 8);
|
|
this.b.text.syscall();
|
|
this.b.text.alu_imm(alu::add, reg::rsp, 32);
|
|
this.results(held);
|
|
}
|
|
op::safepoint => {
|
|
# The call is rare, so it is made after the function's body, which keeps
|
|
# every register around it.
|
|
# Only the first thread's record notes signals, so only it runs handlers.
|
|
val signals := this.b.own_word(state_signals);
|
|
this.b.text.alu_mem_imm(alu::cmp, signals, 0);
|
|
val patch := this.b.text.jcc(cc::not_equal);
|
|
this.safe_points.append(safe_site {
|
|
patch = patch, back = this.b.text.length, at = held.at, site = held.site, index = index
|
|
});
|
|
}
|
|
op::signals => {
|
|
# On Windows the event is reset before the signals are taken, so that one
|
|
# noted between its reset and the exchange sets it again. Taking signals
|
|
# is no call, so the registers the cache may hold a value in that the
|
|
# reset would clobber are kept around it; `rsp` is a word of 16 within a
|
|
# function's body, so the call needs only the 32 bytes above it.
|
|
if this.b.options.windows {
|
|
this.b.text.load(8, false, reg::rcx, absolute(this.b.state_address(state_signalled)));
|
|
this.b.text.test(reg::rcx, reg::rcx);
|
|
val unasked := this.b.text.jcc(cc::equal);
|
|
this.b.text.push(reg::r8);
|
|
this.b.text.push(reg::r9);
|
|
this.b.text.push(reg::r10);
|
|
this.b.text.push(reg::r11);
|
|
this.b.text.alu_imm(alu::sub, reg::rsp, 32);
|
|
this.b.call_system(system_reset_event);
|
|
this.b.text.alu_imm(alu::add, reg::rsp, 32);
|
|
this.b.text.pop(reg::r11);
|
|
this.b.text.pop(reg::r10);
|
|
this.b.text.pop(reg::r9);
|
|
this.b.text.pop(reg::r8);
|
|
this.b.text.land(unasked);
|
|
}
|
|
val into := this.target(held.result, reg::rax);
|
|
this.b.text.mov_imm(into, 0);
|
|
this.b.text.xchg(8, absolute(this.b.state_address(state_signals)), into);
|
|
this.put(held.result, into);
|
|
}
|
|
op::environ => {
|
|
# The system leaves the variables on the first stack after `argv` and
|
|
# its null.
|
|
this.b.text.load(8, false, reg::rax, absolute(this.b.state_address(state_argc)));
|
|
this.b.text.load(8, false, reg::rcx, absolute(this.b.state_address(state_argv)));
|
|
val into := this.target(held.result, reg::rax);
|
|
this.b.text.lea(into, indexed(reg::rcx, reg::rax, 8, 8));
|
|
this.put(held.result, into);
|
|
}
|
|
else => {}
|
|
}
|
|
}
|
|
|
|
#! Pushes a word an instruction reads onto the stack.
|
|
#! @param this The selector.
|
|
#! @param value The word.
|
|
#! @param index The instruction.
|
|
def selector::push_operand(&mut this, value: u32, index: u32): void {
|
|
this.b.text.push(this.word_reg(value, index, reg::rax));
|
|
}
|
|
|
|
|
|
# Ends of blocks.
|
|
|
|
#! The code of an instruction ending a block: a jump, a branch, a switch, a
|
|
#! return, or a trap.
|
|
#! @param this The selector.
|
|
#! @param index The instruction.
|
|
#! @param held The instruction.
|
|
#! @param next The block laid out next, which a jump to needs no jump.
|
|
def selector::ending(&mut this, index: u32, held: &inst, next: u32): void {
|
|
val end := this.p.end_slots[held.block];
|
|
match held.kind {
|
|
op::jump => {
|
|
this.gap(end);
|
|
this.edge(held.aux);
|
|
this.jump_to(this.body.edges[held.aux].target, next);
|
|
}
|
|
op::branch => this.branch(index, held, next, end);
|
|
op::switch => this.switch_on(index, held, next, end);
|
|
op::ret => this.ret(index, held);
|
|
op::trap => {
|
|
if held.argc != 0 {
|
|
this.load_into(this.body.arg(index, 0), index, reg::rdi);
|
|
} else {
|
|
this.b.text.mov_imm(reg::rdi, held.aux as u64);
|
|
}
|
|
this.b.text.patch(this.b.text.call(), this.b.trap_stub);
|
|
}
|
|
else => {
|
|
this.b.text.mov_imm(reg::rdi, fault::unreachable as u64);
|
|
this.b.text.patch(this.b.text.call(), this.b.trap_stub);
|
|
}
|
|
}
|
|
}
|
|
|
|
#! Jumps to a block, unless it is laid out next.
|
|
#! @param this The selector.
|
|
#! @param target The block.
|
|
#! @param next The block laid out next.
|
|
def selector::jump_to(&mut this, target: u32, next: u32): void {
|
|
return if target == next;
|
|
this.jumps.add(this.b.text.jmp(), target);
|
|
}
|
|
|
|
#! Jumps to a block where a condition holds.
|
|
#! @param this The selector.
|
|
#! @param condition The condition.
|
|
#! @param target The block.
|
|
def selector::jump_if(&mut this, condition: cc, target: u32): void {
|
|
this.jumps.add(this.b.text.jcc(condition), target);
|
|
}
|
|
|
|
#! A branch: to its first edge where its condition holds, and to the second
|
|
#! otherwise, each moving what its block takes down a path of its own.
|
|
#! @param this The selector.
|
|
#! @param index The branch.
|
|
#! @param held The branch.
|
|
#! @param next The block laid out next.
|
|
#! @param end The place where its block's edges leave.
|
|
def selector::branch(&mut this, index: u32, held: &inst, next: u32, end: u32): void {
|
|
var condition := cc::not_equal;
|
|
val reader := this.p.reader_of(this.body, index);
|
|
if reader != index {
|
|
val kind: op = this.body.insts[reader].kind;
|
|
condition = this.compare(index, kind, this.body.arg(reader, 0), this.body.arg(reader, 1));
|
|
} else {
|
|
val tested := this.word_reg(this.body.arg(index, 0), index, reg::rax);
|
|
this.b.text.test(tested, tested);
|
|
}
|
|
|
|
# The moves of the place the edges leave from are only moves, which leave
|
|
# the flags as they are.
|
|
this.gap(end);
|
|
val then := held.aux;
|
|
val other := held.aux + 1;
|
|
val then_target: u32 = this.body.edges[then].target;
|
|
val other_target: u32 = this.body.edges[other].target;
|
|
val then_moves := this.moves_on(then);
|
|
val other_moves := this.moves_on(other);
|
|
if !then_moves && !other_moves {
|
|
if then_target == next {
|
|
this.jump_if(opposite(condition), other_target);
|
|
return;
|
|
}
|
|
this.jump_if(condition, then_target);
|
|
this.jump_to(other_target, next);
|
|
return;
|
|
}
|
|
if !then_moves {
|
|
this.jump_if(condition, then_target);
|
|
this.edge(other);
|
|
this.jump_to(other_target, next);
|
|
return;
|
|
}
|
|
if !other_moves {
|
|
this.jump_if(opposite(condition), other_target);
|
|
this.edge(then);
|
|
this.jump_to(then_target, next);
|
|
return;
|
|
}
|
|
val otherwise := this.b.text.jcc(opposite(condition));
|
|
this.edge(then);
|
|
this.jump_to(then_target, never);
|
|
this.b.text.land(otherwise);
|
|
this.edge(other);
|
|
this.jump_to(other_target, next);
|
|
}
|
|
|
|
#! A switch: compares the word with each case in turn, then goes to the
|
|
#! default, each edge moving what its block takes on a path of its own.
|
|
#! @param this The selector.
|
|
#! @param index The switch.
|
|
#! @param held The switch.
|
|
#! @param next The block laid out next.
|
|
#! @param end The place where its block's edges leave.
|
|
def selector::switch_on(&mut this, index: u32, held: &inst, next: u32, end: u32): void {
|
|
# `rdx` holds the word, which no move touches.
|
|
this.load_into(this.body.arg(index, 0), index, reg::rdx);
|
|
this.gap(end);
|
|
val edges := held.imm as u32;
|
|
return if this.switch_by_table(held, edges);
|
|
var paths := vector::<u64>::new(scratch());
|
|
for e in 0..edges - 1 {
|
|
val edge := held.aux + e;
|
|
val case: u64 = this.body.edges[edge].case;
|
|
if case == 0 {
|
|
this.b.text.test(reg::rdx, reg::rdx);
|
|
} else if fits_i32(case) {
|
|
this.b.text.alu_imm(alu::cmp, reg::rdx, case as i32);
|
|
} else {
|
|
this.b.text.mov_imm(reg::rax, case);
|
|
this.b.text.alu(alu::cmp, reg::rdx, reg::rax);
|
|
}
|
|
if this.moves_on(edge) {
|
|
paths.append(this.b.text.jcc(cc::equal));
|
|
} else {
|
|
paths.append(never as u64);
|
|
this.jump_if(cc::equal, this.body.edges[edge].target);
|
|
}
|
|
}
|
|
# Where the cases' moves come after, the fallback can't fall through to the
|
|
# block laid out next.
|
|
var landings := false;
|
|
for e in 0..edges - 1 {
|
|
landings = landings || paths[e as u64] != never as u64;
|
|
}
|
|
val fallback := held.aux + edges - 1;
|
|
this.edge(fallback);
|
|
this.jump_to(this.body.edges[fallback].target, landings ? never : next);
|
|
for e in 0..edges - 1 {
|
|
val path: u64 = paths[e as u64];
|
|
continue if path == never as u64;
|
|
this.b.text.land(path);
|
|
this.edge(held.aux + e);
|
|
this.jump_to(this.body.edges[held.aux + e].target, never);
|
|
}
|
|
}
|
|
|
|
# The fewest cases a switch jumps through a table for, the most entries a table
|
|
# has, and how many entries it may have for each case.
|
|
const table_least: u64 = 4;
|
|
const table_most: u64 = 1024;
|
|
const table_spread: u64 = 3;
|
|
|
|
#! The lowest case of a switch, and how many entries its table has, where it
|
|
#! goes through one: where its cases are enough and close enough together.
|
|
#! @param this The selector.
|
|
#! @param held The switch.
|
|
#! @param low Where its lowest case is written.
|
|
#! @returns How many entries, or 0 where it compares each case instead.
|
|
def selector::table_size(&this, held: &inst, low: &mut i64): u64 {
|
|
val count := held.imm - 1;
|
|
return 0 if count < table_least;
|
|
low = this.body.edges[held.aux].case as i64;
|
|
var high := low;
|
|
for e in 0..count {
|
|
val case := this.body.edges[held.aux + e as u32].case as i64;
|
|
low = case < low ? case : low;
|
|
high = case > high ? case : high;
|
|
}
|
|
# Far enough apart that the difference overflows is too far.
|
|
return 0 if high - low < 0;
|
|
val size := (high - low) as u64 + 1;
|
|
return size > table_most || size > table_spread * count ? 0 : size;
|
|
}
|
|
|
|
#! A switch whose cases are close enough together: the word, less the lowest
|
|
#! case, indexes a table of where each goes, written after the function's
|
|
#! code, and any word past the table goes where no case does.
|
|
#! @param this The selector.
|
|
#! @param held The switch.
|
|
#! @param edges How many edges it has, the last where no case matches.
|
|
#! @returns Whether it was, which it is not where the cases are too few or
|
|
#! too far apart.
|
|
def selector::switch_by_table(&mut this, held: &inst, edges: u32): bool {
|
|
val count := edges as u64 - 1;
|
|
var low: i64 = 0;
|
|
val size := this.table_size(held, &mut low);
|
|
return false if size == 0;
|
|
|
|
if low != 0 {
|
|
if fits_i32(low as u64) {
|
|
this.b.text.alu_imm(alu::sub, reg::rdx, low as i32);
|
|
} else {
|
|
this.b.text.mov_imm(reg::rax, low as u64);
|
|
this.b.text.alu(alu::sub, reg::rdx, reg::rax);
|
|
}
|
|
}
|
|
this.b.text.alu_imm(alu::cmp, reg::rdx, (size - 1) as i32);
|
|
val fallback := held.aux + edges - 1;
|
|
var past: u64 = never as u64;
|
|
if this.moves_on(fallback) {
|
|
past = this.b.text.jcc(cc::above);
|
|
} else {
|
|
this.jump_if(cc::above, this.body.edges[fallback].target);
|
|
}
|
|
# Each entry is the address of where its case goes, which fits 32 bits, as
|
|
# every address of an executable's image does.
|
|
this.b.text.load(4, false, reg::rax, indexed(reg::none, reg::rdx, 4, 0));
|
|
val patch := this.b.text.length - 4;
|
|
this.b.text.jmp_reg(reg::rax);
|
|
|
|
# Where each case goes: its block, or the moves its edge makes first.
|
|
var going := vector::<table_entry>::new(scratch());
|
|
for e in 0..edges {
|
|
val edge := held.aux + e;
|
|
val target: u32 = this.body.edges[edge].target;
|
|
if !this.moves_on(edge) {
|
|
going.append(table_entry { block = target, at = 0 });
|
|
continue;
|
|
}
|
|
if e == edges - 1 {
|
|
this.b.text.land(past);
|
|
}
|
|
going.append(table_entry { block = never, at = this.b.text.length });
|
|
this.edge(edge);
|
|
this.jump_to(target, never);
|
|
}
|
|
|
|
val first := this.entries.length as u32;
|
|
for _ in 0..size {
|
|
this.entries.append(going[count]);
|
|
}
|
|
for e in 0..count {
|
|
val at := (this.body.edges[held.aux + e as u32].case as i64 - low) as u64;
|
|
this.entries[first as u64 + at] = going[e];
|
|
}
|
|
this.tables.append(switch_table { patch = patch, first = first, count = size as u32 });
|
|
return true;
|
|
}
|
|
|
|
#! Writes the tables of the function's switches after its code, and points
|
|
#! the jumps through them at them.
|
|
#! @param this The selector.
|
|
def selector::write_tables(&mut this): void {
|
|
return if this.tables.length == 0;
|
|
this.b.text.align_to(4);
|
|
for t in 0..this.tables.length {
|
|
val table: switch_table = this.tables[t];
|
|
this.b.text.set_u32(table.patch, this.b.text_base + this.b.text.length);
|
|
for k in table.first..table.first + table.count {
|
|
val entry: table_entry = this.entries[k as u64];
|
|
val at := entry.block != never ? this.labels[entry.block] : entry.at;
|
|
this.b.text.u32(this.b.text_base + at);
|
|
}
|
|
}
|
|
}
|
|
|
|
#! A return: the words in `rax`, `rdx`, and `rcx`, or the memory at the
|
|
#! address copied to where the caller asked, then back to the caller.
|
|
#! @param this The selector.
|
|
#! @param index The return.
|
|
#! @param held The return.
|
|
def selector::ret(&mut this, index: u32, held: &inst): void {
|
|
if this.returned != 0 {
|
|
# Every value is dead here, so any register may be used.
|
|
if held.argc != 0 {
|
|
this.load_into(this.body.arg(index, 0), index, reg::rsi);
|
|
this.b.text.load(8, false, reg::rdi, at(reg::rbp, -(this.dst_at as i64) as i32));
|
|
this.b.text.test(reg::rdi, reg::rdi);
|
|
val no_dst := this.b.text.jcc(cc::equal);
|
|
this.b.text.test(reg::rsi, reg::rsi);
|
|
val no_src := this.b.text.jcc(cc::equal);
|
|
# Copied 16 bytes at a time, as the caller reads a vector of them back,
|
|
# which the processor forwards only from a store as wide.
|
|
if this.returned <= 128 {
|
|
this.b.text.copy_small(at(reg::rdi, 0), at(reg::rsi, 0), this.returned, reg::rax, true);
|
|
} else {
|
|
this.b.text.mov_imm(reg::rcx, this.returned);
|
|
this.b.text.rep_movsb();
|
|
}
|
|
this.b.text.land(no_dst);
|
|
this.b.text.land(no_src);
|
|
}
|
|
} else {
|
|
val given: reg[3] = [reg::rax, reg::rdx, reg::rcx];
|
|
var moves := vector::<transfer>::new(scratch());
|
|
for a in 0..held.argc {
|
|
break if a >= 3;
|
|
moves.append(transfer { from = this.read(this.body.arg(index, a), index), to = in_gpr(given[a as u64]) });
|
|
}
|
|
this.parallel(moves.view());
|
|
}
|
|
this.epilogue(1);
|
|
this.b.text.ret();
|
|
}
|
|
|
|
#! Leaves the frame before the instruction leaving the function: gives back
|
|
#! the registers a call keeps that it saved, clears the upper halves of the
|
|
#! vector registers where its code held 32 bytes in them, and puts back the
|
|
#! caller's frame and stack pointers.
|
|
#! @param this The selector.
|
|
#! @param bytes The bytes of the instruction leaving, which debugging
|
|
#! information notes.
|
|
def selector::epilogue(&mut this, bytes: u8): void {
|
|
for k in 0..this.save_count {
|
|
this.b.text.load(8, false, this.saved[k], at(reg::rbp, -((8 * (k + 1)) as i64) as i32));
|
|
}
|
|
if this.body.wide || this.uses_upper {
|
|
this.b.text.vzeroupper();
|
|
}
|
|
this.b.text.leave();
|
|
if this.b.options.debug {
|
|
this.b.debug.returns.append((this.b.text.length - this.start) as u32);
|
|
this.b.debug.return_bytes.append(bytes);
|
|
}
|
|
}
|
|
|
|
|
|
# A function.
|
|
|
|
#! The code of one instruction, but for those ending blocks.
|
|
#! @param this The selector.
|
|
#! @param index The instruction.
|
|
#! @param held The instruction.
|
|
def selector::emit(&mut this, index: u32, held: &inst): void {
|
|
val kind := held.kind;
|
|
val number := kind as u32;
|
|
# Code holding 32 bytes in registers clears their upper halves before a call,
|
|
# across which it holds nothing in registers, so the callee's SSE runs at
|
|
# speed.
|
|
if (this.body.wide || this.uses_upper) && calls(kind) {
|
|
this.b.text.vzeroupper();
|
|
}
|
|
if number >= op::add as u32 && number <= op::fle as u32 {
|
|
this.binary(index, held);
|
|
return;
|
|
}
|
|
if number >= op::neg as u32 && number <= op::wrap as u32 {
|
|
this.unary_op(index, held);
|
|
return;
|
|
}
|
|
match kind {
|
|
op::select => this.choose(index, held);
|
|
op::load => this.load(index, held);
|
|
op::store => this.store(index, held);
|
|
op::copy => this.copy(index, held);
|
|
op::clear => this.clear(index, held);
|
|
op::call, op::call_trait, op::call_value => this.call(index, held);
|
|
op::call_c => this.call_c(index, held);
|
|
op::allocate, op::release, op::resize, op::equal, op::format, op::format_text,
|
|
op::format_value, op::format_bytes, op::format_owned, op::exit, op::abort, op::fault => this.runtime_op(index, held);
|
|
op::syscall, op::catch, op::safepoint, op::signals, op::environ => this.system_op(index, held);
|
|
op::retrait => this.retrait(index, held);
|
|
op::check => this.check(index, held);
|
|
op::ready, op::reach, op::same => this.trap_always(halt_unsupported, index);
|
|
op::cpu => {
|
|
val into := this.target(held.result, reg::rax);
|
|
this.b.text.load(8, false, into, absolute(this.b.state_address(state_features)));
|
|
this.put(held.result, into);
|
|
}
|
|
op::lanes => this.lanes(index, held);
|
|
op::vzero => this.zero_lanes(index, held);
|
|
op::atomic => this.atomic(index, held);
|
|
op::start => {
|
|
# The function value's words and the context go to the scratch of the frame,
|
|
# which the runtime copies into the thread's record.
|
|
for a in 0..3 {
|
|
val from := this.read(this.body.arg(index, a as u32), index);
|
|
this.move_one(&transfer { from = from, to = loc { kind = place::none } });
|
|
this.store_scratch(from, a as u64);
|
|
}
|
|
this.b.text.lea(gprs[0], this.scratch_word(0));
|
|
this.b.call_to(this.b.helper_of(helper::start_thread));
|
|
this.results(held);
|
|
}
|
|
op::current => {
|
|
val into := this.target(held.result, reg::rax);
|
|
val itself := this.b.own_word(state_itself);
|
|
this.b.text.load(8, false, into, itself);
|
|
this.put(held.result, into);
|
|
}
|
|
op::clone => this.clone_thread(index, held);
|
|
else => {}
|
|
}
|
|
}
|
|
|
|
#! `clone`: a thread of the system sharing everything the program has, on the
|
|
#! stack of the record, whose number the system writes in the record as it
|
|
#! starts and clears as it ends. The new thread points `gs` at its record, takes
|
|
#! the signals it may, which are its faults, every other going to the first
|
|
#! thread, and runs `runtime::thread_main`, then ends.
|
|
#! The record is kept in `r9`, which `clone` takes no argument in, and which no
|
|
#! system call changes.
|
|
#! @param this The selector.
|
|
#! @param index The instruction.
|
|
#! @param held The instruction.
|
|
def selector::clone_thread(&mut this, index: u32, held: &inst): void {
|
|
if this.b.options.windows {
|
|
this.clone_thread_windows(index, held);
|
|
return;
|
|
}
|
|
val record := this.word_reg(this.body.arg(index, 0), index, reg::rdx);
|
|
this.b.text.mov(reg::r9, record);
|
|
this.b.text.mov_imm(reg::rdi, 0x350F00);
|
|
this.b.text.load(8, false, reg::rsi, at(reg::r9, (8 * state_base) as i32));
|
|
this.b.text.lea(reg::rdx, at(reg::r9, (8 * state_tid) as i32));
|
|
this.b.text.mov(reg::r10, reg::rdx);
|
|
this.b.text.mov_imm(reg::r8, 0);
|
|
this.b.text.mov_imm(reg::rax, 56);
|
|
this.b.text.syscall();
|
|
this.b.text.test(reg::rax, reg::rax);
|
|
val parent := this.b.text.jcc(cc::not_equal);
|
|
|
|
this.b.text.mov_imm(reg::rax, 158);
|
|
this.b.text.mov_imm(reg::rdi, 0x1001);
|
|
this.b.text.mov(reg::rsi, reg::r9);
|
|
this.b.text.syscall();
|
|
this.b.text.mov_imm(reg::rbp, 0);
|
|
val taken: u64 = (1u << 3) | (1u << 4) | (1u << 6) | (1u << 7) | (1u << 10);
|
|
this.b.text.alu_imm(alu::sub, reg::rsp, 16);
|
|
this.b.text.mov_imm(reg::rax, ~taken);
|
|
this.b.text.store(8, at(reg::rsp, 0), reg::rax);
|
|
this.b.text.mov_imm(reg::rax, 14);
|
|
this.b.text.mov_imm(reg::rdi, 2);
|
|
this.b.text.mov(reg::rsi, reg::rsp);
|
|
this.b.text.mov_imm(reg::rdx, 0);
|
|
this.b.text.mov_imm(reg::r10, 8);
|
|
this.b.text.syscall();
|
|
this.b.text.alu_imm(alu::add, reg::rsp, 16);
|
|
this.b.call_to(this.b.helper_of(helper::thread_main));
|
|
this.b.text.mov_imm(reg::rax, 60);
|
|
this.b.text.mov_imm(reg::rdi, 0);
|
|
this.b.text.syscall();
|
|
|
|
this.b.text.land(parent);
|
|
this.put(held.result, reg::rax);
|
|
}
|
|
|
|
#! The error a construct executables cannot run stops them with.
|
|
pub const halt_unsupported: u32 = 3;
|
|
|
|
#! Notes where in the source the code being written is from, where it changes.
|
|
#! @param this The selector.
|
|
#! @param held The instruction it is the code of.
|
|
def selector::mark(&mut this, at: u64, site: u32): void {
|
|
return if at == this.last_at && site == this.last_site;
|
|
this.last_at = at;
|
|
this.last_site = site;
|
|
this.b.rows.append(line_row { offset = this.b.text.length - this.start, at = at, site = site });
|
|
}
|
|
|
|
#! Writes the prologue: the frame is made, checked against the stack's limit,
|
|
#! the registers a call keeps that the function uses saved at its top, the
|
|
#! address a result in memory goes to kept, the parameters held in memory
|
|
#! copied, the cells zeroed, and each parameter moved to where its value is.
|
|
#! @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::prologue(&mut this, memory: span<bool>, sizes: span<u64>): void {
|
|
this.b.text.push(reg::rbp);
|
|
this.b.text.mov(reg::rbp, reg::rsp);
|
|
if !this.leaf || this.frame > 4096 {
|
|
val limit := this.b.own_word(state_limit);
|
|
this.b.text.lea(reg::rax, at(reg::rsp, -(this.frame as i64) as i32));
|
|
this.b.text.alu_load(alu::cmp, reg::rax, limit);
|
|
this.b.patch_jcc(cc::below, this.b.overflow_stub);
|
|
}
|
|
for k in 0..this.save_count {
|
|
this.b.text.push(this.saved[k]);
|
|
}
|
|
if this.b.options.debug || this.b.options.windows {
|
|
this.b.debug.saves[this.index] = this.saved_mask();
|
|
this.b.debug.saved_at[this.index] = (this.b.text.length - this.start) as u32;
|
|
this.b.debug.frames[this.index] = this.frame as u32;
|
|
}
|
|
val rest := this.frame - 8 * this.save_count;
|
|
if rest != 0 {
|
|
# On Windows the stack grows by guard pages, so a frame larger than a page
|
|
# is made a page at a time, each touched so the system commits it.
|
|
if this.b.options.windows && rest > page_size {
|
|
this.b.descend(rest);
|
|
} else {
|
|
this.b.text.alu_imm(alu::sub, reg::rsp, rest as i32);
|
|
}
|
|
}
|
|
if this.returned != 0 {
|
|
this.b.text.store(8, at(reg::rbp, -(this.dst_at as i64) as i32), reg::rdx);
|
|
}
|
|
|
|
# The copies of what is held in memory, from the addresses the caller gave.
|
|
for i in 0..memory.length {
|
|
continue if !memory[i];
|
|
val from := i < arg_count ? in_gpr(gprs[i]) : loc { kind = place::incoming, value = i as u32 };
|
|
this.move_one(&transfer { from = from, to = in_gpr(reg::rax) });
|
|
this.b.text.test(reg::rax, reg::rax);
|
|
val none := this.b.text.jcc(cc::equal);
|
|
val size := sizes[i];
|
|
val to := at(reg::rbp, -(this.copies[i] as i64) as i32);
|
|
if size <= 128 {
|
|
this.b.text.copy_small(to, at(reg::rax, 0), size, reg::rcx, false);
|
|
} else {
|
|
this.b.text.push(reg::rsi);
|
|
this.b.text.push(reg::rdi);
|
|
this.b.text.mov(reg::rsi, reg::rax);
|
|
this.b.text.lea(reg::rdi, to);
|
|
this.b.text.mov_imm(reg::rcx, size);
|
|
this.b.text.rep_movsb();
|
|
this.b.text.pop(reg::rdi);
|
|
this.b.text.pop(reg::rsi);
|
|
}
|
|
this.b.text.land(none);
|
|
}
|
|
|
|
# The cells the frame zeroes, sixteen bytes at a time from `xmm0`, which
|
|
# holds nothing as the function begins: written out where they are few, and
|
|
# by a loop down from the top otherwise, a word left at the bottom alone.
|
|
val bytes := this.zero_to - this.zero_from;
|
|
if bytes != 0 && bytes <= 256 {
|
|
this.b.text.zero_small(at(reg::rbp, -(this.zero_to as i64) as i32), bytes);
|
|
} else if bytes != 0 {
|
|
val pairs := bytes & ~15u;
|
|
this.b.text.packed(vec_pxor, 0, 0);
|
|
this.b.text.lea(reg::rax, at(reg::rbp, -(this.zero_to as i64) as i32));
|
|
if pairs != bytes {
|
|
this.b.text.store_imm(at(reg::rax, pairs as i32), 0);
|
|
}
|
|
this.b.text.mov_imm(reg::rcx, pairs);
|
|
val loop := this.b.text.length;
|
|
this.b.text.packed_mem(vec_movdqu_store, 0, indexed(reg::rax, reg::rcx, 1, -16));
|
|
this.b.text.alu_imm(alu::sub, reg::rcx, 16);
|
|
this.b.text.patch(this.b.text.jcc(cc::not_equal), loop);
|
|
}
|
|
|
|
# Each parameter, from where the caller passed it to where its value is.
|
|
val entry := &this.body.blocks[0];
|
|
var moves := vector::<transfer>::new(scratch());
|
|
for i in 0..entry.params.length {
|
|
val value: u32 = entry.params[i];
|
|
continue if this.p.remade[value];
|
|
val to := this.p.made_at(value);
|
|
continue if to.kind == place::none;
|
|
val from := i < arg_count ? in_gpr(gprs[i]) : loc { kind = place::incoming, value = i as u32 };
|
|
moves.append(transfer { from = from, to = to });
|
|
}
|
|
this.parallel(moves.view());
|
|
}
|
|
|
|
#! Writes the stubs the checks that fail jump to, each reporting its error from
|
|
#! where its check is.
|
|
#! @param this The selector.
|
|
def selector::stubs(&mut this): void {
|
|
# A safe point calls the code every function shares, which keeps the
|
|
# registers around the call of the handlers.
|
|
for i in 0..this.safe_points.length {
|
|
val site: safe_site = this.safe_points[i];
|
|
this.b.text.land(site.patch);
|
|
val from := this.b.text.length - this.start;
|
|
this.mark(site.at, site.site);
|
|
this.b.text.patch(this.b.text.call(), this.b.safe_point);
|
|
this.b.text.patch(this.b.text.jmp(), site.back);
|
|
this.note_stub(from, site.index);
|
|
}
|
|
for i in 0..this.traps.length {
|
|
val site: trap_site = this.traps[i];
|
|
this.b.text.land(site.patch);
|
|
val from := this.b.text.length - this.start;
|
|
this.mark(site.at, site.site);
|
|
this.b.text.mov_imm(reg::rdi, site.code as u64);
|
|
this.b.text.patch(this.b.text.call(), this.b.trap_stub);
|
|
this.note_stub(from, site.index);
|
|
}
|
|
}
|
|
|
|
#! Notes where a stub's code is, for the debugging information, which shows
|
|
#! the variables there as they are where the instruction it is of is.
|
|
#! @param this The selector.
|
|
#! @param from Where its code begins within the function's.
|
|
#! @param index The instruction it is of.
|
|
def selector::note_stub(&mut this, from: u64, index: u32): void {
|
|
return if !this.b.options.debug;
|
|
this.stub_spans.append(from as u32);
|
|
this.stub_spans.append((this.b.text.length - this.start) as u32);
|
|
this.stub_spans.append(index);
|
|
}
|
|
|