lvm.c 58 KB

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