Debian Codegraph @

texk/web2c/luatexdir/luaffi/call_x64.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 x64 version 1.4.0
5 ** DO NOT EDIT! The original file is in "call_x64.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 //|.arch x64
17 #if DASM_VERSION != 10500
18 #error "Version mismatch between DynASM and included encoding engine"
19 #endif
20
21 //|.actionlist build_actionlist
22 static const unsigned char build_actionlist[1994] = {
23 72,139,141,233,255,72,137,132,253,36,233,255,252,243,15,126,133,233,255,252,
24 243,15,90,133,233,255,102,15,214,132,253,36,233,255,252,242,15,90,192,102,
25 15,214,132,253,36,233,255,252,242,15,90,192,102,15,126,132,253,36,233,255,
26 85,72,137,229,65,84,72,129,252,236,239,102,15,214,69,252,240,102,15,214,77,
27 232,102,15,214,85,224,102,15,214,93,216,102,15,214,101,208,102,15,214,109,
28 200,102,15,214,117,192,102,15,214,125,184,72,137,125,176,72,137,117,168,72,
29 137,85,160,72,137,77,152,76,137,69,144,76,137,77,136,255,73,188,237,237,255,
30 72,199,194,237,72,199,198,237,76,137,231,232,251,1,0,255,72,199,194,237,72,
31 199,198,252,255,252,255,252,255,252,255,76,137,231,232,251,1,0,255,72,199,
32 194,237,72,199,198,237,76,137,231,232,251,1,0,72,186,237,237,72,199,198,252,
33 255,252,255,252,255,252,255,76,137,231,232,251,1,1,255,72,137,8,72,199,198,
34 252,254,252,255,252,255,252,255,76,137,231,232,251,1,2,255,72,186,237,237,
35 72,199,198,0,0,0,0,76,137,231,232,251,1,1,255,72,137,8,255,102,15,214,0,255,
36 102,15,214,64,8,255,76,137,231,232,251,1,3,255,15,182,201,72,137,206,76,137,
37 231,232,251,1,4,255,15,182,201,255,15,190,201,255,72,137,206,76,137,231,232,
38 251,1,5,255,15,183,201,255,15,191,201,255,72,137,206,76,137,231,232,251,1,
39 6,255,72,185,237,237,72,199,194,237,72,199,198,237,76,137,231,232,251,1,7,
40 255,72,199,194,237,72,199,198,252,254,252,255,252,255,252,255,76,137,231,
41 232,251,1,0,72,185,237,237,72,199,194,252,255,252,255,252,255,252,255,72,
42 199,198,252,254,252,255,252,255,252,255,76,137,231,232,251,1,8,72,137,68,
43 36,32,72,199,198,252,252,252,255,252,255,252,255,76,137,231,232,251,1,9,72,
44 139,68,36,32,255,72,199,194,237,72,199,198,252,254,252,255,252,255,252,255,
45 76,137,231,232,251,1,0,72,185,237,237,72,199,194,252,255,252,255,252,255,
46 252,255,72,199,198,252,254,252,255,252,255,252,255,76,137,231,232,251,1,10,
47 137,68,36,32,72,199,198,252,252,252,255,252,255,252,255,76,137,231,232,251,
48 1,9,139,68,36,32,255,72,199,198,252,254,252,255,252,255,252,255,76,137,231,
49 232,251,1,9,255,72,199,198,252,255,252,255,252,255,252,255,76,137,231,232,
50 251,1,11,255,72,199,198,252,255,252,255,252,255,252,255,76,137,231,232,251,
51 1,12,255,137,68,36,32,72,199,198,252,253,252,255,252,255,252,255,76,137,231,
52 232,251,1,9,139,68,36,32,255,72,199,198,252,255,252,255,252,255,252,255,76,
53 137,231,232,251,1,13,255,72,199,198,252,255,252,255,252,255,252,255,76,137,
54 231,232,251,1,14,255,72,137,68,36,32,72,199,198,252,253,252,255,252,255,252,
55 255,76,137,231,232,251,1,9,72,139,68,36,32,255,72,199,198,252,255,252,255,
56 252,255,252,255,76,137,231,232,251,1,15,72,137,68,36,32,72,199,198,252,253,
57 252,255,252,255,252,255,76,137,231,232,251,1,9,72,139,68,36,32,255,72,199,
58 198,252,255,252,255,252,255,252,255,76,137,231,232,251,1,16,102,15,214,68,
59 36,32,72,199,198,252,253,252,255,252,255,252,255,76,137,231,232,251,1,9,255,
60 252,242,15,90,68,36,32,255,252,243,15,126,68,36,32,255,72,199,198,252,255,
61 252,255,252,255,252,255,76,137,231,232,251,1,17,102,15,214,68,36,32,72,199,
62 198,252,253,252,255,252,255,252,255,76,137,231,232,251,1,9,252,243,15,126,
63 68,36,32,255,72,199,198,252,255,252,255,252,255,252,255,76,137,231,232,251,
64 1,18,102,15,214,68,36,32,102,15,214,76,36,40,72,199,198,252,253,252,255,252,
65 255,252,255,76,137,231,232,251,1,9,252,243,15,126,68,36,32,252,243,15,126,
66 76,36,40,255,76,139,101,252,248,72,137,252,236,93,194,236,255,85,72,137,229,
67 65,84,65,85,73,137,252,252,76,137,231,232,251,1,19,73,137,197,72,129,252,
68 248,239,255,15,141,244,248.0,102,184,0,0,72,190,237,237,76,137,231,232,251,
69 1,20,248,2,15,142,244,247.0,102,184,0,0,72,190,237,237,76,137,231,232,251,
70 1,20,255,15,141,244,247.0,102,184,0,0,72,190,237,237,76,137,231,232,251,1,
71 20,255,248,1,255,72,193,224,4,72,41,196,72,129,252,236,239,255,72,185,237,
72 237,72,199,194,237,72,199,198,237,76,137,231,232,251,1,8,255,72,185,237,237,
73 72,199,194,237,72,199,198,237,76,137,231,232,251,1,21,255,72,185,237,237,
74 72,199,194,237,72,199,198,237,76,137,231,232,251,1,10,255,72,199,198,237,
75 76,137,231,232,251,1,12,255,15,182,192,255,15,190,192,255,15,183,192,255,
76 15,191,192,255,72,199,198,237,76,137,231,232,251,1,12,131,252,248,0,15,149,
77 208,15,182,192,255,72,199,198,237,76,137,231,232,251,1,11,255,72,199,198,
78 237,76,137,231,232,251,1,15,255,72,199,198,237,76,137,231,232,251,1,13,255,
79 72,199,198,237,76,137,231,232,251,1,14,255,72,199,198,237,76,137,231,232,
80 251,1,16,255,72,199,198,237,76,137,231,232,251,1,18,255,252,243,15,126,193,
81 255,72,141,132,253,36,233,72,131,252,236,4,72,199,194,237,76,137,230,72,137,
82 199,232,251,1,18,255,72,199,198,237,76,137,231,232,251,1,17,255,72,199,198,
83 237,76,137,231,232,251,1,17,137,4,36,217,4,36,255,137,20,36,217,4,36,255,
84 72,137,224,72,129,192,239,73,137,192,72,199,193,237,76,137,252,234,72,199,
85 198,237,76,137,231,232,251,1,22,255,72,137,224,72,129,192,239,73,137,192,
86 72,199,193,237,76,137,252,234,72,199,198,237,76,137,231,232,251,1,23,255,
87 72,137,224,72,129,192,239,73,137,193,73,199,192,237,72,199,193,237,76,137,
88 252,234,72,199,198,237,76,137,231,232,251,1,24,255,72,185,237,237,139,1,72,
89 137,199,232,251,1,25,255,72,131,196,32,255,252,243,15,126,188,253,36,233,
90 255,252,243,15,126,180,253,36,233,255,252,243,15,126,172,253,36,233,255,252,
91 243,15,126,164,253,36,233,255,252,243,15,126,156,253,36,233,255,252,243,15,
92 126,148,253,36,233,255,252,243,15,126,140,253,36,233,255,252,243,15,126,132,
93 253,36,233,255,76,139,140,253,36,233,255,76,139,132,253,36,233,255,72,139,
94 140,253,36,233,255,72,139,148,253,36,233,255,72,139,180,253,36,233,255,72,
95 139,60,36,255,72,129,196,239,255,176,8,255,232,251,1,26,72,131,252,236,48,
96 255,72,137,68,36,32,232,251,1,27,72,185,237,237,137,1,72,186,237,237,72,199,
97 198,237,76,137,231,232,251,1,1,72,139,76,36,32,72,137,8,184,1,0,0,0,76,139,
98 109,252,240,76,139,101,252,248,72,137,252,236,93,195,255,72,137,68,36,32,
99 232,251,1,27,72,185,237,237,137,1,72,139,68,36,32,72,137,198,76,137,231,232,
100 251,1,28,184,1,0,0,0,76,139,109,252,240,76,139,101,252,248,72,137,252,236,
101 93,195,255,102,15,214,68,36,32,232,251,1,27,72,185,237,237,137,1,72,186,237,
102 237,72,199,198,237,76,137,231,232,251,1,1,72,139,76,36,32,72,137,8,184,1,
103 0,0,0,76,139,109,252,240,76,139,101,252,248,72,137,252,236,93,195,255,102,
104 15,214,76,36,40,102,15,214,68,36,32,232,251,1,27,72,185,237,237,137,1,72,
105 186,237,237,72,199,198,237,76,137,231,232,251,1,1,72,139,76,36,40,72,137,
106 72,8,72,139,76,36,32,72,137,8,184,1,0,0,0,76,139,109,252,240,76,139,101,252,
107 248,72,137,252,236,93,195,255,232,251,1,27,72,185,237,237,137,1,184,0,0,0,
108 0,76,139,109,252,240,76,139,101,252,248,72,137,252,236,93,195,255,15,182,
109 192,137,68,36,32,232,251,1,27,72,185,237,237,137,1,139,68,36,32,72,137,198,
110 76,137,231,232,251,1,4,184,1,0,0,0,76,139,109,252,240,76,139,101,252,248,
111 72,137,252,236,93,195,255,137,68,36,32,232,251,1,27,72,185,237,237,137,1,
112 139,68,36,32,72,137,198,76,137,231,232,251,1,5,184,1,0,0,0,76,139,109,252,
113 240,76,139,101,252,248,72,137,252,236,93,195,255,137,68,36,32,232,251,1,27,
114 72,185,237,237,137,1,139,68,36,32,72,137,198,76,137,231,232,251,1,6,184,1,
115 0,0,0,76,139,109,252,240,76,139,101,252,248,72,137,252,236,93,195,255,252,
116 243,15,90,192,102,15,214,68,36,32,232,251,1,27,72,185,237,237,137,1,252,243,
117 15,126,68,36,32,76,137,231,232,251,1,3,184,1,0,0,0,76,139,109,252,240,76,
118 139,101,252,248,72,137,252,236,93,195,255
119 };
120
121 //|.globalnames globnames
122 static const char *const globnames[] = {
123 (const char *)0
124 };
125 //|.externnames extnames
126 static const char *const extnames[] = {
127 "lua_rawgeti",
128 "push_cdata",
129 "lua_remove",
130 "lua_pushnumber",
131 "lua_pushboolean",
132 "push_int",
133 "push_uint",
134 "lua_callk",
135 "check_typed_pointer",
136 "lua_settop",
137 "check_enum",
138 "check_uint32",
139 "check_int32",
140 "check_uint64",
141 "check_int64",
142 "check_uintptr",
143 "check_double",
144 "check_complex_float",
145 "check_complex_double",
146 "lua_gettop",
147 "luaL_error",
148 "check_typed_cfunction",
149 "unpack_varargs_float",
150 "unpack_varargs_int",
151 "unpack_varargs_stack_skip",
152 "SetLastError",
153 "FUNCTION",
154 "GetLastError",
155 "lua_pushinteger",
156 (const char *)0
157 };
158
159 //|.define L_ARG, r12
160 //|.define TOP, r13
161
162 //| // the 5 and 6 arg forms are only used on posix x64
163 //|.macro call_rrrrrr, func, arg0, arg1, arg2, arg3, arg4, arg5
164 //| mov r9, arg5
165 //| mov r8, arg4
166 //| mov rcx, arg3
167 //| mov rdx, arg2
168 //| mov rsi, arg1
169 //| mov rdi, arg0
170 //| call func
171 //|.endmacro
172 //|.macro call_rrrrr, func, arg0, arg1, arg2, arg3, arg4
173 //| mov r8, arg4
174 //| mov rcx, arg3
175 //| mov rdx, arg2
176 //| mov rsi, arg1
177 //| mov rdi, arg0
178 //| call func
179 //|.endmacro
180 //|
181 //|.macro call_rrrp, func, arg0, arg1, arg2, arg3
182 //| mov64 rcx, arg3
183 //| mov rdx, arg2
184 //| mov rsi, arg1
185 //| mov rdi, arg0
186 //| call func
187 //|.endmacro
188 //|.macro call_rrrr, func, arg0, arg1, arg2, arg3
189 //| mov rcx, arg3
190 //| mov rdx, arg2
191 //| mov rsi, arg1
192 //| mov rdi, arg0
193 //| call func
194 //|.endmacro
195 //|
196 //|.macro call_rrp, func, arg0, arg1, arg2
197 //| mov64 rdx, arg2
198 //| mov rsi, arg1
199 //| mov rdi, arg0
200 //| call func
201 //|.endmacro
202 //|.macro call_rrr, func, arg0, arg1, arg2
203 //| mov rdx, arg2
204 //| mov rsi, arg1
205 //| mov rdi, arg0
206 //| call func
207 //|.endmacro
208 //|
209 //|.macro call_rp, func, arg0, arg1
210 //| mov64 rsi, arg1
211 //| mov rdi, arg0
212 //| call func
213 //|.endmacro
214 //|.macro call_rr, func, arg0, arg1
215 //| mov rsi, arg1
216 //| mov rdi, arg0
217 //| call func
218 //|.endmacro
219 //|
220 //|.macro call_r, func, arg0
221 //| mov rdi, arg0
222 //| call func
223 //|.endmacro
224
225
226 //|.macro epilog
227 //| mov TOP, [rbp-16]
228 //| mov L_ARG, [rbp-8]
229 //| mov rsp, rbp
230 //| pop rbp
231 //| ret
232 //|.endmacro
233
234 //|.macro get_errno // note trashes registers
235 //| call extern GetLastError
236 //| mov64 rcx, perr
237 //| mov dword [rcx], eax
238 //|.endmacro
239
240 //|.macro too_few_arguments
241 //| mov ax, 0
242 //| call_rp extern luaL_error, L_ARG, &"too few arguments"
243 //|.endmacro
244
245 //|.macro too_many_arguments
246 //| mov ax, 0
247 //| call_rp extern luaL_error, L_ARG, &"too many arguments"
248 //|.endmacro
249
250 //|.macro lua_return_arg
251 //| mov eax, 1
252 //| epilog
253 //|.endmacro
254
255 //|.macro lua_return_void
256 //| get_errno
257 //| mov eax, 0
258 //| epilog
259 //|.endmacro
260
261 //|.macro lua_return_double
262 //| movq qword [rsp+32], xmm0
263 //| get_errno
264 //| movq xmm0, qword [rsp+32]
265 //| mov rdi, L_ARG
266 //| call extern lua_pushnumber
267 //| lua_return_arg
268 //|.endmacro
269
270 //|.macro lua_return_bool
271 //| movzx eax, al
272 //| mov [rsp+32], eax
273 //| get_errno
274 //| mov eax, [rsp+32]
275 //| call_rr extern lua_pushboolean, L_ARG, rax
276 //| lua_return_arg
277 //|.endmacro
278
279 //|.macro lua_return_int
280 //| mov [rsp+32], eax
281 //| get_errno
282 //| mov eax, [rsp+32]
283 //| call_rr extern push_int, L_ARG, rax
284 //| lua_return_arg
285 //|.endmacro
286
287 //|.macro lua_return_uint
288 //| mov [rsp+32], eax
289 //| get_errno
290 //| mov eax, [rsp+32]
291 //| call_rr extern push_uint, L_ARG, rax
292 //| lua_return_arg
293 //|.endmacro
294
295 //|.macro lua_return_long
296 //| mov [rsp+32], rax
297 //| get_errno
298 //| mov rax, [rsp+32]
299 //| call_rr extern lua_pushinteger, L_ARG, rax
300 //| lua_return_arg
301 //|.endmacro
302
303 //|.macro lua_return_ulong
304 //| mov [rsp+32], rax
305 //| get_errno
306 //| mov rax, [rsp+32]
307 //| call_rr extern lua_pushinteger, L_ARG, rax
308 //| lua_return_arg
309 //|.endmacro
310
311 //|.macro save_registers
312 //| // use rbp relative so we store values in the outer stack frame
313 //| movq qword [rbp-16], xmm0
314 //| movq qword [rbp-24], xmm1
315 //| movq qword [rbp-32], xmm2
316 //| movq qword [rbp-40], xmm3
317 //| movq qword [rbp-48], xmm4
318 //| movq qword [rbp-56], xmm5
319 //| movq qword [rbp-64], xmm6
320 //| movq qword [rbp-72], xmm7
321 //| mov [rbp-80], rdi
322 //| mov [rbp-88], rsi
323 //| mov [rbp-96], rdx
324 //| mov [rbp-104], rcx
325 //| mov [rbp-112], r8
326 //| mov [rbp-120], r9
327 //|.endmacro
328
329 #define JUMP_SIZE 14
330 #define MIN_BRANCH INT32_MIN
331 #define MAX_BRANCH INT32_MAX
332 #define BRANCH_OFF 4
333
334 static void compile_extern_jump(struct jit* jit, lua_State* L, cfunction func, uint8_t* code)
335 {
336 /* The jump code is the function pointer followed by a stub to call the
337 * function pointer. The stub exists in 64 bit so we can jump to functions
338 * with an offset greater than 2 GB.
339 *
340 * Note we have to manually set this up since there are commands buffered
341 * in the jit state and dynasm doesn't support rip relative addressing.
342 *
343 * eg on 64 bit:
344 * 0-8: function ptr
345 * 8-14: jmp aword [rip-14]
346 *
347 */
348 *(cfunction*) code = func;
349 code[8] = 0xFF; /* FF /4 operand for jmp */
350 code[9] = 0x25; /* RIP displacement */
351 *(int32_t*) &code[10] = -14;
352 }
353
354 void compile_globals(struct jit* jit, lua_State* L)
355 {
356 struct jit* Dst = jit;
357 int* perr = &jit->last_errno;
358 dasm_setup(Dst, build_actionlist);
359
360 /* Note: since the return code uses EBP to reset the stack pointer, we
361 * don't have to track the amount of stack space used. It also means we
362 * can handle stdcall and cdecl with the same code.
363 */
364
365 /* Note the various call_* functions want 32 bytes of 16 byte aligned
366 * stack
367 */
368
369 compile(Dst, L, NULL, LUA_NOREF);
370 }
371
372 int x86_return_size(lua_State* L, int usr, const struct ctype* ct)
373 {
374 int ret = 0;
375 const struct ctype* mt;
376
377 if (ct->calling_convention != C_CALL) {
378 size_t i;
379 size_t argn = lua_rawlen(L, usr);
380 for (i = 1; i <= argn; i++) {
381 lua_rawgeti(L, usr, (int) i);
382 mt = (const struct ctype*) lua_touserdata(L, -1);
383
384 if (mt->pointers || mt->is_reference) {
385 ret += sizeof(void*);
386 } else {
387 switch (mt->type) {
388 case DOUBLE_TYPE:
389 case COMPLEX_FLOAT_TYPE:
390 case INT64_TYPE:
391 ret += 8;
392 break;
393 case COMPLEX_DOUBLE_TYPE:
394 ret += 16;
395 break;
396 case INTPTR_TYPE:
397 ret += sizeof(intptr_t);
398 break;
399 case FUNCTION_PTR_TYPE:
400 ret += sizeof(cfunction);
401 break;
402 case BOOL_TYPE:
403 case FLOAT_TYPE:
404 case INT8_TYPE:
405 case INT16_TYPE:
406 case INT32_TYPE:
407 case ENUM_TYPE:
408 ret += 4;
409 break;
410 default:
411 return luaL_error(L, "NYI - argument type");
412 }
413 }
414
415 lua_pop(L, 1);
416 }
417 }
418
419 return ret;
420 }
421
422 #define MAX_INT_REGISTERS(ct) 6 /* rdi, rsi, rdx, rcx, r8, r9 */
423 #define MAX_FLOAT_REGISTERS(ct) 8 /* xmm0-7 */
424
425 struct reg_alloc {
426 int floats;
427 int ints;
428 int off;
429 };
430
431 #define REGISTER_STACK_SPACE(ct) (14*8)
432
433 /* Fastcall:
434 * Uses ecx, edx as first two int registers
435 * Everything else on stack (include 64bit ints)
436 * No overflow stack space
437 * Pops the stack before returning
438 * Returns int in eax, float in ST0
439 * We use the same register allocation logic as posix x64 with 2 int regs and 0 float regs
440 */
441
442 static void get_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64)
443 {
444 /* grab the register from the shadow space */
445 if (reg->ints < MAX_INT_REGISTERS(ct)) {
446 //| mov rcx, [rbp - 80 - 8*reg->ints]
447 dasm_put(Dst, 0, - 80 - 8*reg->ints);
448 reg->ints++;
449 }
450 else if (is_int64) {
451 //| mov rcx, [rbp + reg->off]
452 dasm_put(Dst, 0, reg->off);
453 reg->off += 8;
454 } else {
455 //| mov ecx, [rbp + reg->off]
456 dasm_put(Dst, 1, reg->off);
457 /* The parameters to a function on stack are always 8 byte aligned. */
458 reg->off += 8;
459 }
460 }
461
462 static void add_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_int64)
463 {
464 if (reg->ints < MAX_INT_REGISTERS(ct)) {
465 //| mov [rsp + 32 + 8*reg->ints], rax
466 dasm_put(Dst, 5, 32 + 8*reg->ints);
467 reg->ints++;
468 }
469 else {
470 if (reg->off % 8 != 0) {
471 reg->off += 8 - (reg->off % 8);
472 }
473 if (is_int64) {
474 //| mov [rsp + reg->off], rax
475 dasm_put(Dst, 5, reg->off);
476 reg->off += 8;
477 } else {
478 //| mov [rsp+reg->off], eax
479 dasm_put(Dst, 6, reg->off);
480 reg->off += 4;
481 }
482 }
483 }
484
485 static void get_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
486 {
487 int off;
488
489 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
490 off = -16 - 8*reg->floats;
491 reg->floats++;
492 }
493 else {
494 off = reg->off;
495 reg->off += is_double ? 8 : 4;
496 }
497
498 if (is_double) {
499 //| movq xmm0, qword [rbp + off]
500 dasm_put(Dst, 12, off);
501 } else {
502 //| cvtss2sd xmm0, dword [rbp + off]
503 dasm_put(Dst, 19, off);
504 }
505 }
506
507 static void add_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
508 {
509 if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
510 if (is_double) {
511 //| movq qword [rsp + 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)], xmm0
512 dasm_put(Dst, 26, 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats));
513 } else {
514 //| cvtsd2ss xmm0, xmm0
515 //| movq qword [rsp + 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats)], xmm0
516 dasm_put(Dst, 34, 32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats));
517 }
518 reg->floats++;
519 }
520 else {
521 if (reg->off % 8 != 0) {
522 reg->off += 8 - (reg->off % 8);
523 }
524 if (is_double) {
525 //| movq qword [rsp + reg->off], xmm0
526 dasm_put(Dst, 26, reg->off);
527 reg->off += 8;
528 } else {
529 //| cvtsd2ss xmm0, xmm0
530 //| movd dword [rsp + reg->off], xmm0
531 dasm_put(Dst, 47, reg->off);
532 reg->off += 8;
533 }
534 }
535 }
536
537 #define add_pointer(jit, ct, reg) add_int(jit, ct, reg, 1)
538 #define get_pointer(jit, ct, reg) get_int(jit, ct, reg, 1)
539
540 cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct)
541 {
542 int i, nargs;
543 cfunction* pf;
544 struct ctype ct2 = *ct;
545 const struct ctype* mt;
546 struct reg_alloc reg;
547 int num_upvals = 0;
548 int top = lua_gettop(L);
549 struct jit* Dst = get_jit(L);
550 int ref;
551 int hidden_arg_off = 0;
552
553 ct_usr = lua_absindex(L, ct_usr);
554 fidx = lua_absindex(L, fidx);
555
556 assert(lua_isnil(L, fidx) || lua_isfunction(L, fidx));
557
558 memset(&reg, 0, sizeof(reg));
559 reg.off = 16;
560
561 dasm_setup(Dst, build_actionlist);
562
563 // add a table to store ctype and function upvalues
564 // callback_set assumes the first value is the lua function
565 nargs = (int) lua_rawlen(L, ct_usr);
566 lua_newtable(L);
567 lua_pushvalue(L, -1);
568 ref = luaL_ref(L, LUA_REGISTRYINDEX);
569
570 if (ct->has_var_arg) {
571 luaL_error(L, "can't create callbacks with varargs");
572 }
573
574 // setup a stack frame to hold args for the call into lua_call
575
576 //| push rbp
577 //| mov rbp, rsp
578 //| push L_ARG
579 //| // stack is 4 or 8 (mod 16) (L_ARG, rbp, rip)
580 //| // 8 to realign, 16 for return vars, 32 for local calls, rest to save registers
581 //| sub rsp, 8 + 16 + 32 + REGISTER_STACK_SPACE(ct)
582 //| save_registers
583 dasm_put(Dst, 60, 8 + 16 + 32 + REGISTER_STACK_SPACE(ct));
584 if (ct->calling_convention == FAST_CALL) {
585 }
586
587 // hardcode the lua_State* value into the assembly
588 //| mov64 L_ARG, L
589 dasm_put(Dst, 137, (unsigned int)((uintptr_t)(L)), (unsigned int)(((uintptr_t)(L))>>32));
590
591 /* get the upval table */
592 //| call_rrr extern lua_rawgeti, L_ARG, LUA_REGISTRYINDEX, ref
593 dasm_put(Dst, 142, ref, LUA_REGISTRYINDEX);
594
595 /* get the lua function */
596 lua_pushvalue(L, fidx);
597 lua_rawseti(L, -2, ++num_upvals);
598 assert(num_upvals == CALLBACK_FUNC_USR_IDX);
599 //| call_rrr extern lua_rawgeti, L_ARG, -1, num_upvals
600 dasm_put(Dst, 158, num_upvals);
601
602 (void) hidden_arg_off;
603
604 for (i = 1; i <= nargs; i++) {
605 lua_rawgeti(L, ct_usr, i);
606 mt = (const struct ctype*) lua_touserdata(L, -1);
607
608 if (mt->pointers || mt->is_reference) {
609 lua_getuservalue(L, -1);
610 lua_rawseti(L, -3, ++num_upvals); /* usr value */
611 lua_rawseti(L, -2, ++num_upvals); /* mt */
612 /* on the lua stack in the callback:
613 * upval tbl, lua func, i-1 args
614 */
615 //| call_rrr extern lua_rawgeti, L_ARG, -i-1, num_upvals-1
616 //| call_rrp extern push_cdata, L_ARG, -1, mt
617 dasm_put(Dst, 181, num_upvals-1, -i-1, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32));
618 get_pointer(Dst, ct, &reg);
619 //| mov [rax], rcx
620 //| call_rr, extern lua_remove, L_ARG, -2
621 dasm_put(Dst, 219);
622 } else {
623 switch (mt->type) {
624 case INT64_TYPE:
625 lua_getuservalue(L, -1);
626 lua_rawseti(L, -3, ++num_upvals); /* mt */
627 lua_pop(L, 1);
628 //| call_rrp extern push_cdata, L_ARG, 0, mt
629 dasm_put(Dst, 241, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32));
630 get_int(Dst, ct, &reg, 1);
631 //| mov [rax], rcx
632 dasm_put(Dst, 260);
633 break;
634
635 case INTPTR_TYPE:
636 lua_getuservalue(L, -1);
637 lua_rawseti(L, -3, ++num_upvals); /* mt */
638 lua_pop(L, 1);
639 //| call_rrp extern push_cdata, L_ARG, 0, mt
640 dasm_put(Dst, 241, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32));
641 get_pointer(Dst, ct, &reg);
642 //| mov [rax], rcx
643 dasm_put(Dst, 260);
644 break;
645
646 case COMPLEX_FLOAT_TYPE:
647 lua_pop(L, 1);
648 /* complex floats are two floats packed into a double */
649 //| call_rrp extern push_cdata, L_ARG, 0, mt
650 dasm_put(Dst, 241, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32));
651 get_float(Dst, ct, &reg, 1);
652 //| movq qword [rax], xmm0
653 dasm_put(Dst, 264);
654 break;
655
656 case COMPLEX_DOUBLE_TYPE:
657 lua_pop(L, 1);
658 //| call_rrp extern push_cdata, L_ARG, 0, mt
659 dasm_put(Dst, 241, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32));
660 /* real */
661 get_float(Dst, ct, &reg, 1);
662 //| movq qword [rax], xmm0
663 dasm_put(Dst, 264);
664 /* imag */
665 get_float(Dst, ct, &reg, 1);
666 //| movq qword [rax+8], xmm0
667 dasm_put(Dst, 269);
668 break;
669
670 case FLOAT_TYPE:
671 case DOUBLE_TYPE:
672 lua_pop(L, 1);
673 get_float(Dst, ct, &reg, mt->type == DOUBLE_TYPE);
674 //| // for 64bit xmm0 is already set
675 //| mov rdi, L_ARG
676 //| call extern lua_pushnumber
677 dasm_put(Dst, 275);
678 break;
679
680 case BOOL_TYPE:
681 lua_pop(L, 1);
682 get_int(Dst, ct, &reg, 0);
683 //| movzx ecx, cl
684 //| call_rr extern lua_pushboolean, L_ARG, rcx
685 dasm_put(Dst, 283);
686 break;
687
688 case INT8_TYPE:
689 lua_pop(L, 1);
690 get_int(Dst, ct, &reg, 0);
691 if (mt->is_unsigned) {
692 //| movzx ecx, cl
693 dasm_put(Dst, 297);
694 } else {
695 //| movsx ecx, cl
696 dasm_put(Dst, 301);
697 }
698 //| call_rr extern push_int, L_ARG, rcx
699 dasm_put(Dst, 305);
700 break;
701
702 case INT16_TYPE:
703 lua_pop(L, 1);
704 get_int(Dst, ct, &reg, 0);
705 if (mt->is_unsigned) {
706 //| movzx ecx, cx
707 dasm_put(Dst, 316);
708 } else {
709 //| movsx ecx, cx
710 dasm_put(Dst, 320);
711 }
712 //| call_rr extern push_int, L_ARG, rcx
713 dasm_put(Dst, 305);
714 break;
715
716 case ENUM_TYPE:
717 case INT32_TYPE:
718 lua_pop(L, 1);
719 get_int(Dst, ct, &reg, 0);
720 if (mt->is_unsigned) {
721 //| call_rr extern push_uint, L_ARG, rcx
722 dasm_put(Dst, 324);
723 } else {
724 //| call_rr extern push_int, L_ARG, rcx
725 dasm_put(Dst, 305);
726 }
727 break;
728
729 default:
730 luaL_error(L, "NYI: callback arg type");
731 }
732 }
733 }
734
735 lua_rawgeti(L, ct_usr, 0);
736 mt = (const struct ctype*) lua_touserdata(L, -1);
737
738 //| call_rrrp extern lua_callk, L_ARG, nargs, (mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0, 0
739 dasm_put(Dst, 335, (unsigned int)((uintptr_t)(0)), (unsigned int)(((uintptr_t)(0))>>32), (mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0, nargs);
740
741 // Unpack the return argument if not "void", also clean-up the lua stack
742 // to remove the return argument and bind table. Use lua_settop rather
743 // than lua_pop as lua_pop is implemented as a macro.
744 if (mt->pointers || mt->is_reference) {
745 lua_getuservalue(L, -1);
746 lua_rawseti(L, -3, ++num_upvals); /* usr value */
747 lua_rawseti(L, -2, ++num_upvals); /* mt */
748 //| call_rrr extern lua_rawgeti, L_ARG, -2, num_upvals-1
749 //| call_rrrp extern check_typed_pointer, L_ARG, -2, -1, mt
750 //| mov [rsp+32], rax
751 //| call_rr extern lua_settop, L_ARG, -4
752 //| mov rax, [rsp+32]
753 dasm_put(Dst, 355, num_upvals-1, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32));
754
755 } else {
756 switch (mt->type) {
757 case ENUM_TYPE:
758 lua_getuservalue(L, -1);
759 lua_rawseti(L, -3, ++num_upvals); /* usr value */
760 lua_rawseti(L, -2, ++num_upvals); /* mt */
761 //| call_rrr extern lua_rawgeti, L_ARG, -2, num_upvals-1
762 //| call_rrrp, extern check_enum, L_ARG, -2, -1, mt
763 //| mov [rsp+32], eax
764 //| call_rr extern lua_settop, L_ARG, -4
765 //| mov eax, [rsp+32]
766 dasm_put(Dst, 439, num_upvals-1, (unsigned int)((uintptr_t)(mt)), (unsigned int)(((uintptr_t)(mt))>>32));
767 break;
768
769 case VOID_TYPE:
770 lua_pop(L, 1);
771 //| call_rr extern lua_settop, L_ARG, -2
772 dasm_put(Dst, 521);
773 break;
774
775 case BOOL_TYPE:
776 case INT8_TYPE:
777 case INT16_TYPE:
778 case INT32_TYPE:
779 lua_pop(L, 1);
780 if (mt->is_unsigned) {
781 //| call_rr extern check_uint32, L_ARG, -1
782 dasm_put(Dst, 540);
783 } else {
784 //| call_rr extern check_int32, L_ARG, -1
785 dasm_put(Dst, 559);
786 }
787 //| mov [rsp+32], eax
788 //| call_rr extern lua_settop, L_ARG, -3
789 //| mov eax, [rsp+32]
790 dasm_put(Dst, 578);
791 break;
792
793 case INT64_TYPE:
794 lua_pop(L, 1);
795
796 if (mt->is_unsigned) {
797 //| call_rr extern check_uint64, L_ARG, -1
798 dasm_put(Dst, 605);
799 } else {
800 //| call_rr extern check_int64, L_ARG, -1
801 dasm_put(Dst, 624);
802 }
803
804 //| mov [rsp+32], rax
805 //| call_rr extern lua_settop, L_ARG, -3
806 //| mov rax, [rsp+32]
807 dasm_put(Dst, 643);
808 break;
809
810 case INTPTR_TYPE:
811 lua_pop(L, 1);
812 //| call_rr extern check_uintptr, L_ARG, -1
813 //| mov [rsp+32], rax
814 //| call_rr extern lua_settop, L_ARG, -3
815 //| mov rax, [rsp+32]
816 dasm_put(Dst, 672);
817 break;
818
819 case FLOAT_TYPE:
820 case DOUBLE_TYPE:
821 lua_pop(L, 1);
822 //| call_rr extern check_double, L_ARG, -1
823 //| movq qword [rsp+32], xmm0
824 //| call_rr extern lua_settop, L_ARG, -3
825 dasm_put(Dst, 719);
826 if (mt->type == FLOAT_TYPE) {
827 //| cvtsd2ss xmm0, qword [rsp+32]
828 dasm_put(Dst, 762);
829 } else {
830 //| movq xmm0, qword [rsp+32]
831 dasm_put(Dst, 770);
832 }
833 break;
834
835 case COMPLEX_FLOAT_TYPE:
836 lua_pop(L, 1);
837 #if !defined HAVE_COMPLEX
838 luaL_error(L, "ffi lib compiled without complex number support");
839 #endif
840 /* on 64 bit complex floats are two floats packed into a double,
841 * on 32 bit returned complex floats use eax and edx */
842 //| call_rr extern check_complex_float, L_ARG, -1
843 //|
844 //| movq qword [rsp+32], xmm0
845 //|
846 //| call_rr extern lua_settop, L_ARG, -3
847 //|
848 //| movq xmm0, qword [rsp+32]
849 dasm_put(Dst, 778);
850 break;
851
852 case COMPLEX_DOUBLE_TYPE:
853 lua_pop(L, 1);
854 #if !defined HAVE_COMPLEX
855 luaL_error(L, "ffi lib compiled without complex number support");
856 #endif
857 /* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit
858 * there is a hidden first parameter that points to 16 bytes where
859 * the returned arg is stored which is popped by the called
860 * function */
861 //| call_rr extern check_complex_double, L_ARG, -1
862 //| movq qword [rsp+32], xmm0
863 //| movq qword [rsp+40], xmm1
864 //| call_rr extern lua_settop, L_ARG, -3
865 //| movq xmm0, qword [rsp+32]
866 //| movq xmm1, qword [rsp+40]
867 dasm_put(Dst, 828);
868 break;
869
870 default:
871 luaL_error(L, "NYI: callback return type");
872 }
873 }
874
875 //| mov L_ARG, [rbp-8]
876 //| mov rsp, rbp
877 //| pop rbp
878 //| ret x86_return_size(L, ct_usr, ct)
879 dasm_put(Dst, 891, x86_return_size(L, ct_usr, ct));
880
881 lua_pop(L, 1); /* upval table - already in registry */
882 assert(lua_gettop(L) == top);
883
884 ct2.is_jitted = 1;
885 pf = (cfunction*) push_cdata(L, ct_usr, &ct2);
886 *pf = compile(Dst, L, NULL, ref);
887
888 assert(lua_gettop(L) == top + 1);
889
890 return *pf;
891 }
892
893 void compile_function(lua_State* L, cfunction func, int ct_usr, const struct ctype* ct)
894 {
895 size_t i, nargs;
896 int num_upvals;
897 int orig_top;
898 const struct ctype* mbr_ct;
899 struct jit* Dst = get_jit(L);
900 struct reg_alloc reg;
901 void* p;
902 int top = lua_gettop(L);
903 int* perr = &Dst->last_errno;
904
905 ct_usr = lua_absindex(L, ct_usr);
906
907 memset(&reg, 0, sizeof(reg));
908 reg.off = 32 + REGISTER_STACK_SPACE(ct);
909
910 dasm_setup(Dst, build_actionlist);
911
912 orig_top = lua_gettop(L);
913 p = push_cdata(L, ct_usr, ct);
914 *(cfunction*) p = func;
915
916 nargs = lua_rawlen(L, ct_usr);
917
918 if (ct->calling_convention != C_CALL && ct->has_var_arg) {
919 luaL_error(L, "vararg is only allowed with the c calling convention");
920 }
921
922 //| push rbp
923 //| mov rbp, rsp
924 //| push L_ARG
925 //| push TOP
926 //| // stack is 0 (mod 16) (TOP, L_ARG, rbp, rip)
927 //|
928 //| // Get L from our arguments and allocate some stack for lua_gettop
929 //| mov L_ARG, rdi
930 //|
931 //| call_r extern lua_gettop, L_ARG
932 //| mov TOP, rax // no need for movzxd rax, eax - high word guarenteed to be zero by x86-64
933 //| cmp rax, nargs
934 dasm_put(Dst, 904, nargs);
935 if (!ct->has_var_arg) {
936 //| jge >2
937 //| too_few_arguments
938 //| 2:
939 //| jle >1
940 //| too_many_arguments
941 dasm_put(Dst, 932, (unsigned int)((uintptr_t)(&"too few arguments")), (unsigned int)(((uintptr_t)(&"too few arguments"))>>32), (unsigned int)((uintptr_t)(&"too many arguments")), (unsigned int)(((uintptr_t)(&"too many arguments"))>>32));
942 } else {
943 //| jge >1
944 //| too_few_arguments
945 dasm_put(Dst, 973, (unsigned int)((uintptr_t)(&"too few arguments")), (unsigned int)(((uintptr_t)(&"too few arguments"))>>32));
946 }
947
948 //| 1:
949 dasm_put(Dst, 993);
950
951 /* no need to zero extend eax returned by lua_gettop to rax as x86-64
952 * preguarentees that the upper 32 bits will be zero */
953 //| shl rax, 4 // reserve 16 bytes per argument - this maintains the alignment mod 16
954 //| sub rsp, rax
955 //| sub rsp, 32 + REGISTER_STACK_SPACE(ct) // reserve an extra 32 to call local functions
956 dasm_put(Dst, 996, 32 + REGISTER_STACK_SPACE(ct));
957
958 for (i = 1; i <= nargs; i++) {
959 lua_rawgeti(L, ct_usr, (int) i);
960 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
961
962 if (mbr_ct->pointers || mbr_ct->is_reference) {
963 lua_getuservalue(L, -1);
964 num_upvals = lua_gettop(L) - orig_top;
965 //| call_rrrp extern check_typed_pointer, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
966 dasm_put(Dst, 1009, (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals), i);
967 add_pointer(Dst, ct, &reg);
968 } else {
969 switch (mbr_ct->type) {
970 case FUNCTION_PTR_TYPE:
971 lua_getuservalue(L, -1);
972 num_upvals = lua_gettop(L) - orig_top;
973 //| call_rrrp extern check_typed_cfunction, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
974 dasm_put(Dst, 1029, (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals), i);
975 add_pointer(Dst, ct, &reg);
976 break;
977
978 case ENUM_TYPE:
979 lua_getuservalue(L, -1);
980 num_upvals = lua_gettop(L) - orig_top;
981 //| call_rrrp, extern check_enum, L_ARG, i, lua_upvalueindex(num_upvals), mbr_ct
982 dasm_put(Dst, 1049, (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals), i);
983 add_int(Dst, ct, &reg, 0);
984 break;
985
986 case INT8_TYPE:
987 //| call_rr extern check_int32, L_ARG, i
988 dasm_put(Dst, 1069, i);
989 if (mbr_ct->is_unsigned) {
990 //| movzx eax, al
991 dasm_put(Dst, 1081);
992 } else {
993 //| movsx eax, al
994 dasm_put(Dst, 1085);
995 }
996 add_int(Dst, ct, &reg, 0);
997 lua_pop(L, 1);
998 break;
999
1000 case INT16_TYPE:
1001 //| call_rr extern check_int32, L_ARG, i
1002 dasm_put(Dst, 1069, i);
1003 if (mbr_ct->is_unsigned) {
1004 //| movzx eax, ax
1005 dasm_put(Dst, 1089);
1006 } else {
1007 //| movsx eax, ax
1008 dasm_put(Dst, 1093);
1009 }
1010 add_int(Dst, ct, &reg, 0);
1011 lua_pop(L, 1);
1012 break;
1013
1014 case BOOL_TYPE:
1015 //| call_rr extern check_int32, L_ARG, i
1016 //| cmp eax, 0
1017 //| setne al
1018 //| movzx eax, al
1019 dasm_put(Dst, 1097, i);
1020 add_int(Dst, ct, &reg, 0);
1021 lua_pop(L, 1);
1022 break;
1023
1024 case INT32_TYPE:
1025 if (mbr_ct->is_unsigned) {
1026 //| call_rr extern check_uint32, L_ARG, i
1027 dasm_put(Dst, 1119, i);
1028 } else {
1029 //| call_rr extern check_int32, L_ARG, i
1030 dasm_put(Dst, 1069, i);
1031 }
1032 add_int(Dst, ct, &reg, 0);
1033 lua_pop(L, 1);
1034 break;
1035
1036 case INTPTR_TYPE:
1037 //| call_rr extern check_uintptr, L_ARG, i
1038 dasm_put(Dst, 1131, i);
1039 add_pointer(Dst, ct, &reg);
1040 lua_pop(L, 1);
1041 break;
1042
1043 case INT64_TYPE:
1044 if (mbr_ct->is_unsigned) {
1045 //| call_rr extern check_uint64, L_ARG, i
1046 dasm_put(Dst, 1143, i);
1047 } else {
1048 //| call_rr extern check_int64, L_ARG, i
1049 dasm_put(Dst, 1155, i);
1050 }
1051 add_int(Dst, ct, &reg, 1);
1052 lua_pop(L, 1);
1053 break;
1054
1055 case DOUBLE_TYPE:
1056 //| call_rr extern check_double, L_ARG, i
1057 dasm_put(Dst, 1167, i);
1058 add_float(Dst, ct, &reg, 1);
1059 lua_pop(L, 1);
1060 break;
1061
1062 case COMPLEX_DOUBLE_TYPE:
1063 /* on 64 bit, returned complex doubles use xmm0, xmm1, on 32 bit
1064 * there is a hidden first parameter that points to 16 bytes where
1065 * the returned arg is stored (this is popped by the called
1066 * function) */
1067 #if defined _WIN64 || defined __amd64__
1068 //| call_rr extern check_complex_double, L_ARG, i
1069 dasm_put(Dst, 1179, i);
1070 add_float(Dst, ct, &reg, 1);
1071 //| movq xmm0, xmm1
1072 dasm_put(Dst, 1191);
1073 add_float(Dst, ct, &reg, 1);
1074 #else
1075 //| lea rax, [rsp+reg.off]
1076 //| sub rsp, 4
1077 //| call_rrr extern check_complex_double, rax, L_ARG, i
1078 dasm_put(Dst, 1197, reg.off, i);
1079 reg.off += 16;
1080 #endif
1081 lua_pop(L, 1);
1082 break;
1083
1084 case FLOAT_TYPE:
1085 //| call_rr extern check_double, L_ARG, i
1086 dasm_put(Dst, 1167, i);
1087 add_float(Dst, ct, &reg, 0);
1088 lua_pop(L, 1);
1089 break;
1090
1091 case COMPLEX_FLOAT_TYPE:
1092 #if defined _WIN64 || defined __amd64__
1093 //| call_rr extern check_complex_float, L_ARG, i
1094 dasm_put(Dst, 1223, i);
1095 /* complex floats are two floats packed into a double */
1096 add_float(Dst, ct, &reg, 1);
1097 #else
1098 /* returned complex floats use eax and edx */
1099 //| call_rr extern check_complex_float, L_ARG, i
1100 //| mov [rsp], eax
1101 //| fld dword [rsp]
1102 dasm_put(Dst, 1235, i);
1103 add_float(Dst, ct, &reg, 0);
1104 //| mov [rsp], edx
1105 //| fld dword [rsp]
1106 dasm_put(Dst, 1253);
1107 add_float(Dst, ct, &reg, 0);
1108 #endif
1109 lua_pop(L, 1);
1110 break;
1111
1112 default:
1113 luaL_error(L, "NYI: call arg type");
1114 }
1115 }
1116 }
1117
1118 if (ct->has_var_arg) {
1119 if (reg.floats < MAX_FLOAT_REGISTERS(ct)) {
1120 //| mov rax, rsp
1121 //| add rax, 32 + 8*(MAX_INT_REGISTERS(ct) + reg.floats)
1122 //| call_rrrrr extern unpack_varargs_float, L_ARG, nargs+1, TOP, MAX_FLOAT_REGISTERS(ct) - reg.floats, rax
1123 dasm_put(Dst, 1260, 32 + 8*(MAX_INT_REGISTERS(ct) + reg.floats), MAX_FLOAT_REGISTERS(ct) - reg.floats, nargs+1);
1124 }
1125
1126 if (reg.ints < MAX_INT_REGISTERS(ct)) {
1127 //| mov rax, rsp
1128 //| add rax, 32 + 8*(reg.ints)
1129 //| call_rrrrr extern unpack_varargs_int, L_ARG, nargs+1, TOP, MAX_INT_REGISTERS(ct) - reg.ints, rax
1130 dasm_put(Dst, 1290, 32 + 8*(reg.ints), MAX_INT_REGISTERS(ct) - reg.ints, nargs+1);
1131 }
1132
1133 //| mov rax, rsp
1134 //| add rax, reg.off
1135 //| 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
1136 dasm_put(Dst, 1320, reg.off, MAX_FLOAT_REGISTERS(ct) - reg.floats, MAX_INT_REGISTERS(ct) - reg.ints, nargs+1);
1137
1138 reg.floats = MAX_FLOAT_REGISTERS(ct);
1139 reg.ints = MAX_INT_REGISTERS(ct);
1140 }
1141
1142 //| mov64 rcx, perr
1143 //| mov eax, dword [rcx]
1144 //| call_r extern SetLastError, rax
1145 dasm_put(Dst, 1354, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1146
1147 /* remove the stack space to call local functions */
1148 //| add rsp, 32
1149 dasm_put(Dst, 1368);
1150
1151 switch (reg.floats) {
1152 case 8:
1153 //| movq xmm7, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+7)]
1154 dasm_put(Dst, 1373, 8*(MAX_INT_REGISTERS(ct)+7));
1155 case 7:
1156 //| movq xmm6, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+6)]
1157 dasm_put(Dst, 1382, 8*(MAX_INT_REGISTERS(ct)+6));
1158 case 6:
1159 //| movq xmm5, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+5)]
1160 dasm_put(Dst, 1391, 8*(MAX_INT_REGISTERS(ct)+5));
1161 case 5:
1162 //| movq xmm4, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+4)]
1163 dasm_put(Dst, 1400, 8*(MAX_INT_REGISTERS(ct)+4));
1164 case 4:
1165 //| movq xmm3, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+3)]
1166 dasm_put(Dst, 1409, 8*(MAX_INT_REGISTERS(ct)+3));
1167 case 3:
1168 //| movq xmm2, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+2)]
1169 dasm_put(Dst, 1418, 8*(MAX_INT_REGISTERS(ct)+2));
1170 case 2:
1171 //| movq xmm1, qword [rsp + 8*(MAX_INT_REGISTERS(ct)+1)]
1172 dasm_put(Dst, 1427, 8*(MAX_INT_REGISTERS(ct)+1));
1173 case 1:
1174 //| movq xmm0, qword [rsp + 8*(MAX_INT_REGISTERS(ct))]
1175 dasm_put(Dst, 1436, 8*(MAX_INT_REGISTERS(ct)));
1176 case 0:
1177 break;
1178 }
1179
1180 switch (reg.ints) {
1181 case 6:
1182 //| mov r9, [rsp + 8*5]
1183 dasm_put(Dst, 1445, 8*5);
1184 case 5:
1185 //| mov r8, [rsp + 8*4]
1186 dasm_put(Dst, 1452, 8*4);
1187 case 4:
1188 //| mov rcx, [rsp + 8*3]
1189 dasm_put(Dst, 1459, 8*3);
1190 case 3:
1191 //| mov rdx, [rsp + 8*2]
1192 dasm_put(Dst, 1466, 8*2);
1193 case 2:
1194 //| mov rsi, [rsp + 8*1]
1195 dasm_put(Dst, 1473, 8*1);
1196 case 1:
1197 //| mov rdi, [rsp]
1198 dasm_put(Dst, 1480);
1199 case 0:
1200 break;
1201 }
1202
1203 //| add rsp, REGISTER_STACK_SPACE(ct)
1204 dasm_put(Dst, 1485, REGISTER_STACK_SPACE(ct));
1205
1206 if (ct->has_var_arg) {
1207 /* al stores an upper limit on the number of float register, note that
1208 * its allowed to be more than the actual number of float registers used as
1209 * long as its 0-8 */
1210 //| mov al, 8
1211 dasm_put(Dst, 1490);
1212 }
1213
1214 //| call extern FUNCTION
1215 //| sub rsp, 48 // 32 to be able to call local functions, 16 so we can store some local variables
1216 dasm_put(Dst, 1493);
1217
1218 /* note on windows X86 the stack may be only aligned to 4 (stdcall will
1219 * have popped a multiple of 4 bytes), but we don't need 16 byte alignment on
1220 * that platform
1221 */
1222
1223 lua_rawgeti(L, ct_usr, 0);
1224 mbr_ct = (const struct ctype*) lua_touserdata(L, -1);
1225
1226 if (mbr_ct->pointers || mbr_ct->is_reference || mbr_ct->type == INTPTR_TYPE) {
1227 lua_getuservalue(L, -1);
1228 num_upvals = lua_gettop(L) - orig_top;
1229 //| mov [rsp+32], rax // save the pointer
1230 //| get_errno
1231 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1232 //| mov rcx, [rsp+32]
1233 //| mov [rax], rcx // *(void**) cdata = val
1234 //| lua_return_arg
1235 dasm_put(Dst, 1503, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals));
1236
1237 } else {
1238 switch (mbr_ct->type) {
1239 case FUNCTION_PTR_TYPE:
1240 lua_getuservalue(L, -1);
1241 num_upvals = lua_gettop(L) - orig_top;
1242 //| mov [rsp+32], rax // save the function pointer
1243 //| get_errno
1244 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1245 //| mov rcx, [rsp+32]
1246 //| mov [rax], rcx // *(cfunction**) cdata = val
1247 //| lua_return_arg
1248 dasm_put(Dst, 1503, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals));
1249 break;
1250
1251 case INT64_TYPE:
1252 lua_pop(L, 1);
1253 if (mbr_ct->is_unsigned) {
1254 //| lua_return_ulong
1255 dasm_put(Dst, 1563, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1256 } else {
1257 //| lua_return_long
1258 dasm_put(Dst, 1563, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1259 }
1260 break;
1261
1262 case COMPLEX_FLOAT_TYPE:
1263 lua_getuservalue(L, -1);
1264 num_upvals = lua_gettop(L) - orig_top;
1265 //| // complex floats are returned as two floats packed into xmm0
1266 //| movq qword [rsp+32], xmm0
1267 //|
1268 //| get_errno
1269 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1270 //|
1271 //| // ((complex_float*) cdata) = val
1272 //| mov rcx, [rsp+32]
1273 //| mov [rax], rcx
1274 //|
1275 //| lua_return_arg
1276 dasm_put(Dst, 1615, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals));
1277 break;
1278
1279 case COMPLEX_DOUBLE_TYPE:
1280 lua_getuservalue(L, -1);
1281 num_upvals = lua_gettop(L) - orig_top;
1282 //| // complex doubles are returned as xmm0 and xmm1
1283 //| movq qword [rsp+40], xmm1
1284 //| movq qword [rsp+32], xmm0
1285 //|
1286 //| get_errno
1287 //| call_rrp extern push_cdata, L_ARG, lua_upvalueindex(num_upvals), mbr_ct
1288 //|
1289 //| // ((complex_double*) cdata)->real = val0
1290 //| // ((complex_double*) cdata)->imag = val1
1291 //| mov rcx, [rsp+40]
1292 //| mov [rax+8], rcx
1293 //| mov rcx, [rsp+32]
1294 //| mov [rax], rcx
1295 //|
1296 //|
1297 //| lua_return_arg
1298 dasm_put(Dst, 1676, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32), (unsigned int)((uintptr_t)(mbr_ct)), (unsigned int)(((uintptr_t)(mbr_ct))>>32), lua_upvalueindex(num_upvals));
1299 break;
1300
1301 case VOID_TYPE:
1302 lua_pop(L, 1);
1303 //| lua_return_void
1304 dasm_put(Dst, 1752, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1305 break;
1306
1307 case BOOL_TYPE:
1308 lua_pop(L, 1);
1309 //| lua_return_bool
1310 dasm_put(Dst, 1784, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1311 break;
1312
1313 case INT8_TYPE:
1314 lua_pop(L, 1);
1315 if (mbr_ct->is_unsigned) {
1316 //| movzx eax, al
1317 dasm_put(Dst, 1081);
1318 } else {
1319 //| movsx eax, al
1320 dasm_put(Dst, 1085);
1321 }
1322 //| lua_return_int
1323 dasm_put(Dst, 1837, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1324 break;
1325
1326 case INT16_TYPE:
1327 lua_pop(L, 1);
1328 if (mbr_ct->is_unsigned) {
1329 //| movzx eax, ax
1330 dasm_put(Dst, 1089);
1331 } else {
1332 //| movsx eax, ax
1333 dasm_put(Dst, 1093);
1334 }
1335 //| lua_return_int
1336 dasm_put(Dst, 1837, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1337 break;
1338
1339 case INT32_TYPE:
1340 case ENUM_TYPE:
1341 lua_pop(L, 1);
1342 if (mbr_ct->is_unsigned) {
1343 //| lua_return_uint
1344 dasm_put(Dst, 1887, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1345 } else {
1346 //| lua_return_int
1347 dasm_put(Dst, 1837, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1348 }
1349 break;
1350
1351 case FLOAT_TYPE:
1352 lua_pop(L, 1);
1353 //| cvtss2sd xmm0, xmm0
1354 //| lua_return_double
1355 dasm_put(Dst, 1937, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1356 break;
1357
1358 case DOUBLE_TYPE:
1359 lua_pop(L, 1);
1360 //| lua_return_double
1361 dasm_put(Dst, 1942, (unsigned int)((uintptr_t)(perr)), (unsigned int)(((uintptr_t)(perr))>>32));
1362 break;
1363
1364 default:
1365 luaL_error(L, "NYI: call return type");
1366 }
1367 }
1368
1369 {
1370 cfunction f = compile(Dst, L, func, LUA_NOREF);
1371 /* add a callback as an upval so that the jitted code gets cleaned up when
1372 * the function gets gc'd */
1373 push_callback(L, f, func);
1374 num_upvals = lua_gettop(L) - orig_top;
1375 lua_pushcclosure(L, (lua_CFunction) f, num_upvals);
1376 }
1377 }
1378