lcode.c 20 KB

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