2 ** $Id: dump.c,v 1.30 2000/10/31 16:57:23 lhf Exp $
3 ** save bytecodes to file
4 ** See Copyright Notice in lua.h
13 #define DumpVector(b,n,size,D) fwrite(b,size,n,D)
14 #define DumpBlock(b,size,D) fwrite(b,size,1,D)
15 #define DumpByte fputc
17 static void DumpInt(int x
, FILE* D
)
19 DumpBlock(&x
,sizeof(x
),D
);
22 static void DumpSize(size_t x
, FILE* D
)
24 DumpBlock(&x
,sizeof(x
),D
);
27 static void DumpNumber(Number x
, FILE* D
)
29 DumpBlock(&x
,sizeof(x
),D
);
32 static void DumpString(const TString
* s
, FILE* D
)
34 if (s
==NULL
|| s
->str
==NULL
)
38 size_t size
=s
->len
+1; /* include trailing '\0' */
40 DumpBlock(s
->str
,size
,D
);
44 static void DumpCode(const Proto
* tf
, FILE* D
)
47 DumpVector(tf
->code
,tf
->ncode
,sizeof(*tf
->code
),D
);
50 static void DumpLocals(const Proto
* tf
, FILE* D
)
56 DumpString(tf
->locvars
[i
].varname
,D
);
57 DumpInt(tf
->locvars
[i
].startpc
,D
);
58 DumpInt(tf
->locvars
[i
].endpc
,D
);
62 static void DumpLines(const Proto
* tf
, FILE* D
)
64 DumpInt(tf
->nlineinfo
,D
);
65 DumpVector(tf
->lineinfo
,tf
->nlineinfo
,sizeof(*tf
->lineinfo
),D
);
68 static void DumpFunction(const Proto
* tf
, FILE* D
);
70 static void DumpConstants(const Proto
* tf
, FILE* D
)
73 DumpInt(n
=tf
->nkstr
,D
);
75 DumpString(tf
->kstr
[i
],D
);
77 DumpVector(tf
->knum
,tf
->nknum
,sizeof(*tf
->knum
),D
);
78 DumpInt(n
=tf
->nkproto
,D
);
80 DumpFunction(tf
->kproto
[i
],D
);
83 static void DumpFunction(const Proto
* tf
, FILE* D
)
85 DumpString(tf
->source
,D
);
86 DumpInt(tf
->lineDefined
,D
);
87 DumpInt(tf
->numparams
,D
);
88 DumpByte(tf
->is_vararg
,D
);
89 DumpInt(tf
->maxstacksize
,D
);
96 perror("luac: write error");
101 static void DumpHeader(FILE* D
)
103 DumpByte(ID_CHUNK
,D
);
106 DumpByte(luaU_endianess(),D
);
107 DumpByte(sizeof(int),D
);
108 DumpByte(sizeof(size_t),D
);
109 DumpByte(sizeof(Instruction
),D
);
110 DumpByte(SIZE_INSTRUCTION
,D
);
113 DumpByte(sizeof(Number
),D
);
114 DumpNumber(TEST_NUMBER
,D
);
117 void luaU_dumpchunk(const Proto
* Main
, FILE* D
)
120 DumpFunction(Main
,D
);