lvm.c 30 KB

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