| 1 |
// SPDX-License-Identifier: Apache-2.0 |
| 2 |
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors |
| 3 |
|
| 4 |
//go:build !cgo && (darwin || linux || freebsd) |
| 5 |
|
| 6 |
/* |
| 7 |
trampoline for emulating required C functions for cgo in go (see cgo.go) |
| 8 |
(we convert cdecl calling convention to go and vice-versa) |
| 9 |
|
| 10 |
C Calling convention cdecl used here (we only need integer args): |
| 11 |
1. arg: DI |
| 12 |
2. arg: SI |
| 13 |
3. arg: DX |
| 14 |
4. arg: CX |
| 15 |
5. arg: R8 |
| 16 |
6. arg: R9 |
| 17 |
We don't need floats with these functions -> AX=0 |
| 18 |
return value will be in AX |
| 19 |
temporary register is R11 |
| 20 |
*/ |
| 21 |
#include "textflag.h" |
| 22 |
#include "go_asm.h" |
| 23 |
#include "abi_amd64.h" |
| 24 |
|
| 25 |
// these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions. |
| 26 |
|
| 27 |
TEXT x_cgo_init_trampoline(SB), NOSPLIT, $16 |
| 28 |
MOVQ DI, AX |
| 29 |
MOVQ SI, BX |
| 30 |
MOVQ ·x_cgo_init_call(SB), R11 |
| 31 |
MOVQ (R11), R11 |
| 32 |
CALL R11 |
| 33 |
RET |
| 34 |
|
| 35 |
TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT, $8 |
| 36 |
MOVQ DI, AX |
| 37 |
MOVQ ·x_cgo_thread_start_call(SB), R11 |
| 38 |
MOVQ (R11), R11 |
| 39 |
CALL R11 |
| 40 |
RET |
| 41 |
|
| 42 |
TEXT x_cgo_setenv_trampoline(SB), NOSPLIT, $8 |
| 43 |
MOVQ DI, AX |
| 44 |
MOVQ ·x_cgo_setenv_call(SB), R11 |
| 45 |
MOVQ (R11), R11 |
| 46 |
CALL R11 |
| 47 |
RET |
| 48 |
|
| 49 |
TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT, $8 |
| 50 |
MOVQ DI, AX |
| 51 |
MOVQ ·x_cgo_unsetenv_call(SB), R11 |
| 52 |
MOVQ (R11), R11 |
| 53 |
CALL R11 |
| 54 |
RET |
| 55 |
|
| 56 |
TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT, $0 |
| 57 |
JMP ·x_cgo_notify_runtime_init_done(SB) |
| 58 |
|
| 59 |
TEXT x_cgo_bindm_trampoline(SB), NOSPLIT, $0 |
| 60 |
JMP ·x_cgo_bindm(SB) |
| 61 |
|
| 62 |
// func setg_trampoline(setg uintptr, g uintptr) |
| 63 |
TEXT ·setg_trampoline(SB), NOSPLIT, $0-16 |
| 64 |
MOVQ G+8(FP), DI |
| 65 |
MOVQ setg+0(FP), R11 |
| 66 |
XORL AX, AX |
| 67 |
CALL R11 |
| 68 |
RET |
| 69 |
|
| 70 |
TEXT threadentry_trampoline(SB), NOSPLIT, $0 |
| 71 |
// See crosscall2. |
| 72 |
PUSH_REGS_HOST_TO_ABI0() |
| 73 |
|
| 74 |
// X15 is designated by Go as a fixed zero register. |
| 75 |
// Calling directly into ABIInternal, ensure it is zero. |
| 76 |
PXOR X15, X15 |
| 77 |
|
| 78 |
MOVQ DI, AX |
| 79 |
MOVQ ·threadentry_call(SB), R11 |
| 80 |
MOVQ (R11), R11 |
| 81 |
CALL R11 |
| 82 |
|
| 83 |
POP_REGS_HOST_TO_ABI0() |
| 84 |
RET |
| 85 |
|
| 86 |
TEXT ·call5(SB), NOSPLIT, $0-56 |
| 87 |
MOVQ fn+0(FP), R11 |
| 88 |
MOVQ a1+8(FP), DI |
| 89 |
MOVQ a2+16(FP), SI |
| 90 |
MOVQ a3+24(FP), DX |
| 91 |
MOVQ a4+32(FP), CX |
| 92 |
MOVQ a5+40(FP), R8 |
| 93 |
|
| 94 |
XORL AX, AX // no floats |
| 95 |
|
| 96 |
PUSHQ BP // save BP |
| 97 |
MOVQ SP, BP // save SP inside BP bc BP is callee-saved |
| 98 |
SUBQ $16, SP // allocate space for alignment |
| 99 |
ANDQ $-16, SP // align on 16 bytes for SSE |
| 100 |
|
| 101 |
CALL R11 |
| 102 |
|
| 103 |
MOVQ BP, SP // get SP back |
| 104 |
POPQ BP // restore BP |
| 105 |
|
| 106 |
MOVQ AX, ret+48(FP) |
| 107 |
RET |