You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

872 regels
28 KiB

  1. /*
  2. ** $Id: lvm.c,v 2.152 2012/06/08 15:14:04 roberto Exp $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define lvm_c
  13. #define LUA_CORE
  14. #include "lua.h"
  15. #include "ldebug.h"
  16. #include "ldo.h"
  17. #include "lfunc.h"
  18. #include "lgc.h"
  19. #include "lobject.h"
  20. #include "lopcodes.h"
  21. #include "lstate.h"
  22. #include "lstring.h"
  23. #include "ltable.h"
  24. #include "ltm.h"
  25. #include "lvm.h"
  26. /* limit for table tag-method chains (to avoid loops) */
  27. #define MAXTAGLOOP 100
  28. const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
  29. lua_Number num;
  30. if (ttisnumber(obj)) return obj;
  31. if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
  32. setnvalue(n, num);
  33. return n;
  34. }
  35. else
  36. return NULL;
  37. }
  38. int luaV_tostring (lua_State *L, StkId obj) {
  39. if (!ttisnumber(obj))
  40. return 0;
  41. else {
  42. char s[LUAI_MAXNUMBER2STR];
  43. lua_Number n = nvalue(obj);
  44. int l = lua_number2str(s, n);
  45. setsvalue2s(L, obj, luaS_newlstr(L, s, l));
  46. return 1;
  47. }
  48. }
  49. static void traceexec (lua_State *L) {
  50. CallInfo *ci = L->ci;
  51. lu_byte mask = L->hookmask;
  52. int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
  53. if (counthook)
  54. resethookcount(L); /* reset count */
  55. if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
  56. ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
  57. return; /* do not call hook again (VM yielded, so it did not move) */
  58. }
  59. if (counthook)
  60. luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
  61. if (mask & LUA_MASKLINE) {
  62. Proto *p = ci_func(ci)->p;
  63. int npc = pcRel(ci->u.l.savedpc, p);
  64. int newline = getfuncline(p, npc);
  65. if (npc == 0 || /* call linehook when enter a new function, */
  66. ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
  67. newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
  68. luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
  69. }
  70. L->oldpc = ci->u.l.savedpc;
  71. if (L->status == LUA_YIELD) { /* did hook yield? */
  72. if (counthook)
  73. L->hookcount = 1; /* undo decrement to zero */
  74. ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
  75. ci->callstatus |= CIST_HOOKYIELD; /* mark that it yieled */
  76. ci->func = L->top - 1; /* protect stack below results */
  77. luaD_throw(L, LUA_YIELD);
  78. }
  79. }
  80. static void callTM (lua_State *L, const TValue *f, const TValue *p1,
  81. const TValue *p2, TValue *p3, int hasres) {
  82. ptrdiff_t result = savestack(L, p3);
  83. setobj2s(L, L->top++, f); /* push function */
  84. setobj2s(L, L->top++, p1); /* 1st argument */
  85. setobj2s(L, L->top++, p2); /* 2nd argument */
  86. if (!hasres) /* no result? 'p3' is third argument */
  87. setobj2s(L, L->top++, p3); /* 3rd argument */
  88. luaD_checkstack(L, 0);
  89. /* metamethod may yield only when called from Lua code */
  90. luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
  91. if (hasres) { /* if has result, move it to its place */
  92. p3 = restorestack(L, result);
  93. setobjs2s(L, p3, --L->top);
  94. }
  95. }
  96. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  97. int loop;
  98. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  99. const TValue *tm;
  100. if (ttistable(t)) { /* `t' is a table? */
  101. Table *h = hvalue(t);
  102. const TValue *res = luaH_get(h, key); /* do a primitive get */
  103. if (!ttisnil(res) || /* result is not nil? */
  104. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  105. setobj2s(L, val, res);
  106. return;
  107. }
  108. /* else will try the tag method */
  109. }
  110. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  111. luaG_typeerror(L, t, "index");
  112. if (ttisfunction(tm)) {
  113. callTM(L, tm, t, key, val, 1);
  114. return;
  115. }
  116. t = tm; /* else repeat with 'tm' */
  117. }
  118. luaG_runerror(L, "loop in gettable");
  119. }
  120. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  121. int loop;
  122. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  123. const TValue *tm;
  124. if (ttistable(t)) { /* `t' is a table? */
  125. Table *h = hvalue(t);
  126. TValue *oldval = cast(TValue *, luaH_get(h, key));
  127. /* if previous value is not nil, there must be a previous entry
  128. in the table; moreover, a metamethod has no relevance */
  129. if (!ttisnil(oldval) ||
  130. /* previous value is nil; must check the metamethod */
  131. ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
  132. /* no metamethod; is there a previous entry in the table? */
  133. (oldval != luaO_nilobject ||
  134. /* no previous entry; must create one. (The next test is
  135. always true; we only need the assignment.) */
  136. (oldval = luaH_newkey(L, h, key), 1)))) {
  137. /* no metamethod and (now) there is an entry with given key */
  138. setobj2t(L, oldval, val); /* assign new value to that entry */
  139. invalidateTMcache(h);
  140. luaC_barrierback(L, obj2gco(h), val);
  141. return;
  142. }
  143. /* else will try the metamethod */
  144. }
  145. else /* not a table; check metamethod */
  146. if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  147. luaG_typeerror(L, t, "index");
  148. /* there is a metamethod */
  149. if (ttisfunction(tm)) {
  150. callTM(L, tm, t, key, val, 0);
  151. return;
  152. }
  153. t = tm; /* else repeat with 'tm' */
  154. }
  155. luaG_runerror(L, "loop in settable");
  156. }
  157. static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
  158. StkId res, TMS event) {
  159. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  160. if (ttisnil(tm))
  161. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  162. if (ttisnil(tm)) return 0;
  163. callTM(L, tm, p1, p2, res, 1);
  164. return 1;
  165. }
  166. static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2,
  167. TMS event) {
  168. const TValue *tm1 = fasttm(L, mt1, event);
  169. const TValue *tm2;
  170. if (tm1 == NULL) return NULL; /* no metamethod */
  171. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  172. tm2 = fasttm(L, mt2, event);
  173. if (tm2 == NULL) return NULL; /* no metamethod */
  174. if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
  175. return tm1;
  176. return NULL;
  177. }
  178. static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
  179. TMS event) {
  180. if (!call_binTM(L, p1, p2, L->top, event))
  181. return -1; /* no metamethod */
  182. else
  183. return !l_isfalse(L->top);
  184. }
  185. static int l_strcmp (const TString *ls, const TString *rs) {
  186. const char *l = getstr(ls);
  187. size_t ll = ls->tsv.len;
  188. const char *r = getstr(rs);
  189. size_t lr = rs->tsv.len;
  190. for (;;) {
  191. int temp = strcoll(l, r);
  192. if (temp != 0) return temp;
  193. else { /* strings are equal up to a `\0' */
  194. size_t len = strlen(l); /* index of first `\0' in both strings */
  195. if (len == lr) /* r is finished? */
  196. return (len == ll) ? 0 : 1;
  197. else if (len == ll) /* l is finished? */
  198. return -1; /* l is smaller than r (because r is not finished) */
  199. /* both strings longer than `len'; go on comparing (after the `\0') */
  200. len++;
  201. l += len; ll -= len; r += len; lr -= len;
  202. }
  203. }
  204. }
  205. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  206. int res;
  207. if (ttisnumber(l) && ttisnumber(r))
  208. return luai_numlt(L, nvalue(l), nvalue(r));
  209. else if (ttisstring(l) && ttisstring(r))
  210. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  211. else if ((res = call_orderTM(L, l, r, TM_LT)) < 0)
  212. luaG_ordererror(L, l, r);
  213. return res;
  214. }
  215. int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  216. int res;
  217. if (ttisnumber(l) && ttisnumber(r))
  218. return luai_numle(L, nvalue(l), nvalue(r));
  219. else if (ttisstring(l) && ttisstring(r))
  220. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  221. else if ((res = call_orderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
  222. return res;
  223. else if ((res = call_orderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */
  224. luaG_ordererror(L, l, r);
  225. return !res;
  226. }
  227. /*
  228. ** equality of Lua values. L == NULL means raw equality (no metamethods)
  229. */
  230. int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) {
  231. const TValue *tm;
  232. lua_assert(ttisequal(t1, t2));
  233. switch (ttype(t1)) {
  234. case LUA_TNIL: return 1;
  235. case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
  236. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  237. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  238. case LUA_TLCF: return fvalue(t1) == fvalue(t2);
  239. case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2));
  240. case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2));
  241. case LUA_TUSERDATA: {
  242. if (uvalue(t1) == uvalue(t2)) return 1;
  243. else if (L == NULL) return 0;
  244. tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
  245. break; /* will try TM */
  246. }
  247. case LUA_TTABLE: {
  248. if (hvalue(t1) == hvalue(t2)) return 1;
  249. else if (L == NULL) return 0;
  250. tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
  251. break; /* will try TM */
  252. }
  253. default:
  254. lua_assert(iscollectable(t1));
  255. return gcvalue(t1) == gcvalue(t2);
  256. }
  257. if (tm == NULL) return 0; /* no TM? */
  258. callTM(L, tm, t1, t2, L->top, 1); /* call TM */
  259. return !l_isfalse(L->top);
  260. }
  261. void luaV_concat (lua_State *L, int total) {
  262. lua_assert(total >= 2);
  263. do {
  264. StkId top = L->top;
  265. int n = 2; /* number of elements handled in this pass (at least 2) */
  266. if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
  267. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  268. luaG_concaterror(L, top-2, top-1);
  269. }
  270. else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
  271. (void)tostring(L, top - 2); /* result is first operand */
  272. else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
  273. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  274. }
  275. else {
  276. /* at least two non-empty string values; get as many as possible */
  277. size_t tl = tsvalue(top-1)->len;
  278. char *buffer;
  279. int i;
  280. /* collect total length */
  281. for (i = 1; i < total && tostring(L, top-i-1); i++) {
  282. size_t l = tsvalue(top-i-1)->len;
  283. if (l >= (MAX_SIZET/sizeof(char)) - tl)
  284. luaG_runerror(L, "string length overflow");
  285. tl += l;
  286. }
  287. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  288. tl = 0;
  289. n = i;
  290. do { /* concat all strings */
  291. size_t l = tsvalue(top-i)->len;
  292. memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
  293. tl += l;
  294. } while (--i > 0);
  295. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  296. }
  297. total -= n-1; /* got 'n' strings to create 1 new */
  298. L->top -= n-1; /* popped 'n' strings and pushed one */
  299. } while (total > 1); /* repeat until only 1 result left */
  300. }
  301. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  302. const TValue *tm;
  303. switch (ttypenv(rb)) {
  304. case LUA_TTABLE: {
  305. Table *h = hvalue(rb);
  306. tm = fasttm(L, h->metatable, TM_LEN);
  307. if (tm) break; /* metamethod? break switch to call it */
  308. setnvalue(ra, cast_num(luaH_getn(h))); /* else primitive len */
  309. return;
  310. }
  311. case LUA_TSTRING: {
  312. setnvalue(ra, cast_num(tsvalue(rb)->len));
  313. return;
  314. }
  315. default: { /* try metamethod */
  316. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  317. if (ttisnil(tm)) /* no metamethod? */
  318. luaG_typeerror(L, rb, "get length of");
  319. break;
  320. }
  321. }
  322. callTM(L, tm, rb, rb, ra, 1);
  323. }
  324. void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
  325. const TValue *rc, TMS op) {
  326. TValue tempb, tempc;
  327. const TValue *b, *c;
  328. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  329. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  330. lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
  331. setnvalue(ra, res);
  332. }
  333. else if (!call_binTM(L, rb, rc, ra, op))
  334. luaG_aritherror(L, rb, rc);
  335. }
  336. /*
  337. ** check whether cached closure in prototype 'p' may be reused, that is,
  338. ** whether there is a cached closure with the same upvalues needed by
  339. ** new closure to be created.
  340. */
  341. static Closure *getcached (Proto *p, UpVal **encup, StkId base) {
  342. Closure *c = p->cache;
  343. if (c != NULL) { /* is there a cached closure? */
  344. int nup = p->sizeupvalues;
  345. Upvaldesc *uv = p->upvalues;
  346. int i;
  347. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  348. TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
  349. if (c->l.upvals[i]->v != v)
  350. return NULL; /* wrong upvalue; cannot reuse closure */
  351. }
  352. }
  353. return c; /* return cached closure (or NULL if no cached closure) */
  354. }
  355. /*
  356. ** create a new Lua closure, push it in the stack, and initialize
  357. ** its upvalues. Note that the call to 'luaC_barrierproto' must come
  358. ** before the assignment to 'p->cache', as the function needs the
  359. ** original value of that field.
  360. */
  361. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  362. StkId ra) {
  363. int nup = p->sizeupvalues;
  364. Upvaldesc *uv = p->upvalues;
  365. int i;
  366. Closure *ncl = luaF_newLclosure(L, nup);
  367. ncl->l.p = p;
  368. setclLvalue(L, ra, ncl); /* anchor new closure in stack */
  369. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  370. if (uv[i].instack) /* upvalue refers to local variable? */
  371. ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
  372. else /* get upvalue from enclosing function */
  373. ncl->l.upvals[i] = encup[uv[i].idx];
  374. }
  375. luaC_barrierproto(L, p, ncl);
  376. p->cache = ncl; /* save it on cache for reuse */
  377. }
  378. /*
  379. ** finish execution of an opcode interrupted by an yield
  380. */
  381. void luaV_finishOp (lua_State *L) {
  382. CallInfo *ci = L->ci;
  383. StkId base = ci->u.l.base;
  384. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  385. OpCode op = GET_OPCODE(inst);
  386. switch (op) { /* finish its execution */
  387. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
  388. case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
  389. case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
  390. setobjs2s(L, base + GETARG_A(inst), --L->top);
  391. break;
  392. }
  393. case OP_LE: case OP_LT: case OP_EQ: {
  394. int res = !l_isfalse(L->top - 1);
  395. L->top--;
  396. /* metamethod should not be called when operand is K */
  397. lua_assert(!ISK(GETARG_B(inst)));
  398. if (op == OP_LE && /* "<=" using "<" instead? */
  399. ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
  400. res = !res; /* invert result */
  401. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  402. if (res != GETARG_A(inst)) /* condition failed? */
  403. ci->u.l.savedpc++; /* skip jump instruction */
  404. break;
  405. }
  406. case OP_CONCAT: {
  407. StkId top = L->top - 1; /* top when 'call_binTM' was called */
  408. int b = GETARG_B(inst); /* first element to concatenate */
  409. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  410. setobj2s(L, top - 2, top); /* put TM result in proper position */
  411. if (total > 1) { /* are there elements to concat? */
  412. L->top = top - 1; /* top is one after last element (at top-2) */
  413. luaV_concat(L, total); /* concat them (may yield again) */
  414. }
  415. /* move final result to final position */
  416. setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
  417. L->top = ci->top; /* restore top */
  418. break;
  419. }
  420. case OP_TFORCALL: {
  421. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  422. L->top = ci->top; /* correct top */
  423. break;
  424. }
  425. case OP_CALL: {
  426. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  427. L->top = ci->top; /* adjust results */
  428. break;
  429. }
  430. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  431. break;
  432. default: lua_assert(0);
  433. }
  434. }
  435. /*
  436. ** some macros for common tasks in `luaV_execute'
  437. */
  438. #if !defined luai_runtimecheck
  439. #define luai_runtimecheck(L, c) /* void */
  440. #endif
  441. #define RA(i) (base+GETARG_A(i))
  442. /* to be used after possible stack reallocation */
  443. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  444. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  445. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  446. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  447. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  448. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  449. #define KBx(i) \
  450. (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
  451. /* execute a jump instruction */
  452. #define dojump(ci,i,e) \
  453. { int a = GETARG_A(i); \
  454. if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
  455. ci->u.l.savedpc += GETARG_sBx(i) + e; }
  456. /* for test instructions, execute the jump instruction that follows it */
  457. #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
  458. #define Protect(x) { {x;}; base = ci->u.l.base; }
  459. #define checkGC(L,c) \
  460. Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
  461. luaC_step(L); \
  462. L->top = ci->top;}) /* restore top */ \
  463. luai_threadyield(L); )
  464. #define arith_op(op,tm) { \
  465. TValue *rb = RKB(i); \
  466. TValue *rc = RKC(i); \
  467. if (ttisnumber(rb) && ttisnumber(rc)) { \
  468. lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  469. setnvalue(ra, op(L, nb, nc)); \
  470. } \
  471. else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
  472. #define vmdispatch(o) switch(o)
  473. #define vmcase(l,b) case l: {b} break;
  474. #define vmcasenb(l,b) case l: {b} /* nb = no break */
  475. void luaV_execute (lua_State *L) {
  476. CallInfo *ci = L->ci;
  477. LClosure *cl;
  478. TValue *k;
  479. StkId base;
  480. newframe: /* reentry point when frame changes (call/return) */
  481. lua_assert(ci == L->ci);
  482. cl = clLvalue(ci->func);
  483. k = cl->p->k;
  484. base = ci->u.l.base;
  485. /* main loop of interpreter */
  486. for (;;) {
  487. Instruction i = *(ci->u.l.savedpc++);
  488. StkId ra;
  489. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  490. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  491. Protect(traceexec(L));
  492. }
  493. /* WARNING: several calls may realloc the stack and invalidate `ra' */
  494. ra = RA(i);
  495. lua_assert(base == ci->u.l.base);
  496. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  497. vmdispatch (GET_OPCODE(i)) {
  498. vmcase(OP_MOVE,
  499. setobjs2s(L, ra, RB(i));
  500. )
  501. vmcase(OP_LOADK,
  502. TValue *rb = k + GETARG_Bx(i);
  503. setobj2s(L, ra, rb);
  504. )
  505. vmcase(OP_LOADKX,
  506. TValue *rb;
  507. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  508. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  509. setobj2s(L, ra, rb);
  510. )
  511. vmcase(OP_LOADBOOL,
  512. setbvalue(ra, GETARG_B(i));
  513. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  514. )
  515. vmcase(OP_LOADNIL,
  516. int b = GETARG_B(i);
  517. do {
  518. setnilvalue(ra++);
  519. } while (b--);
  520. )
  521. vmcase(OP_GETUPVAL,
  522. int b = GETARG_B(i);
  523. setobj2s(L, ra, cl->upvals[b]->v);
  524. )
  525. vmcase(OP_GETTABUP,
  526. int b = GETARG_B(i);
  527. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  528. )
  529. vmcase(OP_GETTABLE,
  530. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  531. )
  532. vmcase(OP_SETTABUP,
  533. int a = GETARG_A(i);
  534. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  535. )
  536. vmcase(OP_SETUPVAL,
  537. UpVal *uv = cl->upvals[GETARG_B(i)];
  538. setobj(L, uv->v, ra);
  539. luaC_barrier(L, uv, ra);
  540. )
  541. vmcase(OP_SETTABLE,
  542. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  543. )
  544. vmcase(OP_NEWTABLE,
  545. int b = GETARG_B(i);
  546. int c = GETARG_C(i);
  547. Table *t = luaH_new(L);
  548. sethvalue(L, ra, t);
  549. if (b != 0 || c != 0)
  550. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  551. checkGC(L, ra + 1);
  552. )
  553. vmcase(OP_SELF,
  554. StkId rb = RB(i);
  555. setobjs2s(L, ra+1, rb);
  556. Protect(luaV_gettable(L, rb, RKC(i), ra));
  557. )
  558. vmcase(OP_ADD,
  559. arith_op(luai_numadd, TM_ADD);
  560. )
  561. vmcase(OP_SUB,
  562. arith_op(luai_numsub, TM_SUB);
  563. )
  564. vmcase(OP_MUL,
  565. arith_op(luai_nummul, TM_MUL);
  566. )
  567. vmcase(OP_DIV,
  568. arith_op(luai_numdiv, TM_DIV);
  569. )
  570. vmcase(OP_MOD,
  571. arith_op(luai_nummod, TM_MOD);
  572. )
  573. vmcase(OP_POW,
  574. arith_op(luai_numpow, TM_POW);
  575. )
  576. vmcase(OP_UNM,
  577. TValue *rb = RB(i);
  578. if (ttisnumber(rb)) {
  579. lua_Number nb = nvalue(rb);
  580. setnvalue(ra, luai_numunm(L, nb));
  581. }
  582. else {
  583. Protect(luaV_arith(L, ra, rb, rb, TM_UNM));
  584. }
  585. )
  586. vmcase(OP_NOT,
  587. TValue *rb = RB(i);
  588. int res = l_isfalse(rb); /* next assignment may change this value */
  589. setbvalue(ra, res);
  590. )
  591. vmcase(OP_LEN,
  592. Protect(luaV_objlen(L, ra, RB(i)));
  593. )
  594. vmcase(OP_CONCAT,
  595. int b = GETARG_B(i);
  596. int c = GETARG_C(i);
  597. StkId rb;
  598. L->top = base + c + 1; /* mark the end of concat operands */
  599. Protect(luaV_concat(L, c - b + 1));
  600. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  601. rb = b + base;
  602. setobjs2s(L, ra, rb);
  603. checkGC(L, (ra >= rb ? ra + 1 : rb));
  604. L->top = ci->top; /* restore top */
  605. )
  606. vmcase(OP_JMP,
  607. dojump(ci, i, 0);
  608. )
  609. vmcase(OP_EQ,
  610. TValue *rb = RKB(i);
  611. TValue *rc = RKC(i);
  612. Protect(
  613. if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i))
  614. ci->u.l.savedpc++;
  615. else
  616. donextjump(ci);
  617. )
  618. )
  619. vmcase(OP_LT,
  620. Protect(
  621. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  622. ci->u.l.savedpc++;
  623. else
  624. donextjump(ci);
  625. )
  626. )
  627. vmcase(OP_LE,
  628. Protect(
  629. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  630. ci->u.l.savedpc++;
  631. else
  632. donextjump(ci);
  633. )
  634. )
  635. vmcase(OP_TEST,
  636. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  637. ci->u.l.savedpc++;
  638. else
  639. donextjump(ci);
  640. )
  641. vmcase(OP_TESTSET,
  642. TValue *rb = RB(i);
  643. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  644. ci->u.l.savedpc++;
  645. else {
  646. setobjs2s(L, ra, rb);
  647. donextjump(ci);
  648. }
  649. )
  650. vmcase(OP_CALL,
  651. int b = GETARG_B(i);
  652. int nresults = GETARG_C(i) - 1;
  653. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  654. if (luaD_precall(L, ra, nresults)) { /* C function? */
  655. if (nresults >= 0) L->top = ci->top; /* adjust results */
  656. base = ci->u.l.base;
  657. }
  658. else { /* Lua function */
  659. ci = L->ci;
  660. ci->callstatus |= CIST_REENTRY;
  661. goto newframe; /* restart luaV_execute over new Lua function */
  662. }
  663. )
  664. vmcase(OP_TAILCALL,
  665. int b = GETARG_B(i);
  666. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  667. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  668. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  669. base = ci->u.l.base;
  670. else {
  671. /* tail call: put called frame (n) in place of caller one (o) */
  672. CallInfo *nci = L->ci; /* called frame */
  673. CallInfo *oci = nci->previous; /* caller frame */
  674. StkId nfunc = nci->func; /* called function */
  675. StkId ofunc = oci->func; /* caller function */
  676. /* last stack slot filled by 'precall' */
  677. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  678. int aux;
  679. /* close all upvalues from previous call */
  680. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  681. /* move new frame into old one */
  682. for (aux = 0; nfunc + aux < lim; aux++)
  683. setobjs2s(L, ofunc + aux, nfunc + aux);
  684. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  685. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  686. oci->u.l.savedpc = nci->u.l.savedpc;
  687. oci->callstatus |= CIST_TAIL; /* function was tail called */
  688. ci = L->ci = oci; /* remove new frame */
  689. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  690. goto newframe; /* restart luaV_execute over new Lua function */
  691. }
  692. )
  693. vmcasenb(OP_RETURN,
  694. int b = GETARG_B(i);
  695. if (b != 0) L->top = ra+b-1;
  696. if (cl->p->sizep > 0) luaF_close(L, base);
  697. b = luaD_poscall(L, ra);
  698. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  699. return; /* external invocation: return */
  700. else { /* invocation via reentry: continue execution */
  701. ci = L->ci;
  702. if (b) L->top = ci->top;
  703. lua_assert(isLua(ci));
  704. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  705. goto newframe; /* restart luaV_execute over new Lua function */
  706. }
  707. )
  708. vmcase(OP_FORLOOP,
  709. lua_Number step = nvalue(ra+2);
  710. lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
  711. lua_Number limit = nvalue(ra+1);
  712. if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
  713. : luai_numle(L, limit, idx)) {
  714. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  715. setnvalue(ra, idx); /* update internal index... */
  716. setnvalue(ra+3, idx); /* ...and external index */
  717. }
  718. )
  719. vmcase(OP_FORPREP,
  720. const TValue *init = ra;
  721. const TValue *plimit = ra+1;
  722. const TValue *pstep = ra+2;
  723. if (!tonumber(init, ra))
  724. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  725. else if (!tonumber(plimit, ra+1))
  726. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  727. else if (!tonumber(pstep, ra+2))
  728. luaG_runerror(L, LUA_QL("for") " step must be a number");
  729. setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
  730. ci->u.l.savedpc += GETARG_sBx(i);
  731. )
  732. vmcasenb(OP_TFORCALL,
  733. StkId cb = ra + 3; /* call base */
  734. setobjs2s(L, cb+2, ra+2);
  735. setobjs2s(L, cb+1, ra+1);
  736. setobjs2s(L, cb, ra);
  737. L->top = cb + 3; /* func. + 2 args (state and index) */
  738. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  739. L->top = ci->top;
  740. i = *(ci->u.l.savedpc++); /* go to next instruction */
  741. ra = RA(i);
  742. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  743. goto l_tforloop;
  744. )
  745. vmcase(OP_TFORLOOP,
  746. l_tforloop:
  747. if (!ttisnil(ra + 1)) { /* continue loop? */
  748. setobjs2s(L, ra, ra + 1); /* save control variable */
  749. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  750. }
  751. )
  752. vmcase(OP_SETLIST,
  753. int n = GETARG_B(i);
  754. int c = GETARG_C(i);
  755. int last;
  756. Table *h;
  757. if (n == 0) n = cast_int(L->top - ra) - 1;
  758. if (c == 0) {
  759. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  760. c = GETARG_Ax(*ci->u.l.savedpc++);
  761. }
  762. luai_runtimecheck(L, ttistable(ra));
  763. h = hvalue(ra);
  764. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  765. if (last > h->sizearray) /* needs more space? */
  766. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  767. for (; n > 0; n--) {
  768. TValue *val = ra+n;
  769. luaH_setint(L, h, last--, val);
  770. luaC_barrierback(L, obj2gco(h), val);
  771. }
  772. L->top = ci->top; /* correct top (in case of previous open call) */
  773. )
  774. vmcase(OP_CLOSURE,
  775. Proto *p = cl->p->p[GETARG_Bx(i)];
  776. Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  777. if (ncl == NULL) /* no match? */
  778. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  779. else
  780. setclLvalue(L, ra, ncl); /* push cashed closure */
  781. checkGC(L, ra + 1);
  782. )
  783. vmcase(OP_VARARG,
  784. int b = GETARG_B(i) - 1;
  785. int j;
  786. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  787. if (b < 0) { /* B == 0? */
  788. b = n; /* get all var. arguments */
  789. Protect(luaD_checkstack(L, n));
  790. ra = RA(i); /* previous call may change the stack */
  791. L->top = ra + n;
  792. }
  793. for (j = 0; j < b; j++) {
  794. if (j < n) {
  795. setobjs2s(L, ra + j, base - n + j);
  796. }
  797. else {
  798. setnilvalue(ra + j);
  799. }
  800. }
  801. )
  802. vmcase(OP_EXTRAARG,
  803. lua_assert(0);
  804. )
  805. }
  806. }
  807. }