lcode.c 21 KB

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