337 lines
12 KiB
Plaintext
337 lines
12 KiB
Plaintext
#! @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.view();
|
||
|
|
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.view();
|
||
|
|
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.view());
|
||
|
|
|
||
|
|
# 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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|