lcode.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /*
  2. ** $Id: lcode.c,v 2.123 2017/09/19 18:38:14 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lcode_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <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(getBMode(o) != OpArgN || b == 0);
  316. lua_assert(getCMode(o) != OpArgN || c == 0);
  317. lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
  318. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  319. }
  320. /*
  321. ** Format and emit an 'iABx' instruction.
  322. */
  323. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  324. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  325. lua_assert(getCMode(o) == OpArgN);
  326. lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
  327. return luaK_code(fs, CREATE_ABx(o, a, bc));
  328. }
  329. /*
  330. ** Emit an "extra argument" instruction (format 'iAx')
  331. */
  332. static int codeextraarg (FuncState *fs, int a) {
  333. lua_assert(a <= MAXARG_Ax);
  334. return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
  335. }
  336. /*
  337. ** Emit a "load constant" instruction, using either 'OP_LOADK'
  338. ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
  339. ** instruction with "extra argument".
  340. */
  341. static int luaK_codek (FuncState *fs, int reg, int k) {
  342. if (k <= MAXARG_Bx)
  343. return luaK_codeABx(fs, OP_LOADK, reg, k);
  344. else {
  345. int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
  346. codeextraarg(fs, k);
  347. return p;
  348. }
  349. }
  350. /*
  351. ** Check register-stack level, keeping track of its maximum size
  352. ** in field 'maxstacksize'
  353. */
  354. void luaK_checkstack (FuncState *fs, int n) {
  355. int newstack = fs->freereg + n;
  356. if (newstack > fs->f->maxstacksize) {
  357. if (newstack >= MAXREGS)
  358. luaX_syntaxerror(fs->ls,
  359. "function or expression needs too many registers");
  360. fs->f->maxstacksize = cast_byte(newstack);
  361. }
  362. }
  363. /*
  364. ** Reserve 'n' registers in register stack
  365. */
  366. void luaK_reserveregs (FuncState *fs, int n) {
  367. luaK_checkstack(fs, n);
  368. fs->freereg += n;
  369. }
  370. /*
  371. ** Free register 'reg', if it is neither a constant index nor
  372. ** a local variable.
  373. )
  374. */
  375. static void freereg (FuncState *fs, int reg) {
  376. if (!ISK(reg) && reg >= fs->nactvar) {
  377. fs->freereg--;
  378. lua_assert(reg == fs->freereg);
  379. }
  380. }
  381. /*
  382. ** Free two registers in proper order
  383. */
  384. static void freeregs (FuncState *fs, int r1, int r2) {
  385. if (r1 > r2) {
  386. freereg(fs, r1);
  387. freereg(fs, r2);
  388. }
  389. else {
  390. freereg(fs, r2);
  391. freereg(fs, r1);
  392. }
  393. }
  394. /*
  395. ** Free register used by expression 'e' (if any)
  396. */
  397. static void freeexp (FuncState *fs, expdesc *e) {
  398. if (e->k == VNONRELOC)
  399. freereg(fs, e->u.info);
  400. }
  401. /*
  402. ** Free registers used by expressions 'e1' and 'e2' (if any) in proper
  403. ** order.
  404. */
  405. static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
  406. int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
  407. int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
  408. freeregs(fs, r1, r2);
  409. }
  410. /*
  411. ** Add constant 'v' to prototype's list of constants (field 'k').
  412. ** Use scanner's table to cache position of constants in constant list
  413. ** and try to reuse constants. Because some values should not be used
  414. ** as keys (nil cannot be a key, integer keys can collapse with float
  415. ** keys), the caller must provide a useful 'key' for indexing the cache.
  416. */
  417. static int addk (FuncState *fs, TValue *key, TValue *v) {
  418. lua_State *L = fs->ls->L;
  419. Proto *f = fs->f;
  420. TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
  421. int k, oldsize;
  422. if (ttisinteger(idx)) { /* is there an index there? */
  423. k = cast_int(ivalue(idx));
  424. /* correct value? (warning: must distinguish floats from integers!) */
  425. if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
  426. luaV_rawequalobj(&f->k[k], v))
  427. return k; /* reuse index */
  428. }
  429. /* constant not found; create a new entry */
  430. oldsize = f->sizek;
  431. k = fs->nk;
  432. /* numerical value does not need GC barrier;
  433. table has no metatable, so it does not need to invalidate cache */
  434. setivalue(idx, k);
  435. luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  436. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  437. setobj(L, &f->k[k], v);
  438. fs->nk++;
  439. luaC_barrier(L, f, v);
  440. return k;
  441. }
  442. /*
  443. ** Add a string to list of constants and return its index.
  444. */
  445. int luaK_stringK (FuncState *fs, TString *s) {
  446. TValue o;
  447. setsvalue(fs->ls->L, &o, s);
  448. return addk(fs, &o, &o); /* use string itself as key */
  449. }
  450. /*
  451. ** Add an integer to list of constants and return its index.
  452. ** Integers use userdata as keys to avoid collision with floats with
  453. ** same value; conversion to 'void*' is used only for hashing, so there
  454. ** are no "precision" problems.
  455. */
  456. static int luaK_intK (FuncState *fs, lua_Integer n) {
  457. TValue k, o;
  458. setpvalue(&k, cast(void*, cast(size_t, n)));
  459. setivalue(&o, n);
  460. return addk(fs, &k, &o);
  461. }
  462. /*
  463. ** Add a float to list of constants and return its index.
  464. */
  465. static int luaK_numberK (FuncState *fs, lua_Number r) {
  466. TValue o;
  467. setfltvalue(&o, r);
  468. return addk(fs, &o, &o); /* use number itself as key */
  469. }
  470. /*
  471. ** Add a boolean to list of constants and return its index.
  472. */
  473. static int boolK (FuncState *fs, int b) {
  474. TValue o;
  475. setbvalue(&o, b);
  476. return addk(fs, &o, &o); /* use boolean itself as key */
  477. }
  478. /*
  479. ** Add nil to list of constants and return its index.
  480. */
  481. static int nilK (FuncState *fs) {
  482. TValue k, v;
  483. setnilvalue(&v);
  484. /* cannot use nil as key; instead use table itself to represent nil */
  485. sethvalue(fs->ls->L, &k, fs->ls->h);
  486. return addk(fs, &k, &v);
  487. }
  488. void luaK_int (FuncState *fs, int reg, lua_Integer i) {
  489. if (l_castS2U(i) + MAXARG_sBx <= l_castS2U(MAXARG_Bx))
  490. luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
  491. else
  492. luaK_codek(fs, reg, luaK_intK(fs, i));
  493. }
  494. static void luaK_float (FuncState *fs, int reg, lua_Number f) {
  495. TValue v;
  496. lua_Integer fi;
  497. setfltvalue(&v, f);
  498. if (luaV_tointeger(&v, &fi, 0) &&
  499. l_castS2U(fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx))
  500. luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
  501. else
  502. luaK_codek(fs, reg, luaK_numberK(fs, f));
  503. }
  504. /*
  505. ** Fix an expression to return the number of results 'nresults'.
  506. ** Either 'e' is a multi-ret expression (function call or vararg)
  507. ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
  508. */
  509. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  510. if (e->k == VCALL) { /* expression is an open function call? */
  511. SETARG_C(getinstruction(fs, e), nresults + 1);
  512. }
  513. else if (e->k == VVARARG) {
  514. Instruction *pc = &getinstruction(fs, e);
  515. SETARG_B(*pc, nresults + 1);
  516. SETARG_A(*pc, fs->freereg);
  517. luaK_reserveregs(fs, 1);
  518. }
  519. else lua_assert(nresults == LUA_MULTRET);
  520. }
  521. /*
  522. ** Fix an expression to return one result.
  523. ** If expression is not a multi-ret expression (function call or
  524. ** vararg), it already returns one result, so nothing needs to be done.
  525. ** Function calls become VNONRELOC expressions (as its result comes
  526. ** fixed in the base register of the call), while vararg expressions
  527. ** become VRELOCABLE (as OP_VARARG puts its results where it wants).
  528. ** (Calls are created returning one result, so that does not need
  529. ** to be fixed.)
  530. */
  531. void luaK_setoneret (FuncState *fs, expdesc *e) {
  532. if (e->k == VCALL) { /* expression is an open function call? */
  533. /* already returns 1 value */
  534. lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
  535. e->k = VNONRELOC; /* result has fixed position */
  536. e->u.info = GETARG_A(getinstruction(fs, e));
  537. }
  538. else if (e->k == VVARARG) {
  539. SETARG_B(getinstruction(fs, e), 2);
  540. e->k = VRELOCABLE; /* can relocate its simple result */
  541. }
  542. }
  543. /*
  544. ** Ensure that expression 'e' is not a variable.
  545. */
  546. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  547. switch (e->k) {
  548. case VLOCAL: { /* already in a register */
  549. e->k = VNONRELOC; /* becomes a non-relocatable value */
  550. break;
  551. }
  552. case VUPVAL: { /* move value to some (pending) register */
  553. e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
  554. e->k = VRELOCABLE;
  555. break;
  556. }
  557. case VINDEXUP: {
  558. e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
  559. e->k = VRELOCABLE;
  560. break;
  561. }
  562. case VINDEXI: {
  563. freereg(fs, e->u.ind.t);
  564. e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
  565. e->k = VRELOCABLE;
  566. break;
  567. }
  568. case VINDEXSTR: {
  569. freereg(fs, e->u.ind.t);
  570. e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
  571. e->k = VRELOCABLE;
  572. break;
  573. }
  574. case VINDEXED: {
  575. freeregs(fs, e->u.ind.t, e->u.ind.idx);
  576. e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
  577. e->k = VRELOCABLE;
  578. break;
  579. }
  580. case VVARARG: case VCALL: {
  581. luaK_setoneret(fs, e);
  582. break;
  583. }
  584. default: break; /* there is one value available (somewhere) */
  585. }
  586. }
  587. /*
  588. ** Ensures expression value is in register 'reg' (and therefore
  589. ** 'e' will become a non-relocatable expression).
  590. */
  591. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  592. luaK_dischargevars(fs, e);
  593. switch (e->k) {
  594. case VNIL: {
  595. luaK_nil(fs, reg, 1);
  596. break;
  597. }
  598. case VFALSE: case VTRUE: {
  599. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  600. break;
  601. }
  602. case VK: {
  603. luaK_codek(fs, reg, e->u.info);
  604. break;
  605. }
  606. case VKFLT: {
  607. luaK_float(fs, reg, e->u.nval);
  608. break;
  609. }
  610. case VKINT: {
  611. luaK_int(fs, reg, e->u.ival);
  612. break;
  613. }
  614. case VRELOCABLE: {
  615. Instruction *pc = &getinstruction(fs, e);
  616. SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
  617. break;
  618. }
  619. case VNONRELOC: {
  620. if (reg != e->u.info)
  621. luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
  622. break;
  623. }
  624. default: {
  625. lua_assert(e->k == VJMP);
  626. return; /* nothing to do... */
  627. }
  628. }
  629. e->u.info = reg;
  630. e->k = VNONRELOC;
  631. }
  632. /*
  633. ** Ensures expression value is in any register.
  634. */
  635. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  636. if (e->k != VNONRELOC) { /* no fixed register yet? */
  637. luaK_reserveregs(fs, 1); /* get a register */
  638. discharge2reg(fs, e, fs->freereg-1); /* put value there */
  639. }
  640. }
  641. static int code_loadbool (FuncState *fs, int A, int b, int jump) {
  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. for (; list != NO_JUMP; list = getjump(fs, list)) {
  651. Instruction i = *getjumpcontrol(fs, list);
  652. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  653. }
  654. return 0; /* not found */
  655. }
  656. /*
  657. ** Ensures final expression result (including results from its jump
  658. ** lists) is in register 'reg'.
  659. ** If expression has jumps, need to patch these jumps either to
  660. ** its final position or to "load" instructions (for those tests
  661. ** that do not produce values).
  662. */
  663. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  664. discharge2reg(fs, e, reg);
  665. if (e->k == VJMP) /* expression itself is a test? */
  666. luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
  667. if (hasjumps(e)) {
  668. int final; /* position after whole expression */
  669. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  670. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  671. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  672. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  673. p_f = code_loadbool(fs, reg, 0, 1);
  674. p_t = code_loadbool(fs, reg, 1, 0);
  675. luaK_patchtohere(fs, fj);
  676. }
  677. final = luaK_getlabel(fs);
  678. patchlistaux(fs, e->f, final, reg, p_f);
  679. patchlistaux(fs, e->t, final, reg, p_t);
  680. }
  681. e->f = e->t = NO_JUMP;
  682. e->u.info = reg;
  683. e->k = VNONRELOC;
  684. }
  685. /*
  686. ** Ensures final expression result (including results from its jump
  687. ** lists) is in next available register.
  688. */
  689. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  690. luaK_dischargevars(fs, e);
  691. freeexp(fs, e);
  692. luaK_reserveregs(fs, 1);
  693. exp2reg(fs, e, fs->freereg - 1);
  694. }
  695. /*
  696. ** Ensures final expression result (including results from its jump
  697. ** lists) is in some (any) register and return that register.
  698. */
  699. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  700. luaK_dischargevars(fs, e);
  701. if (e->k == VNONRELOC) { /* expression already has a register? */
  702. if (!hasjumps(e)) /* no jumps? */
  703. return e->u.info; /* result is already in a register */
  704. if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
  705. exp2reg(fs, e, e->u.info); /* put final result in it */
  706. return e->u.info;
  707. }
  708. }
  709. luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
  710. return e->u.info;
  711. }
  712. /*
  713. ** Ensures final expression result is either in a register or in an
  714. ** upvalue.
  715. */
  716. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  717. if (e->k != VUPVAL || hasjumps(e))
  718. luaK_exp2anyreg(fs, e);
  719. }
  720. /*
  721. ** Ensures final expression result is either in a register or it is
  722. ** a constant.
  723. */
  724. void luaK_exp2val (FuncState *fs, expdesc *e) {
  725. if (hasjumps(e))
  726. luaK_exp2anyreg(fs, e);
  727. else
  728. luaK_dischargevars(fs, e);
  729. }
  730. /*
  731. ** Ensures final expression result is in a valid R/K index
  732. ** (that is, it is either in a register or in 'k' with an index
  733. ** in the range of R/K indices).
  734. ** Returns R/K index.
  735. */
  736. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  737. luaK_exp2val(fs, e);
  738. switch (e->k) { /* move constants to 'k' */
  739. case VTRUE: e->u.info = boolK(fs, 1); goto vk;
  740. case VFALSE: e->u.info = boolK(fs, 0); goto vk;
  741. case VNIL: e->u.info = nilK(fs); goto vk;
  742. case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk;
  743. case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk;
  744. case VK:
  745. vk:
  746. e->k = VK;
  747. if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */
  748. return RKASK(e->u.info);
  749. else break;
  750. default: break;
  751. }
  752. /* not a constant in the right range: put it in a register */
  753. return luaK_exp2anyreg(fs, e);
  754. }
  755. /*
  756. ** Generate code to store result of expression 'ex' into variable 'var'.
  757. */
  758. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  759. switch (var->k) {
  760. case VLOCAL: {
  761. freeexp(fs, ex);
  762. exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */
  763. return;
  764. }
  765. case VUPVAL: {
  766. int e = luaK_exp2anyreg(fs, ex);
  767. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  768. break;
  769. }
  770. case VINDEXUP: {
  771. int e = luaK_exp2RK(fs, ex);
  772. luaK_codeABC(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, e);
  773. break;
  774. }
  775. case VINDEXI: {
  776. int e = luaK_exp2RK(fs, ex);
  777. luaK_codeABC(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, e);
  778. break;
  779. }
  780. case VINDEXSTR: {
  781. int e = luaK_exp2RK(fs, ex);
  782. luaK_codeABC(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, e);
  783. break;
  784. }
  785. case VINDEXED: {
  786. int e = luaK_exp2RK(fs, ex);
  787. luaK_codeABC(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, e);
  788. break;
  789. }
  790. default: lua_assert(0); /* invalid var kind to store */
  791. }
  792. freeexp(fs, ex);
  793. }
  794. /*
  795. ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
  796. */
  797. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  798. int ereg;
  799. luaK_exp2anyreg(fs, e);
  800. ereg = e->u.info; /* register where 'e' was placed */
  801. freeexp(fs, e);
  802. e->u.info = fs->freereg; /* base register for op_self */
  803. e->k = VNONRELOC; /* self expression has a fixed register */
  804. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  805. luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
  806. freeexp(fs, key);
  807. }
  808. /*
  809. ** Negate condition 'e' (where 'e' is a comparison).
  810. */
  811. static void negatecondition (FuncState *fs, expdesc *e) {
  812. Instruction *pc = getjumpcontrol(fs, e->u.info);
  813. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  814. GET_OPCODE(*pc) != OP_TEST);
  815. SETARG_A(*pc, !(GETARG_A(*pc)));
  816. }
  817. /*
  818. ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
  819. ** is true, code will jump if 'e' is true.) Return jump position.
  820. ** Optimize when 'e' is 'not' something, inverting the condition
  821. ** and removing the 'not'.
  822. */
  823. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  824. if (e->k == VRELOCABLE) {
  825. Instruction ie = getinstruction(fs, e);
  826. if (GET_OPCODE(ie) == OP_NOT) {
  827. fs->pc--; /* remove previous OP_NOT */
  828. return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
  829. }
  830. /* else go through */
  831. }
  832. discharge2anyreg(fs, e);
  833. freeexp(fs, e);
  834. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
  835. }
  836. /*
  837. ** Emit code to go through if 'e' is true, jump otherwise.
  838. */
  839. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  840. int pc; /* pc of new jump */
  841. luaK_dischargevars(fs, e);
  842. switch (e->k) {
  843. case VJMP: { /* condition? */
  844. negatecondition(fs, e); /* jump when it is false */
  845. pc = e->u.info; /* save jump position */
  846. break;
  847. }
  848. case VK: case VKFLT: case VKINT: case VTRUE: {
  849. pc = NO_JUMP; /* always true; do nothing */
  850. break;
  851. }
  852. default: {
  853. pc = jumponcond(fs, e, 0); /* jump when false */
  854. break;
  855. }
  856. }
  857. luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
  858. luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
  859. e->t = NO_JUMP;
  860. }
  861. /*
  862. ** Emit code to go through if 'e' is false, jump otherwise.
  863. */
  864. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  865. int pc; /* pc of new jump */
  866. luaK_dischargevars(fs, e);
  867. switch (e->k) {
  868. case VJMP: {
  869. pc = e->u.info; /* already jump if true */
  870. break;
  871. }
  872. case VNIL: case VFALSE: {
  873. pc = NO_JUMP; /* always false; do nothing */
  874. break;
  875. }
  876. default: {
  877. pc = jumponcond(fs, e, 1); /* jump if true */
  878. break;
  879. }
  880. }
  881. luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
  882. luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
  883. e->f = NO_JUMP;
  884. }
  885. /*
  886. ** Code 'not e', doing constant folding.
  887. */
  888. static void codenot (FuncState *fs, expdesc *e) {
  889. luaK_dischargevars(fs, e);
  890. switch (e->k) {
  891. case VNIL: case VFALSE: {
  892. e->k = VTRUE; /* true == not nil == not false */
  893. break;
  894. }
  895. case VK: case VKFLT: case VKINT: case VTRUE: {
  896. e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
  897. break;
  898. }
  899. case VJMP: {
  900. negatecondition(fs, e);
  901. break;
  902. }
  903. case VRELOCABLE:
  904. case VNONRELOC: {
  905. discharge2anyreg(fs, e);
  906. freeexp(fs, e);
  907. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  908. e->k = VRELOCABLE;
  909. break;
  910. }
  911. default: lua_assert(0); /* cannot happen */
  912. }
  913. /* interchange true and false lists */
  914. { int temp = e->f; e->f = e->t; e->t = temp; }
  915. removevalues(fs, e->f); /* values are useless when negated */
  916. removevalues(fs, e->t);
  917. }
  918. /*
  919. ** Check whether expression 'e' is a small literal string
  920. */
  921. static int isKstr (FuncState *fs, expdesc *e) {
  922. return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_C &&
  923. ttisshrstring(&fs->f->k[e->u.info]));
  924. }
  925. /*
  926. ** Check whether expression 'e' is a literal integer in
  927. ** proper range
  928. */
  929. static int isKint (expdesc *e) {
  930. return (e->k == VKINT && !hasjumps(e) &&
  931. l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
  932. }
  933. /*
  934. ** Create expression 't[k]'. 't' must have its final result already in a
  935. ** register or upvalue. Upvalues can only be indexed by literal strings.
  936. ** Keys can be literal strings in the constant table or arbitrary
  937. ** values in registers.
  938. */
  939. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  940. lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL));
  941. if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */
  942. luaK_exp2anyreg(fs, t); /* put it in a register */
  943. t->u.ind.t = t->u.info; /* register or upvalue index */
  944. if (t->k == VUPVAL) {
  945. t->u.ind.idx = k->u.info; /* literal string */
  946. t->k = VINDEXUP;
  947. }
  948. else if (isKstr(fs, k)) {
  949. t->u.ind.idx = k->u.info; /* literal string */
  950. t->k = VINDEXSTR;
  951. }
  952. else if (isKint(k)) {
  953. t->u.ind.idx = k->u.ival; /* integer constant */
  954. t->k = VINDEXI;
  955. }
  956. else {
  957. t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */
  958. t->k = VINDEXED;
  959. }
  960. }
  961. /*
  962. ** Return false if folding can raise an error.
  963. ** Bitwise operations need operands convertible to integers; division
  964. ** operations cannot have 0 as divisor.
  965. */
  966. static int validop (int op, TValue *v1, TValue *v2) {
  967. switch (op) {
  968. case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
  969. case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
  970. lua_Integer i;
  971. return (tointeger(v1, &i) && tointeger(v2, &i));
  972. }
  973. case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
  974. return (nvalue(v2) != 0);
  975. default: return 1; /* everything else is valid */
  976. }
  977. }
  978. /*
  979. ** Try to "constant-fold" an operation; return 1 iff successful.
  980. ** (In this case, 'e1' has the final result.)
  981. */
  982. static int constfolding (FuncState *fs, int op, expdesc *e1,
  983. const expdesc *e2) {
  984. TValue v1, v2, res;
  985. if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
  986. return 0; /* non-numeric operands or not safe to fold */
  987. luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
  988. if (ttisinteger(&res)) {
  989. e1->k = VKINT;
  990. e1->u.ival = ivalue(&res);
  991. }
  992. else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
  993. lua_Number n = fltvalue(&res);
  994. if (luai_numisnan(n) || n == 0)
  995. return 0;
  996. e1->k = VKFLT;
  997. e1->u.nval = n;
  998. }
  999. return 1;
  1000. }
  1001. /*
  1002. ** Emit code for unary expressions that "produce values"
  1003. ** (everything but 'not').
  1004. ** Expression to produce final result will be encoded in 'e'.
  1005. */
  1006. static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
  1007. int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
  1008. freeexp(fs, e);
  1009. e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
  1010. e->k = VRELOCABLE; /* all those operations are relocatable */
  1011. luaK_fixline(fs, line);
  1012. }
  1013. /*
  1014. ** Emit code for binary expressions that "produce values"
  1015. ** (everything but logical operators 'and'/'or' and comparison
  1016. ** operators).
  1017. ** Expression to produce final result will be encoded in 'e1'.
  1018. ** Because 'luaK_exp2RK' can free registers, its calls must be
  1019. ** in "stack order" (that is, first on 'e2', which may have more
  1020. ** recent registers to be released).
  1021. */
  1022. static void codebinexpval (FuncState *fs, OpCode op,
  1023. expdesc *e1, expdesc *e2, int line) {
  1024. int v1, v2;
  1025. if (op == OP_ADD && (isKint(e1) || isKint(e2))) {
  1026. if (isKint(e2)) {
  1027. v2 = cast_int(e2->u.ival);
  1028. v1 = luaK_exp2anyreg(fs, e1);
  1029. }
  1030. else { /* exchange operands to make 2nd one a constant */
  1031. v2 = cast_int(e1->u.ival);
  1032. v1 = luaK_exp2anyreg(fs, e2) | BITRK; /* K bit signal the exchange */
  1033. }
  1034. op = OP_ADDI;
  1035. }
  1036. else {
  1037. v2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */
  1038. v1 = luaK_exp2RK(fs, e1);
  1039. }
  1040. freeexps(fs, e1, e2);
  1041. e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */
  1042. e1->k = VRELOCABLE; /* all those operations are relocatable */
  1043. luaK_fixline(fs, line);
  1044. }
  1045. /*
  1046. ** Emit code for comparisons.
  1047. ** 'e1' was already put in R/K form by 'luaK_infix'.
  1048. */
  1049. static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
  1050. int rk1 = (e1->k == VK) ? RKASK(e1->u.info)
  1051. : check_exp(e1->k == VNONRELOC, e1->u.info);
  1052. int rk2 = luaK_exp2RK(fs, e2);
  1053. freeexps(fs, e1, e2);
  1054. switch (opr) {
  1055. case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */
  1056. e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2);
  1057. break;
  1058. }
  1059. case OPR_GT: case OPR_GE: {
  1060. /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */
  1061. OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
  1062. e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */
  1063. break;
  1064. }
  1065. default: { /* '==', '<', '<=' use their own opcodes */
  1066. OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
  1067. e1->u.info = condjump(fs, op, 1, rk1, rk2);
  1068. break;
  1069. }
  1070. }
  1071. e1->k = VJMP;
  1072. }
  1073. /*
  1074. ** Aplly prefix operation 'op' to expression 'e'.
  1075. */
  1076. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  1077. static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
  1078. switch (op) {
  1079. case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
  1080. if (constfolding(fs, op + LUA_OPUNM, e, &ef))
  1081. break;
  1082. /* FALLTHROUGH */
  1083. case OPR_LEN:
  1084. codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
  1085. break;
  1086. case OPR_NOT: codenot(fs, e); break;
  1087. default: lua_assert(0);
  1088. }
  1089. }
  1090. /*
  1091. ** Process 1st operand 'v' of binary operation 'op' before reading
  1092. ** 2nd operand.
  1093. */
  1094. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  1095. switch (op) {
  1096. case OPR_AND: {
  1097. luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
  1098. break;
  1099. }
  1100. case OPR_OR: {
  1101. luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
  1102. break;
  1103. }
  1104. case OPR_CONCAT: {
  1105. luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */
  1106. break;
  1107. }
  1108. case OPR_ADD: case OPR_SUB:
  1109. case OPR_MUL: case OPR_DIV: case OPR_IDIV:
  1110. case OPR_MOD: case OPR_POW:
  1111. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  1112. case OPR_SHL: case OPR_SHR: {
  1113. if (!tonumeral(v, NULL))
  1114. luaK_exp2RK(fs, v);
  1115. /* else keep numeral, which may be folded with 2nd operand */
  1116. break;
  1117. }
  1118. case OPR_EQ: case OPR_LT: case OPR_LE:
  1119. case OPR_NE: case OPR_GT: case OPR_GE: {
  1120. luaK_exp2RK(fs, v);
  1121. break;
  1122. }
  1123. default: lua_assert(0);
  1124. }
  1125. }
  1126. /*
  1127. ** Finalize code for binary operation, after reading 2nd operand.
  1128. ** For '(a .. b .. c)' (which is '(a .. (b .. c))', because
  1129. ** concatenation is right associative), merge second CONCAT into first
  1130. ** one.
  1131. */
  1132. void luaK_posfix (FuncState *fs, BinOpr op,
  1133. expdesc *e1, expdesc *e2, int line) {
  1134. switch (op) {
  1135. case OPR_AND: {
  1136. lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */
  1137. luaK_dischargevars(fs, e2);
  1138. luaK_concat(fs, &e2->f, e1->f);
  1139. *e1 = *e2;
  1140. break;
  1141. }
  1142. case OPR_OR: {
  1143. lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */
  1144. luaK_dischargevars(fs, e2);
  1145. luaK_concat(fs, &e2->t, e1->t);
  1146. *e1 = *e2;
  1147. break;
  1148. }
  1149. case OPR_CONCAT: {
  1150. luaK_exp2val(fs, e2);
  1151. if (e2->k == VRELOCABLE &&
  1152. GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) {
  1153. lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1);
  1154. freeexp(fs, e1);
  1155. SETARG_B(getinstruction(fs, e2), e1->u.info);
  1156. e1->k = VRELOCABLE; e1->u.info = e2->u.info;
  1157. }
  1158. else {
  1159. luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
  1160. codebinexpval(fs, OP_CONCAT, e1, e2, line);
  1161. }
  1162. break;
  1163. }
  1164. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  1165. case OPR_IDIV: case OPR_MOD: case OPR_POW:
  1166. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  1167. case OPR_SHL: case OPR_SHR: {
  1168. if (!constfolding(fs, op + LUA_OPADD, e1, e2))
  1169. codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line);
  1170. break;
  1171. }
  1172. case OPR_EQ: case OPR_LT: case OPR_LE:
  1173. case OPR_NE: case OPR_GT: case OPR_GE: {
  1174. codecomp(fs, op, e1, e2);
  1175. break;
  1176. }
  1177. default: lua_assert(0);
  1178. }
  1179. }
  1180. /*
  1181. ** Change line information associated with current position. If that
  1182. ** information is absolute, just change it and correct 'previousline'.
  1183. ** Otherwise, restore 'previousline' to its value before saving the
  1184. ** current position and than saves the line information again, with the
  1185. ** new line.
  1186. */
  1187. void luaK_fixline (FuncState *fs, int line) {
  1188. Proto *f = fs->f;
  1189. if (f->lineinfo[fs->pc - 1] == ABSLINEINFO) {
  1190. lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == fs->pc - 1);
  1191. f->abslineinfo[fs->nabslineinfo - 1].line = line;
  1192. fs->previousline = line;
  1193. }
  1194. else {
  1195. fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */
  1196. savelineinfo(fs, f, fs->pc - 1, line); /* redo it */
  1197. }
  1198. }
  1199. /*
  1200. ** Emit a SETLIST instruction.
  1201. ** 'base' is register that keeps table;
  1202. ** 'nelems' is #table plus those to be stored now;
  1203. ** 'tostore' is number of values (in registers 'base + 1',...) to add to
  1204. ** table (or LUA_MULTRET to add up to stack top).
  1205. */
  1206. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  1207. int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
  1208. int b = (tostore == LUA_MULTRET) ? 0 : tostore;
  1209. lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
  1210. if (c <= MAXARG_C)
  1211. luaK_codeABC(fs, OP_SETLIST, base, b, c);
  1212. else if (c <= MAXARG_Ax) {
  1213. luaK_codeABC(fs, OP_SETLIST, base, b, 0);
  1214. codeextraarg(fs, c);
  1215. }
  1216. else
  1217. luaX_syntaxerror(fs->ls, "constructor too long");
  1218. fs->freereg = base + 1; /* free registers with list values */
  1219. }