lvm.c 23 KB

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