lvm.c 27 KB

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