lvm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. ** $Id: lvm.c,v 2.17 2004/11/01 15:06:50 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define lvm_c
  9. #define LUA_CORE
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "lobject.h"
  16. #include "lopcodes.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #include "lvm.h"
  22. /* limit for table tag-method chains (to avoid loops) */
  23. #define MAXTAGLOOP 100
  24. const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
  25. lua_Number num;
  26. if (ttisnumber(obj)) return obj;
  27. if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  28. setnvalue(n, num);
  29. return n;
  30. }
  31. else
  32. return NULL;
  33. }
  34. int luaV_tostring (lua_State *L, StkId obj) {
  35. if (!ttisnumber(obj))
  36. return 0;
  37. else {
  38. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  39. lua_number2str(s, nvalue(obj));
  40. setsvalue2s(L, obj, luaS_new(L, s));
  41. return 1;
  42. }
  43. }
  44. static void traceexec (lua_State *L, const Instruction *pc) {
  45. lu_byte mask = L->hookmask;
  46. CallInfo *ci = L->ci;
  47. const Instruction *oldpc = ci->savedpc;
  48. ci->savedpc = pc;
  49. if (mask > LUA_MASKLINE) { /* instruction-hook set? */
  50. if (L->hookcount == 0) {
  51. resethookcount(L);
  52. luaD_callhook(L, LUA_HOOKCOUNT, -1);
  53. return;
  54. }
  55. }
  56. if (mask & LUA_MASKLINE) {
  57. Proto *p = ci_func(ci)->l.p;
  58. int npc = pcRel(pc, p);
  59. int newline = getline(p, npc);
  60. /* call linehook when enter a new function, when jump back (loop),
  61. or when enter a new line */
  62. if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
  63. luaD_callhook(L, LUA_HOOKLINE, newline);
  64. }
  65. }
  66. static void prepTMcall (lua_State *L, const TValue *f,
  67. const TValue *p1, const TValue *p2) {
  68. setobj2s(L, L->top, f); /* push function */
  69. setobj2s(L, L->top+1, p1); /* 1st argument */
  70. setobj2s(L, L->top+2, p2); /* 2nd argument */
  71. }
  72. static void callTMres (lua_State *L, StkId res) {
  73. ptrdiff_t result = savestack(L, res);
  74. luaD_checkstack(L, 3);
  75. L->top += 3;
  76. luaD_call(L, L->top - 3, 1);
  77. res = restorestack(L, result);
  78. L->top--;
  79. setobjs2s(L, res, L->top);
  80. }
  81. static void callTM (lua_State *L) {
  82. luaD_checkstack(L, 4);
  83. L->top += 4;
  84. luaD_call(L, L->top - 4, 0);
  85. }
  86. StkId luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val,
  87. const Instruction *pc) {
  88. int loop;
  89. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  90. const TValue *tm;
  91. if (ttistable(t)) { /* `t' is a table? */
  92. Table *h = hvalue(t);
  93. const TValue *res = luaH_get(h, key); /* do a primitive set */
  94. if (!ttisnil(res) || /* result is no nil? */
  95. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  96. setobj2s(L, val, res);
  97. return L->base;
  98. }
  99. /* else will try the tag method */
  100. }
  101. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) {
  102. L->ci->savedpc = pc;
  103. luaG_typeerror(L, t, "index");
  104. }
  105. if (ttisfunction(tm)) {
  106. L->ci->savedpc = pc;
  107. prepTMcall(L, tm, t, key);
  108. callTMres(L, val);
  109. return L->base;
  110. }
  111. t = tm; /* else repeat with `tm' */
  112. }
  113. L->ci->savedpc = pc;
  114. luaG_runerror(L, "loop in gettable");
  115. return NULL; /* to avoid warnings */
  116. }
  117. StkId luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val,
  118. const Instruction *pc) {
  119. int loop;
  120. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  121. const TValue *tm;
  122. if (ttistable(t)) { /* `t' is a table? */
  123. Table *h = hvalue(t);
  124. TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
  125. if (!ttisnil(oldval) || /* result is no nil? */
  126. (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
  127. setobj2t(L, oldval, val);
  128. luaC_barriert(L, h, val);
  129. return L->base;
  130. }
  131. /* else will try the tag method */
  132. }
  133. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) {
  134. L->ci->savedpc = pc;
  135. luaG_typeerror(L, t, "index");
  136. }
  137. if (ttisfunction(tm)) {
  138. L->ci->savedpc = pc;
  139. prepTMcall(L, tm, t, key);
  140. setobj2s(L, L->top+3, val); /* 3th argument */
  141. callTM(L);
  142. return L->base;
  143. }
  144. t = tm; /* else repeat with `tm' */
  145. }
  146. L->ci->savedpc = pc;
  147. luaG_runerror(L, "loop in settable");
  148. return NULL; /* to avoid warnings */
  149. }
  150. static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
  151. StkId res, TMS event) {
  152. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  153. if (ttisnil(tm))
  154. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  155. if (!ttisfunction(tm)) return 0;
  156. prepTMcall(L, tm, p1, p2);
  157. callTMres(L, res);
  158. return 1;
  159. }
  160. static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
  161. TMS event) {
  162. const TValue *tm1 = fasttm(L, mt1, event);
  163. const TValue *tm2;
  164. if (tm1 == NULL) return NULL; /* no metamethod */
  165. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  166. tm2 = fasttm(L, mt2, event);
  167. if (tm2 == NULL) return NULL; /* no metamethod */
  168. if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
  169. return tm1;
  170. return NULL;
  171. }
  172. static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
  173. TMS event) {
  174. const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
  175. const TValue *tm2;
  176. if (ttisnil(tm1)) return -1; /* no metamethod? */
  177. tm2 = luaT_gettmbyobj(L, p2, event);
  178. if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
  179. return -1;
  180. prepTMcall(L, tm1, p1, p2);
  181. callTMres(L, L->top);
  182. return !l_isfalse(L->top);
  183. }
  184. static int 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 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 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 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, nvalue(b) + nvalue(c)); break;
  296. case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break;
  297. case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break;
  298. case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break;
  299. case TM_POW: {
  300. const TValue *f = luaH_getstr(hvalue(gt(L)), G(L)->tmname[TM_POW]);
  301. if (!ttisfunction(f))
  302. luaG_runerror(L, "`__pow' (`^' operator) is not a function");
  303. prepTMcall(L, f, b, c);
  304. callTMres(L, ra);
  305. break;
  306. }
  307. default: lua_assert(0); break;
  308. }
  309. }
  310. else if (!call_binTM(L, rb, rc, ra, op))
  311. luaG_aritherror(L, rb, rc);
  312. return L->base;
  313. }
  314. /*
  315. ** some macros for common tasks in `luaV_execute'
  316. */
  317. #define runtime_check(L, c) { if (!(c)) return 0; }
  318. #define RA(i) (base+GETARG_A(i))
  319. /* to be used after possible stack reallocation */
  320. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  321. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  322. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  323. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  324. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  325. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  326. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  327. #define dojump(L,pc,i) {(pc) += (i); lua_threadyield(L);}
  328. StkId luaV_execute (lua_State *L, int nexeccalls) {
  329. LClosure *cl;
  330. TValue *k;
  331. StkId base;
  332. const Instruction *pc;
  333. callentry: /* entry point when calling new functions */
  334. if (L->hookmask & LUA_MASKCALL)
  335. luaD_callhook(L, LUA_HOOKCALL, -1);
  336. retentry: /* entry point when returning to old functions */
  337. pc = L->ci->savedpc;
  338. cl = &clvalue(L->ci->func)->l;
  339. base = L->base;
  340. k = cl->p->k;
  341. /* main loop of interpreter */
  342. for (;;) {
  343. const Instruction i = *pc++;
  344. StkId ra;
  345. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  346. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  347. traceexec(L, pc); /***/
  348. if (L->status == LUA_YIELD) { /* did hook yield? */
  349. L->ci->savedpc = pc - 1;
  350. return NULL;
  351. }
  352. base = L->base;
  353. }
  354. /* warning!! several calls may realloc the stack and invalidate `ra' */
  355. ra = RA(i);
  356. lua_assert(base == L->ci->base && base == L->base);
  357. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  358. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  359. switch (GET_OPCODE(i)) {
  360. case OP_MOVE: {
  361. setobjs2s(L, ra, RB(i));
  362. continue;
  363. }
  364. case OP_LOADK: {
  365. setobj2s(L, ra, KBx(i));
  366. continue;
  367. }
  368. case OP_LOADBOOL: {
  369. setbvalue(ra, GETARG_B(i));
  370. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  371. continue;
  372. }
  373. case OP_LOADNIL: {
  374. TValue *rb = RB(i);
  375. do {
  376. setnilvalue(rb--);
  377. } while (rb >= ra);
  378. continue;
  379. }
  380. case OP_GETUPVAL: {
  381. int b = GETARG_B(i);
  382. setobj2s(L, ra, cl->upvals[b]->v);
  383. continue;
  384. }
  385. case OP_GETGLOBAL: {
  386. TValue *rb = KBx(i);
  387. lua_assert(ttisstring(rb) && ttistable(&cl->g));
  388. base = luaV_gettable(L, &cl->g, rb, ra, pc); /***/
  389. continue;
  390. }
  391. case OP_GETTABLE: {
  392. base = luaV_gettable(L, RB(i), RKC(i), ra, pc); /***/
  393. continue;
  394. }
  395. case OP_SETGLOBAL: {
  396. lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
  397. base = luaV_settable(L, &cl->g, KBx(i), ra, pc); /***/
  398. continue;
  399. }
  400. case OP_SETUPVAL: {
  401. UpVal *uv = cl->upvals[GETARG_B(i)];
  402. setobj(L, uv->v, ra);
  403. luaC_barrier(L, uv, ra);
  404. continue;
  405. }
  406. case OP_SETTABLE: {
  407. base = luaV_settable(L, ra, RKB(i), RKC(i), pc); /***/
  408. continue;
  409. }
  410. case OP_NEWTABLE: {
  411. int b = GETARG_B(i);
  412. b = luaO_fb2int(b);
  413. sethvalue(L, ra, luaH_new(L, b, GETARG_C(i)));
  414. L->ci->savedpc = pc;
  415. luaC_checkGC(L); /***/
  416. base = L->base;
  417. continue;
  418. }
  419. case OP_SELF: {
  420. StkId rb = RB(i);
  421. setobjs2s(L, ra+1, rb);
  422. base = luaV_gettable(L, rb, RKC(i), ra, pc); /***/
  423. continue;
  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. continue;
  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. continue;
  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. continue;
  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. continue;
  464. }
  465. case OP_POW: {
  466. base = Arith(L, ra, RKB(i), RKC(i), TM_POW, pc); /***/
  467. continue;
  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->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. continue;
  483. }
  484. case OP_NOT: {
  485. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  486. setbvalue(ra, res);
  487. continue;
  488. }
  489. case OP_CONCAT: {
  490. int b = GETARG_B(i);
  491. int c = GETARG_C(i);
  492. L->ci->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. continue;
  498. }
  499. case OP_JMP: {
  500. dojump(L, pc, GETARG_sBx(i));
  501. continue;
  502. }
  503. case OP_EQ: {
  504. L->ci->savedpc = pc;
  505. if (equalobj(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  506. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  507. base = L->base;
  508. continue;
  509. }
  510. case OP_LT: {
  511. L->ci->savedpc = pc;
  512. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  513. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  514. base = L->base;
  515. continue;
  516. }
  517. case OP_LE: {
  518. L->ci->savedpc = pc;
  519. if (lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  520. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  521. base = L->base;
  522. continue;
  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(L, pc, GETARG_sBx(*pc) + 1);
  530. }
  531. continue;
  532. }
  533. case OP_CALL: { /***/
  534. int pcr;
  535. int b = GETARG_B(i);
  536. int nresults = GETARG_C(i) - 1;
  537. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  538. L->ci->savedpc = pc;
  539. pcr = luaD_precall(L, ra, nresults);
  540. if (pcr == PCRLUA) {
  541. nexeccalls++;
  542. goto callentry; /* restart luaV_execute over new Lua function */
  543. }
  544. else if (pcr == PCRC) {
  545. /* it was a C function (`precall' called it); adjust results */
  546. if (nresults >= 0) L->top = L->ci->top;
  547. base = L->base;
  548. continue;
  549. }
  550. else {
  551. lua_assert(pcr == PCRYIELD);
  552. return NULL;
  553. }
  554. }
  555. case OP_TAILCALL: { /***/
  556. int pcr;
  557. int b = GETARG_B(i);
  558. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  559. L->ci->savedpc = pc;
  560. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  561. pcr = luaD_precall(L, ra, LUA_MULTRET);
  562. if (pcr == PCRLUA) {
  563. /* tail call: put new frame in place of previous one */
  564. CallInfo *ci = L->ci - 1; /* previous frame */
  565. int aux;
  566. StkId func = ci->func;
  567. StkId pfunc = (ci+1)->func; /* previous function index */
  568. base = ci->base = ci->func + ((ci+1)->base - pfunc);
  569. L->base = base;
  570. if (L->openupval) luaF_close(L, base);
  571. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  572. setobjs2s(L, func+aux, pfunc+aux);
  573. ci->top = L->top = func+aux; /* correct top */
  574. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  575. ci->savedpc = L->ci->savedpc;
  576. ci->tailcalls++; /* one more call lost */
  577. L->ci--; /* remove new frame */
  578. goto callentry;
  579. }
  580. else if (pcr == PCRC) {
  581. /* it was a C function (`precall' called it) */
  582. base = L->base;
  583. continue;
  584. }
  585. else {
  586. lua_assert(pcr == PCRYIELD);
  587. return NULL;
  588. }
  589. }
  590. case OP_RETURN: {
  591. CallInfo *ci = L->ci - 1; /* previous function frame */
  592. int b = GETARG_B(i);
  593. if (b != 0) L->top = ra+b-1;
  594. if (L->openupval) luaF_close(L, base);
  595. L->ci->savedpc = pc;
  596. if (--nexeccalls == 0) /* was previous function running `here'? */
  597. return ra; /* no: return */
  598. else { /* yes: continue its execution */
  599. int nresults = (ci+1)->nresults;
  600. lua_assert(isLua(ci));
  601. lua_assert(GET_OPCODE(*(ci->savedpc - 1)) == OP_CALL);
  602. luaD_poscall(L, nresults, ra);
  603. if (nresults >= 0) L->top = L->ci->top;
  604. goto retentry;
  605. }
  606. }
  607. case OP_FORLOOP: {
  608. lua_Number step = nvalue(ra+2);
  609. lua_Number idx = nvalue(ra) + step; /* increment index */
  610. lua_Number limit = nvalue(ra+1);
  611. if (step > 0 ? idx <= limit : idx >= limit) {
  612. dojump(L, pc, GETARG_sBx(i)); /* jump back */
  613. setnvalue(ra, idx); /* update internal index... */
  614. setnvalue(ra+3, idx); /* ...and external index */
  615. }
  616. continue;
  617. }
  618. case OP_FORPREP: { /***/
  619. const TValue *init = ra;
  620. const TValue *plimit = ra+1;
  621. const TValue *pstep = ra+2;
  622. L->ci->savedpc = pc;
  623. if (!tonumber(init, ra))
  624. luaG_runerror(L, "`for' initial value must be a number");
  625. else if (!tonumber(plimit, ra+1))
  626. luaG_runerror(L, "`for' limit must be a number");
  627. else if (!tonumber(pstep, ra+2))
  628. luaG_runerror(L, "`for' step must be a number");
  629. setnvalue(ra, nvalue(ra) - nvalue(pstep));
  630. dojump(L, pc, GETARG_sBx(i));
  631. continue;
  632. }
  633. case OP_TFORLOOP: {
  634. StkId cb = ra + 3; /* call base */
  635. setobjs2s(L, cb+2, ra+2);
  636. setobjs2s(L, cb+1, ra+1);
  637. setobjs2s(L, cb, ra);
  638. L->top = cb+3; /* func. + 2 args (state and index) */
  639. L->ci->savedpc = pc;
  640. luaD_call(L, cb, GETARG_C(i)); /***/
  641. L->top = L->ci->top;
  642. base = L->base;
  643. cb = RA(i) + 3; /* previous call may change the stack */
  644. if (ttisnil(cb)) /* break loop? */
  645. pc++; /* skip jump (break loop) */
  646. else {
  647. setobjs2s(L, cb-1, cb); /* save control variable */
  648. dojump(L, pc, GETARG_sBx(*pc) + 1); /* jump back */
  649. }
  650. continue;
  651. }
  652. case OP_TFORPREP: { /* for compatibility only */
  653. if (ttistable(ra)) {
  654. setobjs2s(L, ra+1, ra);
  655. setobj2s(L, ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
  656. }
  657. dojump(L, pc, GETARG_sBx(i));
  658. continue;
  659. }
  660. case OP_SETLIST: {
  661. int n = GETARG_B(i);
  662. int c = GETARG_C(i);
  663. int last;
  664. Table *h;
  665. runtime_check(L, ttistable(ra));
  666. h = hvalue(ra);
  667. if (n == 0) {
  668. n = L->top - ra - 1;
  669. L->top = L->ci->top;
  670. }
  671. if (c == 0) c = cast(int, *pc++);
  672. last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1;
  673. if (last > h->sizearray) /* needs more space? */
  674. luaH_resize(L, h, last, h->lsizenode); /* pre-alloc it at once */
  675. for (; n > 0; n--) {
  676. TValue *val = ra+n;
  677. setobj2t(L, luaH_setnum(L, h, last--), val);
  678. luaC_barriert(L, h, val);
  679. }
  680. continue;
  681. }
  682. case OP_CLOSE: {
  683. luaF_close(L, ra);
  684. continue;
  685. }
  686. case OP_CLOSURE: {
  687. Proto *p;
  688. Closure *ncl;
  689. int nup, j;
  690. p = cl->p->p[GETARG_Bx(i)];
  691. nup = p->nups;
  692. ncl = luaF_newLclosure(L, nup, &cl->g);
  693. ncl->l.p = p;
  694. for (j=0; j<nup; j++, pc++) {
  695. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  696. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  697. else {
  698. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  699. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  700. }
  701. }
  702. setclvalue(L, ra, ncl);
  703. L->ci->savedpc = pc;
  704. luaC_checkGC(L); /***/
  705. base = L->base;
  706. continue;
  707. }
  708. case OP_VARARG: {
  709. int b = GETARG_B(i) - 1;
  710. int j;
  711. CallInfo *ci = L->ci;
  712. int n = ci->base - ci->func - cl->p->numparams - 1;
  713. if (b == LUA_MULTRET) {
  714. b = n;
  715. L->top = ra + n;
  716. }
  717. for (j=0; j<b && j<n; j++)
  718. setobjs2s(L, ra+j, ci->base - n + j);
  719. for (; j<b; j++)
  720. setnilvalue(ra+j);
  721. continue;
  722. }
  723. }
  724. }
  725. }