lvm.c 23 KB

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