lvm.c 31 KB

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