lcode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 (Instruction)(-1);/* no optimizations after an invalid instruction */
  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 = (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[(int)nvalue(index)], k));
  183. return (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 VGLOBAL: {
  223. e->u.i.info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->u.i.info);
  224. e->k = VRELOCABLE;
  225. break;
  226. }
  227. case VINDEXED: {
  228. freereg(fs, e->u.i.aux);
  229. freereg(fs, e->u.i.info);
  230. e->u.i.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.i.info, e->u.i.aux);
  231. e->k = VRELOCABLE;
  232. break;
  233. }
  234. case VCALL: {
  235. luaK_setcallreturns(fs, e, 1);
  236. break;
  237. }
  238. default: break; /* there is one value available (somewhere) */
  239. }
  240. }
  241. static int code_label (FuncState *fs, OpCode op, int A, int sBc) {
  242. luaK_getlabel(fs); /* those instructions may be jump targets */
  243. return luaK_codeAsBc(fs, op, A, sBc);
  244. }
  245. static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
  246. if (hasjumps(e)) {
  247. int final; /* position after whole expression */
  248. int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
  249. int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
  250. if (need_value(fs, e->f, OP_TESTF) || need_value(fs, e->t, OP_TESTT)) {
  251. /* expression needs values */
  252. if (e->k != VJMP)
  253. code_label(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  254. p_nil = code_label(fs, OP_NILJMP, reg, 0);
  255. p_1 = code_label(fs, OP_LOADINT, reg, 1);
  256. }
  257. final = luaK_getlabel(fs);
  258. luaK_patchlistaux(fs, e->f, p_nil, NO_REG, final, reg, p_nil);
  259. luaK_patchlistaux(fs, e->t, final, reg, p_1, NO_REG, p_1);
  260. }
  261. e->f = e->t = NO_JUMP;
  262. }
  263. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  264. luaK_dischargevars(fs, e);
  265. switch (e->k) {
  266. case VNIL: {
  267. luaK_nil(fs, reg, 1);
  268. break;
  269. }
  270. case VNUMBER: {
  271. lua_Number f = e->u.n;
  272. int i = (int)f;
  273. if ((lua_Number)i == f && -MAXARG_sBc <= i && i <= MAXARG_sBc)
  274. luaK_codeAsBc(fs, OP_LOADINT, reg, i); /* f has a small int value */
  275. else
  276. luaK_codeABc(fs, OP_LOADK, reg, number_constant(fs, f));
  277. break;
  278. }
  279. case VK: {
  280. luaK_codeABc(fs, OP_LOADK, reg, e->u.i.info);
  281. break;
  282. }
  283. case VRELOCABLE: {
  284. Instruction *pc = &getcode(fs, e);
  285. SETARG_A(*pc, reg);
  286. break;
  287. }
  288. default: return;
  289. }
  290. e->u.i.info = reg;
  291. e->k = VNONRELOC;
  292. }
  293. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  294. if (e->k != VNONRELOC) {
  295. luaK_reserveregs(fs, 1);
  296. discharge2reg(fs, e, fs->freereg-1);
  297. }
  298. }
  299. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  300. discharge2reg(fs, e, reg);
  301. switch (e->k) {
  302. case VVOID: {
  303. return; /* nothing to do... */
  304. }
  305. case VNONRELOC: {
  306. if (reg != e->u.i.info)
  307. luaK_codeABC(fs, OP_MOVE, reg, e->u.i.info, 0);
  308. break;
  309. }
  310. case VJMP: {
  311. luaK_concat(fs, &e->t, e->u.i.info); /* put this jump in `t' list */
  312. break;
  313. }
  314. default: {
  315. lua_assert(0); /* cannot happen */
  316. break;
  317. }
  318. }
  319. dischargejumps(fs, e, reg);
  320. e->u.i.info = reg;
  321. e->k = VNONRELOC;
  322. }
  323. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  324. int reg;
  325. luaK_dischargevars(fs, e);
  326. freeexp(fs, e);
  327. reg = fs->freereg;
  328. luaK_reserveregs(fs, 1);
  329. luaK_exp2reg(fs, e, reg);
  330. }
  331. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  332. luaK_dischargevars(fs, e);
  333. if (e->k == VNONRELOC) {
  334. if (!hasjumps(e)) return e->u.i.info; /* exp is already in a register */
  335. if (e->u.i.info >= fs->nactloc) { /* reg. is not a local? */
  336. dischargejumps(fs, e, e->u.i.info); /* put value on it */
  337. return e->u.i.info;
  338. }
  339. }
  340. luaK_exp2nextreg(fs, e); /* default */
  341. return e->u.i.info;
  342. }
  343. void luaK_exp2val (FuncState *fs, expdesc *e) {
  344. if (hasjumps(e))
  345. luaK_exp2anyreg(fs, e);
  346. else
  347. luaK_dischargevars(fs, e);
  348. }
  349. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  350. luaK_exp2val(fs, e);
  351. if (e->k == VNUMBER && fs->nk + MAXSTACK <= MAXARG_C) {
  352. e->u.i.info = number_constant(fs, e->u.n);
  353. e->k = VK;
  354. }
  355. else if (!(e->k == VK && e->u.i.info + MAXSTACK <= MAXARG_C))
  356. luaK_exp2anyreg(fs, e); /* not a constant in the right range */
  357. return (e->k == VK) ? e->u.i.info+MAXSTACK : e->u.i.info;
  358. }
  359. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  360. switch (var->k) {
  361. case VLOCAL: {
  362. freeexp(fs, exp);
  363. luaK_exp2reg(fs, exp, var->u.i.info);
  364. break;
  365. }
  366. case VGLOBAL: {
  367. int e = luaK_exp2anyreg(fs, exp);
  368. freereg(fs, e);
  369. luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
  370. break;
  371. }
  372. case VINDEXED: {
  373. int e = luaK_exp2anyreg(fs, exp);
  374. freereg(fs, e);
  375. luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
  376. break;
  377. }
  378. default: {
  379. lua_assert(0); /* invalid var kind to store */
  380. break;
  381. }
  382. }
  383. }
  384. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  385. int func;
  386. luaK_exp2anyreg(fs, e);
  387. freeexp(fs, e);
  388. func = fs->freereg;
  389. luaK_reserveregs(fs, 2);
  390. luaK_codeABC(fs, OP_SELF, func, e->u.i.info, luaK_exp2RK(fs, key));
  391. freeexp(fs, key);
  392. e->u.i.info = func;
  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, NO_REG, GETARG_B(ie), 0);
  419. }
  420. /* else go through */
  421. }
  422. discharge2anyreg(fs, e);
  423. freeexp(fs, e);
  424. return luaK_condjump(fs, op, NO_REG, e->u.i.info, 0);
  425. }
  426. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  427. int pc; /* pc of last jump */
  428. luaK_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. luaK_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. luaK_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. luaK_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. luaK_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, 0, 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, unsigned int bc) {
  685. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  686. return luaK_code(fs, CREATE_ABc(o, a, bc));
  687. }