lvm.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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. TString *st = tsvalue(obj);
  75. size_t stlen;
  76. const char *s = getlstr(st, stlen);
  77. return (luaO_str2num(s, result) == stlen + 1);
  78. }
  79. }
  80. /*
  81. ** Try to convert a value to a float. The float case is already handled
  82. ** by the macro 'tonumber'.
  83. */
  84. int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
  85. TValue v;
  86. if (ttisinteger(obj)) {
  87. *n = cast_num(ivalue(obj));
  88. return 1;
  89. }
  90. else if (l_strton(obj, &v)) { /* string coercible to number? */
  91. *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */
  92. return 1;
  93. }
  94. else
  95. return 0; /* conversion failed */
  96. }
  97. /*
  98. ** try to convert a float to an integer, rounding according to 'mode'.
  99. */
  100. int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
  101. lua_Number f = l_floor(n);
  102. if (n != f) { /* not an integral value? */
  103. if (mode == F2Ieq) return 0; /* fails if mode demands integral value */
  104. else if (mode == F2Iceil) /* needs ceil? */
  105. f += 1; /* convert floor to ceil (remember: n != f) */
  106. }
  107. return lua_numbertointeger(f, p);
  108. }
  109. /*
  110. ** try to convert a value to an integer, rounding according to 'mode',
  111. ** without string coercion.
  112. ** ("Fast track" handled by macro 'tointegerns'.)
  113. */
  114. int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
  115. if (ttisfloat(obj))
  116. return luaV_flttointeger(fltvalue(obj), p, mode);
  117. else if (ttisinteger(obj)) {
  118. *p = ivalue(obj);
  119. return 1;
  120. }
  121. else
  122. return 0;
  123. }
  124. /*
  125. ** try to convert a value to an integer.
  126. */
  127. int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
  128. TValue v;
  129. if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
  130. obj = &v; /* change it to point to its corresponding number */
  131. return luaV_tointegerns(obj, p, mode);
  132. }
  133. /*
  134. ** Try to convert a 'for' limit to an integer, preserving the semantics
  135. ** of the loop. Return true if the loop must not run; otherwise, '*p'
  136. ** gets the integer limit.
  137. ** (The following explanation assumes a positive step; it is valid for
  138. ** negative steps mutatis mutandis.)
  139. ** If the limit is an integer or can be converted to an integer,
  140. ** rounding down, that is the limit.
  141. ** Otherwise, check whether the limit can be converted to a float. If
  142. ** the float is too large, clip it to LUA_MAXINTEGER. If the float
  143. ** is too negative, the loop should not run, because any initial
  144. ** integer value is greater than such limit; so, the function returns
  145. ** true to signal that. (For this latter case, no integer limit would be
  146. ** correct; even a limit of LUA_MININTEGER would run the loop once for
  147. ** an initial value equal to LUA_MININTEGER.)
  148. */
  149. static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
  150. lua_Integer *p, lua_Integer step) {
  151. if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
  152. /* not coercible to in integer */
  153. lua_Number flim; /* try to convert to float */
  154. if (!tonumber(lim, &flim)) /* cannot convert to float? */
  155. luaG_forerror(L, lim, "limit");
  156. /* else 'flim' is a float out of integer bounds */
  157. if (luai_numlt(0, flim)) { /* if it is positive, it is too large */
  158. if (step < 0) return 1; /* initial value must be less than it */
  159. *p = LUA_MAXINTEGER; /* truncate */
  160. }
  161. else { /* it is less than min integer */
  162. if (step > 0) return 1; /* initial value must be greater than it */
  163. *p = LUA_MININTEGER; /* truncate */
  164. }
  165. }
  166. return (step > 0 ? init > *p : init < *p); /* not to run? */
  167. }
  168. /*
  169. ** Prepare a numerical for loop (opcode OP_FORPREP).
  170. ** Before execution, stack is as follows:
  171. ** ra : initial value
  172. ** ra + 1 : limit
  173. ** ra + 2 : step
  174. ** Return true to skip the loop. Otherwise,
  175. ** after preparation, stack will be as follows:
  176. ** ra : loop counter (integer loops) or limit (float loops)
  177. ** ra + 1 : step
  178. ** ra + 2 : control variable
  179. */
  180. static int forprep (lua_State *L, StkId ra) {
  181. TValue *pinit = s2v(ra);
  182. TValue *plimit = s2v(ra + 1);
  183. TValue *pstep = s2v(ra + 2);
  184. if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
  185. lua_Integer init = ivalue(pinit);
  186. lua_Integer step = ivalue(pstep);
  187. lua_Integer limit;
  188. if (step == 0)
  189. luaG_runerror(L, "'for' step is zero");
  190. if (forlimit(L, init, plimit, &limit, step))
  191. return 1; /* skip the loop */
  192. else { /* prepare loop counter */
  193. lua_Unsigned count;
  194. if (step > 0) { /* ascending loop? */
  195. count = l_castS2U(limit) - l_castS2U(init);
  196. if (step != 1) /* avoid division in the too common case */
  197. count /= l_castS2U(step);
  198. }
  199. else { /* step < 0; descending loop */
  200. count = l_castS2U(init) - l_castS2U(limit);
  201. /* 'step+1' avoids negating 'mininteger' */
  202. count /= l_castS2U(-(step + 1)) + 1u;
  203. }
  204. /* use 'chgivalue' for places that for sure had integers */
  205. chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */
  206. setivalue(s2v(ra + 1), step); /* change limit to step */
  207. chgivalue(s2v(ra + 2), init); /* change step to init */
  208. }
  209. }
  210. else { /* try making all values floats */
  211. lua_Number init; lua_Number limit; lua_Number step;
  212. if (l_unlikely(!tonumber(plimit, &limit)))
  213. luaG_forerror(L, plimit, "limit");
  214. if (l_unlikely(!tonumber(pstep, &step)))
  215. luaG_forerror(L, pstep, "step");
  216. if (l_unlikely(!tonumber(pinit, &init)))
  217. luaG_forerror(L, pinit, "initial value");
  218. if (step == 0)
  219. luaG_runerror(L, "'for' step is zero");
  220. if (luai_numlt(0, step) ? luai_numlt(limit, init)
  221. : luai_numlt(init, limit))
  222. return 1; /* skip the loop */
  223. else {
  224. /* make sure all values are floats */
  225. setfltvalue(s2v(ra), limit);
  226. setfltvalue(s2v(ra + 1), step);
  227. setfltvalue(s2v(ra + 2), init); /* control variable */
  228. }
  229. }
  230. return 0;
  231. }
  232. /*
  233. ** Execute a step of a float numerical for loop, returning
  234. ** true iff the loop must continue. (The integer case is
  235. ** written online with opcode OP_FORLOOP, for performance.)
  236. */
  237. static int floatforloop (StkId ra) {
  238. lua_Number step = fltvalue(s2v(ra + 1));
  239. lua_Number limit = fltvalue(s2v(ra));
  240. lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */
  241. idx = luai_numadd(L, idx, step); /* increment index */
  242. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  243. : luai_numle(limit, idx)) {
  244. chgfltvalue(s2v(ra + 2), idx); /* update control variable */
  245. return 1; /* jump back */
  246. }
  247. else
  248. return 0; /* finish the loop */
  249. }
  250. /*
  251. ** Finish the table access 'val = t[key]' and return the tag of the result.
  252. */
  253. int luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
  254. int tag) {
  255. int loop; /* counter to avoid infinite loops */
  256. const TValue *tm; /* metamethod */
  257. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  258. if (tag == LUA_VNOTABLE) { /* 't' is not a table? */
  259. lua_assert(!ttistable(t));
  260. tm = luaT_gettmbyobj(L, t, TM_INDEX);
  261. if (l_unlikely(notm(tm)))
  262. luaG_typeerror(L, t, "index"); /* no metamethod */
  263. /* else will try the metamethod */
  264. }
  265. else { /* 't' is a table */
  266. tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */
  267. if (tm == NULL) { /* no metamethod? */
  268. setnilvalue(s2v(val)); /* result is nil */
  269. return LUA_VNIL;
  270. }
  271. /* else will try the metamethod */
  272. }
  273. if (ttisfunction(tm)) { /* is metamethod a function? */
  274. tag = luaT_callTMres(L, tm, t, key, val); /* call it */
  275. return tag; /* return tag of the result */
  276. }
  277. t = tm; /* else try to access 'tm[key]' */
  278. luaV_fastget(t, key, s2v(val), luaH_get, tag);
  279. if (!tagisempty(tag))
  280. return tag; /* done */
  281. /* else repeat (tail call 'luaV_finishget') */
  282. }
  283. luaG_runerror(L, "'__index' chain too long; possible loop");
  284. return 0; /* to avoid warnings */
  285. }
  286. /*
  287. ** Finish a table assignment 't[key] = val'.
  288. */
  289. void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
  290. TValue *val, int hres) {
  291. int loop; /* counter to avoid infinite loops */
  292. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  293. const TValue *tm; /* '__newindex' metamethod */
  294. if (hres != HNOTATABLE) { /* is 't' a table? */
  295. Table *h = hvalue(t); /* save 't' table */
  296. tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */
  297. if (tm == NULL) { /* no metamethod? */
  298. luaH_finishset(L, h, key, val, hres); /* set new value */
  299. invalidateTMcache(h);
  300. luaC_barrierback(L, obj2gco(h), val);
  301. return;
  302. }
  303. /* else will try the metamethod */
  304. }
  305. else { /* not a table; check metamethod */
  306. tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
  307. if (l_unlikely(notm(tm)))
  308. luaG_typeerror(L, t, "index");
  309. }
  310. /* try the metamethod */
  311. if (ttisfunction(tm)) {
  312. luaT_callTM(L, tm, t, key, val);
  313. return;
  314. }
  315. t = tm; /* else repeat assignment over 'tm' */
  316. luaV_fastset(t, key, val, hres, luaH_pset);
  317. if (hres == HOK)
  318. return; /* done */
  319. /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
  320. }
  321. luaG_runerror(L, "'__newindex' chain too long; possible loop");
  322. }
  323. /*
  324. ** Compare two strings 'ts1' x 'ts2', returning an integer less-equal-
  325. ** -greater than zero if 'ts1' is less-equal-greater than 'ts2'.
  326. ** The code is a little tricky because it allows '\0' in the strings
  327. ** and it uses 'strcoll' (to respect locales) for each segment
  328. ** of the strings. Note that segments can compare equal but still
  329. ** have different lengths.
  330. */
  331. static int l_strcmp (const TString *ts1, const TString *ts2) {
  332. size_t rl1; /* real length */
  333. const char *s1 = getlstr(ts1, rl1);
  334. size_t rl2;
  335. const char *s2 = getlstr(ts2, rl2);
  336. for (;;) { /* for each segment */
  337. int temp = strcoll(s1, s2);
  338. if (temp != 0) /* not equal? */
  339. return temp; /* done */
  340. else { /* strings are equal up to a '\0' */
  341. size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */
  342. size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */
  343. if (zl2 == rl2) /* 's2' is finished? */
  344. return (zl1 == rl1) ? 0 : 1; /* check 's1' */
  345. else if (zl1 == rl1) /* 's1' is finished? */
  346. return -1; /* 's1' is less than 's2' ('s2' is not finished) */
  347. /* both strings longer than 'zl'; go on comparing after the '\0' */
  348. zl1++; zl2++;
  349. s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2;
  350. }
  351. }
  352. }
  353. /*
  354. ** Check whether integer 'i' is less than float 'f'. If 'i' has an
  355. ** exact representation as a float ('l_intfitsf'), compare numbers as
  356. ** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'.
  357. ** If 'ceil(f)' is out of integer range, either 'f' is greater than
  358. ** all integers or less than all integers.
  359. ** (The test with 'l_intfitsf' is only for performance; the else
  360. ** case is correct for all values, but it is slow due to the conversion
  361. ** from float to int.)
  362. ** When 'f' is NaN, comparisons must result in false.
  363. */
  364. l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
  365. if (l_intfitsf(i))
  366. return luai_numlt(cast_num(i), f); /* compare them as floats */
  367. else { /* i < f <=> i < ceil(f) */
  368. lua_Integer fi;
  369. if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
  370. return i < fi; /* compare them as integers */
  371. else /* 'f' is either greater or less than all integers */
  372. return f > 0; /* greater? */
  373. }
  374. }
  375. /*
  376. ** Check whether integer 'i' is less than or equal to float 'f'.
  377. ** See comments on previous function.
  378. */
  379. l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
  380. if (l_intfitsf(i))
  381. return luai_numle(cast_num(i), f); /* compare them as floats */
  382. else { /* i <= f <=> i <= floor(f) */
  383. lua_Integer fi;
  384. if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
  385. return i <= fi; /* compare them as integers */
  386. else /* 'f' is either greater or less than all integers */
  387. return f > 0; /* greater? */
  388. }
  389. }
  390. /*
  391. ** Check whether float 'f' is less than integer 'i'.
  392. ** See comments on previous function.
  393. */
  394. l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
  395. if (l_intfitsf(i))
  396. return luai_numlt(f, cast_num(i)); /* compare them as floats */
  397. else { /* f < i <=> floor(f) < i */
  398. lua_Integer fi;
  399. if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
  400. return fi < i; /* compare them as integers */
  401. else /* 'f' is either greater or less than all integers */
  402. return f < 0; /* less? */
  403. }
  404. }
  405. /*
  406. ** Check whether float 'f' is less than or equal to integer 'i'.
  407. ** See comments on previous function.
  408. */
  409. l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
  410. if (l_intfitsf(i))
  411. return luai_numle(f, cast_num(i)); /* compare them as floats */
  412. else { /* f <= i <=> ceil(f) <= i */
  413. lua_Integer fi;
  414. if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
  415. return fi <= i; /* compare them as integers */
  416. else /* 'f' is either greater or less than all integers */
  417. return f < 0; /* less? */
  418. }
  419. }
  420. /*
  421. ** Return 'l < r', for numbers.
  422. */
  423. l_sinline int LTnum (const TValue *l, const TValue *r) {
  424. lua_assert(ttisnumber(l) && ttisnumber(r));
  425. if (ttisinteger(l)) {
  426. lua_Integer li = ivalue(l);
  427. if (ttisinteger(r))
  428. return li < ivalue(r); /* both are integers */
  429. else /* 'l' is int and 'r' is float */
  430. return LTintfloat(li, fltvalue(r)); /* l < r ? */
  431. }
  432. else {
  433. lua_Number lf = fltvalue(l); /* 'l' must be float */
  434. if (ttisfloat(r))
  435. return luai_numlt(lf, fltvalue(r)); /* both are float */
  436. else /* 'l' is float and 'r' is int */
  437. return LTfloatint(lf, ivalue(r));
  438. }
  439. }
  440. /*
  441. ** Return 'l <= r', for numbers.
  442. */
  443. l_sinline int LEnum (const TValue *l, const TValue *r) {
  444. lua_assert(ttisnumber(l) && ttisnumber(r));
  445. if (ttisinteger(l)) {
  446. lua_Integer li = ivalue(l);
  447. if (ttisinteger(r))
  448. return li <= ivalue(r); /* both are integers */
  449. else /* 'l' is int and 'r' is float */
  450. return LEintfloat(li, fltvalue(r)); /* l <= r ? */
  451. }
  452. else {
  453. lua_Number lf = fltvalue(l); /* 'l' must be float */
  454. if (ttisfloat(r))
  455. return luai_numle(lf, fltvalue(r)); /* both are float */
  456. else /* 'l' is float and 'r' is int */
  457. return LEfloatint(lf, ivalue(r));
  458. }
  459. }
  460. /*
  461. ** return 'l < r' for non-numbers.
  462. */
  463. static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
  464. lua_assert(!ttisnumber(l) || !ttisnumber(r));
  465. if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  466. return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
  467. else
  468. return luaT_callorderTM(L, l, r, TM_LT);
  469. }
  470. /*
  471. ** Main operation less than; return 'l < r'.
  472. */
  473. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  474. if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
  475. return LTnum(l, r);
  476. else return lessthanothers(L, l, r);
  477. }
  478. /*
  479. ** return 'l <= r' for non-numbers.
  480. */
  481. static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
  482. lua_assert(!ttisnumber(l) || !ttisnumber(r));
  483. if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  484. return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  485. else
  486. return luaT_callorderTM(L, l, r, TM_LE);
  487. }
  488. /*
  489. ** Main operation less than or equal to; return 'l <= r'.
  490. */
  491. int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  492. if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
  493. return LEnum(l, r);
  494. else return lessequalothers(L, l, r);
  495. }
  496. /*
  497. ** Main operation for equality of Lua values; return 't1 == t2'.
  498. ** L == NULL means raw equality (no metamethods)
  499. */
  500. int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
  501. const TValue *tm;
  502. if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */
  503. if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER)
  504. return 0; /* only numbers can be equal with different variants */
  505. else { /* two numbers with different variants */
  506. /* One of them is an integer. If the other does not have an
  507. integer value, they cannot be equal; otherwise, compare their
  508. integer values. */
  509. lua_Integer i1, i2;
  510. return (luaV_tointegerns(t1, &i1, F2Ieq) &&
  511. luaV_tointegerns(t2, &i2, F2Ieq) &&
  512. i1 == i2);
  513. }
  514. }
  515. /* values have same type and same variant */
  516. switch (ttypetag(t1)) {
  517. case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1;
  518. case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2));
  519. case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
  520. case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  521. case LUA_VLCF: return fvalue(t1) == fvalue(t2);
  522. case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
  523. case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
  524. case LUA_VUSERDATA: {
  525. if (uvalue(t1) == uvalue(t2)) return 1;
  526. else if (L == NULL) return 0;
  527. tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
  528. if (tm == NULL)
  529. tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
  530. break; /* will try TM */
  531. }
  532. case LUA_VTABLE: {
  533. if (hvalue(t1) == hvalue(t2)) return 1;
  534. else if (L == NULL) return 0;
  535. tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
  536. if (tm == NULL)
  537. tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
  538. break; /* will try TM */
  539. }
  540. default:
  541. return gcvalue(t1) == gcvalue(t2);
  542. }
  543. if (tm == NULL) /* no TM? */
  544. return 0; /* objects are different */
  545. else {
  546. int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */
  547. return !tagisfalse(tag);
  548. }
  549. }
  550. /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
  551. #define tostring(L,o) \
  552. (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
  553. #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
  554. /* copy strings in stack from top - n up to top - 1 to buffer */
  555. static void copy2buff (StkId top, int n, char *buff) {
  556. size_t tl = 0; /* size already copied */
  557. do {
  558. TString *st = tsvalue(s2v(top - n));
  559. size_t l; /* length of string being copied */
  560. const char *s = getlstr(st, l);
  561. memcpy(buff + tl, s, 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 = tsslen(tsvalue(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 = tsslen(tsvalue(s2v(top - n - 1)));
  590. if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - 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, getlngstr(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 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 = ci_func(ci);
  1015. k = cl->p->k;
  1016. pc = ci->u.l.savedpc;
  1017. if (l_unlikely(trap))
  1018. trap = luaG_tracecall(L);
  1019. base = ci->func.p + 1;
  1020. /* main loop of interpreter */
  1021. for (;;) {
  1022. Instruction i; /* instruction being executed */
  1023. vmfetch();
  1024. #if 0
  1025. /* low-level line tracing for debugging Lua */
  1026. printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p)));
  1027. #endif
  1028. lua_assert(base == ci->func.p + 1);
  1029. lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p);
  1030. /* invalidate top for instructions not expecting it */
  1031. lua_assert(isIT(i) || (cast_void(L->top.p = base), 1));
  1032. vmdispatch (GET_OPCODE(i)) {
  1033. vmcase(OP_MOVE) {
  1034. StkId ra = RA(i);
  1035. setobjs2s(L, ra, RB(i));
  1036. vmbreak;
  1037. }
  1038. vmcase(OP_LOADI) {
  1039. StkId ra = RA(i);
  1040. lua_Integer b = GETARG_sBx(i);
  1041. setivalue(s2v(ra), b);
  1042. vmbreak;
  1043. }
  1044. vmcase(OP_LOADF) {
  1045. StkId ra = RA(i);
  1046. int b = GETARG_sBx(i);
  1047. setfltvalue(s2v(ra), cast_num(b));
  1048. vmbreak;
  1049. }
  1050. vmcase(OP_LOADK) {
  1051. StkId ra = RA(i);
  1052. TValue *rb = k + GETARG_Bx(i);
  1053. setobj2s(L, ra, rb);
  1054. vmbreak;
  1055. }
  1056. vmcase(OP_LOADKX) {
  1057. StkId ra = RA(i);
  1058. TValue *rb;
  1059. rb = k + GETARG_Ax(*pc); pc++;
  1060. setobj2s(L, ra, rb);
  1061. vmbreak;
  1062. }
  1063. vmcase(OP_LOADFALSE) {
  1064. StkId ra = RA(i);
  1065. setbfvalue(s2v(ra));
  1066. vmbreak;
  1067. }
  1068. vmcase(OP_LFALSESKIP) {
  1069. StkId ra = RA(i);
  1070. setbfvalue(s2v(ra));
  1071. pc++; /* skip next instruction */
  1072. vmbreak;
  1073. }
  1074. vmcase(OP_LOADTRUE) {
  1075. StkId ra = RA(i);
  1076. setbtvalue(s2v(ra));
  1077. vmbreak;
  1078. }
  1079. vmcase(OP_LOADNIL) {
  1080. StkId ra = RA(i);
  1081. int b = GETARG_B(i);
  1082. do {
  1083. setnilvalue(s2v(ra++));
  1084. } while (b--);
  1085. vmbreak;
  1086. }
  1087. vmcase(OP_GETUPVAL) {
  1088. StkId ra = RA(i);
  1089. int b = GETARG_B(i);
  1090. setobj2s(L, ra, cl->upvals[b]->v.p);
  1091. vmbreak;
  1092. }
  1093. vmcase(OP_SETUPVAL) {
  1094. StkId ra = RA(i);
  1095. UpVal *uv = cl->upvals[GETARG_B(i)];
  1096. setobj(L, uv->v.p, s2v(ra));
  1097. luaC_barrier(L, uv, s2v(ra));
  1098. vmbreak;
  1099. }
  1100. vmcase(OP_GETTABUP) {
  1101. StkId ra = RA(i);
  1102. TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
  1103. TValue *rc = KC(i);
  1104. TString *key = tsvalue(rc); /* key must be a short string */
  1105. int tag;
  1106. luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag);
  1107. if (tagisempty(tag))
  1108. Protect(luaV_finishget(L, upval, rc, ra, tag));
  1109. vmbreak;
  1110. }
  1111. vmcase(OP_GETTABLE) {
  1112. StkId ra = RA(i);
  1113. TValue *rb = vRB(i);
  1114. TValue *rc = vRC(i);
  1115. int tag;
  1116. if (ttisinteger(rc)) { /* fast track for integers? */
  1117. luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag);
  1118. }
  1119. else
  1120. luaV_fastget(rb, rc, s2v(ra), luaH_get, tag);
  1121. if (tagisempty(tag))
  1122. Protect(luaV_finishget(L, rb, rc, ra, tag));
  1123. vmbreak;
  1124. }
  1125. vmcase(OP_GETI) {
  1126. StkId ra = RA(i);
  1127. TValue *rb = vRB(i);
  1128. int c = GETARG_C(i);
  1129. int tag;
  1130. luaV_fastgeti(rb, c, s2v(ra), tag);
  1131. if (tagisempty(tag)) {
  1132. TValue key;
  1133. setivalue(&key, c);
  1134. Protect(luaV_finishget(L, rb, &key, ra, tag));
  1135. }
  1136. vmbreak;
  1137. }
  1138. vmcase(OP_GETFIELD) {
  1139. StkId ra = RA(i);
  1140. TValue *rb = vRB(i);
  1141. TValue *rc = KC(i);
  1142. TString *key = tsvalue(rc); /* key must be a short string */
  1143. int tag;
  1144. luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag);
  1145. if (tagisempty(tag))
  1146. Protect(luaV_finishget(L, rb, rc, ra, tag));
  1147. vmbreak;
  1148. }
  1149. vmcase(OP_SETTABUP) {
  1150. int hres;
  1151. TValue *upval = cl->upvals[GETARG_A(i)]->v.p;
  1152. TValue *rb = KB(i);
  1153. TValue *rc = RKC(i);
  1154. TString *key = tsvalue(rb); /* key must be a short string */
  1155. luaV_fastset(upval, key, rc, hres, luaH_psetshortstr);
  1156. if (hres == HOK)
  1157. luaV_finishfastset(L, upval, rc);
  1158. else
  1159. Protect(luaV_finishset(L, upval, rb, rc, hres));
  1160. vmbreak;
  1161. }
  1162. vmcase(OP_SETTABLE) {
  1163. StkId ra = RA(i);
  1164. int hres;
  1165. TValue *rb = vRB(i); /* key (table is in 'ra') */
  1166. TValue *rc = RKC(i); /* value */
  1167. if (ttisinteger(rb)) { /* fast track for integers? */
  1168. luaV_fastseti(s2v(ra), ivalue(rb), rc, hres);
  1169. }
  1170. else {
  1171. luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset);
  1172. }
  1173. if (hres == HOK)
  1174. luaV_finishfastset(L, s2v(ra), rc);
  1175. else
  1176. Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
  1177. vmbreak;
  1178. }
  1179. vmcase(OP_SETI) {
  1180. StkId ra = RA(i);
  1181. int hres;
  1182. int b = GETARG_B(i);
  1183. TValue *rc = RKC(i);
  1184. luaV_fastseti(s2v(ra), b, rc, hres);
  1185. if (hres == HOK)
  1186. luaV_finishfastset(L, s2v(ra), rc);
  1187. else {
  1188. TValue key;
  1189. setivalue(&key, b);
  1190. Protect(luaV_finishset(L, s2v(ra), &key, rc, hres));
  1191. }
  1192. vmbreak;
  1193. }
  1194. vmcase(OP_SETFIELD) {
  1195. StkId ra = RA(i);
  1196. int hres;
  1197. TValue *rb = KB(i);
  1198. TValue *rc = RKC(i);
  1199. TString *key = tsvalue(rb); /* key must be a short string */
  1200. luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr);
  1201. if (hres == HOK)
  1202. luaV_finishfastset(L, s2v(ra), rc);
  1203. else
  1204. Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
  1205. vmbreak;
  1206. }
  1207. vmcase(OP_NEWTABLE) {
  1208. StkId ra = RA(i);
  1209. int b = GETARG_B(i); /* log2(hash size) + 1 */
  1210. int c = GETARG_C(i); /* array size */
  1211. Table *t;
  1212. if (b > 0)
  1213. b = 1 << (b - 1); /* size is 2^(b - 1) */
  1214. lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0));
  1215. if (TESTARG_k(i)) /* non-zero extra argument? */
  1216. c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */
  1217. pc++; /* skip extra argument */
  1218. L->top.p = ra + 1; /* correct top in case of emergency GC */
  1219. t = luaH_new(L); /* memory allocation */
  1220. sethvalue2s(L, ra, t);
  1221. if (b != 0 || c != 0)
  1222. luaH_resize(L, t, c, b); /* idem */
  1223. checkGC(L, ra + 1);
  1224. vmbreak;
  1225. }
  1226. vmcase(OP_SELF) {
  1227. StkId ra = RA(i);
  1228. int tag;
  1229. TValue *rb = vRB(i);
  1230. TValue *rc = RKC(i);
  1231. TString *key = tsvalue(rc); /* key must be a string */
  1232. setobj2s(L, ra + 1, rb);
  1233. luaV_fastget(rb, key, s2v(ra), luaH_getstr, tag);
  1234. if (tagisempty(tag))
  1235. Protect(luaV_finishget(L, rb, rc, ra, tag));
  1236. vmbreak;
  1237. }
  1238. vmcase(OP_ADDI) {
  1239. op_arithI(L, l_addi, luai_numadd);
  1240. vmbreak;
  1241. }
  1242. vmcase(OP_ADDK) {
  1243. op_arithK(L, l_addi, luai_numadd);
  1244. vmbreak;
  1245. }
  1246. vmcase(OP_SUBK) {
  1247. op_arithK(L, l_subi, luai_numsub);
  1248. vmbreak;
  1249. }
  1250. vmcase(OP_MULK) {
  1251. op_arithK(L, l_muli, luai_nummul);
  1252. vmbreak;
  1253. }
  1254. vmcase(OP_MODK) {
  1255. savestate(L, ci); /* in case of division by 0 */
  1256. op_arithK(L, luaV_mod, luaV_modf);
  1257. vmbreak;
  1258. }
  1259. vmcase(OP_POWK) {
  1260. op_arithfK(L, luai_numpow);
  1261. vmbreak;
  1262. }
  1263. vmcase(OP_DIVK) {
  1264. op_arithfK(L, luai_numdiv);
  1265. vmbreak;
  1266. }
  1267. vmcase(OP_IDIVK) {
  1268. savestate(L, ci); /* in case of division by 0 */
  1269. op_arithK(L, luaV_idiv, luai_numidiv);
  1270. vmbreak;
  1271. }
  1272. vmcase(OP_BANDK) {
  1273. op_bitwiseK(L, l_band);
  1274. vmbreak;
  1275. }
  1276. vmcase(OP_BORK) {
  1277. op_bitwiseK(L, l_bor);
  1278. vmbreak;
  1279. }
  1280. vmcase(OP_BXORK) {
  1281. op_bitwiseK(L, l_bxor);
  1282. vmbreak;
  1283. }
  1284. vmcase(OP_SHRI) {
  1285. StkId ra = RA(i);
  1286. TValue *rb = vRB(i);
  1287. int ic = GETARG_sC(i);
  1288. lua_Integer ib;
  1289. if (tointegerns(rb, &ib)) {
  1290. pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
  1291. }
  1292. vmbreak;
  1293. }
  1294. vmcase(OP_SHLI) {
  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(ic, ib));
  1301. }
  1302. vmbreak;
  1303. }
  1304. vmcase(OP_ADD) {
  1305. op_arith(L, l_addi, luai_numadd);
  1306. vmbreak;
  1307. }
  1308. vmcase(OP_SUB) {
  1309. op_arith(L, l_subi, luai_numsub);
  1310. vmbreak;
  1311. }
  1312. vmcase(OP_MUL) {
  1313. op_arith(L, l_muli, luai_nummul);
  1314. vmbreak;
  1315. }
  1316. vmcase(OP_MOD) {
  1317. savestate(L, ci); /* in case of division by 0 */
  1318. op_arith(L, luaV_mod, luaV_modf);
  1319. vmbreak;
  1320. }
  1321. vmcase(OP_POW) {
  1322. op_arithf(L, luai_numpow);
  1323. vmbreak;
  1324. }
  1325. vmcase(OP_DIV) { /* float division (always with floats) */
  1326. op_arithf(L, luai_numdiv);
  1327. vmbreak;
  1328. }
  1329. vmcase(OP_IDIV) { /* floor division */
  1330. savestate(L, ci); /* in case of division by 0 */
  1331. op_arith(L, luaV_idiv, luai_numidiv);
  1332. vmbreak;
  1333. }
  1334. vmcase(OP_BAND) {
  1335. op_bitwise(L, l_band);
  1336. vmbreak;
  1337. }
  1338. vmcase(OP_BOR) {
  1339. op_bitwise(L, l_bor);
  1340. vmbreak;
  1341. }
  1342. vmcase(OP_BXOR) {
  1343. op_bitwise(L, l_bxor);
  1344. vmbreak;
  1345. }
  1346. vmcase(OP_SHR) {
  1347. op_bitwise(L, luaV_shiftr);
  1348. vmbreak;
  1349. }
  1350. vmcase(OP_SHL) {
  1351. op_bitwise(L, luaV_shiftl);
  1352. vmbreak;
  1353. }
  1354. vmcase(OP_MMBIN) {
  1355. StkId ra = RA(i);
  1356. Instruction pi = *(pc - 2); /* original arith. expression */
  1357. TValue *rb = vRB(i);
  1358. TMS tm = (TMS)GETARG_C(i);
  1359. StkId result = RA(pi);
  1360. lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
  1361. Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
  1362. vmbreak;
  1363. }
  1364. vmcase(OP_MMBINI) {
  1365. StkId ra = RA(i);
  1366. Instruction pi = *(pc - 2); /* original arith. expression */
  1367. int imm = GETARG_sB(i);
  1368. TMS tm = (TMS)GETARG_C(i);
  1369. int flip = GETARG_k(i);
  1370. StkId result = RA(pi);
  1371. Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
  1372. vmbreak;
  1373. }
  1374. vmcase(OP_MMBINK) {
  1375. StkId ra = RA(i);
  1376. Instruction pi = *(pc - 2); /* original arith. expression */
  1377. TValue *imm = KB(i);
  1378. TMS tm = (TMS)GETARG_C(i);
  1379. int flip = GETARG_k(i);
  1380. StkId result = RA(pi);
  1381. Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
  1382. vmbreak;
  1383. }
  1384. vmcase(OP_UNM) {
  1385. StkId ra = RA(i);
  1386. TValue *rb = vRB(i);
  1387. lua_Number nb;
  1388. if (ttisinteger(rb)) {
  1389. lua_Integer ib = ivalue(rb);
  1390. setivalue(s2v(ra), intop(-, 0, ib));
  1391. }
  1392. else if (tonumberns(rb, nb)) {
  1393. setfltvalue(s2v(ra), luai_numunm(L, nb));
  1394. }
  1395. else
  1396. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1397. vmbreak;
  1398. }
  1399. vmcase(OP_BNOT) {
  1400. StkId ra = RA(i);
  1401. TValue *rb = vRB(i);
  1402. lua_Integer ib;
  1403. if (tointegerns(rb, &ib)) {
  1404. setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
  1405. }
  1406. else
  1407. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1408. vmbreak;
  1409. }
  1410. vmcase(OP_NOT) {
  1411. StkId ra = RA(i);
  1412. TValue *rb = vRB(i);
  1413. if (l_isfalse(rb))
  1414. setbtvalue(s2v(ra));
  1415. else
  1416. setbfvalue(s2v(ra));
  1417. vmbreak;
  1418. }
  1419. vmcase(OP_LEN) {
  1420. StkId ra = RA(i);
  1421. Protect(luaV_objlen(L, ra, vRB(i)));
  1422. vmbreak;
  1423. }
  1424. vmcase(OP_CONCAT) {
  1425. StkId ra = RA(i);
  1426. int n = GETARG_B(i); /* number of elements to concatenate */
  1427. L->top.p = ra + n; /* mark the end of concat operands */
  1428. ProtectNT(luaV_concat(L, n));
  1429. checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */
  1430. vmbreak;
  1431. }
  1432. vmcase(OP_CLOSE) {
  1433. StkId ra = RA(i);
  1434. Protect(luaF_close(L, ra, LUA_OK, 1));
  1435. vmbreak;
  1436. }
  1437. vmcase(OP_TBC) {
  1438. StkId ra = RA(i);
  1439. /* create new to-be-closed upvalue */
  1440. halfProtect(luaF_newtbcupval(L, ra));
  1441. vmbreak;
  1442. }
  1443. vmcase(OP_JMP) {
  1444. dojump(ci, i, 0);
  1445. vmbreak;
  1446. }
  1447. vmcase(OP_EQ) {
  1448. StkId ra = RA(i);
  1449. int cond;
  1450. TValue *rb = vRB(i);
  1451. Protect(cond = luaV_equalobj(L, s2v(ra), rb));
  1452. docondjump();
  1453. vmbreak;
  1454. }
  1455. vmcase(OP_LT) {
  1456. op_order(L, l_lti, LTnum, lessthanothers);
  1457. vmbreak;
  1458. }
  1459. vmcase(OP_LE) {
  1460. op_order(L, l_lei, LEnum, lessequalothers);
  1461. vmbreak;
  1462. }
  1463. vmcase(OP_EQK) {
  1464. StkId ra = RA(i);
  1465. TValue *rb = KB(i);
  1466. /* basic types do not use '__eq'; we can use raw equality */
  1467. int cond = luaV_rawequalobj(s2v(ra), rb);
  1468. docondjump();
  1469. vmbreak;
  1470. }
  1471. vmcase(OP_EQI) {
  1472. StkId ra = RA(i);
  1473. int cond;
  1474. int im = GETARG_sB(i);
  1475. if (ttisinteger(s2v(ra)))
  1476. cond = (ivalue(s2v(ra)) == im);
  1477. else if (ttisfloat(s2v(ra)))
  1478. cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
  1479. else
  1480. cond = 0; /* other types cannot be equal to a number */
  1481. docondjump();
  1482. vmbreak;
  1483. }
  1484. vmcase(OP_LTI) {
  1485. op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
  1486. vmbreak;
  1487. }
  1488. vmcase(OP_LEI) {
  1489. op_orderI(L, l_lei, luai_numle, 0, TM_LE);
  1490. vmbreak;
  1491. }
  1492. vmcase(OP_GTI) {
  1493. op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
  1494. vmbreak;
  1495. }
  1496. vmcase(OP_GEI) {
  1497. op_orderI(L, l_gei, luai_numge, 1, TM_LE);
  1498. vmbreak;
  1499. }
  1500. vmcase(OP_TEST) {
  1501. StkId ra = RA(i);
  1502. int cond = !l_isfalse(s2v(ra));
  1503. docondjump();
  1504. vmbreak;
  1505. }
  1506. vmcase(OP_TESTSET) {
  1507. StkId ra = RA(i);
  1508. TValue *rb = vRB(i);
  1509. if (l_isfalse(rb) == GETARG_k(i))
  1510. pc++;
  1511. else {
  1512. setobj2s(L, ra, rb);
  1513. donextjump(ci);
  1514. }
  1515. vmbreak;
  1516. }
  1517. vmcase(OP_CALL) {
  1518. StkId ra = RA(i);
  1519. CallInfo *newci;
  1520. int b = GETARG_B(i);
  1521. int nresults = GETARG_C(i) - 1;
  1522. if (b != 0) /* fixed number of arguments? */
  1523. L->top.p = ra + b; /* top signals number of arguments */
  1524. /* else previous instruction set top */
  1525. savepc(L); /* in case of errors */
  1526. if ((newci = luaD_precall(L, ra, nresults)) == NULL)
  1527. updatetrap(ci); /* C call; nothing else to be done */
  1528. else { /* Lua call: run function in this same C frame */
  1529. ci = newci;
  1530. goto startfunc;
  1531. }
  1532. vmbreak;
  1533. }
  1534. vmcase(OP_TAILCALL) {
  1535. StkId ra = RA(i);
  1536. int b = GETARG_B(i); /* number of arguments + 1 (function) */
  1537. int n; /* number of results when calling a C function */
  1538. int nparams1 = GETARG_C(i);
  1539. /* delta is virtual 'func' - real 'func' (vararg functions) */
  1540. int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
  1541. if (b != 0)
  1542. L->top.p = ra + b;
  1543. else /* previous instruction set top */
  1544. b = cast_int(L->top.p - ra);
  1545. savepc(ci); /* several calls here can raise errors */
  1546. if (TESTARG_k(i)) {
  1547. luaF_closeupval(L, base); /* close upvalues from current call */
  1548. lua_assert(L->tbclist.p < base); /* no pending tbc variables */
  1549. lua_assert(base == ci->func.p + 1);
  1550. }
  1551. if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */
  1552. goto startfunc; /* execute the callee */
  1553. else { /* C function? */
  1554. ci->func.p -= delta; /* restore 'func' (if vararg) */
  1555. luaD_poscall(L, ci, n); /* finish caller */
  1556. updatetrap(ci); /* 'luaD_poscall' can change hooks */
  1557. goto ret; /* caller returns after the tail call */
  1558. }
  1559. }
  1560. vmcase(OP_RETURN) {
  1561. StkId ra = RA(i);
  1562. int n = GETARG_B(i) - 1; /* number of results */
  1563. int nparams1 = GETARG_C(i);
  1564. if (n < 0) /* not fixed? */
  1565. n = cast_int(L->top.p - ra); /* get what is available */
  1566. savepc(ci);
  1567. if (TESTARG_k(i)) { /* may there be open upvalues? */
  1568. ci->u2.nres = n; /* save number of returns */
  1569. if (L->top.p < ci->top.p)
  1570. L->top.p = ci->top.p;
  1571. luaF_close(L, base, CLOSEKTOP, 1);
  1572. updatetrap(ci);
  1573. updatestack(ci);
  1574. }
  1575. if (nparams1) /* vararg function? */
  1576. ci->func.p -= ci->u.l.nextraargs + nparams1;
  1577. L->top.p = ra + n; /* set call for 'luaD_poscall' */
  1578. luaD_poscall(L, ci, n);
  1579. updatetrap(ci); /* 'luaD_poscall' can change hooks */
  1580. goto ret;
  1581. }
  1582. vmcase(OP_RETURN0) {
  1583. if (l_unlikely(L->hookmask)) {
  1584. StkId ra = RA(i);
  1585. L->top.p = ra;
  1586. savepc(ci);
  1587. luaD_poscall(L, ci, 0); /* no hurry... */
  1588. trap = 1;
  1589. }
  1590. else { /* do the 'poscall' here */
  1591. int nres;
  1592. L->ci = ci->previous; /* back to caller */
  1593. L->top.p = base - 1;
  1594. for (nres = ci->nresults; l_unlikely(nres > 0); nres--)
  1595. setnilvalue(s2v(L->top.p++)); /* all results are nil */
  1596. }
  1597. goto ret;
  1598. }
  1599. vmcase(OP_RETURN1) {
  1600. if (l_unlikely(L->hookmask)) {
  1601. StkId ra = RA(i);
  1602. L->top.p = ra + 1;
  1603. savepc(ci);
  1604. luaD_poscall(L, ci, 1); /* no hurry... */
  1605. trap = 1;
  1606. }
  1607. else { /* do the 'poscall' here */
  1608. int nres = ci->nresults;
  1609. L->ci = ci->previous; /* back to caller */
  1610. if (nres == 0)
  1611. L->top.p = base - 1; /* asked for no results */
  1612. else {
  1613. StkId ra = RA(i);
  1614. setobjs2s(L, base - 1, ra); /* at least this result */
  1615. L->top.p = base;
  1616. for (; l_unlikely(nres > 1); nres--)
  1617. setnilvalue(s2v(L->top.p++)); /* complete missing results */
  1618. }
  1619. }
  1620. ret: /* return from a Lua function */
  1621. if (ci->callstatus & CIST_FRESH)
  1622. return; /* end this frame */
  1623. else {
  1624. ci = ci->previous;
  1625. goto returning; /* continue running caller in this frame */
  1626. }
  1627. }
  1628. vmcase(OP_FORLOOP) {
  1629. StkId ra = RA(i);
  1630. if (ttisinteger(s2v(ra + 1))) { /* integer loop? */
  1631. lua_Unsigned count = l_castS2U(ivalue(s2v(ra)));
  1632. if (count > 0) { /* still more iterations? */
  1633. lua_Integer step = ivalue(s2v(ra + 1));
  1634. lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */
  1635. chgivalue(s2v(ra), count - 1); /* update counter */
  1636. idx = intop(+, idx, step); /* add step to index */
  1637. chgivalue(s2v(ra + 2), idx); /* update control variable */
  1638. pc -= GETARG_Bx(i); /* jump back */
  1639. }
  1640. }
  1641. else if (floatforloop(ra)) /* float loop */
  1642. pc -= GETARG_Bx(i); /* jump back */
  1643. updatetrap(ci); /* allows a signal to break the loop */
  1644. vmbreak;
  1645. }
  1646. vmcase(OP_FORPREP) {
  1647. StkId ra = RA(i);
  1648. savestate(L, ci); /* in case of errors */
  1649. if (forprep(L, ra))
  1650. pc += GETARG_Bx(i) + 1; /* skip the loop */
  1651. vmbreak;
  1652. }
  1653. vmcase(OP_TFORPREP) {
  1654. /* before: 'ra' has the iterator function, 'ra + 1' has the state,
  1655. 'ra + 2' has the initial value for the control variable, and
  1656. 'ra + 3' has the closing variable. This opcode then swaps the
  1657. control and the closing variables and marks the closing variable
  1658. as to-be-closed.
  1659. */
  1660. StkId ra = RA(i);
  1661. TValue temp; /* to swap control and closing variables */
  1662. setobj(L, &temp, s2v(ra + 3));
  1663. setobjs2s(L, ra + 3, ra + 2);
  1664. setobj2s(L, ra + 2, &temp);
  1665. /* create to-be-closed upvalue (if closing var. is not nil) */
  1666. halfProtect(luaF_newtbcupval(L, ra + 2));
  1667. pc += GETARG_Bx(i); /* go to end of the loop */
  1668. i = *(pc++); /* fetch next instruction */
  1669. lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
  1670. goto l_tforcall;
  1671. }
  1672. vmcase(OP_TFORCALL) {
  1673. l_tforcall: {
  1674. /* 'ra' has the iterator function, 'ra + 1' has the state,
  1675. 'ra + 2' has the closing variable, and 'ra + 3' has the control
  1676. variable. The call will use the stack starting at 'ra + 3',
  1677. so that it preserves the first three values, and the first
  1678. return will be the new value for the control variable.
  1679. */
  1680. StkId ra = RA(i);
  1681. setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */
  1682. setobjs2s(L, ra + 4, ra + 1); /* copy state */
  1683. setobjs2s(L, ra + 3, ra); /* copy function */
  1684. L->top.p = ra + 3 + 3;
  1685. ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */
  1686. updatestack(ci); /* stack may have changed */
  1687. i = *(pc++); /* go to next instruction */
  1688. lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
  1689. goto l_tforloop;
  1690. }}
  1691. vmcase(OP_TFORLOOP) {
  1692. l_tforloop: {
  1693. StkId ra = RA(i);
  1694. if (!ttisnil(s2v(ra + 3))) /* continue loop? */
  1695. pc -= GETARG_Bx(i); /* jump back */
  1696. vmbreak;
  1697. }}
  1698. vmcase(OP_SETLIST) {
  1699. StkId ra = RA(i);
  1700. int n = GETARG_B(i);
  1701. unsigned int last = GETARG_C(i);
  1702. Table *h = hvalue(s2v(ra));
  1703. if (n == 0)
  1704. n = cast_int(L->top.p - ra) - 1; /* get up to the top */
  1705. else
  1706. L->top.p = ci->top.p; /* correct top in case of emergency GC */
  1707. last += n;
  1708. if (TESTARG_k(i)) {
  1709. last += GETARG_Ax(*pc) * (MAXARG_C + 1);
  1710. pc++;
  1711. }
  1712. if (last > luaH_realasize(h)) /* needs more space? */
  1713. luaH_resizearray(L, h, last); /* preallocate it at once */
  1714. for (; n > 0; n--) {
  1715. TValue *val = s2v(ra + n);
  1716. obj2arr(h, last, val);
  1717. last--;
  1718. luaC_barrierback(L, obj2gco(h), val);
  1719. }
  1720. vmbreak;
  1721. }
  1722. vmcase(OP_CLOSURE) {
  1723. StkId ra = RA(i);
  1724. Proto *p = cl->p->p[GETARG_Bx(i)];
  1725. halfProtect(pushclosure(L, p, cl->upvals, base, ra));
  1726. checkGC(L, ra + 1);
  1727. vmbreak;
  1728. }
  1729. vmcase(OP_VARARG) {
  1730. StkId ra = RA(i);
  1731. int n = GETARG_C(i) - 1; /* required results */
  1732. Protect(luaT_getvarargs(L, ci, ra, n));
  1733. vmbreak;
  1734. }
  1735. vmcase(OP_VARARGPREP) {
  1736. ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p));
  1737. if (l_unlikely(trap)) { /* previous "Protect" updated trap */
  1738. luaD_hookcall(L, ci);
  1739. L->oldpc = 1; /* next opcode will be seen as a "new" line */
  1740. }
  1741. updatebase(ci); /* function has new base after adjustment */
  1742. vmbreak;
  1743. }
  1744. vmcase(OP_EXTRAARG) {
  1745. lua_assert(0);
  1746. vmbreak;
  1747. }
  1748. }
  1749. }
  1750. }
  1751. /* }================================================================== */