lvm.c 32 KB

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