lvm.c 58 KB

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