lcode.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. ** $Id: lcode.c,v 2.116 2017/04/25 20:01:14 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lcode_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include "lua.h"
  12. #include "lcode.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lgc.h"
  16. #include "llex.h"
  17. #include "lmem.h"
  18. #include "lobject.h"
  19. #include "lopcodes.h"
  20. #include "lparser.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "lvm.h"
  24. /* Maximum number of registers in a Lua function (must fit in 8 bits) */
  25. #define MAXREGS 255
  26. #define hasjumps(e) ((e)->t != (e)->f)
  27. /*
  28. ** If expression is a numeric constant, fills 'v' with its value
  29. ** and returns 1. Otherwise, returns 0.
  30. */
  31. static int tonumeral(const expdesc *e, TValue *v) {
  32. if (hasjumps(e))
  33. return 0; /* not a numeral */
  34. switch (e->k) {
  35. case VKINT:
  36. if (v) setivalue(v, e->u.ival);
  37. return 1;
  38. case VKFLT:
  39. if (v) setfltvalue(v, e->u.nval);
  40. return 1;
  41. default: return 0;
  42. }
  43. }
  44. /*
  45. ** Create a OP_LOADNIL instruction, but try to optimize: if the previous
  46. ** instruction is also OP_LOADNIL and ranges are compatible, adjust
  47. ** range of previous instruction instead of emitting a new one. (For
  48. ** instance, 'local a; local b' will generate a single opcode.)
  49. */
  50. void luaK_nil (FuncState *fs, int from, int n) {
  51. Instruction *previous;
  52. int l = from + n - 1; /* last register to set nil */
  53. if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
  54. previous = &fs->f->code[fs->pc-1];
  55. if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
  56. int pfrom = GETARG_A(*previous); /* get previous range */
  57. int pl = pfrom + GETARG_B(*previous);
  58. if ((pfrom <= from && from <= pl + 1) ||
  59. (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
  60. if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
  61. if (pl > l) l = pl; /* l = max(l, pl) */
  62. SETARG_A(*previous, from);
  63. SETARG_B(*previous, l - from);
  64. return;
  65. }
  66. } /* else go through */
  67. }
  68. luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
  69. }
  70. /*
  71. ** Gets the destination address of a jump instruction. Used to traverse
  72. ** a list of jumps.
  73. */
  74. static int getjump (FuncState *fs, int pc) {
  75. int offset = GETARG_sBx(fs->f->code[pc]);
  76. if (offset == NO_JUMP) /* point to itself represents end of list */
  77. return NO_JUMP; /* end of list */
  78. else
  79. return (pc+1)+offset; /* turn offset into absolute position */
  80. }
  81. /*
  82. ** Fix jump instruction at position 'pc' to jump to 'dest'.
  83. ** (Jump addresses are relative in Lua)
  84. */
  85. static void fixjump (FuncState *fs, int pc, int dest) {
  86. Instruction *jmp = &fs->f->code[pc];
  87. int offset = dest - (pc + 1);
  88. lua_assert(dest != NO_JUMP);
  89. if (abs(offset) > MAXARG_sBx)
  90. luaX_syntaxerror(fs->ls, "control structure too long");
  91. SETARG_sBx(*jmp, offset);
  92. }
  93. /*
  94. ** Concatenate jump-list 'l2' into jump-list 'l1'
  95. */
  96. void luaK_concat (FuncState *fs, int *l1, int l2) {
  97. if (l2 == NO_JUMP) return; /* nothing to concatenate? */
  98. else if (*l1 == NO_JUMP) /* no original list? */
  99. *l1 = l2; /* 'l1' points to 'l2' */
  100. else {
  101. int list = *l1;
  102. int next;
  103. while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
  104. list = next;
  105. fixjump(fs, list, l2); /* last element links to 'l2' */
  106. }
  107. }
  108. /*
  109. ** Create a jump instruction and return its position, so its destination
  110. ** can be fixed later (with 'fixjump'). If there are jumps to
  111. ** this position (kept in 'jpc'), link them all together so that
  112. ** 'patchlistaux' will fix all them directly to the final destination.
  113. */
  114. int luaK_jump (FuncState *fs) {
  115. int jpc = fs->jpc; /* save list of jumps to here */
  116. int j;
  117. fs->jpc = NO_JUMP; /* no more jumps to here */
  118. j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
  119. luaK_concat(fs, &j, jpc); /* keep them on hold */
  120. return j;
  121. }
  122. /*
  123. ** Code a 'return' instruction
  124. */
  125. void luaK_ret (FuncState *fs, int first, int nret) {
  126. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  127. }
  128. /*
  129. ** Code a "conditional jump", that is, a test or comparison opcode
  130. ** followed by a jump. Return jump position.
  131. */
  132. static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
  133. luaK_codeABC(fs, op, A, B, C);
  134. return luaK_jump(fs);
  135. }
  136. /*
  137. ** returns current 'pc' and marks it as a jump target (to avoid wrong
  138. ** optimizations with consecutive instructions not in the same basic block).
  139. */
  140. int luaK_getlabel (FuncState *fs) {
  141. fs->lasttarget = fs->pc;
  142. return fs->pc;
  143. }
  144. /*
  145. ** Returns the position of the instruction "controlling" a given
  146. ** jump (that is, its condition), or the jump itself if it is
  147. ** unconditional.
  148. */
  149. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  150. Instruction *pi = &fs->f->code[pc];
  151. if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
  152. return pi-1;
  153. else
  154. return pi;
  155. }
  156. /*
  157. ** Patch destination register for a TESTSET instruction.
  158. ** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
  159. ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
  160. ** register. Otherwise, change instruction to a simple 'TEST' (produces
  161. ** no register value)
  162. */
  163. static int patchtestreg (FuncState *fs, int node, int reg) {
  164. Instruction *i = getjumpcontrol(fs, node);
  165. if (GET_OPCODE(*i) != OP_TESTSET)
  166. return 0; /* cannot patch other instructions */
  167. if (reg != NO_REG && reg != GETARG_B(*i))
  168. SETARG_A(*i, reg);
  169. else {
  170. /* no register to put value or register already has the value;
  171. change instruction to simple test */
  172. *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
  173. }
  174. return 1;
  175. }
  176. /*
  177. ** Traverse a list of tests ensuring no one produces a value
  178. */
  179. static void removevalues (FuncState *fs, int list) {
  180. for (; list != NO_JUMP; list = getjump(fs, list))
  181. patchtestreg(fs, list, NO_REG);
  182. }
  183. /*
  184. ** Traverse a list of tests, patching their destination address and
  185. ** registers: tests producing values jump to 'vtarget' (and put their
  186. ** values in 'reg'), other tests jump to 'dtarget'.
  187. */
  188. static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
  189. int dtarget) {
  190. while (list != NO_JUMP) {
  191. int next = getjump(fs, list);
  192. if (patchtestreg(fs, list, reg))
  193. fixjump(fs, list, vtarget);
  194. else
  195. fixjump(fs, list, dtarget); /* jump to default target */
  196. list = next;
  197. }
  198. }
  199. /*
  200. ** Ensure all pending jumps to current position are fixed (jumping
  201. ** to current position with no values) and reset list of pending
  202. ** jumps
  203. */
  204. static void dischargejpc (FuncState *fs) {
  205. patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
  206. fs->jpc = NO_JUMP;
  207. }
  208. /*
  209. ** Add elements in 'list' to list of pending jumps to "here"
  210. ** (current position)
  211. */
  212. void luaK_patchtohere (FuncState *fs, int list) {
  213. luaK_getlabel(fs); /* mark "here" as a jump target */
  214. luaK_concat(fs, &fs->jpc, list);
  215. }
  216. /*
  217. ** Path all jumps in 'list' to jump to 'target'.
  218. ** (The assert means that we cannot fix a jump to a forward address
  219. ** because we only know addresses once code is generated.)
  220. */
  221. void luaK_patchlist (FuncState *fs, int list, int target) {
  222. if (target == fs->pc) /* 'target' is current position? */
  223. luaK_patchtohere(fs, list); /* add list to pending jumps */
  224. else {
  225. lua_assert(target < fs->pc);
  226. patchlistaux(fs, list, target, NO_REG, target);
  227. }
  228. }
  229. /*
  230. ** Path all jumps in 'list' to close upvalues up to given 'level'
  231. ** (The assertion checks that jumps either were closing nothing
  232. ** or were closing higher levels, from inner blocks.)
  233. */
  234. void luaK_patchclose (FuncState *fs, int list, int level) {
  235. level++; /* argument is +1 to reserve 0 as non-op */
  236. for (; list != NO_JUMP; list = getjump(fs, list)) {
  237. lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
  238. (GETARG_A(fs->f->code[list]) == 0 ||
  239. GETARG_A(fs->f->code[list]) >= level));
  240. SETARG_A(fs->f->code[list], level);
  241. }
  242. }
  243. /*
  244. ** Emit instruction 'i', checking for array sizes and saving also its
  245. ** line information. Return 'i' position.
  246. */
  247. static int luaK_code (FuncState *fs, Instruction i) {
  248. Proto *f = fs->f;
  249. dischargejpc(fs); /* 'pc' will change */
  250. /* put new instruction in code array */
  251. luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
  252. MAX_INT, "opcodes");
  253. f->code[fs->pc] = i;
  254. /* save corresponding line information */
  255. luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
  256. MAX_INT, "opcodes");
  257. f->lineinfo[fs->pc] = fs->ls->lastline;
  258. return fs->pc++;
  259. }
  260. /*
  261. ** Format and emit an 'iABC' instruction. (Assertions check consistency
  262. ** of parameters versus opcode.)
  263. */
  264. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  265. lua_assert(getOpMode(o) == iABC);
  266. lua_assert(getBMode(o) != OpArgN || b == 0);
  267. lua_assert(getCMode(o) != OpArgN || c == 0);
  268. lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
  269. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  270. }
  271. /*
  272. ** Format and emit an 'iABx' instruction.
  273. */
  274. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  275. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  276. lua_assert(getCMode(o) == OpArgN);
  277. lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
  278. return luaK_code(fs, CREATE_ABx(o, a, bc));
  279. }
  280. /*
  281. ** Emit an "extra argument" instruction (format 'iAx')
  282. */
  283. static int codeextraarg (FuncState *fs, int a) {
  284. lua_assert(a <= MAXARG_Ax);
  285. return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
  286. }
  287. /*
  288. ** Emit a "load constant" instruction, using either 'OP_LOADK'
  289. ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
  290. ** instruction with "extra argument".
  291. */
  292. static int luaK_codek (FuncState *fs, int reg, int k) {
  293. if (k <= MAXARG_Bx)
  294. return luaK_codeABx(fs, OP_LOADK, reg, k);
  295. else {
  296. int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
  297. codeextraarg(fs, k);
  298. return p;
  299. }
  300. }
  301. /*
  302. ** Check register-stack level, keeping track of its maximum size
  303. ** in field 'maxstacksize'
  304. */
  305. void luaK_checkstack (FuncState *fs, int n) {
  306. int newstack = fs->freereg + n;
  307. if (newstack > fs->f->maxstacksize) {
  308. if (newstack >= MAXREGS)
  309. luaX_syntaxerror(fs->ls,
  310. "function or expression needs too many registers");
  311. fs->f->maxstacksize = cast_byte(newstack);
  312. }
  313. }
  314. /*
  315. ** Reserve 'n' registers in register stack
  316. */
  317. void luaK_reserveregs (FuncState *fs, int n) {
  318. luaK_checkstack(fs, n);
  319. fs->freereg += n;
  320. }
  321. /*
  322. ** Free register 'reg', if it is neither a constant index nor
  323. ** a local variable.
  324. )
  325. */
  326. static void freereg (FuncState *fs, int reg) {
  327. if (!ISK(reg) && reg >= fs->nactvar) {
  328. fs->freereg--;
  329. lua_assert(reg == fs->freereg);
  330. }
  331. }
  332. /*
  333. ** Free two registers in proper order
  334. */
  335. static void freeregs (FuncState *fs, int r1, int r2) {
  336. if (r1 > r2) {
  337. freereg(fs, r1);
  338. freereg(fs, r2);
  339. }
  340. else {
  341. freereg(fs, r2);
  342. freereg(fs, r1);
  343. }
  344. }
  345. /*
  346. ** Free register used by expression 'e' (if any)
  347. */
  348. static void freeexp (FuncState *fs, expdesc *e) {
  349. if (e->k == VNONRELOC)
  350. freereg(fs, e->u.info);
  351. }
  352. /*
  353. ** Free registers used by expressions 'e1' and 'e2' (if any) in proper
  354. ** order.
  355. */
  356. static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
  357. int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
  358. int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
  359. freeregs(fs, r1, r2);
  360. }
  361. /*
  362. ** Add constant 'v' to prototype's list of constants (field 'k').
  363. ** Use scanner's table to cache position of constants in constant list
  364. ** and try to reuse constants. Because some values should not be used
  365. ** as keys (nil cannot be a key, integer keys can collapse with float
  366. ** keys), the caller must provide a useful 'key' for indexing the cache.
  367. */
  368. static int addk (FuncState *fs, TValue *key, TValue *v) {
  369. lua_State *L = fs->ls->L;
  370. Proto *f = fs->f;
  371. TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
  372. int k, oldsize;
  373. if (ttisinteger(idx)) { /* is there an index there? */
  374. k = cast_int(ivalue(idx));
  375. /* correct value? (warning: must distinguish floats from integers!) */
  376. if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
  377. luaV_rawequalobj(&f->k[k], v))
  378. return k; /* reuse index */
  379. }
  380. /* constant not found; create a new entry */
  381. oldsize = f->sizek;
  382. k = fs->nk;
  383. /* numerical value does not need GC barrier;
  384. table has no metatable, so it does not need to invalidate cache */
  385. setivalue(idx, k);
  386. luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  387. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  388. setobj(L, &f->k[k], v);
  389. fs->nk++;
  390. luaC_barrier(L, f, v);
  391. return k;
  392. }
  393. /*
  394. ** Add a string to list of constants and return its index.
  395. */
  396. int luaK_stringK (FuncState *fs, TString *s) {
  397. TValue o;
  398. setsvalue(fs->ls->L, &o, s);
  399. return addk(fs, &o, &o); /* use string itself as key */
  400. }
  401. /*
  402. ** Add an integer to list of constants and return its index.
  403. ** Integers use userdata as keys to avoid collision with floats with
  404. ** same value; conversion to 'void*' is used only for hashing, so there
  405. ** are no "precision" problems.
  406. */
  407. static int luaK_intK (FuncState *fs, lua_Integer n) {
  408. TValue k, o;
  409. setpvalue(&k, cast(void*, cast(size_t, n)));
  410. setivalue(&o, n);
  411. return addk(fs, &k, &o);
  412. }
  413. /*
  414. ** Add a float to list of constants and return its index.
  415. */
  416. static int luaK_numberK (FuncState *fs, lua_Number r) {
  417. TValue o;
  418. setfltvalue(&o, r);
  419. return addk(fs, &o, &o); /* use number itself as key */
  420. }
  421. /*
  422. ** Add a boolean to list of constants and return its index.
  423. */
  424. static int boolK (FuncState *fs, int b) {
  425. TValue o;
  426. setbvalue(&o, b);
  427. return addk(fs, &o, &o); /* use boolean itself as key */
  428. }
  429. /*
  430. ** Add nil to list of constants and return its index.
  431. */
  432. static int nilK (FuncState *fs) {
  433. TValue k, v;
  434. setnilvalue(&v);
  435. /* cannot use nil as key; instead use table itself to represent nil */
  436. sethvalue(fs->ls->L, &k, fs->ls->h);
  437. return addk(fs, &k, &v);
  438. }
  439. void luaK_int (FuncState *fs, int reg, lua_Integer i) {
  440. if (l_castS2U(i) + MAXARG_sBx <= l_castS2U(MAXARG_Bx))
  441. luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
  442. else
  443. luaK_codek(fs, reg, luaK_intK(fs, i));
  444. }
  445. /*
  446. ** Fix an expression to return the number of results 'nresults'.
  447. ** Either 'e' is a multi-ret expression (function call or vararg)
  448. ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
  449. */
  450. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  451. if (e->k == VCALL) { /* expression is an open function call? */
  452. SETARG_C(getinstruction(fs, e), nresults + 1);
  453. }
  454. else if (e->k == VVARARG) {
  455. Instruction *pc = &getinstruction(fs, e);
  456. SETARG_B(*pc, nresults + 1);
  457. SETARG_A(*pc, fs->freereg);
  458. luaK_reserveregs(fs, 1);
  459. }
  460. else lua_assert(nresults == LUA_MULTRET);
  461. }
  462. /*
  463. ** Fix an expression to return one result.
  464. ** If expression is not a multi-ret expression (function call or
  465. ** vararg), it already returns one result, so nothing needs to be done.
  466. ** Function calls become VNONRELOC expressions (as its result comes
  467. ** fixed in the base register of the call), while vararg expressions
  468. ** become VRELOCABLE (as OP_VARARG puts its results where it wants).
  469. ** (Calls are created returning one result, so that does not need
  470. ** to be fixed.)
  471. */
  472. void luaK_setoneret (FuncState *fs, expdesc *e) {
  473. if (e->k == VCALL) { /* expression is an open function call? */
  474. /* already returns 1 value */
  475. lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
  476. e->k = VNONRELOC; /* result has fixed position */
  477. e->u.info = GETARG_A(getinstruction(fs, e));
  478. }
  479. else if (e->k == VVARARG) {
  480. SETARG_B(getinstruction(fs, e), 2);
  481. e->k = VRELOCABLE; /* can relocate its simple result */
  482. }
  483. }
  484. /*
  485. ** Ensure that expression 'e' is not a variable.
  486. */
  487. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  488. switch (e->k) {
  489. case VLOCAL: { /* already in a register */
  490. e->k = VNONRELOC; /* becomes a non-relocatable value */
  491. break;
  492. }
  493. case VUPVAL: { /* move value to some (pending) register */
  494. e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
  495. e->k = VRELOCABLE;
  496. break;
  497. }
  498. case VINDEXED: {
  499. OpCode op;
  500. if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */
  501. freeregs(fs, e->u.ind.t, e->u.ind.idx);
  502. op = OP_GETTABLE;
  503. }
  504. else {
  505. lua_assert(e->u.ind.vt == VUPVAL);
  506. freereg(fs, e->u.ind.idx);
  507. op = OP_GETTABUP; /* 't' is in an upvalue */
  508. }
  509. e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
  510. e->k = VRELOCABLE;
  511. break;
  512. }
  513. case VVARARG: case VCALL: {
  514. luaK_setoneret(fs, e);
  515. break;
  516. }
  517. default: break; /* there is one value available (somewhere) */
  518. }
  519. }
  520. /*
  521. ** Ensures expression value is in register 'reg' (and therefore
  522. ** 'e' will become a non-relocatable expression).
  523. */
  524. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  525. luaK_dischargevars(fs, e);
  526. switch (e->k) {
  527. case VNIL: {
  528. luaK_nil(fs, reg, 1);
  529. break;
  530. }
  531. case VFALSE: case VTRUE: {
  532. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  533. break;
  534. }
  535. case VK: {
  536. luaK_codek(fs, reg, e->u.info);
  537. break;
  538. }
  539. case VKFLT: {
  540. luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
  541. break;
  542. }
  543. case VKINT: {
  544. luaK_int(fs, reg, e->u.ival);
  545. break;
  546. }
  547. case VRELOCABLE: {
  548. Instruction *pc = &getinstruction(fs, e);
  549. SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
  550. break;
  551. }
  552. case VNONRELOC: {
  553. if (reg != e->u.info)
  554. luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
  555. break;
  556. }
  557. default: {
  558. lua_assert(e->k == VJMP);
  559. return; /* nothing to do... */
  560. }
  561. }
  562. e->u.info = reg;
  563. e->k = VNONRELOC;
  564. }
  565. /*
  566. ** Ensures expression value is in any register.
  567. */
  568. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  569. if (e->k != VNONRELOC) { /* no fixed register yet? */
  570. luaK_reserveregs(fs, 1); /* get a register */
  571. discharge2reg(fs, e, fs->freereg-1); /* put value there */
  572. }
  573. }
  574. static int code_loadbool (FuncState *fs, int A, int b, int jump) {
  575. luaK_getlabel(fs); /* those instructions may be jump targets */
  576. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  577. }
  578. /*
  579. ** check whether list has any jump that do not produce a value
  580. ** or produce an inverted value
  581. */
  582. static int need_value (FuncState *fs, int list) {
  583. for (; list != NO_JUMP; list = getjump(fs, list)) {
  584. Instruction i = *getjumpcontrol(fs, list);
  585. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  586. }
  587. return 0; /* not found */
  588. }
  589. /*
  590. ** Ensures final expression result (including results from its jump
  591. ** lists) is in register 'reg'.
  592. ** If expression has jumps, need to patch these jumps either to
  593. ** its final position or to "load" instructions (for those tests
  594. ** that do not produce values).
  595. */
  596. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  597. discharge2reg(fs, e, reg);
  598. if (e->k == VJMP) /* expression itself is a test? */
  599. luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
  600. if (hasjumps(e)) {
  601. int final; /* position after whole expression */
  602. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  603. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  604. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  605. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  606. p_f = code_loadbool(fs, reg, 0, 1);
  607. p_t = code_loadbool(fs, reg, 1, 0);
  608. luaK_patchtohere(fs, fj);
  609. }
  610. final = luaK_getlabel(fs);
  611. patchlistaux(fs, e->f, final, reg, p_f);
  612. patchlistaux(fs, e->t, final, reg, p_t);
  613. }
  614. e->f = e->t = NO_JUMP;
  615. e->u.info = reg;
  616. e->k = VNONRELOC;
  617. }
  618. /*
  619. ** Ensures final expression result (including results from its jump
  620. ** lists) is in next available register.
  621. */
  622. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  623. luaK_dischargevars(fs, e);
  624. freeexp(fs, e);
  625. luaK_reserveregs(fs, 1);
  626. exp2reg(fs, e, fs->freereg - 1);
  627. }
  628. /*
  629. ** Ensures final expression result (including results from its jump
  630. ** lists) is in some (any) register and return that register.
  631. */
  632. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  633. luaK_dischargevars(fs, e);
  634. if (e->k == VNONRELOC) { /* expression already has a register? */
  635. if (!hasjumps(e)) /* no jumps? */
  636. return e->u.info; /* result is already in a register */
  637. if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
  638. exp2reg(fs, e, e->u.info); /* put final result in it */
  639. return e->u.info;
  640. }
  641. }
  642. luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
  643. return e->u.info;
  644. }
  645. /*
  646. ** Ensures final expression result is either in a register or in an
  647. ** upvalue.
  648. */
  649. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  650. if (e->k != VUPVAL || hasjumps(e))
  651. luaK_exp2anyreg(fs, e);
  652. }
  653. /*
  654. ** Ensures final expression result is either in a register or it is
  655. ** a constant.
  656. */
  657. void luaK_exp2val (FuncState *fs, expdesc *e) {
  658. if (hasjumps(e))
  659. luaK_exp2anyreg(fs, e);
  660. else
  661. luaK_dischargevars(fs, e);
  662. }
  663. /*
  664. ** Ensures final expression result is in a valid R/K index
  665. ** (that is, it is either in a register or in 'k' with an index
  666. ** in the range of R/K indices).
  667. ** Returns R/K index.
  668. */
  669. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  670. luaK_exp2val(fs, e);
  671. switch (e->k) { /* move constants to 'k' */
  672. case VTRUE: e->u.info = boolK(fs, 1); goto vk;
  673. case VFALSE: e->u.info = boolK(fs, 0); goto vk;
  674. case VNIL: e->u.info = nilK(fs); goto vk;
  675. case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk;
  676. case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk;
  677. case VK:
  678. vk:
  679. e->k = VK;
  680. if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */
  681. return RKASK(e->u.info);
  682. else break;
  683. default: break;
  684. }
  685. /* not a constant in the right range: put it in a register */
  686. return luaK_exp2anyreg(fs, e);
  687. }
  688. /*
  689. ** Generate code to store result of expression 'ex' into variable 'var'.
  690. */
  691. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  692. switch (var->k) {
  693. case VLOCAL: {
  694. freeexp(fs, ex);
  695. exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */
  696. return;
  697. }
  698. case VUPVAL: {
  699. int e = luaK_exp2anyreg(fs, ex);
  700. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  701. break;
  702. }
  703. case VINDEXED: {
  704. OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
  705. int e = luaK_exp2RK(fs, ex);
  706. luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
  707. break;
  708. }
  709. default: lua_assert(0); /* invalid var kind to store */
  710. }
  711. freeexp(fs, ex);
  712. }
  713. /*
  714. ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
  715. */
  716. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  717. int ereg;
  718. luaK_exp2anyreg(fs, e);
  719. ereg = e->u.info; /* register where 'e' was placed */
  720. freeexp(fs, e);
  721. e->u.info = fs->freereg; /* base register for op_self */
  722. e->k = VNONRELOC; /* self expression has a fixed register */
  723. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  724. luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
  725. freeexp(fs, key);
  726. }
  727. /*
  728. ** Negate condition 'e' (where 'e' is a comparison).
  729. */
  730. static void negatecondition (FuncState *fs, expdesc *e) {
  731. Instruction *pc = getjumpcontrol(fs, e->u.info);
  732. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  733. GET_OPCODE(*pc) != OP_TEST);
  734. SETARG_A(*pc, !(GETARG_A(*pc)));
  735. }
  736. /*
  737. ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
  738. ** is true, code will jump if 'e' is true.) Return jump position.
  739. ** Optimize when 'e' is 'not' something, inverting the condition
  740. ** and removing the 'not'.
  741. */
  742. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  743. if (e->k == VRELOCABLE) {
  744. Instruction ie = getinstruction(fs, e);
  745. if (GET_OPCODE(ie) == OP_NOT) {
  746. fs->pc--; /* remove previous OP_NOT */
  747. return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
  748. }
  749. /* else go through */
  750. }
  751. discharge2anyreg(fs, e);
  752. freeexp(fs, e);
  753. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
  754. }
  755. /*
  756. ** Emit code to go through if 'e' is true, jump otherwise.
  757. */
  758. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  759. int pc; /* pc of new jump */
  760. luaK_dischargevars(fs, e);
  761. switch (e->k) {
  762. case VJMP: { /* condition? */
  763. negatecondition(fs, e); /* jump when it is false */
  764. pc = e->u.info; /* save jump position */
  765. break;
  766. }
  767. case VK: case VKFLT: case VKINT: case VTRUE: {
  768. pc = NO_JUMP; /* always true; do nothing */
  769. break;
  770. }
  771. default: {
  772. pc = jumponcond(fs, e, 0); /* jump when false */
  773. break;
  774. }
  775. }
  776. luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
  777. luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
  778. e->t = NO_JUMP;
  779. }
  780. /*
  781. ** Emit code to go through if 'e' is false, jump otherwise.
  782. */
  783. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  784. int pc; /* pc of new jump */
  785. luaK_dischargevars(fs, e);
  786. switch (e->k) {
  787. case VJMP: {
  788. pc = e->u.info; /* already jump if true */
  789. break;
  790. }
  791. case VNIL: case VFALSE: {
  792. pc = NO_JUMP; /* always false; do nothing */
  793. break;
  794. }
  795. default: {
  796. pc = jumponcond(fs, e, 1); /* jump if true */
  797. break;
  798. }
  799. }
  800. luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
  801. luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
  802. e->f = NO_JUMP;
  803. }
  804. /*
  805. ** Code 'not e', doing constant folding.
  806. */
  807. static void codenot (FuncState *fs, expdesc *e) {
  808. luaK_dischargevars(fs, e);
  809. switch (e->k) {
  810. case VNIL: case VFALSE: {
  811. e->k = VTRUE; /* true == not nil == not false */
  812. break;
  813. }
  814. case VK: case VKFLT: case VKINT: case VTRUE: {
  815. e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
  816. break;
  817. }
  818. case VJMP: {
  819. negatecondition(fs, e);
  820. break;
  821. }
  822. case VRELOCABLE:
  823. case VNONRELOC: {
  824. discharge2anyreg(fs, e);
  825. freeexp(fs, e);
  826. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  827. e->k = VRELOCABLE;
  828. break;
  829. }
  830. default: lua_assert(0); /* cannot happen */
  831. }
  832. /* interchange true and false lists */
  833. { int temp = e->f; e->f = e->t; e->t = temp; }
  834. removevalues(fs, e->f); /* values are useless when negated */
  835. removevalues(fs, e->t);
  836. }
  837. /*
  838. ** Check whether expression 'e' is a literal string
  839. */
  840. static int isKstr (FuncState *fs, expdesc *e) {
  841. return (e->k == VK && ttisstring(&fs->f->k[e->u.info]));
  842. }
  843. /*
  844. ** Check whether expression 'e' is a literal integer in
  845. ** proper range
  846. */
  847. static int isKint (expdesc *e) {
  848. return (e->k == VKINT && !hasjumps(e) &&
  849. l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
  850. }
  851. /*
  852. ** Create expression 't[k]'. 't' must have its final result already in a
  853. ** register or upvalue. Upvalues can only be indexed by literal strings.
  854. */
  855. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  856. lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL));
  857. if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */
  858. luaK_exp2anyreg(fs, t); /* put it in a register */
  859. t->u.ind.t = t->u.info; /* register or upvalue index */
  860. t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */
  861. t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL;
  862. t->k = VINDEXED;
  863. }
  864. /*
  865. ** Return false if folding can raise an error.
  866. ** Bitwise operations need operands convertible to integers; division
  867. ** operations cannot have 0 as divisor.
  868. */
  869. static int validop (int op, TValue *v1, TValue *v2) {
  870. switch (op) {
  871. case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
  872. case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
  873. lua_Integer i;
  874. return (tointeger(v1, &i) && tointeger(v2, &i));
  875. }
  876. case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
  877. return (nvalue(v2) != 0);
  878. default: return 1; /* everything else is valid */
  879. }
  880. }
  881. /*
  882. ** Try to "constant-fold" an operation; return 1 iff successful.
  883. ** (In this case, 'e1' has the final result.)
  884. */
  885. static int constfolding (FuncState *fs, int op, expdesc *e1,
  886. const expdesc *e2) {
  887. TValue v1, v2, res;
  888. if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
  889. return 0; /* non-numeric operands or not safe to fold */
  890. luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
  891. if (ttisinteger(&res)) {
  892. e1->k = VKINT;
  893. e1->u.ival = ivalue(&res);
  894. }
  895. else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
  896. lua_Number n = fltvalue(&res);
  897. if (luai_numisnan(n) || n == 0)
  898. return 0;
  899. e1->k = VKFLT;
  900. e1->u.nval = n;
  901. }
  902. return 1;
  903. }
  904. /*
  905. ** Emit code for unary expressions that "produce values"
  906. ** (everything but 'not').
  907. ** Expression to produce final result will be encoded in 'e'.
  908. */
  909. static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
  910. int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
  911. freeexp(fs, e);
  912. e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
  913. e->k = VRELOCABLE; /* all those operations are relocatable */
  914. luaK_fixline(fs, line);
  915. }
  916. /*
  917. ** Emit code for binary expressions that "produce values"
  918. ** (everything but logical operators 'and'/'or' and comparison
  919. ** operators).
  920. ** Expression to produce final result will be encoded in 'e1'.
  921. ** Because 'luaK_exp2RK' can free registers, its calls must be
  922. ** in "stack order" (that is, first on 'e2', which may have more
  923. ** recent registers to be released).
  924. */
  925. static void codebinexpval (FuncState *fs, OpCode op,
  926. expdesc *e1, expdesc *e2, int line) {
  927. int v1, v2;
  928. if (op == OP_ADD && (isKint(e1) || isKint(e2))) {
  929. if (isKint(e2)) {
  930. v2 = cast_int(e2->u.ival);
  931. v1 = luaK_exp2anyreg(fs, e1);
  932. }
  933. else { /* exchange operands to make 2nd one a constant */
  934. v2 = cast_int(e1->u.ival);
  935. v1 = luaK_exp2anyreg(fs, e2) | BITRK; /* K bit signal the exchange */
  936. }
  937. op = OP_ADDI;
  938. }
  939. else {
  940. v2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */
  941. v1 = luaK_exp2RK(fs, e1);
  942. }
  943. freeexps(fs, e1, e2);
  944. e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */
  945. e1->k = VRELOCABLE; /* all those operations are relocatable */
  946. luaK_fixline(fs, line);
  947. }
  948. /*
  949. ** Emit code for comparisons.
  950. ** 'e1' was already put in R/K form by 'luaK_infix'.
  951. */
  952. static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
  953. int rk1 = (e1->k == VK) ? RKASK(e1->u.info)
  954. : check_exp(e1->k == VNONRELOC, e1->u.info);
  955. int rk2 = luaK_exp2RK(fs, e2);
  956. freeexps(fs, e1, e2);
  957. switch (opr) {
  958. case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */
  959. e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2);
  960. break;
  961. }
  962. case OPR_GT: case OPR_GE: {
  963. /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */
  964. OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
  965. e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */
  966. break;
  967. }
  968. default: { /* '==', '<', '<=' use their own opcodes */
  969. OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
  970. e1->u.info = condjump(fs, op, 1, rk1, rk2);
  971. break;
  972. }
  973. }
  974. e1->k = VJMP;
  975. }
  976. /*
  977. ** Aplly prefix operation 'op' to expression 'e'.
  978. */
  979. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  980. static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
  981. switch (op) {
  982. case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
  983. if (constfolding(fs, op + LUA_OPUNM, e, &ef))
  984. break;
  985. /* FALLTHROUGH */
  986. case OPR_LEN:
  987. codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
  988. break;
  989. case OPR_NOT: codenot(fs, e); break;
  990. default: lua_assert(0);
  991. }
  992. }
  993. /*
  994. ** Process 1st operand 'v' of binary operation 'op' before reading
  995. ** 2nd operand.
  996. */
  997. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  998. switch (op) {
  999. case OPR_AND: {
  1000. luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
  1001. break;
  1002. }
  1003. case OPR_OR: {
  1004. luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
  1005. break;
  1006. }
  1007. case OPR_CONCAT: {
  1008. luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */
  1009. break;
  1010. }
  1011. case OPR_ADD: case OPR_SUB:
  1012. case OPR_MUL: case OPR_DIV: case OPR_IDIV:
  1013. case OPR_MOD: case OPR_POW:
  1014. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  1015. case OPR_SHL: case OPR_SHR: {
  1016. if (!tonumeral(v, NULL))
  1017. luaK_exp2RK(fs, v);
  1018. /* else keep numeral, which may be folded with 2nd operand */
  1019. break;
  1020. }
  1021. default: {
  1022. luaK_exp2RK(fs, v);
  1023. break;
  1024. }
  1025. }
  1026. }
  1027. /*
  1028. ** Finalize code for binary operation, after reading 2nd operand.
  1029. ** For '(a .. b .. c)' (which is '(a .. (b .. c))', because
  1030. ** concatenation is right associative), merge second CONCAT into first
  1031. ** one.
  1032. */
  1033. void luaK_posfix (FuncState *fs, BinOpr op,
  1034. expdesc *e1, expdesc *e2, int line) {
  1035. switch (op) {
  1036. case OPR_AND: {
  1037. lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */
  1038. luaK_dischargevars(fs, e2);
  1039. luaK_concat(fs, &e2->f, e1->f);
  1040. *e1 = *e2;
  1041. break;
  1042. }
  1043. case OPR_OR: {
  1044. lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */
  1045. luaK_dischargevars(fs, e2);
  1046. luaK_concat(fs, &e2->t, e1->t);
  1047. *e1 = *e2;
  1048. break;
  1049. }
  1050. case OPR_CONCAT: {
  1051. luaK_exp2val(fs, e2);
  1052. if (e2->k == VRELOCABLE &&
  1053. GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) {
  1054. lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1);
  1055. freeexp(fs, e1);
  1056. SETARG_B(getinstruction(fs, e2), e1->u.info);
  1057. e1->k = VRELOCABLE; e1->u.info = e2->u.info;
  1058. }
  1059. else {
  1060. luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
  1061. codebinexpval(fs, OP_CONCAT, e1, e2, line);
  1062. }
  1063. break;
  1064. }
  1065. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  1066. case OPR_IDIV: case OPR_MOD: case OPR_POW:
  1067. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  1068. case OPR_SHL: case OPR_SHR: {
  1069. if (!constfolding(fs, op + LUA_OPADD, e1, e2))
  1070. codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line);
  1071. break;
  1072. }
  1073. case OPR_EQ: case OPR_LT: case OPR_LE:
  1074. case OPR_NE: case OPR_GT: case OPR_GE: {
  1075. codecomp(fs, op, e1, e2);
  1076. break;
  1077. }
  1078. default: lua_assert(0);
  1079. }
  1080. }
  1081. /*
  1082. ** Change line information associated with current position.
  1083. */
  1084. void luaK_fixline (FuncState *fs, int line) {
  1085. fs->f->lineinfo[fs->pc - 1] = line;
  1086. }
  1087. /*
  1088. ** Emit a SETLIST instruction.
  1089. ** 'base' is register that keeps table;
  1090. ** 'nelems' is #table plus those to be stored now;
  1091. ** 'tostore' is number of values (in registers 'base + 1',...) to add to
  1092. ** table (or LUA_MULTRET to add up to stack top).
  1093. */
  1094. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  1095. int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
  1096. int b = (tostore == LUA_MULTRET) ? 0 : tostore;
  1097. lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
  1098. if (c <= MAXARG_C)
  1099. luaK_codeABC(fs, OP_SETLIST, base, b, c);
  1100. else if (c <= MAXARG_Ax) {
  1101. luaK_codeABC(fs, OP_SETLIST, base, b, 0);
  1102. codeextraarg(fs, c);
  1103. }
  1104. else
  1105. luaX_syntaxerror(fs->ls, "constructor too long");
  1106. fs->freereg = base + 1; /* free registers with list values */
  1107. }