176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman/* 276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman** $Id: luac.c,v 1.69 2011/11/29 17:46:33 lhf Exp $ 376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman** Lua compiler (saves bytecodes to files; also list bytecodes) 476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman** See Copyright Notice in lua.h 576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman*/ 676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <errno.h> 876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <stdio.h> 976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <stdlib.h> 1076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <string.h> 1176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 1276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define luac_c 1376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define LUA_CORE 1476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 1576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "lua.h" 1676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "lauxlib.h" 1776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 1876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "lobject.h" 1976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "lstate.h" 2076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "lundump.h" 2176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 2276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void PrintFunction(const Proto* f, int full); 2376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define luaU_print PrintFunction 2476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 2576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define PROGNAME "luac" /* default program name */ 2676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define OUTPUT PROGNAME ".out" /* default output file */ 2776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 2876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic int listing=0; /* list bytecodes? */ 2976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic int dumping=1; /* dump bytecodes? */ 3076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic int stripping=0; /* strip debug information? */ 3176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic char Output[]={ OUTPUT }; /* default output file name */ 3276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic const char* output=Output; /* actual output file name */ 3376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic const char* progname=PROGNAME; /* actual program name */ 3476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 3576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void fatal(const char* message) 3676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 3776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman fprintf(stderr,"%s: %s\n",progname,message); 3876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman exit(EXIT_FAILURE); 3976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 4076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 4176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void cannot(const char* what) 4276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 4376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); 4476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman exit(EXIT_FAILURE); 4576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 4676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 4776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void usage(const char* message) 4876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 4976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (*message=='-') 5076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); 5176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else 5276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman fprintf(stderr,"%s: %s\n",progname,message); 5376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman fprintf(stderr, 5476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman "usage: %s [options] [filenames]\n" 5576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman "Available options are:\n" 5676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman " -l list (use -l -l for full listing)\n" 5776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman " -o name output to file " LUA_QL("name") " (default is \"%s\")\n" 5876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman " -p parse only\n" 5976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman " -s strip debug information\n" 6076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman " -v show version information\n" 6176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman " -- stop handling options\n" 6276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman " - stop handling options and process stdin\n" 6376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman ,progname,Output); 6476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman exit(EXIT_FAILURE); 6576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 6676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 6776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define IS(s) (strcmp(argv[i],s)==0) 6876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 6976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic int doargs(int argc, char* argv[]) 7076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 7176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int i; 7276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int version=0; 7376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; 7476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=1; i<argc; i++) 7576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 7676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (*argv[i]!='-') /* end of options; keep it */ 7776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 7876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (IS("--")) /* end of options; skip it */ 7976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 8076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman ++i; 8176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (version) ++version; 8276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 8376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 8476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (IS("-")) /* end of options; use stdin */ 8576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 8676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (IS("-l")) /* list */ 8776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman ++listing; 8876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (IS("-o")) /* output file */ 8976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 9076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman output=argv[++i]; 9176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (output==NULL || *output==0 || (*output=='-' && output[1]!=0)) 9276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman usage(LUA_QL("-o") " needs argument"); 9376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (IS("-")) output=NULL; 9476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 9576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (IS("-p")) /* parse only */ 9676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman dumping=0; 9776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (IS("-s")) /* strip debug information */ 9876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman stripping=1; 9976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (IS("-v")) /* show version */ 10076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman ++version; 10176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else /* unknown option */ 10276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman usage(argv[i]); 10376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 10476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (i==argc && (listing || !dumping)) 10576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 10676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman dumping=0; 10776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman argv[--i]=Output; 10876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 10976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (version) 11076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 11176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%s\n",LUA_COPYRIGHT); 11276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (version==argc-1) exit(EXIT_SUCCESS); 11376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 11476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return i; 11576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 11676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 11776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define FUNCTION "(function()end)();" 11876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 11976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic const char* reader(lua_State *L, void *ud, size_t *size) 12076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 12176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman UNUSED(L); 12276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if ((*(int*)ud)--) 12376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 12476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman *size=sizeof(FUNCTION)-1; 12576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return FUNCTION; 12676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 12776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else 12876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 12976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman *size=0; 13076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return NULL; 13176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 13276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 13376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 13476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define toproto(L,i) getproto(L->top+(i)) 13576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 13676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic const Proto* combine(lua_State* L, int n) 13776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 13876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (n==1) 13976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return toproto(L,-1); 14076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else 14176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 14276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman Proto* f; 14376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int i=n; 14476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); 14576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman f=toproto(L,-1); 14676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=0; i<n; i++) 14776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 14876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman f->p[i]=toproto(L,i-n-1); 14976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; 15076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 15176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman f->sizelineinfo=0; 15276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return f; 15376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 15476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 15576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 15676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic int writer(lua_State* L, const void* p, size_t size, void* u) 15776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 15876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman UNUSED(L); 15976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); 16076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 16176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 16276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic int pmain(lua_State* L) 16376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 16476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int argc=(int)lua_tointeger(L,1); 16576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman char** argv=(char**)lua_touserdata(L,2); 16676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman const Proto* f; 16776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int i; 16876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (!lua_checkstack(L,argc)) fatal("too many input files"); 16976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=0; i<argc; i++) 17076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 17176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman const char* filename=IS("-") ? NULL : argv[i]; 17276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1)); 17376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 17476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman f=combine(L,argc); 17576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (listing) luaU_print(f,listing>1); 17676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (dumping) 17776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 17876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); 17976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (D==NULL) cannot("open"); 18076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman lua_lock(L); 18176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman luaU_dump(L,f,writer,D,stripping); 18276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman lua_unlock(L); 18376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ferror(D)) cannot("write"); 18476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (fclose(D)) cannot("close"); 18576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 18676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return 0; 18776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 18876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 18976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanint main(int argc, char* argv[]) 19076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 19176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman lua_State* L; 19276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int i=doargs(argc,argv); 19376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman argc-=i; argv+=i; 19476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (argc<=0) usage("no input files given"); 19576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman L=luaL_newstate(); 19676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (L==NULL) fatal("cannot create state: not enough memory"); 19776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman lua_pushcfunction(L,&pmain); 19876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman lua_pushinteger(L,argc); 19976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman lua_pushlightuserdata(L,argv); 20076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); 20176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman lua_close(L); 20276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman return EXIT_SUCCESS; 20376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 20476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 20576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman/* 20676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $ 20776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman** print bytecodes 20876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman** See Copyright Notice in lua.h 20976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman*/ 21076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 21176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <ctype.h> 21276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <stdio.h> 21376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 21476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define luac_c 21576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define LUA_CORE 21676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 21776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "ldebug.h" 21876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "lobject.h" 21976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include "lopcodes.h" 22076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 22176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define VOID(p) ((const void*)(p)) 22276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 22376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void PrintString(const TString* ts) 22476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 22576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman const char* s=getstr(ts); 22676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman size_t i,n=ts->tsv.len; 22776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%c",'"'); 22876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=0; i<n; i++) 22976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 23076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int c=(int)(unsigned char)s[i]; 23176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman switch (c) 23276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 23376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '"': printf("\\\""); break; 23476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\\': printf("\\\\"); break; 23576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\a': printf("\\a"); break; 23676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\b': printf("\\b"); break; 23776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\f': printf("\\f"); break; 23876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\n': printf("\\n"); break; 23976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\r': printf("\\r"); break; 24076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\t': printf("\\t"); break; 24176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case '\v': printf("\\v"); break; 24276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman default: if (isprint(c)) 24376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%c",c); 24476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else 24576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\\%03d",c); 24676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 24776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 24876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%c",'"'); 24976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 25076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 25176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void PrintConstant(const Proto* f, int i) 25276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 25376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman const TValue* o=&f->k[i]; 25476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman switch (ttypenv(o)) 25576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 25676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case LUA_TNIL: 25776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("nil"); 25876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 25976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case LUA_TBOOLEAN: 26076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf(bvalue(o) ? "true" : "false"); 26176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 26276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case LUA_TNUMBER: 26376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf(LUA_NUMBER_FMT,nvalue(o)); 26476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 26576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case LUA_TSTRING: 26676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman PrintString(rawtsvalue(o)); 26776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 26876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman default: /* cannot happen */ 26976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("? type=%d",ttype(o)); 27076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 27176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 27276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 27376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 27476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") 27576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define MYK(x) (-1-(x)) 27676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 27776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void PrintCode(const Proto* f) 27876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 27976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman const Instruction* code=f->code; 28076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int pc,n=f->sizecode; 28176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (pc=0; pc<n; pc++) 28276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 28376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman Instruction i=code[pc]; 28476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman OpCode o=GET_OPCODE(i); 28576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int a=GETARG_A(i); 28676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int b=GETARG_B(i); 28776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int c=GETARG_C(i); 28876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int ax=GETARG_Ax(i); 28976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int bx=GETARG_Bx(i); 29076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int sbx=GETARG_sBx(i); 29176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int line=getfuncline(f,pc); 29276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t%d\t",pc+1); 29376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (line>0) printf("[%d]\t",line); else printf("[-]\t"); 29476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%-9s\t",luaP_opnames[o]); 29576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman switch (getOpMode(o)) 29676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 29776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case iABC: 29876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%d",a); 29976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b); 30076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c); 30176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 30276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case iABx: 30376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%d",a); 30476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (getBMode(o)==OpArgK) printf(" %d",MYK(bx)); 30576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (getBMode(o)==OpArgU) printf(" %d",bx); 30676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 30776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case iAsBx: 30876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%d %d",a,sbx); 30976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 31076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case iAx: 31176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%d",MYK(ax)); 31276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 31376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 31476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman switch (o) 31576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 31676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_LOADK: 31776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; "); PrintConstant(f,bx); 31876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 31976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_GETUPVAL: 32076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_SETUPVAL: 32176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; %s",UPVALNAME(b)); 32276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 32376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_GETTABUP: 32476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; %s",UPVALNAME(b)); 32576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } 32676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 32776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_SETTABUP: 32876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; %s",UPVALNAME(a)); 32976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); } 33076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } 33176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 33276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_GETTABLE: 33376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_SELF: 33476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 33576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 33676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_SETTABLE: 33776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_ADD: 33876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_SUB: 33976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_MUL: 34076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_DIV: 34176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_POW: 34276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_EQ: 34376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_LT: 34476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_LE: 34576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ISK(b) || ISK(c)) 34676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 34776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; "); 34876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 34976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf(" "); 35076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 35176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 35276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 35376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_JMP: 35476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_FORLOOP: 35576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_FORPREP: 35676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_TFORLOOP: 35776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; to %d",sbx+pc+2); 35876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 35976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_CLOSURE: 36076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; %p",VOID(f->p[bx])); 36176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 36276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_SETLIST: 36376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c); 36476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 36576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman case OP_EXTRAARG: 36676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t; "); PrintConstant(f,ax); 36776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 36876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman default: 36976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman break; 37076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 37176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\n"); 37276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 37376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 37476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 37576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define SS(x) ((x==1)?"":"s") 37676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define S(x) (int)(x),SS(x) 37776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 37876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void PrintHeader(const Proto* f) 37976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 38076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman const char* s=f->source ? getstr(f->source) : "=?"; 38176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (*s=='@' || *s=='=') 38276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman s++; 38376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else if (*s==LUA_SIGNATURE[0]) 38476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman s="(bstring)"; 38576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman else 38676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman s="(string)"; 38776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", 38876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman (f->linedefined==0)?"main":"function",s, 38976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman f->linedefined,f->lastlinedefined, 39076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman S(f->sizecode),VOID(f)); 39176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 39276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), 39376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman S(f->maxstacksize),S(f->sizeupvalues)); 39476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("%d local%s, %d constant%s, %d function%s\n", 39576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman S(f->sizelocvars),S(f->sizek),S(f->sizep)); 39676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 39776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 39876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void PrintDebug(const Proto* f) 39976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 40076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int i,n; 40176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman n=f->sizek; 40276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("constants (%d) for %p:\n",n,VOID(f)); 40376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=0; i<n; i++) 40476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 40576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t%d\t",i+1); 40676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman PrintConstant(f,i); 40776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\n"); 40876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 40976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman n=f->sizelocvars; 41076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("locals (%d) for %p:\n",n,VOID(f)); 41176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=0; i<n; i++) 41276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 41376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t%d\t%s\t%d\t%d\n", 41476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 41576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 41676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman n=f->sizeupvalues; 41776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("upvalues (%d) for %p:\n",n,VOID(f)); 41876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=0; i<n; i++) 41976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman { 42076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman printf("\t%d\t%s\t%d\t%d\n", 42176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx); 42276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman } 42376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 42476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman 42576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanstatic void PrintFunction(const Proto* f, int full) 42676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{ 42776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman int i,n=f->sizep; 42876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman PrintHeader(f); 42976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman PrintCode(f); 43076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman if (full) PrintDebug(f); 43176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman for (i=0; i<n; i++) PrintFunction(f->p[i],full); 43276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} 433