Imported from ../lua-5.0.1.tar.gz.
[lua.git] / src / luac / luac.c
blob1aff0bd93f00049d7c432eaf845aab7ce2ed7cec
1 /*
2 ** $Id: luac.c,v 1.44a 2003/04/07 20:34:20 lhf Exp $
3 ** Lua compiler (saves bytecodes to files; also list bytecodes)
4 ** See Copyright Notice in lua.h
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "lua.h"
12 #include "lauxlib.h"
14 #include "lfunc.h"
15 #include "lmem.h"
16 #include "lobject.h"
17 #include "lopcodes.h"
18 #include "lstring.h"
19 #include "lundump.h"
21 #ifndef LUA_DEBUG
22 #define luaB_opentests(L)
23 #endif
25 #ifndef PROGNAME
26 #define PROGNAME "luac" /* program name */
27 #endif
29 #define OUTPUT "luac.out" /* default output file */
31 static int listing=0; /* list bytecodes? */
32 static int dumping=1; /* dump bytecodes? */
33 static int stripping=0; /* strip debug information? */
34 static char Output[]={ OUTPUT }; /* default output file name */
35 static const char* output=Output; /* output file name */
36 static const char* progname=PROGNAME; /* actual program name */
38 static void fatal(const char* message)
40 fprintf(stderr,"%s: %s\n",progname,message);
41 exit(EXIT_FAILURE);
44 static void cannot(const char* name, const char* what, const char* mode)
46 fprintf(stderr,"%s: cannot %s %sput file ",progname,what,mode);
47 perror(name);
48 exit(EXIT_FAILURE);
51 static void usage(const char* message, const char* arg)
53 if (message!=NULL)
55 fprintf(stderr,"%s: ",progname); fprintf(stderr,message,arg); fprintf(stderr,"\n");
57 fprintf(stderr,
58 "usage: %s [options] [filenames]. Available options are:\n"
59 " - process stdin\n"
60 " -l list\n"
61 " -o name output to file `name' (default is \"" OUTPUT "\")\n"
62 " -p parse only\n"
63 " -s strip debug information\n"
64 " -v show version information\n"
65 " -- stop handling options\n",
66 progname);
67 exit(EXIT_FAILURE);
70 #define IS(s) (strcmp(argv[i],s)==0)
72 static int doargs(int argc, char* argv[])
74 int i;
75 if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
76 for (i=1; i<argc; i++)
78 if (*argv[i]!='-') /* end of options; keep it */
79 break;
80 else if (IS("--")) /* end of options; skip it */
82 ++i;
83 break;
85 else if (IS("-")) /* end of options; use stdin */
86 return i;
87 else if (IS("-l")) /* list */
88 listing=1;
89 else if (IS("-o")) /* output file */
91 output=argv[++i];
92 if (output==NULL || *output==0) usage("`-o' needs argument",NULL);
94 else if (IS("-p")) /* parse only */
95 dumping=0;
96 else if (IS("-s")) /* strip debug information */
97 stripping=1;
98 else if (IS("-v")) /* show version */
100 printf("%s %s\n",LUA_VERSION,LUA_COPYRIGHT);
101 if (argc==2) exit(EXIT_SUCCESS);
103 else /* unknown option */
104 usage("unrecognized option `%s'",argv[i]);
106 if (i==argc && (listing || !dumping))
108 dumping=0;
109 argv[--i]=Output;
111 return i;
114 static Proto* toproto(lua_State* L, int i)
116 const Closure* c=(const Closure*)lua_topointer(L,i);
117 return c->l.p;
120 static Proto* combine(lua_State* L, int n)
122 if (n==1)
123 return toproto(L,-1);
124 else
126 int i,pc=0;
127 Proto* f=luaF_newproto(L);
128 f->source=luaS_newliteral(L,"=(" PROGNAME ")");
129 f->maxstacksize=1;
130 f->p=luaM_newvector(L,n,Proto*);
131 f->sizep=n;
132 f->sizecode=2*n+1;
133 f->code=luaM_newvector(L,f->sizecode,Instruction);
134 for (i=0; i<n; i++)
136 f->p[i]=toproto(L,i-n);
137 f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
138 f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
140 f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
141 return f;
145 static void strip(lua_State* L, Proto* f)
147 int i,n=f->sizep;
148 luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
149 luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
150 luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
151 f->lineinfo=NULL; f->sizelineinfo=0;
152 f->locvars=NULL; f->sizelocvars=0;
153 f->upvalues=NULL; f->sizeupvalues=0;
154 f->source=luaS_newliteral(L,"=(none)");
155 for (i=0; i<n; i++) strip(L,f->p[i]);
158 static int writer(lua_State* L, const void* p, size_t size, void* u)
160 UNUSED(L);
161 return fwrite(p,size,1,(FILE*)u)==1;
164 int main(int argc, char* argv[])
166 lua_State* L;
167 Proto* f;
168 int i=doargs(argc,argv);
169 argc-=i; argv+=i;
170 if (argc<=0) usage("no input files given",NULL);
171 L=lua_open();
172 luaB_opentests(L);
173 for (i=0; i<argc; i++)
175 const char* filename=IS("-") ? NULL : argv[i];
176 if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
178 f=combine(L,argc);
179 if (listing) luaU_print(f);
180 if (dumping)
182 FILE* D=fopen(output,"wb");
183 if (D==NULL) cannot(output,"open","out");
184 if (stripping) strip(L,f);
185 lua_lock(L);
186 luaU_dump(L,f,writer,D);
187 lua_unlock(L);
188 if (ferror(D)) cannot(output,"write","out");
189 fclose(D);
191 lua_close(L);
192 return 0;