lvm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. ** $Id: lvm.c,v 1.259 2002/11/06 19:08:00 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lobject.h"
  15. #include "lopcodes.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #include "lvm.h"
  21. /* function to convert a lua_Number to a string */
  22. #ifndef lua_number2str
  23. #include <stdio.h>
  24. #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  25. #endif
  26. /* limit for table tag-method chains (to avoid loops) */
  27. #define MAXTAGLOOP 100
  28. static void luaV_checkGC (lua_State *L, StkId top) {
  29. if (G(L)->nblocks >= G(L)->GCthreshold) {
  30. L->top = top; /* limit for active registers */
  31. luaC_collectgarbage(L);
  32. L->top = L->ci->top; /* restore old top position */
  33. }
  34. }
  35. const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
  36. lua_Number num;
  37. if (ttisnumber(obj)) return obj;
  38. if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  39. setnvalue(n, num);
  40. return n;
  41. }
  42. else
  43. return NULL;
  44. }
  45. int luaV_tostring (lua_State *L, TObject *obj) {
  46. if (!ttisnumber(obj))
  47. return 0;
  48. else {
  49. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  50. lua_number2str(s, nvalue(obj));
  51. setsvalue(obj, luaS_new(L, s));
  52. return 1;
  53. }
  54. }
  55. static void traceexec (lua_State *L) {
  56. unsigned long mask = L->hookmask;
  57. if (mask > LUA_MASKLINE) { /* instruction-hook set? */
  58. if (L->hookcount == 0) {
  59. luaD_callhook(L, LUA_HOOKCOUNT, -1);
  60. resethookcount(L);
  61. return;
  62. }
  63. }
  64. if (mask & LUA_MASKLINE) {
  65. CallInfo *ci = L->ci;
  66. Proto *p = ci_func(ci)->l.p;
  67. int newline = getline(p, pcRel(*ci->u.l.pc, p));
  68. lua_assert(ci->state & CI_HASFRAME);
  69. if (pcRel(*ci->u.l.pc, p) == 0) /* tracing may be starting now? */
  70. ci->u.l.savedpc = *ci->u.l.pc; /* initialize `savedpc' */
  71. /* calls linehook when enters a new line or jumps back (loop) */
  72. if (*ci->u.l.pc <= ci->u.l.savedpc ||
  73. newline != getline(p, pcRel(ci->u.l.savedpc, p))) {
  74. luaD_callhook(L, LUA_HOOKLINE, newline);
  75. ci = L->ci; /* previous call may reallocate `ci' */
  76. }
  77. ci->u.l.savedpc = *ci->u.l.pc;
  78. }
  79. }
  80. static void callTMres (lua_State *L, const TObject *f,
  81. const TObject *p1, const TObject *p2) {
  82. setobj2s(L->top, f); /* push function */
  83. setobj2s(L->top+1, p1); /* 1st argument */
  84. setobj2s(L->top+2, p2); /* 2nd argument */
  85. luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
  86. L->top += 3;
  87. luaD_call(L, L->top - 3, 1);
  88. L->top--; /* result will be in L->top */
  89. }
  90. static void callTM (lua_State *L, const TObject *f,
  91. const TObject *p1, const TObject *p2, const TObject *p3) {
  92. setobj2s(L->top, f); /* push function */
  93. setobj2s(L->top+1, p1); /* 1st argument */
  94. setobj2s(L->top+2, p2); /* 2nd argument */
  95. setobj2s(L->top+3, p3); /* 3th argument */
  96. luaD_checkstack(L, 4); /* cannot check before (could invalidate p1...p3) */
  97. L->top += 4;
  98. luaD_call(L, L->top - 4, 0);
  99. }
  100. static const TObject *luaV_index (lua_State *L, const TObject *t,
  101. TObject *key, int loop) {
  102. const TObject *tm = fasttm(L, hvalue(t)->metatable, TM_INDEX);
  103. if (tm == NULL) return &luaO_nilobject; /* no TM */
  104. if (ttisfunction(tm)) {
  105. callTMres(L, tm, t, key);
  106. return L->top;
  107. }
  108. else return luaV_gettable(L, tm, key, loop);
  109. }
  110. static const TObject *luaV_getnotable (lua_State *L, const TObject *t,
  111. TObject *key, int loop) {
  112. const TObject *tm = luaT_gettmbyobj(L, t, TM_INDEX);
  113. if (ttisnil(tm))
  114. luaG_typeerror(L, t, "index");
  115. if (ttisfunction(tm)) {
  116. callTMres(L, tm, t, key);
  117. return L->top;
  118. }
  119. else return luaV_gettable(L, tm, key, loop);
  120. }
  121. /*
  122. ** Function to index a table.
  123. ** Receives the table at `t' and the key at `key'.
  124. ** leaves the result at `res'.
  125. */
  126. const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key,
  127. int loop) {
  128. if (loop > MAXTAGLOOP)
  129. luaG_runerror(L, "loop in gettable");
  130. if (ttistable(t)) { /* `t' is a table? */
  131. Table *h = hvalue(t);
  132. const TObject *v = luaH_get(h, key); /* do a primitive get */
  133. if (!ttisnil(v)) return v;
  134. else return luaV_index(L, t, key, loop+1);
  135. }
  136. else return luaV_getnotable(L, t, key, loop+1);
  137. }
  138. /*
  139. ** Receives table at `t', key at `key' and value at `val'.
  140. */
  141. void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
  142. const TObject *tm;
  143. int loop = 0;
  144. do {
  145. if (ttistable(t)) { /* `t' is a table? */
  146. Table *h = hvalue(t);
  147. TObject *oldval = luaH_set(L, h, key); /* do a primitive set */
  148. if (!ttisnil(oldval) || /* result is no nil? */
  149. (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
  150. setobj2t(oldval, val);
  151. return;
  152. }
  153. /* else will try the tag method */
  154. }
  155. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  156. luaG_typeerror(L, t, "index");
  157. if (ttisfunction(tm)) {
  158. callTM(L, tm, t, key, val);
  159. return;
  160. }
  161. t = tm; /* else repeat with `tm' */
  162. } while (++loop <= MAXTAGLOOP);
  163. luaG_runerror(L, "loop in settable");
  164. }
  165. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  166. StkId res, TMS event) {
  167. ptrdiff_t result = savestack(L, res);
  168. const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  169. if (ttisnil(tm))
  170. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  171. if (!ttisfunction(tm)) return 0;
  172. callTMres(L, tm, p1, p2);
  173. res = restorestack(L, result); /* previous call may change stack */
  174. setobjs2s(res, L->top);
  175. return 1;
  176. }
  177. static int luaV_strcmp (const TString *ls, const TString *rs) {
  178. const char *l = getstr(ls);
  179. size_t ll = ls->tsv.len;
  180. const char *r = getstr(rs);
  181. size_t lr = rs->tsv.len;
  182. for (;;) {
  183. int temp = strcoll(l, r);
  184. if (temp != 0) return temp;
  185. else { /* strings are equal up to a `\0' */
  186. size_t len = strlen(l); /* index of first `\0' in both strings */
  187. if (len == lr) /* r is finished? */
  188. return (len == ll) ? 0 : 1;
  189. else if (len == ll) /* l is finished? */
  190. return -1; /* l is smaller than r (because r is not finished) */
  191. /* both strings longer than `len'; go on comparing (after the `\0') */
  192. len++;
  193. l += len; ll -= len; r += len; lr -= len;
  194. }
  195. }
  196. }
  197. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
  198. if (ttype(l) != ttype(r))
  199. return luaG_ordererror(L, l, r);
  200. else if (ttisnumber(l))
  201. return nvalue(l) < nvalue(r);
  202. else if (ttisstring(l))
  203. return luaV_strcmp(tsvalue(l), tsvalue(r)) < 0;
  204. else if (call_binTM(L, l, r, L->top, TM_LT))
  205. return !l_isfalse(L->top);
  206. return luaG_ordererror(L, l, r);
  207. }
  208. static int luaV_lessequal (lua_State *L, const TObject *l, const TObject *r) {
  209. if (ttype(l) != ttype(r))
  210. return luaG_ordererror(L, l, r);
  211. else if (ttisnumber(l))
  212. return nvalue(l) <= nvalue(r);
  213. else if (ttisstring(l))
  214. return luaV_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  215. else if (call_binTM(L, l, r, L->top, TM_LE)) /* first try `le' */
  216. return !l_isfalse(L->top);
  217. else if (call_binTM(L, r, l, L->top, TM_LT)) /* else try `lt' */
  218. return l_isfalse(L->top);
  219. return luaG_ordererror(L, l, r);
  220. }
  221. int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2) {
  222. const TObject *tm = NULL;
  223. lua_assert(ttype(t1) == ttype(t2));
  224. switch (ttype(t1)) {
  225. case LUA_TNIL: return 1;
  226. case LUA_TNUMBER: return nvalue(t1) == nvalue(t2);
  227. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  228. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  229. case LUA_TUSERDATA: {
  230. if (uvalue(t1) == uvalue(t2)) return 1;
  231. else if ((tm = fasttm(L, uvalue(t1)->uv.metatable, TM_EQ)) == NULL &&
  232. (tm = fasttm(L, uvalue(t2)->uv.metatable, TM_EQ)) == NULL)
  233. return 0; /* no TM */
  234. else break; /* will try TM */
  235. }
  236. case LUA_TTABLE: {
  237. if (hvalue(t1) == hvalue(t2)) return 1;
  238. else if ((tm = fasttm(L, hvalue(t1)->metatable, TM_EQ)) == NULL &&
  239. (tm = fasttm(L, hvalue(t2)->metatable, TM_EQ)) == NULL)
  240. return 0; /* no TM */
  241. else break; /* will try TM */
  242. }
  243. default: return gcvalue(t1) == gcvalue(t2);
  244. }
  245. callTMres(L, tm, t1, t2); /* call TM */
  246. return !l_isfalse(L->top);
  247. }
  248. void luaV_concat (lua_State *L, int total, int last) {
  249. do {
  250. StkId top = L->ci->base + last + 1;
  251. int n = 2; /* number of elements handled in this pass (at least 2) */
  252. if (!tostring(L, top-2) || !tostring(L, top-1)) {
  253. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  254. luaG_concaterror(L, top-2, top-1);
  255. } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
  256. /* at least two string values; get as many as possible */
  257. lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
  258. cast(lu_mem, tsvalue(top-2)->tsv.len);
  259. char *buffer;
  260. int i;
  261. while (n < total && tostring(L, top-n-1)) { /* collect total length */
  262. tl += tsvalue(top-n-1)->tsv.len;
  263. n++;
  264. }
  265. if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
  266. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  267. tl = 0;
  268. for (i=n; i>0; i--) { /* concat all strings */
  269. size_t l = tsvalue(top-i)->tsv.len;
  270. memcpy(buffer+tl, svalue(top-i), l);
  271. tl += l;
  272. }
  273. setsvalue2s(top-n, luaS_newlstr(L, buffer, tl));
  274. }
  275. total -= n-1; /* got `n' strings to create 1 new */
  276. last -= n-1;
  277. } while (total > 1); /* repeat until only 1 result left */
  278. }
  279. static void Arith (lua_State *L, StkId ra,
  280. const TObject *rb, const TObject *rc, TMS op) {
  281. TObject tempb, tempc;
  282. const TObject *b, *c;
  283. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  284. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  285. switch (op) {
  286. case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break;
  287. case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break;
  288. case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break;
  289. case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break;
  290. case TM_POW: {
  291. const TObject *f = luaH_getstr(hvalue(registry(L)),
  292. G(L)->tmname[TM_POW]);
  293. ptrdiff_t res = savestack(L, ra);
  294. if (!ttisfunction(f))
  295. luaG_runerror(L, "`pow' (for `^' operator) is not a function");
  296. callTMres(L, f, b, c);
  297. ra = restorestack(L, res); /* previous call may change stack */
  298. setobjs2s(ra, L->top);
  299. break;
  300. }
  301. default: lua_assert(0); break;
  302. }
  303. }
  304. else if (!call_binTM(L, rb, rc, ra, op))
  305. luaG_aritherror(L, rb, rc);
  306. }
  307. /*
  308. ** some macros for common tasks in `luaV_execute'
  309. */
  310. #define runtime_check(L, c) { if (!(c)) return 0; }
  311. #define RA(i) (base+GETARG_A(i))
  312. #define RB(i) (base+GETARG_B(i))
  313. #define RKB(i) ((GETARG_B(i) < MAXSTACK) ? RB(i) : k+GETARG_B(i)-MAXSTACK)
  314. #define RC(i) (base+GETARG_C(i))
  315. #define RKC(i) ((GETARG_C(i) < MAXSTACK) ? RC(i) : k+GETARG_C(i)-MAXSTACK)
  316. #define KBx(i) (k+GETARG_Bx(i))
  317. #define dojump(pc, i) ((pc) += (i))
  318. StkId luaV_execute (lua_State *L) {
  319. StkId base;
  320. LClosure *cl;
  321. TObject *k;
  322. const Instruction *pc;
  323. callentry: /* entry point when calling new functions */
  324. L->ci->u.l.pb = &base;
  325. L->ci->u.l.pc = &pc;
  326. if (L->hookmask & LUA_MASKCALL)
  327. luaD_callhook(L, LUA_HOOKCALL, -1);
  328. retentry: /* entry point when returning to old functions */
  329. lua_assert(L->ci->state & CI_SAVEDPC);
  330. L->ci->state = CI_HASFRAME; /* activate frame */
  331. pc = L->ci->u.l.savedpc;
  332. base = L->ci->base;
  333. cl = &clvalue(base - 1)->l;
  334. k = cl->p->k;
  335. /* main loop of interpreter */
  336. for (;;) {
  337. const Instruction i = *pc++;
  338. StkId ra;
  339. if (L->hookmask >= LUA_MASKLINE &&
  340. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE))
  341. traceexec(L);
  342. /* warning!! several calls may realloc the stack and invalidate `ra' */
  343. ra = RA(i);
  344. lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
  345. lua_assert(L->top == L->ci->top ||
  346. GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  347. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  348. switch (GET_OPCODE(i)) {
  349. case OP_MOVE: {
  350. setobjs2s(ra, RB(i));
  351. break;
  352. }
  353. case OP_LOADK: {
  354. setobj2s(ra, KBx(i));
  355. break;
  356. }
  357. case OP_LOADBOOL: {
  358. setbvalue(ra, GETARG_B(i));
  359. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  360. break;
  361. }
  362. case OP_LOADNIL: {
  363. TObject *rb = RB(i);
  364. do {
  365. setnilvalue(rb--);
  366. } while (rb >= ra);
  367. break;
  368. }
  369. case OP_GETUPVAL: {
  370. int b = GETARG_B(i);
  371. setobj2s(ra, cl->upvals[b]->v);
  372. break;
  373. }
  374. case OP_GETGLOBAL: {
  375. TObject *rb = KBx(i);
  376. const TObject *v;
  377. lua_assert(ttisstring(rb) && ttistable(&cl->g));
  378. v = luaH_getstr(hvalue(&cl->g), tsvalue(rb));
  379. if (!ttisnil(v)) { setobj2s(ra, v); }
  380. else
  381. setobj2s(RA(i), luaV_index(L, &cl->g, rb, 0));
  382. break;
  383. }
  384. case OP_GETTABLE: {
  385. StkId rb = RB(i);
  386. TObject *rc = RKC(i);
  387. if (ttistable(rb)) {
  388. const TObject *v = luaH_get(hvalue(rb), rc);
  389. if (!ttisnil(v)) { setobj2s(ra, v); }
  390. else
  391. setobj2s(RA(i), luaV_index(L, rb, rc, 0));
  392. }
  393. else
  394. setobj2s(RA(i), luaV_getnotable(L, rb, rc, 0));
  395. break;
  396. }
  397. case OP_SETGLOBAL: {
  398. lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
  399. luaV_settable(L, &cl->g, KBx(i), ra);
  400. break;
  401. }
  402. case OP_SETUPVAL: {
  403. int b = GETARG_B(i);
  404. setobj(cl->upvals[b]->v, ra);
  405. break;
  406. }
  407. case OP_SETTABLE: {
  408. luaV_settable(L, ra, RKB(i), RKC(i));
  409. break;
  410. }
  411. case OP_NEWTABLE: {
  412. int b = GETARG_B(i);
  413. if (b > 0) b = twoto(b-1);
  414. sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
  415. luaV_checkGC(L, ra+1);
  416. break;
  417. }
  418. case OP_SELF: {
  419. StkId rb = RB(i);
  420. TObject *rc = RKC(i);
  421. runtime_check(L, ttisstring(rc));
  422. setobjs2s(ra+1, rb);
  423. if (ttistable(rb)) {
  424. const TObject *v = luaH_getstr(hvalue(rb), tsvalue(rc));
  425. if (!ttisnil(v)) { setobj2s(ra, v); }
  426. else
  427. setobj2s(RA(i), luaV_index(L, rb, rc, 0));
  428. }
  429. else
  430. setobj2s(RA(i), luaV_getnotable(L, rb, rc, 0));
  431. break;
  432. }
  433. case OP_ADD: {
  434. TObject *rb = RKB(i);
  435. TObject *rc = RKC(i);
  436. if (ttisnumber(rb) && ttisnumber(rc)) {
  437. setnvalue(ra, nvalue(rb) + nvalue(rc));
  438. }
  439. else
  440. Arith(L, ra, rb, rc, TM_ADD);
  441. break;
  442. }
  443. case OP_SUB: {
  444. TObject *rb = RKB(i);
  445. TObject *rc = RKC(i);
  446. if (ttisnumber(rb) && ttisnumber(rc)) {
  447. setnvalue(ra, nvalue(rb) - nvalue(rc));
  448. }
  449. else
  450. Arith(L, ra, rb, rc, TM_SUB);
  451. break;
  452. }
  453. case OP_MUL: {
  454. TObject *rb = RKB(i);
  455. TObject *rc = RKC(i);
  456. if (ttisnumber(rb) && ttisnumber(rc)) {
  457. setnvalue(ra, nvalue(rb) * nvalue(rc));
  458. }
  459. else
  460. Arith(L, ra, rb, rc, TM_MUL);
  461. break;
  462. }
  463. case OP_DIV: {
  464. TObject *rb = RKB(i);
  465. TObject *rc = RKC(i);
  466. if (ttisnumber(rb) && ttisnumber(rc)) {
  467. setnvalue(ra, nvalue(rb) / nvalue(rc));
  468. }
  469. else
  470. Arith(L, ra, rb, rc, TM_DIV);
  471. break;
  472. }
  473. case OP_POW: {
  474. Arith(L, ra, RKB(i), RKC(i), TM_POW);
  475. break;
  476. }
  477. case OP_UNM: {
  478. const TObject *rb = RB(i);
  479. TObject temp;
  480. if (tonumber(rb, &temp)) {
  481. setnvalue(ra, -nvalue(rb));
  482. }
  483. else {
  484. setnilvalue(&temp);
  485. if (!call_binTM(L, RB(i), &temp, ra, TM_UNM))
  486. luaG_aritherror(L, RB(i), &temp);
  487. }
  488. break;
  489. }
  490. case OP_NOT: {
  491. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  492. setbvalue(ra, res);
  493. break;
  494. }
  495. case OP_CONCAT: {
  496. int b = GETARG_B(i);
  497. int c = GETARG_C(i);
  498. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */
  499. setobjs2s(RA(i), base+b);
  500. luaV_checkGC(L, base+c+1);
  501. break;
  502. }
  503. case OP_JMP: {
  504. dojump(pc, GETARG_sBx(i));
  505. break;
  506. }
  507. case OP_EQ: {
  508. if (equalobj(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++;
  509. else dojump(pc, GETARG_sBx(*pc) + 1);
  510. break;
  511. }
  512. case OP_LT: {
  513. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++;
  514. else dojump(pc, GETARG_sBx(*pc) + 1);
  515. break;
  516. }
  517. case OP_LE: {
  518. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++;
  519. else dojump(pc, GETARG_sBx(*pc) + 1);
  520. break;
  521. }
  522. case OP_TEST: {
  523. TObject *rb = RB(i);
  524. if (l_isfalse(rb) == GETARG_C(i)) pc++;
  525. else {
  526. setobjs2s(ra, rb);
  527. dojump(pc, GETARG_sBx(*pc) + 1);
  528. }
  529. break;
  530. }
  531. case OP_CALL:
  532. case OP_TAILCALL: {
  533. StkId firstResult;
  534. int b = GETARG_B(i);
  535. int nresults;
  536. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  537. nresults = GETARG_C(i) - 1;
  538. firstResult = luaD_precall(L, ra);
  539. if (firstResult) {
  540. if (firstResult > L->top) { /* yield? */
  541. (L->ci - 1)->u.l.savedpc = pc;
  542. (L->ci - 1)->state = CI_SAVEDPC;
  543. L->ci->state |= CI_YIELD;
  544. return NULL;
  545. }
  546. /* it was a C function (`precall' called it); adjust results */
  547. luaD_poscall(L, nresults, firstResult);
  548. if (nresults >= 0) L->top = L->ci->top;
  549. }
  550. else { /* it is a Lua function */
  551. if (GET_OPCODE(i) == OP_CALL) { /* regular call? */
  552. (L->ci-1)->u.l.savedpc = pc; /* save `pc' to return later */
  553. (L->ci-1)->state = (CI_SAVEDPC | CI_CALLING);
  554. }
  555. else { /* tail call: put new frame in place of previous one */
  556. int aux;
  557. StkId ra1 = RA(i); /* `luaD_precall' may change the stack */
  558. if (L->openupval) luaF_close(L, base);
  559. for (aux = 0; ra1+aux < L->top; aux++) /* move frame down */
  560. setobjs2s(base+aux-1, ra1+aux);
  561. (L->ci - 1)->top = L->top = base+aux; /* correct top */
  562. lua_assert(L->ci->state & CI_SAVEDPC);
  563. (L->ci - 1)->u.l.savedpc = L->ci->u.l.savedpc;
  564. (L->ci - 1)->state = CI_SAVEDPC;
  565. L->ci--; /* remove previous frame */
  566. }
  567. goto callentry;
  568. }
  569. break;
  570. }
  571. case OP_RETURN: {
  572. CallInfo *ci = L->ci - 1;
  573. int b = GETARG_B(i);
  574. if (b != 0) L->top = ra+b-1;
  575. lua_assert(L->ci->state & CI_HASFRAME);
  576. if (L->openupval) luaF_close(L, base);
  577. L->ci->state = CI_SAVEDPC; /* deactivate current function */
  578. L->ci->u.l.savedpc = pc;
  579. /* previous function was running `here'? */
  580. if (!(ci->state & CI_CALLING))
  581. return ra; /* no: return */
  582. else { /* yes: continue its execution (go through) */
  583. int nresults;
  584. lua_assert(ttisfunction(ci->base - 1));
  585. lua_assert(ci->state & CI_SAVEDPC);
  586. lua_assert(GET_OPCODE(*(ci->u.l.savedpc - 1)) == OP_CALL);
  587. nresults = GETARG_C(*(ci->u.l.savedpc - 1)) - 1;
  588. luaD_poscall(L, nresults, ra);
  589. if (nresults >= 0) L->top = L->ci->top;
  590. goto retentry;
  591. }
  592. }
  593. case OP_FORLOOP: {
  594. lua_Number step, index, limit;
  595. const TObject *plimit = ra+1;
  596. const TObject *pstep = ra+2;
  597. if (!ttisnumber(ra))
  598. luaG_runerror(L, "`for' initial value must be a number");
  599. if (!tonumber(plimit, ra+1))
  600. luaG_runerror(L, "`for' limit must be a number");
  601. if (!tonumber(pstep, ra+2))
  602. luaG_runerror(L, "`for' step must be a number");
  603. step = nvalue(pstep);
  604. index = nvalue(ra) + step; /* increment index */
  605. limit = nvalue(plimit);
  606. if (step > 0 ? index <= limit : index >= limit) {
  607. dojump(pc, GETARG_sBx(i)); /* jump back */
  608. chgnvalue(ra, index); /* update index */
  609. }
  610. break;
  611. }
  612. case OP_TFORLOOP: {
  613. setobjs2s(ra+4, ra+2);
  614. setobjs2s(ra+3, ra+1);
  615. setobjs2s(ra+2, ra);
  616. L->top = ra+5;
  617. luaD_call(L, ra+2, GETARG_C(i) + 1);
  618. L->top = L->ci->top;
  619. if (ttisnil(RA(i)+2)) pc++; /* skip jump (break loop) */
  620. else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
  621. break;
  622. }
  623. case OP_TFORPREP: { /* for compatibility only */
  624. if (ttistable(ra)) {
  625. setobjs2s(ra+1, ra);
  626. setobj2s(ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
  627. }
  628. dojump(pc, GETARG_sBx(i));
  629. break;
  630. }
  631. case OP_SETLIST:
  632. case OP_SETLISTO: {
  633. int bc;
  634. int n;
  635. Table *h;
  636. runtime_check(L, ttistable(ra));
  637. h = hvalue(ra);
  638. bc = GETARG_Bx(i);
  639. if (GET_OPCODE(i) == OP_SETLIST)
  640. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  641. else {
  642. n = L->top - ra - 1;
  643. L->top = L->ci->top;
  644. }
  645. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  646. for (; n > 0; n--)
  647. setobj2t(luaH_setnum(L, h, bc+n), ra+n);
  648. break;
  649. }
  650. case OP_CLOSE: {
  651. luaF_close(L, ra);
  652. break;
  653. }
  654. case OP_CLOSURE: {
  655. Proto *p;
  656. Closure *ncl;
  657. int nup, j;
  658. p = cl->p->p[GETARG_Bx(i)];
  659. nup = p->nupvalues;
  660. ncl = luaF_newLclosure(L, nup, &cl->g);
  661. ncl->l.p = p;
  662. for (j=0; j<nup; j++, pc++) {
  663. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  664. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  665. else {
  666. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  667. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  668. }
  669. }
  670. setclvalue(ra, ncl);
  671. luaV_checkGC(L, L->top);
  672. break;
  673. }
  674. }
  675. }
  676. }