lcode.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. ** $Id: lcode.c,v 2.76 2013/12/18 18:44:42 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. /*
  245. ** Use scanner's table to cache position of constants in contant list
  246. ** and try to reuse constants
  247. */
  248. static int addk (FuncState *fs, TValue *key, TValue *v) {
  249. lua_State *L = fs->ls->L;
  250. Proto *f = fs->f;
  251. TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
  252. int k, oldsize;
  253. if (ttisinteger(idx)) { /* is there an index there? */
  254. k = ivalue(idx);
  255. /* correct value? (warning: must distinguish floats from integers!) */
  256. if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
  257. luaV_rawequalobj(&f->k[k], v))
  258. return k; /* reuse index */
  259. }
  260. /* constant not found; create a new entry */
  261. oldsize = f->sizek;
  262. k = fs->nk;
  263. /* numerical value does not need GC barrier;
  264. table has no metatable, so it does not need to invalidate cache */
  265. setivalue(idx, k);
  266. luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  267. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  268. setobj(L, &f->k[k], v);
  269. fs->nk++;
  270. luaC_barrier(L, f, v);
  271. return k;
  272. }
  273. int luaK_stringK (FuncState *fs, TString *s) {
  274. TValue o;
  275. setsvalue(fs->ls->L, &o, s);
  276. return addk(fs, &o, &o);
  277. }
  278. /*
  279. ** use userdata as key to avoid collision with float with same value;
  280. ** conversion to 'void*' used only for hash, no "precision" problems
  281. */
  282. int luaK_intK (FuncState *fs, lua_Integer n) {
  283. TValue k, o;
  284. setpvalue(&k, cast(void*, cast(size_t, n)));
  285. setivalue(&o, n);
  286. return addk(fs, &k, &o);
  287. }
  288. /*
  289. ** Both NaN and -0.0 should not go to the constant table, as they have
  290. ** problems with the hashing. (NaN is not ** a valid key,
  291. ** -0.0 collides with +0.0.)
  292. */
  293. static int luaK_numberK (FuncState *fs, lua_Number r) {
  294. TValue o;
  295. lua_assert(!luai_numisnan(NULL, r) && !isminuszero(r));
  296. setnvalue(&o, r);
  297. return addk(fs, &o, &o);
  298. }
  299. static int boolK (FuncState *fs, int b) {
  300. TValue o;
  301. setbvalue(&o, b);
  302. return addk(fs, &o, &o);
  303. }
  304. static int nilK (FuncState *fs) {
  305. TValue k, v;
  306. setnilvalue(&v);
  307. /* cannot use nil as key; instead use table itself to represent nil */
  308. sethvalue(fs->ls->L, &k, fs->ls->h);
  309. return addk(fs, &k, &v);
  310. }
  311. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  312. if (e->k == VCALL) { /* expression is an open function call? */
  313. SETARG_C(getcode(fs, e), nresults+1);
  314. }
  315. else if (e->k == VVARARG) {
  316. SETARG_B(getcode(fs, e), nresults+1);
  317. SETARG_A(getcode(fs, e), fs->freereg);
  318. luaK_reserveregs(fs, 1);
  319. }
  320. }
  321. void luaK_setoneret (FuncState *fs, expdesc *e) {
  322. if (e->k == VCALL) { /* expression is an open function call? */
  323. e->k = VNONRELOC;
  324. e->u.info = GETARG_A(getcode(fs, e));
  325. }
  326. else if (e->k == VVARARG) {
  327. SETARG_B(getcode(fs, e), 2);
  328. e->k = VRELOCABLE; /* can relocate its simple result */
  329. }
  330. }
  331. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  332. switch (e->k) {
  333. case VLOCAL: {
  334. e->k = VNONRELOC;
  335. break;
  336. }
  337. case VUPVAL: {
  338. e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
  339. e->k = VRELOCABLE;
  340. break;
  341. }
  342. case VINDEXED: {
  343. OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
  344. freereg(fs, e->u.ind.idx);
  345. if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
  346. freereg(fs, e->u.ind.t);
  347. op = OP_GETTABLE;
  348. }
  349. e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
  350. e->k = VRELOCABLE;
  351. break;
  352. }
  353. case VVARARG:
  354. case VCALL: {
  355. luaK_setoneret(fs, e);
  356. break;
  357. }
  358. default: break; /* there is one value available (somewhere) */
  359. }
  360. }
  361. static int code_label (FuncState *fs, int A, int b, int jump) {
  362. luaK_getlabel(fs); /* those instructions may be jump targets */
  363. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  364. }
  365. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  366. luaK_dischargevars(fs, e);
  367. switch (e->k) {
  368. case VNIL: {
  369. luaK_nil(fs, reg, 1);
  370. break;
  371. }
  372. case VFALSE: case VTRUE: {
  373. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  374. break;
  375. }
  376. case VK: {
  377. luaK_codek(fs, reg, e->u.info);
  378. break;
  379. }
  380. case VKFLT: {
  381. luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
  382. break;
  383. }
  384. case VKINT: {
  385. luaK_codek(fs, reg, luaK_intK(fs, e->u.ival));
  386. break;
  387. }
  388. case VRELOCABLE: {
  389. Instruction *pc = &getcode(fs, e);
  390. SETARG_A(*pc, reg);
  391. break;
  392. }
  393. case VNONRELOC: {
  394. if (reg != e->u.info)
  395. luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
  396. break;
  397. }
  398. default: {
  399. lua_assert(e->k == VVOID || e->k == VJMP);
  400. return; /* nothing to do... */
  401. }
  402. }
  403. e->u.info = reg;
  404. e->k = VNONRELOC;
  405. }
  406. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  407. if (e->k != VNONRELOC) {
  408. luaK_reserveregs(fs, 1);
  409. discharge2reg(fs, e, fs->freereg-1);
  410. }
  411. }
  412. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  413. discharge2reg(fs, e, reg);
  414. if (e->k == VJMP)
  415. luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
  416. if (hasjumps(e)) {
  417. int final; /* position after whole expression */
  418. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  419. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  420. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  421. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  422. p_f = code_label(fs, reg, 0, 1);
  423. p_t = code_label(fs, reg, 1, 0);
  424. luaK_patchtohere(fs, fj);
  425. }
  426. final = luaK_getlabel(fs);
  427. patchlistaux(fs, e->f, final, reg, p_f);
  428. patchlistaux(fs, e->t, final, reg, p_t);
  429. }
  430. e->f = e->t = NO_JUMP;
  431. e->u.info = reg;
  432. e->k = VNONRELOC;
  433. }
  434. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  435. luaK_dischargevars(fs, e);
  436. freeexp(fs, e);
  437. luaK_reserveregs(fs, 1);
  438. exp2reg(fs, e, fs->freereg - 1);
  439. }
  440. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  441. luaK_dischargevars(fs, e);
  442. if (e->k == VNONRELOC) {
  443. if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
  444. if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
  445. exp2reg(fs, e, e->u.info); /* put value on it */
  446. return e->u.info;
  447. }
  448. }
  449. luaK_exp2nextreg(fs, e); /* default */
  450. return e->u.info;
  451. }
  452. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  453. if (e->k != VUPVAL || hasjumps(e))
  454. luaK_exp2anyreg(fs, e);
  455. }
  456. void luaK_exp2val (FuncState *fs, expdesc *e) {
  457. if (hasjumps(e))
  458. luaK_exp2anyreg(fs, e);
  459. else
  460. luaK_dischargevars(fs, e);
  461. }
  462. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  463. luaK_exp2val(fs, e);
  464. switch (e->k) {
  465. case VTRUE:
  466. case VFALSE:
  467. case VNIL: {
  468. if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
  469. e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
  470. e->k = VK;
  471. return RKASK(e->u.info);
  472. }
  473. else break;
  474. }
  475. case VKINT: {
  476. e->u.info = luaK_intK(fs, e->u.ival);
  477. e->k = VK;
  478. goto vk;
  479. }
  480. case VKFLT: {
  481. e->u.info = luaK_numberK(fs, e->u.nval);
  482. e->k = VK;
  483. /* go through */
  484. }
  485. case VK: {
  486. vk:
  487. if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
  488. return RKASK(e->u.info);
  489. else break;
  490. }
  491. default: break;
  492. }
  493. /* not a constant in the right range: put it in a register */
  494. return luaK_exp2anyreg(fs, e);
  495. }
  496. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  497. switch (var->k) {
  498. case VLOCAL: {
  499. freeexp(fs, ex);
  500. exp2reg(fs, ex, var->u.info);
  501. return;
  502. }
  503. case VUPVAL: {
  504. int e = luaK_exp2anyreg(fs, ex);
  505. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  506. break;
  507. }
  508. case VINDEXED: {
  509. OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
  510. int e = luaK_exp2RK(fs, ex);
  511. luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
  512. break;
  513. }
  514. default: {
  515. lua_assert(0); /* invalid var kind to store */
  516. break;
  517. }
  518. }
  519. freeexp(fs, ex);
  520. }
  521. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  522. int ereg;
  523. luaK_exp2anyreg(fs, e);
  524. ereg = e->u.info; /* register where 'e' was placed */
  525. freeexp(fs, e);
  526. e->u.info = fs->freereg; /* base register for op_self */
  527. e->k = VNONRELOC;
  528. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  529. luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
  530. freeexp(fs, key);
  531. }
  532. static void invertjump (FuncState *fs, expdesc *e) {
  533. Instruction *pc = getjumpcontrol(fs, e->u.info);
  534. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  535. GET_OPCODE(*pc) != OP_TEST);
  536. SETARG_A(*pc, !(GETARG_A(*pc)));
  537. }
  538. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  539. if (e->k == VRELOCABLE) {
  540. Instruction ie = getcode(fs, e);
  541. if (GET_OPCODE(ie) == OP_NOT) {
  542. fs->pc--; /* remove previous OP_NOT */
  543. return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
  544. }
  545. /* else go through */
  546. }
  547. discharge2anyreg(fs, e);
  548. freeexp(fs, e);
  549. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
  550. }
  551. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  552. int pc; /* pc of last jump */
  553. luaK_dischargevars(fs, e);
  554. switch (e->k) {
  555. case VJMP: {
  556. invertjump(fs, e);
  557. pc = e->u.info;
  558. break;
  559. }
  560. case VK: case VKFLT: case VKINT: case VTRUE: {
  561. pc = NO_JUMP; /* always true; do nothing */
  562. break;
  563. }
  564. default: {
  565. pc = jumponcond(fs, e, 0);
  566. break;
  567. }
  568. }
  569. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  570. luaK_patchtohere(fs, e->t);
  571. e->t = NO_JUMP;
  572. }
  573. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  574. int pc; /* pc of last jump */
  575. luaK_dischargevars(fs, e);
  576. switch (e->k) {
  577. case VJMP: {
  578. pc = e->u.info;
  579. break;
  580. }
  581. case VNIL: case VFALSE: {
  582. pc = NO_JUMP; /* always false; do nothing */
  583. break;
  584. }
  585. default: {
  586. pc = jumponcond(fs, e, 1);
  587. break;
  588. }
  589. }
  590. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  591. luaK_patchtohere(fs, e->f);
  592. e->f = NO_JUMP;
  593. }
  594. static void codenot (FuncState *fs, expdesc *e) {
  595. luaK_dischargevars(fs, e);
  596. switch (e->k) {
  597. case VNIL: case VFALSE: {
  598. e->k = VTRUE;
  599. break;
  600. }
  601. case VK: case VKFLT: case VKINT: case VTRUE: {
  602. e->k = VFALSE;
  603. break;
  604. }
  605. case VJMP: {
  606. invertjump(fs, e);
  607. break;
  608. }
  609. case VRELOCABLE:
  610. case VNONRELOC: {
  611. discharge2anyreg(fs, e);
  612. freeexp(fs, e);
  613. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  614. e->k = VRELOCABLE;
  615. break;
  616. }
  617. default: {
  618. lua_assert(0); /* cannot happen */
  619. break;
  620. }
  621. }
  622. /* interchange true and false lists */
  623. { int temp = e->f; e->f = e->t; e->t = temp; }
  624. removevalues(fs, e->f);
  625. removevalues(fs, e->t);
  626. }
  627. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  628. lua_assert(!hasjumps(t));
  629. t->u.ind.t = t->u.info;
  630. t->u.ind.idx = luaK_exp2RK(fs, k);
  631. t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
  632. : check_exp(vkisinreg(t->k), VLOCAL);
  633. t->k = VINDEXED;
  634. }
  635. /* return false if folding can raise an error */
  636. static int validop (OpCode op, TValue *v1, TValue *v2) {
  637. lua_Integer i;
  638. switch (op) {
  639. case OP_IDIV: /* division by 0 and conversion errors */
  640. return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0);
  641. case OP_BAND: case OP_BOR: case OP_BXOR:
  642. case OP_SHL: case OP_SHR: case OP_BNOT: /* conversion errors */
  643. return (tointeger(v1, &i) && tointeger(v2, &i));
  644. case OP_MOD: /* integer module by 0 */
  645. return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
  646. case OP_POW: /* negative integer exponentiation */
  647. return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0);
  648. default: return 1; /* everything else is valid */
  649. }
  650. }
  651. static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
  652. TValue v1, v2, res;
  653. if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
  654. return 0; /* non-numeric operands or not safe to fold */
  655. luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res);
  656. if (ttisinteger(&res)) {
  657. e1->k = VKINT;
  658. e1->u.ival = ivalue(&res);
  659. }
  660. else {
  661. lua_Number n = fltvalue(&res);
  662. if (luai_numisnan(NULL, n) || isminuszero(n))
  663. return 0; /* folds neither NaN nor -0 */
  664. e1->k = VKFLT;
  665. e1->u.nval = n;
  666. }
  667. return 1;
  668. }
  669. static void codearith (FuncState *fs, OpCode op,
  670. expdesc *e1, expdesc *e2, int line) {
  671. if (!constfolding(op, e1, e2)) { /* could not fold operation? */
  672. int o1, o2;
  673. if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) {
  674. o2 = 0;
  675. o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */
  676. }
  677. else { /* regular case (binary operators) */
  678. o2 = luaK_exp2RK(fs, e2);
  679. o1 = luaK_exp2RK(fs, e1);
  680. }
  681. if (o1 > o2) {
  682. freeexp(fs, e1);
  683. freeexp(fs, e2);
  684. }
  685. else {
  686. freeexp(fs, e2);
  687. freeexp(fs, e1);
  688. }
  689. e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
  690. e1->k = VRELOCABLE;
  691. luaK_fixline(fs, line);
  692. }
  693. }
  694. static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
  695. expdesc *e2) {
  696. int o1 = luaK_exp2RK(fs, e1);
  697. int o2 = luaK_exp2RK(fs, e2);
  698. freeexp(fs, e2);
  699. freeexp(fs, e1);
  700. if (cond == 0 && op != OP_EQ) {
  701. int temp; /* exchange args to replace by `<' or `<=' */
  702. temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
  703. cond = 1;
  704. }
  705. e1->u.info = condjump(fs, op, cond, o1, o2);
  706. e1->k = VJMP;
  707. }
  708. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  709. expdesc e2;
  710. e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
  711. switch (op) {
  712. case OPR_MINUS: case OPR_BNOT: case OPR_LEN: {
  713. codearith(fs, op - OPR_MINUS + OP_UNM, e, &e2, line);
  714. break;
  715. }
  716. case OPR_NOT: codenot(fs, e); break;
  717. default: lua_assert(0);
  718. }
  719. }
  720. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  721. switch (op) {
  722. case OPR_AND: {
  723. luaK_goiftrue(fs, v);
  724. break;
  725. }
  726. case OPR_OR: {
  727. luaK_goiffalse(fs, v);
  728. break;
  729. }
  730. case OPR_CONCAT: {
  731. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  732. break;
  733. }
  734. case OPR_ADD: case OPR_SUB:
  735. case OPR_MUL: case OPR_DIV: case OPR_IDIV:
  736. case OPR_MOD: case OPR_POW:
  737. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  738. case OPR_SHL: case OPR_SHR: {
  739. if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v);
  740. break;
  741. }
  742. default: {
  743. luaK_exp2RK(fs, v);
  744. break;
  745. }
  746. }
  747. }
  748. void luaK_posfix (FuncState *fs, BinOpr op,
  749. expdesc *e1, expdesc *e2, int line) {
  750. switch (op) {
  751. case OPR_AND: {
  752. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  753. luaK_dischargevars(fs, e2);
  754. luaK_concat(fs, &e2->f, e1->f);
  755. *e1 = *e2;
  756. break;
  757. }
  758. case OPR_OR: {
  759. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  760. luaK_dischargevars(fs, e2);
  761. luaK_concat(fs, &e2->t, e1->t);
  762. *e1 = *e2;
  763. break;
  764. }
  765. case OPR_CONCAT: {
  766. luaK_exp2val(fs, e2);
  767. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  768. lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
  769. freeexp(fs, e1);
  770. SETARG_B(getcode(fs, e2), e1->u.info);
  771. e1->k = VRELOCABLE; e1->u.info = e2->u.info;
  772. }
  773. else {
  774. luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
  775. codearith(fs, OP_CONCAT, e1, e2, line);
  776. }
  777. break;
  778. }
  779. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  780. case OPR_IDIV: case OPR_MOD: case OPR_POW:
  781. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  782. case OPR_SHL: case OPR_SHR: {
  783. codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
  784. break;
  785. }
  786. case OPR_EQ: case OPR_LT: case OPR_LE: {
  787. codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
  788. break;
  789. }
  790. case OPR_NE: case OPR_GT: case OPR_GE: {
  791. codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
  792. break;
  793. }
  794. default: lua_assert(0);
  795. }
  796. }
  797. void luaK_fixline (FuncState *fs, int line) {
  798. fs->f->lineinfo[fs->pc - 1] = line;
  799. }
  800. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  801. int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
  802. int b = (tostore == LUA_MULTRET) ? 0 : tostore;
  803. lua_assert(tostore != 0);
  804. if (c <= MAXARG_C)
  805. luaK_codeABC(fs, OP_SETLIST, base, b, c);
  806. else if (c <= MAXARG_Ax) {
  807. luaK_codeABC(fs, OP_SETLIST, base, b, 0);
  808. codeextraarg(fs, c);
  809. }
  810. else
  811. luaX_syntaxerror(fs->ls, "constructor too long");
  812. fs->freereg = base + 1; /* free registers with list values */
  813. }