1
//! Dynamically emitted HashX assembly code for x86_64 targets
2

            
3
use crate::compiler::{util, Architecture, Executable};
4
use crate::program::{Instruction, InstructionArray, NUM_INSTRUCTIONS};
5
use crate::register::{RegisterFile, RegisterId};
6
use crate::CompilerError;
7
use dynasmrt::{x64, x64::Rq, DynasmApi, DynasmLabelApi};
8
use std::mem;
9

            
10
impl Architecture for Executable {
11
6954
    fn compile(program: &InstructionArray) -> Result<Self, CompilerError> {
12
6954
        let mut asm = Assembler::new();
13
6954
        {
14
6954
            emit_save_regs(&mut asm);
15
6954
            emit_load_input(&mut asm);
16
6954
            emit_init_locals(&mut asm);
17
6954
            debug_assert_eq!(asm.len(), PROLOGUE_SIZE);
18
        }
19
3567402
        for inst in program {
20
3560448
            let prev_len = asm.len();
21
3560448
            emit_instruction(&mut asm, inst);
22
3560448
            debug_assert!(asm.len() - prev_len <= INSTRUCTION_SIZE_LIMIT);
23
        }
24
        {
25
6954
            let prev_len = asm.len();
26
6954
            emit_store_output(&mut asm);
27
6954
            emit_restore_regs(&mut asm);
28
6954
            emit_return(&mut asm);
29
6954
            debug_assert_eq!(asm.len() - prev_len, EPILOGUE_SIZE);
30
        }
31
6954
        asm.finalize()
32
6954
    }
33

            
34
280232862
    fn invoke(&self, regs: &mut RegisterFile) {
35
280232862
        // Choose the System V ABI for x86_64. (Rust now lets us do this even on
36
280232862
        // targets that use a different default C ABI.) We aren't using the
37
280232862
        // stack red zone, and we only need one register-sized parameter.
38
280232862
        //
39
280232862
        // Parameters: rdi rsi rdx rcx r8 r9
40
280232862
        // Callee save: rbx rsp rbp r12 r13 r14 r15
41
280232862
        // Scratch: rax rdi rsi rdx rcx r8 r9 r10 r11
42
280232862

            
43
280232862
        let entry = self.buffer.ptr(Assembler::entry());
44
280232862
        let entry: extern "sysv64" fn(*mut RegisterFile) -> () = unsafe { mem::transmute(entry) };
45
280232862
        entry(regs);
46
280232862
    }
47
}
48

            
49
/// Architecture-specific fixed prologue size
50
const PROLOGUE_SIZE: usize = 0x68;
51

            
52
/// Architecture-specific fixed epilogue size
53
const EPILOGUE_SIZE: usize = 0x60;
54

            
55
/// Architecture-specific maximum size for one instruction
56
const INSTRUCTION_SIZE_LIMIT: usize = 0x11;
57

            
58
/// Capacity for the temporary output buffer, before code is copied into
59
/// a long-lived allocation that can be made executable.
60
const BUFFER_CAPACITY: usize =
61
    PROLOGUE_SIZE + EPILOGUE_SIZE + NUM_INSTRUCTIONS * INSTRUCTION_SIZE_LIMIT;
