lcode.c 23 KB

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