lcode.c 20 KB

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