lvm.c 28 KB

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