2
0

lcode.c 24 KB

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