lcode.c 15 KB

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