lcode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. ** $Id: lcode.c,v 2.68 2013/05/02 12:37:24 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define lcode_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lcode.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lgc.h"
  14. #include "llex.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lopcodes.h"
  18. #include "lparser.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "lvm.h"
  22. #define hasjumps(e) ((e)->t != (e)->f)
  23. static int tonumeral(expdesc *e, TValue *v) {
  24. if (e->t != NO_JUMP || e->f != NO_JUMP)
  25. return 0; /* not a numeral */
  26. switch (e->k) {
  27. case VKINT:
  28. if (v) setivalue(v, e->u.ival);
  29. return 1;
  30. case VKFLT:
  31. if (v) setnvalue(v, e->u.nval);
  32. return 1;
  33. default: return 0;
  34. }
  35. }
  36. void luaK_nil (FuncState *fs, int from, int n) {
  37. Instruction *previous;
  38. int l = from + n - 1; /* last register to set nil */
  39. if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
  40. previous = &fs->f->code[fs->pc-1];
  41. if (GET_OPCODE(*previous) == OP_LOADNIL) {
  42. int pfrom = GETARG_A(*previous);
  43. int pl = pfrom + GETARG_B(*previous);
  44. if ((pfrom <= from && from <= pl + 1) ||
  45. (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
  46. if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
  47. if (pl > l) l = pl; /* l = max(l, pl) */
  48. SETARG_A(*previous, from);
  49. SETARG_B(*previous, l - from);
  50. return;
  51. }
  52. } /* else go through */
  53. }
  54. luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
  55. }
  56. int luaK_jump (FuncState *fs) {
  57. int jpc = fs->jpc; /* save list of jumps to here */
  58. int j;
  59. fs->jpc = NO_JUMP;
  60. j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
  61. luaK_concat(fs, &j, jpc); /* keep them on hold */
  62. return j;
  63. }
  64. void luaK_ret (FuncState *fs, int first, int nret) {
  65. luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
  66. }
  67. static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
  68. luaK_codeABC(fs, op, A, B, C);
  69. return luaK_jump(fs);
  70. }
  71. static void fixjump (FuncState *fs, int pc, int dest) {
  72. Instruction *jmp = &fs->f->code[pc];
  73. int offset = dest-(pc+1);
  74. lua_assert(dest != NO_JUMP);
  75. if (abs(offset) > MAXARG_sBx)
  76. luaX_syntaxerror(fs->ls, "control structure too long");
  77. SETARG_sBx(*jmp, offset);
  78. }
  79. /*
  80. ** returns current `pc' and marks it as a jump target (to avoid wrong
  81. ** optimizations with consecutive instructions not in the same basic block).
  82. */
  83. int luaK_getlabel (FuncState *fs) {
  84. fs->lasttarget = fs->pc;
  85. return fs->pc;
  86. }
  87. static int getjump (FuncState *fs, int pc) {
  88. int offset = GETARG_sBx(fs->f->code[pc]);
  89. if (offset == NO_JUMP) /* point to itself represents end of list */
  90. return NO_JUMP; /* end of list */
  91. else
  92. return (pc+1)+offset; /* turn offset into absolute position */
  93. }
  94. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  95. Instruction *pi = &fs->f->code[pc];
  96. if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
  97. return pi-1;
  98. else
  99. return pi;
  100. }
  101. /*
  102. ** check whether list has any jump that do not produce a value
  103. ** (or produce an inverted value)
  104. */
  105. static int need_value (FuncState *fs, int list) {
  106. for (; list != NO_JUMP; list = getjump(fs, list)) {
  107. Instruction i = *getjumpcontrol(fs, list);
  108. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  109. }
  110. return 0; /* not found */
  111. }
  112. static int patchtestreg (FuncState *fs, int node, int reg) {
  113. Instruction *i = getjumpcontrol(fs, node);
  114. if (GET_OPCODE(*i) != OP_TESTSET)
  115. return 0; /* cannot patch other instructions */
  116. if (reg != NO_REG && reg != GETARG_B(*i))
  117. SETARG_A(*i, reg);
  118. else /* no register to put value or register already has the value */
  119. *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
  120. return 1;
  121. }
  122. static void removevalues (FuncState *fs, int list) {
  123. for (; list != NO_JUMP; list = getjump(fs, list))
  124. patchtestreg(fs, list, NO_REG);
  125. }
  126. static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
  127. int dtarget) {
  128. while (list != NO_JUMP) {
  129. int next = getjump(fs, list);
  130. if (patchtestreg(fs, list, reg))
  131. fixjump(fs, list, vtarget);
  132. else
  133. fixjump(fs, list, dtarget); /* jump to default target */
  134. list = next;
  135. }
  136. }
  137. static void dischargejpc (FuncState *fs) {
  138. patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
  139. fs->jpc = NO_JUMP;
  140. }
  141. void luaK_patchlist (FuncState *fs, int list, int target) {
  142. if (target == fs->pc)
  143. luaK_patchtohere(fs, list);
  144. else {
  145. lua_assert(target < fs->pc);
  146. patchlistaux(fs, list, target, NO_REG, target);
  147. }
  148. }
  149. LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
  150. level++; /* argument is +1 to reserve 0 as non-op */
  151. while (list != NO_JUMP) {
  152. int next = getjump(fs, list);
  153. lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
  154. (GETARG_A(fs->f->code[list]) == 0 ||
  155. GETARG_A(fs->f->code[list]) >= level));
  156. SETARG_A(fs->f->code[list], level);
  157. list = next;
  158. }
  159. }
  160. void luaK_patchtohere (FuncState *fs, int list) {
  161. luaK_getlabel(fs);
  162. luaK_concat(fs, &fs->jpc, list);
  163. }
  164. void luaK_concat (FuncState *fs, int *l1, int l2) {
  165. if (l2 == NO_JUMP) return;
  166. else if (*l1 == NO_JUMP)
  167. *l1 = l2;
  168. else {
  169. int list = *l1;
  170. int next;
  171. while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
  172. list = next;
  173. fixjump(fs, list, l2);
  174. }
  175. }
  176. static int luaK_code (FuncState *fs, Instruction i) {
  177. Proto *f = fs->f;
  178. dischargejpc(fs); /* `pc' will change */
  179. /* put new instruction in code array */
  180. luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
  181. MAX_INT, "opcodes");
  182. f->code[fs->pc] = i;
  183. /* save corresponding line information */
  184. luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
  185. MAX_INT, "opcodes");
  186. f->lineinfo[fs->pc] = fs->ls->lastline;
  187. return fs->pc++;
  188. }
  189. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  190. lua_assert(getOpMode(o) == iABC);
  191. lua_assert(getBMode(o) != OpArgN || b == 0);
  192. lua_assert(getCMode(o) != OpArgN || c == 0);
  193. lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
  194. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  195. }
  196. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  197. lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
  198. lua_assert(getCMode(o) == OpArgN);
  199. lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
  200. return luaK_code(fs, CREATE_ABx(o, a, bc));
  201. }
  202. static int codeextraarg (FuncState *fs, int a) {
  203. lua_assert(a <= MAXARG_Ax);
  204. return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
  205. }
  206. int luaK_codek (FuncState *fs, int reg, int k) {
  207. if (k <= MAXARG_Bx)
  208. return luaK_codeABx(fs, OP_LOADK, reg, k);
  209. else {
  210. int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
  211. codeextraarg(fs, k);
  212. return p;
  213. }
  214. }
  215. void luaK_checkstack (FuncState *fs, int n) {
  216. int newstack = fs->freereg + n;
  217. if (newstack > fs->f->maxstacksize) {
  218. if (newstack >= MAXSTACK)
  219. luaX_syntaxerror(fs->ls, "function or expression too complex");
  220. fs->f->maxstacksize = cast_byte(newstack);
  221. }
  222. }
  223. void luaK_reserveregs (FuncState *fs, int n) {
  224. luaK_checkstack(fs, n);
  225. fs->freereg += n;
  226. }
  227. static void freereg (FuncState *fs, int reg) {
  228. if (!ISK(reg) && reg >= fs->nactvar) {
  229. fs->freereg--;
  230. lua_assert(reg == fs->freereg);
  231. }
  232. }
  233. static void freeexp (FuncState *fs, expdesc *e) {
  234. if (e->k == VNONRELOC)
  235. freereg(fs, e->u.info);
  236. }
  237. static int addk (FuncState *fs, TValue *key, TValue *v) {
  238. lua_State *L = fs->ls->L;
  239. TValue *idx = luaH_set(L, fs->h, key);
  240. Proto *f = fs->f;
  241. int k, oldsize;
  242. if (ttisinteger(idx)) {
  243. k = ivalue(idx);
  244. if (luaV_rawequalobj(&f->k[k], v))
  245. return k;
  246. /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
  247. go through and create a new entry for this value */
  248. }
  249. /* constant not found; create a new entry */
  250. oldsize = f->sizek;
  251. k = fs->nk;
  252. /* numerical value does not need GC barrier;
  253. table has no metatable, so it does not need to invalidate cache */
  254. setivalue(idx, k);
  255. luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  256. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  257. setobj(L, &f->k[k], v);
  258. fs->nk++;
  259. luaC_barrier(L, f, v);
  260. return k;
  261. }
  262. int luaK_stringK (FuncState *fs, TString *s) {
  263. TValue o;
  264. setsvalue(fs->ls->L, &o, s);
  265. return addk(fs, &o, &o);
  266. }
  267. int luaK_intK (FuncState *fs, lua_Integer n) {
  268. TValue o;
  269. setivalue(&o, n);
  270. return addk(fs, &o, &o);
  271. }
  272. static int luaK_numberK (FuncState *fs, lua_Number r) {
  273. int n;
  274. lua_State *L = fs->ls->L;
  275. TValue o;
  276. setnvalue(&o, r);
  277. if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
  278. /* use raw representation as key to avoid numeric problems */
  279. setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r)));
  280. n = addk(fs, L->top - 1, &o);
  281. L->top--;
  282. }
  283. else {
  284. TValue k;
  285. setnvalue(&k, r + 0.5); /* ???? (avoid some collisions with ints) */
  286. n = addk(fs, &k, &o); /* regular case */
  287. }
  288. return n;
  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->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. /* go through */
  475. }
  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. static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
  627. TValue v1, v2, res;
  628. lua_Integer i;
  629. if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2))
  630. return 0;
  631. if (op == OP_IDIV &&
  632. (!tointeger(&v1, &i) || !tointeger(&v2, &i) || i == 0))
  633. return 0; /* avoid division by 0 and conversion errors */
  634. if (op == OP_MOD && ttisinteger(&v1) && ttisinteger(&v2) && ivalue(&v2) == 0)
  635. return 0; /* avoid module by 0 at compile time */
  636. luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res);
  637. if (ttisinteger(&res)) {
  638. e1->k = VKINT;
  639. e1->u.ival = ivalue(&res);
  640. }
  641. else {
  642. e1->k = VKFLT;
  643. e1->u.nval = fltvalue(&res);
  644. }
  645. return 1;
  646. }
  647. static void codearith (FuncState *fs, OpCode op,
  648. expdesc *e1, expdesc *e2, int line) {
  649. if (!constfolding(op, e1, e2)) { /* could not fold operation? */
  650. int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
  651. int o1 = luaK_exp2RK(fs, e1);
  652. if (o1 > o2) {
  653. freeexp(fs, e1);
  654. freeexp(fs, e2);
  655. }
  656. else {
  657. freeexp(fs, e2);
  658. freeexp(fs, e1);
  659. }
  660. e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
  661. e1->k = VRELOCABLE;
  662. luaK_fixline(fs, line);
  663. }
  664. }
  665. static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
  666. expdesc *e2) {
  667. int o1 = luaK_exp2RK(fs, e1);
  668. int o2 = luaK_exp2RK(fs, e2);
  669. freeexp(fs, e2);
  670. freeexp(fs, e1);
  671. if (cond == 0 && op != OP_EQ) {
  672. int temp; /* exchange args to replace by `<' or `<=' */
  673. temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
  674. cond = 1;
  675. }
  676. e1->u.info = condjump(fs, op, cond, o1, o2);
  677. e1->k = VJMP;
  678. }
  679. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  680. expdesc e2;
  681. e2.t = e2.f = NO_JUMP; e2.k = VKFLT; e2.u.nval = 0;
  682. switch (op) {
  683. case OPR_MINUS: {
  684. if (!constfolding(OP_UNM, e, e)) { /* cannot fold it? */
  685. luaK_exp2anyreg(fs, e);
  686. codearith(fs, OP_UNM, e, &e2, line);
  687. }
  688. break;
  689. }
  690. case OPR_NOT: codenot(fs, e); break;
  691. case OPR_LEN: {
  692. luaK_exp2anyreg(fs, e); /* cannot operate on constants */
  693. codearith(fs, OP_LEN, e, &e2, line);
  694. break;
  695. }
  696. default: lua_assert(0);
  697. }
  698. }
  699. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  700. switch (op) {
  701. case OPR_AND: {
  702. luaK_goiftrue(fs, v);
  703. break;
  704. }
  705. case OPR_OR: {
  706. luaK_goiffalse(fs, v);
  707. break;
  708. }
  709. case OPR_CONCAT: {
  710. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  711. break;
  712. }
  713. case OPR_ADD: case OPR_SUB:
  714. case OPR_MUL: case OPR_DIV: case OPR_IDIV:
  715. case OPR_MOD: case OPR_POW: {
  716. if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v);
  717. break;
  718. }
  719. default: {
  720. luaK_exp2RK(fs, v);
  721. break;
  722. }
  723. }
  724. }
  725. void luaK_posfix (FuncState *fs, BinOpr op,
  726. expdesc *e1, expdesc *e2, int line) {
  727. switch (op) {
  728. case OPR_AND: {
  729. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  730. luaK_dischargevars(fs, e2);
  731. luaK_concat(fs, &e2->f, e1->f);
  732. *e1 = *e2;
  733. break;
  734. }
  735. case OPR_OR: {
  736. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  737. luaK_dischargevars(fs, e2);
  738. luaK_concat(fs, &e2->t, e1->t);
  739. *e1 = *e2;
  740. break;
  741. }
  742. case OPR_CONCAT: {
  743. luaK_exp2val(fs, e2);
  744. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  745. lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
  746. freeexp(fs, e1);
  747. SETARG_B(getcode(fs, e2), e1->u.info);
  748. e1->k = VRELOCABLE; e1->u.info = e2->u.info;
  749. }
  750. else {
  751. luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
  752. codearith(fs, OP_CONCAT, e1, e2, line);
  753. }
  754. break;
  755. }
  756. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  757. case OPR_IDIV: case OPR_MOD: case OPR_POW: {
  758. codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
  759. break;
  760. }
  761. case OPR_EQ: case OPR_LT: case OPR_LE: {
  762. codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
  763. break;
  764. }
  765. case OPR_NE: case OPR_GT: case OPR_GE: {
  766. codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
  767. break;
  768. }
  769. default: lua_assert(0);
  770. }
  771. }
  772. void luaK_fixline (FuncState *fs, int line) {
  773. fs->f->lineinfo[fs->pc - 1] = line;
  774. }
  775. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  776. int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
  777. int b = (tostore == LUA_MULTRET) ? 0 : tostore;
  778. lua_assert(tostore != 0);
  779. if (c <= MAXARG_C)
  780. luaK_codeABC(fs, OP_SETLIST, base, b, c);
  781. else if (c <= MAXARG_Ax) {
  782. luaK_codeABC(fs, OP_SETLIST, base, b, 0);
  783. codeextraarg(fs, c);
  784. }
  785. else
  786. luaX_syntaxerror(fs->ls, "constructor too long");
  787. fs->freereg = base + 1; /* free registers with list values */
  788. }