lvm.c 25 KB

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