lcode.c 35 KB

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