src/lib_jit.c - luajit-2.0-src

Global variables defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** JIT library.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #define lib_jit_c
  6. #define LUA_LIB

  7. #include "lua.h"
  8. #include "lauxlib.h"
  9. #include "lualib.h"

  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_debug.h"
  14. #include "lj_str.h"
  15. #include "lj_tab.h"
  16. #include "lj_state.h"
  17. #include "lj_bc.h"
  18. #if LJ_HASFFI
  19. #include "lj_ctype.h"
  20. #endif
  21. #if LJ_HASJIT
  22. #include "lj_ir.h"
  23. #include "lj_jit.h"
  24. #include "lj_ircall.h"
  25. #include "lj_iropt.h"
  26. #include "lj_target.h"
  27. #endif
  28. #include "lj_trace.h"
  29. #include "lj_dispatch.h"
  30. #include "lj_vm.h"
  31. #include "lj_vmevent.h"
  32. #include "lj_lib.h"

  33. #include "luajit.h"

  34. /* -- jit.* functions ----------------------------------------------------- */

  35. #define LJLIB_MODULE_jit

  36. static int setjitmode(lua_State *L, int mode)
  37. {
  38.   int idx = 0;
  39.   if (L->base == L->top || tvisnil(L->base)) {  /* jit.on/off/flush([nil]) */
  40.     mode |= LUAJIT_MODE_ENGINE;
  41.   } else {
  42.     /* jit.on/off/flush(func|proto, nil|true|false) */
  43.     if (tvisfunc(L->base) || tvisproto(L->base))
  44.       idx = 1;
  45.     else if (!tvistrue(L->base))  /* jit.on/off/flush(true, nil|true|false) */
  46.       goto err;
  47.     if (L->base+1 < L->top && tvisbool(L->base+1))
  48.       mode |= boolV(L->base+1) ? LUAJIT_MODE_ALLFUNC : LUAJIT_MODE_ALLSUBFUNC;
  49.     else
  50.       mode |= LUAJIT_MODE_FUNC;
  51.   }
  52.   if (luaJIT_setmode(L, idx, mode) != 1) {
  53.     if ((mode & LUAJIT_MODE_MASK) == LUAJIT_MODE_ENGINE)
  54.       lj_err_caller(L, LJ_ERR_NOJIT);
  55.   err:
  56.     lj_err_argt(L, 1, LUA_TFUNCTION);
  57.   }
  58.   return 0;
  59. }

  60. LJLIB_CF(jit_on)
  61. {
  62.   return setjitmode(L, LUAJIT_MODE_ON);
  63. }

  64. LJLIB_CF(jit_off)
  65. {
  66.   return setjitmode(L, LUAJIT_MODE_OFF);
  67. }

  68. LJLIB_CF(jit_flush)
  69. {
  70. #if LJ_HASJIT
  71.   if (L->base < L->top && tvisnumber(L->base)) {
  72.     int traceno = lj_lib_checkint(L, 1);
  73.     luaJIT_setmode(L, traceno, LUAJIT_MODE_FLUSH|LUAJIT_MODE_TRACE);
  74.     return 0;
  75.   }
  76. #endif
  77.   return setjitmode(L, LUAJIT_MODE_FLUSH);
  78. }

  79. #if LJ_HASJIT
  80. /* Push a string for every flag bit that is set. */
  81. static void flagbits_to_strings(lua_State *L, uint32_t flags, uint32_t base,
  82.                                 const char *str)
  83. {
  84.   for (; *str; base <<= 1, str += 1+*str)
  85.     if (flags & base)
  86.       setstrV(L, L->top++, lj_str_new(L, str+1, *(uint8_t *)str));
  87. }
  88. #endif

  89. LJLIB_CF(jit_status)
  90. {
  91. #if LJ_HASJIT
  92.   jit_State *J = L2J(L);
  93.   L->top = L->base;
  94.   setboolV(L->top++, (J->flags & JIT_F_ON) ? 1 : 0);
  95.   flagbits_to_strings(L, J->flags, JIT_F_CPU_FIRST, JIT_F_CPUSTRING);
  96.   flagbits_to_strings(L, J->flags, JIT_F_OPT_FIRST, JIT_F_OPTSTRING);
  97.   return (int)(L->top - L->base);
  98. #else
  99.   setboolV(L->top++, 0);
  100.   return 1;
  101. #endif
  102. }

  103. LJLIB_CF(jit_attach)
  104. {
  105. #ifdef LUAJIT_DISABLE_VMEVENT
  106.   luaL_error(L, "vmevent API disabled");
  107. #else
  108.   GCfunc *fn = lj_lib_checkfunc(L, 1);
  109.   GCstr *s = lj_lib_optstr(L, 2);
  110.   luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE);
  111.   if (s) {  /* Attach to given event. */
  112.     const uint8_t *p = (const uint8_t *)strdata(s);
  113.     uint32_t h = s->len;
  114.     while (*p) h = h ^ (lj_rol(h, 6) + *p++);
  115.     lua_pushvalue(L, 1);
  116.     lua_rawseti(L, -2, VMEVENT_HASHIDX(h));
  117.     G(L)->vmevmask = VMEVENT_NOCACHE/* Invalidate cache. */
  118.   } else/* Detach if no event given. */
  119.     setnilV(L->top++);
  120.     while (lua_next(L, -2)) {
  121.       L->top--;
  122.       if (tvisfunc(L->top) && funcV(L->top) == fn) {
  123.         setnilV(lj_tab_set(L, tabV(L->top-2), L->top-1));
  124.       }
  125.     }
  126.   }
  127. #endif
  128.   return 0;
  129. }

  130. LJLIB_PUSH(top-5) LJLIB_SET(os)
  131. LJLIB_PUSH(top-4) LJLIB_SET(arch)
  132. LJLIB_PUSH(top-3) LJLIB_SET(version_num)
  133. LJLIB_PUSH(top-2) LJLIB_SET(version)

  134. #include "lj_libdef.h"

  135. /* -- jit.util.* functions ------------------------------------------------ */

  136. #define LJLIB_MODULE_jit_util

  137. /* -- Reflection API for Lua functions ------------------------------------ */

  138. /* Return prototype of first argument (Lua function or prototype object) */
  139. static GCproto *check_Lproto(lua_State *L, int nolua)
  140. {
  141.   TValue *o = L->base;
  142.   if (L->top > o) {
  143.     if (tvisproto(o)) {
  144.       return protoV(o);
  145.     } else if (tvisfunc(o)) {
  146.       if (isluafunc(funcV(o)))
  147.         return funcproto(funcV(o));
  148.       else if (nolua)
  149.         return NULL;
  150.     }
  151.   }
  152.   lj_err_argt(L, 1, LUA_TFUNCTION);
  153.   return NULL/* unreachable */
  154. }

  155. static void setintfield(lua_State *L, GCtab *t, const char *name, int32_t val)
  156. {
  157.   setintV(lj_tab_setstr(L, t, lj_str_newz(L, name)), val);
  158. }

  159. /* local info = jit.util.funcinfo(func [,pc]) */
  160. LJLIB_CF(jit_util_funcinfo)
  161. {
  162.   GCproto *pt = check_Lproto(L, 1);
  163.   if (pt) {
  164.     BCPos pc = (BCPos)lj_lib_optint(L, 2, 0);
  165.     GCtab *t;
  166.     lua_createtable(L, 0, 16);  /* Increment hash size if fields are added. */
  167.     t = tabV(L->top-1);
  168.     setintfield(L, t, "linedefined", pt->firstline);
  169.     setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline);
  170.     setintfield(L, t, "stackslots", pt->framesize);
  171.     setintfield(L, t, "params", pt->numparams);
  172.     setintfield(L, t, "bytecodes", (int32_t)pt->sizebc);
  173.     setintfield(L, t, "gcconsts", (int32_t)pt->sizekgc);
  174.     setintfield(L, t, "nconsts", (int32_t)pt->sizekn);
  175.     setintfield(L, t, "upvalues", (int32_t)pt->sizeuv);
  176.     if (pc < pt->sizebc)
  177.       setintfield(L, t, "currentline", lj_debug_line(pt, pc));
  178.     lua_pushboolean(L, (pt->flags & PROTO_VARARG));
  179.     lua_setfield(L, -2, "isvararg");
  180.     lua_pushboolean(L, (pt->flags & PROTO_CHILD));
  181.     lua_setfield(L, -2, "children");
  182.     setstrV(L, L->top++, proto_chunkname(pt));
  183.     lua_setfield(L, -2, "source");
  184.     lj_debug_pushloc(L, pt, pc);
  185.     lua_setfield(L, -2, "loc");
  186.   } else {
  187.     GCfunc *fn = funcV(L->base);
  188.     GCtab *t;
  189.     lua_createtable(L, 0, 4);  /* Increment hash size if fields are added. */
  190.     t = tabV(L->top-1);
  191.     if (!iscfunc(fn))
  192.       setintfield(L, t, "ffid", fn->c.ffid);
  193.     setintptrV(lj_tab_setstr(L, t, lj_str_newlit(L, "addr")),
  194.                (intptr_t)(void *)fn->c.f);
  195.     setintfield(L, t, "upvalues", fn->c.nupvalues);
  196.   }
  197.   return 1;
  198. }

  199. /* local ins, m = jit.util.funcbc(func, pc) */
  200. LJLIB_CF(jit_util_funcbc)
  201. {
  202.   GCproto *pt = check_Lproto(L, 0);
  203.   BCPos pc = (BCPos)lj_lib_checkint(L, 2);
  204.   if (pc < pt->sizebc) {
  205.     BCIns ins = proto_bc(pt)[pc];
  206.     BCOp op = bc_op(ins);
  207.     lua_assert(op < BC__MAX);
  208.     setintV(L->top, ins);
  209.     setintV(L->top+1, lj_bc_mode[op]);
  210.     L->top += 2;
  211.     return 2;
  212.   }
  213.   return 0;
  214. }

  215. /* local k = jit.util.funck(func, idx) */
  216. LJLIB_CF(jit_util_funck)
  217. {
  218.   GCproto *pt = check_Lproto(L, 0);
  219.   ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2);
  220.   if (idx >= 0) {
  221.     if (idx < (ptrdiff_t)pt->sizekn) {
  222.       copyTV(L, L->top-1, proto_knumtv(pt, idx));
  223.       return 1;
  224.     }
  225.   } else {
  226.     if (~idx < (ptrdiff_t)pt->sizekgc) {
  227.       GCobj *gc = proto_kgc(pt, idx);
  228.       setgcV(L, L->top-1, gc, ~gc->gch.gct);
  229.       return 1;
  230.     }
  231.   }
  232.   return 0;
  233. }

  234. /* local name = jit.util.funcuvname(func, idx) */
  235. LJLIB_CF(jit_util_funcuvname)
  236. {
  237.   GCproto *pt = check_Lproto(L, 0);
  238.   uint32_t idx = (uint32_t)lj_lib_checkint(L, 2);
  239.   if (idx < pt->sizeuv) {
  240.     setstrV(L, L->top-1, lj_str_newz(L, lj_debug_uvname(pt, idx)));
  241.     return 1;
  242.   }
  243.   return 0;
  244. }

  245. /* -- Reflection API for traces ------------------------------------------- */

  246. #if LJ_HASJIT

  247. /* Check trace argument. Must not throw for non-existent trace numbers. */
  248. static GCtrace *jit_checktrace(lua_State *L)
  249. {
  250.   TraceNo tr = (TraceNo)lj_lib_checkint(L, 1);
  251.   jit_State *J = L2J(L);
  252.   if (tr > 0 && tr < J->sizetrace)
  253.     return traceref(J, tr);
  254.   return NULL;
  255. }

  256. /* Names of link types. ORDER LJ_TRLINK */
  257. static const char *const jit_trlinkname[] = {
  258.   "none", "root", "loop", "tail-recursion", "up-recursion", "down-recursion",
  259.   "interpreter", "return", "stitch"
  260. };

  261. /* local info = jit.util.traceinfo(tr) */
  262. LJLIB_CF(jit_util_traceinfo)
  263. {
  264.   GCtrace *T = jit_checktrace(L);
  265.   if (T) {
  266.     GCtab *t;
  267.     lua_createtable(L, 0, 8);  /* Increment hash size if fields are added. */
  268.     t = tabV(L->top-1);
  269.     setintfield(L, t, "nins", (int32_t)T->nins - REF_BIAS - 1);
  270.     setintfield(L, t, "nk", REF_BIAS - (int32_t)T->nk);
  271.     setintfield(L, t, "link", T->link);
  272.     setintfield(L, t, "nexit", T->nsnap);
  273.     setstrV(L, L->top++, lj_str_newz(L, jit_trlinkname[T->linktype]));
  274.     lua_setfield(L, -2, "linktype");
  275.     /* There are many more fields. Add them only when needed. */
  276.     return 1;
  277.   }
  278.   return 0;
  279. }

  280. /* local m, ot, op1, op2, prev = jit.util.traceir(tr, idx) */
  281. LJLIB_CF(jit_util_traceir)
  282. {
  283.   GCtrace *T = jit_checktrace(L);
  284.   IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS;
  285.   if (T && ref >= REF_BIAS && ref < T->nins) {
  286.     IRIns *ir = &T->ir[ref];
  287.     int32_t m = lj_ir_mode[ir->o];
  288.     setintV(L->top-2, m);
  289.     setintV(L->top-1, ir->ot);
  290.     setintV(L->top++, (int32_t)ir->op1 - (irm_op1(m)==IRMref ? REF_BIAS : 0));
  291.     setintV(L->top++, (int32_t)ir->op2 - (irm_op2(m)==IRMref ? REF_BIAS : 0));
  292.     setintV(L->top++, ir->prev);
  293.     return 5;
  294.   }
  295.   return 0;
  296. }

  297. /* local k, t [, slot] = jit.util.tracek(tr, idx) */
  298. LJLIB_CF(jit_util_tracek)
  299. {
  300.   GCtrace *T = jit_checktrace(L);
  301.   IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS;
  302.   if (T && ref >= T->nk && ref < REF_BIAS) {
  303.     IRIns *ir = &T->ir[ref];
  304.     int32_t slot = -1;
  305.     if (ir->o == IR_KSLOT) {
  306.       slot = ir->op2;
  307.       ir = &T->ir[ir->op1];
  308.     }
  309. #if LJ_HASFFI
  310.     if (ir->o == IR_KINT64 && !ctype_ctsG(G(L))) {
  311.       ptrdiff_t oldtop = savestack(L, L->top);
  312.       luaopen_ffi(L);  /* Load FFI library on-demand. */
  313.       L->top = restorestack(L, oldtop);
  314.     }
  315. #endif
  316.     lj_ir_kvalue(L, L->top-2, ir);
  317.     setintV(L->top-1, (int32_t)irt_type(ir->t));
  318.     if (slot == -1)
  319.       return 2;
  320.     setintV(L->top++, slot);
  321.     return 3;
  322.   }
  323.   return 0;
  324. }

  325. /* local snap = jit.util.tracesnap(tr, sn) */
  326. LJLIB_CF(jit_util_tracesnap)
  327. {
  328.   GCtrace *T = jit_checktrace(L);
  329.   SnapNo sn = (SnapNo)lj_lib_checkint(L, 2);
  330.   if (T && sn < T->nsnap) {
  331.     SnapShot *snap = &T->snap[sn];
  332.     SnapEntry *map = &T->snapmap[snap->mapofs];
  333.     MSize n, nent = snap->nent;
  334.     GCtab *t;
  335.     lua_createtable(L, nent+2, 0);
  336.     t = tabV(L->top-1);
  337.     setintV(lj_tab_setint(L, t, 0), (int32_t)snap->ref - REF_BIAS);
  338.     setintV(lj_tab_setint(L, t, 1), (int32_t)snap->nslots);
  339.     for (n = 0; n < nent; n++)
  340.       setintV(lj_tab_setint(L, t, (int32_t)(n+2)), (int32_t)map[n]);
  341.     setintV(lj_tab_setint(L, t, (int32_t)(nent+2)), (int32_t)SNAP(255, 0, 0));
  342.     return 1;
  343.   }
  344.   return 0;
  345. }

  346. /* local mcode, addr, loop = jit.util.tracemc(tr) */
  347. LJLIB_CF(jit_util_tracemc)
  348. {
  349.   GCtrace *T = jit_checktrace(L);
  350.   if (T && T->mcode != NULL) {
  351.     setstrV(L, L->top-1, lj_str_new(L, (const char *)T->mcode, T->szmcode));
  352.     setintptrV(L->top++, (intptr_t)(void *)T->mcode);
  353.     setintV(L->top++, T->mcloop);
  354.     return 3;
  355.   }
  356.   return 0;
  357. }

  358. /* local addr = jit.util.traceexitstub([tr,] exitno) */
  359. LJLIB_CF(jit_util_traceexitstub)
  360. {
  361. #ifdef EXITSTUBS_PER_GROUP
  362.   ExitNo exitno = (ExitNo)lj_lib_checkint(L, 1);
  363.   jit_State *J = L2J(L);
  364.   if (exitno < EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) {
  365.     setintptrV(L->top-1, (intptr_t)(void *)exitstub_addr(J, exitno));
  366.     return 1;
  367.   }
  368. #else
  369.   if (L->top > L->base+1) {  /* Don't throw for one-argument variant. */
  370.     GCtrace *T = jit_checktrace(L);
  371.     ExitNo exitno = (ExitNo)lj_lib_checkint(L, 2);
  372.     ExitNo maxexit = T->root ? T->nsnap+1 : T->nsnap;
  373.     if (T && T->mcode != NULL && exitno < maxexit) {
  374.       setintptrV(L->top-1, (intptr_t)(void *)exitstub_trace_addr(T, exitno));
  375.       return 1;
  376.     }
  377.   }
  378. #endif
  379.   return 0;
  380. }

  381. /* local addr = jit.util.ircalladdr(idx) */
  382. LJLIB_CF(jit_util_ircalladdr)
  383. {
  384.   uint32_t idx = (uint32_t)lj_lib_checkint(L, 1);
  385.   if (idx < IRCALL__MAX) {
  386.     setintptrV(L->top-1, (intptr_t)(void *)lj_ir_callinfo[idx].func);
  387.     return 1;
  388.   }
  389.   return 0;
  390. }

  391. #endif

  392. #include "lj_libdef.h"

  393. static int luaopen_jit_util(lua_State *L)
  394. {
  395.   LJ_LIB_REG(L, NULL, jit_util);
  396.   return 1;
  397. }

  398. /* -- jit.opt module ------------------------------------------------------ */

  399. #if LJ_HASJIT

  400. #define LJLIB_MODULE_jit_opt

  401. /* Parse optimization level. */
  402. static int jitopt_level(jit_State *J, const char *str)
  403. {
  404.   if (str[0] >= '0' && str[0] <= '9' && str[1] == '\0') {
  405.     uint32_t flags;
  406.     if (str[0] == '0') flags = JIT_F_OPT_0;
  407.     else if (str[0] == '1') flags = JIT_F_OPT_1;
  408.     else if (str[0] == '2') flags = JIT_F_OPT_2;
  409.     else flags = JIT_F_OPT_3;
  410.     J->flags = (J->flags & ~JIT_F_OPT_MASK) | flags;
  411.     return 1/* Ok. */
  412.   }
  413.   return 0/* No match. */
  414. }

  415. /* Parse optimization flag. */
  416. static int jitopt_flag(jit_State *J, const char *str)
  417. {
  418.   const char *lst = JIT_F_OPTSTRING;
  419.   uint32_t opt;
  420.   int set = 1;
  421.   if (str[0] == '+') {
  422.     str++;
  423.   } else if (str[0] == '-') {
  424.     str++;
  425.     set = 0;
  426.   } else if (str[0] == 'n' && str[1] == 'o') {
  427.     str += str[2] == '-' ? 3 : 2;
  428.     set = 0;
  429.   }
  430.   for (opt = JIT_F_OPT_FIRST; ; opt <<= 1) {
  431.     size_t len = *(const uint8_t *)lst;
  432.     if (len == 0)
  433.       break;
  434.     if (strncmp(str, lst+1, len) == 0 && str[len] == '\0') {
  435.       if (set) J->flags |= opt; else J->flags &= ~opt;
  436.       return 1/* Ok. */
  437.     }
  438.     lst += 1+len;
  439.   }
  440.   return 0/* No match. */
  441. }

  442. /* Parse optimization parameter. */
  443. static int jitopt_param(jit_State *J, const char *str)
  444. {
  445.   const char *lst = JIT_P_STRING;
  446.   int i;
  447.   for (i = 0; i < JIT_P__MAX; i++) {
  448.     size_t len = *(const uint8_t *)lst;
  449.     lua_assert(len != 0);
  450.     if (strncmp(str, lst+1, len) == 0 && str[len] == '=') {
  451.       int32_t n = 0;
  452.       const char *p = &str[len+1];
  453.       while (*p >= '0' && *p <= '9')
  454.         n = n*10 + (*p++ - '0');
  455.       if (*p) return 0/* Malformed number. */
  456.       J->param[i] = n;
  457.       if (i == JIT_P_hotloop)
  458.         lj_dispatch_init_hotcount(J2G(J));
  459.       return 1/* Ok. */
  460.     }
  461.     lst += 1+len;
  462.   }
  463.   return 0/* No match. */
  464. }

  465. /* jit.opt.start(flags...) */
  466. LJLIB_CF(jit_opt_start)
  467. {
  468.   jit_State *J = L2J(L);
  469.   int nargs = (int)(L->top - L->base);
  470.   if (nargs == 0) {
  471.     J->flags = (J->flags & ~JIT_F_OPT_MASK) | JIT_F_OPT_DEFAULT;
  472.   } else {
  473.     int i;
  474.     for (i = 1; i <= nargs; i++) {
  475.       const char *str = strdata(lj_lib_checkstr(L, i));
  476.       if (!jitopt_level(J, str) &&
  477.           !jitopt_flag(J, str) &&
  478.           !jitopt_param(J, str))
  479.         lj_err_callerv(L, LJ_ERR_JITOPT, str);
  480.     }
  481.   }
  482.   return 0;
  483. }

  484. #include "lj_libdef.h"

  485. #endif

  486. /* -- jit.profile module -------------------------------------------------- */

  487. #if LJ_HASPROFILE

  488. #define LJLIB_MODULE_jit_profile

  489. /* Not loaded by default, use: local profile = require("jit.profile") */

  490. static const char KEY_PROFILE_THREAD = 't';
  491. static const char KEY_PROFILE_FUNC = 'f';

  492. static void jit_profile_callback(lua_State *L2, lua_State *L, int samples,
  493.                                  int vmstate)
  494. {
  495.   TValue key;
  496.   cTValue *tv;
  497.   setlightudV(&key, (void *)&KEY_PROFILE_FUNC);
  498.   tv = lj_tab_get(L, tabV(registry(L)), &key);
  499.   if (tvisfunc(tv)) {
  500.     char vmst = (char)vmstate;
  501.     int status;
  502.     setfuncV(L2, L2->top++, funcV(tv));
  503.     setthreadV(L2, L2->top++, L);
  504.     setintV(L2->top++, samples);
  505.     setstrV(L2, L2->top++, lj_str_new(L2, &vmst, 1));
  506.     status = lua_pcall(L2, 3, 0, 0);  /* callback(thread, samples, vmstate) */
  507.     if (status) {
  508.       if (G(L2)->panic) G(L2)->panic(L2);
  509.       exit(EXIT_FAILURE);
  510.     }
  511.     lj_trace_abort(G(L2));
  512.   }
  513. }

  514. /* profile.start(mode, cb) */
  515. LJLIB_CF(jit_profile_start)
  516. {
  517.   GCtab *registry = tabV(registry(L));
  518.   GCstr *mode = lj_lib_optstr(L, 1);
  519.   GCfunc *func = lj_lib_checkfunc(L, 2);
  520.   lua_State *L2 = lua_newthread(L);  /* Thread that runs profiler callback. */
  521.   TValue key;
  522.   /* Anchor thread and function in registry. */
  523.   setlightudV(&key, (void *)&KEY_PROFILE_THREAD);
  524.   setthreadV(L, lj_tab_set(L, registry, &key), L2);
  525.   setlightudV(&key, (void *)&KEY_PROFILE_FUNC);
  526.   setfuncV(L, lj_tab_set(L, registry, &key), func);
  527.   lj_gc_anybarriert(L, registry);
  528.   luaJIT_profile_start(L, mode ? strdata(mode) : "",
  529.                        (luaJIT_profile_callback)jit_profile_callback, L2);
  530.   return 0;
  531. }

  532. /* profile.stop() */
  533. LJLIB_CF(jit_profile_stop)
  534. {
  535.   GCtab *registry;
  536.   TValue key;
  537.   luaJIT_profile_stop(L);
  538.   registry = tabV(registry(L));
  539.   setlightudV(&key, (void *)&KEY_PROFILE_THREAD);
  540.   setnilV(lj_tab_set(L, registry, &key));
  541.   setlightudV(&key, (void *)&KEY_PROFILE_FUNC);
  542.   setnilV(lj_tab_set(L, registry, &key));
  543.   lj_gc_anybarriert(L, registry);
  544.   return 0;
  545. }

  546. /* dump = profile.dumpstack([thread,] fmt, depth) */
  547. LJLIB_CF(jit_profile_dumpstack)
  548. {
  549.   lua_State *L2 = L;
  550.   int arg = 0;
  551.   size_t len;
  552.   int depth;
  553.   GCstr *fmt;
  554.   const char *p;
  555.   if (L->top > L->base && tvisthread(L->base)) {
  556.     L2 = threadV(L->base);
  557.     arg = 1;
  558.   }
  559.   fmt = lj_lib_checkstr(L, arg+1);
  560.   depth = lj_lib_checkint(L, arg+2);
  561.   p = luaJIT_profile_dumpstack(L2, strdata(fmt), depth, &len);
  562.   lua_pushlstring(L, p, len);
  563.   return 1;
  564. }

  565. #include "lj_libdef.h"

  566. static int luaopen_jit_profile(lua_State *L)
  567. {
  568.   LJ_LIB_REG(L, NULL, jit_profile);
  569.   return 1;
  570. }

  571. #endif

  572. /* -- JIT compiler initialization ----------------------------------------- */

  573. #if LJ_HASJIT
  574. /* Default values for JIT parameters. */
  575. static const int32_t jit_param_default[JIT_P__MAX+1] = {
  576. #define JIT_PARAMINIT(len, name, value)        (value),
  577. JIT_PARAMDEF(JIT_PARAMINIT)
  578. #undef JIT_PARAMINIT
  579.   0
  580. };
  581. #endif

  582. #if LJ_TARGET_ARM && LJ_TARGET_LINUX
  583. #include <sys/utsname.h>
  584. #endif

  585. /* Arch-dependent CPU detection. */
  586. static uint32_t jit_cpudetect(lua_State *L)
  587. {
  588.   uint32_t flags = 0;
  589. #if LJ_TARGET_X86ORX64
  590.   uint32_t vendor[4];
  591.   uint32_t features[4];
  592.   if (lj_vm_cpuid(0, vendor) && lj_vm_cpuid(1, features)) {
  593. #if !LJ_HASJIT
  594. #define JIT_F_SSE2        2
  595. #endif
  596.     flags |= ((features[3] >> 26)&1) * JIT_F_SSE2;
  597. #if LJ_HASJIT
  598.     flags |= ((features[2] >> 0)&1) * JIT_F_SSE3;
  599.     flags |= ((features[2] >> 19)&1) * JIT_F_SSE4_1;
  600.     if (vendor[2] == 0x6c65746e) {  /* Intel. */
  601.       if ((features[0] & 0x0fff0ff0) == 0x000106c0/* Atom. */
  602.         flags |= JIT_F_LEA_AGU;
  603.     } else if (vendor[2] == 0x444d4163) {  /* AMD. */
  604.       uint32_t fam = (features[0] & 0x0ff00f00);
  605.       if (fam >= 0x00000f00/* K8, K10. */
  606.         flags |= JIT_F_PREFER_IMUL;
  607.     }
  608. #endif
  609.   }
  610.   /* Check for required instruction set support on x86 (unnecessary on x64). */
  611. #if LJ_TARGET_X86
  612.   if (!(flags & JIT_F_SSE2))
  613.     luaL_error(L, "CPU with SSE2 required");
  614. #endif
  615. #elif LJ_TARGET_ARM
  616. #if LJ_HASJIT
  617.   int ver = LJ_ARCH_VERSION/* Compile-time ARM CPU detection. */
  618. #if LJ_TARGET_LINUX
  619.   if (ver < 70) {  /* Runtime ARM CPU detection. */
  620.     struct utsname ut;
  621.     uname(&ut);
  622.     if (strncmp(ut.machine, "armv", 4) == 0) {
  623.       if (ut.machine[4] >= '7')
  624.         ver = 70;
  625.       else if (ut.machine[4] == '6')
  626.         ver = 60;
  627.     }
  628.   }
  629. #endif
  630.   flags |= ver >= 70 ? JIT_F_ARMV7 :
  631.            ver >= 61 ? JIT_F_ARMV6T2_ :
  632.            ver >= 60 ? JIT_F_ARMV6_ : 0;
  633.   flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2;
  634. #endif
  635. #elif LJ_TARGET_ARM64
  636.   /* No optional CPU features to detect (for now). */
  637. #elif LJ_TARGET_PPC
  638. #if LJ_HASJIT
  639. #if LJ_ARCH_SQRT
  640.   flags |= JIT_F_SQRT;
  641. #endif
  642. #if LJ_ARCH_ROUND
  643.   flags |= JIT_F_ROUND;
  644. #endif
  645. #endif
  646. #elif LJ_TARGET_MIPS
  647. #if LJ_HASJIT
  648.   /* Compile-time MIPS CPU detection. */
  649. #if LJ_ARCH_VERSION >= 20
  650.   flags |= JIT_F_MIPS32R2;
  651. #endif
  652.   /* Runtime MIPS CPU detection. */
  653. #if defined(__GNUC__)
  654.   if (!(flags & JIT_F_MIPS32R2)) {
  655.     int x;
  656.     /* On MIPS32R1 rotr is treated as srl. rotr r2,r2,1 -> srl r2,r2,1. */
  657.     __asm__("li $2, 1\n\t.long 0x00221042\n\tmove %0, $2" : "=r"(x) : : "$2");
  658.     if (x) flags |= JIT_F_MIPS32R2/* Either 0x80000000 (R2) or 0 (R1). */
  659.   }
  660. #endif
  661. #endif
  662. #else
  663. #error "Missing CPU detection for this architecture"
  664. #endif
  665.   UNUSED(L);
  666.   return flags;
  667. }

  668. /* Initialize JIT compiler. */
  669. static void jit_init(lua_State *L)
  670. {
  671.   uint32_t flags = jit_cpudetect(L);
  672. #if LJ_HASJIT
  673.   jit_State *J = L2J(L);
  674.   J->flags = flags | JIT_F_ON | JIT_F_OPT_DEFAULT;
  675.   memcpy(J->param, jit_param_default, sizeof(J->param));
  676.   lj_dispatch_update(G(L));
  677. #else
  678.   UNUSED(flags);
  679. #endif
  680. }

  681. LUALIB_API int luaopen_jit(lua_State *L)
  682. {
  683.   jit_init(L);
  684.   lua_pushliteral(L, LJ_OS_NAME);
  685.   lua_pushliteral(L, LJ_ARCH_NAME);
  686.   lua_pushinteger(L, LUAJIT_VERSION_NUM);
  687.   lua_pushliteral(L, LUAJIT_VERSION);
  688.   LJ_LIB_REG(L, LUA_JITLIBNAME, jit);
  689. #if LJ_HASPROFILE
  690.   lj_lib_prereg(L, LUA_JITLIBNAME ".profile", luaopen_jit_profile,
  691.                 tabref(L->env));
  692. #endif
  693. #ifndef LUAJIT_DISABLE_JITUTIL
  694.   lj_lib_prereg(L, LUA_JITLIBNAME ".util", luaopen_jit_util, tabref(L->env));
  695. #endif
  696. #if LJ_HASJIT
  697.   LJ_LIB_REG(L, "jit.opt", jit_opt);
  698. #endif
  699.   L->top -= 2;
  700.   return 1;
  701. }