koffi 2.3.8 → 2.3.10-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/LICENSE.txt +22 -165
  3. package/README.md +2 -2
  4. package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_darwin_arm64/koffi.node +0 -0
  5. package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_darwin_x64/koffi.node +0 -0
  6. package/build/2.3.10-beta.1/koffi_freebsd_arm64/koffi.node +0 -0
  7. package/build/2.3.10-beta.1/koffi_freebsd_ia32/koffi.node +0 -0
  8. package/build/2.3.10-beta.1/koffi_freebsd_x64/koffi.node +0 -0
  9. package/build/2.3.10-beta.1/koffi_linux_arm32hf/koffi.node +0 -0
  10. package/build/2.3.10-beta.1/koffi_linux_arm64/koffi.node +0 -0
  11. package/build/2.3.10-beta.1/koffi_linux_ia32/koffi.node +0 -0
  12. package/build/2.3.10-beta.1/koffi_linux_riscv64hf64/koffi.node +0 -0
  13. package/build/2.3.10-beta.1/koffi_linux_x64/koffi.node +0 -0
  14. package/build/2.3.10-beta.1/koffi_openbsd_ia32/koffi.node +0 -0
  15. package/build/2.3.10-beta.1/koffi_openbsd_x64/koffi.node +0 -0
  16. package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_arm64/koffi.node +0 -0
  17. package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_ia32/koffi.node +0 -0
  18. package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_x64/koffi.node +0 -0
  19. package/doc/index.rst +2 -2
  20. package/doc/static/custom.css +18 -10
  21. package/package.json +7 -7
  22. package/src/cnoke/LICENSE.txt +22 -165
  23. package/src/cnoke/assets/FindCNoke.cmake +18 -10
  24. package/src/cnoke/assets/win_delay_hook.c +18 -10
  25. package/src/cnoke/cnoke.js +18 -10
  26. package/src/cnoke/package.json +1 -1
  27. package/src/cnoke/src/builder.js +18 -10
  28. package/src/cnoke/src/index.js +18 -10
  29. package/src/cnoke/src/tools.js +18 -10
  30. package/src/core/libcc/libcc.cc +93 -33
  31. package/src/core/libcc/libcc.hh +31 -14
  32. package/src/{koffi/src/index.d.ts → index.d.ts} +22 -14
  33. package/src/index.js +50 -0
  34. package/src/koffi/CMakeLists.txt +27 -19
  35. package/src/koffi/src/abi_arm32.cc +19 -11
  36. package/src/koffi/src/abi_arm32_asm.S +211 -0
  37. package/src/koffi/src/abi_arm64.cc +19 -11
  38. package/src/koffi/src/abi_arm64_asm.S +238 -0
  39. package/src/koffi/src/abi_arm64_asm.asm +207 -0
  40. package/src/koffi/src/abi_riscv64.cc +19 -11
  41. package/src/koffi/src/abi_riscv64_asm.S +239 -0
  42. package/src/koffi/src/abi_x64_sysv.cc +19 -11
  43. package/src/koffi/src/abi_x64_sysv_asm.S +272 -0
  44. package/src/koffi/src/abi_x64_win.cc +19 -11
  45. package/src/koffi/src/abi_x64_win_asm.asm +203 -0
  46. package/src/koffi/src/abi_x86.cc +19 -11
  47. package/src/koffi/src/abi_x86_asm.S +213 -0
  48. package/src/koffi/src/abi_x86_asm.asm +191 -0
  49. package/src/koffi/src/call.cc +18 -10
  50. package/src/koffi/src/call.hh +18 -10
  51. package/src/koffi/src/ffi.cc +18 -10
  52. package/src/koffi/src/ffi.hh +18 -10
  53. package/src/koffi/src/parser.cc +18 -10
  54. package/src/koffi/src/parser.hh +18 -10
  55. package/src/koffi/src/{abi_arm64_fwd.asm → trampolines/armasm.inc} +18 -193
  56. package/src/koffi/src/{abi_arm64_fwd.S → trampolines/gnu.inc} +18 -226
  57. package/src/koffi/src/{abi_x86_fwd.asm → trampolines/masm32.inc} +1042 -1203
  58. package/src/koffi/src/{abi_x64_win_fwd.asm → trampolines/masm64.inc} +1042 -1215
  59. package/src/koffi/src/{abi_trampolines.inc → trampolines/prototypes.inc} +18 -10
  60. package/src/koffi/src/util.cc +18 -10
  61. package/src/koffi/src/util.hh +18 -10
  62. package/src/koffi/src/win32.hh +18 -10
  63. package/src/koffi/build/2.3.8/koffi_freebsd_arm64/koffi.node +0 -0
  64. package/src/koffi/build/2.3.8/koffi_freebsd_ia32/koffi.node +0 -0
  65. package/src/koffi/build/2.3.8/koffi_freebsd_x64/koffi.node +0 -0
  66. package/src/koffi/build/2.3.8/koffi_linux_arm32hf/koffi.node +0 -0
  67. package/src/koffi/build/2.3.8/koffi_linux_arm64/koffi.node +0 -0
  68. package/src/koffi/build/2.3.8/koffi_linux_ia32/koffi.node +0 -0
  69. package/src/koffi/build/2.3.8/koffi_linux_riscv64hf64/koffi.node +0 -0
  70. package/src/koffi/build/2.3.8/koffi_linux_x64/koffi.node +0 -0
  71. package/src/koffi/build/2.3.8/koffi_openbsd_ia32/koffi.node +0 -0
  72. package/src/koffi/build/2.3.8/koffi_openbsd_x64/koffi.node +0 -0
  73. package/src/koffi/src/abi_arm32_fwd.S +0 -6344
  74. package/src/koffi/src/abi_riscv64_fwd.S +0 -6375
  75. package/src/koffi/src/abi_x64_sysv_fwd.S +0 -6410
  76. package/src/koffi/src/abi_x86_fwd.S +0 -6346
  77. package/src/koffi/src/index.js +0 -57
  78. /package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_arm64/koffi.exp +0 -0
  79. /package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_arm64/koffi.lib +0 -0
  80. /package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_ia32/koffi.exp +0 -0
  81. /package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_ia32/koffi.lib +0 -0
  82. /package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_x64/koffi.exp +0 -0
  83. /package/{src/koffi/build/2.3.8 → build/2.3.10-beta.1}/koffi_win32_x64/koffi.lib +0 -0
