root/lj_ccall.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ccall_classify_struct
  2. ccall_classify_ct
  3. ccall_classify_struct
  4. ccall_struct_reg
  5. ccall_struct_arg
  6. ccall_struct_ret
  7. ccall_classify_struct
  8. ccall_classify_struct
  9. ccall_classify_struct
  10. ccall_copy_struct
  11. lj_ccall_ctid_vararg
  12. ccall_set_args
  13. ccall_get_results
  14. lj_ccall_func

   1 /*
   2 ** FFI C call handling.
   3 ** Copyright (C) 2005-2017 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].lo; } 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 = sz == 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 #if LJ_BE
 363 #define CCALL_HANDLE_RET \
 364   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
 365     sp = (uint8_t *)&cc->fpr[0].f;
 366 #endif
 367 
 368 
 369 #elif LJ_TARGET_PPC
 370 /* -- PPC calling conventions --------------------------------------------- */
 371 
 372 #define CCALL_HANDLE_STRUCTRET \
 373   cc->retref = 1;  /* Return all structs by reference. */ \
 374   cc->gpr[ngpr++] = (GPRArg)dp;
 375 
 376 #define CCALL_HANDLE_COMPLEXRET \
 377   /* Complex values are returned in 2 or 4 GPRs. */ \
 378   cc->retref = 0;
 379 
 380 #define CCALL_HANDLE_COMPLEXRET2 \
 381   memcpy(dp, sp, ctr->size);  /* Copy complex from GPRs. */
 382 
 383 #define CCALL_HANDLE_STRUCTARG \
 384   rp = cdataptr(lj_cdata_new(cts, did, sz)); \
 385   sz = CTSIZE_PTR;  /* Pass all structs by reference. */
 386 
 387 #define CCALL_HANDLE_COMPLEXARG \
 388   /* Pass complex by value in 2 or 4 GPRs. */
 389 
 390 #define CCALL_HANDLE_GPR \
 391   /* 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) || ctype_isfp(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 #if LJ_ABI_SOFTFP
 406 #define CCALL_HANDLE_REGARG  CCALL_HANDLE_GPR
 407 #else
 408 #define CCALL_HANDLE_REGARG \
 409   if (isfp) {  /* Try to pass argument in FPRs. */ \
 410     if (nfpr + 1 <= CCALL_NARG_FPR) { \
 411       dp = &cc->fpr[nfpr]; \
 412       nfpr += 1; \
 413       d = ctype_get(cts, CTID_DOUBLE);  /* FPRs always hold doubles. */ \
 414       goto done; \
 415     } \
 416   } else { \
 417     CCALL_HANDLE_GPR \
 418   }
 419 #endif
 420 
 421 #if !LJ_ABI_SOFTFP
 422 #define CCALL_HANDLE_RET \
 423   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
 424     ctr = ctype_get(cts, CTID_DOUBLE);  /* FPRs always hold doubles. */
 425 #endif
 426 
 427 #elif LJ_TARGET_MIPS32
 428 /* -- MIPS o32 calling conventions ---------------------------------------- */
 429 
 430 #define CCALL_HANDLE_STRUCTRET \
 431   cc->retref = 1;  /* Return all structs by reference. */ \
 432   cc->gpr[ngpr++] = (GPRArg)dp;
 433 
 434 #define CCALL_HANDLE_COMPLEXRET \
 435   /* Complex values are returned in 1 or 2 FPRs. */ \
 436   cc->retref = 0;
 437 
 438 #if LJ_ABI_SOFTFP
 439 #define CCALL_HANDLE_COMPLEXRET2 \
 440   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from GPRs. */ \
 441     ((intptr_t *)dp)[0] = cc->gpr[0]; \
 442     ((intptr_t *)dp)[1] = cc->gpr[1]; \
 443   } else {  /* Copy complex double from GPRs. */ \
 444     ((intptr_t *)dp)[0] = cc->gpr[0]; \
 445     ((intptr_t *)dp)[1] = cc->gpr[1]; \
 446     ((intptr_t *)dp)[2] = cc->gpr[2]; \
 447     ((intptr_t *)dp)[3] = cc->gpr[3]; \
 448   }
 449 #else
 450 #define CCALL_HANDLE_COMPLEXRET2 \
 451   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPRs. */ \
 452     ((float *)dp)[0] = cc->fpr[0].f; \
 453     ((float *)dp)[1] = cc->fpr[1].f; \
 454   } else {  /* Copy complex double from FPRs. */ \
 455     ((double *)dp)[0] = cc->fpr[0].d; \
 456     ((double *)dp)[1] = cc->fpr[1].d; \
 457   }
 458 #endif
 459 
 460 #define CCALL_HANDLE_STRUCTARG \
 461   /* Pass all structs by value in registers and/or on the stack. */
 462 
 463 #define CCALL_HANDLE_COMPLEXARG \
 464   /* Pass complex by value in 2 or 4 GPRs. */
 465 
 466 #define CCALL_HANDLE_GPR \
 467   if ((d->info & CTF_ALIGN) > CTALIGN_PTR) \
 468     ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \
 469   if (ngpr < maxgpr) { \
 470     dp = &cc->gpr[ngpr]; \
 471     if (ngpr + n > maxgpr) { \
 472      nsp += ngpr + n - maxgpr;  /* Assumes contiguous gpr/stack fields. */ \
 473      if (nsp > CCALL_MAXSTACK) goto err_nyi;  /* Too many arguments. */ \
 474      ngpr = maxgpr; \
 475     } else { \
 476      ngpr += n; \
 477     } \
 478     goto done; \
 479   }
 480 
 481 #if !LJ_ABI_SOFTFP      /* MIPS32 hard-float */
 482 #define CCALL_HANDLE_REGARG \
 483   if (isfp && nfpr < CCALL_NARG_FPR && !(ct->info & CTF_VARARG)) { \
 484     /* Try to pass argument in FPRs. */ \
 485     dp = n == 1 ? (void *)&cc->fpr[nfpr].f : (void *)&cc->fpr[nfpr].d; \
 486     nfpr++; ngpr += n; \
 487     goto done; \
 488   } else {  /* Try to pass argument in GPRs. */ \
 489     nfpr = CCALL_NARG_FPR; \
 490     CCALL_HANDLE_GPR \
 491   }
 492 #else                   /* MIPS32 soft-float */
 493 #define CCALL_HANDLE_REGARG CCALL_HANDLE_GPR
 494 #endif
 495 
 496 #if !LJ_ABI_SOFTFP
 497 /* On MIPS64 soft-float, position of float return values is endian-dependant. */
 498 #define CCALL_HANDLE_RET \
 499   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
 500     sp = (uint8_t *)&cc->fpr[0].f;
 501 #endif
 502 
 503 #elif LJ_TARGET_MIPS64
 504 /* -- MIPS n64 calling conventions ---------------------------------------- */
 505 
 506 #define CCALL_HANDLE_STRUCTRET \
 507   cc->retref = !(sz <= 16); \
 508   if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp;
 509 
 510 #define CCALL_HANDLE_STRUCTRET2 \
 511   ccall_copy_struct(cc, ctr, dp, sp, ccall_classify_struct(cts, ctr, ct));
 512 
 513 #define CCALL_HANDLE_COMPLEXRET \
 514   /* Complex values are returned in 1 or 2 FPRs. */ \
 515   cc->retref = 0;
 516 
 517 #if LJ_ABI_SOFTFP       /* MIPS64 soft-float */
 518 
 519 #define CCALL_HANDLE_COMPLEXRET2 \
 520   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from GPRs. */ \
 521     ((intptr_t *)dp)[0] = cc->gpr[0]; \
 522   } else {  /* Copy complex double from GPRs. */ \
 523     ((intptr_t *)dp)[0] = cc->gpr[0]; \
 524     ((intptr_t *)dp)[1] = cc->gpr[1]; \
 525   }
 526 
 527 #define CCALL_HANDLE_COMPLEXARG \
 528   /* Pass complex by value in 2 or 4 GPRs. */
 529 
 530 /* Position of soft-float 'float' return value depends on endianess.  */
 531 #define CCALL_HANDLE_RET \
 532   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
 533     sp = (uint8_t *)cc->gpr + LJ_ENDIAN_SELECT(0, 4);
 534 
 535 #else                   /* MIPS64 hard-float */
 536 
 537 #define CCALL_HANDLE_COMPLEXRET2 \
 538   if (ctr->size == 2*sizeof(float)) {  /* Copy complex float from FPRs. */ \
 539     ((float *)dp)[0] = cc->fpr[0].f; \
 540     ((float *)dp)[1] = cc->fpr[1].f; \
 541   } else {  /* Copy complex double from FPRs. */ \
 542     ((double *)dp)[0] = cc->fpr[0].d; \
 543     ((double *)dp)[1] = cc->fpr[1].d; \
 544   }
 545 
 546 #define CCALL_HANDLE_COMPLEXARG \
 547   if (sz == 2*sizeof(float)) { \
 548     isfp = 2; \
 549     if (ngpr < maxgpr) \
 550       sz *= 2; \
 551   }
 552 
 553 #define CCALL_HANDLE_RET \
 554   if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
 555     sp = (uint8_t *)&cc->fpr[0].f;
 556 
 557 #endif
 558 
 559 #define CCALL_HANDLE_STRUCTARG \
 560   /* Pass all structs by value in registers and/or on the stack. */
 561 
 562 #define CCALL_HANDLE_REGARG \
 563   if (ngpr < maxgpr) { \
 564     dp = &cc->gpr[ngpr]; \
 565     if (ngpr + n > maxgpr) { \
 566       nsp += ngpr + n - maxgpr;  /* Assumes contiguous gpr/stack fields. */ \
 567       if (nsp > CCALL_MAXSTACK) goto err_nyi;  /* Too many arguments. */ \
 568       ngpr = maxgpr; \
 569     } else { \
 570       ngpr += n; \
 571     } \
 572     goto done; \
 573   }
 574 
 575 #else
 576 #error "Missing calling convention definitions for this architecture"
 577 #endif
 578 
 579 #ifndef CCALL_HANDLE_STRUCTRET2
 580 #define CCALL_HANDLE_STRUCTRET2 \
 581   memcpy(dp, sp, ctr->size);  /* Copy struct return value from GPRs. */
 582 #endif
 583 
 584 /* -- x86 OSX ABI struct classification ----------------------------------- */
 585 
 586 #if LJ_TARGET_X86 && LJ_TARGET_OSX
 587 
 588 /* Check for struct with single FP field. */
 589 static int ccall_classify_struct(CTState *cts, CType *ct)
 590 {
 591   CTSize sz = ct->size;
 592   if (!(sz == sizeof(float) || sz == sizeof(double))) return 0;
 593   if ((ct->info & CTF_UNION)) return 0;
 594   while (ct->sib) {
 595     ct = ctype_get(cts, ct->sib);
 596     if (ctype_isfield(ct->info)) {
 597       CType *sct = ctype_rawchild(cts, ct);
 598       if (ctype_isfp(sct->info)) {
 599         if (sct->size == sz)
 600           return (sz >> 2);  /* Return 1 for float or 2 for double. */
 601       } else if (ctype_isstruct(sct->info)) {
 602         if (sct->size)
 603           return ccall_classify_struct(cts, sct);
 604       } else {
 605         break;
 606       }
 607     } else if (ctype_isbitfield(ct->info)) {
 608       break;
 609     } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
 610       CType *sct = ctype_rawchild(cts, ct);
 611       if (sct->size)
 612         return ccall_classify_struct(cts, sct);
 613     }
 614   }
 615   return 0;
 616 }
 617 
 618 #endif
 619 
 620 /* -- x64 struct classification ------------------------------------------- */
 621 
 622 #if LJ_TARGET_X64 && !LJ_ABI_WIN
 623 
 624 /* Register classes for x64 struct classification. */
 625 #define CCALL_RCL_INT   1
 626 #define CCALL_RCL_SSE   2
 627 #define CCALL_RCL_MEM   4
 628 /* NYI: classify vectors. */
 629 
 630 static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs);
 631 
 632 /* Classify a C type. */
 633 static void ccall_classify_ct(CTState *cts, CType *ct, int *rcl, CTSize ofs)
 634 {
 635   if (ctype_isarray(ct->info)) {
 636     CType *cct = ctype_rawchild(cts, ct);
 637     CTSize eofs, esz = cct->size, asz = ct->size;
 638     for (eofs = 0; eofs < asz; eofs += esz)
 639       ccall_classify_ct(cts, cct, rcl, ofs+eofs);
 640   } else if (ctype_isstruct(ct->info)) {
 641     ccall_classify_struct(cts, ct, rcl, ofs);
 642   } else {
 643     int cl = ctype_isfp(ct->info) ? CCALL_RCL_SSE : CCALL_RCL_INT;
 644     lua_assert(ctype_hassize(ct->info));
 645     if ((ofs & (ct->size-1))) cl = CCALL_RCL_MEM;  /* Unaligned. */
 646     rcl[(ofs >= 8)] |= cl;
 647   }
 648 }
 649 
 650 /* Recursively classify a struct based on its fields. */
 651 static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs)
 652 {
 653   if (ct->size > 16) return CCALL_RCL_MEM;  /* Too big, gets memory class. */
 654   while (ct->sib) {
 655     CTSize fofs;
 656     ct = ctype_get(cts, ct->sib);
 657     fofs = ofs+ct->size;
 658     if (ctype_isfield(ct->info))
 659       ccall_classify_ct(cts, ctype_rawchild(cts, ct), rcl, fofs);
 660     else if (ctype_isbitfield(ct->info))
 661       rcl[(fofs >= 8)] |= CCALL_RCL_INT;  /* NYI: unaligned bitfields? */
 662     else if (ctype_isxattrib(ct->info, CTA_SUBTYPE))
 663       ccall_classify_struct(cts, ctype_rawchild(cts, ct), rcl, fofs);
 664   }
 665   return ((rcl[0]|rcl[1]) & CCALL_RCL_MEM);  /* Memory class? */
 666 }
 667 
 668 /* Try to split up a small struct into registers. */
 669 static int ccall_struct_reg(CCallState *cc, GPRArg *dp, int *rcl)
 670 {
 671   MSize ngpr = cc->ngpr, nfpr = cc->nfpr;
 672   uint32_t i;
 673   for (i = 0; i < 2; i++) {
 674     lua_assert(!(rcl[i] & CCALL_RCL_MEM));
 675     if ((rcl[i] & CCALL_RCL_INT)) {  /* Integer class takes precedence. */
 676       if (ngpr >= CCALL_NARG_GPR) return 1;  /* Register overflow. */
 677       cc->gpr[ngpr++] = dp[i];
 678     } else if ((rcl[i] & CCALL_RCL_SSE)) {
 679       if (nfpr >= CCALL_NARG_FPR) return 1;  /* Register overflow. */
 680       cc->fpr[nfpr++].l[0] = dp[i];
 681     }
 682   }
 683   cc->ngpr = ngpr; cc->nfpr = nfpr;
 684   return 0;  /* Ok. */
 685 }
 686 
 687 /* Pass a small struct argument. */
 688 static int ccall_struct_arg(CCallState *cc, CTState *cts, CType *d, int *rcl,
 689                             TValue *o, int narg)
 690 {
 691   GPRArg dp[2];
 692   dp[0] = dp[1] = 0;
 693   /* Convert to temp. struct. */
 694   lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));
 695   if (ccall_struct_reg(cc, dp, rcl)) {  /* Register overflow? Pass on stack. */
 696     MSize nsp = cc->nsp, n = rcl[1] ? 2 : 1;
 697     if (nsp + n > CCALL_MAXSTACK) return 1;  /* Too many arguments. */
 698     cc->nsp = nsp + n;
 699     memcpy(&cc->stack[nsp], dp, n*CTSIZE_PTR);
 700   }
 701   return 0;  /* Ok. */
 702 }
 703 
 704 /* Combine returned small struct. */
 705 static void ccall_struct_ret(CCallState *cc, int *rcl, uint8_t *dp, CTSize sz)
 706 {
 707   GPRArg sp[2];
 708   MSize ngpr = 0, nfpr = 0;
 709   uint32_t i;
 710   for (i = 0; i < 2; i++) {
 711     if ((rcl[i] & CCALL_RCL_INT)) {  /* Integer class takes precedence. */
 712       sp[i] = cc->gpr[ngpr++];
 713     } else if ((rcl[i] & CCALL_RCL_SSE)) {
 714       sp[i] = cc->fpr[nfpr++].l[0];
 715     }
 716   }
 717   memcpy(dp, sp, sz);
 718 }
 719 #endif
 720 
 721 /* -- ARM hard-float ABI struct classification ---------------------------- */
 722 
 723 #if LJ_TARGET_ARM && !LJ_ABI_SOFTFP
 724 
 725 /* Classify a struct based on its fields. */
 726 static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf)
 727 {
 728   CTSize sz = ct->size;
 729   unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);
 730   if ((ctf->info & CTF_VARARG)) goto noth;
 731   while (ct->sib) {
 732     CType *sct;
 733     ct = ctype_get(cts, ct->sib);
 734     if (ctype_isfield(ct->info)) {
 735       sct = ctype_rawchild(cts, ct);
 736       if (ctype_isfp(sct->info)) {
 737         r |= sct->size;
 738         if (!isu) n++; else if (n == 0) n = 1;
 739       } else if (ctype_iscomplex(sct->info)) {
 740         r |= (sct->size >> 1);
 741         if (!isu) n += 2; else if (n < 2) n = 2;
 742       } else if (ctype_isstruct(sct->info)) {
 743         goto substruct;
 744       } else {
 745         goto noth;
 746       }
 747     } else if (ctype_isbitfield(ct->info)) {
 748       goto noth;
 749     } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
 750       sct = ctype_rawchild(cts, ct);
 751     substruct:
 752       if (sct->size > 0) {
 753         unsigned int s = ccall_classify_struct(cts, sct, ctf);
 754         if (s <= 1) goto noth;
 755         r |= (s & 255);
 756         if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);
 757       }
 758     }
 759   }
 760   if ((r == 4 || r == 8) && n <= 4)
 761     return r + (n << 8);
 762 noth:  /* Not a homogeneous float/double aggregate. */
 763   return (sz <= 4);  /* Return structs of size <= 4 in a GPR. */
 764 }
 765 
 766 #endif
 767 
 768 /* -- ARM64 ABI struct classification ------------------------------------- */
 769 
 770 #if LJ_TARGET_ARM64
 771 
 772 /* Classify a struct based on its fields. */
 773 static unsigned int ccall_classify_struct(CTState *cts, CType *ct)
 774 {
 775   CTSize sz = ct->size;
 776   unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION);
 777   while (ct->sib) {
 778     CType *sct;
 779     ct = ctype_get(cts, ct->sib);
 780     if (ctype_isfield(ct->info)) {
 781       sct = ctype_rawchild(cts, ct);
 782       if (ctype_isfp(sct->info)) {
 783         r |= sct->size;
 784         if (!isu) n++; else if (n == 0) n = 1;
 785       } else if (ctype_iscomplex(sct->info)) {
 786         r |= (sct->size >> 1);
 787         if (!isu) n += 2; else if (n < 2) n = 2;
 788       } else if (ctype_isstruct(sct->info)) {
 789         goto substruct;
 790       } else {
 791         goto noth;
 792       }
 793     } else if (ctype_isbitfield(ct->info)) {
 794       goto noth;
 795     } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
 796       sct = ctype_rawchild(cts, ct);
 797     substruct:
 798       if (sct->size > 0) {
 799         unsigned int s = ccall_classify_struct(cts, sct);
 800         if (s <= 1) goto noth;
 801         r |= (s & 255);
 802         if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8);
 803       }
 804     }
 805   }
 806   if ((r == 4 || r == 8) && n <= 4)
 807     return r + (n << 8);
 808 noth:  /* Not a homogeneous float/double aggregate. */
 809   return (sz <= 16);  /* Return structs of size <= 16 in GPRs. */
 810 }
 811 
 812 #endif
 813 
 814 /* -- MIPS64 ABI struct classification ---------------------------- */
 815 
 816 #if LJ_TARGET_MIPS64
 817 
 818 #define FTYPE_FLOAT     1
 819 #define FTYPE_DOUBLE    2
 820 
 821 /* Classify FP fields (max. 2) and their types. */
 822 static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf)
 823 {
 824   int n = 0, ft = 0;
 825   if ((ctf->info & CTF_VARARG) || (ct->info & CTF_UNION))
 826     goto noth;
 827   while (ct->sib) {
 828     CType *sct;
 829     ct = ctype_get(cts, ct->sib);
 830     if (n == 2) {
 831       goto noth;
 832     } else if (ctype_isfield(ct->info)) {
 833       sct = ctype_rawchild(cts, ct);
 834       if (ctype_isfp(sct->info)) {
 835         ft |= (sct->size == 4 ? FTYPE_FLOAT : FTYPE_DOUBLE) << 2*n;
 836         n++;
 837       } else {
 838         goto noth;
 839       }
 840     } else if (ctype_isbitfield(ct->info) ||
 841                ctype_isxattrib(ct->info, CTA_SUBTYPE)) {
 842       goto noth;
 843     }
 844   }
 845   if (n <= 2)
 846     return ft;
 847 noth:  /* Not a homogeneous float/double aggregate. */
 848   return 0;  /* Struct is in GPRs. */
 849 }
 850 
 851 static void ccall_copy_struct(CCallState *cc, CType *ctr, void *dp, void *sp,
 852                               int ft)
 853 {
 854   if (LJ_ABI_SOFTFP ? ft :
 855       ((ft & 3) == FTYPE_FLOAT || (ft >> 2) == FTYPE_FLOAT)) {
 856     int i, ofs = 0;
 857     for (i = 0; ft != 0; i++, ft >>= 2) {
 858       if ((ft & 3) == FTYPE_FLOAT) {
 859 #if LJ_ABI_SOFTFP
 860         /* The 2nd FP struct result is in CARG1 (gpr[2]) and not CRET2. */
 861         memcpy((uint8_t *)dp + ofs,
 862                (uint8_t *)&cc->gpr[2*i] + LJ_ENDIAN_SELECT(0, 4), 4);
 863 #else
 864         *(float *)((uint8_t *)dp + ofs) = cc->fpr[i].f;
 865 #endif
 866         ofs += 4;
 867       } else {
 868         ofs = (ofs + 7) & ~7;  /* 64 bit alignment. */
 869 #if LJ_ABI_SOFTFP
 870         *(intptr_t *)((uint8_t *)dp + ofs) = cc->gpr[2*i];
 871 #else
 872         *(double *)((uint8_t *)dp + ofs) = cc->fpr[i].d;
 873 #endif
 874         ofs += 8;
 875       }
 876     }
 877   } else {
 878 #if !LJ_ABI_SOFTFP
 879     if (ft) sp = (uint8_t *)&cc->fpr[0];
 880 #endif
 881     memcpy(dp, sp, ctr->size);
 882   }
 883 }
 884 
 885 #endif
 886 
 887 /* -- Common C call handling ---------------------------------------------- */
 888 
 889 /* Infer the destination CTypeID for a vararg argument. */
 890 CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o)
 891 {
 892   if (tvisnumber(o)) {
 893     return CTID_DOUBLE;
 894   } else if (tviscdata(o)) {
 895     CTypeID id = cdataV(o)->ctypeid;
 896     CType *s = ctype_get(cts, id);
 897     if (ctype_isrefarray(s->info)) {
 898       return lj_ctype_intern(cts,
 899                CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR);
 900     } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) {
 901       /* NYI: how to pass a struct by value in a vararg argument? */
 902       return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR);
 903     } else if (ctype_isfp(s->info) && s->size == sizeof(float)) {
 904       return CTID_DOUBLE;
 905     } else {
 906       return id;
 907     }
 908   } else if (tvisstr(o)) {
 909     return CTID_P_CCHAR;
 910   } else if (tvisbool(o)) {
 911     return CTID_BOOL;
 912   } else {
 913     return CTID_P_VOID;
 914   }
 915 }
 916 
 917 /* Setup arguments for C call. */
 918 static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
 919                           CCallState *cc)
 920 {
 921   int gcsteps = 0;
 922   TValue *o, *top = L->top;
 923   CTypeID fid;
 924   CType *ctr;
 925   MSize maxgpr, ngpr = 0, nsp = 0, narg;
 926 #if CCALL_NARG_FPR
 927   MSize nfpr = 0;
 928 #if LJ_TARGET_ARM
 929   MSize fprodd = 0;
 930 #endif
 931 #endif
 932 
 933   /* Clear unused regs to get some determinism in case of misdeclaration. */
 934   memset(cc->gpr, 0, sizeof(cc->gpr));
 935 #if CCALL_NUM_FPR
 936   memset(cc->fpr, 0, sizeof(cc->fpr));
 937 #endif
 938 
 939 #if LJ_TARGET_X86
 940   /* x86 has several different calling conventions. */
 941   cc->resx87 = 0;
 942   switch (ctype_cconv(ct->info)) {
 943   case CTCC_FASTCALL: maxgpr = 2; break;
 944   case CTCC_THISCALL: maxgpr = 1; break;
 945   default: maxgpr = 0; break;
 946   }
 947 #else
 948   maxgpr = CCALL_NARG_GPR;
 949 #endif
 950 
 951   /* Perform required setup for some result types. */
 952   ctr = ctype_rawchild(cts, ct);
 953   if (ctype_isvector(ctr->info)) {
 954     if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16)))
 955       goto err_nyi;
 956   } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) {
 957     /* Preallocate cdata object and anchor it after arguments. */
 958     CTSize sz = ctr->size;
 959     GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz);
 960     void *dp = cdataptr(cd);
 961     setcdataV(L, L->top++, cd);
 962     if (ctype_isstruct(ctr->info)) {
 963       CCALL_HANDLE_STRUCTRET
 964     } else {
 965       CCALL_HANDLE_COMPLEXRET
 966     }
 967 #if LJ_TARGET_X86
 968   } else if (ctype_isfp(ctr->info)) {
 969     cc->resx87 = ctr->size == sizeof(float) ? 1 : 2;
 970 #endif
 971   }
 972 
 973   /* Skip initial attributes. */
 974   fid = ct->sib;
 975   while (fid) {
 976     CType *ctf = ctype_get(cts, fid);
 977     if (!ctype_isattrib(ctf->info)) break;
 978     fid = ctf->sib;
 979   }
 980 
 981   /* Walk through all passed arguments. */
 982   for (o = L->base+1, narg = 1; o < top; o++, narg++) {
 983     CTypeID did;
 984     CType *d;
 985     CTSize sz;
 986     MSize n, isfp = 0, isva = 0;
 987     void *dp, *rp = NULL;
 988 
 989     if (fid) {  /* Get argument type from field. */
 990       CType *ctf = ctype_get(cts, fid);
 991       fid = ctf->sib;
 992       lua_assert(ctype_isfield(ctf->info));
 993       did = ctype_cid(ctf->info);
 994     } else {
 995       if (!(ct->info & CTF_VARARG))
 996         lj_err_caller(L, LJ_ERR_FFI_NUMARG);  /* Too many arguments. */
 997       did = lj_ccall_ctid_vararg(cts, o);  /* Infer vararg type. */
 998       isva = 1;
 999     }
1000     d = ctype_raw(cts, did);
1001     sz = d->size;
1002 
1003     /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */
1004     if (ctype_isnum(d->info)) {
1005       if (sz > 8) goto err_nyi;
1006       if ((d->info & CTF_FP))
1007         isfp = 1;
1008     } else if (ctype_isvector(d->info)) {
1009       if (CCALL_VECTOR_REG && (sz == 8 || sz == 16))
1010         isfp = 1;
1011       else
1012         goto err_nyi;
1013     } else if (ctype_isstruct(d->info)) {
1014       CCALL_HANDLE_STRUCTARG
1015     } else if (ctype_iscomplex(d->info)) {
1016       CCALL_HANDLE_COMPLEXARG
1017     } else {
1018       sz = CTSIZE_PTR;
1019     }
1020     sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
1021     n = sz / CTSIZE_PTR;  /* Number of GPRs or stack slots needed. */
1022 
1023     CCALL_HANDLE_REGARG  /* Handle register arguments. */
1024 
1025     /* Otherwise pass argument on stack. */
1026     if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) {
1027       MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1;
1028       nsp = (nsp + align) & ~align;  /* Align argument on stack. */
1029     }
1030     if (nsp + n > CCALL_MAXSTACK) {  /* Too many arguments. */
1031     err_nyi:
1032       lj_err_caller(L, LJ_ERR_FFI_NYICALL);
1033     }
1034     dp = &cc->stack[nsp];
1035     nsp += n;
1036     isva = 0;
1037 
1038   done:
1039     if (rp) {  /* Pass by reference. */
1040       gcsteps++;
1041       *(void **)dp = rp;
1042       dp = rp;
1043     }
1044     lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg));
1045     /* Extend passed integers to 32 bits at least. */
1046     if (ctype_isinteger_or_bool(d->info) && d->size < 4) {
1047       if (d->info & CTF_UNSIGNED)
1048         *(uint32_t *)dp = d->size == 1 ? (uint32_t)*(uint8_t *)dp :
1049                                          (uint32_t)*(uint16_t *)dp;
1050       else
1051         *(int32_t *)dp = d->size == 1 ? (int32_t)*(int8_t *)dp :
1052                                         (int32_t)*(int16_t *)dp;
1053     }
1054 #if LJ_TARGET_ARM64 && LJ_BE
1055     if (isfp && d->size == sizeof(float))
1056       ((float *)dp)[1] = ((float *)dp)[0];  /* Floats occupy high slot. */
1057 #endif
1058 #if LJ_TARGET_MIPS64 || (LJ_TARGET_ARM64 && LJ_BE)
1059     if ((ctype_isinteger_or_bool(d->info) || ctype_isenum(d->info)
1060 #if LJ_TARGET_MIPS64
1061          || (isfp && nsp == 0)
1062 #endif
1063          ) && d->size <= 4) {
1064       *(int64_t *)dp = (int64_t)*(int32_t *)dp;  /* Sign-extend to 64 bit. */
1065     }
1066 #endif
1067 #if LJ_TARGET_X64 && LJ_ABI_WIN
1068     if (isva) {  /* Windows/x64 mirrors varargs in both register sets. */
1069       if (nfpr == ngpr)
1070         cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0];
1071       else
1072         cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1];
1073     }
1074 #else
1075     UNUSED(isva);
1076 #endif
1077 #if LJ_TARGET_X64 && !LJ_ABI_WIN
1078     if (isfp == 2 && n == 2 && (uint8_t *)dp == (uint8_t *)&cc->fpr[nfpr-2]) {
1079       cc->fpr[nfpr-1].d[0] = cc->fpr[nfpr-2].d[1];  /* Split complex double. */
1080       cc->fpr[nfpr-2].d[1] = 0;
1081     }
1082 #elif LJ_TARGET_ARM64 || (LJ_TARGET_MIPS64 && !LJ_ABI_SOFTFP)
1083     if (isfp == 2 && (uint8_t *)dp < (uint8_t *)cc->stack) {
1084       /* Split float HFA or complex float into separate registers. */
1085       CTSize i = (sz >> 2) - 1;
1086       do { ((uint64_t *)dp)[i] = ((uint32_t *)dp)[i]; } while (i--);
1087     }
1088 #else
1089     UNUSED(isfp);
1090 #endif
1091   }
1092   if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG);  /* Too few arguments. */
1093 
1094 #if LJ_TARGET_X64 || (LJ_TARGET_PPC && !LJ_ABI_SOFTFP)
1095   cc->nfpr = nfpr;  /* Required for vararg functions. */
1096 #endif
1097   cc->nsp = nsp;
1098   cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR;
1099   if (nsp > CCALL_SPS_FREE)
1100     cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u);
1101   return gcsteps;
1102 }
1103 
1104 /* Get results from C call. */
1105 static int ccall_get_results(lua_State *L, CTState *cts, CType *ct,
1106                              CCallState *cc, int *ret)
1107 {
1108   CType *ctr = ctype_rawchild(cts, ct);
1109   uint8_t *sp = (uint8_t *)&cc->gpr[0];
1110   if (ctype_isvoid(ctr->info)) {
1111     *ret = 0;  /* Zero results. */
1112     return 0;  /* No additional GC step. */
1113   }
1114   *ret = 1;  /* One result. */
1115   if (ctype_isstruct(ctr->info)) {
1116     /* Return cdata object which is already on top of stack. */
1117     if (!cc->retref) {
1118       void *dp = cdataptr(cdataV(L->top-1));  /* Use preallocated object. */
1119       CCALL_HANDLE_STRUCTRET2
1120     }
1121     return 1;  /* One GC step. */
1122   }
1123   if (ctype_iscomplex(ctr->info)) {
1124     /* Return cdata object which is already on top of stack. */
1125     void *dp = cdataptr(cdataV(L->top-1));  /* Use preallocated object. */
1126     CCALL_HANDLE_COMPLEXRET2
1127     return 1;  /* One GC step. */
1128   }
1129   if (LJ_BE && ctr->size < CTSIZE_PTR &&
1130       (ctype_isinteger_or_bool(ctr->info) || ctype_isenum(ctr->info)))
1131     sp += (CTSIZE_PTR - ctr->size);
1132 #if CCALL_NUM_FPR
1133   if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info))
1134     sp = (uint8_t *)&cc->fpr[0];
1135 #endif
1136 #ifdef CCALL_HANDLE_RET
1137   CCALL_HANDLE_RET
1138 #endif
1139   /* No reference types end up here, so there's no need for the CTypeID. */
1140   lua_assert(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info)));
1141   return lj_cconv_tv_ct(cts, ctr, 0, L->top-1, sp);
1142 }
1143 
1144 /* Call C function. */
1145 int lj_ccall_func(lua_State *L, GCcdata *cd)
1146 {
1147   CTState *cts = ctype_cts(L);
1148   CType *ct = ctype_raw(cts, cd->ctypeid);
1149   CTSize sz = CTSIZE_PTR;
1150   if (ctype_isptr(ct->info)) {
1151     sz = ct->size;
1152     ct = ctype_rawchild(cts, ct);
1153   }
1154   if (ctype_isfunc(ct->info)) {
1155     CCallState cc;
1156     int gcsteps, ret;
1157     cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz);
1158     gcsteps = ccall_set_args(L, cts, ct, &cc);
1159     ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab);
1160     cts->cb.slot = ~0u;
1161     lj_vm_ffi_call(&cc);
1162     if (cts->cb.slot != ~0u) {  /* Blacklist function that called a callback. */
1163       TValue tv;
1164       setlightudV(&tv, (void *)cc.func);
1165       setboolV(lj_tab_set(L, cts->miscmap, &tv), 1);
1166     }
1167     ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab);  /* May be reallocated. */
1168     gcsteps += ccall_get_results(L, cts, ct, &cc, &ret);
1169 #if LJ_TARGET_X86 && LJ_ABI_WIN
1170     /* Automatically detect __stdcall and fix up C function declaration. */
1171     if (cc.spadj && ctype_cconv(ct->info) == CTCC_CDECL) {
1172       CTF_INSERT(ct->info, CCONV, CTCC_STDCALL);
1173       lj_trace_abort(G(L));
1174     }
1175 #endif
1176     while (gcsteps-- > 0)
1177       lj_gc_check(L);
1178     return ret;
1179   }
1180   return -1;  /* Not a function. */
1181 }
1182 
1183 #endif

/* [<][>][^][v][top][bottom][index][help] */