Line data Source code
1 : /*
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
5 : */
6 :
7 : #include <errno.h>
8 : #include <stdio.h>
9 : #include <stdlib.h>
10 : #include <string.h>
11 :
12 : #define luac_c
13 : #define LUA_CORE
14 :
15 : #include "lua.h"
16 : #include "lauxlib.h"
17 :
18 : #include "lobject.h"
19 : #include "lstate.h"
20 : #include "lundump.h"
21 :
22 : static void PrintFunction(const Proto* f, int full);
23 : #define luaU_print PrintFunction
24 :
25 : #define PROGNAME "luac" /* default program name */
26 : #define OUTPUT PROGNAME ".out" /* default output file */
27 :
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 */
34 :
35 7 : static void fatal(const char* message)
36 : {
37 7 : fprintf(stderr,"%s: %s\n",progname,message);
38 7 : exit(EXIT_FAILURE);
39 : }
40 :
41 0 : static void cannot(const char* what)
42 : {
43 0 : fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
44 0 : exit(EXIT_FAILURE);
45 : }
46 :
47 0 : static void usage(const char* message)
48 : {
49 0 : if (*message=='-')
50 0 : fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
51 : else
52 0 : fprintf(stderr,"%s: %s\n",progname,message);
53 0 : fprintf(stderr,
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"
58 : " -p parse only\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"
63 : ,progname,Output);
64 0 : exit(EXIT_FAILURE);
65 : }
66 :
67 : #define IS(s) (strcmp(argv[i],s)==0)
68 :
69 20 : static int doargs(int argc, char* argv[])
70 : {
71 : int i;
72 20 : int version=0;
73 20 : if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
74 39 : for (i=1; i<argc; i++)
75 : {
76 37 : if (*argv[i]!='-') /* end of options; keep it */
77 15 : break;
78 22 : else if (IS("--")) /* end of options; skip it */
79 : {
80 1 : ++i;
81 1 : if (version) ++version;
82 1 : break;
83 : }
84 21 : else if (IS("-")) /* end of options; use stdin */
85 2 : break;
86 19 : else if (IS("-l")) /* list */
87 8 : ++listing;
88 11 : else if (IS("-o")) /* output file */
89 : {
90 3 : output=argv[++i];
91 3 : if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
92 0 : usage(LUA_QL("-o") " needs argument");
93 3 : if (IS("-")) output=NULL;
94 : }
95 8 : else if (IS("-p")) /* parse only */
96 3 : dumping=0;
97 5 : else if (IS("-s")) /* strip debug information */
98 2 : stripping=1;
99 3 : else if (IS("-v")) /* show version */
100 3 : ++version;
101 : else /* unknown option */
102 0 : usage(argv[i]);
103 : }
104 20 : if (i==argc && (listing || !dumping))
105 : {
106 1 : dumping=0;
107 1 : argv[--i]=Output;
108 : }
109 20 : if (version)
110 : {
111 3 : printf("%s\n",LUA_COPYRIGHT);
112 3 : if (version==argc-1) exit(EXIT_SUCCESS);
113 : }
114 18 : return i;
115 : }
116 :
117 : #define FUNCTION "(function()end)();"
118 :
119 3 : static const char* reader(lua_State *L, void *ud, size_t *size)
120 : {
121 : UNUSED(L);
122 3 : if ((*(int*)ud)--)
123 : {
124 2 : *size=sizeof(FUNCTION)-1;
125 2 : return FUNCTION;
126 : }
127 : else
128 : {
129 1 : *size=0;
130 1 : return NULL;
131 : }
132 : }
133 :
134 : #define toproto(L,i) getproto(L->top+(i))
135 :
136 11 : static const Proto* combine(lua_State* L, int n)
137 : {
138 11 : if (n==1)
139 10 : return toproto(L,-1);
140 : else
141 : {
142 : Proto* f;
143 1 : int i=n;
144 1 : if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
145 1 : f=toproto(L,-1);
146 3 : for (i=0; i<n; i++)
147 : {
148 2 : f->p[i]=toproto(L,i-n-1);
149 2 : if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
150 : }
151 1 : f->sizelineinfo=0;
152 1 : return f;
153 : }
154 : }
155 :
156 523 : static int writer(lua_State* L, const void* p, size_t size, void* u)
157 : {
158 : UNUSED(L);
159 523 : return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
160 : }
161 :
162 18 : static int pmain(lua_State* L)
163 : {
164 18 : int argc=(int)lua_tointeger(L,1);
165 18 : char** argv=(char**)lua_touserdata(L,2);
166 : const Proto* f;
167 : int i;
168 18 : if (!lua_checkstack(L,argc)) fatal("too many input files");
169 30 : for (i=0; i<argc; i++)
170 : {
171 19 : const char* filename=IS("-") ? NULL : argv[i];
172 19 : if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
173 : }
174 11 : f=combine(L,argc);
175 11 : if (listing) luaU_print(f,listing>1);
176 11 : if (dumping)
177 : {
178 8 : FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
179 8 : if (D==NULL) cannot("open");
180 : lua_lock(L);
181 8 : luaU_dump(L,f,writer,D,stripping);
182 : lua_unlock(L);
183 8 : if (ferror(D)) cannot("write");
184 8 : if (fclose(D)) cannot("close");
185 : }
186 11 : return 0;
187 : }
188 :
189 20 : int main(int argc, char* argv[])
190 : {
191 : lua_State* L;
192 20 : int i=doargs(argc,argv);
193 18 : argc-=i; argv+=i;
194 18 : if (argc<=0) usage("no input files given");
195 18 : L=luaL_newstate();
196 18 : if (L==NULL) fatal("cannot create state: not enough memory");
197 18 : lua_pushcfunction(L,&pmain);
198 18 : lua_pushinteger(L,argc);
199 18 : lua_pushlightuserdata(L,argv);
200 18 : if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
201 11 : lua_close(L);
202 11 : return EXIT_SUCCESS;
203 : }
204 :
205 : /*
206 : ** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $
207 : ** print bytecodes
208 : ** See Copyright Notice in lua.h
209 : */
210 :
211 : #include <ctype.h>
212 : #include <stdio.h>
213 :
214 : #define luac_c
215 : #define LUA_CORE
216 :
217 : #include "ldebug.h"
218 : #include "lobject.h"
219 : #include "lopcodes.h"
220 :
221 : #define VOID(p) ((const void*)(p))
222 :
223 44 : static void PrintString(const TString* ts)
224 : {
225 44 : const char* s=getstr(ts);
226 44 : size_t i,n=ts->tsv.len;
227 44 : printf("%c",'"');
228 263 : for (i=0; i<n; i++)
229 : {
230 219 : int c=(int)(unsigned char)s[i];
231 219 : switch (c)
232 : {
233 3 : case '"': printf("\\\""); break;
234 3 : case '\\': printf("\\\\"); break;
235 3 : case '\a': printf("\\a"); break;
236 3 : case '\b': printf("\\b"); break;
237 3 : case '\f': printf("\\f"); break;
238 3 : case '\n': printf("\\n"); break;
239 3 : case '\r': printf("\\r"); break;
240 3 : case '\t': printf("\\t"); break;
241 3 : case '\v': printf("\\v"); break;
242 192 : default: if (isprint(c))
243 189 : printf("%c",c);
244 : else
245 3 : printf("\\%03d",c);
246 : }
247 : }
248 44 : printf("%c",'"');
249 44 : }
250 :
251 58 : static void PrintConstant(const Proto* f, int i)
252 : {
253 58 : const TValue* o=&f->k[i];
254 58 : switch (ttypenv(o))
255 : {
256 3 : case LUA_TNIL:
257 3 : printf("nil");
258 3 : break;
259 3 : case LUA_TBOOLEAN:
260 3 : printf(bvalue(o) ? "true" : "false");
261 3 : break;
262 8 : case LUA_TNUMBER:
263 8 : printf(LUA_NUMBER_FMT,nvalue(o));
264 8 : break;
265 44 : case LUA_TSTRING:
266 44 : PrintString(rawtsvalue(o));
267 44 : break;
268 0 : default: /* cannot happen */
269 0 : printf("? type=%d",ttype(o));
270 0 : break;
271 : }
272 58 : }
273 :
274 : #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
275 : #define MYK(x) (-1-(x))
276 :
277 10 : static void PrintCode(const Proto* f)
278 : {
279 10 : const Instruction* code=f->code;
280 10 : int pc,n=f->sizecode;
281 82 : for (pc=0; pc<n; pc++)
282 : {
283 72 : Instruction i=code[pc];
284 72 : OpCode o=GET_OPCODE(i);
285 72 : int a=GETARG_A(i);
286 72 : int b=GETARG_B(i);
287 72 : int c=GETARG_C(i);
288 72 : int ax=GETARG_Ax(i);
289 72 : int bx=GETARG_Bx(i);
290 72 : int sbx=GETARG_sBx(i);
291 72 : int line=getfuncline(f,pc);
292 72 : printf("\t%d\t",pc+1);
293 72 : if (line>0) printf("[%d]\t",line); else printf("[-]\t");
294 72 : printf("%-9s\t",luaP_opnames[o]);
295 72 : switch (getOpMode(o))
296 : {
297 54 : case iABC:
298 54 : printf("%d",a);
299 54 : if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b);
300 54 : if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c);
301 54 : break;
302 16 : case iABx:
303 16 : printf("%d",a);
304 16 : if (getBMode(o)==OpArgK) printf(" %d",MYK(bx));
305 16 : if (getBMode(o)==OpArgU) printf(" %d",bx);
306 16 : break;
307 2 : case iAsBx:
308 2 : printf("%d %d",a,sbx);
309 2 : break;
310 0 : case iAx:
311 0 : printf("%d",MYK(ax));
312 0 : break;
313 : }
314 72 : switch (o)
315 : {
316 12 : case OP_LOADK:
317 12 : printf("\t; "); PrintConstant(f,bx);
318 12 : break;
319 2 : case OP_GETUPVAL:
320 : case OP_SETUPVAL:
321 2 : printf("\t; %s",UPVALNAME(b));
322 2 : break;
323 8 : case OP_GETTABUP:
324 8 : printf("\t; %s",UPVALNAME(b));
325 8 : if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
326 8 : break;
327 8 : case OP_SETTABUP:
328 8 : printf("\t; %s",UPVALNAME(a));
329 8 : if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); }
330 8 : if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
331 8 : break;
332 2 : case OP_GETTABLE:
333 : case OP_SELF:
334 2 : if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
335 2 : break;
336 4 : case OP_SETTABLE:
337 : case OP_ADD:
338 : case OP_SUB:
339 : case OP_MUL:
340 : case OP_DIV:
341 : case OP_POW:
342 : case OP_EQ:
343 : case OP_LT:
344 : case OP_LE:
345 4 : if (ISK(b) || ISK(c))
346 : {
347 4 : printf("\t; ");
348 4 : if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
349 4 : printf(" ");
350 4 : if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
351 : }
352 4 : break;
353 2 : case OP_JMP:
354 : case OP_FORLOOP:
355 : case OP_FORPREP:
356 : case OP_TFORLOOP:
357 2 : printf("\t; to %d",sbx+pc+2);
358 2 : break;
359 4 : case OP_CLOSURE:
360 4 : printf("\t; %p",VOID(f->p[bx]));
361 4 : break;
362 4 : case OP_SETLIST:
363 4 : if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c);
364 4 : break;
365 0 : case OP_EXTRAARG:
366 0 : printf("\t; "); PrintConstant(f,ax);
367 0 : break;
368 26 : default:
369 26 : break;
370 : }
371 72 : printf("\n");
372 : }
373 10 : }
374 :
375 : #define SS(x) ((x==1)?"":"s")
376 : #define S(x) (int)(x),SS(x)
377 :
378 10 : static void PrintHeader(const Proto* f)
379 : {
380 10 : const char* s=f->source ? getstr(f->source) : "=?";
381 10 : if (*s=='@' || *s=='=')
382 10 : s++;
383 0 : else if (*s==LUA_SIGNATURE[0])
384 0 : s="(bstring)";
385 : else
386 0 : s="(string)";
387 20 : printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
388 10 : (f->linedefined==0)?"main":"function",s,
389 10 : f->linedefined,f->lastlinedefined,
390 20 : S(f->sizecode),VOID(f));
391 40 : printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
392 20 : (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
393 30 : S(f->maxstacksize),S(f->sizeupvalues));
394 30 : printf("%d local%s, %d constant%s, %d function%s\n",
395 40 : S(f->sizelocvars),S(f->sizek),S(f->sizep));
396 10 : }
397 :
398 4 : static void PrintDebug(const Proto* f)
399 : {
400 : int i,n;
401 4 : n=f->sizek;
402 4 : printf("constants (%d) for %p:\n",n,VOID(f));
403 20 : for (i=0; i<n; i++)
404 : {
405 16 : printf("\t%d\t",i+1);
406 16 : PrintConstant(f,i);
407 16 : printf("\n");
408 : }
409 4 : n=f->sizelocvars;
410 4 : printf("locals (%d) for %p:\n",n,VOID(f));
411 9 : for (i=0; i<n; i++)
412 : {
413 5 : printf("\t%d\t%s\t%d\t%d\n",
414 5 : i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
415 : }
416 4 : n=f->sizeupvalues;
417 4 : printf("upvalues (%d) for %p:\n",n,VOID(f));
418 8 : for (i=0; i<n; i++)
419 : {
420 4 : printf("\t%d\t%s\t%d\t%d\n",
421 4 : i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
422 : }
423 4 : }
424 :
425 10 : static void PrintFunction(const Proto* f, int full)
426 : {
427 10 : int i,n=f->sizep;
428 10 : PrintHeader(f);
429 10 : PrintCode(f);
430 10 : if (full) PrintDebug(f);
431 14 : for (i=0; i<n; i++) PrintFunction(f->p[i],full);
432 10 : }
|