lvm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. ** $Id: lvm.c,v 2.22 2005/01/10 18:17:39 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[MAX_NUMBER2STR];
  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 l_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 num_lt(nvalue(l), nvalue(r));
  210. else if (ttisstring(l))
  211. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  212. else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  213. return res;
  214. return luaG_ordererror(L, l, r);
  215. }
  216. static int 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 num_le(nvalue(l), nvalue(r));
  222. else if (ttisstring(l))
  223. return l_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 num_eq(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. size_t tl = tsvalue(top-1)->len;
  266. char *buffer;
  267. int i;
  268. /* collect total length */
  269. for (n = 1; n < total && tostring(L, top-n-1); n++) {
  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. }
  274. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  275. tl = 0;
  276. for (i=n; i>0; i--) { /* concat all strings */
  277. size_t l = tsvalue(top-i)->len;
  278. memcpy(buffer+tl, svalue(top-i), l);
  279. tl += l;
  280. }
  281. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  282. }
  283. total -= n-1; /* got `n' strings to create 1 new */
  284. last -= n-1;
  285. } while (total > 1); /* repeat until only 1 result left */
  286. }
  287. static StkId Arith (lua_State *L, StkId ra, const TValue *rb,
  288. const TValue *rc, TMS op, const Instruction *pc) {
  289. TValue tempb, tempc;
  290. const TValue *b, *c;
  291. L->ci->savedpc = pc;
  292. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  293. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  294. switch (op) {
  295. case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break;
  296. case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break;
  297. case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break;
  298. case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break;
  299. case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break;
  300. default: lua_assert(0); break;
  301. }
  302. }
  303. else if (!call_binTM(L, rb, rc, ra, op))
  304. luaG_aritherror(L, rb, rc);
  305. return L->base;
  306. }
  307. /*
  308. ** some macros for common tasks in `luaV_execute'
  309. */
  310. #define runtime_check(L, c) { if (!(c)) return 0; }
  311. #define RA(i) (base+GETARG_A(i))
  312. /* to be used after possible stack reallocation */
  313. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  314. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  315. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  316. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  317. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  318. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  319. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  320. #define dojump(L,pc,i) {(pc) += (i); lua_threadyield(L);}
  321. StkId luaV_execute (lua_State *L, int nexeccalls) {
  322. LClosure *cl;
  323. TValue *k;
  324. StkId base;
  325. const Instruction *pc;
  326. callentry: /* entry point when calling new functions */
  327. if (L->hookmask & LUA_MASKCALL)
  328. luaD_callhook(L, LUA_HOOKCALL, -1);
  329. retentry: /* entry point when returning to old functions */
  330. pc = L->ci->savedpc;
  331. cl = &clvalue(L->ci->func)->l;
  332. base = L->base;
  333. k = cl->p->k;
  334. /* main loop of interpreter */
  335. for (;;) {
  336. const Instruction i = *pc++;
  337. StkId ra;
  338. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  339. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  340. traceexec(L, pc); /***/
  341. if (L->status == LUA_YIELD) { /* did hook yield? */
  342. L->ci->savedpc = pc - 1;
  343. return NULL;
  344. }
  345. base = L->base;
  346. }
  347. /* warning!! several calls may realloc the stack and invalidate `ra' */
  348. ra = RA(i);
  349. lua_assert(base == L->ci->base && base == L->base);
  350. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  351. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  352. switch (GET_OPCODE(i)) {
  353. case OP_MOVE: {
  354. setobjs2s(L, ra, RB(i));
  355. continue;
  356. }
  357. case OP_LOADK: {
  358. setobj2s(L, ra, KBx(i));
  359. continue;
  360. }
  361. case OP_LOADBOOL: {
  362. setbvalue(ra, GETARG_B(i));
  363. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  364. continue;
  365. }
  366. case OP_LOADNIL: {
  367. TValue *rb = RB(i);
  368. do {
  369. setnilvalue(rb--);
  370. } while (rb >= ra);
  371. continue;
  372. }
  373. case OP_GETUPVAL: {
  374. int b = GETARG_B(i);
  375. setobj2s(L, ra, cl->upvals[b]->v);
  376. continue;
  377. }
  378. case OP_GETGLOBAL: {
  379. TValue *rb = KBx(i);
  380. lua_assert(ttisstring(rb) && ttistable(&cl->g));
  381. base = luaV_gettable(L, &cl->g, rb, ra, pc); /***/
  382. continue;
  383. }
  384. case OP_GETTABLE: {
  385. base = luaV_gettable(L, RB(i), RKC(i), ra, pc); /***/
  386. continue;
  387. }
  388. case OP_SETGLOBAL: {
  389. lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
  390. base = luaV_settable(L, &cl->g, KBx(i), ra, pc); /***/
  391. continue;
  392. }
  393. case OP_SETUPVAL: {
  394. UpVal *uv = cl->upvals[GETARG_B(i)];
  395. setobj(L, uv->v, ra);
  396. luaC_barrier(L, uv, ra);
  397. continue;
  398. }
  399. case OP_SETTABLE: {
  400. base = luaV_settable(L, ra, RKB(i), RKC(i), pc); /***/
  401. continue;
  402. }
  403. case OP_NEWTABLE: {
  404. int b = GETARG_B(i);
  405. int c = GETARG_C(i);
  406. sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
  407. L->ci->savedpc = pc;
  408. luaC_checkGC(L); /***/
  409. base = L->base;
  410. continue;
  411. }
  412. case OP_SELF: {
  413. StkId rb = RB(i);
  414. setobjs2s(L, ra+1, rb);
  415. base = luaV_gettable(L, rb, RKC(i), ra, pc); /***/
  416. continue;
  417. }
  418. case OP_ADD: {
  419. TValue *rb = RKB(i);
  420. TValue *rc = RKC(i);
  421. if (ttisnumber(rb) && ttisnumber(rc)) {
  422. setnvalue(ra, num_add(nvalue(rb), nvalue(rc)));
  423. }
  424. else
  425. base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/
  426. continue;
  427. }
  428. case OP_SUB: {
  429. TValue *rb = RKB(i);
  430. TValue *rc = RKC(i);
  431. if (ttisnumber(rb) && ttisnumber(rc)) {
  432. setnvalue(ra, num_sub(nvalue(rb), nvalue(rc)));
  433. }
  434. else
  435. base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/
  436. continue;
  437. }
  438. case OP_MUL: {
  439. TValue *rb = RKB(i);
  440. TValue *rc = RKC(i);
  441. if (ttisnumber(rb) && ttisnumber(rc)) {
  442. setnvalue(ra, num_mul(nvalue(rb), nvalue(rc)));
  443. }
  444. else
  445. base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/
  446. continue;
  447. }
  448. case OP_DIV: {
  449. TValue *rb = RKB(i);
  450. TValue *rc = RKC(i);
  451. if (ttisnumber(rb) && ttisnumber(rc)) {
  452. setnvalue(ra, num_div(nvalue(rb), nvalue(rc)));
  453. }
  454. else
  455. base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/
  456. continue;
  457. }
  458. case OP_POW: {
  459. TValue *rb = RKB(i);
  460. TValue *rc = RKC(i);
  461. if (ttisnumber(rb) && ttisnumber(rc)) {
  462. setnvalue(ra, num_pow(nvalue(rb), nvalue(rc)));
  463. }
  464. else
  465. base = Arith(L, ra, rb, rc, TM_POW, pc); /***/
  466. continue;
  467. }
  468. case OP_UNM: {
  469. const TValue *rb = RB(i);
  470. TValue temp;
  471. if (tonumber(rb, &temp)) {
  472. setnvalue(ra, num_unm(nvalue(rb)));
  473. }
  474. else {
  475. setnilvalue(&temp);
  476. L->ci->savedpc = pc;
  477. if (!call_binTM(L, RB(i), &temp, ra, TM_UNM)) /***/
  478. luaG_aritherror(L, RB(i), &temp);
  479. base = L->base;
  480. }
  481. continue;
  482. }
  483. case OP_NOT: {
  484. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  485. setbvalue(ra, res);
  486. continue;
  487. }
  488. case OP_CONCAT: {
  489. int b = GETARG_B(i);
  490. int c = GETARG_C(i);
  491. L->ci->savedpc = pc;
  492. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */ /***/
  493. luaC_checkGC(L); /***/
  494. base = L->base;
  495. setobjs2s(L, RA(i), base+b);
  496. continue;
  497. }
  498. case OP_JMP: {
  499. dojump(L, pc, GETARG_sBx(i));
  500. continue;
  501. }
  502. case OP_EQ: {
  503. L->ci->savedpc = pc;
  504. if (equalobj(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  505. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  506. base = L->base;
  507. continue;
  508. }
  509. case OP_LT: {
  510. L->ci->savedpc = pc;
  511. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  512. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  513. base = L->base;
  514. continue;
  515. }
  516. case OP_LE: {
  517. L->ci->savedpc = pc;
  518. if (lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  519. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  520. base = L->base;
  521. continue;
  522. }
  523. case OP_TEST: {
  524. TValue *rb = RB(i);
  525. if (l_isfalse(rb) == GETARG_C(i)) pc++;
  526. else {
  527. setobjs2s(L, ra, rb);
  528. dojump(L, pc, GETARG_sBx(*pc) + 1);
  529. }
  530. continue;
  531. }
  532. case OP_CALL: { /***/
  533. int pcr;
  534. int b = GETARG_B(i);
  535. int nresults = GETARG_C(i) - 1;
  536. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  537. L->ci->savedpc = pc;
  538. pcr = luaD_precall(L, ra, nresults);
  539. if (pcr == PCRLUA) {
  540. nexeccalls++;
  541. goto callentry; /* restart luaV_execute over new Lua function */
  542. }
  543. else if (pcr == PCRC) {
  544. /* it was a C function (`precall' called it); adjust results */
  545. if (nresults >= 0) L->top = L->ci->top;
  546. base = L->base;
  547. continue;
  548. }
  549. else {
  550. lua_assert(pcr == PCRYIELD);
  551. return NULL;
  552. }
  553. }
  554. case OP_TAILCALL: { /***/
  555. int pcr;
  556. int b = GETARG_B(i);
  557. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  558. L->ci->savedpc = pc;
  559. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  560. pcr = luaD_precall(L, ra, LUA_MULTRET);
  561. if (pcr == PCRLUA) {
  562. /* tail call: put new frame in place of previous one */
  563. CallInfo *ci = L->ci - 1; /* previous frame */
  564. int aux;
  565. StkId func = ci->func;
  566. StkId pfunc = (ci+1)->func; /* previous function index */
  567. base = ci->base = ci->func + ((ci+1)->base - pfunc);
  568. L->base = base;
  569. if (L->openupval) luaF_close(L, base);
  570. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  571. setobjs2s(L, func+aux, pfunc+aux);
  572. ci->top = L->top = func+aux; /* correct top */
  573. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  574. ci->savedpc = L->ci->savedpc;
  575. ci->tailcalls++; /* one more call lost */
  576. L->ci--; /* remove new frame */
  577. goto callentry;
  578. }
  579. else if (pcr == PCRC) {
  580. /* it was a C function (`precall' called it) */
  581. base = L->base;
  582. continue;
  583. }
  584. else {
  585. lua_assert(pcr == PCRYIELD);
  586. return NULL;
  587. }
  588. }
  589. case OP_RETURN: {
  590. CallInfo *ci = L->ci - 1; /* previous function frame */
  591. int b = GETARG_B(i);
  592. if (b != 0) L->top = ra+b-1;
  593. if (L->openupval) luaF_close(L, base);
  594. L->ci->savedpc = pc;
  595. if (--nexeccalls == 0) /* was previous function running `here'? */
  596. return ra; /* no: return */
  597. else { /* yes: continue its execution */
  598. int nresults = (ci+1)->nresults;
  599. lua_assert(isLua(ci));
  600. lua_assert(GET_OPCODE(*(ci->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 = num_add(nvalue(ra), step); /* increment index */
  609. lua_Number limit = nvalue(ra+1);
  610. if (step > 0 ? num_le(idx, limit) : num_le(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. L->ci->savedpc = pc;
  622. if (!tonumber(init, ra))
  623. luaG_runerror(L, "`for' initial value must be a number");
  624. else if (!tonumber(plimit, ra+1))
  625. luaG_runerror(L, "`for' limit must be a number");
  626. else if (!tonumber(pstep, ra+2))
  627. luaG_runerror(L, "`for' step must be a number");
  628. setnvalue(ra, num_sub(nvalue(ra), nvalue(pstep)));
  629. dojump(L, pc, GETARG_sBx(i));
  630. continue;
  631. }
  632. case OP_TFORLOOP: {
  633. StkId cb = ra + 3; /* call base */
  634. setobjs2s(L, cb+2, ra+2);
  635. setobjs2s(L, cb+1, ra+1);
  636. setobjs2s(L, cb, ra);
  637. L->top = cb+3; /* func. + 2 args (state and index) */
  638. L->ci->savedpc = pc;
  639. luaD_call(L, cb, GETARG_C(i)); /***/
  640. L->top = L->ci->top;
  641. base = L->base;
  642. cb = RA(i) + 3; /* previous call may change the stack */
  643. if (ttisnil(cb)) /* break loop? */
  644. pc++; /* skip jump (break loop) */
  645. else {
  646. setobjs2s(L, cb-1, cb); /* save control variable */
  647. dojump(L, pc, GETARG_sBx(*pc) + 1); /* jump back */
  648. }
  649. continue;
  650. }
  651. case OP_TFORPREP: { /* for compatibility only */
  652. if (ttistable(ra)) {
  653. setobjs2s(L, ra+1, ra);
  654. setobj2s(L, ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
  655. }
  656. dojump(L, pc, GETARG_sBx(i));
  657. continue;
  658. }
  659. case OP_SETLIST: {
  660. int n = GETARG_B(i);
  661. int c = GETARG_C(i);
  662. int last;
  663. Table *h;
  664. runtime_check(L, ttistable(ra));
  665. h = hvalue(ra);
  666. if (n == 0) {
  667. n = L->top - ra - 1;
  668. L->top = L->ci->top;
  669. }
  670. if (c == 0) c = cast(int, *pc++);
  671. last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1;
  672. if (last > h->sizearray) /* needs more space? */
  673. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  674. for (; n > 0; n--) {
  675. TValue *val = ra+n;
  676. setobj2t(L, luaH_setnum(L, h, last--), val);
  677. luaC_barriert(L, h, val);
  678. }
  679. continue;
  680. }
  681. case OP_CLOSE: {
  682. luaF_close(L, ra);
  683. continue;
  684. }
  685. case OP_CLOSURE: {
  686. Proto *p;
  687. Closure *ncl;
  688. int nup, j;
  689. p = cl->p->p[GETARG_Bx(i)];
  690. nup = p->nups;
  691. ncl = luaF_newLclosure(L, nup, &cl->g);
  692. ncl->l.p = p;
  693. for (j=0; j<nup; j++, pc++) {
  694. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  695. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  696. else {
  697. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  698. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  699. }
  700. }
  701. setclvalue(L, ra, ncl);
  702. L->ci->savedpc = pc;
  703. luaC_checkGC(L); /***/
  704. base = L->base;
  705. continue;
  706. }
  707. case OP_VARARG: {
  708. int b = GETARG_B(i) - 1;
  709. int j;
  710. CallInfo *ci = L->ci;
  711. int n = ci->base - ci->func - cl->p->numparams - 1;
  712. if (b == LUA_MULTRET) {
  713. b = n;
  714. L->top = ra + n;
  715. }
  716. for (j=0; j<b && j<n; j++)
  717. setobjs2s(L, ra+j, ci->base - n + j);
  718. for (; j<b; j++)
  719. setnilvalue(ra+j);
  720. continue;
  721. }
  722. }
  723. }
  724. }