lcode.c 19 KB

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