lvm.c 23 KB

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