summaryrefslogtreecommitdiff
path: root/src/luac/dump.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/luac/dump.c')
-rw-r--r--src/luac/dump.c126
1 files changed, 61 insertions, 65 deletions
diff --git a/src/luac/dump.c b/src/luac/dump.c
index ce9551e6..479ce5d4 100644
--- a/src/luac/dump.c
+++ b/src/luac/dump.c
@@ -1,16 +1,20 @@
/*
-** $Id: dump.c,v 1.11 1998/07/12 00:17:37 lhf Exp $
+** $Id: dump.c,v 1.20 1999/07/02 19:34:26 lhf Exp $
** save bytecodes to file
** See Copyright Notice in lua.h
*/
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "luac.h"
-#define NotWord(x) ((unsigned short)x!=x)
+#ifdef OLD_ANSI
+#define strerror(e) "(no error message provided by operating system)"
+#endif
+
#define DumpBlock(b,size,D) fwrite(b,size,1,D)
-#define DumpNative(t,D) DumpBlock(&t,sizeof(t),D)
+#define DumpInt DumpLong
static void DumpWord(int i, FILE* D)
{
@@ -28,43 +32,24 @@ static void DumpLong(long i, FILE* D)
DumpWord(lo,D);
}
-#if ID_NUMBER==ID_REAL4
-/* LUA_NUMBER */
-/* assumes sizeof(long)==4 and sizeof(float)==4 (IEEE) */
-static void DumpFloat(float f, FILE* D)
+static void DumpNumber(real x, FILE* D, int native, TProtoFunc* tf)
{
- long l=*(long*)&f;
- DumpLong(l,D);
-}
-#endif
-
-#if ID_NUMBER==ID_REAL8
-/* LUA_NUMBER */
-/* assumes sizeof(long)==4 and sizeof(double)==8 (IEEE) */
-static void DumpDouble(double f, FILE* D)
-{
- long* l=(long*)&f;
- int x=1;
- if (*(char*)&x==1) /* little-endian */
- {
- DumpLong(l[1],D);
- DumpLong(l[0],D);
- }
- else /* big-endian */
+ if (native)
+ DumpBlock(&x,sizeof(x),D);
+ else
{
- DumpLong(l[0],D);
- DumpLong(l[1],D);
+ char b[256];
+ int n;
+ sprintf(b,NUMBER_FMT"%n",x,&n);
+ luaU_str2d(b,tf->source->str); /* help lundump not to fail */
+ fputc(n,D);
+ DumpBlock(b,n,D);
}
}
-#endif
static void DumpCode(TProtoFunc* tf, FILE* D)
{
- int size=CodeSize(tf);
- if (NotWord(size))
- fprintf(stderr,"luac: warning: "
- "\"%s\":%d code too long for 16-bit machines (%d bytes)\n",
- fileName(tf),tf->lineDefined,size);
+ int size=luaU_codesize(tf);
DumpLong(size,D);
DumpBlock(tf->code,size,D);
}
@@ -72,87 +57,98 @@ static void DumpCode(TProtoFunc* tf, FILE* D)
static void DumpString(char* s, int size, FILE* D)
{
if (s==NULL)
- DumpWord(0,D);
+ DumpLong(0,D);
else
{
- if (NotWord(size))
- luaL_verror("string too long (%d bytes): \"%.32s...\"",size,s);
- DumpWord(size,D);
+ DumpLong(size,D);
DumpBlock(s,size,D);
}
}
static void DumpTString(TaggedString* s, FILE* D)
{
- if (s==NULL) DumpString(NULL,0,D); else DumpString(s->str,s->u.s.len+1,D);
+ if (s==NULL)
+ DumpString(NULL,0,D);
+ else
+ DumpString(s->str,s->u.s.len+1,D);
}
static void DumpLocals(TProtoFunc* tf, FILE* D)
{
- int n;
- LocVar* lv;
- for (n=0,lv=tf->locvars; lv && lv->line>=0; lv++) ++n;
- DumpWord(n,D);
- for (lv=tf->locvars; lv && lv->line>=0; lv++)
+ if (tf->locvars==NULL)
+ DumpInt(0,D);
+ else
{
- DumpWord(lv->line,D);
- DumpTString(lv->varname,D);
+ LocVar* v;
+ int n=0;
+ for (v=tf->locvars; v->line>=0; v++)
+ ++n;
+ DumpInt(n,D);
+ for (v=tf->locvars; v->line>=0; v++)
+ {
+ DumpInt(v->line,D);
+ DumpTString(v->varname,D);
+ }
}
}
-static void DumpFunction(TProtoFunc* tf, FILE* D);
+static void DumpFunction(TProtoFunc* tf, FILE* D, int native);
-static void DumpConstants(TProtoFunc* tf, FILE* D)
+static void DumpConstants(TProtoFunc* tf, FILE* D, int native)
{
int i,n=tf->nconsts;
- DumpWord(n,D);
+ DumpInt(n,D);
for (i=0; i<n; i++)
{
TObject* o=tf->consts+i;
- fputc(-ttype(o),D);
+ fputc(-ttype(o),D); /* ttype(o) is negative - ORDER LUA_T */
switch (ttype(o))
{
case LUA_T_NUMBER:
- DumpNumber(nvalue(o),D);
+ DumpNumber(nvalue(o),D,native,tf);
break;
case LUA_T_STRING:
DumpTString(tsvalue(o),D);
break;
case LUA_T_PROTO:
- DumpFunction(tfvalue(o),D);
+ DumpFunction(tfvalue(o),D,native);
break;
case LUA_T_NIL:
break;
default: /* cannot happen */
- luaL_verror("cannot dump constant #%d: type=%d [%s]",
- i,ttype(o),luaO_typename(o));
+ luaU_badconstant("dump",i,o,tf);
break;
}
}
}
-static void DumpFunction(TProtoFunc* tf, FILE* D)
+static void DumpFunction(TProtoFunc* tf, FILE* D, int native)
{
- DumpWord(tf->lineDefined,D);
- DumpTString(tf->fileName,D);
+ DumpInt(tf->lineDefined,D);
+ DumpTString(tf->source,D);
DumpCode(tf,D);
DumpLocals(tf,D);
- DumpConstants(tf,D);
+ DumpConstants(tf,D,native);
+ if (ferror(D))
+ luaL_verror("write error" IN ": %s (errno=%d)",INLOC,strerror(errno),errno);
}
-static void DumpHeader(TProtoFunc* Main, FILE* D)
+static void DumpHeader(TProtoFunc* Main, FILE* D, int native)
{
- real t=TEST_NUMBER;
fputc(ID_CHUNK,D);
fputs(SIGNATURE,D);
fputc(VERSION,D);
- fputc(ID_NUMBER,D);
- fputc(sizeof(t),D);
- DumpNumber(t,D);
+ if (native)
+ {
+ fputc(sizeof(real),D);
+ DumpNumber(TEST_NUMBER,D,native,Main);
+ }
+ else
+ fputc(0,D);
}
-void DumpChunk(TProtoFunc* Main, FILE* D)
+void luaU_dumpchunk(TProtoFunc* Main, FILE* D, int native)
{
- DumpHeader(Main,D);
- DumpFunction(Main,D);
+ DumpHeader(Main,D,native);
+ DumpFunction(Main,D,native);
}