lcode.c 19 KB

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