ljx

FORK: LuaJIT with native 5.2 and 5.3 support
git clone https://git.neptards.moe/neptards/ljx.git
Log | Files | Refs | README

lj_ccall.c (35211B)


      1 /*
      2 ** FFI C call handling.
      3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
      4 */
      5 
      6 #include "lj_obj.h"
      7 
      8 #if LJ_HASFFI
      9 
     10 #include "lj_gc.h"
     11 #include "lj_err.h"
     12 #include "lj_tab.h"
     13 #include "lj_ctype.h"
     14 #include "lj_cconv.h"
     15 #include "lj_cdata.h"
     16 #include "lj_ccall.h"
     17 #include "lj_trace.h"
     18 
     19 /* Target-specific handling of register arguments. */
     20 #if LJ_TARGET_X86
     21 /* -- x86 calling conventions --------------------------------------------- */
     22 
     23 #if LJ_ABI_WIN
     24 
     25 #define CCALL_HANDLE_STRUCTRET \
     26   /* Return structs bigger than 8 by reference (on stack only). */ \
     27   cc->retref = (sz > 8); \
     28   if (cc->retref) cc->stack[nsp++] = (GPRArg)dp;
     29 
     30 #define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET
     31 
     32 #else
     33 
     34 #if LJ_TARGET_OSX
     35 
     36 #define CCALL_HANDLE_STRUCTRET \
     37   /* Return structs of size 1, 2, 4 or 8 in registers. */ \
     38   cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \
     39   if (cc->retref) { \
     40     if (ngpr < maxgpr) \
     41       cc->gpr[ngpr++] = (GPRArg)dp; \
     42     else \
     43       cc->stack[nsp++] = (GPRArg)dp; \
     44   } else {  /* Struct with single FP field ends up in FPR. */ \
     45     cc->resx87 = ccall_classify_struct(cts, ctr); \
     46   }
     47 
     48 #define CCALL_HANDLE_STRUCTRET2 \
     49   if (cc->resx87) sp = (uint8_t *)&cc->fpr[0]; \
     50   memcpy(dp, sp, ctr->size);
     51 
     52 #else
     53 
     54 #define CCALL_HANDLE_STRUCTRET \
     55   cc->retref = 1;  /* Return all structs by reference (in reg or on stack). */ \
     56   if (ngpr < maxgpr) \
     57     cc->gpr[ngpr++] = (GPRArg)dp; \
     58   else \
     59     cc->stack[nsp++] = (GPRArg)dp;
     60 
     61 #endif
     62 
     63 #define CCALL_HANDLE_COMPLEXRET \
     64   /* Return complex float in GPRs and complex double by reference. */ \
     65   cc->retref = (sz > 8); \
     66   if (cc->retref) { \
     67     if (ngpr < maxgpr) \
     68       cc->gpr[ngpr++] = (GPRArg)dp; \
     69     else \
     70       cc->stack[nsp++] = (GPRArg)dp; \
     71   }
     72 
     73 #endif
     74 
     75 #define CCALL_HANDLE_COMPLEXRET2 \
     76   if (!cc->retref) \
     77     *(int64_t *)dp = *(int64_t *)sp;  /* Copy complex float from GPRs. */
     78 
     79 #define CCALL_HANDLE_STRUCTARG \
     80   ngpr = maxgpr;  /* Pass all structs by value on the stack. */
     81 
     82 #define CCALL_HANDLE_COMPLEXARG \
     83   isfp = 1;  /* Pass complex by value on stack. */
     84 
     85 #define CCALL_HANDLE_REGARG \
     86   if (!isfp) {  /* Only non-FP values may be passed in registers. */ \
     87     if (n > 1) {  /* Anything > 32 bit is passed on the stack. */ \
     88       if (!LJ_ABI_WIN) ngpr = maxgpr;  /* Prevent reordering. */ \
     89     } else if (ngpr + 1 <= maxgpr) { \
     90       dp = &cc->gpr[ngpr]; \
     91       ngpr += n; \
     92       goto done; \
     93     } \
     94   }
     95 
     96 #elif LJ_TARGET_X64 && LJ_ABI_WIN
     97 /* -- Windows/x64 calling conventions ------------------------------------- */
     98 
     99 #define CCALL_HANDLE_STRUCTRET \
    100   /* Return structs of size 1, 2, 4 or 8 in a GPR. */ \
    101   cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \
    102   if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
    103 
    104 #define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET
    105 
    106 #define CCALL_HANDLE_COMPLEXRET2 \
    107   if (!cc->retref) \
    108     *(int64_t *)dp = *(int64_t *)sp;  /* Copy complex float from GPRs. */
    109 
    110 #define CCALL_HANDLE_STRUCTARG \
    111   /* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */ \
    112   if (!(sz == 1 || sz == 2 || sz == 4 || sz == 8)) { \
    113     rp = cdataptr(lj_cdata_new(cts, did, sz)); \
    114     sz = CTSIZE_PTR;  /* Pass all other structs by reference. */ \
    115   }
    116 
    117 #define CCALL_HANDLE_COMPLEXARG \
    118   /* Pass complex float in a GPR and complex double by reference. */ \
    119   if (sz != 2*sizeof(float)) { \
    120     rp = cdataptr(lj_cdata_new(cts, did, sz)); \
    121     sz = CTSIZE_PTR; \
    122   }
    123 
    124 /* Windows/x64 argument registers are strictly positional (use ngpr). */
    125 #define CCALL_HANDLE_REGARG \
    126   if (isfp) { \
    127     if (ngpr < maxgpr) { dp = &cc->fpr[ngpr++]; nfpr = ngpr; goto done; } \
    128   } else { \
    129     if (ngpr < maxgpr) { dp = &cc->gpr[ngpr++]; goto done; } \
    130   }
    131 
    132 #elif LJ_TARGET_X64
    133 /* -- POSIX/x64 calling conventions --------------------------------------- */
    134 
    135 #define CCALL_HANDLE_STRUCTRET \
    136   int rcl[2]; rcl[0] = rcl[1] = 0; \
    137   if (ccall_classify_struct(cts, ctr, rcl, 0)) { \
    138     cc->retref = 1;  /* Return struct by reference. */ \
    139     cc->gpr[ngpr++] = (GPRArg)dp; \
    140   } else { \
    141     cc->retref = 0;  /* Return small structs in registers. */ \
    142   }
    143 
    144 #define CCALL_HANDLE_STRUCTRET2 \
    145   int rcl[2]; rcl[0] = rcl[1] = 0; \
    146   ccall_classify_struct(cts, ctr, rcl, 0); \
    147   ccall_struct_ret(cc, rcl, dp, ctr->size);
    148 
    149 #define CCALL_HANDLE_COMPLEXRET \
    150   /* Complex values are returned in one or two FPRs. */ \
    151   cc->retref = 0;
    152 
    153 #define CCALL_HANDLE_COMPLEXRET2 \
    154   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPR. */ \
    155     *(int64_t *)dp = cc->fpr[0].l[0]; \
    156   } else {  /* Copy non-contiguous complex double from FPRs. */ \
    157     ((int64_t *)dp)[0] = cc->fpr[0].l[0]; \
    158     ((int64_t *)dp)[1] = cc->fpr[1].l[0]; \
    159   }
    160 
    161 #define CCALL_HANDLE_STRUCTARG \
    162   int rcl[2]; rcl[0] = rcl[1] = 0; \
    163   if (!ccall_classify_struct(cts, d, rcl, 0)) { \
    164     cc->nsp = nsp; cc->ngpr = ngpr; cc->nfpr = nfpr; \
    165     if (ccall_struct_arg(cc, cts, d, rcl, o, narg)) goto err_nyi; \
    166     nsp = cc->nsp; ngpr = cc->ngpr; nfpr = cc->nfpr; \
    167     continue; \
    168   }  /* Pass all other structs by value on stack. */
    169 
    170 #define CCALL_HANDLE_COMPLEXARG \
    171   isfp = 2;  /* Pass complex in FPRs or on stack. Needs postprocessing. */
    172 
    173 #define CCALL_HANDLE_REGARG \
    174   if (isfp) {  /* Try to pass argument in FPRs. */ \
    175     int n2 = ctype_isvector(d->info) ? 1 : n; \
    176     if (nfpr + n2 <= CCALL_NARG_FPR) { \
    177       dp = &cc->fpr[nfpr]; \
    178       nfpr += n2; \
    179       goto done; \
    180     } \
    181   } else {  /* Try to pass argument in GPRs. */ \
    182     /* Note that reordering is explicitly allowed in the x64 ABI. */ \
    183     if (n <= 2 && ngpr + n <= maxgpr) { \
    184       dp = &cc->gpr[ngpr]; \
    185       ngpr += n; \
    186       goto done; \
    187     } \
    188   }
    189 
    190 #elif LJ_TARGET_ARM
    191 /* -- ARM calling conventions --------------------------------------------- */
    192 
    193 #if LJ_ABI_SOFTFP
    194 
    195 #define CCALL_HANDLE_STRUCTRET \
    196   /* Return structs of size <= 4 in a GPR. */ \
    197   cc->retref = !(sz <= 4); \
    198   if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
    199 
    200 #define CCALL_HANDLE_COMPLEXRET \
    201   cc->retref = 1;  /* Return all complex values by reference. */ \
    202   cc->gpr[ngpr++] = (GPRArg)dp;
    203 
    204 #define CCALL_HANDLE_COMPLEXRET2 \
    205   UNUSED(dp); /* Nothing to do. */
    206 
    207 #define CCALL_HANDLE_STRUCTARG \
    208   /* Pass all structs by value in registers and/or on the stack. */
    209 
    210 #define CCALL_HANDLE_COMPLEXARG \
    211   /* Pass complex by value in 2 or 4 GPRs. */
    212 
    213 #define CCALL_HANDLE_REGARG_FP1
    214 #define CCALL_HANDLE_REGARG_FP2
    215 
    216 #else
    217 
    218 #define CCALL_HANDLE_STRUCTRET \
    219   cc->retref = !ccall_classify_struct(cts, ctr, ct); \
    220   if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
    221 
    222 #define CCALL_HANDLE_STRUCTRET2 \
    223   if (ccall_classify_struct(cts, ctr, ct) > 1) sp = (uint8_t *)&cc->fpr[0]; \
    224   memcpy(dp, sp, ctr->size);
    225 
    226 #define CCALL_HANDLE_COMPLEXRET \
    227   if (!(ct->info & CTF_VARARG)) cc->retref = 0;  /* Return complex in FPRs. */
    228 
    229 #define CCALL_HANDLE_COMPLEXRET2 \
    230   if (!(ct->info & CTF_VARARG)) memcpy(dp, &cc->fpr[0], ctr->size);
    231 
    232 #define CCALL_HANDLE_STRUCTARG \
    233   isfp = (ccall_classify_struct(cts, d, ct) > 1);
    234   /* Pass all structs by value in registers and/or on the stack. */
    235 
    236 #define CCALL_HANDLE_COMPLEXARG \
    237   isfp = 1;  /* Pass complex by value in FPRs or on stack. */
    238 
    239 #define CCALL_HANDLE_REGARG_FP1 \
    240   if (isfp && !(ct->info & CTF_VARARG)) { \
    241     if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \
    242       if (nfpr + (n >> 1) <= CCALL_NARG_FPR) { \
    243 	dp = &cc->fpr[nfpr]; \
    244 	nfpr += (n >> 1); \
    245 	goto done; \
    246       } \
    247     } else { \
    248       if (sz > 1 && fprodd != nfpr) fprodd = 0; \
    249       if (fprodd) { \
    250 	if (2*nfpr+n <= 2*CCALL_NARG_FPR+1) { \
    251 	  dp = (void *)&cc->fpr[fprodd-1].f[1]; \
    252 	  nfpr += (n >> 1); \
    253 	  if ((n & 1)) fprodd = 0; else fprodd = nfpr-1; \
    254 	  goto done; \
    255 	} \
    256       } else { \
    257 	if (2*nfpr+n <= 2*CCALL_NARG_FPR) { \
    258 	  dp = (void *)&cc->fpr[nfpr]; \
    259 	  nfpr += (n >> 1); \
    260 	  if ((n & 1)) fprodd = ++nfpr; else fprodd = 0; \
    261 	  goto done; \
    262 	} \
    263       } \
    264     } \
    265     fprodd = 0;  /* No reordering after the first FP value is on stack. */ \
    266   } else {
    267 
    268 #define CCALL_HANDLE_REGARG_FP2	}
    269 
    270 #endif
    271 
    272 #define CCALL_HANDLE_REGARG \
    273   CCALL_HANDLE_REGARG_FP1 \
    274   if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \
    275     if (ngpr < maxgpr) \
    276       ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \
    277   } \
    278   if (ngpr < maxgpr) { \
    279     dp = &cc->gpr[ngpr]; \
    280     if (ngpr + n > maxgpr) { \
    281       nsp += ngpr + n - maxgpr;  /* Assumes contiguous gpr/stack fields. */ \
    282       if (nsp > CCALL_MAXSTACK) goto err_nyi;  /* Too many arguments. */ \
    283       ngpr = maxgpr; \
    284     } else { \
    285       ngpr += n; \
    286     } \
    287     goto done; \
    288   } CCALL_HANDLE_REGARG_FP2
    289 
    290 #define CCALL_HANDLE_RET \
    291   if ((ct->info & CTF_VARARG)) sp = (uint8_t *)&cc->gpr[0];
    292 
    293 #elif LJ_TARGET_ARM64
    294 /* -- ARM64 calling conventions ------------------------------------------- */
    295 
    296 #define CCALL_HANDLE_STRUCTRET \
    297   cc->retref = !ccall_classify_struct(cts, ctr); \
    298   if (cc->retref) cc->retp = dp;
    299 
    300 #define CCALL_HANDLE_STRUCTRET2 \
    301   unsigned int cl = ccall_classify_struct(cts, ctr); \
    302   if ((cl & 4)) { /* Combine float HFA from separate registers. */ \
    303     CTSize i = (cl >> 8) - 1; \
    304     do { ((uint32_t *)dp)[i] = cc->fpr[i].u32; } while (i--); \
    305   } else { \
    306     if (cl > 1) sp = (uint8_t *)&cc->fpr[0]; \
    307     memcpy(dp, sp, ctr->size); \
    308   }
    309 
    310 #define CCALL_HANDLE_COMPLEXRET \
    311   /* Complex values are returned in one or two FPRs. */ \
    312   cc->retref = 0;
    313 
    314 #define CCALL_HANDLE_COMPLEXRET2 \
    315   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPRs. */ \
    316     ((float *)dp)[0] = cc->fpr[0].f; \
    317     ((float *)dp)[1] = cc->fpr[1].f; \
    318   } else {  /* Copy complex double from FPRs. */ \
    319     ((double *)dp)[0] = cc->fpr[0].d; \
    320     ((double *)dp)[1] = cc->fpr[1].d; \
    321   }
    322 
    323 #define CCALL_HANDLE_STRUCTARG \
    324   unsigned int cl = ccall_classify_struct(cts, d); \
    325   if (cl == 0) {  /* Pass struct by reference. */ \
    326     rp = cdataptr(lj_cdata_new(cts, did, sz)); \
    327     sz = CTSIZE_PTR; \
    328   } else if (cl > 1) {  /* Pass struct in FPRs or on stack. */ \
    329     isfp = (cl & 4) ? 2 : 1; \
    330   }  /* else: Pass struct in GPRs or on stack. */
    331 
    332 #define CCALL_HANDLE_COMPLEXARG \
    333   /* Pass complex by value in separate (!) FPRs or on stack. */ \
    334   isfp = ctr->size == 2*sizeof(float) ? 2 : 1;
    335 
    336 #define CCALL_HANDLE_REGARG \
    337   if (LJ_TARGET_IOS && isva) { \
    338     /* IOS: All variadic arguments are on the stack. */ \
    339   } else if (isfp) {  /* Try to pass argument in FPRs. */ \
    340     int n2 = ctype_isvector(d->info) ? 1 : n*isfp; \
    341     if (nfpr + n2 <= CCALL_NARG_FPR) { \
    342       dp = &cc->fpr[nfpr]; \
    343       nfpr += n2; \
    344       goto done; \
    345     } else { \
    346       nfpr = CCALL_NARG_FPR;  /* Prevent reordering. */ \
    347       if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \
    348     } \
    349   } else {  /* Try to pass argument in GPRs. */ \
    350     if (!LJ_TARGET_IOS && (d->info & CTF_ALIGN) > CTALIGN_PTR) \
    351       ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \
    352     if (ngpr + n <= maxgpr) { \
    353       dp = &cc->gpr[ngpr]; \
    354       ngpr += n; \
    355       goto done; \
    356     } else { \
    357       ngpr = maxgpr;  /* Prevent reordering. */ \
    358       if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \
    359     } \
    360   }
    361 
    362 #elif LJ_TARGET_PPC
    363 /* -- PPC calling conventions --------------------------------------------- */
    364 
    365 #define CCALL_HANDLE_STRUCTRET \
    366   cc->retref = 1;  /* Return all structs by reference. */ \
    367   cc->gpr[ngpr++] = (GPRArg)dp;
    368 
    369 #define CCALL_HANDLE_COMPLEXRET \
    370   /* Complex values are returned in 2 or 4 GPRs. */ \
    371   cc->retref = 0;
    372 
    373 #define CCALL_HANDLE_COMPLEXRET2 \
    374   memcpy(dp, sp, ctr->size);  /* Copy complex from GPRs. */
    375 
    376 #define CCALL_HANDLE_STRUCTARG \
    377   rp = cdataptr(lj_cdata_new(cts, did, sz)); \
    378   sz = CTSIZE_PTR;  /* Pass all structs by reference. */
    379 
    380 #define CCALL_HANDLE_COMPLEXARG \
    381   /* Pass complex by value in 2 or 4 GPRs. */
    382 
    383 #define CCALL_HANDLE_REGARG \
    384   if (isfp) {  /* Try to pass argument in FPRs. */ \
    385     if (nfpr + 1 <= CCALL_NARG_FPR) { \
    386       dp = &cc->fpr[nfpr]; \
    387       nfpr += 1; \
    388       d = ctype_get(cts, CTID_DOUBLE);  /* FPRs always hold doubles. */ \
    389       goto done; \
    390     } \
    391   } else {  /* Try to pass argument in GPRs. */ \
    392     if (n > 1) { \
    393       lua_assert(n == 2 || n == 4);  /* int64_t or complex (float). */ \
    394       if (ctype_isinteger(d->info)) \
    395 	ngpr = (ngpr + 1u) & ~1u;  /* Align int64_t to regpair. */ \
    396       else if (ngpr + n > maxgpr) \
    397 	ngpr = maxgpr;  /* Prevent reordering. */ \
    398     } \
    399     if (ngpr + n <= maxgpr) { \
    400       dp = &cc->gpr[ngpr]; \
    401       ngpr += n; \
    402       goto done; \
    403     } \
    404   }
    405 
    406 #define CCALL_HANDLE_RET \
    407   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
    408     ctr = ctype_get(cts, CTID_DOUBLE);  /* FPRs always hold doubles. */
    409 
    410 #elif LJ_TARGET_MIPS32
    411 /* -- MIPS o32 calling conventions ---------------------------------------- */
    412 
    413 #define CCALL_HANDLE_STRUCTRET \
    414   cc->retref = 1;  /* Return all structs by reference. */ \
    415   cc->gpr[ngpr++] = (GPRArg)dp;
    416 
    417 #define CCALL_HANDLE_COMPLEXRET \
    418   /* Complex values are returned in 1 or 2 FPRs. */ \
    419   cc->retref = 0;
    420 
    421 #if LJ_ABI_SOFTFP
    422 #define CCALL_HANDLE_COMPLEXRET2 \
    423   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from GPRs. */ \
    424     ((intptr_t *)dp)[0] = cc->gpr[0]; \
    425     ((intptr_t *)dp)[1] = cc->gpr[1]; \
    426   } else {  /* Copy complex double from GPRs. */ \
    427     ((intptr_t *)dp)[0] = cc->gpr[0]; \
    428     ((intptr_t *)dp)[1] = cc->gpr[1]; \
    429     ((intptr_t *)dp)[2] = cc->gpr[2]; \
    430     ((intptr_t *)dp)[3] = cc->gpr[3]; \
    431   }
    432 #else
    433 #define CCALL_HANDLE_COMPLEXRET2 \
    434   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPRs. */ \
    435     ((float *)dp)[0] = cc->fpr[0].f; \
    436     ((float *)dp)[1] = cc->fpr[1].f; \
    437   } else {  /* Copy complex double from FPRs. */ \
    438     ((double *)dp)[0] = cc->fpr[0].d; \
    439     ((double *)dp)[1] = cc->fpr[1].d; \
    440   }
    441 #endif
    442 
    443 #define CCALL_HANDLE_STRUCTARG \
    444   /* Pass all structs by value in registers and/or on the stack. */
    445 
    446 #define CCALL_HANDLE_COMPLEXARG \
    447   /* Pass complex by value in 2 or 4 GPRs. */
    448 
    449 #define CCALL_HANDLE_GPR \
    450   if ((d->info & CTF_ALIGN) > CTALIGN_PTR) \
    451     ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \
    452   if (ngpr < maxgpr) { \
    453     dp = &cc->gpr[ngpr]; \
    454     if (ngpr + n > maxgpr) { \
    455      nsp += ngpr + n - maxgpr;  /* Assumes contiguous gpr/stack fields. */ \
    456      if (nsp > CCALL_MAXSTACK) goto err_nyi;  /* Too many arguments. */ \
    457      ngpr = maxgpr; \
    458     } else { \
    459      ngpr += n; \
    460     } \
    461     goto done; \
    462   }
    463 
    464 #if !LJ_ABI_SOFTFP	/* MIPS32 hard-float */
    465 #define CCALL_HANDLE_REGARG \
    466   if (isfp && nfpr < CCALL_NARG_FPR && !(ct->info & CTF_VARARG)) { \
    467     /* Try to pass argument in FPRs. */ \
    468     dp = n == 1 ? (void *)&cc->fpr[nfpr].f : (void *)&cc->fpr[nfpr].d; \
    469     nfpr++; ngpr += n; \
    470     goto done; \
    471   } else {  /* Try to pass argument in GPRs. */ \
    472     nfpr = CCALL_NARG_FPR; \
    473     CCALL_HANDLE_GPR \
    474   }
    475 #else			/* MIPS32 soft-float */
    476 #define CCALL_HANDLE_REGARG CCALL_HANDLE_GPR
    477 #endif
    478 
    479 #if !LJ_ABI_SOFTFP
    480 /* On MIPS64 soft-float, position of float return values is endian-dependant. */
    481 #define CCALL_HANDLE_RET \
    482   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
    483     sp = (uint8_t *)&cc->fpr[0].f;
    484 #endif
    485 
    486 #elif LJ_TARGET_MIPS64
    487 /* -- MIPS n64 calling conventions ---------------------------------------- */
    488 
    489 #define CCALL_HANDLE_STRUCTRET \
    490   cc->retref = !(sz <= 16); \
    491   if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
    492 
    493 #define CCALL_HANDLE_STRUCTRET2 \
    494   ccall_copy_struct(cc, ctr, dp, sp, ccall_classify_struct(cts, ctr, ct));
    495 
    496 #define CCALL_HANDLE_COMPLEXRET \
    497   /* Complex values are returned in 1 or 2 FPRs. */ \
    498   cc->retref = 0;
    499 
    500 #if LJ_ABI_SOFTFP	/* MIPS64 soft-float */
    501 
    502 #define CCALL_HANDLE_COMPLEXRET2 \
    503   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from GPRs. */ \
    504     ((intptr_t *)dp)[0] = cc->gpr[0]; \
    505   } else {  /* Copy complex double from GPRs. */ \
    506     ((intptr_t *)dp)[0] = cc->gpr[0]; \
    507     ((intptr_t *)dp)[1] = cc->gpr[1]; \
    508   }
    509 
    510 #define CCALL_HANDLE_COMPLEXARG \
    511   /* Pass complex by value in 2 or 4 GPRs. */
    512 
    513 /* Position of soft-float 'float' return value depends on endianess.  */
    514 #define CCALL_HANDLE_RET \
    515   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
    516     sp = (uint8_t *)cc->gpr + LJ_ENDIAN_SELECT(0, 4);
    517 
    518 #else			/* MIPS64 hard-float */
    519 
    520 #define CCALL_HANDLE_COMPLEXRET2 \
    521   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPRs. */ \
    522     ((float *)dp)[0] = cc->fpr[0].f; \
    523     ((float *)dp)[1] = cc->fpr[1].f; \
    524   } else {  /* Copy complex double from FPRs. */ \
    525     ((double *)dp)[0] = cc->fpr[0].d; \
    526     ((double *)dp)[1] = cc->fpr[1].d; \
    527   }
    528 
    529 #define CCALL_HANDLE_COMPLEXARG \
    530   if (sz == 2*sizeof(float)) { \
    531     isfp = 2; \
    532     if (ngpr < maxgpr) \
    533       sz *= 2; \
    534   }
    535 
    536 #define CCALL_HANDLE_RET \
    537   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
    538     sp = (uint8_t *)&cc->fpr[0].f;
    539 
    540 #endif
    541 
    542 #define CCALL_HANDLE_STRUCTARG \
    543   /* Pass all structs by value in registers and/or on the stack. */
    544 
    545 #define CCALL_HANDLE_REGARG \
    546   if (ngpr < maxgpr) { \
    547     dp = &cc->gpr[ngpr]; \
    548     if (ngpr + n > maxgpr) { \
    549       nsp += ngpr + n - maxgpr;  /* Assumes contiguous gpr/stack fields. */ \
    550       if (nsp > CCALL_MAXSTACK) goto err_nyi;  /* Too many arguments. */ \
    551       ngpr = maxgpr; \
    552     } else { \
    553       ngpr += n; \
    554     } \
    555     goto done; \
    556   }
    557 
    558 #else
    559 #error "Missing calling convention definitions for this architecture"
    560 #endif
    561 
    562 #ifndef CCALL_HANDLE_STRUCTRET2
    563 #define CCALL_HANDLE_STRUCTRET2 \
    564   memcpy(dp, sp, ctr->size);  /* Copy struct return value from GPRs. */
    565 #endif
    566 
    567 /* -- x86 OSX ABI struct classification ----------------------------------- */
    568 
    569 #if LJ_TARGET_X86 && LJ_TARGET_OSX
    570 
    571 /* Check for struct with single FP field. */
    572 static int ccall_classify_struct(CTState *cts, CType *ct)
    573 {
    574   CTSize sz = ct->size;
    575   if (!(sz == sizeof(float) || sz == sizeof(double))) return 0;
    576   if ((ct->info & CTF_UNION)) return 0;
    577   while (ct->sib) {
    578     ct = ctype_get(cts, ct->sib);
    579     if (ctype_isfield(ct->info)) {
    580       CType *sct = ctype_rawchild(cts, ct);
    581       if (ctype_isfp(sct->info)) {
    582 	if (sct->size == sz)
    583 	  return (sz >> 2);  /* Return 1 for float or 2 for double. */
    584       } else if (ctype_isstruct(sct->info)) {
    585 	if (sct->size)
    586 	  return ccall_classify_struct(cts, sct);
    587       } else {
    588 	break;
    589       }
    590     } else if (ctype_isbitfield(ct->info)) {
    591       break;
    592     } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
    593       CType *sct = ctype_rawchild(cts, ct);
    594       if (sct->size)
    595 	return ccall_classify_struct(cts, sct);
    596     }
    597   }
    598   return 0;
    599 }
    600 
    601 #endif
    602 
    603 /* -- x64 struct classification ------------------------------------------- */
    604 
    605 #if LJ_TARGET_X64 && !LJ_ABI_WIN
    606 
    607 /* Register classes for x64 struct classification. */
    608 #define CCALL_RCL_INT	1
    609 #define CCALL_RCL_SSE	2
    610 #define CCALL_RCL_MEM	4
    611 /* NYI: classify vectors. */
    612 
    613 static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs);
    614 
    615 /* Classify a C type. */
    616 static void ccall_classify_ct(CTState *cts, CType *ct, int *rcl, CTSize ofs)
    617 {
    618   if (ctype_isarray(ct->info)) {
    619     CType *cct = ctype_rawchild(cts, ct);
    620     CTSize eofs, esz = cct->size, asz = ct->size;
    621     for (eofs = 0; eofs < asz; eofs += esz)
    622       ccall_classify_ct(cts, cct, rcl, ofs+eofs);
    623   } else if (ctype_isstruct(ct->info)) {
    624     ccall_classify_struct(cts, ct, rcl, ofs);
    625   } else {
    626     int cl = ctype_isfp(ct->info) ? CCALL_RCL_SSE : CCALL_RCL_INT;
    627     lua_assert(ctype_hassize(ct->info));
    628     if ((ofs & (ct->size-1))) cl = CCALL_RCL_MEM;  /* Unaligned. */
    629     rcl[(ofs >= 8)] |= cl;
    630   }
    631 }
    632 
    633 /* Recursively classify a struct based on its fields. */
    634 static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs)
    635 {
    636   if (ct->size > 16) return CCALL_RCL_MEM;  /* Too big, gets memory class. */
    637   while (ct->sib) {
    638     CTSize fofs;
    639     ct = ctype_get(cts, ct->sib);
    640     fofs = ofs+ct->size;
    641     if (ctype_isfield(ct->info))
    642       ccall_classify_ct(cts, ctype_rawchild(cts, ct), rcl, fofs);
    643     else if (ctype_isbitfield(ct->info))
    644       rcl[(fofs >= 8)] |= CCALL_RCL_INT;  /* NYI: unaligned bitfields? */
    645     else if (ctype_isxattrib(ct->info, CTA_SUBTYPE))
    646       ccall_classify_struct(cts, ctype_rawchild(cts, ct), rcl, fofs);
    647   }
    648   return ((rcl[0]|rcl[1]) & CCALL_RCL_MEM);  /* Memory class? */
    649 }
    650 
    651 /* Try to split up a small struct into registers. */
    652 static int ccall_struct_reg(CCallState *cc, GPRArg *dp, int *rcl)
    653 {
    654   MSize ngpr = cc->ngpr, nfpr = cc->nfpr;
    655   uint32_t i;
    656   for (i = 0; i < 2; i++) {
    657     lua_assert(!(rcl[i] & CCALL_RCL_MEM));
    658     if ((rcl[i] & CCALL_RCL_INT)) {  /* Integer class takes precedence. */
    659       if (ngpr >= CCALL_NARG_GPR) return 1;  /* Register overflow. */
    660       cc->gpr[ngpr++] = dp[i];
    661     } else if ((rcl[i] & CCALL_RCL_SSE)) {
    662       if (nfpr >= CCALL_NARG_FPR) return 1;  /* Register overflow. */
    663       cc->fpr[nfpr++].l[0] = dp[i];
    664     }
    665   }
    666   cc->ngpr = ngpr; cc->nfpr = nfpr;
    667   return 0;  /* Ok. */
    668 }
    669 
    670 /* Pass a small struct argument. */
    671 static int ccall_struct_arg(CCallState *cc, CTState *cts, CType *d, int *rcl,
    672 			    TValue *o, int narg)
    673 {
    674   GPRArg dp[2];
    675   dp[0] = dp[1] = 0;
    676   /* Convert to temp. struct. */
    677   lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));
    678   if (ccall_struct_reg(cc, dp, rcl)) {  /* Register overflow? Pass on stack. */
    679     MSize nsp = cc->nsp, n = rcl[1] ? 2 : 1;
    680     if (nsp + n > CCALL_MAXSTACK) return 1;  /* Too many arguments. */
    681     cc->nsp = nsp + n;
    682     memcpy(&cc->stack[nsp], dp, n*CTSIZE_PTR);
    683   }
    684   return 0;  /* Ok. */
    685 }
    686 
    687 /* Combine returned small struct. */
    688 static void ccall_struct_ret(CCallState *cc, int *rcl, uint8_t *dp, CTSize sz)
    689 {
    690   GPRArg sp[2];
    691   MSize ngpr = 0, nfpr = 0;
    692   uint32_t i;
    693   for (i = 0; i < 2; i++) {
    694     if ((rcl[i] & CCALL_RCL_INT)) {  /* Integer class takes precedence. */
    695       sp[i] = cc->gpr[ngpr++];
    696     } else if ((rcl[i] & CCALL_RCL_SSE)) {
    697       sp[i] = cc->fpr[nfpr++].l[0];
    698     }
    699   }
    700   memcpy(dp, sp, sz);
    701 }
    702 #endif
    703 
    704 /* -- ARM hard-float ABI struct classification ---------------------------- */
    705 
    706 #if LJ_TARGET_ARM && !LJ_ABI_SOFTFP
    707 
    708 /* Classify a struct based on its fields. */
    709 static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf)
    710 {
    711   CTSize sz = ct->size;
    712   unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);
    713   if ((ctf->info & CTF_VARARG)) goto noth;
    714   while (ct->sib) {
    715     CType *sct;
    716     ct = ctype_get(cts, ct->sib);
    717     if (ctype_isfield(ct->info)) {
    718       sct = ctype_rawchild(cts, ct);
    719       if (ctype_isfp(sct->info)) {
    720 	r |= sct->size;
    721 	if (!isu) n++; else if (n == 0) n = 1;
    722       } else if (ctype_iscomplex(sct->info)) {
    723 	r |= (sct->size >> 1);
    724 	if (!isu) n += 2; else if (n < 2) n = 2;
    725       } else if (ctype_isstruct(sct->info)) {
    726 	goto substruct;
    727       } else {
    728 	goto noth;
    729       }
    730     } else if (ctype_isbitfield(ct->info)) {
    731       goto noth;
    732     } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
    733       sct = ctype_rawchild(cts, ct);
    734     substruct:
    735       if (sct->size > 0) {
    736 	unsigned int s = ccall_classify_struct(cts, sct, ctf);
    737 	if (s <= 1) goto noth;
    738 	r |= (s & 255);
    739 	if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);
    740       }
    741     }
    742   }
    743   if ((r == 4 || r == 8) && n <= 4)
    744     return r + (n << 8);
    745 noth:  /* Not a homogeneous float/double aggregate. */
    746   return (sz <= 4);  /* Return structs of size <= 4 in a GPR. */
    747 }
    748 
    749 #endif
    750 
    751 /* -- ARM64 ABI struct classification ------------------------------------- */
    752 
    753 #if LJ_TARGET_ARM64
    754 
    755 /* Classify a struct based on its fields. */
    756 static unsigned int ccall_classify_struct(CTState *cts, CType *ct)
    757 {
    758   CTSize sz = ct->size;
    759   unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);
    760   while (ct->sib) {
    761     CType *sct;
    762     ct = ctype_get(cts, ct->sib);
    763     if (ctype_isfield(ct->info)) {
    764       sct = ctype_rawchild(cts, ct);
    765       if (ctype_isfp(sct->info)) {
    766 	r |= sct->size;
    767 	if (!isu) n++; else if (n == 0) n = 1;
    768       } else if (ctype_iscomplex(sct->info)) {
    769 	r |= (sct->size >> 1);
    770 	if (!isu) n += 2; else if (n < 2) n = 2;
    771       } else if (ctype_isstruct(sct->info)) {
    772 	goto substruct;
    773       } else {
    774 	goto noth;
    775       }
    776     } else if (ctype_isbitfield(ct->info)) {
    777       goto noth;
    778     } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
    779       sct = ctype_rawchild(cts, ct);
    780     substruct:
    781       if (sct->size > 0) {
    782 	unsigned int s = ccall_classify_struct(cts, sct);
    783 	if (s <= 1) goto noth;
    784 	r |= (s & 255);
    785 	if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);
    786       }
    787     }
    788   }
    789   if ((r == 4 || r == 8) && n <= 4)
    790     return r + (n << 8);
    791 noth:  /* Not a homogeneous float/double aggregate. */
    792   return (sz <= 16);  /* Return structs of size <= 16 in GPRs. */
    793 }
    794 
    795 #endif
    796 
    797 /* -- MIPS64 ABI struct classification ---------------------------- */
    798 
    799 #if LJ_TARGET_MIPS64
    800 
    801 #define FTYPE_FLOAT	1
    802 #define FTYPE_DOUBLE	2
    803 
    804 /* Classify FP fields (max. 2) and their types. */
    805 static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf)
    806 {
    807   int n = 0, ft = 0;
    808   if ((ctf->info & CTF_VARARG) || (ct->info & CTF_UNION))
    809     goto noth;
    810   while (ct->sib) {
    811     CType *sct;
    812     ct = ctype_get(cts, ct->sib);
    813     if (n == 2) {
    814       goto noth;
    815     } else if (ctype_isfield(ct->info)) {
    816       sct = ctype_rawchild(cts, ct);
    817       if (ctype_isfp(sct->info)) {
    818 	ft |= (sct->size == 4 ? FTYPE_FLOAT : FTYPE_DOUBLE) << 2*n;
    819 	n++;
    820       } else {
    821 	goto noth;
    822       }
    823     } else if (ctype_isbitfield(ct->info) ||
    824 	       ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
    825       goto noth;
    826     }
    827   }
    828   if (n <= 2)
    829     return ft;
    830 noth:  /* Not a homogeneous float/double aggregate. */
    831   return 0;  /* Struct is in GPRs. */
    832 }
    833 
    834 void ccall_copy_struct(CCallState *cc, CType *ctr, void *dp, void *sp, int ft)
    835 {
    836   if (LJ_ABI_SOFTFP ? ft :
    837       ((ft & 3) == FTYPE_FLOAT || (ft >> 2) == FTYPE_FLOAT)) {
    838     int i, ofs = 0;
    839     for (i = 0; ft != 0; i++, ft >>= 2) {
    840       if ((ft & 3) == FTYPE_FLOAT) {
    841 #if LJ_ABI_SOFTFP
    842 	/* The 2nd FP struct result is in CARG1 (gpr[2]) and not CRET2. */
    843 	memcpy((uint8_t *)dp + ofs,
    844 	       (uint8_t *)&cc->gpr[2*i] + LJ_ENDIAN_SELECT(0, 4), 4);
    845 #else
    846 	*(float *)((uint8_t *)dp + ofs) = cc->fpr[i].f;
    847 #endif
    848 	ofs += 4;
    849       } else {
    850 	ofs = (ofs + 7) & ~7;  /* 64 bit alignment. */
    851 #if LJ_ABI_SOFTFP
    852 	*(intptr_t *)((uint8_t *)dp + ofs) = cc->gpr[2*i];
    853 #else
    854 	*(double *)((uint8_t *)dp + ofs) = cc->fpr[i].d;
    855 #endif
    856 	ofs += 8;
    857       }
    858     }
    859   } else {
    860 #if !LJ_ABI_SOFTFP
    861     if (ft) sp = (uint8_t *)&cc->fpr[0];
    862 #endif
    863     memcpy(dp, sp, ctr->size);
    864   }
    865 }
    866 
    867 #endif
    868 
    869 /* -- Common C call handling ---------------------------------------------- */
    870 
    871 /* Infer the destination CTypeID for a vararg argument. */
    872 CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o)
    873 {
    874   if (tvisnumber(o)) {
    875     return CTID_DOUBLE;
    876   } else if (tviscdata(o)) {
    877     CTypeID id = cdataV(o)->ctypeid;
    878     CType *s = ctype_get(cts, id);
    879     if (ctype_isrefarray(s->info)) {
    880       return lj_ctype_intern(cts,
    881 	       CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR);
    882     } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) {
    883       /* NYI: how to pass a struct by value in a vararg argument? */
    884       return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR);
    885     } else if (ctype_isfp(s->info) && s->size == sizeof(float)) {
    886       return CTID_DOUBLE;
    887     } else {
    888       return id;
    889     }
    890   } else if (tvisstr(o)) {
    891     return CTID_P_CCHAR;
    892   } else if (tvisbool(o)) {
    893     return CTID_BOOL;
    894   } else {
    895     return CTID_P_VOID;
    896   }
    897 }
    898 
    899 /* Setup arguments for C call. */
    900 static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
    901 			  CCallState *cc)
    902 {
    903   int gcsteps = 0;
    904   TValue *o, *top = L->top;
    905   CTypeID fid;
    906   CType *ctr;
    907   MSize maxgpr, ngpr = 0, nsp = 0, narg;
    908 #if CCALL_NARG_FPR
    909   MSize nfpr = 0;
    910 #if LJ_TARGET_ARM
    911   MSize fprodd = 0;
    912 #endif
    913 #endif
    914 
    915   /* Clear unused regs to get some determinism in case of misdeclaration. */
    916   memset(cc->gpr, 0, sizeof(cc->gpr));
    917 #if CCALL_NUM_FPR
    918   memset(cc->fpr, 0, sizeof(cc->fpr));
    919 #endif
    920 
    921 #if LJ_TARGET_X86
    922   /* x86 has several different calling conventions. */
    923   cc->resx87 = 0;
    924   switch (ctype_cconv(ct->info)) {
    925   case CTCC_FASTCALL: maxgpr = 2; break;
    926   case CTCC_THISCALL: maxgpr = 1; break;
    927   default: maxgpr = 0; break;
    928   }
    929 #else
    930   maxgpr = CCALL_NARG_GPR;
    931 #endif
    932 
    933   /* Perform required setup for some result types. */
    934   ctr = ctype_rawchild(cts, ct);
    935   if (ctype_isvector(ctr->info)) {
    936     if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16)))
    937       goto err_nyi;
    938   } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) {
    939     /* Preallocate cdata object and anchor it after arguments. */
    940     CTSize sz = ctr->size;
    941     GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz);
    942     void *dp = cdataptr(cd);
    943     setcdataV(L, L->top++, cd);
    944     if (ctype_isstruct(ctr->info)) {
    945       CCALL_HANDLE_STRUCTRET
    946     } else {
    947       CCALL_HANDLE_COMPLEXRET
    948     }
    949 #if LJ_TARGET_X86
    950   } else if (ctype_isfp(ctr->info)) {
    951     cc->resx87 = ctr->size == sizeof(float) ? 1 : 2;
    952 #endif
    953   }
    954 
    955   /* Skip initial attributes. */
    956   fid = ct->sib;
    957   while (fid) {
    958     CType *ctf = ctype_get(cts, fid);
    959     if (!ctype_isattrib(ctf->info)) break;
    960     fid = ctf->sib;
    961   }
    962 
    963   /* Walk through all passed arguments. */
    964   for (o = L->base+1, narg = 1; o < top; o++, narg++) {
    965     CTypeID did;
    966     CType *d;
    967     CTSize sz;
    968     MSize n, isfp = 0, isva = 0;
    969     void *dp, *rp = NULL;
    970 
    971     if (fid) {  /* Get argument type from field. */
    972       CType *ctf = ctype_get(cts, fid);
    973       fid = ctf->sib;
    974       lua_assert(ctype_isfield(ctf->info));
    975       did = ctype_cid(ctf->info);
    976     } else {
    977       if (!(ct->info & CTF_VARARG))
    978 	lj_err_caller(L, LJ_ERR_FFI_NUMARG);  /* Too many arguments. */
    979       did = lj_ccall_ctid_vararg(cts, o);  /* Infer vararg type. */
    980       isva = 1;
    981     }
    982     d = ctype_raw(cts, did);
    983     sz = d->size;
    984 
    985     /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */
    986     if (ctype_isnum(d->info)) {
    987       if (sz > 8) goto err_nyi;
    988       if ((d->info & CTF_FP))
    989 	isfp = 1;
    990     } else if (ctype_isvector(d->info)) {
    991       if (CCALL_VECTOR_REG && (sz == 8 || sz == 16))
    992 	isfp = 1;
    993       else
    994 	goto err_nyi;
    995     } else if (ctype_isstruct(d->info)) {
    996       CCALL_HANDLE_STRUCTARG
    997     } else if (ctype_iscomplex(d->info)) {
    998       CCALL_HANDLE_COMPLEXARG
    999     } else {
   1000       sz = CTSIZE_PTR;
   1001     }
   1002     sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
   1003     n = sz / CTSIZE_PTR;  /* Number of GPRs or stack slots needed. */
   1004 
   1005     CCALL_HANDLE_REGARG  /* Handle register arguments. */
   1006 
   1007     /* Otherwise pass argument on stack. */
   1008     if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) {
   1009       MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1;
   1010       nsp = (nsp + align) & ~align;  /* Align argument on stack. */
   1011     }
   1012     if (nsp + n > CCALL_MAXSTACK) {  /* Too many arguments. */
   1013     err_nyi:
   1014       lj_err_caller(L, LJ_ERR_FFI_NYICALL);
   1015     }
   1016     dp = &cc->stack[nsp];
   1017     nsp += n;
   1018     isva = 0;
   1019 
   1020   done:
   1021     if (rp) {  /* Pass by reference. */
   1022       gcsteps++;
   1023       *(void **)dp = rp;
   1024       dp = rp;
   1025     }
   1026     lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));
   1027     /* Extend passed integers to 32 bits at least. */
   1028     if (ctype_isinteger_or_bool(d->info) && d->size < 4) {
   1029       if (d->info & CTF_UNSIGNED)
   1030 	*(uint32_t *)dp = d->size == 1 ? (uint32_t)*(uint8_t *)dp :
   1031 					 (uint32_t)*(uint16_t *)dp;
   1032       else
   1033 	*(int32_t *)dp = d->size == 1 ? (int32_t)*(int8_t *)dp :
   1034 					(int32_t)*(int16_t *)dp;
   1035     }
   1036 #if LJ_TARGET_MIPS64
   1037     if ((ctype_isinteger_or_bool(d->info) || ctype_isenum(d->info) ||
   1038 	 (isfp && nsp == 0)) && d->size <= 4) {
   1039       *(int64_t *)dp = (int64_t)*(int32_t *)dp;  /* Sign-extend to 64 bit. */
   1040     }
   1041 #endif
   1042 #if LJ_TARGET_X64 && LJ_ABI_WIN
   1043     if (isva) {  /* Windows/x64 mirrors varargs in both register sets. */
   1044       if (nfpr == ngpr)
   1045 	cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0];
   1046       else
   1047 	cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1];
   1048     }
   1049 #else
   1050     UNUSED(isva);
   1051 #endif
   1052 #if LJ_TARGET_X64 && !LJ_ABI_WIN
   1053     if (isfp == 2 && n == 2 && (uint8_t *)dp == (uint8_t *)&cc->fpr[nfpr-2]) {
   1054       cc->fpr[nfpr-1].d[0] = cc->fpr[nfpr-2].d[1];  /* Split complex double. */
   1055       cc->fpr[nfpr-2].d[1] = 0;
   1056     }
   1057 #elif LJ_TARGET_ARM64 || (LJ_TARGET_MIPS64 && !LJ_ABI_SOFTFP)
   1058     if (isfp == 2 && (uint8_t *)dp < (uint8_t *)cc->stack) {
   1059       /* Split float HFA or complex float into separate registers. */
   1060       CTSize i = (sz >> 2) - 1;
   1061       do { ((uint64_t *)dp)[i] = ((uint32_t *)dp)[i]; } while (i--);
   1062     }
   1063 #else
   1064     UNUSED(isfp);
   1065 #endif
   1066   }
   1067   if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG);  /* Too few arguments. */
   1068 
   1069 #if LJ_TARGET_X64 || LJ_TARGET_PPC
   1070   cc->nfpr = nfpr;  /* Required for vararg functions. */
   1071 #endif
   1072   cc->nsp = nsp;
   1073   cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR;
   1074   if (nsp > CCALL_SPS_FREE)
   1075     cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u);
   1076   return gcsteps;
   1077 }
   1078 
   1079 /* Get results from C call. */
   1080 static int ccall_get_results(lua_State *L, CTState *cts, CType *ct,
   1081 			     CCallState *cc, int *ret)
   1082 {
   1083   CType *ctr = ctype_rawchild(cts, ct);
   1084   uint8_t *sp = (uint8_t *)&cc->gpr[0];
   1085   if (ctype_isvoid(ctr->info)) {
   1086     *ret = 0;  /* Zero results. */
   1087     return 0;  /* No additional GC step. */
   1088   }
   1089   *ret = 1;  /* One result. */
   1090   if (ctype_isstruct(ctr->info)) {
   1091     /* Return cdata object which is already on top of stack. */
   1092     if (!cc->retref) {
   1093       void *dp = cdataptr(cdataV(L->top-1));  /* Use preallocated object. */
   1094       CCALL_HANDLE_STRUCTRET2
   1095     }
   1096     return 1;  /* One GC step. */
   1097   }
   1098   if (ctype_iscomplex(ctr->info)) {
   1099     /* Return cdata object which is already on top of stack. */
   1100     void *dp = cdataptr(cdataV(L->top-1));  /* Use preallocated object. */
   1101     CCALL_HANDLE_COMPLEXRET2
   1102     return 1;  /* One GC step. */
   1103   }
   1104   if (LJ_BE && ctr->size < CTSIZE_PTR &&
   1105       (ctype_isinteger_or_bool(ctr->info) || ctype_isenum(ctr->info)))
   1106     sp += (CTSIZE_PTR - ctr->size);
   1107 #if CCALL_NUM_FPR
   1108   if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info))
   1109     sp = (uint8_t *)&cc->fpr[0];
   1110 #endif
   1111 #ifdef CCALL_HANDLE_RET
   1112   CCALL_HANDLE_RET
   1113 #endif
   1114   /* No reference types end up here, so there's no need for the CTypeID. */
   1115   lua_assert(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info)));
   1116   return lj_cconv_tv_ct(cts, ctr, 0, L->top-1, sp);
   1117 }
   1118 
   1119 /* Call C function. */
   1120 int lj_ccall_func(lua_State *L, GCcdata *cd)
   1121 {
   1122   CTState *cts = ctype_cts(L);
   1123   CType *ct = ctype_raw(cts, cd->ctypeid);
   1124   CTSize sz = CTSIZE_PTR;
   1125   if (ctype_isptr(ct->info)) {
   1126     sz = ct->size;
   1127     ct = ctype_rawchild(cts, ct);
   1128   }
   1129   if (ctype_isfunc(ct->info)) {
   1130     CCallState cc;
   1131     int gcsteps, ret;
   1132     cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz);
   1133     gcsteps = ccall_set_args(L, cts, ct, &cc);
   1134     ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab);
   1135     cts->cb.slot = ~0u;
   1136     lj_vm_ffi_call(&cc);
   1137     if (cts->cb.slot != ~0u) {  /* Blacklist function that called a callback. */
   1138       TValue tv;
   1139       setlightudV(&tv, (void *)cc.func);
   1140       setboolV(lj_tab_set(L, cts->miscmap, &tv), 1);
   1141     }
   1142     ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab);  /* May be reallocated. */
   1143     gcsteps += ccall_get_results(L, cts, ct, &cc, &ret);
   1144 #if LJ_TARGET_X86 && LJ_ABI_WIN
   1145     /* Automatically detect __stdcall and fix up C function declaration. */
   1146     if (cc.spadj && ctype_cconv(ct->info) == CTCC_CDECL) {
   1147       CTF_INSERT(ct->info, CCONV, CTCC_STDCALL);
   1148       lj_trace_abort(G(L));
   1149     }
   1150 #endif
   1151     while (gcsteps-- > 0)
   1152       lj_gc_check(L);
   1153     return ret;
   1154   }
   1155   return -1;  /* Not a function. */
   1156 }
   1157 
   1158 #endif