lvm.c 24 KB

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