Debian Codegraph @

texk/web2c/luatexdir/luaffi/call_x86.h

raw
1 /*
2 ** This file has been pre-processed with DynASM.
3 ** https://luajit.org/dynasm.html
4 ** DynASM version 1.5.0, DynASM x86 version 1.4.0
5 ** DO NOT EDIT! The original file is in "call_x86.dasc".
6 */
7
8 /* vim: ts=4 sw=4 sts=4 et tw=78
9 * Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved.
10 * Portions copyright (c) 2011 James R. McKaskill.
11 *
12 * This source code is licensed under the BSD-style license found in the
13 * LICENSE file in the root directory of this source tree. An additional grant
14 * of patent rights can be found in the PATENTS file in the same directory.
15 */
16 //|.if X64
17 //|.arch x64
18 //|.else
19 //|.arch x86
20 #if DASM_VERSION != 10500
21 #error "Version mismatch between DynASM and included encoding engine"
22 #endif
23 //|.endif
24
25 //|.actionlist build_actionlist
26 static const unsigned char build_actionlist[1915] = {
27 139,141,233,255,139,141,233,139,149,233,255,137,132,253,36,233,255,137,132,
28 253,36,233,137,148,253,36,233,255,221,133,233,255,217,133,233,255,252,243,
29 15,126,133,233,255,252,243,15,90,133,233,255,221,156,253,36,233,255,217,156,
30 253,36,233,255,102,15,214,132,253,36,233,255,252,242,15,90,192,102,15,214,
31 132,253,36,233,255,252,242,15,90,192,102,15,126,132,253,36,233,255,85,137,
32 229,87,129,252,236,239,255,137,77,252,248,137,85,252,244,255,191,237,255,
33 199,68,36,8,237,199,68,36,4,237,137,60,36,232,251,1,0,255,199,68,36,8,237,
34 199,68,36,4,252,255,252,255,252,255,252,255,137,60,36,232,251,1,0,255,199,
35 68,36,8,237,199,68,36,4,237,137,60,36,232,251,1,0,199,68,36,8,237,199,68,
36 36,4,252,255,252,255,252,255,252,255,137,60,36,232,251,1,1,255,137,8,199,
37 68,36,4,252,254,252,255,252,255,252,255,137,60,36,232,251,1,2,255,199,68,
38 36,8,237,199,68,36,4,0,0,0,0,137,60,36,232,251,1,1,255,137,8,137,80,4,255,
39 137,8,255,102,15,214,0,255,217,24,255,217,88,4,255,221,24,255,221,88,8,255,
40 221,92,36,4,137,60,36,232,251,1,3,255,15,182,201,137,76,36,4,137,60,36,232,
41 251,1,4,255,15,182,201,255,15,190,201,255,137,76,36,4,137,60,36,232,251,1,
42 5,255,15,183,201,255,15,191,201,255,137,76,36,4,137,60,36,232,251,1,6,255,
43 199,68,36,12,0,0,0,0,199,68,36,8,237,199,68,36,4,237,137,60,36,232,251,1,
44 7,255,199,68,36,8,237,199,68,36,4,252,254,252,255,252,255,252,255,137,60,
45 36,232,251,1,0,199,68,36,12,237,199,68,36,8,252,255,252,255,252,255,252,255,
46 199,68,36,4,252,254,252,255,252,255,252,255,137,60,36,232,251,1,8,137,68,
47 36,32,199,68,36,4,252,252,252,255,252,255,252,255,137,60,36,232,251,1,9,139,
48 68,36,32,255,199,68,36,8,237,199,68,36,4,252,254,252,255,252,255,252,255,
49 137,60,36,232,251,1,0,199,68,36,12,237,199,68,36,8,252,255,252,255,252,255,
50 252,255,199,68,36,4,252,254,252,255,252,255,252,255,137,60,36,232,251,1,10,
51 137,68,36,32,199,68,36,4,252,252,252,255,252,255,252,255,137,60,36,232,251,
52 1,9,139,68,36,32,255,199,68,36,4,252,254,252,255,252,255,252,255,137,60,36,
53 232,251,1,9,255,199,68,36,4,252,255,252,255,252,255,252,255,137,60,36,232,
54 251,1,11,255,199,68,36,4,252,255,252,255,252,255,252,255,137,60,36,232,251,
55 1,12,255,137,68,36,32,199,68,36,4,252,253,252,255,252,255,252,255,137,60,
56 36,232,251,1,9,139,68,36,32,255,199,68,36,4,252,255,252,255,252,255,252,255,
57 137,60,36,232,251,1,13,255,199,68,36,4,252,255,252,255,252,255,252,255,137,
58 60,36,232,251,1,14,255,137,68,36,32,137,84,36,36,199,68,36,4,252,253,252,
59 255,252,255,252,255,137,60,36,232,251,1,9,139,68,36,32,139,84,36,36,255,199,
60 68,36,4,252,255,252,255,252,255,252,255,137,60,36,232,251,1,15,137,68,36,
61 32,199,68,36,4,252,253,252,255,252,255,252,255,137,60,36,232,251,1,9,139,
62 68,36,32,255,199,68,36,4,252,255,252,255,252,255,252,255,137,60,36,232,251,
63 1,16,255,221,92,36,32,199,68,36,4,252,253,252,255,252,255,252,255,137,60,
64 36,232,251,1,9,221,68,36,32,255,199,68,36,4,252,255,252,255,252,255,252,255,
65 137,60,36,232,251,1,17,137,68,36,32,137,84,36,36,199,68,36,4,252,253,252,
66 255,252,255,252,255,137,60,36,232,251,1,9,139,68,36,32,139,84,36,36,255,199,
67 68,36,4,252,255,252,255,252,255,252,255,137,60,36,232,251,1,18,102,15,214,
68 68,36,32,102,15,214,76,36,40,199,68,36,4,252,253,252,255,252,255,252,255,
69 137,60,36,232,251,1,9,252,243,15,126,68,36,32,252,243,15,126,76,36,40,255,
70 139,141,233,199,68,36,8,252,255,252,255,252,255,252,255,137,124,36,4,137,
71 12,36,232,251,1,18,131,252,236,4,199,68,36,4,252,253,252,255,252,255,252,
72 255,137,60,36,232,251,1,9,255,139,125,252,252,137,252,236,93,194,236,255,
73 85,137,229,87,86,139,189,233,131,252,236,16,137,60,36,232,251,1,19,137,198,
74 129,252,248,239,255,15,141,244,248,102,184,0,0,199,68,36,4,237,137,60,36,
75 232,251,1,20,248,2,15,142,244,247,102,184,0,0,199,68,36,4,237,137,60,36,232,
76 251,1,20,255,15,141,244,247,102,184,0,0,199,68,36,4,237,137,60,36,232,251,
77 1,20,255,248,1,255,193,224,4,41,196,129,252,236,239,255,199,68,36,8,237,199,
78 68,36,4,0,0,0,0,137,60,36,232,251,1,1,131,252,236,16,255,199,68,36,12,237,
79 199,68,36,8,237,199,68,36,4,237,137,60,36,232,251,1,8,255,199,68,36,12,237,
80 199,68,36,8,237,199,68,36,4,237,137,60,36,232,251,1,21,255,199,68,36,12,237,
81 199,68,36,8,237,199,68,36,4,237,137,60,36,232,251,1,10,255,199,68,36,4,237,
82 137,60,36,232,251,1,12,255,15,182,192,255,15,190,192,255,15,183,192,255,15,
83 191,192,255,199,68,36,4,237,137,60,36,232,251,1,12,131,252,248,0,15,149,208,
84 15,182,192,255,199,68,36,4,237,137,60,36,232,251,1,11,255,199,68,36,4,237,
85 137,60,36,232,251,1,15,255,199,68,36,4,237,137,60,36,232,251,1,13,255,199,
86 68,36,4,237,137,60,36,232,251,1,14,255,199,68,36,4,237,137,60,36,232,251,
87 1,16,255,199,68,36,4,237,137,60,36,232,251,1,18,255,252,243,15,126,193,255,
88 141,132,253,36,233,131,252,236,4,199,68,36,8,237,137,124,36,4,137,4,36,232,
89 251,1,18,255,199,68,36,4,237,137,60,36,232,251,1,17,255,199,68,36,4,237,137,
90 60,36,232,251,1,17,137,4,36,217,4,36,255,137,20,36,217,4,36,255,137,224,129,
91 192,239,137,68,36,12,137,116,36,8,199,68,36,4,237,137,60,36,232,251,1,22,
92 255,185,237,139,1,137,4,36,232,251,1,23,255,131,196,32,255,139,148,253,36,
93 233,255,139,12,36,255,129,196,239,255,232,251,1,24,131,252,236,48,255,137,
94 68,36,32,232,251,1,25,185,237,137,1,199,68,36,8,237,199,68,36,4,237,137,60,
95 36,232,251,1,1,139,76,36,32,137,8,184,1,0,0,0,139,117,252,248,139,125,252,
96 252,137,252,236,93,195,255,137,68,36,32,232,251,1,25,185,237,137,1,139,68,
97 36,32,137,68,36,4,137,60,36,232,251,1,26,184,1,0,0,0,139,117,252,248,139,
98 125,252,252,137,252,236,93,195,255,137,84,36,36,137,68,36,32,232,251,1,25,
99 185,237,137,1,199,68,36,8,237,199,68,36,4,0,0,0,0,137,60,36,232,251,1,1,139,
100 76,36,36,139,84,36,32,137,72,4,137,16,184,1,0,0,0,139,117,252,248,139,125,
101 252,252,137,252,236,93,195,255,137,68,36,32,137,84,36,36,232,251,1,25,185,
102 237,137,1,199,68,36,8,237,199,68,36,4,237,137,60,36,232,251,1,1,139,76,36,
103 32,137,8,139,76,36,36,137,72,4,184,1,0,0,0,139,117,252,248,139,125,252,252,
104 137,252,236,93,195,255,131,252,236,4,232,251,1,25,185,237,137,1,184,1,0,0,
105 0,139,117,252,248,139,125,252,252,137,252,236,93,195,255,232,251,1,25,185,
106 237,137,1,184,0,0,0,0,139,117,252,248,139,125,252,252,137,252,236,93,195,
107 255,15,182,192,137,68,36,32,232,251,1,25,185,237,137,1,139,68,36,32,137,68,
108 36,4,137,60,36,232,251,1,4,184,1,0,0,0,139,117,252,248,139,125,252,252,137,
109 252,236,93,195,255,137,68,36,32,232,251,1,25,185,237,137,1,139,68,36,32,137,
110 68,36,4,137,60,36,232,251,1,5,184,1,0,0,0,139,117,252,248,139,125,252,252,
111 137,252,236,93,195,255,137,68,36,32,232,251,1,25,185,237,137,1,139,68,36,
112 32,137,68,36,4,137,60,36,232,251,1,6,184,1,0,0,0,139,117,252,248,139,125,
113 252,252,137,252,236,93,195,255,221,92,36,4,232,251,1,25,185,237,137,1,137,
114 60,36,232,251,1,3,184,1,0,0,0,139,117,252,248,139,125,252,252,137,252,236,
115 93,195,255
116 };
117
118 //|.globalnames globnames
119 static const char *const globnames[] = {
120 (const char *)0
121 };
122 //|.externnames extnames
123 static const char *const extnames[] = {
124 "lua_rawgeti",
125 "push_cdata",
126 "lua_remove",
127 "lua_pushnumber",
128 "lua_pushboolean",
129 "push_int",
130 "push_uint",
131 "lua_callk",
132 "check_typed_pointer",
133 "lua_settop",
134 "check_enum",
135 "check_uint32",
136 "check_int32",
137 "check_uint64",
138 "check_int64",
139 "check_uintptr",
140 "check_double",
141 "check_complex_float",
142 "check_complex_double",
143 "lua_gettop",
144 "luaL_error",
145 "check_typed_cfunction",
146 "unpack_varargs_stack",
147 "SetLastError",
148 "FUNCTION",
149 "GetLastError",
150 "lua_pushinteger",
151 (const char *)0
152 };
153
154 //|.if not X64
155 //|.define RET_H, edx // for int64_t returns
156 //|.define RET_L, eax
157 //|.endif
158
159 //|.if X64
160 //|.define L_ARG, r12
161 //|.define TOP, r13
162 //|.else
163 //|.define L_ARG, rdi
164 //|.define TOP, rsi
165 //|.endif
166
167 //|.if X64WIN
168 //|
169 //|.macro call_rrrp, func, arg0, arg1, arg2, arg3
170 //| mov64 r9, arg3
171 //| mov r8, arg2
172 //| mov rdx, arg1
173 //| mov rcx, arg0
174 //| call func
175 //|.endmacro
176 //|.macro call_rrrr, func, arg0, arg1, arg2, arg3
177 //| mov r9, arg3
178 //| mov r8, arg2
179 //| mov rdx, arg1
180 //| mov rcx, arg0
181 //| call func
182 //|.endmacro
183 //|
184 //|.macro call_rrp, func, arg0, arg1, arg2
185 //| mov64 r8, arg2
186 //| mov rdx, arg1
187 //| mov rcx, arg0
188 //| call func
189 //|.endmacro
190 //|.macro call_rrr, func, arg0, arg1, arg2
191 //| mov r8, arg2
192 //| mov rdx, arg1
193 //| mov rcx, arg0
194 //| call func
195 //|.endmacro
196 //|
197 //|.macro call_rp, func, arg0, arg1
198 //| mov64 rdx, arg1
199 //| mov rcx, arg0
200 //| call func
201 //|.endmacro
202 //|.macro call_rr, func, arg0, arg1
203 //| mov rdx, arg1
204 //| mov rcx, arg0
205 //| call func
206 //|.endmacro
207 //|
208 //|.macro call_r, func, arg0
209 //| mov rcx, arg0
210 //| call func
211 //|.endmacro
212 //|
213 //|.elif X64
214 //|
215 //| // the 5 and 6 arg forms are only used on posix x64
216 //|.macro call_rrrrrr, func, arg0, arg1, arg2, arg3, arg4, arg5
217 //| mov r9, arg5
218 //| mov r8, arg4
219 //| mov rcx, arg3
220 //| mov rdx, arg2
221 //| mov rsi, arg1
222 //| mov rdi, arg0
223 //| call func
224 //|.endmacro
225 //|.macro call_rrrrr, func, arg0, arg1, arg2, arg3, arg4
226 //| mov r8, arg4
227 //| mov rcx, arg3
228 //| mov rdx, arg2
229 //| mov rsi, arg1
230 //| mov rdi, arg0
231 //| call func
232 //|.endmacro
233 //|
234 //|.macro call_rrrp, func, arg0, arg1, arg2, arg3
235 //| mov64 rcx, arg3
236 //| mov rdx, arg2
237 //| mov rsi, arg1
238 //| mov rdi, arg0
239 //| call func
240 //|.endmacro
241 //|.macro call_rrrr, func, arg0, arg1, arg2, arg3
242 //| mov rcx, arg3
243 //| mov rdx, arg2
244 //| mov rsi, arg1
245 //| mov rdi, arg0
246 //| call func
247 //|.endmacro
248 //|
249 //|.macro call_rrp, func, arg0, arg1, arg2
250 //| mov64 rdx, arg2
251 //| mov rsi, arg1
252 //| mov rdi, arg0
253 //| call func
254 //|.endmacro
255 //|.macro call_rrr, func, arg0, arg1, arg2
256 //| mov rdx, arg2
257 //| mov rsi, arg1
258 //| mov rdi, arg0
259 //| call func
260 //|.endmacro
261 //|
262 //|.macro call_rp, func, arg0, arg1
263 //| mov64 rsi, arg1
264 //| mov rdi, arg0
265 //| call func
266 //|.endmacro
267 //|.macro call_rr, func, arg0, arg1
268 //| mov rsi, arg1
269 //| mov rdi, arg0
270 //| call func
271 //|.endmacro
272 //|
273 //|.macro call_r, func, arg0
274 //| mov rdi, arg0
275 //| call func
276 //|.endmacro
277 //|
278 //|.else
279 //| // define the 64bit registers to the 32 bit counterparts, so the common
280 //| // code can use r*x for all pointers
281 //|.define rax, eax
282 //|.define rcx, ecx
283 //|.define rdx, edx
284 //|.define rsp, esp
285 //|.define rbp, ebp
286 //|.define rdi, edi
287 //|.define rsi, esi
288 //|.define mov64, mov
289 //|
290 //|.macro call_rrrr, func, arg0, arg1, arg2, arg3
291 //| mov dword [rsp+12], arg3
292 //| mov dword [rsp+8], arg2
293 //| mov dword [rsp+4], arg1
294 //| mov dword [rsp], arg0
295 //| call func
296 //|.endmacro
297 //|.macro call_rrr, func, arg0, arg1, arg2
298 //| mov dword [rsp+8], arg2
299 //| mov dword [rsp+4], arg1
300 //| mov dword [rsp], arg0
301 //| call func
302 //|.endmacro
303 //|.macro call_rr, func, arg0, arg1
304 //| mov dword [rsp+4], arg1
305 //| mov dword [rsp], arg0
306 //| call func
307 //|.endmacro
308 //|.macro call_r, func, arg0
309 //| mov dword [rsp], arg0
310 //| call func
311 //|.endmacro
312 //|
313 //|.define call_rrrp, call_rrrr
314 //|.define call_rrp, call_rrr
315 //|.define call_rp, call_rr
316 //|
317 //|.endif
318
319 //|.macro epilog
320 //|.if X64
321 //| mov TOP, [rbp-16]
322 //| mov L_ARG, [rbp-8]
323 //|.else
324 //| mov TOP, [rbp-8]
325 //| mov L_ARG, [rbp-4]
326 //|.endif
327 //| mov rsp, rbp
328 //| pop rbp
329 //| ret
330 //|.endmacro
331
332 //|.macro get_errno // note trashes registers
333 //| call extern GetLastError
334 //| mov64 rcx, perr
335 //| mov dword [rcx], eax
336 //|.endmacro
337
338 //|.macro too_few_arguments
339 //| mov ax, 0
340 //| call_rp extern luaL_error, L_ARG, &"too few arguments"
341 //|.endmacro
342
343 //|.macro too_many_arguments
344 //| mov ax, 0
345 //| call_rp extern luaL_error, L_ARG, &"too many arguments"
346 //|.endmacro
347
348 //|.macro lua_return_arg
349 //| mov eax, 1
350 //| epilog
351 //|.endmacro
352
353 //|.macro lua_return_void
354 //| get_errno
355 //| mov eax, 0
356 //| epilog
357 //|.endmacro
358
359 //|.macro lua_return_double
360 //|.if X64
361 //| movq qword [rsp+32], xmm0
362 //|.else
363 //| fstp qword [rsp+4] // note get_errno doesn't require any stack on x86
364 //|.endif
365 //|
366 //| get_errno
367 //|
368 //|.if X64WIN
369 //| movq xmm1, qword [rsp+32]
370 //| mov rcx, L_ARG
371 //|.elif X64
372 //| movq xmm0, qword [rsp+32]
373 //| mov rdi, L_ARG
374 //|.else
375 //| mov [rsp], L_ARG
376 //|.endif
377 //| call extern lua_pushnumber
378 //| lua_return_arg
379 //|.endmacro
380
381 //|.macro lua_return_bool
382 //| movzx eax, al
383 //| mov [rsp+32], eax
384 //| get_errno
385 //| mov eax, [rsp+32]
386 //| call_rr extern lua_pushboolean, L_ARG, rax
387 //| lua_return_arg
388 //|.endmacro
389
390 //|.macro lua_return_int
391 //| mov [rsp+32], eax
392 //| get_errno
393 //| mov eax, [rsp+32]
394 //| call_rr extern push_int, L_ARG, rax
395 //| lua_return_arg
396 //|.endmacro
397
398 //|.macro lua_return_uint
399 //| mov [rsp+32], eax
400 //| get_errno
401 //| mov eax, [rsp+32]
402 //| call_rr extern push_uint, L_ARG, rax
403 //| lua_return_arg
404 //|.endmacro
405
406 //|.macro lua_return_long
407 //| mov [rsp+32], rax
408 //| get_errno
409 //| mov rax, [rsp+32]
410 //| call_rr extern lua_pushinteger, L_ARG, rax
411 //| lua_return_arg
412 //|.endmacro
413
414 //|.macro lua_return_ulong
415 //| mov [rsp+32], rax
416 //| get_errno
417 //| mov rax, [rsp+32]
418 //| call_rr extern lua_pushinteger, L_ARG, rax
419 //| lua_return_arg
420 //|.endmacro
421
422 //|.macro save_registers
423 //| // use rbp relative so we store values in the outer stack frame
424 //|.if X64WIN
425 //| // use the provided shadow space for int registers above prev rbp and
426 //| // return address
427 //| mov [rbp+16], rcx
428 //| mov [rbp+24], rdx
429 //| mov [rbp+32], r8
430 //| mov [rbp+40], r9
431 //| // use the extra space we added for float registers
432 //| // -16 to store underneath previous value of L_ARG
433 //| movq qword [rbp-16], xmm0
434 //| movq qword [rbp-24], xmm1
435 //| movq qword [rbp-32], xmm2
436 //| movq qword [rbp-40], xmm3
437 //|.elif X64
438 //| movq qword [rbp-16], xmm0
439 //| movq qword [rbp-24], xmm1
440 //| movq qword [rbp-32], xmm2
441 //| movq qword [rbp-40], xmm3
442 //| movq qword [rbp-48], xmm4
443 //| movq qword [rbp-56], xmm5
444 //| movq qword [rbp-64], xmm6
445 //| movq qword [rbp-72], xmm7
446 //| mov [rbp-80], rdi
447 //| mov [rbp-88], rsi
448 //| mov [rbp-96], rdx
449 //| mov [rbp-104], rcx
450 //| mov [rbp-112], r8
451 //| mov [rbp-120], r9
452 //|.else
453 //| // fastcall, -8 to store underneath previous value of L_ARG
454 //| mov [rbp-8], ecx
455 //| mov [rbp-12], edx
456 //|.endif
457 //|.endmacro
458
459 #if defined _WIN64 || defined __amd64__
460 #define JUMP_SIZE 14
461 #else
462 #define JUMP_SIZE 4
463 #endif
464
465 #define MIN_BRANCH INT32_MIN
466 #define MAX_BRANCH INT32_MAX
467 #define BRANCH_OFF 4
468
469 static void compile_extern_jump(struct jit* jit, lua_State* L, cfunction func, uint8_t* code)
470 {
471 /* The jump code is the function pointer followed by a stub to call the
472 * function pointer. The stub exists in 64 bit so we can jump to functions
473 * with an offset greater than 2 GB.
474 *
475 * Note we have to manually set this up since there are commands buffered
476 * in the jit state and dynasm doesn't support rip relative addressing.
477 *
478 * eg on 64 bit:
479 * 0-8: function ptr
480 * 8-14: jmp aword [rip-14]
481 *
482 * for 32 bit we only set the function ptr as it can always fit in a 32
483 * bit displacement
484 */
485 #if defined _WIN64 || defined __amd64__
486 *(cfunction*) code = func;
487 code[8] = 0xFF; /* FF /4 operand for jmp */
488 code[9] = 0x25; /* RIP displacement */
489 *(int32_t*) &code[10] = -14;
490 #else
491 *(cfunction*) code = func;
492 #endif
493 }
494
495 void compile_globals(struct jit* jit, lua_State* L)
496 {
497 struct jit* Dst = jit;
498 int* perr = &jit->last_errno;
499 dasm_setup(Dst, build_actionlist);
500
501 /* Note: since the return code uses EBP to reset the stack pointer, we
502 * don't have to track the amount of stack space used. It also means we
503 * can handle stdcall and cdecl with the same code.
504 */
505
506 /* Note the various call_* functions want 32 bytes of 16 byte aligned
507 * stack
508 */
509
510
511
512 compile(Dst, L, NULL, LUA_NOREF);
513 }
514
515 int x86_return_size(lua_State* L, int usr, const struct ctype* ct)
516 {
517 int ret = 0;
518 const struct ctype* mt;
519
520 if (ct->calling_convention != C_CALL) {
521 size_t i;
522 size_t argn = lua_rawlen(L, usr);
523 for (i = 1; i <= argn; i++) {
524 lua_rawgeti(L, usr, (int) i);
525 mt = (const struct ctype*) lua_touserdata(L, -1);
526
527 if (mt->pointers || mt->is_reference) {
528 ret += sizeof(void*);
529 } else {
530 switch (mt->type) {
531 case DOUBLE_TYPE:
532 case COMPLEX_FLOAT_TYPE:
533 case INT64_TYPE:
534 ret += 8;
535 break;
536 case COMPLEX_DOUBLE_TYPE:
537 ret += 16;
538 break;
539 case INTPTR_TYPE:
540 ret += sizeof(intptr_t);
541 break;
542 case FUNCTION_PTR_TYPE:
543 ret += sizeof(cfunction);
544 break;
545 case BOOL_TYPE:
546 case FLOAT_TYPE:
547 case INT8_TYPE:
548 case INT16_TYPE:
549 case INT32_TYPE:
550 case ENUM_TYPE:
551 ret += 4;
552 break;
553 default:
554 return luaL_error(L, "NYI - argument type");
555 }
556 }
557
558 lua_pop(L, 1);
559 }
560 }
561
562 #if !defined _WIN64 && !defined __amd64__
563 lua_rawgeti(L, usr, 0);
564 mt = (const struct ctype*) lua_touserdata(L, -1);
565 if (!mt->pointers && !mt->is_reference && mt->type == COMPLEX_DOUBLE_TYPE) {
566 ret += sizeof(void*);
567 }
568 lua_pop(L, 1);
569 #endif
570
571 return ret;
572 }
573
574 #ifdef _WIN64
575 #define MAX_REGISTERS(ct) 4 /* rcx, rdx, r8, r9 */
576
577 #elif defined __amd64__
578 #define MAX_INT_REGISTERS(ct) 6 /* rdi, rsi, rdx, rcx, r8, r9 */
579 #define MAX_FLOAT_REGISTERS(ct) 8 /* xmm0-7 */
580
581 #else
582 #define MAX_INT_REGISTERS(ct) ((ct)->calling_convention == FAST_CALL ? 2 /* ecx, edx */ : 0)
583 #define MAX_FLOAT_REGISTERS(ct) 0
584 #endif
585
586 struct reg_alloc {
587 #ifdef _WIN64
588 int regs;
589 int is_float[4];
590 int is_int[4];
591 #else
592 int floats;
593 int ints;
594 #endif
595 int off;
596 };
597
598 #ifdef _WIN64
599 #define REGISTER_STACK_SPACE(ct) (4*8)
600 #elif defined __amd64__
601 #define REGISTER_STACK_SPACE(ct) (14*8)
602 #else
603 #define REGISTER_STACK_SPACE(ct) ALIGN_UP(((ct)->calling_convention == FAST_CALL ? 2*4 : 0), 15)
604 #endif
605
606 /* Fastcall:
607 * Uses ecx, edx as first two int registers
608 * Everything else on stack (include 64bit ints)
609 * No overflow stack space
610 * Pops the stack before returning
611 * Returns int in eax, float in ST0
612 * We use the same register allocation logic as posix x64 with 2 int regs and 0 float regs
613 */
614
615 static void get_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64)
616 {
617 /* grab the register from the shadow space */
618 #ifdef _WIN64
619 if (reg->regs < MAX_REGISTERS(ct)) {
620 //| mov rcx, [rbp + 16 + 8*reg->regs]
621 dasm_put(Dst, 0, 16 + 8*reg->regs);
622 reg->regs++;
623 }
624 #elif __amd64__
625 if (reg->ints < MAX_INT_REGISTERS(ct)) {
626 //| mov rcx, [rbp - 80 - 8*reg->ints]
627 dasm_put(Dst, 0, - 80 - 8*reg->ints);
628 reg->ints++;
629 }
630 #else
631 if (!is_int64 && reg->ints < MAX_INT_REGISTERS(ct)) {
632 //| mov ecx, [rbp - 8 - 4*reg->ints]
633 dasm_put(Dst, 0, - 8 - 4*reg->ints);
634 reg->ints++;
635 }
636 #endif
637 else if (is_int64) {
638 //|.if X64
639 //| mov rcx, [rbp + reg->off]
640 //|.else
641 //| mov rcx, [rbp + reg->off]
642 //| mov rdx, [rbp + reg->off + 4]
643 //|.endif
644 dasm_put(Dst, 4, reg->off, reg->off + 4);
645 reg->off += 8;
646 } else {
647 //| mov ecx, [rbp + reg->off]
648 dasm_put(Dst, 0, reg->off);
649 #if defined __amd64__ || defined _WIN64
650 /* The parameters to a function on stack are always 8 byte aligned. */
651 reg->off += 8;
652 #else
653 reg->off += 4;
654 #endif
655 }
656 }
657
658 static void add_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64)
659 {
660 #ifdef _WIN64
661 if (reg->regs < MAX_REGISTERS(ct)) {
662 //| mov [rsp + 32 + 8*(reg->regs)], rax
663 dasm_put(Dst, 11, 32 + 8*(reg->regs));
664 reg->is_int[reg->regs++] = 1;
665 }
666 #elif __amd64__
667 if (reg->ints < MAX_INT_REGISTERS(ct)) {
668 //| mov [rsp + 32 + 8*reg->ints], rax
669 dasm_put(Dst, 11, 32 + 8*reg->ints);
670 reg->ints++;
671 }
672 #else
673 if (!is_int64 && reg->ints < MAX_INT_REGISTERS(ct)) {
674 //| mov [rsp + 32 + 4*reg->ints], rax
675 dasm_put(Dst, 11, 32 + 4*reg->ints);
676 reg->ints++;
677 }
678 #endif
679 else {
680 #if defined _WIN64 || defined __amd64__
681 if (reg->off % 8 != 0) {
682 reg->off += 8 - (reg->off % 8);
683 }
684 #endif
685 if (is_int64) {
686 //|.if X64
687 //| mov [rsp + reg->off], rax
688 //|.else
689 //| mov [rsp + reg->off], RET_L
690 //| mov [rsp + reg->off + 4], RET_H
691 //|.endif
692 dasm_put(Dst, 17, reg->off, reg->off + 4);
693 reg->off += 8;
694 } else {
695 //| mov [rsp+reg->off], eax
696 dasm_put(Dst, 11, reg->off);
697 reg->off += 4;
698 }
699 }
700 }
701
702 static void get_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
703 {
704 #if !defined _WIN64 && !defined __amd64__
705 assert(MAX_FLOAT_REGISTERS(ct) == 0);
706 if (is_double) {
707 //| fld qword [rbp + reg->off]
708 dasm_put(Dst, 28, reg->off);
709 reg->off += 8;
710 } else {
711 //| fld dword [rbp + reg->off]
712 dasm_put(Dst, 32, reg->off);
713 reg->off += 4;
714 }
715 #else
716 int off;
717
718 #ifdef _WIN64
719 if (reg->regs < MAX_REGISTERS(ct)) {
720 off = -16 - 8*reg->regs;
721 reg->regs++;
722 }
723 #else
724 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
725 off = -16 - 8*reg->floats;
726 reg->floats++;
727 }
728 #endif
729 else {
730 off = reg->off;
731 reg->off += is_double ? 8 : 4;
732 }
733
734 if (is_double) {
735 //| movq xmm0, qword [rbp + off]
736 dasm_put(Dst, 36, off);
737 } else {
738 //| cvtss2sd xmm0, dword [rbp + off]
739 dasm_put(Dst, 43, off);
740 }
741 #endif
742 }
743
744 static void add_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
745 {
746 #if !defined _WIN64 && !defined __amd64__
747 assert(MAX_FLOAT_REGISTERS(ct) == 0);
748 if (is_double) {
749 //| fstp qword [rsp + reg->off]
750 dasm_put(Dst, 50, reg->off);
751 reg->off += 8;
752 } else {
753 //| fstp dword [rsp + reg->off]
754 dasm_put(Dst, 56, reg->off);
755 reg->off += 4;
756 }
757 #else
758
759 #ifdef _WIN64
760 if (reg->regs < MAX_REGISTERS(ct)) {
761 if (is_double) {
762 //| movq qword [rsp + 32 + 8*(reg->regs)], xmm0
763 dasm_put(Dst, 62, 32 + 8*(reg->regs));
764 } else {
765 //| cvtsd2ss xmm0, xmm0
766 //| movq qword [rsp + 32 + 8*(reg->regs)], xmm0
767 dasm_put(Dst, 70, 32 + 8*(reg->regs));
768 }
769 reg->is_float[reg->regs++] = 1;
770 }
771 #else
772 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
773 if (is_double) {
774 //| movq qword [rsp + 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)], xmm0
775 dasm_put(Dst, 62, 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats));
776 } else {
777 //| cvtsd2ss xmm0, xmm0
778 //| movq qword [rsp + 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)], xmm0
779 dasm_put(Dst, 70, 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats));
780 }
781 reg->floats++;
782 }
783 #endif
784
785 else if (is_double) {
786 //| movq qword [rsp + reg->off], xmm0
787 dasm_put(Dst, 62, reg->off);
788 reg->off += 8;
789 } else {
790 //| cvtsd2ss xmm0, xmm0
791 //| movd dword [rsp + reg->off], xmm0
792 dasm_put(Dst, 83, reg->off);
793 reg->off += 4;
794 }
795 #endif
796 }
797
798 #if defined _WIN64 || defined __amd64__
799 #define add_pointer(jit, ct, reg) add_int(jit, ct, reg, 1)
800 #define get_pointer(jit, ct, reg) get_int(jit, ct, reg, 1)
801 #else
802 #define add_pointer(jit, ct, reg) add_int(jit, ct, reg, 0)
803 #define get_pointer(jit, ct, reg) get_int(jit, ct, reg, 0)
804 #endif
805
806 cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct)
807 {
808 int i, nargs;
809 cfunction* pf;
810 struct ctype ct2 = *ct;
811 const struct ctype* mt;
812 struct reg_alloc reg;
813 int num_upvals = 0;
814 int top = lua_gettop(L);
815 struct jit* Dst = get_jit(L);
816 int ref;
817 int hidden_arg_off = 0;
818
819 ct_usr = lua_absindex(L, ct_usr);
820 fidx = lua_absindex(L, fidx);
821
822 assert(lua_isnil(L, fidx) || lua_isfunction(L, fidx));
823
824 memset(&reg, 0, sizeof(reg));
825 #ifdef _WIN64
826 reg.off = 16 + REGISTER_STACK_SPACE(ct); /* stack registers are above the shadow space */
827 #elif __amd64__
828 reg.off = 16;
829 #else
830 reg.off = 8;
831 #endif
832
833 dasm_setup(Dst, build_actionlist);
834
835 // add a table to store ctype and function upvalues
836 // callback_set assumes the first value is the lua function
837 nargs = (int) lua_rawlen(L, ct_usr);
838 lua_newtable(L);
839 lua_pushvalue(L, -1);
840 ref = luaL_ref(L, LUA_REGISTRYINDEX);
841
842 if (ct->has_var_arg) {
843 luaL_error(L, "can't create callbacks with varargs");
844 }
845
846 // setup a stack frame to hold args for the call into lua_call
847
848 //| push rbp
849 //| mov rbp, rsp
850 //| push L_ARG
851 //| // stack is 4 or 8 (mod 16) (L_ARG, rbp, rip)
852 //|.if X64
853 //| // 8 to realign, 16 for return vars, 32 for local calls, rest to save registers
854 //| sub rsp, 8 + 16 + 32 + REGISTER_STACK_SPACE(ct)
855 //| save_registers
856 //|.else
857 //| // 4 to realign, 16 for return vars, 32 for local calls, rest to save registers
858 //| sub rsp, 4 + 16 + 32 + REGISTER_STACK_SPACE(ct)
859 dasm_put(Dst, 96, 4 + 16 + 32 + REGISTER_STACK_SPACE(ct));
860 if (ct->calling_convention == FAST_CALL) {
861 //| save_registers
862 dasm_put(Dst, 105);
863 }
864 //|.endif
865
866 // hardcode the lua_State* value into the assembly
867 //| mov64 L_ARG, L
868 dasm_put(Dst, 114, L);
869
870 /* get the upval table */
871 //| call_rrr extern lua_rawgeti, L_ARG, LUA_REGISTRYINDEX, ref
872 dasm_put(Dst, 117, ref, LUA_REGISTRYINDEX);
873
874 /* get the lua function */
875 lua_pushvalue(L, fidx);
876 lua_rawseti(L, -2, ++num_upvals);
877 assert(num_upvals == CALLBACK_FUNC_USR_IDX);
878 //| call_rrr extern lua_rawgeti, L_ARG, -1, num_upvals
879 dasm_put(Dst, 135, num_upvals);
880
881 #if !defined _WIN64 && !defined __amd64__
882 lua_rawgeti(L, ct_usr, 0);
883 mt = (const struct ctype*) lua_touserdata(L, -1);
884 if (!mt->pointers && !mt->is_reference && mt->type == COMPLEX_DOUBLE_TYPE) {
885 hidden_arg_off = reg.off;
886 reg.off += sizeof(void*);
887 }
888 lua_pop(L, 1);
889 #else
890 (void) hidden_arg_off;
891 #endif
892
893 for (i = 1; i <= nargs; i++) {
894 lua_rawgeti(L, ct_usr, i);
895 mt = (const struct ctype*) lua_touserdata(L, -1);
896
897 if (mt->pointers || mt->is_reference) {
898 lua_getuservalue(L, -1);
899 lua_rawseti(L, -3, ++num_upvals); /* usr value */
900 lua_rawseti(L, -2, ++num_upvals); /* mt */
901 /* on the lua stack in the callback:
902 * upval tbl, lua func, i-1 args
903 */
904 //| call_rrr extern lua_rawgeti, L_ARG, -i-1, num_upvals-1
905 //| call_rrp extern push_cdata, L_ARG, -1, mt
906 dasm_put(Dst, 160, num_upvals-1, -i-1, mt);
907 get_pointer(Dst, ct, &reg);
908 //| mov [rax], rcx
909 //| call_rr, extern lua_remove, L_ARG, -2
910 dasm_put(Dst, 202);
911 } else {
912 switch (mt->type) {
913 case INT64_TYPE:
914 lua_getuservalue(L, -1);
915 lua_rawseti(L, -3, ++num_upvals); /* mt */
916 lua_pop(L, 1);
917 //| call_rrp extern push_cdata, L_ARG, 0, mt
918 dasm_put(Dst, 224, mt);
919 get_int(Dst, ct, &reg, 1);
920 //|.if X64
921 //| mov [rax], rcx
922 //|.else
923 //| mov [rax], ecx
924 //| mov [rax+4], edx
925 //|.endif
926 dasm_put(Dst, 245);
927 break;
928
929 case INTPTR_TYPE:
930 lua_getuservalue(L, -1);
931 lua_rawseti(L, -3, ++num_upvals); /* mt */
932 lua_pop(L, 1);
933 //| call_rrp extern push_cdata, L_ARG, 0, mt
934 dasm_put(Dst, 224, mt);
935 get_pointer(Dst, ct, &reg);
936 //| mov [rax], rcx
937 dasm_put(Dst, 251);
938 break;
939
940 case COMPLEX_FLOAT_TYPE:
941 lua_pop(L, 1);
942 #if defined _WIN64 || defined __amd64__
943 /* complex floats are two floats packed into a double */
944 //| call_rrp extern push_cdata, L_ARG, 0, mt
945 dasm_put(Dst, 224, mt);
946 get_float(Dst, ct, &reg, 1);
947 //| movq qword [rax], xmm0
948 dasm_put(Dst, 254);
949 #else
950 /* complex floats are real followed by imag on the stack */
951 //| call_rrp extern push_cdata, L_ARG, 0, mt
952 dasm_put(Dst, 224, mt);
953 get_float(Dst, ct, &reg, 0);
954 //| fstp dword [rax]
955 dasm_put(Dst, 259);
956 get_float(Dst, ct, &reg, 0);
957 //| fstp dword [rax+4]
958 dasm_put(Dst, 262);
959 #endif
960 break;
961
962 case COMPLEX_DOUBLE_TYPE:
963 lua_pop(L, 1);
964 //| call_rrp extern push_cdata, L_ARG, 0, mt
965 dasm_put(Dst, 224, mt);
966 /* real */
967 get_float(Dst, ct, &reg, 1);
968 //|.if X64
969 //| movq qword [rax], xmm0
970 //|.else
971 //| fstp qword [rax]
972 //|.endif
973 dasm_put(Dst, 266);
974 /* imag */
975 get_float(Dst, ct, &reg, 1);
976 //|.if X64
977 //| movq qword [rax+8], xmm0
978 //|.else
979 //| fstp qword [rax+8]
980 //|.endif
981 dasm_put(Dst, 269);
982 break;
983
984 case FLOAT_TYPE:
985 case DOUBLE_TYPE:
986 lua_pop(L, 1);
987 get_float(Dst, ct, &reg, mt->type == DOUBLE_TYPE);
988 //|.if X64WIN
989 //| movq xmm1, xmm0
990 //| mov rcx, L_ARG
991 //|.elif X64
992 //| // for 64bit xmm0 is already set
993 //| mov rdi, L_ARG
994 //|.else
995 //| fstp qword [rsp+4]
996 //| mov [rsp], L_ARG
997 //|.endif
998 //| call extern lua_pushnumber
999 dasm_put(Dst, 273);
1000 break;
1001
1002 case BOOL_TYPE:
1003 lua_pop(L, 1);
1004 get_int(Dst, ct, &reg, 0);
1005 //| movzx ecx, cl
1006 //| call_rr extern lua_pushboolean, L_ARG, rcx
1007 dasm_put(Dst, 285);
1008 break;
1009
1010 case INT8_TYPE:
1011 lua_pop(L, 1);
1012 get_int(Dst, ct, &reg, 0);
1013 if (mt->is_unsigned) {
1014 //| movzx ecx, cl
1015 dasm_put(Dst, 300);
1016 } else {
1017 //| movsx ecx, cl
1018 dasm_put(Dst, 304);
1019 }
1020 //| call_rr extern push_int, L_ARG, rcx
1021 dasm_put(Dst, 308);
1022 break;
1023
1024 case INT16_TYPE:
1025 lua_pop(L, 1);
1026 get_int(Dst, ct, &reg, 0);
1027 if (mt->is_unsigned) {
1028 //| movzx ecx, cx
1029 dasm_put(Dst, 320);
1030 } else {
1031 //| movsx ecx, cx
1032 dasm_put(Dst, 324);
1033 }
1034 //| call_rr extern push_int, L_ARG, rcx
1035 dasm_put(Dst, 308);
1036 break;
1037
1038 case ENUM_TYPE:
1039 case INT32_TYPE:
1040 lua_pop(L, 1);
1041 get_int(Dst, ct, &reg, 0);
1042 if (mt->is_unsigned) {
1043 //| call_rr extern push_uint, L_ARG, rcx
1044 dasm_put(Dst, 328);
1045 } else {
1046 //| call_rr extern push_int, L_ARG, rcx
1047 dasm_put(Dst, 308);
1048 }
1049 break;
1050
1051 default:
1052 luaL_error(L, "NYI: callback arg type");
1053 }
1054 }
1055 }
1056
1057 lua_rawgeti(L, ct_usr, 0);
1058 mt = (const struct ctype*) lua_touserdata(L, -1);
1059
1060 //| call_rrrp extern lua_callk, L_ARG, nargs, (mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0, 0
1061 dasm_put(Dst, 340, (mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0, nargs);
1062
1063 // Unpack the return argument if not "void", also clean-up the lua stack
1064 // to remove the return argument and bind table. Use lua_settop rather
1065 // than lua_pop as lua_pop is implemented as a macro.
1066 if (mt->pointers || mt->is_reference) {
1067 lua_getuservalue(L, -1);
1068 lua_rawseti(L, -3, ++num_upvals); /* usr value */
1069 lua_rawseti(L, -2, ++num_upvals); /* mt */
1070 //| call_rrr extern lua_rawgeti, L_ARG, -2, num_upvals-1
1071 //| call_rrrp extern check_typed_pointer, L_ARG, -2, -1, mt
1072 //| mov [rsp+32], rax
1073 //| call_rr extern lua_settop, L_ARG, -4
1074 //| mov rax, [rsp+32]
1075 dasm_put(Dst, 366, num_upvals-1, mt);
1076
1077 } else {
1078 switch (mt->type) {
1079 case ENUM_TYPE:
1080 lua_getuservalue(L, -1);
1081 lua_rawseti(L, -3, ++num_upvals); /* usr value */
1082 lua_rawseti(L, -2, ++num_upvals); /* mt */
1083 //| call_rrr extern lua_rawgeti, L_ARG, -2, num_upvals-1
1084 //| call_rrrp, extern check_enum, L_ARG, -2, -1, mt
1085 //| mov [rsp+32], eax
1086 //| call_rr extern lua_settop, L_ARG, -4
1087 //| mov eax, [rsp+32]
1088 dasm_put(Dst, 454, num_upvals-1, mt);
1089 break;
1090
1091 case VOID_TYPE:
1092 lua_pop(L, 1);
1093 //| call_rr extern lua_settop, L_ARG, -2
1094 dasm_put(Dst, 542);
1095 break;
1096
1097 case BOOL_TYPE:
1098 case INT8_TYPE:
1099 case INT16_TYPE:
1100 case INT32_TYPE:
1101 lua_pop(L, 1);
1102 if (mt->is_unsigned) {
1103 //| call_rr extern check_uint32, L_ARG, -1
1104 dasm_put(Dst, 562);
1105 } else {
1106 //| call_rr extern check_int32, L_ARG, -1
1107 dasm_put(Dst, 582);
1108 }
1109 //| mov [rsp+32], eax
1110 //| call_rr extern lua_settop, L_ARG, -3
1111 //| mov eax, [rsp+32]
1112 dasm_put(Dst, 602);
1113 break;
1114
1115 case INT64_TYPE:
1116 lua_pop(L, 1);
1117
1118 if (mt->is_unsigned) {
1119 //| call_rr extern check_uint64, L_ARG, -1
1120 dasm_put(Dst, 630);
1121 } else {
1122 //| call_rr extern check_int64, L_ARG, -1
1123 dasm_put(Dst, 650);
1124 }
1125
1126 //|.if X64
1127 //| mov [rsp+32], rax
1128 //|.else
1129 //| mov [rsp+32], RET_L
1130 //| mov [rsp+36], RET_H
1131 //|.endif
1132 //| call_rr extern lua_settop, L_ARG, -3
1133 //|.if X64
1134 //| mov rax, [rsp+32]
1135 //|.else
1136 //| mov RET_L, [rsp+32]
1137 //| mov RET_H, [rsp+36]
1138 //|.endif
1139 dasm_put(Dst, 670);
1140 break;
1141
1142 case INTPTR_TYPE:
1143 lua_pop(L, 1);
1144 //| call_rr extern check_uintptr, L_ARG, -1
1145 //| mov [rsp+32], rax
1146 //| call_rr extern lua_settop, L_ARG, -3
1147 //| mov rax, [rsp+32]
1148 dasm_put(Dst, 706);
1149 break;
1150
1151 case FLOAT_TYPE:
1152 case DOUBLE_TYPE:
1153 lua_pop(L, 1);
1154 //| call_rr extern check_double, L_ARG, -1
1155 //|.if X64
1156 //| movq qword [rsp+32], xmm0
1157 //| call_rr extern lua_settop, L_ARG, -3
1158 dasm_put(Dst, 753);
1159 if (mt->type == FLOAT_TYPE) {
1160 //| cvtsd2ss xmm0, qword [rsp+32]
1161 } else {
1162 //| movq xmm0, qword [rsp+32]
1163 }
1164 //|.else
1165 //| fstp qword [rsp+32]
1166 //| call_rr extern lua_settop, L_ARG, -3
1167 //| fld qword [rsp+32]
1168 //|.endif
1169 dasm_put(Dst, 773);
1170 break;
1171
1172 case COMPLEX_FLOAT_TYPE:
1173 lua_pop(L, 1);
1174 #if !defined HAVE_COMPLEX
1175 luaL_error(L, "ffi lib compiled without complex number support");
1176 #endif
1177 /* on 64 bit complex floats are two floats packed into a double,
1178 * on 32 bit returned complex floats use eax and edx */
1179 //| call_rr extern check_complex_float, L_ARG, -1
1180 //|
1181 //|.if X64
1182 //| movq qword [rsp+32], xmm0
1183 //|.else
1184 //| mov [rsp+32], eax
1185 //| mov [rsp+36], edx
1186 //|.endif
1187 //|
1188 //| call_rr extern lua_settop, L_ARG, -3
1189 //|
1190 //|.if X64
1191 //| movq xmm0, qword [rsp+32]
1192 //|.else
1193 //| mov eax, [rsp+32]
1194 //| mov edx, [rsp+36]
1195 //|.endif
1196 dasm_put(Dst, 801);
1197 break;
1198
1199 case COMPLEX_DOUBLE_TYPE:
1200 lua_pop(L, 1);
1201 #if !defined HAVE_COMPLEX
1202 luaL_error(L, "ffi lib compiled without complex number support");
1203 #endif
1204 /* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit
1205 * there is a hidden first parameter that points to 16 bytes where
1206 * the returned arg is stored which is popped by the called
1207 * function */
1208 #if defined _WIN64 || defined __amd64__
1209 //| call_rr extern check_complex_double, L_ARG, -1
1210 //| movq qword [rsp+32], xmm0
1211 //| movq qword [rsp+40], xmm1
1212 //| call_rr extern lua_settop, L_ARG, -3
1213 //| movq xmm0, qword [rsp+32]
1214 //| movq xmm1, qword [rsp+40]
1215 dasm_put(Dst, 856);
1216 #else
1217 //| mov rcx, [rbp + hidden_arg_off]
1218 //| call_rrr extern check_complex_double, rcx, L_ARG, -1
1219 //| sub rsp, 4 // to realign from popped hidden arg
1220 //| call_rr extern lua_settop, L_ARG, -3
1221 dasm_put(Dst, 921, hidden_arg_off);
1222 #endif
1223 break;
1224
1225 default:
1226 luaL_error(L, "NYI: callback return type");
1227 }
1228 }
1229
1230 //|.if X64
1231 //| mov L_ARG, [rbp-8]
1232 //|.else
1233 //| mov L_ARG, [rbp-4]
1234 //|.endif
1235 //| mov rsp, rbp
1236 //| pop rbp
1237 //| ret x86_return_size(L, ct_usr, ct)
1238 dasm_put(Dst, 971, x86_return_size(L, ct_usr, ct));
1239
1240 lua_pop(L, 1); /* upval table - already in registry */
1241 assert(lua_gettop(L) == top);
1242
1243 ct2.is_jitted = 1;
1244 pf = (cfunction*) push_cdata(L, ct_usr, &ct2);
1245 *pf = compile(Dst, L, NULL, ref);
1246
1247 assert(lua_gettop(L) == top + 1);
1248
1249 return *pf;
1250 }
1251
1252 void compile_function(lua_State* L, cfunction func, int ct_usr, const struct ctype* ct)
1253 {
1254 size_t i, nargs;
1255 int num_upvals;
1256 const struct ctype* mbr_ct;
1257 struct jit* Dst = get_jit(L);
1258 struct reg_alloc reg;
1259 void* p;
1260 int top = lua_gettop(L);
1261 int* perr = &Dst->last_errno;
1262
1263 ct_usr = lua_absindex(L, ct_usr);
1264
1265 memset(&reg, 0, sizeof(reg));
1266 reg.off = 32 + REGISTER_STACK_SPACE(ct);
1267
1268 dasm_setup(Dst, build_actionlist);
1269
1270 p = push_cdata(L, ct_usr, ct);
1271 *(cfunction*) p = func;
1272 num_upvals = 1;
1273
1274 nargs = lua_rawlen(L, ct_usr);
1275
1276 if (ct->calling_convention != C_CALL && ct->has_var_arg) {
1277 luaL_error(L, "vararg is only allowed with the c calling convention");
1278 }
1279
1280 //| push rbp
1281 //| mov rbp, rsp
1282 //| push L_ARG
1283 //| push TOP
1284 //| // stack is 0 (mod 16) (TOP, L_ARG, rbp, rip)
1285 //|
1286 //| // Get L from our arguments and allocate some stack for lua_gettop
1287 //|.if X64WIN
1288 //| mov L_ARG, rcx
1289 //| sub rsp, 32 // shadow space
1290 //|.elif X64
1291 //| mov L_ARG, rdi
1292 //|.else
1293 //| mov L_ARG, [rbp + 8]
1294 //| sub rsp, 16
1295 //|.endif
1296 //|
1297 //| call_r extern lua_gettop, L_ARG
1298 //| mov TOP, rax // no need for movzxd rax, eax - high word guarenteed to be zero by x86-64
1299 //| cmp rax, nargs
1300 dasm_put(Dst, 982, 8, nargs);
1301 if (!ct->has_var_arg) {
1302 //| jge >2
1303 //| too_few_arguments
1304 //| 2:
1305 //| jle >1
1306 //| too_many_arguments
1307 dasm_put(Dst, 1008, (ptrdiff_t)("too few arguments"), (ptrdiff_t)("too many arguments"));
1308 } else {
1309 //| jge >1
1310 //| too_few_arguments
1311 dasm_put(Dst, 1051, (ptrdiff_t)("too few arguments"));
1312 }
1313
1314 //| 1:
1315 dasm_put(Dst, 1072);
1316
1317 /* no need to zero extend eax returned by lua_gettop to rax as x86-64
1318 * preguarentees that the upper 32 bits will be zero */
1319 //| shl rax, 4 // reserve 16 bytes per argument - this maintains the alignment mod 16
1320 //| sub rsp, rax
1321 //| sub rsp, 32 + REGISTER_STACK_SPACE(ct) // reserve an extra 32 to call local functions
1322 dasm_put(Dst, 1075, 32 + REGISTER_STACK_SPACE(ct));
1323
1324 #if !defined _WIN64 && !defined __amd64__
1325 /* Returned complex doubles require a hidden first parameter where the
1326 * data is stored, which is popped by the calling code. */
1327 lua_rawgeti(L, ct_usr, 0);
1328 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
1329 if (!mbr_ct->pointers && !mbr_ct->is_reference && mbr_ct->type == COMPLEX_DOUBLE_TYPE) {
1330 /* we can allocate more space for arguments as long as no add_*
1331 * function has been called yet, mbr_ct will be added as an upvalue in
1332 * the return processing later */
1333 //| call_rrp extern push_cdata, L_ARG, 0, mbr_ct
1334 //| sub rsp, 16
1335 dasm_put(Dst, 1085, mbr_ct);
1336 add_pointer(Dst, ct, &reg);
1337 }
1338 lua_pop(L, 1);
1339 #endif
1340
1341 for (i = 1; i <= nargs; i++) {
1342 lua_rawgeti(L, ct_usr, (int) i);
1343 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
1344
1345 if (mbr_ct->pointers || mbr_ct->is_reference) {
1346 lua_getuservalue(L, -1);
1347 num_upvals += 2;
1348 //| call_rrrp extern check_typed_pointer, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
1349 dasm_put(Dst, 1110, mbr_ct, lua_upvalueindex(num_upvals), i);
1350 add_pointer(Dst, ct, &reg);
1351 } else {
1352 switch (mbr_ct->type) {
1353 case FUNCTION_PTR_TYPE:
1354 lua_getuservalue(L, -1);
1355 num_upvals += 2;
1356 //| call_rrrp extern check_typed_cfunction, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
1357 dasm_put(Dst, 1133, mbr_ct, lua_upvalueindex(num_upvals), i);
1358 add_pointer(Dst, ct, &reg);
1359 break;
1360
1361 case ENUM_TYPE:
1362 lua_getuservalue(L, -1);
1363 num_upvals += 2;
1364 //| call_rrrp, extern check_enum, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
1365 dasm_put(Dst, 1156, mbr_ct, lua_upvalueindex(num_upvals), i);
1366 add_int(Dst, ct, &reg, 0);
1367 break;
1368
1369 case INT8_TYPE:
1370 //| call_rr extern check_int32, L_ARG, i
1371 dasm_put(Dst, 1179, i);
1372 if (mbr_ct->is_unsigned) {
1373 //| movzx eax, al
1374 dasm_put(Dst, 1192);
1375 } else {
1376 //| movsx eax, al
1377 dasm_put(Dst, 1196);
1378 }
1379 add_int(Dst, ct, &reg, 0);
1380 lua_pop(L, 1);
1381 break;
1382
1383 case INT16_TYPE:
1384 //| call_rr extern check_int32, L_ARG, i
1385 dasm_put(Dst, 1179, i);
1386 if (mbr_ct->is_unsigned) {
1387 //| movzx eax, ax
1388 dasm_put(Dst, 1200);
1389 } else {
1390 //| movsx eax, ax
1391 dasm_put(Dst, 1204);
1392 }
1393 add_int(Dst, ct, &reg, 0);
1394 lua_pop(L, 1);
1395 break;
1396
1397 case BOOL_TYPE:
1398 //| call_rr extern check_int32, L_ARG, i
1399 //| cmp eax, 0
1400 //| setne al
1401 //| movzx eax, al
1402 dasm_put(Dst, 1208, i);
1403 add_int(Dst, ct, &reg, 0);
1404 lua_pop(L, 1);
1405 break;
1406
1407 case INT32_TYPE:
1408 if (mbr_ct->is_unsigned) {
1409 //| call_rr extern check_uint32, L_ARG, i
1410 dasm_put(Dst, 1231, i);
1411 } else {
1412 //| call_rr extern check_int32, L_ARG, i
1413 dasm_put(Dst, 1179, i);
1414 }
1415 add_int(Dst, ct, &reg, 0);
1416 lua_pop(L, 1);
1417 break;
1418
1419 case INTPTR_TYPE:
1420 //| call_rr extern check_uintptr, L_ARG, i
1421 dasm_put(Dst, 1244, i);
1422 add_pointer(Dst, ct, &reg);
1423 lua_pop(L, 1);
1424 break;
1425
1426 case INT64_TYPE:
1427 if (mbr_ct->is_unsigned) {
1428 //| call_rr extern check_uint64, L_ARG, i
1429 dasm_put(Dst, 1257, i);
1430 } else {
1431 //| call_rr extern check_int64, L_ARG, i
1432 dasm_put(Dst, 1270, i);
1433 }
1434 add_int(Dst, ct, &reg, 1);
1435 lua_pop(L, 1);
1436 break;
1437
1438 case DOUBLE_TYPE:
1439 //| call_rr extern check_double, L_ARG, i
1440 dasm_put(Dst, 1283, i);
1441 add_float(Dst, ct, &reg, 1);
1442 lua_pop(L, 1);
1443 break;
1444
1445 case COMPLEX_DOUBLE_TYPE:
1446 /* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit
1447 * there is a hidden first parameter that points to 16 bytes where
1448 * the returned arg is stored (this is popped by the called
1449 * function) */
1450 #if defined _WIN64 || defined __amd64__
1451 //| call_rr extern check_complex_double, L_ARG, i
1452 dasm_put(Dst, 1296, i);
1453 add_float(Dst, ct, &reg, 1);
1454 //| movq xmm0, xmm1
1455 dasm_put(Dst, 1309);
1456 add_float(Dst, ct, &reg, 1);
1457 #else
1458 //| lea rax, [rsp+reg.off]
1459 //| sub rsp, 4
1460 //| call_rrr extern check_complex_double, rax, L_ARG, i
1461 dasm_put(Dst, 1315, reg.off, i);
1462 reg.off += 16;
1463 #endif
1464 lua_pop(L, 1);
1465 break;
1466
1467 case FLOAT_TYPE:
1468 //| call_rr extern check_double, L_ARG, i
1469 dasm_put(Dst, 1283, i);
1470 add_float(Dst, ct, &reg, 0);
1471 lua_pop(L, 1);
1472 break;
1473
1474 case COMPLEX_FLOAT_TYPE:
1475 #if defined _WIN64 || defined __amd64__
1476 //| call_rr extern check_complex_float, L_ARG, i
1477 dasm_put(Dst, 1341, i);
1478 /* complex floats are two floats packed into a double */
1479 add_float(Dst, ct, &reg, 1);
1480 #else
1481 /* returned complex floats use eax and edx */
1482 //| call_rr extern check_complex_float, L_ARG, i
1483 //| mov [rsp], eax
1484 //| fld dword [rsp]
1485 dasm_put(Dst, 1354, i);
1486 add_float(Dst, ct, &reg, 0);
1487 //| mov [rsp], edx
1488 //| fld dword [rsp]
1489 dasm_put(Dst, 1373);
1490 add_float(Dst, ct, &reg, 0);
1491 #endif
1492 lua_pop(L, 1);
1493 break;
1494
1495 default:
1496 luaL_error(L, "NYI: call arg type");
1497 }
1498 }
1499 }
1500
1501 if (ct->has_var_arg) {
1502 #ifdef _WIN64
1503 //|.if X64WIN
1504 if (reg.regs < MAX_REGISTERS(ct)) {
1505 assert(reg.regs == nargs);
1506 //| cmp TOP, MAX_REGISTERS(ct)
1507 //| jle >1
1508 //| // unpack onto stack
1509 //| mov rax, rsp
1510 //| add rax, 32 + 8*MAX_REGISTERS(ct)
1511 //| call_rrrr extern unpack_varargs_stack, L_ARG, MAX_REGISTERS(ct)+1, TOP, rax
1512 //| // unpack to registers
1513 //| mov rax, rsp
1514 //| add rax, 32 + 8*(reg.regs)
1515 //| call_rrrr extern unpack_varargs_reg, L_ARG, nargs+1, MAX_REGISTERS(ct), rax
1516 //| jmp >2
1517 //|1:
1518 //| // unpack just to registers
1519 //| mov rax, rsp
1520 //| add rax, 32 + 8*(reg.regs)
1521 //| call_rrrr extern unpack_varargs_reg, L_ARG, nargs+1, TOP, rax
1522 //|2:
1523 } else {
1524 //| // unpack just to stack
1525 //| mov rax, rsp
1526 //| add rax, reg.off
1527 //| call_rrrr extern unpack_varargs_stack, L_ARG, nargs+1, TOP, rax
1528 }
1529
1530 for (i = nargs; i < MAX_REGISTERS(ct); i++) {
1531 reg.is_int[i] = reg.is_float[i] = 1;
1532 }
1533 reg.regs = MAX_REGISTERS(ct);
1534 #elif defined __amd64__
1535 //|.elif X64
1536 if (reg.floats < MAX_FLOAT_REGISTERS(ct)) {
1537 //| mov rax, rsp
1538 //| add rax, 32 + 8*(MAX_INT_REGISTERS(ct) + reg.floats)
1539 //| call_rrrrr extern unpack_varargs_float, L_ARG, nargs+1, TOP, MAX_FLOAT_REGISTERS(ct) - reg.floats, rax
1540 }
1541
1542 if (reg.ints < MAX_INT_REGISTERS(ct)) {
1543 //| mov rax, rsp
1544 //| add rax, 32 + 8*(reg.ints)
1545 //| call_rrrrr extern unpack_varargs_int, L_ARG, nargs+1, TOP, MAX_INT_REGISTERS(ct) - reg.ints, rax
1546 }
1547
1548 //| mov rax, rsp
1549 //| add rax, reg.off
1550 //| call_rrrrrr extern unpack_varargs_stack_skip, L_ARG, nargs+1, TOP, MAX_INT_REGISTERS(ct) - reg.ints, MAX_FLOAT_REGISTERS(ct) - reg.floats, rax
1551
1552 reg.floats = MAX_FLOAT_REGISTERS(ct);
1553 reg.ints = MAX_INT_REGISTERS(ct);
1554 #else
1555 //|.else
1556 //| mov rax, rsp
1557 //| add rax, reg.off
1558 //| call_rrrr extern unpack_varargs_stack, L_ARG, nargs+1, TOP, rax
1559 //|.endif
1560 dasm_put(Dst, 1380, reg.off, nargs+1);
1561 #endif
1562 }
1563
1564 //| mov64 rcx, perr
1565 //| mov eax, dword [rcx]
1566 //| call_r extern SetLastError, rax
1567 dasm_put(Dst, 1406, perr);
1568
1569 /* remove the stack space to call local functions */
1570 //|.if X32WIN
1571 //| add rsp, 28 // SetLastError will have already popped 4
1572 //|.else
1573 //| add rsp, 32
1574 //|.endif
1575 dasm_put(Dst, 1418);
1576
1577 #ifdef _WIN64
1578 //|.if X64WIN
1579 switch (reg.regs) {
1580 case 4:
1581 if (reg.is_float[3]) {
1582 //| movq xmm3, qword [rsp + 8*3]
1583 }
1584 if (reg.is_int[3]) {
1585 //| mov r9, [rsp + 8*3]
1586 }
1587 case 3:
1588 if (reg.is_float[2]) {
1589 //| movq xmm2, qword [rsp + 8*2]
1590 }
1591 if (reg.is_int[2]) {
1592 //| mov r8, [rsp + 8*2]
1593 }
1594 case 2:
1595 if (reg.is_float[1]) {
1596 //| movq xmm1, qword [rsp + 8*1]
1597 }
1598 if (reg.is_int[1]) {
1599 //| mov rdx, [rsp + 8*1]
1600 }
1601 case 1:
1602 if (reg.is_float[0]) {
1603 //| movq xmm0, qword [rsp]
1604 }
1605 if (reg.is_int[0]) {
1606 //| mov rcx, [rsp]
1607 }
1608 case 0:
1609 break;
1610 }
1611
1612 /* don't remove the space for the registers as we need 32 bytes of register overflow space */
1613 assert(REGISTER_STACK_SPACE(ct) == 32);
1614
1615 #elif defined __amd64__
1616 //|.elif X64
1617 switch (reg.floats) {
1618 case 8:
1619 //| movq xmm7, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+7)]
1620 case 7:
1621 //| movq xmm6, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+6)]
1622 case 6:
1623 //| movq xmm5, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+5)]
1624 case 5:
1625 //| movq xmm4, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+4)]
1626 case 4:
1627 //| movq xmm3, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+3)]
1628 case 3:
1629 //| movq xmm2, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+2)]
1630 case 2:
1631 //| movq xmm1, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+1)]
1632 case 1:
1633 //| movq xmm0, qword [rsp + 8*(MAX_INT_REGISTERS(ct))]
1634 case 0:
1635 break;
1636 }
1637
1638 switch (reg.ints) {
1639 case 6:
1640 //| mov r9, [rsp + 8*5]
1641 case 5:
1642 //| mov r8, [rsp + 8*4]
1643 case 4:
1644 //| mov rcx, [rsp + 8*3]
1645 case 3:
1646 //| mov rdx, [rsp + 8*2]
1647 case 2:
1648 //| mov rsi, [rsp + 8*1]
1649 case 1:
1650 //| mov rdi, [rsp]
1651 case 0:
1652 break;
1653 }
1654
1655 //| add rsp, REGISTER_STACK_SPACE(ct)
1656 #else
1657 //|.else
1658 if (ct->calling_convention == FAST_CALL) {
1659 switch (reg.ints) {
1660 case 2:
1661 //| mov edx, [rsp + 4]
1662 dasm_put(Dst, 1422, 4);
1663 case 1:
1664 //| mov ecx, [rsp]
1665 dasm_put(Dst, 1428);
1666 case 0:
1667 break;
1668 }
1669
1670 //| add rsp, REGISTER_STACK_SPACE(ct)
1671 dasm_put(Dst, 1432, REGISTER_STACK_SPACE(ct));
1672 }
1673 //|.endif
1674 #endif
1675
1676 #ifdef __amd64__
1677 if (ct->has_var_arg) {
1678 /* al stores an upper limit on the number of float register, note that
1679 * its allowed to be more than the actual number of float registers used as
1680 * long as its 0-8 */
1681 //|.if X64 and not X64WIN
1682 //| mov al, 8
1683 //|.endif
1684 }
1685 #endif
1686
1687 //| call extern FUNCTION
1688 //| sub rsp, 48 // 32 to be able to call local functions, 16 so we can store some local variables
1689 dasm_put(Dst, 1436);
1690
1691 /* note on windows X86 the stack may be only aligned to 4 (stdcall will
1692 * have popped a multiple of 4 bytes), but we don't need 16 byte alignment on
1693 * that platform
1694 */
1695
1696 lua_rawgeti(L, ct_usr, 0);
1697 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
1698
1699 if (mbr_ct->pointers || mbr_ct->is_reference || mbr_ct->type == INTPTR_TYPE) {
1700 lua_getuservalue(L, -1);
1701 num_upvals += 2;
1702 //| mov [rsp+32], rax // save the pointer
1703 //| get_errno
1704 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1705 //| mov rcx, [rsp+32]
1706 //| mov [rax], rcx // *(void**) cdata = val
1707 //| lua_return_arg
1708 dasm_put(Dst, 1445, perr, mbr_ct, lua_upvalueindex(num_upvals));
1709
1710 } else {
1711 switch (mbr_ct->type) {
1712 case FUNCTION_PTR_TYPE:
1713 lua_getuservalue(L, -1);
1714 num_upvals += 2;
1715 //| mov [rsp+32], rax // save the function pointer
1716 //| get_errno
1717 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1718 //| mov rcx, [rsp+32]
1719 //| mov [rax], rcx // *(cfunction**) cdata = val
1720 //| lua_return_arg
1721 dasm_put(Dst, 1445, perr, mbr_ct, lua_upvalueindex(num_upvals));
1722 break;
1723
1724 case INT64_TYPE:
1725 #if LUA_VERSION_NUM >= 503
1726 lua_pop(L, 1);
1727 if (mbr_ct->is_unsigned) {
1728 //| lua_return_ulong
1729 dasm_put(Dst, 1499, perr);
1730 } else {
1731 //| lua_return_long
1732 dasm_put(Dst, 1499, perr);
1733 }
1734 #else
1735 num_upvals++;
1736 //| // save the return value
1737 //|.if X64
1738 //| mov [rsp+32], rax
1739 //|.else
1740 //| mov [rsp+36], edx // high
1741 //| mov [rsp+32], eax // low
1742 //|.endif
1743 //|
1744 //| get_errno
1745 //| call_rrp extern push_cdata, L_ARG, 0, mbr_ct
1746 //|
1747 //| // *(int64_t*) cdata = val
1748 //|.if X64
1749 //| mov rcx, [rsp+32]
1750 //| mov [rax], rcx
1751 //|.else
1752 //| mov rcx, [rsp+36]
1753 //| mov rdx, [rsp+32]
1754 //| mov [rax+4], rcx
1755 //| mov [rax], rdx
1756 //|.endif
1757 //|
1758 //| lua_return_arg
1759 dasm_put(Dst, 1545, perr, mbr_ct);
1760 #endif
1761 break;
1762
1763 case COMPLEX_FLOAT_TYPE:
1764 lua_getuservalue(L, -1);
1765 num_upvals += 2;
1766 //|.if X64
1767 //| // complex floats are returned as two floats packed into xmm0
1768 //| movq qword [rsp+32], xmm0
1769 //|.else
1770 //| // complex floats are returned as floats in eax and edx
1771 //| mov [rsp+32], eax
1772 //| mov [rsp+36], edx
1773 //|.endif
1774 //|
1775 //| get_errno
1776 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1777 //|
1778 //| // ((complex_float*) cdata) = val
1779 //|.if X64
1780 //| mov rcx, [rsp+32]
1781 //| mov [rax], rcx
1782 //|.else
1783 //| mov ecx, [rsp+32]
1784 //| mov [rax], ecx
1785 //| mov ecx, [rsp+36]
1786 //| mov [rax+4], ecx
1787 //|.endif
1788 //|
1789 //| lua_return_arg
1790 dasm_put(Dst, 1613, perr, mbr_ct, lua_upvalueindex(num_upvals));
1791 break;
1792
1793 case COMPLEX_DOUBLE_TYPE:
1794 lua_getuservalue(L, -1);
1795 num_upvals += 2;
1796 //|.if X64
1797 //| // complex doubles are returned as xmm0 and xmm1
1798 //| movq qword [rsp+40], xmm1
1799 //| movq qword [rsp+32], xmm0
1800 //|
1801 //| get_errno
1802 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1803 //|
1804 //| // ((complex_double*) cdata)->real = val0
1805 //| // ((complex_double*) cdata)->imag = val1
1806 //| mov rcx, [rsp+40]
1807 //| mov [rax+8], rcx
1808 //| mov rcx, [rsp+32]
1809 //| mov [rax], rcx
1810 //|
1811 //|.else
1812 //| // On 32 bit we have already handled this by pushing a new cdata
1813 //| // and handing the cdata ptr in as the hidden first param, but
1814 //| // still need to add mbr_ct as an upval as its used earlier.
1815 //| // Hidden param was popped by called function, we need to realign.
1816 //| sub rsp, 4
1817 //| get_errno
1818 //|.endif
1819 //|
1820 //| lua_return_arg
1821 dasm_put(Dst, 1678, perr);
1822 break;
1823
1824 case VOID_TYPE:
1825 lua_pop(L, 1);
1826 //| lua_return_void
1827 dasm_put(Dst, 1709, perr);
1828 break;
1829
1830 case BOOL_TYPE:
1831 lua_pop(L, 1);
1832 //| lua_return_bool
1833 dasm_put(Dst, 1736, perr);
1834 break;
1835
1836 case INT8_TYPE:
1837 lua_pop(L, 1);
1838 if (mbr_ct->is_unsigned) {
1839 //| movzx eax, al
1840 dasm_put(Dst, 1192);
1841 } else {
1842 //| movsx eax, al
1843 dasm_put(Dst, 1196);
1844 }
1845 //| lua_return_int
1846 dasm_put(Dst, 1785, perr);
1847 break;
1848
1849 case INT16_TYPE:
1850 lua_pop(L, 1);
1851 if (mbr_ct->is_unsigned) {
1852 //| movzx eax, ax
1853 dasm_put(Dst, 1200);
1854 } else {
1855 //| movsx eax, ax
1856 dasm_put(Dst, 1204);
1857 }
1858 //| lua_return_int
1859 dasm_put(Dst, 1785, perr);
1860 break;
1861
1862 case INT32_TYPE:
1863 case ENUM_TYPE:
1864 lua_pop(L, 1);
1865 if (mbr_ct->is_unsigned) {
1866 //| lua_return_uint
1867 dasm_put(Dst, 1831, perr);
1868 } else {
1869 //| lua_return_int
1870 dasm_put(Dst, 1785, perr);
1871 }
1872 break;
1873
1874 case FLOAT_TYPE:
1875 lua_pop(L, 1);
1876 //|.if X64
1877 //| cvtss2sd xmm0, xmm0
1878 //|.endif
1879 //| lua_return_double
1880 dasm_put(Dst, 1877, perr);
1881 break;
1882
1883 case DOUBLE_TYPE:
1884 lua_pop(L, 1);
1885 //| lua_return_double
1886 dasm_put(Dst, 1877, perr);
1887 break;
1888
1889 default:
1890 luaL_error(L, "NYI: call return type");
1891 }
1892 }
1893
1894 assert(lua_gettop(L) == top + num_upvals);
1895 {
1896 cfunction f = compile(Dst, L, func, LUA_NOREF);
1897 /* add a callback as an upval so that the jitted code gets cleaned up when
1898 * the function gets gc'd */
1899 push_callback(L, f, func);
1900 lua_pushcclosure(L, (lua_CFunction) f, num_upvals+1);
1901 }
1902 }
1903