2
0

lcode.c 22 KB

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