Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user