lcode.c 39 KB

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