lcode.c 20 KB

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