lcode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. ** $Id: lcode.c,v 1.17 2000/03/24 12:18:30 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. setprevious(fs, CREATE_S(OP_IFTJMP, 0));
  112. }
  113. }
  114. void luaK_retcode (FuncState *fs, int nlocals, int nexps) {
  115. Instruction previous = prepare(fs, CREATE_U(OP_RETURN, nlocals), 0);
  116. if (nexps > 0 && GET_OPCODE(previous) == OP_CALL) {
  117. LUA_ASSERT(fs->L, GETARG_B(previous) == MULT_RET, "call should be open");
  118. SET_OPCODE(previous, OP_TAILCALL);
  119. SETARG_B(previous, nlocals);
  120. setprevious(fs, previous);
  121. }
  122. }
  123. static void luaK_pushnil (FuncState *fs, int n) {
  124. Instruction previous = prepare(fs, CREATE_U(OP_PUSHNIL, n), n);
  125. switch(GET_OPCODE(previous)) {
  126. case OP_PUSHNIL: SETARG_U(previous, GETARG_U(previous)+n); break;
  127. default: return;
  128. }
  129. setprevious(fs, previous);
  130. }
  131. void luaK_fixjump (FuncState *fs, int pc, int dest) {
  132. Instruction *jmp = &fs->f->code[pc];
  133. if (dest == NO_JUMP)
  134. SETARG_S(*jmp, 0); /* absolute value to represent end of list */
  135. else { /* jump is relative to position following jump instruction */
  136. int offset = dest-(pc+1);
  137. if (offset < -MAXARG_S || offset > MAXARG_S)
  138. luaK_error(fs->ls, "control structure too long");
  139. SETARG_S(*jmp, offset);
  140. }
  141. }
  142. static int luaK_getjump (FuncState *fs, int pc) {
  143. int offset = GETARG_S(fs->f->code[pc]);
  144. if (offset == 0)
  145. return NO_JUMP; /* end of list */
  146. else
  147. return (pc+1)+offset; /* turn offset into absolute position */
  148. }
  149. /*
  150. ** returns current `pc' and marks it as a jump target (to avoid wrong
  151. ** optimizations with consecutive instructions not in the same basic block).
  152. */
  153. int luaK_getlabel (FuncState *fs) {
  154. fs->lasttarget = fs->pc;
  155. return fs->pc;
  156. }
  157. void luaK_deltastack (FuncState *fs, int delta) {
  158. fs->stacksize += delta;
  159. if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
  160. if (fs->stacksize > MAXSTACK)
  161. luaK_error(fs->ls, "function or expression too complex");
  162. fs->f->maxstacksize = fs->stacksize;
  163. }
  164. }
  165. void luaK_kstr (LexState *ls, int c) {
  166. luaK_U(ls->fs, OP_PUSHSTRING, c, 1);
  167. }
  168. static int real_constant (FuncState *fs, Number r) {
  169. /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  170. Proto *f = fs->f;
  171. int c = f->nknum;
  172. int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
  173. while (--c >= lim)
  174. if (f->knum[c] == r) return c;
  175. /* not found; create a new entry */
  176. luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
  177. c = f->nknum++;
  178. f->knum[c] = r;
  179. return c;
  180. }
  181. void luaK_number (FuncState *fs, Number f) {
  182. if (f <= (Number)MAXARG_S && (int)f == f)
  183. luaK_S(fs, OP_PUSHINT, (int)f, 1); /* f has a short integer value */
  184. else
  185. luaK_U(fs, OP_PUSHNUM, real_constant(fs, f), 1);
  186. }
  187. void luaK_adjuststack (FuncState *fs, int n) {
  188. if (n > 0)
  189. luaK_U(fs, OP_POP, n, -n);
  190. else if (n < 0)
  191. luaK_pushnil(fs, -n);
  192. }
  193. int luaK_lastisopen (FuncState *fs) {
  194. /* check whether last instruction is an open function call */
  195. Instruction i = previous_instruction(fs);
  196. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET)
  197. return 1;
  198. else return 0;
  199. }
  200. void luaK_setcallreturns (FuncState *fs, int nresults) {
  201. if (luaK_lastisopen(fs)) { /* expression is an open function call? */
  202. SETARG_B(fs->f->code[fs->pc-1], nresults); /* set number of results */
  203. luaK_deltastack(fs, nresults); /* push results */
  204. }
  205. }
  206. static void assertglobal (FuncState *fs, int index) {
  207. luaS_assertglobal(fs->L, fs->f->kstr[index]);
  208. }
  209. static int discharge (FuncState *fs, expdesc *var) {
  210. switch (var->k) {
  211. case VLOCAL:
  212. luaK_U(fs, OP_PUSHLOCAL, var->u.index, 1);
  213. break;
  214. case VGLOBAL:
  215. luaK_U(fs, OP_GETGLOBAL, var->u.index, 1);
  216. assertglobal(fs, var->u.index); /* make sure that there is a global */
  217. break;
  218. case VINDEXED:
  219. luaK_gettable(fs);
  220. break;
  221. case VEXP:
  222. return 0; /* nothing to do */
  223. }
  224. var->k = VEXP;
  225. var->u.l.t = var->u.l.f = NO_JUMP;
  226. return 1;
  227. }
  228. static void discharge1 (FuncState *fs, expdesc *var) {
  229. discharge(fs, var);
  230. /* if it has jumps it is already discharged */
  231. if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
  232. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  233. }
  234. void luaK_storevar (LexState *ls, const expdesc *var) {
  235. FuncState *fs = ls->fs;
  236. switch (var->k) {
  237. case VLOCAL:
  238. luaK_U(fs, OP_SETLOCAL, var->u.index, -1);
  239. break;
  240. case VGLOBAL:
  241. luaK_U(fs, OP_SETGLOBAL, var->u.index, -1);
  242. assertglobal(fs, var->u.index); /* make sure that there is a global */
  243. break;
  244. case VINDEXED:
  245. luaK_0(fs, OP_SETTABLEPOP, -3);
  246. break;
  247. default:
  248. LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  249. }
  250. }
  251. static OpCode invertjump (OpCode op) {
  252. switch (op) {
  253. case OP_IFNEQJMP: return OP_IFEQJMP;
  254. case OP_IFEQJMP: return OP_IFNEQJMP;
  255. case OP_IFLTJMP: return OP_IFGEJMP;
  256. case OP_IFLEJMP: return OP_IFGTJMP;
  257. case OP_IFGTJMP: return OP_IFLEJMP;
  258. case OP_IFGEJMP: return OP_IFLTJMP;
  259. case OP_IFTJMP: case OP_ONTJMP: return OP_IFFJMP;
  260. case OP_IFFJMP: case OP_ONFJMP: return OP_IFTJMP;
  261. default:
  262. LUA_INTERNALERROR(NULL, "invalid jump instruction");
  263. return OP_END; /* to avoid warnings */
  264. }
  265. }
  266. static void luaK_jump (FuncState *fs, OpCode jump) {
  267. Instruction previous = prepare(fs, CREATE_S(jump, 0), -1);
  268. switch (GET_OPCODE(previous)) {
  269. case OP_NOT: previous = CREATE_S(invertjump(jump), 0); break;
  270. case OP_PUSHINT:
  271. if (jump == OP_IFTJMP) {
  272. previous = CREATE_S(OP_JMP, 0);
  273. break;
  274. }
  275. else return; /* do not set previous */
  276. default: return;
  277. }
  278. setprevious(fs, previous);
  279. }
  280. static void insert_last (FuncState *fs, int *list) {
  281. int first = *list;
  282. *list = fs->pc-1; /* insert last instruction in the list */
  283. luaK_fixjump(fs, *list, first);
  284. }
  285. static void luaK_patchlistaux (FuncState *fs, int list, int target,
  286. OpCode special, int special_target) {
  287. Instruction *code = fs->f->code;
  288. while (list != NO_JUMP) {
  289. int next = luaK_getjump(fs, list);
  290. Instruction *i = &code[list];
  291. OpCode op = GET_OPCODE(*i);
  292. if (op == special) /* this `op' already has a value */
  293. luaK_fixjump(fs, list, special_target);
  294. else {
  295. luaK_fixjump(fs, list, target); /* do the patch */
  296. if (op == OP_ONTJMP) /* remove eventual values */
  297. SET_OPCODE(*i, OP_IFTJMP);
  298. else if (op == OP_ONFJMP)
  299. SET_OPCODE(*i, OP_IFFJMP);
  300. }
  301. list = next;
  302. }
  303. }
  304. void luaK_patchlist (FuncState *fs, int list, int target) {
  305. luaK_patchlistaux(fs, list, target, OP_END, 0);
  306. }
  307. static int need_value (FuncState *fs, int list, OpCode hasvalue) {
  308. /* check whether list has a jump without a value */
  309. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  310. if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
  311. return 0; /* not found */
  312. }
  313. static void concatlists (FuncState *fs, int *l1, int l2) {
  314. if (*l1 == NO_JUMP)
  315. *l1 = l2;
  316. else {
  317. int list = *l1;
  318. for (;;) { /* traverse `l1' */
  319. int next = luaK_getjump(fs, list);
  320. if (next == NO_JUMP) { /* end of list? */
  321. luaK_fixjump(fs, list, l2);
  322. return;
  323. }
  324. list = next;
  325. }
  326. }
  327. }
  328. static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
  329. Instruction *previous;
  330. int *golist = &v->u.l.f;
  331. int *exitlist = &v->u.l.t;
  332. if (invert) { /* interchange `golist' and `exitlist' */
  333. int *temp = golist; golist = exitlist; exitlist = temp;
  334. }
  335. discharge1(fs, v);
  336. previous = &fs->f->code[fs->pc-1];
  337. LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line");
  338. if (ISJUMP(GET_OPCODE(*previous))) {
  339. if (invert)
  340. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  341. }
  342. else
  343. luaK_jump(fs, jump);
  344. insert_last(fs, exitlist);
  345. luaK_patchlist(fs, *golist, luaK_getlabel(fs));
  346. *golist = NO_JUMP;
  347. }
  348. void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
  349. luaK_testgo(fs, v, 1, keepvalue ? OP_ONFJMP : OP_IFFJMP);
  350. }
  351. void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
  352. luaK_testgo(fs, v, 0, keepvalue ? OP_ONTJMP : OP_IFTJMP);
  353. }
  354. void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  355. FuncState *fs = ls->fs;
  356. if (!discharge(fs, v)) { /* `v' is an expression? */
  357. OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
  358. LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
  359. if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
  360. /* it is an expression without jumps */
  361. if (onlyone)
  362. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  363. }
  364. else { /* expression has jumps... */
  365. int p_nil = 0; /* position of an eventual PUSHNIL */
  366. int p_1 = 0; /* position of an eventual PUSHINT */
  367. int final; /* position after whole expression */
  368. if (ISJUMP(previous)) {
  369. insert_last(fs, &v->u.l.t); /* put `previous' in true list */
  370. p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
  371. p_1 = luaK_S(fs, OP_PUSHINT, 1, 1);
  372. }
  373. else { /* still may need a PUSHNIL or a PUSHINT */
  374. int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP);
  375. int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP);
  376. if (need_nil && need_1) {
  377. luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */
  378. p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
  379. p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
  380. }
  381. else if (need_nil || need_1) {
  382. luaK_S(fs, OP_JMP, 1, 0); /* skip one push */
  383. if (need_nil)
  384. p_nil = luaK_U(fs, OP_PUSHNIL, 1, 0);
  385. else /* need_1 */
  386. p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
  387. }
  388. }
  389. final = luaK_getlabel(fs);
  390. luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final);
  391. luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final);
  392. v->u.l.f = v->u.l.t = NO_JUMP;
  393. }
  394. }
  395. }
  396. void luaK_prefix (LexState *ls, int op, expdesc *v) {
  397. FuncState *fs = ls->fs;
  398. if (op == '-') {
  399. luaK_tostack(ls, v, 1);
  400. luaK_minus(fs);
  401. }
  402. else { /* op == NOT */
  403. Instruction *previous;
  404. discharge1(fs, v);
  405. previous = &fs->f->code[fs->pc-1];
  406. if (ISJUMP(GET_OPCODE(*previous)))
  407. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  408. else
  409. luaK_0(fs, OP_NOT, 0);
  410. /* interchange true and false lists */
  411. { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  412. }
  413. }
  414. void luaK_infix (LexState *ls, int op, expdesc *v) {
  415. FuncState *fs = ls->fs;
  416. if (op == TK_AND)
  417. luaK_goiftrue(fs, v, 1);
  418. else if (op == TK_OR)
  419. luaK_goiffalse(fs, v, 1);
  420. else
  421. luaK_tostack(ls, v, 1); /* all other binary operators need a value */
  422. }
  423. void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  424. FuncState *fs = ls->fs;
  425. if (op == TK_AND) {
  426. LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
  427. discharge1(fs, v2);
  428. v1->u.l.t = v2->u.l.t;
  429. concatlists(fs, &v1->u.l.f, v2->u.l.f);
  430. }
  431. else if (op == TK_OR) {
  432. LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
  433. discharge1(fs, v2);
  434. v1->u.l.f = v2->u.l.f;
  435. concatlists(fs, &v1->u.l.t, v2->u.l.t);
  436. }
  437. else {
  438. luaK_tostack(ls, v2, 1); /* `v2' must be a value */
  439. switch (op) {
  440. case '+': luaK_add(fs); break;
  441. case '-': luaK_sub(fs); break;
  442. case '*': luaK_0(fs, OP_MULT, -1); break;
  443. case '/': luaK_0(fs, OP_DIV, -1); break;
  444. case '^': luaK_0(fs, OP_POW, -1); break;
  445. case TK_CONC: luaK_conc(fs); break;
  446. case TK_EQ: luaK_eq(fs); break;
  447. case TK_NE: luaK_neq(fs); break;
  448. case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break;
  449. case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break;
  450. case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break;
  451. case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break;
  452. }
  453. }
  454. }