/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 | 21.5M | #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) |
43 | | |
44 | 213k | #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 | 22.2M | static void dumpBlock (DumpState *D, const void *b, size_t size) { |
53 | 22.2M | if (D->status == 0) { /* do not write anything after an error */ |
54 | 22.2M | lua_unlock(D->L); |
55 | 22.2M | D->status = (*D->writer)(D->L, b, size, D->data); |
56 | 22.2M | lua_lock(D->L); |
57 | 22.2M | D->offset += size; |
58 | 22.2M | } |
59 | 22.2M | } |
60 | | |
61 | | |
62 | | /* |
63 | | ** Dump enough zeros to ensure that current position is a multiple of |
64 | | ** 'align'. |
65 | | */ |
66 | 520k | static void dumpAlign (DumpState *D, unsigned align) { |
67 | 520k | unsigned padding = align - cast_uint(D->offset % align); |
68 | 520k | if (padding < align) { /* padding == align means no padding */ |
69 | 381k | static lua_Integer paddingContent = 0; |
70 | 381k | lua_assert(align <= sizeof(lua_Integer)); |
71 | 381k | dumpBlock(D, &paddingContent, padding); |
72 | 381k | } |
73 | 520k | lua_assert(D->offset % align == 0); |
74 | 520k | } |
75 | | |
76 | | |
77 | 7.37M | #define dumpVar(D,x) dumpVector(D,&x,1) |
78 | | |
79 | | |
80 | 5.58M | static void dumpByte (DumpState *D, int y) { |
81 | 5.58M | lu_byte x = (lu_byte)y; |
82 | 5.58M | dumpVar(D, x); |
83 | 5.58M | } |
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 | 13.5M | #define DIBS ((l_numbits(lua_Unsigned) + 6) / 7) |
91 | | |
92 | | /* |
93 | | ** Dumps an unsigned integer using the MSB Varint encoding |
94 | | */ |
95 | 12.2M | static void dumpVarint (DumpState *D, lua_Unsigned x) { |
96 | 12.2M | lu_byte buff[DIBS]; |
97 | 12.2M | unsigned n = 1; |
98 | 12.2M | buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ |
99 | 13.5M | while ((x >>= 7) != 0) /* fill other bytes in reverse order */ |
100 | 12.2M | buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80); |
101 | 12.2M | dumpVector(D, buff + DIBS - n, n); |
102 | 12.2M | } |
103 | | |
104 | | |
105 | 985k | static void dumpSize (DumpState *D, size_t sz) { |
106 | 985k | dumpVarint(D, cast(lua_Unsigned, sz)); |
107 | 985k | } |
108 | | |
109 | | |
110 | 7.47M | static void dumpInt (DumpState *D, int x) { |
111 | 7.47M | lua_assert(x >= 0); |
112 | 7.47M | dumpVarint(D, cast_uint(x)); |
113 | 7.47M | } |
114 | | |
115 | | |
116 | 1.36M | static void dumpNumber (DumpState *D, lua_Number x) { |
117 | 1.36M | dumpVar(D, x); |
118 | 1.36M | } |
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 | 23.4k | static void dumpInteger (DumpState *D, lua_Integer x) { |
128 | 23.4k | lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x) |
129 | 23.4k | : (2u * ~l_castS2U(x)) + 1; |
130 | 23.4k | dumpVarint(D, cx); |
131 | 23.4k | } |
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 | 2.87M | static void dumpString (DumpState *D, TString *ts) { |
142 | 2.87M | if (ts == NULL) |
143 | 71.9k | dumpSize(D, 0); |
144 | 2.80M | else { |
145 | 2.80M | TValue idx; |
146 | 2.80M | int tag = luaH_getstr(D->h, ts, &idx); |
147 | 2.80M | if (!tagisempty(tag)) { /* string already saved? */ |
148 | 1.89M | dumpVarint(D, 1); /* reuse a saved string */ |
149 | 1.89M | dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ |
150 | 1.89M | } |
151 | 913k | else { /* must write and save the string */ |
152 | 913k | TValue key, value; /* to save the string in the hash */ |
153 | 913k | size_t size; |
154 | 913k | const char *s = getlstr(ts, size); |
155 | 913k | dumpSize(D, size + 2); |
156 | 913k | dumpVector(D, s, size + 1); /* include ending '\0' */ |
157 | 913k | D->nstr++; /* one more saved string */ |
158 | 913k | setsvalue(D->L, &key, ts); /* the string is the key */ |
159 | 913k | setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ |
160 | 913k | luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ |
161 | | /* integer value does not need barrier */ |
162 | 913k | } |
163 | 2.80M | } |
164 | 2.87M | } |
165 | | |
166 | | |
167 | 504k | static void dumpCode (DumpState *D, const Proto *f) { |
168 | 504k | dumpInt(D, f->sizecode); |
169 | 504k | dumpAlign(D, sizeof(f->code[0])); |
170 | 504k | lua_assert(f->code != NULL); |
171 | 504k | dumpVector(D, f->code, cast_uint(f->sizecode)); |
172 | 504k | } |
173 | | |
174 | | |
175 | | static void dumpFunction (DumpState *D, const Proto *f); |
176 | | |
177 | 504k | static void dumpConstants (DumpState *D, const Proto *f) { |
178 | 504k | int i; |
179 | 504k | int n = f->sizek; |
180 | 504k | dumpInt(D, n); |
181 | 2.71M | for (i = 0; i < n; i++) { |
182 | 2.21M | const TValue *o = &f->k[i]; |
183 | 2.21M | int tt = ttypetag(o); |
184 | 2.21M | dumpByte(D, tt); |
185 | 2.21M | switch (tt) { |
186 | 1.36M | case LUA_VNUMFLT: |
187 | 1.36M | dumpNumber(D, fltvalue(o)); |
188 | 0 | break; |
189 | 23.4k | case LUA_VNUMINT: |
190 | 23.4k | dumpInteger(D, ivalue(o)); |
191 | 0 | break; |
192 | 784k | case LUA_VSHRSTR: |
193 | 797k | case LUA_VLNGSTR: |
194 | 797k | dumpString(D, tsvalue(o)); |
195 | 0 | break; |
196 | 27.2k | default: |
197 | 27.2k | lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); |
198 | 2.21M | } |
199 | 2.21M | } |
200 | 504k | } |
201 | | |
202 | | |
203 | 504k | static void dumpProtos (DumpState *D, const Proto *f) { |
204 | 504k | int i; |
205 | 504k | int n = f->sizep; |
206 | 504k | dumpInt(D, n); |
207 | 902k | for (i = 0; i < n; i++) |
208 | 397k | dumpFunction(D, f->p[i]); |
209 | 504k | } |
210 | | |
211 | | |
212 | 504k | static void dumpUpvalues (DumpState *D, const Proto *f) { |
213 | 504k | int i, n = f->sizeupvalues; |
214 | 504k | dumpInt(D, n); |
215 | 875k | for (i = 0; i < n; i++) { |
216 | 371k | dumpByte(D, f->upvalues[i].instack); |
217 | 371k | dumpByte(D, f->upvalues[i].idx); |
218 | 371k | dumpByte(D, f->upvalues[i].kind); |
219 | 371k | } |
220 | 504k | } |
221 | | |
222 | | |
223 | 504k | static void dumpDebug (DumpState *D, const Proto *f) { |
224 | 504k | int i, n; |
225 | 504k | n = (D->strip) ? 0 : f->sizelineinfo; |
226 | 504k | dumpInt(D, n); |
227 | 504k | if (f->lineinfo != NULL) |
228 | 469k | dumpVector(D, f->lineinfo, cast_uint(n)); |
229 | 504k | n = (D->strip) ? 0 : f->sizeabslineinfo; |
230 | 504k | dumpInt(D, n); |
231 | 504k | if (n > 0) { |
232 | | /* 'abslineinfo' is an array of structures of int's */ |
233 | 15.4k | dumpAlign(D, sizeof(int)); |
234 | 15.4k | dumpVector(D, f->abslineinfo, cast_uint(n)); |
235 | 15.4k | } |
236 | 504k | n = (D->strip) ? 0 : f->sizelocvars; |
237 | 504k | dumpInt(D, n); |
238 | 1.72M | for (i = 0; i < n; i++) { |
239 | 1.21M | dumpString(D, f->locvars[i].varname); |
240 | 1.21M | dumpInt(D, f->locvars[i].startpc); |
241 | 1.21M | dumpInt(D, f->locvars[i].endpc); |
242 | 1.21M | } |
243 | 504k | n = (D->strip) ? 0 : f->sizeupvalues; |
244 | 504k | dumpInt(D, n); |
245 | 866k | for (i = 0; i < n; i++) |
246 | 361k | dumpString(D, f->upvalues[i].name); |
247 | 504k | } |
248 | | |
249 | | |
250 | 504k | static void dumpFunction (DumpState *D, const Proto *f) { |
251 | 504k | dumpInt(D, f->linedefined); |
252 | 504k | dumpInt(D, f->lastlinedefined); |
253 | 504k | dumpByte(D, f->numparams); |
254 | 504k | dumpByte(D, f->flag); |
255 | 504k | dumpByte(D, f->maxstacksize); |
256 | 504k | dumpCode(D, f); |
257 | 504k | dumpConstants(D, f); |
258 | 504k | dumpUpvalues(D, f); |
259 | 504k | dumpProtos(D, f); |
260 | 504k | dumpString(D, D->strip ? NULL : f->source); |
261 | 504k | dumpDebug(D, f); |
262 | 504k | } |
263 | | |
264 | | |
265 | | #define dumpNumInfo(D, tvar, value) \ |
266 | 427k | { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); } |
267 | | |
268 | | |
269 | 106k | static void dumpHeader (DumpState *D) { |
270 | 106k | dumpLiteral(D, LUA_SIGNATURE); |
271 | 106k | dumpByte(D, LUAC_VERSION); |
272 | 106k | dumpByte(D, LUAC_FORMAT); |
273 | 106k | dumpLiteral(D, LUAC_DATA); |
274 | 106k | dumpNumInfo(D, int, LUAC_INT); |
275 | 106k | dumpNumInfo(D, Instruction, LUAC_INST); |
276 | 106k | dumpNumInfo(D, lua_Integer, LUAC_INT); |
277 | 106k | dumpNumInfo(D, lua_Number, LUAC_NUM); |
278 | 106k | } |
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 | 106k | int strip) { |
286 | 106k | DumpState D; |
287 | 106k | D.h = luaH_new(L); /* aux. table to keep strings already dumped */ |
288 | 106k | sethvalue2s(L, L->top.p, D.h); /* anchor it */ |
289 | 106k | L->top.p++; |
290 | 106k | D.L = L; |
291 | 106k | D.writer = w; |
292 | 106k | D.offset = 0; |
293 | 106k | D.data = data; |
294 | 106k | D.strip = strip; |
295 | 106k | D.status = 0; |
296 | 106k | D.nstr = 0; |
297 | 106k | dumpHeader(&D); |
298 | 106k | dumpByte(&D, f->sizeupvalues); |
299 | 106k | dumpFunction(&D, f); |
300 | 106k | dumpBlock(&D, NULL, 0); /* signal end of dump */ |
301 | 106k | return D.status; |
302 | 106k | } |
303 | | |