]>
git.proxmox.com Git - ceph.git/blob - ceph/src/lua/src/luac.c
2 ** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $
3 ** Lua compiler (saves bytecodes to files; also lists bytecodes)
4 ** See Copyright Notice in lua.h
25 static void PrintFunction(const Proto
* f
, int full
);
26 #define luaU_print PrintFunction
28 #define PROGNAME "luac" /* default program name */
29 #define OUTPUT PROGNAME ".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
; /* actual 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
);
44 static void cannot(const char* what
)
46 fprintf(stderr
,"%s: cannot %s %s: %s\n",progname
,what
,output
,strerror(errno
));
50 static void usage(const char* message
)
53 fprintf(stderr
,"%s: unrecognized option '%s'\n",progname
,message
);
55 fprintf(stderr
,"%s: %s\n",progname
,message
);
57 "usage: %s [options] [filenames]\n"
58 "Available options are:\n"
59 " -l list (use -l -l for full listing)\n"
60 " -o name output to file 'name' (default is \"%s\")\n"
62 " -s strip debug information\n"
63 " -v show version information\n"
64 " -- stop handling options\n"
65 " - stop handling options and process stdin\n"
70 #define IS(s) (strcmp(argv[i],s)==0)
72 static int doargs(int argc
, char* argv
[])
76 if (argv
[0]!=NULL
&& *argv
[0]!=0) progname
=argv
[0];
77 for (i
=1; i
<argc
; i
++)
79 if (*argv
[i
]!='-') /* end of options; keep it */
81 else if (IS("--")) /* end of options; skip it */
84 if (version
) ++version
;
87 else if (IS("-")) /* end of options; use stdin */
89 else if (IS("-l")) /* list */
91 else if (IS("-o")) /* output file */
94 if (output
==NULL
|| *output
==0 || (*output
=='-' && output
[1]!=0))
95 usage("'-o' needs argument");
96 if (IS("-")) output
=NULL
;
98 else if (IS("-p")) /* parse only */
100 else if (IS("-s")) /* strip debug information */
102 else if (IS("-v")) /* show version */
104 else /* unknown option */
107 if (i
==argc
&& (listing
|| !dumping
))
114 printf("%s\n",LUA_COPYRIGHT
);
115 if (version
==argc
-1) exit(EXIT_SUCCESS
);
120 #define FUNCTION "(function()end)();"
122 static const char* reader(lua_State
*L
, void *ud
, size_t *size
)
127 *size
=sizeof(FUNCTION
)-1;
137 #define toproto(L,i) getproto(L->top+(i))
139 static const Proto
* combine(lua_State
* L
, int n
)
142 return toproto(L
,-1);
147 if (lua_load(L
,reader
,&i
,"=(" PROGNAME
")",NULL
)!=LUA_OK
) fatal(lua_tostring(L
,-1));
151 f
->p
[i
]=toproto(L
,i
-n
-1);
152 if (f
->p
[i
]->sizeupvalues
>0) f
->p
[i
]->upvalues
[0].instack
=0;
159 static int writer(lua_State
* L
, const void* p
, size_t size
, void* u
)
162 return (fwrite(p
,size
,1,(FILE*)u
)!=1) && (size
!=0);
165 static int pmain(lua_State
* L
)
167 int argc
=(int)lua_tointeger(L
,1);
168 char** argv
=(char**)lua_touserdata(L
,2);
171 if (!lua_checkstack(L
,argc
)) fatal("too many input files");
172 for (i
=0; i
<argc
; i
++)
174 const char* filename
=IS("-") ? NULL
: argv
[i
];
175 if (luaL_loadfile(L
,filename
)!=LUA_OK
) fatal(lua_tostring(L
,-1));
178 if (listing
) luaU_print(f
,listing
>1);
181 FILE* D
= (output
==NULL
) ? stdout
: fopen(output
,"wb");
182 if (D
==NULL
) cannot("open");
184 luaU_dump(L
,f
,writer
,D
,stripping
);
186 if (ferror(D
)) cannot("write");
187 if (fclose(D
)) cannot("close");
192 int main(int argc
, char* argv
[])
195 int i
=doargs(argc
,argv
);
197 if (argc
<=0) usage("no input files given");
199 if (L
==NULL
) fatal("cannot create state: not enough memory");
200 lua_pushcfunction(L
,&pmain
);
201 lua_pushinteger(L
,argc
);
202 lua_pushlightuserdata(L
,argv
);
203 if (lua_pcall(L
,2,0,0)!=LUA_OK
) fatal(lua_tostring(L
,-1));
209 ** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $
211 ** See Copyright Notice in lua.h
222 #include "lopcodes.h"
224 #define VOID(p) ((const void*)(p))
226 static void PrintString(const TString
* ts
)
228 const char* s
=getstr(ts
);
229 size_t i
,n
=tsslen(ts
);
233 int c
=(int)(unsigned char)s
[i
];
236 case '"': printf("\\\""); break;
237 case '\\': printf("\\\\"); break;
238 case '\a': printf("\\a"); break;
239 case '\b': printf("\\b"); break;
240 case '\f': printf("\\f"); break;
241 case '\n': printf("\\n"); break;
242 case '\r': printf("\\r"); break;
243 case '\t': printf("\\t"); break;
244 case '\v': printf("\\v"); break;
245 default: if (isprint(c
))
254 static void PrintConstant(const Proto
* f
, int i
)
256 const TValue
* o
=&f
->k
[i
];
263 printf(bvalue(o
) ? "true" : "false");
268 sprintf(buff
,LUA_NUMBER_FMT
,fltvalue(o
));
270 if (buff
[strspn(buff
,"-0123456789")]=='\0') printf(".0");
274 printf(LUA_INTEGER_FMT
,ivalue(o
));
276 case LUA_TSHRSTR
: case LUA_TLNGSTR
:
277 PrintString(tsvalue(o
));
279 default: /* cannot happen */
280 printf("? type=%d",ttype(o
));
285 #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
286 #define MYK(x) (-1-(x))
288 static void PrintCode(const Proto
* f
)
290 const Instruction
* code
=f
->code
;
291 int pc
,n
=f
->sizecode
;
292 for (pc
=0; pc
<n
; pc
++)
294 Instruction i
=code
[pc
];
295 OpCode o
=GET_OPCODE(i
);
301 int sbx
=GETARG_sBx(i
);
302 int line
=getfuncline(f
,pc
);
303 printf("\t%d\t",pc
+1);
304 if (line
>0) printf("[%d]\t",line
); else printf("[-]\t");
305 printf("%-9s\t",luaP_opnames
[o
]);
306 switch (getOpMode(o
))
310 if (getBMode(o
)!=OpArgN
) printf(" %d",ISK(b
) ? (MYK(INDEXK(b
))) : b
);
311 if (getCMode(o
)!=OpArgN
) printf(" %d",ISK(c
) ? (MYK(INDEXK(c
))) : c
);
315 if (getBMode(o
)==OpArgK
) printf(" %d",MYK(bx
));
316 if (getBMode(o
)==OpArgU
) printf(" %d",bx
);
319 printf("%d %d",a
,sbx
);
322 printf("%d",MYK(ax
));
328 printf("\t; "); PrintConstant(f
,bx
);
332 printf("\t; %s",UPVALNAME(b
));
335 printf("\t; %s",UPVALNAME(b
));
336 if (ISK(c
)) { printf(" "); PrintConstant(f
,INDEXK(c
)); }
339 printf("\t; %s",UPVALNAME(a
));
340 if (ISK(b
)) { printf(" "); PrintConstant(f
,INDEXK(b
)); }
341 if (ISK(c
)) { printf(" "); PrintConstant(f
,INDEXK(c
)); }
345 if (ISK(c
)) { printf("\t; "); PrintConstant(f
,INDEXK(c
)); }
362 if (ISK(b
) || ISK(c
))
365 if (ISK(b
)) PrintConstant(f
,INDEXK(b
)); else printf("-");
367 if (ISK(c
)) PrintConstant(f
,INDEXK(c
)); else printf("-");
374 printf("\t; to %d",sbx
+pc
+2);
377 printf("\t; %p",VOID(f
->p
[bx
]));
380 if (c
==0) printf("\t; %d",(int)code
[++pc
]); else printf("\t; %d",c
);
383 printf("\t; "); PrintConstant(f
,ax
);
392 #define SS(x) ((x==1)?"":"s")
393 #define S(x) (int)(x),SS(x)
395 static void PrintHeader(const Proto
* f
)
397 const char* s
=f
->source
? getstr(f
->source
) : "=?";
398 if (*s
=='@' || *s
=='=')
400 else if (*s
==LUA_SIGNATURE
[0])
404 printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
405 (f
->linedefined
==0)?"main":"function",s
,
406 f
->linedefined
,f
->lastlinedefined
,
407 S(f
->sizecode
),VOID(f
));
408 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
409 (int)(f
->numparams
),f
->is_vararg
?"+":"",SS(f
->numparams
),
410 S(f
->maxstacksize
),S(f
->sizeupvalues
));
411 printf("%d local%s, %d constant%s, %d function%s\n",
412 S(f
->sizelocvars
),S(f
->sizek
),S(f
->sizep
));
415 static void PrintDebug(const Proto
* f
)
419 printf("constants (%d) for %p:\n",n
,VOID(f
));
422 printf("\t%d\t",i
+1);
427 printf("locals (%d) for %p:\n",n
,VOID(f
));
430 printf("\t%d\t%s\t%d\t%d\n",
431 i
,getstr(f
->locvars
[i
].varname
),f
->locvars
[i
].startpc
+1,f
->locvars
[i
].endpc
+1);
434 printf("upvalues (%d) for %p:\n",n
,VOID(f
));
437 printf("\t%d\t%s\t%d\t%d\n",
438 i
,UPVALNAME(i
),f
->upvalues
[i
].instack
,f
->upvalues
[i
].idx
);
442 static void PrintFunction(const Proto
* f
, int full
)
447 if (full
) PrintDebug(f
);
448 for (i
=0; i
<n
; i
++) PrintFunction(f
->p
[i
],full
);