lcode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. ** $Id: lcode.c,v 1.72 2001/06/08 12:29:27 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define LUA_PRIVATE
  8. #include "lua.h"
  9. #include "lcode.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "llex.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lopcodes.h"
  16. #include "lparser.h"
  17. #define hasjumps(e) ((e)->t != (e)->f)
  18. #define getcode(fs,e) ((fs)->f->code[(e)->u.i.info])
  19. void luaK_error (LexState *ls, const l_char *msg) {
  20. luaX_error(ls, msg, ls->t.token);
  21. }
  22. /*
  23. ** Returns the the previous instruction, for optimizations.
  24. ** If there is a jump target between this and the current instruction,
  25. ** returns a dummy instruction to avoid wrong optimizations.
  26. */
  27. static Instruction previous_instruction (FuncState *fs) {
  28. if (fs->pc > fs->lasttarget) /* no jumps to current position? */
  29. return fs->f->code[fs->pc-1]; /* returns previous instruction */
  30. else
  31. return (Instruction)(-1);/* no optimizations after an invalid instruction */
  32. }
  33. void luaK_nil (FuncState *fs, int from, int n) {
  34. Instruction previous = previous_instruction(fs);
  35. if (GET_OPCODE(previous) == OP_LOADNIL) {
  36. int pfrom = GETARG_A(previous);
  37. int pto = GETARG_B(previous);
  38. if (pfrom <= from && from <= pto+1) { /* can connect both? */
  39. if (from+n-1 > pto)
  40. SETARG_B(fs->f->code[fs->pc-1], from+n-1);
  41. return;
  42. }
  43. }
  44. luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
  45. }
  46. int luaK_jump (FuncState *fs) {
  47. int j = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
  48. if (j == fs->lasttarget) { /* possible jumps to this jump? */
  49. luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
  50. fs->jlt = NO_JUMP;
  51. }
  52. return j;
  53. }
  54. static int luaK_condjump (FuncState *fs, OpCode op, int B, int C) {
  55. luaK_codeABC(fs, op, NO_REG, B, C);
  56. return luaK_codeAsBc(fs, OP_CJMP, 0, NO_JUMP);
  57. }
  58. static void luaK_fixjump (FuncState *fs, int pc, int dest) {
  59. Instruction *jmp = &fs->f->code[pc];
  60. if (dest == NO_JUMP)
  61. SETARG_sBc(*jmp, NO_JUMP); /* point to itself to represent end of list */
  62. else { /* jump is relative to position following jump instruction */
  63. int offset = dest-(pc+1);
  64. if (abs(offset) > MAXARG_sBc)
  65. luaK_error(fs->ls, l_s("control structure too long"));
  66. SETARG_sBc(*jmp, offset);
  67. }
  68. }
  69. /*
  70. ** prep-for instructions (OP_FORPREP & OP_TFORPREP) have a negated jump,
  71. ** as they simulate the real jump...
  72. */
  73. void luaK_fixfor (FuncState *fs, int pc, int dest) {
  74. Instruction *jmp = &fs->f->code[pc];
  75. int offset = dest-(pc+1);
  76. SETARG_sBc(*jmp, -offset);
  77. }
  78. /*
  79. ** returns current `pc' and marks it as a jump target (to avoid wrong
  80. ** optimizations with consecutive instructions not in the same basic block).
  81. ** discharge list of jumps to last target.
  82. */
  83. int luaK_getlabel (FuncState *fs) {
  84. if (fs->pc != fs->lasttarget) {
  85. int lasttarget = fs->lasttarget;
  86. fs->lasttarget = fs->pc;
  87. luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
  88. fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
  89. }
  90. return fs->pc;
  91. }
  92. static int luaK_getjump (FuncState *fs, int pc) {
  93. int offset = GETARG_sBc(fs->f->code[pc]);
  94. if (offset == NO_JUMP) /* point to itself represents end of list */
  95. return NO_JUMP; /* end of list */
  96. else
  97. return (pc+1)+offset; /* turn offset into absolute position */
  98. }
  99. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  100. Instruction *pi = &fs->f->code[pc];
  101. OpCode op = GET_OPCODE(*pi);
  102. if (op == OP_CJMP)
  103. return pi-1;
  104. else {
  105. lua_assert(op == OP_JMP || op == OP_FORLOOP || op == OP_TFORLOOP);
  106. return pi;
  107. }
  108. }
  109. static int need_value (FuncState *fs, int list, OpCode op) {
  110. /* check whether list has any jump different from `op' */
  111. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  112. if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1;
  113. return 0; /* not found */
  114. }
  115. static void patchtestreg (Instruction *i, int reg) {
  116. if (reg == NO_REG) reg = GETARG_B(*i);
  117. SETARG_A(*i, reg);
  118. }
  119. static void luaK_patchlistaux (FuncState *fs, int list,
  120. int ttarget, int treg, int ftarget, int freg, int dtarget) {
  121. while (list != NO_JUMP) {
  122. int next = luaK_getjump(fs, list);
  123. Instruction *i = getjumpcontrol(fs, list);
  124. switch (GET_OPCODE(*i)) {
  125. case OP_TESTT: {
  126. patchtestreg(i, treg);
  127. luaK_fixjump(fs, list, ttarget);
  128. break;
  129. }
  130. case OP_TESTF: {
  131. patchtestreg(i, freg);
  132. luaK_fixjump(fs, list, ftarget);
  133. break;
  134. }
  135. default: {
  136. luaK_fixjump(fs, list, dtarget); /* jump to default target */
  137. break;
  138. }
  139. }
  140. list = next;
  141. }
  142. }
  143. void luaK_patchlist (FuncState *fs, int list, int target) {
  144. if (target == fs->lasttarget) /* same target that list `jlt'? */
  145. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  146. else
  147. luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
  148. }
  149. void luaK_concat (FuncState *fs, int *l1, int l2) {
  150. if (*l1 == NO_JUMP)
  151. *l1 = l2;
  152. else {
  153. int list = *l1;
  154. int next;
  155. while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
  156. list = next;
  157. luaK_fixjump(fs, list, l2);
  158. }
  159. }
  160. void luaK_reserveregs (FuncState *fs, int n) {
  161. fs->freereg += n;
  162. if (fs->freereg > fs->f->maxstacksize) {
  163. if (fs->freereg >= MAXSTACK)
  164. luaK_error(fs->ls, l_s("function or expression too complex"));
  165. fs->f->maxstacksize = (short)fs->freereg;
  166. }
  167. }
  168. static void freereg (FuncState *fs, int reg) {
  169. if (reg >= fs->nactloc && reg < MAXSTACK) {
  170. fs->freereg--;
  171. lua_assert(reg == fs->freereg);
  172. }
  173. }
  174. static void freeexp (FuncState *fs, expdesc *e) {
  175. if (e->k == VNONRELOC)
  176. freereg(fs, e->u.i.info);
  177. }
  178. static int addk (FuncState *fs, TObject *k) {
  179. Proto *f = fs->f;
  180. luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
  181. MAXARG_Bc, l_s("constant table overflow"));
  182. setobj(&f->k[fs->nk], k);
  183. return fs->nk++;
  184. }
  185. int luaK_stringk (FuncState *fs, TString *s) {
  186. Proto *f = fs->f;
  187. int c = s->constindex;
  188. if (c >= fs->nk || ttype(&f->k[c]) != LUA_TSTRING || tsvalue(&f->k[c]) != s) {
  189. TObject o;
  190. setsvalue(&o, s);
  191. c = addk(fs, &o);
  192. s->constindex = (unsigned short)c; /* hint for next time */
  193. }
  194. return c;
  195. }
  196. static int number_constant (FuncState *fs, lua_Number r) {
  197. /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  198. TObject o;
  199. Proto *f = fs->f;
  200. int c = fs->nk;
  201. int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
  202. while (--c >= lim) {
  203. if (ttype(&f->k[c]) == LUA_TNUMBER && nvalue(&f->k[c]) == r)
  204. return c;
  205. }
  206. /* not found; create a new entry */
  207. setnvalue(&o, r);
  208. return addk(fs, &o);
  209. }
  210. void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
  211. if (e->k == VCALL) { /* expression is an open function call? */
  212. int a = GETARG_A(getcode(fs, e));
  213. int c = (nresults == LUA_MULTRET) ? NO_REG : a + nresults;
  214. SETARG_C(getcode(fs, e), c);
  215. if (nresults == 1) { /* `regular' expression? */
  216. e->k = VNONRELOC;
  217. e->u.i.info = a;
  218. }
  219. }
  220. }
  221. static void dischargevars (FuncState *fs, expdesc *e) {
  222. switch (e->k) {
  223. case VLOCAL: {
  224. e->k = VNONRELOC;
  225. break;
  226. }
  227. case VGLOBAL: {
  228. e->u.i.info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->u.i.info);
  229. e->k = VRELOCABLE;
  230. break;
  231. }
  232. case VINDEXED: {
  233. freereg(fs, e->u.i.aux);
  234. freereg(fs, e->u.i.info);
  235. e->u.i.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.i.info, e->u.i.aux);
  236. e->k = VRELOCABLE;
  237. break;
  238. }
  239. case VCALL: {
  240. luaK_setcallreturns(fs, e, 1);
  241. break;
  242. }
  243. default: break; /* there is one value available (somewhere) */
  244. }
  245. }
  246. static int code_label (FuncState *fs, OpCode op, int A, int sBc) {
  247. luaK_getlabel(fs); /* those instructions may be jump targets */
  248. return luaK_codeAsBc(fs, op, A, sBc);
  249. }
  250. static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
  251. if (hasjumps(e)) {
  252. int final; /* position after whole expression */
  253. int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
  254. int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
  255. if (need_value(fs, e->f, OP_TESTF) || need_value(fs, e->t, OP_TESTT)) {
  256. /* expression needs values */
  257. if (e->k != VJMP)
  258. code_label(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  259. p_nil = code_label(fs, OP_NILJMP, reg, 0);
  260. p_1 = code_label(fs, OP_LOADINT, reg, 1);
  261. }
  262. final = luaK_getlabel(fs);
  263. luaK_patchlistaux(fs, e->f, p_nil, NO_REG, final, reg, p_nil);
  264. luaK_patchlistaux(fs, e->t, final, reg, p_1, NO_REG, p_1);
  265. }
  266. e->f = e->t = NO_JUMP;
  267. }
  268. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  269. dischargevars(fs, e);
  270. switch (e->k) {
  271. case VNIL: {
  272. luaK_nil(fs, reg, 1);
  273. break;
  274. }
  275. case VNUMBER: {
  276. lua_Number f = e->u.n;
  277. int i = (int)f;
  278. if ((lua_Number)i == f && -MAXARG_sBc <= i && i <= MAXARG_sBc)
  279. luaK_codeAsBc(fs, OP_LOADINT, reg, i); /* f has a small int value */
  280. else
  281. luaK_codeABc(fs, OP_LOADK, reg, number_constant(fs, f));
  282. break;
  283. }
  284. case VK: {
  285. luaK_codeABc(fs, OP_LOADK, reg, e->u.i.info);
  286. break;
  287. }
  288. case VRELOCABLE: {
  289. Instruction *pc = &getcode(fs, e);
  290. SETARG_A(*pc, reg);
  291. break;
  292. }
  293. default: return;
  294. }
  295. e->u.i.info = reg;
  296. e->k = VNONRELOC;
  297. }
  298. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  299. if (e->k != VNONRELOC) {
  300. luaK_reserveregs(fs, 1);
  301. discharge2reg(fs, e, fs->freereg-1);
  302. }
  303. }
  304. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  305. discharge2reg(fs, e, reg);
  306. switch (e->k) {
  307. case VVOID: {
  308. return; /* nothing to do... */
  309. }
  310. case VNONRELOC: {
  311. if (reg != e->u.i.info)
  312. luaK_codeABC(fs, OP_MOVE, reg, e->u.i.info, 0);
  313. break;
  314. }
  315. case VJMP: {
  316. luaK_concat(fs, &e->t, e->u.i.info); /* put this jump in `t' list */
  317. break;
  318. }
  319. default: {
  320. lua_assert(0); /* cannot happen */
  321. break;
  322. }
  323. }
  324. dischargejumps(fs, e, reg);
  325. e->u.i.info = reg;
  326. e->k = VNONRELOC;
  327. }
  328. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  329. int reg;
  330. dischargevars(fs, e);
  331. freeexp(fs, e);
  332. reg = fs->freereg;
  333. luaK_reserveregs(fs, 1);
  334. luaK_exp2reg(fs, e, reg);
  335. }
  336. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  337. dischargevars(fs, e);
  338. if (e->k == VNONRELOC) {
  339. if (!hasjumps(e)) return e->u.i.info; /* exp is already in a register */
  340. if (e->u.i.info >= fs->nactloc) { /* reg. is not a local? */
  341. dischargejumps(fs, e, e->u.i.info); /* put value on it */
  342. return e->u.i.info;
  343. }
  344. }
  345. luaK_exp2nextreg(fs, e); /* default */
  346. return e->u.i.info;
  347. }
  348. void luaK_exp2val (FuncState *fs, expdesc *e) {
  349. if (hasjumps(e))
  350. luaK_exp2anyreg(fs, e);
  351. else
  352. dischargevars(fs, e);
  353. }
  354. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  355. luaK_exp2val(fs, e);
  356. if (e->k == VNUMBER && fs->nk + MAXSTACK <= MAXARG_C) {
  357. e->u.i.info = number_constant(fs, e->u.n);
  358. e->k = VK;
  359. }
  360. else if (!(e->k == VK && e->u.i.info + MAXSTACK <= MAXARG_C))
  361. luaK_exp2anyreg(fs, e); /* not a constant in the right range */
  362. return (e->k == VK) ? e->u.i.info+MAXSTACK : e->u.i.info;
  363. }
  364. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  365. switch (var->k) {
  366. case VLOCAL: {
  367. freeexp(fs, exp);
  368. luaK_exp2reg(fs, exp, var->u.i.info);
  369. break;
  370. }
  371. case VGLOBAL: {
  372. int e = luaK_exp2anyreg(fs, exp);
  373. freereg(fs, e);
  374. luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
  375. break;
  376. }
  377. case VINDEXED: {
  378. int e = luaK_exp2anyreg(fs, exp);
  379. freereg(fs, e);
  380. luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
  381. break;
  382. }
  383. default: {
  384. lua_assert(0); /* invalid var kind to store */
  385. break;
  386. }
  387. }
  388. }
  389. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  390. luaK_exp2anyreg(fs, e);
  391. freeexp(fs, e);
  392. luaK_reserveregs(fs, 2);
  393. luaK_codeABC(fs, OP_SELF, fs->freereg-2, e->u.i.info, luaK_exp2RK(fs, key));
  394. e->u.i.info = fs->freereg-2;
  395. e->k = VNONRELOC;
  396. }
  397. static OpCode invertoperator (OpCode op) {
  398. switch (op) {
  399. case OP_TESTNE: return OP_TESTEQ;
  400. case OP_TESTEQ: return OP_TESTNE;
  401. case OP_TESTLT: return OP_TESTGE;
  402. case OP_TESTLE: return OP_TESTGT;
  403. case OP_TESTGT: return OP_TESTLE;
  404. case OP_TESTGE: return OP_TESTLT;
  405. case OP_TESTT: return OP_TESTF;
  406. case OP_TESTF: return OP_TESTT;
  407. default: lua_assert(0); return op; /* invalid jump instruction */
  408. }
  409. }
  410. static void invertjump (FuncState *fs, expdesc *e) {
  411. Instruction *pc = getjumpcontrol(fs, e->u.i.info);
  412. *pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  413. }
  414. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  415. if (e->k == VRELOCABLE) {
  416. Instruction ie = getcode(fs, e);
  417. if (GET_OPCODE(ie) == OP_NOT) {
  418. op = invertoperator(op);
  419. fs->pc--; /* remove previous OP_NOT */
  420. return luaK_condjump(fs, op, GETARG_B(ie), 0);
  421. }
  422. /* else go through */
  423. }
  424. discharge2anyreg(fs, e);
  425. freeexp(fs, e);
  426. return luaK_condjump(fs, op, e->u.i.info, 0);
  427. }
  428. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  429. int pc; /* pc of last jump */
  430. dischargevars(fs, e);
  431. switch (e->k) {
  432. case VK: case VNUMBER: {
  433. pc = NO_JUMP; /* always true; do nothing */
  434. break;
  435. }
  436. case VNIL: {
  437. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  438. break;
  439. }
  440. case VJMP: {
  441. invertjump(fs, e);
  442. pc = e->u.i.info;
  443. break;
  444. }
  445. case VRELOCABLE:
  446. case VNONRELOC: {
  447. pc = jumponcond(fs, e, OP_TESTF);
  448. break;
  449. }
  450. default: {
  451. pc = 0; /* to avoid warnings */
  452. lua_assert(0); /* cannot happen */
  453. break;
  454. }
  455. }
  456. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  457. luaK_patchlist(fs, e->t, luaK_getlabel(fs));
  458. e->t = NO_JUMP;
  459. }
  460. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  461. int pc; /* pc of last jump */
  462. dischargevars(fs, e);
  463. switch (e->k) {
  464. case VNIL: {
  465. pc = NO_JUMP; /* always false; do nothing */
  466. break;
  467. }
  468. case VJMP: {
  469. pc = e->u.i.info;
  470. break;
  471. }
  472. case VK: case VNUMBER: /* cannot optimize it (`or' must keep value) */
  473. case VRELOCABLE:
  474. case VNONRELOC: {
  475. pc = jumponcond(fs, e, OP_TESTT);
  476. break;
  477. }
  478. default: {
  479. pc = 0; /* to avoid warnings */
  480. lua_assert(0); /* cannot happen */
  481. break;
  482. }
  483. }
  484. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  485. luaK_patchlist(fs, e->f, luaK_getlabel(fs));
  486. e->f = NO_JUMP;
  487. }
  488. static void codenot (FuncState *fs, expdesc *e) {
  489. dischargevars(fs, e);
  490. switch (e->k) {
  491. case VNIL: {
  492. e->u.n = 1;
  493. e->k = VNUMBER;
  494. break;
  495. }
  496. case VK: case VNUMBER: {
  497. e->k = VNIL;
  498. break;
  499. }
  500. case VJMP: {
  501. invertjump(fs, e);
  502. break;
  503. }
  504. case VRELOCABLE:
  505. case VNONRELOC: {
  506. discharge2anyreg(fs, e);
  507. freeexp(fs, e);
  508. e->u.i.info = luaK_codeABC(fs, OP_NOT, 0, e->u.i.info, 0);
  509. e->k = VRELOCABLE;
  510. break;
  511. }
  512. default: {
  513. lua_assert(0); /* cannot happen */
  514. break;
  515. }
  516. }
  517. /* interchange true and false lists */
  518. { int temp = e->f; e->f = e->t; e->t = temp; }
  519. }
  520. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  521. t->u.i.aux = luaK_exp2RK(fs, k);
  522. t->k = VINDEXED;
  523. }
  524. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  525. if (op == OPR_MINUS) {
  526. luaK_exp2val(fs, e);
  527. if (e->k == VNUMBER)
  528. e->u.n = -e->u.n;
  529. else {
  530. luaK_exp2anyreg(fs, e);
  531. freeexp(fs, e);
  532. e->u.i.info = luaK_codeABC(fs, OP_UNM, 0, e->u.i.info, 0);
  533. e->k = VRELOCABLE;
  534. }
  535. }
  536. else /* op == NOT */
  537. codenot(fs, e);
  538. }
  539. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  540. switch (op) {
  541. case OPR_AND: {
  542. luaK_goiftrue(fs, v);
  543. break;
  544. }
  545. case OPR_OR: {
  546. luaK_goiffalse(fs, v);
  547. break;
  548. }
  549. case OPR_CONCAT: {
  550. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  551. break;
  552. }
  553. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  554. /* non-comutative operators */
  555. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  556. break;
  557. }
  558. default: {
  559. luaK_exp2RK(fs, v);
  560. break;
  561. }
  562. }
  563. }
  564. /* opcode for each binary operator */
  565. static const OpCode codes[] = { /* ORDER OPR */
  566. OP_ADD, OP_SUB, OP_MUL, OP_DIV,
  567. OP_POW, OP_CONCAT,
  568. OP_TESTNE, OP_TESTEQ,
  569. OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
  570. };
  571. /* `inverted' opcode for each binary operator */
  572. /* ( -1 means operator has no inverse) */
  573. static const OpCode invcodes[] = { /* ORDER OPR */
  574. OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
  575. (OpCode)-1, (OpCode)-1,
  576. OP_TESTNE, OP_TESTEQ,
  577. OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
  578. };
  579. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  580. switch (op) {
  581. case OPR_AND: {
  582. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  583. dischargevars(fs, e2);
  584. luaK_concat(fs, &e1->f, e2->f);
  585. e1->k = e2->k; e1->u = e2->u; e1->t = e2->t;
  586. break;
  587. }
  588. case OPR_OR: {
  589. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  590. dischargevars(fs, e2);
  591. luaK_concat(fs, &e1->t, e2->t);
  592. e1->k = e2->k; e1->u = e2->u; e1->f = e2->f;
  593. break;
  594. }
  595. case OPR_CONCAT: {
  596. luaK_exp2val(fs, e2);
  597. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  598. lua_assert(e1->u.i.info == GETARG_B(getcode(fs, e2))-1);
  599. freeexp(fs, e1);
  600. SETARG_B(getcode(fs, e2), e1->u.i.info);
  601. e1->k = e2->k; e1->u.i.info = e2->u.i.info;
  602. }
  603. else {
  604. luaK_exp2nextreg(fs, e2);
  605. freeexp(fs, e2);
  606. freeexp(fs, e1);
  607. e1->u.i.info = luaK_codeABC(fs, codes[op], 0, e1->u.i.info,
  608. e2->u.i.info);
  609. e1->k = VRELOCABLE;
  610. }
  611. break;
  612. }
  613. case OPR_EQ: case OPR_NE: {
  614. luaK_exp2val(fs, e2);
  615. if (e2->k == VNIL) { /* exp x= nil ? */
  616. if (e1->k == VK) { /* constant x= nil ? */
  617. if (op == OPR_EQ) /* constant == nil ? */
  618. e1->k = VNIL; /* always false */
  619. /* else always true (leave the constant itself) */
  620. }
  621. else {
  622. OpCode opc = (op == OPR_EQ) ? OP_TESTF : OP_TESTT;
  623. e1->u.i.info = jumponcond(fs, e1, opc);
  624. e1->k = VJMP;
  625. }
  626. break;
  627. }
  628. /* else go through */
  629. }
  630. default: {
  631. int o1, o2;
  632. OpCode opc;
  633. if (e1->k != VK) { /* not a constant operator? */
  634. o1 = e1->u.i.info;
  635. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  636. opc = codes[op];
  637. }
  638. else { /* invert operands */
  639. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  640. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  641. opc = invcodes[op]; /* use inverted operator */
  642. }
  643. freeexp(fs, e2);
  644. freeexp(fs, e1);
  645. if (op < OPR_NE) { /* ORDER OPR */
  646. e1->u.i.info = luaK_codeABC(fs, opc, 0, o1, o2);
  647. e1->k = VRELOCABLE;
  648. }
  649. else { /* jump */
  650. e1->u.i.info = luaK_condjump(fs, opc, o1, o2);
  651. e1->k = VJMP;
  652. }
  653. }
  654. }
  655. }
  656. static void codelineinfo (FuncState *fs) {
  657. Proto *f = fs->f;
  658. LexState *ls = fs->ls;
  659. if (ls->lastline > fs->lastline) {
  660. if (ls->lastline > fs->lastline+1) {
  661. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  662. MAX_INT, l_s("line info overflow"));
  663. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  664. }
  665. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  666. MAX_INT, l_s("line info overflow"));
  667. f->lineinfo[fs->nlineinfo++] = fs->pc;
  668. fs->lastline = ls->lastline;
  669. }
  670. }
  671. static int luaK_code (FuncState *fs, Instruction i) {
  672. Proto *f;
  673. codelineinfo(fs);
  674. f = fs->f;
  675. /* put new instruction in code array */
  676. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  677. MAX_INT, l_s("code size overflow"));
  678. f->code[fs->pc] = i;
  679. /*printf("free: %d ", fs->freereg); printopcode(f, fs->pc);*/
  680. return fs->pc++;
  681. }
  682. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  683. lua_assert(getOpMode(o) == iABC);
  684. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  685. }
  686. int luaK_codeABc (FuncState *fs, OpCode o, int a, int bc) {
  687. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  688. return luaK_code(fs, CREATE_ABc(o, a, bc));
  689. }