lvm.c 26 KB

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