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