Imported from ../lua-3.2.tar.gz.
[lua.git] / src / luac / dump.c
blob479ce5d416e16d341bee5abd73e641e4acc6ddb6
1 /*
2 ** $Id: dump.c,v 1.20 1999/07/02 19:34:26 lhf Exp $
3 ** save bytecodes to file
4 ** See Copyright Notice in lua.h
5 */
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "luac.h"
12 #ifdef OLD_ANSI
13 #define strerror(e) "(no error message provided by operating system)"
14 #endif
16 #define DumpBlock(b,size,D) fwrite(b,size,1,D)
17 #define DumpInt DumpLong
19 static void DumpWord(int i, FILE* D)
21 int hi= 0x0000FF & (i>>8);
22 int lo= 0x0000FF & i;
23 fputc(hi,D);
24 fputc(lo,D);
27 static void DumpLong(long i, FILE* D)
29 int hi= 0x00FFFF & (i>>16);
30 int lo= 0x00FFFF & i;
31 DumpWord(hi,D);
32 DumpWord(lo,D);
35 static void DumpNumber(real x, FILE* D, int native, TProtoFunc* tf)
37 if (native)
38 DumpBlock(&x,sizeof(x),D);
39 else
41 char b[256];
42 int n;
43 sprintf(b,NUMBER_FMT"%n",x,&n);
44 luaU_str2d(b,tf->source->str); /* help lundump not to fail */
45 fputc(n,D);
46 DumpBlock(b,n,D);
50 static void DumpCode(TProtoFunc* tf, FILE* D)
52 int size=luaU_codesize(tf);
53 DumpLong(size,D);
54 DumpBlock(tf->code,size,D);
57 static void DumpString(char* s, int size, FILE* D)
59 if (s==NULL)
60 DumpLong(0,D);
61 else
63 DumpLong(size,D);
64 DumpBlock(s,size,D);
68 static void DumpTString(TaggedString* s, FILE* D)
70 if (s==NULL)
71 DumpString(NULL,0,D);
72 else
73 DumpString(s->str,s->u.s.len+1,D);
76 static void DumpLocals(TProtoFunc* tf, FILE* D)
78 if (tf->locvars==NULL)
79 DumpInt(0,D);
80 else
82 LocVar* v;
83 int n=0;
84 for (v=tf->locvars; v->line>=0; v++)
85 ++n;
86 DumpInt(n,D);
87 for (v=tf->locvars; v->line>=0; v++)
89 DumpInt(v->line,D);
90 DumpTString(v->varname,D);
95 static void DumpFunction(TProtoFunc* tf, FILE* D, int native);
97 static void DumpConstants(TProtoFunc* tf, FILE* D, int native)
99 int i,n=tf->nconsts;
100 DumpInt(n,D);
101 for (i=0; i<n; i++)
103 TObject* o=tf->consts+i;
104 fputc(-ttype(o),D); /* ttype(o) is negative - ORDER LUA_T */
105 switch (ttype(o))
107 case LUA_T_NUMBER:
108 DumpNumber(nvalue(o),D,native,tf);
109 break;
110 case LUA_T_STRING:
111 DumpTString(tsvalue(o),D);
112 break;
113 case LUA_T_PROTO:
114 DumpFunction(tfvalue(o),D,native);
115 break;
116 case LUA_T_NIL:
117 break;
118 default: /* cannot happen */
119 luaU_badconstant("dump",i,o,tf);
120 break;
125 static void DumpFunction(TProtoFunc* tf, FILE* D, int native)
127 DumpInt(tf->lineDefined,D);
128 DumpTString(tf->source,D);
129 DumpCode(tf,D);
130 DumpLocals(tf,D);
131 DumpConstants(tf,D,native);
132 if (ferror(D))
133 luaL_verror("write error" IN ": %s (errno=%d)",INLOC,strerror(errno),errno);
136 static void DumpHeader(TProtoFunc* Main, FILE* D, int native)
138 fputc(ID_CHUNK,D);
139 fputs(SIGNATURE,D);
140 fputc(VERSION,D);
141 if (native)
143 fputc(sizeof(real),D);
144 DumpNumber(TEST_NUMBER,D,native,Main);
146 else
147 fputc(0,D);
150 void luaU_dumpchunk(TProtoFunc* Main, FILE* D, int native)
152 DumpHeader(Main,D,native);
153 DumpFunction(Main,D,native);