lvm.c 28 KB

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