1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
3
4//go:build linux
5
6#include "textflag.h"
7#include "go_asm.h"
8#include "funcdata.h"
9#include "abi_loong64.h"
10
11TEXT callbackasm1(SB), NOSPLIT|NOFRAME, $0
12 NO_LOCAL_POINTERS
13
14 SUBV $(16*8), R3, R14
15 MOVD F0, 0(R14)
16 MOVD F1, 8(R14)
17 MOVD F2, 16(R14)
18 MOVD F3, 24(R14)
19 MOVD F4, 32(R14)
20 MOVD F5, 40(R14)
21 MOVD F6, 48(R14)
22 MOVD F7, 56(R14)
23 MOVV R4, 64(R14)
24 MOVV R5, 72(R14)
25 MOVV R6, 80(R14)
26 MOVV R7, 88(R14)
27 MOVV R8, 96(R14)
28 MOVV R9, 104(R14)
29 MOVV R10, 112(R14)
30 MOVV R11, 120(R14)
31
32 // Adjust SP by frame size.
33 // Layout: R1(8) + R30(8) + callbackArgs(48) = 64 bytes below R14,
34 // plus 128 bytes of saved registers above = 192 = 24*8.
35 // callbackArgs is placed at SP+16, ending at SP+64 = R14.
36 SUBV $(24*8), R3
37
38 // It is important to save R30 because the go assembler
39 // uses it for move instructions for a variable.
40 // This line:
41 // MOVV ·callbackWrap_call(SB), R4
42 // Creates the instructions:
43 // PCALAU12I off1(PC), R30
44 // MOVV off2(R30), R4
45 // R30 is a callee saved register so we are responsible
46 // for ensuring its value doesn't change. So save it and
47 // restore it at the end of this function.
48 // R1 is the link register. crosscall2 doesn't save it
49 // so it's saved here.
50 MOVV R1, 0(R3)
51 MOVV R30, 8(R3)
52
53 // Create a struct callbackArgs on our stack at SP+16
54 // (right after the saved R1 and R30).
55 MOVV $16(R3), R13
56 MOVV R12, callbackArgs_index(R13) // callback index
57 MOVV R14, callbackArgs_args(R13) // address of args vector
58 MOVV $0, callbackArgs_result(R13) // result
59
60 // Move parameters into registers
61 // Get the ABIInternal function pointer
62 // without <ABIInternal> by using a closure.
63 MOVV ·callbackWrap_call(SB), R4
64 MOVV (R4), R4 // fn unsafe.Pointer
65 MOVV R13, R5 // frame (&callbackArgs{...})
66 MOVV $0, R7 // ctxt uintptr
67
68 JAL crosscall2(SB)
69
70 // Get callback result.
71 MOVV $16(R3), R13
72 MOVV callbackArgs_result(R13), R4
73
74 // Restore LR and R30
75 MOVV 0(R3), R1
76 MOVV 8(R3), R30
77 ADDV $(24*8), R3
78
79 RET