lvm.c 58 KB

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