lvm.c 34 KB

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