lcode.c 22 KB

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