lcode.c 21 KB

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