lcode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. ** $Id: lcode.c,v 1.90 2002/03/05 12:42:47 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lua.h"
  8. #include "lcode.h"
  9. #include "ldebug.h"
  10. #include "ldo.h"
  11. #include "llex.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lopcodes.h"
  15. #include "lparser.h"
  16. #include "ltable.h"
  17. #define hasjumps(e) ((e)->t != (e)->f)
  18. #define getcode(fs,e) ((fs)->f->code[(e)->info])
  19. void luaK_error (LexState *ls, const char *msg) {
  20. luaX_error(ls, msg, ls->t.token);
  21. }
  22. void luaK_nil (FuncState *fs, int from, int n) {
  23. Instruction *previous;
  24. if (fs->pc > fs->lasttarget && /* no jumps to current position? */
  25. GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == OP_LOADNIL) {
  26. int pfrom = GETARG_A(*previous);
  27. int pto = GETARG_B(*previous);
  28. if (pfrom <= from && from <= pto+1) { /* can connect both? */
  29. if (from+n-1 > pto)
  30. SETARG_B(*previous, from+n-1);
  31. return;
  32. }
  33. }
  34. luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
  35. }
  36. int luaK_jump (FuncState *fs) {
  37. int j = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
  38. if (j == fs->lasttarget) { /* possible jumps to this jump? */
  39. luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
  40. fs->jlt = NO_JUMP;
  41. }
  42. return j;
  43. }
  44. static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) {
  45. luaK_codeABC(fs, op, A, B, C);
  46. return luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
  47. }
  48. static void luaK_fixjump (FuncState *fs, int pc, int dest) {
  49. Instruction *jmp = &fs->f->code[pc];
  50. if (dest == NO_JUMP)
  51. SETARG_sBc(*jmp, NO_JUMP); /* point to itself to represent end of list */
  52. else { /* jump is relative to position following jump instruction */
  53. int offset = dest-(pc+1);
  54. if (abs(offset) > MAXARG_sBc)
  55. luaK_error(fs->ls, "control structure too long");
  56. SETARG_sBc(*jmp, offset);
  57. }
  58. }
  59. /*
  60. ** returns current `pc' and marks it as a jump target (to avoid wrong
  61. ** optimizations with consecutive instructions not in the same basic block).
  62. ** discharge list of jumps to last target.
  63. */
  64. int luaK_getlabel (FuncState *fs) {
  65. if (fs->pc != fs->lasttarget) {
  66. int lasttarget = fs->lasttarget;
  67. fs->lasttarget = fs->pc;
  68. luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
  69. fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
  70. }
  71. return fs->pc;
  72. }
  73. static int luaK_getjump (FuncState *fs, int pc) {
  74. int offset = GETARG_sBc(fs->f->code[pc]);
  75. if (offset == NO_JUMP) /* point to itself represents end of list */
  76. return NO_JUMP; /* end of list */
  77. else
  78. return (pc+1)+offset; /* turn offset into absolute position */
  79. }
  80. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  81. Instruction *pi = &fs->f->code[pc];
  82. if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT))
  83. return pi-1;
  84. else
  85. return pi;
  86. }
  87. static int need_value (FuncState *fs, int list, OpCode op) {
  88. /* check whether list has any jump different from `op' */
  89. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  90. if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1;
  91. return 0; /* not found */
  92. }
  93. static void patchtestreg (Instruction *i, int reg) {
  94. if (reg == NO_REG) reg = GETARG_B(*i);
  95. SETARG_A(*i, reg);
  96. }
  97. static void luaK_patchlistaux (FuncState *fs, int list,
  98. int ttarget, int treg, int ftarget, int freg, int dtarget) {
  99. while (list != NO_JUMP) {
  100. int next = luaK_getjump(fs, list);
  101. Instruction *i = getjumpcontrol(fs, list);
  102. switch (GET_OPCODE(*i)) {
  103. case OP_TESTT: {
  104. patchtestreg(i, treg);
  105. luaK_fixjump(fs, list, ttarget);
  106. break;
  107. }
  108. case OP_TESTF: {
  109. patchtestreg(i, freg);
  110. luaK_fixjump(fs, list, ftarget);
  111. break;
  112. }
  113. default: {
  114. luaK_fixjump(fs, list, dtarget); /* jump to default target */
  115. break;
  116. }
  117. }
  118. list = next;
  119. }
  120. }
  121. void luaK_patchlist (FuncState *fs, int list, int target) {
  122. if (target == fs->lasttarget) /* same target that list `jlt'? */
  123. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  124. else
  125. luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
  126. }
  127. void luaK_patchtohere (FuncState *fs, int list) {
  128. luaK_patchlist(fs, list, luaK_getlabel(fs));
  129. }
  130. void luaK_concat (FuncState *fs, int *l1, int l2) {
  131. if (*l1 == NO_JUMP)
  132. *l1 = l2;
  133. else {
  134. int list = *l1;
  135. int next;
  136. while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
  137. list = next;
  138. luaK_fixjump(fs, list, l2);
  139. }
  140. }
  141. void luaK_checkstack (FuncState *fs, int n) {
  142. int newstack = fs->freereg + n;
  143. if (newstack > fs->f->maxstacksize) {
  144. if (newstack >= MAXSTACK)
  145. luaK_error(fs->ls, "function or expression too complex");
  146. fs->f->maxstacksize = cast(lu_byte, newstack);
  147. }
  148. }
  149. void luaK_reserveregs (FuncState *fs, int n) {
  150. luaK_checkstack(fs, n);
  151. fs->freereg += n;
  152. }
  153. static void freereg (FuncState *fs, int reg) {
  154. if (reg >= fs->nactloc && reg < MAXSTACK) {
  155. fs->freereg--;
  156. lua_assert(reg == fs->freereg);
  157. }
  158. }
  159. static void freeexp (FuncState *fs, expdesc *e) {
  160. if (e->k == VNONRELOC)
  161. freereg(fs, e->info);
  162. }
  163. static int addk (FuncState *fs, TObject *k, TObject *v) {
  164. const TObject *index = luaH_get(fs->h, k);
  165. if (ttype(index) == LUA_TNUMBER) {
  166. lua_assert(luaO_equalObj(&fs->f->k[cast(int, nvalue(index))], v));
  167. return cast(int, nvalue(index));
  168. }
  169. else { /* constant not found; create a new entry */
  170. TObject o;
  171. Proto *f = fs->f;
  172. luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
  173. MAXARG_Bc, "constant table overflow");
  174. setobj(&f->k[fs->nk], v);
  175. setnvalue(&o, fs->nk);
  176. luaH_set(fs->L, fs->h, k, &o);
  177. return fs->nk++;
  178. }
  179. }
  180. int luaK_stringK (FuncState *fs, TString *s) {
  181. TObject o;
  182. setsvalue(&o, s);
  183. return addk(fs, &o, &o);
  184. }
  185. int luaK_numberK (FuncState *fs, lua_Number r) {
  186. TObject o;
  187. setnvalue(&o, r);
  188. return addk(fs, &o, &o);
  189. }
  190. static int nil_constant (FuncState *fs) {
  191. TObject k, v;
  192. setnilvalue(&v);
  193. sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */
  194. return addk(fs, &k, &v);
  195. }
  196. void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
  197. if (e->k == VCALL) { /* expression is an open function call? */
  198. SETARG_C(getcode(fs, e), nresults+1);
  199. if (nresults == 1) { /* `regular' expression? */
  200. e->k = VNONRELOC;
  201. e->info = GETARG_A(getcode(fs, e));
  202. }
  203. }
  204. }
  205. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  206. switch (e->k) {
  207. case VLOCAL: {
  208. e->k = VNONRELOC;
  209. break;
  210. }
  211. case VUPVAL: {
  212. e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
  213. e->k = VRELOCABLE;
  214. break;
  215. }
  216. case VGLOBAL: {
  217. e->info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->info);
  218. e->k = VRELOCABLE;
  219. break;
  220. }
  221. case VINDEXED: {
  222. freereg(fs, e->aux);
  223. freereg(fs, e->info);
  224. e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux);
  225. e->k = VRELOCABLE;
  226. break;
  227. }
  228. case VCALL: {
  229. luaK_setcallreturns(fs, e, 1);
  230. break;
  231. }
  232. default: break; /* there is one value available (somewhere) */
  233. }
  234. }
  235. static int code_label (FuncState *fs, int A, int b, int jump) {
  236. luaK_getlabel(fs); /* those instructions may be jump targets */
  237. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  238. }
  239. static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
  240. if (e->k == VJMP || hasjumps(e)) {
  241. int final; /* position after whole expression */
  242. int p_f = NO_JUMP; /* position of an eventual PUSH false */
  243. int p_t = NO_JUMP; /* position of an eventual PUSH true */
  244. if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) ||
  245. need_value(fs, e->t, OP_TESTT)) {
  246. /* expression needs values */
  247. if (e->k != VJMP) {
  248. luaK_getlabel(fs); /* these instruction may be jump target */
  249. luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  250. }
  251. else { /* last expression is a conditional (test + jump) */
  252. fs->pc--; /* remove its jump */
  253. lua_assert(testOpMode(GET_OPCODE(fs->f->code[fs->pc - 1]), OpModeT));
  254. }
  255. p_t = code_label(fs, reg, 1, 1);
  256. p_f = code_label(fs, reg, 0, 0);
  257. }
  258. final = luaK_getlabel(fs);
  259. luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
  260. luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
  261. }
  262. e->f = e->t = NO_JUMP;
  263. }
  264. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  265. luaK_dischargevars(fs, e);
  266. switch (e->k) {
  267. case VNIL: {
  268. luaK_nil(fs, reg, 1);
  269. break;
  270. }
  271. case VFALSE: case VTRUE: {
  272. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  273. break;
  274. }
  275. case VK: {
  276. luaK_codeABc(fs, OP_LOADK, reg, e->info);
  277. break;
  278. }
  279. case VRELOCABLE: {
  280. Instruction *pc = &getcode(fs, e);
  281. SETARG_A(*pc, reg);
  282. break;
  283. }
  284. default: return;
  285. }
  286. e->info = reg;
  287. e->k = VNONRELOC;
  288. }
  289. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  290. if (e->k != VNONRELOC) {
  291. luaK_reserveregs(fs, 1);
  292. discharge2reg(fs, e, fs->freereg-1);
  293. }
  294. }
  295. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  296. discharge2reg(fs, e, reg);
  297. switch (e->k) {
  298. case VVOID: {
  299. return; /* nothing to do... */
  300. }
  301. case VNONRELOC: {
  302. if (reg != e->info)
  303. luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
  304. break;
  305. }
  306. case VJMP: {
  307. break;
  308. }
  309. default: {
  310. lua_assert(0); /* cannot happen */
  311. break;
  312. }
  313. }
  314. dischargejumps(fs, e, reg);
  315. e->info = reg;
  316. e->k = VNONRELOC;
  317. }
  318. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  319. int reg;
  320. luaK_dischargevars(fs, e);
  321. freeexp(fs, e);
  322. reg = fs->freereg;
  323. luaK_reserveregs(fs, 1);
  324. luaK_exp2reg(fs, e, reg);
  325. }
  326. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  327. luaK_dischargevars(fs, e);
  328. if (e->k == VNONRELOC) {
  329. if (!hasjumps(e)) return e->info; /* exp is already in a register */
  330. if (e->info >= fs->nactloc) { /* reg. is not a local? */
  331. dischargejumps(fs, e, e->info); /* put value on it */
  332. return e->info;
  333. }
  334. }
  335. luaK_exp2nextreg(fs, e); /* default */
  336. return e->info;
  337. }
  338. void luaK_exp2val (FuncState *fs, expdesc *e) {
  339. if (hasjumps(e))
  340. luaK_exp2anyreg(fs, e);
  341. else
  342. luaK_dischargevars(fs, e);
  343. }
  344. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  345. luaK_exp2val(fs, e);
  346. switch (e->k) {
  347. case VNIL: {
  348. if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
  349. e->info = nil_constant(fs);
  350. e->k = VK;
  351. return e->info + MAXSTACK;
  352. }
  353. else break;
  354. }
  355. case VK: {
  356. if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
  357. return e->info + MAXSTACK;
  358. else break;
  359. }
  360. default: break;
  361. }
  362. /* not a constant in the right range: put in a register */
  363. return luaK_exp2anyreg(fs, e);
  364. }
  365. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  366. switch (var->k) {
  367. case VLOCAL: {
  368. freeexp(fs, exp);
  369. luaK_exp2reg(fs, exp, var->info);
  370. break;
  371. }
  372. case VUPVAL: {
  373. int e = luaK_exp2anyreg(fs, exp);
  374. freereg(fs, e);
  375. luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
  376. break;
  377. }
  378. case VGLOBAL: {
  379. int e = luaK_exp2anyreg(fs, exp);
  380. freereg(fs, e);
  381. luaK_codeABc(fs, OP_SETGLOBAL, e, var->info);
  382. break;
  383. }
  384. case VINDEXED: {
  385. int e = luaK_exp2anyreg(fs, exp);
  386. freereg(fs, e);
  387. luaK_codeABC(fs, OP_SETTABLE, e, var->info, var->aux);
  388. break;
  389. }
  390. default: {
  391. lua_assert(0); /* invalid var kind to store */
  392. break;
  393. }
  394. }
  395. }
  396. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  397. int func;
  398. luaK_exp2anyreg(fs, e);
  399. freeexp(fs, e);
  400. func = fs->freereg;
  401. luaK_reserveregs(fs, 2);
  402. luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key));
  403. freeexp(fs, key);
  404. e->info = func;
  405. e->k = VNONRELOC;
  406. }
  407. static OpCode invertoperator (OpCode op) {
  408. switch (op) {
  409. case OP_TESTNE: return OP_TESTEQ;
  410. case OP_TESTEQ: return OP_TESTNE;
  411. case OP_TESTLT: return OP_TESTGE;
  412. case OP_TESTLE: return OP_TESTGT;
  413. case OP_TESTGT: return OP_TESTLE;
  414. case OP_TESTGE: return OP_TESTLT;
  415. case OP_TESTT: return OP_TESTF;
  416. case OP_TESTF: return OP_TESTT;
  417. default: lua_assert(0); return op; /* invalid jump instruction */
  418. }
  419. }
  420. static void invertjump (FuncState *fs, expdesc *e) {
  421. Instruction *pc = getjumpcontrol(fs, e->info);
  422. *pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  423. }
  424. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  425. if (e->k == VRELOCABLE) {
  426. Instruction ie = getcode(fs, e);
  427. if (GET_OPCODE(ie) == OP_NOT) {
  428. op = invertoperator(op);
  429. fs->pc--; /* remove previous OP_NOT */
  430. return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
  431. }
  432. /* else go through */
  433. }
  434. discharge2anyreg(fs, e);
  435. freeexp(fs, e);
  436. return luaK_condjump(fs, op, NO_REG, e->info, 0);
  437. }
  438. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  439. int pc; /* pc of last jump */
  440. luaK_dischargevars(fs, e);
  441. switch (e->k) {
  442. case VK: case VTRUE: {
  443. pc = NO_JUMP; /* always true; do nothing */
  444. break;
  445. }
  446. case VFALSE: {
  447. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  448. break;
  449. }
  450. case VJMP: {
  451. invertjump(fs, e);
  452. pc = e->info;
  453. break;
  454. }
  455. default: {
  456. pc = jumponcond(fs, e, OP_TESTF);
  457. break;
  458. }
  459. }
  460. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  461. luaK_patchtohere(fs, e->t);
  462. e->t = NO_JUMP;
  463. }
  464. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  465. int pc; /* pc of last jump */
  466. luaK_dischargevars(fs, e);
  467. switch (e->k) {
  468. case VNIL: case VFALSE: {
  469. pc = NO_JUMP; /* always false; do nothing */
  470. break;
  471. }
  472. case VTRUE: {
  473. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  474. break;
  475. }
  476. case VJMP: {
  477. pc = e->info;
  478. break;
  479. }
  480. default: {
  481. pc = jumponcond(fs, e, OP_TESTT);
  482. break;
  483. }
  484. }
  485. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  486. luaK_patchtohere(fs, e->f);
  487. e->f = NO_JUMP;
  488. }
  489. static void codenot (FuncState *fs, expdesc *e) {
  490. luaK_dischargevars(fs, e);
  491. switch (e->k) {
  492. case VNIL: case VFALSE: {
  493. e->k = VTRUE;
  494. break;
  495. }
  496. case VK: case VTRUE: {
  497. e->k = VFALSE;
  498. break;
  499. }
  500. case VJMP: {
  501. invertjump(fs, e);
  502. break;
  503. }
  504. case VRELOCABLE:
  505. case VNONRELOC: {
  506. discharge2anyreg(fs, e);
  507. freeexp(fs, e);
  508. e->info = luaK_codeABC(fs, OP_NOT, 0, e->info, 0);
  509. e->k = VRELOCABLE;
  510. break;
  511. }
  512. default: {
  513. lua_assert(0); /* cannot happen */
  514. break;
  515. }
  516. }
  517. /* interchange true and false lists */
  518. { int temp = e->f; e->f = e->t; e->t = temp; }
  519. }
  520. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  521. t->aux = luaK_exp2RK(fs, k);
  522. t->k = VINDEXED;
  523. }
  524. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  525. if (op == OPR_MINUS) {
  526. luaK_exp2val(fs, e);
  527. if (e->k == VK && ttype(&fs->f->k[e->info]) == LUA_TNUMBER)
  528. e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
  529. else {
  530. luaK_exp2anyreg(fs, e);
  531. freeexp(fs, e);
  532. e->info = luaK_codeABC(fs, OP_UNM, 0, e->info, 0);
  533. e->k = VRELOCABLE;
  534. }
  535. }
  536. else /* op == NOT */
  537. codenot(fs, e);
  538. }
  539. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  540. switch (op) {
  541. case OPR_AND: {
  542. luaK_goiftrue(fs, v);
  543. break;
  544. }
  545. case OPR_OR: {
  546. luaK_goiffalse(fs, v);
  547. break;
  548. }
  549. case OPR_CONCAT: {
  550. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  551. break;
  552. }
  553. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  554. /* non-comutative operators */
  555. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  556. break;
  557. }
  558. default: {
  559. luaK_exp2RK(fs, v);
  560. break;
  561. }
  562. }
  563. }
  564. /* opcode for each binary operator */
  565. static const OpCode codes[] = { /* ORDER OPR */
  566. OP_ADD, OP_SUB, OP_MUL, OP_DIV,
  567. OP_POW, OP_CONCAT,
  568. OP_TESTNE, OP_TESTEQ,
  569. OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
  570. };
  571. /* `inverted' opcode for each binary operator */
  572. /* ( -1 means operator has no inverse) */
  573. static const OpCode invcodes[] = { /* ORDER OPR */
  574. OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
  575. (OpCode)-1, (OpCode)-1,
  576. OP_TESTNE, OP_TESTEQ,
  577. OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
  578. };
  579. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  580. switch (op) {
  581. case OPR_AND: {
  582. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  583. luaK_dischargevars(fs, e2);
  584. luaK_concat(fs, &e1->f, e2->f);
  585. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->t = e2->t;
  586. break;
  587. }
  588. case OPR_OR: {
  589. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  590. luaK_dischargevars(fs, e2);
  591. luaK_concat(fs, &e1->t, e2->t);
  592. e1->k = e2->k; e1->info = e2->info; e1->aux = e2->aux; e1->f = e2->f;
  593. break;
  594. }
  595. case OPR_CONCAT: {
  596. luaK_exp2val(fs, e2);
  597. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  598. lua_assert(e1->info == GETARG_B(getcode(fs, e2))-1);
  599. freeexp(fs, e1);
  600. SETARG_B(getcode(fs, e2), e1->info);
  601. e1->k = e2->k; e1->info = e2->info;
  602. }
  603. else {
  604. luaK_exp2nextreg(fs, e2);
  605. freeexp(fs, e2);
  606. freeexp(fs, e1);
  607. e1->info = luaK_codeABC(fs, codes[op], 0, e1->info, e2->info);
  608. e1->k = VRELOCABLE;
  609. }
  610. break;
  611. }
  612. default: {
  613. int o1, o2;
  614. OpCode opc;
  615. if (e1->k != VK) { /* not a constant operator? */
  616. o1 = e1->info;
  617. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  618. opc = codes[op];
  619. }
  620. else { /* invert operands */
  621. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  622. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  623. opc = invcodes[op]; /* use inverted operator */
  624. }
  625. freeexp(fs, e2);
  626. freeexp(fs, e1);
  627. if (op < OPR_NE) { /* ORDER OPR */
  628. e1->info = luaK_codeABC(fs, opc, 0, o1, o2);
  629. e1->k = VRELOCABLE;
  630. }
  631. else { /* jump */
  632. e1->info = luaK_condjump(fs, opc, o1, 0, o2);
  633. e1->k = VJMP;
  634. }
  635. }
  636. }
  637. }
  638. static void codelineinfo (FuncState *fs) {
  639. Proto *f = fs->f;
  640. LexState *ls = fs->ls;
  641. if (ls->lastline > fs->lastline) {
  642. if (ls->lastline > fs->lastline+1) {
  643. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  644. MAX_INT, "line info overflow");
  645. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  646. }
  647. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  648. MAX_INT, "line info overflow");
  649. f->lineinfo[fs->nlineinfo++] = fs->pc;
  650. fs->lastline = ls->lastline;
  651. }
  652. }
  653. static int luaK_code (FuncState *fs, Instruction i) {
  654. Proto *f;
  655. codelineinfo(fs);
  656. f = fs->f;
  657. /* put new instruction in code array */
  658. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  659. MAX_INT, "code size overflow");
  660. f->code[fs->pc] = i;
  661. return fs->pc++;
  662. }
  663. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  664. lua_assert(getOpMode(o) == iABC);
  665. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  666. }
  667. int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
  668. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  669. return luaK_code(fs, CREATE_ABc(o, a, bc));
  670. }