lvm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*
  2. ** $Id: lvm.c,v 2.200 2014/04/29 18:11:57 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lvm_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "lobject.h"
  18. #include "lopcodes.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. #include "lvm.h"
  24. /* limit for table tag-method chains (to avoid loops) */
  25. #define MAXTAGLOOP 2000
  26. /* maximum length of the conversion of a number to a string */
  27. #define MAXNUMBER2STR 50
  28. /*
  29. ** Similar to 'tonumber', but does not attempt to convert strings and
  30. ** ensure correct precision (no extra bits). Used in comparisons.
  31. */
  32. static int tofloat (const TValue *obj, lua_Number *n) {
  33. if (ttisfloat(obj)) *n = fltvalue(obj);
  34. else if (ttisinteger(obj)) {
  35. volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */
  36. *n = x;
  37. }
  38. else {
  39. *n = 0; /* to avoid warnings */
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
  45. lua_assert(!ttisfloat(obj));
  46. if (ttisinteger(obj)) {
  47. *n = cast_num(ivalue(obj));
  48. return 1;
  49. }
  50. else
  51. return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n));
  52. }
  53. int luaV_tostring (lua_State *L, StkId obj) {
  54. if (!ttisnumber(obj))
  55. return 0;
  56. else {
  57. char buff[MAXNUMBER2STR];
  58. size_t len = (ttisinteger(obj))
  59. ? lua_integer2str(buff, ivalue(obj))
  60. : lua_number2str(buff, fltvalue(obj));
  61. setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
  62. return 1;
  63. }
  64. }
  65. /*
  66. ** Check whether a float number is within the range of a lua_Integer.
  67. ** (The comparisons are tricky because of rounding, which can or
  68. ** not occur depending on the relative sizes of floats and integers.)
  69. ** This function should be called only when 'n' has an integral value.
  70. */
  71. int luaV_numtointeger (lua_Number n, lua_Integer *p) {
  72. if (cast_num(LUA_MININTEGER) <= n && n < (LUA_MAXINTEGER + cast_num(1))) {
  73. *p = cast(lua_Integer, n);
  74. lua_assert(cast_num(*p) == n);
  75. return 1;
  76. }
  77. return 0; /* number is outside integer limits */
  78. }
  79. /*
  80. ** try to convert a non-integer value to an integer
  81. */
  82. int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
  83. lua_Number n;
  84. lua_assert(!ttisinteger(obj));
  85. if (tonumber(obj, &n)) {
  86. n = l_floor(n);
  87. return luaV_numtointeger(n, p);
  88. }
  89. else return 0;
  90. }
  91. /*
  92. ** Check whether the limit of a 'for' loop can be safely converted
  93. ** to an integer (rounding down or up depending on the signal of 'step')
  94. */
  95. static int limittointeger (const TValue *n, lua_Integer *p, lua_Integer step) {
  96. if (ttisinteger(n)) {
  97. *p = ivalue(n);
  98. return 1;
  99. }
  100. else if (!ttisfloat(n)) return 0;
  101. else {
  102. lua_Number f = fltvalue(n);
  103. f = (step >= 0) ? l_floor(f) : -l_floor(-f);
  104. return luaV_numtointeger(f, p);
  105. }
  106. }
  107. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  108. int loop;
  109. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  110. const TValue *tm;
  111. if (ttistable(t)) { /* `t' is a table? */
  112. Table *h = hvalue(t);
  113. const TValue *res = luaH_get(h, key); /* do a primitive get */
  114. if (!ttisnil(res) || /* result is not nil? */
  115. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  116. setobj2s(L, val, res);
  117. return;
  118. }
  119. /* else will try the tag method */
  120. }
  121. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  122. luaG_typeerror(L, t, "index");
  123. if (ttisfunction(tm)) {
  124. luaT_callTM(L, tm, t, key, val, 1);
  125. return;
  126. }
  127. t = tm; /* else repeat with 'tm' */
  128. }
  129. luaG_runerror(L, "gettable chain too long; possible loop");
  130. }
  131. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  132. int loop;
  133. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  134. const TValue *tm;
  135. if (ttistable(t)) { /* `t' is a table? */
  136. Table *h = hvalue(t);
  137. TValue *oldval = cast(TValue *, luaH_get(h, key));
  138. /* if previous value is not nil, there must be a previous entry
  139. in the table; moreover, a metamethod has no relevance */
  140. if (!ttisnil(oldval) ||
  141. /* previous value is nil; must check the metamethod */
  142. ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
  143. /* no metamethod; is there a previous entry in the table? */
  144. (oldval != luaO_nilobject ||
  145. /* no previous entry; must create one. (The next test is
  146. always true; we only need the assignment.) */
  147. (oldval = luaH_newkey(L, h, key), 1)))) {
  148. /* no metamethod and (now) there is an entry with given key */
  149. setobj2t(L, oldval, val); /* assign new value to that entry */
  150. invalidateTMcache(h);
  151. luaC_barrierback(L, h, val);
  152. return;
  153. }
  154. /* else will try the metamethod */
  155. }
  156. else /* not a table; check metamethod */
  157. if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  158. luaG_typeerror(L, t, "index");
  159. /* there is a metamethod */
  160. if (ttisfunction(tm)) {
  161. luaT_callTM(L, tm, t, key, val, 0);
  162. return;
  163. }
  164. t = tm; /* else repeat with 'tm' */
  165. }
  166. luaG_runerror(L, "settable chain too long; possible loop");
  167. }
  168. static int l_strcmp (const TString *ls, const TString *rs) {
  169. const char *l = getstr(ls);
  170. size_t ll = ls->tsv.len;
  171. const char *r = getstr(rs);
  172. size_t lr = rs->tsv.len;
  173. for (;;) {
  174. int temp = strcoll(l, r);
  175. if (temp != 0) return temp;
  176. else { /* strings are equal up to a `\0' */
  177. size_t len = strlen(l); /* index of first `\0' in both strings */
  178. if (len == lr) /* r is finished? */
  179. return (len == ll) ? 0 : 1;
  180. else if (len == ll) /* l is finished? */
  181. return -1; /* l is smaller than r (because r is not finished) */
  182. /* both strings longer than `len'; go on comparing (after the `\0') */
  183. len++;
  184. l += len; ll -= len; r += len; lr -= len;
  185. }
  186. }
  187. }
  188. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  189. int res;
  190. lua_Number nl, nr;
  191. if (ttisinteger(l) && ttisinteger(r))
  192. return (ivalue(l) < ivalue(r));
  193. else if (tofloat(l, &nl) && tofloat(r, &nr))
  194. return luai_numlt(nl, nr);
  195. else if (ttisstring(l) && ttisstring(r))
  196. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  197. else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)
  198. luaG_ordererror(L, l, r);
  199. return res;
  200. }
  201. int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  202. int res;
  203. lua_Number nl, nr;
  204. if (ttisinteger(l) && ttisinteger(r))
  205. return (ivalue(l) <= ivalue(r));
  206. else if (tofloat(l, &nl) && tofloat(r, &nr))
  207. return luai_numle(nl, nr);
  208. else if (ttisstring(l) && ttisstring(r))
  209. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  210. else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
  211. return res;
  212. else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */
  213. luaG_ordererror(L, l, r);
  214. return !res;
  215. }
  216. /*
  217. ** equality of Lua values. L == NULL means raw equality (no metamethods)
  218. */
  219. int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
  220. const TValue *tm;
  221. if (ttype(t1) != ttype(t2)) {
  222. if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
  223. return 0; /* only numbers can be equal with different variants */
  224. else { /* two numbers with different variants */
  225. lua_Number n1, n2;
  226. lua_assert(ttisnumber(t1) && ttisnumber(t2));
  227. cast_void(tofloat(t1, &n1)); cast_void(tofloat(t2, &n2));
  228. return luai_numeq(n1, n2);
  229. }
  230. }
  231. /* values have same type and same variant */
  232. switch (ttype(t1)) {
  233. case LUA_TNIL: return 1;
  234. case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2));
  235. case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
  236. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  237. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  238. case LUA_TLCF: return fvalue(t1) == fvalue(t2);
  239. case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2));
  240. case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2));
  241. case LUA_TUSERDATA: {
  242. if (uvalue(t1) == uvalue(t2)) return 1;
  243. else if (L == NULL) return 0;
  244. tm = luaT_getequalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable);
  245. break; /* will try TM */
  246. }
  247. case LUA_TTABLE: {
  248. if (hvalue(t1) == hvalue(t2)) return 1;
  249. else if (L == NULL) return 0;
  250. tm = luaT_getequalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable);
  251. break; /* will try TM */
  252. }
  253. default:
  254. return gcvalue(t1) == gcvalue(t2);
  255. }
  256. if (tm == NULL) return 0; /* no TM? */
  257. luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */
  258. return !l_isfalse(L->top);
  259. }
  260. #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o)))
  261. void luaV_concat (lua_State *L, int total) {
  262. lua_assert(total >= 2);
  263. do {
  264. StkId top = L->top;
  265. int n = 2; /* number of elements handled in this pass (at least 2) */
  266. if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1))
  267. luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
  268. else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
  269. cast_void(tostring(L, top - 2)); /* result is first operand */
  270. else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
  271. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  272. }
  273. else {
  274. /* at least two non-empty string values; get as many as possible */
  275. size_t tl = tsvalue(top-1)->len;
  276. char *buffer;
  277. int i;
  278. /* collect total length */
  279. for (i = 1; i < total && tostring(L, top-i-1); i++) {
  280. size_t l = tsvalue(top-i-1)->len;
  281. if (l >= (MAX_SIZE/sizeof(char)) - tl)
  282. luaG_runerror(L, "string length overflow");
  283. tl += l;
  284. }
  285. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  286. tl = 0;
  287. n = i;
  288. do { /* concat all strings */
  289. size_t l = tsvalue(top-i)->len;
  290. memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
  291. tl += l;
  292. } while (--i > 0);
  293. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  294. }
  295. total -= n-1; /* got 'n' strings to create 1 new */
  296. L->top -= n-1; /* popped 'n' strings and pushed one */
  297. } while (total > 1); /* repeat until only 1 result left */
  298. }
  299. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  300. const TValue *tm;
  301. switch (ttnov(rb)) {
  302. case LUA_TTABLE: {
  303. Table *h = hvalue(rb);
  304. tm = fasttm(L, h->metatable, TM_LEN);
  305. if (tm) break; /* metamethod? break switch to call it */
  306. setivalue(ra, luaH_getn(h)); /* else primitive len */
  307. return;
  308. }
  309. case LUA_TSTRING: {
  310. setivalue(ra, tsvalue(rb)->len);
  311. return;
  312. }
  313. default: { /* try metamethod */
  314. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  315. if (ttisnil(tm)) /* no metamethod? */
  316. luaG_typeerror(L, rb, "get length of");
  317. break;
  318. }
  319. }
  320. luaT_callTM(L, tm, rb, rb, ra, 1);
  321. }
  322. lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) {
  323. if (l_castS2U(y) + 1u <= 1u) { /* special cases: -1 or 0 */
  324. if (y == 0)
  325. luaG_runerror(L, "attempt to divide by zero");
  326. return intop(-, 0, x); /* y==-1; avoid overflow with 0x80000...//-1 */
  327. }
  328. else {
  329. lua_Integer d = x / y; /* perform division */
  330. if ((x ^ y) >= 0 || x % y == 0) /* same signal or no rest? */
  331. return d;
  332. else
  333. return d - 1; /* correct 'div' for negative case */
  334. }
  335. }
  336. lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) {
  337. if (l_castS2U(y) + 1u <= 1u) { /* special cases: -1 or 0 */
  338. if (y == 0)
  339. luaG_runerror(L, "attempt to perform 'n%%0'");
  340. return 0; /* y==-1; avoid overflow with 0x80000...%-1 */
  341. }
  342. else {
  343. lua_Integer r = x % y;
  344. if (r == 0 || (x ^ y) >= 0)
  345. return r;
  346. else
  347. return r + y; /* correct 'mod' for negative case */
  348. }
  349. }
  350. lua_Integer luaV_pow (lua_Integer x, lua_Integer y) {
  351. lua_assert(y >= 0);
  352. if (y == 0)
  353. return 1; /* x^0 == 1 */
  354. else {
  355. lua_Integer r = 1;
  356. for (; y > 1; y >>= 1) {
  357. if (y & 1) r = intop(*, r, x);
  358. x = intop(*, x, x);
  359. }
  360. r = intop(*, r, x);
  361. return r;
  362. }
  363. }
  364. /* number of bits in an integer */
  365. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  366. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  367. if (y < 0) { /* shift right? */
  368. if (y <= -NBITS) return 0;
  369. else return intop(>>, x, -y);
  370. }
  371. else { /* shift left */
  372. if (y >= NBITS) return 0;
  373. else return intop(<<, x, y);
  374. }
  375. }
  376. /*
  377. ** check whether cached closure in prototype 'p' may be reused, that is,
  378. ** whether there is a cached closure with the same upvalues needed by
  379. ** new closure to be created.
  380. */
  381. static Closure *getcached (Proto *p, UpVal **encup, StkId base) {
  382. Closure *c = p->cache;
  383. if (c != NULL) { /* is there a cached closure? */
  384. int nup = p->sizeupvalues;
  385. Upvaldesc *uv = p->upvalues;
  386. int i;
  387. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  388. TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
  389. if (c->l.upvals[i]->v != v)
  390. return NULL; /* wrong upvalue; cannot reuse closure */
  391. }
  392. }
  393. return c; /* return cached closure (or NULL if no cached closure) */
  394. }
  395. /*
  396. ** create a new Lua closure, push it in the stack, and initialize
  397. ** its upvalues. Note that the closure is not cached if prototype is
  398. ** already black (which means that 'cache' was already cleared by the
  399. ** GC).
  400. */
  401. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  402. StkId ra) {
  403. int nup = p->sizeupvalues;
  404. Upvaldesc *uv = p->upvalues;
  405. int i;
  406. Closure *ncl = luaF_newLclosure(L, nup);
  407. ncl->l.p = p;
  408. setclLvalue(L, ra, ncl); /* anchor new closure in stack */
  409. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  410. if (uv[i].instack) /* upvalue refers to local variable? */
  411. ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
  412. else /* get upvalue from enclosing function */
  413. ncl->l.upvals[i] = encup[uv[i].idx];
  414. ncl->l.upvals[i]->refcount++;
  415. /* new closure is white, so we do not need a barrier here */
  416. }
  417. if (!isblack(obj2gco(p))) /* cache will not break GC invariant? */
  418. p->cache = ncl; /* save it on cache for reuse */
  419. }
  420. /*
  421. ** finish execution of an opcode interrupted by an yield
  422. */
  423. void luaV_finishOp (lua_State *L) {
  424. CallInfo *ci = L->ci;
  425. StkId base = ci->u.l.base;
  426. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  427. OpCode op = GET_OPCODE(inst);
  428. switch (op) { /* finish its execution */
  429. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV:
  430. case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
  431. case OP_MOD: case OP_POW:
  432. case OP_UNM: case OP_BNOT: case OP_LEN:
  433. case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
  434. setobjs2s(L, base + GETARG_A(inst), --L->top);
  435. break;
  436. }
  437. case OP_LE: case OP_LT: case OP_EQ: {
  438. int res = !l_isfalse(L->top - 1);
  439. L->top--;
  440. /* metamethod should not be called when operand is K */
  441. lua_assert(!ISK(GETARG_B(inst)));
  442. if (op == OP_LE && /* "<=" using "<" instead? */
  443. ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
  444. res = !res; /* invert result */
  445. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  446. if (res != GETARG_A(inst)) /* condition failed? */
  447. ci->u.l.savedpc++; /* skip jump instruction */
  448. break;
  449. }
  450. case OP_CONCAT: {
  451. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  452. int b = GETARG_B(inst); /* first element to concatenate */
  453. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  454. setobj2s(L, top - 2, top); /* put TM result in proper position */
  455. if (total > 1) { /* are there elements to concat? */
  456. L->top = top - 1; /* top is one after last element (at top-2) */
  457. luaV_concat(L, total); /* concat them (may yield again) */
  458. }
  459. /* move final result to final position */
  460. setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
  461. L->top = ci->top; /* restore top */
  462. break;
  463. }
  464. case OP_TFORCALL: {
  465. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  466. L->top = ci->top; /* correct top */
  467. break;
  468. }
  469. case OP_CALL: {
  470. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  471. L->top = ci->top; /* adjust results */
  472. break;
  473. }
  474. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  475. break;
  476. default: lua_assert(0);
  477. }
  478. }
  479. /*
  480. ** some macros for common tasks in `luaV_execute'
  481. */
  482. #if !defined luai_runtimecheck
  483. #define luai_runtimecheck(L, c) /* void */
  484. #endif
  485. #define RA(i) (base+GETARG_A(i))
  486. /* to be used after possible stack reallocation */
  487. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  488. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  489. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  490. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  491. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  492. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  493. #define KBx(i) \
  494. (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
  495. /* execute a jump instruction */
  496. #define dojump(ci,i,e) \
  497. { int a = GETARG_A(i); \
  498. if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
  499. ci->u.l.savedpc += GETARG_sBx(i) + e; }
  500. /* for test instructions, execute the jump instruction that follows it */
  501. #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
  502. #define Protect(x) { {x;}; base = ci->u.l.base; }
  503. #define checkGC(L,c) \
  504. Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
  505. luaC_step(L); \
  506. L->top = ci->top;}) /* restore top */ \
  507. luai_threadyield(L); )
  508. #define vmdispatch(o) switch(o)
  509. #define vmcase(l,b) case l: {b} break;
  510. #define vmcasenb(l,b) case l: {b} /* nb = no break */
  511. void luaV_execute (lua_State *L) {
  512. CallInfo *ci = L->ci;
  513. LClosure *cl;
  514. TValue *k;
  515. StkId base;
  516. newframe: /* reentry point when frame changes (call/return) */
  517. lua_assert(ci == L->ci);
  518. cl = clLvalue(ci->func);
  519. k = cl->p->k;
  520. base = ci->u.l.base;
  521. /* main loop of interpreter */
  522. for (;;) {
  523. Instruction i = *(ci->u.l.savedpc++);
  524. StkId ra;
  525. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  526. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  527. Protect(luaG_traceexec(L));
  528. }
  529. /* WARNING: several calls may realloc the stack and invalidate `ra' */
  530. ra = RA(i);
  531. lua_assert(base == ci->u.l.base);
  532. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  533. vmdispatch (GET_OPCODE(i)) {
  534. vmcase(OP_MOVE,
  535. setobjs2s(L, ra, RB(i));
  536. )
  537. vmcase(OP_LOADK,
  538. TValue *rb = k + GETARG_Bx(i);
  539. setobj2s(L, ra, rb);
  540. )
  541. vmcase(OP_LOADKX,
  542. TValue *rb;
  543. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  544. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  545. setobj2s(L, ra, rb);
  546. )
  547. vmcase(OP_LOADBOOL,
  548. setbvalue(ra, GETARG_B(i));
  549. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  550. )
  551. vmcase(OP_LOADNIL,
  552. int b = GETARG_B(i);
  553. do {
  554. setnilvalue(ra++);
  555. } while (b--);
  556. )
  557. vmcase(OP_GETUPVAL,
  558. int b = GETARG_B(i);
  559. setobj2s(L, ra, cl->upvals[b]->v);
  560. )
  561. vmcase(OP_GETTABUP,
  562. int b = GETARG_B(i);
  563. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  564. )
  565. vmcase(OP_GETTABLE,
  566. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  567. )
  568. vmcase(OP_SETTABUP,
  569. int a = GETARG_A(i);
  570. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  571. )
  572. vmcase(OP_SETUPVAL,
  573. UpVal *uv = cl->upvals[GETARG_B(i)];
  574. setobj(L, uv->v, ra);
  575. luaC_upvalbarrier(L, uv);
  576. )
  577. vmcase(OP_SETTABLE,
  578. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  579. )
  580. vmcase(OP_NEWTABLE,
  581. int b = GETARG_B(i);
  582. int c = GETARG_C(i);
  583. Table *t = luaH_new(L);
  584. sethvalue(L, ra, t);
  585. if (b != 0 || c != 0)
  586. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  587. checkGC(L, ra + 1);
  588. )
  589. vmcase(OP_SELF,
  590. StkId rb = RB(i);
  591. setobjs2s(L, ra+1, rb);
  592. Protect(luaV_gettable(L, rb, RKC(i), ra));
  593. )
  594. vmcase(OP_ADD,
  595. TValue *rb = RKB(i);
  596. TValue *rc = RKC(i);
  597. lua_Number nb; lua_Number nc;
  598. if (ttisinteger(rb) && ttisinteger(rc)) {
  599. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  600. setivalue(ra, intop(+, ib, ic));
  601. }
  602. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  603. setfltvalue(ra, luai_numadd(L, nb, nc));
  604. }
  605. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  606. )
  607. vmcase(OP_SUB,
  608. TValue *rb = RKB(i);
  609. TValue *rc = RKC(i);
  610. lua_Number nb; lua_Number nc;
  611. if (ttisinteger(rb) && ttisinteger(rc)) {
  612. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  613. setivalue(ra, intop(-, ib, ic));
  614. }
  615. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  616. setfltvalue(ra, luai_numsub(L, nb, nc));
  617. }
  618. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  619. )
  620. vmcase(OP_MUL,
  621. TValue *rb = RKB(i);
  622. TValue *rc = RKC(i);
  623. lua_Number nb; lua_Number nc;
  624. if (ttisinteger(rb) && ttisinteger(rc)) {
  625. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  626. setivalue(ra, intop(*, ib, ic));
  627. }
  628. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  629. setfltvalue(ra, luai_nummul(L, nb, nc));
  630. }
  631. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  632. )
  633. vmcase(OP_DIV, /* float division (always with floats) */
  634. TValue *rb = RKB(i);
  635. TValue *rc = RKC(i);
  636. lua_Number nb; lua_Number nc;
  637. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  638. setfltvalue(ra, luai_numdiv(L, nb, nc));
  639. }
  640. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  641. )
  642. vmcase(OP_IDIV, /* integer division */
  643. TValue *rb = RKB(i);
  644. TValue *rc = RKC(i);
  645. lua_Integer ib; lua_Integer ic;
  646. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  647. setivalue(ra, luaV_div(L, ib, ic));
  648. }
  649. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  650. )
  651. vmcase(OP_BAND,
  652. TValue *rb = RKB(i);
  653. TValue *rc = RKC(i);
  654. lua_Integer ib; lua_Integer ic;
  655. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  656. setivalue(ra, intop(&, ib, ic));
  657. }
  658. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  659. )
  660. vmcase(OP_BOR,
  661. TValue *rb = RKB(i);
  662. TValue *rc = RKC(i);
  663. lua_Integer ib; lua_Integer ic;
  664. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  665. setivalue(ra, intop(|, ib, ic));
  666. }
  667. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  668. )
  669. vmcase(OP_BXOR,
  670. TValue *rb = RKB(i);
  671. TValue *rc = RKC(i);
  672. lua_Integer ib; lua_Integer ic;
  673. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  674. setivalue(ra, intop(^, ib, ic));
  675. }
  676. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  677. )
  678. vmcase(OP_SHL,
  679. TValue *rb = RKB(i);
  680. TValue *rc = RKC(i);
  681. lua_Integer ib; lua_Integer ic;
  682. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  683. setivalue(ra, luaV_shiftl(ib, ic));
  684. }
  685. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  686. )
  687. vmcase(OP_SHR,
  688. TValue *rb = RKB(i);
  689. TValue *rc = RKC(i);
  690. lua_Integer ib; lua_Integer ic;
  691. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  692. setivalue(ra, luaV_shiftl(ib, -ic));
  693. }
  694. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  695. )
  696. vmcase(OP_MOD,
  697. TValue *rb = RKB(i);
  698. TValue *rc = RKC(i);
  699. lua_Number nb; lua_Number nc;
  700. if (ttisinteger(rb) && ttisinteger(rc)) {
  701. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  702. setivalue(ra, luaV_mod(L, ib, ic));
  703. }
  704. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  705. lua_Number m;
  706. luai_nummod(L, nb, nc, m);
  707. setfltvalue(ra, m);
  708. }
  709. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  710. )
  711. vmcase(OP_POW,
  712. TValue *rb = RKB(i);
  713. TValue *rc = RKC(i);
  714. lua_Number nb; lua_Number nc;
  715. lua_Integer ic;
  716. if (ttisinteger(rb) && ttisinteger(rc) && (ic = ivalue(rc)) >= 0) {
  717. lua_Integer ib = ivalue(rb);
  718. setivalue(ra, luaV_pow(ib, ic));
  719. }
  720. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  721. setfltvalue(ra, luai_numpow(L, nb, nc));
  722. }
  723. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  724. )
  725. vmcase(OP_UNM,
  726. TValue *rb = RB(i);
  727. lua_Number nb;
  728. if (ttisinteger(rb)) {
  729. lua_Integer ib = ivalue(rb);
  730. setivalue(ra, intop(-, 0, ib));
  731. }
  732. else if (tonumber(rb, &nb)) {
  733. setfltvalue(ra, luai_numunm(L, nb));
  734. }
  735. else {
  736. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  737. }
  738. )
  739. vmcase(OP_BNOT,
  740. TValue *rb = RB(i);
  741. lua_Integer ib;
  742. if (tointeger(rb, &ib)) {
  743. setivalue(ra, intop(^, ~l_castS2U(0), ib));
  744. }
  745. else {
  746. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  747. }
  748. )
  749. vmcase(OP_NOT,
  750. TValue *rb = RB(i);
  751. int res = l_isfalse(rb); /* next assignment may change this value */
  752. setbvalue(ra, res);
  753. )
  754. vmcase(OP_LEN,
  755. Protect(luaV_objlen(L, ra, RB(i)));
  756. )
  757. vmcase(OP_CONCAT,
  758. int b = GETARG_B(i);
  759. int c = GETARG_C(i);
  760. StkId rb;
  761. L->top = base + c + 1; /* mark the end of concat operands */
  762. Protect(luaV_concat(L, c - b + 1));
  763. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  764. rb = b + base;
  765. setobjs2s(L, ra, rb);
  766. checkGC(L, (ra >= rb ? ra + 1 : rb));
  767. L->top = ci->top; /* restore top */
  768. )
  769. vmcase(OP_JMP,
  770. dojump(ci, i, 0);
  771. )
  772. vmcase(OP_EQ,
  773. TValue *rb = RKB(i);
  774. TValue *rc = RKC(i);
  775. Protect(
  776. if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
  777. ci->u.l.savedpc++;
  778. else
  779. donextjump(ci);
  780. )
  781. )
  782. vmcase(OP_LT,
  783. Protect(
  784. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  785. ci->u.l.savedpc++;
  786. else
  787. donextjump(ci);
  788. )
  789. )
  790. vmcase(OP_LE,
  791. Protect(
  792. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  793. ci->u.l.savedpc++;
  794. else
  795. donextjump(ci);
  796. )
  797. )
  798. vmcase(OP_TEST,
  799. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  800. ci->u.l.savedpc++;
  801. else
  802. donextjump(ci);
  803. )
  804. vmcase(OP_TESTSET,
  805. TValue *rb = RB(i);
  806. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  807. ci->u.l.savedpc++;
  808. else {
  809. setobjs2s(L, ra, rb);
  810. donextjump(ci);
  811. }
  812. )
  813. vmcase(OP_CALL,
  814. int b = GETARG_B(i);
  815. int nresults = GETARG_C(i) - 1;
  816. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  817. if (luaD_precall(L, ra, nresults)) { /* C function? */
  818. if (nresults >= 0) L->top = ci->top; /* adjust results */
  819. base = ci->u.l.base;
  820. }
  821. else { /* Lua function */
  822. ci = L->ci;
  823. ci->callstatus |= CIST_REENTRY;
  824. goto newframe; /* restart luaV_execute over new Lua function */
  825. }
  826. )
  827. vmcase(OP_TAILCALL,
  828. int b = GETARG_B(i);
  829. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  830. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  831. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  832. base = ci->u.l.base;
  833. else {
  834. /* tail call: put called frame (n) in place of caller one (o) */
  835. CallInfo *nci = L->ci; /* called frame */
  836. CallInfo *oci = nci->previous; /* caller frame */
  837. StkId nfunc = nci->func; /* called function */
  838. StkId ofunc = oci->func; /* caller function */
  839. /* last stack slot filled by 'precall' */
  840. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  841. int aux;
  842. /* close all upvalues from previous call */
  843. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  844. /* move new frame into old one */
  845. for (aux = 0; nfunc + aux < lim; aux++)
  846. setobjs2s(L, ofunc + aux, nfunc + aux);
  847. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  848. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  849. oci->u.l.savedpc = nci->u.l.savedpc;
  850. oci->callstatus |= CIST_TAIL; /* function was tail called */
  851. ci = L->ci = oci; /* remove new frame */
  852. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  853. goto newframe; /* restart luaV_execute over new Lua function */
  854. }
  855. )
  856. vmcasenb(OP_RETURN,
  857. int b = GETARG_B(i);
  858. if (b != 0) L->top = ra+b-1;
  859. if (cl->p->sizep > 0) luaF_close(L, base);
  860. b = luaD_poscall(L, ra);
  861. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  862. return; /* external invocation: return */
  863. else { /* invocation via reentry: continue execution */
  864. ci = L->ci;
  865. if (b) L->top = ci->top;
  866. lua_assert(isLua(ci));
  867. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  868. goto newframe; /* restart luaV_execute over new Lua function */
  869. }
  870. )
  871. vmcase(OP_FORLOOP,
  872. if (ttisinteger(ra)) { /* integer loop? */
  873. lua_Integer step = ivalue(ra + 2);
  874. lua_Integer idx = ivalue(ra) + step; /* increment index */
  875. lua_Integer limit = ivalue(ra + 1);
  876. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  877. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  878. setivalue(ra, idx); /* update internal index... */
  879. setivalue(ra + 3, idx); /* ...and external index */
  880. }
  881. }
  882. else { /* floating loop */
  883. lua_Number step = fltvalue(ra + 2);
  884. lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
  885. lua_Number limit = fltvalue(ra + 1);
  886. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  887. : luai_numle(limit, idx)) {
  888. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  889. setfltvalue(ra, idx); /* update internal index... */
  890. setfltvalue(ra + 3, idx); /* ...and external index */
  891. }
  892. }
  893. )
  894. vmcase(OP_FORPREP,
  895. TValue *init = ra;
  896. TValue *plimit = ra + 1;
  897. TValue *pstep = ra + 2;
  898. lua_Integer ilimit;
  899. if (ttisinteger(init) && ttisinteger(pstep) &&
  900. limittointeger(plimit, &ilimit, ivalue(pstep))) {
  901. /* all values are integer */
  902. setivalue(init, ivalue(init) - ivalue(pstep));
  903. setivalue(plimit, ilimit);
  904. }
  905. else { /* try making all values floats */
  906. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  907. if (!tonumber(plimit, &nlimit))
  908. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  909. setfltvalue(plimit, nlimit);
  910. if (!tonumber(pstep, &nstep))
  911. luaG_runerror(L, LUA_QL("for") " step must be a number");
  912. setfltvalue(pstep, nstep);
  913. if (!tonumber(init, &ninit))
  914. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  915. setfltvalue(init, luai_numsub(L, ninit, nstep));
  916. }
  917. ci->u.l.savedpc += GETARG_sBx(i);
  918. )
  919. vmcasenb(OP_TFORCALL,
  920. StkId cb = ra + 3; /* call base */
  921. setobjs2s(L, cb+2, ra+2);
  922. setobjs2s(L, cb+1, ra+1);
  923. setobjs2s(L, cb, ra);
  924. L->top = cb + 3; /* func. + 2 args (state and index) */
  925. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  926. L->top = ci->top;
  927. i = *(ci->u.l.savedpc++); /* go to next instruction */
  928. ra = RA(i);
  929. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  930. goto l_tforloop;
  931. )
  932. vmcase(OP_TFORLOOP,
  933. l_tforloop:
  934. if (!ttisnil(ra + 1)) { /* continue loop? */
  935. setobjs2s(L, ra, ra + 1); /* save control variable */
  936. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  937. }
  938. )
  939. vmcase(OP_SETLIST,
  940. int n = GETARG_B(i);
  941. int c = GETARG_C(i);
  942. int last;
  943. Table *h;
  944. if (n == 0) n = cast_int(L->top - ra) - 1;
  945. if (c == 0) {
  946. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  947. c = GETARG_Ax(*ci->u.l.savedpc++);
  948. }
  949. luai_runtimecheck(L, ttistable(ra));
  950. h = hvalue(ra);
  951. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  952. if (last > h->sizearray) /* needs more space? */
  953. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  954. for (; n > 0; n--) {
  955. TValue *val = ra+n;
  956. luaH_setint(L, h, last--, val);
  957. luaC_barrierback(L, h, val);
  958. }
  959. L->top = ci->top; /* correct top (in case of previous open call) */
  960. )
  961. vmcase(OP_CLOSURE,
  962. Proto *p = cl->p->p[GETARG_Bx(i)];
  963. Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  964. if (ncl == NULL) /* no match? */
  965. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  966. else
  967. setclLvalue(L, ra, ncl); /* push cashed closure */
  968. checkGC(L, ra + 1);
  969. )
  970. vmcase(OP_VARARG,
  971. int b = GETARG_B(i) - 1;
  972. int j;
  973. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  974. if (b < 0) { /* B == 0? */
  975. b = n; /* get all var. arguments */
  976. Protect(luaD_checkstack(L, n));
  977. ra = RA(i); /* previous call may change the stack */
  978. L->top = ra + n;
  979. }
  980. for (j = 0; j < b; j++) {
  981. if (j < n) {
  982. setobjs2s(L, ra + j, base - n + j);
  983. }
  984. else {
  985. setnilvalue(ra + j);
  986. }
  987. }
  988. )
  989. vmcase(OP_EXTRAARG,
  990. lua_assert(0);
  991. )
  992. }
  993. }
  994. }