lcode.c 24 KB

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