lvm.c 58 KB

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