lcode.c 21 KB

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