lvm.c 28 KB

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