Remove building with NOCRYPTO option
[minix.git] / external / mit / lua / dist / src / luac.c
blob76b542c3335d88e4f6fa3a53c9c4cc5906634758
1 /* $NetBSD: luac.c,v 1.4 2015/10/08 13:21:00 mbalmer Exp $ */
3 /*
4 ** Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp
5 ** Lua compiler (saves bytecodes to files; also lists bytecodes)
6 ** See Copyright Notice in lua.h
7 */
9 #define luac_c
10 #define LUA_CORE
12 #include "lprefix.h"
14 #include <ctype.h>
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "lua.h"
21 #include "lauxlib.h"
23 #include "lobject.h"
24 #include "lstate.h"
25 #include "lundump.h"
27 static void PrintFunction(const Proto* f, int full);
28 #define luaU_print PrintFunction
30 #define PROGNAME "luac" /* default program name */
31 #define OUTPUT PROGNAME ".out" /* default output file */
33 static int listing=0; /* list bytecodes? */
34 static int dumping=1; /* dump bytecodes? */
35 static int stripping=0; /* strip debug information? */
36 static char Output[]={ OUTPUT }; /* default output file name */
37 static const char* output=Output; /* actual output file name */
38 static const char* progname=PROGNAME; /* actual program name */
40 static void fatal(const char* message)
42 fprintf(stderr,"%s: %s\n",progname,message);
43 exit(EXIT_FAILURE);
46 static void cannot(const char* what)
48 fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
49 exit(EXIT_FAILURE);
52 static void usage(const char* message)
54 if (*message=='-')
55 fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message);
56 else
57 fprintf(stderr,"%s: %s\n",progname,message);
58 fprintf(stderr,
59 "usage: %s [options] [filenames]\n"
60 "Available options are:\n"
61 " -l list (use -l -l for full listing)\n"
62 " -o name output to file 'name' (default is \"%s\")\n"
63 " -p parse only\n"
64 " -s strip debug information\n"
65 " -v show version information\n"
66 " -- stop handling options\n"
67 " - stop handling options and process stdin\n"
68 ,progname,Output);
69 exit(EXIT_FAILURE);
72 #define IS(s) (strcmp(argv[i],s)==0)
74 static int doargs(int argc, char* argv[])
76 int i;
77 int version=0;
78 if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
79 for (i=1; i<argc; i++)
81 if (*argv[i]!='-') /* end of options; keep it */
82 break;
83 else if (IS("--")) /* end of options; skip it */
85 ++i;
86 if (version) ++version;
87 break;
89 else if (IS("-")) /* end of options; use stdin */
90 break;
91 else if (IS("-l")) /* list */
92 ++listing;
93 else if (IS("-o")) /* output file */
95 output=argv[++i];
96 if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
97 usage("'-o' needs argument");
98 if (IS("-")) output=NULL;
100 else if (IS("-p")) /* parse only */
101 dumping=0;
102 else if (IS("-s")) /* strip debug information */
103 stripping=1;
104 else if (IS("-v")) /* show version */
105 ++version;
106 else /* unknown option */
107 usage(argv[i]);
109 if (i==argc && (listing || !dumping))
111 dumping=0;
112 argv[--i]=Output;
114 if (version)
116 printf("%s\n",LUA_COPYRIGHT);
117 if (version==argc-1) exit(EXIT_SUCCESS);
119 return i;
122 #define FUNCTION "(function()end)();"
124 static const char* reader(lua_State *L, void *ud, size_t *size)
126 UNUSED(L);
127 if ((*(int*)ud)--)
129 *size=sizeof(FUNCTION)-1;
130 return FUNCTION;
132 else
134 *size=0;
135 return NULL;
139 #define toproto(L,i) getproto(L->top+(i))
141 static const Proto* combine(lua_State* L, int n)
143 if (n==1)
144 return toproto(L,-1);
145 else
147 Proto* f;
148 int i=n;
149 if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
150 f=toproto(L,-1);
151 for (i=0; i<n; i++)
153 f->p[i]=toproto(L,i-n-1);
154 if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
156 f->sizelineinfo=0;
157 return f;
161 static int writer(lua_State* L, const void* p, size_t size, void* u)
163 UNUSED(L);
164 return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
167 static int pmain(lua_State* L)
169 int argc=(int)lua_tointeger(L,1);
170 char** argv=(char**)lua_touserdata(L,2);
171 const Proto* f;
172 int i;
173 if (!lua_checkstack(L,argc)) fatal("too many input files");
174 for (i=0; i<argc; i++)
176 const char* filename=IS("-") ? NULL : argv[i];
177 if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
179 f=combine(L,argc);
180 if (listing) luaU_print(f,listing>1);
181 if (dumping)
183 FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
184 if (D==NULL) cannot("open");
185 lua_lock(L);
186 luaU_dump(L,f,writer,D,stripping);
187 lua_unlock(L);
188 if (ferror(D)) cannot("write");
189 if (fclose(D)) cannot("close");
191 return 0;
194 int main(int argc, char* argv[])
196 lua_State* L;
197 int i=doargs(argc,argv);
198 argc-=i; argv+=i;
199 if (argc<=0) usage("no input files given");
200 L=luaL_newstate();
201 if (L==NULL) fatal("cannot create state: not enough memory");
202 lua_pushcfunction(L,&pmain);
203 lua_pushinteger(L,argc);
204 lua_pushlightuserdata(L,argv);
205 if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
206 lua_close(L);
207 return EXIT_SUCCESS;
211 ** Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp
212 ** print bytecodes
213 ** See Copyright Notice in lua.h
216 #include <ctype.h>
217 #include <stdio.h>
219 #define luac_c
220 #define LUA_CORE
222 #include "ldebug.h"
223 #include "lobject.h"
224 #include "lopcodes.h"
226 #define VOID(p) ((const void*)(p))
228 static void PrintString(const TString* ts)
230 const char* s=getstr(ts);
231 size_t i,n=tsslen(ts);
232 printf("%c",'"');
233 for (i=0; i<n; i++)
235 int c=(int)(unsigned char)s[i];
236 switch (c)
238 case '"': printf("\\\""); break;
239 case '\\': printf("\\\\"); break;
240 case '\a': printf("\\a"); break;
241 case '\b': printf("\\b"); break;
242 case '\f': printf("\\f"); break;
243 case '\n': printf("\\n"); break;
244 case '\r': printf("\\r"); break;
245 case '\t': printf("\\t"); break;
246 case '\v': printf("\\v"); break;
247 default: if (isprint(c))
248 printf("%c",c);
249 else
250 printf("\\%03d",c);
253 printf("%c",'"');
256 static void PrintConstant(const Proto* f, int i)
258 const TValue* o=&f->k[i];
259 switch (ttype(o))
261 case LUA_TNIL:
262 printf("nil");
263 break;
264 case LUA_TBOOLEAN:
265 printf(bvalue(o) ? "true" : "false");
266 break;
267 case LUA_TNUMFLT:
269 char buff[100];
270 sprintf(buff,LUA_NUMBER_FMT,fltvalue(o));
271 printf("%s",buff);
272 if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0");
273 break;
275 case LUA_TNUMINT:
276 printf(LUA_INTEGER_FMT,ivalue(o));
277 break;
278 case LUA_TSHRSTR: case LUA_TLNGSTR:
279 PrintString(tsvalue(o));
280 break;
281 default: /* cannot happen */
282 printf("? type=%d",ttype(o));
283 break;
287 #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
288 #define MYK(x) (-1-(x))
290 static void PrintCode(const Proto* f)
292 const Instruction* code=f->code;
293 int pc,n=f->sizecode;
294 for (pc=0; pc<n; pc++)
296 Instruction i=code[pc];
297 OpCode o=GET_OPCODE(i);
298 int a=GETARG_A(i);
299 int b=GETARG_B(i);
300 int c=GETARG_C(i);
301 int ax=GETARG_Ax(i);
302 int bx=GETARG_Bx(i);
303 int sbx=GETARG_sBx(i);
304 int line=getfuncline(f,pc);
305 printf("\t%d\t",pc+1);
306 if (line>0) printf("[%d]\t",line); else printf("[-]\t");
307 printf("%-9s\t",luaP_opnames[o]);
308 switch (getOpMode(o))
310 case iABC:
311 printf("%d",a);
312 if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b);
313 if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c);
314 break;
315 case iABx:
316 printf("%d",a);
317 if (getBMode(o)==OpArgK) printf(" %d",MYK(bx));
318 if (getBMode(o)==OpArgU) printf(" %d",bx);
319 break;
320 case iAsBx:
321 printf("%d %d",a,sbx);
322 break;
323 case iAx:
324 printf("%d",MYK(ax));
325 break;
327 switch (o)
329 case OP_LOADK:
330 printf("\t; "); PrintConstant(f,bx);
331 break;
332 case OP_GETUPVAL:
333 case OP_SETUPVAL:
334 printf("\t; %s",UPVALNAME(b));
335 break;
336 case OP_GETTABUP:
337 printf("\t; %s",UPVALNAME(b));
338 if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
339 break;
340 case OP_SETTABUP:
341 printf("\t; %s",UPVALNAME(a));
342 if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); }
343 if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
344 break;
345 case OP_GETTABLE:
346 case OP_SELF:
347 if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
348 break;
349 case OP_SETTABLE:
350 case OP_ADD:
351 case OP_SUB:
352 case OP_MUL:
353 case OP_POW:
354 case OP_DIV:
355 case OP_IDIV:
356 case OP_BAND:
357 case OP_BOR:
358 case OP_BXOR:
359 case OP_SHL:
360 case OP_SHR:
361 case OP_EQ:
362 case OP_LT:
363 case OP_LE:
364 if (ISK(b) || ISK(c))
366 printf("\t; ");
367 if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
368 printf(" ");
369 if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
371 break;
372 case OP_JMP:
373 case OP_FORLOOP:
374 case OP_FORPREP:
375 case OP_TFORLOOP:
376 printf("\t; to %d",sbx+pc+2);
377 break;
378 case OP_CLOSURE:
379 printf("\t; %p",VOID(f->p[bx]));
380 break;
381 case OP_SETLIST:
382 if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c);
383 break;
384 case OP_EXTRAARG:
385 printf("\t; "); PrintConstant(f,ax);
386 break;
387 default:
388 break;
390 printf("\n");
394 #define SS(x) ((x==1)?"":"s")
395 #define S(x) (int)(x),SS(x)
397 static void PrintHeader(const Proto* f)
399 const char* s=f->source ? getstr(f->source) : "=?";
400 if (*s=='@' || *s=='=')
401 s++;
402 else if (*s==LUA_SIGNATURE[0])
403 s="(bstring)";
404 else
405 s="(string)";
406 printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
407 (f->linedefined==0)?"main":"function",s,
408 f->linedefined,f->lastlinedefined,
409 S(f->sizecode),VOID(f));
410 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
411 (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
412 S(f->maxstacksize),S(f->sizeupvalues));
413 printf("%d local%s, %d constant%s, %d function%s\n",
414 S(f->sizelocvars),S(f->sizek),S(f->sizep));
417 static void PrintDebug(const Proto* f)
419 int i,n;
420 n=f->sizek;
421 printf("constants (%d) for %p:\n",n,VOID(f));
422 for (i=0; i<n; i++)
424 printf("\t%d\t",i+1);
425 PrintConstant(f,i);
426 printf("\n");
428 n=f->sizelocvars;
429 printf("locals (%d) for %p:\n",n,VOID(f));
430 for (i=0; i<n; i++)
432 printf("\t%d\t%s\t%d\t%d\n",
433 i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
435 n=f->sizeupvalues;
436 printf("upvalues (%d) for %p:\n",n,VOID(f));
437 for (i=0; i<n; i++)
439 printf("\t%d\t%s\t%d\t%d\n",
440 i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
444 static void PrintFunction(const Proto* f, int full)
446 int i,n=f->sizep;
447 PrintHeader(f);
448 PrintCode(f);
449 if (full) PrintDebug(f);
450 for (i=0; i<n; i++) PrintFunction(f->p[i],full);