github.com/notti/nocgo@v0.0.0-20190619201224-fc443047424c/internal/fakecgo/trampoline_amd64.s (about) 1 /* 2 trampoline for emulating required C functions for cgo in go (see cgo.go) 3 (we convert cdecl calling convention to go and vice-versa) 4 5 Since we're called from go and call into C we can cheat a bit with the calling conventions: 6 - in go all the registers are caller saved 7 - in C we have a couple of callee saved registers 8 9 => we can use BX, R12, R13, R14, R15 instead of the stack 10 11 C Calling convention cdecl used here (we only need integer args): 12 1. arg: DI 13 2. arg: SI 14 3. arg: DX 15 4. arg: CX 16 5. arg: R8 17 6. arg: R9 18 We don't need floats with these functions -> AX=0 19 return value will be in AX 20 */ 21 #include "textflag.h" 22 23 TEXT x_cgo_init_trampoline(SB),NOSPLIT,$16 24 MOVQ DI, 0(SP) 25 MOVQ SI, 8(SP) 26 CALL ·x_cgo_init(SB) 27 RET 28 29 TEXT x_cgo_thread_start_trampoline(SB),NOSPLIT,$8 30 MOVQ DI, 0(SP) 31 CALL ·x_cgo_thread_start(SB) 32 RET 33 34 TEXT ·threadentry_trampoline(SB),NOSPLIT,$16 35 MOVQ DI, 0(SP) 36 CALL ·threadentry(SB) 37 MOVQ 8(SP), AX 38 RET 39 40 // func setg_trampoline(setg uintptr, g uintptr) 41 TEXT ·setg_trampoline(SB),NOSPLIT,$0-16 42 MOVQ g+8(FP), DI 43 MOVQ setg+0(FP), AX 44 CALL AX 45 RET 46 47 TEXT x_cgo_notify_runtime_init_done_trampoline(SB),NOSPLIT,$0 48 CALL ·x_cgo_notify_runtime_init_done(SB) 49 RET 50 51 TEXT x_cgo_setenv_trampoline(SB),NOSPLIT,$8 52 MOVQ DI, 0(SP) 53 CALL ·x_cgo_setenv(SB) 54 RET 55 56 TEXT x_cgo_unsetenv_trampoline(SB),NOSPLIT,$8 57 MOVQ DI, 0(SP) 58 CALL ·x_cgo_unsetenv(SB) 59 RET 60 61 // func asmlibccall6(fn, n, args uintptr) uintptr 62 TEXT ·asmlibccall6(SB),NOSPLIT,$0-32 63 MOVQ n+8(FP), AX 64 65 TESTQ AX, AX 66 JZ skipargs 67 68 MOVQ args+16(FP), AX 69 MOVQ 0(AX), DI 70 MOVQ 8(AX), SI 71 MOVQ 16(AX), DX 72 MOVQ 24(AX), CX 73 MOVQ 32(AX), R8 74 MOVQ 40(AX), R9 75 76 skipargs: 77 78 MOVQ fn+0(FP), BX 79 80 MOVQ SP, R12 81 ANDQ $~0xF, SP // 16 byte alignment for cdecl 82 83 XORQ AX, AX // no fp arguments 84 CALL BX 85 86 MOVQ R12, SP 87 88 MOVQ AX, ret+24(FP) 89 90 RET