lcode.c 24 KB

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