lvm.c 25 KB

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