lcode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. ** $Id: lcode.c,v 1.13 2000/03/16 18:03:09 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_REENTRANT
  7. #include "lcode.h"
  8. #include "ldo.h"
  9. #include "llex.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lopcodes.h"
  13. #include "lparser.h"
  14. #include "lstring.h"
  15. void luaK_error (LexState *ls, const char *msg) {
  16. luaX_error(ls, msg, ls->token);
  17. }
  18. int luaK_code (FuncState *fs, Instruction i, int delta) {
  19. luaK_deltastack(fs, delta);
  20. luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT);
  21. fs->f->code[fs->pc] = i;
  22. return fs->pc++;
  23. }
  24. int luaK_0(FuncState *fs, OpCode o, int d) {
  25. return luaK_code(fs, CREATE_0(o), d);
  26. }
  27. int luaK_U(FuncState *fs, OpCode o, int u, int d) {
  28. return luaK_code(fs, CREATE_U(o,u), d);
  29. }
  30. int luaK_S(FuncState *fs, OpCode o, int s, int d) {
  31. return luaK_code(fs, CREATE_S(o,s), d);
  32. }
  33. int luaK_AB(FuncState *fs, OpCode o, int a, int b, int d) {
  34. return luaK_code(fs, CREATE_AB(o,a,b), d);
  35. }
  36. /*
  37. ** Returns the the previous instruction, for optimizations.
  38. ** If there is a jump target between this and the current instruction,
  39. ** returns a dummy instruction to avoid wrong optimizations.
  40. */
  41. static Instruction previous_instruction (FuncState *fs) {
  42. if (fs->pc > fs->lasttarget) /* no jumps to current position? */
  43. return fs->f->code[fs->pc-1]; /* returns previous instruction */
  44. else
  45. return CREATE_0(OP_END); /* no optimizations after an `END' */
  46. }
  47. static Instruction prepare (FuncState *fs, Instruction i, int delta) {
  48. Instruction previous = previous_instruction(fs);
  49. luaK_code(fs, i, delta);
  50. return previous;
  51. }
  52. static void setprevious (FuncState *fs, Instruction i) {
  53. fs->pc--; /* remove last instruction */
  54. fs->f->code[fs->pc-1] = i; /* change previous instruction */
  55. }
  56. static void luaK_minus (FuncState *fs) {
  57. Instruction previous = prepare(fs, CREATE_0(OP_MINUS), 0);
  58. switch(GET_OPCODE(previous)) {
  59. case OP_PUSHINT: SETARG_S(previous, -GETARG_S(previous)); break;
  60. case OP_PUSHNUM: SET_OPCODE(previous, OP_PUSHNEGNUM); break;
  61. case OP_PUSHNEGNUM: SET_OPCODE(previous, OP_PUSHNUM); break;
  62. default: return;
  63. }
  64. setprevious(fs, previous);
  65. }
  66. static void luaK_gettable (FuncState *fs) {
  67. Instruction previous = prepare(fs, CREATE_0(OP_GETTABLE), -1);
  68. switch(GET_OPCODE(previous)) {
  69. case OP_PUSHSTRING: SET_OPCODE(previous, OP_GETDOTTED); break;
  70. default: return;
  71. }
  72. setprevious(fs, previous);
  73. }
  74. static void luaK_add (FuncState *fs) {
  75. Instruction previous = prepare(fs, CREATE_0(OP_ADD), -1);
  76. switch(GET_OPCODE(previous)) {
  77. case OP_PUSHINT: SET_OPCODE(previous, OP_ADDI); break;
  78. default: return;
  79. }
  80. setprevious(fs, previous);
  81. }
  82. static void luaK_sub (FuncState *fs) {
  83. Instruction previous = prepare(fs, CREATE_0(OP_SUB), -1);
  84. switch(GET_OPCODE(previous)) {
  85. case OP_PUSHINT:
  86. SET_OPCODE(previous, OP_ADDI);
  87. SETARG_S(previous, -GETARG_S(previous));
  88. break;
  89. default: return;
  90. }
  91. setprevious(fs, previous);
  92. }
  93. static void luaK_conc (FuncState *fs) {
  94. Instruction previous = prepare(fs, CREATE_U(OP_CONC, 2), -1);
  95. switch(GET_OPCODE(previous)) {
  96. case OP_CONC: SETARG_U(previous, GETARG_U(previous)+1); break;
  97. default: return;
  98. }
  99. setprevious(fs, previous);
  100. }
  101. static void luaK_eq (FuncState *fs) {
  102. Instruction previous = prepare(fs, CREATE_S(OP_IFEQJMP, 0), -2);
  103. if (previous == CREATE_U(OP_PUSHNIL, 1)) {
  104. setprevious(fs, CREATE_0(OP_NOT));
  105. luaK_deltastack(fs, 1); /* undo delta from `prepare' */
  106. }
  107. }
  108. static void luaK_neq (FuncState *fs) {
  109. Instruction previous = prepare(fs, CREATE_S(OP_IFNEQJMP, 0), -2);
  110. if (previous == CREATE_U(OP_PUSHNIL, 1)) {
  111. fs->pc -= 2; /* remove PUSHNIL and IFNEQJMP */
  112. luaK_deltastack(fs, 1); /* undo delta from `prepare' */
  113. }
  114. }
  115. void luaK_retcode (FuncState *fs, int nlocals, int nexps) {
  116. Instruction previous = prepare(fs, CREATE_U(OP_RETURN, nlocals), 0);
  117. if (nexps > 0 && GET_OPCODE(previous) == OP_CALL) {
  118. LUA_ASSERT(fs->L, GETARG_B(previous) == MULT_RET, "call should be open");
  119. SET_OPCODE(previous, OP_TAILCALL);
  120. SETARG_B(previous, nlocals);
  121. setprevious(fs, previous);
  122. }
  123. }
  124. static void luaK_pushnil (FuncState *fs, int n) {
  125. Instruction previous = prepare(fs, CREATE_U(OP_PUSHNIL, n), n);
  126. switch(GET_OPCODE(previous)) {
  127. case OP_PUSHNIL: SETARG_U(previous, GETARG_U(previous)+n); break;
  128. default: return;
  129. }
  130. setprevious(fs, previous);
  131. }
  132. void luaK_fixjump (FuncState *fs, int pc, int dest) {
  133. Instruction *jmp = &fs->f->code[pc];
  134. if (dest == NO_JUMP)
  135. SETARG_S(*jmp, 0); /* absolute value to represent end of list */
  136. else { /* jump is relative to position following jump instruction */
  137. int offset = dest-(pc+1);
  138. if (offset < -MAXARG_S || offset > MAXARG_S)
  139. luaK_error(fs->ls, "control structure too long");
  140. SETARG_S(*jmp, offset);
  141. }
  142. }
  143. static int luaK_getjump (FuncState *fs, int pc) {
  144. int offset = GETARG_S(fs->f->code[pc]);
  145. if (offset == 0)
  146. return NO_JUMP; /* end of list */
  147. else
  148. return (pc+1)+offset; /* turn offset into absolute position */
  149. }
  150. /*
  151. ** returns current `pc' and marks it as a jump target (to avoid wrong
  152. ** optimizations with consecutive instructions not in the same basic block).
  153. */
  154. int luaK_getlabel (FuncState *fs) {
  155. fs->lasttarget = fs->pc;
  156. return fs->pc;
  157. }
  158. void luaK_deltastack (FuncState *fs, int delta) {
  159. fs->stacksize += delta;
  160. if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
  161. if (fs->stacksize > MAXSTACK)
  162. luaK_error(fs->ls, "function or expression too complex");
  163. fs->f->maxstacksize = fs->stacksize;
  164. }
  165. }
  166. void luaK_kstr (LexState *ls, int c) {
  167. luaK_U(ls->fs, OP_PUSHSTRING, c, 1);
  168. }
  169. #ifndef LOOKBACKNUMS
  170. #define LOOKBACKNUMS 20 /* arbitrary limit */
  171. #endif
  172. static int real_constant (FuncState *fs, Number r) {
  173. /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  174. Proto *f = fs->f;
  175. int c = f->nknum;
  176. int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
  177. while (--c >= lim)
  178. if (f->knum[c] == r) return c;
  179. /* not found; create a new entry */
  180. luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
  181. c = f->nknum++;
  182. f->knum[c] = r;
  183. return c;
  184. }
  185. void luaK_number (FuncState *fs, Number f) {
  186. if (f <= (Number)MAXARG_S && (int)f == f)
  187. luaK_S(fs, OP_PUSHINT, (int)f, 1); /* f has a short integer value */
  188. else
  189. luaK_U(fs, OP_PUSHNUM, real_constant(fs, f), 1);
  190. }
  191. void luaK_adjuststack (FuncState *fs, int n) {
  192. if (n > 0)
  193. luaK_U(fs, OP_POP, n, -n);
  194. else if (n < 0)
  195. luaK_pushnil(fs, -n);
  196. }
  197. int luaK_lastisopen (FuncState *fs) {
  198. /* check whether last instruction is an open function call */
  199. Instruction i = previous_instruction(fs);
  200. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET)
  201. return 1;
  202. else return 0;
  203. }
  204. void luaK_setcallreturns (FuncState *fs, int nresults) {
  205. if (luaK_lastisopen(fs)) { /* expression is an open function call? */
  206. SETARG_B(fs->f->code[fs->pc-1], nresults); /* set number of results */
  207. luaK_deltastack(fs, nresults); /* push results */
  208. }
  209. }
  210. static void assertglobal (FuncState *fs, int index) {
  211. luaS_assertglobal(fs->L, fs->f->kstr[index]);
  212. }
  213. static int discharge (FuncState *fs, expdesc *var) {
  214. switch (var->k) {
  215. case VLOCAL:
  216. luaK_U(fs, OP_PUSHLOCAL, var->u.index, 1);
  217. break;
  218. case VGLOBAL:
  219. luaK_U(fs, OP_GETGLOBAL, var->u.index, 1);
  220. assertglobal(fs, var->u.index); /* make sure that there is a global */
  221. break;
  222. case VINDEXED:
  223. luaK_gettable(fs);
  224. break;
  225. case VEXP:
  226. return 0; /* nothing to do */
  227. }
  228. var->k = VEXP;
  229. var->u.l.t = var->u.l.f = NO_JUMP;
  230. return 1;
  231. }
  232. static void discharge1 (FuncState *fs, expdesc *var) {
  233. discharge(fs, var);
  234. /* if it has jumps it is already discharged */
  235. if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
  236. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  237. }
  238. void luaK_storevar (LexState *ls, const expdesc *var) {
  239. FuncState *fs = ls->fs;
  240. switch (var->k) {
  241. case VLOCAL:
  242. luaK_U(fs, OP_SETLOCAL, var->u.index, -1);
  243. break;
  244. case VGLOBAL:
  245. luaK_U(fs, OP_SETGLOBAL, var->u.index, -1);
  246. assertglobal(fs, var->u.index); /* make sure that there is a global */
  247. break;
  248. case VINDEXED:
  249. luaK_0(fs, OP_SETTABLEPOP, -3);
  250. break;
  251. default:
  252. LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  253. }
  254. }
  255. static OpCode invertjump (OpCode op) {
  256. switch (op) {
  257. case OP_IFNEQJMP: return OP_IFEQJMP;
  258. case OP_IFEQJMP: return OP_IFNEQJMP;
  259. case OP_IFLTJMP: return OP_IFGEJMP;
  260. case OP_IFLEJMP: return OP_IFGTJMP;
  261. case OP_IFGTJMP: return OP_IFLEJMP;
  262. case OP_IFGEJMP: return OP_IFLTJMP;
  263. case OP_IFTJMP: case OP_ONTJMP: return OP_IFFJMP;
  264. case OP_IFFJMP: case OP_ONFJMP: return OP_IFTJMP;
  265. default:
  266. LUA_INTERNALERROR(NULL, "invalid jump instruction");
  267. return OP_END; /* to avoid warnings */
  268. }
  269. }
  270. static void luaK_jump (FuncState *fs, OpCode jump) {
  271. Instruction previous = prepare(fs, CREATE_S(jump, 0), -1);
  272. if (previous == CREATE_0(OP_NOT))
  273. setprevious(fs, CREATE_S(invertjump(jump), 0));
  274. }
  275. static void insert_last (FuncState *fs, int *list) {
  276. int first = *list;
  277. *list = fs->pc-1; /* insert last instruction in the list */
  278. luaK_fixjump(fs, *list, first);
  279. }
  280. static void luaK_patchlistaux (FuncState *fs, int list, int target,
  281. OpCode special, int special_target) {
  282. Instruction *code = fs->f->code;
  283. while (list != NO_JUMP) {
  284. int next = luaK_getjump(fs, list);
  285. Instruction *i = &code[list];
  286. OpCode op = GET_OPCODE(*i);
  287. if (op == special) /* this `op' already has a value */
  288. luaK_fixjump(fs, list, special_target);
  289. else {
  290. luaK_fixjump(fs, list, target); /* do the patch */
  291. if (op == OP_ONTJMP) /* remove eventual values */
  292. SET_OPCODE(*i, OP_IFTJMP);
  293. else if (op == OP_ONFJMP)
  294. SET_OPCODE(*i, OP_IFFJMP);
  295. }
  296. list = next;
  297. }
  298. }
  299. void luaK_patchlist (FuncState *fs, int list, int target) {
  300. luaK_patchlistaux(fs, list, target, OP_END, 0);
  301. }
  302. static int need_value (FuncState *fs, int list, OpCode hasvalue) {
  303. /* check whether list has a jump without a value */
  304. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  305. if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
  306. return 0; /* not found */
  307. }
  308. static void concatlists (FuncState *fs, int *l1, int l2) {
  309. if (*l1 == NO_JUMP)
  310. *l1 = l2;
  311. else {
  312. int list = *l1;
  313. for (;;) { /* traverse `l1' */
  314. int next = luaK_getjump(fs, list);
  315. if (next == NO_JUMP) { /* end of list? */
  316. luaK_fixjump(fs, list, l2);
  317. return;
  318. }
  319. list = next;
  320. }
  321. }
  322. }
  323. void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
  324. Instruction *previous;
  325. discharge1(fs, v);
  326. previous = &fs->f->code[fs->pc-1];
  327. if (ISJUMP(GET_OPCODE(*previous)))
  328. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  329. else {
  330. OpCode jump = keepvalue ? OP_ONFJMP : OP_IFFJMP;
  331. luaK_jump(fs, jump);
  332. }
  333. insert_last(fs, &v->u.l.f);
  334. luaK_patchlist(fs, v->u.l.t, luaK_getlabel(fs));
  335. v->u.l.t = NO_JUMP;
  336. }
  337. void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
  338. Instruction previous;
  339. discharge1(fs, v);
  340. previous = fs->f->code[fs->pc-1];
  341. if (!ISJUMP(GET_OPCODE(previous))) {
  342. OpCode jump = keepvalue ? OP_ONTJMP : OP_IFTJMP;
  343. luaK_jump(fs, jump);
  344. }
  345. insert_last(fs, &v->u.l.t);
  346. luaK_patchlist(fs, v->u.l.f, luaK_getlabel(fs));
  347. v->u.l.f = NO_JUMP;
  348. }
  349. void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  350. FuncState *fs = ls->fs;
  351. if (discharge(fs, v)) return;
  352. else { /* is an expression */
  353. OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
  354. if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
  355. /* it is an expression without jumps */
  356. if (onlyone && v->k == VEXP)
  357. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  358. return;
  359. }
  360. else { /* expression has jumps... */
  361. int p_nil = 0; /* position of an eventual PUSHNIL */
  362. int p_1 = 0; /* position of an eventual PUSHINT */
  363. int final; /* position after whole expression */
  364. if (ISJUMP(previous)) {
  365. insert_last(fs, &v->u.l.t); /* put `previous' in true list */
  366. p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
  367. p_1 = luaK_S(fs, OP_PUSHINT, 1, 1);
  368. }
  369. else { /* still may need a PUSHNIL or a PUSHINT */
  370. int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP);
  371. int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP);
  372. if (need_nil && need_1) {
  373. luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */
  374. p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
  375. p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
  376. }
  377. else if (need_nil || need_1) {
  378. luaK_S(fs, OP_JMP, 1, 0); /* skip one push */
  379. if (need_nil)
  380. p_nil = luaK_U(fs, OP_PUSHNIL, 1, 0);
  381. else /* need_1 */
  382. p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
  383. }
  384. }
  385. final = luaK_getlabel(fs);
  386. luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final);
  387. luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final);
  388. v->u.l.f = v->u.l.t = NO_JUMP;
  389. }
  390. }
  391. }
  392. void luaK_prefix (LexState *ls, int op, expdesc *v) {
  393. FuncState *fs = ls->fs;
  394. if (op == '-') {
  395. luaK_tostack(ls, v, 1);
  396. luaK_minus(fs);
  397. }
  398. else { /* op == NOT */
  399. Instruction *previous;
  400. discharge1(fs, v);
  401. previous = &fs->f->code[fs->pc-1];
  402. if (ISJUMP(GET_OPCODE(*previous)))
  403. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  404. else
  405. luaK_0(fs, OP_NOT, 0);
  406. /* interchange true and false lists */
  407. { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  408. }
  409. }
  410. void luaK_infix (LexState *ls, int op, expdesc *v) {
  411. FuncState *fs = ls->fs;
  412. if (op == TK_AND)
  413. luaK_goiftrue(fs, v, 1);
  414. else if (op == TK_OR)
  415. luaK_goiffalse(fs, v, 1);
  416. else
  417. luaK_tostack(ls, v, 1); /* all other binary operators need a value */
  418. }
  419. void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  420. FuncState *fs = ls->fs;
  421. if (op == TK_AND) {
  422. LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
  423. discharge1(fs, v2);
  424. v1->u.l.t = v2->u.l.t;
  425. concatlists(fs, &v1->u.l.f, v2->u.l.f);
  426. }
  427. else if (op == TK_OR) {
  428. LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
  429. discharge1(fs, v2);
  430. v1->u.l.f = v2->u.l.f;
  431. concatlists(fs, &v1->u.l.t, v2->u.l.t);
  432. }
  433. else {
  434. luaK_tostack(ls, v2, 1); /* `v2' must be a value */
  435. switch (op) {
  436. case '+': luaK_add(fs); break;
  437. case '-': luaK_sub(fs); break;
  438. case '*': luaK_0(fs, OP_MULT, -1); break;
  439. case '/': luaK_0(fs, OP_DIV, -1); break;
  440. case '^': luaK_0(fs, OP_POW, -1); break;
  441. case TK_CONC: luaK_conc(fs); break;
  442. case TK_EQ: luaK_eq(fs); break;
  443. case TK_NE: luaK_neq(fs); break;
  444. case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break;
  445. case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break;
  446. case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break;
  447. case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break;
  448. }
  449. }
  450. }