lvm.c 58 KB

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