lvm.c 23 KB

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