lcode.c 19 KB

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