From: John L. <jr...@us...> - 2005-06-06 23:06:27
|
Update of /cvsroot/wxlua/wxLua/modules/lua/src/luac In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14733/wxLua/modules/lua/src/luac Added Files: Makefile README luac.c luac.dsp print.c Log Message: moved files to the modules directory structure --- NEW FILE: README --- This is luac, the Lua compiler. There are man pages for it in both nroff and html in ../../doc. luac translates Lua programs into binary files that can be loaded latter. The main advantages of pre-compiling chunks are: faster loading, protecting source code from user changes, and off-line syntax error detection. luac can also be used to learn about the Lua virtual machine. Usage: /l/luac/luac [options] [filenames]. Available options are: - process stdin -l list -o name output to file `name' (default is "luac.out") -p parse only -s strip debug information -v show version information -- stop handling options luac is also an example of how to use the internals of Lua (politely). --- NEW FILE: luac.dsp --- # Microsoft Developer Studio Project File - Name="luac" - Package Owner=<4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT EDIT ** # TARGTYPE "Win32 (x86) Console Application" 0x0103 CFG=luac - Win32 Debug !MESSAGE This is not a valid makefile. To build this project using NMAKE, !MESSAGE use the Export Makefile command and run !MESSAGE !MESSAGE NMAKE /f "Luac.mak". !MESSAGE !MESSAGE You can specify a configuration when running NMAKE !MESSAGE by defining the macro CFG on the command line. For example: !MESSAGE !MESSAGE NMAKE /f "Luac.mak" CFG="luac - Win32 Debug" !MESSAGE !MESSAGE Possible choices for configuration are: !MESSAGE !MESSAGE "luac - Win32 Release" (based on "Win32 (x86) Console Application") !MESSAGE "luac - Win32 Debug" (based on "Win32 (x86) Console Application") !MESSAGE # Begin Project # PROP AllowPerConfigDependencies 0 # PROP Scc_ProjName "" # PROP Scc_LocalPath "" CPP=cl.exe RSC=rc.exe !IF "$(CFG)" == "luac - Win32 Release" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "..\..\..\Release" # PROP Intermediate_Dir "Release" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /FdRelease\luac.pdb /c # ADD CPP /nologo /MD /W4 /GX /O2 /I "..\..\include" /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "LUA_OPNAMES" /D LUACALL=__stdcall /YX /FD /c # ADD BASE RSC /l 0x809 /d "NDEBUG" # ADD RSC /l 0x809 /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 /nologo /subsystem:console /machine:I386 # ADD LINK32 /nologo /subsystem:console /machine:I386 !ELSEIF "$(CFG)" == "luac - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "Debug" # PROP BASE Intermediate_Dir "Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "..\..\..\Debug" # PROP Intermediate_Dir "Debug" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /FdDebug\luac.pdb /c # ADD CPP /nologo /MDd /W4 /Gm /GR /GX /ZI /Od /I "..\..\include" /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "LUA_OPNAMES" /D LUACALL=__stdcall /YX /FD /GZ /c # ADD BASE RSC /l 0x809 /d "_DEBUG" # ADD RSC /l 0x809 /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept # ADD LINK32 /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept !ENDIF # Begin Target # Name "luac - Win32 Release" # Name "luac - Win32 Debug" # Begin Group "Source Files" # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Source File SOURCE=..\lapi.c # End Source File # Begin Source File SOURCE=..\lib\lauxlib.c # End Source File # Begin Source File SOURCE=..\lcode.c # End Source File # Begin Source File SOURCE=..\ldebug.c # End Source File # Begin Source File SOURCE=..\ldo.c # End Source File # Begin Source File SOURCE=..\ldump.c # End Source File # Begin Source File SOURCE=..\lfunc.c # End Source File # Begin Source File SOURCE=..\lgc.c # End Source File # Begin Source File SOURCE=..\llex.c # End Source File # Begin Source File SOURCE=..\lmem.c # End Source File # Begin Source File SOURCE=..\lobject.c # End Source File # Begin Source File SOURCE=..\lopcodes.c # End Source File # Begin Source File SOURCE=..\lparser.c # End Source File # Begin Source File SOURCE=..\lstate.c # End Source File # Begin Source File SOURCE=..\lstring.c # End Source File # Begin Source File SOURCE=..\ltable.c # End Source File # Begin Source File SOURCE=..\ltests.c # End Source File # Begin Source File SOURCE=..\ltm.c # End Source File # Begin Source File SOURCE=.\luac.c # End Source File # Begin Source File SOURCE=..\lundump.c # End Source File # Begin Source File SOURCE=..\lvm.c # End Source File # Begin Source File SOURCE=..\lzio.c # End Source File # Begin Source File SOURCE=.\print.c # End Source File # End Group # Begin Group "Header Files" # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File SOURCE=..\lapi.h # End Source File # Begin Source File SOURCE=..\..\include\lauxlib.h # End Source File # Begin Source File SOURCE=..\lcode.h # End Source File # Begin Source File SOURCE=..\ldebug.h # End Source File # Begin Source File SOURCE=..\ldo.h # End Source File # Begin Source File SOURCE=..\lfunc.h # End Source File # Begin Source File SOURCE=..\LGC.H # End Source File # Begin Source File SOURCE=..\llex.h # End Source File # Begin Source File SOURCE=..\llimits.h # End Source File # Begin Source File SOURCE=..\lmem.h # End Source File # Begin Source File SOURCE=..\lobject.h # End Source File # Begin Source File SOURCE=..\lopcodes.h # End Source File # Begin Source File SOURCE=..\lparser.h # End Source File # Begin Source File SOURCE=..\lstate.h # End Source File # Begin Source File SOURCE=..\lstring.h # End Source File # Begin Source File SOURCE=..\ltable.h # End Source File # Begin Source File SOURCE=..\LTM.H # End Source File # Begin Source File SOURCE=..\..\include\lua.h # End Source File # Begin Source File SOURCE=.\luac.h # End Source File # Begin Source File SOURCE=..\..\include\luadebug.h # End Source File # Begin Source File SOURCE=..\..\include\lualib.h # End Source File # Begin Source File SOURCE=..\lundump.h # End Source File # Begin Source File SOURCE=..\LVM.H # End Source File # Begin Source File SOURCE=..\lzio.h # End Source File # Begin Source File SOURCE=.\print.h # End Source File # End Group # Begin Group "Resource Files" # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" # End Group # End Target # End Project --- NEW FILE: Makefile --- # makefile for Lua compiler LUA= ../.. include $(LUA)/config INCS= -I$(INC) -I.. $(EXTRA_INCS) OBJS= luac.o print.o lopcodes.o SRCS= luac.c print.c T= $(BIN)/luac all: $T $T: $(OBJS) $(LIB)/liblua.a $(LIB)/liblualib.a $(CC) -o $@ $(MYLDFLAGS) $(OBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(DLLIB) # print.c needs opcode names from lopcodes.c lopcodes.o: ../lopcodes.c ../lopcodes.h $(CC) -o $@ -c $(CFLAGS) -DLUA_OPNAMES ../lopcodes.c $(LIB)/liblua.a: cd ..; $(MAKE) $(LIB)/liblualib.a: cd ../lib; $(MAKE) clean: rm -f $(OBJS) $T co: co -q -f -M $(SRCS) klean: clean rm -f $(SRCS) --- NEW FILE: luac.c --- /* ** $Id: luac.c,v 1.1 2005/06/06 23:06:17 jrl1 Exp $ ** Lua compiler (saves bytecodes to files; also list bytecodes) ** See Copyright Notice in lua.h */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "lua.h" #include "lauxlib.h" #include "lfunc.h" #include "lmem.h" #include "lobject.h" #include "lopcodes.h" #include "lstring.h" #include "lundump.h" #ifndef LUA_DEBUG #define luaB_opentests(L) #endif #ifndef PROGNAME #define PROGNAME "luac" /* program name */ #endif #define OUTPUT "luac.out" /* default output file */ static int listing=0; /* list bytecodes? */ static int dumping=1; /* dump bytecodes? */ static int stripping=0; /* strip debug information? */ static char Output[]={ OUTPUT }; /* default output file name */ static const char* output=Output; /* output file name */ static const char* progname=PROGNAME; /* actual program name */ static void fatal(const char* message) { fprintf(stderr,"%s: %s\n",progname,message); exit(EXIT_FAILURE); } static void cannot(const char* name, const char* what, const char* mode) { fprintf(stderr,"%s: cannot %s %sput file ",progname,what,mode); perror(name); exit(EXIT_FAILURE); } static void usage(const char* message, const char* arg) { if (message!=NULL) { fprintf(stderr,"%s: ",progname); fprintf(stderr,message,arg); fprintf(stderr,"\n"); } fprintf(stderr, "usage: %s [options] [filenames]. Available options are:\n" " - process stdin\n" " -l list\n" " -o name output to file `name' (default is \"" OUTPUT "\")\n" " -p parse only\n" " -s strip debug information\n" " -v show version information\n" " -- stop handling options\n", progname); exit(EXIT_FAILURE); } #define IS(s) (strcmp(argv[i],s)==0) static int doargs(int argc, char* argv[]) { int i; if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; for (i=1; i<argc; i++) { if (*argv[i]!='-') /* end of options; keep it */ break; else if (IS("--")) /* end of options; skip it */ { ++i; break; } else if (IS("-")) /* end of options; use stdin */ return i; else if (IS("-l")) /* list */ listing=1; else if (IS("-o")) /* output file */ { output=argv[++i]; if (output==NULL || *output==0) usage("`-o' needs argument",NULL); } else if (IS("-p")) /* parse only */ dumping=0; else if (IS("-s")) /* strip debug information */ stripping=1; else if (IS("-v")) /* show version */ { printf("%s %s\n",LUA_VERSION,LUA_COPYRIGHT); if (argc==2) exit(EXIT_SUCCESS); } else /* unknown option */ usage("unrecognized option `%s'",argv[i]); } if (i==argc && (listing || !dumping)) { dumping=0; argv[--i]=Output; } return i; } static Proto* toproto(lua_State* L, int i) { const Closure* c=(const Closure*)lua_topointer(L,i); return c->l.p; } static Proto* combine(lua_State* L, int n) { if (n==1) return toproto(L,-1); else { int i,pc=0; Proto* f=luaF_newproto(L); f->source=luaS_newliteral(L,"=(" PROGNAME ")"); f->maxstacksize=1; f->p=luaM_newvector(L,n,Proto*); f->sizep=n; f->sizecode=2*n+1; f->code=luaM_newvector(L,f->sizecode,Instruction); for (i=0; i<n; i++) { f->p[i]=toproto(L,i-n); f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i); f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1); } f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0); return f; } } static void strip(lua_State* L, Proto* f) { int i,n=f->sizep; luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); f->lineinfo=NULL; f->sizelineinfo=0; f->locvars=NULL; f->sizelocvars=0; f->upvalues=NULL; f->sizeupvalues=0; f->source=luaS_newliteral(L,"=(none)"); for (i=0; i<n; i++) strip(L,f->p[i]); } static int writer(lua_State* L, const void* p, size_t size, void* u) { UNUSED(L); return fwrite(p,size,1,(FILE*)u)==1; } int main(int argc, char* argv[]) { lua_State* L; Proto* f; int i=doargs(argc,argv); argc-=i; argv+=i; if (argc<=0) usage("no input files given",NULL); L=lua_open(); luaB_opentests(L); for (i=0; i<argc; i++) { const char* filename=IS("-") ? NULL : argv[i]; if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1)); } f=combine(L,argc); if (listing) luaU_print(f); if (dumping) { FILE* D=fopen(output,"wb"); if (D==NULL) cannot(output,"open","out"); if (stripping) strip(L,f); lua_lock(L); luaU_dump(L,f,writer,D); lua_unlock(L); if (ferror(D)) cannot(output,"write","out"); fclose(D); } lua_close(L); return 0; } --- NEW FILE: print.c --- /* ** $Id: print.c,v 1.1 2005/06/06 23:06:17 jrl1 Exp $ ** print bytecodes ** See Copyright Notice in lua.h */ #include <stdio.h> #if 0 #define DEBUG_PRINT #endif #ifndef LUA_OPNAMES #define LUA_OPNAMES #endif #include "ldebug.h" #include "lobject.h" #include "lopcodes.h" #include "lundump.h" #define Sizeof(x) ((int)sizeof(x)) #define VOID(p) ((const void*)(p)) static void PrintString(const Proto* f, int n) { const char* s=svalue(&f->k[n]); putchar('"'); for (; *s; s++) { switch (*s) { case '"': printf("\\\""); break; case '\a': printf("\\a"); break; case '\b': printf("\\b"); break; case '\f': printf("\\f"); break; case '\n': printf("\\n"); break; case '\r': printf("\\r"); break; case '\t': printf("\\t"); break; case '\v': printf("\\v"); break; default: putchar(*s); break; } } putchar('"'); } static void PrintConstant(const Proto* f, int i) { const TObject* o=&f->k[i]; switch (ttype(o)) { case LUA_TNUMBER: printf(LUA_NUMBER_FMT,nvalue(o)); break; case LUA_TSTRING: PrintString(f,i); break; case LUA_TNIL: printf("nil"); break; default: /* cannot happen */ printf("? type=%d",ttype(o)); break; } } static void PrintCode(const Proto* f) { const Instruction* code=f->code; int pc,n=f->sizecode; for (pc=0; pc<n; pc++) { Instruction i=code[pc]; OpCode o=GET_OPCODE(i); int a=GETARG_A(i); int b=GETARG_B(i); int c=GETARG_C(i); int bc=GETARG_Bx(i); int sbc=GETARG_sBx(i); int line=getline(f,pc); #if 0 printf("%0*lX",Sizeof(i)*2,i); #endif printf("\t%d\t",pc+1); if (line>0) printf("[%d]\t",line); else printf("[-]\t"); printf("%-9s\t",luaP_opnames[o]); switch (getOpMode(o)) { case iABC: printf("%d %d %d",a,b,c); break; case iABx: printf("%d %d",a,bc); break; case iAsBx: printf("%d %d",a,sbc); break; } switch (o) { case OP_LOADK: printf("\t; "); PrintConstant(f,bc); break; case OP_GETUPVAL: case OP_SETUPVAL: printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); break; case OP_GETGLOBAL: case OP_SETGLOBAL: printf("\t; %s",svalue(&f->k[bc])); break; case OP_GETTABLE: case OP_SELF: if (c>=MAXSTACK) { printf("\t; "); PrintConstant(f,c-MAXSTACK); } break; case OP_SETTABLE: case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_POW: case OP_EQ: case OP_LT: case OP_LE: if (b>=MAXSTACK || c>=MAXSTACK) { printf("\t; "); if (b>=MAXSTACK) PrintConstant(f,b-MAXSTACK); else printf("-"); printf(" "); if (c>=MAXSTACK) PrintConstant(f,c-MAXSTACK); } break; case OP_JMP: case OP_FORLOOP: case OP_TFORPREP: printf("\t; to %d",sbc+pc+2); break; case OP_CLOSURE: printf("\t; %p",VOID(f->p[bc])); break; default: break; } printf("\n"); } } static const char* Source(const Proto* f) { const char* s=getstr(f->source); if (*s=='@' || *s=='=') return s+1; else if (*s==LUA_SIGNATURE[0]) return "(bstring)"; else return "(string)"; } #define IsMain(f) (f->lineDefined==0) #define SS(x) (x==1)?"":"s" #define S(x) x,SS(x) static void PrintHeader(const Proto* f) { printf("\n%s <%s:%d> (%d instruction%s, %d bytes at %p)\n", IsMain(f)?"main":"function",Source(f),f->lineDefined, S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); printf("%d%s param%s, %d stack%s, %d upvalue%s, ", f->numparams,f->is_vararg?"+":"",SS(f->numparams),S(f->maxstacksize), S(f->nups)); printf("%d local%s, %d constant%s, %d function%s\n", S(f->sizelocvars),S(f->sizek),S(f->sizep)); } #ifdef DEBUG_PRINT static void PrintConstants(const Proto* f) { int i,n=f->sizek; printf("constants (%d) for %p:\n",n,VOID(f)); for (i=0; i<n; i++) { printf("\t%d\t",i); PrintConstant(f,i); printf("\n"); } } static void PrintLocals(const Proto* f) { int i,n=f->sizelocvars; printf("locals (%d) for %p:\n",n,VOID(f)); for (i=0; i<n; i++) { printf("\t%d\t%s\t%d\t%d\n", i,getstr(f->locvars[i].varname),f->locvars[i].startpc,f->locvars[i].endpc); } } static void PrintUpvalues(const Proto* f) { int i,n=f->sizeupvalues; printf("upvalues (%d) for %p:\n",n,VOID(f)); if (f->upvalues==NULL) return; for (i=0; i<n; i++) { printf("\t%d\t%s\n",i,getstr(f->upvalues[i])); } } #endif void luaU_print(const Proto* f) { int i,n=f->sizep; PrintHeader(f); PrintCode(f); #ifdef DEBUG_PRINT PrintConstants(f); PrintLocals(f); PrintUpvalues(f); #endif for (i=0; i<n; i++) luaU_print(f->p[i]); } |