lvm.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. ** $Id: lvm.c,v 2.182 2013/12/18 14:12:03 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <limits.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. /* maximum length of the conversion of a number to a string */
  27. #define MAXNUMBER2STR 50
  28. int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
  29. lua_assert(!ttisfloat(obj));
  30. if (ttisinteger(obj)) {
  31. *n = cast_num(ivalue(obj));
  32. return 1;
  33. }
  34. else
  35. return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n));
  36. }
  37. int luaV_tostring (lua_State *L, StkId obj) {
  38. if (!ttisnumber(obj))
  39. return 0;
  40. else {
  41. char buff[MAXNUMBER2STR];
  42. size_t len;
  43. if (ttisinteger(obj))
  44. len = lua_integer2str(buff, ivalue(obj));
  45. else {
  46. len = lua_number2str(buff, fltvalue(obj));
  47. if (strspn(buff, "-0123456789") == len) { /* look like an integer? */
  48. buff[len++] = '.'; /* add a '.0' */
  49. buff[len++] = '0';
  50. buff[len] = '\0';
  51. }
  52. }
  53. setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
  54. return 1;
  55. }
  56. }
  57. /*
  58. ** Check whether a float number is within the range of a lua_Integer.
  59. ** (The comparisons are tricky because of rounding, which can or
  60. ** not occur depending on the relative sizes of floats and integers.)
  61. ** This function is called only when 'n' has an integer value.
  62. */
  63. int luaV_numtointeger (lua_Number n, lua_Integer *p) {
  64. if (cast_num(MIN_INTEGER) <= n && n < (MAX_INTEGER + cast_num(1))) {
  65. *p = cast_integer(n);
  66. lua_assert(cast_num(*p) == n);
  67. return 1;
  68. }
  69. return 0; /* number is outside integer limits */
  70. }
  71. /*
  72. ** try to convert a non-integer value to an integer
  73. */
  74. int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
  75. lua_Number n;
  76. lua_assert(!ttisinteger(obj));
  77. if (tonumber(obj, &n)) {
  78. n = l_floor(n);
  79. return luaV_numtointeger(n, p);
  80. }
  81. else return 0;
  82. }
  83. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  84. int loop;
  85. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  86. const TValue *tm;
  87. if (ttistable(t)) { /* `t' is a table? */
  88. Table *h = hvalue(t);
  89. const TValue *res = luaH_get(h, key); /* do a primitive get */
  90. if (!ttisnil(res) || /* result is not nil? */
  91. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  92. setobj2s(L, val, res);
  93. return;
  94. }
  95. /* else will try the tag method */
  96. }
  97. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  98. luaG_typeerror(L, t, "index");
  99. if (ttisfunction(tm)) {
  100. luaT_callTM(L, tm, t, key, val, 1);
  101. return;
  102. }
  103. t = tm; /* else repeat with 'tm' */
  104. }
  105. luaG_runerror(L, "loop in gettable");
  106. }
  107. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  108. int loop;
  109. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  110. const TValue *tm;
  111. if (ttistable(t)) { /* `t' is a table? */
  112. Table *h = hvalue(t);
  113. TValue *oldval = cast(TValue *, luaH_get(h, key));
  114. /* if previous value is not nil, there must be a previous entry
  115. in the table; moreover, a metamethod has no relevance */
  116. if (!ttisnil(oldval) ||
  117. /* previous value is nil; must check the metamethod */
  118. ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
  119. /* no metamethod; is there a previous entry in the table? */
  120. (oldval != luaO_nilobject ||
  121. /* no previous entry; must create one. (The next test is
  122. always true; we only need the assignment.) */
  123. (oldval = luaH_newkey(L, h, key), 1)))) {
  124. /* no metamethod and (now) there is an entry with given key */
  125. setobj2t(L, oldval, val); /* assign new value to that entry */
  126. invalidateTMcache(h);
  127. luaC_barrierback(L, h, val);
  128. return;
  129. }
  130. /* else will try the metamethod */
  131. }
  132. else /* not a table; check metamethod */
  133. if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  134. luaG_typeerror(L, t, "index");
  135. /* there is a metamethod */
  136. if (ttisfunction(tm)) {
  137. luaT_callTM(L, tm, t, key, val, 0);
  138. return;
  139. }
  140. t = tm; /* else repeat with 'tm' */
  141. }
  142. luaG_runerror(L, "loop in settable");
  143. }
  144. static int l_strcmp (const TString *ls, const TString *rs) {
  145. const char *l = getstr(ls);
  146. size_t ll = ls->tsv.len;
  147. const char *r = getstr(rs);
  148. size_t lr = rs->tsv.len;
  149. for (;;) {
  150. int temp = strcoll(l, r);
  151. if (temp != 0) return temp;
  152. else { /* strings are equal up to a `\0' */
  153. size_t len = strlen(l); /* index of first `\0' in both strings */
  154. if (len == lr) /* r is finished? */
  155. return (len == ll) ? 0 : 1;
  156. else if (len == ll) /* l is finished? */
  157. return -1; /* l is smaller than r (because r is not finished) */
  158. /* both strings longer than `len'; go on comparing (after the `\0') */
  159. len++;
  160. l += len; ll -= len; r += len; lr -= len;
  161. }
  162. }
  163. }
  164. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  165. int res;
  166. lua_Number nl, nr;
  167. if (ttisinteger(l) && ttisinteger(r))
  168. return (ivalue(l) < ivalue(r));
  169. else if (tonumber(l, &nl) && tonumber(r, &nr))
  170. return luai_numlt(L, nl, nr);
  171. else if (ttisstring(l) && ttisstring(r))
  172. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  173. else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)
  174. luaG_ordererror(L, l, r);
  175. return res;
  176. }
  177. int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  178. int res;
  179. lua_Number nl, nr;
  180. if (ttisinteger(l) && ttisinteger(r))
  181. return (ivalue(l) <= ivalue(r));
  182. else if (tonumber(l, &nl) && tonumber(r, &nr))
  183. return luai_numle(L, nl, nr);
  184. else if (ttisstring(l) && ttisstring(r))
  185. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  186. else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
  187. return res;
  188. else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */
  189. luaG_ordererror(L, l, r);
  190. return !res;
  191. }
  192. /*
  193. ** equality of Lua values. L == NULL means raw equality (no metamethods)
  194. */
  195. int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
  196. const TValue *tm;
  197. if (ttype(t1) != ttype(t2)) {
  198. if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
  199. return 0; /* only numbers can be equal with different variants */
  200. else { /* two numbers with different variants */
  201. lua_Number n1, n2;
  202. lua_assert(ttisnumber(t1) && ttisnumber(t2));
  203. (void)tonumber(t1, &n1); (void)tonumber(t2, &n2);
  204. return luai_numeq(n1, n2);
  205. }
  206. }
  207. /* values have same type and same variant */
  208. switch (ttype(t1)) {
  209. case LUA_TNIL: return 1;
  210. case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2));
  211. case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
  212. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  213. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  214. case LUA_TLCF: return fvalue(t1) == fvalue(t2);
  215. case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2));
  216. case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2));
  217. case LUA_TUSERDATA: {
  218. if (uvalue(t1) == uvalue(t2)) return 1;
  219. else if (L == NULL) return 0;
  220. tm = luaT_getequalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable);
  221. break; /* will try TM */
  222. }
  223. case LUA_TTABLE: {
  224. if (hvalue(t1) == hvalue(t2)) return 1;
  225. else if (L == NULL) return 0;
  226. tm = luaT_getequalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable);
  227. break; /* will try TM */
  228. }
  229. default:
  230. return gcvalue(t1) == gcvalue(t2);
  231. }
  232. if (tm == NULL) return 0; /* no TM? */
  233. luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */
  234. return !l_isfalse(L->top);
  235. }
  236. void luaV_concat (lua_State *L, int total) {
  237. lua_assert(total >= 2);
  238. do {
  239. StkId top = L->top;
  240. int n = 2; /* number of elements handled in this pass (at least 2) */
  241. if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1))
  242. luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
  243. else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
  244. (void)tostring(L, top - 2); /* result is first operand */
  245. else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
  246. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  247. }
  248. else {
  249. /* at least two non-empty string values; get as many as possible */
  250. size_t tl = tsvalue(top-1)->len;
  251. char *buffer;
  252. int i;
  253. /* collect total length */
  254. for (i = 1; i < total && tostring(L, top-i-1); i++) {
  255. size_t l = tsvalue(top-i-1)->len;
  256. if (l >= (MAX_SIZE/sizeof(char)) - tl)
  257. luaG_runerror(L, "string length overflow");
  258. tl += l;
  259. }
  260. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  261. tl = 0;
  262. n = i;
  263. do { /* concat all strings */
  264. size_t l = tsvalue(top-i)->len;
  265. memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
  266. tl += l;
  267. } while (--i > 0);
  268. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  269. }
  270. total -= n-1; /* got 'n' strings to create 1 new */
  271. L->top -= n-1; /* popped 'n' strings and pushed one */
  272. } while (total > 1); /* repeat until only 1 result left */
  273. }
  274. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  275. const TValue *tm;
  276. switch (ttnov(rb)) {
  277. case LUA_TTABLE: {
  278. Table *h = hvalue(rb);
  279. tm = fasttm(L, h->metatable, TM_LEN);
  280. if (tm) break; /* metamethod? break switch to call it */
  281. setivalue(ra, luaH_getn(h)); /* else primitive len */
  282. return;
  283. }
  284. case LUA_TSTRING: {
  285. setivalue(ra, tsvalue(rb)->len);
  286. return;
  287. }
  288. default: { /* try metamethod */
  289. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  290. if (ttisnil(tm)) /* no metamethod? */
  291. luaG_typeerror(L, rb, "get length of");
  292. break;
  293. }
  294. }
  295. luaT_callTM(L, tm, rb, rb, ra, 1);
  296. }
  297. lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) {
  298. if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */
  299. if (y == 0)
  300. luaG_runerror(L, "attempt to divide by zero");
  301. return intop(-, 0, x); /* y==-1; avoid overflow with 0x80000...//-1 */
  302. }
  303. else {
  304. lua_Integer d = x / y; /* perform division */
  305. if ((x ^ y) >= 0 || x % y == 0) /* same signal or no rest? */
  306. return d;
  307. else
  308. return d - 1; /* correct 'div' for negative case */
  309. }
  310. }
  311. lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) {
  312. if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */
  313. if (y == 0)
  314. luaG_runerror(L, "attempt to perform 'n%%0'");
  315. return 0; /* y==-1; avoid overflow with 0x80000...%-1 */
  316. }
  317. else {
  318. lua_Integer r = x % y;
  319. if (r == 0 || (x ^ y) >= 0)
  320. return r;
  321. else
  322. return r + y; /* correct 'mod' for negative case */
  323. }
  324. }
  325. lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y) {
  326. if (y <= 0) { /* special cases: 0 or negative exponent */
  327. if (y < 0)
  328. luaG_runerror(L, "integer exponentiation with negative exponent");
  329. return 1; /* x^0 == 1 */
  330. }
  331. else {
  332. lua_Integer r = 1;
  333. for (; y > 1; y >>= 1) {
  334. if (y & 1) r = intop(*, r, x);
  335. x = intop(*, x, x);
  336. }
  337. r = intop(*, r, x);
  338. return r;
  339. }
  340. }
  341. /* number of bits in an integer */
  342. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  343. LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  344. if (y < 0) { /* shift right? */
  345. if (y <= -NBITS) return 0;
  346. else return cast_integer(cast_unsigned(x) >> (-y));
  347. }
  348. else { /* shift left */
  349. if (y >= NBITS) return 0;
  350. else return x << y;
  351. }
  352. }
  353. /*
  354. ** check whether cached closure in prototype 'p' may be reused, that is,
  355. ** whether there is a cached closure with the same upvalues needed by
  356. ** new closure to be created.
  357. */
  358. static Closure *getcached (Proto *p, UpVal **encup, StkId base) {
  359. Closure *c = p->cache;
  360. if (c != NULL) { /* is there a cached closure? */
  361. int nup = p->sizeupvalues;
  362. Upvaldesc *uv = p->upvalues;
  363. int i;
  364. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  365. TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
  366. if (c->l.upvals[i]->v != v)
  367. return NULL; /* wrong upvalue; cannot reuse closure */
  368. }
  369. }
  370. return c; /* return cached closure (or NULL if no cached closure) */
  371. }
  372. /*
  373. ** create a new Lua closure, push it in the stack, and initialize
  374. ** its upvalues. Note that the closure is not cached if prototype is
  375. ** already black (which means that 'cache' was already cleared by the
  376. ** GC).
  377. */
  378. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  379. StkId ra) {
  380. int nup = p->sizeupvalues;
  381. Upvaldesc *uv = p->upvalues;
  382. int i;
  383. Closure *ncl = luaF_newLclosure(L, nup);
  384. ncl->l.p = p;
  385. setclLvalue(L, ra, ncl); /* anchor new closure in stack */
  386. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  387. if (uv[i].instack) /* upvalue refers to local variable? */
  388. ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
  389. else /* get upvalue from enclosing function */
  390. ncl->l.upvals[i] = encup[uv[i].idx];
  391. ncl->l.upvals[i]->refcount++;
  392. /* new closure is white, so we do not need a barrier here */
  393. }
  394. if (!isblack(obj2gco(p))) /* cache will not break GC invariant? */
  395. p->cache = ncl; /* save it on cache for reuse */
  396. }
  397. /*
  398. ** finish execution of an opcode interrupted by an yield
  399. */
  400. void luaV_finishOp (lua_State *L) {
  401. CallInfo *ci = L->ci;
  402. StkId base = ci->u.l.base;
  403. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  404. OpCode op = GET_OPCODE(inst);
  405. switch (op) { /* finish its execution */
  406. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV:
  407. case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
  408. case OP_MOD: case OP_POW:
  409. case OP_UNM: case OP_BNOT: case OP_LEN:
  410. case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
  411. setobjs2s(L, base + GETARG_A(inst), --L->top);
  412. break;
  413. }
  414. case OP_LE: case OP_LT: case OP_EQ: {
  415. int res = !l_isfalse(L->top - 1);
  416. L->top--;
  417. /* metamethod should not be called when operand is K */
  418. lua_assert(!ISK(GETARG_B(inst)));
  419. if (op == OP_LE && /* "<=" using "<" instead? */
  420. ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
  421. res = !res; /* invert result */
  422. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  423. if (res != GETARG_A(inst)) /* condition failed? */
  424. ci->u.l.savedpc++; /* skip jump instruction */
  425. break;
  426. }
  427. case OP_CONCAT: {
  428. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  429. int b = GETARG_B(inst); /* first element to concatenate */
  430. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  431. setobj2s(L, top - 2, top); /* put TM result in proper position */
  432. if (total > 1) { /* are there elements to concat? */
  433. L->top = top - 1; /* top is one after last element (at top-2) */
  434. luaV_concat(L, total); /* concat them (may yield again) */
  435. }
  436. /* move final result to final position */
  437. setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
  438. L->top = ci->top; /* restore top */
  439. break;
  440. }
  441. case OP_TFORCALL: {
  442. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  443. L->top = ci->top; /* correct top */
  444. break;
  445. }
  446. case OP_CALL: {
  447. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  448. L->top = ci->top; /* adjust results */
  449. break;
  450. }
  451. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  452. break;
  453. default: lua_assert(0);
  454. }
  455. }
  456. /*
  457. ** some macros for common tasks in `luaV_execute'
  458. */
  459. #if !defined luai_runtimecheck
  460. #define luai_runtimecheck(L, c) /* void */
  461. #endif
  462. #define RA(i) (base+GETARG_A(i))
  463. /* to be used after possible stack reallocation */
  464. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  465. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  466. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  467. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  468. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  469. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  470. #define KBx(i) \
  471. (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
  472. /* execute a jump instruction */
  473. #define dojump(ci,i,e) \
  474. { int a = GETARG_A(i); \
  475. if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
  476. ci->u.l.savedpc += GETARG_sBx(i) + e; }
  477. /* for test instructions, execute the jump instruction that follows it */
  478. #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
  479. #define Protect(x) { {x;}; base = ci->u.l.base; }
  480. #define checkGC(L,c) \
  481. Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
  482. luaC_step(L); \
  483. L->top = ci->top;}) /* restore top */ \
  484. luai_threadyield(L); )
  485. #define vmdispatch(o) switch(o)
  486. #define vmcase(l,b) case l: {b} break;
  487. #define vmcasenb(l,b) case l: {b} /* nb = no break */
  488. void luaV_execute (lua_State *L) {
  489. CallInfo *ci = L->ci;
  490. LClosure *cl;
  491. TValue *k;
  492. StkId base;
  493. newframe: /* reentry point when frame changes (call/return) */
  494. lua_assert(ci == L->ci);
  495. cl = clLvalue(ci->func);
  496. k = cl->p->k;
  497. base = ci->u.l.base;
  498. /* main loop of interpreter */
  499. for (;;) {
  500. Instruction i = *(ci->u.l.savedpc++);
  501. StkId ra;
  502. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  503. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  504. Protect(luaG_traceexec(L));
  505. }
  506. /* WARNING: several calls may realloc the stack and invalidate `ra' */
  507. ra = RA(i);
  508. lua_assert(base == ci->u.l.base);
  509. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  510. vmdispatch (GET_OPCODE(i)) {
  511. vmcase(OP_MOVE,
  512. setobjs2s(L, ra, RB(i));
  513. )
  514. vmcase(OP_LOADK,
  515. TValue *rb = k + GETARG_Bx(i);
  516. setobj2s(L, ra, rb);
  517. )
  518. vmcase(OP_LOADKX,
  519. TValue *rb;
  520. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  521. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  522. setobj2s(L, ra, rb);
  523. )
  524. vmcase(OP_LOADBOOL,
  525. setbvalue(ra, GETARG_B(i));
  526. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  527. )
  528. vmcase(OP_LOADNIL,
  529. int b = GETARG_B(i);
  530. do {
  531. setnilvalue(ra++);
  532. } while (b--);
  533. )
  534. vmcase(OP_GETUPVAL,
  535. int b = GETARG_B(i);
  536. setobj2s(L, ra, cl->upvals[b]->v);
  537. )
  538. vmcase(OP_GETTABUP,
  539. int b = GETARG_B(i);
  540. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  541. )
  542. vmcase(OP_GETTABLE,
  543. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  544. )
  545. vmcase(OP_SETTABUP,
  546. int a = GETARG_A(i);
  547. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  548. )
  549. vmcase(OP_SETUPVAL,
  550. UpVal *uv = cl->upvals[GETARG_B(i)];
  551. setobj(L, uv->v, ra);
  552. luaC_upvalbarrier(L, uv);
  553. )
  554. vmcase(OP_SETTABLE,
  555. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  556. )
  557. vmcase(OP_NEWTABLE,
  558. int b = GETARG_B(i);
  559. int c = GETARG_C(i);
  560. Table *t = luaH_new(L);
  561. sethvalue(L, ra, t);
  562. if (b != 0 || c != 0)
  563. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  564. checkGC(L, ra + 1);
  565. )
  566. vmcase(OP_SELF,
  567. StkId rb = RB(i);
  568. setobjs2s(L, ra+1, rb);
  569. Protect(luaV_gettable(L, rb, RKC(i), ra));
  570. )
  571. vmcase(OP_ADD,
  572. TValue *rb = RKB(i);
  573. TValue *rc = RKC(i);
  574. lua_Number nb; lua_Number nc;
  575. if (ttisinteger(rb) && ttisinteger(rc)) {
  576. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  577. setivalue(ra, intop(+, ib, ic));
  578. }
  579. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  580. setnvalue(ra, luai_numadd(L, nb, nc));
  581. }
  582. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  583. )
  584. vmcase(OP_SUB,
  585. TValue *rb = RKB(i);
  586. TValue *rc = RKC(i);
  587. lua_Number nb; lua_Number nc;
  588. if (ttisinteger(rb) && ttisinteger(rc)) {
  589. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  590. setivalue(ra, intop(-, ib, ic));
  591. }
  592. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  593. setnvalue(ra, luai_numsub(L, nb, nc));
  594. }
  595. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  596. )
  597. vmcase(OP_MUL,
  598. TValue *rb = RKB(i);
  599. TValue *rc = RKC(i);
  600. lua_Number nb; lua_Number nc;
  601. if (ttisinteger(rb) && ttisinteger(rc)) {
  602. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  603. setivalue(ra, intop(*, ib, ic));
  604. }
  605. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  606. setnvalue(ra, luai_nummul(L, nb, nc));
  607. }
  608. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  609. )
  610. vmcase(OP_DIV, /* float division (always with floats) */
  611. TValue *rb = RKB(i);
  612. TValue *rc = RKC(i);
  613. lua_Number nb; lua_Number nc;
  614. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  615. setnvalue(ra, luai_numdiv(L, nb, nc));
  616. }
  617. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  618. )
  619. vmcase(OP_IDIV, /* integer division */
  620. TValue *rb = RKB(i);
  621. TValue *rc = RKC(i);
  622. lua_Integer ib; lua_Integer ic;
  623. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  624. setivalue(ra, luaV_div(L, ib, ic));
  625. }
  626. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  627. )
  628. vmcase(OP_BAND,
  629. TValue *rb = RKB(i);
  630. TValue *rc = RKC(i);
  631. lua_Integer ib; lua_Integer ic;
  632. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  633. setivalue(ra, intop(&, ib, ic));
  634. }
  635. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  636. )
  637. vmcase(OP_BOR,
  638. TValue *rb = RKB(i);
  639. TValue *rc = RKC(i);
  640. lua_Integer ib; lua_Integer ic;
  641. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  642. setivalue(ra, intop(|, ib, ic));
  643. }
  644. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  645. )
  646. vmcase(OP_BXOR,
  647. TValue *rb = RKB(i);
  648. TValue *rc = RKC(i);
  649. lua_Integer ib; lua_Integer ic;
  650. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  651. setivalue(ra, intop(^, ib, ic));
  652. }
  653. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  654. )
  655. vmcase(OP_SHL,
  656. TValue *rb = RKB(i);
  657. TValue *rc = RKC(i);
  658. lua_Integer ib; lua_Integer ic;
  659. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  660. setivalue(ra, luaV_shiftl(ib, ic));
  661. }
  662. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  663. )
  664. vmcase(OP_SHR,
  665. TValue *rb = RKB(i);
  666. TValue *rc = RKC(i);
  667. lua_Integer ib; lua_Integer ic;
  668. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  669. setivalue(ra, luaV_shiftl(ib, -ic));
  670. }
  671. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  672. )
  673. vmcase(OP_MOD,
  674. TValue *rb = RKB(i);
  675. TValue *rc = RKC(i);
  676. lua_Number nb; lua_Number nc;
  677. if (ttisinteger(rb) && ttisinteger(rc)) {
  678. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  679. setivalue(ra, luaV_mod(L, ib, ic));
  680. }
  681. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  682. setnvalue(ra, luai_nummod(L, nb, nc));
  683. }
  684. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  685. )
  686. vmcase(OP_POW,
  687. TValue *rb = RKB(i);
  688. TValue *rc = RKC(i);
  689. lua_Number nb; lua_Number nc;
  690. if (ttisinteger(rb) && ttisinteger(rc)) {
  691. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  692. setivalue(ra, luaV_pow(L, ib, ic));
  693. }
  694. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  695. setnvalue(ra, luai_numpow(L, nb, nc));
  696. }
  697. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  698. )
  699. vmcase(OP_UNM,
  700. TValue *rb = RB(i);
  701. lua_Number nb;
  702. if (ttisinteger(rb)) {
  703. lua_Integer ib = ivalue(rb);
  704. setivalue(ra, intop(-, 0, ib));
  705. }
  706. else if (tonumber(rb, &nb)) {
  707. setnvalue(ra, luai_numunm(L, nb));
  708. }
  709. else {
  710. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  711. }
  712. )
  713. vmcase(OP_BNOT,
  714. TValue *rb = RB(i);
  715. lua_Integer ib;
  716. if (tointeger(rb, &ib)) {
  717. setivalue(ra, intop(^, cast_integer(-1), ib));
  718. }
  719. else {
  720. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  721. }
  722. )
  723. vmcase(OP_NOT,
  724. TValue *rb = RB(i);
  725. int res = l_isfalse(rb); /* next assignment may change this value */
  726. setbvalue(ra, res);
  727. )
  728. vmcase(OP_LEN,
  729. Protect(luaV_objlen(L, ra, RB(i)));
  730. )
  731. vmcase(OP_CONCAT,
  732. int b = GETARG_B(i);
  733. int c = GETARG_C(i);
  734. StkId rb;
  735. L->top = base + c + 1; /* mark the end of concat operands */
  736. Protect(luaV_concat(L, c - b + 1));
  737. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  738. rb = b + base;
  739. setobjs2s(L, ra, rb);
  740. checkGC(L, (ra >= rb ? ra + 1 : rb));
  741. L->top = ci->top; /* restore top */
  742. )
  743. vmcase(OP_JMP,
  744. dojump(ci, i, 0);
  745. )
  746. vmcase(OP_EQ,
  747. TValue *rb = RKB(i);
  748. TValue *rc = RKC(i);
  749. Protect(
  750. if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
  751. ci->u.l.savedpc++;
  752. else
  753. donextjump(ci);
  754. )
  755. )
  756. vmcase(OP_LT,
  757. Protect(
  758. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  759. ci->u.l.savedpc++;
  760. else
  761. donextjump(ci);
  762. )
  763. )
  764. vmcase(OP_LE,
  765. Protect(
  766. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  767. ci->u.l.savedpc++;
  768. else
  769. donextjump(ci);
  770. )
  771. )
  772. vmcase(OP_TEST,
  773. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  774. ci->u.l.savedpc++;
  775. else
  776. donextjump(ci);
  777. )
  778. vmcase(OP_TESTSET,
  779. TValue *rb = RB(i);
  780. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  781. ci->u.l.savedpc++;
  782. else {
  783. setobjs2s(L, ra, rb);
  784. donextjump(ci);
  785. }
  786. )
  787. vmcase(OP_CALL,
  788. int b = GETARG_B(i);
  789. int nresults = GETARG_C(i) - 1;
  790. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  791. if (luaD_precall(L, ra, nresults)) { /* C function? */
  792. if (nresults >= 0) L->top = ci->top; /* adjust results */
  793. base = ci->u.l.base;
  794. }
  795. else { /* Lua function */
  796. ci = L->ci;
  797. ci->callstatus |= CIST_REENTRY;
  798. goto newframe; /* restart luaV_execute over new Lua function */
  799. }
  800. )
  801. vmcase(OP_TAILCALL,
  802. int b = GETARG_B(i);
  803. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  804. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  805. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  806. base = ci->u.l.base;
  807. else {
  808. /* tail call: put called frame (n) in place of caller one (o) */
  809. CallInfo *nci = L->ci; /* called frame */
  810. CallInfo *oci = nci->previous; /* caller frame */
  811. StkId nfunc = nci->func; /* called function */
  812. StkId ofunc = oci->func; /* caller function */
  813. /* last stack slot filled by 'precall' */
  814. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  815. int aux;
  816. /* close all upvalues from previous call */
  817. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  818. /* move new frame into old one */
  819. for (aux = 0; nfunc + aux < lim; aux++)
  820. setobjs2s(L, ofunc + aux, nfunc + aux);
  821. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  822. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  823. oci->u.l.savedpc = nci->u.l.savedpc;
  824. oci->callstatus |= CIST_TAIL; /* function was tail called */
  825. ci = L->ci = oci; /* remove new frame */
  826. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  827. goto newframe; /* restart luaV_execute over new Lua function */
  828. }
  829. )
  830. vmcasenb(OP_RETURN,
  831. int b = GETARG_B(i);
  832. if (b != 0) L->top = ra+b-1;
  833. if (cl->p->sizep > 0) luaF_close(L, base);
  834. b = luaD_poscall(L, ra);
  835. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  836. return; /* external invocation: return */
  837. else { /* invocation via reentry: continue execution */
  838. ci = L->ci;
  839. if (b) L->top = ci->top;
  840. lua_assert(isLua(ci));
  841. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  842. goto newframe; /* restart luaV_execute over new Lua function */
  843. }
  844. )
  845. vmcase(OP_FORLOOP,
  846. if (ttisinteger(ra)) { /* integer count? */
  847. lua_Integer step = ivalue(ra + 2);
  848. lua_Integer idx = ivalue(ra) + step; /* increment index */
  849. lua_Integer limit = ivalue(ra + 1);
  850. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  851. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  852. setivalue(ra, idx); /* update internal index... */
  853. setivalue(ra + 3, idx); /* ...and external index */
  854. }
  855. }
  856. else { /* floating count */
  857. lua_Number step = fltvalue(ra + 2);
  858. lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
  859. lua_Number limit = fltvalue(ra + 1);
  860. if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
  861. : luai_numle(L, limit, idx)) {
  862. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  863. setnvalue(ra, idx); /* update internal index... */
  864. setnvalue(ra + 3, idx); /* ...and external index */
  865. }
  866. }
  867. )
  868. vmcase(OP_FORPREP,
  869. TValue *init = ra;
  870. TValue *plimit = ra + 1;
  871. TValue *pstep = ra + 2;
  872. if (ttisinteger(ra) && ttisinteger(ra + 1) && ttisinteger(ra + 2)) {
  873. setivalue(ra, ivalue(ra) - ivalue(pstep));
  874. }
  875. else { /* try with floats */
  876. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  877. if (!tonumber(plimit, &nlimit))
  878. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  879. setnvalue(plimit, nlimit);
  880. if (!tonumber(pstep, &nstep))
  881. luaG_runerror(L, LUA_QL("for") " step must be a number");
  882. setnvalue(pstep, nstep);
  883. if (!tonumber(init, &ninit))
  884. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  885. setnvalue(ra, luai_numsub(L, ninit, nstep));
  886. }
  887. ci->u.l.savedpc += GETARG_sBx(i);
  888. )
  889. vmcasenb(OP_TFORCALL,
  890. StkId cb = ra + 3; /* call base */
  891. setobjs2s(L, cb+2, ra+2);
  892. setobjs2s(L, cb+1, ra+1);
  893. setobjs2s(L, cb, ra);
  894. L->top = cb + 3; /* func. + 2 args (state and index) */
  895. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  896. L->top = ci->top;
  897. i = *(ci->u.l.savedpc++); /* go to next instruction */
  898. ra = RA(i);
  899. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  900. goto l_tforloop;
  901. )
  902. vmcase(OP_TFORLOOP,
  903. l_tforloop:
  904. if (!ttisnil(ra + 1)) { /* continue loop? */
  905. setobjs2s(L, ra, ra + 1); /* save control variable */
  906. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  907. }
  908. )
  909. vmcase(OP_SETLIST,
  910. int n = GETARG_B(i);
  911. int c = GETARG_C(i);
  912. int last;
  913. Table *h;
  914. if (n == 0) n = cast_int(L->top - ra) - 1;
  915. if (c == 0) {
  916. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  917. c = GETARG_Ax(*ci->u.l.savedpc++);
  918. }
  919. luai_runtimecheck(L, ttistable(ra));
  920. h = hvalue(ra);
  921. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  922. if (last > h->sizearray) /* needs more space? */
  923. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  924. for (; n > 0; n--) {
  925. TValue *val = ra+n;
  926. luaH_setint(L, h, last--, val);
  927. luaC_barrierback(L, h, val);
  928. }
  929. L->top = ci->top; /* correct top (in case of previous open call) */
  930. )
  931. vmcase(OP_CLOSURE,
  932. Proto *p = cl->p->p[GETARG_Bx(i)];
  933. Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  934. if (ncl == NULL) /* no match? */
  935. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  936. else
  937. setclLvalue(L, ra, ncl); /* push cashed closure */
  938. checkGC(L, ra + 1);
  939. )
  940. vmcase(OP_VARARG,
  941. int b = GETARG_B(i) - 1;
  942. int j;
  943. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  944. if (b < 0) { /* B == 0? */
  945. b = n; /* get all var. arguments */
  946. Protect(luaD_checkstack(L, n));
  947. ra = RA(i); /* previous call may change the stack */
  948. L->top = ra + n;
  949. }
  950. for (j = 0; j < b; j++) {
  951. if (j < n) {
  952. setobjs2s(L, ra + j, base - n + j);
  953. }
  954. else {
  955. setnilvalue(ra + j);
  956. }
  957. }
  958. )
  959. vmcase(OP_EXTRAARG,
  960. lua_assert(0);
  961. )
  962. }
  963. }
  964. }