/script_binding/lua/lua/lcode.c

http://ftk.googlecode.com/ · C · 839 lines · 699 code · 121 blank · 19 comment · 156 complexity · 4c793d782a899cef8efea6c01fe0387a MD5 · raw file

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