lvm.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  1. /*
  2. ** $Id: lvm.c $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lvm_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <float.h>
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "lua.h"
  16. #include "ldebug.h"
  17. #include "ldo.h"
  18. #include "lfunc.h"
  19. #include "lgc.h"
  20. #include "lobject.h"
  21. #include "lopcodes.h"
  22. #include "lstate.h"
  23. #include "lstring.h"
  24. #include "ltable.h"
  25. #include "ltm.h"
  26. #include "lvm.h"
  27. /*
  28. ** By default, use jump tables in the main interpreter loop on gcc
  29. ** and compatible compilers.
  30. */
  31. #if !defined(LUA_USE_JUMPTABLE)
  32. #if defined(__GNUC__)
  33. #define LUA_USE_JUMPTABLE 1
  34. #else
  35. #define LUA_USE_JUMPTABLE 0
  36. #endif
  37. #endif
  38. /* limit for table tag-method chains (to avoid infinite loops) */
  39. #define MAXTAGLOOP 2000
  40. /*
  41. ** 'l_intfitsf' checks whether a given integer is in the range that
  42. ** can be converted to a float without rounding. Used in comparisons.
  43. */
  44. /* number of bits in the mantissa of a float */
  45. #define NBM (l_floatatt(MANT_DIG))
  46. /*
  47. ** Check whether some integers may not fit in a float, testing whether
  48. ** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.)
  49. ** (The shifts are done in parts, to avoid shifting by more than the size
  50. ** of an integer. In a worst case, NBM == 113 for long double and
  51. ** sizeof(long) == 32.)
  52. */
  53. #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
  54. >> (NBM - (3 * (NBM / 4)))) > 0
  55. /* limit for integers that fit in a float */
  56. #define MAXINTFITSF ((lua_Unsigned)1 << NBM)
  57. /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */
  58. #define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF))
  59. #else /* all integers fit in a float precisely */
  60. #define l_intfitsf(i) 1
  61. #endif
  62. /*
  63. ** Try to convert a value from string to a number value.
  64. ** If the value is not a string or is a string not representing
  65. ** a valid numeral (or if coercions from strings to numbers
  66. ** are disabled via macro 'cvt2num'), do not modify 'result'
  67. ** and return 0.
  68. */
  69. static int l_strton (const TValue *obj, TValue *result) {
  70. lua_assert(obj != result);
  71. if (!cvt2num(obj)) /* is object not a string? */
  72. return 0;
  73. else {
  74. TString *st = tsvalue(obj);
  75. size_t stlen;
  76. const char *s = getlstr(st, stlen);
  77. return (luaO_str2num(s, result) == stlen + 1);
  78. }
  79. }
  80. /*
  81. ** Try to convert a value to a float. The float case is already handled
  82. ** by the macro 'tonumber'.
  83. */
  84. int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
  85. TValue v;
  86. if (ttisinteger(obj)) {
  87. *n = cast_num(ivalue(obj));
  88. return 1;
  89. }
  90. else if (l_strton(obj, &v)) { /* string coercible to number? */
  91. *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */
  92. return 1;
  93. }
  94. else
  95. return 0; /* conversion failed */
  96. }
  97. /*
  98. ** try to convert a float to an integer, rounding according to 'mode'.
  99. */
  100. int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
  101. lua_Number f = l_floor(n);
  102. if (n != f) { /* not an integral value? */
  103. if (mode == F2Ieq) return 0; /* fails if mode demands integral value */
  104. else if (mode == F2Iceil) /* needs ceil? */
  105. f += 1; /* convert floor to ceil (remember: n != f) */
  106. }
  107. return lua_numbertointeger(f, p);
  108. }
  109. /*
  110. ** try to convert a value to an integer, rounding according to 'mode',
  111. ** without string coercion.
  112. ** ("Fast track" handled by macro 'tointegerns'.)
  113. */
  114. int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
  115. if (ttisfloat(obj))
  116. return luaV_flttointeger(fltvalue(obj), p, mode);
  117. else if (ttisinteger(obj)) {
  118. *p = ivalue(obj);
  119. return 1;
  120. }
  121. else
  122. return 0;
  123. }
  124. /*
  125. ** try to convert a value to an integer.
  126. */
  127. int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
  128. TValue v;
  129. if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
  130. obj = &v; /* change it to point to its corresponding number */
  131. return luaV_tointegerns(obj, p, mode);
  132. }
  133. /*
  134. ** Try to convert a 'for' limit to an integer, preserving the semantics
  135. ** of the loop. Return true if the loop must not run; otherwise, '*p'
  136. ** gets the integer limit.
  137. ** (The following explanation assumes a positive step; it is valid for
  138. ** negative steps mutatis mutandis.)
  139. ** If the limit is an integer or can be converted to an integer,
  140. ** rounding down, that is the limit.
  141. ** Otherwise, check whether the limit can be converted to a float. If
  142. ** the float is too large, clip it to LUA_MAXINTEGER. If the float
  143. ** is too negative, the loop should not run, because any initial
  144. ** integer value is greater than such limit; so, the function returns
  145. ** true to signal that. (For this latter case, no integer limit would be
  146. ** correct; even a limit of LUA_MININTEGER would run the loop once for
  147. ** an initial value equal to LUA_MININTEGER.)
  148. */
  149. static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
  150. lua_Integer *p, lua_Integer step) {
  151. if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
  152. /* not coercible to in integer */
  153. lua_Number flim; /* try to convert to float */
  154. if (!tonumber(lim, &flim)) /* cannot convert to float? */
  155. luaG_forerror(L, lim, "limit");
  156. /* else 'flim' is a float out of integer bounds */
  157. if (luai_numlt(0, flim)) { /* if it is positive, it is too large */
  158. if (step < 0) return 1; /* initial value must be less than it */
  159. *p = LUA_MAXINTEGER; /* truncate */
  160. }
  161. else { /* it is less than min integer */
  162. if (step > 0) return 1; /* initial value must be greater than it */
  163. *p = LUA_MININTEGER; /* truncate */
  164. }
  165. }
  166. return (step > 0 ? init > *p : init < *p); /* not to run? */
  167. }
  168. /*
  169. ** Prepare a numerical for loop (opcode OP_FORPREP).
  170. ** Before execution, stack is as follows:
  171. ** ra : initial value
  172. ** ra + 1 : limit
  173. ** ra + 2 : step
  174. ** Return true to skip the loop. Otherwise,
  175. ** after preparation, stack will be as follows:
  176. ** ra : loop counter (integer loops) or limit (float loops)
  177. ** ra + 1 : step
  178. ** ra + 2 : control variable
  179. */
  180. static int forprep (lua_State *L, StkId ra) {
  181. TValue *pinit = s2v(ra);
  182. TValue *plimit = s2v(ra + 1);
  183. TValue *pstep = s2v(ra + 2);
  184. if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
  185. lua_Integer init = ivalue(pinit);
  186. lua_Integer step = ivalue(pstep);
  187. lua_Integer limit;
  188. if (step == 0)
  189. luaG_runerror(L, "'for' step is zero");
  190. if (forlimit(L, init, plimit, &limit, step))
  191. return 1; /* skip the loop */
  192. else { /* prepare loop counter */
  193. lua_Unsigned count;
  194. if (step > 0) { /* ascending loop? */
  195. count = l_castS2U(limit) - l_castS2U(init);
  196. if (step != 1) /* avoid division in the too common case */
  197. count /= l_castS2U(step);
  198. }
  199. else { /* step < 0; descending loop */
  200. count = l_castS2U(init) - l_castS2U(limit);
  201. /* 'step+1' avoids negating 'mininteger' */
  202. count /= l_castS2U(-(step + 1)) + 1u;
  203. }
  204. /* use 'chgivalue' for places that for sure had integers */
  205. chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */
  206. setivalue(s2v(ra + 1), step); /* change limit to step */
  207. chgivalue(s2v(ra + 2), init); /* change step to init */
  208. }
  209. }
  210. else { /* try making all values floats */
  211. lua_Number init; lua_Number limit; lua_Number step;
  212. if (l_unlikely(!tonumber(plimit, &limit)))
  213. luaG_forerror(L, plimit, "limit");
  214. if (l_unlikely(!tonumber(pstep, &step)))
  215. luaG_forerror(L, pstep, "step");
  216. if (l_unlikely(!tonumber(pinit, &init)))
  217. luaG_forerror(L, pinit, "initial value");
  218. if (step == 0)
  219. luaG_runerror(L, "'for' step is zero");
  220. if (luai_numlt(0, step) ? luai_numlt(limit, init)
  221. : luai_numlt(init, limit))
  222. return 1; /* skip the loop */
  223. else {
  224. /* make sure all values are floats */
  225. setfltvalue(s2v(ra), limit);
  226. setfltvalue(s2v(ra + 1), step);
  227. setfltvalue(s2v(ra + 2), init); /* control variable */
  228. }
  229. }
  230. return 0;
  231. }
  232. /*
  233. ** Execute a step of a float numerical for loop, returning
  234. ** true iff the loop must continue. (The integer case is
  235. ** written online with opcode OP_FORLOOP, for performance.)
  236. */
  237. static int floatforloop (StkId ra) {
  238. lua_Number step = fltvalue(s2v(ra + 1));
  239. lua_Number limit = fltvalue(s2v(ra));
  240. lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */
  241. idx = luai_numadd(L, idx, step); /* increment index */
  242. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  243. : luai_numle(limit, idx)) {
  244. chgfltvalue(s2v(ra + 2), idx); /* update control variable */
  245. return 1; /* jump back */
  246. }
  247. else
  248. return 0; /* finish the loop */
  249. }
  250. /*
  251. ** Finish the table access 'val = t[key]'.
  252. */
  253. void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
  254. int hres) {
  255. int loop; /* counter to avoid infinite loops */
  256. const TValue *tm; /* metamethod */
  257. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  258. if (hres == HNOTATABLE) { /* 't' is not a table? */
  259. lua_assert(!ttistable(t));
  260. tm = luaT_gettmbyobj(L, t, TM_INDEX);
  261. if (l_unlikely(notm(tm)))
  262. luaG_typeerror(L, t, "index"); /* no metamethod */
  263. /* else will try the metamethod */
  264. }
  265. else { /* 't' is a table */
  266. tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */
  267. if (tm == NULL) { /* no metamethod? */
  268. setnilvalue(s2v(val)); /* result is nil */
  269. return;
  270. }
  271. /* else will try the metamethod */
  272. }
  273. if (ttisfunction(tm)) { /* is metamethod a function? */
  274. luaT_callTMres(L, tm, t, key, val); /* call it */
  275. return;
  276. }
  277. t = tm; /* else try to access 'tm[key]' */
  278. luaV_fastget(t, key, s2v(val), luaH_get, hres);
  279. if (hres == HOK)
  280. return; /* done */
  281. /* else repeat (tail call 'luaV_finishget') */
  282. }
  283. luaG_runerror(L, "'__index' chain too long; possible loop");
  284. }
  285. /*
  286. ** Finish a table assignment 't[key] = val'.
  287. */
  288. void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
  289. TValue *val, int hres) {
  290. int loop; /* counter to avoid infinite loops */
  291. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  292. const TValue *tm; /* '__newindex' metamethod */
  293. if (hres != HNOTATABLE) { /* is 't' a table? */
  294. Table *h = hvalue(t); /* save 't' table */
  295. tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */
  296. if (tm == NULL) { /* no metamethod? */
  297. luaH_finishset(L, h, key, val, hres); /* set new value */
  298. invalidateTMcache(h);
  299. luaC_barrierback(L, obj2gco(h), val);
  300. return;
  301. }
  302. /* else will try the metamethod */
  303. }
  304. else { /* not a table; check metamethod */
  305. tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
  306. if (l_unlikely(notm(tm)))
  307. luaG_typeerror(L, t, "index");
  308. }
  309. /* try the metamethod */
  310. if (ttisfunction(tm)) {
  311. luaT_callTM(L, tm, t, key, val);
  312. return;
  313. }
  314. t = tm; /* else repeat assignment over 'tm' */
  315. luaV_fastset(t, key, val, hres, luaH_pset);
  316. if (hres == HOK)
  317. return; /* done */
  318. /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
  319. }
  320. luaG_runerror(L, "'__newindex' chain too long; possible loop");
  321. }
  322. /*
  323. ** Compare two strings 'ts1' x 'ts2', returning an integer less-equal-
  324. ** -greater than zero if 'ts1' is less-equal-greater than 'ts2'.
  325. ** The code is a little tricky because it allows '\0' in the strings
  326. ** and it uses 'strcoll' (to respect locales) for each segment
  327. ** of the strings. Note that segments can compare equal but still
  328. ** have different lengths.
  329. */
  330. static int l_strcmp (const TString *ts1, const TString *ts2) {
  331. size_t rl1; /* real length */
  332. const char *s1 = getlstr(ts1, rl1);
  333. size_t rl2;
  334. const char *s2 = getlstr(ts2, rl2);
  335. for (;;) { /* for each segment */
  336. int temp = strcoll(s1, s2);
  337. if (temp != 0) /* not equal? */
  338. return temp; /* done */
  339. else { /* strings are equal up to a '\0' */
  340. size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */
  341. size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */
  342. if (zl2 == rl2) /* 's2' is finished? */
  343. return (zl1 == rl1) ? 0 : 1; /* check 's1' */
  344. else if (zl1 == rl1) /* 's1' is finished? */
  345. return -1; /* 's1' is less than 's2' ('s2' is not finished) */
  346. /* both strings longer than 'zl'; go on comparing after the '\0' */
  347. zl1++; zl2++;
  348. s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2;
  349. }
  350. }
  351. }
  352. /*
  353. ** Check whether integer 'i' is less than float 'f'. If 'i' has an
  354. ** exact representation as a float ('l_intfitsf'), compare numbers as
  355. ** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'.
  356. ** If 'ceil(f)' is out of integer range, either 'f' is greater than
  357. ** all integers or less than all integers.
  358. ** (The test with 'l_intfitsf' is only for performance; the else
  359. ** case is correct for all values, but it is slow due to the conversion
  360. ** from float to int.)
  361. ** When 'f' is NaN, comparisons must result in false.
  362. */
  363. l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
  364. if (l_intfitsf(i))
  365. return luai_numlt(cast_num(i), f); /* compare them as floats */
  366. else { /* i < f <=> i < ceil(f) */
  367. lua_Integer fi;
  368. if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
  369. return i < fi; /* compare them as integers */
  370. else /* 'f' is either greater or less than all integers */
  371. return f > 0; /* greater? */
  372. }
  373. }
  374. /*
  375. ** Check whether integer 'i' is less than or equal to float 'f'.
  376. ** See comments on previous function.
  377. */
  378. l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
  379. if (l_intfitsf(i))
  380. return luai_numle(cast_num(i), f); /* compare them as floats */
  381. else { /* i <= f <=> i <= floor(f) */
  382. lua_Integer fi;
  383. if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
  384. return i <= fi; /* compare them as integers */
  385. else /* 'f' is either greater or less than all integers */
  386. return f > 0; /* greater? */
  387. }
  388. }
  389. /*
  390. ** Check whether float 'f' is less than integer 'i'.
  391. ** See comments on previous function.
  392. */
  393. l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
  394. if (l_intfitsf(i))
  395. return luai_numlt(f, cast_num(i)); /* compare them as floats */
  396. else { /* f < i <=> floor(f) < i */
  397. lua_Integer fi;
  398. if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
  399. return fi < i; /* compare them as integers */
  400. else /* 'f' is either greater or less than all integers */
  401. return f < 0; /* less? */
  402. }
  403. }
  404. /*
  405. ** Check whether float 'f' is less than or equal to integer 'i'.
  406. ** See comments on previous function.
  407. */
  408. l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
  409. if (l_intfitsf(i))
  410. return luai_numle(f, cast_num(i)); /* compare them as floats */
  411. else { /* f <= i <=> ceil(f) <= i */
  412. lua_Integer fi;
  413. if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
  414. return fi <= i; /* compare them as integers */
  415. else /* 'f' is either greater or less than all integers */
  416. return f < 0; /* less? */
  417. }
  418. }
  419. /*
  420. ** Return 'l < r', for numbers.
  421. */
  422. l_sinline int LTnum (const TValue *l, const TValue *r) {
  423. lua_assert(ttisnumber(l) && ttisnumber(r));
  424. if (ttisinteger(l)) {
  425. lua_Integer li = ivalue(l);
  426. if (ttisinteger(r))
  427. return li < ivalue(r); /* both are integers */
  428. else /* 'l' is int and 'r' is float */
  429. return LTintfloat(li, fltvalue(r)); /* l < r ? */
  430. }
  431. else {
  432. lua_Number lf = fltvalue(l); /* 'l' must be float */
  433. if (ttisfloat(r))
  434. return luai_numlt(lf, fltvalue(r)); /* both are float */
  435. else /* 'l' is float and 'r' is int */
  436. return LTfloatint(lf, ivalue(r));
  437. }
  438. }
  439. /*
  440. ** Return 'l <= r', for numbers.
  441. */
  442. l_sinline int LEnum (const TValue *l, const TValue *r) {
  443. lua_assert(ttisnumber(l) && ttisnumber(r));
  444. if (ttisinteger(l)) {
  445. lua_Integer li = ivalue(l);
  446. if (ttisinteger(r))
  447. return li <= ivalue(r); /* both are integers */
  448. else /* 'l' is int and 'r' is float */
  449. return LEintfloat(li, fltvalue(r)); /* l <= r ? */
  450. }
  451. else {
  452. lua_Number lf = fltvalue(l); /* 'l' must be float */
  453. if (ttisfloat(r))
  454. return luai_numle(lf, fltvalue(r)); /* both are float */
  455. else /* 'l' is float and 'r' is int */
  456. return LEfloatint(lf, ivalue(r));
  457. }
  458. }
  459. /*
  460. ** return 'l < r' for non-numbers.
  461. */
  462. static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
  463. lua_assert(!ttisnumber(l) || !ttisnumber(r));
  464. if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  465. return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
  466. else
  467. return luaT_callorderTM(L, l, r, TM_LT);
  468. }
  469. /*
  470. ** Main operation less than; return 'l < r'.
  471. */
  472. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  473. if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
  474. return LTnum(l, r);
  475. else return lessthanothers(L, l, r);
  476. }
  477. /*
  478. ** return 'l <= r' for non-numbers.
  479. */
  480. static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
  481. lua_assert(!ttisnumber(l) || !ttisnumber(r));
  482. if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  483. return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  484. else
  485. return luaT_callorderTM(L, l, r, TM_LE);
  486. }
  487. /*
  488. ** Main operation less than or equal to; return 'l <= r'.
  489. */
  490. int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  491. if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
  492. return LEnum(l, r);
  493. else return lessequalothers(L, l, r);
  494. }
  495. /*
  496. ** Main operation for equality of Lua values; return 't1 == t2'.
  497. ** L == NULL means raw equality (no metamethods)
  498. */
  499. int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
  500. const TValue *tm;
  501. if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */
  502. if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER)
  503. return 0; /* only numbers can be equal with different variants */
  504. else { /* two numbers with different variants */
  505. /* One of them is an integer. If the other does not have an
  506. integer value, they cannot be equal; otherwise, compare their
  507. integer values. */
  508. lua_Integer i1, i2;
  509. return (luaV_tointegerns(t1, &i1, F2Ieq) &&
  510. luaV_tointegerns(t2, &i2, F2Ieq) &&
  511. i1 == i2);
  512. }
  513. }
  514. /* values have same type and same variant */
  515. switch (ttypetag(t1)) {
  516. case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1;
  517. case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2));
  518. case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
  519. case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  520. case LUA_VLCF: return fvalue(t1) == fvalue(t2);
  521. case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
  522. case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
  523. case LUA_VUSERDATA: {
  524. if (uvalue(t1) == uvalue(t2)) return 1;
  525. else if (L == NULL) return 0;
  526. tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
  527. if (tm == NULL)
  528. tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
  529. break; /* will try TM */
  530. }
  531. case LUA_VTABLE: {
  532. if (hvalue(t1) == hvalue(t2)) return 1;
  533. else if (L == NULL) return 0;
  534. tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
  535. if (tm == NULL)
  536. tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
  537. break; /* will try TM */
  538. }
  539. default:
  540. return gcvalue(t1) == gcvalue(t2);
  541. }
  542. if (tm == NULL) /* no TM? */
  543. return 0; /* objects are different */
  544. else {
  545. luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */
  546. return !l_isfalse(s2v(L->top.p));
  547. }
  548. }
  549. /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
  550. #define tostring(L,o) \
  551. (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
  552. #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
  553. /* copy strings in stack from top - n up to top - 1 to buffer */
  554. static void copy2buff (StkId top, int n, char *buff) {
  555. size_t tl = 0; /* size already copied */
  556. do {
  557. TString *st = tsvalue(s2v(top - n));
  558. size_t l; /* length of string being copied */
  559. const char *s = getlstr(st, l);
  560. memcpy(buff + tl, s, l * sizeof(char));
  561. tl += l;
  562. } while (--n > 0);
  563. }
  564. /*
  565. ** Main operation for concatenation: concat 'total' values in the stack,
  566. ** from 'L->top.p - total' up to 'L->top.p - 1'.
  567. */
  568. void luaV_concat (lua_State *L, int total) {
  569. if (total == 1)
  570. return; /* "all" values already concatenated */
  571. do {
  572. StkId top = L->top.p;
  573. int n = 2; /* number of elements handled in this pass (at least 2) */
  574. if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
  575. !tostring(L, s2v(top - 1)))
  576. luaT_tryconcatTM(L); /* may invalidate 'top' */
  577. else if (isemptystr(s2v(top - 1))) /* second operand is empty? */
  578. cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
  579. else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
  580. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  581. }
  582. else {
  583. /* at least two non-empty string values; get as many as possible */
  584. size_t tl = tsslen(tsvalue(s2v(top - 1)));
  585. TString *ts;
  586. /* collect total length and number of strings */
  587. for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
  588. size_t l = tsslen(tsvalue(s2v(top - n - 1)));
  589. if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) {
  590. L->top.p = top - total; /* pop strings to avoid wasting stack */
  591. luaG_runerror(L, "string length overflow");
  592. }
  593. tl += l;
  594. }
  595. if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
  596. char buff[LUAI_MAXSHORTLEN];
  597. copy2buff(top, n, buff); /* copy strings to buffer */
  598. ts = luaS_newlstr(L, buff, tl);
  599. }
  600. else { /* long string; copy strings directly to final result */
  601. ts = luaS_createlngstrobj(L, tl);
  602. copy2buff(top, n, getlngstr(ts));
  603. }
  604. setsvalue2s(L, top - n, ts); /* create result */
  605. }
  606. total -= n - 1; /* got 'n' strings to create one new */
  607. L->top.p -= n - 1; /* popped 'n' strings and pushed one */
  608. } while (total > 1); /* repeat until only 1 result left */
  609. }
  610. /*
  611. ** Main operation 'ra = #rb'.
  612. */
  613. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  614. const TValue *tm;
  615. switch (ttypetag(rb)) {
  616. case LUA_VTABLE: {
  617. Table *h = hvalue(rb);
  618. tm = fasttm(L, h->metatable, TM_LEN);
  619. if (tm) break; /* metamethod? break switch to call it */
  620. setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */
  621. return;
  622. }
  623. case LUA_VSHRSTR: {
  624. setivalue(s2v(ra), tsvalue(rb)->shrlen);
  625. return;
  626. }
  627. case LUA_VLNGSTR: {
  628. setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
  629. return;
  630. }
  631. default: { /* try metamethod */
  632. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  633. if (l_unlikely(notm(tm))) /* no metamethod? */
  634. luaG_typeerror(L, rb, "get length of");
  635. break;
  636. }
  637. }
  638. luaT_callTMres(L, tm, rb, rb, ra);
  639. }
  640. /*
  641. ** Integer division; return 'm // n', that is, floor(m/n).
  642. ** C division truncates its result (rounds towards zero).
  643. ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
  644. ** otherwise 'floor(q) == trunc(q) - 1'.
  645. */
  646. lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
  647. if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
  648. if (n == 0)
  649. luaG_runerror(L, "attempt to divide by zero");
  650. return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
  651. }
  652. else {
  653. lua_Integer q = m / n; /* perform C division */
  654. if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
  655. q -= 1; /* correct result for different rounding */
  656. return q;
  657. }
  658. }
  659. /*
  660. ** Integer modulus; return 'm % n'. (Assume that C '%' with
  661. ** negative operands follows C99 behavior. See previous comment
  662. ** about luaV_idiv.)
  663. */
  664. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  665. if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
  666. if (n == 0)
  667. luaG_runerror(L, "attempt to perform 'n%%0'");
  668. return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
  669. }
  670. else {
  671. lua_Integer r = m % n;
  672. if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */
  673. r += n; /* correct result for different rounding */
  674. return r;
  675. }
  676. }
  677. /*
  678. ** Float modulus
  679. */
  680. lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
  681. lua_Number r;
  682. luai_nummod(L, m, n, r);
  683. return r;
  684. }
  685. /* number of bits in an integer */
  686. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  687. /*
  688. ** Shift left operation. (Shift right just negates 'y'.)
  689. */
  690. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  691. if (y < 0) { /* shift right? */
  692. if (y <= -NBITS) return 0;
  693. else return intop(>>, x, -y);
  694. }
  695. else { /* shift left */
  696. if (y >= NBITS) return 0;
  697. else return intop(<<, x, y);
  698. }
  699. }
  700. /*
  701. ** create a new Lua closure, push it in the stack, and initialize
  702. ** its upvalues.
  703. */
  704. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  705. StkId ra) {
  706. int nup = p->sizeupvalues;
  707. Upvaldesc *uv = p->upvalues;
  708. int i;
  709. LClosure *ncl = luaF_newLclosure(L, nup);
  710. ncl->p = p;
  711. setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */
  712. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  713. if (uv[i].instack) /* upvalue refers to local variable? */
  714. ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
  715. else /* get upvalue from enclosing function */
  716. ncl->upvals[i] = encup[uv[i].idx];
  717. luaC_objbarrier(L, ncl, ncl->upvals[i]);
  718. }
  719. }
  720. /*
  721. ** finish execution of an opcode interrupted by a yield
  722. */
  723. void luaV_finishOp (lua_State *L) {
  724. CallInfo *ci = L->ci;
  725. StkId base = ci->func.p + 1;
  726. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  727. OpCode op = GET_OPCODE(inst);
  728. switch (op) { /* finish its execution */
  729. case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
  730. setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p);
  731. break;
  732. }
  733. case OP_UNM: case OP_BNOT: case OP_LEN:
  734. case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
  735. case OP_GETFIELD: case OP_SELF: {
  736. setobjs2s(L, base + GETARG_A(inst), --L->top.p);
  737. break;
  738. }
  739. case OP_LT: case OP_LE:
  740. case OP_LTI: case OP_LEI:
  741. case OP_GTI: case OP_GEI:
  742. case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
  743. int res = !l_isfalse(s2v(L->top.p - 1));
  744. L->top.p--;
  745. #if defined(LUA_COMPAT_LT_LE)
  746. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  747. ci->callstatus ^= CIST_LEQ; /* clear mark */
  748. res = !res; /* negate result */
  749. }
  750. #endif
  751. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  752. if (res != GETARG_k(inst)) /* condition failed? */
  753. ci->u.l.savedpc++; /* skip jump instruction */
  754. break;
  755. }
  756. case OP_CONCAT: {
  757. StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */
  758. int a = GETARG_A(inst); /* first element to concatenate */
  759. int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */
  760. setobjs2s(L, top - 2, top); /* put TM result in proper position */
  761. L->top.p = top - 1; /* top is one after last element (at top-2) */
  762. luaV_concat(L, total); /* concat them (may yield again) */
  763. break;
  764. }
  765. case OP_CLOSE: { /* yielded closing variables */
  766. ci->u.l.savedpc--; /* repeat instruction to close other vars. */
  767. break;
  768. }
  769. case OP_RETURN: { /* yielded closing variables */
  770. StkId ra = base + GETARG_A(inst);
  771. /* adjust top to signal correct number of returns, in case the
  772. return is "up to top" ('isIT') */
  773. L->top.p = ra + ci->u2.nres;
  774. /* repeat instruction to close other vars. and complete the return */
  775. ci->u.l.savedpc--;
  776. break;
  777. }
  778. default: {
  779. /* only these other opcodes can yield */
  780. lua_assert(op == OP_TFORCALL || op == OP_CALL ||
  781. op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE ||
  782. op == OP_SETI || op == OP_SETFIELD);
  783. break;
  784. }
  785. }
  786. }
  787. /*
  788. ** {==================================================================
  789. ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute'
  790. ** ===================================================================
  791. */
  792. #define l_addi(L,a,b) intop(+, a, b)
  793. #define l_subi(L,a,b) intop(-, a, b)
  794. #define l_muli(L,a,b) intop(*, a, b)
  795. #define l_band(a,b) intop(&, a, b)
  796. #define l_bor(a,b) intop(|, a, b)
  797. #define l_bxor(a,b) intop(^, a, b)
  798. #define l_lti(a,b) (a < b)
  799. #define l_lei(a,b) (a <= b)
  800. #define l_gti(a,b) (a > b)
  801. #define l_gei(a,b) (a >= b)
  802. /*
  803. ** Arithmetic operations with immediate operands. 'iop' is the integer
  804. ** operation, 'fop' is the float operation.
  805. */
  806. #define op_arithI(L,iop,fop) { \
  807. StkId ra = RA(i); \
  808. TValue *v1 = vRB(i); \
  809. int imm = GETARG_sC(i); \
  810. if (ttisinteger(v1)) { \
  811. lua_Integer iv1 = ivalue(v1); \
  812. pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \
  813. } \
  814. else if (ttisfloat(v1)) { \
  815. lua_Number nb = fltvalue(v1); \
  816. lua_Number fimm = cast_num(imm); \
  817. pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
  818. }}
  819. /*
  820. ** Auxiliary function for arithmetic operations over floats and others
  821. ** with two register operands.
  822. */
  823. #define op_arithf_aux(L,v1,v2,fop) { \
  824. lua_Number n1; lua_Number n2; \
  825. if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
  826. pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
  827. }}
  828. /*
  829. ** Arithmetic operations over floats and others with register operands.
  830. */
  831. #define op_arithf(L,fop) { \
  832. StkId ra = RA(i); \
  833. TValue *v1 = vRB(i); \
  834. TValue *v2 = vRC(i); \
  835. op_arithf_aux(L, v1, v2, fop); }
  836. /*
  837. ** Arithmetic operations with K operands for floats.
  838. */
  839. #define op_arithfK(L,fop) { \
  840. StkId ra = RA(i); \
  841. TValue *v1 = vRB(i); \
  842. TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \
  843. op_arithf_aux(L, v1, v2, fop); }
  844. /*
  845. ** Arithmetic operations over integers and floats.
  846. */
  847. #define op_arith_aux(L,v1,v2,iop,fop) { \
  848. StkId ra = RA(i); \
  849. if (ttisinteger(v1) && ttisinteger(v2)) { \
  850. lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
  851. pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
  852. } \
  853. else op_arithf_aux(L, v1, v2, fop); }
  854. /*
  855. ** Arithmetic operations with register operands.
  856. */
  857. #define op_arith(L,iop,fop) { \
  858. TValue *v1 = vRB(i); \
  859. TValue *v2 = vRC(i); \
  860. op_arith_aux(L, v1, v2, iop, fop); }
  861. /*
  862. ** Arithmetic operations with K operands.
  863. */
  864. #define op_arithK(L,iop,fop) { \
  865. TValue *v1 = vRB(i); \
  866. TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \
  867. op_arith_aux(L, v1, v2, iop, fop); }
  868. /*
  869. ** Bitwise operations with constant operand.
  870. */
  871. #define op_bitwiseK(L,op) { \
  872. StkId ra = RA(i); \
  873. TValue *v1 = vRB(i); \
  874. TValue *v2 = KC(i); \
  875. lua_Integer i1; \
  876. lua_Integer i2 = ivalue(v2); \
  877. if (tointegerns(v1, &i1)) { \
  878. pc++; setivalue(s2v(ra), op(i1, i2)); \
  879. }}
  880. /*
  881. ** Bitwise operations with register operands.
  882. */
  883. #define op_bitwise(L,op) { \
  884. StkId ra = RA(i); \
  885. TValue *v1 = vRB(i); \
  886. TValue *v2 = vRC(i); \
  887. lua_Integer i1; lua_Integer i2; \
  888. if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \
  889. pc++; setivalue(s2v(ra), op(i1, i2)); \
  890. }}
  891. /*
  892. ** Order operations with register operands. 'opn' actually works
  893. ** for all numbers, but the fast track improves performance for
  894. ** integers.
  895. */
  896. #define op_order(L,opi,opn,other) { \
  897. StkId ra = RA(i); \
  898. int cond; \
  899. TValue *rb = vRB(i); \
  900. if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \
  901. lua_Integer ia = ivalue(s2v(ra)); \
  902. lua_Integer ib = ivalue(rb); \
  903. cond = opi(ia, ib); \
  904. } \
  905. else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \
  906. cond = opn(s2v(ra), rb); \
  907. else \
  908. Protect(cond = other(L, s2v(ra), rb)); \
  909. docondjump(); }
  910. /*
  911. ** Order operations with immediate operand. (Immediate operand is
  912. ** always small enough to have an exact representation as a float.)
  913. */
  914. #define op_orderI(L,opi,opf,inv,tm) { \
  915. StkId ra = RA(i); \
  916. int cond; \
  917. int im = GETARG_sB(i); \
  918. if (ttisinteger(s2v(ra))) \
  919. cond = opi(ivalue(s2v(ra)), im); \
  920. else if (ttisfloat(s2v(ra))) { \
  921. lua_Number fa = fltvalue(s2v(ra)); \
  922. lua_Number fim = cast_num(im); \
  923. cond = opf(fa, fim); \
  924. } \
  925. else { \
  926. int isf = GETARG_C(i); \
  927. Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \
  928. } \
  929. docondjump(); }
  930. /* }================================================================== */
  931. /*
  932. ** {==================================================================
  933. ** Function 'luaV_execute': main interpreter loop
  934. ** ===================================================================
  935. */
  936. /*
  937. ** some macros for common tasks in 'luaV_execute'
  938. */
  939. #define RA(i) (base+GETARG_A(i))
  940. #define RB(i) (base+GETARG_B(i))
  941. #define vRB(i) s2v(RB(i))
  942. #define KB(i) (k+GETARG_B(i))
  943. #define RC(i) (base+GETARG_C(i))
  944. #define vRC(i) s2v(RC(i))
  945. #define KC(i) (k+GETARG_C(i))
  946. #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
  947. #define updatetrap(ci) (trap = ci->u.l.trap)
  948. #define updatebase(ci) (base = ci->func.p + 1)
  949. #define updatestack(ci) \
  950. { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } }
  951. /*
  952. ** Execute a jump instruction. The 'updatetrap' allows signals to stop
  953. ** tight loops. (Without it, the local copy of 'trap' could never change.)
  954. */
  955. #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); }
  956. /* for test instructions, execute the jump instruction that follows it */
  957. #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); }
  958. /*
  959. ** do a conditional jump: skip next instruction if 'cond' is not what
  960. ** was expected (parameter 'k'), else do next instruction, which must
  961. ** be a jump.
  962. */
  963. #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci);
  964. /*
  965. ** Correct global 'pc'.
  966. */
  967. #define savepc(L) (ci->u.l.savedpc = pc)
  968. /*
  969. ** Whenever code can raise errors, the global 'pc' and the global
  970. ** 'top' must be correct to report occasional errors.
  971. */
  972. #define savestate(L,ci) (savepc(L), L->top.p = ci->top.p)
  973. /*
  974. ** Protect code that, in general, can raise errors, reallocate the
  975. ** stack, and change the hooks.
  976. */
  977. #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci))
  978. /* special version that does not change the top */
  979. #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci))
  980. /*
  981. ** Protect code that can only raise errors. (That is, it cannot change
  982. ** the stack or hooks.)
  983. */
  984. #define halfProtect(exp) (savestate(L,ci), (exp))
  985. /* 'c' is the limit of live values in the stack */
  986. #define checkGC(L,c) \
  987. { luaC_condGC(L, (savepc(L), L->top.p = (c)), \
  988. updatetrap(ci)); \
  989. luai_threadyield(L); }
  990. /* fetch an instruction and prepare its execution */
  991. #define vmfetch() { \
  992. if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \
  993. trap = luaG_traceexec(L, pc); /* handle hooks */ \
  994. updatebase(ci); /* correct stack */ \
  995. } \
  996. i = *(pc++); \
  997. }
  998. #define vmdispatch(o) switch(o)
  999. #define vmcase(l) case l:
  1000. #define vmbreak break
  1001. void luaV_execute (lua_State *L, CallInfo *ci) {
  1002. LClosure *cl;
  1003. TValue *k;
  1004. StkId base;
  1005. const Instruction *pc;
  1006. int trap;
  1007. #if LUA_USE_JUMPTABLE
  1008. #include "ljumptab.h"
  1009. #endif
  1010. startfunc:
  1011. trap = L->hookmask;
  1012. returning: /* trap already set */
  1013. cl = ci_func(ci);
  1014. k = cl->p->k;
  1015. pc = ci->u.l.savedpc;
  1016. if (l_unlikely(trap))
  1017. trap = luaG_tracecall(L);
  1018. base = ci->func.p + 1;
  1019. /* main loop of interpreter */
  1020. for (;;) {
  1021. Instruction i; /* instruction being executed */
  1022. vmfetch();
  1023. #if 0
  1024. /* low-level line tracing for debugging Lua */
  1025. printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p)));
  1026. #endif
  1027. lua_assert(base == ci->func.p + 1);
  1028. lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p);
  1029. /* invalidate top for instructions not expecting it */
  1030. lua_assert(isIT(i) || (cast_void(L->top.p = base), 1));
  1031. vmdispatch (GET_OPCODE(i)) {
  1032. vmcase(OP_MOVE) {
  1033. StkId ra = RA(i);
  1034. setobjs2s(L, ra, RB(i));
  1035. vmbreak;
  1036. }
  1037. vmcase(OP_LOADI) {
  1038. StkId ra = RA(i);
  1039. lua_Integer b = GETARG_sBx(i);
  1040. setivalue(s2v(ra), b);
  1041. vmbreak;
  1042. }
  1043. vmcase(OP_LOADF) {
  1044. StkId ra = RA(i);
  1045. int b = GETARG_sBx(i);
  1046. setfltvalue(s2v(ra), cast_num(b));
  1047. vmbreak;
  1048. }
  1049. vmcase(OP_LOADK) {
  1050. StkId ra = RA(i);
  1051. TValue *rb = k + GETARG_Bx(i);
  1052. setobj2s(L, ra, rb);
  1053. vmbreak;
  1054. }
  1055. vmcase(OP_LOADKX) {
  1056. StkId ra = RA(i);
  1057. TValue *rb;
  1058. rb = k + GETARG_Ax(*pc); pc++;
  1059. setobj2s(L, ra, rb);
  1060. vmbreak;
  1061. }
  1062. vmcase(OP_LOADFALSE) {
  1063. StkId ra = RA(i);
  1064. setbfvalue(s2v(ra));
  1065. vmbreak;
  1066. }
  1067. vmcase(OP_LFALSESKIP) {
  1068. StkId ra = RA(i);
  1069. setbfvalue(s2v(ra));
  1070. pc++; /* skip next instruction */
  1071. vmbreak;
  1072. }
  1073. vmcase(OP_LOADTRUE) {
  1074. StkId ra = RA(i);
  1075. setbtvalue(s2v(ra));
  1076. vmbreak;
  1077. }
  1078. vmcase(OP_LOADNIL) {
  1079. StkId ra = RA(i);
  1080. int b = GETARG_B(i);
  1081. do {
  1082. setnilvalue(s2v(ra++));
  1083. } while (b--);
  1084. vmbreak;
  1085. }
  1086. vmcase(OP_GETUPVAL) {
  1087. StkId ra = RA(i);
  1088. int b = GETARG_B(i);
  1089. setobj2s(L, ra, cl->upvals[b]->v.p);
  1090. vmbreak;
  1091. }
  1092. vmcase(OP_SETUPVAL) {
  1093. StkId ra = RA(i);
  1094. UpVal *uv = cl->upvals[GETARG_B(i)];
  1095. setobj(L, uv->v.p, s2v(ra));
  1096. luaC_barrier(L, uv, s2v(ra));
  1097. vmbreak;
  1098. }
  1099. vmcase(OP_GETTABUP) {
  1100. StkId ra = RA(i);
  1101. TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
  1102. TValue *rc = KC(i);
  1103. TString *key = tsvalue(rc); /* key must be a short string */
  1104. int hres;
  1105. luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, hres);
  1106. if (hres != HOK)
  1107. Protect(luaV_finishget(L, upval, rc, ra, hres));
  1108. vmbreak;
  1109. }
  1110. vmcase(OP_GETTABLE) {
  1111. StkId ra = RA(i);
  1112. TValue *rb = vRB(i);
  1113. TValue *rc = vRC(i);
  1114. int hres;
  1115. if (ttisinteger(rc)) { /* fast track for integers? */
  1116. luaV_fastgeti(rb, ivalue(rc), s2v(ra), hres);
  1117. }
  1118. else
  1119. luaV_fastget(rb, rc, s2v(ra), luaH_get, hres);
  1120. if (hres != HOK) /* fast track for integers? */
  1121. Protect(luaV_finishget(L, rb, rc, ra, hres));
  1122. vmbreak;
  1123. }
  1124. vmcase(OP_GETI) {
  1125. StkId ra = RA(i);
  1126. TValue *rb = vRB(i);
  1127. int c = GETARG_C(i);
  1128. int hres;
  1129. luaV_fastgeti(rb, c, s2v(ra), hres);
  1130. if (hres != HOK) {
  1131. TValue key;
  1132. setivalue(&key, c);
  1133. Protect(luaV_finishget(L, rb, &key, ra, hres));
  1134. }
  1135. vmbreak;
  1136. }
  1137. vmcase(OP_GETFIELD) {
  1138. StkId ra = RA(i);
  1139. TValue *rb = vRB(i);
  1140. TValue *rc = KC(i);
  1141. TString *key = tsvalue(rc); /* key must be a short string */
  1142. int hres;
  1143. luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, hres);
  1144. if (hres != HOK)
  1145. Protect(luaV_finishget(L, rb, rc, ra, hres));
  1146. vmbreak;
  1147. }
  1148. vmcase(OP_SETTABUP) {
  1149. int hres;
  1150. TValue *upval = cl->upvals[GETARG_A(i)]->v.p;
  1151. TValue *rb = KB(i);
  1152. TValue *rc = RKC(i);
  1153. TString *key = tsvalue(rb); /* key must be a short string */
  1154. luaV_fastset(upval, key, rc, hres, luaH_psetshortstr);
  1155. if (hres == HOK)
  1156. luaV_finishfastset(L, upval, rc);
  1157. else
  1158. Protect(luaV_finishset(L, upval, rb, rc, hres));
  1159. vmbreak;
  1160. }
  1161. vmcase(OP_SETTABLE) {
  1162. StkId ra = RA(i);
  1163. int hres;
  1164. TValue *rb = vRB(i); /* key (table is in 'ra') */
  1165. TValue *rc = RKC(i); /* value */
  1166. if (ttisinteger(rb)) { /* fast track for integers? */
  1167. luaV_fastseti(s2v(ra), ivalue(rb), rc, hres);
  1168. }
  1169. else {
  1170. luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset);
  1171. }
  1172. if (hres == HOK)
  1173. luaV_finishfastset(L, s2v(ra), rc);
  1174. else
  1175. Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
  1176. vmbreak;
  1177. }
  1178. vmcase(OP_SETI) {
  1179. StkId ra = RA(i);
  1180. int hres;
  1181. int b = GETARG_B(i);
  1182. TValue *rc = RKC(i);
  1183. luaV_fastseti(s2v(ra), b, rc, hres);
  1184. if (hres == HOK)
  1185. luaV_finishfastset(L, s2v(ra), rc);
  1186. else {
  1187. TValue key;
  1188. setivalue(&key, b);
  1189. Protect(luaV_finishset(L, s2v(ra), &key, rc, hres));
  1190. }
  1191. vmbreak;
  1192. }
  1193. vmcase(OP_SETFIELD) {
  1194. StkId ra = RA(i);
  1195. int hres;
  1196. TValue *rb = KB(i);
  1197. TValue *rc = RKC(i);
  1198. TString *key = tsvalue(rb); /* key must be a short string */
  1199. luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr);
  1200. if (hres == HOK)
  1201. luaV_finishfastset(L, s2v(ra), rc);
  1202. else
  1203. Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
  1204. vmbreak;
  1205. }
  1206. vmcase(OP_NEWTABLE) {
  1207. StkId ra = RA(i);
  1208. int b = GETARG_B(i); /* log2(hash size) + 1 */
  1209. int c = GETARG_C(i); /* array size */
  1210. Table *t;
  1211. if (b > 0)
  1212. b = 1 << (b - 1); /* size is 2^(b - 1) */
  1213. lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0));
  1214. if (TESTARG_k(i)) /* non-zero extra argument? */
  1215. c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */
  1216. pc++; /* skip extra argument */
  1217. L->top.p = ra + 1; /* correct top in case of emergency GC */
  1218. t = luaH_new(L); /* memory allocation */
  1219. sethvalue2s(L, ra, t);
  1220. if (b != 0 || c != 0)
  1221. luaH_resize(L, t, c, b); /* idem */
  1222. checkGC(L, ra + 1);
  1223. vmbreak;
  1224. }
  1225. vmcase(OP_SELF) {
  1226. StkId ra = RA(i);
  1227. int hres;
  1228. TValue *rb = vRB(i);
  1229. TValue *rc = RKC(i);
  1230. TString *key = tsvalue(rc); /* key must be a string */
  1231. setobj2s(L, ra + 1, rb);
  1232. luaV_fastget(rb, key, s2v(ra), luaH_getstr, hres);
  1233. if (hres != HOK)
  1234. Protect(luaV_finishget(L, rb, rc, ra, hres));
  1235. vmbreak;
  1236. }
  1237. vmcase(OP_ADDI) {
  1238. op_arithI(L, l_addi, luai_numadd);
  1239. vmbreak;
  1240. }
  1241. vmcase(OP_ADDK) {
  1242. op_arithK(L, l_addi, luai_numadd);
  1243. vmbreak;
  1244. }
  1245. vmcase(OP_SUBK) {
  1246. op_arithK(L, l_subi, luai_numsub);
  1247. vmbreak;
  1248. }
  1249. vmcase(OP_MULK) {
  1250. op_arithK(L, l_muli, luai_nummul);
  1251. vmbreak;
  1252. }
  1253. vmcase(OP_MODK) {
  1254. savestate(L, ci); /* in case of division by 0 */
  1255. op_arithK(L, luaV_mod, luaV_modf);
  1256. vmbreak;
  1257. }
  1258. vmcase(OP_POWK) {
  1259. op_arithfK(L, luai_numpow);
  1260. vmbreak;
  1261. }
  1262. vmcase(OP_DIVK) {
  1263. op_arithfK(L, luai_numdiv);
  1264. vmbreak;
  1265. }
  1266. vmcase(OP_IDIVK) {
  1267. savestate(L, ci); /* in case of division by 0 */
  1268. op_arithK(L, luaV_idiv, luai_numidiv);
  1269. vmbreak;
  1270. }
  1271. vmcase(OP_BANDK) {
  1272. op_bitwiseK(L, l_band);
  1273. vmbreak;
  1274. }
  1275. vmcase(OP_BORK) {
  1276. op_bitwiseK(L, l_bor);
  1277. vmbreak;
  1278. }
  1279. vmcase(OP_BXORK) {
  1280. op_bitwiseK(L, l_bxor);
  1281. vmbreak;
  1282. }
  1283. vmcase(OP_SHRI) {
  1284. StkId ra = RA(i);
  1285. TValue *rb = vRB(i);
  1286. int ic = GETARG_sC(i);
  1287. lua_Integer ib;
  1288. if (tointegerns(rb, &ib)) {
  1289. pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
  1290. }
  1291. vmbreak;
  1292. }
  1293. vmcase(OP_SHLI) {
  1294. StkId ra = RA(i);
  1295. TValue *rb = vRB(i);
  1296. int ic = GETARG_sC(i);
  1297. lua_Integer ib;
  1298. if (tointegerns(rb, &ib)) {
  1299. pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
  1300. }
  1301. vmbreak;
  1302. }
  1303. vmcase(OP_ADD) {
  1304. op_arith(L, l_addi, luai_numadd);
  1305. vmbreak;
  1306. }
  1307. vmcase(OP_SUB) {
  1308. op_arith(L, l_subi, luai_numsub);
  1309. vmbreak;
  1310. }
  1311. vmcase(OP_MUL) {
  1312. op_arith(L, l_muli, luai_nummul);
  1313. vmbreak;
  1314. }
  1315. vmcase(OP_MOD) {
  1316. savestate(L, ci); /* in case of division by 0 */
  1317. op_arith(L, luaV_mod, luaV_modf);
  1318. vmbreak;
  1319. }
  1320. vmcase(OP_POW) {
  1321. op_arithf(L, luai_numpow);
  1322. vmbreak;
  1323. }
  1324. vmcase(OP_DIV) { /* float division (always with floats) */
  1325. op_arithf(L, luai_numdiv);
  1326. vmbreak;
  1327. }
  1328. vmcase(OP_IDIV) { /* floor division */
  1329. savestate(L, ci); /* in case of division by 0 */
  1330. op_arith(L, luaV_idiv, luai_numidiv);
  1331. vmbreak;
  1332. }
  1333. vmcase(OP_BAND) {
  1334. op_bitwise(L, l_band);
  1335. vmbreak;
  1336. }
  1337. vmcase(OP_BOR) {
  1338. op_bitwise(L, l_bor);
  1339. vmbreak;
  1340. }
  1341. vmcase(OP_BXOR) {
  1342. op_bitwise(L, l_bxor);
  1343. vmbreak;
  1344. }
  1345. vmcase(OP_SHR) {
  1346. op_bitwise(L, luaV_shiftr);
  1347. vmbreak;
  1348. }
  1349. vmcase(OP_SHL) {
  1350. op_bitwise(L, luaV_shiftl);
  1351. vmbreak;
  1352. }
  1353. vmcase(OP_MMBIN) {
  1354. StkId ra = RA(i);
  1355. Instruction pi = *(pc - 2); /* original arith. expression */
  1356. TValue *rb = vRB(i);
  1357. TMS tm = (TMS)GETARG_C(i);
  1358. StkId result = RA(pi);
  1359. lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
  1360. Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
  1361. vmbreak;
  1362. }
  1363. vmcase(OP_MMBINI) {
  1364. StkId ra = RA(i);
  1365. Instruction pi = *(pc - 2); /* original arith. expression */
  1366. int imm = GETARG_sB(i);
  1367. TMS tm = (TMS)GETARG_C(i);
  1368. int flip = GETARG_k(i);
  1369. StkId result = RA(pi);
  1370. Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
  1371. vmbreak;
  1372. }
  1373. vmcase(OP_MMBINK) {
  1374. StkId ra = RA(i);
  1375. Instruction pi = *(pc - 2); /* original arith. expression */
  1376. TValue *imm = KB(i);
  1377. TMS tm = (TMS)GETARG_C(i);
  1378. int flip = GETARG_k(i);
  1379. StkId result = RA(pi);
  1380. Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
  1381. vmbreak;
  1382. }
  1383. vmcase(OP_UNM) {
  1384. StkId ra = RA(i);
  1385. TValue *rb = vRB(i);
  1386. lua_Number nb;
  1387. if (ttisinteger(rb)) {
  1388. lua_Integer ib = ivalue(rb);
  1389. setivalue(s2v(ra), intop(-, 0, ib));
  1390. }
  1391. else if (tonumberns(rb, nb)) {
  1392. setfltvalue(s2v(ra), luai_numunm(L, nb));
  1393. }
  1394. else
  1395. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1396. vmbreak;
  1397. }
  1398. vmcase(OP_BNOT) {
  1399. StkId ra = RA(i);
  1400. TValue *rb = vRB(i);
  1401. lua_Integer ib;
  1402. if (tointegerns(rb, &ib)) {
  1403. setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
  1404. }
  1405. else
  1406. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1407. vmbreak;
  1408. }
  1409. vmcase(OP_NOT) {
  1410. StkId ra = RA(i);
  1411. TValue *rb = vRB(i);
  1412. if (l_isfalse(rb))
  1413. setbtvalue(s2v(ra));
  1414. else
  1415. setbfvalue(s2v(ra));
  1416. vmbreak;
  1417. }
  1418. vmcase(OP_LEN) {
  1419. StkId ra = RA(i);
  1420. Protect(luaV_objlen(L, ra, vRB(i)));
  1421. vmbreak;
  1422. }
  1423. vmcase(OP_CONCAT) {
  1424. StkId ra = RA(i);
  1425. int n = GETARG_B(i); /* number of elements to concatenate */
  1426. L->top.p = ra + n; /* mark the end of concat operands */
  1427. ProtectNT(luaV_concat(L, n));
  1428. checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */
  1429. vmbreak;
  1430. }
  1431. vmcase(OP_CLOSE) {
  1432. StkId ra = RA(i);
  1433. Protect(luaF_close(L, ra, LUA_OK, 1));
  1434. vmbreak;
  1435. }
  1436. vmcase(OP_TBC) {
  1437. StkId ra = RA(i);
  1438. /* create new to-be-closed upvalue */
  1439. halfProtect(luaF_newtbcupval(L, ra));
  1440. vmbreak;
  1441. }
  1442. vmcase(OP_JMP) {
  1443. dojump(ci, i, 0);
  1444. vmbreak;
  1445. }
  1446. vmcase(OP_EQ) {
  1447. StkId ra = RA(i);
  1448. int cond;
  1449. TValue *rb = vRB(i);
  1450. Protect(cond = luaV_equalobj(L, s2v(ra), rb));
  1451. docondjump();
  1452. vmbreak;
  1453. }
  1454. vmcase(OP_LT) {
  1455. op_order(L, l_lti, LTnum, lessthanothers);
  1456. vmbreak;
  1457. }
  1458. vmcase(OP_LE) {
  1459. op_order(L, l_lei, LEnum, lessequalothers);
  1460. vmbreak;
  1461. }
  1462. vmcase(OP_EQK) {
  1463. StkId ra = RA(i);
  1464. TValue *rb = KB(i);
  1465. /* basic types do not use '__eq'; we can use raw equality */
  1466. int cond = luaV_rawequalobj(s2v(ra), rb);
  1467. docondjump();
  1468. vmbreak;
  1469. }
  1470. vmcase(OP_EQI) {
  1471. StkId ra = RA(i);
  1472. int cond;
  1473. int im = GETARG_sB(i);
  1474. if (ttisinteger(s2v(ra)))
  1475. cond = (ivalue(s2v(ra)) == im);
  1476. else if (ttisfloat(s2v(ra)))
  1477. cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
  1478. else
  1479. cond = 0; /* other types cannot be equal to a number */
  1480. docondjump();
  1481. vmbreak;
  1482. }
  1483. vmcase(OP_LTI) {
  1484. op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
  1485. vmbreak;
  1486. }
  1487. vmcase(OP_LEI) {
  1488. op_orderI(L, l_lei, luai_numle, 0, TM_LE);
  1489. vmbreak;
  1490. }
  1491. vmcase(OP_GTI) {
  1492. op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
  1493. vmbreak;
  1494. }
  1495. vmcase(OP_GEI) {
  1496. op_orderI(L, l_gei, luai_numge, 1, TM_LE);
  1497. vmbreak;
  1498. }
  1499. vmcase(OP_TEST) {
  1500. StkId ra = RA(i);
  1501. int cond = !l_isfalse(s2v(ra));
  1502. docondjump();
  1503. vmbreak;
  1504. }
  1505. vmcase(OP_TESTSET) {
  1506. StkId ra = RA(i);
  1507. TValue *rb = vRB(i);
  1508. if (l_isfalse(rb) == GETARG_k(i))
  1509. pc++;
  1510. else {
  1511. setobj2s(L, ra, rb);
  1512. donextjump(ci);
  1513. }
  1514. vmbreak;
  1515. }
  1516. vmcase(OP_CALL) {
  1517. StkId ra = RA(i);
  1518. CallInfo *newci;
  1519. int b = GETARG_B(i);
  1520. int nresults = GETARG_C(i) - 1;
  1521. if (b != 0) /* fixed number of arguments? */
  1522. L->top.p = ra + b; /* top signals number of arguments */
  1523. /* else previous instruction set top */
  1524. savepc(L); /* in case of errors */
  1525. if ((newci = luaD_precall(L, ra, nresults)) == NULL)
  1526. updatetrap(ci); /* C call; nothing else to be done */
  1527. else { /* Lua call: run function in this same C frame */
  1528. ci = newci;
  1529. goto startfunc;
  1530. }
  1531. vmbreak;
  1532. }
  1533. vmcase(OP_TAILCALL) {
  1534. StkId ra = RA(i);
  1535. int b = GETARG_B(i); /* number of arguments + 1 (function) */
  1536. int n; /* number of results when calling a C function */
  1537. int nparams1 = GETARG_C(i);
  1538. /* delta is virtual 'func' - real 'func' (vararg functions) */
  1539. int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
  1540. if (b != 0)
  1541. L->top.p = ra + b;
  1542. else /* previous instruction set top */
  1543. b = cast_int(L->top.p - ra);
  1544. savepc(ci); /* several calls here can raise errors */
  1545. if (TESTARG_k(i)) {
  1546. luaF_closeupval(L, base); /* close upvalues from current call */
  1547. lua_assert(L->tbclist.p < base); /* no pending tbc variables */
  1548. lua_assert(base == ci->func.p + 1);
  1549. }
  1550. if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */
  1551. goto startfunc; /* execute the callee */
  1552. else { /* C function? */
  1553. ci->func.p -= delta; /* restore 'func' (if vararg) */
  1554. luaD_poscall(L, ci, n); /* finish caller */
  1555. updatetrap(ci); /* 'luaD_poscall' can change hooks */
  1556. goto ret; /* caller returns after the tail call */
  1557. }
  1558. }
  1559. vmcase(OP_RETURN) {
  1560. StkId ra = RA(i);
  1561. int n = GETARG_B(i) - 1; /* number of results */
  1562. int nparams1 = GETARG_C(i);
  1563. if (n < 0) /* not fixed? */
  1564. n = cast_int(L->top.p - ra); /* get what is available */
  1565. savepc(ci);
  1566. if (TESTARG_k(i)) { /* may there be open upvalues? */
  1567. ci->u2.nres = n; /* save number of returns */
  1568. if (L->top.p < ci->top.p)
  1569. L->top.p = ci->top.p;
  1570. luaF_close(L, base, CLOSEKTOP, 1);
  1571. updatetrap(ci);
  1572. updatestack(ci);
  1573. }
  1574. if (nparams1) /* vararg function? */
  1575. ci->func.p -= ci->u.l.nextraargs + nparams1;
  1576. L->top.p = ra + n; /* set call for 'luaD_poscall' */
  1577. luaD_poscall(L, ci, n);
  1578. updatetrap(ci); /* 'luaD_poscall' can change hooks */
  1579. goto ret;
  1580. }
  1581. vmcase(OP_RETURN0) {
  1582. if (l_unlikely(L->hookmask)) {
  1583. StkId ra = RA(i);
  1584. L->top.p = ra;
  1585. savepc(ci);
  1586. luaD_poscall(L, ci, 0); /* no hurry... */
  1587. trap = 1;
  1588. }
  1589. else { /* do the 'poscall' here */
  1590. int nres;
  1591. L->ci = ci->previous; /* back to caller */
  1592. L->top.p = base - 1;
  1593. for (nres = ci->nresults; l_unlikely(nres > 0); nres--)
  1594. setnilvalue(s2v(L->top.p++)); /* all results are nil */
  1595. }
  1596. goto ret;
  1597. }
  1598. vmcase(OP_RETURN1) {
  1599. if (l_unlikely(L->hookmask)) {
  1600. StkId ra = RA(i);
  1601. L->top.p = ra + 1;
  1602. savepc(ci);
  1603. luaD_poscall(L, ci, 1); /* no hurry... */
  1604. trap = 1;
  1605. }
  1606. else { /* do the 'poscall' here */
  1607. int nres = ci->nresults;
  1608. L->ci = ci->previous; /* back to caller */
  1609. if (nres == 0)
  1610. L->top.p = base - 1; /* asked for no results */
  1611. else {
  1612. StkId ra = RA(i);
  1613. setobjs2s(L, base - 1, ra); /* at least this result */
  1614. L->top.p = base;
  1615. for (; l_unlikely(nres > 1); nres--)
  1616. setnilvalue(s2v(L->top.p++)); /* complete missing results */
  1617. }
  1618. }
  1619. ret: /* return from a Lua function */
  1620. if (ci->callstatus & CIST_FRESH)
  1621. return; /* end this frame */
  1622. else {
  1623. ci = ci->previous;
  1624. goto returning; /* continue running caller in this frame */
  1625. }
  1626. }
  1627. vmcase(OP_FORLOOP) {
  1628. StkId ra = RA(i);
  1629. if (ttisinteger(s2v(ra + 1))) { /* integer loop? */
  1630. lua_Unsigned count = l_castS2U(ivalue(s2v(ra)));
  1631. if (count > 0) { /* still more iterations? */
  1632. lua_Integer step = ivalue(s2v(ra + 1));
  1633. lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */
  1634. chgivalue(s2v(ra), count - 1); /* update counter */
  1635. idx = intop(+, idx, step); /* add step to index */
  1636. chgivalue(s2v(ra + 2), idx); /* update control variable */
  1637. pc -= GETARG_Bx(i); /* jump back */
  1638. }
  1639. }
  1640. else if (floatforloop(ra)) /* float loop */
  1641. pc -= GETARG_Bx(i); /* jump back */
  1642. updatetrap(ci); /* allows a signal to break the loop */
  1643. vmbreak;
  1644. }
  1645. vmcase(OP_FORPREP) {
  1646. StkId ra = RA(i);
  1647. savestate(L, ci); /* in case of errors */
  1648. if (forprep(L, ra))
  1649. pc += GETARG_Bx(i) + 1; /* skip the loop */
  1650. vmbreak;
  1651. }
  1652. vmcase(OP_TFORPREP) {
  1653. /* before: 'ra' has the iterator function, 'ra + 1' has the state,
  1654. 'ra + 2' has the initial value for the control variable, and
  1655. 'ra + 3' has the closing variable. This opcode then swaps the
  1656. control and the closing variables and marks the closing variable
  1657. as to-be-closed.
  1658. */
  1659. StkId ra = RA(i);
  1660. TValue temp; /* to swap control and closing variables */
  1661. setobj(L, &temp, s2v(ra + 3));
  1662. setobjs2s(L, ra + 3, ra + 2);
  1663. setobj2s(L, ra + 2, &temp);
  1664. /* create to-be-closed upvalue (if closing var. is not nil) */
  1665. halfProtect(luaF_newtbcupval(L, ra + 2));
  1666. pc += GETARG_Bx(i); /* go to end of the loop */
  1667. i = *(pc++); /* fetch next instruction */
  1668. lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
  1669. goto l_tforcall;
  1670. }
  1671. vmcase(OP_TFORCALL) {
  1672. l_tforcall: {
  1673. /* 'ra' has the iterator function, 'ra + 1' has the state,
  1674. 'ra + 2' has the closing variable, and 'ra + 3' has the control
  1675. variable. The call will use the stack starting at 'ra + 3',
  1676. so that it preserves the first three values, and the first
  1677. return will be the new value for the control variable.
  1678. */
  1679. StkId ra = RA(i);
  1680. setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */
  1681. setobjs2s(L, ra + 4, ra + 1); /* copy state */
  1682. setobjs2s(L, ra + 3, ra); /* copy function */
  1683. L->top.p = ra + 3 + 3;
  1684. ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */
  1685. updatestack(ci); /* stack may have changed */
  1686. i = *(pc++); /* go to next instruction */
  1687. lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
  1688. goto l_tforloop;
  1689. }}
  1690. vmcase(OP_TFORLOOP) {
  1691. l_tforloop: {
  1692. StkId ra = RA(i);
  1693. if (!ttisnil(s2v(ra + 3))) /* continue loop? */
  1694. pc -= GETARG_Bx(i); /* jump back */
  1695. vmbreak;
  1696. }}
  1697. vmcase(OP_SETLIST) {
  1698. StkId ra = RA(i);
  1699. int n = GETARG_B(i);
  1700. unsigned int last = GETARG_C(i);
  1701. Table *h = hvalue(s2v(ra));
  1702. if (n == 0)
  1703. n = cast_int(L->top.p - ra) - 1; /* get up to the top */
  1704. else
  1705. L->top.p = ci->top.p; /* correct top in case of emergency GC */
  1706. last += n;
  1707. if (TESTARG_k(i)) {
  1708. last += GETARG_Ax(*pc) * (MAXARG_C + 1);
  1709. pc++;
  1710. }
  1711. if (last > luaH_realasize(h)) /* needs more space? */
  1712. luaH_resizearray(L, h, last); /* preallocate it at once */
  1713. for (; n > 0; n--) {
  1714. TValue *val = s2v(ra + n);
  1715. obj2arr(h, last, val);
  1716. last--;
  1717. luaC_barrierback(L, obj2gco(h), val);
  1718. }
  1719. vmbreak;
  1720. }
  1721. vmcase(OP_CLOSURE) {
  1722. StkId ra = RA(i);
  1723. Proto *p = cl->p->p[GETARG_Bx(i)];
  1724. halfProtect(pushclosure(L, p, cl->upvals, base, ra));
  1725. checkGC(L, ra + 1);
  1726. vmbreak;
  1727. }
  1728. vmcase(OP_VARARG) {
  1729. StkId ra = RA(i);
  1730. int n = GETARG_C(i) - 1; /* required results */
  1731. Protect(luaT_getvarargs(L, ci, ra, n));
  1732. vmbreak;
  1733. }
  1734. vmcase(OP_VARARGPREP) {
  1735. ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p));
  1736. if (l_unlikely(trap)) { /* previous "Protect" updated trap */
  1737. luaD_hookcall(L, ci);
  1738. L->oldpc = 1; /* next opcode will be seen as a "new" line */
  1739. }
  1740. updatebase(ci); /* function has new base after adjustment */
  1741. vmbreak;
  1742. }
  1743. vmcase(OP_EXTRAARG) {
  1744. lua_assert(0);
  1745. vmbreak;
  1746. }
  1747. }
  1748. }
  1749. }
  1750. /* }================================================================== */