lcode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. ** $Id: lcode.c,v 1.79 2001/08/27 15:16:28 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. #include "ltable.h"
  18. #define hasjumps(e) ((e)->t != (e)->f)
  19. #define getcode(fs,e) ((fs)->f->code[(e)->u.i.info])
  20. void luaK_error (LexState *ls, const l_char *msg) {
  21. luaX_error(ls, msg, ls->t.token);
  22. }
  23. /*
  24. ** Returns the the previous instruction, for optimizations.
  25. ** If there is a jump target between this and the current instruction,
  26. ** returns a dummy instruction to avoid wrong optimizations.
  27. */
  28. static Instruction previous_instruction (FuncState *fs) {
  29. if (fs->pc > fs->lasttarget) /* no jumps to current position? */
  30. return fs->f->code[fs->pc-1]; /* returns previous instruction */
  31. else
  32. return cast(Instruction, -1);/* invalid instruction avoids optimizations */
  33. }
  34. void luaK_nil (FuncState *fs, int from, int n) {
  35. Instruction previous = previous_instruction(fs);
  36. if (GET_OPCODE(previous) == OP_LOADNIL) {
  37. int pfrom = GETARG_A(previous);
  38. int pto = GETARG_B(previous);
  39. if (pfrom <= from && from <= pto+1) { /* can connect both? */
  40. if (from+n-1 > pto)
  41. SETARG_B(fs->f->code[fs->pc-1], from+n-1);
  42. return;
  43. }
  44. }
  45. luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
  46. }
  47. int luaK_jump (FuncState *fs) {
  48. int j = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
  49. if (j == fs->lasttarget) { /* possible jumps to this jump? */
  50. luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
  51. fs->jlt = NO_JUMP;
  52. }
  53. return j;
  54. }
  55. static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) {
  56. luaK_codeABC(fs, op, A, B, C);
  57. return luaK_codeAsBc(fs, OP_CJMP, 0, NO_JUMP);
  58. }
  59. static void luaK_fixjump (FuncState *fs, int pc, int dest) {
  60. Instruction *jmp = &fs->f->code[pc];
  61. if (dest == NO_JUMP)
  62. SETARG_sBc(*jmp, NO_JUMP); /* point to itself to represent end of list */
  63. else { /* jump is relative to position following jump instruction */
  64. int offset = dest-(pc+1);
  65. if (abs(offset) > MAXARG_sBc)
  66. luaK_error(fs->ls, l_s("control structure too long"));
  67. SETARG_sBc(*jmp, offset);
  68. }
  69. }
  70. /*
  71. ** prep-for instructions (OP_FORPREP & OP_TFORPREP) have a negated jump,
  72. ** as they simulate the real jump...
  73. */
  74. void luaK_fixfor (FuncState *fs, int pc, int dest) {
  75. Instruction *jmp = &fs->f->code[pc];
  76. int offset = dest-(pc+1);
  77. SETARG_sBc(*jmp, -offset);
  78. }
  79. /*
  80. ** returns current `pc' and marks it as a jump target (to avoid wrong
  81. ** optimizations with consecutive instructions not in the same basic block).
  82. ** discharge list of jumps to last target.
  83. */
  84. int luaK_getlabel (FuncState *fs) {
  85. if (fs->pc != fs->lasttarget) {
  86. int lasttarget = fs->lasttarget;
  87. fs->lasttarget = fs->pc;
  88. luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
  89. fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
  90. }
  91. return fs->pc;
  92. }
  93. static int luaK_getjump (FuncState *fs, int pc) {
  94. int offset = GETARG_sBc(fs->f->code[pc]);
  95. if (offset == NO_JUMP) /* point to itself represents end of list */
  96. return NO_JUMP; /* end of list */
  97. else
  98. return (pc+1)+offset; /* turn offset into absolute position */
  99. }
  100. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  101. Instruction *pi = &fs->f->code[pc];
  102. OpCode op = GET_OPCODE(*pi);
  103. if (op == OP_CJMP)
  104. return pi-1;
  105. else {
  106. lua_assert(op == OP_JMP || op == OP_FORLOOP || op == OP_TFORLOOP);
  107. return pi;
  108. }
  109. }
  110. static int need_value (FuncState *fs, int list, OpCode op) {
  111. /* check whether list has any jump different from `op' */
  112. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  113. if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1;
  114. return 0; /* not found */
  115. }
  116. static void patchtestreg (Instruction *i, int reg) {
  117. if (reg == NO_REG) reg = GETARG_B(*i);
  118. SETARG_A(*i, reg);
  119. }
  120. static void luaK_patchlistaux (FuncState *fs, int list,
  121. int ttarget, int treg, int ftarget, int freg, int dtarget) {
  122. while (list != NO_JUMP) {
  123. int next = luaK_getjump(fs, list);
  124. Instruction *i = getjumpcontrol(fs, list);
  125. switch (GET_OPCODE(*i)) {
  126. case OP_TESTT: {
  127. patchtestreg(i, treg);
  128. luaK_fixjump(fs, list, ttarget);
  129. break;
  130. }
  131. case OP_TESTF: {
  132. patchtestreg(i, freg);
  133. luaK_fixjump(fs, list, ftarget);
  134. break;
  135. }
  136. default: {
  137. luaK_fixjump(fs, list, dtarget); /* jump to default target */
  138. break;
  139. }
  140. }
  141. list = next;
  142. }
  143. }
  144. void luaK_patchlist (FuncState *fs, int list, int target) {
  145. if (target == fs->lasttarget) /* same target that list `jlt'? */
  146. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  147. else
  148. luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
  149. }
  150. void luaK_concat (FuncState *fs, int *l1, int l2) {
  151. if (*l1 == NO_JUMP)
  152. *l1 = l2;
  153. else {
  154. int list = *l1;
  155. int next;
  156. while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
  157. list = next;
  158. luaK_fixjump(fs, list, l2);
  159. }
  160. }
  161. void luaK_reserveregs (FuncState *fs, int n) {
  162. fs->freereg += n;
  163. if (fs->freereg > fs->f->maxstacksize) {
  164. if (fs->freereg >= MAXSTACK)
  165. luaK_error(fs->ls, l_s("function or expression too complex"));
  166. fs->f->maxstacksize = cast(short, fs->freereg);
  167. }
  168. }
  169. static void freereg (FuncState *fs, int reg) {
  170. if (reg >= fs->nactloc && reg < MAXSTACK) {
  171. fs->freereg--;
  172. lua_assert(reg == fs->freereg);
  173. }
  174. }
  175. static void freeexp (FuncState *fs, expdesc *e) {
  176. if (e->k == VNONRELOC)
  177. freereg(fs, e->u.i.info);
  178. }
  179. static int addk (FuncState *fs, TObject *k) {
  180. const TObject *index = luaH_get(fs->h, k);
  181. if (ttype(index) == LUA_TNUMBER) {
  182. lua_assert(luaO_equalObj(&fs->f->k[cast(int, nvalue(index))], k));
  183. return cast(int, nvalue(index));
  184. }
  185. else { /* constant not found; create a new entry */
  186. TObject o;
  187. Proto *f = fs->f;
  188. luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
  189. MAXARG_Bc, l_s("constant table overflow"));
  190. setobj(&f->k[fs->nk], k);
  191. setnvalue(&o, fs->nk);
  192. luaH_set(fs->L, fs->h, k, &o);
  193. return fs->nk++;
  194. }
  195. }
  196. int luaK_stringk (FuncState *fs, TString *s) {
  197. TObject o;
  198. setsvalue(&o, s);
  199. return addk(fs, &o);
  200. }
  201. static int number_constant (FuncState *fs, lua_Number r) {
  202. TObject o;
  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. if (nresults == LUA_MULTRET) nresults = NO_REG;
  209. SETARG_C(getcode(fs, e), nresults);
  210. if (nresults == 1) { /* `regular' expression? */
  211. e->k = VNONRELOC;
  212. e->u.i.info = GETARG_A(getcode(fs, e));
  213. }
  214. }
  215. }
  216. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  217. switch (e->k) {
  218. case VLOCAL: {
  219. e->k = VNONRELOC;
  220. break;
  221. }
  222. case VUPVAL: {
  223. e->u.i.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.i.info, 0);
  224. e->k = VRELOCABLE;
  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. luaK_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 = cast(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. luaK_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. luaK_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. luaK_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 VUPVAL: {
  372. int e = luaK_exp2anyreg(fs, exp);
  373. freereg(fs, e);
  374. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.i.info, 0);
  375. break;
  376. }
  377. case VGLOBAL: {
  378. int e = luaK_exp2anyreg(fs, exp);
  379. freereg(fs, e);
  380. luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
  381. break;
  382. }
  383. case VINDEXED: {
  384. int e = luaK_exp2anyreg(fs, exp);
  385. freereg(fs, e);
  386. luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
  387. break;
  388. }
  389. default: {
  390. lua_assert(0); /* invalid var kind to store */
  391. break;
  392. }
  393. }
  394. }
  395. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  396. int func;
  397. luaK_exp2anyreg(fs, e);
  398. freeexp(fs, e);
  399. func = fs->freereg;
  400. luaK_reserveregs(fs, 2);
  401. luaK_codeABC(fs, OP_SELF, func, e->u.i.info, luaK_exp2RK(fs, key));
  402. freeexp(fs, key);
  403. e->u.i.info = func;
  404. e->k = VNONRELOC;
  405. }
  406. static OpCode invertoperator (OpCode op) {
  407. switch (op) {
  408. case OP_TESTNE: return OP_TESTEQ;
  409. case OP_TESTEQ: return OP_TESTNE;
  410. case OP_TESTLT: return OP_TESTGE;
  411. case OP_TESTLE: return OP_TESTGT;
  412. case OP_TESTGT: return OP_TESTLE;
  413. case OP_TESTGE: return OP_TESTLT;
  414. case OP_TESTT: return OP_TESTF;
  415. case OP_TESTF: return OP_TESTT;
  416. default: lua_assert(0); return op; /* invalid jump instruction */
  417. }
  418. }
  419. static void invertjump (FuncState *fs, expdesc *e) {
  420. Instruction *pc = getjumpcontrol(fs, e->u.i.info);
  421. *pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  422. }
  423. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  424. if (e->k == VRELOCABLE) {
  425. Instruction ie = getcode(fs, e);
  426. if (GET_OPCODE(ie) == OP_NOT) {
  427. op = invertoperator(op);
  428. fs->pc--; /* remove previous OP_NOT */
  429. return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
  430. }
  431. /* else go through */
  432. }
  433. discharge2anyreg(fs, e);
  434. freeexp(fs, e);
  435. return luaK_condjump(fs, op, NO_REG, e->u.i.info, 0);
  436. }
  437. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  438. int pc; /* pc of last jump */
  439. luaK_dischargevars(fs, e);
  440. switch (e->k) {
  441. case VK: case VNUMBER: {
  442. pc = NO_JUMP; /* always true; do nothing */
  443. break;
  444. }
  445. case VNIL: {
  446. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  447. break;
  448. }
  449. case VJMP: {
  450. invertjump(fs, e);
  451. pc = e->u.i.info;
  452. break;
  453. }
  454. case VRELOCABLE:
  455. case VNONRELOC: {
  456. pc = jumponcond(fs, e, OP_TESTF);
  457. break;
  458. }
  459. default: {
  460. pc = 0; /* to avoid warnings */
  461. lua_assert(0); /* cannot happen */
  462. break;
  463. }
  464. }
  465. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  466. luaK_patchlist(fs, e->t, luaK_getlabel(fs));
  467. e->t = NO_JUMP;
  468. }
  469. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  470. int pc; /* pc of last jump */
  471. luaK_dischargevars(fs, e);
  472. switch (e->k) {
  473. case VNIL: {
  474. pc = NO_JUMP; /* always false; do nothing */
  475. break;
  476. }
  477. case VJMP: {
  478. pc = e->u.i.info;
  479. break;
  480. }
  481. case VK: case VNUMBER: /* cannot optimize it (`or' must keep value) */
  482. case VRELOCABLE:
  483. case VNONRELOC: {
  484. pc = jumponcond(fs, e, OP_TESTT);
  485. break;
  486. }
  487. default: {
  488. pc = 0; /* to avoid warnings */
  489. lua_assert(0); /* cannot happen */
  490. break;
  491. }
  492. }
  493. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  494. luaK_patchlist(fs, e->f, luaK_getlabel(fs));
  495. e->f = NO_JUMP;
  496. }
  497. static void codenot (FuncState *fs, expdesc *e) {
  498. luaK_dischargevars(fs, e);
  499. switch (e->k) {
  500. case VNIL: {
  501. e->u.n = 1;
  502. e->k = VNUMBER;
  503. break;
  504. }
  505. case VK: case VNUMBER: {
  506. e->k = VNIL;
  507. break;
  508. }
  509. case VJMP: {
  510. invertjump(fs, e);
  511. break;
  512. }
  513. case VRELOCABLE:
  514. case VNONRELOC: {
  515. discharge2anyreg(fs, e);
  516. freeexp(fs, e);
  517. e->u.i.info = luaK_codeABC(fs, OP_NOT, 0, e->u.i.info, 0);
  518. e->k = VRELOCABLE;
  519. break;
  520. }
  521. default: {
  522. lua_assert(0); /* cannot happen */
  523. break;
  524. }
  525. }
  526. /* interchange true and false lists */
  527. { int temp = e->f; e->f = e->t; e->t = temp; }
  528. }
  529. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  530. t->u.i.aux = luaK_exp2RK(fs, k);
  531. t->k = VINDEXED;
  532. }
  533. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  534. if (op == OPR_MINUS) {
  535. luaK_exp2val(fs, e);
  536. if (e->k == VNUMBER)
  537. e->u.n = -e->u.n;
  538. else {
  539. luaK_exp2anyreg(fs, e);
  540. freeexp(fs, e);
  541. e->u.i.info = luaK_codeABC(fs, OP_UNM, 0, e->u.i.info, 0);
  542. e->k = VRELOCABLE;
  543. }
  544. }
  545. else /* op == NOT */
  546. codenot(fs, e);
  547. }
  548. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  549. switch (op) {
  550. case OPR_AND: {
  551. luaK_goiftrue(fs, v);
  552. break;
  553. }
  554. case OPR_OR: {
  555. luaK_goiffalse(fs, v);
  556. break;
  557. }
  558. case OPR_CONCAT: {
  559. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  560. break;
  561. }
  562. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  563. /* non-comutative operators */
  564. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  565. break;
  566. }
  567. default: {
  568. luaK_exp2RK(fs, v);
  569. break;
  570. }
  571. }
  572. }
  573. /* opcode for each binary operator */
  574. static const OpCode codes[] = { /* ORDER OPR */
  575. OP_ADD, OP_SUB, OP_MUL, OP_DIV,
  576. OP_POW, OP_CONCAT,
  577. OP_TESTNE, OP_TESTEQ,
  578. OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
  579. };
  580. /* `inverted' opcode for each binary operator */
  581. /* ( -1 means operator has no inverse) */
  582. static const OpCode invcodes[] = { /* ORDER OPR */
  583. OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
  584. (OpCode)-1, (OpCode)-1,
  585. OP_TESTNE, OP_TESTEQ,
  586. OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
  587. };
  588. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  589. switch (op) {
  590. case OPR_AND: {
  591. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  592. luaK_dischargevars(fs, e2);
  593. luaK_concat(fs, &e1->f, e2->f);
  594. e1->k = e2->k; e1->u = e2->u; e1->t = e2->t;
  595. break;
  596. }
  597. case OPR_OR: {
  598. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  599. luaK_dischargevars(fs, e2);
  600. luaK_concat(fs, &e1->t, e2->t);
  601. e1->k = e2->k; e1->u = e2->u; e1->f = e2->f;
  602. break;
  603. }
  604. case OPR_CONCAT: {
  605. luaK_exp2val(fs, e2);
  606. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  607. lua_assert(e1->u.i.info == GETARG_B(getcode(fs, e2))-1);
  608. freeexp(fs, e1);
  609. SETARG_B(getcode(fs, e2), e1->u.i.info);
  610. e1->k = e2->k; e1->u.i.info = e2->u.i.info;
  611. }
  612. else {
  613. luaK_exp2nextreg(fs, e2);
  614. freeexp(fs, e2);
  615. freeexp(fs, e1);
  616. e1->u.i.info = luaK_codeABC(fs, codes[op], 0, e1->u.i.info,
  617. e2->u.i.info);
  618. e1->k = VRELOCABLE;
  619. }
  620. break;
  621. }
  622. case OPR_EQ: case OPR_NE: {
  623. luaK_exp2val(fs, e2);
  624. if (e2->k == VNIL) { /* exp x= nil ? */
  625. if (e1->k == VK) { /* constant x= nil ? */
  626. if (op == OPR_EQ) /* constant == nil ? */
  627. e1->k = VNIL; /* always false */
  628. /* else always true (leave the constant itself) */
  629. }
  630. else {
  631. OpCode opc = (op == OPR_EQ) ? OP_TESTF : OP_TESTT;
  632. e1->u.i.info = jumponcond(fs, e1, opc);
  633. e1->k = VJMP;
  634. }
  635. break;
  636. }
  637. /* else go through */
  638. }
  639. default: {
  640. int o1, o2;
  641. OpCode opc;
  642. if (e1->k != VK) { /* not a constant operator? */
  643. o1 = e1->u.i.info;
  644. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  645. opc = codes[op];
  646. }
  647. else { /* invert operands */
  648. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  649. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  650. opc = invcodes[op]; /* use inverted operator */
  651. }
  652. freeexp(fs, e2);
  653. freeexp(fs, e1);
  654. if (op < OPR_NE) { /* ORDER OPR */
  655. e1->u.i.info = luaK_codeABC(fs, opc, 0, o1, o2);
  656. e1->k = VRELOCABLE;
  657. }
  658. else { /* jump */
  659. e1->u.i.info = luaK_condjump(fs, opc, o1, 0, o2);
  660. e1->k = VJMP;
  661. }
  662. }
  663. }
  664. }
  665. static void codelineinfo (FuncState *fs) {
  666. Proto *f = fs->f;
  667. LexState *ls = fs->ls;
  668. if (ls->lastline > fs->lastline) {
  669. if (ls->lastline > fs->lastline+1) {
  670. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  671. MAX_INT, l_s("line info overflow"));
  672. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  673. }
  674. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  675. MAX_INT, l_s("line info overflow"));
  676. f->lineinfo[fs->nlineinfo++] = fs->pc;
  677. fs->lastline = ls->lastline;
  678. }
  679. }
  680. static int luaK_code (FuncState *fs, Instruction i) {
  681. Proto *f;
  682. codelineinfo(fs);
  683. f = fs->f;
  684. /* put new instruction in code array */
  685. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  686. MAX_INT, l_s("code size overflow"));
  687. f->code[fs->pc] = i;
  688. /*printf("free: %d ", fs->freereg); printopcode(f, fs->pc);*/
  689. return fs->pc++;
  690. }
  691. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  692. lua_assert(getOpMode(o) == iABC);
  693. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  694. }
  695. int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
  696. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  697. return luaK_code(fs, CREATE_ABc(o, a, bc));
  698. }