lcode.c 17 KB

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