lcode.c 21 KB

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