diff options
Diffstat (limited to '')
-rw-r--r-- | LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/ldump.lua | 362 |
1 files changed, 362 insertions, 0 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/ldump.lua b/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/ldump.lua new file mode 100644 index 0000000..1b3c608 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/ldump.lua | |||
@@ -0,0 +1,362 @@ | |||
1 | --[[-------------------------------------------------------------------- | ||
2 | |||
3 | ldump.lua | ||
4 | Save bytecodes in Lua | ||
5 | This file is part of Yueliang. | ||
6 | |||
7 | Copyright (c) 2005 Kein-Hong Man <khman@users.sf.net> | ||
8 | The COPYRIGHT file describes the conditions | ||
9 | under which this software may be distributed. | ||
10 | |||
11 | See the ChangeLog for more information. | ||
12 | |||
13 | ----------------------------------------------------------------------]] | ||
14 | |||
15 | --[[-------------------------------------------------------------------- | ||
16 | -- Notes: | ||
17 | -- * LUA_NUMBER (double), byte order (little endian) and some other | ||
18 | -- header values hard-coded; see other notes below... | ||
19 | -- * One significant difference is that instructions are still in table | ||
20 | -- form (with OP/A/B/C/Bx fields) and luaP:Instruction() is needed to | ||
21 | -- convert them into 4-char strings | ||
22 | -- * Deleted: | ||
23 | -- luaU:DumpVector: folded into DumpLines, DumpCode | ||
24 | -- * Added: | ||
25 | -- luaU:endianness() (from lundump.c) | ||
26 | -- luaU:make_setS: create a chunk writer that writes to a string | ||
27 | -- luaU:make_setF: create a chunk writer that writes to a file | ||
28 | -- (lua.h contains a typedef for a Chunkwriter pointer, and | ||
29 | -- a Lua-based implementation exists, writer() in lstrlib.c) | ||
30 | -- luaU:from_double(x): encode double value for writing | ||
31 | -- luaU:from_int(x): encode integer value for writing | ||
32 | -- (error checking is limited for these conversion functions) | ||
33 | -- (double conversion does not support denormals or NaNs) | ||
34 | -- luaU:ttype(o) (from lobject.h) | ||
35 | ----------------------------------------------------------------------]] | ||
36 | |||
37 | --requires luaP | ||
38 | luaU = {} | ||
39 | |||
40 | -- constants used by dumper | ||
41 | luaU.LUA_TNUMBER = 3 -- (all in lua.h) | ||
42 | luaU.LUA_TSTRING = 4 | ||
43 | luaU.LUA_TNIL = 0 | ||
44 | luaU.LUA_TNONE = -1 | ||
45 | |||
46 | -- definitions for headers of binary files | ||
47 | luaU.LUA_SIGNATURE = "\27Lua" -- binary files start with "<esc>Lua" | ||
48 | luaU.VERSION = 80 -- 0x50; last format change was in 5.0 | ||
49 | luaU.VERSION0 = 80 -- 0x50; last major change was in 5.0 | ||
50 | |||
51 | -- a multiple of PI for testing native format | ||
52 | -- multiplying by 1E7 gives non-trivial integer values | ||
53 | luaU.TEST_NUMBER = 3.14159265358979323846E7 | ||
54 | |||
55 | --[[-------------------------------------------------------------------- | ||
56 | -- Additional functions to handle chunk writing | ||
57 | -- * to use make_setS and make_setF, see test_ldump.lua elsewhere | ||
58 | ----------------------------------------------------------------------]] | ||
59 | |||
60 | ------------------------------------------------------------------------ | ||
61 | -- works like the lobject.h version except that TObject used in these | ||
62 | -- scripts only has a 'value' field, no 'tt' field (native types used) | ||
63 | ------------------------------------------------------------------------ | ||
64 | function luaU:ttype(o) | ||
65 | local tt = type(o.value) | ||
66 | if tt == "number" then return self.LUA_TNUMBER | ||
67 | elseif tt == "string" then return self.LUA_TSTRING | ||
68 | elseif tt == "nil" then return self.LUA_TNIL | ||
69 | else | ||
70 | return self.LUA_TNONE -- the rest should not appear | ||
71 | end | ||
72 | end | ||
73 | |||
74 | ------------------------------------------------------------------------ | ||
75 | -- create a chunk writer that writes to a string | ||
76 | -- * returns the writer function and a table containing the string | ||
77 | -- * to get the final result, look in buff.data | ||
78 | ------------------------------------------------------------------------ | ||
79 | function luaU:make_setS() | ||
80 | local buff = {} | ||
81 | buff.data = "" | ||
82 | local writer = | ||
83 | function(s, buff) -- chunk writer | ||
84 | if not s then return end | ||
85 | buff.data = buff.data..s | ||
86 | end | ||
87 | return writer, buff | ||
88 | end | ||
89 | |||
90 | ------------------------------------------------------------------------ | ||
91 | -- create a chunk writer that writes to a file | ||
92 | -- * returns the writer function and a table containing the file handle | ||
93 | -- * if a nil is passed, then writer should close the open file | ||
94 | ------------------------------------------------------------------------ | ||
95 | function luaU:make_setF(filename) | ||
96 | local buff = {} | ||
97 | buff.h = io.open(filename, "wb") | ||
98 | if not buff.h then return nil end | ||
99 | local writer = | ||
100 | function(s, buff) -- chunk writer | ||
101 | if not buff.h then return end | ||
102 | if not s then buff.h:close(); return end | ||
103 | buff.h:write(s) | ||
104 | end | ||
105 | return writer, buff | ||
106 | end | ||
107 | |||
108 | ----------------------------------------------------------------------- | ||
109 | -- converts a IEEE754 double number to an 8-byte little-endian string | ||
110 | -- * luaU:from_double() and luaU:from_int() are from ChunkBake project | ||
111 | -- * supports +/- Infinity, but not denormals or NaNs | ||
112 | ----------------------------------------------------------------------- | ||
113 | function luaU:from_double(x) | ||
114 | local function grab_byte(v) | ||
115 | return math.floor(v / 256), | ||
116 | string.char(math.mod(math.floor(v), 256)) | ||
117 | end | ||
118 | local sign = 0 | ||
119 | if x < 0 then sign = 1; x = -x end | ||
120 | local mantissa, exponent = math.frexp(x) | ||
121 | if x == 0 then -- zero | ||
122 | mantissa, exponent = 0, 0 | ||
123 | elseif x == 1/0 then | ||
124 | mantissa, exponent = 0, 2047 | ||
125 | else | ||
126 | mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, 53) | ||
127 | exponent = exponent + 1022 | ||
128 | end | ||
129 | local v, byte = "" -- convert to bytes | ||
130 | x = mantissa | ||
131 | for i = 1,6 do | ||
132 | x, byte = grab_byte(x); v = v..byte -- 47:0 | ||
133 | end | ||
134 | x, byte = grab_byte(exponent * 16 + x); v = v..byte -- 55:48 | ||
135 | x, byte = grab_byte(sign * 128 + x); v = v..byte -- 63:56 | ||
136 | return v | ||
137 | end | ||
138 | |||
139 | ----------------------------------------------------------------------- | ||
140 | -- converts a number to a little-endian 32-bit integer string | ||
141 | -- * input value assumed to not overflow, can be signed/unsigned | ||
142 | ----------------------------------------------------------------------- | ||
143 | function luaU:from_int(x) | ||
144 | local v = "" | ||
145 | x = math.floor(x) | ||
146 | if x >= 0 then | ||
147 | for i = 1, 4 do | ||
148 | v = v..string.char(math.mod(x, 256)); x = math.floor(x / 256) | ||
149 | end | ||
150 | else-- x < 0 | ||
151 | x = -x | ||
152 | local carry = 1 | ||
153 | for i = 1, 4 do | ||
154 | local c = 255 - math.mod(x, 256) + carry | ||
155 | if c == 256 then c = 0; carry = 1 else carry = 0 end | ||
156 | v = v..string.char(c); x = math.floor(x / 256) | ||
157 | end | ||
158 | end | ||
159 | return v | ||
160 | end | ||
161 | |||
162 | --[[-------------------------------------------------------------------- | ||
163 | -- Functions to make a binary chunk | ||
164 | -- * many functions have the size parameter removed, since output is | ||
165 | -- in the form of a string and some sizes are implicit or hard-coded | ||
166 | -- * luaU:DumpVector has been deleted (used in DumpCode & DumpLines) | ||
167 | ----------------------------------------------------------------------]] | ||
168 | |||
169 | ------------------------------------------------------------------------ | ||
170 | -- dump a block of literal bytes | ||
171 | ------------------------------------------------------------------------ | ||
172 | function luaU:DumpLiteral(s, D) self:DumpBlock(s, D) end | ||
173 | |||
174 | --[[-------------------------------------------------------------------- | ||
175 | -- struct DumpState: | ||
176 | -- L -- lua_State (not used in this script) | ||
177 | -- write -- lua_Chunkwriter (chunk writer function) | ||
178 | -- data -- void* (chunk writer context or data already written) | ||
179 | ----------------------------------------------------------------------]] | ||
180 | |||
181 | ------------------------------------------------------------------------ | ||
182 | -- dumps a block of bytes | ||
183 | -- * lua_unlock(D.L), lua_lock(D.L) deleted | ||
184 | ------------------------------------------------------------------------ | ||
185 | function luaU:DumpBlock(b, D) D.write(b, D.data) end | ||
186 | |||
187 | ------------------------------------------------------------------------ | ||
188 | -- dumps a single byte | ||
189 | ------------------------------------------------------------------------ | ||
190 | function luaU:DumpByte(y, D) | ||
191 | self:DumpBlock(string.char(y), D) | ||
192 | end | ||
193 | |||
194 | ------------------------------------------------------------------------ | ||
195 | -- dumps a 32-bit signed integer (for int) | ||
196 | ------------------------------------------------------------------------ | ||
197 | function luaU:DumpInt(x, D) | ||
198 | self:DumpBlock(self:from_int(x), D) | ||
199 | end | ||
200 | |||
201 | ------------------------------------------------------------------------ | ||
202 | -- dumps a 32-bit unsigned integer (for size_t) | ||
203 | ------------------------------------------------------------------------ | ||
204 | function luaU:DumpSize(x, D) | ||
205 | self:DumpBlock(self:from_int(x), D) | ||
206 | end | ||
207 | |||
208 | ------------------------------------------------------------------------ | ||
209 | -- dumps a LUA_NUMBER (hard-coded as a double) | ||
210 | ------------------------------------------------------------------------ | ||
211 | function luaU:DumpNumber(x, D) | ||
212 | self:DumpBlock(self:from_double(x), D) | ||
213 | end | ||
214 | |||
215 | ------------------------------------------------------------------------ | ||
216 | -- dumps a Lua string | ||
217 | ------------------------------------------------------------------------ | ||
218 | function luaU:DumpString(s, D) | ||
219 | if s == nil then | ||
220 | self:DumpSize(0, D) | ||
221 | else | ||
222 | s = s.."\0" -- include trailing '\0' | ||
223 | self:DumpSize(string.len(s), D) | ||
224 | self:DumpBlock(s, D) | ||
225 | end | ||
226 | end | ||
227 | |||
228 | ------------------------------------------------------------------------ | ||
229 | -- dumps instruction block from function prototype | ||
230 | ------------------------------------------------------------------------ | ||
231 | function luaU:DumpCode(f, D) | ||
232 | local n = f.sizecode | ||
233 | self:DumpInt(n, D) | ||
234 | --was DumpVector | ||
235 | for i = 0, n - 1 do | ||
236 | self:DumpBlock(luaP:Instruction(f.code[i]), D) | ||
237 | end | ||
238 | end | ||
239 | |||
240 | ------------------------------------------------------------------------ | ||
241 | -- dumps local variable names from function prototype | ||
242 | ------------------------------------------------------------------------ | ||
243 | function luaU:DumpLocals(f, D) | ||
244 | local n = f.sizelocvars | ||
245 | self:DumpInt(n, D) | ||
246 | for i = 0, n - 1 do | ||
247 | self:DumpString(f.locvars[i].varname, D) | ||
248 | self:DumpInt(f.locvars[i].startpc, D) | ||
249 | self:DumpInt(f.locvars[i].endpc, D) | ||
250 | end | ||
251 | end | ||
252 | |||
253 | ------------------------------------------------------------------------ | ||
254 | -- dumps line information from function prototype | ||
255 | ------------------------------------------------------------------------ | ||
256 | function luaU:DumpLines(f, D) | ||
257 | local n = f.sizelineinfo | ||
258 | self:DumpInt(n, D) | ||
259 | --was DumpVector | ||
260 | for i = 0, n - 1 do | ||
261 | self:DumpInt(f.lineinfo[i], D) -- was DumpBlock | ||
262 | end | ||
263 | end | ||
264 | |||
265 | ------------------------------------------------------------------------ | ||
266 | -- dump upvalue names from function prototype | ||
267 | ------------------------------------------------------------------------ | ||
268 | function luaU:DumpUpvalues(f, D) | ||
269 | local n = f.sizeupvalues | ||
270 | self:DumpInt(n, D) | ||
271 | for i = 0, n - 1 do | ||
272 | self:DumpString(f.upvalues[i], D) | ||
273 | end | ||
274 | end | ||
275 | |||
276 | ------------------------------------------------------------------------ | ||
277 | -- dump constant pool from function prototype | ||
278 | -- * nvalue(o) and tsvalue(o) macros removed | ||
279 | ------------------------------------------------------------------------ | ||
280 | function luaU:DumpConstants(f, D) | ||
281 | local n = f.sizek | ||
282 | self:DumpInt(n, D) | ||
283 | for i = 0, n - 1 do | ||
284 | local o = f.k[i] -- TObject | ||
285 | local tt = self:ttype(o) | ||
286 | self:DumpByte(tt, D) | ||
287 | if tt == self.LUA_TNUMBER then | ||
288 | self:DumpNumber(o.value, D) | ||
289 | elseif tt == self.LUA_TSTRING then | ||
290 | self:DumpString(o.value, D) | ||
291 | elseif tt == self.LUA_TNIL then | ||
292 | else | ||
293 | --lua_assert(0) -- cannot happen | ||
294 | end | ||
295 | end | ||
296 | n = f.sizep | ||
297 | self:DumpInt(n, D) | ||
298 | for i = 0, n - 1 do | ||
299 | self:DumpFunction(f.p[i], f.source, D) | ||
300 | end | ||
301 | end | ||
302 | |||
303 | ------------------------------------------------------------------------ | ||
304 | -- dump child function prototypes from function prototype | ||
305 | ------------------------------------------------------------------------ | ||
306 | function luaU:DumpFunction(f, p, D) | ||
307 | local source = f.source | ||
308 | if source == p then source = nil end | ||
309 | self:DumpString(source, D) | ||
310 | self:DumpInt(f.lineDefined, D) | ||
311 | self:DumpByte(f.nups, D) | ||
312 | self:DumpByte(f.numparams, D) | ||
313 | self:DumpByte(f.is_vararg, D) | ||
314 | self:DumpByte(f.maxstacksize, D) | ||
315 | self:DumpLines(f, D) | ||
316 | self:DumpLocals(f, D) | ||
317 | self:DumpUpvalues(f, D) | ||
318 | self:DumpConstants(f, D) | ||
319 | self:DumpCode(f, D) | ||
320 | end | ||
321 | |||
322 | ------------------------------------------------------------------------ | ||
323 | -- dump Lua header section (some sizes hard-coded) | ||
324 | ------------------------------------------------------------------------ | ||
325 | function luaU:DumpHeader(D) | ||
326 | self:DumpLiteral(self.LUA_SIGNATURE, D) | ||
327 | self:DumpByte(self.VERSION, D) | ||
328 | self:DumpByte(self:endianness(), D) | ||
329 | self:DumpByte(4, D) -- sizeof(int) | ||
330 | self:DumpByte(4, D) -- sizeof(size_t) | ||
331 | self:DumpByte(4, D) -- sizeof(Instruction) | ||
332 | self:DumpByte(luaP.SIZE_OP, D) | ||
333 | self:DumpByte(luaP.SIZE_A, D) | ||
334 | self:DumpByte(luaP.SIZE_B, D) | ||
335 | self:DumpByte(luaP.SIZE_C, D) | ||
336 | self:DumpByte(8, D) -- sizeof(lua_Number) | ||
337 | self:DumpNumber(self.TEST_NUMBER, D) | ||
338 | end | ||
339 | |||
340 | ------------------------------------------------------------------------ | ||
341 | -- dump function as precompiled chunk | ||
342 | -- * w, data are created from make_setS, make_setF | ||
343 | ------------------------------------------------------------------------ | ||
344 | function luaU:dump(L, Main, w, data) | ||
345 | local D = {} -- DumpState | ||
346 | D.L = L | ||
347 | D.write = w | ||
348 | D.data = data | ||
349 | self:DumpHeader(D) | ||
350 | self:DumpFunction(Main, nil, D) | ||
351 | -- added: for a chunk writer writing to a file, this final call with | ||
352 | -- nil data is to indicate to the writer to close the file | ||
353 | D.write(nil, D.data) | ||
354 | end | ||
355 | |||
356 | ------------------------------------------------------------------------ | ||
357 | -- find byte order (from lundump.c) | ||
358 | -- * hard-coded to little-endian | ||
359 | ------------------------------------------------------------------------ | ||
360 | function luaU:endianness() | ||
361 | return 1 | ||
362 | end | ||