@@ -0,0 +1,211 @@
1
+ # Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ # this software and associated documentation files (the “Software”), to deal in
5
+ # the Software without restriction, including without limitation the rights to use,
6
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ # Software, and to permit persons to whom the Software is furnished to do so,
8
+ # subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ # OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ .syntax unified
23
+
24
+ #define SYMBOL(Symbol) Symbol
25
+
26
+ # Forward
27
+ # ----------------------------
28
+
29
+ # These three are the same, but they differ (in the C side) by their return type.
30
+ # Unlike the three next functions, these ones don't forward XMM argument registers.
31
+ .global ForwardCallGG
32
+ .global ForwardCallF
33
+ .global ForwardCallDDDD
34
+
35
+ # The X variants are slightly slower, and are used when XMM arguments must be forwarded.
36
+ .global ForwardCallXGG
37
+ .global ForwardCallXF
38
+ .global ForwardCallXDDDD
39
+
40
+ # Copy function pointer to r12, in order to save it through argument forwarding.
41
+ # Also make a copy of the SP to CallData::old_sp because the callback system might need it.
42
+ # Save RSP in fp (non-volatile), and use carefully assembled stack provided by caller.
43
+ .macro prologue
44
+ .cfi_startproc
45
+ push {fp, lr}
46
+ .cfi_def_cfa sp, 8
47
+ .cfi_offset 11, 4
48
+ .cfi_offset 14, 8
49
+ mov fp, sp
50
+ .cfi_def_cfa fp, 8
51
+ str fp, [r2, 0]
52
+ mov r12, r0
53
+ mov sp, r1
54
+ add sp, sp, #80
55
+ .endm
56
+
57
+ # Call native function.
58
+ # Once done, restore normal stack pointer and return.
59
+ # The return value is passed untouched through r0, r1, and or FP registers
60
+ .macro epilogue
61
+ blx r12
62
+ mov sp, fp
63
+ .cfi_def_cfa sp, 8
64
+ pop {fp, lr}
65
+ .cfi_def_cfa sp, 0
66
+ .cfi_restore 11
67
+ .cfi_restore 14
68
+ bx lr
69
+ .cfi_endproc
70
+ .endm
71
+
72
+ ForwardCallGG:
73
+ prologue
74
+ add r1, r1, #64
75
+ ldmia r1, {r0-r3}
76
+ epilogue
77
+
78
+ ForwardCallF:
79
+ prologue
80
+ add r1, r1, #64
81
+ ldmia r1, {r0-r3}
82
+ epilogue
83
+
84
+ ForwardCallDDDD:
85
+ prologue
86
+ add r1, r1, #64
87
+ ldmia r1, {r0-r3}
88
+ epilogue
89
+
90
+ ForwardCallXGG:
91
+ prologue
92
+ vldmia r1!, {d0-d7}
93
+ ldmia r1, {r0-r3}
94
+ epilogue
95
+
96
+ ForwardCallXF:
97
+ prologue
98
+ vldmia r1!, {d0-d7}
99
+ ldmia r1, {r0-r3}
100
+ epilogue
101
+
102
+ ForwardCallXDDDD:
103
+ prologue
104
+ vldmia r1!, {d0-d7}
105
+ ldmia r1, {r0-r3}
106
+ epilogue
107
+
108
+ # Callbacks
109
+ # ----------------------------
110
+
111
+ .global RelayCallback
112
+ .global CallSwitchStack
113
+
114
+ # First, make a copy of the GPR argument registers (r0 to r7).
115
+ # Then call the C function RelayCallback with the following arguments:
116
+ # static trampoline ID, a pointer to the saved GPR array, a pointer to the stack
117
+ # arguments of this call, and a pointer to a struct that will contain the result registers.
118
+ # After the call, simply load these registers from the output struct.
119
+ .macro trampoline id
120
+ .cfi_startproc
121
+ push {fp, lr}
122
+ .cfi_def_cfa sp, 8
123
+ .cfi_offset 11, 4
124
+ .cfi_offset 14, 8
125
+ sub sp, sp, #120
126
+ .cfi_def_cfa sp, 128
127
+ add r12, sp, 64
128
+ stmia r12!, {r0-r3}
129
+ mov r0, \id
130
+ mov r1, sp
131
+ add r2, sp, #128
132
+ mov r3, r12
133
+ bl RelayCallback
134
+ add sp, sp, #80
135
+ ldmia sp!, {r0-r1}
136
+ add sp, sp, #32
137
+ .cfi_def_cfa sp, 8
138
+ pop {fp, lr}
139
+ .cfi_def_cfa sp, 0
140
+ .cfi_restore 11
141
+ .cfi_restore 14
142
+ bx lr
143
+ .cfi_endproc
144
+ .endm
145
+
146
+ # Same thing, but also forwards the floating-point argument registers and loads them at the end.
147
+ .macro trampoline_vec id
148
+ .cfi_startproc
149
+ push {fp, lr}
150
+ .cfi_def_cfa sp, 8
151
+ .cfi_offset 11, 4
152
+ .cfi_offset 14, 8
153
+ sub sp, sp, #120
154
+ .cfi_def_cfa sp, 128
155
+ mov r12, sp
156
+ vstmia r12!, {d0-d7}
157
+ stmia r12!, {r0-r3}
158
+ mov r0, \id
159
+ mov r1, sp
160
+ add r2, sp, #128
161
+ mov r3, r12
162
+ bl RelayCallback
163
+ add sp, sp, #80
164
+ ldmia sp!, {r0-r1}
165
+ vldmia sp!, {d0-d3}
166
+ .cfi_def_cfa sp, 8
167
+ pop {fp, lr}
168
+ .cfi_def_cfa sp, 0
169
+ .cfi_restore 11
170
+ .cfi_restore 14
171
+ bx lr
172
+ .cfi_endproc
173
+ .endm
174
+
175
+ # When a callback is relayed, Koffi will call into Node.js and V8 to execute Javascript.
176
+ # The problem is that we're still running on the separate Koffi stack, and V8 will
177
+ # probably misdetect this as a "stack overflow". We have to restore the old
178
+ # stack pointer, call Node.js/V8 and go back to ours.
179
+ # The first three parameters (r0, r1, r2) are passed through untouched.
180
+ CallSwitchStack:
181
+ .cfi_startproc
182
+ push {fp, lr}
183
+ .cfi_def_cfa sp, 8
184
+ push {r4, r5}
185
+ .cfi_def_cfa sp, 16
186
+ .cfi_offset 11, 4
187
+ .cfi_offset 14, 8
188
+ mov fp, sp
189
+ ldr r4, [sp, 16]
190
+ ldr r5, [r4, 0]
191
+ sub r5, sp, r5
192
+ and r5, r5, #-16
193
+ str r5, [r4, 4]
194
+ ldr r4, [sp, 20]
195
+ mov sp, r3
196
+ blx r4
197
+ mov sp, fp
198
+ .cfi_def_cfa sp, 16
199
+ pop {r4, r5}
200
+ .cfi_def_cfa sp, 8
201
+ pop {fp, lr}
202
+ .cfi_def_cfa sp, 0
203
+ .cfi_restore 11
204
+ .cfi_restore 14
205
+ bx lr
206
+ .cfi_endproc
207
+
208
+ # Trampolines
209
+ # ----------------------------
210
+
211
+ #include "trampolines/gnu.inc"
@@ -1,15 +1,23 @@
1
- // This program is free software: you can redistribute it and/or modify
2
- // it under the terms of the GNU Lesser General Public License as published by
3
- // the Free Software Foundation, either version 3 of the License, or
4
- // (at your option) any later version.
1
+ // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
5
2
  //
