lcode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. ** $Id: lcode.c,v 1.71 2001/06/07 15:01:21 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. SETARG_C(getcode(fs, e), nresults); /* set number of results */
  213. if (nresults == 1) { /* `regular' expression? */
  214. e->k = VNONRELOC;
  215. e->u.i.info = GETARG_A(getcode(fs, e));
  216. }
  217. }
  218. }
  219. static void dischargevars (FuncState *fs, expdesc *e) {
  220. switch (e->k) {
  221. case VLOCAL: {
  222. e->k = VNONRELOC;
  223. break;
  224. }
  225. case VGLOBAL: {
  226. e->u.i.info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->u.i.info);
  227. e->k = VRELOCABLE;
  228. break;
  229. }
  230. case VINDEXED: {
  231. freereg(fs, e->u.i.aux);
  232. freereg(fs, e->u.i.info);
  233. e->u.i.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.i.info, e->u.i.aux);
  234. e->k = VRELOCABLE;
  235. break;
  236. }
  237. case VCALL: {
  238. luaK_setcallreturns(fs, e, 1);
  239. break;
  240. }
  241. default: break; /* there is one value available (somewhere) */
  242. }
  243. }
  244. static int code_label (FuncState *fs, OpCode op, int A, int sBc) {
  245. luaK_getlabel(fs); /* those instructions may be jump targets */
  246. return luaK_codeAsBc(fs, op, A, sBc);
  247. }
  248. static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
  249. if (hasjumps(e)) {
  250. int final; /* position after whole expression */
  251. int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
  252. int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
  253. if (need_value(fs, e->f, OP_TESTF) || need_value(fs, e->t, OP_TESTT)) {
  254. /* expression needs values */
  255. if (e->k != VJMP)
  256. code_label(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  257. p_nil = code_label(fs, OP_NILJMP, reg, 0);
  258. p_1 = code_label(fs, OP_LOADINT, reg, 1);
  259. }
  260. final = luaK_getlabel(fs);
  261. luaK_patchlistaux(fs, e->f, p_nil, NO_REG, final, reg, p_nil);
  262. luaK_patchlistaux(fs, e->t, final, reg, p_1, NO_REG, p_1);
  263. }
  264. e->f = e->t = NO_JUMP;
  265. }
  266. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  267. dischargevars(fs, e);
  268. switch (e->k) {
  269. case VNIL: {
  270. luaK_nil(fs, reg, 1);
  271. break;
  272. }
  273. case VNUMBER: {
  274. lua_Number f = e->u.n;
  275. int i = (int)f;
  276. if ((lua_Number)i == f && -MAXARG_sBc <= i && i <= MAXARG_sBc)
  277. luaK_codeAsBc(fs, OP_LOADINT, reg, i); /* f has a small int value */
  278. else
  279. luaK_codeABc(fs, OP_LOADK, reg, number_constant(fs, f));
  280. break;
  281. }
  282. case VK: {
  283. luaK_codeABc(fs, OP_LOADK, reg, e->u.i.info);
  284. break;
  285. }
  286. case VRELOCABLE: {
  287. Instruction *pc = &getcode(fs, e);
  288. SETARG_A(*pc, reg);
  289. break;
  290. }
  291. default: return;
  292. }
  293. e->u.i.info = reg;
  294. e->k = VNONRELOC;
  295. }
  296. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  297. if (e->k != VNONRELOC) {
  298. luaK_reserveregs(fs, 1);
  299. discharge2reg(fs, e, fs->freereg-1);
  300. }
  301. }
  302. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  303. discharge2reg(fs, e, reg);
  304. switch (e->k) {
  305. case VVOID: {
  306. return; /* nothing to do... */
  307. }
  308. case VNONRELOC: {
  309. if (reg != e->u.i.info)
  310. luaK_codeABC(fs, OP_MOVE, reg, e->u.i.info, 0);
  311. break;
  312. }
  313. case VJMP: {
  314. luaK_concat(fs, &e->t, e->u.i.info); /* put this jump in `t' list */
  315. break;
  316. }
  317. default: {
  318. lua_assert(0); /* cannot happen */
  319. break;
  320. }
  321. }
  322. dischargejumps(fs, e, reg);
  323. e->u.i.info = reg;
  324. e->k = VNONRELOC;
  325. }
  326. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  327. int reg;
  328. dischargevars(fs, e);
  329. freeexp(fs, e);
  330. reg = fs->freereg;
  331. luaK_reserveregs(fs, 1);
  332. luaK_exp2reg(fs, e, reg);
  333. }
  334. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  335. dischargevars(fs, e);
  336. if (e->k == VNONRELOC) {
  337. if (!hasjumps(e)) return e->u.i.info; /* exp is already in a register */
  338. if (e->u.i.info >= fs->nactloc) { /* reg. is not a local? */
  339. dischargejumps(fs, e, e->u.i.info); /* put value on it */
  340. return e->u.i.info;
  341. }
  342. }
  343. luaK_exp2nextreg(fs, e); /* default */
  344. return e->u.i.info;
  345. }
  346. void luaK_exp2val (FuncState *fs, expdesc *e) {
  347. if (hasjumps(e))
  348. luaK_exp2anyreg(fs, e);
  349. else
  350. dischargevars(fs, e);
  351. }
  352. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  353. luaK_exp2val(fs, e);
  354. if (e->k == VNUMBER && fs->nk + MAXSTACK <= MAXARG_C) {
  355. e->u.i.info = number_constant(fs, e->u.n);
  356. e->k = VK;
  357. }
  358. else if (!(e->k == VK && e->u.i.info + MAXSTACK <= MAXARG_C))
  359. luaK_exp2anyreg(fs, e); /* not a constant in the right range */
  360. return (e->k == VK) ? e->u.i.info+MAXSTACK : e->u.i.info;
  361. }
  362. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  363. switch (var->k) {
  364. case VLOCAL: {
  365. freeexp(fs, exp);
  366. luaK_exp2reg(fs, exp, var->u.i.info);
  367. break;
  368. }
  369. case VGLOBAL: {
  370. int e = luaK_exp2anyreg(fs, exp);
  371. freereg(fs, e);
  372. luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
  373. break;
  374. }
  375. case VINDEXED: {
  376. int e = luaK_exp2anyreg(fs, exp);
  377. freereg(fs, e);
  378. luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
  379. break;
  380. }
  381. default: {
  382. lua_assert(0); /* invalid var kind to store */
  383. break;
  384. }
  385. }
  386. }
  387. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  388. luaK_exp2anyreg(fs, e);
  389. freeexp(fs, e);
  390. luaK_reserveregs(fs, 2);
  391. luaK_codeABC(fs, OP_SELF, fs->freereg-2, e->u.i.info, luaK_exp2RK(fs, key));
  392. e->u.i.info = fs->freereg-2;
  393. e->k = VNONRELOC;
  394. }
  395. static OpCode invertoperator (OpCode op) {
  396. switch (op) {
  397. case OP_TESTNE: return OP_TESTEQ;
  398. case OP_TESTEQ: return OP_TESTNE;
  399. case OP_TESTLT: return OP_TESTGE;
  400. case OP_TESTLE: return OP_TESTGT;
  401. case OP_TESTGT: return OP_TESTLE;
  402. case OP_TESTGE: return OP_TESTLT;
  403. case OP_TESTT: return OP_TESTF;
  404. case OP_TESTF: return OP_TESTT;
  405. default: lua_assert(0); return op; /* invalid jump instruction */
  406. }
  407. }
  408. static void invertjump (FuncState *fs, expdesc *e) {
  409. Instruction *pc = getjumpcontrol(fs, e->u.i.info);
  410. *pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  411. }
  412. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  413. if (e->k == VRELOCABLE) {
  414. Instruction ie = getcode(fs, e);
  415. if (GET_OPCODE(ie) == OP_NOT) {
  416. op = invertoperator(op);
  417. fs->pc--; /* remove previous OP_NOT */
  418. return luaK_condjump(fs, op, GETARG_B(ie), 0);
  419. }
  420. /* else go through */
  421. }
  422. discharge2anyreg(fs, e);
  423. freeexp(fs, e);
  424. return luaK_condjump(fs, op, e->u.i.info, 0);
  425. }
  426. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  427. int pc; /* pc of last jump */
  428. dischargevars(fs, e);
  429. switch (e->k) {
  430. case VK: case VNUMBER: {
  431. pc = NO_JUMP; /* always true; do nothing */
  432. break;
  433. }
  434. case VNIL: {
  435. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  436. break;
  437. }
  438. case VJMP: {
  439. invertjump(fs, e);
  440. pc = e->u.i.info;
  441. break;
  442. }
  443. case VRELOCABLE:
  444. case VNONRELOC: {
  445. pc = jumponcond(fs, e, OP_TESTF);
  446. break;
  447. }
  448. default: {
  449. pc = 0; /* to avoid warnings */
  450. lua_assert(0); /* cannot happen */
  451. break;
  452. }
  453. }
  454. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  455. luaK_patchlist(fs, e->t, luaK_getlabel(fs));
  456. e->t = NO_JUMP;
  457. }
  458. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  459. int pc; /* pc of last jump */
  460. dischargevars(fs, e);
  461. switch (e->k) {
  462. case VNIL: {
  463. pc = NO_JUMP; /* always false; do nothing */
  464. break;
  465. }
  466. case VJMP: {
  467. pc = e->u.i.info;
  468. break;
  469. }
  470. case VK: case VNUMBER: /* cannot optimize it (`or' must keep value) */
  471. case VRELOCABLE:
  472. case VNONRELOC: {
  473. pc = jumponcond(fs, e, OP_TESTT);
  474. break;
  475. }
  476. default: {
  477. pc = 0; /* to avoid warnings */
  478. lua_assert(0); /* cannot happen */
  479. break;
  480. }
  481. }
  482. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  483. luaK_patchlist(fs, e->f, luaK_getlabel(fs));
  484. e->f = NO_JUMP;
  485. }
  486. static void codenot (FuncState *fs, expdesc *e) {
  487. dischargevars(fs, e);
  488. switch (e->k) {
  489. case VNIL: {
  490. e->u.n = 1;
  491. e->k = VNUMBER;
  492. break;
  493. }
  494. case VK: case VNUMBER: {
  495. e->k = VNIL;
  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->u.i.info = luaK_codeABC(fs, OP_NOT, 0, e->u.i.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. }
  518. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  519. t->u.i.aux = luaK_exp2RK(fs, k);
  520. t->k = VINDEXED;
  521. }
  522. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  523. if (op == OPR_MINUS) {
  524. luaK_exp2val(fs, e);
  525. if (e->k == VNUMBER)
  526. e->u.n = -e->u.n;
  527. else {
  528. luaK_exp2anyreg(fs, e);
  529. freeexp(fs, e);
  530. e->u.i.info = luaK_codeABC(fs, OP_UNM, 0, e->u.i.info, 0);
  531. e->k = VRELOCABLE;
  532. }
  533. }
  534. else /* op == NOT */
  535. codenot(fs, e);
  536. }
  537. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  538. switch (op) {
  539. case OPR_AND: {
  540. luaK_goiftrue(fs, v);
  541. break;
  542. }
  543. case OPR_OR: {
  544. luaK_goiffalse(fs, v);
  545. break;
  546. }
  547. case OPR_CONCAT: {
  548. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  549. break;
  550. }
  551. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  552. /* non-comutative operators */
  553. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  554. break;
  555. }
  556. default: {
  557. luaK_exp2RK(fs, v);
  558. break;
  559. }
  560. }
  561. }
  562. /* opcode for each binary operator */
  563. static const OpCode codes[] = { /* ORDER OPR */
  564. OP_ADD, OP_SUB, OP_MUL, OP_DIV,
  565. OP_POW, OP_CONCAT,
  566. OP_TESTNE, OP_TESTEQ,
  567. OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
  568. };
  569. /* `inverted' opcode for each binary operator */
  570. /* ( -1 means operator has no inverse) */
  571. static const OpCode invcodes[] = { /* ORDER OPR */
  572. OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
  573. (OpCode)-1, (OpCode)-1,
  574. OP_TESTNE, OP_TESTEQ,
  575. OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
  576. };
  577. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  578. switch (op) {
  579. case OPR_AND: {
  580. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  581. dischargevars(fs, e2);
  582. luaK_concat(fs, &e1->f, e2->f);
  583. e1->k = e2->k; e1->u = e2->u; e1->t = e2->t;
  584. break;
  585. }
  586. case OPR_OR: {
  587. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  588. dischargevars(fs, e2);
  589. luaK_concat(fs, &e1->t, e2->t);
  590. e1->k = e2->k; e1->u = e2->u; e1->f = e2->f;
  591. break;
  592. }
  593. case OPR_CONCAT: {
  594. luaK_exp2val(fs, e2);
  595. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  596. lua_assert(e1->u.i.info == GETARG_B(getcode(fs, e2))-1);
  597. freeexp(fs, e1);
  598. SETARG_B(getcode(fs, e2), e1->u.i.info);
  599. e1->k = e2->k; e1->u.i.info = e2->u.i.info;
  600. }
  601. else {
  602. luaK_exp2nextreg(fs, e2);
  603. freeexp(fs, e2);
  604. freeexp(fs, e1);
  605. e1->u.i.info = luaK_codeABC(fs, codes[op], 0, e1->u.i.info,
  606. e2->u.i.info);
  607. e1->k = VRELOCABLE;
  608. }
  609. break;
  610. }
  611. case OPR_EQ: case OPR_NE: {
  612. luaK_exp2val(fs, e2);
  613. if (e2->k == VNIL) { /* exp x= nil ? */
  614. if (e1->k == VK) { /* constant x= nil ? */
  615. if (op == OPR_EQ) /* constant == nil ? */
  616. e1->k = VNIL; /* always false */
  617. /* else always true (leave the constant itself) */
  618. }
  619. else {
  620. OpCode opc = (op == OPR_EQ) ? OP_TESTF : OP_TESTT;
  621. e1->u.i.info = jumponcond(fs, e1, opc);
  622. e1->k = VJMP;
  623. }
  624. break;
  625. }
  626. /* else go through */
  627. }
  628. default: {
  629. int o1, o2;
  630. OpCode opc;
  631. if (e1->k != VK) { /* not a constant operator? */
  632. o1 = e1->u.i.info;
  633. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  634. opc = codes[op];
  635. }
  636. else { /* invert operands */
  637. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  638. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  639. opc = invcodes[op]; /* use inverted operator */
  640. }
  641. freeexp(fs, e2);
  642. freeexp(fs, e1);
  643. if (op < OPR_NE) { /* ORDER OPR */
  644. e1->u.i.info = luaK_codeABC(fs, opc, 0, o1, o2);
  645. e1->k = VRELOCABLE;
  646. }
  647. else { /* jump */
  648. e1->u.i.info = luaK_condjump(fs, opc, o1, o2);
  649. e1->k = VJMP;
  650. }
  651. }
  652. }
  653. }
  654. static void codelineinfo (FuncState *fs) {
  655. Proto *f = fs->f;
  656. LexState *ls = fs->ls;
  657. if (ls->lastline > fs->lastline) {
  658. if (ls->lastline > fs->lastline+1) {
  659. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  660. MAX_INT, l_s("line info overflow"));
  661. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  662. }
  663. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  664. MAX_INT, l_s("line info overflow"));
  665. f->lineinfo[fs->nlineinfo++] = fs->pc;
  666. fs->lastline = ls->lastline;
  667. }
  668. }
  669. static int luaK_code (FuncState *fs, Instruction i) {
  670. Proto *f;
  671. codelineinfo(fs);
  672. f = fs->f;
  673. /* put new instruction in code array */
  674. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  675. MAX_INT, l_s("code size overflow"));
  676. f->code[fs->pc] = i;
  677. /*printf("free: %d ", fs->freereg); printopcode(f, fs->pc);*/
  678. return fs->pc++;
  679. }
  680. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  681. lua_assert(getOpMode(o) == iABC);
  682. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  683. }
  684. int luaK_codeABc (FuncState *fs, OpCode o, int a, int bc) {
  685. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  686. return luaK_code(fs, CREATE_ABc(o, a, bc));
  687. }