compile/minichunkspy.lua

Thu, 27 May 2010 03:58:27 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 27 May 2010 03:58:27 +0100
changeset 57
db9b079c1d33
parent 45
69b3487c71cc
permissions
-rw-r--r--

compile: Update copy of minichunkspy.lua (new release)

45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Minichunkspy: Disassemble and reassemble chunks.
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright M Joonas Pihlaja 2009
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- MIT license
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- minichunkspy = require"minichunkspy"
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- chunk = string.dump(loadfile"blabla.lua")
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- disassembled_chunk = minichunkspy.disassemble(chunk)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 -- chunk = minichunkspy.assemble(disassembled_chunk)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 -- assert(minichunkspy.validate(<function or chunk>))
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 --
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
12 -- Tested on little-endian 32 and 64 bit platforms.
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local string, table, math = string, table, math
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local ipairs, setmetatable, type, assert = ipairs, setmetatable, type, assert
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local _ = __END_OF_GLOBALS__
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local string_char, string_byte, string_sub = string.char, string.byte, string.sub
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
17 local math_frexp, math_ldexp, math_abs = math.frexp, math.ldexp, math.abs
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local table_concat = table.concat
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local Inf = math.huge
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
20 local NaN = Inf - Inf
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
21
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
22 local BIG_ENDIAN = false
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
23 local SIZEOF_SIZE_T = 4
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
24 local SIZEOF_INT = 4
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
25 local SIZEOF_NUMBER = 8
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
26
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
27 local save_stack = {}
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
29 local function save()
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
30 save_stack[#save_stack+1]
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
31 = {BIG_ENDIAN, SIZEOF_SIZE_T, SIZEOF_INT, SIZEOF_NUMBER}
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
32 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
33 local function restore ()
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
34 BIG_ENDIAN, SIZEOF_SIZE_T, SIZEOF_INT, SIZEOF_NUMBER
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
35 = unpack(save_stack[#save_stack])
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
36 save_stack[#save_stack] = nil
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
37 end
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
39 local function construct (class, self)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
40 return class.new(class, self)
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local mt_memo = {}
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local Field = construct{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 new =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 function (class, self)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local self = self or {}
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local mt = mt_memo[class] or {
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 __index = class,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 __call = construct
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 mt_memo[class] = mt
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return setmetatable(self, mt)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local None = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 unpack = function (self, bytes, ix) return nil, ix end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 pack = function (self, val) return "" end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local char_memo = {}
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local function char(n)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local field = char_memo[n] or Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 unpack = function (self, bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return string_sub(bytes, ix, ix+n-1), ix+n
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 pack = function (self, val) return string_sub(val, 1, n) end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 char_memo[n] = field
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 return field
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local uint8 = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 unpack = function (self, bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return string_byte(bytes, ix, ix), ix+1
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 pack = function (self, val) return string_char(val) end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local uint32 = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 unpack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 function (self, bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local a,b,c,d = string_byte(bytes, ix, ix+3)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if BIG_ENDIAN then a,b,c,d = d,c,b,a end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return a + b*256 + c*256^2 + d*256^3, ix+4
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 pack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 function (self, val)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 assert(type(val) == "number",
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 "unexpected value type to pack as an uint32")
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local a,b,c,d
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 d = val % 2^32
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 a = d % 256; d = (d - a) / 256
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 b = d % 256; d = (d - b) / 256
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 c = d % 256; d = (d - c) / 256
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if BIG_ENDIAN then a,b,c,d = d,c,b,a end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 return string_char(a,b,c,d)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
104 local uint64 = Field{
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
105 unpack =
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
106 function (self, bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
107 local a = uint32:unpack(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
108 local b = uint32:unpack(bytes, ix+4)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
109 if BIG_ENDIAN then a,b = b,a end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
110 return a + b*2^32, ix+8
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
111 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
112 pack =
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
113 function (self, val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
114 assert(type(val) == "number",
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
115 "unexpected value type to pack as an uint64")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
116 local a = val % 2^32
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
117 local b = (val - a) / 2^32
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
118 if BIG_ENDIAN then a,b = b,a end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
119 return uint32:pack(a) .. uint32:pack(b)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
120 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
121 }
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
122
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
123 local function explode_double(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
124 local a = uint32:unpack(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
125 local b = uint32:unpack(bytes, ix+4)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
126 if BIG_ENDIAN then a,b = b,a end --XXX: ARM mixed-endian
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
127
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
128 local sig_hi = b % 2^20
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
129 local sig_lo = a
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
130 local significand = sig_lo + sig_hi*2^32
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
131
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
132 b = (b - sig_hi) / 2^20
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
133
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
134 local biased_exp = b % 2^11
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
135 local sign = b <= biased_exp and 1 or -1
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
136
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
137 --print(sign, significand, biased_exp, "explode")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
138 return sign, biased_exp, significand
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
139 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
140
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
141 local function implode_double(sign, biased_exp, significand)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
142 --print(sign, significand, biased_exp, "implode")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
143 local sig_lo = significand % 2^32
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
144 local sig_hi = (significand - sig_lo) / 2^32
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
145
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
146 local a = sig_lo
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
147 local b = ((sign < 0 and 2^11 or 0) + biased_exp)*2^20 + sig_hi
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
148
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
149 if BIG_ENDIAN then a,b = b,a end --XXX: ARM mixed-endian
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
150 return uint32.pack(nil, a) .. uint32.pack(nil, b)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
151 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
152
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
153 local function math_sign(x)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
154 if x ~= x then return x end --sign of NaN is NaN
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
155 if x == 0 then x = 1/x end --extract sign of zero
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
156 return x > 0 and 1 or -1
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
157 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
158
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
159 local SMALLEST_SUBNORMAL = math_ldexp(1, -1022 - 52)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
160 local SMALLEST_NORMAL = SMALLEST_SUBNORMAL * 2^52
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
161 local LARGEST_SUBNORMAL = math_ldexp(2^52 - 1, -1022 - 52)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
162 local LARGEST_NORMAL = math_ldexp(2^53 - 1, 1023 - 52)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
163 assert(SMALLEST_SUBNORMAL ~= 0.0 and SMALLEST_SUBNORMAL / 2 == 0.0)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
164 assert(LARGEST_NORMAL ~= Inf)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
165 assert(LARGEST_NORMAL * 2 == Inf)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
166
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
167 local double = Field{
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
168 unpack =
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
169 function (self, bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
170 local sign, biased_exp, significand = explode_double(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
171
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
172 local val
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
173 if biased_exp == 0 then --subnormal
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
174 val = math_ldexp(significand, -1022 - 52)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
175 elseif biased_exp == 2047 then
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
176 val = significand == 0 and Inf or NaN --XXX: loses NaN mantissa
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
177 else --normal
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
178 val = math_ldexp(2^52 + significand, biased_exp - 1023 - 52)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
179 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
180 val = sign*val
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
181 return val, ix+8
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
182 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
183
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
184 pack =
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
185 function (self, val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
186 if val ~= val then
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
187 return implode_double(1,2047,2^52-1) --XXX: loses NaN mantissa
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
188 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
189
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
190 local sign = math_sign(val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
191 val = math_abs(val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
192
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
193 if val == Inf then return implode_double(sign, 2047, 0) end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
194 if val == 0 then return implode_double(sign, 0, 0) end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
195
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
196 local biased_exp, significand
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
197
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
198 if val <= LARGEST_SUBNORMAL then
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
199 biased_exp = 0
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
200 significand = val / SMALLEST_SUBNORMAL
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
201 else
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
202 local frac, exp = math_frexp(val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
203 significand = (2*frac - 1)*2^52
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
204 biased_exp = exp + 1022
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
205 end
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
206 return implode_double(sign, biased_exp, significand)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
207 end
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 local Byte = uint8
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
211
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
212 local IntegralTypes = {
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
213 [4] = uint32,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
214 [8] = uint64
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
215 }
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
216
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
217 local FloatTypes = {
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
218 [4] = float,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
219 [8] = double
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
220 }
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
221
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
222 local Size_t = Field{
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
223 unpack = function (self, bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
224 return IntegralTypes[SIZEOF_SIZE_T]:unpack(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
225 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
226 pack = function (self, val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
227 return IntegralTypes[SIZEOF_SIZE_T]:pack(val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
228 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
229 }
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
230
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
231 local Integer = Field{
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
232 unpack = function (self, bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
233 return IntegralTypes[SIZEOF_INT]:unpack(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
234 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
235 pack = function (self, val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
236 return IntegralTypes[SIZEOF_INT]:pack(val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
237 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
238 }
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
239
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
240 local Number = Field{
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
241 unpack = function (self, bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
242 return FloatTypes[SIZEOF_NUMBER]:unpack(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
243 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
244 pack = function (self, val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
245 return FloatTypes[SIZEOF_NUMBER]:pack(val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
246 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
247 }
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 -- Opaque types:
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 local Insn = char(4)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 local Struct = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 unpack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 function (self, bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 local val = {}
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 local i,j = 1,1
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 while self[i] do
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 local field = self[i]
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 local key = field.name
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 if not key then key, j = j, j+1 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 --print("unpacking struct field", key, " at index ", ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 val[key], ix = field:unpack(bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 i = i+1
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 return val, ix
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 pack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 function (self, val)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 local data = {}
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 local i,j = 1,1
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 while self[i] do
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 local field = self[i]
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 local key = field.name
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 if not key then key, j = j, j+1 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 data[i] = field:pack(val[key])
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 i = i+1
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 return table_concat(data)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 local List = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 unpack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 function (self, bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 local len, ix = Integer:unpack(bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 local vals = {}
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 local field = self.type
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 for i=1,len do
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 --print("unpacking list field", i, " at index ", ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 vals[i], ix = field:unpack(bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 return vals, ix
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 pack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 function (self, vals)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 local len = #vals
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 local data = { Integer:pack(len) }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 local field = self.type
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 for i=1,len do
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 data[#data+1] = field:pack(vals[i])
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 return table_concat(data)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 local Boolean = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 unpack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 function (self, bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 local val, ix = Integer:unpack(bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 assert(val == 0 or val == 1,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 "unpacked an unexpected value "..val.." for a Boolean")
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 return val == 1, ix
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 pack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 function (self, val)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 assert(type(val) == "boolean",
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 "unexpected value type to pack as a Boolean")
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 return Integer:pack(val and 1 or 0)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 local String = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 unpack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 function (self, bytes, ix)
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
325 local len, ix = Size_t:unpack(bytes, ix)
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 local val = nil
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 if len > 0 then
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 -- len includes trailing nul byte; ignore it
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 local string_len = len - 1
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 val = bytes:sub(ix, ix+string_len-1)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 return val, ix + len
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 pack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 function (self, val)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 assert(type(val) == "nil" or type(val) == "string",
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 "unexpected value type to pack as a String")
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 if val == nil then
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
339 return Size_t:pack(0)
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 end
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
341 return Size_t:pack(#val+1) .. val .. "\000"
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 local ChunkHeader = Struct{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 char(4){name = "signature"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 Byte{name = "version"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 Byte{name = "format"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 Byte{name = "endianness"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 Byte{name = "sizeof_int"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 Byte{name = "sizeof_size_t"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 Byte{name = "sizeof_insn"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 Byte{name = "sizeof_Number"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 Byte{name = "integral_flag"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 local ConstantTypes = {
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 [0] = None,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 [1] = Boolean,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 [3] = Number,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 [4] = String,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 local Constant = Field{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 unpack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 function (self, bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 local t, ix = Byte:unpack(bytes, ix)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 local field = ConstantTypes[t]
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 assert(field, "unknown constant type "..t.." to unpack")
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 local v, ix = field:unpack(bytes, ix)
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
370 if t == 3 then
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
371 assert(type(v) == "number")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
372 end
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 return {
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 type = t,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 value = v
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 }, ix
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 pack =
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 function (self, val)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 local t, v = val.type, val.value
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 return Byte:pack(t) .. ConstantTypes[t]:pack(v)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 local Local = Struct{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 String{name = "name"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 Integer{name = "startpc"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 Integer{name = "endpc"}
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 local Function = Struct{
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 String{name = "name"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 Integer{name = "line"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 Integer{name = "last_line"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 Byte{name = "num_upvalues"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 Byte{name = "num_parameters"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 Byte{name = "is_vararg"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 Byte{name = "max_stack_size"},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 List{name = "insns", type = Insn},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 List{name = "constants", type = Constant},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 List{name = "prototypes", type = nil}, --patch type below
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 List{name = "source_lines", type = Integer},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 List{name = "locals", type = Local},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 List{name = "upvalues", type = String},
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 assert(Function[10].name == "prototypes",
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 "missed the function prototype list")
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 Function[10].type = Function
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409
57
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
410 local Chunk = Field{
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
411 unpack =
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
412 function (self, bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
413 local chunk = {}
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
414 local header, ix = ChunkHeader:unpack(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
415 assert(header.signature == "\027Lua", "signature check failed")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
416 assert(header.version == 81, "version mismatch")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
417 assert(header.format == 0, "format mismatch")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
418 assert(header.endianness == 0 or
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
419 header.endianness == 1, "endianness mismatch")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
420 assert(IntegralTypes[header.sizeof_int], "int size unsupported")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
421 assert(IntegralTypes[header.sizeof_size_t], "size_t size unsupported")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
422 assert(header.sizeof_insn == 4, "insn size unsupported")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
423 assert(FloatTypes[header.sizeof_Number], "number size unsupported")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
424 assert(header.integral_flag == 0, "integral flag mismatch; only floats supported")
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
425
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
426 save()
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
427 BIG_ENDIAN = header.endianness == 0
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
428 SIZEOF_SIZE_T = header.sizeof_size_t
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
429 SIZEOF_INT = header.sizeof_int
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
430 SIZEOF_NUMBER = header.sizeof_Number
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
431 chunk.header = header
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
432 chunk.body, ix = Function:unpack(bytes, ix)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
433 restore()
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
434 return chunk, ix
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
435 end,
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
436
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
437 pack =
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
438 function (self, val)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
439 local data
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
440 save()
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
441 local header = val.header
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
442 BIG_ENDIAN = header.endianness == 0
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
443 SIZEOF_SIZE_T = header.sizeof_size_t
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
444 SIZEOF_INT = header.sizeof_int
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
445 SIZEOF_NUMBER = header.sizeof_Number
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
446 data = ChunkHeader:pack(val.header) .. Function:pack(val.body)
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
447 restore()
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
448 return data
db9b079c1d33 compile: Update copy of minichunkspy.lua (new release)
Matthew Wild <mwild1@gmail.com>
parents: 45
diff changeset
449 end
45
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 }
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 local function validate(chunk)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 if type(chunk) == "function" then
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 return validate(string.dump(chunk))
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 local f = Chunk:unpack(chunk, 1)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 local chunk2 = Chunk:pack(f)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 if chunk == chunk2 then return true end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 local i
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 local len = math.min(#chunk, #chunk2)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 for i=1,len do
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 local a = chunk:sub(i,i)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 local b = chunk:sub(i,i)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 if a ~= b then
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 return false, ("chunk roundtripping failed: "..
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468 "first byte difference at index %d"):format(i)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 return false, ("chunk round tripping failed: "..
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 "original length %d vs. %d"):format(#chunk, #chunk2)
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 end
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 return {
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 disassemble = function (chunk) return Chunk:unpack(chunk, 1) end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 assemble = function (disassembled) return Chunk:pack(disassembled) end,
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 validate = validate
69b3487c71cc compile: Use minichunkspy for stripping debug info
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 }

mercurial