lcode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. ** $Id: lcode.c,v 1.120 2003/11/19 19:59:18 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, TObject *k, TObject *v) {
  166. TObject *idx = luaH_set(fs->L, fs->h, k);
  167. Proto *f = fs->f;
  168. int oldsize = f->sizek;
  169. if (ttisnumber(idx)) {
  170. lua_assert(luaO_rawequalObj(&fs->f->k[cast(int, nvalue(idx))], v));
  171. return cast(int, nvalue(idx));
  172. }
  173. else { /* constant not found; create a new entry */
  174. setnvalue(idx, cast(lua_Number, fs->nk));
  175. luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
  176. MAXARG_Bx, "constant table overflow");
  177. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  178. setobj(&f->k[fs->nk], v);
  179. luaC_barrier(fs->L, f, v);
  180. return fs->nk++;
  181. }
  182. }
  183. int luaK_stringK (FuncState *fs, TString *s) {
  184. TObject o;
  185. setsvalue(&o, s);
  186. return addk(fs, &o, &o);
  187. }
  188. int luaK_numberK (FuncState *fs, lua_Number r) {
  189. TObject o;
  190. setnvalue(&o, r);
  191. return addk(fs, &o, &o);
  192. }
  193. static int nil_constant (FuncState *fs) {
  194. TObject k, v;
  195. setnilvalue(&v);
  196. sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */
  197. return addk(fs, &k, &v);
  198. }
  199. void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
  200. if (e->k == VCALL) { /* expression is an open function call? */
  201. SETARG_C(getcode(fs, e), nresults+1);
  202. if (nresults == 1) { /* `regular' expression? */
  203. e->k = VNONRELOC;
  204. e->info = GETARG_A(getcode(fs, e));
  205. }
  206. }
  207. }
  208. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  209. switch (e->k) {
  210. case VLOCAL: {
  211. e->k = VNONRELOC;
  212. break;
  213. }
  214. case VUPVAL: {
  215. e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
  216. e->k = VRELOCABLE;
  217. break;
  218. }
  219. case VGLOBAL: {
  220. e->info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->info);
  221. e->k = VRELOCABLE;
  222. break;
  223. }
  224. case VINDEXED: {
  225. freereg(fs, e->aux);
  226. freereg(fs, e->info);
  227. e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux);
  228. e->k = VRELOCABLE;
  229. break;
  230. }
  231. case VCALL: {
  232. luaK_setcallreturns(fs, e, 1);
  233. break;
  234. }
  235. default: break; /* there is one value available (somewhere) */
  236. }
  237. }
  238. static int code_label (FuncState *fs, int A, int b, int jump) {
  239. luaK_getlabel(fs); /* those instructions may be jump targets */
  240. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  241. }
  242. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  243. luaK_dischargevars(fs, e);
  244. switch (e->k) {
  245. case VNIL: {
  246. luaK_nil(fs, reg, 1);
  247. break;
  248. }
  249. case VFALSE: case VTRUE: {
  250. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  251. break;
  252. }
  253. case VK: {
  254. luaK_codeABx(fs, OP_LOADK, reg, e->info);
  255. break;
  256. }
  257. case VRELOCABLE: {
  258. Instruction *pc = &getcode(fs, e);
  259. SETARG_A(*pc, reg);
  260. break;
  261. }
  262. case VNONRELOC: {
  263. if (reg != e->info)
  264. luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
  265. break;
  266. }
  267. default: {
  268. lua_assert(e->k == VVOID || e->k == VJMP);
  269. return; /* nothing to do... */
  270. }
  271. }
  272. e->info = reg;
  273. e->k = VNONRELOC;
  274. }
  275. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  276. if (e->k != VNONRELOC) {
  277. luaK_reserveregs(fs, 1);
  278. discharge2reg(fs, e, fs->freereg-1);
  279. }
  280. }
  281. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  282. discharge2reg(fs, e, reg);
  283. if (e->k == VJMP)
  284. luaK_concat(fs, &e->t, e->info); /* put this jump in `t' list */
  285. if (hasjumps(e)) {
  286. int final; /* position after whole expression */
  287. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  288. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  289. if (need_value(fs, e->t, 1) || need_value(fs, e->f, 0)) {
  290. int fj = NO_JUMP; /* first jump (over LOAD ops.) */
  291. if (e->k != VJMP)
  292. fj = luaK_jump(fs);
  293. p_f = code_label(fs, reg, 0, 1);
  294. p_t = code_label(fs, reg, 1, 0);
  295. luaK_patchtohere(fs, fj);
  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->nactvar) { /* 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 it in a register */
  348. return luaK_exp2anyreg(fs, e);
  349. }
  350. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  351. switch (var->k) {
  352. case VLOCAL: {
  353. freeexp(fs, ex);
  354. luaK_exp2reg(fs, ex, var->info);
  355. return;
  356. }
  357. case VUPVAL: {
  358. int e = luaK_exp2anyreg(fs, ex);
  359. luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
  360. break;
  361. }
  362. case VGLOBAL: {
  363. int e = luaK_exp2anyreg(fs, ex);
  364. luaK_codeABx(fs, OP_SETGLOBAL, e, var->info);
  365. break;
  366. }
  367. case VINDEXED: {
  368. int e = luaK_exp2RK(fs, ex);
  369. luaK_codeABC(fs, OP_SETTABLE, var->info, var->aux, e);
  370. break;
  371. }
  372. default: {
  373. lua_assert(0); /* invalid var kind to store */
  374. break;
  375. }
  376. }
  377. freeexp(fs, ex);
  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 void invertjump (FuncState *fs, expdesc *e) {
  391. Instruction *pc = getjumpcontrol(fs, e->info);
  392. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TEST);
  393. SETARG_A(*pc, !(GETARG_A(*pc)));
  394. }
  395. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  396. if (e->k == VRELOCABLE) {
  397. Instruction ie = getcode(fs, e);
  398. if (GET_OPCODE(ie) == OP_NOT) {
  399. fs->pc--; /* remove previous OP_NOT */
  400. return luaK_condjump(fs, OP_TEST, NO_REG, GETARG_B(ie), !cond);
  401. }
  402. /* else go through */
  403. }
  404. discharge2anyreg(fs, e);
  405. freeexp(fs, e);
  406. return luaK_condjump(fs, OP_TEST, NO_REG, e->info, cond);
  407. }
  408. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  409. int pc; /* pc of last jump */
  410. luaK_dischargevars(fs, e);
  411. switch (e->k) {
  412. case VK: case VTRUE: {
  413. pc = NO_JUMP; /* always true; do nothing */
  414. break;
  415. }
  416. case VFALSE: {
  417. pc = luaK_jump(fs); /* always jump */
  418. break;
  419. }
  420. case VJMP: {
  421. invertjump(fs, e);
  422. pc = e->info;
  423. break;
  424. }
  425. default: {
  426. pc = jumponcond(fs, e, 0);
  427. break;
  428. }
  429. }
  430. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  431. }
  432. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  433. int pc; /* pc of last jump */
  434. luaK_dischargevars(fs, e);
  435. switch (e->k) {
  436. case VNIL: case VFALSE: {
  437. pc = NO_JUMP; /* always false; do nothing */
  438. break;
  439. }
  440. case VTRUE: {
  441. pc = luaK_jump(fs); /* always jump */
  442. break;
  443. }
  444. case VJMP: {
  445. pc = e->info;
  446. break;
  447. }
  448. default: {
  449. pc = jumponcond(fs, e, 1);
  450. break;
  451. }
  452. }
  453. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  454. }
  455. static void codenot (FuncState *fs, expdesc *e) {
  456. luaK_dischargevars(fs, e);
  457. switch (e->k) {
  458. case VNIL: case VFALSE: {
  459. e->k = VTRUE;
  460. break;
  461. }
  462. case VK: case VTRUE: {
  463. e->k = VFALSE;
  464. break;
  465. }
  466. case VJMP: {
  467. invertjump(fs, e);
  468. break;
  469. }
  470. case VRELOCABLE:
  471. case VNONRELOC: {
  472. discharge2anyreg(fs, e);
  473. freeexp(fs, e);
  474. e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
  475. e->k = VRELOCABLE;
  476. break;
  477. }
  478. default: {
  479. lua_assert(0); /* cannot happen */
  480. break;
  481. }
  482. }
  483. /* interchange true and false lists */
  484. { int temp = e->f; e->f = e->t; e->t = temp; }
  485. }
  486. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  487. t->aux = luaK_exp2RK(fs, k);
  488. t->k = VINDEXED;
  489. }
  490. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  491. if (op == OPR_MINUS) {
  492. luaK_exp2val(fs, e);
  493. if (e->k == VK && ttisnumber(&fs->f->k[e->info]))
  494. e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
  495. else {
  496. luaK_exp2anyreg(fs, e);
  497. freeexp(fs, e);
  498. e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
  499. e->k = VRELOCABLE;
  500. }
  501. }
  502. else /* op == NOT */
  503. codenot(fs, e);
  504. }
  505. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  506. switch (op) {
  507. case OPR_AND: {
  508. luaK_goiftrue(fs, v);
  509. luaK_patchtohere(fs, v->t);
  510. v->t = NO_JUMP;
  511. break;
  512. }
  513. case OPR_OR: {
  514. luaK_goiffalse(fs, v);
  515. luaK_patchtohere(fs, v->f);
  516. v->f = NO_JUMP;
  517. break;
  518. }
  519. case OPR_CONCAT: {
  520. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  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) {
  531. if (op <= OPR_POW) { /* arithmetic operator? */
  532. OpCode opc = cast(OpCode, (op - OPR_ADD) + OP_ADD); /* ORDER OP */
  533. res->info = luaK_codeABC(fs, opc, 0, o1, o2);
  534. res->k = VRELOCABLE;
  535. }
  536. else { /* test operator */
  537. static const OpCode ops[] = {OP_EQ, OP_EQ, OP_LT, OP_LE, OP_LT, OP_LE};
  538. int cond = 1;
  539. if (op >= OPR_GT) { /* `>' or `>='? */
  540. int temp; /* exchange args and replace by `<' or `<=' */
  541. temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
  542. }
  543. else if (op == OPR_NE) cond = 0;
  544. res->info = luaK_condjump(fs, ops[op - OPR_NE], cond, o1, o2);
  545. res->k = VJMP;
  546. }
  547. }
  548. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  549. switch (op) {
  550. case OPR_AND: {
  551. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  552. luaK_dischargevars(fs, e2);
  553. luaK_concat(fs, &e1->f, e2->f);
  554. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
  555. break;
  556. }
  557. case OPR_OR: {
  558. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  559. luaK_dischargevars(fs, e2);
  560. luaK_concat(fs, &e1->t, e2->t);
  561. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
  562. break;
  563. }
  564. case OPR_CONCAT: {
  565. luaK_exp2val(fs, e2);
  566. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  567. lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
  568. freeexp(fs, e1);
  569. SETARG_B(getcode(fs, e2), e1->info);
  570. e1->k = e2->k; e1->info = e2->info;
  571. }
  572. else {
  573. luaK_exp2nextreg(fs, e2);
  574. freeexp(fs, e2);
  575. freeexp(fs, e1);
  576. e1->info = luaK_codeABC(fs, OP_CONCAT, 0, e1->info, e2->info);
  577. e1->k = VRELOCABLE;
  578. }
  579. break;
  580. }
  581. default: {
  582. int o1 = luaK_exp2RK(fs, e1);
  583. int o2 = luaK_exp2RK(fs, e2);
  584. freeexp(fs, e2);
  585. freeexp(fs, e1);
  586. codebinop(fs, e1, op, o1, o2);
  587. }
  588. }
  589. }
  590. void luaK_fixline (FuncState *fs, int line) {
  591. fs->f->lineinfo[fs->pc - 1] = line;
  592. }
  593. int luaK_code (FuncState *fs, Instruction i, int line) {
  594. Proto *f = fs->f;
  595. luaK_dischargejpc(fs); /* `pc' will change */
  596. /* put new instruction in code array */
  597. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  598. MAX_INT, "code size overflow");
  599. f->code[fs->pc] = i;
  600. /* save corresponding line information */
  601. luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
  602. MAX_INT, "code size overflow");
  603. f->lineinfo[fs->pc] = line;
  604. return fs->pc++;
  605. }
  606. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  607. lua_assert(getOpMode(o) == iABC);
  608. lua_assert(getBMode(o) != OpArgN || b == 0);
  609. lua_assert(getCMode(o) != OpArgN || c == 0);
  610. return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
  611. }
  612. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  613. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  614. lua_assert(getCMode(o) == OpArgN);
  615. return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
  616. }