lcode.c 24 KB

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