lvm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. ** $Id: lvm.c,v 2.143 2011/08/17 20:26:47 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. void luaV_execute (lua_State *L) {
  457. CallInfo *ci = L->ci;
  458. LClosure *cl;
  459. TValue *k;
  460. StkId base;
  461. newframe: /* reentry point when frame changes (call/return) */
  462. lua_assert(ci == L->ci);
  463. cl = clLvalue(ci->func);
  464. k = cl->p->k;
  465. base = ci->u.l.base;
  466. /* main loop of interpreter */
  467. for (;;) {
  468. Instruction i = *(ci->u.l.savedpc++);
  469. StkId ra;
  470. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  471. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  472. Protect(traceexec(L));
  473. }
  474. /* warning!! several calls may realloc the stack and invalidate `ra' */
  475. ra = RA(i);
  476. lua_assert(base == ci->u.l.base);
  477. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  478. vmdispatch (GET_OPCODE(i)) {
  479. vmcase(OP_MOVE,
  480. setobjs2s(L, ra, RB(i));
  481. )
  482. vmcase(OP_LOADK,
  483. TValue *rb = k + GETARG_Bx(i);
  484. setobj2s(L, ra, rb);
  485. )
  486. vmcase(OP_LOADKX,
  487. TValue *rb;
  488. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  489. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  490. setobj2s(L, ra, rb);
  491. )
  492. vmcase(OP_LOADBOOL,
  493. setbvalue(ra, GETARG_B(i));
  494. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  495. )
  496. vmcase(OP_LOADNIL,
  497. int b = GETARG_B(i);
  498. do {
  499. setnilvalue(ra++);
  500. } while (b--);
  501. )
  502. vmcase(OP_GETUPVAL,
  503. int b = GETARG_B(i);
  504. setobj2s(L, ra, cl->upvals[b]->v);
  505. )
  506. vmcase(OP_GETTABUP,
  507. int b = GETARG_B(i);
  508. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  509. )
  510. vmcase(OP_GETTABLE,
  511. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  512. )
  513. vmcase(OP_SETTABUP,
  514. int a = GETARG_A(i);
  515. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  516. )
  517. vmcase(OP_SETUPVAL,
  518. UpVal *uv = cl->upvals[GETARG_B(i)];
  519. setobj(L, uv->v, ra);
  520. luaC_barrier(L, uv, ra);
  521. )
  522. vmcase(OP_SETTABLE,
  523. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  524. )
  525. vmcase(OP_NEWTABLE,
  526. int b = GETARG_B(i);
  527. int c = GETARG_C(i);
  528. Table *t = luaH_new(L);
  529. sethvalue(L, ra, t);
  530. if (b != 0 || c != 0)
  531. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  532. checkGC(L,
  533. L->top = ra + 1; /* limit of live values */
  534. luaC_step(L);
  535. L->top = ci->top; /* restore top */
  536. )
  537. )
  538. vmcase(OP_SELF,
  539. StkId rb = RB(i);
  540. setobjs2s(L, ra+1, rb);
  541. Protect(luaV_gettable(L, rb, RKC(i), ra));
  542. )
  543. vmcase(OP_ADD,
  544. arith_op(luai_numadd, TM_ADD);
  545. )
  546. vmcase(OP_SUB,
  547. arith_op(luai_numsub, TM_SUB);
  548. )
  549. vmcase(OP_MUL,
  550. arith_op(luai_nummul, TM_MUL);
  551. )
  552. vmcase(OP_DIV,
  553. arith_op(luai_numdiv, TM_DIV);
  554. )
  555. vmcase(OP_MOD,
  556. arith_op(luai_nummod, TM_MOD);
  557. )
  558. vmcase(OP_POW,
  559. arith_op(luai_numpow, TM_POW);
  560. )
  561. vmcase(OP_UNM,
  562. TValue *rb = RB(i);
  563. if (ttisnumber(rb)) {
  564. lua_Number nb = nvalue(rb);
  565. setnvalue(ra, luai_numunm(L, nb));
  566. }
  567. else {
  568. Protect(luaV_arith(L, ra, rb, rb, TM_UNM));
  569. }
  570. )
  571. vmcase(OP_NOT,
  572. TValue *rb = RB(i);
  573. int res = l_isfalse(rb); /* next assignment may change this value */
  574. setbvalue(ra, res);
  575. )
  576. vmcase(OP_LEN,
  577. Protect(luaV_objlen(L, ra, RB(i)));
  578. )
  579. vmcase(OP_CONCAT,
  580. int b = GETARG_B(i);
  581. int c = GETARG_C(i);
  582. StkId rb;
  583. L->top = base + c + 1; /* mark the end of concat operands */
  584. Protect(luaV_concat(L, c - b + 1));
  585. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  586. rb = b + base;
  587. setobjs2s(L, ra, rb);
  588. checkGC(L,
  589. L->top = (ra >= rb ? ra + 1 : rb); /* limit of live values */
  590. luaC_step(L);
  591. )
  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 (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. vmcase(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. vmcase(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,
  770. L->top = ra + 1; /* limit of live values */
  771. luaC_step(L);
  772. L->top = ci->top; /* restore top */
  773. )
  774. )
  775. vmcase(OP_VARARG,
  776. int b = GETARG_B(i) - 1;
  777. int j;
  778. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  779. if (b < 0) { /* B == 0? */
  780. b = n; /* get all var. arguments */
  781. Protect(luaD_checkstack(L, n));
  782. ra = RA(i); /* previous call may change the stack */
  783. L->top = ra + n;
  784. }
  785. for (j = 0; j < b; j++) {
  786. if (j < n) {
  787. setobjs2s(L, ra + j, base - n + j);
  788. }
  789. else {
  790. setnilvalue(ra + j);
  791. }
  792. }
  793. )
  794. vmcase(OP_EXTRAARG,
  795. lua_assert(0);
  796. )
  797. }
  798. }
  799. }