1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4/*
5trampoline for emulating required C functions for cgo in go (see cgo.go)
6(we convert cdecl calling convention to go and vice-versa)
7
8Since we're called from go and call into C we can cheat a bit with the calling conventions:
9 - in go all the registers are caller saved
10 - in C we have a couple of callee saved registers
11
12=> we can use BX, R12, R13, R14, R15 instead of the stack
13
14C Calling convention cdecl used here (we only need integer args):
151. arg: DI
162. arg: SI
173. arg: DX
184. arg: CX
195. arg: R8
206. arg: R9
21We don't need floats with these functions -> AX=0
22return value will be in AX
23*/
24#include "textflag.h"
25#include "go_asm.h"
26
27// these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions.
28
29TEXT x_cgo_init_trampoline(SB), NOSPLIT, $16
30 MOVQ DI, AX
31 MOVQ SI, BX
32 MOVQ ·x_cgo_init_call(SB), DX
33 MOVQ (DX), CX
34 CALL CX
35 RET
36
37TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT, $8
38 MOVQ DI, AX
39 MOVQ ·x_cgo_thread_start_call(SB), DX
40 MOVQ (DX), CX
41 CALL CX
42 RET
43
44TEXT x_cgo_setenv_trampoline(SB), NOSPLIT, $8
45 MOVQ DI, AX
46 MOVQ ·x_cgo_setenv_call(SB), DX
47 MOVQ (DX), CX
48 CALL CX
49 RET
50
51TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT, $8
52 MOVQ DI, AX
53 MOVQ ·x_cgo_unsetenv_call(SB), DX
54 MOVQ (DX), CX
55 CALL CX
56 RET
57
58TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT, $0
59 CALL ·x_cgo_notify_runtime_init_done(SB)
60 RET
61
62// func setg_trampoline(setg uintptr, g uintptr)
63TEXT ·setg_trampoline(SB), NOSPLIT, $0-16
64 MOVQ G+8(FP), DI
65 MOVQ setg+0(FP), BX
66 XORL AX, AX
67 CALL BX
68 RET
69
70TEXT threadentry_trampoline(SB), NOSPLIT, $16
71 MOVQ DI, AX
72 MOVQ ·threadentry_call(SB), DX
73 MOVQ (DX), CX
74 CALL CX
75 RET
76
77TEXT ·call5(SB), NOSPLIT, $0-56
78 MOVQ fn+0(FP), BX
79 MOVQ a1+8(FP), DI
80 MOVQ a2+16(FP), SI
81 MOVQ a3+24(FP), DX
82 MOVQ a4+32(FP), CX
83 MOVQ a5+40(FP), R8
84 XORL AX, AX
85 CALL BX
86 MOVQ AX, ret+48(FP)
87 RET