lcode.c 18 KB

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