lcode.c 23 KB

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