lvm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. ** $Id: lvm.c,v 2.150 2012/05/08 13:53:33 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, nup);
  355. ncl->l.p = p;
  356. setclLvalue(L, ra, ncl); /* anchor new closure in stack */
  357. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  358. if (uv[i].instack) /* upvalue refers to local variable? */
  359. ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
  360. else /* get upvalue from enclosing function */
  361. ncl->l.upvals[i] = encup[uv[i].idx];
  362. }
  363. luaC_barrierproto(L, p, ncl);
  364. p->cache = ncl; /* save it on cache for reuse */
  365. }
  366. /*
  367. ** finish execution of an opcode interrupted by an yield
  368. */
  369. void luaV_finishOp (lua_State *L) {
  370. CallInfo *ci = L->ci;
  371. StkId base = ci->u.l.base;
  372. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  373. OpCode op = GET_OPCODE(inst);
  374. switch (op) { /* finish its execution */
  375. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
  376. case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
  377. case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
  378. setobjs2s(L, base + GETARG_A(inst), --L->top);
  379. break;
  380. }
  381. case OP_LE: case OP_LT: case OP_EQ: {
  382. int res = !l_isfalse(L->top - 1);
  383. L->top--;
  384. /* metamethod should not be called when operand is K */
  385. lua_assert(!ISK(GETARG_B(inst)));
  386. if (op == OP_LE && /* "<=" using "<" instead? */
  387. ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
  388. res = !res; /* invert result */
  389. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  390. if (res != GETARG_A(inst)) /* condition failed? */
  391. ci->u.l.savedpc++; /* skip jump instruction */
  392. break;
  393. }
  394. case OP_CONCAT: {
  395. StkId top = L->top - 1; /* top when 'call_binTM' was called */
  396. int b = GETARG_B(inst); /* first element to concatenate */
  397. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  398. setobj2s(L, top - 2, top); /* put TM result in proper position */
  399. if (total > 1) { /* are there elements to concat? */
  400. L->top = top - 1; /* top is one after last element (at top-2) */
  401. luaV_concat(L, total); /* concat them (may yield again) */
  402. }
  403. /* move final result to final position */
  404. setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
  405. L->top = ci->top; /* restore top */
  406. break;
  407. }
  408. case OP_TFORCALL: {
  409. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  410. L->top = ci->top; /* correct top */
  411. break;
  412. }
  413. case OP_CALL: {
  414. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  415. L->top = ci->top; /* adjust results */
  416. break;
  417. }
  418. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  419. break;
  420. default: lua_assert(0);
  421. }
  422. }
  423. /*
  424. ** some macros for common tasks in `luaV_execute'
  425. */
  426. #if !defined luai_runtimecheck
  427. #define luai_runtimecheck(L, c) /* void */
  428. #endif
  429. #define RA(i) (base+GETARG_A(i))
  430. /* to be used after possible stack reallocation */
  431. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  432. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  433. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  434. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  435. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  436. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  437. #define KBx(i) \
  438. (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
  439. /* execute a jump instruction */
  440. #define dojump(ci,i,e) \
  441. { int a = GETARG_A(i); \
  442. if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
  443. ci->u.l.savedpc += GETARG_sBx(i) + e; }
  444. /* for test instructions, execute the jump instruction that follows it */
  445. #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
  446. #define Protect(x) { {x;}; base = ci->u.l.base; }
  447. #define checkGC(L,c) \
  448. Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
  449. luaC_step(L); \
  450. L->top = ci->top;}) /* restore top */ \
  451. luai_threadyield(L); )
  452. #define arith_op(op,tm) { \
  453. TValue *rb = RKB(i); \
  454. TValue *rc = RKC(i); \
  455. if (ttisnumber(rb) && ttisnumber(rc)) { \
  456. lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  457. setnvalue(ra, op(L, nb, nc)); \
  458. } \
  459. else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
  460. #define vmdispatch(o) switch(o)
  461. #define vmcase(l,b) case l: {b} break;
  462. #define vmcasenb(l,b) case l: {b} /* nb = no break */
  463. void luaV_execute (lua_State *L) {
  464. CallInfo *ci = L->ci;
  465. LClosure *cl;
  466. TValue *k;
  467. StkId base;
  468. newframe: /* reentry point when frame changes (call/return) */
  469. lua_assert(ci == L->ci);
  470. cl = clLvalue(ci->func);
  471. k = cl->p->k;
  472. base = ci->u.l.base;
  473. /* main loop of interpreter */
  474. for (;;) {
  475. Instruction i = *(ci->u.l.savedpc++);
  476. StkId ra;
  477. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  478. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  479. Protect(traceexec(L));
  480. }
  481. /* WARNING: several calls may realloc the stack and invalidate `ra' */
  482. ra = RA(i);
  483. lua_assert(base == ci->u.l.base);
  484. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  485. vmdispatch (GET_OPCODE(i)) {
  486. vmcase(OP_MOVE,
  487. setobjs2s(L, ra, RB(i));
  488. )
  489. vmcase(OP_LOADK,
  490. TValue *rb = k + GETARG_Bx(i);
  491. setobj2s(L, ra, rb);
  492. )
  493. vmcase(OP_LOADKX,
  494. TValue *rb;
  495. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  496. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  497. setobj2s(L, ra, rb);
  498. )
  499. vmcase(OP_LOADBOOL,
  500. setbvalue(ra, GETARG_B(i));
  501. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  502. )
  503. vmcase(OP_LOADNIL,
  504. int b = GETARG_B(i);
  505. do {
  506. setnilvalue(ra++);
  507. } while (b--);
  508. )
  509. vmcase(OP_GETUPVAL,
  510. int b = GETARG_B(i);
  511. setobj2s(L, ra, cl->upvals[b]->v);
  512. )
  513. vmcase(OP_GETTABUP,
  514. int b = GETARG_B(i);
  515. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  516. )
  517. vmcase(OP_GETTABLE,
  518. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  519. )
  520. vmcase(OP_SETTABUP,
  521. int a = GETARG_A(i);
  522. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  523. )
  524. vmcase(OP_SETUPVAL,
  525. UpVal *uv = cl->upvals[GETARG_B(i)];
  526. setobj(L, uv->v, ra);
  527. luaC_barrier(L, uv, ra);
  528. )
  529. vmcase(OP_SETTABLE,
  530. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  531. )
  532. vmcase(OP_NEWTABLE,
  533. int b = GETARG_B(i);
  534. int c = GETARG_C(i);
  535. Table *t = luaH_new(L);
  536. sethvalue(L, ra, t);
  537. if (b != 0 || c != 0)
  538. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  539. checkGC(L, ra + 1);
  540. )
  541. vmcase(OP_SELF,
  542. StkId rb = RB(i);
  543. setobjs2s(L, ra+1, rb);
  544. Protect(luaV_gettable(L, rb, RKC(i), ra));
  545. )
  546. vmcase(OP_ADD,
  547. arith_op(luai_numadd, TM_ADD);
  548. )
  549. vmcase(OP_SUB,
  550. arith_op(luai_numsub, TM_SUB);
  551. )
  552. vmcase(OP_MUL,
  553. arith_op(luai_nummul, TM_MUL);
  554. )
  555. vmcase(OP_DIV,
  556. arith_op(luai_numdiv, TM_DIV);
  557. )
  558. vmcase(OP_MOD,
  559. arith_op(luai_nummod, TM_MOD);
  560. )
  561. vmcase(OP_POW,
  562. arith_op(luai_numpow, TM_POW);
  563. )
  564. vmcase(OP_UNM,
  565. TValue *rb = RB(i);
  566. if (ttisnumber(rb)) {
  567. lua_Number nb = nvalue(rb);
  568. setnvalue(ra, luai_numunm(L, nb));
  569. }
  570. else {
  571. Protect(luaV_arith(L, ra, rb, rb, TM_UNM));
  572. }
  573. )
  574. vmcase(OP_NOT,
  575. TValue *rb = RB(i);
  576. int res = l_isfalse(rb); /* next assignment may change this value */
  577. setbvalue(ra, res);
  578. )
  579. vmcase(OP_LEN,
  580. Protect(luaV_objlen(L, ra, RB(i)));
  581. )
  582. vmcase(OP_CONCAT,
  583. int b = GETARG_B(i);
  584. int c = GETARG_C(i);
  585. StkId rb;
  586. L->top = base + c + 1; /* mark the end of concat operands */
  587. Protect(luaV_concat(L, c - b + 1));
  588. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  589. rb = b + base;
  590. setobjs2s(L, ra, rb);
  591. checkGC(L, (ra >= rb ? ra + 1 : rb));
  592. L->top = ci->top; /* restore top */
  593. )
  594. vmcase(OP_JMP,
  595. dojump(ci, i, 0);
  596. )
  597. vmcase(OP_EQ,
  598. TValue *rb = RKB(i);
  599. TValue *rc = RKC(i);
  600. Protect(
  601. if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i))
  602. ci->u.l.savedpc++;
  603. else
  604. donextjump(ci);
  605. )
  606. )
  607. vmcase(OP_LT,
  608. Protect(
  609. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  610. ci->u.l.savedpc++;
  611. else
  612. donextjump(ci);
  613. )
  614. )
  615. vmcase(OP_LE,
  616. Protect(
  617. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  618. ci->u.l.savedpc++;
  619. else
  620. donextjump(ci);
  621. )
  622. )
  623. vmcase(OP_TEST,
  624. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  625. ci->u.l.savedpc++;
  626. else
  627. donextjump(ci);
  628. )
  629. vmcase(OP_TESTSET,
  630. TValue *rb = RB(i);
  631. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  632. ci->u.l.savedpc++;
  633. else {
  634. setobjs2s(L, ra, rb);
  635. donextjump(ci);
  636. }
  637. )
  638. vmcase(OP_CALL,
  639. int b = GETARG_B(i);
  640. int nresults = GETARG_C(i) - 1;
  641. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  642. if (luaD_precall(L, ra, nresults)) { /* C function? */
  643. if (nresults >= 0) L->top = ci->top; /* adjust results */
  644. base = ci->u.l.base;
  645. }
  646. else { /* Lua function */
  647. ci = L->ci;
  648. ci->callstatus |= CIST_REENTRY;
  649. goto newframe; /* restart luaV_execute over new Lua function */
  650. }
  651. )
  652. vmcase(OP_TAILCALL,
  653. int b = GETARG_B(i);
  654. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  655. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  656. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  657. base = ci->u.l.base;
  658. else {
  659. /* tail call: put called frame (n) in place of caller one (o) */
  660. CallInfo *nci = L->ci; /* called frame */
  661. CallInfo *oci = nci->previous; /* caller frame */
  662. StkId nfunc = nci->func; /* called function */
  663. StkId ofunc = oci->func; /* caller function */
  664. /* last stack slot filled by 'precall' */
  665. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  666. int aux;
  667. /* close all upvalues from previous call */
  668. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  669. /* move new frame into old one */
  670. for (aux = 0; nfunc + aux < lim; aux++)
  671. setobjs2s(L, ofunc + aux, nfunc + aux);
  672. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  673. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  674. oci->u.l.savedpc = nci->u.l.savedpc;
  675. oci->callstatus |= CIST_TAIL; /* function was tail called */
  676. ci = L->ci = oci; /* remove new frame */
  677. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  678. goto newframe; /* restart luaV_execute over new Lua function */
  679. }
  680. )
  681. vmcasenb(OP_RETURN,
  682. int b = GETARG_B(i);
  683. if (b != 0) L->top = ra+b-1;
  684. if (cl->p->sizep > 0) luaF_close(L, base);
  685. b = luaD_poscall(L, ra);
  686. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  687. return; /* external invocation: return */
  688. else { /* invocation via reentry: continue execution */
  689. ci = L->ci;
  690. if (b) L->top = ci->top;
  691. lua_assert(isLua(ci));
  692. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  693. goto newframe; /* restart luaV_execute over new Lua function */
  694. }
  695. )
  696. vmcase(OP_FORLOOP,
  697. lua_Number step = nvalue(ra+2);
  698. lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
  699. lua_Number limit = nvalue(ra+1);
  700. if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
  701. : luai_numle(L, limit, idx)) {
  702. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  703. setnvalue(ra, idx); /* update internal index... */
  704. setnvalue(ra+3, idx); /* ...and external index */
  705. }
  706. )
  707. vmcase(OP_FORPREP,
  708. const TValue *init = ra;
  709. const TValue *plimit = ra+1;
  710. const TValue *pstep = ra+2;
  711. if (!tonumber(init, ra))
  712. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  713. else if (!tonumber(plimit, ra+1))
  714. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  715. else if (!tonumber(pstep, ra+2))
  716. luaG_runerror(L, LUA_QL("for") " step must be a number");
  717. setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
  718. ci->u.l.savedpc += GETARG_sBx(i);
  719. )
  720. vmcasenb(OP_TFORCALL,
  721. StkId cb = ra + 3; /* call base */
  722. setobjs2s(L, cb+2, ra+2);
  723. setobjs2s(L, cb+1, ra+1);
  724. setobjs2s(L, cb, ra);
  725. L->top = cb + 3; /* func. + 2 args (state and index) */
  726. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  727. L->top = ci->top;
  728. i = *(ci->u.l.savedpc++); /* go to next instruction */
  729. ra = RA(i);
  730. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  731. goto l_tforloop;
  732. )
  733. vmcase(OP_TFORLOOP,
  734. l_tforloop:
  735. if (!ttisnil(ra + 1)) { /* continue loop? */
  736. setobjs2s(L, ra, ra + 1); /* save control variable */
  737. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  738. }
  739. )
  740. vmcase(OP_SETLIST,
  741. int n = GETARG_B(i);
  742. int c = GETARG_C(i);
  743. int last;
  744. Table *h;
  745. if (n == 0) n = cast_int(L->top - ra) - 1;
  746. if (c == 0) {
  747. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  748. c = GETARG_Ax(*ci->u.l.savedpc++);
  749. }
  750. luai_runtimecheck(L, ttistable(ra));
  751. h = hvalue(ra);
  752. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  753. if (last > h->sizearray) /* needs more space? */
  754. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  755. for (; n > 0; n--) {
  756. TValue *val = ra+n;
  757. luaH_setint(L, h, last--, val);
  758. luaC_barrierback(L, obj2gco(h), val);
  759. }
  760. L->top = ci->top; /* correct top (in case of previous open call) */
  761. )
  762. vmcase(OP_CLOSURE,
  763. Proto *p = cl->p->p[GETARG_Bx(i)];
  764. Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  765. if (ncl == NULL) /* no match? */
  766. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  767. else
  768. setclLvalue(L, ra, ncl); /* push cashed closure */
  769. checkGC(L, ra + 1);
  770. )
  771. vmcase(OP_VARARG,
  772. int b = GETARG_B(i) - 1;
  773. int j;
  774. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  775. if (b < 0) { /* B == 0? */
  776. b = n; /* get all var. arguments */
  777. Protect(luaD_checkstack(L, n));
  778. ra = RA(i); /* previous call may change the stack */
  779. L->top = ra + n;
  780. }
  781. for (j = 0; j < b; j++) {
  782. if (j < n) {
  783. setobjs2s(L, ra + j, base - n + j);
  784. }
  785. else {
  786. setnilvalue(ra + j);
  787. }
  788. }
  789. )
  790. vmcase(OP_EXTRAARG,
  791. lua_assert(0);
  792. )
  793. }
  794. }
  795. }