lvm.c 27 KB

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