lcode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. ** $Id: lcode.c,v 1.108 2002/06/13 13:39:55 roberto Exp $
  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. static 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 (ttisnumber(index)) {
  166. lua_assert(luaO_rawequalObj(&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. lua_assert(testOpMode(GET_OPCODE(*pc), OpModeT));
  388. SETARG_B(*pc, !(GETARG_B(*pc)));
  389. }
  390. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  391. if (e->k == VRELOCABLE) {
  392. Instruction ie = getcode(fs, e);
  393. if (GET_OPCODE(ie) == OP_NOT) {
  394. fs->pc--; /* remove previous OP_NOT */
  395. return luaK_condjump(fs, OP_TEST, NO_REG, !cond ,GETARG_B(ie));
  396. }
  397. /* else go through */
  398. }
  399. discharge2anyreg(fs, e);
  400. freeexp(fs, e);
  401. return luaK_condjump(fs, OP_TEST, NO_REG, cond, e->info);
  402. }
  403. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  404. int pc; /* pc of last jump */
  405. luaK_dischargevars(fs, e);
  406. switch (e->k) {
  407. case VK: case VTRUE: {
  408. pc = NO_JUMP; /* always true; do nothing */
  409. break;
  410. }
  411. case VFALSE: {
  412. pc = luaK_jump(fs); /* always jump */
  413. break;
  414. }
  415. case VJMP: {
  416. invertjump(fs, e);
  417. pc = e->info;
  418. break;
  419. }
  420. default: {
  421. pc = jumponcond(fs, e, 0);
  422. break;
  423. }
  424. }
  425. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  426. }
  427. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  428. int pc; /* pc of last jump */
  429. luaK_dischargevars(fs, e);
  430. switch (e->k) {
  431. case VNIL: case VFALSE: {
  432. pc = NO_JUMP; /* always false; do nothing */
  433. break;
  434. }
  435. case VTRUE: {
  436. pc = luaK_jump(fs); /* always jump */
  437. break;
  438. }
  439. case VJMP: {
  440. pc = e->info;
  441. break;
  442. }
  443. default: {
  444. pc = jumponcond(fs, e, 1);
  445. break;
  446. }
  447. }
  448. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  449. }
  450. static void codenot (FuncState *fs, expdesc *e) {
  451. luaK_dischargevars(fs, e);
  452. switch (e->k) {
  453. case VNIL: case VFALSE: {
  454. e->k = VTRUE;
  455. break;
  456. }
  457. case VK: case VTRUE: {
  458. e->k = VFALSE;
  459. break;
  460. }
  461. case VJMP: {
  462. invertjump(fs, e);
  463. break;
  464. }
  465. case VRELOCABLE:
  466. case VNONRELOC: {
  467. discharge2anyreg(fs, e);
  468. freeexp(fs, e);
  469. e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
  470. e->k = VRELOCABLE;
  471. break;
  472. }
  473. default: {
  474. lua_assert(0); /* cannot happen */
  475. break;
  476. }
  477. }
  478. /* interchange true and false lists */
  479. { int temp = e->f; e->f = e->t; e->t = temp; }
  480. }
  481. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  482. t->aux = luaK_exp2RK(fs, k);
  483. t->k = VINDEXED;
  484. }
  485. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  486. if (op == OPR_MINUS) {
  487. luaK_exp2val(fs, e);
  488. if (e->k == VK && ttisnumber(&fs->f->k[e->info]))
  489. e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
  490. else {
  491. luaK_exp2anyreg(fs, e);
  492. freeexp(fs, e);
  493. e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
  494. e->k = VRELOCABLE;
  495. }
  496. }
  497. else /* op == NOT */
  498. codenot(fs, e);
  499. }
  500. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  501. switch (op) {
  502. case OPR_AND: {
  503. luaK_goiftrue(fs, v);
  504. luaK_patchtohere(fs, v->t);
  505. v->t = NO_JUMP;
  506. break;
  507. }
  508. case OPR_OR: {
  509. luaK_goiffalse(fs, v);
  510. luaK_patchtohere(fs, v->f);
  511. v->f = NO_JUMP;
  512. break;
  513. }
  514. case OPR_CONCAT: {
  515. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  516. break;
  517. }
  518. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  519. /* non-comutative operators */
  520. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  521. break;
  522. }
  523. default: {
  524. luaK_exp2RK(fs, v);
  525. break;
  526. }
  527. }
  528. }
  529. static void codebinop (FuncState *fs, expdesc *res, BinOpr op,
  530. int o1, int o2, int ic) {
  531. switch (op) {
  532. case OPR_SUB:
  533. case OPR_DIV:
  534. case OPR_POW:
  535. lua_assert(!ic);
  536. /* go through */
  537. case OPR_ADD:
  538. case OPR_MULT: { /* ORDER OPR */
  539. OpCode opc = cast(OpCode, (op - OPR_ADD) + OP_ADD);
  540. res->info = luaK_codeABC(fs, opc, 0, o1, o2);
  541. res->k = VRELOCABLE;
  542. break;
  543. }
  544. case OPR_NE:
  545. case OPR_EQ: {
  546. res->info = luaK_condjump(fs, OP_EQ, o1, (op == OPR_EQ), o2);
  547. res->k = VJMP;
  548. break;
  549. }
  550. case OPR_LT:
  551. case OPR_LE:
  552. case OPR_GT:
  553. case OPR_GE: { /* ORDER OPR */
  554. OpCode opc;
  555. int i = op - OPR_LT;
  556. if (ic) /* operands were interchanged? */
  557. i = (i+2)&3; /* correct operator */
  558. opc = cast(OpCode, i + OP_LT);
  559. res->info = luaK_condjump(fs, opc, o1, 1, o2);
  560. res->k = VJMP;
  561. break;
  562. }
  563. default: lua_assert(0);
  564. }
  565. }
  566. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  567. switch (op) {
  568. case OPR_AND: {
  569. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  570. luaK_dischargevars(fs, e2);
  571. luaK_concat(fs, &e1->f, e2->f);
  572. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
  573. break;
  574. }
  575. case OPR_OR: {
  576. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  577. luaK_dischargevars(fs, e2);
  578. luaK_concat(fs, &e1->t, e2->t);
  579. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
  580. break;
  581. }
  582. case OPR_CONCAT: {
  583. luaK_exp2val(fs, e2);
  584. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  585. lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
  586. freeexp(fs, e1);
  587. SETARG_B(getcode(fs, e2), e1->info);
  588. e1->k = e2->k; e1->info = e2->info;
  589. }
  590. else {
  591. luaK_exp2nextreg(fs, e2);
  592. freeexp(fs, e2);
  593. freeexp(fs, e1);
  594. e1->info = luaK_codeABC(fs, OP_CONCAT, 0, e1->info, e2->info);
  595. e1->k = VRELOCABLE;
  596. }
  597. break;
  598. }
  599. default: {
  600. int o1, o2;
  601. int ic; /* interchange flag */
  602. if (e1->k != VK) { /* not a constant operator? */
  603. o1 = e1->info;
  604. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  605. ic = 0;
  606. }
  607. else { /* interchange operands */
  608. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  609. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  610. ic = 1;
  611. }
  612. freeexp(fs, e2);
  613. freeexp(fs, e1);
  614. codebinop(fs, e1, op, o1, o2, ic);
  615. }
  616. }
  617. }
  618. void luaK_fixline (FuncState *fs, int line) {
  619. fs->f->lineinfo[fs->pc - 1] = line;
  620. }
  621. int luaK_code (FuncState *fs, Instruction i, int line) {
  622. Proto *f = fs->f;
  623. int oldsize = f->sizecode;
  624. luaK_dischargejpc(fs); /* `pc' will change */
  625. /* put new instruction in code array */
  626. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  627. MAX_INT, "code size overflow");
  628. f->code[fs->pc] = i;
  629. if (f->sizecode != oldsize)
  630. luaM_reallocvector(fs->L, f->lineinfo, oldsize, f->sizecode, int);
  631. f->lineinfo[fs->pc] = line;
  632. return fs->pc++;
  633. }
  634. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  635. lua_assert(getOpMode(o) == iABC);
  636. return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
  637. }
  638. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  639. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  640. return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
  641. }