lvm.c 59 KB

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