lvm.c 31 KB

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