lvm.c 28 KB

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