lcode.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #include "lcode.h"
  7. #include "ldo.h"
  8. #include "llex.h"
  9. #include "lmem.h"
  10. #include "lobject.h"
  11. #include "lopcodes.h"
  12. #include "lparser.h"
  13. #include "lstring.h"
  14. void luaK_error (LexState *ls, const char *msg) {
  15. luaX_error(ls, msg, ls->token);
  16. }
  17. /*
  18. ** Returns the address of the previous instruction, for optimizations.
  19. ** If there is a jump target between this and the current instruction,
  20. ** returns the address of a dummy instruction to avoid wrong optimizations.
  21. */
  22. static Instruction *previous_instruction (LexState *ls) {
  23. FuncState *fs = ls->fs;
  24. if (fs->pc > fs->lasttarget) /* no jumps to current position? */
  25. return &fs->f->code[fs->pc-1]; /* returns previous instruction */
  26. else {
  27. static Instruction dummy = CREATE_0(ENDCODE);
  28. return &dummy; /* no optimizations after an `ENDCODE' */
  29. }
  30. }
  31. int luaK_primitivecode (LexState *ls, Instruction i) {
  32. FuncState *fs = ls->fs;
  33. luaM_growvector(ls->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAXARG_S);
  34. fs->f->code[fs->pc] = i;
  35. return fs->pc++;
  36. }
  37. static void luaK_minus (LexState *ls) {
  38. Instruction *previous = previous_instruction(ls);
  39. switch(GET_OPCODE(*previous)) {
  40. case PUSHINT: *previous = SETARG_S(*previous, -GETARG_S(*previous)); return;
  41. case PUSHNUM: *previous = SET_OPCODE(*previous, PUSHNEGNUM); return;
  42. case PUSHNEGNUM: *previous = SET_OPCODE(*previous, PUSHNUM); return;
  43. default: luaK_primitivecode(ls, CREATE_0(MINUSOP));
  44. }
  45. }
  46. static void luaK_gettable (LexState *ls) {
  47. Instruction *previous = previous_instruction(ls);
  48. luaK_deltastack(ls, -1);
  49. switch(GET_OPCODE(*previous)) {
  50. case PUSHSTRING: *previous = SET_OPCODE(*previous, GETDOTTED); break;
  51. default: luaK_primitivecode(ls, CREATE_0(GETTABLE));
  52. }
  53. }
  54. static void luaK_add (LexState *ls) {
  55. Instruction *previous = previous_instruction(ls);
  56. luaK_deltastack(ls, -1);
  57. switch(GET_OPCODE(*previous)) {
  58. case PUSHINT: *previous = SET_OPCODE(*previous, ADDI); break;
  59. default: luaK_primitivecode(ls, CREATE_0(ADDOP));
  60. }
  61. }
  62. static void luaK_sub (LexState *ls) {
  63. Instruction *previous = previous_instruction(ls);
  64. luaK_deltastack(ls, -1);
  65. switch(GET_OPCODE(*previous)) {
  66. case PUSHINT:
  67. *previous = SET_OPCODE(*previous, ADDI);
  68. *previous = SETARG_S(*previous, -GETARG_S(*previous));
  69. break;
  70. default: luaK_primitivecode(ls, CREATE_0(SUBOP));
  71. }
  72. }
  73. void luaK_retcode (LexState *ls, int nlocals, int nexps) {
  74. Instruction *previous = previous_instruction(ls);
  75. if (nexps > 0 && GET_OPCODE(*previous) == CALL) {
  76. LUA_ASSERT(ls->L, GETARG_B(*previous) == MULT_RET, "call should be open");
  77. *previous = SET_OPCODE(*previous, TAILCALL);
  78. *previous = SETARG_B(*previous, nlocals);
  79. }
  80. else
  81. luaK_primitivecode(ls, CREATE_U(RETCODE, nlocals));
  82. }
  83. static void luaK_pushnil (LexState *ls, int n) {
  84. Instruction *previous = previous_instruction(ls);
  85. luaK_deltastack(ls, n);
  86. switch(GET_OPCODE(*previous)) {
  87. case PUSHNIL:
  88. *previous = SETARG_U(*previous, GETARG_U(*previous)+n);
  89. break;
  90. default: luaK_primitivecode(ls, CREATE_U(PUSHNIL, n));
  91. }
  92. }
  93. int luaK_code (LexState *ls, Instruction i, int delta) {
  94. luaK_deltastack(ls, delta);
  95. return luaK_primitivecode(ls, i);
  96. }
  97. void luaK_fixjump (LexState *ls, int pc, int dest) {
  98. FuncState *fs = ls->fs;
  99. Instruction *jmp = &fs->f->code[pc];
  100. /* jump is relative to position following jump instruction */
  101. *jmp = SETARG_S(*jmp, dest-(pc+1));
  102. }
  103. /*
  104. ** returns current `pc' and marks it as a jump target (to avoid wrong
  105. ** optimizations with consecutive instructions not in the same basic block).
  106. */
  107. int luaK_getlabel (LexState *ls) {
  108. FuncState *fs = ls->fs;
  109. fs->lasttarget = fs->pc;
  110. return fs->pc;
  111. }
  112. void luaK_deltastack (LexState *ls, int delta) {
  113. FuncState *fs = ls->fs;
  114. fs->stacksize += delta;
  115. if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
  116. if (fs->stacksize > MAXSTACK)
  117. luaK_error(ls, "function or expression too complex");
  118. fs->f->maxstacksize = fs->stacksize;
  119. }
  120. }
  121. void luaK_kstr (LexState *ls, int c) {
  122. luaK_U(ls, PUSHSTRING, c, 1);
  123. }
  124. #ifndef LOOKBACKNUMS
  125. #define LOOKBACKNUMS 20 /* arbitrary limit */
  126. #endif
  127. static int real_constant (LexState *ls, real r) {
  128. /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  129. TProtoFunc *f = ls->fs->f;
  130. int c = f->nknum;
  131. int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
  132. while (--c >= lim)
  133. if (f->knum[c] == r) return c;
  134. /* not found; create a new entry */
  135. luaM_growvector(ls->L, f->knum, f->nknum, 1, real, constantEM, MAXARG_U);
  136. c = f->nknum++;
  137. f->knum[c] = r;
  138. return c;
  139. }
  140. void luaK_number (LexState *ls, real f) {
  141. if (f <= (real)MAXARG_S && (int)f == f)
  142. luaK_S(ls, PUSHINT, (int)f, 1); /* f has a short integer value */
  143. else
  144. luaK_U(ls, PUSHNUM, real_constant(ls, f), 1);
  145. }
  146. void luaK_adjuststack (LexState *ls, int n) {
  147. if (n > 0)
  148. luaK_U(ls, POP, n, -n);
  149. else if (n < 0)
  150. luaK_pushnil(ls, -n);
  151. }
  152. int luaK_lastisopen (LexState *ls) {
  153. /* check whether last instruction is an (open) function call */
  154. Instruction *i = previous_instruction(ls);
  155. if (GET_OPCODE(*i) == CALL) {
  156. LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open");
  157. return 1;
  158. }
  159. else return 0;
  160. }
  161. void luaK_setcallreturns (LexState *ls, int nresults) {
  162. Instruction *i = previous_instruction(ls);
  163. if (GET_OPCODE(*i) == CALL) { /* expression is a function call? */
  164. LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open");
  165. *i = SETARG_B(*i, nresults); /* set nresults */
  166. luaK_deltastack(ls, nresults); /* push results */
  167. }
  168. }
  169. static void assertglobal (LexState *ls, int index) {
  170. luaS_assertglobal(ls->L, ls->fs->f->kstr[index]);
  171. }
  172. void luaK_tostack (LexState *ls, expdesc *var) {
  173. switch (var->k) {
  174. case VLOCAL:
  175. luaK_U(ls, PUSHLOCAL, var->info, 1);
  176. break;
  177. case VGLOBAL:
  178. luaK_U(ls, GETGLOBAL, var->info, 1);
  179. assertglobal(ls, var->info); /* make sure that there is a global */
  180. break;
  181. case VINDEXED:
  182. luaK_gettable(ls);
  183. break;
  184. case VEXP:
  185. return; /* exp result is already on stack */
  186. }
  187. var->k = VEXP;
  188. }
  189. void luaK_1tostack (LexState *ls, expdesc *var) {
  190. if (var->k == VEXP)
  191. luaK_setcallreturns(ls, 1); /* call must return 1 value */
  192. else
  193. luaK_tostack(ls, var);
  194. }
  195. void luaK_storevar (LexState *ls, const expdesc *var) {
  196. switch (var->k) {
  197. case VLOCAL:
  198. luaK_U(ls, SETLOCAL, var->info, -1);
  199. break;
  200. case VGLOBAL:
  201. luaK_U(ls, SETGLOBAL, var->info, -1);
  202. assertglobal(ls, var->info); /* make sure that there is a global */
  203. break;
  204. case VINDEXED:
  205. luaK_0(ls, SETTABLEPOP, -3);
  206. break;
  207. default:
  208. LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  209. }
  210. }
  211. void luaK_prefix (LexState *ls, int op, expdesc *v) {
  212. luaK_1tostack(ls, v);
  213. if (op == '-') luaK_minus(ls);
  214. else luaK_0(ls, NOTOP, 0);
  215. }
  216. void luaK_infix (LexState *ls, int op, expdesc *v) {
  217. luaK_1tostack(ls, v);
  218. if (op == AND)
  219. v->info = luaK_0(ls, ONFJMP, -1);
  220. else if (op == OR)
  221. v->info = luaK_0(ls, ONTJMP, -1);
  222. }
  223. void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  224. luaK_1tostack(ls, v2);
  225. switch (op) {
  226. case AND: case OR:
  227. luaK_fixjump(ls, v1->info, luaK_getlabel(ls));
  228. break;
  229. case '+': luaK_add(ls); break;
  230. case '-': luaK_sub(ls); break;
  231. case '*': luaK_0(ls, MULTOP, -1); break;
  232. case '/': luaK_0(ls, DIVOP, -1); break;
  233. case '^': luaK_0(ls, POWOP, -1); break;
  234. case CONC: luaK_0(ls, CONCOP, -1); break;
  235. case EQ: luaK_0(ls, EQOP, -1); break;
  236. case NE: luaK_0(ls, NEQOP, -1); break;
  237. case '>': luaK_0(ls, GTOP, -1); break;
  238. case '<': luaK_0(ls, LTOP, -1); break;
  239. case GE: luaK_0(ls, GEOP, -1); break;
  240. case LE: luaK_0(ls, LEOP, -1); break;
  241. }
  242. }