diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-10-25 18:31:28 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-10-25 18:31:28 -0300 |
commit | de00d0d0add8686917bc28b7fe0f2974c155f22e (patch) | |
tree | 626fd65bcf4cbe0794d1ef4fbb63510c2186c492 | |
parent | 1713b64065d0cf5909939bb29bd4181f567cb4e9 (diff) | |
download | lua-de00d0d0add8686917bc28b7fe0f2974c155f22e.tar.gz lua-de00d0d0add8686917bc28b7fe0f2974c155f22e.tar.bz2 lua-de00d0d0add8686917bc28b7fe0f2974c155f22e.zip |
module for dumping chunks
-rw-r--r-- | ldump.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/ldump.c b/ldump.c new file mode 100644 index 00000000..10e9acd2 --- /dev/null +++ b/ldump.c | |||
@@ -0,0 +1,158 @@ | |||
1 | /* | ||
2 | ** $Id: dump.c,v 1.37 2002/08/07 00:36:03 lhf Exp lhf $ | ||
3 | ** save bytecodes | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | #include <stddef.h> | ||
8 | |||
9 | #include "lua.h" | ||
10 | |||
11 | #include "lobject.h" | ||
12 | #include "lopcodes.h" | ||
13 | #include "lstate.h" | ||
14 | #include "lundump.h" | ||
15 | |||
16 | #define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D) | ||
17 | #define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D) | ||
18 | |||
19 | typedef struct { | ||
20 | lua_State *L; | ||
21 | lua_Chunkwriter write; | ||
22 | void *data; | ||
23 | } DumpState; | ||
24 | |||
25 | |||
26 | static void DumpBlock(const void *b, size_t size, DumpState* D) | ||
27 | { | ||
28 | lua_unlock(D->L); | ||
29 | (*D->write)(D->L,b,size,D->data); | ||
30 | lua_lock(D->L); | ||
31 | } | ||
32 | |||
33 | static void DumpByte(int y, DumpState* D) | ||
34 | { | ||
35 | char x=(char)y; | ||
36 | DumpBlock(&x,sizeof(x),D); | ||
37 | } | ||
38 | |||
39 | static void DumpInt(int x, DumpState* D) | ||
40 | { | ||
41 | DumpBlock(&x,sizeof(x),D); | ||
42 | } | ||
43 | |||
44 | static void DumpSize(size_t x, DumpState* D) | ||
45 | { | ||
46 | DumpBlock(&x,sizeof(x),D); | ||
47 | } | ||
48 | |||
49 | static void DumpNumber(lua_Number x, DumpState* D) | ||
50 | { | ||
51 | DumpBlock(&x,sizeof(x),D); | ||
52 | } | ||
53 | |||
54 | static void DumpString(TString* s, DumpState* D) | ||
55 | { | ||
56 | if (s==NULL || getstr(s)==NULL) | ||
57 | DumpSize(0,D); | ||
58 | else | ||
59 | { | ||
60 | size_t size=s->tsv.len+1; /* include trailing '\0' */ | ||
61 | DumpSize(size,D); | ||
62 | DumpBlock(getstr(s),size,D); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | static void DumpCode(const Proto* f, DumpState* D) | ||
67 | { | ||
68 | DumpInt(f->sizecode,D); | ||
69 | DumpVector(f->code,f->sizecode,sizeof(*f->code),D); | ||
70 | } | ||
71 | |||
72 | static void DumpLocals(const Proto* f, DumpState* D) | ||
73 | { | ||
74 | int i,n=f->sizelocvars; | ||
75 | DumpInt(n,D); | ||
76 | for (i=0; i<n; i++) | ||
77 | { | ||
78 | DumpString(f->locvars[i].varname,D); | ||
79 | DumpInt(f->locvars[i].startpc,D); | ||
80 | DumpInt(f->locvars[i].endpc,D); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | static void DumpLines(const Proto* f, DumpState* D) | ||
85 | { | ||
86 | DumpInt(f->sizelineinfo,D); | ||
87 | DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D); | ||
88 | } | ||
89 | |||
90 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); | ||
91 | |||
92 | static void DumpConstants(const Proto* f, DumpState* D) | ||
93 | { | ||
94 | int i,n; | ||
95 | DumpInt(n=f->sizek,D); | ||
96 | for (i=0; i<n; i++) | ||
97 | { | ||
98 | const TObject* o=&f->k[i]; | ||
99 | DumpByte(ttype(o),D); | ||
100 | switch (ttype(o)) | ||
101 | { | ||
102 | case LUA_TNUMBER: | ||
103 | DumpNumber(nvalue(o),D); | ||
104 | break; | ||
105 | case LUA_TSTRING: | ||
106 | DumpString(tsvalue(o),D); | ||
107 | break; | ||
108 | case LUA_TNIL: | ||
109 | break; | ||
110 | default: | ||
111 | lua_assert(0); /* cannot happen */ | ||
112 | break; | ||
113 | } | ||
114 | } | ||
115 | DumpInt(n=f->sizep,D); | ||
116 | for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); | ||
117 | } | ||
118 | |||
119 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) | ||
120 | { | ||
121 | DumpString((f->source==p) ? NULL : f->source,D); | ||
122 | DumpInt(f->lineDefined,D); | ||
123 | DumpByte(f->nupvalues,D); | ||
124 | DumpByte(f->numparams,D); | ||
125 | DumpByte(f->is_vararg,D); | ||
126 | DumpByte(f->maxstacksize,D); | ||
127 | DumpLocals(f,D); | ||
128 | DumpLines(f,D); | ||
129 | DumpConstants(f,D); | ||
130 | DumpCode(f,D); | ||
131 | } | ||
132 | |||
133 | static void DumpHeader(DumpState* D) | ||
134 | { | ||
135 | DumpLiteral(LUA_SIGNATURE,D); | ||
136 | DumpByte(VERSION,D); | ||
137 | DumpByte(luaU_endianness(),D); | ||
138 | DumpByte(sizeof(int),D); | ||
139 | DumpByte(sizeof(size_t),D); | ||
140 | DumpByte(sizeof(Instruction),D); | ||
141 | DumpByte(SIZE_OP,D); | ||
142 | DumpByte(SIZE_A,D); | ||
143 | DumpByte(SIZE_B,D); | ||
144 | DumpByte(SIZE_C,D); | ||
145 | DumpByte(sizeof(lua_Number),D); | ||
146 | DumpNumber(TEST_NUMBER,D); | ||
147 | } | ||
148 | |||
149 | void luaU_dump(lua_State *L, const Proto* Main, lua_Chunkwriter w, void* data) | ||
150 | { | ||
151 | DumpState D; | ||
152 | D.L=L; | ||
153 | D.write=w; | ||
154 | D.data=data; | ||
155 | DumpHeader(&D); | ||
156 | DumpFunction(Main,NULL,&D); | ||
157 | } | ||
158 | |||