2 ** $Id: luac.c,v 1.69 2011/11/29 17:46:33 lhf Exp $
3 ** Lua compiler (saves bytecodes to files; also list bytecodes)
4 ** See Copyright Notice in lua.h
22 static void PrintFunction(const Proto* f, int full);
23 #define luaU_print PrintFunction
25 #define PROGNAME "luac" /* default program name */
26 #define OUTPUT PROGNAME ".out" /* default output file */
28 static int listing=0; /* list bytecodes? */
29 static int dumping=1; /* dump bytecodes? */
30 static int stripping=0; /* strip debug information? */
31 static char Output[]={ OUTPUT }; /* default output file name */
32 static const char* output=Output; /* actual output file name */
33 static const char* progname=PROGNAME; /* actual program name */
35 static void fatal(const char* message)
37 fprintf(stderr,"%s: %s\n",progname,message);
41 static void cannot(const char* what)
43 fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
47 static void usage(const char* message)
50 fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
52 fprintf(stderr,"%s: %s\n",progname,message);
54 "usage: %s [options] [filenames]\n"
55 "Available options are:\n"
56 " -l list (use -l -l for full listing)\n"
57 " -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
59 " -s strip debug information\n"
60 " -v show version information\n"
61 " -- stop handling options\n"
62 " - stop handling options and process stdin\n"
67 #define IS(s) (strcmp(argv[i],s)==0)
69 static int doargs(int argc, char* argv[])
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 */
78 else if (IS("--")) /* end of options; skip it */
81 if (version) ++version;
84 else if (IS("-")) /* end of options; use stdin */
86 else if (IS("-l")) /* list */
88 else if (IS("-o")) /* output file */
91 if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
92 usage(LUA_QL("-o") " needs argument");
93 if (IS("-")) output=NULL;
95 else if (IS("-p")) /* parse only */
97 else if (IS("-s")) /* strip debug information */
99 else if (IS("-v")) /* show version */
101 else /* unknown option */
104 if (i==argc && (listing || !dumping))
111 printf("%s\n",LUA_COPYRIGHT);
112 if (version==argc-1) exit(EXIT_SUCCESS);
117 #define FUNCTION "(function()end)();"
119 static const char* reader(lua_State *L, void *ud, size_t *size)
124 *size=sizeof(FUNCTION)-1;
134 #define toproto(L,i) getproto(L->top+(i))
136 static const Proto* combine(lua_State* L, int n)
139 return toproto(L,-1);
144 if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
148 f->p[i]=toproto(L,i-n-1);
149 if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
156 static int writer(lua_State* L, const void* p, size_t size, void* u)
159 return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
162 static int pmain(lua_State* L)
164 int argc=(int)lua_tointeger(L,1);
165 char** argv=(char**)lua_touserdata(L,2);
168 if (!lua_checkstack(L,argc)) fatal("too many input files");
169 for (i=0; i<argc; i++)
171 const char* filename=IS("-") ? NULL : argv[i];
172 if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
175 if (listing) luaU_print(f,listing>1);
178 FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
179 if (D==NULL) cannot("open");
181 luaU_dump(L,f,writer,D,stripping);
183 if (ferror(D)) cannot("write");
184 if (fclose(D)) cannot("close");
189 int main(int argc, char* argv[])
192 int i=doargs(argc,argv);
194 if (argc<=0) usage("no input files given");
196 if (L==NULL) fatal("cannot create state: not enough memory");
197 lua_pushcfunction(L,&pmain);
198 lua_pushinteger(L,argc);
199 lua_pushlightuserdata(L,argv);
200 if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
206 ** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $
208 ** See Copyright Notice in lua.h
219 #include "lopcodes.h"
221 #define VOID(p) ((const void*)(p))
223 static void PrintString(const TString* ts)
225 const char* s=getstr(ts);
226 size_t i,n=ts->tsv.len;
230 int c=(int)(unsigned char)s[i];
233 case '"': printf("\\\""); break;
234 case '\\': printf("\\\\"); break;
235 case '\a': printf("\\a"); break;
236 case '\b': printf("\\b"); break;
237 case '\f': printf("\\f"); break;
238 case '\n': printf("\\n"); break;
239 case '\r': printf("\\r"); break;
240 case '\t': printf("\\t"); break;
241 case '\v': printf("\\v"); break;
242 default: if (isprint(c))
251 static void PrintConstant(const Proto* f, int i)
253 const TValue* o=&f->k[i];
260 printf(bvalue(o) ? "true" : "false");
263 printf(LUA_NUMBER_FMT,nvalue(o));
266 PrintString(rawtsvalue(o));
268 default: /* cannot happen */
269 printf("? type=%d",ttype(o));
274 #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
275 #define MYK(x) (-1-(x))
277 static void PrintCode(const Proto* f)
279 const Instruction* code=f->code;
280 int pc,n=f->sizecode;
281 for (pc=0; pc<n; pc++)
283 Instruction i=code[pc];
284 OpCode o=GET_OPCODE(i);
290 int sbx=GETARG_sBx(i);
291 int line=getfuncline(f,pc);
292 printf("\t%d\t",pc+1);
293 if (line>0) printf("[%d]\t",line); else printf("[-]\t");
294 printf("%-9s\t",luaP_opnames[o]);
295 switch (getOpMode(o))
299 if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b);
300 if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c);
304 if (getBMode(o)==OpArgK) printf(" %d",MYK(bx));
305 if (getBMode(o)==OpArgU) printf(" %d",bx);
308 printf("%d %d",a,sbx);
311 printf("%d",MYK(ax));
317 printf("\t; "); PrintConstant(f,bx);
321 printf("\t; %s",UPVALNAME(b));
324 printf("\t; %s",UPVALNAME(b));
325 if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
328 printf("\t; %s",UPVALNAME(a));
329 if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); }
330 if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
334 if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
345 if (ISK(b) || ISK(c))
348 if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
350 if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
357 printf("\t; to %d",sbx+pc+2);
360 printf("\t; %p",VOID(f->p[bx]));
363 if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c);
366 printf("\t; "); PrintConstant(f,ax);
375 #define SS(x) ((x==1)?"":"s")
376 #define S(x) (int)(x),SS(x)
378 static void PrintHeader(const Proto* f)
380 const char* s=f->source ? getstr(f->source) : "=?";
381 if (*s=='@' || *s=='=')
383 else if (*s==LUA_SIGNATURE[0])
387 printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
388 (f->linedefined==0)?"main":"function",s,
389 f->linedefined,f->lastlinedefined,
390 S(f->sizecode),VOID(f));
391 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
392 (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
393 S(f->maxstacksize),S(f->sizeupvalues));
394 printf("%d local%s, %d constant%s, %d function%s\n",
395 S(f->sizelocvars),S(f->sizek),S(f->sizep));
398 static void PrintDebug(const Proto* f)
402 printf("constants (%d) for %p:\n",n,VOID(f));
405 printf("\t%d\t",i+1);
410 printf("locals (%d) for %p:\n",n,VOID(f));
413 printf("\t%d\t%s\t%d\t%d\n",
414 i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
417 printf("upvalues (%d) for %p:\n",n,VOID(f));
420 printf("\t%d\t%s\t%d\t%d\n",
421 i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
425 static void PrintFunction(const Proto* f, int full)
430 if (full) PrintDebug(f);
431 for (i=0; i<n; i++) PrintFunction(f->p[i],full);