lvm.c 23 KB

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