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