lcode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. ** $Id: lcode.c,v 2.25.1.5 2011/01/31 14:53:16 roberto Exp $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define lcode_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lcode.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lgc.h"
  14. #include "llex.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lopcodes.h"
  18. #include "lparser.h"
  19. #include "ltable.h"
  20. #define hasjumps(e) ((e)->t != (e)->f)
  21. static int isnumeral(expdesc *e) {
  22. return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
  23. }
  24. void luaK_nil (FuncState *fs, int from, int n) {
  25. Instruction *previous;
  26. if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
  27. if (fs->pc == 0) { /* function start? */
  28. if (from >= fs->nactvar)
  29. return; /* positions are already clean */
  30. }
  31. else {
  32. previous = &fs->f->code[fs->pc-1];
  33. if (GET_OPCODE(*previous) == OP_LOADNIL) {
  34. int pfrom = GETARG_A(*previous);
  35. int pto = GETARG_B(*previous);
  36. if (pfrom <= from && from <= pto+1) { /* can connect both? */
  37. if (from+n-1 > pto)
  38. SETARG_B(*previous, from+n-1);
  39. return;
  40. }
  41. }
  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 jpc = fs->jpc; /* save list of jumps to here */
  48. int j;
  49. fs->jpc = NO_JUMP;
  50. j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
  51. luaK_concat(fs, &j, jpc); /* keep them on hold */
  52. return j;
  53. }
  54. void luaK_ret (FuncState *fs, int first, int nret) {
  55. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  56. }
  57. static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
  58. luaK_codeABC(fs, op, A, B, C);
  59. return luaK_jump(fs);
  60. }
  61. static void fixjump (FuncState *fs, int pc, int dest) {
  62. Instruction *jmp = &fs->f->code[pc];
  63. int offset = dest-(pc+1);
  64. lua_assert(dest != NO_JUMP);
  65. if (abs(offset) > MAXARG_sBx)
  66. luaX_syntaxerror(fs->ls, "control structure too long");
  67. SETARG_sBx(*jmp, offset);
  68. }
  69. /*
  70. ** returns current `pc' and marks it as a jump target (to avoid wrong
  71. ** optimizations with consecutive instructions not in the same basic block).
  72. */
  73. int luaK_getlabel (FuncState *fs) {
  74. fs->lasttarget = fs->pc;
  75. return fs->pc;
  76. }
  77. static int getjump (FuncState *fs, int pc) {
  78. int offset = GETARG_sBx(fs->f->code[pc]);
  79. if (offset == NO_JUMP) /* point to itself represents end of list */
  80. return NO_JUMP; /* end of list */
  81. else
  82. return (pc+1)+offset; /* turn offset into absolute position */
  83. }
  84. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  85. Instruction *pi = &fs->f->code[pc];
  86. if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
  87. return pi-1;
  88. else
  89. return pi;
  90. }
  91. /*
  92. ** check whether list has any jump that do not produce a value
  93. ** (or produce an inverted value)
  94. */
  95. static int need_value (FuncState *fs, int list) {
  96. for (; list != NO_JUMP; list = getjump(fs, list)) {
  97. Instruction i = *getjumpcontrol(fs, list);
  98. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  99. }
  100. return 0; /* not found */
  101. }
  102. static int patchtestreg (FuncState *fs, int node, int reg) {
  103. Instruction *i = getjumpcontrol(fs, node);
  104. if (GET_OPCODE(*i) != OP_TESTSET)
  105. return 0; /* cannot patch other instructions */
  106. if (reg != NO_REG && reg != GETARG_B(*i))
  107. SETARG_A(*i, reg);
  108. else /* no register to put value or register already has the value */
  109. *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
  110. return 1;
  111. }
  112. static void removevalues (FuncState *fs, int list) {
  113. for (; list != NO_JUMP; list = getjump(fs, list))
  114. patchtestreg(fs, list, NO_REG);
  115. }
  116. static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
  117. int dtarget) {
  118. while (list != NO_JUMP) {
  119. int next = getjump(fs, list);
  120. if (patchtestreg(fs, list, reg))
  121. fixjump(fs, list, vtarget);
  122. else
  123. fixjump(fs, list, dtarget); /* jump to default target */
  124. list = next;
  125. }
  126. }
  127. static void dischargejpc (FuncState *fs) {
  128. patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
  129. fs->jpc = NO_JUMP;
  130. }
  131. void luaK_patchlist (FuncState *fs, int list, int target) {
  132. if (target == fs->pc)
  133. luaK_patchtohere(fs, list);
  134. else {
  135. lua_assert(target < fs->pc);
  136. patchlistaux(fs, list, target, NO_REG, target);
  137. }
  138. }
  139. void luaK_patchtohere (FuncState *fs, int list) {
  140. luaK_getlabel(fs);
  141. luaK_concat(fs, &fs->jpc, list);
  142. }
  143. void luaK_concat (FuncState *fs, int *l1, int l2) {
  144. if (l2 == NO_JUMP) return;
  145. else if (*l1 == NO_JUMP)
  146. *l1 = l2;
  147. else {
  148. int list = *l1;
  149. int next;
  150. while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
  151. list = next;
  152. fixjump(fs, list, l2);
  153. }
  154. }
  155. void luaK_checkstack (FuncState *fs, int n) {
  156. int newstack = fs->freereg + n;
  157. if (newstack > fs->f->maxstacksize) {
  158. if (newstack >= MAXSTACK)
  159. luaX_syntaxerror(fs->ls, "function or expression too complex");
  160. fs->f->maxstacksize = cast_byte(newstack);
  161. }
  162. }
  163. void luaK_reserveregs (FuncState *fs, int n) {
  164. luaK_checkstack(fs, n);
  165. fs->freereg += n;
  166. }
  167. static void freereg (FuncState *fs, int reg) {
  168. if (!ISK(reg) && reg >= fs->nactvar) {
  169. fs->freereg--;
  170. lua_assert(reg == fs->freereg);
  171. }
  172. }
  173. static void freeexp (FuncState *fs, expdesc *e) {
  174. if (e->k == VNONRELOC)
  175. freereg(fs, e->u.s.info);
  176. }
  177. static int addk (FuncState *fs, TValue *k, TValue *v) {
  178. lua_State *L = fs->L;
  179. TValue *idx = luaH_set(L, fs->h, k);
  180. Proto *f = fs->f;
  181. int oldsize = f->sizek;
  182. if (ttisnumber(idx)) {
  183. lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
  184. return cast_int(nvalue(idx));
  185. }
  186. else { /* constant not found; create a new entry */
  187. setnvalue(idx, cast_num(fs->nk));
  188. luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
  189. MAXARG_Bx, "constant table overflow");
  190. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  191. setobj(L, &f->k[fs->nk], v);
  192. luaC_barrier(L, f, v);
  193. return fs->nk++;
  194. }
  195. }
  196. int luaK_stringK (FuncState *fs, TString *s) {
  197. TValue o;
  198. setsvalue(fs->L, &o, s);
  199. return addk(fs, &o, &o);
  200. }
  201. int luaK_numberK (FuncState *fs, lua_Number r) {
  202. TValue o;
  203. setnvalue(&o, r);
  204. return addk(fs, &o, &o);
  205. }
  206. static int boolK (FuncState *fs, int b) {
  207. TValue o;
  208. setbvalue(&o, b);
  209. return addk(fs, &o, &o);
  210. }
  211. static int nilK (FuncState *fs) {
  212. TValue k, v;
  213. setnilvalue(&v);
  214. /* cannot use nil as key; instead use table itself to represent nil */
  215. sethvalue(fs->L, &k, fs->h);
  216. return addk(fs, &k, &v);
  217. }
  218. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  219. if (e->k == VCALL) { /* expression is an open function call? */
  220. SETARG_C(getcode(fs, e), nresults+1);
  221. }
  222. else if (e->k == VVARARG) {
  223. SETARG_B(getcode(fs, e), nresults+1);
  224. SETARG_A(getcode(fs, e), fs->freereg);
  225. luaK_reserveregs(fs, 1);
  226. }
  227. }
  228. void luaK_setoneret (FuncState *fs, expdesc *e) {
  229. if (e->k == VCALL) { /* expression is an open function call? */
  230. e->k = VNONRELOC;
  231. e->u.s.info = GETARG_A(getcode(fs, e));
  232. }
  233. else if (e->k == VVARARG) {
  234. SETARG_B(getcode(fs, e), 2);
  235. e->k = VRELOCABLE; /* can relocate its simple result */
  236. }
  237. }
  238. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  239. switch (e->k) {
  240. case VLOCAL: {
  241. e->k = VNONRELOC;
  242. break;
  243. }
  244. case VUPVAL: {
  245. e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);
  246. e->k = VRELOCABLE;
  247. break;
  248. }
  249. case VGLOBAL: {
  250. e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
  251. e->k = VRELOCABLE;
  252. break;
  253. }
  254. case VINDEXED: {
  255. freereg(fs, e->u.s.aux);
  256. freereg(fs, e->u.s.info);
  257. e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);
  258. e->k = VRELOCABLE;
  259. break;
  260. }
  261. case VVARARG:
  262. case VCALL: {
  263. luaK_setoneret(fs, e);
  264. break;
  265. }
  266. default: break; /* there is one value available (somewhere) */
  267. }
  268. }
  269. static int code_label (FuncState *fs, int A, int b, int jump) {
  270. luaK_getlabel(fs); /* those instructions may be jump targets */
  271. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  272. }
  273. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  274. luaK_dischargevars(fs, e);
  275. switch (e->k) {
  276. case VNIL: {
  277. luaK_nil(fs, reg, 1);
  278. break;
  279. }
  280. case VFALSE: case VTRUE: {
  281. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  282. break;
  283. }
  284. case VK: {
  285. luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
  286. break;
  287. }
  288. case VKNUM: {
  289. luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
  290. break;
  291. }
  292. case VRELOCABLE: {
  293. Instruction *pc = &getcode(fs, e);
  294. SETARG_A(*pc, reg);
  295. break;
  296. }
  297. case VNONRELOC: {
  298. if (reg != e->u.s.info)
  299. luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
  300. break;
  301. }
  302. default: {
  303. lua_assert(e->k == VVOID || e->k == VJMP);
  304. return; /* nothing to do... */
  305. }
  306. }
  307. e->u.s.info = reg;
  308. e->k = VNONRELOC;
  309. }
  310. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  311. if (e->k != VNONRELOC) {
  312. luaK_reserveregs(fs, 1);
  313. discharge2reg(fs, e, fs->freereg-1);
  314. }
  315. }
  316. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  317. discharge2reg(fs, e, reg);
  318. if (e->k == VJMP)
  319. luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */
  320. if (hasjumps(e)) {
  321. int final; /* position after whole expression */
  322. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  323. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  324. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  325. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  326. p_f = code_label(fs, reg, 0, 1);
  327. p_t = code_label(fs, reg, 1, 0);
  328. luaK_patchtohere(fs, fj);
  329. }
  330. final = luaK_getlabel(fs);
  331. patchlistaux(fs, e->f, final, reg, p_f);
  332. patchlistaux(fs, e->t, final, reg, p_t);
  333. }
  334. e->f = e->t = NO_JUMP;
  335. e->u.s.info = reg;
  336. e->k = VNONRELOC;
  337. }
  338. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  339. luaK_dischargevars(fs, e);
  340. freeexp(fs, e);
  341. luaK_reserveregs(fs, 1);
  342. exp2reg(fs, e, fs->freereg - 1);
  343. }
  344. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  345. luaK_dischargevars(fs, e);
  346. if (e->k == VNONRELOC) {
  347. if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */
  348. if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */
  349. exp2reg(fs, e, e->u.s.info); /* put value on it */
  350. return e->u.s.info;
  351. }
  352. }
  353. luaK_exp2nextreg(fs, e); /* default */
  354. return e->u.s.info;
  355. }
  356. void luaK_exp2val (FuncState *fs, expdesc *e) {
  357. if (hasjumps(e))
  358. luaK_exp2anyreg(fs, e);
  359. else
  360. luaK_dischargevars(fs, e);
  361. }
  362. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  363. luaK_exp2val(fs, e);
  364. switch (e->k) {
  365. case VKNUM:
  366. case VTRUE:
  367. case VFALSE:
  368. case VNIL: {
  369. if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
  370. e->u.s.info = (e->k == VNIL) ? nilK(fs) :
  371. (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
  372. boolK(fs, (e->k == VTRUE));
  373. e->k = VK;
  374. return RKASK(e->u.s.info);
  375. }
  376. else break;
  377. }
  378. case VK: {
  379. if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */
  380. return RKASK(e->u.s.info);
  381. else break;
  382. }
  383. default: break;
  384. }
  385. /* not a constant in the right range: put it in a register */
  386. return luaK_exp2anyreg(fs, e);
  387. }
  388. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  389. switch (var->k) {
  390. case VLOCAL: {
  391. freeexp(fs, ex);
  392. exp2reg(fs, ex, var->u.s.info);
  393. return;
  394. }
  395. case VUPVAL: {
  396. int e = luaK_exp2anyreg(fs, ex);
  397. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
  398. break;
  399. }
  400. case VGLOBAL: {
  401. int e = luaK_exp2anyreg(fs, ex);
  402. luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
  403. break;
  404. }
  405. case VINDEXED: {
  406. int e = luaK_exp2RK(fs, ex);
  407. luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
  408. break;
  409. }
  410. default: {
  411. lua_assert(0); /* invalid var kind to store */
  412. break;
  413. }
  414. }
  415. freeexp(fs, ex);
  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.s.info, luaK_exp2RK(fs, key));
  424. freeexp(fs, key);
  425. e->u.s.info = func;
  426. e->k = VNONRELOC;
  427. }
  428. static void invertjump (FuncState *fs, expdesc *e) {
  429. Instruction *pc = getjumpcontrol(fs, e->u.s.info);
  430. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  431. GET_OPCODE(*pc) != OP_TEST);
  432. SETARG_A(*pc, !(GETARG_A(*pc)));
  433. }
  434. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  435. if (e->k == VRELOCABLE) {
  436. Instruction ie = getcode(fs, e);
  437. if (GET_OPCODE(ie) == OP_NOT) {
  438. fs->pc--; /* remove previous OP_NOT */
  439. return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
  440. }
  441. /* else go through */
  442. }
  443. discharge2anyreg(fs, e);
  444. freeexp(fs, e);
  445. return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
  446. }
  447. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  448. int pc; /* pc of last jump */
  449. luaK_dischargevars(fs, e);
  450. switch (e->k) {
  451. case VK: case VKNUM: case VTRUE: {
  452. pc = NO_JUMP; /* always true; do nothing */
  453. break;
  454. }
  455. case VJMP: {
  456. invertjump(fs, e);
  457. pc = e->u.s.info;
  458. break;
  459. }
  460. default: {
  461. pc = jumponcond(fs, e, 0);
  462. break;
  463. }
  464. }
  465. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  466. luaK_patchtohere(fs, e->t);
  467. e->t = NO_JUMP;
  468. }
  469. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  470. int pc; /* pc of last jump */
  471. luaK_dischargevars(fs, e);
  472. switch (e->k) {
  473. case VNIL: case VFALSE: {
  474. pc = NO_JUMP; /* always false; do nothing */
  475. break;
  476. }
  477. case VJMP: {
  478. pc = e->u.s.info;
  479. break;
  480. }
  481. default: {
  482. pc = jumponcond(fs, e, 1);
  483. break;
  484. }
  485. }
  486. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  487. luaK_patchtohere(fs, e->f);
  488. e->f = NO_JUMP;
  489. }
  490. static void codenot (FuncState *fs, expdesc *e) {
  491. luaK_dischargevars(fs, e);
  492. switch (e->k) {
  493. case VNIL: case VFALSE: {
  494. e->k = VTRUE;
  495. break;
  496. }
  497. case VK: case VKNUM: case VTRUE: {
  498. e->k = VFALSE;
  499. break;
  500. }
  501. case VJMP: {
  502. invertjump(fs, e);
  503. break;
  504. }
  505. case VRELOCABLE:
  506. case VNONRELOC: {
  507. discharge2anyreg(fs, e);
  508. freeexp(fs, e);
  509. e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);
  510. e->k = VRELOCABLE;
  511. break;
  512. }
  513. default: {
  514. lua_assert(0); /* cannot happen */
  515. break;
  516. }
  517. }
  518. /* interchange true and false lists */
  519. { int temp = e->f; e->f = e->t; e->t = temp; }
  520. removevalues(fs, e->f);
  521. removevalues(fs, e->t);
  522. }
  523. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  524. t->u.s.aux = luaK_exp2RK(fs, k);
  525. t->k = VINDEXED;
  526. }
  527. static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
  528. lua_Number v1, v2, r;
  529. if (!isnumeral(e1) || !isnumeral(e2)) return 0;
  530. v1 = e1->u.nval;
  531. v2 = e2->u.nval;
  532. switch (op) {
  533. case OP_ADD: r = luai_numadd(v1, v2); break;
  534. case OP_SUB: r = luai_numsub(v1, v2); break;
  535. case OP_MUL: r = luai_nummul(v1, v2); break;
  536. case OP_DIV:
  537. if (v2 == 0) return 0; /* do not attempt to divide by 0 */
  538. r = luai_numdiv(v1, v2); break;
  539. case OP_MOD:
  540. if (v2 == 0) return 0; /* do not attempt to divide by 0 */
  541. r = luai_nummod(v1, v2); break;
  542. case OP_POW: r = luai_numpow(v1, v2); break;
  543. case OP_UNM: r = luai_numunm(v1); break;
  544. case OP_LEN: return 0; /* no constant folding for 'len' */
  545. default: lua_assert(0); r = 0; break;
  546. }
  547. if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
  548. e1->u.nval = r;
  549. return 1;
  550. }
  551. static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
  552. if (constfolding(op, e1, e2))
  553. return;
  554. else {
  555. int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
  556. int o1 = luaK_exp2RK(fs, e1);
  557. if (o1 > o2) {
  558. freeexp(fs, e1);
  559. freeexp(fs, e2);
  560. }
  561. else {
  562. freeexp(fs, e2);
  563. freeexp(fs, e1);
  564. }
  565. e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
  566. e1->k = VRELOCABLE;
  567. }
  568. }
  569. static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
  570. expdesc *e2) {
  571. int o1 = luaK_exp2RK(fs, e1);
  572. int o2 = luaK_exp2RK(fs, e2);
  573. freeexp(fs, e2);
  574. freeexp(fs, e1);
  575. if (cond == 0 && op != OP_EQ) {
  576. int temp; /* exchange args to replace by `<' or `<=' */
  577. temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
  578. cond = 1;
  579. }
  580. e1->u.s.info = condjump(fs, op, cond, o1, o2);
  581. e1->k = VJMP;
  582. }
  583. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  584. expdesc e2;
  585. e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
  586. switch (op) {
  587. case OPR_MINUS: {
  588. if (!isnumeral(e))
  589. luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
  590. codearith(fs, OP_UNM, e, &e2);
  591. break;
  592. }
  593. case OPR_NOT: codenot(fs, e); break;
  594. case OPR_LEN: {
  595. luaK_exp2anyreg(fs, e); /* cannot operate on constants */
  596. codearith(fs, OP_LEN, e, &e2);
  597. break;
  598. }
  599. default: lua_assert(0);
  600. }
  601. }
  602. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  603. switch (op) {
  604. case OPR_AND: {
  605. luaK_goiftrue(fs, v);
  606. break;
  607. }
  608. case OPR_OR: {
  609. luaK_goiffalse(fs, v);
  610. break;
  611. }
  612. case OPR_CONCAT: {
  613. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  614. break;
  615. }
  616. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  617. case OPR_MOD: case OPR_POW: {
  618. if (!isnumeral(v)) luaK_exp2RK(fs, v);
  619. break;
  620. }
  621. default: {
  622. luaK_exp2RK(fs, v);
  623. break;
  624. }
  625. }
  626. }
  627. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  628. switch (op) {
  629. case OPR_AND: {
  630. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  631. luaK_dischargevars(fs, e2);
  632. luaK_concat(fs, &e2->f, e1->f);
  633. *e1 = *e2;
  634. break;
  635. }
  636. case OPR_OR: {
  637. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  638. luaK_dischargevars(fs, e2);
  639. luaK_concat(fs, &e2->t, e1->t);
  640. *e1 = *e2;
  641. break;
  642. }
  643. case OPR_CONCAT: {
  644. luaK_exp2val(fs, e2);
  645. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  646. lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
  647. freeexp(fs, e1);
  648. SETARG_B(getcode(fs, e2), e1->u.s.info);
  649. e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;
  650. }
  651. else {
  652. luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
  653. codearith(fs, OP_CONCAT, e1, e2);
  654. }
  655. break;
  656. }
  657. case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
  658. case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
  659. case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
  660. case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
  661. case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
  662. case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
  663. case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
  664. case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
  665. case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
  666. case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
  667. case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
  668. case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
  669. default: lua_assert(0);
  670. }
  671. }
  672. void luaK_fixline (FuncState *fs, int line) {
  673. fs->f->lineinfo[fs->pc - 1] = line;
  674. }
  675. static int luaK_code (FuncState *fs, Instruction i, int line) {
  676. Proto *f = fs->f;
  677. dischargejpc(fs); /* `pc' will change */
  678. /* put new instruction in code array */
  679. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  680. MAX_INT, "code size overflow");
  681. f->code[fs->pc] = i;
  682. /* save corresponding line information */
  683. luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
  684. MAX_INT, "code size overflow");
  685. f->lineinfo[fs->pc] = line;
  686. return fs->pc++;
  687. }
  688. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  689. lua_assert(getOpMode(o) == iABC);
  690. lua_assert(getBMode(o) != OpArgN || b == 0);
  691. lua_assert(getCMode(o) != OpArgN || c == 0);
  692. return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
  693. }
  694. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  695. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  696. lua_assert(getCMode(o) == OpArgN);
  697. return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
  698. }
  699. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  700. int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
  701. int b = (tostore == LUA_MULTRET) ? 0 : tostore;
  702. lua_assert(tostore != 0);
  703. if (c <= MAXARG_C)
  704. luaK_codeABC(fs, OP_SETLIST, base, b, c);
  705. else {
  706. luaK_codeABC(fs, OP_SETLIST, base, b, 0);
  707. luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
  708. }
  709. fs->freereg = base + 1; /* free registers with list values */
  710. }