lvm.c 58 KB

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