lcode.c 19 KB

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