lvm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. ** $Id: lvm.c,v 1.251 2002/08/07 19:22:39 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. setobj(L->top, f); /* push function */
  83. setobj(L->top+1, p1); /* 1st argument */
  84. setobj(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. setobj(L->top, f); /* push function */
  93. setobj(L->top+1, p1); /* 1st argument */
  94. setobj(L->top+2, p2); /* 2nd argument */
  95. setobj(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_GETTABLE);
  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. setobj(oldval, val);
  151. return;
  152. }
  153. /* else will try the tag method */
  154. }
  155. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)))
  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. TObject *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. setobj(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_TSTRING: return tsvalue(t1) == tsvalue(t2);
  228. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  229. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  230. case LUA_TFUNCTION: return clvalue(t1) == clvalue(t2);
  231. case LUA_TUSERDATA:
  232. if (uvalue(t1) == uvalue(t2)) return 1;
  233. else if ((tm = fasttm(L, uvalue(t1)->uv.metatable, TM_EQ)) == NULL &&
  234. (tm = fasttm(L, uvalue(t2)->uv.metatable, TM_EQ)) == NULL)
  235. return 0; /* no TM */
  236. else break; /* will try TM */
  237. case LUA_TTABLE:
  238. if (hvalue(t1) == hvalue(t2)) return 1;
  239. else if ((tm = fasttm(L, hvalue(t1)->metatable, TM_EQ)) == NULL &&
  240. (tm = fasttm(L, hvalue(t2)->metatable, TM_EQ)) == NULL)
  241. return 0; /* no TM */
  242. else break; /* will try TM */
  243. }
  244. callTMres(L, tm, t1, t2); /* call TM */
  245. return !l_isfalse(L->top);
  246. }
  247. void luaV_concat (lua_State *L, int total, int last) {
  248. do {
  249. StkId top = L->ci->base + last + 1;
  250. int n = 2; /* number of elements handled in this pass (at least 2) */
  251. if (!tostring(L, top-2) || !tostring(L, top-1)) {
  252. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  253. luaG_concaterror(L, top-2, top-1);
  254. } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
  255. /* at least two string values; get as many as possible */
  256. lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
  257. cast(lu_mem, tsvalue(top-2)->tsv.len);
  258. char *buffer;
  259. int i;
  260. while (n < total && tostring(L, top-n-1)) { /* collect total length */
  261. tl += tsvalue(top-n-1)->tsv.len;
  262. n++;
  263. }
  264. if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
  265. buffer = luaO_openspace(L, tl, char);
  266. tl = 0;
  267. for (i=n; i>0; i--) { /* concat all strings */
  268. size_t l = tsvalue(top-i)->tsv.len;
  269. memcpy(buffer+tl, svalue(top-i), l);
  270. tl += l;
  271. }
  272. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  273. }
  274. total -= n-1; /* got `n' strings to create 1 new */
  275. last -= n-1;
  276. } while (total > 1); /* repeat until only 1 result left */
  277. }
  278. static void Arith (lua_State *L, StkId ra, StkId rb, StkId rc, TMS op) {
  279. TObject tempb, tempc;
  280. const TObject *b, *c;
  281. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  282. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  283. switch (op) {
  284. case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break;
  285. case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break;
  286. case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break;
  287. case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break;
  288. case TM_POW: {
  289. const TObject *f = luaH_getstr(hvalue(registry(L)),
  290. G(L)->tmname[TM_POW]);
  291. ptrdiff_t res = savestack(L, ra);
  292. if (!ttisfunction(f))
  293. luaG_runerror(L, "`pow' (for `^' operator) is not a function");
  294. callTMres(L, f, b, c);
  295. ra = restorestack(L, res); /* previous call may change stack */
  296. setobj(ra, L->top);
  297. break;
  298. }
  299. default: lua_assert(0); break;
  300. }
  301. }
  302. else if (!call_binTM(L, rb, rc, ra, op))
  303. luaG_aritherror(L, rb, rc);
  304. }
  305. /*
  306. ** some macros for common tasks in `luaV_execute'
  307. */
  308. #define runtime_check(L, c) { if (!(c)) return 0; }
  309. #define RA(i) (base+GETARG_A(i))
  310. #define RB(i) (base+GETARG_B(i))
  311. #define RC(i) (base+GETARG_C(i))
  312. #define RKC(i) ((GETARG_C(i) < MAXSTACK) ? \
  313. base+GETARG_C(i) : \
  314. k+GETARG_C(i)-MAXSTACK)
  315. #define KBx(i) (k+GETARG_Bx(i))
  316. #define dojump(pc, i) ((pc) += (i))
  317. StkId luaV_execute (lua_State *L) {
  318. StkId base;
  319. LClosure *cl;
  320. TObject *k;
  321. const Instruction *pc;
  322. callentry: /* entry point when calling new functions */
  323. L->ci->u.l.pb = &base;
  324. L->ci->u.l.pc = &pc;
  325. if (L->hookmask & LUA_MASKCALL)
  326. luaD_callhook(L, LUA_HOOKCALL, -1);
  327. retentry: /* entry point when returning to old functions */
  328. lua_assert(L->ci->state & CI_SAVEDPC);
  329. L->ci->state = CI_HASFRAME; /* activate frame */
  330. pc = L->ci->u.l.savedpc;
  331. base = L->ci->base;
  332. cl = &clvalue(base - 1)->l;
  333. k = cl->p->k;
  334. /* main loop of interpreter */
  335. for (;;) {
  336. const Instruction i = *pc++;
  337. StkId ra;
  338. if (L->hookmask >= LUA_MASKLINE &&
  339. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE))
  340. traceexec(L);
  341. /* warning!! several calls may realloc the stack and invalidate `ra' */
  342. ra = RA(i);
  343. lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
  344. lua_assert(L->top == L->ci->top ||
  345. GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  346. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  347. switch (GET_OPCODE(i)) {
  348. case OP_MOVE: {
  349. setobj(ra, RB(i));
  350. break;
  351. }
  352. case OP_LOADK: {
  353. setobj(ra, KBx(i));
  354. break;
  355. }
  356. case OP_LOADBOOL: {
  357. setbvalue(ra, GETARG_B(i));
  358. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  359. break;
  360. }
  361. case OP_LOADNIL: {
  362. TObject *rb = RB(i);
  363. do {
  364. setnilvalue(rb--);
  365. } while (rb >= ra);
  366. break;
  367. }
  368. case OP_GETUPVAL: {
  369. int b = GETARG_B(i);
  370. setobj(ra, cl->upvals[b]->v);
  371. break;
  372. }
  373. case OP_GETGLOBAL: {
  374. StkId rb = KBx(i);
  375. const TObject *v;
  376. lua_assert(ttisstring(rb) && ttistable(&cl->g));
  377. v = luaH_getstr(hvalue(&cl->g), tsvalue(rb));
  378. if (!ttisnil(v)) { setobj(ra, v); }
  379. else
  380. setobj(RA(i), luaV_index(L, &cl->g, rb, 0));
  381. break;
  382. }
  383. case OP_GETTABLE: {
  384. StkId rb = RB(i);
  385. TObject *rc = RKC(i);
  386. if (ttistable(rb)) {
  387. const TObject *v = luaH_get(hvalue(rb), rc);
  388. if (!ttisnil(v)) { setobj(ra, v); }
  389. else
  390. setobj(RA(i), luaV_index(L, rb, rc, 0));
  391. }
  392. else
  393. setobj(RA(i), luaV_getnotable(L, rb, rc, 0));
  394. break;
  395. }
  396. case OP_SETGLOBAL: {
  397. lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
  398. luaV_settable(L, &cl->g, KBx(i), ra);
  399. break;
  400. }
  401. case OP_SETUPVAL: {
  402. int b = GETARG_B(i);
  403. setobj(cl->upvals[b]->v, ra);
  404. break;
  405. }
  406. case OP_SETTABLE: {
  407. luaV_settable(L, RB(i), RKC(i), ra);
  408. break;
  409. }
  410. case OP_NEWTABLE: {
  411. int b = GETARG_B(i);
  412. if (b > 0) b = twoto(b-1);
  413. sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
  414. luaV_checkGC(L, ra+1);
  415. break;
  416. }
  417. case OP_SELF: {
  418. StkId rb = RB(i);
  419. TObject *rc = RKC(i);
  420. runtime_check(L, ttisstring(rc));
  421. setobj(ra+1, rb);
  422. if (ttistable(rb)) {
  423. const TObject *v = luaH_getstr(hvalue(rb), tsvalue(rc));
  424. if (!ttisnil(v)) { setobj(ra, v); }
  425. else
  426. setobj(RA(i), luaV_index(L, rb, rc, 0));
  427. }
  428. else
  429. setobj(RA(i), luaV_getnotable(L, rb, rc, 0));
  430. break;
  431. }
  432. case OP_ADD: {
  433. StkId rb = RB(i);
  434. StkId rc = RKC(i);
  435. if (ttisnumber(rb) && ttisnumber(rc)) {
  436. setnvalue(ra, nvalue(rb) + nvalue(rc));
  437. }
  438. else
  439. Arith(L, ra, rb, rc, TM_ADD);
  440. break;
  441. }
  442. case OP_SUB: {
  443. StkId rb = RB(i);
  444. StkId rc = RKC(i);
  445. if (ttisnumber(rb) && ttisnumber(rc)) {
  446. setnvalue(ra, nvalue(rb) - nvalue(rc));
  447. }
  448. else
  449. Arith(L, ra, rb, rc, TM_SUB);
  450. break;
  451. }
  452. case OP_MUL: {
  453. StkId rb = RB(i);
  454. StkId rc = RKC(i);
  455. if (ttisnumber(rb) && ttisnumber(rc)) {
  456. setnvalue(ra, nvalue(rb) * nvalue(rc));
  457. }
  458. else
  459. Arith(L, ra, rb, rc, TM_MUL);
  460. break;
  461. }
  462. case OP_DIV: {
  463. StkId rb = RB(i);
  464. StkId rc = RKC(i);
  465. if (ttisnumber(rb) && ttisnumber(rc)) {
  466. setnvalue(ra, nvalue(rb) / nvalue(rc));
  467. }
  468. else
  469. Arith(L, ra, rb, rc, TM_DIV);
  470. break;
  471. }
  472. case OP_POW: {
  473. Arith(L, ra, RB(i), RKC(i), TM_POW);
  474. break;
  475. }
  476. case OP_UNM: {
  477. const TObject *rb = RB(i);
  478. TObject temp;
  479. if (tonumber(rb, &temp)) {
  480. setnvalue(ra, -nvalue(rb));
  481. }
  482. else {
  483. setnilvalue(&temp);
  484. if (!call_binTM(L, RB(i), &temp, ra, TM_UNM))
  485. luaG_aritherror(L, RB(i), &temp);
  486. }
  487. break;
  488. }
  489. case OP_NOT: {
  490. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  491. setbvalue(ra, res);
  492. break;
  493. }
  494. case OP_CONCAT: {
  495. int b = GETARG_B(i);
  496. int c = GETARG_C(i);
  497. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */
  498. setobj(RA(i), base+b);
  499. luaV_checkGC(L, base+c+1);
  500. break;
  501. }
  502. case OP_JMP: {
  503. dojump(pc, GETARG_sBx(i));
  504. break;
  505. }
  506. case OP_EQ: { /* skip next instruction if test fails */
  507. if (equalobj(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  508. else dojump(pc, GETARG_sBx(*pc) + 1);
  509. break;
  510. }
  511. case OP_LT: {
  512. if (luaV_lessthan(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  513. else dojump(pc, GETARG_sBx(*pc) + 1);
  514. break;
  515. }
  516. case OP_LE: {
  517. if (luaV_lessequal(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  518. else dojump(pc, GETARG_sBx(*pc) + 1);
  519. break;
  520. }
  521. case OP_GT: {
  522. if (luaV_lessthan(L, RKC(i), ra) != GETARG_B(i)) pc++;
  523. else dojump(pc, GETARG_sBx(*pc) + 1);
  524. break;
  525. }
  526. case OP_GE: {
  527. if (luaV_lessequal(L, RKC(i), ra) != GETARG_B(i)) pc++;
  528. else dojump(pc, GETARG_sBx(*pc) + 1);
  529. break;
  530. }
  531. case OP_TEST: {
  532. StkId rc = RKC(i);
  533. if (l_isfalse(rc) == GETARG_B(i)) pc++;
  534. else {
  535. setobj(ra, rc);
  536. dojump(pc, GETARG_sBx(*pc) + 1);
  537. }
  538. break;
  539. }
  540. case OP_CALL:
  541. case OP_TAILCALL: {
  542. StkId firstResult;
  543. int b = GETARG_B(i);
  544. int nresults;
  545. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  546. nresults = GETARG_C(i) - 1;
  547. firstResult = luaD_precall(L, ra);
  548. if (firstResult) {
  549. if (firstResult > L->top) { /* yield? */
  550. (L->ci - 1)->u.l.savedpc = pc;
  551. (L->ci - 1)->state = CI_SAVEDPC;
  552. return NULL;
  553. }
  554. /* it was a C function (`precall' called it); adjust results */
  555. luaD_poscall(L, nresults, firstResult);
  556. if (nresults >= 0) L->top = L->ci->top;
  557. }
  558. else { /* it is a Lua function */
  559. if (GET_OPCODE(i) == OP_CALL) { /* regular call? */
  560. (L->ci-1)->u.l.savedpc = pc; /* save `pc' to return later */
  561. (L->ci-1)->state = (CI_SAVEDPC | CI_CALLING);
  562. }
  563. else { /* tail call: put new frame in place of previous one */
  564. int aux;
  565. StkId ra1 = RA(i); /* `luaD_precall' may change the stack */
  566. if (L->openupval) luaF_close(L, base);
  567. for (aux = 0; ra1+aux < L->top; aux++) /* move frame down */
  568. setobj(base+aux-1, ra1+aux);
  569. (L->ci - 1)->top = L->top = base+aux; /* correct top */
  570. lua_assert(L->ci->state & CI_SAVEDPC);
  571. (L->ci - 1)->u.l.savedpc = L->ci->u.l.savedpc;
  572. (L->ci - 1)->state = CI_SAVEDPC;
  573. L->ci--; /* remove previous frame */
  574. }
  575. goto callentry;
  576. }
  577. break;
  578. }
  579. case OP_RETURN: {
  580. CallInfo *ci = L->ci - 1;
  581. int b = GETARG_B(i);
  582. if (b != 0) L->top = ra+b-1;
  583. lua_assert(L->ci->state & CI_HASFRAME);
  584. if (L->openupval) luaF_close(L, base);
  585. L->ci->state = CI_SAVEDPC; /* deactivate current function */
  586. L->ci->u.l.savedpc = pc;
  587. /* previous function was running `here'? */
  588. if (!(ci->state & CI_CALLING))
  589. return ra; /* no: return */
  590. else { /* yes: continue its execution (go through) */
  591. int nresults;
  592. lua_assert(ttisfunction(ci->base - 1));
  593. lua_assert(ci->state & CI_SAVEDPC);
  594. lua_assert(GET_OPCODE(*(ci->u.l.savedpc - 1)) == OP_CALL);
  595. nresults = GETARG_C(*(ci->u.l.savedpc - 1)) - 1;
  596. luaD_poscall(L, nresults, ra);
  597. if (nresults >= 0) L->top = L->ci->top;
  598. goto retentry;
  599. }
  600. }
  601. case OP_FORLOOP: {
  602. lua_Number step, index, limit;
  603. const TObject *plimit = ra+1;
  604. const TObject *pstep = ra+2;
  605. if (!ttisnumber(ra))
  606. luaG_runerror(L, "`for' initial value must be a number");
  607. if (!tonumber(plimit, ra+1))
  608. luaG_runerror(L, "`for' limit must be a number");
  609. if (!tonumber(pstep, ra+2))
  610. luaG_runerror(L, "`for' step must be a number");
  611. step = nvalue(pstep);
  612. index = nvalue(ra) + step; /* increment index */
  613. limit = nvalue(plimit);
  614. if (step > 0 ? index <= limit : index >= limit) {
  615. dojump(pc, GETARG_sBx(i)); /* jump back */
  616. chgnvalue(ra, index); /* update index */
  617. }
  618. break;
  619. }
  620. case OP_TFORLOOP: {
  621. setobj(ra+4, ra+2);
  622. setobj(ra+3, ra+1);
  623. setobj(ra+2, ra);
  624. L->top = ra+5;
  625. luaD_call(L, ra+2, GETARG_C(i) + 1);
  626. L->top = L->ci->top;
  627. if (ttisnil(RA(i)+2)) pc++; /* skip jump (break loop) */
  628. else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
  629. break;
  630. }
  631. case OP_TFORPREP: { /* for compatibility only */
  632. if (ttistable(ra)) {
  633. setobj(ra+1, ra);
  634. setobj(ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
  635. }
  636. dojump(pc, GETARG_sBx(i));
  637. break;
  638. }
  639. case OP_SETLIST:
  640. case OP_SETLISTO: {
  641. int bc;
  642. int n;
  643. Table *h;
  644. runtime_check(L, ttistable(ra));
  645. h = hvalue(ra);
  646. bc = GETARG_Bx(i);
  647. if (GET_OPCODE(i) == OP_SETLIST)
  648. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  649. else {
  650. n = L->top - ra - 1;
  651. L->top = L->ci->top;
  652. }
  653. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  654. for (; n > 0; n--)
  655. setobj(luaH_setnum(L, h, bc+n), ra+n);
  656. break;
  657. }
  658. case OP_CLOSE: {
  659. luaF_close(L, ra);
  660. break;
  661. }
  662. case OP_CLOSURE: {
  663. Proto *p;
  664. Closure *ncl;
  665. int nup, j;
  666. p = cl->p->p[GETARG_Bx(i)];
  667. nup = p->nupvalues;
  668. ncl = luaF_newLclosure(L, nup, &cl->g);
  669. ncl->l.p = p;
  670. for (j=0; j<nup; j++, pc++) {
  671. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  672. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  673. else {
  674. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  675. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  676. }
  677. }
  678. setclvalue(ra, ncl);
  679. luaV_checkGC(L, L->top);
  680. break;
  681. }
  682. }
  683. }
  684. }