lvm.c 60 KB

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