src/lj_crecord.c - luajit-2.0-src

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** Trace recorder for C data operations.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #define lj_ffrecord_c
  6. #define LUA_CORE

  7. #include "lj_obj.h"

  8. #if LJ_HASJIT && LJ_HASFFI

  9. #include "lj_err.h"
  10. #include "lj_tab.h"
  11. #include "lj_frame.h"
  12. #include "lj_ctype.h"
  13. #include "lj_cdata.h"
  14. #include "lj_cparse.h"
  15. #include "lj_cconv.h"
  16. #include "lj_carith.h"
  17. #include "lj_clib.h"
  18. #include "lj_ccall.h"
  19. #include "lj_ff.h"
  20. #include "lj_ir.h"
  21. #include "lj_jit.h"
  22. #include "lj_ircall.h"
  23. #include "lj_iropt.h"
  24. #include "lj_trace.h"
  25. #include "lj_record.h"
  26. #include "lj_ffrecord.h"
  27. #include "lj_snap.h"
  28. #include "lj_crecord.h"
  29. #include "lj_dispatch.h"
  30. #include "lj_strfmt.h"

  31. /* Some local macros to save typing. Undef'd at the end. */
  32. #define IR(ref)                        (&J->cur.ir[(ref)])

  33. /* Pass IR on to next optimization in chain (FOLD). */
  34. #define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))

  35. #define emitconv(a, dt, st, flags) \
  36.   emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))

  37. /* -- C type checks ------------------------------------------------------- */

  38. static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)
  39. {
  40.   GCcdata *cd;
  41.   TRef trtypeid;
  42.   if (!tref_iscdata(tr))
  43.     lj_trace_err(J, LJ_TRERR_BADTYPE);
  44.   cd = cdataV(o);
  45.   /* Specialize to the CTypeID. */
  46.   trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID);
  47.   emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid));
  48.   return cd;
  49. }

  50. /* Specialize to the CTypeID held by a cdata constructor. */
  51. static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr)
  52. {
  53.   CTypeID id;
  54.   lua_assert(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID);
  55.   id = *(CTypeID *)cdataptr(cd);
  56.   tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT);
  57.   emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id));
  58.   return id;
  59. }

  60. static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)
  61. {
  62.   if (tref_isstr(tr)) {
  63.     GCstr *s = strV(o);
  64.     CPState cp;
  65.     CTypeID oldtop;
  66.     /* Specialize to the string containing the C type declaration. */
  67.     emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));
  68.     cp.L = J->L;
  69.     cp.cts = ctype_ctsG(J2G(J));
  70.     oldtop = cp.cts->top;
  71.     cp.srcname = strdata(s);
  72.     cp.p = strdata(s);
  73.     cp.param = NULL;
  74.     cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
  75.     if (lj_cparse(&cp) || cp.cts->top > oldtop)  /* Avoid new struct defs. */
  76.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  77.     return cp.val.id;
  78.   } else {
  79.     GCcdata *cd = argv2cdata(J, tr, o);
  80.     return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) :
  81.                                         cd->ctypeid;
  82.   }
  83. }

  84. /* Convert CType to IRType (if possible). */
  85. static IRType crec_ct2irt(CTState *cts, CType *ct)
  86. {
  87.   if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  88.   if (LJ_LIKELY(ctype_isnum(ct->info))) {
  89.     if ((ct->info & CTF_FP)) {
  90.       if (ct->size == sizeof(double))
  91.         return IRT_NUM;
  92.       else if (ct->size == sizeof(float))
  93.         return IRT_FLOAT;
  94.     } else {
  95.       uint32_t b = lj_fls(ct->size);
  96.       if (b <= 3)
  97.         return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0);
  98.     }
  99.   } else if (ctype_isptr(ct->info)) {
  100.     return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
  101.   } else if (ctype_iscomplex(ct->info)) {
  102.     if (ct->size == 2*sizeof(double))
  103.       return IRT_NUM;
  104.     else if (ct->size == 2*sizeof(float))
  105.       return IRT_FLOAT;
  106.   }
  107.   return IRT_CDATA;
  108. }

  109. /* -- Optimized memory fill and copy -------------------------------------- */

  110. /* Maximum length and unroll of inlined copy/fill. */
  111. #define CREC_COPY_MAXUNROLL                16
  112. #define CREC_COPY_MAXLEN                128

  113. #define CREC_FILL_MAXUNROLL                16

  114. /* Number of windowed registers used for optimized memory copy. */
  115. #if LJ_TARGET_X86
  116. #define CREC_COPY_REGWIN                2
  117. #elif LJ_TARGET_PPC || LJ_TARGET_MIPS
  118. #define CREC_COPY_REGWIN                8
  119. #else
  120. #define CREC_COPY_REGWIN                4
  121. #endif

  122. /* List of memory offsets for copy/fill. */
  123. typedef struct CRecMemList {
  124.   CTSize ofs;                /* Offset in bytes. */
  125.   IRType tp;                /* Type of load/store. */
  126.   TRef trofs;                /* TRef of interned offset. */
  127.   TRef trval;                /* TRef of load value. */
  128. } CRecMemList;

  129. /* Generate copy list for element-wise struct copy. */
  130. static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct)
  131. {
  132.   CTypeID fid = ct->sib;
  133.   MSize mlp = 0;
  134.   while (fid) {
  135.     CType *df = ctype_get(cts, fid);
  136.     fid = df->sib;
  137.     if (ctype_isfield(df->info)) {
  138.       CType *cct;
  139.       IRType tp;
  140.       if (!gcref(df->name)) continue/* Ignore unnamed fields. */
  141.       cct = ctype_rawchild(cts, df);  /* Field type. */
  142.       tp = crec_ct2irt(cts, cct);
  143.       if (tp == IRT_CDATA) return 0/* NYI: aggregates. */
  144.       if (mlp >= CREC_COPY_MAXUNROLL) return 0;
  145.       ml[mlp].ofs = df->size;
  146.       ml[mlp].tp = tp;
  147.       mlp++;
  148.       if (ctype_iscomplex(cct->info)) {
  149.         if (mlp >= CREC_COPY_MAXUNROLL) return 0;
  150.         ml[mlp].ofs = df->size + (cct->size >> 1);
  151.         ml[mlp].tp = tp;
  152.         mlp++;
  153.       }
  154.     } else if (!ctype_isconstval(df->info)) {
  155.       /* NYI: bitfields and sub-structures. */
  156.       return 0;
  157.     }
  158.   }
  159.   return mlp;
  160. }

  161. /* Generate unrolled copy list, from highest to lowest step size/alignment. */
  162. static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step,
  163.                               IRType tp)
  164. {
  165.   CTSize ofs = 0;
  166.   MSize mlp = 0;
  167.   if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step);
  168.   do {
  169.     while (ofs + step <= len) {
  170.       if (mlp >= CREC_COPY_MAXUNROLL) return 0;
  171.       ml[mlp].ofs = ofs;
  172.       ml[mlp].tp = tp;
  173.       mlp++;
  174.       ofs += step;
  175.     }
  176.     step >>= 1;
  177.     tp -= 2;
  178.   } while (ofs < len);
  179.   return mlp;
  180. }

  181. /*
  182. ** Emit copy list with windowed loads/stores.
  183. ** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such).
  184. */
  185. static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp,
  186.                            TRef trdst, TRef trsrc)
  187. {
  188.   MSize i, j, rwin = 0;
  189.   for (i = 0, j = 0; i < mlp; ) {
  190.     TRef trofs = lj_ir_kintp(J, ml[i].ofs);
  191.     TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs);
  192.     ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0);
  193.     ml[i].trofs = trofs;
  194.     i++;
  195.     rwin += (LJ_SOFTFP && ml[i].tp == IRT_NUM) ? 2 : 1;
  196.     if (rwin >= CREC_COPY_REGWIN || i >= mlp) {  /* Flush buffered stores. */
  197.       rwin = 0;
  198.       for ( ; j < i; j++) {
  199.         TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs);
  200.         emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval);
  201.       }
  202.     }
  203.   }
  204. }

  205. /* Optimized memory copy. */
  206. static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen,
  207.                       CType *ct)
  208. {
  209.   if (tref_isk(trlen)) {  /* Length must be constant. */
  210.     CRecMemList ml[CREC_COPY_MAXUNROLL];
  211.     MSize mlp = 0;
  212.     CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i;
  213.     IRType tp = IRT_CDATA;
  214.     int needxbar = 0;
  215.     if (len == 0) return/* Shortcut. */
  216.     if (len > CREC_COPY_MAXLEN) goto fallback;
  217.     if (ct) {
  218.       CTState *cts = ctype_ctsG(J2G(J));
  219.       lua_assert(ctype_isarray(ct->info) || ctype_isstruct(ct->info));
  220.       if (ctype_isarray(ct->info)) {
  221.         CType *cct = ctype_rawchild(cts, ct);
  222.         tp = crec_ct2irt(cts, cct);
  223.         if (tp == IRT_CDATA) goto rawcopy;
  224.         step = lj_ir_type_size[tp];
  225.         lua_assert((len & (step-1)) == 0);
  226.       } else if ((ct->info & CTF_UNION)) {
  227.         step = (1u << ctype_align(ct->info));
  228.         goto rawcopy;
  229.       } else {
  230.         mlp = crec_copy_struct(ml, cts, ct);
  231.         goto emitcopy;
  232.       }
  233.     } else {
  234.     rawcopy:
  235.       needxbar = 1;
  236.       if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
  237.         step = CTSIZE_PTR;
  238.     }
  239.     mlp = crec_copy_unroll(ml, len, step, tp);
  240.   emitcopy:
  241.     if (mlp) {
  242.       crec_copy_emit(J, ml, mlp, trdst, trsrc);
  243.       if (needxbar)
  244.         emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
  245.       return;
  246.     }
  247.   }
  248. fallback:
  249.   /* Call memcpy. Always needs a barrier to disable alias analysis. */
  250.   lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen);
  251.   emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
  252. }

  253. /* Generate unrolled fill list, from highest to lowest step size/alignment. */
  254. static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step)
  255. {
  256.   CTSize ofs = 0;
  257.   MSize mlp = 0;
  258.   IRType tp = IRT_U8 + 2*lj_fls(step);
  259.   do {
  260.     while (ofs + step <= len) {
  261.       if (mlp >= CREC_COPY_MAXUNROLL) return 0;
  262.       ml[mlp].ofs = ofs;
  263.       ml[mlp].tp = tp;
  264.       mlp++;
  265.       ofs += step;
  266.     }
  267.     step >>= 1;
  268.     tp -= 2;
  269.   } while (ofs < len);
  270.   return mlp;
  271. }

  272. /*
  273. ** Emit stores for fill list.
  274. ** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such).
  275. */
  276. static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp,
  277.                            TRef trdst, TRef trfill)
  278. {
  279.   MSize i;
  280.   for (i = 0; i < mlp; i++) {
  281.     TRef trofs = lj_ir_kintp(J, ml[i].ofs);
  282.     TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs);
  283.     emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill);
  284.   }
  285. }

  286. /* Optimized memory fill. */
  287. static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill,
  288.                       CTSize step)
  289. {
  290.   if (tref_isk(trlen)) {  /* Length must be constant. */
  291.     CRecMemList ml[CREC_FILL_MAXUNROLL];
  292.     MSize mlp;
  293.     CTSize len = (CTSize)IR(tref_ref(trlen))->i;
  294.     if (len == 0) return/* Shortcut. */
  295.     if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
  296.       step = CTSIZE_PTR;
  297.     if (step * CREC_FILL_MAXUNROLL < len) goto fallback;
  298.     mlp = crec_fill_unroll(ml, len, step);
  299.     if (!mlp) goto fallback;
  300.     if (tref_isk(trfill) || ml[0].tp != IRT_U8)
  301.       trfill = emitconv(trfill, IRT_INT, IRT_U8, 0);
  302.     if (ml[0].tp != IRT_U8) {  /* Scatter U8 to U16/U32/U64. */
  303.       if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) {
  304.         if (tref_isk(trfill))  /* Pointless on x64 with zero-extended regs. */
  305.           trfill = emitconv(trfill, IRT_U64, IRT_U32, 0);
  306.         trfill = emitir(IRT(IR_MUL, IRT_U64), trfill,
  307.                         lj_ir_kint64(J, U64x(01010101,01010101)));
  308.       } else {
  309.         trfill = emitir(IRTI(IR_MUL), trfill,
  310.                    lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101));
  311.       }
  312.     }
  313.     crec_fill_emit(J, ml, mlp, trdst, trfill);
  314.   } else {
  315. fallback:
  316.     /* Call memset. Always needs a barrier to disable alias analysis. */
  317.     lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen);  /* Note: arg order! */
  318.   }
  319.   emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
  320. }

  321. /* -- Convert C type to C type -------------------------------------------- */

  322. /*
  323. ** This code mirrors the code in lj_cconv.c. It performs the same steps
  324. ** for the trace recorder that lj_cconv.c does for the interpreter.
  325. **
  326. ** One major difference is that we can get away with much fewer checks
  327. ** here. E.g. checks for casts, constness or correct types can often be
  328. ** omitted, even if they might fail. The interpreter subsequently throws
  329. ** an error, which aborts the trace.
  330. **
  331. ** All operations are specialized to their C types, so the on-trace
  332. ** outcome must be the same as the outcome in the interpreter. If the
  333. ** interpreter doesn't throw an error, then the trace is correct, too.
  334. ** Care must be taken not to generate invalid (temporary) IR or to
  335. ** trigger asserts.
  336. */

  337. /* Determine whether a passed number or cdata number is non-zero. */
  338. static int crec_isnonzero(CType *s, void *p)
  339. {
  340.   if (p == (void *)0)
  341.     return 0;
  342.   if (p == (void *)1)
  343.     return 1;
  344.   if ((s->info & CTF_FP)) {
  345.     if (s->size == sizeof(float))
  346.       return (*(float *)p != 0);
  347.     else
  348.       return (*(double *)p != 0);
  349.   } else {
  350.     if (s->size == 1)
  351.       return (*(uint8_t *)p != 0);
  352.     else if (s->size == 2)
  353.       return (*(uint16_t *)p != 0);
  354.     else if (s->size == 4)
  355.       return (*(uint32_t *)p != 0);
  356.     else
  357.       return (*(uint64_t *)p != 0);
  358.   }
  359. }

  360. static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp,
  361.                        void *svisnz)
  362. {
  363.   IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d);
  364.   IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s);
  365.   CTSize dsize = d->size, ssize = s->size;
  366.   CTInfo dinfo = d->info, sinfo = s->info;

  367.   if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
  368.     goto err_conv;

  369.   /*
  370.   ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
  371.   ** numbers up to 8 bytes. Otherwise sp holds a pointer.
  372.   */

  373.   switch (cconv_idx2(dinfo, sinfo)) {
  374.   /* Destination is a bool. */
  375.   case CCX(B, B):
  376.     goto xstore;  /* Source operand is already normalized. */
  377.   case CCX(B, I):
  378.   case CCX(B, F):
  379.     if (st != IRT_CDATA) {
  380.       /* Specialize to the result of a comparison against 0. */
  381.       TRef zero = (st == IRT_NUM  || st == IRT_FLOAT) ? lj_ir_knum(J, 0) :
  382.                   (st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) :
  383.                   lj_ir_kint(J, 0);
  384.       int isnz = crec_isnonzero(s, svisnz);
  385.       emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero);
  386.       sp = lj_ir_kint(J, isnz);
  387.       goto xstore;
  388.     }
  389.     goto err_nyi;

  390.   /* Destination is an integer. */
  391.   case CCX(I, B):
  392.   case CCX(I, I):
  393.   conv_I_I:
  394.     if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
  395.     /* Extend 32 to 64 bit integer. */
  396.     if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED)))
  397.       sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st,
  398.                     (sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
  399.     else if (dsize < 8 && ssize == 8/* Truncate from 64 bit integer. */
  400.       sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0);
  401.     else if (st == IRT_INT)
  402.       sp = lj_opt_narrow_toint(J, sp);
  403.   xstore:
  404.     if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J);
  405.     if (dp == 0) return sp;
  406.     emitir(IRT(IR_XSTORE, dt), dp, sp);
  407.     break;
  408.   case CCX(I, C):
  409.     sp = emitir(IRT(IR_XLOAD, st), sp, 0);  /* Load re. */
  410.     /* fallthrough */
  411.   case CCX(I, F):
  412.     if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
  413.     sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_ANY);
  414.     goto xstore;
  415.   case CCX(I, P):
  416.   case CCX(I, A):
  417.     sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
  418.     ssize = CTSIZE_PTR;
  419.     st = IRT_UINTP;
  420.     if (((dsize ^ ssize) & 8) == 0) {  /* Must insert no-op type conversion. */
  421.       sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, IRT_PTR, 0);
  422.       goto xstore;
  423.     }
  424.     goto conv_I_I;

  425.   /* Destination is a floating-point number. */
  426.   case CCX(F, B):
  427.   case CCX(F, I):
  428.   conv_F_I:
  429.     if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
  430.     sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0);
  431.     goto xstore;
  432.   case CCX(F, C):
  433.     sp = emitir(IRT(IR_XLOAD, st), sp, 0);  /* Load re. */
  434.     /* fallthrough */
  435.   case CCX(F, F):
  436.   conv_F_F:
  437.     if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
  438.     if (dt != st) sp = emitconv(sp, dt, st, 0);
  439.     goto xstore;

  440.   /* Destination is a complex number. */
  441.   case CCX(C, I):
  442.   case CCX(C, F):
  443.     {  /* Clear im. */
  444.       TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
  445.       emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0));
  446.     }
  447.     /* Convert to re. */
  448.     if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I;

  449.   case CCX(C, C):
  450.     if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
  451.     {
  452.       TRef re, im, ptr;
  453.       re = emitir(IRT(IR_XLOAD, st), sp, 0);
  454.       ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1)));
  455.       im = emitir(IRT(IR_XLOAD, st), ptr, 0);
  456.       if (dt != st) {
  457.         re = emitconv(re, dt, st, 0);
  458.         im = emitconv(im, dt, st, 0);
  459.       }
  460.       emitir(IRT(IR_XSTORE, dt), dp, re);
  461.       ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
  462.       emitir(IRT(IR_XSTORE, dt), ptr, im);
  463.     }
  464.     break;

  465.   /* Destination is a vector. */
  466.   case CCX(V, I):
  467.   case CCX(V, F):
  468.   case CCX(V, C):
  469.   case CCX(V, V):
  470.     goto err_nyi;

  471.   /* Destination is a pointer. */
  472.   case CCX(P, P):
  473.   case CCX(P, A):
  474.   case CCX(P, S):
  475.     /* There are only 32 bit pointers/addresses on 32 bit machines.
  476.     ** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
  477.     */
  478.     goto xstore;
  479.   case CCX(P, I):
  480.     if (st == IRT_CDATA) goto err_nyi;
  481.     if (!LJ_64 && ssize == 8/* Truncate from 64 bit integer. */
  482.       sp = emitconv(sp, IRT_U32, st, 0);
  483.     goto xstore;
  484.   case CCX(P, F):
  485.     if (st == IRT_CDATA) goto err_nyi;
  486.     /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
  487.     sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32,
  488.                   st, IRCONV_ANY);
  489.     goto xstore;

  490.   /* Destination is an array. */
  491.   case CCX(A, A):
  492.   /* Destination is a struct/union. */
  493.   case CCX(S, S):
  494.     if (dp == 0) goto err_conv;
  495.     crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d);
  496.     break;

  497.   default:
  498.   err_conv:
  499.   err_nyi:
  500.     lj_trace_err(J, LJ_TRERR_NYICONV);
  501.     break;
  502.   }
  503.   return 0;
  504. }

  505. /* -- Convert C type to TValue (load) ------------------------------------- */

  506. static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)
  507. {
  508.   CTState *cts = ctype_ctsG(J2G(J));
  509.   IRType t = crec_ct2irt(cts, s);
  510.   CTInfo sinfo = s->info;
  511.   if (ctype_isnum(sinfo)) {
  512.     TRef tr;
  513.     if (t == IRT_CDATA)
  514.       goto err_nyi;  /* NYI: copyval of >64 bit integers. */
  515.     tr = emitir(IRT(IR_XLOAD, t), sp, 0);
  516.     if (t == IRT_FLOAT || t == IRT_U32) {  /* Keep uint32_t/float as numbers. */
  517.       return emitconv(tr, IRT_NUM, t, 0);
  518.     } else if (t == IRT_I64 || t == IRT_U64) {  /* Box 64 bit integer. */
  519.       sp = tr;
  520.       lj_needsplit(J);
  521.     } else if ((sinfo & CTF_BOOL)) {
  522.       /* Assume not equal to zero. Fixup and emit pending guard later. */
  523.       lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
  524.       J->postproc = LJ_POST_FIXGUARD;
  525.       return TREF_TRUE;
  526.     } else {
  527.       return tr;
  528.     }
  529.   } else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) {
  530.     sp = emitir(IRT(IR_XLOAD, t), sp, 0);  /* Box pointers and enums. */
  531.   } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
  532.     cts->L = J->L;
  533.     sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR);  /* Create ref. */
  534.   } else if (ctype_iscomplex(sinfo)) {  /* Unbox/box complex. */
  535.     ptrdiff_t esz = (ptrdiff_t)(s->size >> 1);
  536.     TRef ptr, tr1, tr2, dp;
  537.     dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL);
  538.     tr1 = emitir(IRT(IR_XLOAD, t), sp, 0);
  539.     ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz));
  540.     tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0);
  541.     ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)));
  542.     emitir(IRT(IR_XSTORE, t), ptr, tr1);
  543.     ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz));
  544.     emitir(IRT(IR_XSTORE, t), ptr, tr2);
  545.     return dp;
  546.   } else {
  547.     /* NYI: copyval of vectors. */
  548.   err_nyi:
  549.     lj_trace_err(J, LJ_TRERR_NYICONV);
  550.   }
  551.   /* Box pointer, ref, enum or 64 bit integer. */
  552.   return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp);
  553. }

  554. /* -- Convert TValue to C type (store) ------------------------------------ */

  555. static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval)
  556. {
  557.   CTState *cts = ctype_ctsG(J2G(J));
  558.   CTypeID sid = CTID_P_VOID;
  559.   void *svisnz = 0;
  560.   CType *s;
  561.   if (LJ_LIKELY(tref_isinteger(sp))) {
  562.     sid = CTID_INT32;
  563.     svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
  564.   } else if (tref_isnum(sp)) {
  565.     sid = CTID_DOUBLE;
  566.     svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
  567.   } else if (tref_isbool(sp)) {
  568.     sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0);
  569.     sid = CTID_BOOL;
  570.   } else if (tref_isnil(sp)) {
  571.     sp = lj_ir_kptr(J, NULL);
  572.   } else if (tref_isudata(sp)) {
  573.     GCudata *ud = udataV(sval);
  574.     if (ud->udtype == UDTYPE_IO_FILE) {
  575.       TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE);
  576.       emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE));
  577.       sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_UDATA_FILE);
  578.     } else {
  579.       sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCudata)));
  580.     }
  581.   } else if (tref_isstr(sp)) {
  582.     if (ctype_isenum(d->info)) {  /* Match string against enum constant. */
  583.       GCstr *str = strV(sval);
  584.       CTSize ofs;
  585.       CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
  586.       /* Specialize to the name of the enum constant. */
  587.       emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str));
  588.       if (cct && ctype_isconstval(cct->info)) {
  589.         lua_assert(ctype_child(cts, cct)->size == 4);
  590.         svisnz = (void *)(intptr_t)(ofs != 0);
  591.         sp = lj_ir_kint(J, (int32_t)ofs);
  592.         sid = ctype_cid(cct->info);
  593.       }  /* else: interpreter will throw. */
  594.     } else if (ctype_isrefarray(d->info)) {  /* Copy string to array. */
  595.       lj_trace_err(J, LJ_TRERR_BADTYPE);  /* NYI */
  596.     } else/* Otherwise pass the string data as a const char[]. */
  597.       /* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */
  598.       sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCstr)));
  599.       sid = CTID_A_CCHAR;
  600.     }
  601.   } else if (tref_islightud(sp)) {
  602. #if LJ_64
  603.     sp = emitir(IRT(IR_BAND, IRT_P64), sp,
  604.                 lj_ir_kint64(J, U64x(00007fff,ffffffff)));
  605. #endif
  606.   } else/* NYI: tref_istab(sp). */
  607.     IRType t;
  608.     sid = argv2cdata(J, sp, sval)->ctypeid;
  609.     s = ctype_raw(cts, sid);
  610.     svisnz = cdataptr(cdataV(sval));
  611.     if (ctype_isfunc(s->info)) {
  612.       sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);
  613.       s = ctype_get(cts, sid);
  614.       t = IRT_PTR;
  615.     } else {
  616.       t = crec_ct2irt(cts, s);
  617.     }
  618.     if (ctype_isptr(s->info)) {
  619.       sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR);
  620.       if (ctype_isref(s->info)) {
  621.         svisnz = *(void **)svisnz;
  622.         s = ctype_rawchild(cts, s);
  623.         if (ctype_isenum(s->info)) s = ctype_child(cts, s);
  624.         t = crec_ct2irt(cts, s);
  625.       } else {
  626.         goto doconv;
  627.       }
  628.     } else if (t == IRT_I64 || t == IRT_U64) {
  629.       sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64);
  630.       lj_needsplit(J);
  631.       goto doconv;
  632.     } else if (t == IRT_INT || t == IRT_U32) {
  633.       if (ctype_isenum(s->info)) s = ctype_child(cts, s);
  634.       sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT);
  635.       goto doconv;
  636.     } else {
  637.       sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));
  638.     }
  639.     if (ctype_isnum(s->info) && t != IRT_CDATA)
  640.       sp = emitir(IRT(IR_XLOAD, t), sp, 0);  /* Load number value. */
  641.     goto doconv;
  642.   }
  643.   s = ctype_get(cts, sid);
  644. doconv:
  645.   if (ctype_isenum(d->info)) d = ctype_child(cts, d);
  646.   return crec_ct_ct(J, d, s, dp, sp, svisnz);
  647. }

  648. /* -- C data metamethods -------------------------------------------------- */

  649. /* This would be rather difficult in FOLD, so do it here:
  650. ** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
  651. ** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
  652. */
  653. static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz)
  654. {
  655.   IRIns *ir = IR(tref_ref(tr));
  656.   if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) &&
  657.       (ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) {
  658.     IRIns *irk = IR(ir->op2);
  659.     ptrdiff_t k;
  660.     if (LJ_64 && irk->o == IR_KINT64)
  661.       k = (ptrdiff_t)ir_kint64(irk)->u64 * sz;
  662.     else
  663.       k = (ptrdiff_t)irk->i * sz;
  664.     if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k;
  665.     tr = ir->op1;  /* Not a TRef, but the caller doesn't care. */
  666.   }
  667.   return tr;
  668. }

  669. /* Record ctype __index/__newindex metamethods. */
  670. static void crec_index_meta(jit_State *J, CTState *cts, CType *ct,
  671.                             RecordFFData *rd)
  672. {
  673.   CTypeID id = ctype_typeid(cts, ct);
  674.   cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index);
  675.   if (!tv)
  676.     lj_trace_err(J, LJ_TRERR_BADTYPE);
  677.   if (tvisfunc(tv)) {
  678.     J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME;
  679.     rd->nres = -1/* Pending tailcall. */
  680.   } else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) {
  681.     /* Specialize to result of __index lookup. */
  682.     cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]);
  683.     J->base[0] = lj_record_constify(J, o);
  684.     if (!J->base[0])
  685.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  686.     /* Always specialize to the key. */
  687.     emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));
  688.   } else {
  689.     /* NYI: resolving of non-function metamethods. */
  690.     /* NYI: non-string keys for __index table. */
  691.     /* NYI: stores to __newindex table. */
  692.     lj_trace_err(J, LJ_TRERR_BADTYPE);
  693.   }
  694. }

  695. void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
  696. {
  697.   TRef idx, ptr = J->base[0];
  698.   ptrdiff_t ofs = sizeof(GCcdata);
  699.   GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]);
  700.   CTState *cts = ctype_ctsG(J2G(J));
  701.   CType *ct = ctype_raw(cts, cd->ctypeid);
  702.   CTypeID sid = 0;

  703.   /* Resolve pointer or reference for cdata object. */
  704.   if (ctype_isptr(ct->info)) {
  705.     IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
  706.     if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
  707.     ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR);
  708.     ofs = 0;
  709.     ptr = crec_reassoc_ofs(J, ptr, &ofs, 1);
  710.   }

  711. again:
  712.   idx = J->base[1];
  713.   if (tref_isnumber(idx)) {
  714.     idx = lj_opt_narrow_cindex(J, idx);
  715.     if (ctype_ispointer(ct->info)) {
  716.       CTSize sz;
  717.   integer_key:
  718.       if ((ct->info & CTF_COMPLEX))
  719.         idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1));
  720.       sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info)));
  721.       idx = crec_reassoc_ofs(J, idx, &ofs, sz);
  722. #if LJ_TARGET_ARM || LJ_TARGET_PPC
  723.       /* Hoist base add to allow fusion of index/shift into operands. */
  724.       if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs
  725. #if LJ_TARGET_ARM
  726.           && (sz == 1 || sz == 4)
  727. #endif
  728.           ) {
  729.         ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
  730.         ofs = 0;
  731.       }
  732. #endif
  733.       idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
  734.       ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);
  735.     }
  736.   } else if (tref_iscdata(idx)) {
  737.     GCcdata *cdk = cdataV(&rd->argv[1]);
  738.     CType *ctk = ctype_raw(cts, cdk->ctypeid);
  739.     IRType t = crec_ct2irt(cts, ctk);
  740.     if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) {
  741.       if (ctk->size == 8) {
  742.         idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64);
  743.       } else if (ctk->size == 4) {
  744.         idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT);
  745.       } else {
  746.         idx = emitir(IRT(IR_ADD, IRT_PTR), idx,
  747.                      lj_ir_kintp(J, sizeof(GCcdata)));
  748.         idx = emitir(IRT(IR_XLOAD, t), idx, 0);
  749.       }
  750.       if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED))
  751.         idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT);
  752.       if (!LJ_64 && ctk->size > sizeof(intptr_t)) {
  753.         idx = emitconv(idx, IRT_INTP, t, 0);
  754.         lj_needsplit(J);
  755.       }
  756.       goto integer_key;
  757.     }
  758.   } else if (tref_isstr(idx)) {
  759.     GCstr *name = strV(&rd->argv[1]);
  760.     if (cd && cd->ctypeid == CTID_CTYPEID)
  761.       ct = ctype_raw(cts, crec_constructor(J, cd, ptr));
  762.     if (ctype_isstruct(ct->info)) {
  763.       CTSize fofs;
  764.       CType *fct;
  765.       fct = lj_ctype_getfield(cts, ct, name, &fofs);
  766.       if (fct) {
  767.         /* Always specialize to the field name. */
  768.         emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
  769.         if (ctype_isconstval(fct->info)) {
  770.           if (fct->size >= 0x80000000u &&
  771.               (ctype_child(cts, fct)->info & CTF_UNSIGNED)) {
  772.             J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size);
  773.             return;
  774.           }
  775.           J->base[0] = lj_ir_kint(J, (int32_t)fct->size);
  776.           return/* Interpreter will throw for newindex. */
  777.         } else if (ctype_isbitfield(fct->info)) {
  778.           lj_trace_err(J, LJ_TRERR_NYICONV);
  779.         } else {
  780.           lua_assert(ctype_isfield(fct->info));
  781.           sid = ctype_cid(fct->info);
  782.         }
  783.         ofs += (ptrdiff_t)fofs;
  784.       }
  785.     } else if (ctype_iscomplex(ct->info)) {
  786.       if (name->len == 2 &&
  787.           ((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') ||
  788.            (strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) {
  789.         /* Always specialize to the field name. */
  790.         emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
  791.         if (strdata(name)[0] == 'i') ofs += (ct->size >> 1);
  792.         sid = ctype_cid(ct->info);
  793.       }
  794.     }
  795.   }
  796.   if (!sid) {
  797.     if (ctype_isptr(ct->info)) {  /* Automatically perform '->'. */
  798.       CType *cct = ctype_rawchild(cts, ct);
  799.       if (ctype_isstruct(cct->info)) {
  800.         ct = cct;
  801.         cd = NULL;
  802.         if (tref_isstr(idx)) goto again;
  803.       }
  804.     }
  805.     crec_index_meta(J, cts, ct, rd);
  806.     return;
  807.   }

  808.   if (ofs)
  809.     ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));

  810.   /* Resolve reference for field. */
  811.   ct = ctype_get(cts, sid);
  812.   if (ctype_isref(ct->info)) {
  813.     ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0);
  814.     sid = ctype_cid(ct->info);
  815.     ct = ctype_get(cts, sid);
  816.   }

  817.   while (ctype_isattrib(ct->info))
  818.     ct = ctype_child(cts, ct);  /* Skip attributes. */

  819.   if (rd->data == 0) {  /* __index metamethod. */
  820.     J->base[0] = crec_tv_ct(J, ct, sid, ptr);
  821.   } else/* __newindex metamethod. */
  822.     rd->nres = 0;
  823.     J->needsnap = 1;
  824.     crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
  825.   }
  826. }

  827. /* Record setting a finalizer. */
  828. static void crec_finalizer(jit_State *J, TRef trcd, TRef trfin, cTValue *fin)
  829. {
  830.   if (tvisgcv(fin)) {
  831.     if (!trfin) trfin = lj_ir_kptr(J, gcval(fin));
  832.   } else if (tvisnil(fin)) {
  833.     trfin = lj_ir_kptr(J, NULL);
  834.   } else {
  835.     lj_trace_err(J, LJ_TRERR_BADTYPE);
  836.   }
  837.   lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd,
  838.              trfin, lj_ir_kint(J, (int32_t)itype(fin)));
  839.   J->needsnap = 1;
  840. }

  841. /* Record cdata allocation. */
  842. static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)
  843. {
  844.   CTState *cts = ctype_ctsG(J2G(J));
  845.   CTSize sz;
  846.   CTInfo info = lj_ctype_info(cts, id, &sz);
  847.   CType *d = ctype_raw(cts, id);
  848.   TRef trcd, trid = lj_ir_kint(J, id);
  849.   cTValue *fin;
  850.   /* Use special instruction to box pointer or 32/64 bit integer. */
  851.   if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) {
  852.     TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) :
  853.               ctype_isptr(info) ? lj_ir_kptr(J, NULL) :
  854.               sz == 4 ? lj_ir_kint(J, 0) :
  855.               (lj_needsplit(J), lj_ir_kint64(J, 0));
  856.     J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp);
  857.     return;
  858.   } else {
  859.     TRef trsz = TREF_NIL;
  860.     if ((info & CTF_VLA)) {  /* Calculate VLA/VLS size at runtime. */
  861.       CTSize sz0, sz1;
  862.       if (!J->base[1] || J->base[2])
  863.         lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init VLA/VLS. */
  864.       trsz = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0,
  865.                         J->base[1], &rd->argv[1]);
  866.       sz0 = lj_ctype_vlsize(cts, d, 0);
  867.       sz1 = lj_ctype_vlsize(cts, d, 1);
  868.       trsz = emitir(IRTGI(IR_MULOV), trsz, lj_ir_kint(J, (int32_t)(sz1-sz0)));
  869.       trsz = emitir(IRTGI(IR_ADDOV), trsz, lj_ir_kint(J, (int32_t)sz0));
  870.       J->base[1] = 0/* Simplify logic below. */
  871.     } else if (ctype_align(info) > CT_MEMALIGN) {
  872.       trsz = lj_ir_kint(J, sz);
  873.     }
  874.     trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, trsz);
  875.     if (sz > 128 || (info & CTF_VLA)) {
  876.       TRef dp;
  877.       CTSize align;
  878.     special:  /* Only handle bulk zero-fill for large/VLA/VLS types. */
  879.       if (J->base[1])
  880.         lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init large/VLA/VLS types. */
  881.       dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
  882.       if (trsz == TREF_NIL) trsz = lj_ir_kint(J, sz);
  883.       align = ctype_align(info);
  884.       if (align < CT_MEMALIGN) align = CT_MEMALIGN;
  885.       crec_fill(J, dp, trsz, lj_ir_kint(J, 0), (1u << align));
  886.     } else if (J->base[1] && !J->base[2] &&
  887.         !lj_cconv_multi_init(cts, d, &rd->argv[1])) {
  888.       goto single_init;
  889.     } else if (ctype_isarray(d->info)) {
  890.       CType *dc = ctype_rawchild(cts, d);  /* Array element type. */
  891.       CTSize ofs, esize = dc->size;
  892.       TRef sp = 0;
  893.       TValue tv;
  894.       TValue *sval = &tv;
  895.       MSize i;
  896.       tv.u64 = 0;
  897.       if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)) ||
  898.           esize * CREC_FILL_MAXUNROLL < sz)
  899.         goto special;
  900.       for (i = 1, ofs = 0; ofs < sz; ofs += esize) {
  901.         TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
  902.                          lj_ir_kintp(J, ofs + sizeof(GCcdata)));
  903.         if (J->base[i]) {
  904.           sp = J->base[i];
  905.           sval = &rd->argv[i];
  906.           i++;
  907.         } else if (i != 2) {
  908.           sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
  909.         }
  910.         crec_ct_tv(J, dc, dp, sp, sval);
  911.       }
  912.     } else if (ctype_isstruct(d->info)) {
  913.       CTypeID fid = d->sib;
  914.       MSize i = 1;
  915.       while (fid) {
  916.         CType *df = ctype_get(cts, fid);
  917.         fid = df->sib;
  918.         if (ctype_isfield(df->info)) {
  919.           CType *dc;
  920.           TRef sp, dp;
  921.           TValue tv;
  922.           TValue *sval = &tv;
  923.           setintV(&tv, 0);
  924.           if (!gcref(df->name)) continue/* Ignore unnamed fields. */
  925.           dc = ctype_rawchild(cts, df);  /* Field type. */
  926.           if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) ||
  927.                 ctype_isenum(dc->info)))
  928.             lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init aggregates. */
  929.           if (J->base[i]) {
  930.             sp = J->base[i];
  931.             sval = &rd->argv[i];
  932.             i++;
  933.           } else {
  934.             sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0);
  935.           }
  936.           dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
  937.                       lj_ir_kintp(J, df->size + sizeof(GCcdata)));
  938.           crec_ct_tv(J, dc, dp, sp, sval);
  939.         } else if (!ctype_isconstval(df->info)) {
  940.           /* NYI: init bitfields and sub-structures. */
  941.           lj_trace_err(J, LJ_TRERR_NYICONV);
  942.         }
  943.       }
  944.     } else {
  945.       TRef dp;
  946.     single_init:
  947.       dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
  948.       if (J->base[1]) {
  949.         crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]);
  950.       } else {
  951.         TValue tv;
  952.         tv.u64 = 0;
  953.         crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv);
  954.       }
  955.     }
  956.   }
  957.   J->base[0] = trcd;
  958.   /* Handle __gc metamethod. */
  959.   fin = lj_ctype_meta(cts, id, MM_gc);
  960.   if (fin)
  961.     crec_finalizer(J, trcd, 0, fin);
  962. }

  963. /* Record argument conversions. */
  964. static TRef crec_call_args(jit_State *J, RecordFFData *rd,
  965.                            CTState *cts, CType *ct)
  966. {
  967.   TRef args[CCI_NARGS_MAX];
  968.   CTypeID fid;
  969.   MSize i, n;
  970.   TRef tr, *base;
  971.   cTValue *o;
  972. #if LJ_TARGET_X86
  973. #if LJ_ABI_WIN
  974.   TRef *arg0 = NULL, *arg1 = NULL;
  975. #endif
  976.   int ngpr = 0;
  977.   if (ctype_cconv(ct->info) == CTCC_THISCALL)
  978.     ngpr = 1;
  979.   else if (ctype_cconv(ct->info) == CTCC_FASTCALL)
  980.     ngpr = 2;
  981. #endif

  982.   /* Skip initial attributes. */
  983.   fid = ct->sib;
  984.   while (fid) {
  985.     CType *ctf = ctype_get(cts, fid);
  986.     if (!ctype_isattrib(ctf->info)) break;
  987.     fid = ctf->sib;
  988.   }
  989.   args[0] = TREF_NIL;
  990.   for (n = 0, base = J->base+1, o = rd->argv+1; *base; n++, base++, o++) {
  991.     CTypeID did;
  992.     CType *d;

  993.     if (n >= CCI_NARGS_MAX)
  994.       lj_trace_err(J, LJ_TRERR_NYICALL);

  995.     if (fid) {  /* Get argument type from field. */
  996.       CType *ctf = ctype_get(cts, fid);
  997.       fid = ctf->sib;
  998.       lua_assert(ctype_isfield(ctf->info));
  999.       did = ctype_cid(ctf->info);
  1000.     } else {
  1001.       if (!(ct->info & CTF_VARARG))
  1002.         lj_trace_err(J, LJ_TRERR_NYICALL);  /* Too many arguments. */
  1003.       did = lj_ccall_ctid_vararg(cts, o);  /* Infer vararg type. */
  1004.     }
  1005.     d = ctype_raw(cts, did);
  1006.     if (!(ctype_isnum(d->info) || ctype_isptr(d->info) ||
  1007.           ctype_isenum(d->info)))
  1008.       lj_trace_err(J, LJ_TRERR_NYICALL);
  1009.     tr = crec_ct_tv(J, d, 0, *base, o);
  1010.     if (ctype_isinteger_or_bool(d->info)) {
  1011.       if (d->size < 4) {
  1012.         if ((d->info & CTF_UNSIGNED))
  1013.           tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_U8 : IRT_U16, 0);
  1014.         else
  1015.           tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_I8 : IRT_I16,IRCONV_SEXT);
  1016.       }
  1017.     } else if (LJ_SOFTFP && ctype_isfp(d->info) && d->size > 4) {
  1018.       lj_needsplit(J);
  1019.     }
  1020. #if LJ_TARGET_X86
  1021.     /* 64 bit args must not end up in registers for fastcall/thiscall. */
  1022. #if LJ_ABI_WIN
  1023.     if (!ctype_isfp(d->info)) {
  1024.       /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */
  1025.       if (tref_typerange(tr, IRT_I64, IRT_U64)) {
  1026.         if (ngpr) {
  1027.           arg0 = &args[n]; args[n++] = TREF_NIL; ngpr--;
  1028.           if (ngpr) {
  1029.             arg1 = &args[n]; args[n++] = TREF_NIL; ngpr--;
  1030.           }
  1031.         }
  1032.       } else {
  1033.         if (arg0) { *arg0 = tr; arg0 = NULL; n--; continue; }
  1034.         if (arg1) { *arg1 = tr; arg1 = NULL; n--; continue; }
  1035.         if (ngpr) ngpr--;
  1036.       }
  1037.     }
  1038. #else
  1039.     if (!ctype_isfp(d->info) && ngpr) {
  1040.       if (tref_typerange(tr, IRT_I64, IRT_U64)) {
  1041.         /* No reordering for other x86 ABIs. Simply add alignment args. */
  1042.         do { args[n++] = TREF_NIL; } while (--ngpr);
  1043.       } else {
  1044.         ngpr--;
  1045.       }
  1046.     }
  1047. #endif
  1048. #endif
  1049.     args[n] = tr;
  1050.   }
  1051.   tr = args[0];
  1052.   for (i = 1; i < n; i++)
  1053.     tr = emitir(IRT(IR_CARG, IRT_NIL), tr, args[i]);
  1054.   return tr;
  1055. }

  1056. /* Create a snapshot for the caller, simulating a 'false' return value. */
  1057. static void crec_snap_caller(jit_State *J)
  1058. {
  1059.   lua_State *L = J->L;
  1060.   TValue *base = L->base, *top = L->top;
  1061.   const BCIns *pc = J->pc;
  1062.   TRef ftr = J->base[-1];
  1063.   ptrdiff_t delta;
  1064.   if (!frame_islua(base-1) || J->framedepth <= 0)
  1065.     lj_trace_err(J, LJ_TRERR_NYICALL);
  1066.   J->pc = frame_pc(base-1); delta = 1+LJ_FR2+bc_a(J->pc[-1]);
  1067.   L->top = base; L->base = base - delta;
  1068.   J->base[-1] = TREF_FALSE;
  1069.   J->base -= delta; J->baseslot -= (BCReg)delta;
  1070.   J->maxslot = (BCReg)delta; J->framedepth--;
  1071.   lj_snap_add(J);
  1072.   L->base = base; L->top = top;
  1073.   J->framedepth++; J->maxslot = 1;
  1074.   J->base += delta; J->baseslot += (BCReg)delta;
  1075.   J->base[-1] = ftr; J->pc = pc;
  1076. }

  1077. /* Record function call. */
  1078. static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd)
  1079. {
  1080.   CTState *cts = ctype_ctsG(J2G(J));
  1081.   CType *ct = ctype_raw(cts, cd->ctypeid);
  1082.   IRType tp = IRT_PTR;
  1083.   if (ctype_isptr(ct->info)) {
  1084.     tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
  1085.     ct = ctype_rawchild(cts, ct);
  1086.   }
  1087.   if (ctype_isfunc(ct->info)) {
  1088.     TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR);
  1089.     CType *ctr = ctype_rawchild(cts, ct);
  1090.     IRType t = crec_ct2irt(cts, ctr);
  1091.     TRef tr;
  1092.     TValue tv;
  1093.     /* Check for blacklisted C functions that might call a callback. */
  1094.     setlightudV(&tv,
  1095.                 cdata_getptr(cdataptr(cd), (LJ_64 && tp == IRT_P64) ? 8 : 4));
  1096.     if (tvistrue(lj_tab_get(J->L, cts->miscmap, &tv)))
  1097.       lj_trace_err(J, LJ_TRERR_BLACKL);
  1098.     if (ctype_isvoid(ctr->info)) {
  1099.       t = IRT_NIL;
  1100.       rd->nres = 0;
  1101.     } else if (!(ctype_isnum(ctr->info) || ctype_isptr(ctr->info) ||
  1102.                  ctype_isenum(ctr->info)) || t == IRT_CDATA) {
  1103.       lj_trace_err(J, LJ_TRERR_NYICALL);
  1104.     }
  1105.     if ((ct->info & CTF_VARARG)
  1106. #if LJ_TARGET_X86
  1107.         || ctype_cconv(ct->info) != CTCC_CDECL
  1108. #endif
  1109.         )
  1110.       func = emitir(IRT(IR_CARG, IRT_NIL), func,
  1111.                     lj_ir_kint(J, ctype_typeid(cts, ct)));
  1112.     tr = emitir(IRT(IR_CALLXS, t), crec_call_args(J, rd, cts, ct), func);
  1113.     if (ctype_isbool(ctr->info)) {
  1114.       if (frame_islua(J->L->base-1) && bc_b(frame_pc(J->L->base-1)[-1]) == 1) {
  1115.         /* Don't check result if ignored. */
  1116.         tr = TREF_NIL;
  1117.       } else {
  1118.         crec_snap_caller(J);
  1119. #if LJ_TARGET_X86ORX64
  1120.         /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */
  1121.         lj_ir_set(J, IRTG(IR_NE, IRT_U8), tr, lj_ir_kint(J, 0));
  1122. #else
  1123.         lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
  1124. #endif
  1125.         J->postproc = LJ_POST_FIXGUARDSNAP;
  1126.         tr = TREF_TRUE;
  1127.       }
  1128.     } else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) ||
  1129.                t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) {
  1130.       TRef trid = lj_ir_kint(J, ctype_cid(ct->info));
  1131.       tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr);
  1132.       if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
  1133.     } else if (t == IRT_FLOAT || t == IRT_U32) {
  1134.       tr = emitconv(tr, IRT_NUM, t, 0);
  1135.     } else if (t == IRT_I8 || t == IRT_I16) {
  1136.       tr = emitconv(tr, IRT_INT, t, IRCONV_SEXT);
  1137.     } else if (t == IRT_U8 || t == IRT_U16) {
  1138.       tr = emitconv(tr, IRT_INT, t, 0);
  1139.     }
  1140.     J->base[0] = tr;
  1141.     J->needsnap = 1;
  1142.     return 1;
  1143.   }
  1144.   return 0;
  1145. }

  1146. void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd)
  1147. {
  1148.   CTState *cts = ctype_ctsG(J2G(J));
  1149.   GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]);
  1150.   CTypeID id = cd->ctypeid;
  1151.   CType *ct;
  1152.   cTValue *tv;
  1153.   MMS mm = MM_call;
  1154.   if (id == CTID_CTYPEID) {
  1155.     id = crec_constructor(J, cd, J->base[0]);
  1156.     mm = MM_new;
  1157.   } else if (crec_call(J, rd, cd)) {
  1158.     return;
  1159.   }
  1160.   /* Record ctype __call/__new metamethod. */
  1161.   ct = ctype_raw(cts, id);
  1162.   tv = lj_ctype_meta(cts, ctype_isptr(ct->info) ? ctype_cid(ct->info) : id, mm);
  1163.   if (tv) {
  1164.     if (tvisfunc(tv)) {
  1165.       J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME;
  1166.       rd->nres = -1/* Pending tailcall. */
  1167.       return;
  1168.     }
  1169.   } else if (mm == MM_new) {
  1170.     crec_alloc(J, rd, id);
  1171.     return;
  1172.   }
  1173.   /* No metamethod or NYI: non-function metamethods. */
  1174.   lj_trace_err(J, LJ_TRERR_BADTYPE);
  1175. }

  1176. static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm)
  1177. {
  1178.   if (ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) {
  1179.     IRType dt;
  1180.     CTypeID id;
  1181.     TRef tr;
  1182.     MSize i;
  1183.     IROp op;
  1184.     lj_needsplit(J);
  1185.     if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) ||
  1186.         ((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) {
  1187.       dt = IRT_U64; id = CTID_UINT64;
  1188.     } else {
  1189.       dt = IRT_I64; id = CTID_INT64;
  1190.       if (mm < MM_add &&
  1191.           !((s[0]->info | s[1]->info) & CTF_FP) &&
  1192.           s[0]->size == 4 && s[1]->size == 4) {  /* Try to narrow comparison. */
  1193.         if (!((s[0]->info ^ s[1]->info) & CTF_UNSIGNED) ||
  1194.             (tref_isk(sp[1]) && IR(tref_ref(sp[1]))->i >= 0)) {
  1195.           dt = (s[0]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;
  1196.           goto comp;
  1197.         } else if (tref_isk(sp[0]) && IR(tref_ref(sp[0]))->i >= 0) {
  1198.           dt = (s[1]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;
  1199.           goto comp;
  1200.         }
  1201.       }
  1202.     }
  1203.     for (i = 0; i < 2; i++) {
  1204.       IRType st = tref_type(sp[i]);
  1205.       if (st == IRT_NUM || st == IRT_FLOAT)
  1206.         sp[i] = emitconv(sp[i], dt, st, IRCONV_ANY);
  1207.       else if (!(st == IRT_I64 || st == IRT_U64))
  1208.         sp[i] = emitconv(sp[i], dt, IRT_INT,
  1209.                          (s[i]->info & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
  1210.     }
  1211.     if (mm < MM_add) {
  1212.     comp:
  1213.       /* Assume true comparison. Fixup and emit pending guard later. */
  1214.       if (mm == MM_eq) {
  1215.         op = IR_EQ;
  1216.       } else {
  1217.         op = mm == MM_lt ? IR_LT : IR_LE;
  1218.         if (dt == IRT_U32 || dt == IRT_U64)
  1219.           op += (IR_ULT-IR_LT);
  1220.       }
  1221.       lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]);
  1222.       J->postproc = LJ_POST_FIXGUARD;
  1223.       return TREF_TRUE;
  1224.     } else {
  1225.       tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]);
  1226.     }
  1227.     return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
  1228.   }
  1229.   return 0;
  1230. }

  1231. static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm)
  1232. {
  1233.   CTState *cts = ctype_ctsG(J2G(J));
  1234.   CType *ctp = s[0];
  1235.   if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
  1236.     if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
  1237.         (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
  1238.       if (mm == MM_sub) {  /* Pointer difference. */
  1239.         TRef tr;
  1240.         CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
  1241.         if (sz == 0 || (sz & (sz-1)) != 0)
  1242.           return 0/* NYI: integer division. */
  1243.         tr = emitir(IRT(IR_SUB, IRT_INTP), sp[0], sp[1]);
  1244.         tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz)));
  1245. #if LJ_64
  1246.         tr = emitconv(tr, IRT_NUM, IRT_INTP, 0);
  1247. #endif
  1248.         return tr;
  1249.       } else/* Pointer comparison (unsigned). */
  1250.         /* Assume true comparison. Fixup and emit pending guard later. */
  1251.         IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE;
  1252.         lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]);
  1253.         J->postproc = LJ_POST_FIXGUARD;
  1254.         return TREF_TRUE;
  1255.       }
  1256.     }
  1257.     if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info)))
  1258.       return 0;
  1259.   } else if (mm == MM_add && ctype_isnum(ctp->info) &&
  1260.              (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
  1261.     TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr;  /* Swap pointer and index. */
  1262.     ctp = s[1];
  1263.   } else {
  1264.     return 0;
  1265.   }
  1266.   {
  1267.     TRef tr = sp[1];
  1268.     IRType t = tref_type(tr);
  1269.     CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
  1270.     CTypeID id;
  1271. #if LJ_64
  1272.     if (t == IRT_NUM || t == IRT_FLOAT)
  1273.       tr = emitconv(tr, IRT_INTP, t, IRCONV_ANY);
  1274.     else if (!(t == IRT_I64 || t == IRT_U64))
  1275.       tr = emitconv(tr, IRT_INTP, IRT_INT,
  1276.                     ((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT);
  1277. #else
  1278.     if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) {
  1279.       tr = emitconv(tr, IRT_INTP, t,
  1280.                     (t == IRT_NUM || t == IRT_FLOAT) ? IRCONV_ANY : 0);
  1281.     }
  1282. #endif
  1283.     tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz));
  1284.     tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, IRT_PTR), sp[0], tr);
  1285.     id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
  1286.                          CTSIZE_PTR);
  1287.     return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
  1288.   }
  1289. }

  1290. /* Record ctype arithmetic metamethods. */
  1291. static TRef crec_arith_meta(jit_State *J, TRef *sp, CType **s, CTState *cts,
  1292.                             RecordFFData *rd)
  1293. {
  1294.   cTValue *tv = NULL;
  1295.   if (J->base[0]) {
  1296.     if (tviscdata(&rd->argv[0])) {
  1297.       CTypeID id = argv2cdata(J, J->base[0], &rd->argv[0])->ctypeid;
  1298.       CType *ct = ctype_raw(cts, id);
  1299.       if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  1300.       tv = lj_ctype_meta(cts, id, (MMS)rd->data);
  1301.     }
  1302.     if (!tv && J->base[1] && tviscdata(&rd->argv[1])) {
  1303.       CTypeID id = argv2cdata(J, J->base[1], &rd->argv[1])->ctypeid;
  1304.       CType *ct = ctype_raw(cts, id);
  1305.       if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  1306.       tv = lj_ctype_meta(cts, id, (MMS)rd->data);
  1307.     }
  1308.   }
  1309.   if (tv) {
  1310.     if (tvisfunc(tv)) {
  1311.       J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME;
  1312.       rd->nres = -1/* Pending tailcall. */
  1313.       return 0;
  1314.     }  /* NYI: non-function metamethods. */
  1315.   } else if ((MMS)rd->data == MM_eq) {  /* Fallback cdata pointer comparison. */
  1316.     if (sp[0] && sp[1] && ctype_isnum(s[0]->info) == ctype_isnum(s[1]->info)) {
  1317.       /* Assume true comparison. Fixup and emit pending guard later. */
  1318.       lj_ir_set(J, IRTG(IR_EQ, IRT_PTR), sp[0], sp[1]);
  1319.       J->postproc = LJ_POST_FIXGUARD;
  1320.       return TREF_TRUE;
  1321.     } else {
  1322.       return TREF_FALSE;
  1323.     }
  1324.   }
  1325.   lj_trace_err(J, LJ_TRERR_BADTYPE);
  1326.   return 0;
  1327. }

  1328. void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd)
  1329. {
  1330.   CTState *cts = ctype_ctsG(J2G(J));
  1331.   TRef sp[2];
  1332.   CType *s[2];
  1333.   MSize i;
  1334.   for (i = 0; i < 2; i++) {
  1335.     TRef tr = J->base[i];
  1336.     CType *ct = ctype_get(cts, CTID_DOUBLE);
  1337.     if (!tr) {
  1338.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  1339.     } else if (tref_iscdata(tr)) {
  1340.       CTypeID id = argv2cdata(J, tr, &rd->argv[i])->ctypeid;
  1341.       IRType t;
  1342.       ct = ctype_raw(cts, id);
  1343.       t = crec_ct2irt(cts, ct);
  1344.       if (ctype_isptr(ct->info)) {  /* Resolve pointer or reference. */
  1345.         tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR);
  1346.         if (ctype_isref(ct->info)) {
  1347.           ct = ctype_rawchild(cts, ct);
  1348.           t = crec_ct2irt(cts, ct);
  1349.         }
  1350.       } else if (t == IRT_I64 || t == IRT_U64) {
  1351.         tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64);
  1352.         lj_needsplit(J);
  1353.         goto ok;
  1354.       } else if (t == IRT_INT || t == IRT_U32) {
  1355.         tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT);
  1356.         if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  1357.         goto ok;
  1358.       } else if (ctype_isfunc(ct->info)) {
  1359.         tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR);
  1360.         ct = ctype_get(cts,
  1361.           lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));
  1362.         goto ok;
  1363.       } else {
  1364.         tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata)));
  1365.       }
  1366.       if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  1367.       if (ctype_isnum(ct->info)) {
  1368.         if (t == IRT_CDATA) {
  1369.           tr = 0;
  1370.         } else {
  1371.           if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
  1372.           tr = emitir(IRT(IR_XLOAD, t), tr, 0);
  1373.         }
  1374.       }
  1375.     } else if (tref_isnil(tr)) {
  1376.       tr = lj_ir_kptr(J, NULL);
  1377.       ct = ctype_get(cts, CTID_P_VOID);
  1378.     } else if (tref_isinteger(tr)) {
  1379.       ct = ctype_get(cts, CTID_INT32);
  1380.     } else if (tref_isstr(tr)) {
  1381.       TRef tr2 = J->base[1-i];
  1382.       CTypeID id = argv2cdata(J, tr2, &rd->argv[1-i])->ctypeid;
  1383.       ct = ctype_raw(cts, id);
  1384.       if (ctype_isenum(ct->info)) {  /* Match string against enum constant. */
  1385.         GCstr *str = strV(&rd->argv[i]);
  1386.         CTSize ofs;
  1387.         CType *cct = lj_ctype_getfield(cts, ct, str, &ofs);
  1388.         if (cct && ctype_isconstval(cct->info)) {
  1389.           /* Specialize to the name of the enum constant. */
  1390.           emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, str));
  1391.           ct = ctype_child(cts, cct);
  1392.           tr = lj_ir_kint(J, (int32_t)ofs);
  1393.         } else/* Interpreter will throw or return false. */
  1394.           ct = ctype_get(cts, CTID_P_VOID);
  1395.         }
  1396.       } else if (ctype_isptr(ct->info)) {
  1397.         tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCstr)));
  1398.       } else {
  1399.         ct = ctype_get(cts, CTID_P_VOID);
  1400.       }
  1401.     } else if (!tref_isnum(tr)) {
  1402.       tr = 0;
  1403.       ct = ctype_get(cts, CTID_P_VOID);
  1404.     }
  1405.   ok:
  1406.     s[i] = ct;
  1407.     sp[i] = tr;
  1408.   }
  1409.   {
  1410.     TRef tr;
  1411.     if (!(tr = crec_arith_int64(J, sp, s, (MMS)rd->data)) &&
  1412.         !(tr = crec_arith_ptr(J, sp, s, (MMS)rd->data)) &&
  1413.         !(tr = crec_arith_meta(J, sp, s, cts, rd)))
  1414.       return;
  1415.     J->base[0] = tr;
  1416.     /* Fixup cdata comparisons, too. Avoids some cdata escapes. */
  1417.     if (J->postproc == LJ_POST_FIXGUARD && frame_iscont(J->L->base-1) &&
  1418.         !irt_isguard(J->guardemit)) {
  1419.       const BCIns *pc = frame_contpc(J->L->base-1) - 1;
  1420.       if (bc_op(*pc) <= BC_ISNEP) {
  1421.         J2G(J)->tmptv.u64 = (uint64_t)(uintptr_t)pc;
  1422.         J->postproc = LJ_POST_FIXCOMP;
  1423.       }
  1424.     }
  1425.   }
  1426. }

  1427. /* -- C library namespace metamethods ------------------------------------- */

  1428. void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd)
  1429. {
  1430.   CTState *cts = ctype_ctsG(J2G(J));
  1431.   if (tref_isudata(J->base[0]) && tref_isstr(J->base[1]) &&
  1432.       udataV(&rd->argv[0])->udtype == UDTYPE_FFI_CLIB) {
  1433.     CLibrary *cl = (CLibrary *)uddata(udataV(&rd->argv[0]));
  1434.     GCstr *name = strV(&rd->argv[1]);
  1435.     CType *ct;
  1436.     CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX);
  1437.     cTValue *tv = lj_tab_getstr(cl->cache, name);
  1438.     rd->nres = rd->data;
  1439.     if (id && tv && !tvisnil(tv)) {
  1440.       /* Specialize to the symbol name and make the result a constant. */
  1441.       emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, name));
  1442.       if (ctype_isconstval(ct->info)) {
  1443.         if (ct->size >= 0x80000000u &&
  1444.             (ctype_child(cts, ct)->info & CTF_UNSIGNED))
  1445.           J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)ct->size);
  1446.         else
  1447.           J->base[0] = lj_ir_kint(J, (int32_t)ct->size);
  1448.       } else if (ctype_isextern(ct->info)) {
  1449.         CTypeID sid = ctype_cid(ct->info);
  1450.         void *sp = *(void **)cdataptr(cdataV(tv));
  1451.         TRef ptr;
  1452.         ct = ctype_raw(cts, sid);
  1453.         if (LJ_64 && !checkptr32(sp))
  1454.           ptr = lj_ir_kintp(J, (uintptr_t)sp);
  1455.         else
  1456.           ptr = lj_ir_kptr(J, sp);
  1457.         if (rd->data) {
  1458.           J->base[0] = crec_tv_ct(J, ct, sid, ptr);
  1459.         } else {
  1460.           J->needsnap = 1;
  1461.           crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
  1462.         }
  1463.       } else {
  1464.         J->base[0] = lj_ir_kgc(J, obj2gco(cdataV(tv)), IRT_CDATA);
  1465.       }
  1466.     } else {
  1467.       lj_trace_err(J, LJ_TRERR_NOCACHE);
  1468.     }
  1469.   }  /* else: interpreter will throw. */
  1470. }

  1471. /* -- FFI library functions ----------------------------------------------- */

  1472. static TRef crec_toint(jit_State *J, CTState *cts, TRef sp, TValue *sval)
  1473. {
  1474.   return crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, sp, sval);
  1475. }

  1476. void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd)
  1477. {
  1478.   crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0]));
  1479. }

  1480. void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd)
  1481. {
  1482.   UNUSED(rd);
  1483.   if (J->base[0])
  1484.     lj_trace_err(J, LJ_TRERR_NYICALL);
  1485.   J->base[0] = lj_ir_call(J, IRCALL_lj_vm_errno);
  1486. }

  1487. void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd)
  1488. {
  1489.   CTState *cts = ctype_ctsG(J2G(J));
  1490.   TRef tr = J->base[0];
  1491.   if (tr) {
  1492.     TRef trlen = J->base[1];
  1493.     if (!tref_isnil(trlen)) {
  1494.       trlen = crec_toint(J, cts, trlen, &rd->argv[1]);
  1495.       tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]);
  1496.     } else {
  1497.       tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]);
  1498.       trlen = lj_ir_call(J, IRCALL_strlen, tr);
  1499.     }
  1500.     J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), tr, trlen);
  1501.   }  /* else: interpreter will throw. */
  1502. }

  1503. void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd)
  1504. {
  1505.   CTState *cts = ctype_ctsG(J2G(J));
  1506.   TRef trdst = J->base[0], trsrc = J->base[1], trlen = J->base[2];
  1507.   if (trdst && trsrc && (trlen || tref_isstr(trsrc))) {
  1508.     trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);
  1509.     trsrc = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, trsrc, &rd->argv[1]);
  1510.     if (trlen) {
  1511.       trlen = crec_toint(J, cts, trlen, &rd->argv[2]);
  1512.     } else {
  1513.       trlen = emitir(IRTI(IR_FLOAD), J->base[1], IRFL_STR_LEN);
  1514.       trlen = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1));
  1515.     }
  1516.     rd->nres = 0;
  1517.     crec_copy(J, trdst, trsrc, trlen, NULL);
  1518.   }  /* else: interpreter will throw. */
  1519. }

  1520. void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd)
  1521. {
  1522.   CTState *cts = ctype_ctsG(J2G(J));
  1523.   TRef trdst = J->base[0], trlen = J->base[1], trfill = J->base[2];
  1524.   if (trdst && trlen) {
  1525.     CTSize step = 1;
  1526.     if (tviscdata(&rd->argv[0])) {  /* Get alignment of original destination. */
  1527.       CTSize sz;
  1528.       CType *ct = ctype_raw(cts, cdataV(&rd->argv[0])->ctypeid);
  1529.       if (ctype_isptr(ct->info))
  1530.         ct = ctype_rawchild(cts, ct);
  1531.       step = (1u<<ctype_align(lj_ctype_info(cts, ctype_typeid(cts, ct), &sz)));
  1532.     }
  1533.     trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);
  1534.     trlen = crec_toint(J, cts, trlen, &rd->argv[1]);
  1535.     if (trfill)
  1536.       trfill = crec_toint(J, cts, trfill, &rd->argv[2]);
  1537.     else
  1538.       trfill = lj_ir_kint(J, 0);
  1539.     rd->nres = 0;
  1540.     crec_fill(J, trdst, trlen, trfill, step);
  1541.   }  /* else: interpreter will throw. */
  1542. }

  1543. void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd)
  1544. {
  1545.   if (tref_iscdata(J->base[0])) {
  1546.     TRef trid = lj_ir_kint(J, argv2ctype(J, J->base[0], &rd->argv[0]));
  1547.     J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA),
  1548.                         lj_ir_kint(J, CTID_CTYPEID), trid);
  1549.   } else {
  1550.     setfuncV(J->L, &J->errinfo, J->fn);
  1551.     lj_trace_err_info(J, LJ_TRERR_NYIFFU);
  1552.   }
  1553. }

  1554. void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd)
  1555. {
  1556.   argv2ctype(J, J->base[0], &rd->argv[0]);
  1557.   if (tref_iscdata(J->base[1])) {
  1558.     argv2ctype(J, J->base[1], &rd->argv[1]);
  1559.     J->postproc = LJ_POST_FIXBOOL;
  1560.     J->base[0] = TREF_TRUE;
  1561.   } else {
  1562.     J->base[0] = TREF_FALSE;
  1563.   }
  1564. }

  1565. void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd)
  1566. {
  1567.   if (tref_isstr(J->base[0])) {
  1568.     /* Specialize to the ABI string to make the boolean result a constant. */
  1569.     emitir(IRTG(IR_EQ, IRT_STR), J->base[0], lj_ir_kstr(J, strV(&rd->argv[0])));
  1570.     J->postproc = LJ_POST_FIXBOOL;
  1571.     J->base[0] = TREF_TRUE;
  1572.   } else {
  1573.     lj_trace_err(J, LJ_TRERR_BADTYPE);
  1574.   }
  1575. }

  1576. /* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */
  1577. void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd)
  1578. {
  1579.   CTypeID id = argv2ctype(J, J->base[0], &rd->argv[0]);
  1580.   if (rd->data == FF_ffi_sizeof) {
  1581.     CType *ct = lj_ctype_rawref(ctype_ctsG(J2G(J)), id);
  1582.     if (ctype_isvltype(ct->info))
  1583.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  1584.   } else if (rd->data == FF_ffi_offsetof) {  /* Specialize to the field name. */
  1585.     if (!tref_isstr(J->base[1]))
  1586.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  1587.     emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));
  1588.     rd->nres = 3/* Just in case. */
  1589.   }
  1590.   J->postproc = LJ_POST_FIXCONST;
  1591.   J->base[0] = J->base[1] = J->base[2] = TREF_NIL;
  1592. }

  1593. void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd)
  1594. {
  1595.   argv2cdata(J, J->base[0], &rd->argv[0]);
  1596.   if (!J->base[1])
  1597.     lj_trace_err(J, LJ_TRERR_BADTYPE);
  1598.   crec_finalizer(J, J->base[0], J->base[1], &rd->argv[1]);
  1599. }

  1600. /* -- 64 bit bit.* library functions -------------------------------------- */

  1601. /* Determine bit operation type from argument type. */
  1602. static CTypeID crec_bit64_type(CTState *cts, cTValue *tv)
  1603. {
  1604.   if (tviscdata(tv)) {
  1605.     CType *ct = lj_ctype_rawref(cts, cdataV(tv)->ctypeid);
  1606.     if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  1607.     if ((ct->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) ==
  1608.         CTINFO(CT_NUM, CTF_UNSIGNED) && ct->size == 8)
  1609.       return CTID_UINT64;  /* Use uint64_t, since it has the highest rank. */
  1610.     return CTID_INT64;  /* Otherwise use int64_t. */
  1611.   }
  1612.   return 0/* Use regular 32 bit ops. */
  1613. }

  1614. void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd)
  1615. {
  1616.   CTState *cts = ctype_ctsG(J2G(J));
  1617.   TRef tr = crec_ct_tv(J, ctype_get(cts, CTID_INT64), 0,
  1618.                        J->base[0], &rd->argv[0]);
  1619.   if (!tref_isinteger(tr))
  1620.     tr = emitconv(tr, IRT_INT, tref_type(tr), 0);
  1621.   J->base[0] = tr;
  1622. }

  1623. int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd)
  1624. {
  1625.   CTState *cts = ctype_ctsG(J2G(J));
  1626.   CTypeID id = crec_bit64_type(cts, &rd->argv[0]);
  1627.   if (id) {
  1628.     TRef tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]);
  1629.     tr = emitir(IRT(rd->data, id-CTID_INT64+IRT_I64), tr, 0);
  1630.     J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
  1631.     return 1;
  1632.   }
  1633.   return 0;
  1634. }

  1635. int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd)
  1636. {
  1637.   CTState *cts = ctype_ctsG(J2G(J));
  1638.   CTypeID id = 0;
  1639.   MSize i;
  1640.   for (i = 0; J->base[i] != 0; i++) {
  1641.     CTypeID aid = crec_bit64_type(cts, &rd->argv[i]);
  1642.     if (id < aid) id = aid;  /* Determine highest type rank of all arguments. */
  1643.   }
  1644.   if (id) {
  1645.     CType *ct = ctype_get(cts, id);
  1646.     uint32_t ot = IRT(rd->data, id-CTID_INT64+IRT_I64);
  1647.     TRef tr = crec_ct_tv(J, ct, 0, J->base[0], &rd->argv[0]);
  1648.     for (i = 1; J->base[i] != 0; i++) {
  1649.       TRef tr2 = crec_ct_tv(J, ct, 0, J->base[i], &rd->argv[i]);
  1650.       tr = emitir(ot, tr, tr2);
  1651.     }
  1652.     J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
  1653.     return 1;
  1654.   }
  1655.   return 0;
  1656. }

  1657. int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd)
  1658. {
  1659.   CTState *cts = ctype_ctsG(J2G(J));
  1660.   CTypeID id;
  1661.   TRef tsh = 0;
  1662.   if (J->base[0] && tref_iscdata(J->base[1])) {
  1663.     tsh = crec_ct_tv(J, ctype_get(cts, CTID_INT64), 0,
  1664.                      J->base[1], &rd->argv[1]);
  1665.     if (!tref_isinteger(tsh))
  1666.       tsh = emitconv(tsh, IRT_INT, tref_type(tsh), 0);
  1667.     J->base[1] = tsh;
  1668.   }
  1669.   id = crec_bit64_type(cts, &rd->argv[0]);
  1670.   if (id) {
  1671.     TRef tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]);
  1672.     uint32_t op = rd->data;
  1673.     if (!tsh) tsh = lj_opt_narrow_tobit(J, J->base[1]);
  1674.     if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&
  1675.         !tref_isk(tsh))
  1676.       tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 63));
  1677. #ifdef LJ_TARGET_UNIFYROT
  1678.       if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) {
  1679.         op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR;
  1680.         tsh = emitir(IRTI(IR_NEG), tsh, tsh);
  1681.       }
  1682. #endif
  1683.     tr = emitir(IRT(op, id-CTID_INT64+IRT_I64), tr, tsh);
  1684.     J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
  1685.     return 1;
  1686.   }
  1687.   return 0;
  1688. }

  1689. TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr)
  1690. {
  1691.   CTState *cts = ctype_ctsG(J2G(J));
  1692.   CTypeID id = crec_bit64_type(cts, &rd->argv[0]);
  1693.   TRef tr, trsf = J->base[1];
  1694.   SFormat sf = (STRFMT_UINT|STRFMT_T_HEX);
  1695.   int32_t n;
  1696.   if (trsf) {
  1697.     CTypeID id2 = 0;
  1698.     n = (int32_t)lj_carith_check64(J->L, 2, &id2);
  1699.     if (id2)
  1700.       trsf = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, trsf, &rd->argv[1]);
  1701.     else
  1702.       trsf = lj_opt_narrow_tobit(J, trsf);
  1703.     emitir(IRTGI(IR_EQ), trsf, lj_ir_kint(J, n));  /* Specialize to n. */
  1704.   } else {
  1705.     n = id ? 16 : 8;
  1706.   }
  1707.   if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; }
  1708.   sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC);
  1709.   if (id) {
  1710.     tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]);
  1711.     if (n < 16)
  1712.       tr = emitir(IRT(IR_BAND, IRT_U64), tr,
  1713.                   lj_ir_kint64(J, ((uint64_t)1 << 4*n)-1));
  1714.   } else {
  1715.     tr = lj_opt_narrow_tobit(J, J->base[0]);
  1716.     if (n < 8)
  1717.       tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << 4*n)-1)));
  1718.     tr = emitconv(tr, IRT_U64, IRT_INT, 0);  /* No sign-extension. */
  1719.     lj_needsplit(J);
  1720.   }
  1721.   return lj_ir_call(J, IRCALL_lj_strfmt_putfxint, hdr, lj_ir_kint(J, sf), tr);
  1722. }

  1723. /* -- Miscellaneous library functions ------------------------------------- */

  1724. void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd)
  1725. {
  1726.   CTState *cts = ctype_ctsG(J2G(J));
  1727.   CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->ctypeid);
  1728.   if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  1729.   if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
  1730.     if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 &&
  1731.         !(ct->size == 4 && (ct->info & CTF_UNSIGNED)))
  1732.       d = ctype_get(cts, CTID_INT32);
  1733.     else
  1734.       d = ctype_get(cts, CTID_DOUBLE);
  1735.     J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]);
  1736.   } else {
  1737.     J->base[0] = TREF_NIL;
  1738.   }
  1739. }

  1740. #undef IR
  1741. #undef emitir
  1742. #undef emitconv

  1743. #endif