1
// Copyright (C) 2003 Dolphin Project.
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
18
#include "x64Emitter.h"
23
// Shared code between Win64 and Unix64
25
// Sets up a __cdecl function.
26
void XEmitter::ABI_EmitPrologue(int maxCallParams)
29
// Don't really need to do anything
32
int stacksize = ((maxCallParams + 1) & ~1) * 8 + 8;
33
// Set up a stack frame so that we can call functions
34
// TODO: use maxCallParams
35
SUB(64, R(RSP), Imm8(stacksize));
38
#error Arch not supported
42
void XEmitter::ABI_EmitEpilogue(int maxCallParams)
48
int stacksize = ((maxCallParams+1)&~1)*8 + 8;
49
ADD(64, R(RSP), Imm8(stacksize));
53
#error Arch not supported
59
#ifdef _M_IX86 // All32
61
// Shared code between Win32 and Unix32
62
void XEmitter::ABI_CallFunction(const void *func) {
68
void XEmitter::ABI_CallFunctionC16(const void *func, u16 param1) {
69
ABI_AlignStack(1 * 2);
70
PUSH(16, Imm16(param1));
72
ABI_RestoreStack(1 * 2);
75
void XEmitter::ABI_CallFunctionCC16(const void *func, u32 param1, u16 param2) {
76
ABI_AlignStack(1 * 2 + 1 * 4);
77
PUSH(16, Imm16(param2));
78
PUSH(32, Imm32(param1));
80
ABI_RestoreStack(1 * 2 + 1 * 4);
83
void XEmitter::ABI_CallFunctionC(const void *func, u32 param1) {
84
ABI_AlignStack(1 * 4);
85
PUSH(32, Imm32(param1));
87
ABI_RestoreStack(1 * 4);
90
void XEmitter::ABI_CallFunctionCC(const void *func, u32 param1, u32 param2) {
91
ABI_AlignStack(2 * 4);
92
PUSH(32, Imm32(param2));
93
PUSH(32, Imm32(param1));
95
ABI_RestoreStack(2 * 4);
98
void XEmitter::ABI_CallFunctionCCC(const void *func, u32 param1, u32 param2, u32 param3) {
99
ABI_AlignStack(3 * 4);
100
PUSH(32, Imm32(param3));
101
PUSH(32, Imm32(param2));
102
PUSH(32, Imm32(param1));
104
ABI_RestoreStack(3 * 4);
107
void XEmitter::ABI_CallFunctionCCP(const void *func, u32 param1, u32 param2, void *param3) {
108
ABI_AlignStack(3 * 4);
109
PUSH(32, ImmPtr(param3));
110
PUSH(32, Imm32(param2));
111
PUSH(32, Imm32(param1));
113
ABI_RestoreStack(3 * 4);
116
void XEmitter::ABI_CallFunctionCCCP(const void *func, u32 param1, u32 param2,u32 param3, void *param4) {
117
ABI_AlignStack(4 * 4);
118
PUSH(32, ImmPtr(param4));
119
PUSH(32, Imm32(param3));
120
PUSH(32, Imm32(param2));
121
PUSH(32, Imm32(param1));
123
ABI_RestoreStack(4 * 4);
126
void XEmitter::ABI_CallFunctionP(const void *func, void *param1) {
127
ABI_AlignStack(1 * 4);
128
PUSH(32, ImmPtr(param1));
130
ABI_RestoreStack(1 * 4);
133
void XEmitter::ABI_CallFunctionPA(const void *func, void *param1, const Gen::OpArg &arg2) {
134
ABI_AlignStack(2 * 4);
136
PUSH(32, ImmPtr(param1));
138
ABI_RestoreStack(2 * 4);
141
void XEmitter::ABI_CallFunctionPAA(const void *func, void *param1, const Gen::OpArg &arg2, const Gen::OpArg &arg3) {
142
ABI_AlignStack(3 * 4);
145
PUSH(32, ImmPtr(param1));
147
ABI_RestoreStack(3 * 4);
150
void XEmitter::ABI_CallFunctionPPC(const void *func, void *param1, void *param2, u32 param3) {
151
ABI_AlignStack(3 * 4);
152
PUSH(32, Imm32(param3));
153
PUSH(32, ImmPtr(param2));
154
PUSH(32, ImmPtr(param1));
156
ABI_RestoreStack(3 * 4);
159
// Pass a register as a parameter.
160
void XEmitter::ABI_CallFunctionR(const void *func, X64Reg reg1) {
161
ABI_AlignStack(1 * 4);
164
ABI_RestoreStack(1 * 4);
167
// Pass two registers as parameters.
168
void XEmitter::ABI_CallFunctionRR(const void *func, Gen::X64Reg reg1, Gen::X64Reg reg2)
170
ABI_AlignStack(2 * 4);
174
ABI_RestoreStack(2 * 4);
177
void XEmitter::ABI_CallFunctionAC(const void *func, const Gen::OpArg &arg1, u32 param2)
179
ABI_AlignStack(2 * 4);
180
PUSH(32, Imm32(param2));
183
ABI_RestoreStack(2 * 4);
186
void XEmitter::ABI_CallFunctionACC(const void *func, const Gen::OpArg &arg1, u32 param2, u32 param3)
188
ABI_AlignStack(3 * 4);
189
PUSH(32, Imm32(param3));
190
PUSH(32, Imm32(param2));
193
ABI_RestoreStack(3 * 4);
196
void XEmitter::ABI_CallFunctionA(const void *func, const Gen::OpArg &arg1)
198
ABI_AlignStack(1 * 4);
201
ABI_RestoreStack(1 * 4);
204
void XEmitter::ABI_CallFunctionAA(const void *func, const Gen::OpArg &arg1, const Gen::OpArg &arg2)
206
ABI_AlignStack(2 * 4);
210
ABI_RestoreStack(2 * 4);
213
void XEmitter::ABI_PushAllCalleeSavedRegsAndAdjustStack() {
214
// Note: 4 * 4 = 16 bytes, so alignment is preserved.
221
void XEmitter::ABI_PopAllCalleeSavedRegsAndAdjustStack() {
228
unsigned int XEmitter::ABI_GetAlignedFrameSize(unsigned int frameSize) {
229
frameSize += 4; // reserve space for return address
230
unsigned int alignedSize =
232
(frameSize + 15) & -16;
234
(frameSize + 3) & -4;
240
void XEmitter::ABI_AlignStack(unsigned int frameSize) {
241
// Mac OS X requires the stack to be 16-byte aligned before every call.
242
// Linux requires the stack to be 16-byte aligned before calls that put SSE
243
// vectors on the stack, but since we do not keep track of which calls do that,
244
// it is effectively every call as well.
245
// Windows binaries compiled with MSVC do not have such a restriction*, but I
246
// expect that GCC on Windows acts the same as GCC on Linux in this respect.
247
// It would be nice if someone could verify this.
248
// *However, the MSVC optimizing compiler assumes a 4-byte-aligned stack at times.
249
unsigned int fillSize =
250
ABI_GetAlignedFrameSize(frameSize) - (frameSize + 4);
252
SUB(32, R(ESP), Imm8(fillSize));
256
void XEmitter::ABI_RestoreStack(unsigned int frameSize) {
257
unsigned int alignedSize = ABI_GetAlignedFrameSize(frameSize);
258
alignedSize -= 4; // return address is POPped at end of call
259
if (alignedSize != 0) {
260
ADD(32, R(ESP), Imm8(alignedSize));
267
void XEmitter::ABI_CallFunction(const void *func) {
268
u64 distance = u64(func) - (u64(code) + 5);
269
if (distance >= 0x0000000080000000ULL
270
&& distance < 0xFFFFFFFF80000000ULL) {
272
MOV(64, R(RAX), ImmPtr(func));
279
void XEmitter::ABI_CallFunctionC16(const void *func, u16 param1) {
280
MOV(32, R(ABI_PARAM1), Imm32((u32)param1));
281
u64 distance = u64(func) - (u64(code) + 5);
282
if (distance >= 0x0000000080000000ULL
283
&& distance < 0xFFFFFFFF80000000ULL) {
285
MOV(64, R(RAX), ImmPtr(func));
292
void XEmitter::ABI_CallFunctionCC16(const void *func, u32 param1, u16 param2) {
293
MOV(32, R(ABI_PARAM1), Imm32(param1));
294
MOV(32, R(ABI_PARAM2), Imm32((u32)param2));
295
u64 distance = u64(func) - (u64(code) + 5);
296
if (distance >= 0x0000000080000000ULL
297
&& distance < 0xFFFFFFFF80000000ULL) {
299
MOV(64, R(RAX), ImmPtr(func));
306
void XEmitter::ABI_CallFunctionC(const void *func, u32 param1) {
307
MOV(32, R(ABI_PARAM1), Imm32(param1));
308
u64 distance = u64(func) - (u64(code) + 5);
309
if (distance >= 0x0000000080000000ULL
310
&& distance < 0xFFFFFFFF80000000ULL) {
312
MOV(64, R(RAX), ImmPtr(func));
319
void XEmitter::ABI_CallFunctionCC(const void *func, u32 param1, u32 param2) {
320
MOV(32, R(ABI_PARAM1), Imm32(param1));
321
MOV(32, R(ABI_PARAM2), Imm32(param2));
322
u64 distance = u64(func) - (u64(code) + 5);
323
if (distance >= 0x0000000080000000ULL
324
&& distance < 0xFFFFFFFF80000000ULL) {
326
MOV(64, R(RAX), ImmPtr(func));
333
void XEmitter::ABI_CallFunctionCCC(const void *func, u32 param1, u32 param2, u32 param3) {
334
MOV(32, R(ABI_PARAM1), Imm32(param1));
335
MOV(32, R(ABI_PARAM2), Imm32(param2));
336
MOV(32, R(ABI_PARAM3), Imm32(param3));
337
u64 distance = u64(func) - (u64(code) + 5);
338
if (distance >= 0x0000000080000000ULL
339
&& distance < 0xFFFFFFFF80000000ULL) {
341
MOV(64, R(RAX), ImmPtr(func));
348
void XEmitter::ABI_CallFunctionCCP(const void *func, u32 param1, u32 param2, void *param3) {
349
MOV(32, R(ABI_PARAM1), Imm32(param1));
350
MOV(32, R(ABI_PARAM2), Imm32(param2));
351
MOV(64, R(ABI_PARAM3), ImmPtr(param3));
352
u64 distance = u64(func) - (u64(code) + 5);
353
if (distance >= 0x0000000080000000ULL
354
&& distance < 0xFFFFFFFF80000000ULL) {
356
MOV(64, R(RAX), ImmPtr(func));
363
void XEmitter::ABI_CallFunctionCCCP(const void *func, u32 param1, u32 param2, u32 param3, void *param4) {
364
MOV(32, R(ABI_PARAM1), Imm32(param1));
365
MOV(32, R(ABI_PARAM2), Imm32(param2));
366
MOV(32, R(ABI_PARAM3), Imm32(param3));
367
MOV(64, R(ABI_PARAM4), ImmPtr(param4));
368
u64 distance = u64(func) - (u64(code) + 5);
369
if (distance >= 0x0000000080000000ULL
370
&& distance < 0xFFFFFFFF80000000ULL) {
372
MOV(64, R(RAX), ImmPtr(func));
379
void XEmitter::ABI_CallFunctionP(const void *func, void *param1) {
380
MOV(64, R(ABI_PARAM1), ImmPtr(param1));
381
u64 distance = u64(func) - (u64(code) + 5);
382
if (distance >= 0x0000000080000000ULL
383
&& distance < 0xFFFFFFFF80000000ULL) {
385
MOV(64, R(RAX), ImmPtr(func));
392
void XEmitter::ABI_CallFunctionPA(const void *func, void *param1, const Gen::OpArg &arg2) {
393
MOV(64, R(ABI_PARAM1), ImmPtr(param1));
394
if (!arg2.IsSimpleReg(ABI_PARAM2))
395
MOV(32, R(ABI_PARAM2), arg2);
396
u64 distance = u64(func) - (u64(code) + 5);
397
if (distance >= 0x0000000080000000ULL
398
&& distance < 0xFFFFFFFF80000000ULL) {
400
MOV(64, R(RAX), ImmPtr(func));
407
void XEmitter::ABI_CallFunctionPAA(const void *func, void *param1, const Gen::OpArg &arg2, const Gen::OpArg &arg3) {
408
MOV(64, R(ABI_PARAM1), ImmPtr(param1));
409
if (!arg2.IsSimpleReg(ABI_PARAM2))
410
MOV(32, R(ABI_PARAM2), arg2);
411
if (!arg3.IsSimpleReg(ABI_PARAM3))
412
MOV(32, R(ABI_PARAM3), arg3);
413
u64 distance = u64(func) - (u64(code) + 5);
414
if (distance >= 0x0000000080000000ULL
415
&& distance < 0xFFFFFFFF80000000ULL) {
417
MOV(64, R(RAX), ImmPtr(func));
424
void XEmitter::ABI_CallFunctionPPC(const void *func, void *param1, void *param2, u32 param3) {
425
MOV(64, R(ABI_PARAM1), ImmPtr(param1));
426
MOV(64, R(ABI_PARAM2), ImmPtr(param2));
427
MOV(32, R(ABI_PARAM3), Imm32(param3));
428
u64 distance = u64(func) - (u64(code) + 5);
429
if (distance >= 0x0000000080000000ULL
430
&& distance < 0xFFFFFFFF80000000ULL) {
432
MOV(64, R(RAX), ImmPtr(func));
439
// Pass a register as a parameter.
440
void XEmitter::ABI_CallFunctionR(const void *func, X64Reg reg1) {
441
if (reg1 != ABI_PARAM1)
442
MOV(32, R(ABI_PARAM1), R(reg1));
443
u64 distance = u64(func) - (u64(code) + 5);
444
if (distance >= 0x0000000080000000ULL
445
&& distance < 0xFFFFFFFF80000000ULL) {
447
MOV(64, R(RAX), ImmPtr(func));
454
// Pass two registers as parameters.
455
void XEmitter::ABI_CallFunctionRR(const void *func, X64Reg reg1, X64Reg reg2) {
456
if (reg2 != ABI_PARAM1) {
457
if (reg1 != ABI_PARAM1)
458
MOV(64, R(ABI_PARAM1), R(reg1));
459
if (reg2 != ABI_PARAM2)
460
MOV(64, R(ABI_PARAM2), R(reg2));
462
if (reg2 != ABI_PARAM2)
463
MOV(64, R(ABI_PARAM2), R(reg2));
464
if (reg1 != ABI_PARAM1)
465
MOV(64, R(ABI_PARAM1), R(reg1));
467
u64 distance = u64(func) - (u64(code) + 5);
468
if (distance >= 0x0000000080000000ULL
469
&& distance < 0xFFFFFFFF80000000ULL) {
471
MOV(64, R(RAX), ImmPtr(func));
478
void XEmitter::ABI_CallFunctionAC(const void *func, const Gen::OpArg &arg1, u32 param2)
480
if (!arg1.IsSimpleReg(ABI_PARAM1))
481
MOV(32, R(ABI_PARAM1), arg1);
482
MOV(32, R(ABI_PARAM2), Imm32(param2));
483
u64 distance = u64(func) - (u64(code) + 5);
484
if (distance >= 0x0000000080000000ULL
485
&& distance < 0xFFFFFFFF80000000ULL) {
487
MOV(64, R(RAX), ImmPtr(func));
494
void XEmitter::ABI_CallFunctionACC(const void *func, const Gen::OpArg &arg1, u32 param2, u32 param3)
496
if (!arg1.IsSimpleReg(ABI_PARAM1))
497
MOV(32, R(ABI_PARAM1), arg1);
498
MOV(32, R(ABI_PARAM2), Imm32(param2));
499
MOV(64, R(ABI_PARAM3), Imm64(param3));
500
u64 distance = u64(func) - (u64(code) + 5);
501
if (distance >= 0x0000000080000000ULL
502
&& distance < 0xFFFFFFFF80000000ULL) {
504
MOV(64, R(RAX), ImmPtr(func));
511
void XEmitter::ABI_CallFunctionA(const void *func, const Gen::OpArg &arg1)
513
if (!arg1.IsSimpleReg(ABI_PARAM1))
514
MOV(32, R(ABI_PARAM1), arg1);
515
u64 distance = u64(func) - (u64(code) + 5);
516
if (distance >= 0x0000000080000000ULL
517
&& distance < 0xFFFFFFFF80000000ULL) {
519
MOV(64, R(RAX), ImmPtr(func));
526
void XEmitter::ABI_CallFunctionAA(const void *func, const Gen::OpArg &arg1, const Gen::OpArg &arg2)
528
if (!arg1.IsSimpleReg(ABI_PARAM1))
529
MOV(32, R(ABI_PARAM1), arg1);
530
if (!arg2.IsSimpleReg(ABI_PARAM2))
531
MOV(32, R(ABI_PARAM2), arg2);
532
u64 distance = u64(func) - (u64(code) + 5);
533
if (distance >= 0x0000000080000000ULL
534
&& distance < 0xFFFFFFFF80000000ULL) {
536
MOV(64, R(RAX), ImmPtr(func));
543
unsigned int XEmitter::ABI_GetAlignedFrameSize(unsigned int frameSize) {
549
// The Windows x64 ABI requires XMM6 - XMM15 to be callee saved. 10 regs.
550
// But, not saving XMM4 and XMM5 breaks things in VS 2010, even though they are volatile regs.
551
// Let's just save all 16.
552
const int XMM_STACK_SPACE = 16 * 16;
554
// Win64 Specific Code
555
void XEmitter::ABI_PushAllCalleeSavedRegsAndAdjustStack() {
556
//we only want to do this once
567
// Do this after aligning, because before it's offset by 8.
568
SUB(64, R(RSP), Imm32(XMM_STACK_SPACE));
569
for (int i = 0; i < 16; ++i)
570
MOVAPS(MDisp(RSP, i * 16), (X64Reg)(XMM0 + i));
573
void XEmitter::ABI_PopAllCalleeSavedRegsAndAdjustStack() {
574
for (int i = 0; i < 16; ++i)
575
MOVAPS((X64Reg)(XMM0 + i), MDisp(RSP, i * 16));
576
ADD(64, R(RSP), Imm32(XMM_STACK_SPACE));
589
// Win64 Specific Code
590
void XEmitter::ABI_PushAllCallerSavedRegsAndAdjustStack() {
599
// TODO: Callers preserve XMM4-5 (XMM0-3 are args.)
603
void XEmitter::ABI_PopAllCallerSavedRegsAndAdjustStack() {
615
void XEmitter::ABI_AlignStack(unsigned int /*frameSize*/) {
616
SUB(64, R(RSP), Imm8(0x28));
619
void XEmitter::ABI_RestoreStack(unsigned int /*frameSize*/) {
620
ADD(64, R(RSP), Imm8(0x28));
624
// Unix64 Specific Code
625
void XEmitter::ABI_PushAllCalleeSavedRegsAndAdjustStack() {
632
PUSH(R15); //just to align stack. duped push/pop doesn't hurt.
636
void XEmitter::ABI_PopAllCalleeSavedRegsAndAdjustStack() {
646
void XEmitter::ABI_PushAllCallerSavedRegsAndAdjustStack() {
658
void XEmitter::ABI_PopAllCallerSavedRegsAndAdjustStack() {
670
void XEmitter::ABI_AlignStack(unsigned int /*frameSize*/) {
671
SUB(64, R(RSP), Imm8(0x08));
674
void XEmitter::ABI_RestoreStack(unsigned int /*frameSize*/) {
675
ADD(64, R(RSP), Imm8(0x08));