luac.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. ** $Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp $
  3. ** Lua compiler (saves bytecodes to files; also list bytecodes)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define luac_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lmem.h"
  17. #include "lobject.h"
  18. #include "lopcodes.h"
  19. #include "lstring.h"
  20. #include "lundump.h"
  21. #define PROGNAME "luac" /* default program name */
  22. #define OUTPUT PROGNAME ".out" /* default output file */
  23. static int listing=0; /* list bytecodes? */
  24. static int dumping=1; /* dump bytecodes? */
  25. static int stripping=0; /* strip debug information? */
  26. static char Output[]={ OUTPUT }; /* default output file name */
  27. static const char* output=Output; /* actual output file name */
  28. static const char* progname=PROGNAME; /* actual program name */
  29. static void fatal(const char* message)
  30. {
  31. fprintf(stderr,"%s: %s\n",progname,message);
  32. exit(EXIT_FAILURE);
  33. }
  34. static void cannot(const char* what)
  35. {
  36. fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
  37. exit(EXIT_FAILURE);
  38. }
  39. static void usage(const char* message)
  40. {
  41. if (*message=='-')
  42. fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
  43. else
  44. fprintf(stderr,"%s: %s\n",progname,message);
  45. fprintf(stderr,
  46. "usage: %s [options] [filenames].\n"
  47. "Available options are:\n"
  48. " - process stdin\n"
  49. " -l list\n"
  50. " -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
  51. " -p parse only\n"
  52. " -s strip debug information\n"
  53. " -v show version information\n"
  54. " -- stop handling options\n",
  55. progname,Output);
  56. exit(EXIT_FAILURE);
  57. }
  58. #define IS(s) (strcmp(argv[i],s)==0)
  59. static int doargs(int argc, char* argv[])
  60. {
  61. int i;
  62. int version=0;
  63. if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
  64. for (i=1; i<argc; i++)
  65. {
  66. if (*argv[i]!='-') /* end of options; keep it */
  67. break;
  68. else if (IS("--")) /* end of options; skip it */
  69. {
  70. ++i;
  71. if (version) ++version;
  72. break;
  73. }
  74. else if (IS("-")) /* end of options; use stdin */
  75. break;
  76. else if (IS("-l")) /* list */
  77. ++listing;
  78. else if (IS("-o")) /* output file */
  79. {
  80. output=argv[++i];
  81. if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
  82. if (IS("-")) output=NULL;
  83. }
  84. else if (IS("-p")) /* parse only */
  85. dumping=0;
  86. else if (IS("-s")) /* strip debug information */
  87. stripping=1;
  88. else if (IS("-v")) /* show version */
  89. ++version;
  90. else /* unknown option */
  91. usage(argv[i]);
  92. }
  93. if (i==argc && (listing || !dumping))
  94. {
  95. dumping=0;
  96. argv[--i]=Output;
  97. }
  98. if (version)
  99. {
  100. printf("%s %s\n",LUA_RELEASE,LUA_COPYRIGHT);
  101. if (version==argc-1) exit(EXIT_SUCCESS);
  102. }
  103. return i;
  104. }
  105. #define toproto(L,i) (clvalue(L->top+(i))->l.p)
  106. static const Proto* combine(lua_State* L, int n)
  107. {
  108. if (n==1)
  109. return toproto(L,-1);
  110. else
  111. {
  112. int i,pc;
  113. Proto* f=luaF_newproto(L);
  114. setptvalue2s(L,L->top,f); incr_top(L);
  115. f->source=luaS_newliteral(L,"=(" PROGNAME ")");
  116. f->maxstacksize=1;
  117. pc=2*n+1;
  118. f->code=luaM_newvector(L,pc,Instruction);
  119. f->sizecode=pc;
  120. f->p=luaM_newvector(L,n,Proto*);
  121. f->sizep=n;
  122. pc=0;
  123. for (i=0; i<n; i++)
  124. {
  125. f->p[i]=toproto(L,i-n-1);
  126. f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
  127. f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
  128. }
  129. f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
  130. return f;
  131. }
  132. }
  133. static int writer(lua_State* L, const void* p, size_t size, void* u)
  134. {
  135. UNUSED(L);
  136. return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
  137. }
  138. struct Smain {
  139. int argc;
  140. char** argv;
  141. };
  142. static int pmain(lua_State* L)
  143. {
  144. struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
  145. int argc=s->argc;
  146. char** argv=s->argv;
  147. const Proto* f;
  148. int i;
  149. if (!lua_checkstack(L,argc)) fatal("too many input files");
  150. for (i=0; i<argc; i++)
  151. {
  152. const char* filename=IS("-") ? NULL : argv[i];
  153. if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
  154. }
  155. f=combine(L,argc);
  156. if (listing) luaU_print(f,listing>1);
  157. if (dumping)
  158. {
  159. FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  160. if (D==NULL) cannot("open");
  161. lua_lock(L);
  162. luaU_dump(L,f,writer,D,stripping);
  163. lua_unlock(L);
  164. if (ferror(D)) cannot("write");
  165. if (fclose(D)) cannot("close");
  166. }
  167. return 0;
  168. }
  169. int main(int argc, char* argv[])
  170. {
  171. lua_State* L;
  172. struct Smain s;
  173. int i=doargs(argc,argv);
  174. argc-=i; argv+=i;
  175. if (argc<=0) usage("no input files given");
  176. L=lua_open();
  177. if (L==NULL) fatal("not enough memory for state");
  178. s.argc=argc;
  179. s.argv=argv;
  180. if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));
  181. lua_close(L);
  182. return EXIT_SUCCESS;
  183. }