lcode.c 24 KB

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