2
0

lvm.c 59 KB

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