├── Makefile ├── lua.man ├── luac.c └── luac.man /Makefile: -------------------------------------------------------------------------------- 1 | # $Id: Makefile,v 1.25 2005/05/12 00:29:32 lhf Exp lhf $ 2 | # makefile for Lua compiler 3 | 4 | LUA= distr 5 | WARN= -Wall -Wextra $(XWARN) 6 | XWARN= -Wc++-compat -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes 7 | XWARN= 8 | 9 | CC= gcc -std=gnu99 10 | CFLAGS= -O2 $(WARN) $(INCS) $G 11 | INCS= -I$(LUA) 12 | 13 | LIBS= $(LUA)/liblua.a -lm 14 | OBJS= luac.o 15 | SRCS= luac.c 16 | 17 | # targets -------------------------------------------------------------------- 18 | 19 | all: luac 20 | 21 | luac: $(OBJS) $(LIBS) 22 | $(CC) -o $@ $(OBJS) $(LIBS) 23 | 24 | $(LIBS): 25 | make -C $(LUA) a "CFLAGS=$(CFLAGS)" 26 | 27 | print.c: $(LUA)/lopcodes.h 28 | @diff lopcodes.h $(LUA) 29 | 30 | clean: 31 | -rm -f luac *.o luac.out a.out core core.* mon.out gmon.out tags luac.lst lua 32 | @#cd test; $(MAKE) $@ 33 | 34 | co: 35 | co -l -M $(SRCS) 36 | 37 | conl: 38 | co -M $(SRCS) 39 | 40 | ci: 41 | ci -u $(SRCS) 42 | 43 | diff: 44 | @#-rcsdiff $(SRCS) Makefile 2>&1 | awk -f rcsdiff.awk 45 | @-rcsdiff $(SRCS) 2>&1 | awk -f rcsdiff.awk 46 | 47 | wl: 48 | @rlog -L -R RCS/* | sed 's/RCS.//;s/,v//' 49 | 50 | opp: 51 | grep Kst lopcodes.h | grep ^OP_; echo '' 52 | grep RK lopcodes.h | grep ^OP_; echo '' 53 | grep pc lopcodes.h | grep ^OP_; echo '' 54 | 55 | diffd: 56 | -diff ldump.c $(LUA) 57 | -diff lundump.c $(LUA) 58 | -diff lundump.h $(LUA) 59 | -cat luac.c print.c | diff - $(LUA)/luac.c 60 | 61 | -------------------------------------------------------------------------------- /lua.man: -------------------------------------------------------------------------------- 1 | .\" $Id: lua.man,v 1.13 2011/11/16 17:16:53 lhf Exp lhf $ 2 | .TH LUA 1 "$Date: 2014/12/10 15:55:45 $" 3 | .SH NAME 4 | lua \- Lua interpreter 5 | .SH SYNOPSIS 6 | .B lua 7 | [ 8 | .I options 9 | ] 10 | [ 11 | .I script 12 | [ 13 | .I args 14 | ] 15 | ] 16 | .SH DESCRIPTION 17 | .B lua 18 | is the standalone Lua interpreter. 19 | It loads and executes Lua programs, 20 | either in textual source form or 21 | in precompiled binary form. 22 | (Precompiled binaries are output by 23 | .BR luac , 24 | the Lua compiler.) 25 | .B lua 26 | can be used as a batch interpreter and also interactively. 27 | .LP 28 | The given 29 | .I options 30 | are handled in order and then 31 | the Lua program in file 32 | .I script 33 | is loaded and executed. 34 | The given 35 | .I args 36 | are available to 37 | .I script 38 | as strings in a global table named 39 | .BR arg . 40 | If no options or arguments are given, 41 | then 42 | .B "\-v \-i" 43 | is assumed when the standard input is a terminal; 44 | otherwise, 45 | .B "\-" 46 | is assumed. 47 | .LP 48 | In interactive mode, 49 | .B lua 50 | prompts the user, 51 | reads lines from the standard input, 52 | and executes them as they are read. 53 | If the line contains an expression or list of expressions, 54 | then the line is evaluated and the results are printed. 55 | If a line does not contain a complete statement, 56 | then a secondary prompt is displayed and 57 | lines are read until a complete statement is formed or 58 | a syntax error is found. 59 | .LP 60 | At the very start, 61 | before even handling the command line, 62 | .B lua 63 | checks the contents of the environment variables 64 | .B LUA_INIT_5_3 65 | or 66 | .BR LUA_INIT , 67 | in that order. 68 | If the contents is of the form 69 | .RI '@ filename ', 70 | then 71 | .I filename 72 | is executed. 73 | Otherwise, the string is assumed to be a Lua statement and is executed. 74 | .SH OPTIONS 75 | .TP 76 | .BI \-e " stat" 77 | execute statement 78 | .IR stat . 79 | .TP 80 | .B \-i 81 | enter interactive mode after executing 82 | .IR script . 83 | .TP 84 | .BI \-l " name" 85 | execute the equivalent of 86 | .IB name =require(' name ') 87 | before executing 88 | .IR script . 89 | .TP 90 | .B \-v 91 | show version information. 92 | .TP 93 | .B \-E 94 | ignore environment variables. 95 | .TP 96 | .B \-\- 97 | stop handling options. 98 | .TP 99 | .B \- 100 | stop handling options and execute the standard input as a file. 101 | .SH "SEE ALSO" 102 | .BR luac (1) 103 | .br 104 | The documentation at lua.org, 105 | especially section 7 of the reference manual. 106 | .SH DIAGNOSTICS 107 | Error messages should be self explanatory. 108 | .SH AUTHORS 109 | R. Ierusalimschy, 110 | L. H. de Figueiredo, 111 | W. Celes 112 | .\" EOF 113 | -------------------------------------------------------------------------------- /luac.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: luac.c,v 1.74 2015/03/12 01:53:53 lhf Exp lhf $ 3 | ** Lua compiler (saves bytecodes to files; also lists bytecodes) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define luac_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "lua.h" 19 | #include "lauxlib.h" 20 | 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | #include "lundump.h" 24 | 25 | static void PrintFunction(const Proto* f, int full); 26 | #define luaU_print PrintFunction 27 | 28 | #define PROGNAME "luac" /* default program name */ 29 | #define OUTPUT PROGNAME ".out" /* default output file */ 30 | 31 | static int listing=0; /* list bytecodes? */ 32 | static int dumping=1; /* dump bytecodes? */ 33 | static int stripping=0; /* strip debug information? */ 34 | static char Output[]={ OUTPUT }; /* default output file name */ 35 | static const char* output=Output; /* actual output file name */ 36 | static const char* progname=PROGNAME; /* actual program name */ 37 | 38 | static void fatal(const char* message) 39 | { 40 | fprintf(stderr,"%s: %s\n",progname,message); 41 | exit(EXIT_FAILURE); 42 | } 43 | 44 | static void cannot(const char* what) 45 | { 46 | fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); 47 | exit(EXIT_FAILURE); 48 | } 49 | 50 | static void usage(const char* message) 51 | { 52 | if (*message=='-') 53 | fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message); 54 | else 55 | fprintf(stderr,"%s: %s\n",progname,message); 56 | fprintf(stderr, 57 | "usage: %s [options] [filenames]\n" 58 | "Available options are:\n" 59 | " -l list (use -l -l for full listing)\n" 60 | " -o name output to file 'name' (default is \"%s\")\n" 61 | " -p parse only\n" 62 | " -s strip debug information\n" 63 | " -v show version information\n" 64 | " -- stop handling options\n" 65 | " - stop handling options and process stdin\n" 66 | ,progname,Output); 67 | exit(EXIT_FAILURE); 68 | } 69 | 70 | #define IS(s) (strcmp(argv[i],s)==0) 71 | 72 | static int doargs(int argc, char* argv[]) 73 | { 74 | int i; 75 | int version=0; 76 | if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; 77 | for (i=1; itop+(i)) 138 | 139 | static const Proto* combine(lua_State* L, int n) 140 | { 141 | if (n==1) 142 | return toproto(L,-1); 143 | else 144 | { 145 | Proto* f; 146 | int i=n; 147 | if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); 148 | f=toproto(L,-1); 149 | for (i=0; ip[i]=toproto(L,i-n-1); 152 | if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; 153 | } 154 | f->sizelineinfo=0; 155 | return f; 156 | } 157 | } 158 | 159 | static int writer(lua_State* L, const void* p, size_t size, void* u) 160 | { 161 | UNUSED(L); 162 | return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); 163 | } 164 | 165 | static int pmain(lua_State* L) 166 | { 167 | int argc=(int)lua_tointeger(L,1); 168 | char** argv=(char**)lua_touserdata(L,2); 169 | const Proto* f; 170 | int i; 171 | if (!lua_checkstack(L,argc)) fatal("too many input files"); 172 | for (i=0; i1); 179 | if (dumping) 180 | { 181 | FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); 182 | if (D==NULL) cannot("open"); 183 | lua_lock(L); 184 | luaU_dump(L,f,writer,D,stripping); 185 | lua_unlock(L); 186 | if (ferror(D)) cannot("write"); 187 | if (fclose(D)) cannot("close"); 188 | } 189 | return 0; 190 | } 191 | 192 | int main(int argc, char* argv[]) 193 | { 194 | lua_State* L; 195 | int i=doargs(argc,argv); 196 | argc-=i; argv+=i; 197 | if (argc<=0) usage("no input files given"); 198 | L=luaL_newstate(); 199 | if (L==NULL) fatal("cannot create state: not enough memory"); 200 | lua_pushcfunction(L,&pmain); 201 | lua_pushinteger(L,argc); 202 | lua_pushlightuserdata(L,argv); 203 | if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); 204 | lua_close(L); 205 | return EXIT_SUCCESS; 206 | } 207 | 208 | /* 209 | ** $Id: luac.c,v 1.74 2015/03/12 01:53:53 lhf Exp lhf $ 210 | ** print bytecodes 211 | ** See Copyright Notice in lua.h 212 | */ 213 | 214 | #include 215 | #include 216 | 217 | #define luac_c 218 | #define LUA_CORE 219 | 220 | #include "ldebug.h" 221 | #include "lobject.h" 222 | #include "lopcodes.h" 223 | 224 | #define VOID(p) ((const void*)(p)) 225 | 226 | static void PrintString(const TString* ts) 227 | { 228 | const char* s=getstr(ts); 229 | size_t i,n=tsslen(ts); 230 | printf("%c",'"'); 231 | for (i=0; ik[i]; 257 | switch (ttype(o)) 258 | { 259 | case LUA_TNIL: 260 | printf("nil"); 261 | break; 262 | case LUA_TBOOLEAN: 263 | printf(bvalue(o) ? "true" : "false"); 264 | break; 265 | case LUA_TNUMFLT: 266 | { 267 | char buff[100]; 268 | sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); 269 | printf("%s",buff); 270 | if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); 271 | break; 272 | } 273 | case LUA_TNUMINT: 274 | printf(LUA_INTEGER_FMT,ivalue(o)); 275 | break; 276 | case LUA_TSHRSTR: case LUA_TLNGSTR: 277 | PrintString(tsvalue(o)); 278 | break; 279 | default: /* cannot happen */ 280 | printf("? type=%d",ttype(o)); 281 | break; 282 | } 283 | } 284 | 285 | #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") 286 | #define MYK(x) (-1-(x)) 287 | 288 | static void PrintCode(const Proto* f) 289 | { 290 | const Instruction* code=f->code; 291 | int pc,n=f->sizecode; 292 | for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); 305 | printf("%-9s\t",luaP_opnames[o]); 306 | switch (getOpMode(o)) 307 | { 308 | case iABC: 309 | printf("%d",a); 310 | if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b); 311 | if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c); 312 | break; 313 | case iABx: 314 | printf("%d",a); 315 | if (getBMode(o)==OpArgK) printf(" %d",MYK(bx)); 316 | if (getBMode(o)==OpArgU) printf(" %d",bx); 317 | break; 318 | case iAsBx: 319 | printf("%d %d",a,sbx); 320 | break; 321 | case iAx: 322 | printf("%d",MYK(ax)); 323 | break; 324 | } 325 | switch (o) 326 | { 327 | case OP_LOADK: 328 | printf("\t; "); PrintConstant(f,bx); 329 | break; 330 | case OP_GETUPVAL: 331 | case OP_SETUPVAL: 332 | printf("\t; %s",UPVALNAME(b)); 333 | break; 334 | case OP_GETTABUP: 335 | printf("\t; %s",UPVALNAME(b)); 336 | if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } 337 | break; 338 | case OP_SETTABUP: 339 | printf("\t; %s",UPVALNAME(a)); 340 | if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); } 341 | if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } 342 | break; 343 | case OP_GETTABLE: 344 | case OP_SELF: 345 | if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 346 | break; 347 | case OP_SETTABLE: 348 | case OP_ADD: 349 | case OP_SUB: 350 | case OP_MUL: 351 | case OP_POW: 352 | case OP_DIV: 353 | case OP_IDIV: 354 | case OP_BAND: 355 | case OP_BOR: 356 | case OP_BXOR: 357 | case OP_SHL: 358 | case OP_SHR: 359 | case OP_EQ: 360 | case OP_LT: 361 | case OP_LE: 362 | if (ISK(b) || ISK(c)) 363 | { 364 | printf("\t; "); 365 | if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 366 | printf(" "); 367 | if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 368 | } 369 | break; 370 | case OP_JMP: 371 | case OP_FORLOOP: 372 | case OP_FORPREP: 373 | case OP_TFORLOOP: 374 | printf("\t; to %d",sbx+pc+2); 375 | break; 376 | case OP_CLOSURE: 377 | printf("\t; %p",VOID(f->p[bx])); 378 | break; 379 | case OP_SETLIST: 380 | if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c); 381 | break; 382 | case OP_EXTRAARG: 383 | printf("\t; "); PrintConstant(f,ax); 384 | break; 385 | default: 386 | break; 387 | } 388 | printf("\n"); 389 | } 390 | } 391 | 392 | #define SS(x) ((x==1)?"":"s") 393 | #define S(x) (int)(x),SS(x) 394 | 395 | static void PrintHeader(const Proto* f) 396 | { 397 | const char* s=f->source ? getstr(f->source) : "=?"; 398 | if (*s=='@' || *s=='=') 399 | s++; 400 | else if (*s==LUA_SIGNATURE[0]) 401 | s="(bstring)"; 402 | else 403 | s="(string)"; 404 | printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", 405 | (f->linedefined==0)?"main":"function",s, 406 | f->linedefined,f->lastlinedefined, 407 | S(f->sizecode),VOID(f)); 408 | printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 409 | (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), 410 | S(f->maxstacksize),S(f->sizeupvalues)); 411 | printf("%d local%s, %d constant%s, %d function%s\n", 412 | S(f->sizelocvars),S(f->sizek),S(f->sizep)); 413 | } 414 | 415 | static void PrintDebug(const Proto* f) 416 | { 417 | int i,n; 418 | n=f->sizek; 419 | printf("constants (%d) for %p:\n",n,VOID(f)); 420 | for (i=0; isizelocvars; 427 | printf("locals (%d) for %p:\n",n,VOID(f)); 428 | for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 432 | } 433 | n=f->sizeupvalues; 434 | printf("upvalues (%d) for %p:\n",n,VOID(f)); 435 | for (i=0; iupvalues[i].instack,f->upvalues[i].idx); 439 | } 440 | } 441 | 442 | static void PrintFunction(const Proto* f, int full) 443 | { 444 | int i,n=f->sizep; 445 | PrintHeader(f); 446 | PrintCode(f); 447 | if (full) PrintDebug(f); 448 | for (i=0; ip[i],full); 449 | } 450 | -------------------------------------------------------------------------------- /luac.man: -------------------------------------------------------------------------------- 1 | .\" $Id: luac.man,v 1.28 2006/01/06 16:03:34 lhf Exp lhf $ 2 | .TH LUAC 1 "$Date: 2006/01/06 16:03:34 $" 3 | .SH NAME 4 | luac \- Lua compiler 5 | .SH SYNOPSIS 6 | .B luac 7 | [ 8 | .I options 9 | ] [ 10 | .I filenames 11 | ] 12 | .SH DESCRIPTION 13 | .B luac 14 | is the Lua compiler. 15 | It translates programs written in the Lua programming language 16 | into binary files containing precompiled chunks 17 | that can be later loaded and executed. 18 | .LP 19 | The main advantages of precompiling chunks are: 20 | faster loading, 21 | protecting source code from accidental user changes, 22 | and 23 | off-line syntax checking. 24 | Precompiling does not imply faster execution 25 | because in Lua chunks are always compiled into bytecodes before being executed. 26 | .B luac 27 | simply allows those bytecodes to be saved in a file for later execution. 28 | Precompiled chunks are not necessarily smaller than the corresponding source. 29 | The main goal in precompiling is faster loading. 30 | .LP 31 | In the command line, 32 | you can mix 33 | text files containing Lua source and 34 | binary files containing precompiled chunks. 35 | .B luac 36 | produces a single output file containing the combined bytecodes 37 | for all files given. 38 | Executing the combined file is equivalent to executing the given files. 39 | By default, 40 | the output file is named 41 | .BR luac.out , 42 | but you can change this with the 43 | .B \-o 44 | option. 45 | .LP 46 | Precompiled chunks are 47 | .I not 48 | portable across different architectures. 49 | Moreover, 50 | the internal format of precompiled chunks 51 | is likely to change when a new version of Lua is released. 52 | Make sure you save the source files of all Lua programs that you precompile. 53 | .LP 54 | .SH OPTIONS 55 | .TP 56 | .B \-l 57 | produce a listing of the compiled bytecode for Lua's virtual machine. 58 | Listing bytecodes is useful to learn about Lua's virtual machine. 59 | If no files are given, then 60 | .B luac 61 | loads 62 | .B luac.out 63 | and lists its contents. 64 | Use 65 | .B \-l \-l 66 | for a full listing. 67 | .TP 68 | .BI \-o " file" 69 | output to 70 | .IR file , 71 | instead of the default 72 | .BR luac.out . 73 | (You can use 74 | .B "'\-'" 75 | for standard output, 76 | but not on platforms that open standard output in text mode.) 77 | The output file may be one of the given files because 78 | all files are loaded before the output file is written. 79 | Be careful not to overwrite precious files. 80 | .TP 81 | .B \-p 82 | load files but do not generate any output file. 83 | Used mainly for syntax checking and for testing precompiled chunks: 84 | corrupted files will probably generate errors when loaded. 85 | If no files are given, then 86 | .B luac 87 | loads 88 | .B luac.out 89 | and tests its contents. 90 | No messages are displayed if the file loads without errors. 91 | .TP 92 | .B \-s 93 | strip debug information before writing the output file. 94 | This saves some space in very large chunks, 95 | but if errors occur when running a stripped chunk, 96 | then the error messages may not contain the full information they usually do. 97 | In particular, 98 | line numbers and names of local variables are lost. 99 | .TP 100 | .B \-v 101 | show version information. 102 | .TP 103 | .B \-\- 104 | stop handling options. 105 | .TP 106 | .B \- 107 | stop handling options and process standard input. 108 | .SH "SEE ALSO" 109 | .BR lua (1) 110 | .br 111 | The documentation at lua.org. 112 | .SH DIAGNOSTICS 113 | Error messages should be self explanatory. 114 | .SH AUTHORS 115 | R. Ierusalimschy, 116 | L. H. de Figueiredo, 117 | W. Celes 118 | .\" EOF 119 | --------------------------------------------------------------------------------