lcode.c 18 KB

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