lcode.c 19 KB

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