lcode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. ** $Id: lcode.c,v 2.3 2004/05/31 18:51:50 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_setreturns (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. }
  206. else if (e->k == VVARARG) {
  207. SETARG_B(getcode(fs, e), nresults+1);
  208. SETARG_A(getcode(fs, e), fs->freereg);
  209. luaK_reserveregs(fs, 1);
  210. }
  211. }
  212. void luaK_setoneret (FuncState *fs, expdesc *e) {
  213. if (e->k == VCALL) { /* expression is an open function call? */
  214. e->k = VNONRELOC;
  215. e->info = GETARG_A(getcode(fs, e));
  216. }
  217. else if (e->k == VVARARG) {
  218. SETARG_B(getcode(fs, e), 2);
  219. e->k = VRELOCABLE; /* can relocate its simple result */
  220. }
  221. }
  222. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  223. switch (e->k) {
  224. case VLOCAL: {
  225. e->k = VNONRELOC;
  226. break;
  227. }
  228. case VUPVAL: {
  229. e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
  230. e->k = VRELOCABLE;
  231. break;
  232. }
  233. case VGLOBAL: {
  234. e->info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->info);
  235. e->k = VRELOCABLE;
  236. break;
  237. }
  238. case VINDEXED: {
  239. freereg(fs, e->aux);
  240. freereg(fs, e->info);
  241. e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux);
  242. e->k = VRELOCABLE;
  243. break;
  244. }
  245. case VVARARG:
  246. case VCALL: {
  247. luaK_setoneret(fs, e);
  248. break;
  249. }
  250. default: break; /* there is one value available (somewhere) */
  251. }
  252. }
  253. static int code_label (FuncState *fs, int A, int b, int jump) {
  254. luaK_getlabel(fs); /* those instructions may be jump targets */
  255. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  256. }
  257. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  258. luaK_dischargevars(fs, e);
  259. switch (e->k) {
  260. case VNIL: {
  261. luaK_nil(fs, reg, 1);
  262. break;
  263. }
  264. case VFALSE: case VTRUE: {
  265. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  266. break;
  267. }
  268. case VK: {
  269. luaK_codeABx(fs, OP_LOADK, reg, e->info);
  270. break;
  271. }
  272. case VRELOCABLE: {
  273. Instruction *pc = &getcode(fs, e);
  274. SETARG_A(*pc, reg);
  275. break;
  276. }
  277. case VNONRELOC: {
  278. if (reg != e->info)
  279. luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
  280. break;
  281. }
  282. default: {
  283. lua_assert(e->k == VVOID || e->k == VJMP);
  284. return; /* nothing to do... */
  285. }
  286. }
  287. e->info = reg;
  288. e->k = VNONRELOC;
  289. }
  290. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  291. if (e->k != VNONRELOC) {
  292. luaK_reserveregs(fs, 1);
  293. discharge2reg(fs, e, fs->freereg-1);
  294. }
  295. }
  296. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  297. discharge2reg(fs, e, reg);
  298. if (e->k == VJMP)
  299. luaK_concat(fs, &e->t, e->info); /* put this jump in `t' list */
  300. if (hasjumps(e)) {
  301. int final; /* position after whole expression */
  302. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  303. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  304. if (need_value(fs, e->t, 1) || need_value(fs, e->f, 0)) {
  305. int fj = NO_JUMP; /* first jump (over LOAD ops.) */
  306. if (e->k != VJMP)
  307. fj = luaK_jump(fs);
  308. p_f = code_label(fs, reg, 0, 1);
  309. p_t = code_label(fs, reg, 1, 0);
  310. luaK_patchtohere(fs, fj);
  311. }
  312. final = luaK_getlabel(fs);
  313. luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
  314. luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
  315. }
  316. e->f = e->t = NO_JUMP;
  317. e->info = reg;
  318. e->k = VNONRELOC;
  319. }
  320. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  321. luaK_dischargevars(fs, e);
  322. freeexp(fs, e);
  323. luaK_reserveregs(fs, 1);
  324. luaK_exp2reg(fs, e, fs->freereg - 1);
  325. }
  326. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  327. luaK_dischargevars(fs, e);
  328. if (e->k == VNONRELOC) {
  329. if (!hasjumps(e)) return e->info; /* exp is already in a register */
  330. if (e->info >= fs->nactvar) { /* reg. is not a local? */
  331. luaK_exp2reg(fs, e, e->info); /* put value on it */
  332. return e->info;
  333. }
  334. }
  335. luaK_exp2nextreg(fs, e); /* default */
  336. return e->info;
  337. }
  338. void luaK_exp2val (FuncState *fs, expdesc *e) {
  339. if (hasjumps(e))
  340. luaK_exp2anyreg(fs, e);
  341. else
  342. luaK_dischargevars(fs, e);
  343. }
  344. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  345. luaK_exp2val(fs, e);
  346. switch (e->k) {
  347. case VNIL: {
  348. if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
  349. e->info = nil_constant(fs);
  350. e->k = VK;
  351. return RKASK(e->info);
  352. }
  353. else break;
  354. }
  355. case VK: {
  356. if (e->info <= MAXINDEXRK) /* constant fit in argC? */
  357. return RKASK(e->info);
  358. else break;
  359. }
  360. default: break;
  361. }
  362. /* not a constant in the right range: put it in a register */
  363. return luaK_exp2anyreg(fs, e);
  364. }
  365. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  366. switch (var->k) {
  367. case VLOCAL: {
  368. freeexp(fs, ex);
  369. luaK_exp2reg(fs, ex, var->info);
  370. return;
  371. }
  372. case VUPVAL: {
  373. int e = luaK_exp2anyreg(fs, ex);
  374. luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
  375. break;
  376. }
  377. case VGLOBAL: {
  378. int e = luaK_exp2anyreg(fs, ex);
  379. luaK_codeABx(fs, OP_SETGLOBAL, e, var->info);
  380. break;
  381. }
  382. case VINDEXED: {
  383. int e = luaK_exp2RK(fs, ex);
  384. luaK_codeABC(fs, OP_SETTABLE, var->info, var->aux, e);
  385. break;
  386. }
  387. default: {
  388. lua_assert(0); /* invalid var kind to store */
  389. break;
  390. }
  391. }
  392. freeexp(fs, ex);
  393. }
  394. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  395. int func;
  396. luaK_exp2anyreg(fs, e);
  397. freeexp(fs, e);
  398. func = fs->freereg;
  399. luaK_reserveregs(fs, 2);
  400. luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key));
  401. freeexp(fs, key);
  402. e->info = func;
  403. e->k = VNONRELOC;
  404. }
  405. static void invertjump (FuncState *fs, expdesc *e) {
  406. Instruction *pc = getjumpcontrol(fs, e->info);
  407. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TEST);
  408. SETARG_A(*pc, !(GETARG_A(*pc)));
  409. }
  410. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  411. if (e->k == VRELOCABLE) {
  412. Instruction ie = getcode(fs, e);
  413. if (GET_OPCODE(ie) == OP_NOT) {
  414. fs->pc--; /* remove previous OP_NOT */
  415. return luaK_condjump(fs, OP_TEST, NO_REG, GETARG_B(ie), !cond);
  416. }
  417. /* else go through */
  418. }
  419. discharge2anyreg(fs, e);
  420. freeexp(fs, e);
  421. return luaK_condjump(fs, OP_TEST, NO_REG, e->info, cond);
  422. }
  423. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  424. int pc; /* pc of last jump */
  425. luaK_dischargevars(fs, e);
  426. switch (e->k) {
  427. case VK: case VTRUE: {
  428. pc = NO_JUMP; /* always true; do nothing */
  429. break;
  430. }
  431. case VFALSE: {
  432. pc = luaK_jump(fs); /* always jump */
  433. break;
  434. }
  435. case VJMP: {
  436. invertjump(fs, e);
  437. pc = e->info;
  438. break;
  439. }
  440. default: {
  441. pc = jumponcond(fs, e, 0);
  442. break;
  443. }
  444. }
  445. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  446. }
  447. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  448. int pc; /* pc of last jump */
  449. luaK_dischargevars(fs, e);
  450. switch (e->k) {
  451. case VNIL: case VFALSE: {
  452. pc = NO_JUMP; /* always false; do nothing */
  453. break;
  454. }
  455. case VTRUE: {
  456. pc = luaK_jump(fs); /* always jump */
  457. break;
  458. }
  459. case VJMP: {
  460. pc = e->info;
  461. break;
  462. }
  463. default: {
  464. pc = jumponcond(fs, e, 1);
  465. break;
  466. }
  467. }
  468. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  469. }
  470. static void codenot (FuncState *fs, expdesc *e) {
  471. luaK_dischargevars(fs, e);
  472. switch (e->k) {
  473. case VNIL: case VFALSE: {
  474. e->k = VTRUE;
  475. break;
  476. }
  477. case VK: case VTRUE: {
  478. e->k = VFALSE;
  479. break;
  480. }
  481. case VJMP: {
  482. invertjump(fs, e);
  483. break;
  484. }
  485. case VRELOCABLE:
  486. case VNONRELOC: {
  487. discharge2anyreg(fs, e);
  488. freeexp(fs, e);
  489. e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
  490. e->k = VRELOCABLE;
  491. break;
  492. }
  493. default: {
  494. lua_assert(0); /* cannot happen */
  495. break;
  496. }
  497. }
  498. /* interchange true and false lists */
  499. { int temp = e->f; e->f = e->t; e->t = temp; }
  500. }
  501. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  502. t->aux = luaK_exp2RK(fs, k);
  503. t->k = VINDEXED;
  504. }
  505. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  506. if (op == OPR_MINUS) {
  507. luaK_exp2val(fs, e);
  508. if (e->k == VK && ttisnumber(&fs->f->k[e->info]))
  509. e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
  510. else {
  511. luaK_exp2anyreg(fs, e);
  512. freeexp(fs, e);
  513. e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
  514. e->k = VRELOCABLE;
  515. }
  516. }
  517. else /* op == NOT */
  518. codenot(fs, e);
  519. }
  520. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  521. switch (op) {
  522. case OPR_AND: {
  523. luaK_goiftrue(fs, v);
  524. luaK_patchtohere(fs, v->t);
  525. v->t = NO_JUMP;
  526. break;
  527. }
  528. case OPR_OR: {
  529. luaK_goiffalse(fs, v);
  530. luaK_patchtohere(fs, v->f);
  531. v->f = NO_JUMP;
  532. break;
  533. }
  534. case OPR_CONCAT: {
  535. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  536. break;
  537. }
  538. default: {
  539. luaK_exp2RK(fs, v);
  540. break;
  541. }
  542. }
  543. }
  544. static void codebinop (FuncState *fs, expdesc *res, BinOpr op,
  545. int o1, int o2) {
  546. if (op <= OPR_POW) { /* arithmetic operator? */
  547. OpCode opc = cast(OpCode, (op - OPR_ADD) + OP_ADD); /* ORDER OP */
  548. res->info = luaK_codeABC(fs, opc, 0, o1, o2);
  549. res->k = VRELOCABLE;
  550. }
  551. else { /* test operator */
  552. static const OpCode ops[] = {OP_EQ, OP_EQ, OP_LT, OP_LE, OP_LT, OP_LE};
  553. int cond = 1;
  554. if (op >= OPR_GT) { /* `>' or `>='? */
  555. int temp; /* exchange args and replace by `<' or `<=' */
  556. temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
  557. }
  558. else if (op == OPR_NE) cond = 0;
  559. res->info = luaK_condjump(fs, ops[op - OPR_NE], cond, o1, o2);
  560. res->k = VJMP;
  561. }
  562. }
  563. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  564. switch (op) {
  565. case OPR_AND: {
  566. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  567. luaK_dischargevars(fs, e2);
  568. luaK_concat(fs, &e1->f, e2->f);
  569. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
  570. break;
  571. }
  572. case OPR_OR: {
  573. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  574. luaK_dischargevars(fs, e2);
  575. luaK_concat(fs, &e1->t, e2->t);
  576. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
  577. break;
  578. }
  579. case OPR_CONCAT: {
  580. luaK_exp2val(fs, e2);
  581. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  582. lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
  583. freeexp(fs, e1);
  584. SETARG_B(getcode(fs, e2), e1->info);
  585. e1->k = e2->k; e1->info = e2->info;
  586. }
  587. else {
  588. luaK_exp2nextreg(fs, e2);
  589. freeexp(fs, e2);
  590. freeexp(fs, e1);
  591. e1->info = luaK_codeABC(fs, OP_CONCAT, 0, e1->info, e2->info);
  592. e1->k = VRELOCABLE;
  593. }
  594. break;
  595. }
  596. default: {
  597. int o1 = luaK_exp2RK(fs, e1);
  598. int o2 = luaK_exp2RK(fs, e2);
  599. freeexp(fs, e2);
  600. freeexp(fs, e1);
  601. codebinop(fs, e1, op, o1, o2);
  602. }
  603. }
  604. }
  605. void luaK_fixline (FuncState *fs, int line) {
  606. fs->f->lineinfo[fs->pc - 1] = line;
  607. }
  608. int luaK_code (FuncState *fs, Instruction i, int line) {
  609. Proto *f = fs->f;
  610. luaK_dischargejpc(fs); /* `pc' will change */
  611. /* put new instruction in code array */
  612. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  613. MAX_INT, "code size overflow");
  614. f->code[fs->pc] = i;
  615. /* save corresponding line information */
  616. luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
  617. MAX_INT, "code size overflow");
  618. f->lineinfo[fs->pc] = line;
  619. return fs->pc++;
  620. }
  621. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  622. lua_assert(getOpMode(o) == iABC);
  623. lua_assert(getBMode(o) != OpArgN || b == 0);
  624. lua_assert(getCMode(o) != OpArgN || c == 0);
  625. return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
  626. }
  627. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  628. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  629. lua_assert(getCMode(o) == OpArgN);
  630. return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
  631. }