lvm.c 58 KB

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