lvm.c 57 KB

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