lvm.c 25 KB

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