lua/undump.c

334 lines
5.6 KiB
C
Raw Normal View History

1996-02-23 11:04:38 -08:00
/*
** undump.c
** load bytecodes from files
*/
char* rcs_undump="$Id: undump.c,v 1.20 1996/11/16 20:14:23 lhf Exp lhf $";
1996-02-23 11:04:38 -08:00
#include <stdio.h>
#include <string.h>
#include "opcode.h"
#include "mem.h"
#include "table.h"
#include "undump.h"
1996-02-23 11:04:38 -08:00
static int swapword=0;
static int swapfloat=0;
static TFunc* Main=NULL; /* functions in a chunk */
static TFunc* lastF=NULL;
1996-03-06 08:01:08 -08:00
static void warn(char* s) /* TODO: remove */
1996-02-23 11:04:38 -08:00
{
#if 0
fprintf(stderr,"undump: %s\n",s);
#endif
1996-02-23 11:04:38 -08:00
}
static void FixCode(Byte* code, Byte* end) /* swap words */
1996-02-23 11:04:38 -08:00
{
Byte* p;
for (p=code; p!=end;)
{
OpCode op=(OpCode)*p;
switch (op)
{
case PUSHNIL:
case PUSH0:
case PUSH1:
case PUSH2:
case PUSHLOCAL0:
case PUSHLOCAL1:
case PUSHLOCAL2:
case PUSHLOCAL3:
case PUSHLOCAL4:
case PUSHLOCAL5:
case PUSHLOCAL6:
case PUSHLOCAL7:
case PUSHLOCAL8:
case PUSHLOCAL9:
case PUSHINDEXED:
case STORELOCAL0:
case STORELOCAL1:
case STORELOCAL2:
case STORELOCAL3:
case STORELOCAL4:
case STORELOCAL5:
case STORELOCAL6:
case STORELOCAL7:
case STORELOCAL8:
case STORELOCAL9:
case STOREINDEXED0:
case ADJUST0:
case EQOP:
case LTOP:
case LEOP:
case GTOP:
case GEOP:
case ADDOP:
case SUBOP:
case MULTOP:
case DIVOP:
case POWOP:
case CONCOP:
case MINUSOP:
case NOTOP:
case POP:
case RETCODE0:
p++;
break;
case PUSHBYTE:
case PUSHLOCAL:
case STORELOCAL:
case STOREINDEXED:
case STORELIST0:
case ADJUST:
case RETCODE:
p+=2;
break;
case STORELIST:
case CALLFUNC:
p+=3;
break;
case PUSHFUNCTION:
1996-11-14 03:44:34 -08:00
p+=5; /* TODO: use sizeof(TFunc*) or old? */
break;
case PUSHWORD:
case PUSHSELF:
case CREATEARRAY:
case ONTJMP:
case ONFJMP:
case JMP:
case UPJMP:
case IFFJMP:
case IFFUPJMP:
case SETLINE:
case PUSHSTRING:
case PUSHGLOBAL:
case STOREGLOBAL:
{
Byte t;
t=p[1]; p[1]=p[2]; p[2]=t;
p+=3;
break;
}
1996-11-14 03:44:34 -08:00
case PUSHFLOAT: /* assumes sizeof(float)==4 */
{
Byte t;
t=p[1]; p[1]=p[4]; p[4]=t;
t=p[2]; p[2]=p[3]; p[3]=t;
p+=5;
break;
}
case STORERECORD:
{
int n=*++p;
p++;
while (n--)
{
Byte t;
t=p[0]; p[0]=p[1]; p[1]=t;
p+=2;
}
break;
}
default:
lua_error("corrupt binary file");
break;
}
}
1996-02-23 11:04:38 -08:00
}
1996-03-06 08:01:08 -08:00
static void Unthread(Byte* code, int i, int v)
1996-02-23 11:04:38 -08:00
{
while (i!=0)
{
1996-11-07 05:59:51 -08:00
Word w;
1996-03-06 08:01:08 -08:00
Byte* p=code+i;
1996-11-07 06:13:28 -08:00
memcpy(&w,p,sizeof(w));
i=w; w=v;
memcpy(p,&w,sizeof(w));
1996-02-23 11:04:38 -08:00
}
}
1996-03-06 08:01:08 -08:00
static int LoadWord(FILE* D)
1996-02-23 11:04:38 -08:00
{
Word w;
fread(&w,sizeof(w),1,D);
if (swapword)
{
1996-03-14 09:31:15 -08:00
Byte* p=(Byte*)&w; /* TODO: need union? */
Byte t;
t=p[0]; p[0]=p[1]; p[1]=t;
}
1996-02-23 11:04:38 -08:00
return w;
}
1996-03-06 13:40:10 -08:00
static int LoadSize(FILE* D)
{
Word hi=LoadWord(D);
Word lo=LoadWord(D);
int s=(hi<<16)|lo;
if ((Word)s != s) lua_error("code too long");
1996-03-06 13:40:10 -08:00
return s;
}
1996-11-16 12:14:23 -08:00
static void* LoadBlock(int size, FILE* D)
{
1996-11-16 12:14:23 -08:00
void* b=luaI_malloc(size);
fread(b,size,1,D);
return b;
}
1996-03-06 08:01:08 -08:00
static char* LoadString(FILE* D)
{
int size=LoadWord(D);
char *b=luaI_buffer(size);
fread(b,size,1,D);
return b;
}
static char* LoadNewString(FILE* D)
1996-02-23 11:04:38 -08:00
{
return LoadBlock(LoadWord(D),D);
1996-02-23 11:04:38 -08:00
}
1996-03-06 08:01:08 -08:00
static void LoadFunction(FILE* D)
1996-02-23 11:04:38 -08:00
{
1996-03-06 08:01:08 -08:00
TFunc* tf=new(TFunc);
tf->next=NULL;
1996-03-06 08:01:08 -08:00
tf->locvars=NULL;
1996-03-06 13:40:10 -08:00
tf->size=LoadSize(D);
1996-02-23 11:04:38 -08:00
tf->lineDefined=LoadWord(D);
if (IsMain(tf)) /* new main */
{
tf->fileName=LoadNewString(D);
Main=lastF=tf;
}
else /* fix PUSHFUNCTION */
{
tf->marked=LoadWord(D);
tf->fileName=Main->fileName;
1996-11-07 05:59:51 -08:00
memcpy(Main->code+tf->marked,&tf,sizeof(tf));
lastF=lastF->next=tf;
}
tf->code=LoadBlock(tf->size,D);
if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
while (1) /* unthread */
1996-02-23 11:04:38 -08:00
{
int c=getc(D);
if (c==ID_VAR) /* global var */
1996-02-23 11:04:38 -08:00
{
int i=LoadWord(D);
1996-03-06 08:01:08 -08:00
char* s=LoadString(D);
int v=luaI_findsymbolbyname(s);
1996-02-23 11:04:38 -08:00
Unthread(tf->code,i,v);
}
else if (c==ID_STR) /* constant string */
1996-02-23 11:04:38 -08:00
{
int i=LoadWord(D);
1996-03-06 08:01:08 -08:00
char* s=LoadString(D);
int v=luaI_findconstantbyname(s);
1996-02-23 11:04:38 -08:00
Unthread(tf->code,i,v);
}
else
{
ungetc(c,D);
break;
1996-02-23 11:04:38 -08:00
}
}
}
1996-03-06 08:01:08 -08:00
static void LoadSignature(FILE* D)
{
1996-03-06 08:01:08 -08:00
char* s=SIGNATURE;
while (*s!=0 && getc(D)==*s)
++s;
if (*s!=0) lua_error("bad signature");
}
1996-03-06 08:01:08 -08:00
static void LoadHeader(FILE* D) /* TODO: error handling */
1996-02-23 11:04:38 -08:00
{
Word w,tw=TEST_WORD;
float f,tf=TEST_FLOAT;
1996-11-14 03:44:34 -08:00
int version;
LoadSignature(D);
1996-11-14 03:44:34 -08:00
version=getc(D);
1996-11-14 07:00:32 -08:00
if (version>0x23) /* after 2.5 */
1996-11-14 03:44:34 -08:00
{
int oldsizeofW=getc(D);
1996-11-14 03:44:34 -08:00
int oldsizeofF=getc(D);
int oldsizeofP=getc(D);
if (oldsizeofW!=2)
lua_error("cannot load binary file created on machine with sizeof(Word)!=2");
if (oldsizeofF!=4)
lua_error("cannot load binary file created on machine with sizeof(float)!=4. not an IEEE machine?");
1996-11-14 05:33:15 -08:00
if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack */
lua_error("cannot load binary file: different pointer sizes");
1996-11-14 03:44:34 -08:00
}
fread(&w,sizeof(w),1,D); /* test word */
if (w!=tw)
{
swapword=1;
warn("different byte order");
}
fread(&f,sizeof(f),1,D); /* test float */
if (f!=tf)
{
1996-03-14 09:31:15 -08:00
Byte* p=(Byte*)&f; /* TODO: need union? */
Byte t;
swapfloat=1;
t=p[0]; p[0]=p[3]; p[3]=t;
t=p[1]; p[1]=p[2]; p[2]=t;
if (f!=tf) /* TODO: try another perm? */
lua_error("different float representation");
else
warn("different byte order in floats");
}
1996-02-23 11:04:38 -08:00
}
1996-03-06 08:01:08 -08:00
static void LoadChunk(FILE* D)
1996-02-23 11:04:38 -08:00
{
LoadHeader(D);
while (1)
{
int c=getc(D);
if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }
1996-02-23 11:04:38 -08:00
}
}
/*
** load one chunk from a file.
** return list of functions found, headed by main, or NULL at EOF.
*/
TFunc* luaI_undump1(FILE* D)
1996-02-23 11:04:38 -08:00
{
while (1)
{
int c=getc(D);
if (c==ID_CHUNK)
{
LoadChunk(D);
return Main;
}
else if (c==EOF)
return NULL;
else
lua_error("not a lua binary file");
1996-02-23 11:04:38 -08:00
}
}
/*
** load and run all chunks in a file
*/
int luaI_undump(FILE* D)
1996-02-23 11:04:38 -08:00
{
TFunc* m;
while ((m=luaI_undump1(D)))
1996-02-26 11:44:17 -08:00
{
int status=luaI_dorun(m);
luaI_freefunc(m);
if (status!=0) return status;
1996-02-26 11:44:17 -08:00
}
return 0;
1996-02-23 11:04:38 -08:00
}