lib_jit.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. ** JIT library.
  3. ** Copyright (C) 2005-2023 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, JIT_F_CPUSTRING);
  96. flagbits_to_strings(L, J->flags, JIT_F_OPT, 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_security)
  104. {
  105. int idx = lj_lib_checkopt(L, 1, -1, LJ_SECURITY_MODESTRING);
  106. setintV(L->top++, ((LJ_SECURITY_MODE >> (2*idx)) & 3));
  107. return 1;
  108. }
  109. LJLIB_CF(jit_attach)
  110. {
  111. #ifdef LUAJIT_DISABLE_VMEVENT
  112. luaL_error(L, "vmevent API disabled");
  113. #else
  114. GCfunc *fn = lj_lib_checkfunc(L, 1);
  115. GCstr *s = lj_lib_optstr(L, 2);
  116. luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE);
  117. if (s) { /* Attach to given event. */
  118. const uint8_t *p = (const uint8_t *)strdata(s);
  119. uint32_t h = s->len;
  120. while (*p) h = h ^ (lj_rol(h, 6) + *p++);
  121. lua_pushvalue(L, 1);
  122. lua_rawseti(L, -2, VMEVENT_HASHIDX(h));
  123. G(L)->vmevmask = VMEVENT_NOCACHE; /* Invalidate cache. */
  124. } else { /* Detach if no event given. */
  125. setnilV(L->top++);
  126. while (lua_next(L, -2)) {
  127. L->top--;
  128. if (tvisfunc(L->top) && funcV(L->top) == fn) {
  129. setnilV(lj_tab_set(L, tabV(L->top-2), L->top-1));
  130. }
  131. }
  132. }
  133. #endif
  134. return 0;
  135. }
  136. LJLIB_PUSH(top-5) LJLIB_SET(os)
  137. LJLIB_PUSH(top-4) LJLIB_SET(arch)
  138. LJLIB_PUSH(top-3) LJLIB_SET(version_num)
  139. LJLIB_PUSH(top-2) LJLIB_SET(version)
  140. #include "lj_libdef.h"
  141. /* -- jit.util.* functions ------------------------------------------------ */
  142. #define LJLIB_MODULE_jit_util
  143. /* -- Reflection API for Lua functions ------------------------------------ */
  144. static void setintfield(lua_State *L, GCtab *t, const char *name, int32_t val)
  145. {
  146. setintV(lj_tab_setstr(L, t, lj_str_newz(L, name)), val);
  147. }
  148. /* local info = jit.util.funcinfo(func [,pc]) */
  149. LJLIB_CF(jit_util_funcinfo)
  150. {
  151. GCproto *pt = lj_lib_checkLproto(L, 1, 1);
  152. if (pt) {
  153. BCPos pc = (BCPos)lj_lib_optint(L, 2, 0);
  154. GCtab *t;
  155. lua_createtable(L, 0, 16); /* Increment hash size if fields are added. */
  156. t = tabV(L->top-1);
  157. setintfield(L, t, "linedefined", pt->firstline);
  158. setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline);
  159. setintfield(L, t, "stackslots", pt->framesize);
  160. setintfield(L, t, "params", pt->numparams);
  161. setintfield(L, t, "bytecodes", (int32_t)pt->sizebc);
  162. setintfield(L, t, "gcconsts", (int32_t)pt->sizekgc);
  163. setintfield(L, t, "nconsts", (int32_t)pt->sizekn);
  164. setintfield(L, t, "upvalues", (int32_t)pt->sizeuv);
  165. if (pc < pt->sizebc)
  166. setintfield(L, t, "currentline", lj_debug_line(pt, pc));
  167. lua_pushboolean(L, (pt->flags & PROTO_VARARG));
  168. lua_setfield(L, -2, "isvararg");
  169. lua_pushboolean(L, (pt->flags & PROTO_CHILD));
  170. lua_setfield(L, -2, "children");
  171. setstrV(L, L->top++, proto_chunkname(pt));
  172. lua_setfield(L, -2, "source");
  173. lj_debug_pushloc(L, pt, pc);
  174. lua_setfield(L, -2, "loc");
  175. setprotoV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "proto")), pt);
  176. } else {
  177. GCfunc *fn = funcV(L->base);
  178. GCtab *t;
  179. lua_createtable(L, 0, 4); /* Increment hash size if fields are added. */
  180. t = tabV(L->top-1);
  181. if (!iscfunc(fn))
  182. setintfield(L, t, "ffid", fn->c.ffid);
  183. setintptrV(lj_tab_setstr(L, t, lj_str_newlit(L, "addr")),
  184. (intptr_t)(void *)fn->c.f);
  185. setintfield(L, t, "upvalues", fn->c.nupvalues);
  186. }
  187. return 1;
  188. }
  189. /* local ins, m = jit.util.funcbc(func, pc) */
  190. LJLIB_CF(jit_util_funcbc)
  191. {
  192. GCproto *pt = lj_lib_checkLproto(L, 1, 0);
  193. BCPos pc = (BCPos)lj_lib_checkint(L, 2);
  194. if (pc < pt->sizebc) {
  195. BCIns ins = proto_bc(pt)[pc];
  196. BCOp op = bc_op(ins);
  197. lj_assertL(op < BC__MAX, "bad bytecode op %d", op);
  198. setintV(L->top, ins);
  199. setintV(L->top+1, lj_bc_mode[op]);
  200. L->top += 2;
  201. return 2;
  202. }
  203. return 0;
  204. }
  205. /* local k = jit.util.funck(func, idx) */
  206. LJLIB_CF(jit_util_funck)
  207. {
  208. GCproto *pt = lj_lib_checkLproto(L, 1, 0);
  209. ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2);
  210. if (idx >= 0) {
  211. if (idx < (ptrdiff_t)pt->sizekn) {
  212. copyTV(L, L->top-1, proto_knumtv(pt, idx));
  213. return 1;
  214. }
  215. } else {
  216. if (~idx < (ptrdiff_t)pt->sizekgc) {
  217. GCobj *gc = proto_kgc(pt, idx);
  218. setgcV(L, L->top-1, gc, ~gc->gch.gct);
  219. return 1;
  220. }
  221. }
  222. return 0;
  223. }
  224. /* local name = jit.util.funcuvname(func, idx) */
  225. LJLIB_CF(jit_util_funcuvname)
  226. {
  227. GCproto *pt = lj_lib_checkLproto(L, 1, 0);
  228. uint32_t idx = (uint32_t)lj_lib_checkint(L, 2);
  229. if (idx < pt->sizeuv) {
  230. setstrV(L, L->top-1, lj_str_newz(L, lj_debug_uvname(pt, idx)));
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. /* -- Reflection API for traces ------------------------------------------- */
  236. #if LJ_HASJIT
  237. /* Check trace argument. Must not throw for non-existent trace numbers. */
  238. static GCtrace *jit_checktrace(lua_State *L)
  239. {
  240. TraceNo tr = (TraceNo)lj_lib_checkint(L, 1);
  241. jit_State *J = L2J(L);
  242. if (tr > 0 && tr < J->sizetrace)
  243. return traceref(J, tr);
  244. return NULL;
  245. }
  246. /* Names of link types. ORDER LJ_TRLINK */
  247. static const char *const jit_trlinkname[] = {
  248. "none", "root", "loop", "tail-recursion", "up-recursion", "down-recursion",
  249. "interpreter", "return", "stitch"
  250. };
  251. /* local info = jit.util.traceinfo(tr) */
  252. LJLIB_CF(jit_util_traceinfo)
  253. {
  254. GCtrace *T = jit_checktrace(L);
  255. if (T) {
  256. GCtab *t;
  257. lua_createtable(L, 0, 8); /* Increment hash size if fields are added. */
  258. t = tabV(L->top-1);
  259. setintfield(L, t, "nins", (int32_t)T->nins - REF_BIAS - 1);
  260. setintfield(L, t, "nk", REF_BIAS - (int32_t)T->nk);
  261. setintfield(L, t, "link", T->link);
  262. setintfield(L, t, "nexit", T->nsnap);
  263. setstrV(L, L->top++, lj_str_newz(L, jit_trlinkname[T->linktype]));
  264. lua_setfield(L, -2, "linktype");
  265. /* There are many more fields. Add them only when needed. */
  266. return 1;
  267. }
  268. return 0;
  269. }
  270. /* local m, ot, op1, op2, prev = jit.util.traceir(tr, idx) */
  271. LJLIB_CF(jit_util_traceir)
  272. {
  273. GCtrace *T = jit_checktrace(L);
  274. IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS;
  275. if (T && ref >= REF_BIAS && ref < T->nins) {
  276. IRIns *ir = &T->ir[ref];
  277. int32_t m = lj_ir_mode[ir->o];
  278. setintV(L->top-2, m);
  279. setintV(L->top-1, ir->ot);
  280. setintV(L->top++, (int32_t)ir->op1 - (irm_op1(m)==IRMref ? REF_BIAS : 0));
  281. setintV(L->top++, (int32_t)ir->op2 - (irm_op2(m)==IRMref ? REF_BIAS : 0));
  282. setintV(L->top++, ir->prev);
  283. return 5;
  284. }
  285. return 0;
  286. }
  287. /* local k, t [, slot] = jit.util.tracek(tr, idx) */
  288. LJLIB_CF(jit_util_tracek)
  289. {
  290. GCtrace *T = jit_checktrace(L);
  291. IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS;
  292. if (T && ref >= T->nk && ref < REF_BIAS) {
  293. IRIns *ir = &T->ir[ref];
  294. int32_t slot = -1;
  295. if (ir->o == IR_KSLOT) {
  296. slot = ir->op2;
  297. ir = &T->ir[ir->op1];
  298. }
  299. #if LJ_HASFFI
  300. if (ir->o == IR_KINT64) ctype_loadffi(L);
  301. #endif
  302. lj_ir_kvalue(L, L->top-2, ir);
  303. setintV(L->top-1, (int32_t)irt_type(ir->t));
  304. if (slot == -1)
  305. return 2;
  306. setintV(L->top++, slot);
  307. return 3;
  308. }
  309. return 0;
  310. }
  311. /* local snap = jit.util.tracesnap(tr, sn) */
  312. LJLIB_CF(jit_util_tracesnap)
  313. {
  314. GCtrace *T = jit_checktrace(L);
  315. SnapNo sn = (SnapNo)lj_lib_checkint(L, 2);
  316. if (T && sn < T->nsnap) {
  317. SnapShot *snap = &T->snap[sn];
  318. SnapEntry *map = &T->snapmap[snap->mapofs];
  319. MSize n, nent = snap->nent;
  320. GCtab *t;
  321. lua_createtable(L, nent+2, 0);
  322. t = tabV(L->top-1);
  323. setintV(lj_tab_setint(L, t, 0), (int32_t)snap->ref - REF_BIAS);
  324. setintV(lj_tab_setint(L, t, 1), (int32_t)snap->nslots);
  325. for (n = 0; n < nent; n++)
  326. setintV(lj_tab_setint(L, t, (int32_t)(n+2)), (int32_t)map[n]);
  327. setintV(lj_tab_setint(L, t, (int32_t)(nent+2)), (int32_t)SNAP(255, 0, 0));
  328. return 1;
  329. }
  330. return 0;
  331. }
  332. /* local mcode, addr, loop = jit.util.tracemc(tr) */
  333. LJLIB_CF(jit_util_tracemc)
  334. {
  335. GCtrace *T = jit_checktrace(L);
  336. if (T && T->mcode != NULL) {
  337. setstrV(L, L->top-1, lj_str_new(L, (const char *)T->mcode, T->szmcode));
  338. setintptrV(L->top++, (intptr_t)(void *)T->mcode);
  339. setintV(L->top++, T->mcloop);
  340. return 3;
  341. }
  342. return 0;
  343. }
  344. /* local addr = jit.util.traceexitstub([tr,] exitno) */
  345. LJLIB_CF(jit_util_traceexitstub)
  346. {
  347. #ifdef EXITSTUBS_PER_GROUP
  348. ExitNo exitno = (ExitNo)lj_lib_checkint(L, 1);
  349. jit_State *J = L2J(L);
  350. if (exitno < EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) {
  351. setintptrV(L->top-1, (intptr_t)(void *)exitstub_addr(J, exitno));
  352. return 1;
  353. }
  354. #else
  355. if (L->top > L->base+1) { /* Don't throw for one-argument variant. */
  356. GCtrace *T = jit_checktrace(L);
  357. ExitNo exitno = (ExitNo)lj_lib_checkint(L, 2);
  358. ExitNo maxexit = T->root ? T->nsnap+1 : T->nsnap;
  359. if (T && T->mcode != NULL && exitno < maxexit) {
  360. setintptrV(L->top-1, (intptr_t)(void *)exitstub_trace_addr(T, exitno));
  361. return 1;
  362. }
  363. }
  364. #endif
  365. return 0;
  366. }
  367. /* local addr = jit.util.ircalladdr(idx) */
  368. LJLIB_CF(jit_util_ircalladdr)
  369. {
  370. uint32_t idx = (uint32_t)lj_lib_checkint(L, 1);
  371. if (idx < IRCALL__MAX) {
  372. ASMFunction func = lj_ir_callinfo[idx].func;
  373. setintptrV(L->top-1, (intptr_t)(void *)lj_ptr_strip(func));
  374. return 1;
  375. }
  376. return 0;
  377. }
  378. #endif
  379. #include "lj_libdef.h"
  380. static int luaopen_jit_util(lua_State *L)
  381. {
  382. LJ_LIB_REG(L, NULL, jit_util);
  383. return 1;
  384. }
  385. /* -- jit.opt module ------------------------------------------------------ */
  386. #if LJ_HASJIT
  387. #define LJLIB_MODULE_jit_opt
  388. /* Parse optimization level. */
  389. static int jitopt_level(jit_State *J, const char *str)
  390. {
  391. if (str[0] >= '0' && str[0] <= '9' && str[1] == '\0') {
  392. uint32_t flags;
  393. if (str[0] == '0') flags = JIT_F_OPT_0;
  394. else if (str[0] == '1') flags = JIT_F_OPT_1;
  395. else if (str[0] == '2') flags = JIT_F_OPT_2;
  396. else flags = JIT_F_OPT_3;
  397. J->flags = (J->flags & ~JIT_F_OPT_MASK) | flags;
  398. return 1; /* Ok. */
  399. }
  400. return 0; /* No match. */
  401. }
  402. /* Parse optimization flag. */
  403. static int jitopt_flag(jit_State *J, const char *str)
  404. {
  405. const char *lst = JIT_F_OPTSTRING;
  406. uint32_t opt;
  407. int set = 1;
  408. if (str[0] == '+') {
  409. str++;
  410. } else if (str[0] == '-') {
  411. str++;
  412. set = 0;
  413. } else if (str[0] == 'n' && str[1] == 'o') {
  414. str += str[2] == '-' ? 3 : 2;
  415. set = 0;
  416. }
  417. for (opt = JIT_F_OPT; ; opt <<= 1) {
  418. size_t len = *(const uint8_t *)lst;
  419. if (len == 0)
  420. break;
  421. if (strncmp(str, lst+1, len) == 0 && str[len] == '\0') {
  422. if (set) J->flags |= opt; else J->flags &= ~opt;
  423. return 1; /* Ok. */
  424. }
  425. lst += 1+len;
  426. }
  427. return 0; /* No match. */
  428. }
  429. /* Parse optimization parameter. */
  430. static int jitopt_param(jit_State *J, const char *str)
  431. {
  432. const char *lst = JIT_P_STRING;
  433. int i;
  434. for (i = 0; i < JIT_P__MAX; i++) {
  435. size_t len = *(const uint8_t *)lst;
  436. lj_assertJ(len != 0, "bad JIT_P_STRING");
  437. if (strncmp(str, lst+1, len) == 0 && str[len] == '=') {
  438. int32_t n = 0;
  439. const char *p = &str[len+1];
  440. while (*p >= '0' && *p <= '9')
  441. n = n*10 + (*p++ - '0');
  442. if (*p) return 0; /* Malformed number. */
  443. J->param[i] = n;
  444. if (i == JIT_P_hotloop)
  445. lj_dispatch_init_hotcount(J2G(J));
  446. return 1; /* Ok. */
  447. }
  448. lst += 1+len;
  449. }
  450. return 0; /* No match. */
  451. }
  452. /* jit.opt.start(flags...) */
  453. LJLIB_CF(jit_opt_start)
  454. {
  455. jit_State *J = L2J(L);
  456. int nargs = (int)(L->top - L->base);
  457. if (nargs == 0) {
  458. J->flags = (J->flags & ~JIT_F_OPT_MASK) | JIT_F_OPT_DEFAULT;
  459. } else {
  460. int i;
  461. for (i = 1; i <= nargs; i++) {
  462. const char *str = strdata(lj_lib_checkstr(L, i));
  463. if (!jitopt_level(J, str) &&
  464. !jitopt_flag(J, str) &&
  465. !jitopt_param(J, str))
  466. lj_err_callerv(L, LJ_ERR_JITOPT, str);
  467. }
  468. }
  469. return 0;
  470. }
  471. #include "lj_libdef.h"
  472. #endif
  473. /* -- jit.profile module -------------------------------------------------- */
  474. #if LJ_HASPROFILE
  475. #define LJLIB_MODULE_jit_profile
  476. /* Not loaded by default, use: local profile = require("jit.profile") */
  477. #define KEY_PROFILE_THREAD (U64x(80000000,00000000)|'t')
  478. #define KEY_PROFILE_FUNC (U64x(80000000,00000000)|'f')
  479. static void jit_profile_callback(lua_State *L2, lua_State *L, int samples,
  480. int vmstate)
  481. {
  482. TValue key;
  483. cTValue *tv;
  484. key.u64 = KEY_PROFILE_FUNC;
  485. tv = lj_tab_get(L, tabV(registry(L)), &key);
  486. if (tvisfunc(tv)) {
  487. char vmst = (char)vmstate;
  488. int status;
  489. setfuncV(L2, L2->top++, funcV(tv));
  490. setthreadV(L2, L2->top++, L);
  491. setintV(L2->top++, samples);
  492. setstrV(L2, L2->top++, lj_str_new(L2, &vmst, 1));
  493. status = lua_pcall(L2, 3, 0, 0); /* callback(thread, samples, vmstate) */
  494. if (status) {
  495. if (G(L2)->panic) G(L2)->panic(L2);
  496. exit(EXIT_FAILURE);
  497. }
  498. lj_trace_abort(G(L2));
  499. }
  500. }
  501. /* profile.start(mode, cb) */
  502. LJLIB_CF(jit_profile_start)
  503. {
  504. GCtab *registry = tabV(registry(L));
  505. GCstr *mode = lj_lib_optstr(L, 1);
  506. GCfunc *func = lj_lib_checkfunc(L, 2);
  507. lua_State *L2 = lua_newthread(L); /* Thread that runs profiler callback. */
  508. TValue key;
  509. /* Anchor thread and function in registry. */
  510. key.u64 = KEY_PROFILE_THREAD;
  511. setthreadV(L, lj_tab_set(L, registry, &key), L2);
  512. key.u64 = KEY_PROFILE_FUNC;
  513. setfuncV(L, lj_tab_set(L, registry, &key), func);
  514. lj_gc_anybarriert(L, registry);
  515. luaJIT_profile_start(L, mode ? strdata(mode) : "",
  516. (luaJIT_profile_callback)jit_profile_callback, L2);
  517. return 0;
  518. }
  519. /* profile.stop() */
  520. LJLIB_CF(jit_profile_stop)
  521. {
  522. GCtab *registry;
  523. TValue key;
  524. luaJIT_profile_stop(L);
  525. registry = tabV(registry(L));
  526. key.u64 = KEY_PROFILE_THREAD;
  527. setnilV(lj_tab_set(L, registry, &key));
  528. key.u64 = KEY_PROFILE_FUNC;
  529. setnilV(lj_tab_set(L, registry, &key));
  530. lj_gc_anybarriert(L, registry);
  531. return 0;
  532. }
  533. /* dump = profile.dumpstack([thread,] fmt, depth) */
  534. LJLIB_CF(jit_profile_dumpstack)
  535. {
  536. lua_State *L2 = L;
  537. int arg = 0;
  538. size_t len;
  539. int depth;
  540. GCstr *fmt;
  541. const char *p;
  542. if (L->top > L->base && tvisthread(L->base)) {
  543. L2 = threadV(L->base);
  544. arg = 1;
  545. }
  546. fmt = lj_lib_checkstr(L, arg+1);
  547. depth = lj_lib_checkint(L, arg+2);
  548. p = luaJIT_profile_dumpstack(L2, strdata(fmt), depth, &len);
  549. lua_pushlstring(L, p, len);
  550. return 1;
  551. }
  552. #include "lj_libdef.h"
  553. static int luaopen_jit_profile(lua_State *L)
  554. {
  555. LJ_LIB_REG(L, NULL, jit_profile);
  556. return 1;
  557. }
  558. #endif
  559. /* -- JIT compiler initialization ----------------------------------------- */
  560. #if LJ_HASJIT
  561. /* Default values for JIT parameters. */
  562. static const int32_t jit_param_default[JIT_P__MAX+1] = {
  563. #define JIT_PARAMINIT(len, name, value) (value),
  564. JIT_PARAMDEF(JIT_PARAMINIT)
  565. #undef JIT_PARAMINIT
  566. 0
  567. };
  568. #if LJ_TARGET_ARM && LJ_TARGET_LINUX
  569. #include <sys/utsname.h>
  570. #endif
  571. /* Arch-dependent CPU feature detection. */
  572. static uint32_t jit_cpudetect(void)
  573. {
  574. uint32_t flags = 0;
  575. #if LJ_TARGET_X86ORX64
  576. uint32_t vendor[4];
  577. uint32_t features[4];
  578. if (lj_vm_cpuid(0, vendor) && lj_vm_cpuid(1, features)) {
  579. flags |= ((features[2] >> 0)&1) * JIT_F_SSE3;
  580. flags |= ((features[2] >> 19)&1) * JIT_F_SSE4_1;
  581. if (vendor[0] >= 7) {
  582. uint32_t xfeatures[4];
  583. lj_vm_cpuid(7, xfeatures);
  584. flags |= ((xfeatures[1] >> 8)&1) * JIT_F_BMI2;
  585. }
  586. }
  587. /* Don't bother checking for SSE2 -- the VM will crash before getting here. */
  588. #elif LJ_TARGET_ARM
  589. int ver = LJ_ARCH_VERSION; /* Compile-time ARM CPU detection. */
  590. #if LJ_TARGET_LINUX
  591. if (ver < 70) { /* Runtime ARM CPU detection. */
  592. struct utsname ut;
  593. uname(&ut);
  594. if (strncmp(ut.machine, "armv", 4) == 0) {
  595. if (ut.machine[4] >= '8') ver = 80;
  596. else if (ut.machine[4] == '7') ver = 70;
  597. else if (ut.machine[4] == '6') ver = 60;
  598. }
  599. }
  600. #endif
  601. flags |= ver >= 70 ? JIT_F_ARMV7 :
  602. ver >= 61 ? JIT_F_ARMV6T2_ :
  603. ver >= 60 ? JIT_F_ARMV6_ : 0;
  604. flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2;
  605. #elif LJ_TARGET_ARM64
  606. /* No optional CPU features to detect (for now). */
  607. #elif LJ_TARGET_PPC
  608. #if LJ_ARCH_SQRT
  609. flags |= JIT_F_SQRT;
  610. #endif
  611. #if LJ_ARCH_ROUND
  612. flags |= JIT_F_ROUND;
  613. #endif
  614. #elif LJ_TARGET_MIPS
  615. /* Compile-time MIPS CPU detection. */
  616. #if LJ_ARCH_VERSION >= 20
  617. flags |= JIT_F_MIPSXXR2;
  618. #endif
  619. /* Runtime MIPS CPU detection. */
  620. #if defined(__GNUC__)
  621. if (!(flags & JIT_F_MIPSXXR2)) {
  622. int x;
  623. #ifdef __mips16
  624. x = 0; /* Runtime detection is difficult. Ensure optimal -march flags. */
  625. #else
  626. /* On MIPS32R1 rotr is treated as srl. rotr r2,r2,1 -> srl r2,r2,1. */
  627. __asm__("li $2, 1\n\t.long 0x00221042\n\tmove %0, $2" : "=r"(x) : : "$2");
  628. #endif
  629. if (x) flags |= JIT_F_MIPSXXR2; /* Either 0x80000000 (R2) or 0 (R1). */
  630. }
  631. #endif
  632. #else
  633. #error "Missing CPU detection for this architecture"
  634. #endif
  635. return flags;
  636. }
  637. /* Initialize JIT compiler. */
  638. static void jit_init(lua_State *L)
  639. {
  640. jit_State *J = L2J(L);
  641. J->flags = jit_cpudetect() | JIT_F_ON | JIT_F_OPT_DEFAULT;
  642. memcpy(J->param, jit_param_default, sizeof(J->param));
  643. lj_dispatch_update(G(L));
  644. }
  645. #endif
  646. LUALIB_API int luaopen_jit(lua_State *L)
  647. {
  648. #if LJ_HASJIT
  649. jit_init(L);
  650. #endif
  651. lua_pushliteral(L, LJ_OS_NAME);
  652. lua_pushliteral(L, LJ_ARCH_NAME);
  653. lua_pushinteger(L, LUAJIT_VERSION_NUM); /* Deprecated. */
  654. lua_pushliteral(L, LUAJIT_VERSION);
  655. LJ_LIB_REG(L, LUA_JITLIBNAME, jit);
  656. #if LJ_HASPROFILE
  657. lj_lib_prereg(L, LUA_JITLIBNAME ".profile", luaopen_jit_profile,
  658. tabref(L->env));
  659. #endif
  660. #ifndef LUAJIT_DISABLE_JITUTIL
  661. lj_lib_prereg(L, LUA_JITLIBNAME ".util", luaopen_jit_util, tabref(L->env));
  662. #endif
  663. #if LJ_HASJIT
  664. LJ_LIB_REG(L, "jit.opt", jit_opt);
  665. #endif
  666. L->top -= 2;
  667. return 1;
  668. }