lvm.c 30 KB

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