2
0

lcode.c 17 KB

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