lcode.c 37 KB

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