lcode.c 18 KB

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