lcode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. ** $Id: lcode.c,v 2.60 2011/08/30 16:26:41 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. incr_top(L);
  267. n = addk(fs, L->top - 1, &o);
  268. L->top--;
  269. }
  270. else
  271. n = addk(fs, &o, &o); /* regular case */
  272. return n;
  273. }
  274. static int boolK (FuncState *fs, int b) {
  275. TValue o;
  276. setbvalue(&o, b);
  277. return addk(fs, &o, &o);
  278. }
  279. static int nilK (FuncState *fs) {
  280. TValue k, v;
  281. setnilvalue(&v);
  282. /* cannot use nil as key; instead use table itself to represent nil */
  283. sethvalue(fs->ls->L, &k, fs->h);
  284. return addk(fs, &k, &v);
  285. }
  286. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  287. if (e->k == VCALL) { /* expression is an open function call? */
  288. SETARG_C(getcode(fs, e), nresults+1);
  289. }
  290. else if (e->k == VVARARG) {
  291. SETARG_B(getcode(fs, e), nresults+1);
  292. SETARG_A(getcode(fs, e), fs->freereg);
  293. luaK_reserveregs(fs, 1);
  294. }
  295. }
  296. void luaK_setoneret (FuncState *fs, expdesc *e) {
  297. if (e->k == VCALL) { /* expression is an open function call? */
  298. e->k = VNONRELOC;
  299. e->u.info = GETARG_A(getcode(fs, e));
  300. }
  301. else if (e->k == VVARARG) {
  302. SETARG_B(getcode(fs, e), 2);
  303. e->k = VRELOCABLE; /* can relocate its simple result */
  304. }
  305. }
  306. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  307. switch (e->k) {
  308. case VLOCAL: {
  309. e->k = VNONRELOC;
  310. break;
  311. }
  312. case VUPVAL: {
  313. e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
  314. e->k = VRELOCABLE;
  315. break;
  316. }
  317. case VINDEXED: {
  318. OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
  319. freereg(fs, e->u.ind.idx);
  320. if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
  321. freereg(fs, e->u.ind.t);
  322. op = OP_GETTABLE;
  323. }
  324. e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
  325. e->k = VRELOCABLE;
  326. break;
  327. }
  328. case VVARARG:
  329. case VCALL: {
  330. luaK_setoneret(fs, e);
  331. break;
  332. }
  333. default: break; /* there is one value available (somewhere) */
  334. }
  335. }
  336. static int code_label (FuncState *fs, int A, int b, int jump) {
  337. luaK_getlabel(fs); /* those instructions may be jump targets */
  338. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  339. }
  340. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  341. luaK_dischargevars(fs, e);
  342. switch (e->k) {
  343. case VNIL: {
  344. luaK_nil(fs, reg, 1);
  345. break;
  346. }
  347. case VFALSE: case VTRUE: {
  348. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  349. break;
  350. }
  351. case VK: {
  352. luaK_codek(fs, reg, e->u.info);
  353. break;
  354. }
  355. case VKNUM: {
  356. luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
  357. break;
  358. }
  359. case VRELOCABLE: {
  360. Instruction *pc = &getcode(fs, e);
  361. SETARG_A(*pc, reg);
  362. break;
  363. }
  364. case VNONRELOC: {
  365. if (reg != e->u.info)
  366. luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
  367. break;
  368. }
  369. default: {
  370. lua_assert(e->k == VVOID || e->k == VJMP);
  371. return; /* nothing to do... */
  372. }
  373. }
  374. e->u.info = reg;
  375. e->k = VNONRELOC;
  376. }
  377. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  378. if (e->k != VNONRELOC) {
  379. luaK_reserveregs(fs, 1);
  380. discharge2reg(fs, e, fs->freereg-1);
  381. }
  382. }
  383. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  384. discharge2reg(fs, e, reg);
  385. if (e->k == VJMP)
  386. luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
  387. if (hasjumps(e)) {
  388. int final; /* position after whole expression */
  389. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  390. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  391. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  392. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  393. p_f = code_label(fs, reg, 0, 1);
  394. p_t = code_label(fs, reg, 1, 0);
  395. luaK_patchtohere(fs, fj);
  396. }
  397. final = luaK_getlabel(fs);
  398. patchlistaux(fs, e->f, final, reg, p_f);
  399. patchlistaux(fs, e->t, final, reg, p_t);
  400. }
  401. e->f = e->t = NO_JUMP;
  402. e->u.info = reg;
  403. e->k = VNONRELOC;
  404. }
  405. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  406. luaK_dischargevars(fs, e);
  407. freeexp(fs, e);
  408. luaK_reserveregs(fs, 1);
  409. exp2reg(fs, e, fs->freereg - 1);
  410. }
  411. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  412. luaK_dischargevars(fs, e);
  413. if (e->k == VNONRELOC) {
  414. if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
  415. if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
  416. exp2reg(fs, e, e->u.info); /* put value on it */
  417. return e->u.info;
  418. }
  419. }
  420. luaK_exp2nextreg(fs, e); /* default */
  421. return e->u.info;
  422. }
  423. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  424. if (e->k != VUPVAL || hasjumps(e))
  425. luaK_exp2anyreg(fs, e);
  426. }
  427. void luaK_exp2val (FuncState *fs, expdesc *e) {
  428. if (hasjumps(e))
  429. luaK_exp2anyreg(fs, e);
  430. else
  431. luaK_dischargevars(fs, e);
  432. }
  433. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  434. luaK_exp2val(fs, e);
  435. switch (e->k) {
  436. case VTRUE:
  437. case VFALSE:
  438. case VNIL: {
  439. if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
  440. e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
  441. e->k = VK;
  442. return RKASK(e->u.info);
  443. }
  444. else break;
  445. }
  446. case VKNUM: {
  447. e->u.info = luaK_numberK(fs, e->u.nval);
  448. e->k = VK;
  449. /* go through */
  450. }
  451. case VK: {
  452. if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
  453. return RKASK(e->u.info);
  454. else break;
  455. }
  456. default: break;
  457. }
  458. /* not a constant in the right range: put it in a register */
  459. return luaK_exp2anyreg(fs, e);
  460. }
  461. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  462. switch (var->k) {
  463. case VLOCAL: {
  464. freeexp(fs, ex);
  465. exp2reg(fs, ex, var->u.info);
  466. return;
  467. }
  468. case VUPVAL: {
  469. int e = luaK_exp2anyreg(fs, ex);
  470. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  471. break;
  472. }
  473. case VINDEXED: {
  474. OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
  475. int e = luaK_exp2RK(fs, ex);
  476. luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
  477. break;
  478. }
  479. default: {
  480. lua_assert(0); /* invalid var kind to store */
  481. break;
  482. }
  483. }
  484. freeexp(fs, ex);
  485. }
  486. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  487. int ereg;
  488. luaK_exp2anyreg(fs, e);
  489. ereg = e->u.info; /* register where 'e' was placed */
  490. freeexp(fs, e);
  491. e->u.info = fs->freereg; /* base register for op_self */
  492. e->k = VNONRELOC;
  493. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  494. luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
  495. freeexp(fs, key);
  496. }
  497. static void invertjump (FuncState *fs, expdesc *e) {
  498. Instruction *pc = getjumpcontrol(fs, e->u.info);
  499. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  500. GET_OPCODE(*pc) != OP_TEST);
  501. SETARG_A(*pc, !(GETARG_A(*pc)));
  502. }
  503. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  504. if (e->k == VRELOCABLE) {
  505. Instruction ie = getcode(fs, e);
  506. if (GET_OPCODE(ie) == OP_NOT) {
  507. fs->pc--; /* remove previous OP_NOT */
  508. return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
  509. }
  510. /* else go through */
  511. }
  512. discharge2anyreg(fs, e);
  513. freeexp(fs, e);
  514. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
  515. }
  516. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  517. int pc; /* pc of last jump */
  518. luaK_dischargevars(fs, e);
  519. switch (e->k) {
  520. case VJMP: {
  521. invertjump(fs, e);
  522. pc = e->u.info;
  523. break;
  524. }
  525. case VK: case VKNUM: case VTRUE: {
  526. pc = NO_JUMP; /* always true; do nothing */
  527. break;
  528. }
  529. default: {
  530. pc = jumponcond(fs, e, 0);
  531. break;
  532. }
  533. }
  534. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  535. luaK_patchtohere(fs, e->t);
  536. e->t = NO_JUMP;
  537. }
  538. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  539. int pc; /* pc of last jump */
  540. luaK_dischargevars(fs, e);
  541. switch (e->k) {
  542. case VJMP: {
  543. pc = e->u.info;
  544. break;
  545. }
  546. case VNIL: case VFALSE: {
  547. pc = NO_JUMP; /* always false; do nothing */
  548. break;
  549. }
  550. default: {
  551. pc = jumponcond(fs, e, 1);
  552. break;
  553. }
  554. }
  555. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  556. luaK_patchtohere(fs, e->f);
  557. e->f = NO_JUMP;
  558. }
  559. static void codenot (FuncState *fs, expdesc *e) {
  560. luaK_dischargevars(fs, e);
  561. switch (e->k) {
  562. case VNIL: case VFALSE: {
  563. e->k = VTRUE;
  564. break;
  565. }
  566. case VK: case VKNUM: case VTRUE: {
  567. e->k = VFALSE;
  568. break;
  569. }
  570. case VJMP: {
  571. invertjump(fs, e);
  572. break;
  573. }
  574. case VRELOCABLE:
  575. case VNONRELOC: {
  576. discharge2anyreg(fs, e);
  577. freeexp(fs, e);
  578. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  579. e->k = VRELOCABLE;
  580. break;
  581. }
  582. default: {
  583. lua_assert(0); /* cannot happen */
  584. break;
  585. }
  586. }
  587. /* interchange true and false lists */
  588. { int temp = e->f; e->f = e->t; e->t = temp; }
  589. removevalues(fs, e->f);
  590. removevalues(fs, e->t);
  591. }
  592. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  593. lua_assert(!hasjumps(t));
  594. t->u.ind.t = t->u.info;
  595. t->u.ind.idx = luaK_exp2RK(fs, k);
  596. t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
  597. : check_exp(vkisinreg(t->k), VLOCAL);
  598. t->k = VINDEXED;
  599. }
  600. static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
  601. lua_Number r;
  602. if (!isnumeral(e1) || !isnumeral(e2)) return 0;
  603. if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
  604. return 0; /* do not attempt to divide by 0 */
  605. r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
  606. e1->u.nval = r;
  607. return 1;
  608. }
  609. static void codearith (FuncState *fs, OpCode op,
  610. expdesc *e1, expdesc *e2, int line) {
  611. if (constfolding(op, e1, e2))
  612. return;
  613. else {
  614. int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
  615. int o1 = luaK_exp2RK(fs, e1);
  616. if (o1 > o2) {
  617. freeexp(fs, e1);
  618. freeexp(fs, e2);
  619. }
  620. else {
  621. freeexp(fs, e2);
  622. freeexp(fs, e1);
  623. }
  624. e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
  625. e1->k = VRELOCABLE;
  626. luaK_fixline(fs, line);
  627. }
  628. }
  629. static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
  630. expdesc *e2) {
  631. int o1 = luaK_exp2RK(fs, e1);
  632. int o2 = luaK_exp2RK(fs, e2);
  633. freeexp(fs, e2);
  634. freeexp(fs, e1);
  635. if (cond == 0 && op != OP_EQ) {
  636. int temp; /* exchange args to replace by `<' or `<=' */
  637. temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
  638. cond = 1;
  639. }
  640. e1->u.info = condjump(fs, op, cond, o1, o2);
  641. e1->k = VJMP;
  642. }
  643. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  644. expdesc e2;
  645. e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
  646. switch (op) {
  647. case OPR_MINUS: {
  648. if (isnumeral(e)) /* minus constant? */
  649. e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */
  650. else {
  651. luaK_exp2anyreg(fs, e);
  652. codearith(fs, OP_UNM, e, &e2, line);
  653. }
  654. break;
  655. }
  656. case OPR_NOT: codenot(fs, e); break;
  657. case OPR_LEN: {
  658. luaK_exp2anyreg(fs, e); /* cannot operate on constants */
  659. codearith(fs, OP_LEN, e, &e2, line);
  660. break;
  661. }
  662. default: lua_assert(0);
  663. }
  664. }
  665. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  666. switch (op) {
  667. case OPR_AND: {
  668. luaK_goiftrue(fs, v);
  669. break;
  670. }
  671. case OPR_OR: {
  672. luaK_goiffalse(fs, v);
  673. break;
  674. }
  675. case OPR_CONCAT: {
  676. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  677. break;
  678. }
  679. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  680. case OPR_MOD: case OPR_POW: {
  681. if (!isnumeral(v)) luaK_exp2RK(fs, v);
  682. break;
  683. }
  684. default: {
  685. luaK_exp2RK(fs, v);
  686. break;
  687. }
  688. }
  689. }
  690. void luaK_posfix (FuncState *fs, BinOpr op,
  691. expdesc *e1, expdesc *e2, int line) {
  692. switch (op) {
  693. case OPR_AND: {
  694. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  695. luaK_dischargevars(fs, e2);
  696. luaK_concat(fs, &e2->f, e1->f);
  697. *e1 = *e2;
  698. break;
  699. }
  700. case OPR_OR: {
  701. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  702. luaK_dischargevars(fs, e2);
  703. luaK_concat(fs, &e2->t, e1->t);
  704. *e1 = *e2;
  705. break;
  706. }
  707. case OPR_CONCAT: {
  708. luaK_exp2val(fs, e2);
  709. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  710. lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
  711. freeexp(fs, e1);
  712. SETARG_B(getcode(fs, e2), e1->u.info);
  713. e1->k = VRELOCABLE; e1->u.info = e2->u.info;
  714. }
  715. else {
  716. luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
  717. codearith(fs, OP_CONCAT, e1, e2, line);
  718. }
  719. break;
  720. }
  721. case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
  722. case OPR_MOD: case OPR_POW: {
  723. codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
  724. break;
  725. }
  726. case OPR_EQ: case OPR_LT: case OPR_LE: {
  727. codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
  728. break;
  729. }
  730. case OPR_NE: case OPR_GT: case OPR_GE: {
  731. codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
  732. break;
  733. }
  734. default: lua_assert(0);
  735. }
  736. }
  737. void luaK_fixline (FuncState *fs, int line) {
  738. fs->f->lineinfo[fs->pc - 1] = line;
  739. }
  740. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  741. int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
  742. int b = (tostore == LUA_MULTRET) ? 0 : tostore;
  743. lua_assert(tostore != 0);
  744. if (c <= MAXARG_C)
  745. luaK_codeABC(fs, OP_SETLIST, base, b, c);
  746. else if (c <= MAXARG_Ax) {
  747. luaK_codeABC(fs, OP_SETLIST, base, b, 0);
  748. codeextraarg(fs, c);
  749. }
  750. else
  751. luaX_syntaxerror(fs->ls, "constructor too long");
  752. fs->freereg = base + 1; /* free registers with list values */
  753. }