lvm.c 25 KB

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