lcode.c 24 KB

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