lvm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. ** $Id: lvm.c,v 2.3 2004/03/26 14:02:41 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->u.l.savedpc;
  48. ci->u.l.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. 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 set */
  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. prepTMcall(L, tm, t, key);
  104. callTMres(L, val);
  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. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  114. const TValue *tm;
  115. if (ttistable(t)) { /* `t' is a table? */
  116. Table *h = hvalue(t);
  117. TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
  118. if (!ttisnil(oldval) || /* result is no nil? */
  119. (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
  120. setobj2t(L, oldval, val);
  121. luaC_barrier(L, h, val);
  122. return;
  123. }
  124. /* else will try the tag method */
  125. }
  126. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  127. luaG_typeerror(L, t, "index");
  128. if (ttisfunction(tm)) {
  129. prepTMcall(L, tm, t, key);
  130. setobj2s(L, L->top+3, val); /* 3th argument */
  131. callTM(L);
  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. prepTMcall(L, tm, p1, p2);
  145. callTMres(L, res);
  146. return 1;
  147. }
  148. static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
  149. TMS event) {
  150. const TValue *tm1 = fasttm(L, mt1, event);
  151. const TValue *tm2;
  152. if (tm1 == NULL) return NULL; /* no metamethod */
  153. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  154. tm2 = fasttm(L, mt2, event);
  155. if (tm2 == NULL) return NULL; /* no metamethod */
  156. if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
  157. return tm1;
  158. return NULL;
  159. }
  160. static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
  161. TMS event) {
  162. const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
  163. const TValue *tm2;
  164. if (ttisnil(tm1)) return -1; /* no metamethod? */
  165. tm2 = luaT_gettmbyobj(L, p2, event);
  166. if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
  167. return -1;
  168. prepTMcall(L, tm1, p1, p2);
  169. callTMres(L, L->top);
  170. return !l_isfalse(L->top);
  171. }
  172. static int luaV_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 nvalue(l) < nvalue(r);
  198. else if (ttisstring(l))
  199. return luaV_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 luaV_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 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_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 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. prepTMcall(L, tm, t1, t2);
  241. callTMres(L, L->top); /* call TM */
  242. return !l_isfalse(L->top);
  243. }
  244. void luaV_concat (lua_State *L, int total, int last) {
  245. do {
  246. StkId top = L->base + last + 1;
  247. int n = 2; /* number of elements handled in this pass (at least 2) */
  248. if (!tostring(L, top-2) || !tostring(L, top-1)) {
  249. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  250. luaG_concaterror(L, top-2, top-1);
  251. } else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */
  252. /* at least two string values; get as many as possible */
  253. lu_mem tl = cast(lu_mem, tsvalue(top-1)->len) +
  254. cast(lu_mem, tsvalue(top-2)->len);
  255. char *buffer;
  256. int i;
  257. while (n < total && tostring(L, top-n-1)) { /* collect total length */
  258. tl += tsvalue(top-n-1)->len;
  259. n++;
  260. }
  261. if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
  262. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  263. tl = 0;
  264. for (i=n; i>0; i--) { /* concat all strings */
  265. size_t l = tsvalue(top-i)->len;
  266. memcpy(buffer+tl, svalue(top-i), l);
  267. tl += l;
  268. }
  269. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  270. }
  271. total -= n-1; /* got `n' strings to create 1 new */
  272. last -= n-1;
  273. } while (total > 1); /* repeat until only 1 result left */
  274. }
  275. static StkId Arith (lua_State *L, StkId ra, const TValue *rb,
  276. const TValue *rc, TMS op, const Instruction *pc) {
  277. TValue tempb, tempc;
  278. const TValue *b, *c;
  279. L->ci->u.l.savedpc = pc;
  280. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  281. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  282. switch (op) {
  283. case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break;
  284. case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break;
  285. case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break;
  286. case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break;
  287. case TM_POW: {
  288. const TValue *f = luaH_getstr(hvalue(gt(L)), G(L)->tmname[TM_POW]);
  289. if (!ttisfunction(f))
  290. luaG_runerror(L, "`__pow' (`^' operator) is not a function");
  291. prepTMcall(L, f, b, c);
  292. callTMres(L, ra);
  293. break;
  294. }
  295. default: lua_assert(0); break;
  296. }
  297. }
  298. else if (!call_binTM(L, rb, rc, ra, op))
  299. luaG_aritherror(L, rb, rc);
  300. return L->base;
  301. }
  302. /*
  303. ** some macros for common tasks in `luaV_execute'
  304. */
  305. #define runtime_check(L, c) { if (!(c)) return 0; }
  306. #define RA(i) (base+GETARG_A(i))
  307. /* to be used after possible stack reallocation */
  308. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  309. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  310. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  311. (GETARG_B(i) < MAXSTACK) ? base+GETARG_B(i) : k+GETARG_B(i)-MAXSTACK)
  312. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  313. (GETARG_C(i) < MAXSTACK) ? base+GETARG_C(i) : k+GETARG_C(i)-MAXSTACK)
  314. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  315. #define dojump(pc, i) ((pc) += (i))
  316. StkId luaV_execute (lua_State *L, int nexeccalls) {
  317. LClosure *cl;
  318. TValue *k;
  319. StkId base;
  320. const Instruction *pc;
  321. callentry: /* entry point when calling new functions */
  322. if (L->hookmask & LUA_MASKCALL)
  323. luaD_callhook(L, LUA_HOOKCALL, -1);
  324. retentry: /* entry point when returning to old functions */
  325. pc = L->ci->u.l.savedpc;
  326. base = L->base;
  327. cl = &clvalue(base - 1)->l;
  328. k = cl->p->k;
  329. /* main loop of interpreter */
  330. for (;;) {
  331. const Instruction i = *pc++;
  332. StkId ra;
  333. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  334. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  335. traceexec(L, pc); /***/
  336. if (L->isSuspended) { /* did hook yield? */
  337. L->ci->u.l.savedpc = pc - 1;
  338. return NULL;
  339. }
  340. base = L->base;
  341. }
  342. /* warning!! several calls may realloc the stack and invalidate `ra' */
  343. ra = RA(i);
  344. lua_assert(base == L->ci->base && base == L->base);
  345. lua_assert(L->top <= L->stack + L->stacksize && L->top >= base);
  346. lua_assert(L->top == L->ci->top ||
  347. GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  348. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  349. switch (GET_OPCODE(i)) {
  350. case OP_MOVE: {
  351. setobjs2s(L, ra, RB(i));
  352. break;
  353. }
  354. case OP_LOADK: {
  355. setobj2s(L, ra, KBx(i));
  356. break;
  357. }
  358. case OP_LOADBOOL: {
  359. setbvalue(ra, GETARG_B(i));
  360. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  361. break;
  362. }
  363. case OP_LOADNIL: {
  364. TValue *rb = RB(i);
  365. do {
  366. setnilvalue(rb--);
  367. } while (rb >= ra);
  368. break;
  369. }
  370. case OP_GETUPVAL: {
  371. int b = GETARG_B(i);
  372. setobj2s(L, ra, cl->upvals[b]->v);
  373. break;
  374. }
  375. case OP_GETGLOBAL: {
  376. TValue *rb = KBx(i);
  377. lua_assert(ttisstring(rb) && ttistable(&cl->g));
  378. L->ci->u.l.savedpc = pc;
  379. luaV_gettable(L, &cl->g, rb, ra); /***/
  380. base = L->base;
  381. break;
  382. }
  383. case OP_GETTABLE: {
  384. L->ci->u.l.savedpc = pc;
  385. luaV_gettable(L, RB(i), RKC(i), ra); /***/
  386. base = L->base;
  387. break;
  388. }
  389. case OP_SETGLOBAL: {
  390. lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
  391. L->ci->u.l.savedpc = pc;
  392. luaV_settable(L, &cl->g, KBx(i), ra); /***/
  393. base = L->base;
  394. break;
  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. break;
  401. }
  402. case OP_SETTABLE: {
  403. L->ci->u.l.savedpc = pc;
  404. luaV_settable(L, ra, RKB(i), RKC(i)); /***/
  405. base = L->base;
  406. break;
  407. }
  408. case OP_NEWTABLE: {
  409. int b = GETARG_B(i);
  410. b = fb2int(b);
  411. sethvalue(L, ra, luaH_new(L, b, GETARG_C(i)));
  412. L->ci->u.l.savedpc = pc;
  413. luaC_checkGC(L); /***/
  414. base = L->base;
  415. break;
  416. }
  417. case OP_SELF: {
  418. StkId rb = RB(i);
  419. setobjs2s(L, ra+1, rb);
  420. L->ci->u.l.savedpc = pc;
  421. luaV_gettable(L, rb, RKC(i), ra); /***/
  422. base = L->base;
  423. break;
  424. }
  425. case OP_ADD: {
  426. TValue *rb = RKB(i);
  427. TValue *rc = RKC(i);
  428. if (ttisnumber(rb) && ttisnumber(rc)) {
  429. setnvalue(ra, nvalue(rb) + nvalue(rc));
  430. }
  431. else
  432. base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/
  433. break;
  434. }
  435. case OP_SUB: {
  436. TValue *rb = RKB(i);
  437. TValue *rc = RKC(i);
  438. if (ttisnumber(rb) && ttisnumber(rc)) {
  439. setnvalue(ra, nvalue(rb) - nvalue(rc));
  440. }
  441. else
  442. base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/
  443. break;
  444. }
  445. case OP_MUL: {
  446. TValue *rb = RKB(i);
  447. TValue *rc = RKC(i);
  448. if (ttisnumber(rb) && ttisnumber(rc)) {
  449. setnvalue(ra, nvalue(rb) * nvalue(rc));
  450. }
  451. else
  452. base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/
  453. break;
  454. }
  455. case OP_DIV: {
  456. TValue *rb = RKB(i);
  457. TValue *rc = RKC(i);
  458. if (ttisnumber(rb) && ttisnumber(rc)) {
  459. setnvalue(ra, nvalue(rb) / nvalue(rc));
  460. }
  461. else
  462. base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/
  463. break;
  464. }
  465. case OP_POW: {
  466. base = Arith(L, ra, RKB(i), RKC(i), TM_POW, pc); /***/
  467. break;
  468. }
  469. case OP_UNM: {
  470. const TValue *rb = RB(i);
  471. TValue temp;
  472. if (tonumber(rb, &temp)) {
  473. setnvalue(ra, -nvalue(rb));
  474. }
  475. else {
  476. setnilvalue(&temp);
  477. L->ci->u.l.savedpc = pc;
  478. if (!call_binTM(L, RB(i), &temp, ra, TM_UNM)) /***/
  479. luaG_aritherror(L, RB(i), &temp);
  480. base = L->base;
  481. }
  482. break;
  483. }
  484. case OP_NOT: {
  485. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  486. setbvalue(ra, res);
  487. break;
  488. }
  489. case OP_CONCAT: {
  490. int b = GETARG_B(i);
  491. int c = GETARG_C(i);
  492. L->ci->u.l.savedpc = pc;
  493. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */ /***/
  494. luaC_checkGC(L); /***/
  495. base = L->base;
  496. setobjs2s(L, RA(i), base+b);
  497. break;
  498. }
  499. case OP_JMP: {
  500. dojump(pc, GETARG_sBx(i));
  501. break;
  502. }
  503. case OP_EQ: {
  504. L->ci->u.l.savedpc = pc;
  505. if (equalobj(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  506. else dojump(pc, GETARG_sBx(*pc) + 1);
  507. base = L->base;
  508. break;
  509. }
  510. case OP_LT: {
  511. L->ci->u.l.savedpc = pc;
  512. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  513. else dojump(pc, GETARG_sBx(*pc) + 1);
  514. base = L->base;
  515. break;
  516. }
  517. case OP_LE: {
  518. L->ci->u.l.savedpc = pc;
  519. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  520. else dojump(pc, GETARG_sBx(*pc) + 1);
  521. base = L->base;
  522. break;
  523. }
  524. case OP_TEST: {
  525. TValue *rb = RB(i);
  526. if (l_isfalse(rb) == GETARG_C(i)) pc++;
  527. else {
  528. setobjs2s(L, ra, rb);
  529. dojump(pc, GETARG_sBx(*pc) + 1);
  530. }
  531. break;
  532. }
  533. case OP_CALL:
  534. case OP_TAILCALL: { /***/
  535. StkId firstResult;
  536. int b = GETARG_B(i);
  537. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  538. L->ci->u.l.savedpc = pc;
  539. firstResult = luaD_precall(L, ra);
  540. if (firstResult) {
  541. int nresults = GETARG_C(i) - 1;
  542. if (firstResult > L->top) { /* yield? */
  543. (L->ci - 1)->u.l.savedpc = pc;
  544. return NULL;
  545. }
  546. /* it was a C function (`precall' called it); adjust results */
  547. luaD_poscall(L, nresults, firstResult);
  548. if (nresults >= 0) L->top = L->ci->top;
  549. }
  550. else { /* it is a Lua function */
  551. if (GET_OPCODE(i) == OP_CALL) /* regular call? */
  552. nexeccalls++;
  553. else { /* tail call: put new frame in place of previous one */
  554. int aux;
  555. base = (L->ci - 1)->base; /* `luaD_precall' may change the stack */
  556. ra = RA(i);
  557. if (L->openupval) luaF_close(L, base);
  558. for (aux = 0; ra+aux < L->top; aux++) /* move frame down */
  559. setobjs2s(L, base+aux-1, ra+aux);
  560. (L->ci - 1)->top = L->top = base+aux; /* correct top */
  561. (L->ci - 1)->u.l.savedpc = L->ci->u.l.savedpc;
  562. (L->ci - 1)->u.l.tailcalls++; /* one more call lost */
  563. L->ci--; /* remove new frame */
  564. L->base = L->ci->base;
  565. }
  566. goto callentry;
  567. }
  568. base = L->base;
  569. break;
  570. }
  571. case OP_RETURN: {
  572. CallInfo *ci = L->ci - 1; /* previous function frame */
  573. int b = GETARG_B(i);
  574. if (b != 0) L->top = ra+b-1;
  575. if (L->openupval) luaF_close(L, base);
  576. L->ci->u.l.savedpc = pc;
  577. if (--nexeccalls == 0) /* was previous function running `here'? */
  578. return ra; /* no: return */
  579. else { /* yes: continue its execution */
  580. int nresults;
  581. lua_assert(isLua(ci));
  582. lua_assert(GET_OPCODE(*(ci->u.l.savedpc - 1)) == OP_CALL);
  583. nresults = GETARG_C(*(ci->u.l.savedpc - 1)) - 1;
  584. luaD_poscall(L, nresults, ra);
  585. if (nresults >= 0) L->top = L->ci->top;
  586. goto retentry;
  587. }
  588. }
  589. case OP_FORLOOP: {
  590. lua_Number step = nvalue(ra+2);
  591. lua_Number idx = nvalue(ra) + step; /* increment index */
  592. lua_Number limit = nvalue(ra+1);
  593. if (step > 0 ? idx <= limit : idx >= limit) {
  594. dojump(pc, GETARG_sBx(i)); /* jump back */
  595. setnvalue(ra, idx); /* update internal index... */
  596. setnvalue(ra+3, idx); /* ...and external index */
  597. }
  598. break;
  599. }
  600. case OP_FORPREP: { /***/
  601. const TValue *init = ra;
  602. const TValue *plimit = ra+1;
  603. const TValue *pstep = ra+2;
  604. L->ci->u.l.savedpc = pc;
  605. if (!tonumber(init, ra))
  606. luaG_runerror(L, "`for' initial value must be a number");
  607. else if (!tonumber(plimit, ra+1))
  608. luaG_runerror(L, "`for' limit must be a number");
  609. else if (!tonumber(pstep, ra+2))
  610. luaG_runerror(L, "`for' step must be a number");
  611. setnvalue(ra, nvalue(ra) - nvalue(pstep));
  612. dojump(pc, GETARG_sBx(i));
  613. break;
  614. }
  615. case OP_TFORLOOP: {
  616. StkId cb = ra + 3; /* call base */
  617. setobjs2s(L, cb+2, ra+2);
  618. setobjs2s(L, cb+1, ra+1);
  619. setobjs2s(L, cb, ra);
  620. L->top = cb+3; /* func. + 2 args (state and index) */
  621. L->ci->u.l.savedpc = pc;
  622. luaD_call(L, cb, GETARG_C(i)); /***/
  623. L->top = L->ci->top;
  624. base = L->base;
  625. cb = RA(i) + 3; /* previous call may change the stack */
  626. if (ttisnil(cb)) /* break loop? */
  627. pc++; /* skip jump (break loop) */
  628. else {
  629. setobjs2s(L, cb-1, cb); /* save control variable */
  630. dojump(pc, GETARG_sBx(*pc) + 1); /* jump back */
  631. }
  632. break;
  633. }
  634. case OP_TFORPREP: { /* for compatibility only */
  635. if (ttistable(ra)) {
  636. setobjs2s(L, ra+1, ra);
  637. setobj2s(L, ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
  638. }
  639. dojump(pc, GETARG_sBx(i));
  640. break;
  641. }
  642. case OP_SETLIST:
  643. case OP_SETLISTO: {
  644. int bc = GETARG_Bx(i);
  645. int n;
  646. Table *h;
  647. runtime_check(L, ttistable(ra));
  648. h = hvalue(ra);
  649. if (GET_OPCODE(i) == OP_SETLIST)
  650. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  651. else {
  652. n = L->top - ra - 1;
  653. L->top = L->ci->top;
  654. }
  655. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  656. if (bc+n > h->sizearray) /* needs more space? */
  657. luaH_resize(L, h, bc+n, h->lsizenode); /* pre-alloc it at once */
  658. for (; n > 0; n--) {
  659. TValue *val = ra+n;
  660. setobj2t(L, luaH_setnum(L, h, bc+n), val);
  661. luaC_barrier(L, h, val);
  662. }
  663. break;
  664. }
  665. case OP_CLOSE: {
  666. luaF_close(L, ra);
  667. break;
  668. }
  669. case OP_CLOSURE: {
  670. Proto *p;
  671. Closure *ncl;
  672. int nup, j;
  673. p = cl->p->p[GETARG_Bx(i)];
  674. nup = p->nups;
  675. ncl = luaF_newLclosure(L, nup, &cl->g);
  676. ncl->l.p = p;
  677. for (j=0; j<nup; j++, pc++) {
  678. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  679. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  680. else {
  681. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  682. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  683. }
  684. }
  685. setclvalue(L, ra, ncl);
  686. L->ci->u.l.savedpc = pc;
  687. luaC_checkGC(L); /***/
  688. base = L->base;
  689. break;
  690. }
  691. }
  692. }
  693. }