lvm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. ** $Id: lvm.c,v 2.80 2008/11/06 12:43:51 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define lvm_c
  10. #define LUA_CORE
  11. #include "lua.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "lobject.h"
  17. #include "lopcodes.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lvm.h"
  23. /* limit for table tag-method chains (to avoid loops) */
  24. #define MAXTAGLOOP 100
  25. const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
  26. lua_Number num;
  27. if (ttisnumber(obj)) return obj;
  28. if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  29. setnvalue(n, num);
  30. return n;
  31. }
  32. else
  33. return NULL;
  34. }
  35. int luaV_tostring (lua_State *L, StkId obj) {
  36. if (!ttisnumber(obj))
  37. return 0;
  38. else {
  39. char s[LUAI_MAXNUMBER2STR];
  40. lua_Number n = nvalue(obj);
  41. lua_number2str(s, n);
  42. setsvalue2s(L, obj, luaS_new(L, s));
  43. return 1;
  44. }
  45. }
  46. static void traceexec (lua_State *L) {
  47. lu_byte mask = L->hookmask;
  48. if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
  49. resethookcount(L);
  50. luaD_callhook(L, LUA_HOOKCOUNT, -1);
  51. }
  52. if (mask & LUA_MASKLINE) {
  53. Proto *p = ci_func(L->ci)->l.p;
  54. int npc = pcRel(L->savedpc, p);
  55. int newline = getline(p, npc);
  56. if (npc == 0 || /* call linehook when enter a new function, */
  57. L->savedpc <= L->oldpc || /* when jump back (loop), or when */
  58. newline != getline(p, pcRel(L->oldpc, p))) /* enter a new line */
  59. luaD_callhook(L, LUA_HOOKLINE, newline);
  60. }
  61. L->oldpc = L->savedpc;
  62. }
  63. static void callTM (lua_State *L, const TValue *f, const TValue *p1,
  64. const TValue *p2, TValue *p3, int hasres) {
  65. ptrdiff_t result = savestack(L, p3);
  66. int oldbase = L->baseCcalls;
  67. setobj2s(L, L->top++, f); /* push function */
  68. setobj2s(L, L->top++, p1); /* 1st argument */
  69. setobj2s(L, L->top++, p2); /* 2nd argument */
  70. if (!hasres) /* no result? 'p3' is third argument */
  71. setobj2s(L, L->top++, p3); /* 3th argument */
  72. luaD_checkstack(L, 0);
  73. if (isLua(L->ci)) /* metamethod invoked from a Lua function? */
  74. L->baseCcalls++; /* allow it to yield */
  75. luaD_call(L, L->top - (4 - hasres), hasres);
  76. L->baseCcalls = oldbase;
  77. if (hasres) { /* if has result, move it to its place */
  78. p3 = restorestack(L, result);
  79. setobjs2s(L, p3, --L->top);
  80. }
  81. }
  82. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  83. int loop;
  84. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  85. const TValue *tm;
  86. if (ttistable(t)) { /* `t' is a table? */
  87. Table *h = hvalue(t);
  88. const TValue *res = luaH_get(h, key); /* do a primitive get */
  89. if (!ttisnil(res) || /* result is no nil? */
  90. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  91. setobj2s(L, val, res);
  92. return;
  93. }
  94. /* else will try the tag method */
  95. }
  96. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  97. luaG_typeerror(L, t, "index");
  98. if (ttisfunction(tm)) {
  99. callTM(L, tm, t, key, val, 1);
  100. return;
  101. }
  102. t = tm; /* else repeat with `tm' */
  103. }
  104. luaG_runerror(L, "loop in gettable");
  105. }
  106. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  107. int loop;
  108. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  109. const TValue *tm;
  110. if (ttistable(t)) { /* `t' is a table? */
  111. Table *h = hvalue(t);
  112. TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
  113. if (!ttisnil(oldval) || /* result is no nil? */
  114. (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
  115. setobj2t(L, oldval, val);
  116. luaC_barriert(L, h, val);
  117. return;
  118. }
  119. /* else will try the tag method */
  120. }
  121. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  122. luaG_typeerror(L, t, "index");
  123. if (ttisfunction(tm)) {
  124. callTM(L, tm, t, key, val, 0);
  125. return;
  126. }
  127. t = tm; /* else repeat with `tm' */
  128. }
  129. luaG_runerror(L, "loop in settable");
  130. }
  131. static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
  132. StkId res, TMS event) {
  133. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  134. if (ttisnil(tm))
  135. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  136. if (ttisnil(tm)) return 0;
  137. callTM(L, tm, p1, p2, res, 1);
  138. return 1;
  139. }
  140. static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
  141. TMS event) {
  142. const TValue *tm1 = fasttm(L, mt1, event);
  143. const TValue *tm2;
  144. if (tm1 == NULL) return NULL; /* no metamethod */
  145. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  146. tm2 = fasttm(L, mt2, event);
  147. if (tm2 == NULL) return NULL; /* no metamethod */
  148. if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
  149. return tm1;
  150. return NULL;
  151. }
  152. static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
  153. TMS event) {
  154. const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
  155. const TValue *tm2;
  156. if (ttisnil(tm1)) return -1; /* no metamethod? */
  157. tm2 = luaT_gettmbyobj(L, p2, event);
  158. if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
  159. return -1;
  160. callTM(L, tm1, p1, p2, L->top, 1);
  161. return !l_isfalse(L->top);
  162. }
  163. static int l_strcmp (const TString *ls, const TString *rs) {
  164. const char *l = getstr(ls);
  165. size_t ll = ls->tsv.len;
  166. const char *r = getstr(rs);
  167. size_t lr = rs->tsv.len;
  168. for (;;) {
  169. int temp = strcoll(l, r);
  170. if (temp != 0) return temp;
  171. else { /* strings are equal up to a `\0' */
  172. size_t len = strlen(l); /* index of first `\0' in both strings */
  173. if (len == lr) /* r is finished? */
  174. return (len == ll) ? 0 : 1;
  175. else if (len == ll) /* l is finished? */
  176. return -1; /* l is smaller than r (because r is not finished) */
  177. /* both strings longer than `len'; go on comparing (after the `\0') */
  178. len++;
  179. l += len; ll -= len; r += len; lr -= len;
  180. }
  181. }
  182. }
  183. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  184. int res;
  185. if (ttype(l) != ttype(r))
  186. return luaG_ordererror(L, l, r);
  187. else if (ttisnumber(l))
  188. return luai_numlt(L, nvalue(l), nvalue(r));
  189. else if (ttisstring(l))
  190. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  191. else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  192. return res;
  193. return luaG_ordererror(L, l, r);
  194. }
  195. static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
  196. int res;
  197. if (ttype(l) != ttype(r))
  198. return luaG_ordererror(L, l, r);
  199. else if (ttisnumber(l))
  200. return luai_numle(L, nvalue(l), nvalue(r));
  201. else if (ttisstring(l))
  202. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  203. else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
  204. return res;
  205. else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
  206. return !res;
  207. return luaG_ordererror(L, l, r);
  208. }
  209. int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) {
  210. const TValue *tm;
  211. lua_assert(ttype(t1) == ttype(t2));
  212. switch (ttype(t1)) {
  213. case LUA_TNIL: return 1;
  214. case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
  215. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  216. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  217. case LUA_TUSERDATA: {
  218. if (uvalue(t1) == uvalue(t2)) return 1;
  219. tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
  220. break; /* will try TM */
  221. }
  222. case LUA_TTABLE: {
  223. if (hvalue(t1) == hvalue(t2)) return 1;
  224. tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
  225. break; /* will try TM */
  226. }
  227. default: return gcvalue(t1) == gcvalue(t2);
  228. }
  229. if (tm == NULL) return 0; /* no TM? */
  230. callTM(L, tm, t1, t2, L->top, 1); /* call TM */
  231. return !l_isfalse(L->top);
  232. }
  233. void luaV_concat (lua_State *L, int total, int last) {
  234. do {
  235. StkId top = L->base + last + 1;
  236. int n = 2; /* number of elements handled in this pass (at least 2) */
  237. if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
  238. L->top = top; /* set top to current position (in case of yield) */
  239. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  240. luaG_concaterror(L, top-2, top-1);
  241. L->top = L->ci->top; /* restore top */
  242. }
  243. else if (tsvalue(top-1)->len == 0) { /* second operand is empty? */
  244. (void)tostring(L, top - 2); /* result is first operand */ ;
  245. }
  246. else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
  247. setsvalue2s(L, top-2, rawtsvalue(top-1)); /* result is second op. */
  248. }
  249. else {
  250. /* at least two (non-empty) string values; get as many as possible */
  251. size_t tl = tsvalue(top-1)->len;
  252. char *buffer;
  253. int i;
  254. /* collect total length */
  255. for (n = 1; n < total && tostring(L, top-n-1); n++) {
  256. size_t l = tsvalue(top-n-1)->len;
  257. if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
  258. tl += l;
  259. }
  260. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  261. tl = 0;
  262. for (i=n; i>0; i--) { /* concat all strings */
  263. size_t l = tsvalue(top-i)->len;
  264. memcpy(buffer+tl, svalue(top-i), l);
  265. tl += l;
  266. }
  267. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  268. }
  269. total -= n-1; /* got `n' strings to create 1 new */
  270. last -= n-1;
  271. } while (total > 1); /* repeat until only 1 result left */
  272. }
  273. static void objlen (lua_State *L, StkId ra, const TValue *rb) {
  274. const TValue *tm;
  275. switch (ttype(rb)) {
  276. case LUA_TTABLE: {
  277. Table *h = hvalue(rb);
  278. tm = fasttm(L, h->metatable, TM_LEN);
  279. if (tm) break; /* metamethod? break switch to call it */
  280. setnvalue(ra, cast_num(luaH_getn(h))); /* else primitive len */
  281. return;
  282. }
  283. case LUA_TSTRING: {
  284. setnvalue(ra, cast_num(tsvalue(rb)->len));
  285. return;
  286. }
  287. default: { /* try metamethod */
  288. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  289. if (ttisnil(tm)) /* no metamethod? */
  290. luaG_typeerror(L, rb, "get length of");
  291. break;
  292. }
  293. }
  294. callTM(L, tm, rb, luaO_nilobject, ra, 1);
  295. }
  296. static void Arith (lua_State *L, StkId ra, const TValue *rb,
  297. const TValue *rc, TMS op) {
  298. TValue tempb, tempc;
  299. const TValue *b, *c;
  300. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  301. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  302. lua_Number nb = nvalue(b), nc = nvalue(c);
  303. switch (op) {
  304. case TM_ADD: setnvalue(ra, luai_numadd(L, nb, nc)); break;
  305. case TM_SUB: setnvalue(ra, luai_numsub(L, nb, nc)); break;
  306. case TM_MUL: setnvalue(ra, luai_nummul(L, nb, nc)); break;
  307. case TM_DIV: setnvalue(ra, luai_numdiv(L, nb, nc)); break;
  308. case TM_MOD: setnvalue(ra, luai_nummod(L, nb, nc)); break;
  309. case TM_POW: setnvalue(ra, luai_numpow(L, nb, nc)); break;
  310. case TM_UNM: setnvalue(ra, luai_numunm(L, nb)); break;
  311. default: lua_assert(0); break;
  312. }
  313. }
  314. else if (!call_binTM(L, rb, rc, ra, op))
  315. luaG_aritherror(L, rb, rc);
  316. }
  317. /*
  318. ** some macros for common tasks in `luaV_execute'
  319. */
  320. #define runtime_check(L, c) { if (!(c)) break; }
  321. #define RA(i) (base+GETARG_A(i))
  322. /* to be used after possible stack reallocation */
  323. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  324. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  325. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  326. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  327. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  328. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  329. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  330. #define dojump(L,i) { L->savedpc += (i); luai_threadyield(L);}
  331. #define Protect(x) { {x;}; base = L->base; }
  332. #define arith_op(op,tm) { \
  333. TValue *rb = RKB(i); \
  334. TValue *rc = RKC(i); \
  335. if (ttisnumber(rb) && ttisnumber(rc)) { \
  336. lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  337. setnvalue(ra, op(L, nb, nc)); \
  338. } \
  339. else \
  340. Protect(Arith(L, ra, rb, rc, tm)); \
  341. }
  342. void luaV_execute (lua_State *L) {
  343. LClosure *cl;
  344. StkId base;
  345. TValue *k;
  346. reentry: /* entry point */
  347. lua_assert(isLua(L->ci));
  348. cl = &clvalue(L->ci->func)->l;
  349. base = L->base;
  350. k = cl->p->k;
  351. /* main loop of interpreter */
  352. for (;;) {
  353. Instruction i = *(L->savedpc++);
  354. StkId ra;
  355. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  356. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  357. traceexec(L);
  358. if (L->status == LUA_YIELD) { /* did hook yield? */
  359. L->savedpc--; /* undo increment */
  360. luaD_throw(L, LUA_YIELD);
  361. }
  362. base = L->base;
  363. }
  364. /* warning!! several calls may realloc the stack and invalidate `ra' */
  365. ra = RA(i);
  366. lua_assert(base == L->base && L->base == L->ci->base);
  367. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  368. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  369. switch (GET_OPCODE(i)) {
  370. case OP_MOVE: {
  371. setobjs2s(L, ra, RB(i));
  372. continue;
  373. }
  374. case OP_LOADK: {
  375. setobj2s(L, ra, KBx(i));
  376. continue;
  377. }
  378. case OP_LOADBOOL: {
  379. setbvalue(ra, GETARG_B(i));
  380. if (GETARG_C(i)) L->savedpc++; /* skip next instruction (if C) */
  381. continue;
  382. }
  383. case OP_LOADNIL: {
  384. TValue *rb = RB(i);
  385. do {
  386. setnilvalue(rb--);
  387. } while (rb >= ra);
  388. continue;
  389. }
  390. case OP_GETUPVAL: {
  391. int b = GETARG_B(i);
  392. setobj2s(L, ra, cl->upvals[b]->v);
  393. continue;
  394. }
  395. case OP_GETGLOBAL: {
  396. TValue g;
  397. TValue *rb = KBx(i);
  398. sethvalue(L, &g, cl->env);
  399. lua_assert(ttisstring(rb));
  400. Protect(luaV_gettable(L, &g, rb, ra));
  401. continue;
  402. }
  403. case OP_GETTABLE: {
  404. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  405. continue;
  406. }
  407. case OP_SETGLOBAL: {
  408. TValue g;
  409. sethvalue(L, &g, cl->env);
  410. lua_assert(ttisstring(KBx(i)));
  411. Protect(luaV_settable(L, &g, KBx(i), ra));
  412. continue;
  413. }
  414. case OP_SETUPVAL: {
  415. UpVal *uv = cl->upvals[GETARG_B(i)];
  416. setobj(L, uv->v, ra);
  417. luaC_barrier(L, uv, ra);
  418. continue;
  419. }
  420. case OP_SETTABLE: {
  421. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  422. continue;
  423. }
  424. case OP_NEWTABLE: {
  425. int b = GETARG_B(i);
  426. int c = GETARG_C(i);
  427. Table *t = luaH_new(L);
  428. sethvalue(L, ra, t);
  429. if (b != 0 || c != 0)
  430. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  431. Protect(luaC_checkGC(L));
  432. continue;
  433. }
  434. case OP_SELF: {
  435. StkId rb = RB(i);
  436. setobjs2s(L, ra+1, rb);
  437. Protect(luaV_gettable(L, rb, RKC(i), ra));
  438. continue;
  439. }
  440. case OP_ADD: {
  441. arith_op(luai_numadd, TM_ADD);
  442. continue;
  443. }
  444. case OP_SUB: {
  445. arith_op(luai_numsub, TM_SUB);
  446. continue;
  447. }
  448. case OP_MUL: {
  449. arith_op(luai_nummul, TM_MUL);
  450. continue;
  451. }
  452. case OP_DIV: {
  453. arith_op(luai_numdiv, TM_DIV);
  454. continue;
  455. }
  456. case OP_MOD: {
  457. arith_op(luai_nummod, TM_MOD);
  458. continue;
  459. }
  460. case OP_POW: {
  461. arith_op(luai_numpow, TM_POW);
  462. continue;
  463. }
  464. case OP_UNM: {
  465. TValue *rb = RB(i);
  466. if (ttisnumber(rb)) {
  467. lua_Number nb = nvalue(rb);
  468. setnvalue(ra, luai_numunm(L, nb));
  469. }
  470. else {
  471. Protect(Arith(L, ra, rb, rb, TM_UNM));
  472. }
  473. continue;
  474. }
  475. case OP_NOT: {
  476. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  477. setbvalue(ra, res);
  478. continue;
  479. }
  480. case OP_LEN: {
  481. Protect(objlen(L, ra, RB(i)));
  482. continue;
  483. }
  484. case OP_CONCAT: {
  485. int b = GETARG_B(i);
  486. int c = GETARG_C(i);
  487. Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
  488. setobjs2s(L, RA(i), base+b);
  489. continue;
  490. }
  491. case OP_JMP: {
  492. dojump(L, GETARG_sBx(i));
  493. continue;
  494. }
  495. case OP_EQ: {
  496. TValue *rb = RKB(i);
  497. TValue *rc = RKC(i);
  498. Protect(
  499. if (equalobj(L, rb, rc) == GETARG_A(i))
  500. dojump(L, GETARG_sBx(*L->savedpc));
  501. )
  502. L->savedpc++;
  503. continue;
  504. }
  505. case OP_LT: {
  506. Protect(
  507. if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
  508. dojump(L, GETARG_sBx(*L->savedpc));
  509. )
  510. L->savedpc++;
  511. continue;
  512. }
  513. case OP_LE: {
  514. Protect(
  515. if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
  516. dojump(L, GETARG_sBx(*L->savedpc));
  517. )
  518. L->savedpc++;
  519. continue;
  520. }
  521. case OP_TEST: {
  522. if (GETARG_C(i) ? !l_isfalse(ra) : l_isfalse(ra))
  523. dojump(L, GETARG_sBx(*L->savedpc));
  524. L->savedpc++;
  525. continue;
  526. }
  527. case OP_TESTSET: {
  528. TValue *rb = RB(i);
  529. if (GETARG_C(i) ? !l_isfalse(rb) : l_isfalse(rb)) {
  530. setobjs2s(L, ra, rb);
  531. dojump(L, GETARG_sBx(*L->savedpc));
  532. }
  533. L->savedpc++;
  534. continue;
  535. }
  536. case OP_CALL: {
  537. int b = GETARG_B(i);
  538. int nresults = GETARG_C(i) - 1;
  539. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  540. if (luaD_precall(L, ra, nresults)) { /* C function? */
  541. if (nresults >= 0) L->top = L->ci->top; /* adjust results */
  542. base = L->base;
  543. continue;
  544. }
  545. else { /* Lua function */
  546. L->ci->callstatus |= CIST_REENTRY;
  547. goto reentry; /* restart luaV_execute over new Lua function */
  548. }
  549. }
  550. case OP_TAILCALL: {
  551. int b = GETARG_B(i);
  552. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  553. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  554. if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */
  555. base = L->base;
  556. continue;
  557. }
  558. else {
  559. /* tail call: put new frame in place of previous one */
  560. CallInfo *ci = L->ci - 1; /* previous frame */
  561. int aux;
  562. StkId func = ci->func;
  563. StkId pfunc = (ci+1)->func; /* previous function index */
  564. if (cl->p->sizep > 0) luaF_close(L, ci->base);
  565. L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
  566. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  567. setobjs2s(L, func+aux, pfunc+aux);
  568. ci->top = L->top = func+aux; /* correct top */
  569. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  570. ci->savedpc = L->savedpc;
  571. ci->tailcalls++; /* one more call lost */
  572. L->ci--; /* remove new frame */
  573. goto reentry;
  574. }
  575. }
  576. case OP_RETURN: {
  577. int b = GETARG_B(i);
  578. if (b != 0) L->top = ra+b-1;
  579. if (cl->p->sizep > 0) luaF_close(L, base);
  580. b = luaD_poscall(L, ra);
  581. if (!((L->ci + 1)->callstatus & CIST_REENTRY))
  582. return; /* external invocation: return */
  583. else { /* invocation via reentry: continue execution */
  584. if (b) L->top = L->ci->top;
  585. lua_assert(isLua(L->ci));
  586. lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
  587. goto reentry;
  588. }
  589. }
  590. case OP_FORLOOP: {
  591. lua_Number step = nvalue(ra+2);
  592. lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
  593. lua_Number limit = nvalue(ra+1);
  594. if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
  595. : luai_numle(L, limit, idx)) {
  596. dojump(L, GETARG_sBx(i)); /* jump back */
  597. setnvalue(ra, idx); /* update internal index... */
  598. setnvalue(ra+3, idx); /* ...and external index */
  599. }
  600. continue;
  601. }
  602. case OP_FORPREP: {
  603. const TValue *init = ra;
  604. const TValue *plimit = ra+1;
  605. const TValue *pstep = ra+2;
  606. if (!tonumber(init, ra))
  607. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  608. else if (!tonumber(plimit, ra+1))
  609. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  610. else if (!tonumber(pstep, ra+2))
  611. luaG_runerror(L, LUA_QL("for") " step must be a number");
  612. setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
  613. dojump(L, GETARG_sBx(i));
  614. continue;
  615. }
  616. case OP_TFORCALL: {
  617. StkId cb = ra + 3; /* call base */
  618. setobjs2s(L, cb+2, ra+2);
  619. setobjs2s(L, cb+1, ra+1);
  620. setobjs2s(L, cb, ra);
  621. L->baseCcalls++; /* allow yields */
  622. L->top = cb + 3; /* func. + 2 args (state and index) */
  623. Protect(luaD_call(L, cb, GETARG_C(i)));
  624. L->top = L->ci->top;
  625. L->baseCcalls--;
  626. i = *(L->savedpc++); /* go to next instruction */
  627. ra = RA(i);
  628. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  629. /* go through */
  630. }
  631. case OP_TFORLOOP: {
  632. if (!ttisnil(ra + 1)) { /* continue loop? */
  633. setobjs2s(L, ra, ra + 1); /* save control variable */
  634. dojump(L, GETARG_sBx(i)); /* jump back */
  635. }
  636. continue;
  637. }
  638. case OP_SETLIST: {
  639. int n = GETARG_B(i);
  640. int c = GETARG_C(i);
  641. int last;
  642. Table *h;
  643. if (n == 0) n = cast_int(L->top - ra) - 1;
  644. if (c == 0) {
  645. lua_assert(GET_OPCODE(*L->savedpc) == OP_EXTRAARG);
  646. c = GETARG_Ax(*L->savedpc++);
  647. }
  648. runtime_check(L, ttistable(ra));
  649. h = hvalue(ra);
  650. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  651. if (last > h->sizearray) /* needs more space? */
  652. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  653. for (; n > 0; n--) {
  654. TValue *val = ra+n;
  655. setobj2t(L, luaH_setnum(L, h, last--), val);
  656. luaC_barriert(L, h, val);
  657. }
  658. L->top = L->ci->top; /* correct top (in case of previous open call) */
  659. continue;
  660. }
  661. case OP_CLOSE: {
  662. luaF_close(L, ra);
  663. continue;
  664. }
  665. case OP_CLOSURE: {
  666. Proto *p;
  667. Closure *ncl;
  668. int nup, j;
  669. p = cl->p->p[GETARG_Bx(i)];
  670. nup = p->nups;
  671. ncl = luaF_newLclosure(L, nup, cl->env);
  672. ncl->l.p = p;
  673. setclvalue(L, ra, ncl);
  674. for (j=0; j<nup; j++, L->savedpc++) {
  675. Instruction u = *L->savedpc;
  676. if (GET_OPCODE(u) == OP_GETUPVAL)
  677. ncl->l.upvals[j] = cl->upvals[GETARG_B(u)];
  678. else {
  679. lua_assert(GET_OPCODE(u) == OP_MOVE);
  680. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(u));
  681. }
  682. }
  683. Protect(luaC_checkGC(L));
  684. continue;
  685. }
  686. case OP_VARARG: {
  687. int b = GETARG_B(i) - 1;
  688. int j;
  689. CallInfo *ci = L->ci;
  690. int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
  691. if (b == LUA_MULTRET) {
  692. Protect(luaD_checkstack(L, n));
  693. ra = RA(i); /* previous call may change the stack */
  694. b = n;
  695. L->top = ra + n;
  696. }
  697. for (j = 0; j < b; j++) {
  698. if (j < n) {
  699. setobjs2s(L, ra + j, ci->base - n + j);
  700. }
  701. else {
  702. setnilvalue(ra + j);
  703. }
  704. }
  705. continue;
  706. }
  707. case OP_EXTRAARG: {
  708. luaG_runerror(L, "bad opcode");
  709. return;
  710. }
  711. }
  712. }
  713. }