lvm.c 57 KB

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