lua/lundump.c

245 lines
5.3 KiB
C
Raw Normal View History

1998-01-14 18:49:01 +03:00
/*
2000-10-20 20:39:03 +04:00
** $Id: lundump.c,v 1.32 2000/09/21 03:15:36 lhf Exp lhf $
1998-01-14 18:49:01 +03:00
** load bytecodes from files
** See Copyright Notice in lua.h
*/
#include <stdio.h>
1999-03-31 00:29:34 +04:00
#include <string.h>
1999-12-02 22:11:51 +03:00
1998-01-14 18:49:01 +03:00
#include "lfunc.h"
#include "lmem.h"
1999-03-31 00:29:34 +04:00
#include "lopcodes.h"
1998-01-14 18:49:01 +03:00
#include "lstring.h"
#include "lundump.h"
2000-04-25 20:44:31 +04:00
#define LoadByte ezgetc
2000-09-20 22:43:54 +04:00
static const char* ZNAME (ZIO* Z)
2000-04-25 20:44:31 +04:00
{
const char* s=zname(Z);
return (*s=='@') ? s+1 : s;
}
1998-03-26 17:50:19 +03:00
1999-12-02 22:11:51 +03:00
static void unexpectedEOZ (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2000-10-20 20:39:03 +04:00
luaO_verror(L,"unexpected end of file in `%.99s'",ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
1999-12-02 22:11:51 +03:00
static int ezgetc (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
int c=zgetc(Z);
1999-12-02 22:11:51 +03:00
if (c==EOZ) unexpectedEOZ(L,Z);
1998-01-14 18:49:01 +03:00
return c;
}
1999-12-02 22:11:51 +03:00
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
1998-01-14 18:49:01 +03:00
{
int r=zread(Z,b,n);
1999-12-02 22:11:51 +03:00
if (r!=0) unexpectedEOZ(L,Z);
1998-01-14 18:49:01 +03:00
}
2000-09-20 22:43:54 +04:00
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-20 22:43:54 +04:00
if (swap)
{
char *p=(char *) b+size-1;
int n=size;
while (n--) *p--=(char)ezgetc(L,Z);
}
else
ezread(L,Z,b,size);
1998-01-14 18:49:01 +03:00
}
2000-09-20 22:43:54 +04:00
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
if (swap)
2000-09-20 22:43:54 +04:00
{
char *q=(char *) b;
while (m--)
{
char *p=q+size-1;
int n=size;
while (n--) *p--=(char)ezgetc(L,Z);
q+=size;
}
}
2000-09-04 22:53:41 +04:00
else
2000-09-20 22:43:54 +04:00
ezread(L,Z,b,m*size);
}
static int LoadInt (lua_State* L, ZIO* Z, int swap)
{
int x;
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
2000-09-04 22:53:41 +04:00
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
{
2000-09-04 22:53:41 +04:00
size_t x;
2000-09-20 22:43:54 +04:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
2000-09-04 22:53:41 +04:00
static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
1999-03-31 00:29:34 +04:00
{
2000-09-04 22:53:41 +04:00
Number x;
2000-09-20 22:43:54 +04:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1999-03-31 00:29:34 +04:00
}
2000-09-04 22:53:41 +04:00
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
size_t size=LoadSize(L,Z,swap);
1998-03-26 17:50:19 +03:00
if (size==0)
return NULL;
1998-01-14 18:49:01 +03:00
else
{
2000-09-11 21:38:42 +04:00
char* s=luaO_openspace(L,size);
2000-09-20 22:43:54 +04:00
LoadBlock(L,s,size,Z,0);
2000-03-03 17:58:26 +03:00
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
1998-01-14 18:49:01 +03:00
}
}
2000-09-04 22:53:41 +04:00
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
2000-04-25 20:44:31 +04:00
{
2000-09-04 22:53:41 +04:00
int size=LoadInt(L,Z,swap);
tf->code=luaM_newvector(L,size,Instruction);
2000-09-20 22:43:54 +04:00
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
2000-10-20 20:39:03 +04:00
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
luaF_protook(L,tf,size);
2000-04-25 20:44:31 +04:00
}
2000-09-04 22:53:41 +04:00
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
2000-04-25 20:44:31 +04:00
{
2000-09-04 22:53:41 +04:00
int i,n;
tf->nlocvars=n=LoadInt(L,Z,swap);
tf->locvars=luaM_newvector(L,n,LocVar);
for (i=0; i<n; i++)
2000-04-25 20:44:31 +04:00
{
2000-09-04 22:53:41 +04:00
tf->locvars[i].varname=LoadString(L,Z,swap);
tf->locvars[i].startpc=LoadInt(L,Z,swap);
tf->locvars[i].endpc=LoadInt(L,Z,swap);
2000-04-25 20:44:31 +04:00
}
}
2000-09-04 22:53:41 +04:00
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
{
2000-10-20 20:39:03 +04:00
int n;
tf->nlineinfo=n=LoadInt(L,Z,swap);
2000-09-04 22:53:41 +04:00
tf->lineinfo=luaM_newvector(L,n,int);
2000-09-20 22:43:54 +04:00
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
1998-01-14 18:49:01 +03:00
}
2000-09-04 22:53:41 +04:00
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
1998-03-26 17:50:19 +03:00
2000-09-04 22:53:41 +04:00
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-03-03 17:58:26 +03:00
int i,n;
2000-09-04 22:53:41 +04:00
tf->nkstr=n=LoadInt(L,Z,swap);
tf->kstr=luaM_newvector(L,n,TString*);
for (i=0; i<n; i++)
tf->kstr[i]=LoadString(L,Z,swap);
tf->nknum=n=LoadInt(L,Z,swap);
tf->knum=luaM_newvector(L,n,Number);
2000-09-20 22:43:54 +04:00
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
2000-09-04 22:53:41 +04:00
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 Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-03-10 21:37:44 +03:00
Proto* tf=luaF_newproto(L);
2000-09-04 22:53:41 +04:00
tf->source=LoadString(L,Z,swap);
tf->lineDefined=LoadInt(L,Z,swap);
tf->numparams=LoadInt(L,Z,swap);
2000-04-25 20:44:31 +04:00
tf->is_vararg=LoadByte(L,Z);
2000-09-04 22:53:41 +04:00
tf->maxstacksize=LoadInt(L,Z,swap);
LoadLocals(L,tf,Z,swap);
LoadLines(L,tf,Z,swap);
LoadConstants(L,tf,Z,swap);
2000-10-20 20:39:03 +04:00
LoadCode(L,tf,Z,swap);
1998-01-14 18:49:01 +03:00
return tf;
}
1999-12-02 22:11:51 +03:00
static void LoadSignature (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-08-17 00:52:00 +04:00
const char* s=SIGNATURE;
1999-12-02 22:11:51 +03:00
while (*s!=0 && ezgetc(L,Z)==*s)
1998-01-14 18:49:01 +03:00
++s;
2000-10-20 20:39:03 +04:00
if (*s!=0) luaO_verror(L,"bad signature in `%.99s'",ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
2000-09-04 22:53:41 +04:00
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
{
int r=ezgetc(L,Z);
if (r!=s)
2000-10-20 20:39:03 +04:00
luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
2000-09-21 18:18:43 +04:00
" %s is %d but read %d",ZNAME(Z),what,s,r);
2000-09-04 22:53:41 +04:00
}
#define TESTSIZE(s) TestSize(L,s,#s,Z)
2000-03-03 17:58:26 +03:00
#define V(v) v/16,v%16
1999-12-02 22:11:51 +03:00
static int LoadHeader (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
int version,swap;
Number f=0,tf=TEST_NUMBER;
1999-12-02 22:11:51 +03:00
LoadSignature(L,Z);
version=ezgetc(L,Z);
1998-01-14 18:49:01 +03:00
if (version>VERSION)
2000-10-20 20:39:03 +04:00
luaO_verror(L,"`%.99s' too new:\n"
2000-04-25 20:44:31 +04:00
" read version %d.%d; expected at most %d.%d",
ZNAME(Z),V(version),V(VERSION));
1998-06-25 20:48:44 +04:00
if (version<VERSION0) /* check last major change */
2000-10-20 20:39:03 +04:00
luaO_verror(L,"`%.99s' too old:\n"
2000-04-25 20:44:31 +04:00
" read version %d.%d; expected at least %d.%d",
ZNAME(Z),V(version),V(VERSION));
2000-09-04 22:53:41 +04:00
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 */
2000-10-20 20:39:03 +04:00
luaO_verror(L,"unknown number format in `%.99s':\n"
" read " NUMBER_FMT "; expected " NUMBER_FMT, ZNAME(Z),f,tf);
2000-09-04 22:53:41 +04:00
return swap;
1998-01-14 18:49:01 +03:00
}
2000-03-10 21:37:44 +03:00
static Proto* LoadChunk (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-12-02 22:11:51 +03:00
return LoadFunction(L,Z,LoadHeader(L,Z));
1998-01-14 18:49:01 +03:00
}
/*
** load one chunk from a file or buffer
** return main if ok and NULL at EOF
*/
2000-09-04 22:53:41 +04:00
Proto* luaU_undump (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2000-09-18 23:42:05 +04:00
Proto* tf=NULL;
1998-01-14 18:49:01 +03:00
int c=zgetc(Z);
if (c==ID_CHUNK)
2000-09-18 23:42:05 +04:00
tf=LoadChunk(L,Z);
c=zgetc(Z);
if (c!=EOZ)
2000-10-20 20:39:03 +04:00
luaO_verror(L,"`%.99s' apparently contains more than one chunk",ZNAME(Z));
2000-09-18 23:42:05 +04:00
return tf;
2000-09-04 22:53:41 +04:00
}
1999-12-02 22:11:51 +03:00
/*
2000-09-04 22:53:41 +04:00
** find byte order
1999-12-02 22:11:51 +03:00
*/
2000-09-04 22:53:41 +04:00
int luaU_endianess (void)
1999-12-02 22:11:51 +03:00
{
2000-09-04 22:53:41 +04:00
int x=1;
return *(char*)&x;
1999-03-31 00:29:34 +04:00
}