lvm.c 31 KB

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