lcode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. ** $Id: lcode.c,v 1.95 2002/04/09 18:49:30 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. static 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 discharge2reg (FuncState *fs, expdesc *e, int reg) {
  239. luaK_dischargevars(fs, e);
  240. switch (e->k) {
  241. case VNIL: {
  242. luaK_nil(fs, reg, 1);
  243. break;
  244. }
  245. case VFALSE: case VTRUE: {
  246. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  247. break;
  248. }
  249. case VK: {
  250. luaK_codeABc(fs, OP_LOADK, reg, e->info);
  251. break;
  252. }
  253. case VRELOCABLE: {
  254. Instruction *pc = &getcode(fs, e);
  255. SETARG_A(*pc, reg);
  256. break;
  257. }
  258. case VNONRELOC: {
  259. if (reg != e->info)
  260. luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
  261. break;
  262. }
  263. default: {
  264. lua_assert(e->k == VVOID || e->k == VJMP);
  265. return; /* nothing to do... */
  266. }
  267. }
  268. e->info = reg;
  269. e->k = VNONRELOC;
  270. }
  271. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  272. if (e->k != VNONRELOC) {
  273. luaK_reserveregs(fs, 1);
  274. discharge2reg(fs, e, fs->freereg-1);
  275. }
  276. }
  277. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  278. discharge2reg(fs, e, reg);
  279. if (e->k == VJMP || hasjumps(e)) {
  280. int final; /* position after whole expression */
  281. int p_f = NO_JUMP; /* position of an eventual PUSH false */
  282. int p_t = NO_JUMP; /* position of an eventual PUSH true */
  283. if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) ||
  284. need_value(fs, e->t, OP_TESTT)) {
  285. /* expression needs values */
  286. if (e->k != VJMP) {
  287. luaK_getlabel(fs); /* these instruction may be jump target */
  288. luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  289. }
  290. else { /* last expression is a conditional (test + jump) */
  291. fs->pc--; /* remove its jump */
  292. lua_assert(testOpMode(GET_OPCODE(fs->f->code[fs->pc - 1]), OpModeT));
  293. }
  294. p_t = code_label(fs, reg, 1, 1);
  295. p_f = code_label(fs, reg, 0, 0);
  296. }
  297. final = luaK_getlabel(fs);
  298. luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
  299. luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
  300. }
  301. e->f = e->t = NO_JUMP;
  302. e->info = reg;
  303. e->k = VNONRELOC;
  304. }
  305. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  306. luaK_dischargevars(fs, e);
  307. freeexp(fs, e);
  308. luaK_reserveregs(fs, 1);
  309. luaK_exp2reg(fs, e, fs->freereg - 1);
  310. }
  311. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  312. luaK_dischargevars(fs, e);
  313. if (e->k == VNONRELOC) {
  314. if (!hasjumps(e)) return e->info; /* exp is already in a register */
  315. if (e->info >= fs->nactloc) { /* reg. is not a local? */
  316. luaK_exp2reg(fs, e, e->info); /* put value on it */
  317. return e->info;
  318. }
  319. }
  320. luaK_exp2nextreg(fs, e); /* default */
  321. return e->info;
  322. }
  323. void luaK_exp2val (FuncState *fs, expdesc *e) {
  324. if (hasjumps(e))
  325. luaK_exp2anyreg(fs, e);
  326. else
  327. luaK_dischargevars(fs, e);
  328. }
  329. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  330. luaK_exp2val(fs, e);
  331. switch (e->k) {
  332. case VNIL: {
  333. if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
  334. e->info = nil_constant(fs);
  335. e->k = VK;
  336. return e->info + MAXSTACK;
  337. }
  338. else break;
  339. }
  340. case VK: {
  341. if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
  342. return e->info + MAXSTACK;
  343. else break;
  344. }
  345. default: break;
  346. }
  347. /* not a constant in the right range: put in a register */
  348. return luaK_exp2anyreg(fs, e);
  349. }
  350. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  351. switch (var->k) {
  352. case VLOCAL: {
  353. freeexp(fs, exp);
  354. luaK_exp2reg(fs, exp, var->info);
  355. return;
  356. }
  357. case VUPVAL: {
  358. int e = luaK_exp2anyreg(fs, exp);
  359. luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
  360. break;
  361. }
  362. case VGLOBAL: {
  363. int e = luaK_exp2anyreg(fs, exp);
  364. luaK_codeABc(fs, OP_SETGLOBAL, e, var->info);
  365. break;
  366. }
  367. case VINDEXED: {
  368. int e = luaK_exp2anyreg(fs, exp);
  369. luaK_codeABC(fs, OP_SETTABLE, e, var->info, var->aux);
  370. break;
  371. }
  372. default: {
  373. lua_assert(0); /* invalid var kind to store */
  374. break;
  375. }
  376. }
  377. freeexp(fs, exp);
  378. }
  379. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  380. int func;
  381. luaK_exp2anyreg(fs, e);
  382. freeexp(fs, e);
  383. func = fs->freereg;
  384. luaK_reserveregs(fs, 2);
  385. luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key));
  386. freeexp(fs, key);
  387. e->info = func;
  388. e->k = VNONRELOC;
  389. }
  390. static OpCode invertoperator (OpCode op) {
  391. switch (op) {
  392. case OP_TESTNE: return OP_TESTEQ;
  393. case OP_TESTEQ: return OP_TESTNE;
  394. case OP_TESTLT: return OP_TESTGE;
  395. case OP_TESTLE: return OP_TESTGT;
  396. case OP_TESTGT: return OP_TESTLE;
  397. case OP_TESTGE: return OP_TESTLT;
  398. case OP_TESTT: return OP_TESTF;
  399. case OP_TESTF: return OP_TESTT;
  400. default: lua_assert(0); return op; /* invalid jump instruction */
  401. }
  402. }
  403. static void invertjump (FuncState *fs, expdesc *e) {
  404. Instruction *pc = getjumpcontrol(fs, e->info);
  405. SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  406. }
  407. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  408. if (e->k == VRELOCABLE) {
  409. Instruction ie = getcode(fs, e);
  410. if (GET_OPCODE(ie) == OP_NOT) {
  411. op = invertoperator(op);
  412. fs->pc--; /* remove previous OP_NOT */
  413. return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
  414. }
  415. /* else go through */
  416. }
  417. discharge2anyreg(fs, e);
  418. freeexp(fs, e);
  419. return luaK_condjump(fs, op, NO_REG, e->info, 0);
  420. }
  421. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  422. int pc; /* pc of last jump */
  423. luaK_dischargevars(fs, e);
  424. switch (e->k) {
  425. case VK: case VTRUE: {
  426. pc = NO_JUMP; /* always true; do nothing */
  427. break;
  428. }
  429. case VFALSE: {
  430. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  431. break;
  432. }
  433. case VJMP: {
  434. invertjump(fs, e);
  435. pc = e->info;
  436. break;
  437. }
  438. default: {
  439. pc = jumponcond(fs, e, OP_TESTF);
  440. break;
  441. }
  442. }
  443. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  444. luaK_patchtohere(fs, e->t);
  445. e->t = NO_JUMP;
  446. }
  447. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  448. int pc; /* pc of last jump */
  449. luaK_dischargevars(fs, e);
  450. switch (e->k) {
  451. case VNIL: case VFALSE: {
  452. pc = NO_JUMP; /* always false; do nothing */
  453. break;
  454. }
  455. case VTRUE: {
  456. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  457. break;
  458. }
  459. case VJMP: {
  460. pc = e->info;
  461. break;
  462. }
  463. default: {
  464. pc = jumponcond(fs, e, OP_TESTT);
  465. break;
  466. }
  467. }
  468. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  469. luaK_patchtohere(fs, e->f);
  470. e->f = NO_JUMP;
  471. }
  472. static void codenot (FuncState *fs, expdesc *e) {
  473. luaK_dischargevars(fs, e);
  474. switch (e->k) {
  475. case VNIL: case VFALSE: {
  476. e->k = VTRUE;
  477. break;
  478. }
  479. case VK: case VTRUE: {
  480. e->k = VFALSE;
  481. break;
  482. }
  483. case VJMP: {
  484. invertjump(fs, e);
  485. break;
  486. }
  487. case VRELOCABLE:
  488. case VNONRELOC: {
  489. discharge2anyreg(fs, e);
  490. freeexp(fs, e);
  491. e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
  492. e->k = VRELOCABLE;
  493. break;
  494. }
  495. default: {
  496. lua_assert(0); /* cannot happen */
  497. break;
  498. }
  499. }
  500. /* interchange true and false lists */
  501. { int temp = e->f; e->f = e->t; e->t = temp; }
  502. }
  503. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  504. t->aux = luaK_exp2RK(fs, k);
  505. t->k = VINDEXED;
  506. }
  507. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  508. if (op == OPR_MINUS) {
  509. luaK_exp2val(fs, e);
  510. if (e->k == VK && ttype(&fs->f->k[e->info]) == LUA_TNUMBER)
  511. e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
  512. else {
  513. luaK_exp2anyreg(fs, e);
  514. freeexp(fs, e);
  515. e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
  516. e->k = VRELOCABLE;
  517. }
  518. }
  519. else /* op == NOT */
  520. codenot(fs, e);
  521. }
  522. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  523. switch (op) {
  524. case OPR_AND: {
  525. luaK_goiftrue(fs, v);
  526. break;
  527. }
  528. case OPR_OR: {
  529. luaK_goiffalse(fs, v);
  530. break;
  531. }
  532. case OPR_CONCAT: {
  533. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  534. break;
  535. }
  536. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  537. /* non-comutative operators */
  538. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  539. break;
  540. }
  541. default: {
  542. luaK_exp2RK(fs, v);
  543. break;
  544. }
  545. }
  546. }
  547. /* opcode for each binary operator */
  548. static const OpCode codes[] = { /* ORDER OPR */
  549. OP_ADD, OP_SUB, OP_MUL, OP_DIV,
  550. OP_POW, OP_CONCAT,
  551. OP_TESTNE, OP_TESTEQ,
  552. OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
  553. };
  554. /* `inverted' opcode for each binary operator */
  555. /* ( -1 means operator has no inverse) */
  556. static const OpCode invcodes[] = { /* ORDER OPR */
  557. OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
  558. (OpCode)-1, (OpCode)-1,
  559. OP_TESTNE, OP_TESTEQ,
  560. OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
  561. };
  562. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  563. switch (op) {
  564. case OPR_AND: {
  565. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  566. luaK_dischargevars(fs, e2);
  567. luaK_concat(fs, &e1->f, e2->f);
  568. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
  569. break;
  570. }
  571. case OPR_OR: {
  572. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  573. luaK_dischargevars(fs, e2);
  574. luaK_concat(fs, &e1->t, e2->t);
  575. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
  576. break;
  577. }
  578. case OPR_CONCAT: {
  579. luaK_exp2val(fs, e2);
  580. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  581. lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
  582. freeexp(fs, e1);
  583. SETARG_B(getcode(fs, e2), e1->info);
  584. e1->k = e2->k; e1->info = e2->info;
  585. }
  586. else {
  587. luaK_exp2nextreg(fs, e2);
  588. freeexp(fs, e2);
  589. freeexp(fs, e1);
  590. e1->info = luaK_codeABC(fs, OP_CONCAT, 0, e1->info, e2->info);
  591. e1->k = VRELOCABLE;
  592. }
  593. break;
  594. }
  595. default: {
  596. int o1, o2;
  597. OpCode opc;
  598. if (e1->k != VK) { /* not a constant operator? */
  599. o1 = e1->info;
  600. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  601. opc = codes[op];
  602. }
  603. else { /* invert operands */
  604. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  605. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  606. opc = invcodes[op]; /* use inverted operator */
  607. }
  608. freeexp(fs, e2);
  609. freeexp(fs, e1);
  610. if (op < OPR_NE) { /* ORDER OPR */
  611. e1->info = luaK_codeABC(fs, opc, 0, o1, o2);
  612. e1->k = VRELOCABLE;
  613. }
  614. else { /* jump */
  615. e1->info = luaK_condjump(fs, opc, o1, 0, o2);
  616. e1->k = VJMP;
  617. }
  618. }
  619. }
  620. }
  621. static int luaK_code (FuncState *fs, Instruction i) {
  622. Proto *f = fs->f;
  623. int oldsize = f->sizecode;
  624. /* put new instruction in code array */
  625. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  626. MAX_INT, "code size overflow");
  627. f->code[fs->pc] = i;
  628. if (f->sizecode != oldsize)
  629. luaM_reallocvector(fs->L, f->lineinfo, oldsize, f->sizecode, int);
  630. f->lineinfo[fs->pc] = fs->ls->lastline;
  631. return fs->pc++;
  632. }
  633. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  634. lua_assert(getOpMode(o) == iABC);
  635. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  636. }
  637. int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
  638. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  639. return luaK_code(fs, CREATE_ABc(o, a, bc));
  640. }