]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/prometheus-cpp/3rdparty/civetweb/src/third_party/lua-5.4.3/src/luac.c
3 ** Lua compiler (saves bytecodes to files; also lists bytecodes)
4 ** See Copyright Notice in lua.h
28 static void PrintFunction(const Proto
* f
, int full
);
29 #define luaU_print PrintFunction
31 #define PROGNAME "luac" /* default program name */
32 #define OUTPUT PROGNAME ".out" /* default output file */
34 static int listing
=0; /* list bytecodes? */
35 static int dumping
=1; /* dump bytecodes? */
36 static int stripping
=0; /* strip debug information? */
37 static char Output
[]={ OUTPUT
}; /* default output file name */
38 static const char* output
=Output
; /* actual output file name */
39 static const char* progname
=PROGNAME
; /* actual program name */
40 static TString
**tmname
;
42 static void fatal(const char* message
)
44 fprintf(stderr
,"%s: %s\n",progname
,message
);
48 static void cannot(const char* what
)
50 fprintf(stderr
,"%s: cannot %s %s: %s\n",progname
,what
,output
,strerror(errno
));
54 static void usage(const char* message
)
57 fprintf(stderr
,"%s: unrecognized option '%s'\n",progname
,message
);
59 fprintf(stderr
,"%s: %s\n",progname
,message
);
61 "usage: %s [options] [filenames]\n"
62 "Available options are:\n"
63 " -l list (use -l -l for full listing)\n"
64 " -o name output to file 'name' (default is \"%s\")\n"
66 " -s strip debug information\n"
67 " -v show version information\n"
68 " -- stop handling options\n"
69 " - stop handling options and process stdin\n"
74 #define IS(s) (strcmp(argv[i],s)==0)
76 static int doargs(int argc
, char* argv
[])
80 if (argv
[0]!=NULL
&& *argv
[0]!=0) progname
=argv
[0];
81 for (i
=1; i
<argc
; i
++)
83 if (*argv
[i
]!='-') /* end of options; keep it */
85 else if (IS("--")) /* end of options; skip it */
88 if (version
) ++version
;
91 else if (IS("-")) /* end of options; use stdin */
93 else if (IS("-l")) /* list */
95 else if (IS("-o")) /* output file */
98 if (output
==NULL
|| *output
==0 || (*output
=='-' && output
[1]!=0))
99 usage("'-o' needs argument");
100 if (IS("-")) output
=NULL
;
102 else if (IS("-p")) /* parse only */
104 else if (IS("-s")) /* strip debug information */
106 else if (IS("-v")) /* show version */
108 else /* unknown option */
111 if (i
==argc
&& (listing
|| !dumping
))
118 printf("%s\n",LUA_COPYRIGHT
);
119 if (version
==argc
-1) exit(EXIT_SUCCESS
);
124 #define FUNCTION "(function()end)();"
126 static const char* reader(lua_State
* L
, void* ud
, size_t* size
)
131 *size
=sizeof(FUNCTION
)-1;
141 #define toproto(L,i) getproto(s2v(L->top+(i)))
143 static const Proto
* combine(lua_State
* L
, int n
)
146 return toproto(L
,-1);
151 if (lua_load(L
,reader
,&i
,"=(" PROGNAME
")",NULL
)!=LUA_OK
) fatal(lua_tostring(L
,-1));
155 f
->p
[i
]=toproto(L
,i
-n
-1);
156 if (f
->p
[i
]->sizeupvalues
>0) f
->p
[i
]->upvalues
[0].instack
=0;
163 static int writer(lua_State
* L
, const void* p
, size_t size
, void* u
)
166 return (fwrite(p
,size
,1,(FILE*)u
)!=1) && (size
!=0);
169 static int pmain(lua_State
* L
)
171 int argc
=(int)lua_tointeger(L
,1);
172 char** argv
=(char**)lua_touserdata(L
,2);
176 if (!lua_checkstack(L
,argc
)) fatal("too many input files");
177 for (i
=0; i
<argc
; i
++)
179 const char* filename
=IS("-") ? NULL
: argv
[i
];
180 if (luaL_loadfile(L
,filename
)!=LUA_OK
) fatal(lua_tostring(L
,-1));
183 if (listing
) luaU_print(f
,listing
>1);
186 FILE* D
= (output
==NULL
) ? stdout
: fopen(output
,"wb");
187 if (D
==NULL
) cannot("open");
189 luaU_dump(L
,f
,writer
,D
,stripping
);
191 if (ferror(D
)) cannot("write");
192 if (fclose(D
)) cannot("close");
197 int main(int argc
, char* argv
[])
200 int i
=doargs(argc
,argv
);
202 if (argc
<=0) usage("no input files given");
204 if (L
==NULL
) fatal("cannot create state: not enough memory");
205 lua_pushcfunction(L
,&pmain
);
206 lua_pushinteger(L
,argc
);
207 lua_pushlightuserdata(L
,argv
);
208 if (lua_pcall(L
,2,0,0)!=LUA_OK
) fatal(lua_tostring(L
,-1));
217 #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
218 #define VOID(p) ((const void*)(p))
219 #define eventname(i) (getstr(tmname[i]))
221 static void PrintString(const TString
* ts
)
223 const char* s
=getstr(ts
);
224 size_t i
,n
=tsslen(ts
);
228 int c
=(int)(unsigned char)s
[i
];
259 if (isprint(c
)) printf("%c",c
); else printf("\\%03d",c
);
266 static void PrintType(const Proto
* f
, int i
)
268 const TValue
* o
=&f
->k
[i
];
288 default: /* cannot happen */
289 printf("?%d",ttypetag(o
));
295 static void PrintConstant(const Proto
* f
, int i
)
297 const TValue
* o
=&f
->k
[i
];
312 sprintf(buff
,LUA_NUMBER_FMT
,fltvalue(o
));
314 if (buff
[strspn(buff
,"-0123456789")]=='\0') printf(".0");
318 printf(LUA_INTEGER_FMT
,ivalue(o
));
322 PrintString(tsvalue(o
));
324 default: /* cannot happen */
325 printf("?%d",ttypetag(o
));
330 #define COMMENT "\t; "
331 #define EXTRAARG GETARG_Ax(code[pc+1])
332 #define EXTRAARGC (EXTRAARG*(MAXARG_C+1))
333 #define ISK (isk ? "k" : "")
335 static void PrintCode(const Proto
* f
)
337 const Instruction
* code
=f
->code
;
338 int pc
,n
=f
->sizecode
;
339 for (pc
=0; pc
<n
; pc
++)
341 Instruction i
=code
[pc
];
342 OpCode o
=GET_OPCODE(i
);
350 int sbx
=GETARG_sBx(i
);
352 int line
=luaG_getfuncline(f
,pc
);
353 printf("\t%d\t",pc
+1);
354 if (line
>0) printf("[%d]\t",line
); else printf("[-]\t");
355 printf("%-9s\t",opnames
[o
]);
362 printf("%d %d",a
,sbx
);
365 printf("%d %d",a
,sbx
);
368 printf("%d %d",a
,bx
);
369 printf(COMMENT
); PrintConstant(f
,bx
);
373 printf(COMMENT
); PrintConstant(f
,EXTRAARG
);
386 printf(COMMENT
"%d out",b
+1);
390 printf(COMMENT
"%s",UPVALNAME(b
));
394 printf(COMMENT
"%s",UPVALNAME(b
));
397 printf("%d %d %d",a
,b
,c
);
398 printf(COMMENT
"%s",UPVALNAME(b
));
399 printf(" "); PrintConstant(f
,c
);
402 printf("%d %d %d",a
,b
,c
);
405 printf("%d %d %d",a
,b
,c
);
408 printf("%d %d %d",a
,b
,c
);
409 printf(COMMENT
); PrintConstant(f
,c
);
412 printf("%d %d %d%s",a
,b
,c
,ISK
);
413 printf(COMMENT
"%s",UPVALNAME(a
));
414 printf(" "); PrintConstant(f
,b
);
415 if (isk
) { printf(" "); PrintConstant(f
,c
); }
418 printf("%d %d %d%s",a
,b
,c
,ISK
);
419 if (isk
) { printf(COMMENT
); PrintConstant(f
,c
); }
422 printf("%d %d %d%s",a
,b
,c
,ISK
);
423 if (isk
) { printf(COMMENT
); PrintConstant(f
,c
); }
426 printf("%d %d %d%s",a
,b
,c
,ISK
);
427 printf(COMMENT
); PrintConstant(f
,b
);
428 if (isk
) { printf(" "); PrintConstant(f
,c
); }
431 printf("%d %d %d",a
,b
,c
);
432 printf(COMMENT
"%d",c
+EXTRAARGC
);
435 printf("%d %d %d%s",a
,b
,c
,ISK
);
436 if (isk
) { printf(COMMENT
); PrintConstant(f
,c
); }
439 printf("%d %d %d",a
,b
,sc
);
442 printf("%d %d %d",a
,b
,c
);
443 printf(COMMENT
); PrintConstant(f
,c
);
446 printf("%d %d %d",a
,b
,c
);
447 printf(COMMENT
); PrintConstant(f
,c
);
450 printf("%d %d %d",a
,b
,c
);
451 printf(COMMENT
); PrintConstant(f
,c
);
454 printf("%d %d %d",a
,b
,c
);
455 printf(COMMENT
); PrintConstant(f
,c
);
458 printf("%d %d %d",a
,b
,c
);
459 printf(COMMENT
); PrintConstant(f
,c
);
462 printf("%d %d %d",a
,b
,c
);
463 printf(COMMENT
); PrintConstant(f
,c
);
466 printf("%d %d %d",a
,b
,c
);
467 printf(COMMENT
); PrintConstant(f
,c
);
470 printf("%d %d %d",a
,b
,c
);
471 printf(COMMENT
); PrintConstant(f
,c
);
474 printf("%d %d %d",a
,b
,c
);
475 printf(COMMENT
); PrintConstant(f
,c
);
478 printf("%d %d %d",a
,b
,c
);
479 printf(COMMENT
); PrintConstant(f
,c
);
482 printf("%d %d %d",a
,b
,sc
);
485 printf("%d %d %d",a
,b
,sc
);
488 printf("%d %d %d",a
,b
,c
);
491 printf("%d %d %d",a
,b
,c
);
494 printf("%d %d %d",a
,b
,c
);
497 printf("%d %d %d",a
,b
,c
);
500 printf("%d %d %d",a
,b
,c
);
503 printf("%d %d %d",a
,b
,c
);
506 printf("%d %d %d",a
,b
,c
);
509 printf("%d %d %d",a
,b
,c
);
512 printf("%d %d %d",a
,b
,c
);
515 printf("%d %d %d",a
,b
,c
);
518 printf("%d %d %d",a
,b
,c
);
521 printf("%d %d %d",a
,b
,c
);
524 printf("%d %d %d",a
,b
,c
);
525 printf(COMMENT
"%s",eventname(c
));
528 printf("%d %d %d %d",a
,sb
,c
,isk
);
529 printf(COMMENT
"%s",eventname(c
));
530 if (isk
) printf(" flip");
533 printf("%d %d %d %d",a
,b
,c
,isk
);
534 printf(COMMENT
"%s ",eventname(c
)); PrintConstant(f
,b
);
535 if (isk
) printf(" flip");
559 printf("%d",GETARG_sJ(i
));
560 printf(COMMENT
"to %d",GETARG_sJ(i
)+pc
+2);
563 printf("%d %d %d",a
,b
,isk
);
566 printf("%d %d %d",a
,b
,isk
);
569 printf("%d %d %d",a
,b
,isk
);
572 printf("%d %d %d",a
,b
,isk
);
573 printf(COMMENT
); PrintConstant(f
,b
);
576 printf("%d %d %d",a
,sb
,isk
);
579 printf("%d %d %d",a
,sb
,isk
);
582 printf("%d %d %d",a
,sb
,isk
);
585 printf("%d %d %d",a
,sb
,isk
);
588 printf("%d %d %d",a
,sb
,isk
);
591 printf("%d %d",a
,isk
);
594 printf("%d %d %d",a
,b
,isk
);
597 printf("%d %d %d",a
,b
,c
);
599 if (b
==0) printf("all in "); else printf("%d in ",b
-1);
600 if (c
==0) printf("all out"); else printf("%d out",c
-1);
603 printf("%d %d %d",a
,b
,c
);
604 printf(COMMENT
"%d in",b
-1);
607 printf("%d %d %d",a
,b
,c
);
609 if (b
==0) printf("all out"); else printf("%d out",b
-1);
617 printf("%d %d",a
,bx
);
618 printf(COMMENT
"to %d",pc
-bx
+2);
621 printf("%d %d",a
,bx
);
622 printf(COMMENT
"to %d",pc
+bx
+2);
625 printf("%d %d",a
,bx
);
626 printf(COMMENT
"to %d",pc
+bx
+2);
632 printf("%d %d",a
,bx
);
633 printf(COMMENT
"to %d",pc
-bx
+2);
636 printf("%d %d %d",a
,b
,c
);
637 if (isk
) printf(COMMENT
"%d",c
+EXTRAARGC
);
640 printf("%d %d",a
,bx
);
641 printf(COMMENT
"%p",VOID(f
->p
[bx
]));
646 if (c
==0) printf("all out"); else printf("%d out",c
-1);
656 printf("%d %d %d",a
,b
,c
);
657 printf(COMMENT
"not handled");
666 #define SS(x) ((x==1)?"":"s")
667 #define S(x) (int)(x),SS(x)
669 static void PrintHeader(const Proto
* f
)
671 const char* s
=f
->source
? getstr(f
->source
) : "=?";
672 if (*s
=='@' || *s
=='=')
674 else if (*s
==LUA_SIGNATURE
[0])
678 printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
679 (f
->linedefined
==0)?"main":"function",s
,
680 f
->linedefined
,f
->lastlinedefined
,
681 S(f
->sizecode
),VOID(f
));
682 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
683 (int)(f
->numparams
),f
->is_vararg
?"+":"",SS(f
->numparams
),
684 S(f
->maxstacksize
),S(f
->sizeupvalues
));
685 printf("%d local%s, %d constant%s, %d function%s\n",
686 S(f
->sizelocvars
),S(f
->sizek
),S(f
->sizep
));
689 static void PrintDebug(const Proto
* f
)
693 printf("constants (%d) for %p:\n",n
,VOID(f
));
702 printf("locals (%d) for %p:\n",n
,VOID(f
));
705 printf("\t%d\t%s\t%d\t%d\n",
706 i
,getstr(f
->locvars
[i
].varname
),f
->locvars
[i
].startpc
+1,f
->locvars
[i
].endpc
+1);
709 printf("upvalues (%d) for %p:\n",n
,VOID(f
));
712 printf("\t%d\t%s\t%d\t%d\n",
713 i
,UPVALNAME(i
),f
->upvalues
[i
].instack
,f
->upvalues
[i
].idx
);
717 static void PrintFunction(const Proto
* f
, int full
)
722 if (full
) PrintDebug(f
);
723 for (i
=0; i
<n
; i
++) PrintFunction(f
->p
[i
],full
);