lvm.c 59 KB

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