62

            
63
/// Architecture-specific specialization of the Assembler
64
type Assembler = util::Assembler<x64::X64Relocation, BUFFER_CAPACITY>;
65

            
66
/// Map RegisterId in our abstract program to concrete registers and addresses.
67
trait RegisterMapper {
68
    /// Map RegisterId(0) to R8, and so on
69
    fn rq(&self) -> u8;
70
    /// Byte offset in a raw RegisterFile
71
    fn offset(&self) -> i32;
72
}
73

            
74
impl RegisterMapper for RegisterId {
75
    #[inline(always)]
76
12010356
    fn rq(&self) -> u8 {
77
12010356
        8 + (self.as_usize() as u8)
78
12010356
    }
79

            
80
    #[inline(always)]
81
111264
    fn offset(&self) -> i32 {
82
111264
        (self.as_usize() * mem::size_of::<u64>()) as i32
83
111264
    }
84
}
85

            
86
/// Wrapper for `dynasm!`, sets the architecture and defines register aliases
87
macro_rules! dynasm {
88
    ($asm:ident $($t:tt)*) => {
89
        dynasmrt::dynasm!($asm
90
            ; .arch x64
91
            ; .alias mulh_in, rax
92
            ; .alias mulh_result64, rdx
93
            ; .alias mulh_result32, edx
94
            ; .alias branch_prohibit_flag, esi
95
            ; .alias const_ones, ecx
96
            ; .alias register_file_ptr, rdi
97
            $($t)*
98
        )
99
    }
100
}
101

            
102
/// Emit code to initialize our local variables to default values.
103
#[inline(always)]
104
6954
fn emit_init_locals<A: DynasmApi>(asm: &mut A) {
105
6954
    dynasm!(asm
106
6954
    ; xor mulh_result64, mulh_result64
107
6954
    ; xor branch_prohibit_flag, branch_prohibit_flag
108
6954
    ; lea const_ones, [branch_prohibit_flag - 1]
109
6954
    );
110
6954
}
111

            
112
/// List of registers to save on the stack, in address order
113
const REGS_TO_SAVE: [Rq; 4] = [Rq::R12, Rq::R13, Rq::R14, Rq::R15];
114

            
115
/// Calculate the amount of stack space to reserve, in bytes.
116
///
117
/// This is enough to hold REGS_TO_SAVE, and to keep the platform's
118
/// 16-byte stack alignment.
119
13908
const fn stack_size() -> i32 {
120
13908
    let size = REGS_TO_SAVE.len() * mem::size_of::<u64>();
121
13908
    let alignment = 0x10;
122
13908
    let offset = size % alignment;
123
13908
    let size = if offset == 0 {
124
13908
        size
125
    } else {
126
        size + alignment - offset
127
    };
128
13908
    size as i32
129
13908
}
130

            
131
/// Emit code to allocate stack space and store REGS_TO_SAVE.
132
#[inline(always)]
133
6954
fn emit_save_regs<A: DynasmApi>(asm: &mut A) {
134
6954
    dynasm!(asm; sub rsp, stack_size());
135
27816
    for (i, reg) in REGS_TO_SAVE.as_ref().iter().enumerate() {
136
27816
        let offset = (i * mem::size_of::<u64>()) as i32;
137
27816
        dynasm!(asm; mov [rsp + offset], Rq(*reg as u8));
138
27816
    }
139
6954
}
140

            
141
/// Emit code to restore REGS_TO_SAVE and deallocate stack space.
142
#[inline(always)]
143
6954
fn emit_restore_regs<A: DynasmApi>(asm: &mut A) {
144
27816
    for (i, reg) in REGS_TO_SAVE.as_ref().iter().enumerate() {
145
27816
        let offset = (i * mem::size_of::<u64>()) as i32;
146
27816
        dynasm!(asm; mov Rq(*reg as u8), [rsp + offset]);
147
27816
    }
148
6954
    dynasm!(asm; add rsp, stack_size());
149
6954
}
150

            
151
/// Emit code to move all input values from the RegisterFile into their
152
/// actual hardware registers.
153
#[inline(always)]
154
6954
fn emit_load_input<A: DynasmApi>(asm: &mut A) {
155
62586
    for reg in RegisterId::all() {
156
55632
        dynasm!(asm; mov Rq(reg.rq()), [register_file_ptr + reg.offset()]);
157
55632
    }
158
6954
}
159

            
160
/// Emit code to move all output values from machine registers back into
161
/// their RegisterFile slots.
162
#[inline(always)]
163
6954
fn emit_store_output<A: DynasmApi>(asm: &mut A) {
164
62586
    for reg in RegisterId::all() {
165
55632
        dynasm!(asm; mov [register_file_ptr + reg.offset()], Rq(reg.rq()));
166
55632
    }
167
6954
}
168

            
169
/// Emit a return instruction.
170
#[inline(always)]
171
6954
fn emit_return<A: DynasmApi>(asm: &mut A) {
172
6954
    dynasm!(asm; ret);
173
6954
}
174

            
175
/// Emit code for a single [`Instruction`] in the hash program.
176
#[inline(always)]
177
3560448
fn emit_instruction(asm: &mut Assembler, inst: &Instruction) {
178
    /// Common implementation for binary operations on registers
179
    macro_rules! reg_op {
180
        ($op:tt, $dst:ident, $src:ident) => {
181
            dynasm!(asm; $op Rq($dst.rq()), Rq($src.rq()))
182
        }
183
    }
184

            
185
    /// Common implementation for binary operations with a const operand
186
    macro_rules! const_op {
187
        ($op:tt, $dst:ident, $src:expr) => {
188
            dynasm!(asm; $op Rq($dst.rq()), $src)
189
        }
190
    }
191

            
192
    /// Common implementation for wide multiply operations.
193
    /// These use the one-argument form of `mul` (one register plus RDX:RAX)
194
    macro_rules! mulh_op {
195
        ($op:tt, $dst:ident, $src:ident) => {
196
            dynasm!(asm
197
                ; mov mulh_in, Rq($dst.rq())
198
                ; $op Rq($src.rq())
199
                ; mov Rq($dst.rq()), mulh_result64
200
            )
201
        }
202
    }
203

            
204
    /// Common implementation for scaled add using `lea`.
205
    /// Currently dynasm can only parse literal scale parameters.
206
    macro_rules! add_scaled_op {
207
        ($scale:tt, $dst:ident, $src:ident) => {
208
            dynasm!(asm
209
                ; lea Rq($dst.rq()), [ Rq($dst.rq()) + $scale * Rq($src.rq()) ]
210
            )
211
        }
212
    }
213

            
214
3560448
    match inst {
215
111264
        Instruction::Target => {
216
111264
            dynasm!(asm; target: );
217
111264
        }
218
111264
        Instruction::Branch { mask } => {
219
111264
            // Only one branch is allowed, `branch_prohibit_flag` keeps the test
220
111264
            // from passing. We get mul result tracking for free by assigning
221
111264
            // mulh_result32 to the corresponding output register used by the
222
111264
            // x86 mul instruction.
223
111264
            dynasm!(asm
224
111264
                ; or mulh_result32, branch_prohibit_flag
225
111264
                ; test mulh_result32, *mask as i32
226
111264
                ; cmovz branch_prohibit_flag, const_ones
227
111264
                ; jz <target
228
111264
            );
229
111264
        }
230
        Instruction::AddShift {
231
240027
            dst,
232
240027
            src,
233
240027
            left_shift,
234
240027
        } => match left_shift {
235
58767
            0 => add_scaled_op!(1, dst, src),
236
61218
            1 => add_scaled_op!(2, dst, src),
237
59337
            2 => add_scaled_op!(4, dst, src),
238
60705
            3 => add_scaled_op!(8, dst, src),
239
            _ => unreachable!(),
240
        },
241
112974
        Instruction::UMulH { dst, src } => {
242
112974
            mulh_op!(mul, dst, src);
243
112974
        }
244
109554
        Instruction::SMulH { dst, src } => {
245
109554
            mulh_op!(imul, dst, src);
246
109554
        }
247
1112640
        Instruction::Mul { dst, src } => {
248
1112640
            reg_op!(imul, dst, src);
249
1112640
        }
250
271377
        Instruction::Xor { dst, src } => {
251
271377
            reg_op!(xor, dst, src);
252
271377
        }
253
246867
        Instruction::Sub { dst, src } => {
254
246867
            reg_op!(sub, dst, src);
255
246867
        }
256
482562
        Instruction::AddConst { dst, src } => {
257
482562
            const_op!(add, dst, *src);
258
482562
        }
259
490029
        Instruction::XorConst { dst, src } => {
260
490029
            const_op!(xor, dst, *src);
261
490029
        }
262
271890
        Instruction::Rotate { dst, right_rotate } => {
263
271890
            const_op!(ror, dst, *right_rotate as i8);
264
271890
        }
265
    }
266
3560448
}