6
- // This program is distributed in the hope that it will be useful,
7
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
8
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
- // GNU Lesser General Public License for more details.
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ // this software and associated documentation files (the “Software”), to deal in
5
+ // the Software without restriction, including without limitation the rights to use,
6
+ // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ // Software, and to permit persons to whom the Software is furnished to do so,
8
+ // subject to the following conditions:
10
9
  //
11
- // You should have received a copy of the GNU Lesser General Public License
12
- // along with this program. If not, see https://www.gnu.org/licenses/.
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ // OTHER DEALINGS IN THE SOFTWARE.
13
21
 
14
22
  #if defined(__aarch64__) || defined(_M_ARM64)
15
23
 
@@ -62,7 +70,7 @@ extern "C" napi_value CallSwitchStack(Napi::Function *func, size_t argc, napi_va
62
70
  uint8_t *old_sp, Span<uint8_t> *new_stack,
63
71
  napi_value (*call)(Napi::Function *func, size_t argc, napi_value *argv));
64
72
 
65
- #include "abi_trampolines.inc"
73
+ #include "trampolines/prototypes.inc"
66
74
 
67
75
  static HfaInfo IsHFA(const TypeInfo *type)
68
76
  {
@@ -0,0 +1,238 @@
1
+ # Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ # this software and associated documentation files (the “Software”), to deal in
5
+ # the Software without restriction, including without limitation the rights to use,
6
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ # Software, and to permit persons to whom the Software is furnished to do so,
8
+ # subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ # OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ #ifdef __APPLE__
23
+ #define SYMBOL(Symbol) _ ## Symbol
24
+ #else
25
+ #define SYMBOL(Symbol) Symbol
26
+ #endif
27
+
28
+ # Forward
29
+ # ----------------------------
30
+
31
+ #ifdef __APPLE__
32
+ # Fix "arm64 function not 4-byte aligned" linker warnings
33
+ .p2align 2
34
+ #endif
35
+
36
+ # These three are the same, but they differ (in the C side) by their return type.
37
+ # Unlike the three next functions, these ones don't forward XMM argument registers.
38
+ .global SYMBOL(ForwardCallGG)
39
+ .global SYMBOL(ForwardCallF)
40
+ .global SYMBOL(ForwardCallDDDD)
41
+
42
+ # The X variants are slightly slower, and are used when XMM arguments must be forwarded.
43
+ .global SYMBOL(ForwardCallXGG)
44
+ .global SYMBOL(ForwardCallXF)
45
+ .global SYMBOL(ForwardCallXDDDD)
46
+
47
+ # Copy function pointer to r9, in order to save it through argument forwarding.
48
+ # Also make a copy of the SP to CallData::old_sp because the callback system might need it.
49
+ # Save RSP in r29 (non-volatile), and use carefully assembled stack provided by caller.
50
+ .macro prologue
51
+ .cfi_startproc
52
+ hint #34
53
+ stp x29, x30, [sp, -16]!
54
+ .cfi_def_cfa sp, 16
55
+ .cfi_offset 29, 16
56
+ .cfi_offset 30, 8
57
+ mov x29, sp
58
+ .cfi_def_cfa x29, 16
59
+ str x29, [x2, 0]
60
+ mov x9, x0
61
+ add sp, x1, #136
62
+ .endm
63
+
64
+ # Call native function.
65
+ # Once done, restore normal stack pointer and return.
66
+ # The return value is passed untouched through r0, r1, v0 and/or v1.
67
+ .macro epilogue
68
+ blr x9
69
+ mov sp, x29
70
+ .cfi_def_cfa sp, 16
71
+ ldp x29, x30, [sp], 16
72
+ .cfi_def_cfa sp, 0
73
+ .cfi_restore 30
74
+ .cfi_restore 29
75
+ ret
76
+ .cfi_endproc
77
+ .endm
78
+
79
+ # Prepare general purpose argument registers from array passed by caller.
80
+ .macro forward_gpr
81
+ ldr x8, [x1, 64]
82
+ ldp x6, x7, [x1, 48]
83
+ ldp x4, x5, [x1, 32]
84
+ ldp x2, x3, [x1, 16]
85
+ ldp x0, x1, [x1, 0]
86
+ .endm
87
+
88
+ # Prepare vector argument registers from array passed by caller.
89
+ .macro forward_vec
90
+ ldp d6, d7, [x1, 120]
91
+ ldp d4, d5, [x1, 104]
92
+ ldp d2, d3, [x1, 88]
93
+ ldp d0, d1, [x1, 72]
94
+ .endm
95
+
96
+ SYMBOL(ForwardCallGG):
97
+ prologue
98
+ forward_gpr
99
+ epilogue
100
+
101
+ SYMBOL(ForwardCallF):
102
+ prologue
103
+ forward_gpr
104
+ epilogue
105
+
106
+ SYMBOL(ForwardCallDDDD):
107
+ prologue
108
+ forward_gpr
109
+ epilogue
110
+
111
+ SYMBOL(ForwardCallXGG):
112
+ prologue
113
+ forward_vec
114
+ forward_gpr
115
+ epilogue
116
+
117
+ SYMBOL(ForwardCallXF):
118
+ prologue
119
+ forward_vec
120
+ forward_gpr
121
+ epilogue
122
+
123
+ SYMBOL(ForwardCallXDDDD):
124
+ prologue
125
+ forward_vec
126
+ forward_gpr
127
+ epilogue
128
+
129
+ # Callbacks
130
+ # ----------------------------
131
+
132
+ .global SYMBOL(RelayCallback)
133
+ .global SYMBOL(CallSwitchStack)
134
+
135
+ # First, make a copy of the GPR argument registers (x0 to x7).
136
+ # Then call the C function RelayCallback with the following arguments:
137
+ # static trampoline ID, a pointer to the saved GPR array, a pointer to the stack
138
+ # arguments of this call, and a pointer to a struct that will contain the result registers.
139
+ # After the call, simply load these registers from the output struct.
140
+ .macro trampoline id
141
+ .cfi_startproc
142
+ hint #34
143
+ stp x29, x30, [sp, -16]!
144
+ .cfi_def_cfa sp, 16
145
+ .cfi_offset 29, 16
146
+ .cfi_offset 30, 8
147
+ sub sp, sp, #192
148
+ .cfi_def_cfa sp, 208
149
+ stp x0, x1, [sp, 0]
150
+ stp x2, x3, [sp, 16]
151
+ stp x4, x5, [sp, 32]
152
+ stp x6, x7, [sp, 48]
153
+ str x8, [sp, 64]
154
+ mov x0, \id
155
+ mov x1, sp
156
+ add x2, sp, #208
157
+ add x3, sp, #136
158
+ bl SYMBOL(RelayCallback)
159
+ ldp x0, x1, [sp, 136]
160
+ add sp, sp, #192
161
+ .cfi_def_cfa sp, 16
162
+ ldp x29, x30, [sp], 16
163
+ .cfi_def_cfa sp, 0
164
+ .cfi_restore 30
165
+ .cfi_restore 29
166
+ ret
167
+ .cfi_endproc
168
+ .endm
169
+
170
+ # Same thing, but also forwards the floating-point argument registers and loads them at the end.
171
+ .macro trampoline_vec id
172
+ .cfi_startproc
173
+ hint #34
174
+ stp x29, x30, [sp, -16]!
175
+ .cfi_def_cfa sp, 16
176
+ .cfi_offset 29, 16
177
+ .cfi_offset 30, 8
178
+ sub sp, sp, #192
179
+ .cfi_def_cfa sp, 208
180
+ stp x0, x1, [sp, 0]
181
+ stp x2, x3, [sp, 16]
182
+ stp x4, x5, [sp, 32]
183
+ stp x6, x7, [sp, 48]
184
+ str x8, [sp, 64]
185
+ stp d0, d1, [sp, 72]
186
+ stp d2, d3, [sp, 88]
187
+ stp d4, d5, [sp, 104]
188
+ stp d6, d7, [sp, 120]
189
+ mov x0, \id
190
+ mov x1, sp
191
+ add x2, sp, #208
192
+ add x3, sp, #136
193
+ bl SYMBOL(RelayCallback)
194
+ ldp x0, x1, [sp, 136]
195
+ ldp d0, d1, [sp, 152]
196
+ ldp d2, d3, [sp, 168]
197
+ add sp, sp, #192
198
+ .cfi_def_cfa sp, 16
199
+ ldp x29, x30, [sp], 16
200
+ .cfi_def_cfa sp, 0
201
+ .cfi_restore 30
202
+ .cfi_restore 29
203
+ ret
204
+ .cfi_endproc
205
+ .endm
206
+
207
+ # When a callback is relayed, Koffi will call into Node.js and V8 to execute Javascript.
208
+ # The problem is that we're still running on the separate Koffi stack, and V8 will
209
+ # probably misdetect this as a "stack overflow". We have to restore the old
210
+ # stack pointer, call Node.js/V8 and go back to ours.
211
+ # The first three parameters (x0, x1, x2) are passed through untouched.
212
+ SYMBOL(CallSwitchStack):
213
+ .cfi_startproc
214
+ hint #34
215
+ stp x29, x30, [sp, -16]!
216
+ .cfi_def_cfa sp, 16
217
+ .cfi_offset 29, 16
218
+ .cfi_offset 30, 8
219
+ mov x29, sp
220
+ ldr x9, [x4, 0]
221
+ sub x9, sp, x9
222
+ and x9, x9, #-16
223
+ str x9, [x4, 8]
224
+ mov sp, x3
225
+ blr x5
226
+ mov sp, x29
227
+ .cfi_def_cfa sp, 16
228
+ ldp x29, x30, [sp], 16
229
+ .cfi_def_cfa sp, 0
230
+ .cfi_restore 30
231
+ .cfi_restore 29
232
+ ret
233
+ .cfi_endproc
234
+
235
+ # Trampolines
236
+ # ----------------------------
237
+
238
+ #include "trampolines/gnu.inc"
@@ -0,0 +1,207 @@
1
+ ; Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+ ;
3
+ ; Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ ; this software and associated documentation files (the “Software”), to deal in
5
+ ; the Software without restriction, including without limitation the rights to use,
6
+ ; copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ ; Software, and to permit persons to whom the Software is furnished to do so,
8
+ ; subject to the following conditions:
9
+ ;
10
+ ; The above copyright notice and this permission notice shall be included in all
11
+ ; copies or substantial portions of the Software.
12
+ ;
13
+ ; THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ ; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ ; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ ; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ ; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ ; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ ; OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ AREA |.text|, CODE
23
+
24
+ ; Forward
25
+ ; ----------------------------
26
+
27
+ ; These three are the same, but they differ (in the C side) by their return type.
28
+ ; Unlike the three next functions, these ones don't forward XMM argument registers.
29
+ EXPORT ForwardCallGG
30
+ EXPORT ForwardCallF
31
+ EXPORT ForwardCallDDDD
32
+
33
+ ; The X variants are slightly slower, and are used when XMM arguments must be forwarded.
34
+ EXPORT ForwardCallXGG
35
+ EXPORT ForwardCallXF
36
+ EXPORT ForwardCallXDDDD
37
+
38
+ ; Copy function pointer to r9, in order to save it through argument forwarding.
39
+ ; Save RSP in r29 (non-volatile), and use carefully assembled stack provided by caller.
40
+ MACRO
41
+ prologue
42
+
43
+ stp x29, x30, [sp, -16]!
44
+ mov x29, sp
45
+ str x29, [x2, 0]
46
+ mov x9, x0
47
+ add sp, x1, #136
48
+ MEND
49
+
50
+ ; Call native function.
51
+ ; Once done, restore normal stack pointer and return.
52
+ ; The return value is passed untouched through r0, r1, v0 and/or v1.
53
+ MACRO
54
+ epilogue
55
+
56
+ blr x9
57
+ mov sp, x29
58
+ ldp x29, x30, [sp], 16
59
+ ret
60
+ MEND
61
+
62
+ ; Prepare general purpose argument registers from array passed by caller.
63
+ MACRO
64
+ forward_gpr
65
+
66
+ ldr x8, [x1, 64]
67
+ ldp x6, x7, [x1, 48]
68
+ ldp x4, x5, [x1, 32]
69
+ ldp x2, x3, [x1, 16]
70
+ ldp x0, x1, [x1, 0]
71
+ MEND
72
+
73
+ ; Prepare vector argument registers from array passed by caller.
74
+ MACRO
75
+ forward_vec
76
+
77
+ ldp d6, d7, [x1, 120]
78
+ ldp d4, d5, [x1, 104]
79
+ ldp d2, d3, [x1, 88]
80
+ ldp d0, d1, [x1, 72]
81
+ MEND
82
+
83
+ ForwardCallGG PROC
84
+ prologue
85
+ forward_gpr
86
+ epilogue
87
+ ENDP
88
+
89
+ ForwardCallF PROC
90
+ prologue
91
+ forward_gpr
92
+ epilogue
93
+ ENDP
94
+
95
+ ForwardCallDDDD PROC
96
+ prologue
97
+ forward_gpr
98
+ epilogue
99
+ ENDP
100
+
101
+ ForwardCallXGG PROC
102
+ prologue
103
+ forward_vec
104
+ forward_gpr
105
+ epilogue
106
+ ENDP
107
+
108
+ ForwardCallXF PROC
109
+ prologue
110
+ forward_vec
111
+ forward_gpr
112
+ epilogue
113
+ ENDP
114
+
115
+ ForwardCallXDDDD PROC
116
+ prologue
117
+ forward_vec
118
+ forward_gpr
119
+ epilogue
120
+ ENDP
121
+
122
+ ; Callbacks
123
+ ; ----------------------------
124
+
125
+ EXPORT RelayCallback
126
+ EXTERN RelayCallback
127
+ EXPORT CallSwitchStack
128
+
129
+ ; First, make a copy of the GPR argument registers (x0 to x7).
130
+ ; Then call the C function RelayCallback with the following arguments:
131
+ ; static trampoline ID, a pointer to the saved GPR array, a pointer to the stack
132
+ ; arguments of this call, and a pointer to a struct that will contain the result registers.
133
+ ; After the call, simply load these registers from the output struct.
134
+ MACRO
135
+ trampoline $ID
136
+
137
+ stp x29, x30, [sp, -16]!
138
+ sub sp, sp, #192
139
+ stp x0, x1, [sp, 0]
140
+ stp x2, x3, [sp, 16]
141
+ stp x4, x5, [sp, 32]
142
+ stp x6, x7, [sp, 48]
143
+ str x8, [sp, 64]
144
+ mov x0, $ID
145
+ mov x1, sp
146
+ add x2, sp, #208
147
+ add x3, sp, #136
148
+ bl RelayCallback
149
+ ldp x0, x1, [sp, 136]
150
+ add sp, sp, #192
151
+ ldp x29, x30, [sp], 16
152
+ ret
153
+ MEND
154
+
155
+ ; Same thing, but also forwards the floating-point argument registers and loads them at the end.
156
+ MACRO
157
+ trampoline_vec $ID
158
+
159
+ stp x29, x30, [sp, -16]!
160
+ sub sp, sp, #192
161
+ stp x0, x1, [sp, 0]
162
+ stp x2, x3, [sp, 16]
163
+ stp x4, x5, [sp, 32]
164
+ stp x6, x7, [sp, 48]
165
+ str x8, [sp, 64]
166
+ stp d0, d1, [sp, 72]
167
+ stp d2, d3, [sp, 88]
168
+ stp d4, d5, [sp, 104]
169
+ stp d6, d7, [sp, 120]
170
+ mov x0, $ID
171
+ mov x1, sp
172
+ add x2, sp, #208
173
+ add x3, sp, #136
174
+ bl RelayCallback
175
+ ldp x0, x1, [sp, 136]
176
+ ldp d0, d1, [sp, 152]
177
+ ldp d2, d3, [sp, 168]
178
+ add sp, sp, #192
179
+ ldp x29, x30, [sp], 16
180
+ ret
181
+ MEND
182
+
183
+ ; When a callback is relayed, Koffi will call into Node.js and V8 to execute Javascript.
184
+ ; The problem is that we're still running on the separate Koffi stack, and V8 will
185
+ ; probably misdetect this as a "stack overflow". We have to restore the old
186
+ ; stack pointer, call Node.js/V8 and go back to ours.
187
+ ; The first three parameters (x0, x1, x2) are passed through untouched.
188
+ CallSwitchStack PROC
189
+ stp x29, x30, [sp, -16]!
190
+ mov x29, sp
191
+ ldr x9, [x4, 0]
192
+ sub x9, sp, x9
193
+ and x9, x9, #-16
194
+ str x9, [x4, 8]
195
+ mov sp, x3
196
+ blr x5
197
+ mov sp, x29
198
+ ldp x29, x30, [sp], 16
199
+ ret
200
+ ENDP
201
+
202
+ ; Trampolines
203
+ ; ----------------------------
204
+
205
+ INCLUDE trampolines/armasm.inc
206
+
207
+ END