lvm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. ** $Id: lvm.c,v 2.79 2008/10/30 15:39:30 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. tm = fasttm(L, G(L)->mt[LUA_TSTRING], TM_LEN);
  285. if (tm) break; /* metamethod? break switch to call it */
  286. setnvalue(ra, cast_num(tsvalue(rb)->len));
  287. return;
  288. }
  289. default: { /* try metamethod */
  290. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  291. if (ttisnil(tm)) /* no metamethod? */
  292. luaG_typeerror(L, rb, "get length of");
  293. break;
  294. }
  295. }
  296. callTM(L, tm, rb, luaO_nilobject, ra, 1);
  297. }
  298. static void Arith (lua_State *L, StkId ra, const TValue *rb,
  299. const TValue *rc, TMS op) {
  300. TValue tempb, tempc;
  301. const TValue *b, *c;
  302. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  303. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  304. lua_Number nb = nvalue(b), nc = nvalue(c);
  305. switch (op) {
  306. case TM_ADD: setnvalue(ra, luai_numadd(L, nb, nc)); break;
  307. case TM_SUB: setnvalue(ra, luai_numsub(L, nb, nc)); break;
  308. case TM_MUL: setnvalue(ra, luai_nummul(L, nb, nc)); break;
  309. case TM_DIV: setnvalue(ra, luai_numdiv(L, nb, nc)); break;
  310. case TM_MOD: setnvalue(ra, luai_nummod(L, nb, nc)); break;
  311. case TM_POW: setnvalue(ra, luai_numpow(L, nb, nc)); break;
  312. case TM_UNM: setnvalue(ra, luai_numunm(L, nb)); break;
  313. default: lua_assert(0); break;
  314. }
  315. }
  316. else if (!call_binTM(L, rb, rc, ra, op))
  317. luaG_aritherror(L, rb, rc);
  318. }
  319. /*
  320. ** some macros for common tasks in `luaV_execute'
  321. */
  322. #define runtime_check(L, c) { if (!(c)) break; }
  323. #define RA(i) (base+GETARG_A(i))
  324. /* to be used after possible stack reallocation */
  325. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  326. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  327. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  328. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  329. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  330. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  331. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  332. #define dojump(L,i) { L->savedpc += (i); luai_threadyield(L);}
  333. #define Protect(x) { {x;}; base = L->base; }
  334. #define arith_op(op,tm) { \
  335. TValue *rb = RKB(i); \
  336. TValue *rc = RKC(i); \
  337. if (ttisnumber(rb) && ttisnumber(rc)) { \
  338. lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  339. setnvalue(ra, op(L, nb, nc)); \
  340. } \
  341. else \
  342. Protect(Arith(L, ra, rb, rc, tm)); \
  343. }
  344. void luaV_execute (lua_State *L) {
  345. LClosure *cl;
  346. StkId base;
  347. TValue *k;
  348. reentry: /* entry point */
  349. lua_assert(isLua(L->ci));
  350. cl = &clvalue(L->ci->func)->l;
  351. base = L->base;
  352. k = cl->p->k;
  353. /* main loop of interpreter */
  354. for (;;) {
  355. Instruction i = *(L->savedpc++);
  356. StkId ra;
  357. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  358. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  359. traceexec(L);
  360. if (L->status == LUA_YIELD) { /* did hook yield? */
  361. L->savedpc--; /* undo increment */
  362. luaD_throw(L, LUA_YIELD);
  363. }
  364. base = L->base;
  365. }
  366. /* warning!! several calls may realloc the stack and invalidate `ra' */
  367. ra = RA(i);
  368. lua_assert(base == L->base && L->base == L->ci->base);
  369. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  370. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  371. switch (GET_OPCODE(i)) {
  372. case OP_MOVE: {
  373. setobjs2s(L, ra, RB(i));
  374. continue;
  375. }
  376. case OP_LOADK: {
  377. setobj2s(L, ra, KBx(i));
  378. continue;
  379. }
  380. case OP_LOADBOOL: {
  381. setbvalue(ra, GETARG_B(i));
  382. if (GETARG_C(i)) L->savedpc++; /* skip next instruction (if C) */
  383. continue;
  384. }
  385. case OP_LOADNIL: {
  386. TValue *rb = RB(i);
  387. do {
  388. setnilvalue(rb--);
  389. } while (rb >= ra);
  390. continue;
  391. }
  392. case OP_GETUPVAL: {
  393. int b = GETARG_B(i);
  394. setobj2s(L, ra, cl->upvals[b]->v);
  395. continue;
  396. }
  397. case OP_GETGLOBAL: {
  398. TValue g;
  399. TValue *rb = KBx(i);
  400. sethvalue(L, &g, cl->env);
  401. lua_assert(ttisstring(rb));
  402. Protect(luaV_gettable(L, &g, rb, ra));
  403. continue;
  404. }
  405. case OP_GETTABLE: {
  406. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  407. continue;
  408. }
  409. case OP_SETGLOBAL: {
  410. TValue g;
  411. sethvalue(L, &g, cl->env);
  412. lua_assert(ttisstring(KBx(i)));
  413. Protect(luaV_settable(L, &g, KBx(i), ra));
  414. continue;
  415. }
  416. case OP_SETUPVAL: {
  417. UpVal *uv = cl->upvals[GETARG_B(i)];
  418. setobj(L, uv->v, ra);
  419. luaC_barrier(L, uv, ra);
  420. continue;
  421. }
  422. case OP_SETTABLE: {
  423. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  424. continue;
  425. }
  426. case OP_NEWTABLE: {
  427. int b = GETARG_B(i);
  428. int c = GETARG_C(i);
  429. Table *t = luaH_new(L);
  430. sethvalue(L, ra, t);
  431. if (b != 0 || c != 0)
  432. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  433. Protect(luaC_checkGC(L));
  434. continue;
  435. }
  436. case OP_SELF: {
  437. StkId rb = RB(i);
  438. setobjs2s(L, ra+1, rb);
  439. Protect(luaV_gettable(L, rb, RKC(i), ra));
  440. continue;
  441. }
  442. case OP_ADD: {
  443. arith_op(luai_numadd, TM_ADD);
  444. continue;
  445. }
  446. case OP_SUB: {
  447. arith_op(luai_numsub, TM_SUB);
  448. continue;
  449. }
  450. case OP_MUL: {
  451. arith_op(luai_nummul, TM_MUL);
  452. continue;
  453. }
  454. case OP_DIV: {
  455. arith_op(luai_numdiv, TM_DIV);
  456. continue;
  457. }
  458. case OP_MOD: {
  459. arith_op(luai_nummod, TM_MOD);
  460. continue;
  461. }
  462. case OP_POW: {
  463. arith_op(luai_numpow, TM_POW);
  464. continue;
  465. }
  466. case OP_UNM: {
  467. TValue *rb = RB(i);
  468. if (ttisnumber(rb)) {
  469. lua_Number nb = nvalue(rb);
  470. setnvalue(ra, luai_numunm(L, nb));
  471. }
  472. else {
  473. Protect(Arith(L, ra, rb, rb, TM_UNM));
  474. }
  475. continue;
  476. }
  477. case OP_NOT: {
  478. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  479. setbvalue(ra, res);
  480. continue;
  481. }
  482. case OP_LEN: {
  483. Protect(objlen(L, ra, RB(i)));
  484. continue;
  485. }
  486. case OP_CONCAT: {
  487. int b = GETARG_B(i);
  488. int c = GETARG_C(i);
  489. Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
  490. setobjs2s(L, RA(i), base+b);
  491. continue;
  492. }
  493. case OP_JMP: {
  494. dojump(L, GETARG_sBx(i));
  495. continue;
  496. }
  497. case OP_EQ: {
  498. TValue *rb = RKB(i);
  499. TValue *rc = RKC(i);
  500. Protect(
  501. if (equalobj(L, rb, rc) == GETARG_A(i))
  502. dojump(L, GETARG_sBx(*L->savedpc));
  503. )
  504. L->savedpc++;
  505. continue;
  506. }
  507. case OP_LT: {
  508. Protect(
  509. if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
  510. dojump(L, GETARG_sBx(*L->savedpc));
  511. )
  512. L->savedpc++;
  513. continue;
  514. }
  515. case OP_LE: {
  516. Protect(
  517. if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
  518. dojump(L, GETARG_sBx(*L->savedpc));
  519. )
  520. L->savedpc++;
  521. continue;
  522. }
  523. case OP_TEST: {
  524. if (GETARG_C(i) ? !l_isfalse(ra) : l_isfalse(ra))
  525. dojump(L, GETARG_sBx(*L->savedpc));
  526. L->savedpc++;
  527. continue;
  528. }
  529. case OP_TESTSET: {
  530. TValue *rb = RB(i);
  531. if (GETARG_C(i) ? !l_isfalse(rb) : l_isfalse(rb)) {
  532. setobjs2s(L, ra, rb);
  533. dojump(L, GETARG_sBx(*L->savedpc));
  534. }
  535. L->savedpc++;
  536. continue;
  537. }
  538. case OP_CALL: {
  539. int b = GETARG_B(i);
  540. int nresults = GETARG_C(i) - 1;
  541. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  542. if (luaD_precall(L, ra, nresults)) { /* C function? */
  543. if (nresults >= 0) L->top = L->ci->top; /* adjust results */
  544. base = L->base;
  545. continue;
  546. }
  547. else { /* Lua function */
  548. L->ci->callstatus |= CIST_REENTRY;
  549. goto reentry; /* restart luaV_execute over new Lua function */
  550. }
  551. }
  552. case OP_TAILCALL: {
  553. int b = GETARG_B(i);
  554. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  555. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  556. if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */
  557. base = L->base;
  558. continue;
  559. }
  560. else {
  561. /* tail call: put new frame in place of previous one */
  562. CallInfo *ci = L->ci - 1; /* previous frame */
  563. int aux;
  564. StkId func = ci->func;
  565. StkId pfunc = (ci+1)->func; /* previous function index */
  566. if (cl->p->sizep > 0) luaF_close(L, ci->base);
  567. L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
  568. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  569. setobjs2s(L, func+aux, pfunc+aux);
  570. ci->top = L->top = func+aux; /* correct top */
  571. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  572. ci->savedpc = L->savedpc;
  573. ci->tailcalls++; /* one more call lost */
  574. L->ci--; /* remove new frame */
  575. goto reentry;
  576. }
  577. }
  578. case OP_RETURN: {
  579. int b = GETARG_B(i);
  580. if (b != 0) L->top = ra+b-1;
  581. if (cl->p->sizep > 0) luaF_close(L, base);
  582. b = luaD_poscall(L, ra);
  583. if (!((L->ci + 1)->callstatus & CIST_REENTRY))
  584. return; /* external invocation: return */
  585. else { /* invocation via reentry: continue execution */
  586. if (b) L->top = L->ci->top;
  587. lua_assert(isLua(L->ci));
  588. lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
  589. goto reentry;
  590. }
  591. }
  592. case OP_FORLOOP: {
  593. lua_Number step = nvalue(ra+2);
  594. lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
  595. lua_Number limit = nvalue(ra+1);
  596. if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
  597. : luai_numle(L, limit, idx)) {
  598. dojump(L, GETARG_sBx(i)); /* jump back */
  599. setnvalue(ra, idx); /* update internal index... */
  600. setnvalue(ra+3, idx); /* ...and external index */
  601. }
  602. continue;
  603. }
  604. case OP_FORPREP: {
  605. const TValue *init = ra;
  606. const TValue *plimit = ra+1;
  607. const TValue *pstep = ra+2;
  608. if (!tonumber(init, ra))
  609. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  610. else if (!tonumber(plimit, ra+1))
  611. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  612. else if (!tonumber(pstep, ra+2))
  613. luaG_runerror(L, LUA_QL("for") " step must be a number");
  614. setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
  615. dojump(L, GETARG_sBx(i));
  616. continue;
  617. }
  618. case OP_TFORCALL: {
  619. StkId cb = ra + 3; /* call base */
  620. setobjs2s(L, cb+2, ra+2);
  621. setobjs2s(L, cb+1, ra+1);
  622. setobjs2s(L, cb, ra);
  623. L->baseCcalls++; /* allow yields */
  624. L->top = cb + 3; /* func. + 2 args (state and index) */
  625. Protect(luaD_call(L, cb, GETARG_C(i)));
  626. L->top = L->ci->top;
  627. L->baseCcalls--;
  628. i = *(L->savedpc++); /* go to next instruction */
  629. ra = RA(i);
  630. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  631. /* go through */
  632. }
  633. case OP_TFORLOOP: {
  634. if (!ttisnil(ra + 1)) { /* continue loop? */
  635. setobjs2s(L, ra, ra + 1); /* save control variable */
  636. dojump(L, GETARG_sBx(i)); /* jump back */
  637. }
  638. continue;
  639. }
  640. case OP_SETLIST: {
  641. int n = GETARG_B(i);
  642. int c = GETARG_C(i);
  643. int last;
  644. Table *h;
  645. if (n == 0) n = cast_int(L->top - ra) - 1;
  646. if (c == 0) {
  647. lua_assert(GET_OPCODE(*L->savedpc) == OP_EXTRAARG);
  648. c = GETARG_Ax(*L->savedpc++);
  649. }
  650. runtime_check(L, ttistable(ra));
  651. h = hvalue(ra);
  652. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  653. if (last > h->sizearray) /* needs more space? */
  654. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  655. for (; n > 0; n--) {
  656. TValue *val = ra+n;
  657. setobj2t(L, luaH_setnum(L, h, last--), val);
  658. luaC_barriert(L, h, val);
  659. }
  660. L->top = L->ci->top; /* correct top (in case of previous open call) */
  661. continue;
  662. }
  663. case OP_CLOSE: {
  664. luaF_close(L, ra);
  665. continue;
  666. }
  667. case OP_CLOSURE: {
  668. Proto *p;
  669. Closure *ncl;
  670. int nup, j;
  671. p = cl->p->p[GETARG_Bx(i)];
  672. nup = p->nups;
  673. ncl = luaF_newLclosure(L, nup, cl->env);
  674. ncl->l.p = p;
  675. setclvalue(L, ra, ncl);
  676. for (j=0; j<nup; j++, L->savedpc++) {
  677. Instruction u = *L->savedpc;
  678. if (GET_OPCODE(u) == OP_GETUPVAL)
  679. ncl->l.upvals[j] = cl->upvals[GETARG_B(u)];
  680. else {
  681. lua_assert(GET_OPCODE(u) == OP_MOVE);
  682. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(u));
  683. }
  684. }
  685. Protect(luaC_checkGC(L));
  686. continue;
  687. }
  688. case OP_VARARG: {
  689. int b = GETARG_B(i) - 1;
  690. int j;
  691. CallInfo *ci = L->ci;
  692. int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
  693. if (b == LUA_MULTRET) {
  694. Protect(luaD_checkstack(L, n));
  695. ra = RA(i); /* previous call may change the stack */
  696. b = n;
  697. L->top = ra + n;
  698. }
  699. for (j = 0; j < b; j++) {
  700. if (j < n) {
  701. setobjs2s(L, ra + j, ci->base - n + j);
  702. }
  703. else {
  704. setnilvalue(ra + j);
  705. }
  706. }
  707. continue;
  708. }
  709. case OP_EXTRAARG: {
  710. luaG_runerror(L, "bad opcode");
  711. return;
  712. }
  713. }
  714. }
  715. }