lvm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. ** $Id: lvm.c,v 2.83 2009/03/04 13:32:29 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. lua_number2str(s, n);
  42. setsvalue2s(L, obj, luaS_new(L, s));
  43. return 1;
  44. }
  45. }
  46. static void traceexec (lua_State *L) {
  47. lu_byte mask = L->hookmask;
  48. if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
  49. resethookcount(L);
  50. luaD_callhook(L, LUA_HOOKCOUNT, -1);
  51. }
  52. if (mask & LUA_MASKLINE) {
  53. Proto *p = ci_func(L->ci)->l.p;
  54. int npc = pcRel(L->savedpc, p);
  55. int newline = getline(p, npc);
  56. if (npc == 0 || /* call linehook when enter a new function, */
  57. L->savedpc <= L->oldpc || /* when jump back (loop), or when */
  58. newline != getline(p, pcRel(L->oldpc, p))) /* enter a new line */
  59. luaD_callhook(L, LUA_HOOKLINE, newline);
  60. }
  61. L->oldpc = L->savedpc;
  62. }
  63. static void callTM (lua_State *L, const TValue *f, const TValue *p1,
  64. const TValue *p2, TValue *p3, int hasres) {
  65. ptrdiff_t result = savestack(L, p3);
  66. setobj2s(L, L->top++, f); /* push function */
  67. setobj2s(L, L->top++, p1); /* 1st argument */
  68. setobj2s(L, L->top++, p2); /* 2nd argument */
  69. if (!hasres) /* no result? 'p3' is third argument */
  70. setobj2s(L, L->top++, p3); /* 3th argument */
  71. luaD_checkstack(L, 0);
  72. /* metamethod may yield only when called from Lua code */
  73. luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
  74. if (hasres) { /* if has result, move it to its place */
  75. p3 = restorestack(L, result);
  76. setobjs2s(L, p3, --L->top);
  77. }
  78. }
  79. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  80. int loop;
  81. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  82. const TValue *tm;
  83. if (ttistable(t)) { /* `t' is a table? */
  84. Table *h = hvalue(t);
  85. const TValue *res = luaH_get(h, key); /* do a primitive get */
  86. if (!ttisnil(res) || /* result is no nil? */
  87. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  88. setobj2s(L, val, res);
  89. return;
  90. }
  91. /* else will try the tag method */
  92. }
  93. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  94. luaG_typeerror(L, t, "index");
  95. if (ttisfunction(tm)) {
  96. callTM(L, tm, t, key, val, 1);
  97. return;
  98. }
  99. t = tm; /* else repeat with `tm' */
  100. }
  101. luaG_runerror(L, "loop in gettable");
  102. }
  103. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  104. int loop;
  105. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  106. const TValue *tm;
  107. if (ttistable(t)) { /* `t' is a table? */
  108. Table *h = hvalue(t);
  109. TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
  110. if (!ttisnil(oldval) || /* result is no nil? */
  111. (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
  112. setobj2t(L, oldval, val);
  113. luaC_barriert(L, h, val);
  114. return;
  115. }
  116. /* else will try the tag method */
  117. }
  118. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  119. luaG_typeerror(L, t, "index");
  120. if (ttisfunction(tm)) {
  121. callTM(L, tm, t, key, val, 0);
  122. return;
  123. }
  124. t = tm; /* else repeat with `tm' */
  125. }
  126. luaG_runerror(L, "loop in settable");
  127. }
  128. static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
  129. StkId res, TMS event) {
  130. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  131. if (ttisnil(tm))
  132. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  133. if (ttisnil(tm)) return 0;
  134. callTM(L, tm, p1, p2, res, 1);
  135. return 1;
  136. }
  137. static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
  138. TMS event) {
  139. const TValue *tm1 = fasttm(L, mt1, event);
  140. const TValue *tm2;
  141. if (tm1 == NULL) return NULL; /* no metamethod */
  142. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  143. tm2 = fasttm(L, mt2, event);
  144. if (tm2 == NULL) return NULL; /* no metamethod */
  145. if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
  146. return tm1;
  147. return NULL;
  148. }
  149. static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
  150. TMS event) {
  151. const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
  152. const TValue *tm2;
  153. if (ttisnil(tm1)) return -1; /* no metamethod? */
  154. tm2 = luaT_gettmbyobj(L, p2, event);
  155. if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
  156. return -1;
  157. callTM(L, tm1, p1, p2, L->top, 1);
  158. return !l_isfalse(L->top);
  159. }
  160. static int l_strcmp (const TString *ls, const TString *rs) {
  161. const char *l = getstr(ls);
  162. size_t ll = ls->tsv.len;
  163. const char *r = getstr(rs);
  164. size_t lr = rs->tsv.len;
  165. for (;;) {
  166. int temp = strcoll(l, r);
  167. if (temp != 0) return temp;
  168. else { /* strings are equal up to a `\0' */
  169. size_t len = strlen(l); /* index of first `\0' in both strings */
  170. if (len == lr) /* r is finished? */
  171. return (len == ll) ? 0 : 1;
  172. else if (len == ll) /* l is finished? */
  173. return -1; /* l is smaller than r (because r is not finished) */
  174. /* both strings longer than `len'; go on comparing (after the `\0') */
  175. len++;
  176. l += len; ll -= len; r += len; lr -= len;
  177. }
  178. }
  179. }
  180. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  181. int res;
  182. if (ttype(l) != ttype(r))
  183. return luaG_ordererror(L, l, r);
  184. else if (ttisnumber(l))
  185. return luai_numlt(L, nvalue(l), nvalue(r));
  186. else if (ttisstring(l))
  187. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  188. else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  189. return res;
  190. return luaG_ordererror(L, l, r);
  191. }
  192. static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
  193. int res;
  194. if (ttype(l) != ttype(r))
  195. return luaG_ordererror(L, l, r);
  196. else if (ttisnumber(l))
  197. return luai_numle(L, nvalue(l), nvalue(r));
  198. else if (ttisstring(l))
  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(ttype(t1) == ttype(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_TUSERDATA: {
  215. if (uvalue(t1) == uvalue(t2)) return 1;
  216. tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
  217. break; /* will try TM */
  218. }
  219. case LUA_TTABLE: {
  220. if (hvalue(t1) == hvalue(t2)) return 1;
  221. tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
  222. break; /* will try TM */
  223. }
  224. default: return gcvalue(t1) == gcvalue(t2);
  225. }
  226. if (tm == NULL) return 0; /* no TM? */
  227. callTM(L, tm, t1, t2, L->top, 1); /* call TM */
  228. return !l_isfalse(L->top);
  229. }
  230. void luaV_concat (lua_State *L, int total, int last) {
  231. do {
  232. StkId top = L->base + last + 1;
  233. int n = 2; /* number of elements handled in this pass (at least 2) */
  234. if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
  235. L->top = top; /* set top to current position (in case of yield) */
  236. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  237. luaG_concaterror(L, top-2, top-1);
  238. L->top = L->ci->top; /* restore top */
  239. }
  240. else if (tsvalue(top-1)->len == 0) { /* second operand is empty? */
  241. (void)tostring(L, top - 2); /* result is first operand */ ;
  242. }
  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 - tl) luaG_runerror(L, "string length overflow");
  255. tl += l;
  256. }
  257. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  258. tl = 0;
  259. for (i=n; i>0; i--) { /* concat all strings */
  260. size_t l = tsvalue(top-i)->len;
  261. memcpy(buffer+tl, svalue(top-i), l);
  262. tl += l;
  263. }
  264. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  265. }
  266. total -= n-1; /* got `n' strings to create 1 new */
  267. last -= n-1;
  268. } while (total > 1); /* repeat until only 1 result left */
  269. }
  270. static void objlen (lua_State *L, StkId ra, const TValue *rb) {
  271. const TValue *tm;
  272. switch (ttype(rb)) {
  273. case LUA_TTABLE: {
  274. Table *h = hvalue(rb);
  275. tm = fasttm(L, h->metatable, TM_LEN);
  276. if (tm) break; /* metamethod? break switch to call it */
  277. setnvalue(ra, cast_num(luaH_getn(h))); /* else primitive len */
  278. return;
  279. }
  280. case LUA_TSTRING: {
  281. setnvalue(ra, cast_num(tsvalue(rb)->len));
  282. return;
  283. }
  284. default: { /* try metamethod */
  285. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  286. if (ttisnil(tm)) /* no metamethod? */
  287. luaG_typeerror(L, rb, "get length of");
  288. break;
  289. }
  290. }
  291. callTM(L, tm, rb, luaO_nilobject, ra, 1);
  292. }
  293. static void Arith (lua_State *L, StkId ra, const TValue *rb,
  294. const TValue *rc, TMS op) {
  295. TValue tempb, tempc;
  296. const TValue *b, *c;
  297. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  298. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  299. lua_Number nb = nvalue(b), nc = nvalue(c);
  300. switch (op) {
  301. case TM_ADD: setnvalue(ra, luai_numadd(L, nb, nc)); break;
  302. case TM_SUB: setnvalue(ra, luai_numsub(L, nb, nc)); break;
  303. case TM_MUL: setnvalue(ra, luai_nummul(L, nb, nc)); break;
  304. case TM_DIV: setnvalue(ra, luai_numdiv(L, nb, nc)); break;
  305. case TM_MOD: setnvalue(ra, luai_nummod(L, nb, nc)); break;
  306. case TM_POW: setnvalue(ra, luai_numpow(L, nb, nc)); break;
  307. case TM_UNM: setnvalue(ra, luai_numunm(L, nb)); break;
  308. default: lua_assert(0); break;
  309. }
  310. }
  311. else if (!call_binTM(L, rb, rc, ra, op))
  312. luaG_aritherror(L, rb, rc);
  313. }
  314. /*
  315. ** finish execution of an opcode interrupted by an yield
  316. */
  317. void luaV_finishOp (lua_State *L) {
  318. Instruction inst = *(L->savedpc - 1); /* interrupted instruction */
  319. switch (GET_OPCODE(inst)) { /* finish its execution */
  320. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
  321. case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
  322. case OP_GETGLOBAL: case OP_GETTABLE: case OP_SELF: {
  323. setobjs2s(L, L->base + GETARG_A(inst), --L->top);
  324. break;
  325. }
  326. case OP_LE: case OP_LT: case OP_EQ: {
  327. int res = !l_isfalse(L->top - 1);
  328. L->top--;
  329. /* metamethod should not be called when operand is K */
  330. lua_assert(!ISK(GETARG_B(inst)));
  331. if (GET_OPCODE(inst) == OP_LE && /* "<=" using "<" instead? */
  332. ttisnil(luaT_gettmbyobj(L, L->base + GETARG_B(inst), TM_LE)))
  333. res = !res; /* invert result */
  334. lua_assert(GET_OPCODE(*L->savedpc) == OP_JMP);
  335. if (res != GETARG_A(inst)) /* condition failed? */
  336. L->savedpc++; /* skip jump instruction */
  337. break;
  338. }
  339. case OP_CONCAT: {
  340. StkId top = L->top - 1; /* top when __concat was called */
  341. int last = cast_int(top - L->base) - 2; /* last element and ... */
  342. int b = GETARG_B(inst); /* ... first element to concatenate */
  343. int total = last - b + 1; /* number of elements to concatenate */
  344. setobj2s(L, top - 2, top); /* put TM result in proper position */
  345. L->top = L->ci->top; /* correct top */
  346. if (total > 1) /* are there elements to concat? */
  347. luaV_concat(L, total, last); /* concat them (may yield again) */
  348. /* move final result to final position */
  349. setobj2s(L, L->base + GETARG_A(inst), L->base + b);
  350. break;
  351. }
  352. case OP_TFORCALL: {
  353. lua_assert(GET_OPCODE(*L->savedpc) == OP_TFORLOOP);
  354. L->top = L->ci->top; /* correct top */
  355. break;
  356. }
  357. case OP_CALL: {
  358. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  359. L->top = L->ci->top; /* adjust results */
  360. break;
  361. }
  362. case OP_TAILCALL: case OP_SETGLOBAL: case OP_SETTABLE:
  363. break;
  364. default: lua_assert(0);
  365. }
  366. }
  367. /*
  368. ** some macros for common tasks in `luaV_execute'
  369. */
  370. #define runtime_check(L, c) { if (!(c)) break; }
  371. #define RA(i) (base+GETARG_A(i))
  372. /* to be used after possible stack reallocation */
  373. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  374. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  375. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  376. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  377. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  378. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  379. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  380. #define dojump(L,i) { L->savedpc += (i); luai_threadyield(L);}
  381. #define Protect(x) { {x;}; base = L->base; }
  382. #define arith_op(op,tm) { \
  383. TValue *rb = RKB(i); \
  384. TValue *rc = RKC(i); \
  385. if (ttisnumber(rb) && ttisnumber(rc)) { \
  386. lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  387. setnvalue(ra, op(L, nb, nc)); \
  388. } \
  389. else \
  390. Protect(Arith(L, ra, rb, rc, tm)); \
  391. }
  392. void luaV_execute (lua_State *L) {
  393. LClosure *cl;
  394. StkId base;
  395. TValue *k;
  396. reentry: /* entry point */
  397. lua_assert(isLua(L->ci));
  398. cl = &curr_func(L)->l;
  399. base = L->base;
  400. k = cl->p->k;
  401. /* main loop of interpreter */
  402. for (;;) {
  403. Instruction i = *(L->savedpc++);
  404. StkId ra;
  405. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  406. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  407. traceexec(L);
  408. if (L->status == LUA_YIELD) { /* did hook yield? */
  409. L->savedpc--; /* undo increment */
  410. luaD_throw(L, LUA_YIELD);
  411. }
  412. base = L->base;
  413. }
  414. /* warning!! several calls may realloc the stack and invalidate `ra' */
  415. ra = RA(i);
  416. lua_assert(base == L->base && L->base == L->ci->base);
  417. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  418. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  419. switch (GET_OPCODE(i)) {
  420. case OP_MOVE: {
  421. setobjs2s(L, ra, RB(i));
  422. continue;
  423. }
  424. case OP_LOADK: {
  425. setobj2s(L, ra, KBx(i));
  426. continue;
  427. }
  428. case OP_LOADBOOL: {
  429. setbvalue(ra, GETARG_B(i));
  430. if (GETARG_C(i)) L->savedpc++; /* skip next instruction (if C) */
  431. continue;
  432. }
  433. case OP_LOADNIL: {
  434. TValue *rb = RB(i);
  435. do {
  436. setnilvalue(rb--);
  437. } while (rb >= ra);
  438. continue;
  439. }
  440. case OP_GETUPVAL: {
  441. int b = GETARG_B(i);
  442. setobj2s(L, ra, cl->upvals[b]->v);
  443. continue;
  444. }
  445. case OP_GETGLOBAL: {
  446. TValue g;
  447. TValue *rb = KBx(i);
  448. sethvalue(L, &g, cl->env);
  449. lua_assert(ttisstring(rb));
  450. Protect(luaV_gettable(L, &g, rb, ra));
  451. continue;
  452. }
  453. case OP_GETTABLE: {
  454. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  455. continue;
  456. }
  457. case OP_SETGLOBAL: {
  458. TValue g;
  459. sethvalue(L, &g, cl->env);
  460. lua_assert(ttisstring(KBx(i)));
  461. Protect(luaV_settable(L, &g, KBx(i), ra));
  462. continue;
  463. }
  464. case OP_SETUPVAL: {
  465. UpVal *uv = cl->upvals[GETARG_B(i)];
  466. setobj(L, uv->v, ra);
  467. luaC_barrier(L, uv, ra);
  468. continue;
  469. }
  470. case OP_SETTABLE: {
  471. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  472. continue;
  473. }
  474. case OP_NEWTABLE: {
  475. int b = GETARG_B(i);
  476. int c = GETARG_C(i);
  477. Table *t = luaH_new(L);
  478. sethvalue(L, ra, t);
  479. if (b != 0 || c != 0)
  480. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  481. Protect(luaC_checkGC(L));
  482. continue;
  483. }
  484. case OP_SELF: {
  485. StkId rb = RB(i);
  486. setobjs2s(L, ra+1, rb);
  487. Protect(luaV_gettable(L, rb, RKC(i), ra));
  488. continue;
  489. }
  490. case OP_ADD: {
  491. arith_op(luai_numadd, TM_ADD);
  492. continue;
  493. }
  494. case OP_SUB: {
  495. arith_op(luai_numsub, TM_SUB);
  496. continue;
  497. }
  498. case OP_MUL: {
  499. arith_op(luai_nummul, TM_MUL);
  500. continue;
  501. }
  502. case OP_DIV: {
  503. arith_op(luai_numdiv, TM_DIV);
  504. continue;
  505. }
  506. case OP_MOD: {
  507. arith_op(luai_nummod, TM_MOD);
  508. continue;
  509. }
  510. case OP_POW: {
  511. arith_op(luai_numpow, TM_POW);
  512. continue;
  513. }
  514. case OP_UNM: {
  515. TValue *rb = RB(i);
  516. if (ttisnumber(rb)) {
  517. lua_Number nb = nvalue(rb);
  518. setnvalue(ra, luai_numunm(L, nb));
  519. }
  520. else {
  521. Protect(Arith(L, ra, rb, rb, TM_UNM));
  522. }
  523. continue;
  524. }
  525. case OP_NOT: {
  526. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  527. setbvalue(ra, res);
  528. continue;
  529. }
  530. case OP_LEN: {
  531. Protect(objlen(L, ra, RB(i)));
  532. continue;
  533. }
  534. case OP_CONCAT: {
  535. int b = GETARG_B(i);
  536. int c = GETARG_C(i);
  537. Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
  538. setobjs2s(L, RA(i), base+b);
  539. continue;
  540. }
  541. case OP_JMP: {
  542. dojump(L, GETARG_sBx(i));
  543. continue;
  544. }
  545. case OP_EQ: {
  546. TValue *rb = RKB(i);
  547. TValue *rc = RKC(i);
  548. Protect(
  549. if (equalobj(L, rb, rc) == GETARG_A(i))
  550. dojump(L, GETARG_sBx(*L->savedpc));
  551. )
  552. L->savedpc++;
  553. continue;
  554. }
  555. case OP_LT: {
  556. Protect(
  557. if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
  558. dojump(L, GETARG_sBx(*L->savedpc));
  559. )
  560. L->savedpc++;
  561. continue;
  562. }
  563. case OP_LE: {
  564. Protect(
  565. if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
  566. dojump(L, GETARG_sBx(*L->savedpc));
  567. )
  568. L->savedpc++;
  569. continue;
  570. }
  571. case OP_TEST: {
  572. if (GETARG_C(i) ? !l_isfalse(ra) : l_isfalse(ra))
  573. dojump(L, GETARG_sBx(*L->savedpc));
  574. L->savedpc++;
  575. continue;
  576. }
  577. case OP_TESTSET: {
  578. TValue *rb = RB(i);
  579. if (GETARG_C(i) ? !l_isfalse(rb) : l_isfalse(rb)) {
  580. setobjs2s(L, ra, rb);
  581. dojump(L, GETARG_sBx(*L->savedpc));
  582. }
  583. L->savedpc++;
  584. continue;
  585. }
  586. case OP_CALL: {
  587. int b = GETARG_B(i);
  588. int nresults = GETARG_C(i) - 1;
  589. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  590. if (luaD_precall(L, ra, nresults)) { /* C function? */
  591. if (nresults >= 0) L->top = L->ci->top; /* adjust results */
  592. base = L->base;
  593. continue;
  594. }
  595. else { /* Lua function */
  596. L->ci->callstatus |= CIST_REENTRY;
  597. goto reentry; /* restart luaV_execute over new Lua function */
  598. }
  599. }
  600. case OP_TAILCALL: {
  601. int b = GETARG_B(i);
  602. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  603. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  604. if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */
  605. base = L->base;
  606. continue;
  607. }
  608. else {
  609. /* tail call: put new frame in place of previous one */
  610. CallInfo *ci = L->ci - 1; /* previous frame */
  611. int aux;
  612. StkId func = ci->func;
  613. StkId pfunc = (ci+1)->func; /* previous function index */
  614. if (cl->p->sizep > 0) luaF_close(L, ci->base);
  615. L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
  616. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  617. setobjs2s(L, func+aux, pfunc+aux);
  618. ci->top = L->top = func+aux; /* correct top */
  619. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  620. ci->savedpc = L->savedpc;
  621. ci->u.l.tailcalls++; /* one more call lost */
  622. L->ci--; /* remove new frame */
  623. goto reentry;
  624. }
  625. }
  626. case OP_RETURN: {
  627. int b = GETARG_B(i);
  628. if (b != 0) L->top = ra+b-1;
  629. if (cl->p->sizep > 0) luaF_close(L, base);
  630. b = luaD_poscall(L, ra);
  631. if (!((L->ci + 1)->callstatus & CIST_REENTRY))
  632. return; /* external invocation: return */
  633. else { /* invocation via reentry: continue execution */
  634. if (b) L->top = L->ci->top;
  635. lua_assert(isLua(L->ci));
  636. lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
  637. goto reentry;
  638. }
  639. }
  640. case OP_FORLOOP: {
  641. lua_Number step = nvalue(ra+2);
  642. lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
  643. lua_Number limit = nvalue(ra+1);
  644. if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
  645. : luai_numle(L, limit, idx)) {
  646. dojump(L, GETARG_sBx(i)); /* jump back */
  647. setnvalue(ra, idx); /* update internal index... */
  648. setnvalue(ra+3, idx); /* ...and external index */
  649. }
  650. continue;
  651. }
  652. case OP_FORPREP: {
  653. const TValue *init = ra;
  654. const TValue *plimit = ra+1;
  655. const TValue *pstep = ra+2;
  656. if (!tonumber(init, ra))
  657. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  658. else if (!tonumber(plimit, ra+1))
  659. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  660. else if (!tonumber(pstep, ra+2))
  661. luaG_runerror(L, LUA_QL("for") " step must be a number");
  662. setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
  663. dojump(L, GETARG_sBx(i));
  664. continue;
  665. }
  666. case OP_TFORCALL: {
  667. StkId cb = ra + 3; /* call base */
  668. setobjs2s(L, cb+2, ra+2);
  669. setobjs2s(L, cb+1, ra+1);
  670. setobjs2s(L, cb, ra);
  671. L->top = cb + 3; /* func. + 2 args (state and index) */
  672. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  673. L->top = L->ci->top;
  674. i = *(L->savedpc++); /* go to next instruction */
  675. ra = RA(i);
  676. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  677. /* go through */
  678. }
  679. case OP_TFORLOOP: {
  680. if (!ttisnil(ra + 1)) { /* continue loop? */
  681. setobjs2s(L, ra, ra + 1); /* save control variable */
  682. dojump(L, GETARG_sBx(i)); /* jump back */
  683. }
  684. continue;
  685. }
  686. case OP_SETLIST: {
  687. int n = GETARG_B(i);
  688. int c = GETARG_C(i);
  689. int last;
  690. Table *h;
  691. if (n == 0) n = cast_int(L->top - ra) - 1;
  692. if (c == 0) {
  693. lua_assert(GET_OPCODE(*L->savedpc) == OP_EXTRAARG);
  694. c = GETARG_Ax(*L->savedpc++);
  695. }
  696. runtime_check(L, ttistable(ra));
  697. h = hvalue(ra);
  698. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  699. if (last > h->sizearray) /* needs more space? */
  700. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  701. for (; n > 0; n--) {
  702. TValue *val = ra+n;
  703. setobj2t(L, luaH_setnum(L, h, last--), val);
  704. luaC_barriert(L, h, val);
  705. }
  706. L->top = L->ci->top; /* correct top (in case of previous open call) */
  707. continue;
  708. }
  709. case OP_CLOSE: {
  710. luaF_close(L, ra);
  711. continue;
  712. }
  713. case OP_CLOSURE: {
  714. Proto *p;
  715. Closure *ncl;
  716. int nup, j;
  717. p = cl->p->p[GETARG_Bx(i)];
  718. nup = p->nups;
  719. ncl = luaF_newLclosure(L, nup, cl->env);
  720. ncl->l.p = p;
  721. setclvalue(L, ra, ncl);
  722. for (j=0; j<nup; j++) {
  723. Instruction u = *L->savedpc++;
  724. if (GET_OPCODE(u) == OP_GETUPVAL)
  725. ncl->l.upvals[j] = cl->upvals[GETARG_B(u)];
  726. else {
  727. lua_assert(GET_OPCODE(u) == OP_MOVE);
  728. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(u));
  729. }
  730. }
  731. Protect(luaC_checkGC(L));
  732. continue;
  733. }
  734. case OP_VARARG: {
  735. int b = GETARG_B(i) - 1;
  736. int j;
  737. CallInfo *ci = L->ci;
  738. int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
  739. if (b == LUA_MULTRET) {
  740. Protect(luaD_checkstack(L, n));
  741. ra = RA(i); /* previous call may change the stack */
  742. b = n;
  743. L->top = ra + n;
  744. }
  745. for (j = 0; j < b; j++) {
  746. if (j < n) {
  747. setobjs2s(L, ra + j, ci->base - n + j);
  748. }
  749. else {
  750. setnilvalue(ra + j);
  751. }
  752. }
  753. continue;
  754. }
  755. case OP_EXTRAARG: {
  756. luaG_runerror(L, "bad opcode");
  757. return;
  758. }
  759. }
  760. }
  761. }