lcode.c 19 KB

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