lcode.c 21 KB

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