summaryrefslogtreecommitdiff
path: root/src/luac
diff options
context:
space:
mode:
Diffstat (limited to 'src/luac')
-rw-r--r--src/luac/Makefile8
l---------src/luac/RCS1
-rw-r--r--src/luac/README2
-rw-r--r--src/luac/luac.c77
-rw-r--r--src/luac/print.c39
5 files changed, 68 insertions, 59 deletions
diff --git a/src/luac/Makefile b/src/luac/Makefile
index 9e772b41..a6c7d38f 100644
--- a/src/luac/Makefile
+++ b/src/luac/Makefile
@@ -12,8 +12,8 @@ T= $(BIN)/luac
all: $T
-$T: $(OBJS) $(LIB)/liblua.a $(LIB)/liblualib.a
- $(CC) -o $@ $(MYLDFLAGS) $(OBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(DLLIB)
+$T: $(OBJS) $(LIB)/liblua.a ../lib/lauxlib.o
+ $(CC) -o $@ $(MYLDFLAGS) $(OBJS) ../lib/lauxlib.o -L$(LIB) -llua $(EXTRA_LIBS)
# print.c needs opcode names from lopcodes.c
lopcodes.o: ../lopcodes.c ../lopcodes.h
@@ -22,8 +22,8 @@ lopcodes.o: ../lopcodes.c ../lopcodes.h
$(LIB)/liblua.a:
cd ..; $(MAKE)
-$(LIB)/liblualib.a:
- cd ../lib; $(MAKE)
+../lib/lauxlib.o:
+ cd ../lib; $(MAKE) lauxlib.o
clean:
rm -f $(OBJS) $T
diff --git a/src/luac/RCS b/src/luac/RCS
new file mode 120000
index 00000000..1ae38936
--- /dev/null
+++ b/src/luac/RCS
@@ -0,0 +1 @@
+../RCS \ No newline at end of file
diff --git a/src/luac/README b/src/luac/README
index ada7bc4b..140d9457 100644
--- a/src/luac/README
+++ b/src/luac/README
@@ -6,7 +6,7 @@ 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:
+Usage: luac [options] [filenames]. Available options are:
- process stdin
-l list
-o name output to file `name' (default is "luac.out")
diff --git a/src/luac/luac.c b/src/luac/luac.c
index 1aff0bd9..f634d5f9 100644
--- a/src/luac/luac.c
+++ b/src/luac/luac.c
@@ -1,9 +1,10 @@
/*
-** $Id: luac.c,v 1.44a 2003/04/07 20:34:20 lhf Exp $
+** $Id: luac.c,v 1.47 2004/03/24 00:25:08 lhf Exp $
** Lua compiler (saves bytecodes to files; also list bytecodes)
** See Copyright Notice in lua.h
*/
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -11,6 +12,7 @@
#include "lua.h"
#include "lauxlib.h"
+#include "ldo.h"
#include "lfunc.h"
#include "lmem.h"
#include "lobject.h"
@@ -18,21 +20,19 @@
#include "lstring.h"
#include "lundump.h"
-#ifndef LUA_DEBUG
-#define luaB_opentests(L)
-#endif
-
#ifndef PROGNAME
-#define PROGNAME "luac" /* program name */
+#define PROGNAME "luac" /* default program name */
#endif
+#ifndef OUTPUT
#define OUTPUT "luac.out" /* default output file */
+#endif
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* output=Output; /* actual output file name */
static const char* progname=PROGNAME; /* actual program name */
static void fatal(const char* message)
@@ -41,29 +41,30 @@ static void fatal(const char* message)
exit(EXIT_FAILURE);
}
-static void cannot(const char* name, const char* what, const char* mode)
+static void cannot(const char* what)
{
- fprintf(stderr,"%s: cannot %s %sput file ",progname,what,mode);
- perror(name);
+ fprintf(stderr,"%s: cannot %s output file %s: %s\n",
+ progname,what,output,strerror(errno));
exit(EXIT_FAILURE);
}
-static void usage(const char* message, const char* arg)
+static void usage(const char* message)
{
- if (message!=NULL)
- {
- fprintf(stderr,"%s: ",progname); fprintf(stderr,message,arg); fprintf(stderr,"\n");
- }
+ if (*message=='-')
+ fprintf(stderr,"%s: unrecognized option `%s'\n",progname,message);
+ else
+ fprintf(stderr,"%s: %s\n",progname,message);
fprintf(stderr,
- "usage: %s [options] [filenames]. Available options are:\n"
+ "usage: %s [options] [filenames].\n"
+ "Available options are:\n"
" - process stdin\n"
" -l list\n"
- " -o name output to file `name' (default is \"" OUTPUT "\")\n"
+ " -o name output to file `name' (default is \"%s\")\n"
" -p parse only\n"
" -s strip debug information\n"
" -v show version information\n"
" -- stop handling options\n",
- progname);
+ progname,Output);
exit(EXIT_FAILURE);
}
@@ -89,7 +90,7 @@ static int doargs(int argc, char* argv[])
else if (IS("-o")) /* output file */
{
output=argv[++i];
- if (output==NULL || *output==0) usage("`-o' needs argument",NULL);
+ if (output==NULL || *output==0) usage("`-o' needs argument");
}
else if (IS("-p")) /* parse only */
dumping=0;
@@ -101,7 +102,7 @@ static int doargs(int argc, char* argv[])
if (argc==2) exit(EXIT_SUCCESS);
}
else /* unknown option */
- usage("unrecognized option `%s'",argv[i]);
+ usage(argv[i]);
}
if (i==argc && (listing || !dumping))
{
@@ -125,6 +126,7 @@ static Proto* combine(lua_State* L, int n)
{
int i,pc=0;
Proto* f=luaF_newproto(L);
+ setptvalue2s(L,L->top,f); incr_top(L);
f->source=luaS_newliteral(L,"=(" PROGNAME ")");
f->maxstacksize=1;
f->p=luaM_newvector(L,n,Proto*);
@@ -142,23 +144,17 @@ static Proto* combine(lua_State* L, int n)
}
}
-static void strip(lua_State* L, Proto* f)
+static int writer(lua_State* L, const void* p, size_t size, void* u)
{
- 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]);
+ UNUSED(L);
+ return fwrite(p,size,1,(FILE*)u)==1;
}
-static int writer(lua_State* L, const void* p, size_t size, void* u)
+static int panic(lua_State *L)
{
UNUSED(L);
- return fwrite(p,size,1,(FILE*)u)==1;
+ fatal("not enough memory!");
+ return 0;
}
int main(int argc, char* argv[])
@@ -167,9 +163,11 @@ int main(int argc, char* argv[])
Proto* f;
int i=doargs(argc,argv);
argc-=i; argv+=i;
- if (argc<=0) usage("no input files given",NULL);
+ if (argc<=0) usage("no input files given");
L=lua_open();
- luaB_opentests(L);
+ if (L==NULL) fatal("not enough memory for state");
+ lua_atpanic(L,panic);
+ if (!lua_checkstack(L,argc)) fatal("too many input files");
for (i=0; i<argc; i++)
{
const char* filename=IS("-") ? NULL : argv[i];
@@ -180,14 +178,13 @@ int main(int argc, char* argv[])
if (dumping)
{
FILE* D=fopen(output,"wb");
- if (D==NULL) cannot(output,"open","out");
- if (stripping) strip(L,f);
+ if (D==NULL) cannot("open");
lua_lock(L);
- luaU_dump(L,f,writer,D);
+ luaU_dump(L,f,writer,D,stripping);
lua_unlock(L);
- if (ferror(D)) cannot(output,"write","out");
- fclose(D);
+ if (ferror(D)) cannot("write");
+ if (fclose(D)) cannot("close");
}
lua_close(L);
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/src/luac/print.c b/src/luac/print.c
index d0b5efb2..2f9e7d12 100644
--- a/src/luac/print.c
+++ b/src/luac/print.c
@@ -1,12 +1,12 @@
/*
-** $Id: print.c,v 1.44 2003/04/07 20:34:20 lhf Exp $
+** $Id: print.c,v 1.46 2004/03/24 00:25:08 lhf Exp $
** print bytecodes
** See Copyright Notice in lua.h
*/
#include <stdio.h>
-#if 0
+#if 1
#define DEBUG_PRINT
#endif
@@ -46,7 +46,7 @@ static void PrintString(const Proto* f, int n)
static void PrintConstant(const Proto* f, int i)
{
- const TObject* o=&f->k[i];
+ const TValue* o=&f->k[i];
switch (ttype(o))
{
case LUA_TNUMBER:
@@ -75,8 +75,8 @@ static void PrintCode(const Proto* f)
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 bx=GETARG_Bx(i);
+ int sbx=GETARG_sBx(i);
int line=getline(f,pc);
#if 0
printf("%0*lX",Sizeof(i)*2,i);
@@ -86,14 +86,24 @@ static void PrintCode(const Proto* f)
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;
+ case iABC:
+ printf("%d",a);
+ if (getBMode(o)!=OpArgN)
+ { if (b>=MAXSTACK) printf(" #%d",b-MAXSTACK); else printf(" %d",b); }
+ if (getCMode(o)!=OpArgN)
+ { if (c>=MAXSTACK) printf(" #%d",c-MAXSTACK); else printf(" %d",c); }
+ break;
+ case iABx:
+ if (getBMode(o)==OpArgK) printf("%d #%d",a,bx); else printf("%d %d",a,bx);
+ break;
+ case iAsBx:
+ if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
+ break;
}
switch (o)
{
case OP_LOADK:
- printf("\t; "); PrintConstant(f,bc);
+ printf("\t; "); PrintConstant(f,bx);
break;
case OP_GETUPVAL:
case OP_SETUPVAL:
@@ -101,7 +111,7 @@ static void PrintCode(const Proto* f)
break;
case OP_GETGLOBAL:
case OP_SETGLOBAL:
- printf("\t; %s",svalue(&f->k[bc]));
+ printf("\t; %s",svalue(&f->k[bx]));
break;
case OP_GETTABLE:
case OP_SELF:
@@ -121,16 +131,17 @@ static void PrintCode(const Proto* f)
printf("\t; ");
if (b>=MAXSTACK) PrintConstant(f,b-MAXSTACK); else printf("-");
printf(" ");
- if (c>=MAXSTACK) PrintConstant(f,c-MAXSTACK);
+ if (c>=MAXSTACK) PrintConstant(f,c-MAXSTACK); else printf("-");
}
break;
case OP_JMP:
case OP_FORLOOP:
+ case OP_FORPREP:
case OP_TFORPREP:
- printf("\t; to %d",sbc+pc+2);
+ printf("\t; to %d",sbx+pc+2);
break;
case OP_CLOSURE:
- printf("\t; %p",VOID(f->p[bc]));
+ printf("\t; %p",VOID(f->p[bx]));
break;
default:
break;
@@ -187,7 +198,7 @@ static void PrintLocals(const Proto* 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);
+ i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
}
}