lvm.c 33 KB

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