lcode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. ** prep-for instructions (OP_FORPREP & OP_TFORPREP) have a negated jump,
  61. ** as they simulate the real jump...
  62. */
  63. void luaK_fixfor (FuncState *fs, int pc, int dest) {
  64. Instruction *jmp = &fs->f->code[pc];
  65. int offset = dest-(pc+1);
  66. SETARG_sBc(*jmp, -offset);
  67. }
  68. /*
  69. ** returns current `pc' and marks it as a jump target (to avoid wrong
  70. ** optimizations with consecutive instructions not in the same basic block).
  71. ** discharge list of jumps to last target.
  72. */
  73. int luaK_getlabel (FuncState *fs) {
  74. if (fs->pc != fs->lasttarget) {
  75. int lasttarget = fs->lasttarget;
  76. fs->lasttarget = fs->pc;
  77. luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
  78. fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
  79. }
  80. return fs->pc;
  81. }
  82. static int luaK_getjump (FuncState *fs, int pc) {
  83. int offset = GETARG_sBc(fs->f->code[pc]);
  84. if (offset == NO_JUMP) /* point to itself represents end of list */
  85. return NO_JUMP; /* end of list */
  86. else
  87. return (pc+1)+offset; /* turn offset into absolute position */
  88. }
  89. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  90. Instruction *pi = &fs->f->code[pc];
  91. if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT))
  92. return pi-1;
  93. else
  94. return pi;
  95. }
  96. static int need_value (FuncState *fs, int list, OpCode op) {
  97. /* check whether list has any jump different from `op' */
  98. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  99. if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1;
  100. return 0; /* not found */
  101. }
  102. static void patchtestreg (Instruction *i, int reg) {
  103. if (reg == NO_REG) reg = GETARG_B(*i);
  104. SETARG_A(*i, reg);
  105. }
  106. static void luaK_patchlistaux (FuncState *fs, int list,
  107. int ttarget, int treg, int ftarget, int freg, int dtarget) {
  108. while (list != NO_JUMP) {
  109. int next = luaK_getjump(fs, list);
  110. Instruction *i = getjumpcontrol(fs, list);
  111. switch (GET_OPCODE(*i)) {
  112. case OP_TESTT: {
  113. patchtestreg(i, treg);
  114. luaK_fixjump(fs, list, ttarget);
  115. break;
  116. }
  117. case OP_TESTF: {
  118. patchtestreg(i, freg);
  119. luaK_fixjump(fs, list, ftarget);
  120. break;
  121. }
  122. default: {
  123. luaK_fixjump(fs, list, dtarget); /* jump to default target */
  124. break;
  125. }
  126. }
  127. list = next;
  128. }
  129. }
  130. void luaK_patchlist (FuncState *fs, int list, int target) {
  131. if (target == fs->lasttarget) /* same target that list `jlt'? */
  132. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  133. else
  134. luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
  135. }
  136. void luaK_concat (FuncState *fs, int *l1, int l2) {
  137. if (*l1 == NO_JUMP)
  138. *l1 = l2;
  139. else {
  140. int list = *l1;
  141. int next;
  142. while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
  143. list = next;
  144. luaK_fixjump(fs, list, l2);
  145. }
  146. }
  147. void luaK_reserveregs (FuncState *fs, int n) {
  148. fs->freereg += n;
  149. if (fs->freereg > fs->f->maxstacksize) {
  150. if (fs->freereg >= MAXSTACK)
  151. luaK_error(fs->ls, "function or expression too complex");
  152. fs->f->maxstacksize = cast(short, fs->freereg);
  153. }
  154. }
  155. static void freereg (FuncState *fs, int reg) {
  156. if (reg >= fs->nactloc && reg < MAXSTACK) {
  157. fs->freereg--;
  158. lua_assert(reg == fs->freereg);
  159. }
  160. }
  161. static void freeexp (FuncState *fs, expdesc *e) {
  162. if (e->k == VNONRELOC)
  163. freereg(fs, e->info);
  164. }
  165. static int addk (FuncState *fs, TObject *k, TObject *v) {
  166. const TObject *index = luaH_get(fs->h, k);
  167. if (ttype(index) == LUA_TNUMBER) {
  168. lua_assert(luaO_equalObj(&fs->f->k[cast(int, nvalue(index))], v));
  169. return cast(int, nvalue(index));
  170. }
  171. else { /* constant not found; create a new entry */
  172. TObject o;
  173. Proto *f = fs->f;
  174. luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
  175. MAXARG_Bc, "constant table overflow");
  176. setobj(&f->k[fs->nk], v);
  177. setnvalue(&o, fs->nk);
  178. luaH_set(fs->L, fs->h, k, &o);
  179. return fs->nk++;
  180. }
  181. }
  182. int luaK_stringK (FuncState *fs, TString *s) {
  183. TObject o;
  184. setsvalue(&o, s);
  185. return addk(fs, &o, &o);
  186. }
  187. int luaK_numberK (FuncState *fs, lua_Number r) {
  188. TObject o;
  189. setnvalue(&o, r);
  190. return addk(fs, &o, &o);
  191. }
  192. static int nil_constant (FuncState *fs) {
  193. TObject k, v;
  194. setnilvalue(&v);
  195. sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */
  196. return addk(fs, &k, &v);
  197. }
  198. void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
  199. if (e->k == VCALL) { /* expression is an open function call? */
  200. SETARG_C(getcode(fs, e), nresults+1);
  201. if (nresults == 1) { /* `regular' expression? */
  202. e->k = VNONRELOC;
  203. e->info = GETARG_A(getcode(fs, e));
  204. }
  205. }
  206. }
  207. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  208. switch (e->k) {
  209. case VLOCAL: {
  210. e->k = VNONRELOC;
  211. break;
  212. }
  213. case VUPVAL: {
  214. e->info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->info, 0);
  215. e->k = VRELOCABLE;
  216. break;
  217. }
  218. case VGLOBAL: {
  219. e->info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->info);
  220. e->k = VRELOCABLE;
  221. break;
  222. }
  223. case VINDEXED: {
  224. freereg(fs, e->aux);
  225. freereg(fs, e->info);
  226. e->info = luaK_codeABC(fs, OP_GETTABLE, 0, e->info, e->aux);
  227. e->k = VRELOCABLE;
  228. break;
  229. }
  230. case VCALL: {
  231. luaK_setcallreturns(fs, e, 1);
  232. break;
  233. }
  234. default: break; /* there is one value available (somewhere) */
  235. }
  236. }
  237. static int code_label (FuncState *fs, int A, int b, int jump) {
  238. luaK_getlabel(fs); /* those instructions may be jump targets */
  239. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  240. }
  241. static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
  242. if (e->k == VJMP || hasjumps(e)) {
  243. int final; /* position after whole expression */
  244. int p_f = NO_JUMP; /* position of an eventual PUSH false */
  245. int p_t = NO_JUMP; /* position of an eventual PUSH true */
  246. if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) ||
  247. need_value(fs, e->t, OP_TESTT)) {
  248. /* expression needs values */
  249. if (e->k != VJMP) {
  250. luaK_getlabel(fs); /* these instruction may be jump target */
  251. luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  252. }
  253. else { /* last expression is a conditional (test + jump) */
  254. fs->pc--; /* remove its jump */
  255. lua_assert(testOpMode(GET_OPCODE(fs->f->code[fs->pc - 1]), OpModeT));
  256. }
  257. p_t = code_label(fs, reg, 1, 1);
  258. p_f = code_label(fs, reg, 0, 0);
  259. }
  260. final = luaK_getlabel(fs);
  261. luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
  262. luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
  263. }
  264. e->f = e->t = NO_JUMP;
  265. }
  266. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  267. luaK_dischargevars(fs, e);
  268. switch (e->k) {
  269. case VNIL: {
  270. luaK_nil(fs, reg, 1);
  271. break;
  272. }
  273. case VFALSE: case VTRUE: {
  274. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  275. break;
  276. }
  277. case VK: {
  278. luaK_codeABc(fs, OP_LOADK, reg, e->info);
  279. break;
  280. }
  281. case VRELOCABLE: {
  282. Instruction *pc = &getcode(fs, e);
  283. SETARG_A(*pc, reg);
  284. break;
  285. }
  286. default: return;
  287. }
  288. e->info = reg;
  289. e->k = VNONRELOC;
  290. }
  291. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  292. if (e->k != VNONRELOC) {
  293. luaK_reserveregs(fs, 1);
  294. discharge2reg(fs, e, fs->freereg-1);
  295. }
  296. }
  297. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  298. discharge2reg(fs, e, reg);
  299. switch (e->k) {
  300. case VVOID: {
  301. return; /* nothing to do... */
  302. }
  303. case VNONRELOC: {
  304. if (reg != e->info)
  305. luaK_codeABC(fs, OP_MOVE, reg, e->info, 0);
  306. break;
  307. }
  308. case VJMP: {
  309. break;
  310. }
  311. default: {
  312. lua_assert(0); /* cannot happen */
  313. break;
  314. }
  315. }
  316. dischargejumps(fs, e, reg);
  317. e->info = reg;
  318. e->k = VNONRELOC;
  319. }
  320. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  321. int reg;
  322. luaK_dischargevars(fs, e);
  323. freeexp(fs, e);
  324. reg = fs->freereg;
  325. luaK_reserveregs(fs, 1);
  326. luaK_exp2reg(fs, e, reg);
  327. }
  328. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  329. luaK_dischargevars(fs, e);
  330. if (e->k == VNONRELOC) {
  331. if (!hasjumps(e)) return e->info; /* exp is already in a register */
  332. if (e->info >= fs->nactloc) { /* reg. is not a local? */
  333. dischargejumps(fs, e, e->info); /* put value on it */
  334. return e->info;
  335. }
  336. }
  337. luaK_exp2nextreg(fs, e); /* default */
  338. return e->info;
  339. }
  340. void luaK_exp2val (FuncState *fs, expdesc *e) {
  341. if (hasjumps(e))
  342. luaK_exp2anyreg(fs, e);
  343. else
  344. luaK_dischargevars(fs, e);
  345. }
  346. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  347. luaK_exp2val(fs, e);
  348. switch (e->k) {
  349. case VNIL: {
  350. if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
  351. e->info = nil_constant(fs);
  352. e->k = VK;
  353. return e->info + MAXSTACK;
  354. }
  355. else break;
  356. }
  357. case VK: {
  358. if (e->info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
  359. return e->info + MAXSTACK;
  360. else break;
  361. }
  362. default: break;
  363. }
  364. /* not a constant in the right range: put in a register */
  365. return luaK_exp2anyreg(fs, e);
  366. }
  367. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  368. switch (var->k) {
  369. case VLOCAL: {
  370. freeexp(fs, exp);
  371. luaK_exp2reg(fs, exp, var->info);
  372. break;
  373. }
  374. case VUPVAL: {
  375. int e = luaK_exp2anyreg(fs, exp);
  376. freereg(fs, e);
  377. luaK_codeABC(fs, OP_SETUPVAL, e, var->info, 0);
  378. break;
  379. }
  380. case VGLOBAL: {
  381. int e = luaK_exp2anyreg(fs, exp);
  382. freereg(fs, e);
  383. luaK_codeABc(fs, OP_SETGLOBAL, e, var->info);
  384. break;
  385. }
  386. case VINDEXED: {
  387. int e = luaK_exp2anyreg(fs, exp);
  388. freereg(fs, e);
  389. luaK_codeABC(fs, OP_SETTABLE, e, var->info, var->aux);
  390. break;
  391. }
  392. default: {
  393. lua_assert(0); /* invalid var kind to store */
  394. break;
  395. }
  396. }
  397. }
  398. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  399. int func;
  400. luaK_exp2anyreg(fs, e);
  401. freeexp(fs, e);
  402. func = fs->freereg;
  403. luaK_reserveregs(fs, 2);
  404. luaK_codeABC(fs, OP_SELF, func, e->info, luaK_exp2RK(fs, key));
  405. freeexp(fs, key);
  406. e->info = func;
  407. e->k = VNONRELOC;
  408. }
  409. static OpCode invertoperator (OpCode op) {
  410. switch (op) {
  411. case OP_TESTNE: return OP_TESTEQ;
  412. case OP_TESTEQ: return OP_TESTNE;
  413. case OP_TESTLT: return OP_TESTGE;
  414. case OP_TESTLE: return OP_TESTGT;
  415. case OP_TESTGT: return OP_TESTLE;
  416. case OP_TESTGE: return OP_TESTLT;
  417. case OP_TESTT: return OP_TESTF;
  418. case OP_TESTF: return OP_TESTT;
  419. default: lua_assert(0); return op; /* invalid jump instruction */
  420. }
  421. }
  422. static void invertjump (FuncState *fs, expdesc *e) {
  423. Instruction *pc = getjumpcontrol(fs, e->info);
  424. *pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  425. }
  426. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  427. if (e->k == VRELOCABLE) {
  428. Instruction ie = getcode(fs, e);
  429. if (GET_OPCODE(ie) == OP_NOT) {
  430. op = invertoperator(op);
  431. fs->pc--; /* remove previous OP_NOT */
  432. return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
  433. }
  434. /* else go through */
  435. }
  436. discharge2anyreg(fs, e);
  437. freeexp(fs, e);
  438. return luaK_condjump(fs, op, NO_REG, e->info, 0);
  439. }
  440. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  441. int pc; /* pc of last jump */
  442. luaK_dischargevars(fs, e);
  443. switch (e->k) {
  444. case VK: case VTRUE: {
  445. pc = NO_JUMP; /* always true; do nothing */
  446. break;
  447. }
  448. case VFALSE: {
  449. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  450. break;
  451. }
  452. case VJMP: {
  453. invertjump(fs, e);
  454. pc = e->info;
  455. break;
  456. }
  457. default: {
  458. pc = jumponcond(fs, e, OP_TESTF);
  459. break;
  460. }
  461. }
  462. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  463. luaK_patchlist(fs, e->t, luaK_getlabel(fs));
  464. e->t = NO_JUMP;
  465. }
  466. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  467. int pc; /* pc of last jump */
  468. luaK_dischargevars(fs, e);
  469. switch (e->k) {
  470. case VNIL: case VFALSE: {
  471. pc = NO_JUMP; /* always false; do nothing */
  472. break;
  473. }
  474. case VTRUE: {
  475. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  476. break;
  477. }
  478. case VJMP: {
  479. pc = e->info;
  480. break;
  481. }
  482. default: {
  483. pc = jumponcond(fs, e, OP_TESTT);
  484. break;
  485. }
  486. }
  487. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  488. luaK_patchlist(fs, e->f, luaK_getlabel(fs));
  489. e->f = NO_JUMP;
  490. }
  491. static void codenot (FuncState *fs, expdesc *e) {
  492. luaK_dischargevars(fs, e);
  493. switch (e->k) {
  494. case VNIL: case VFALSE: {
  495. e->k = VTRUE;
  496. break;
  497. }
  498. case VK: case VTRUE: {
  499. e->k = VFALSE;
  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->info = luaK_codeABC(fs, OP_NOT, 0, e->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->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 == VK && ttype(&fs->f->k[e->info]) == LUA_TNUMBER)
  530. e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info]));
  531. else {
  532. luaK_exp2anyreg(fs, e);
  533. freeexp(fs, e);
  534. e->info = luaK_codeABC(fs, OP_UNM, 0, e->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->info = e2->info; e1->aux = e2->aux; 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->info = e2->info; e1->aux = e2->aux; 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->info == GETARG_B(getcode(fs, e2))-1);
  601. freeexp(fs, e1);
  602. SETARG_B(getcode(fs, e2), e1->info);
  603. e1->k = e2->k; e1->info = e2->info;
  604. }
  605. else {
  606. luaK_exp2nextreg(fs, e2);
  607. freeexp(fs, e2);
  608. freeexp(fs, e1);
  609. e1->info = luaK_codeABC(fs, codes[op], 0, e1->info, e2->info);
  610. e1->k = VRELOCABLE;
  611. }
  612. break;
  613. }
  614. default: {
  615. int o1, o2;
  616. OpCode opc;
  617. if (e1->k != VK) { /* not a constant operator? */
  618. o1 = e1->info;
  619. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  620. opc = codes[op];
  621. }
  622. else { /* invert operands */
  623. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  624. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  625. opc = invcodes[op]; /* use inverted operator */
  626. }
  627. freeexp(fs, e2);
  628. freeexp(fs, e1);
  629. if (op < OPR_NE) { /* ORDER OPR */
  630. e1->info = luaK_codeABC(fs, opc, 0, o1, o2);
  631. e1->k = VRELOCABLE;
  632. }
  633. else { /* jump */
  634. e1->info = luaK_condjump(fs, opc, o1, 0, o2);
  635. e1->k = VJMP;
  636. }
  637. }
  638. }
  639. }
  640. static void codelineinfo (FuncState *fs) {
  641. Proto *f = fs->f;
  642. LexState *ls = fs->ls;
  643. if (ls->lastline > fs->lastline) {
  644. if (ls->lastline > fs->lastline+1) {
  645. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  646. MAX_INT, "line info overflow");
  647. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  648. }
  649. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  650. MAX_INT, "line info overflow");
  651. f->lineinfo[fs->nlineinfo++] = fs->pc;
  652. fs->lastline = ls->lastline;
  653. }
  654. }
  655. static int luaK_code (FuncState *fs, Instruction i) {
  656. Proto *f;
  657. codelineinfo(fs);
  658. f = fs->f;
  659. /* put new instruction in code array */
  660. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  661. MAX_INT, "code size overflow");
  662. f->code[fs->pc] = i;
  663. return fs->pc++;
  664. }
  665. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  666. lua_assert(getOpMode(o) == iABC);
  667. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  668. }
  669. int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
  670. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  671. return luaK_code(fs, CREATE_ABc(o, a, bc));
  672. }