lcode.c 22 KB

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