lvm.c 58 KB

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