lcode.c 19 KB

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