lcode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. ** $Id: lcode.c,v 1.5 2000/03/03 20:30:47 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(ENDCODE);
  29. return &dummy; /* no optimizations after an `ENDCODE' */
  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 PUSHINT: SETARG_S(*previous, -GETARG_S(*previous)); return;
  42. case PUSHNUM: SET_OPCODE(*previous, PUSHNEGNUM); return;
  43. case PUSHNEGNUM: SET_OPCODE(*previous, PUSHNUM); return;
  44. default: luaK_primitivecode(ls, CREATE_0(MINUSOP));
  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 PUSHSTRING: SET_OPCODE(*previous, GETDOTTED); break;
  52. default: luaK_primitivecode(ls, CREATE_0(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 PUSHINT: SET_OPCODE(*previous, ADDI); break;
  60. default: luaK_primitivecode(ls, CREATE_0(ADDOP));
  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 PUSHINT:
  68. SET_OPCODE(*previous, ADDI);
  69. SETARG_S(*previous, -GETARG_S(*previous));
  70. break;
  71. default: luaK_primitivecode(ls, CREATE_0(SUBOP));
  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 CONCOP: SETARG_U(*previous, GETARG_U(*previous)+1); break;
  79. default: luaK_primitivecode(ls, CREATE_U(CONCOP, 2));
  80. }
  81. }
  82. void luaK_retcode (LexState *ls, int nlocals, int nexps) {
  83. Instruction *previous = previous_instruction(ls);
  84. if (nexps > 0 && GET_OPCODE(*previous) == CALL) {
  85. LUA_ASSERT(ls->L, GETARG_B(*previous) == MULT_RET, "call should be open");
  86. SET_OPCODE(*previous, TAILCALL);
  87. SETARG_B(*previous, nlocals);
  88. }
  89. else
  90. luaK_primitivecode(ls, CREATE_U(RETCODE, nlocals));
  91. }
  92. static void luaK_pushnil (LexState *ls, int n) {
  93. Instruction *previous = previous_instruction(ls);
  94. luaK_deltastack(ls, n);
  95. switch(GET_OPCODE(*previous)) {
  96. case PUSHNIL:
  97. SETARG_U(*previous, GETARG_U(*previous)+n);
  98. break;
  99. default: luaK_primitivecode(ls, CREATE_U(PUSHNIL, n));
  100. }
  101. }
  102. int luaK_code (LexState *ls, Instruction i, int delta) {
  103. luaK_deltastack(ls, delta);
  104. return luaK_primitivecode(ls, i);
  105. }
  106. void luaK_fixjump (LexState *ls, int pc, int dest) {
  107. FuncState *fs = ls->fs;
  108. Instruction *jmp = &fs->f->code[pc];
  109. /* jump is relative to position following jump instruction */
  110. SETARG_S(*jmp, dest-(pc+1));
  111. }
  112. /*
  113. ** returns current `pc' and marks it as a jump target (to avoid wrong
  114. ** optimizations with consecutive instructions not in the same basic block).
  115. */
  116. int luaK_getlabel (LexState *ls) {
  117. FuncState *fs = ls->fs;
  118. fs->lasttarget = fs->pc;
  119. return fs->pc;
  120. }
  121. void luaK_deltastack (LexState *ls, int delta) {
  122. FuncState *fs = ls->fs;
  123. fs->stacksize += delta;
  124. if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
  125. if (fs->stacksize > MAXSTACK)
  126. luaK_error(ls, "function or expression too complex");
  127. fs->f->maxstacksize = fs->stacksize;
  128. }
  129. }
  130. void luaK_kstr (LexState *ls, int c) {
  131. luaK_U(ls, PUSHSTRING, c, 1);
  132. }
  133. #ifndef LOOKBACKNUMS
  134. #define LOOKBACKNUMS 20 /* arbitrary limit */
  135. #endif
  136. static int real_constant (LexState *ls, real r) {
  137. /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  138. TProtoFunc *f = ls->fs->f;
  139. int c = f->nknum;
  140. int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
  141. while (--c >= lim)
  142. if (f->knum[c] == r) return c;
  143. /* not found; create a new entry */
  144. luaM_growvector(ls->L, f->knum, f->nknum, 1, real, constantEM, MAXARG_U);
  145. c = f->nknum++;
  146. f->knum[c] = r;
  147. return c;
  148. }
  149. void luaK_number (LexState *ls, real f) {
  150. if (f <= (real)MAXARG_S && (int)f == f)
  151. luaK_S(ls, PUSHINT, (int)f, 1); /* f has a short integer value */
  152. else
  153. luaK_U(ls, PUSHNUM, real_constant(ls, f), 1);
  154. }
  155. void luaK_adjuststack (LexState *ls, int n) {
  156. if (n > 0)
  157. luaK_U(ls, POP, n, -n);
  158. else if (n < 0)
  159. luaK_pushnil(ls, -n);
  160. }
  161. int luaK_lastisopen (LexState *ls) {
  162. /* check whether last instruction is an (open) function call */
  163. Instruction *i = previous_instruction(ls);
  164. if (GET_OPCODE(*i) == CALL) {
  165. LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open");
  166. return 1;
  167. }
  168. else return 0;
  169. }
  170. void luaK_setcallreturns (LexState *ls, int nresults) {
  171. Instruction *i = previous_instruction(ls);
  172. if (GET_OPCODE(*i) == CALL) { /* expression is a function call? */
  173. LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open");
  174. SETARG_B(*i, nresults); /* set nresults */
  175. luaK_deltastack(ls, nresults); /* push results */
  176. }
  177. }
  178. static void assertglobal (LexState *ls, int index) {
  179. luaS_assertglobal(ls->L, ls->fs->f->kstr[index]);
  180. }
  181. static int discharge (LexState *ls, expdesc *var) {
  182. switch (var->k) {
  183. case VLOCAL:
  184. luaK_U(ls, PUSHLOCAL, var->u.index, 1);
  185. break;
  186. case VGLOBAL:
  187. luaK_U(ls, GETGLOBAL, var->u.index, 1);
  188. assertglobal(ls, var->u.index); /* make sure that there is a global */
  189. break;
  190. case VINDEXED:
  191. luaK_gettable(ls);
  192. break;
  193. case VEXP:
  194. return 0; /* nothing to do */
  195. }
  196. var->k = VEXP;
  197. var->u.l.t = var->u.l.f = 0;
  198. return 1;
  199. }
  200. static void discharge1 (LexState *ls, expdesc *var) {
  201. discharge(ls, var);
  202. /* if it has jumps it is already discharged */
  203. if (var->u.l.t == 0 && var->u.l.f == 0)
  204. luaK_setcallreturns(ls, 1); /* call must return 1 value */
  205. }
  206. void luaK_storevar (LexState *ls, const expdesc *var) {
  207. switch (var->k) {
  208. case VLOCAL:
  209. luaK_U(ls, SETLOCAL, var->u.index, -1);
  210. break;
  211. case VGLOBAL:
  212. luaK_U(ls, SETGLOBAL, var->u.index, -1);
  213. assertglobal(ls, var->u.index); /* make sure that there is a global */
  214. break;
  215. case VINDEXED:
  216. luaK_0(ls, SETTABLEPOP, -3);
  217. break;
  218. default:
  219. LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  220. }
  221. }
  222. static OpCode invertjump (OpCode op) {
  223. switch (op) {
  224. case IFNEQJMP: return IFEQJMP;
  225. case IFEQJMP: return IFNEQJMP;
  226. case IFLTJMP: return IFGEJMP;
  227. case IFLEJMP: return IFGTJMP;
  228. case IFGTJMP: return IFLEJMP;
  229. case IFGEJMP: return IFLTJMP;
  230. default:
  231. LUA_INTERNALERROR(NULL, "invalid jump instruction");
  232. return ENDCODE; /* to avoid warnings */
  233. }
  234. }
  235. static void insert_last (FuncState *fs, int *list) {
  236. int temp = *list;
  237. *list = fs->pc-1;
  238. if (temp == 0) /* chain list */
  239. SETARG_S(fs->f->code[*list], 0);
  240. else
  241. SETARG_S(fs->f->code[*list], temp-fs->pc);
  242. }
  243. static void luaK_patchlistaux (LexState *ls, int list, int target,
  244. OpCode special, int special_target) {
  245. if (list != 0) {
  246. Instruction *code = ls->fs->f->code;
  247. for (;;) {
  248. Instruction *i = &code[list];
  249. OpCode op = GET_OPCODE(*i);
  250. int temp = GETARG_S(*i);
  251. if (op == special)
  252. SETARG_S(*i, special_target-(list+1));
  253. else {
  254. SETARG_S(*i, target-(list+1));
  255. if (op == ONTJMP)
  256. SET_OPCODE(*i, IFTJMP);
  257. else if (op == ONFJMP)
  258. SET_OPCODE(*i, IFFJMP);
  259. }
  260. if (temp == 0) return;
  261. list += temp+1;
  262. }
  263. }
  264. }
  265. void luaK_patchlist (LexState *ls, int list, int target) {
  266. luaK_patchlistaux(ls, list, target, ENDCODE, 0);
  267. }
  268. static int has_jumps (FuncState *fs, int list, OpCode ignore) {
  269. if (list == 0) return 0;
  270. else {
  271. Instruction *code = fs->f->code;
  272. for (;;) {
  273. int temp = GETARG_S(code[list]);
  274. if (GET_OPCODE(code[list]) != ignore) return 1;
  275. else if (temp == 0) return 0;
  276. list += temp+1;
  277. }
  278. }
  279. }
  280. static void concatlists (LexState *ls, int *l1, int l2) {
  281. if (*l1 == 0)
  282. *l1 = l2;
  283. else if (l2 != 0) {
  284. FuncState *fs = ls->fs;
  285. int list = *l1;
  286. for (;;) { /* traverse `l1' */
  287. int temp = GETARG_S(fs->f->code[list]);
  288. if (temp == 0) { /* end of list? */
  289. SETARG_S(fs->f->code[list], l2-(list+1)); /* end points to `l2' */
  290. return;
  291. }
  292. list += temp+1;
  293. }
  294. }
  295. }
  296. void luaK_goiftrue (LexState *ls, expdesc *v, int keepvalue) {
  297. FuncState *fs = ls->fs;
  298. Instruction *previous;
  299. discharge1(ls, v);
  300. previous = &fs->f->code[fs->pc-1];
  301. if (ISJUMP(GET_OPCODE(*previous)))
  302. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  303. else {
  304. OpCode jump = keepvalue ? ONFJMP : IFFJMP;
  305. luaK_S(ls, jump, 0, -1);
  306. }
  307. insert_last(fs, &v->u.l.f);
  308. luaK_patchlist(ls, v->u.l.t, luaK_getlabel(ls));
  309. v->u.l.t = 0;
  310. }
  311. void luaK_goiffalse (LexState *ls, expdesc *v, int keepvalue) {
  312. FuncState *fs = ls->fs;
  313. Instruction *previous;
  314. discharge1(ls, v);
  315. previous = &fs->f->code[fs->pc-1];
  316. if (!ISJUMP(GET_OPCODE(*previous))) {
  317. OpCode jump = keepvalue ? ONTJMP : IFTJMP;
  318. luaK_S(ls, jump, 0, -1);
  319. }
  320. insert_last(fs, &v->u.l.t);
  321. luaK_patchlist(ls, v->u.l.f, luaK_getlabel(ls));
  322. v->u.l.f = 0;
  323. }
  324. void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  325. if (discharge(ls, v)) return;
  326. else { /* is an expression */
  327. FuncState *fs = ls->fs;
  328. Instruction *previous = &fs->f->code[fs->pc-1];
  329. if (!ISJUMP(GET_OPCODE(*previous)) && v->u.l.f == 0 && v->u.l.t == 0) {
  330. /* it is an expression without jumps */
  331. if (onlyone && v->k == VEXP)
  332. luaK_setcallreturns(ls, 1); /* call must return 1 value */
  333. return;
  334. }
  335. else { /* expression has jumps... */
  336. int p_nil = 0; /* position of an eventual PUSHNIL */
  337. int p_1 = 0; /* position of an eventual PUSHINT */
  338. int final; /* position after whole expression */
  339. if (ISJUMP(GET_OPCODE(*previous))) {
  340. insert_last(fs, &v->u.l.t);
  341. p_nil = luaK_0(ls, PUSHNILJMP, 0);
  342. p_1 = luaK_S(ls, PUSHINT, 1, 1);
  343. }
  344. else { /* still may need a PUSHNIL or a PUSHINT */
  345. int need_nil = has_jumps(fs, v->u.l.f, ONFJMP); /* needs a PUSHNIL? */
  346. int need_1 = has_jumps(fs, v->u.l.t, ONTJMP); /* needs a PUSHINT? */
  347. if (need_nil && need_1) {
  348. luaK_S(ls, JMP, 2, 0);
  349. p_nil = luaK_0(ls, PUSHNILJMP, 0);
  350. p_1 = luaK_S(ls, PUSHINT, 1, 1);
  351. }
  352. else if (need_nil || need_1) {
  353. luaK_S(ls, JMP, 1, 0);
  354. if (need_nil)
  355. p_nil = luaK_0(ls, PUSHNIL, 1);
  356. else /* need_1 */
  357. p_1 = luaK_S(ls, PUSHINT, 1, 1);
  358. }
  359. }
  360. final = luaK_getlabel(ls);
  361. luaK_patchlistaux(ls, v->u.l.f, p_nil, ONFJMP, final);
  362. luaK_patchlistaux(ls, v->u.l.t, p_1, ONTJMP, final);
  363. v->u.l.f = v->u.l.t = 0;
  364. }
  365. }
  366. }
  367. void luaK_prefix (LexState *ls, int op, expdesc *v) {
  368. if (op == '-') {
  369. luaK_tostack(ls, v, 1);
  370. luaK_minus(ls);
  371. }
  372. else { /* op == NOT */
  373. FuncState *fs = ls->fs;
  374. Instruction *previous;
  375. discharge1(ls, v);
  376. previous = &fs->f->code[fs->pc-1];
  377. if (ISJUMP(GET_OPCODE(*previous)))
  378. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  379. else
  380. luaK_0(ls, NOTOP, 0);
  381. /* interchange true and false lists */
  382. { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  383. }
  384. }
  385. void luaK_infix (LexState *ls, int op, expdesc *v) {
  386. if (op == AND)
  387. luaK_goiftrue(ls, v, 1);
  388. else if (op == OR)
  389. luaK_goiffalse(ls, v, 1);
  390. else
  391. luaK_tostack(ls, v, 1);
  392. }
  393. void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  394. if (op == AND) {
  395. LUA_ASSERT(ls->L, v1->u.l.t == 0, "list must be closed");
  396. discharge1(ls, v2);
  397. v1->u.l.t = v2->u.l.t;
  398. concatlists(ls, &v1->u.l.f, v2->u.l.f);
  399. }
  400. else if (op == OR) {
  401. LUA_ASSERT(ls->L, v1->u.l.f == 0, "list must be closed");
  402. discharge1(ls, v2);
  403. v1->u.l.f = v2->u.l.f;
  404. concatlists(ls, &v1->u.l.t, v2->u.l.t);
  405. }
  406. else {
  407. luaK_tostack(ls, v2, 1); /* `v2' must have a value */
  408. switch (op) {
  409. case '+': luaK_add(ls); break;
  410. case '-': luaK_sub(ls); break;
  411. case '*': luaK_0(ls, MULTOP, -1); break;
  412. case '/': luaK_0(ls, DIVOP, -1); break;
  413. case '^': luaK_0(ls, POWOP, -1); break;
  414. case CONC: luaK_conc(ls); break;
  415. case EQ: luaK_S(ls, IFEQJMP, 0, -2); break;
  416. case NE: luaK_S(ls, IFNEQJMP, 0, -2); break;
  417. case '>': luaK_S(ls, IFGTJMP, 0, -2); break;
  418. case '<': luaK_S(ls, IFLTJMP, 0, -2); break;
  419. case GE: luaK_S(ls, IFGEJMP, 0, -2); break;
  420. case LE: luaK_S(ls, IFLEJMP, 0, -2); break;
  421. }
  422. }
  423. }