lcode.c 24 KB

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