lcode.c 17 KB

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