lvm.c 33 KB

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