You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

886 lines
22 KiB

  1. /*
  2. ** $Id: lcode.c,v 2.60 2011/08/30 16:26:41 roberto Exp $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <stdlib.h>
  10. #define lcode_c
  11. #define LUA_CORE
  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. #define hasjumps(e) ((e)->t != (e)->f)
  26. static int isnumeral(expdesc *e) {
  27. return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
  28. }
  29. void luaK_nil (FuncState *fs, int from, int n) {
  30. Instruction *previous;
  31. int l = from + n - 1; /* last register to set nil */
  32. if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
  33. previous = &fs->f->code[fs->pc-1];
  34. if (GET_OPCODE(*previous) == OP_LOADNIL) {
  35. int pfrom = GETARG_A(*previous);
  36. int pl = pfrom + GETARG_B(*previous);
  37. if ((pfrom <= from && from <= pl + 1) ||
  38. (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
  39. if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
  40. if (pl > l) l = pl; /* l = max(l, pl) */
  41. SETARG_A(*previous, from);
  42. SETARG_B(*previous, l - from);
  43. return;
  44. }
  45. } /* else go through */
  46. }
  47. luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
  48. }
  49. int luaK_jump (FuncState *fs) {
  50. int jpc = fs->jpc; /* save list of jumps to here */
  51. int j;
  52. fs->jpc = NO_JUMP;
  53. j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
  54. luaK_concat(fs, &j, jpc); /* keep them on hold */
  55. return j;
  56. }
  57. void luaK_ret (FuncState *fs, int first, int nret) {
  58. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  59. }
  60. static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
  61. luaK_codeABC(fs, op, A, B, C);
  62. return luaK_jump(fs);
  63. }
  64. static void fixjump (FuncState *fs, int pc, int dest) {
  65. Instruction *jmp = &fs->f->code[pc];
  66. int offset = dest-(pc+1);
  67. lua_assert(dest != NO_JUMP);
  68. if (abs(offset) > MAXARG_sBx)
  69. luaX_syntaxerror(fs->ls, "control structure too long");
  70. SETARG_sBx(*jmp, offset);
  71. }
  72. /*
  73. ** returns current `pc' and marks it as a jump target (to avoid wrong
  74. ** optimizations with consecutive instructions not in the same basic block).
  75. */
  76. int luaK_getlabel (FuncState *fs) {
  77. fs->lasttarget = fs->pc;
  78. return fs->pc;
  79. }
  80. static int getjump (FuncState *fs, int pc) {
  81. int offset = GETARG_sBx(fs->f->code[pc]);
  82. if (offset == NO_JUMP) /* point to itself represents end of list */
  83. return NO_JUMP; /* end of list */
  84. else
  85. return (pc+1)+offset; /* turn offset into absolute position */
  86. }
  87. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  88. Instruction *pi = &fs->f->code[pc];
  89. if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
  90. return pi-1;
  91. else
  92. return pi;
  93. }
  94. /*
  95. ** check whether list has any jump that do not produce a value
  96. ** (or produce an inverted value)
  97. */
  98. static int need_value (FuncState *fs, int list) {
  99. for (; list != NO_JUMP; list = getjump(fs, list)) {
  100. Instruction i = *getjumpcontrol(fs, list);
  101. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  102. }
  103. return 0; /* not found */
  104. }
  105. static int patchtestreg (FuncState *fs, int node, int reg) {
  106. Instruction *i = getjumpcontrol(fs, node);
  107. if (GET_OPCODE(*i) != OP_TESTSET)
  108. return 0; /* cannot patch other instructions */
  109. if (reg != NO_REG && reg != GETARG_B(*i))
  110. SETARG_A(*i, reg);
  111. else /* no register to put value or register already has the value */
  112. *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
  113. return 1;
  114. }
  115. static void removevalues (FuncState *fs, int list) {
  116. for (; list != NO_JUMP; list = getjump(fs, list))
  117. patchtestreg(fs, list, NO_REG);
  118. }
  119. static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
  120. int dtarget) {
  121. while (list != NO_JUMP) {
  122. int next = getjump(fs, list);
  123. if (patchtestreg(fs, list, reg))
  124. fixjump(fs, list, vtarget);
  125. else
  126. fixjump(fs, list, dtarget); /* jump to default target */
  127. list = next;
  128. }
  129. }
  130. static void dischargejpc (FuncState *fs) {
  131. patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
  132. fs->jpc = NO_JUMP;
  133. }
  134. void luaK_patchlist (FuncState *fs, int list, int target) {
  135. if (target == fs->pc)
  136. luaK_patchtohere(fs, list);
  137. else {
  138. lua_assert(target < fs->pc);
  139. patchlistaux(fs, list, target, NO_REG, target);
  140. }
  141. }
  142. LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
  143. level++; /* argument is +1 to reserve 0 as non-op */
  144. while (list != NO_JUMP) {
  145. int next = getjump(fs, list);
  146. lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
  147. (GETARG_A(fs->f->code[list]) == 0 ||
  148. GETARG_A(fs->f->code[list]) >= level));
  149. SETARG_A(fs->f->code[list], level);
  150. list = next;
  151. }
  152. }
  153. void luaK_patchtohere (FuncState *fs, int list) {
  154. luaK_getlabel(fs);
  155. luaK_concat(fs, &fs->jpc, list);
  156. }
  157. void luaK_concat (FuncState *fs, int *l1, int l2) {
  158. if (l2 == NO_JUMP) return;
  159. else if (*l1 == NO_JUMP)
  160. *l1 = l2;
  161. else {
  162. int list = *l1;
  163. int next;
  164. while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
  165. list = next;
  166. fixjump(fs, list, l2);
  167. }
  168. }
  169. static int luaK_code (FuncState *fs, Instruction i) {
  170. Proto *f = fs->f;
  171. dischargejpc(fs); /* `pc' will change */
  172. /* put new instruction in code array */
  173. luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
  174. MAX_INT, "opcodes");
  175. f->code[fs->pc] = i;
  176. /* save corresponding line information */
  177. luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
  178. MAX_INT, "opcodes");
  179. f->lineinfo[fs->pc] = fs->ls->lastline;
  180. return fs->pc++;
  181. }
  182. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  183. lua_assert(getOpMode(o) == iABC);
  184. lua_assert(getBMode(o) != OpArgN || b == 0);
  185. lua_assert(getCMode(o) != OpArgN || c == 0);
  186. lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
  187. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  188. }
  189. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  190. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  191. lua_assert(getCMode(o) == OpArgN);
  192. lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
  193. return luaK_code(fs, CREATE_ABx(o, a, bc));
  194. }
  195. static int codeextraarg (FuncState *fs, int a) {
  196. lua_assert(a <= MAXARG_Ax);
  197. return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
  198. }
  199. int luaK_codek (FuncState *fs, int reg, int k) {
  200. if (k <= MAXARG_Bx)
  201. return luaK_codeABx(fs, OP_LOADK, reg, k);
  202. else {
  203. int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
  204. codeextraarg(fs, k);
  205. return p;
  206. }
  207. }
  208. void luaK_checkstack (FuncState *fs, int n) {
  209. int newstack = fs->freereg + n;
  210. if (newstack > fs->f->maxstacksize) {
  211. if (newstack >= MAXSTACK)
  212. luaX_syntaxerror(fs->ls, "function or expression too complex");
  213. fs->f->maxstacksize = cast_byte(newstack);
  214. }
  215. }
  216. void luaK_reserveregs (FuncState *fs, int n) {
  217. luaK_checkstack(fs, n);
  218. fs->freereg += n;
  219. }
  220. static void freereg (FuncState *fs, int reg) {
  221. if (!ISK(reg) && reg >= fs->nactvar) {
  222. fs->freereg--;
  223. lua_assert(reg == fs->freereg);
  224. }
  225. }
  226. static void freeexp (FuncState *fs, expdesc *e) {
  227. if (e->k == VNONRELOC)
  228. freereg(fs, e->u.info);
  229. }
  230. static int addk (FuncState *fs, TValue *key, TValue *v) {
  231. lua_State *L = fs->ls->L;
  232. TValue *idx = luaH_set(L, fs->h, key);
  233. Proto *f = fs->f;
  234. int k, oldsize;
  235. if (ttisnumber(idx)) {
  236. lua_Number n = nvalue(idx);
  237. lua_number2int(k, n);
  238. if (luaV_rawequalobj(&f->k[k], v))
  239. return k;
  240. /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
  241. go through and create a new entry for this value */
  242. }
  243. /* constant not found; create a new entry */
  244. oldsize = f->sizek;
  245. k = fs->nk;
  246. /* numerical value does not need GC barrier;
  247. table has no metatable, so it does not need to invalidate cache */
  248. setnvalue(idx, cast_num(k));
  249. luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  250. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  251. setobj(L, &f->k[k], v);
  252. fs->nk++;
  253. luaC_barrier(L, f, v);
  254. return k;
  255. }
  256. int luaK_stringK (FuncState *fs, TString *s) {
  257. TValue o;
  258. setsvalue(fs->ls->L, &o, s);
  259. return addk(fs, &o, &o);
  260. }
  261. int luaK_numberK (FuncState *fs, lua_Number r) {
  262. int n;
  263. lua_State *L = fs->ls->L;
  264. TValue o;
  265. setnvalue(&o, r);
  266. if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
  267. /* use raw representation as key to avoid numeric problems */
  268. setsvalue(L, L->top, luaS_newlstr(L, (char *)&r, sizeof(r)));
  269. incr_top(L);
  270. n = addk(fs, L->top - 1, &o);
  271. L->top--;
  272. }
  273. else
  274. n = addk(fs, &o, &o); /* regular case */
  275. return n;
  276. }
  277. static int boolK (FuncState *fs, int b) {
  278. TValue o;
  279. setbvalue(&o, b);
  280. return addk(fs, &o, &o);
  281. }
  282. static int nilK (FuncState *fs) {
  283. TValue k, v;
  284. setnilvalue(&v);
  285. /* cannot use nil as key; instead use table itself to represent nil */
  286. sethvalue(fs->ls->L, &k, fs->h);
  287. return addk(fs, &k, &v);
  288. }
  289. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  290. if (e->k == VCALL) { /* expression is an open function call? */
  291. SETARG_C(getcode(fs, e), nresults+1);
  292. }
  293. else if (e->k == VVARARG) {
  294. SETARG_B(getcode(fs, e), nresults+1);
  295. SETARG_A(getcode(fs, e), fs->freereg);
  296. luaK_reserveregs(fs, 1);
  297. }
  298. }
  299. void luaK_setoneret (FuncState *fs, expdesc *e) {
  300. if (e->k == VCALL) { /* expression is an open function call? */
  301. e->k = VNONRELOC;
  302. e->u.info = GETARG_A(getcode(fs, e));
  303. }
  304. else if (e->k == VVARARG) {
  305. SETARG_B(getcode(fs, e), 2);
  306. e->k = VRELOCABLE; /* can relocate its simple result */
  307. }
  308. }
  309. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  310. switch (e->k) {
  311. case VLOCAL: {
  312. e->k = VNONRELOC;
  313. break;
  314. }
  315. case VUPVAL: {
  316. e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
  317. e->k = VRELOCABLE;
  318. break;
  319. }
  320. case VINDEXED: {
  321. OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
  322. freereg(fs, e->u.ind.idx);
  323. if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
  324. freereg(fs, e->u.ind.t);
  325. op = OP_GETTABLE;
  326. }
  327. e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
  328. e->k = VRELOCABLE;
  329. break;
  330. }
  331. case VVARARG:
  332. case VCALL: {
  333. luaK_setoneret(fs, e);
  334. break;
  335. }
  336. default: break; /* there is one value available (somewhere) */
  337. }
  338. }
  339. static int code_label (FuncState *fs, int A, int b, int jump) {
  340. luaK_getlabel(fs); /* those instructions may be jump targets */
  341. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  342. }
  343. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  344. luaK_dischargevars(fs, e);
  345. switch (e->k) {
  346. case VNIL: {
  347. luaK_nil(fs, reg, 1);
  348. break;
  349. }
  350. case VFALSE: case VTRUE: {
  351. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  352. break;
  353. }
  354. case VK: {
  355. luaK_codek(fs, reg, e->u.info);
  356. break;
  357. }
  358. case VKNUM: {
  359. luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
  360. break;
  361. }
  362. case VRELOCABLE: {
  363. Instruction *pc = &getcode(fs, e);
  364. SETARG_A(*pc, reg);
  365. break;
  366. }
  367. case VNONRELOC: {
  368. if (reg != e->u.info)
  369. luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
  370. break;
  371. }
  372. default: {
  373. lua_assert(e->k == VVOID || e->k == VJMP);
  374. return; /* nothing to do... */
  375. }
  376. }
  377. e->u.info = reg;
  378. e->k = VNONRELOC;
  379. }
  380. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  381. if (e->k != VNONRELOC) {
  382. luaK_reserveregs(fs, 1);
  383. discharge2reg(fs, e, fs->freereg-1);
  384. }
  385. }
  386. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  387. discharge2reg(fs, e, reg);
  388. if (e->k == VJMP)
  389. luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
  390. if (hasjumps(e)) {
  391. int final; /* position after whole expression */
  392. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  393. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  394. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  395. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  396. p_f = code_label(fs, reg, 0, 1);
  397. p_t = code_label(fs, reg, 1, 0);
  398. luaK_patchtohere(fs, fj);
  399. }
  400. final = luaK_getlabel(fs);
  401. patchlistaux(fs, e->f, final, reg, p_f);
  402. patchlistaux(fs, e->t, final, reg, p_t);
  403. }
  404. e->f = e->t = NO_JUMP;
  405. e->u.info = reg;
  406. e->k = VNONRELOC;
  407. }
  408. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  409. luaK_dischargevars(fs, e);
  410. freeexp(fs, e);
  411. luaK_reserveregs(fs, 1);
  412. exp2reg(fs, e, fs->freereg - 1);
  413. }
  414. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  415. luaK_dischargevars(fs, e);
  416. if (e->k == VNONRELOC) {
  417. if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
  418. if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
  419. exp2reg(fs, e, e->u.info); /* put value on it */
  420. return e->u.info;
  421. }
  422. }
  423. luaK_exp2nextreg(fs, e); /* default */
  424. return e->u.info;
  425. }
  426. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  427. if (e->k != VUPVAL || hasjumps(e))
  428. luaK_exp2anyreg(fs, e);
  429. }
  430. void luaK_exp2val (FuncState *fs, expdesc *e) {
  431. if (hasjumps(e))
  432. luaK_exp2anyreg(fs, e);
  433. else
  434. luaK_dischargevars(fs, e);
  435. }
  436. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  437. luaK_exp2val(fs, e);
  438. switch (e->k) {
  439. case VTRUE:
  440. case VFALSE:
  441. case VNIL: {
  442. if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
  443. e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
  444. e->k = VK;
  445. return RKASK(e->u.info);
  446. }
  447. else break;
  448. }
  449. case VKNUM: {
  450. e->u.info = luaK_numberK(fs, e->u.nval);
  451. e->k = VK;
  452. /* go through */
  453. }
  454. case VK: {
  455. if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
  456. return RKASK(e->u.info);
  457. else break;
  458. }
  459. default: break;
  460. }
  461. /* not a constant in the right range: put it in a register */
  462. return luaK_exp2anyreg(fs, e);
  463. }
  464. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  465. switch (var->k) {
  466. case VLOCAL: {
  467. freeexp(fs, ex);
  468. exp2reg(fs, ex, var->u.info);
  469. return;
  470. }
  471. case VUPVAL: {
  472. int e = luaK_exp2anyreg(fs, ex);
  473. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  474. break;
  475. }
  476. case VINDEXED: {
  477. OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
  478. int e = luaK_exp2RK(fs, ex);
  479. luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
  480. break;
  481. }
  482. default: {
  483. lua_assert(0); /* invalid var kind to store */
  484. break;
  485. }
  486. }
  487. freeexp(fs, ex);
  488. }
  489. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  490. int ereg;
  491. luaK_exp2anyreg(fs, e);
  492. ereg = e->u.info; /* register where 'e' was placed */
  493. freeexp(fs, e);
  494. e->u.info = fs->freereg; /* base register for op_self */
  495. e->k = VNONRELOC;
  496. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  497. luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
  498. freeexp(fs, key);
  499. }
  500. static void invertjump (FuncState *fs, expdesc *e) {
  501. Instruction *pc = getjumpcontrol(fs, e->u.info);
  502. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  503. GET_OPCODE(*pc) != OP_TEST);
  504. SETARG_A(*pc, !(GETARG_A(*pc)));
  505. }
  506. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  507. if (e->k == VRELOCABLE) {
  508. Instruction ie = getcode(fs, e);
  509. if (GET_OPCODE(ie) == OP_NOT) {
  510. fs->pc--; /* remove previous OP_NOT */
  511. return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
  512. }
  513. /* else go through */
  514. }
  515. discharge2anyreg(fs, e);
  516. freeexp(fs, e);
  517. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
  518. }
  519. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  520. int pc; /* pc of last jump */
  521. luaK_dischargevars(fs, e);
  522. switch (e->k) {
  523. case VJMP: {
  524. invertjump(fs, e);
  525. pc = e->u.info;
  526. break;
  527. }
  528. case VK: case VKNUM: case VTRUE: {
  529. pc = NO_JUMP; /* always true; do nothing */
  530. break;
  531. }
  532. default: {
  533. pc = jumponcond(fs, e, 0);
  534. break;
  535. }
  536. }
  537. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  538. luaK_patchtohere(fs, e->t);
  539. e->t = NO_JUMP;
  540. }
  541. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  542. int pc; /* pc of last jump */
  543. luaK_dischargevars(fs, e);
  544. switch (e->k) {
  545. case VJMP: {
  546. pc = e->u.info;
  547. break;
  548. }
  549. case VNIL: case VFALSE: {
  550. pc = NO_JUMP; /* always false; do nothing */
  551. break;
  552. }
  553. default: {
  554. pc = jumponcond(fs, e, 1);
  555. break;
  556. }
  557. }
  558. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  559. luaK_patchtohere(fs, e->f);
  560. e->f = NO_JUMP;
  561. }
  562. static void codenot (FuncState *fs, expdesc *e) {
  563. luaK_dischargevars(fs, e);
  564. switch (e->k) {
  565. case VNIL: case VFALSE: {
  566. e->k = VTRUE;
  567. break;
  568. }
  569. case VK: case VKNUM: case VTRUE: {
  570. e->k = VFALSE;
  571. break;
  572. }
  573. case VJMP: {
  574. invertjump(fs, e);
  575. break;
  576. }
  577. case VRELOCABLE:
  578. case VNONRELOC: {
  579. discharge2anyreg(fs, e);
  580. freeexp(fs, e);
  581. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  582. e->k = VRELOCABLE;
  583. break;
  584. }
  585. default: {
  586. lua_assert(0); /* cannot happen */
  587. break;
  588. }
  589. }
  590. /* interchange true and false lists */
  591. { int temp = e->f; e->f = e->t; e->t = temp; }
  592. removevalues(fs, e->f);
  593. removevalues(fs, e->t);
  594. }
  595. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  596. lua_assert(!hasjumps(t));
  597. t->u.ind.t = t->u.info;
  598. t->u.ind.idx = luaK_exp2RK(fs, k);
  599. t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
  600. : check_exp(vkisinreg(t->k), VLOCAL);
  601. t->k = VINDEXED;
  602. }
  603. static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
  604. lua_Number r;
  605. if (!isnumeral(e1) || !isnumeral(e2)) return 0;
  606. if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
  607. return 0; /* do not attempt to divide by 0 */
  608. r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
  609. e1->u.nval = r;
  610. return 1;
  611. }
  612. static void codearith (FuncState *fs, OpCode op,
  613. expdesc *e1, expdesc *e2, int line) {
  614. if (constfolding(op, e1, e2))
  615. return;
  616. else {
  617. int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
  618. int o1 = luaK_exp2RK(fs, e1);
  619. if (o1 > o2) {
  620. freeexp(fs, e1);
  621. freeexp(fs, e2);
  622. }
  623. else {
  624. freeexp(fs, e2);
  625. freeexp(fs, e1);
  626. }
  627. e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
  628. e1->k = VRELOCABLE;
  629. luaK_fixline(fs, line);
  630. }
  631. }
  632. static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
  633. expdesc *e2) {
  634. int o1 = luaK_exp2RK(fs, e1);
  635. int o2 = luaK_exp2RK(fs, e2);
  636. freeexp(fs, e2);
  637. freeexp(fs, e1);
  638. if (cond == 0 && op != OP_EQ) {
  639. int temp; /* exchange args to replace by `<' or `<=' */
  640. temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
  641. cond = 1;
  642. }
  643. e1->u.info = condjump(fs, op, cond, o1, o2);
  644. e1->k = VJMP;
  645. }
  646. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  647. expdesc e2;
  648. e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
  649. switch (op) {
  650. case OPR_MINUS: {
  651. if (isnumeral(e)) /* minus constant? */
  652. e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */
  653. else {
  654. luaK_exp2anyreg(fs, e);
  655. codearith(fs, OP_UNM, e, &e2, line);
  656. }
  657. break;
  658. }
  659. case OPR_NOT: codenot(fs, e); break;
  660. case OPR_LEN: {
  661. luaK_exp2anyreg(fs, e); /* cannot operate on constants */
  662. codearith(fs, OP_LEN, e, &e2, line);
  663. break;
  664. }
  665. default: lua_assert(0);
  666. }
  667. }
  668. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  669. switch (op) {
  670. case OPR_AND: {
  671. luaK_goiftrue(fs, v);
  672. break;
  673. }
  674. case OPR_OR: {
  675. luaK_goiffalse(fs, v);
  676. break;
  677. }
  678. case OPR_CONCAT: {
  679. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  680. break;
  681. }
  682. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  683. case OPR_MOD: case OPR_POW: {
  684. if (!isnumeral(v)) luaK_exp2RK(fs, v);
  685. break;
  686. }
  687. default: {
  688. luaK_exp2RK(fs, v);
  689. break;
  690. }
  691. }
  692. }
  693. void luaK_posfix (FuncState *fs, BinOpr op,
  694. expdesc *e1, expdesc *e2, int line) {
  695. switch (op) {
  696. case OPR_AND: {
  697. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  698. luaK_dischargevars(fs, e2);
  699. luaK_concat(fs, &e2->f, e1->f);
  700. *e1 = *e2;
  701. break;
  702. }
  703. case OPR_OR: {
  704. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  705. luaK_dischargevars(fs, e2);
  706. luaK_concat(fs, &e2->t, e1->t);
  707. *e1 = *e2;
  708. break;
  709. }
  710. case OPR_CONCAT: {
  711. luaK_exp2val(fs, e2);
  712. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  713. lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
  714. freeexp(fs, e1);
  715. SETARG_B(getcode(fs, e2), e1->u.info);
  716. e1->k = VRELOCABLE; e1->u.info = e2->u.info;
  717. }
  718. else {
  719. luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
  720. codearith(fs, OP_CONCAT, e1, e2, line);
  721. }
  722. break;
  723. }
  724. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  725. case OPR_MOD: case OPR_POW: {
  726. codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
  727. break;
  728. }
  729. case OPR_EQ: case OPR_LT: case OPR_LE: {
  730. codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
  731. break;
  732. }
  733. case OPR_NE: case OPR_GT: case OPR_GE: {
  734. codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
  735. break;
  736. }
  737. default: lua_assert(0);
  738. }
  739. }
  740. void luaK_fixline (FuncState *fs, int line) {
  741. fs->f->lineinfo[fs->pc - 1] = line;
  742. }
  743. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  744. int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
  745. int b = (tostore == LUA_MULTRET) ? 0 : tostore;
  746. lua_assert(tostore != 0);
  747. if (c <= MAXARG_C)
  748. luaK_codeABC(fs, OP_SETLIST, base, b, c);
  749. else if (c <= MAXARG_Ax) {
  750. luaK_codeABC(fs, OP_SETLIST, base, b, 0);
  751. codeextraarg(fs, c);
  752. }
  753. else
  754. luaX_syntaxerror(fs->ls, "constructor too long");
  755. fs->freereg = base + 1; /* free registers with list values */
  756. }