lvm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. ** $Id: lvm.c,v 2.45 2005/05/20 15:53:42 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 StkId 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. default: lua_assert(0); break;
  288. }
  289. }
  290. else if (!call_binTM(L, rb, rc, ra, op))
  291. luaG_aritherror(L, rb, rc);
  292. return L->base;
  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. StkId luaV_execute (lua_State *L, int nexeccalls) {
  310. LClosure *cl;
  311. StkId base;
  312. TValue *k;
  313. const Instruction *pc;
  314. callentry: /* entry point when calling new functions */
  315. if (L->hookmask & LUA_MASKCALL)
  316. luaD_callhook(L, LUA_HOOKCALL, -1);
  317. retentry: /* entry point when returning to old functions */
  318. pc = L->savedpc;
  319. cl = &clvalue(L->ci->func)->l;
  320. base = L->base;
  321. k = cl->p->k;
  322. /* main loop of interpreter */
  323. for (;;) {
  324. const Instruction i = *pc++;
  325. StkId ra;
  326. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  327. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  328. traceexec(L, pc);
  329. if (L->status == LUA_YIELD) { /* did hook yield? */
  330. L->savedpc = pc - 1;
  331. return NULL;
  332. }
  333. base = L->base;
  334. }
  335. /* warning!! several calls may realloc the stack and invalidate `ra' */
  336. ra = RA(i);
  337. lua_assert(base == L->base && L->base == L->ci->base);
  338. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  339. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  340. switch (GET_OPCODE(i)) {
  341. case OP_MOVE: {
  342. setobjs2s(L, ra, RB(i));
  343. continue;
  344. }
  345. case OP_LOADK: {
  346. setobj2s(L, ra, KBx(i));
  347. continue;
  348. }
  349. case OP_LOADBOOL: {
  350. setbvalue(ra, GETARG_B(i));
  351. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  352. continue;
  353. }
  354. case OP_LOADNIL: {
  355. TValue *rb = RB(i);
  356. do {
  357. setnilvalue(rb--);
  358. } while (rb >= ra);
  359. continue;
  360. }
  361. case OP_GETUPVAL: {
  362. int b = GETARG_B(i);
  363. setobj2s(L, ra, cl->upvals[b]->v);
  364. continue;
  365. }
  366. case OP_GETGLOBAL: {
  367. TValue g;
  368. TValue *rb = KBx(i);
  369. sethvalue(L, &g, cl->env);
  370. lua_assert(ttisstring(rb));
  371. Protect(luaV_gettable(L, &g, rb, ra));
  372. continue;
  373. }
  374. case OP_GETTABLE: {
  375. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  376. continue;
  377. }
  378. case OP_SETGLOBAL: {
  379. TValue g;
  380. sethvalue(L, &g, cl->env);
  381. lua_assert(ttisstring(KBx(i)));
  382. Protect(luaV_settable(L, &g, KBx(i), ra));
  383. continue;
  384. }
  385. case OP_SETUPVAL: {
  386. UpVal *uv = cl->upvals[GETARG_B(i)];
  387. setobj(L, uv->v, ra);
  388. luaC_barrier(L, uv, ra);
  389. continue;
  390. }
  391. case OP_SETTABLE: {
  392. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  393. continue;
  394. }
  395. case OP_NEWTABLE: {
  396. int b = GETARG_B(i);
  397. int c = GETARG_C(i);
  398. sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
  399. Protect(luaC_checkGC(L));
  400. continue;
  401. }
  402. case OP_SELF: {
  403. StkId rb = RB(i);
  404. setobjs2s(L, ra+1, rb);
  405. Protect(luaV_gettable(L, rb, RKC(i), ra));
  406. continue;
  407. }
  408. case OP_ADD: {
  409. TValue *rb = RKB(i);
  410. TValue *rc = RKC(i);
  411. if (ttisnumber(rb) && ttisnumber(rc)) {
  412. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  413. setnvalue(ra, luai_numadd(L, nb, nc));
  414. }
  415. else
  416. Protect(Arith(L, ra, rb, rc, TM_ADD));
  417. continue;
  418. }
  419. case OP_SUB: {
  420. TValue *rb = RKB(i);
  421. TValue *rc = RKC(i);
  422. if (ttisnumber(rb) && ttisnumber(rc)) {
  423. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  424. setnvalue(ra, luai_numsub(L, nb, nc));
  425. }
  426. else
  427. Protect(Arith(L, ra, rb, rc, TM_SUB));
  428. continue;
  429. }
  430. case OP_MUL: {
  431. TValue *rb = RKB(i);
  432. TValue *rc = RKC(i);
  433. if (ttisnumber(rb) && ttisnumber(rc)) {
  434. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  435. setnvalue(ra, luai_nummul(L, nb, nc));
  436. }
  437. else
  438. Protect(Arith(L, ra, rb, rc, TM_MUL));
  439. continue;
  440. }
  441. case OP_DIV: {
  442. TValue *rb = RKB(i);
  443. TValue *rc = RKC(i);
  444. if (ttisnumber(rb) && ttisnumber(rc)) {
  445. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  446. setnvalue(ra, luai_numdiv(L, nb, nc));
  447. }
  448. else
  449. Protect(Arith(L, ra, rb, rc, TM_DIV));
  450. continue;
  451. }
  452. case OP_MOD: {
  453. TValue *rb = RKB(i);
  454. TValue *rc = RKC(i);
  455. if (ttisnumber(rb) && ttisnumber(rc)) {
  456. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  457. setnvalue(ra, luai_nummod(L, nb, nc));
  458. }
  459. else
  460. Protect(Arith(L, ra, rb, rc, TM_MOD));
  461. continue;
  462. }
  463. case OP_POW: {
  464. TValue *rb = RKB(i);
  465. TValue *rc = RKC(i);
  466. if (ttisnumber(rb) && ttisnumber(rc)) {
  467. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  468. setnvalue(ra, luai_numpow(L, nb, nc));
  469. }
  470. else
  471. Protect(Arith(L, ra, rb, rc, TM_POW));
  472. continue;
  473. }
  474. case OP_UNM: {
  475. const TValue *rb = RB(i);
  476. TValue temp;
  477. if (tonumber(rb, &temp)) {
  478. lua_Number nb = nvalue(rb);
  479. setnvalue(ra, luai_numunm(L, nb));
  480. }
  481. else {
  482. rb = RB(i); /* `tonumber' erased `rb' */
  483. Protect(
  484. if (!call_binTM(L, rb, &luaO_nilobject, ra, TM_UNM))
  485. luaG_aritherror(L, rb, &luaO_nilobject);
  486. )
  487. }
  488. continue;
  489. }
  490. case OP_NOT: {
  491. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  492. setbvalue(ra, res);
  493. continue;
  494. }
  495. case OP_LEN: {
  496. const TValue *rb = RB(i);
  497. if (ttype(rb) == LUA_TTABLE) {
  498. setnvalue(ra, cast(lua_Number, luaH_getn(hvalue(rb))));
  499. }
  500. else { /* try metamethod */
  501. Protect(
  502. if (!call_binTM(L, rb, &luaO_nilobject, ra, TM_LEN))
  503. luaG_typeerror(L, rb, "get length of");
  504. )
  505. }
  506. continue;
  507. }
  508. case OP_CONCAT: {
  509. int b = GETARG_B(i);
  510. int c = GETARG_C(i);
  511. Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
  512. setobjs2s(L, RA(i), base+b);
  513. continue;
  514. }
  515. case OP_JMP: {
  516. dojump(L, pc, GETARG_sBx(i));
  517. continue;
  518. }
  519. case OP_EQ: {
  520. Protect(
  521. if (equalobj(L, RKB(i), RKC(i)) == GETARG_A(i))
  522. dojump(L, pc, GETARG_sBx(*pc));
  523. )
  524. pc++;
  525. continue;
  526. }
  527. case OP_LT: {
  528. Protect(
  529. if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
  530. dojump(L, pc, GETARG_sBx(*pc));
  531. )
  532. pc++;
  533. continue;
  534. }
  535. case OP_LE: {
  536. Protect(
  537. if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
  538. dojump(L, pc, GETARG_sBx(*pc));
  539. )
  540. pc++;
  541. continue;
  542. }
  543. case OP_TEST: {
  544. TValue *rb = RB(i);
  545. if (l_isfalse(rb) == GETARG_C(i)) pc++;
  546. else {
  547. setobjs2s(L, ra, rb);
  548. dojump(L, pc, GETARG_sBx(*pc) + 1);
  549. }
  550. continue;
  551. }
  552. case OP_CALL: {
  553. int b = GETARG_B(i);
  554. int nresults = GETARG_C(i) - 1;
  555. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  556. L->savedpc = pc;
  557. switch (luaD_precall(L, ra, nresults)) {
  558. case PCRLUA: {
  559. nexeccalls++;
  560. goto callentry; /* restart luaV_execute over new Lua function */
  561. }
  562. case PCRC: {
  563. /* it was a C function (`precall' called it); adjust results */
  564. if (nresults >= 0) L->top = L->ci->top;
  565. base = L->base;
  566. continue;
  567. }
  568. default: {
  569. return NULL;
  570. }
  571. }
  572. }
  573. case OP_TAILCALL: {
  574. int b = GETARG_B(i);
  575. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  576. L->savedpc = pc;
  577. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  578. switch (luaD_precall(L, ra, LUA_MULTRET)) {
  579. case PCRLUA: {
  580. /* tail call: put new frame in place of previous one */
  581. CallInfo *ci = L->ci - 1; /* previous frame */
  582. int aux;
  583. StkId func = ci->func;
  584. StkId pfunc = (ci+1)->func; /* previous function index */
  585. if (L->openupval) luaF_close(L, ci->base);
  586. L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
  587. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  588. setobjs2s(L, func+aux, pfunc+aux);
  589. ci->top = L->top = func+aux; /* correct top */
  590. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  591. ci->savedpc = L->savedpc;
  592. ci->tailcalls++; /* one more call lost */
  593. L->ci--; /* remove new frame */
  594. goto callentry;
  595. }
  596. case PCRC: {
  597. /* it was a C function (`precall' called it) */
  598. base = L->base;
  599. continue;
  600. }
  601. default: {
  602. return NULL;
  603. }
  604. }
  605. }
  606. case OP_RETURN: {
  607. int b = GETARG_B(i);
  608. if (b != 0) L->top = ra+b-1;
  609. if (L->openupval) luaF_close(L, base);
  610. L->savedpc = pc;
  611. if (--nexeccalls == 0) /* was previous function running `here'? */
  612. return ra; /* no: return */
  613. else { /* yes: continue its execution */
  614. int nresults = L->ci->nresults;
  615. lua_assert(isLua(L->ci - 1));
  616. lua_assert(GET_OPCODE(*((L->ci - 1)->savedpc - 1)) == OP_CALL);
  617. luaD_poscall(L, nresults, ra);
  618. if (nresults >= 0) L->top = L->ci->top;
  619. goto retentry;
  620. }
  621. }
  622. case OP_FORLOOP: {
  623. lua_Number step = nvalue(ra+2);
  624. lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
  625. lua_Number limit = nvalue(ra+1);
  626. if (step > 0 ? luai_numle(L, idx, limit) : luai_numle(L, limit, idx)) {
  627. dojump(L, pc, GETARG_sBx(i)); /* jump back */
  628. setnvalue(ra, idx); /* update internal index... */
  629. setnvalue(ra+3, idx); /* ...and external index */
  630. }
  631. continue;
  632. }
  633. case OP_FORPREP: {
  634. const TValue *init = ra;
  635. const TValue *plimit = ra+1;
  636. const TValue *pstep = ra+2;
  637. L->savedpc = pc; /* next steps may throw errors */
  638. if (!tonumber(init, ra))
  639. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  640. else if (!tonumber(plimit, ra+1))
  641. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  642. else if (!tonumber(pstep, ra+2))
  643. luaG_runerror(L, LUA_QL("for") " step must be a number");
  644. setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
  645. dojump(L, pc, GETARG_sBx(i));
  646. continue;
  647. }
  648. case OP_TFORLOOP: {
  649. StkId cb = ra + 3; /* call base */
  650. setobjs2s(L, cb+2, ra+2);
  651. setobjs2s(L, cb+1, ra+1);
  652. setobjs2s(L, cb, ra);
  653. L->top = cb+3; /* func. + 2 args (state and index) */
  654. Protect(luaD_call(L, cb, GETARG_C(i)));
  655. L->top = L->ci->top;
  656. cb = RA(i) + 3; /* previous call may change the stack */
  657. if (ttisnil(cb)) /* break loop? */
  658. pc++; /* skip jump (break loop) */
  659. else {
  660. setobjs2s(L, cb-1, cb); /* save control variable */
  661. dojump(L, pc, GETARG_sBx(*pc) + 1); /* jump back */
  662. }
  663. continue;
  664. }
  665. case OP_SETLIST: {
  666. int n = GETARG_B(i);
  667. int c = GETARG_C(i);
  668. int last;
  669. Table *h;
  670. if (n == 0) {
  671. n = cast(int, L->top - ra) - 1;
  672. L->top = L->ci->top;
  673. }
  674. if (c == 0) c = cast(int, *pc++);
  675. runtime_check(L, ttistable(ra));
  676. h = hvalue(ra);
  677. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  678. if (last > h->sizearray) /* needs more space? */
  679. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  680. for (; n > 0; n--) {
  681. TValue *val = ra+n;
  682. setobj2t(L, luaH_setnum(L, h, last--), val);
  683. luaC_barriert(L, h, val);
  684. }
  685. continue;
  686. }
  687. case OP_CLOSE: {
  688. luaF_close(L, ra);
  689. continue;
  690. }
  691. case OP_CLOSURE: {
  692. Proto *p;
  693. Closure *ncl;
  694. int nup, j;
  695. p = cl->p->p[GETARG_Bx(i)];
  696. nup = p->nups;
  697. ncl = luaF_newLclosure(L, nup, cl->env);
  698. ncl->l.p = p;
  699. for (j=0; j<nup; j++, pc++) {
  700. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  701. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  702. else {
  703. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  704. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  705. }
  706. }
  707. setclvalue(L, ra, ncl);
  708. Protect(luaC_checkGC(L));
  709. continue;
  710. }
  711. case OP_VARARG: {
  712. int b = GETARG_B(i) - 1;
  713. int j;
  714. CallInfo *ci = L->ci;
  715. int n = cast(int, ci->base - ci->func) - cl->p->numparams - 1;
  716. if (b == LUA_MULTRET) {
  717. b = n;
  718. L->top = ra + n;
  719. }
  720. for (j=0; j<b && j<n; j++)
  721. setobjs2s(L, ra+j, ci->base - n + j);
  722. for (; j<b; j++)
  723. setnilvalue(ra+j);
  724. continue;
  725. }
  726. }
  727. }
  728. }