summaryrefslogtreecommitdiff
path: root/src/lundump.c
diff options
context:
space:
mode:
authorLua Team <team@lua.org>2000-11-06 12:00:00 +0000
committerrepogen <>2000-11-06 12:00:00 +0000
commit8cb71cb5548e3138e5d4e4744f52c79d9fafb116 (patch)
tree25859eb162c67eafc46866e0ec3a9a7ebf93157a /src/lundump.c
parentb7610da5fed99f59ac73ae452da8839a0f2c1bda (diff)
downloadlua-github-4.0.tar.gz
Lua 4.04.0
Diffstat (limited to 'src/lundump.c')
-rw-r--r--src/lundump.c303
1 files changed, 154 insertions, 149 deletions
diff --git a/src/lundump.c b/src/lundump.c
index 0c3b5fd7..a8d06106 100644
--- a/src/lundump.c
+++ b/src/lundump.c
@@ -1,239 +1,244 @@
/*
-** $Id: lundump.c,v 1.21 1999/07/02 19:34:26 lhf Exp $
+** $Id: lundump.c,v 1.33 2000/10/31 16:57:23 lhf Exp $
** load bytecodes from files
** See Copyright Notice in lua.h
*/
#include <stdio.h>
#include <string.h>
-#include "lauxlib.h"
+
#include "lfunc.h"
#include "lmem.h"
#include "lopcodes.h"
#include "lstring.h"
#include "lundump.h"
-#define LoadBlock(b,size,Z) ezread(Z,b,size)
+#define LoadByte ezgetc
-static void unexpectedEOZ (ZIO* Z)
+static const char* ZNAME (ZIO* Z)
{
- luaL_verror("unexpected end of file in %s",zname(Z));
+ const char* s=zname(Z);
+ return (*s=='@') ? s+1 : s;
}
-static int ezgetc (ZIO* Z)
+static void unexpectedEOZ (lua_State* L, ZIO* Z)
{
- int c=zgetc(Z);
- if (c==EOZ) unexpectedEOZ(Z);
- return c;
+ luaO_verror(L,"unexpected end of file in `%.99s'",ZNAME(Z));
}
-static void ezread (ZIO* Z, void* b, int n)
+static int ezgetc (lua_State* L, ZIO* Z)
{
- int r=zread(Z,b,n);
- if (r!=0) unexpectedEOZ(Z);
+ int c=zgetc(Z);
+ if (c==EOZ) unexpectedEOZ(L,Z);
+ return c;
}
-static unsigned int LoadWord (ZIO* Z)
+static void ezread (lua_State* L, ZIO* Z, void* b, int n)
{
- unsigned int hi=ezgetc(Z);
- unsigned int lo=ezgetc(Z);
- return (hi<<8)|lo;
+ int r=zread(Z,b,n);
+ if (r!=0) unexpectedEOZ(L,Z);
}
-static unsigned long LoadLong (ZIO* Z)
+static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
{
- unsigned long hi=LoadWord(Z);
- unsigned long lo=LoadWord(Z);
- return (hi<<16)|lo;
-}
-
-/*
-* convert number from text
-*/
-double luaU_str2d (char* b, char* where)
-{
- int negative=(b[0]=='-');
- double x=luaO_str2d(b+negative);
- if (x<0) luaL_verror("cannot convert number '%s' in %s",b,where);
- return negative ? -x : x;
+ if (swap)
+ {
+ char *p=(char *) b+size-1;
+ int n=size;
+ while (n--) *p--=(char)ezgetc(L,Z);
+ }
+ else
+ ezread(L,Z,b,size);
}
-static real LoadNumber (ZIO* Z, int native)
+static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
{
- real x;
- if (native)
+ if (swap)
{
- LoadBlock(&x,sizeof(x),Z);
- return x;
+ char *q=(char *) b;
+ while (m--)
+ {
+ char *p=q+size-1;
+ int n=size;
+ while (n--) *p--=(char)ezgetc(L,Z);
+ q+=size;
+ }
}
else
- {
- char b[256];
- int size=ezgetc(Z);
- LoadBlock(b,size,Z);
- b[size]=0;
- return luaU_str2d(b,zname(Z));
- }
+ ezread(L,Z,b,m*size);
}
-static int LoadInt (ZIO* Z, char* message)
+static int LoadInt (lua_State* L, ZIO* Z, int swap)
{
- unsigned long l=LoadLong(Z);
- unsigned int i=l;
- if (i!=l) luaL_verror(message,l,zname(Z));
- return i;
+ int x;
+ LoadBlock(L,&x,sizeof(x),Z,swap);
+ return x;
}
-#define PAD 5 /* two word operands plus opcode */
+static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
+{
+ size_t x;
+ LoadBlock(L,&x,sizeof(x),Z,swap);
+ return x;
+}
-static Byte* LoadCode (ZIO* Z)
+static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
{
- int size=LoadInt(Z,"code too long (%ld bytes) in %s");
- Byte* b=luaM_malloc(size+PAD);
- LoadBlock(b,size,Z);
- if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z));
- memset(b+size,ENDCODE,PAD); /* pad code for safety */
- return b;
+ Number x;
+ LoadBlock(L,&x,sizeof(x),Z,swap);
+ return x;
}
-static TaggedString* LoadTString (ZIO* Z)
+static TString* LoadString (lua_State* L, ZIO* Z, int swap)
{
- long size=LoadLong(Z);
+ size_t size=LoadSize(L,Z,swap);
if (size==0)
return NULL;
else
{
- char* s=luaL_openspace(size);
- LoadBlock(s,size,Z);
- return luaS_newlstr(s,size-1);
+ char* s=luaO_openspace(L,size);
+ LoadBlock(L,s,size,Z,0);
+ return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
}
}
-static void LoadLocals (TProtoFunc* tf, ZIO* Z)
+static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
{
- int i,n=LoadInt(Z,"too many locals (%ld) in %s");
- if (n==0) return;
- tf->locvars=luaM_newvector(n+1,LocVar);
+ int size=LoadInt(L,Z,swap);
+ tf->code=luaM_newvector(L,size,Instruction);
+ LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
+ if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
+ luaF_protook(L,tf,size);
+}
+
+static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
+{
+ int i,n;
+ tf->nlocvars=n=LoadInt(L,Z,swap);
+ tf->locvars=luaM_newvector(L,n,LocVar);
for (i=0; i<n; i++)
{
- tf->locvars[i].line=LoadInt(Z,"too many lines (%ld) in %s");
- tf->locvars[i].varname=LoadTString(Z);
+ tf->locvars[i].varname=LoadString(L,Z,swap);
+ tf->locvars[i].startpc=LoadInt(L,Z,swap);
+ tf->locvars[i].endpc=LoadInt(L,Z,swap);
}
- tf->locvars[i].line=-1; /* flag end of vector */
- tf->locvars[i].varname=NULL;
}
-static TProtoFunc* LoadFunction (ZIO* Z, int native);
+static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
+{
+ int n;
+ tf->nlineinfo=n=LoadInt(L,Z,swap);
+ tf->lineinfo=luaM_newvector(L,n,int);
+ LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
+}
+
+static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
-static void LoadConstants (TProtoFunc* tf, ZIO* Z, int native)
+static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
{
- int i,n=LoadInt(Z,"too many constants (%ld) in %s");
- tf->nconsts=n;
- if (n==0) return;
- tf->consts=luaM_newvector(n,TObject);
+ int i,n;
+ tf->nkstr=n=LoadInt(L,Z,swap);
+ tf->kstr=luaM_newvector(L,n,TString*);
for (i=0; i<n; i++)
- {
- TObject* o=tf->consts+i;
- ttype(o)=-ezgetc(Z); /* ttype(o) is negative - ORDER LUA_T */
- switch (ttype(o))
- {
- case LUA_T_NUMBER:
- nvalue(o)=LoadNumber(Z,native);
- break;
- case LUA_T_STRING:
- tsvalue(o)=LoadTString(Z);
- break;
- case LUA_T_PROTO:
- tfvalue(o)=LoadFunction(Z,native);
- break;
- case LUA_T_NIL:
- break;
- default: /* cannot happen */
- luaU_badconstant("load",i,o,tf);
- break;
- }
- }
+ tf->kstr[i]=LoadString(L,Z,swap);
+ tf->nknum=n=LoadInt(L,Z,swap);
+ tf->knum=luaM_newvector(L,n,Number);
+ LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
+ tf->nkproto=n=LoadInt(L,Z,swap);
+ tf->kproto=luaM_newvector(L,n,Proto*);
+ for (i=0; i<n; i++)
+ tf->kproto[i]=LoadFunction(L,Z,swap);
}
-static TProtoFunc* LoadFunction (ZIO* Z, int native)
+static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
{
- TProtoFunc* tf=luaF_newproto();
- tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s");
- tf->source=LoadTString(Z);
- if (tf->source==NULL) tf->source=luaS_new(zname(Z));
- tf->code=LoadCode(Z);
- LoadLocals(tf,Z);
- LoadConstants(tf,Z,native);
+ Proto* tf=luaF_newproto(L);
+ tf->source=LoadString(L,Z,swap);
+ tf->lineDefined=LoadInt(L,Z,swap);
+ tf->numparams=LoadInt(L,Z,swap);
+ tf->is_vararg=LoadByte(L,Z);
+ tf->maxstacksize=LoadInt(L,Z,swap);
+ LoadLocals(L,tf,Z,swap);
+ LoadLines(L,tf,Z,swap);
+ LoadConstants(L,tf,Z,swap);
+ LoadCode(L,tf,Z,swap);
return tf;
}
-static void LoadSignature (ZIO* Z)
+static void LoadSignature (lua_State* L, ZIO* Z)
{
- char* s=SIGNATURE;
- while (*s!=0 && ezgetc(Z)==*s)
+ const char* s=SIGNATURE;
+ while (*s!=0 && ezgetc(L,Z)==*s)
++s;
- if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
+ if (*s!=0) luaO_verror(L,"bad signature in `%.99s'",ZNAME(Z));
}
-static int LoadHeader (ZIO* Z)
+static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
+{
+ int r=ezgetc(L,Z);
+ if (r!=s)
+ luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
+ " %.20s is %d but read %d",ZNAME(Z),what,s,r);
+}
+
+#define TESTSIZE(s) TestSize(L,s,#s,Z)
+#define V(v) v/16,v%16
+
+static int LoadHeader (lua_State* L, ZIO* Z)
{
- int version,sizeofR;
- int native;
- LoadSignature(Z);
- version=ezgetc(Z);
+ int version,swap;
+ Number f=0,tf=TEST_NUMBER;
+ LoadSignature(L,Z);
+ version=ezgetc(L,Z);
if (version>VERSION)
- luaL_verror(
- "%s too new: version=0x%02x; expected at most 0x%02x",
- zname(Z),version,VERSION);
+ luaO_verror(L,"`%.99s' too new:\n"
+ " read version %d.%d; expected at most %d.%d",
+ ZNAME(Z),V(version),V(VERSION));
if (version<VERSION0) /* check last major change */
- luaL_verror(
- "%s too old: version=0x%02x; expected at least 0x%02x",
- zname(Z),version,VERSION0);
- sizeofR=ezgetc(Z);
- native=(sizeofR!=0);
- if (native) /* test number representation */
- {
- if (sizeofR!=sizeof(real))
- luaL_verror("unknown number size in %s: read %d; expected %d",
- zname(Z),sizeofR,sizeof(real));
- else
- {
- real tf=TEST_NUMBER;
- real f=LoadNumber(Z,native);
- if ((long)f!=(long)tf)
- luaL_verror("unknown number format in %s: "
- "read " NUMBER_FMT "; expected " NUMBER_FMT,
- zname(Z),f,tf);
- }
- }
- return native;
+ luaO_verror(L,"`%.99s' too old:\n"
+ " read version %d.%d; expected at least %d.%d",
+ ZNAME(Z),V(version),V(VERSION));
+ swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
+ TESTSIZE(sizeof(int));
+ TESTSIZE(sizeof(size_t));
+ TESTSIZE(sizeof(Instruction));
+ TESTSIZE(SIZE_INSTRUCTION);
+ TESTSIZE(SIZE_OP);
+ TESTSIZE(SIZE_B);
+ TESTSIZE(sizeof(Number));
+ f=LoadNumber(L,Z,swap);
+ if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
+ luaO_verror(L,"unknown number format in `%.99s':\n"
+ " read " NUMBER_FMT "; expected " NUMBER_FMT, ZNAME(Z),f,tf);
+ return swap;
}
-static TProtoFunc* LoadChunk (ZIO* Z)
+static Proto* LoadChunk (lua_State* L, ZIO* Z)
{
- return LoadFunction(Z,LoadHeader(Z));
+ return LoadFunction(L,Z,LoadHeader(L,Z));
}
/*
** load one chunk from a file or buffer
** return main if ok and NULL at EOF
*/
-TProtoFunc* luaU_undump1 (ZIO* Z)
+Proto* luaU_undump (lua_State* L, ZIO* Z)
{
+ Proto* tf=NULL;
int c=zgetc(Z);
if (c==ID_CHUNK)
- return LoadChunk(Z);
- else if (c!=EOZ)
- luaL_verror("%s is not a Lua binary file",zname(Z));
- return NULL;
+ tf=LoadChunk(L,Z);
+ c=zgetc(Z);
+ if (c!=EOZ)
+ luaO_verror(L,"`%.99s' apparently contains more than one chunk",ZNAME(Z));
+ return tf;
}
/*
-* handle constants that cannot happen
+** find byte order
*/
-void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf)
+int luaU_endianess (void)
{
- int t=ttype(o);
- char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
- luaL_verror("cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
+ int x=1;
+ return *(char*)&x;
}