lvm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. ** $Id: lvm.c,v 1.248 2002/07/17 16:25:13 roberto Exp $
  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. Instruction const *luaV_callingmark = NULL;
  22. /* function to convert a lua_Number to a string */
  23. #ifndef lua_number2str
  24. #include <stdio.h>
  25. #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  26. #endif
  27. /* limit for table tag-method chains (to avoid loops) */
  28. #define MAXTAGLOOP 100
  29. static void luaV_checkGC (lua_State *L, StkId top) {
  30. if (G(L)->nblocks >= G(L)->GCthreshold) {
  31. L->top = top; /* limit for active registers */
  32. luaC_collectgarbage(L);
  33. L->top = L->ci->top; /* restore old top position */
  34. }
  35. }
  36. const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
  37. lua_Number num;
  38. if (ttisnumber(obj)) return obj;
  39. if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  40. setnvalue(n, num);
  41. return n;
  42. }
  43. else
  44. return NULL;
  45. }
  46. int luaV_tostring (lua_State *L, TObject *obj) {
  47. if (!ttisnumber(obj))
  48. return 0;
  49. else {
  50. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  51. lua_number2str(s, nvalue(obj));
  52. setsvalue(obj, luaS_new(L, s));
  53. return 1;
  54. }
  55. }
  56. static void traceexec (lua_State *L) {
  57. int mask = L->hookmask;
  58. if (mask > LUA_MASKLINE) { /* instruction hook set? */
  59. if (L->hookcount == 0) {
  60. luaD_callhook(L, LUA_HOOKCOUNT, -1);
  61. resethookcount(L);
  62. return;
  63. }
  64. }
  65. if (mask & LUA_MASKLINE) {
  66. CallInfo *ci = L->ci;
  67. Proto *p = ci_func(ci)->l.p;
  68. int newline = getline(p, pcRel(*ci->pc, p));
  69. if (pcRel(*ci->pc, p) == 0) /* tracing may be starting now? */
  70. ci->u.l.savedpc = *ci->pc; /* initialize `savedpc' */
  71. /* calls linehook when enters a new line or jumps back (loop) */
  72. if (*ci->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->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->pc = &pc;
  325. pc = L->ci->u.l.savedpc;
  326. if (L->hookmask & LUA_MASKCALL)
  327. luaD_callhook(L, LUA_HOOKCALL, -1);
  328. retentry: /* entry point when returning to old functions */
  329. base = L->ci->base;
  330. cl = &clvalue(base - 1)->l;
  331. k = cl->p->k;
  332. /* main loop of interpreter */
  333. for (;;) {
  334. const Instruction i = *pc++;
  335. const StkId ra = RA(i);
  336. if (L->hookmask >= LUA_MASKLINE &&
  337. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE))
  338. traceexec(L);
  339. lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
  340. lua_assert(L->top == L->ci->top ||
  341. GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  342. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  343. switch (GET_OPCODE(i)) {
  344. case OP_MOVE: {
  345. setobj(ra, RB(i));
  346. break;
  347. }
  348. case OP_LOADK: {
  349. setobj(ra, KBx(i));
  350. break;
  351. }
  352. case OP_LOADBOOL: {
  353. setbvalue(ra, GETARG_B(i));
  354. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  355. break;
  356. }
  357. case OP_LOADNIL: {
  358. TObject *rb = RB(i);
  359. do {
  360. setnilvalue(rb--);
  361. } while (rb >= ra);
  362. break;
  363. }
  364. case OP_GETUPVAL: {
  365. int b = GETARG_B(i);
  366. setobj(ra, cl->upvals[b]->v);
  367. break;
  368. }
  369. case OP_GETGLOBAL: {
  370. StkId rb = KBx(i);
  371. const TObject *v;
  372. lua_assert(ttisstring(rb) && ttistable(&cl->g));
  373. v = luaH_getstr(hvalue(&cl->g), tsvalue(rb));
  374. if (!ttisnil(v)) { setobj(ra, v); }
  375. else
  376. setobj(RA(i), luaV_index(L, &cl->g, rb, 0));
  377. break;
  378. }
  379. case OP_GETTABLE: {
  380. StkId rb = RB(i);
  381. TObject *rc = RKC(i);
  382. if (ttistable(rb)) {
  383. const TObject *v = luaH_get(hvalue(rb), rc);
  384. if (!ttisnil(v)) { setobj(ra, v); }
  385. else
  386. setobj(RA(i), luaV_index(L, rb, rc, 0));
  387. }
  388. else
  389. setobj(RA(i), luaV_getnotable(L, rb, rc, 0));
  390. break;
  391. }
  392. case OP_SETGLOBAL: {
  393. lua_assert(ttisstring(KBx(i)) && ttistable(&cl->g));
  394. luaV_settable(L, &cl->g, KBx(i), ra);
  395. break;
  396. }
  397. case OP_SETUPVAL: {
  398. int b = GETARG_B(i);
  399. setobj(cl->upvals[b]->v, ra);
  400. break;
  401. }
  402. case OP_SETTABLE: {
  403. luaV_settable(L, RB(i), RKC(i), ra);
  404. break;
  405. }
  406. case OP_NEWTABLE: {
  407. int b = GETARG_B(i);
  408. if (b > 0) b = twoto(b-1);
  409. sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
  410. luaV_checkGC(L, ra+1);
  411. break;
  412. }
  413. case OP_SELF: {
  414. StkId rb = RB(i);
  415. TObject *rc = RKC(i);
  416. runtime_check(L, ttisstring(rc));
  417. setobj(ra+1, rb);
  418. if (ttistable(rb)) {
  419. const TObject *v = luaH_getstr(hvalue(rb), tsvalue(rc));
  420. if (!ttisnil(v)) { setobj(ra, v); }
  421. else
  422. setobj(RA(i), luaV_index(L, rb, rc, 0));
  423. }
  424. else
  425. setobj(RA(i), luaV_getnotable(L, rb, rc, 0));
  426. break;
  427. }
  428. case OP_ADD: {
  429. StkId rb = RB(i);
  430. StkId rc = RKC(i);
  431. if (ttisnumber(rb) && ttisnumber(rc)) {
  432. setnvalue(ra, nvalue(rb) + nvalue(rc));
  433. }
  434. else
  435. Arith(L, ra, rb, rc, TM_ADD);
  436. break;
  437. }
  438. case OP_SUB: {
  439. StkId rb = RB(i);
  440. StkId rc = RKC(i);
  441. if (ttisnumber(rb) && ttisnumber(rc)) {
  442. setnvalue(ra, nvalue(rb) - nvalue(rc));
  443. }
  444. else
  445. Arith(L, ra, rb, rc, TM_SUB);
  446. break;
  447. }
  448. case OP_MUL: {
  449. StkId rb = RB(i);
  450. StkId rc = RKC(i);
  451. if (ttisnumber(rb) && ttisnumber(rc)) {
  452. setnvalue(ra, nvalue(rb) * nvalue(rc));
  453. }
  454. else
  455. Arith(L, ra, rb, rc, TM_MUL);
  456. break;
  457. }
  458. case OP_DIV: {
  459. StkId rb = RB(i);
  460. StkId rc = RKC(i);
  461. if (ttisnumber(rb) && ttisnumber(rc)) {
  462. setnvalue(ra, nvalue(rb) / nvalue(rc));
  463. }
  464. else
  465. Arith(L, ra, rb, rc, TM_DIV);
  466. break;
  467. }
  468. case OP_POW: {
  469. Arith(L, ra, RB(i), RKC(i), TM_POW);
  470. break;
  471. }
  472. case OP_UNM: {
  473. const TObject *rb = RB(i);
  474. TObject temp;
  475. if (tonumber(rb, &temp)) {
  476. setnvalue(ra, -nvalue(rb));
  477. }
  478. else {
  479. setnilvalue(&temp);
  480. if (!call_binTM(L, RB(i), &temp, ra, TM_UNM))
  481. luaG_aritherror(L, RB(i), &temp);
  482. }
  483. break;
  484. }
  485. case OP_NOT: {
  486. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  487. setbvalue(ra, res);
  488. break;
  489. }
  490. case OP_CONCAT: {
  491. int b = GETARG_B(i);
  492. int c = GETARG_C(i);
  493. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */
  494. setobj(base+GETARG_A(i), base+b);
  495. luaV_checkGC(L, base+c+1);
  496. break;
  497. }
  498. case OP_JMP: {
  499. dojump(pc, GETARG_sBx(i));
  500. break;
  501. }
  502. case OP_EQ: { /* skip next instruction if test fails */
  503. if (equalobj(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  504. else dojump(pc, GETARG_sBx(*pc) + 1);
  505. break;
  506. }
  507. case OP_LT: {
  508. if (luaV_lessthan(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  509. else dojump(pc, GETARG_sBx(*pc) + 1);
  510. break;
  511. }
  512. case OP_LE: {
  513. if (luaV_lessequal(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  514. else dojump(pc, GETARG_sBx(*pc) + 1);
  515. break;
  516. }
  517. case OP_GT: {
  518. if (luaV_lessthan(L, RKC(i), ra) != GETARG_B(i)) pc++;
  519. else dojump(pc, GETARG_sBx(*pc) + 1);
  520. break;
  521. }
  522. case OP_GE: {
  523. if (luaV_lessequal(L, RKC(i), ra) != GETARG_B(i)) pc++;
  524. else dojump(pc, GETARG_sBx(*pc) + 1);
  525. break;
  526. }
  527. case OP_TEST: {
  528. StkId rc = RKC(i);
  529. if (l_isfalse(rc) == GETARG_B(i)) pc++;
  530. else {
  531. setobj(ra, rc);
  532. dojump(pc, GETARG_sBx(*pc) + 1);
  533. }
  534. break;
  535. }
  536. case OP_CALL:
  537. case OP_TAILCALL: {
  538. StkId firstResult;
  539. int b = GETARG_B(i);
  540. int nresults;
  541. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  542. nresults = GETARG_C(i) - 1;
  543. firstResult = luaD_precall(L, ra);
  544. if (firstResult) {
  545. if (firstResult > L->top) { /* yield? */
  546. (L->ci - 1)->u.l.savedpc = pc;
  547. (L->ci - 1)->pc = &luaV_callingmark;
  548. return NULL;
  549. }
  550. /* it was a C function (`precall' called it); adjust results */
  551. luaD_poscall(L, nresults, firstResult);
  552. if (nresults >= 0) L->top = L->ci->top;
  553. }
  554. else { /* it is a Lua function */
  555. if (GET_OPCODE(i) == OP_CALL) { /* regular call? */
  556. (L->ci-1)->u.l.savedpc = pc; /* save `pc' to return later */
  557. (L->ci-1)->pc = &luaV_callingmark; /* function is calling */
  558. }
  559. else { /* tail call: put new frame in place of previous one */
  560. int aux;
  561. StkId ra1 = RA(i); /* `luaD_precall' may change the stack */
  562. if (L->openupval) luaF_close(L, base);
  563. for (aux = 0; ra1+aux < L->top; aux++) /* move frame down */
  564. setobj(base+aux-1, ra1+aux);
  565. (L->ci - 1)->top = L->top = base+aux; /* correct top */
  566. (L->ci - 1)->u.l.savedpc = L->ci->u.l.savedpc;
  567. L->ci--; /* remove previous frame */
  568. }
  569. goto callentry;
  570. }
  571. break;
  572. }
  573. case OP_RETURN: {
  574. CallInfo *ci = L->ci - 1;
  575. int b = GETARG_B(i);
  576. if (b != 0) L->top = ra+b-1;
  577. lua_assert(L->ci->pc == &pc);
  578. if (L->openupval) luaF_close(L, base);
  579. /* previous function was running `here'? */
  580. if (ci->pc != &luaV_callingmark)
  581. return ra; /* no: return */
  582. else { /* yes: continue its execution (go through) */
  583. int nresults;
  584. lua_assert(ttisfunction(ci->base-1));
  585. ci->pc = &pc; /* function is active again */
  586. pc = ci->u.l.savedpc;
  587. lua_assert(GET_OPCODE(*(pc-1)) == OP_CALL);
  588. nresults = GETARG_C(*(pc-1)) - 1;
  589. luaD_poscall(L, nresults, ra);
  590. if (nresults >= 0) L->top = L->ci->top;
  591. goto retentry;
  592. }
  593. }
  594. case OP_FORLOOP: {
  595. lua_Number step, index, limit;
  596. const TObject *plimit = ra+1;
  597. const TObject *pstep = ra+2;
  598. if (!ttisnumber(ra))
  599. luaG_runerror(L, "`for' initial value must be a number");
  600. if (!tonumber(plimit, ra+1))
  601. luaG_runerror(L, "`for' limit must be a number");
  602. if (!tonumber(pstep, ra+2))
  603. luaG_runerror(L, "`for' step must be a number");
  604. step = nvalue(pstep);
  605. index = nvalue(ra) + step; /* increment index */
  606. limit = nvalue(plimit);
  607. if (step > 0 ? index <= limit : index >= limit) {
  608. dojump(pc, GETARG_sBx(i)); /* jump back */
  609. chgnvalue(ra, index); /* update index */
  610. }
  611. break;
  612. }
  613. case OP_TFORLOOP: {
  614. setobj(ra+4, ra+2);
  615. setobj(ra+3, ra+1);
  616. setobj(ra+2, ra);
  617. L->top = ra+5;
  618. luaD_call(L, ra+2, GETARG_C(i) + 1);
  619. L->top = L->ci->top;
  620. if (ttisnil(ra+2)) pc++; /* skip jump (break loop) */
  621. else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
  622. break;
  623. }
  624. case OP_TFORPREP: { /* for compatibility only */
  625. if (ttistable(ra)) {
  626. setobj(ra+1, ra);
  627. setobj(ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
  628. }
  629. dojump(pc, GETARG_sBx(i));
  630. break;
  631. }
  632. case OP_SETLIST:
  633. case OP_SETLISTO: {
  634. int bc;
  635. int n;
  636. Table *h;
  637. runtime_check(L, ttistable(ra));
  638. h = hvalue(ra);
  639. bc = GETARG_Bx(i);
  640. if (GET_OPCODE(i) == OP_SETLIST)
  641. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  642. else {
  643. n = L->top - ra - 1;
  644. L->top = L->ci->top;
  645. }
  646. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  647. for (; n > 0; n--)
  648. setobj(luaH_setnum(L, h, bc+n), ra+n);
  649. break;
  650. }
  651. case OP_CLOSE: {
  652. luaF_close(L, ra);
  653. break;
  654. }
  655. case OP_CLOSURE: {
  656. Proto *p;
  657. Closure *ncl;
  658. int nup, j;
  659. p = cl->p->p[GETARG_Bx(i)];
  660. nup = p->nupvalues;
  661. ncl = luaF_newLclosure(L, nup, &cl->g);
  662. ncl->l.p = p;
  663. for (j=0; j<nup; j++, pc++) {
  664. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  665. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  666. else {
  667. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  668. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  669. }
  670. }
  671. setclvalue(ra, ncl);
  672. luaV_checkGC(L, L->top);
  673. break;
  674. }
  675. }
  676. }
  677. }