/src/testdir/build/lua-master/source/ldump.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: ldump.c $ |
3 | | ** save precompiled Lua chunks |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define ldump_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <limits.h> |
14 | | #include <stddef.h> |
15 | | |
16 | | #include "lua.h" |
17 | | |
18 | | #include "lapi.h" |
19 | | #include "lgc.h" |
20 | | #include "lobject.h" |
21 | | #include "lstate.h" |
22 | | #include "ltable.h" |
23 | | #include "lundump.h" |
24 | | |
25 | | |
26 | | typedef struct { |
27 | | lua_State *L; |
28 | | lua_Writer writer; |
29 | | void *data; |
30 | | size_t offset; /* current position relative to beginning of dump */ |
31 | | int strip; |
32 | | int status; |
33 | | Table *h; /* table to track saved strings */ |
34 | | lua_Unsigned nstr; /* counter for counting saved strings */ |
35 | | } DumpState; |
36 | | |
37 | | |
38 | | /* |
39 | | ** All high-level dumps go through dumpVector; you can change it to |
40 | | ** change the endianness of the result |
41 | | */ |
42 | 0 | #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) |
43 | | |
44 | 0 | #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) |
45 | | |
46 | | |
47 | | /* |
48 | | ** Dump the block of memory pointed by 'b' with given 'size'. |
49 | | ** 'b' should not be NULL, except for the last call signaling the end |
50 | | ** of the dump. |
51 | | */ |
52 | 0 | static void dumpBlock (DumpState *D, const void *b, size_t size) { |
53 | 0 | if (D->status == 0) { /* do not write anything after an error */ |
54 | 0 | lua_unlock(D->L); |
55 | 0 | D->status = (*D->writer)(D->L, b, size, D->data); |
56 | 0 | lua_lock(D->L); |
57 | 0 | D->offset += size; |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | |
62 | | /* |
63 | | ** Dump enough zeros to ensure that current position is a multiple of |
64 | | ** 'align'. |
65 | | */ |
66 | 0 | static void dumpAlign (DumpState *D, unsigned align) { |
67 | 0 | unsigned padding = align - cast_uint(D->offset % align); |
68 | 0 | if (padding < align) { /* padding == align means no padding */ |
69 | 0 | static lua_Integer paddingContent = 0; |
70 | 0 | lua_assert(align <= sizeof(lua_Integer)); |
71 | 0 | dumpBlock(D, &paddingContent, padding); |
72 | 0 | } |
73 | 0 | lua_assert(D->offset % align == 0); |
74 | 0 | } |
75 | | |
76 | | |
77 | 0 | #define dumpVar(D,x) dumpVector(D,&x,1) |
78 | | |
79 | | |
80 | 0 | static void dumpByte (DumpState *D, int y) { |
81 | 0 | lu_byte x = (lu_byte)y; |
82 | 0 | dumpVar(D, x); |
83 | 0 | } |
84 | | |
85 | | |
86 | | /* |
87 | | ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. |
88 | | ** (The "+6" rounds up the division.) |
89 | | */ |
90 | 0 | #define DIBS ((l_numbits(lua_Unsigned) + 6) / 7) |
91 | | |
92 | | /* |
93 | | ** Dumps an unsigned integer using the MSB Varint encoding |
94 | | */ |
95 | 0 | static void dumpVarint (DumpState *D, lua_Unsigned x) { |
96 | 0 | lu_byte buff[DIBS]; |
97 | 0 | unsigned n = 1; |
98 | 0 | buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ |
99 | 0 | while ((x >>= 7) != 0) /* fill other bytes in reverse order */ |
100 | 0 | buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80); |
101 | 0 | dumpVector(D, buff + DIBS - n, n); |
102 | 0 | } |
103 | | |
104 | | |
105 | 0 | static void dumpSize (DumpState *D, size_t sz) { |
106 | 0 | dumpVarint(D, cast(lua_Unsigned, sz)); |
107 | 0 | } |
108 | | |
109 | | |
110 | 0 | static void dumpInt (DumpState *D, int x) { |
111 | 0 | lua_assert(x >= 0); |
112 | 0 | dumpVarint(D, cast_uint(x)); |
113 | 0 | } |
114 | | |
115 | | |
116 | 0 | static void dumpNumber (DumpState *D, lua_Number x) { |
117 | 0 | dumpVar(D, x); |
118 | 0 | } |
119 | | |
120 | | |
121 | | /* |
122 | | ** Signed integers are coded to keep small values small. (Coding -1 as |
123 | | ** 0xfff...fff would use too many bytes to save a quite common value.) |
124 | | ** A non-negative x is coded as 2x; a negative x is coded as -2x - 1. |
125 | | ** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...) |
126 | | */ |
127 | 0 | static void dumpInteger (DumpState *D, lua_Integer x) { |
128 | 0 | lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x) |
129 | 0 | : (2u * ~l_castS2U(x)) + 1; |
130 | 0 | dumpVarint(D, cx); |
131 | 0 | } |
132 | | |
133 | | |
134 | | /* |
135 | | ** Dump a String. First dump its "size": size==0 means NULL; |
136 | | ** size==1 is followed by an index and means "reuse saved string with |
137 | | ** that index"; size>=2 is followed by the string contents with real |
138 | | ** size==size-2 and means that string, which will be saved with |
139 | | ** the next available index. |
140 | | */ |
141 | 0 | static void dumpString (DumpState *D, TString *ts) { |
142 | 0 | if (ts == NULL) |
143 | 0 | dumpSize(D, 0); |
144 | 0 | else { |
145 | 0 | TValue idx; |
146 | 0 | int tag = luaH_getstr(D->h, ts, &idx); |
147 | 0 | if (!tagisempty(tag)) { /* string already saved? */ |
148 | 0 | dumpVarint(D, 1); /* reuse a saved string */ |
149 | 0 | dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ |
150 | 0 | } |
151 | 0 | else { /* must write and save the string */ |
152 | 0 | TValue key, value; /* to save the string in the hash */ |
153 | 0 | size_t size; |
154 | 0 | const char *s = getlstr(ts, size); |
155 | 0 | dumpSize(D, size + 2); |
156 | 0 | dumpVector(D, s, size + 1); /* include ending '\0' */ |
157 | 0 | D->nstr++; /* one more saved string */ |
158 | 0 | setsvalue(D->L, &key, ts); /* the string is the key */ |
159 | 0 | setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ |
160 | 0 | luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ |
161 | | /* integer value does not need barrier */ |
162 | 0 | } |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | |
167 | 0 | static void dumpCode (DumpState *D, const Proto *f) { |
168 | 0 | dumpInt(D, f->sizecode); |
169 | 0 | dumpAlign(D, sizeof(f->code[0])); |
170 | 0 | lua_assert(f->code != NULL); |
171 | 0 | dumpVector(D, f->code, cast_uint(f->sizecode)); |
172 | 0 | } |
173 | | |
174 | | |
175 | | static void dumpFunction (DumpState *D, const Proto *f); |
176 | | |
177 | 0 | static void dumpConstants (DumpState *D, const Proto *f) { |
178 | 0 | int i; |
179 | 0 | int n = f->sizek; |
180 | 0 | dumpInt(D, n); |
181 | 0 | for (i = 0; i < n; i++) { |
182 | 0 | const TValue *o = &f->k[i]; |
183 | 0 | int tt = ttypetag(o); |
184 | 0 | dumpByte(D, tt); |
185 | 0 | switch (tt) { |
186 | 0 | case LUA_VNUMFLT: |
187 | 0 | dumpNumber(D, fltvalue(o)); |
188 | 0 | break; |
189 | 0 | case LUA_VNUMINT: |
190 | 0 | dumpInteger(D, ivalue(o)); |
191 | 0 | break; |
192 | 0 | case LUA_VSHRSTR: |
193 | 0 | case LUA_VLNGSTR: |
194 | 0 | dumpString(D, tsvalue(o)); |
195 | 0 | break; |
196 | 0 | default: |
197 | 0 | lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); |
198 | 0 | } |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | | |
203 | 0 | static void dumpProtos (DumpState *D, const Proto *f) { |
204 | 0 | int i; |
205 | 0 | int n = f->sizep; |
206 | 0 | dumpInt(D, n); |
207 | 0 | for (i = 0; i < n; i++) |
208 | 0 | dumpFunction(D, f->p[i]); |
209 | 0 | } |
210 | | |
211 | | |
212 | 0 | static void dumpUpvalues (DumpState *D, const Proto *f) { |
213 | 0 | int i, n = f->sizeupvalues; |
214 | 0 | dumpInt(D, n); |
215 | 0 | for (i = 0; i < n; i++) { |
216 | 0 | dumpByte(D, f->upvalues[i].instack); |
217 | 0 | dumpByte(D, f->upvalues[i].idx); |
218 | 0 | dumpByte(D, f->upvalues[i].kind); |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | |
223 | 0 | static void dumpDebug (DumpState *D, const Proto *f) { |
224 | 0 | int i, n; |
225 | 0 | n = (D->strip) ? 0 : f->sizelineinfo; |
226 | 0 | dumpInt(D, n); |
227 | 0 | if (f->lineinfo != NULL) |
228 | 0 | dumpVector(D, f->lineinfo, cast_uint(n)); |
229 | 0 | n = (D->strip) ? 0 : f->sizeabslineinfo; |
230 | 0 | dumpInt(D, n); |
231 | 0 | if (n > 0) { |
232 | | /* 'abslineinfo' is an array of structures of int's */ |
233 | 0 | dumpAlign(D, sizeof(int)); |
234 | 0 | dumpVector(D, f->abslineinfo, cast_uint(n)); |
235 | 0 | } |
236 | 0 | n = (D->strip) ? 0 : f->sizelocvars; |
237 | 0 | dumpInt(D, n); |
238 | 0 | for (i = 0; i < n; i++) { |
239 | 0 | dumpString(D, f->locvars[i].varname); |
240 | 0 | dumpInt(D, f->locvars[i].startpc); |
241 | 0 | dumpInt(D, f->locvars[i].endpc); |
242 | 0 | } |
243 | 0 | n = (D->strip) ? 0 : f->sizeupvalues; |
244 | 0 | dumpInt(D, n); |
245 | 0 | for (i = 0; i < n; i++) |
246 | 0 | dumpString(D, f->upvalues[i].name); |
247 | 0 | } |
248 | | |
249 | | |
250 | 0 | static void dumpFunction (DumpState *D, const Proto *f) { |
251 | 0 | dumpInt(D, f->linedefined); |
252 | 0 | dumpInt(D, f->lastlinedefined); |
253 | 0 | dumpByte(D, f->numparams); |
254 | 0 | dumpByte(D, f->flag); |
255 | 0 | dumpByte(D, f->maxstacksize); |
256 | 0 | dumpCode(D, f); |
257 | 0 | dumpConstants(D, f); |
258 | 0 | dumpUpvalues(D, f); |
259 | 0 | dumpProtos(D, f); |
260 | 0 | dumpString(D, D->strip ? NULL : f->source); |
261 | 0 | dumpDebug(D, f); |
262 | 0 | } |
263 | | |
264 | | |
265 | | #define dumpNumInfo(D, tvar, value) \ |
266 | 0 | { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); } |
267 | | |
268 | | |
269 | 0 | static void dumpHeader (DumpState *D) { |
270 | 0 | dumpLiteral(D, LUA_SIGNATURE); |
271 | 0 | dumpByte(D, LUAC_VERSION); |
272 | 0 | dumpByte(D, LUAC_FORMAT); |
273 | 0 | dumpLiteral(D, LUAC_DATA); |
274 | 0 | dumpNumInfo(D, int, LUAC_INT); |
275 | 0 | dumpNumInfo(D, Instruction, LUAC_INST); |
276 | 0 | dumpNumInfo(D, lua_Integer, LUAC_INT); |
277 | 0 | dumpNumInfo(D, lua_Number, LUAC_NUM); |
278 | 0 | } |
279 | | |
280 | | |
281 | | /* |
282 | | ** dump Lua function as precompiled chunk |
283 | | */ |
284 | | int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data, |
285 | 0 | int strip) { |
286 | 0 | DumpState D; |
287 | 0 | D.h = luaH_new(L); /* aux. table to keep strings already dumped */ |
288 | 0 | sethvalue2s(L, L->top.p, D.h); /* anchor it */ |
289 | 0 | L->top.p++; |
290 | 0 | D.L = L; |
291 | 0 | D.writer = w; |
292 | 0 | D.offset = 0; |
293 | 0 | D.data = data; |
294 | 0 | D.strip = strip; |
295 | 0 | D.status = 0; |
296 | 0 | D.nstr = 0; |
297 | 0 | dumpHeader(&D); |
298 | 0 | dumpByte(&D, f->sizeupvalues); |
299 | 0 | dumpFunction(&D, f); |
300 | 0 | dumpBlock(&D, NULL, 0); /* signal end of dump */ |
301 | 0 | return D.status; |
302 | 0 | } |
303 | | |