lcode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. SETARG_C(getcode(fs, e), nresults+1);
  214. if (nresults == 1) { /* `regular' expression? */
  215. e->k = VNONRELOC;
  216. e->u.i.info = GETARG_A(getcode(fs, e));
  217. }
  218. }
  219. }
  220. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  221. switch (e->k) {
  222. case VLOCAL: {
  223. e->k = VNONRELOC;
  224. break;
  225. }
  226. case VUPVAL: {
  227. e->u.i.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.i.info, 0);
  228. e->k = VRELOCABLE;
  229. break;
  230. }
  231. case VGLOBAL: {
  232. e->u.i.info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->u.i.info);
  233. e->k = VRELOCABLE;
  234. break;
  235. }
  236. case VINDEXED: {
  237. freereg(fs, e->u.i.aux);
  238. freereg(fs, e->u.i.info);
  239. e->u.i.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.i.info, e->u.i.aux);
  240. e->k = VRELOCABLE;
  241. break;
  242. }
  243. case VCALL: {
  244. luaK_setcallreturns(fs, e, 1);
  245. break;
  246. }
  247. default: break; /* there is one value available (somewhere) */
  248. }
  249. }
  250. static int code_label (FuncState *fs, int A, int b, int jump) {
  251. luaK_getlabel(fs); /* those instructions may be jump targets */
  252. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  253. }
  254. static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
  255. if (hasjumps(e)) {
  256. int final; /* position after whole expression */
  257. int p_f = NO_JUMP; /* position of an eventual PUSH false */
  258. int p_t = NO_JUMP; /* position of an eventual PUSH true */
  259. if (need_value(fs, e->f, OP_TESTF) || need_value(fs, e->t, OP_TESTT)) {
  260. /* expression needs values */
  261. if (e->k != VJMP) {
  262. luaK_getlabel(fs); /* these instruction may be jump target */
  263. luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  264. }
  265. p_f = code_label(fs, reg, 0, 1);
  266. p_t = code_label(fs, reg, 1, 0);
  267. }
  268. final = luaK_getlabel(fs);
  269. luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
  270. luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
  271. }
  272. e->f = e->t = NO_JUMP;
  273. }
  274. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  275. luaK_dischargevars(fs, e);
  276. switch (e->k) {
  277. case VNIL: {
  278. luaK_nil(fs, reg, 1);
  279. break;
  280. }
  281. case VFALSE: case VTRUE: {
  282. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  283. break;
  284. }
  285. case VNUMBER: {
  286. lua_Number f = e->u.n;
  287. int i = cast(int, f);
  288. if ((lua_Number)i == f && -MAXARG_sBc <= i && i <= MAXARG_sBc)
  289. luaK_codeAsBc(fs, OP_LOADINT, reg, i); /* f has a small int value */
  290. else
  291. luaK_codeABc(fs, OP_LOADK, reg, number_constant(fs, f));
  292. break;
  293. }
  294. case VK: {
  295. luaK_codeABc(fs, OP_LOADK, reg, e->u.i.info);
  296. break;
  297. }
  298. case VRELOCABLE: {
  299. Instruction *pc = &getcode(fs, e);
  300. SETARG_A(*pc, reg);
  301. break;
  302. }
  303. default: return;
  304. }
  305. e->u.i.info = reg;
  306. e->k = VNONRELOC;
  307. }
  308. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  309. if (e->k != VNONRELOC) {
  310. luaK_reserveregs(fs, 1);
  311. discharge2reg(fs, e, fs->freereg-1);
  312. }
  313. }
  314. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  315. discharge2reg(fs, e, reg);
  316. switch (e->k) {
  317. case VVOID: {
  318. return; /* nothing to do... */
  319. }
  320. case VNONRELOC: {
  321. if (reg != e->u.i.info)
  322. luaK_codeABC(fs, OP_MOVE, reg, e->u.i.info, 0);
  323. break;
  324. }
  325. case VJMP: {
  326. luaK_concat(fs, &e->t, e->u.i.info); /* put this jump in `t' list */
  327. break;
  328. }
  329. default: {
  330. lua_assert(0); /* cannot happen */
  331. break;
  332. }
  333. }
  334. dischargejumps(fs, e, reg);
  335. e->u.i.info = reg;
  336. e->k = VNONRELOC;
  337. }
  338. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  339. int reg;
  340. luaK_dischargevars(fs, e);
  341. freeexp(fs, e);
  342. reg = fs->freereg;
  343. luaK_reserveregs(fs, 1);
  344. luaK_exp2reg(fs, e, reg);
  345. }
  346. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  347. luaK_dischargevars(fs, e);
  348. if (e->k == VNONRELOC) {
  349. if (!hasjumps(e)) return e->u.i.info; /* exp is already in a register */
  350. if (e->u.i.info >= fs->nactloc) { /* reg. is not a local? */
  351. dischargejumps(fs, e, e->u.i.info); /* put value on it */
  352. return e->u.i.info;
  353. }
  354. }
  355. luaK_exp2nextreg(fs, e); /* default */
  356. return e->u.i.info;
  357. }
  358. void luaK_exp2val (FuncState *fs, expdesc *e) {
  359. if (hasjumps(e))
  360. luaK_exp2anyreg(fs, e);
  361. else
  362. luaK_dischargevars(fs, e);
  363. }
  364. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  365. luaK_exp2val(fs, e);
  366. switch (e->k) {
  367. case VNUMBER: case VNIL: {
  368. if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
  369. e->u.i.info = (e->k == VNIL) ? nil_constant(fs) :
  370. number_constant(fs, e->u.n);
  371. e->k = VK;
  372. return e->u.i.info + MAXSTACK;
  373. }
  374. else break;
  375. }
  376. case VK: {
  377. if (e->u.i.info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
  378. return e->u.i.info + MAXSTACK;
  379. else break;
  380. }
  381. default: break;
  382. }
  383. /* not a constant in the right range: put in a register */
  384. return luaK_exp2anyreg(fs, e);
  385. }
  386. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  387. switch (var->k) {
  388. case VLOCAL: {
  389. freeexp(fs, exp);
  390. luaK_exp2reg(fs, exp, var->u.i.info);
  391. break;
  392. }
  393. case VUPVAL: {
  394. int e = luaK_exp2anyreg(fs, exp);
  395. freereg(fs, e);
  396. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.i.info, 0);
  397. break;
  398. }
  399. case VGLOBAL: {
  400. int e = luaK_exp2anyreg(fs, exp);
  401. freereg(fs, e);
  402. luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
  403. break;
  404. }
  405. case VINDEXED: {
  406. int e = luaK_exp2anyreg(fs, exp);
  407. freereg(fs, e);
  408. luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
  409. break;
  410. }
  411. default: {
  412. lua_assert(0); /* invalid var kind to store */
  413. break;
  414. }
  415. }
  416. }
  417. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  418. int func;
  419. luaK_exp2anyreg(fs, e);
  420. freeexp(fs, e);
  421. func = fs->freereg;
  422. luaK_reserveregs(fs, 2);
  423. luaK_codeABC(fs, OP_SELF, func, e->u.i.info, luaK_exp2RK(fs, key));
  424. freeexp(fs, key);
  425. e->u.i.info = func;
  426. e->k = VNONRELOC;
  427. }
  428. static OpCode invertoperator (OpCode op) {
  429. switch (op) {
  430. case OP_TESTNE: return OP_TESTEQ;
  431. case OP_TESTEQ: return OP_TESTNE;
  432. case OP_TESTLT: return OP_TESTGE;
  433. case OP_TESTLE: return OP_TESTGT;
  434. case OP_TESTGT: return OP_TESTLE;
  435. case OP_TESTGE: return OP_TESTLT;
  436. case OP_TESTT: return OP_TESTF;
  437. case OP_TESTF: return OP_TESTT;
  438. default: lua_assert(0); return op; /* invalid jump instruction */
  439. }
  440. }
  441. static void invertjump (FuncState *fs, expdesc *e) {
  442. Instruction *pc = getjumpcontrol(fs, e->u.i.info);
  443. *pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  444. }
  445. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  446. if (e->k == VRELOCABLE) {
  447. Instruction ie = getcode(fs, e);
  448. if (GET_OPCODE(ie) == OP_NOT) {
  449. op = invertoperator(op);
  450. fs->pc--; /* remove previous OP_NOT */
  451. return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
  452. }
  453. /* else go through */
  454. }
  455. discharge2anyreg(fs, e);
  456. freeexp(fs, e);
  457. return luaK_condjump(fs, op, NO_REG, e->u.i.info, 0);
  458. }
  459. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  460. int pc; /* pc of last jump */
  461. luaK_dischargevars(fs, e);
  462. switch (e->k) {
  463. case VK: case VNUMBER: case VTRUE: {
  464. pc = NO_JUMP; /* always true; do nothing */
  465. break;
  466. }
  467. case VFALSE: {
  468. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  469. break;
  470. }
  471. case VJMP: {
  472. invertjump(fs, e);
  473. pc = e->u.i.info;
  474. break;
  475. }
  476. default: {
  477. pc = jumponcond(fs, e, OP_TESTF);
  478. break;
  479. }
  480. }
  481. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  482. luaK_patchlist(fs, e->t, luaK_getlabel(fs));
  483. e->t = NO_JUMP;
  484. }
  485. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  486. int pc; /* pc of last jump */
  487. luaK_dischargevars(fs, e);
  488. switch (e->k) {
  489. case VNIL: case VFALSE: {
  490. pc = NO_JUMP; /* always false; do nothing */
  491. break;
  492. }
  493. case VTRUE: {
  494. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  495. break;
  496. }
  497. case VJMP: {
  498. pc = e->u.i.info;
  499. break;
  500. }
  501. default: {
  502. pc = jumponcond(fs, e, OP_TESTT);
  503. break;
  504. }
  505. }
  506. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  507. luaK_patchlist(fs, e->f, luaK_getlabel(fs));
  508. e->f = NO_JUMP;
  509. }
  510. static void codenot (FuncState *fs, expdesc *e) {
  511. luaK_dischargevars(fs, e);
  512. switch (e->k) {
  513. case VNIL: case VFALSE: {
  514. e->k = VTRUE;
  515. break;
  516. }
  517. case VK: case VNUMBER: case VTRUE: {
  518. e->k = VFALSE;
  519. break;
  520. }
  521. case VJMP: {
  522. invertjump(fs, e);
  523. break;
  524. }
  525. case VRELOCABLE:
  526. case VNONRELOC: {
  527. discharge2anyreg(fs, e);
  528. freeexp(fs, e);
  529. e->u.i.info = luaK_codeABC(fs, OP_NOT, 0, e->u.i.info, 0);
  530. e->k = VRELOCABLE;
  531. break;
  532. }
  533. default: {
  534. lua_assert(0); /* cannot happen */
  535. break;
  536. }
  537. }
  538. /* interchange true and false lists */
  539. { int temp = e->f; e->f = e->t; e->t = temp; }
  540. }
  541. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  542. t->u.i.aux = luaK_exp2RK(fs, k);
  543. t->k = VINDEXED;
  544. }
  545. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  546. if (op == OPR_MINUS) {
  547. luaK_exp2val(fs, e);
  548. if (e->k == VNUMBER)
  549. e->u.n = -e->u.n;
  550. else {
  551. luaK_exp2anyreg(fs, e);
  552. freeexp(fs, e);
  553. e->u.i.info = luaK_codeABC(fs, OP_UNM, 0, e->u.i.info, 0);
  554. e->k = VRELOCABLE;
  555. }
  556. }
  557. else /* op == NOT */
  558. codenot(fs, e);
  559. }
  560. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  561. switch (op) {
  562. case OPR_AND: {
  563. luaK_goiftrue(fs, v);
  564. break;
  565. }
  566. case OPR_OR: {
  567. luaK_goiffalse(fs, v);
  568. break;
  569. }
  570. case OPR_CONCAT: {
  571. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  572. break;
  573. }
  574. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  575. /* non-comutative operators */
  576. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  577. break;
  578. }
  579. default: {
  580. luaK_exp2RK(fs, v);
  581. break;
  582. }
  583. }
  584. }
  585. /* opcode for each binary operator */
  586. static const OpCode codes[] = { /* ORDER OPR */
  587. OP_ADD, OP_SUB, OP_MUL, OP_DIV,
  588. OP_POW, OP_CONCAT,
  589. OP_TESTNE, OP_TESTEQ,
  590. OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
  591. };
  592. /* `inverted' opcode for each binary operator */
  593. /* ( -1 means operator has no inverse) */
  594. static const OpCode invcodes[] = { /* ORDER OPR */
  595. OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
  596. (OpCode)-1, (OpCode)-1,
  597. OP_TESTNE, OP_TESTEQ,
  598. OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
  599. };
  600. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  601. switch (op) {
  602. case OPR_AND: {
  603. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  604. luaK_dischargevars(fs, e2);
  605. luaK_concat(fs, &e1->f, e2->f);
  606. e1->k = e2->k; e1->u = e2->u; e1->t = e2->t;
  607. break;
  608. }
  609. case OPR_OR: {
  610. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  611. luaK_dischargevars(fs, e2);
  612. luaK_concat(fs, &e1->t, e2->t);
  613. e1->k = e2->k; e1->u = e2->u; e1->f = e2->f;
  614. break;
  615. }
  616. case OPR_CONCAT: {
  617. luaK_exp2val(fs, e2);
  618. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  619. lua_assert(e1->u.i.info == GETARG_B(getcode(fs, e2))-1);
  620. freeexp(fs, e1);
  621. SETARG_B(getcode(fs, e2), e1->u.i.info);
  622. e1->k = e2->k; e1->u.i.info = e2->u.i.info;
  623. }
  624. else {
  625. luaK_exp2nextreg(fs, e2);
  626. freeexp(fs, e2);
  627. freeexp(fs, e1);
  628. e1->u.i.info = luaK_codeABC(fs, codes[op], 0, e1->u.i.info,
  629. e2->u.i.info);
  630. e1->k = VRELOCABLE;
  631. }
  632. break;
  633. }
  634. default: {
  635. int o1, o2;
  636. OpCode opc;
  637. if (e1->k != VK) { /* not a constant operator? */
  638. o1 = e1->u.i.info;
  639. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  640. opc = codes[op];
  641. }
  642. else { /* invert operands */
  643. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  644. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  645. opc = invcodes[op]; /* use inverted operator */
  646. }
  647. freeexp(fs, e2);
  648. freeexp(fs, e1);
  649. if (op < OPR_NE) { /* ORDER OPR */
  650. e1->u.i.info = luaK_codeABC(fs, opc, 0, o1, o2);
  651. e1->k = VRELOCABLE;
  652. }
  653. else { /* jump */
  654. e1->u.i.info = luaK_condjump(fs, opc, o1, 0, o2);
  655. e1->k = VJMP;
  656. }
  657. }
  658. }
  659. }
  660. static void codelineinfo (FuncState *fs) {
  661. Proto *f = fs->f;
  662. LexState *ls = fs->ls;
  663. if (ls->lastline > fs->lastline) {
  664. if (ls->lastline > fs->lastline+1) {
  665. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  666. MAX_INT, "line info overflow");
  667. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  668. }
  669. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  670. MAX_INT, "line info overflow");
  671. f->lineinfo[fs->nlineinfo++] = fs->pc;
  672. fs->lastline = ls->lastline;
  673. }
  674. }
  675. static int luaK_code (FuncState *fs, Instruction i) {
  676. Proto *f;
  677. codelineinfo(fs);
  678. f = fs->f;
  679. /* put new instruction in code array */
  680. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  681. MAX_INT, "code size overflow");
  682. f->code[fs->pc] = i;
  683. /*printf("free: %d ", fs->freereg); printopcode(f, fs->pc);*/
  684. return fs->pc++;
  685. }
  686. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  687. lua_assert(getOpMode(o) == iABC);
  688. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  689. }
  690. int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
  691. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  692. return luaK_code(fs, CREATE_ABc(o, a, bc));
  693. }