⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ljitlib.c

📁 lua的即时编译器。支持lua 5.1.2版本
💻 C
📖 第 1 页 / 共 2 页
字号:
  lua_pushvalue(L, -2);  /* Pipeline table as upvalue. */  lua_pushcclosure(L, j_frontend, 1);  lua_rawset(L, LUA_REGISTRYINDEX);  /* Register the frontend wrapper. */  G(L)->jit_state->frontwrap = frontwrap;  /* Add jit.attach with the pipeline table as upvalue. */  lua_pushcclosure(L, j_attach, 1);  lua_setfield(L, -2, "attach");  /* "jit" table must be below. */}/* ------------------------------------------------------------------------ *//* Calculate total mcode size without mfm and only for active mcode blocks. */static size_t mcodesize(Proto *pt){  jit_MCTrailer tr;  size_t sz = 0;  tr.mcode = (char *)pt->jit_mcode;  tr.sz = pt->jit_szmcode;  do {    jit_Mfm *mfm = JIT_MCMFM(tr.mcode, tr.sz);    if (sz != 0 && jit_mfm_ismain(mfm)) break;  /* Stop at old main mfm. */    while (*mfm != JIT_MFM_STOP) mfm--;  /* Search for end of mcode. */    sz += (char *)mfm-(char *)tr.mcode;  /* Add size of mcode without mfm. */    memcpy((void *)&tr, JIT_MCTRAILER(tr.mcode, tr.sz), sizeof(jit_MCTrailer));  } while (tr.mcode != NULL);  return sz;}#define setintfield(name, i) \  do { lua_pushinteger(L, i); lua_setfield(L, -2, name); } while (0)/* local stats = jit.util.stats(func) */static int ju_stats(lua_State *L){  if (!(L->top > L->base))    luaL_argerror(L, 1, "Lua function expected");  if (isLfunction(L->base)) {    Proto *pt = clvalue(L->base)->l.p;    lua_createtable(L, 0, 11);    setintfield("status", pt->jit_status);    setintfield("stackslots", pt->maxstacksize);    setintfield("params", pt->numparams);    setintfield("bytecodes", pt->sizecode);    setintfield("consts", pt->sizek);    setintfield("upvalues", pt->nups);    setintfield("subs", pt->sizep);    lua_pushboolean(L, pt->is_vararg);    lua_setfield(L, -2, "isvararg");    lua_getfenv(L, 1);    lua_setfield(L, -2, "env");    if (pt->jit_szmcode != 0) {      setintfield("mcodesize", (int)mcodesize(pt));      lua_pushnumber(L, (lua_Number)(size_t)pt->jit_mcode);      lua_setfield(L, -2, "mcodeaddr");    }    return 1;  } else {    return 0;  /* Don't throw an error like the other util functions. */  }}/* local op, a, b, c, test = jit.util.bytecode(func, pc) */static int ju_bytecode(lua_State *L){  Proto *pt = check_LCL(L)->l.p;  int pc = luaL_checkint(L, 2);  if (pc >= 1 && pc <= pt->sizecode) {    Instruction ins = pt->code[pc-1];    OpCode op = GET_OPCODE(ins);    if (pc > 1 && (((int)OP_SETLIST) << POS_OP) ==	(pt->code[pc-2] & (MASK1(SIZE_OP,POS_OP) | MASK1(SIZE_C,POS_C)))) {      lua_pushstring(L, luaP_opnames[OP_SETLIST]);      lua_pushnumber(L, (lua_Number)ins);  /* Fake extended op. */      return 1;    }    if (op >= NUM_OPCODES) return 0;  /* Just in case. */    lua_pushstring(L, luaP_opnames[op]);    lua_pushinteger(L, GETARG_A(ins));    switch (getOpMode(op)) {    case iABC: {      int b = GETARG_B(ins), c = GETARG_C(ins);      switch (getBMode(op)) {      case OpArgN: lua_pushnil(L); break;      case OpArgK: if (ISK(b)) b = -1-INDEXK(b);      case OpArgR: case OpArgU: lua_pushinteger(L, b); break;      }      switch (getCMode(op)) {      case OpArgN: lua_pushnil(L); break;      case OpArgK: if (ISK(c)) c = -1-INDEXK(c);      case OpArgR: case OpArgU: lua_pushinteger(L, c); break;      }      lua_pushboolean(L, testTMode(op));      return 5;    }    case iABx: {      int bx = GETARG_Bx(ins);      lua_pushinteger(L, getBMode(op) == OpArgK ? -1-bx : bx);      return 3;    }    case iAsBx:      lua_pushinteger(L, GETARG_sBx(ins));      return 3;    }  }  return 0;}/* local const, ok = jit.util.const(func, idx) */static int ju_const(lua_State *L){  Proto *pt = check_LCL(L)->l.p;  int idx = luaL_checkint(L, 2);  if (idx < 0) idx = -idx;  /* Handle both positive and negative indices. */  if (idx >= 1 && idx <= pt->sizek) {    setobj2s(L, L->top-1, &pt->k[idx-1]);    lua_pushboolean(L, 1);    return 2;  }  lua_pushnil(L);  lua_pushboolean(L, 0);  return 2;}/* local upvalue, ok = jit.util.upvalue(func, idx) */static int ju_upvalue(lua_State *L){  Closure *cl = check_LCL(L);  Proto *pt = cl->l.p;  int idx = luaL_checkint(L, 2);  if (idx >= 0 && idx < pt->nups) {    setobj2s(L, L->top-1, cl->l.upvals[idx]->v);    lua_pushboolean(L, 1);    return 2;  }  lua_pushnil(L);  lua_pushboolean(L, 0);  return 2;}/* local nup = jit.util.closurenup(func, idx) */static int ju_closurenup(lua_State *L){  Closure *cl = check_LCL(L);  Proto *pt = cl->l.p;  int idx = luaL_checkint(L, 2);  if (idx >= 0 && idx < pt->sizep) {    lua_pushinteger(L, pt->p[idx]->nups);    return 1;  }  return 0;}/* for tag, mark in mfmiter do ... end. */static int ju_mfmiter(lua_State *L){  jit_Mfm *mfm = (jit_Mfm *)lua_touserdata(L, lua_upvalueindex(1));  int m = *mfm--;  switch (m) {  case JIT_MFM_STOP: return 0;  case JIT_MFM_COMBINE: lua_pushliteral(L, "COMBINE"); lua_pushnil(L); break;  case JIT_MFM_DEAD: lua_pushliteral(L, "DEAD"); lua_pushnil(L); break;  default:    lua_pushinteger(L, m & JIT_MFM_MASK);    lua_pushboolean(L, m & JIT_MFM_MARK);    break;  }  lua_pushlightuserdata(L, (void *)mfm);  lua_replace(L, lua_upvalueindex(1));  return 2;}/* local addr, mcode, mfmiter = jit.util.mcode(func, block) */static int ju_mcode(lua_State *L){  Proto *pt = check_LCL(L)->l.p;  if (pt->jit_szmcode == 0) {  /* Not compiled (yet): return nil, status. */    lua_pushnil(L);    lua_pushinteger(L, pt->jit_status);    return 2;  } else {    jit_Mfm *mfm;    jit_MCTrailer tr;    int block = luaL_checkint(L, 2);    tr.mcode = (char *)pt->jit_mcode;    tr.sz = pt->jit_szmcode;    while (--block > 0) {      memcpy((void *)&tr, JIT_MCTRAILER(tr.mcode, tr.sz),	     sizeof(jit_MCTrailer));      if (tr.sz == 0) return 0;    }    mfm = JIT_MCMFM(tr.mcode, tr.sz);    while (*mfm != JIT_MFM_STOP) mfm--;  /* Search for end of mcode. */    lua_pushnumber(L, (lua_Number)(size_t)tr.mcode);    lua_pushlstring(L, (const char *)tr.mcode, (char *)mfm-(char *)tr.mcode);    lua_pushlightuserdata(L, (void *)JIT_MCMFM(tr.mcode, tr.sz));    lua_pushvalue(L, 1);  /* Must hold onto function to avoid GC. */    lua_pushcclosure(L, ju_mfmiter, 2);    return 3;  }}/* local addr [, mcode] = jit.util.jsubmcode([idx]) */static int ju_jsubmcode(lua_State *L){  jit_State *J = G(L)->jit_state;  if (lua_isnoneornil(L, 1)) {    lua_pushnumber(L, (lua_Number)(size_t)J->jsubmcode);    lua_pushlstring(L, (const char *)J->jsubmcode, J->szjsubmcode);    return 2;  } else {    int idx = luaL_checkint(L, 1);    if (idx >= 0 && idx < J->numjsub) {      lua_pushnumber(L, (lua_Number)(size_t)J->jsub[idx]);      return 1;    }    return 0;  }}/* FOR INTERNAL DEBUGGING USE ONLY: local addr = jit.util.stackptr() */static int ju_stackptr(lua_State *L){  jit_State *J = G(L)->jit_state;  size_t addr = cast(size_t (*)(void), J->jsub[0])();  /* JSUB_STACKPTR == 0! */  lua_pushnumber(L, (lua_Number)addr);  return 1;}/* jit.util.* functions. */static const luaL_Reg jitutillib[] = {  {"stats",		ju_stats },  {"bytecode",		ju_bytecode },  {"const",		ju_const },  {"upvalue",		ju_upvalue },  {"closurenup",	ju_closurenup },  {"mcode",		ju_mcode },  {"jsubmcode",		ju_jsubmcode },  {"stackptr",		ju_stackptr },  { NULL, NULL }};/* Make hint name to hint number map. */static void makehints(lua_State *L, const char *const *t, int tmax,		      const char *name){  int i;  lua_createtable(L, 0, tmax);  for (i = 1; i < tmax; i++) {    lua_pushinteger(L, JIT_H2NUM(i));    lua_setfield(L, -2, t[i-1]);  }  lua_setfield(L, -2, name);}/* CHECK: must match with ljit.h (grep "ORDER JIT_S"). */static const char *const status_list[] = {  "OK",  "NONE",  "OFF",  "ENGINE_OFF",  "DELAYED",  "TOOLARGE",  "COMPILER_ERROR",  "DASM_ERROR"};/* Make bidirectional status name to status number map. */static void makestatus(lua_State *L, const char *name){  int i;  lua_createtable(L, JIT_S_MAX-1, JIT_S_MAX+1);  /* Codes are not 1-based. */  for (i = 0; i < JIT_S_MAX; i++) {    lua_pushstring(L, status_list[i]);    lua_pushinteger(L, i);    lua_pushvalue(L, -2);    lua_rawseti(L, -4, i);    lua_rawset(L, -3);  }  lua_setfield(L, -2, name);}/* ------------------------------------------------------------------------ *//*** Open JIT library*/LUALIB_API int luaopen_jit(lua_State *L){  /* Add the core JIT library. */  luaL_register(L, LUA_JITLIBNAME, jitlib);  lua_pushliteral(L, LUAJIT_VERSION);  lua_setfield(L, -2, "version");  setintfield("version_num", LUAJIT_VERSION_NUM);  lua_pushstring(L, luaJIT_arch);  lua_setfield(L, -2, "arch");  makepipeline(L);  /* Add the utility JIT library. */  luaL_register(L, LUA_JITLIBNAME ".util", jitutillib);  makestatus(L, "status");  makehints(L, hints_H, JIT_H_MAX, "hints");  makehints(L, hints_FH, JIT_FH_MAX, "fhints");  lua_pop(L, 1);  /* Everything ok, so turn the JIT engine on. Vroooom! */  if (luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_ON) <= 0) {    /* Ouch. Someone screwed up DynASM or the JSUBs. Probably me. */    /* But if you get 999999999, look at jit_consistency_check(). */    return luaL_error(L, "JIT engine init failed (%d)",	G(L)->jit_state->dasmstatus);  }  return 1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -