lvm.c 24 KB

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