src/lj_ir.c - luajit-2.0-src

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** SSA IR (Intermediate Representation) emitter.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #define lj_ir_c
  6. #define LUA_CORE

  7. /* For pointers to libc/libm functions. */
  8. #include <stdio.h>
  9. #include <math.h>

  10. #include "lj_obj.h"

  11. #if LJ_HASJIT

  12. #include "lj_gc.h"
  13. #include "lj_buf.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_ir.h"
  17. #include "lj_jit.h"
  18. #include "lj_ircall.h"
  19. #include "lj_iropt.h"
  20. #include "lj_trace.h"
  21. #if LJ_HASFFI
  22. #include "lj_ctype.h"
  23. #include "lj_cdata.h"
  24. #include "lj_carith.h"
  25. #endif
  26. #include "lj_vm.h"
  27. #include "lj_strscan.h"
  28. #include "lj_strfmt.h"
  29. #include "lj_lib.h"

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

  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. /* -- IR tables ----------------------------------------------------------- */

  36. /* IR instruction modes. */
  37. LJ_DATADEF const uint8_t lj_ir_mode[IR__MAX+1] = {
  38. IRDEF(IRMODE)
  39.   0
  40. };

  41. /* IR type sizes. */
  42. LJ_DATADEF const uint8_t lj_ir_type_size[IRT__MAX+1] = {
  43. #define IRTSIZE(name, size)        size,
  44. IRTDEF(IRTSIZE)
  45. #undef IRTSIZE
  46.   0
  47. };

  48. /* C call info for CALL* instructions. */
  49. LJ_DATADEF const CCallInfo lj_ir_callinfo[] = {
  50. #define IRCALLCI(cond, name, nargs, kind, type, flags) \
  51.   { (ASMFunction)IRCALLCOND_##cond(name), \
  52.     (nargs)|(CCI_CALL_##kind)|(IRT_##type<<CCI_OTSHIFT)|(flags) },
  53. IRCALLDEF(IRCALLCI)
  54. #undef IRCALLCI
  55.   { NULL, 0 }
  56. };

  57. /* -- IR emitter ---------------------------------------------------------- */

  58. /* Grow IR buffer at the top. */
  59. void LJ_FASTCALL lj_ir_growtop(jit_State *J)
  60. {
  61.   IRIns *baseir = J->irbuf + J->irbotlim;
  62.   MSize szins = J->irtoplim - J->irbotlim;
  63.   if (szins) {
  64.     baseir = (IRIns *)lj_mem_realloc(J->L, baseir, szins*sizeof(IRIns),
  65.                                      2*szins*sizeof(IRIns));
  66.     J->irtoplim = J->irbotlim + 2*szins;
  67.   } else {
  68.     baseir = (IRIns *)lj_mem_realloc(J->L, NULL, 0, LJ_MIN_IRSZ*sizeof(IRIns));
  69.     J->irbotlim = REF_BASE - LJ_MIN_IRSZ/4;
  70.     J->irtoplim = J->irbotlim + LJ_MIN_IRSZ;
  71.   }
  72.   J->cur.ir = J->irbuf = baseir - J->irbotlim;
  73. }

  74. /* Grow IR buffer at the bottom or shift it up. */
  75. static void lj_ir_growbot(jit_State *J)
  76. {
  77.   IRIns *baseir = J->irbuf + J->irbotlim;
  78.   MSize szins = J->irtoplim - J->irbotlim;
  79.   lua_assert(szins != 0);
  80.   lua_assert(J->cur.nk == J->irbotlim);
  81.   if (J->cur.nins + (szins >> 1) < J->irtoplim) {
  82.     /* More than half of the buffer is free on top: shift up by a quarter. */
  83.     MSize ofs = szins >> 2;
  84.     memmove(baseir + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns));
  85.     J->irbotlim -= ofs;
  86.     J->irtoplim -= ofs;
  87.     J->cur.ir = J->irbuf = baseir - J->irbotlim;
  88.   } else {
  89.     /* Double the buffer size, but split the growth amongst top/bottom. */
  90.     IRIns *newbase = lj_mem_newt(J->L, 2*szins*sizeof(IRIns), IRIns);
  91.     MSize ofs = szins >= 256 ? 128 : (szins >> 1);  /* Limit bottom growth. */
  92.     memcpy(newbase + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns));
  93.     lj_mem_free(G(J->L), baseir, szins*sizeof(IRIns));
  94.     J->irbotlim -= ofs;
  95.     J->irtoplim = J->irbotlim + 2*szins;
  96.     J->cur.ir = J->irbuf = newbase - J->irbotlim;
  97.   }
  98. }

  99. /* Emit IR without any optimizations. */
  100. TRef LJ_FASTCALL lj_ir_emit(jit_State *J)
  101. {
  102.   IRRef ref = lj_ir_nextins(J);
  103.   IRIns *ir = IR(ref);
  104.   IROp op = fins->o;
  105.   ir->prev = J->chain[op];
  106.   J->chain[op] = (IRRef1)ref;
  107.   ir->o = op;
  108.   ir->op1 = fins->op1;
  109.   ir->op2 = fins->op2;
  110.   J->guardemit.irt |= fins->t.irt;
  111.   return TREF(ref, irt_t((ir->t = fins->t)));
  112. }

  113. /* Emit call to a C function. */
  114. TRef lj_ir_call(jit_State *J, IRCallID id, ...)
  115. {
  116.   const CCallInfo *ci = &lj_ir_callinfo[id];
  117.   uint32_t n = CCI_NARGS(ci);
  118.   TRef tr = TREF_NIL;
  119.   va_list argp;
  120.   va_start(argp, id);
  121.   if ((ci->flags & CCI_L)) n--;
  122.   if (n > 0)
  123.     tr = va_arg(argp, IRRef);
  124.   while (n-- > 1)
  125.     tr = emitir(IRT(IR_CARG, IRT_NIL), tr, va_arg(argp, IRRef));
  126.   va_end(argp);
  127.   if (CCI_OP(ci) == IR_CALLS)
  128.     J->needsnap = 1/* Need snapshot after call with side effect. */
  129.   return emitir(CCI_OPTYPE(ci), tr, id);
  130. }

  131. /* -- Interning of constants ---------------------------------------------- */

  132. /*
  133. ** IR instructions for constants are kept between J->cur.nk >= ref < REF_BIAS.
  134. ** They are chained like all other instructions, but grow downwards.
  135. ** The are interned (like strings in the VM) to facilitate reference
  136. ** comparisons. The same constant must get the same reference.
  137. */

  138. /* Get ref of next IR constant and optionally grow IR.
  139. ** Note: this may invalidate all IRIns *!
  140. */
  141. static LJ_AINLINE IRRef ir_nextk(jit_State *J)
  142. {
  143.   IRRef ref = J->cur.nk;
  144.   if (LJ_UNLIKELY(ref <= J->irbotlim)) lj_ir_growbot(J);
  145.   J->cur.nk = --ref;
  146.   return ref;
  147. }

  148. /* Intern int32_t constant. */
  149. TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k)
  150. {
  151.   IRIns *ir, *cir = J->cur.ir;
  152.   IRRef ref;
  153.   for (ref = J->chain[IR_KINT]; ref; ref = cir[ref].prev)
  154.     if (cir[ref].i == k)
  155.       goto found;
  156.   ref = ir_nextk(J);
  157.   ir = IR(ref);
  158.   ir->i = k;
  159.   ir->t.irt = IRT_INT;
  160.   ir->o = IR_KINT;
  161.   ir->prev = J->chain[IR_KINT];
  162.   J->chain[IR_KINT] = (IRRef1)ref;
  163. found:
  164.   return TREF(ref, IRT_INT);
  165. }

  166. /* The MRef inside the KNUM/KINT64 IR instructions holds the address of the
  167. ** 64 bit constant. The constants themselves are stored in a chained array
  168. ** and shared across traces.
  169. **
  170. ** Rationale for choosing this data structure:
  171. ** - The address of the constants is embedded in the generated machine code
  172. **   and must never move. A resizable array or hash table wouldn't work.
  173. ** - Most apps need very few non-32 bit integer constants (less than a dozen).
  174. ** - Linear search is hard to beat in terms of speed and low complexity.
  175. */
  176. typedef struct K64Array {
  177.   MRef next;                        /* Pointer to next list. */
  178.   MSize numk;                        /* Number of used elements in this array. */
  179.   TValue k[LJ_MIN_K64SZ];        /* Array of constants. */
  180. } K64Array;

  181. /* Free all chained arrays. */
  182. void lj_ir_k64_freeall(jit_State *J)
  183. {
  184.   K64Array *k;
  185.   for (k = mref(J->k64, K64Array); k; ) {
  186.     K64Array *next = mref(k->next, K64Array);
  187.     lj_mem_free(J2G(J), k, sizeof(K64Array));
  188.     k = next;
  189.   }
  190. }

  191. /* Find 64 bit constant in chained array or add it. */
  192. cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64)
  193. {
  194.   K64Array *k, *kp = NULL;
  195.   TValue *ntv;
  196.   MSize idx;
  197.   /* Search for the constant in the whole chain of arrays. */
  198.   for (k = mref(J->k64, K64Array); k; k = mref(k->next, K64Array)) {
  199.     kp = k;  /* Remember previous element in list. */
  200.     for (idx = 0; idx < k->numk; idx++) {  /* Search one array. */
  201.       TValue *tv = &k->k[idx];
  202.       if (tv->u64 == u64)  /* Needed for +-0/NaN/absmask. */
  203.         return tv;
  204.     }
  205.   }
  206.   /* Constant was not found, need to add it. */
  207.   if (!(kp && kp->numk < LJ_MIN_K64SZ)) {  /* Allocate a new array. */
  208.     K64Array *kn = lj_mem_newt(J->L, sizeof(K64Array), K64Array);
  209.     setmref(kn->next, NULL);
  210.     kn->numk = 0;
  211.     if (kp)
  212.       setmref(kp->next, kn);  /* Chain to the end of the list. */
  213.     else
  214.       setmref(J->k64, kn);  /* Link first array. */
  215.     kp = kn;
  216.   }
  217.   ntv = &kp->k[kp->numk++];  /* Add to current array. */
  218.   ntv->u64 = u64;
  219.   return ntv;
  220. }

  221. /* Intern 64 bit constant, given by its address. */
  222. TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv)
  223. {
  224.   IRIns *ir, *cir = J->cur.ir;
  225.   IRRef ref;
  226.   IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64;
  227.   for (ref = J->chain[op]; ref; ref = cir[ref].prev)
  228.     if (ir_k64(&cir[ref]) == tv)
  229.       goto found;
  230.   ref = ir_nextk(J);
  231.   ir = IR(ref);
  232.   lua_assert(checkptrGC(tv));
  233.   setmref(ir->ptr, tv);
  234.   ir->t.irt = t;
  235.   ir->o = op;
  236.   ir->prev = J->chain[op];
  237.   J->chain[op] = (IRRef1)ref;
  238. found:
  239.   return TREF(ref, t);
  240. }

  241. /* Intern FP constant, given by its 64 bit pattern. */
  242. TRef lj_ir_knum_u64(jit_State *J, uint64_t u64)
  243. {
  244.   return lj_ir_k64(J, IR_KNUM, lj_ir_k64_find(J, u64));
  245. }

  246. /* Intern 64 bit integer constant. */
  247. TRef lj_ir_kint64(jit_State *J, uint64_t u64)
  248. {
  249.   return lj_ir_k64(J, IR_KINT64, lj_ir_k64_find(J, u64));
  250. }

  251. /* Check whether a number is int and return it. -0 is NOT considered an int. */
  252. static int numistrueint(lua_Number n, int32_t *kp)
  253. {
  254.   int32_t k = lj_num2int(n);
  255.   if (n == (lua_Number)k) {
  256.     if (kp) *kp = k;
  257.     if (k == 0) {  /* Special check for -0. */
  258.       TValue tv;
  259.       setnumV(&tv, n);
  260.       if (tv.u32.hi != 0)
  261.         return 0;
  262.     }
  263.     return 1;
  264.   }
  265.   return 0;
  266. }

  267. /* Intern number as int32_t constant if possible, otherwise as FP constant. */
  268. TRef lj_ir_knumint(jit_State *J, lua_Number n)
  269. {
  270.   int32_t k;
  271.   if (numistrueint(n, &k))
  272.     return lj_ir_kint(J, k);
  273.   else
  274.     return lj_ir_knum(J, n);
  275. }

  276. /* Intern GC object "constant". */
  277. TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t)
  278. {
  279.   IRIns *ir, *cir = J->cur.ir;
  280.   IRRef ref;
  281.   lua_assert(!LJ_GC64);  /* TODO_GC64: major changes required. */
  282.   lua_assert(!isdead(J2G(J), o));
  283.   for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev)
  284.     if (ir_kgc(&cir[ref]) == o)
  285.       goto found;
  286.   ref = ir_nextk(J);
  287.   ir = IR(ref);
  288.   /* NOBARRIER: Current trace is a GC root. */
  289.   setgcref(ir->gcr, o);
  290.   ir->t.irt = (uint8_t)t;
  291.   ir->o = IR_KGC;
  292.   ir->prev = J->chain[IR_KGC];
  293.   J->chain[IR_KGC] = (IRRef1)ref;
  294. found:
  295.   return TREF(ref, t);
  296. }

  297. /* Intern 32 bit pointer constant. */
  298. TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr)
  299. {
  300.   IRIns *ir, *cir = J->cur.ir;
  301.   IRRef ref;
  302.   lua_assert((void *)(intptr_t)i32ptr(ptr) == ptr);
  303.   for (ref = J->chain[op]; ref; ref = cir[ref].prev)
  304.     if (mref(cir[ref].ptr, void) == ptr)
  305.       goto found;
  306.   ref = ir_nextk(J);
  307.   ir = IR(ref);
  308.   setmref(ir->ptr, ptr);
  309.   ir->t.irt = IRT_P32;
  310.   ir->o = op;
  311.   ir->prev = J->chain[op];
  312.   J->chain[op] = (IRRef1)ref;
  313. found:
  314.   return TREF(ref, IRT_P32);
  315. }

  316. /* Intern typed NULL constant. */
  317. TRef lj_ir_knull(jit_State *J, IRType t)
  318. {
  319.   IRIns *ir, *cir = J->cur.ir;
  320.   IRRef ref;
  321.   for (ref = J->chain[IR_KNULL]; ref; ref = cir[ref].prev)
  322.     if (irt_t(cir[ref].t) == t)
  323.       goto found;
  324.   ref = ir_nextk(J);
  325.   ir = IR(ref);
  326.   ir->i = 0;
  327.   ir->t.irt = (uint8_t)t;
  328.   ir->o = IR_KNULL;
  329.   ir->prev = J->chain[IR_KNULL];
  330.   J->chain[IR_KNULL] = (IRRef1)ref;
  331. found:
  332.   return TREF(ref, t);
  333. }

  334. /* Intern key slot. */
  335. TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot)
  336. {
  337.   IRIns *ir, *cir = J->cur.ir;
  338.   IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot);
  339.   IRRef ref;
  340.   /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */
  341.   lua_assert(tref_isk(key) && slot == (IRRef)(IRRef1)slot);
  342.   for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev)
  343.     if (cir[ref].op12 == op12)
  344.       goto found;
  345.   ref = ir_nextk(J);
  346.   ir = IR(ref);
  347.   ir->op12 = op12;
  348.   ir->t.irt = IRT_P32;
  349.   ir->o = IR_KSLOT;
  350.   ir->prev = J->chain[IR_KSLOT];
  351.   J->chain[IR_KSLOT] = (IRRef1)ref;
  352. found:
  353.   return TREF(ref, IRT_P32);
  354. }

  355. /* -- Access to IR constants ---------------------------------------------- */

  356. /* Copy value of IR constant. */
  357. void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir)
  358. {
  359.   UNUSED(L);
  360.   lua_assert(ir->o != IR_KSLOT);  /* Common mistake. */
  361.   switch (ir->o) {
  362.   case IR_KPRI: setpriV(tv, irt_toitype(ir->t)); break;
  363.   case IR_KINT: setintV(tv, ir->i); break;
  364.   case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break;
  365.   case IR_KPTR: case IR_KKPTR: case IR_KNULL:
  366.     setlightudV(tv, mref(ir->ptr, void));
  367.     break;
  368.   case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break;
  369. #if LJ_HASFFI
  370.   case IR_KINT64: {
  371.     GCcdata *cd = lj_cdata_new_(L, CTID_INT64, 8);
  372.     *(uint64_t *)cdataptr(cd) = ir_kint64(ir)->u64;
  373.     setcdataV(L, tv, cd);
  374.     break;
  375.     }
  376. #endif
  377.   default: lua_assert(0); break;
  378.   }
  379. }

  380. /* -- Convert IR operand types -------------------------------------------- */

  381. /* Convert from string to number. */
  382. TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr)
  383. {
  384.   if (!tref_isnumber(tr)) {
  385.     if (tref_isstr(tr))
  386.       tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);
  387.     else
  388.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  389.   }
  390.   return tr;
  391. }

  392. /* Convert from integer or string to number. */
  393. TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr)
  394. {
  395.   if (!tref_isnum(tr)) {
  396.     if (tref_isinteger(tr))
  397.       tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);
  398.     else if (tref_isstr(tr))
  399.       tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);
  400.     else
  401.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  402.   }
  403.   return tr;
  404. }

  405. /* Convert from integer or number to string. */
  406. TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr)
  407. {
  408.   if (!tref_isstr(tr)) {
  409.     if (!tref_isnumber(tr))
  410.       lj_trace_err(J, LJ_TRERR_BADTYPE);
  411.     tr = emitir(IRT(IR_TOSTR, IRT_STR), tr,
  412.                 tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT);
  413.   }
  414.   return tr;
  415. }

  416. /* -- Miscellaneous IR ops ------------------------------------------------ */

  417. /* Evaluate numeric comparison. */
  418. int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op)
  419. {
  420.   switch (op) {
  421.   case IR_EQ: return (a == b);
  422.   case IR_NE: return (a != b);
  423.   case IR_LT: return (a < b);
  424.   case IR_GE: return (a >= b);
  425.   case IR_LE: return (a <= b);
  426.   case IR_GT: return (a > b);
  427.   case IR_ULT: return !(a >= b);
  428.   case IR_UGE: return !(a < b);
  429.   case IR_ULE: return !(a > b);
  430.   case IR_UGT: return !(a <= b);
  431.   default: lua_assert(0); return 0;
  432.   }
  433. }

  434. /* Evaluate string comparison. */
  435. int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op)
  436. {
  437.   int res = lj_str_cmp(a, b);
  438.   switch (op) {
  439.   case IR_LT: return (res < 0);
  440.   case IR_GE: return (res >= 0);
  441.   case IR_LE: return (res <= 0);
  442.   case IR_GT: return (res > 0);
  443.   default: lua_assert(0); return 0;
  444.   }
  445. }

  446. /* Rollback IR to previous state. */
  447. void lj_ir_rollback(jit_State *J, IRRef ref)
  448. {
  449.   IRRef nins = J->cur.nins;
  450.   while (nins > ref) {
  451.     IRIns *ir;
  452.     nins--;
  453.     ir = IR(nins);
  454.     J->chain[ir->o] = ir->prev;
  455.   }
  456.   J->cur.nins = nins;
  457. }

  458. #undef IR
  459. #undef fins
  460. #undef emitir

  461. #endif