0
|
1 require "ldyntype"
|
|
2
|
|
3 -- utilities
|
|
4
|
|
5 local function align(x, align)
|
|
6 return math.floor( ( x + (align-1) ) / align ) * align
|
|
7 end
|
|
8
|
|
9 -- typeinfo table constructors
|
|
10
|
|
11 function typeinfo_base(signature, size, align)
|
|
12 return { name = signature, signature = signature, class = "base", size = size, align = align }
|
|
13 end
|
|
14
|
|
15 function typeinfo_ptr(signature, basetypeinfo, ptrlevels)
|
|
16 return { name = signature, signature = signature, class = "ptr", basetypeinfo = basetypeinfo, ptrlevels = ptrlevels, size = _TI.p.size, align = _TI.p.align }
|
|
17 end
|
|
18
|
|
19 function typeinfo_struct(name, signature, size, align, fields)
|
|
20 return { name = name, signature = signature, class = "struct" , size = size, align = align , fields = fields }
|
|
21 end
|
|
22
|
|
23 local function istypeinfo(typespec)
|
|
24 return type(typespec) == "table"
|
|
25 end
|
|
26
|
|
27 function typeinfo_tostring(typename)
|
|
28 local ti = gettypeinfo(typename)
|
|
29 local s = ti.name .. "\tsize=" .. ti.size .. "\talign=" .. ti.align
|
|
30 if ti.basetypeinfo then
|
|
31 local baseti = gettypeinfo(ti.basetypeinfo)
|
|
32 if baseti.name then
|
|
33 s = s .. "\tbase=" .. baseti.name
|
|
34 else
|
|
35 s = s .. "\tbase=" .. baseti
|
|
36 end
|
|
37 end
|
|
38 if ti.fields then
|
|
39 for k, v in pairs(ti.fields) do
|
|
40 s = s .. "\n- "..k .. ":\toffset=" .. v.offset
|
|
41 end
|
|
42 end
|
|
43 return s
|
|
44 end
|
|
45
|
|
46
|
|
47 -- typeinfo registry
|
|
48
|
|
49 _TI = { }
|
|
50
|
|
51 function settypeinfo(name,info)
|
|
52 _TI[name] = info
|
|
53 end
|
|
54
|
|
55 function gettypeinfo(typespec)
|
|
56 if istypeinfo(typespec) then return typespec end
|
|
57 local typeinfo = _TI[typespec]
|
|
58 if not typeinfo then -- pointer type ?
|
|
59 local ptrs, basename = typespec:match("(%**)<(%a+)>")
|
|
60 if ptrs and basename then -- pointer type signature:
|
|
61 typeinfo = typeinfo_ptr( typespec, gettypeinfo(basename), #ptrs)
|
|
62 settypeinfo(typespec, typeinfo)
|
|
63 end -- else, typespec remains a string (unknown type)
|
|
64 end
|
|
65 return typeinfo
|
|
66 end
|
|
67
|
|
68 function dumptypeinfos()
|
|
69 for k,v in pairs(_TI) do
|
|
70 print(typeinfo_tostring(v))
|
|
71 end
|
|
72 end
|
|
73
|
|
74 function dumptypeinfo(x)
|
|
75 print(typeinfo_tostring(x))
|
|
76 end
|
|
77
|
|
78 --
|
|
79
|
|
80 local function regbasetypes()
|
|
81 local sigs = "BcCsSiIjJlLpZ"
|
|
82 for typechar in sigs:gmatch(".") do
|
|
83 settypeinfo(typechar, typeinfo_base( typechar, ldyntype.dtSize(typechar), ldyntype.dtAlign(typechar) ) )
|
|
84 end
|
|
85 end
|
|
86
|
|
87 function regstructinfo(structsignature)
|
|
88 local name, typeclass, signature, fieldnames = structsignature:match("(%a+)([{|])(.+)%}(.+)")
|
|
89
|
|
90 if name and type and signature then
|
|
91 local offset = 0
|
|
92 local maxalign = 0
|
|
93 local maxsize = 0
|
|
94 local fields = { }
|
|
95
|
|
96 local fieldname = signature:gmatch("(%S+)")
|
|
97
|
|
98 for typespec, fieldname in signature:gmatch("(%S)") do
|
|
99
|
|
100 local typeinfo = gettypeinfo(typespec)
|
|
101
|
|
102 offset = align(offset, typeinfo.align)
|
|
103 maxalign = math.max(typeinfo.align,maxalign)
|
|
104 maxsize = math.max(typeinfo.size, maxsize)
|
|
105
|
|
106 fields[fieldname] = { offset = offset, typeinfo = typeinfo }
|
|
107
|
|
108 if typeclass == "{" then -- structure
|
|
109 offset = offset + typeinfo.size
|
|
110 end
|
|
111
|
|
112 end
|
|
113
|
|
114 local structsize
|
|
115
|
|
116 if typeclass == "|" then
|
|
117 structsize = maxsize
|
|
118 else
|
|
119 structsize = offset
|
|
120 end
|
|
121
|
|
122 settypeinfo(name, typeinfo_struct(name, signature, structsize, maxalign, fields) )
|
|
123 end
|
|
124
|
|
125 end
|
|
126
|
|
127 function regstructinfo_backup(structsignature)
|
|
128 local name, typeclass, signature = structsignature:match("(%a+)([{|])(.+)%}")
|
|
129
|
|
130 if name and type and signature then
|
|
131 local offset = 0
|
|
132 local maxalign = 0
|
|
133 local maxsize = 0
|
|
134 local fields = { }
|
|
135 for typespec, fieldname in signature:gmatch("(%S+)%s+(%S+)") do
|
|
136
|
|
137 local typeinfo = gettypeinfo(typespec)
|
|
138
|
|
139 offset = align(offset, typeinfo.align)
|
|
140 maxalign = math.max(typeinfo.align,maxalign)
|
|
141 maxsize = math.max(typeinfo.size, maxsize)
|
|
142
|
|
143 fields[fieldname] = { offset = offset, typeinfo = typeinfo }
|
|
144
|
|
145 if typeclass == "{" then -- structure
|
|
146 offset = offset + typeinfo.size
|
|
147 end
|
|
148
|
|
149 end
|
|
150
|
|
151 local structsize
|
|
152
|
|
153 if typeclass == "|" then
|
|
154 structsize = maxsize
|
|
155 else
|
|
156 structsize = offset
|
|
157 end
|
|
158
|
|
159 settypeinfo(name, typeinfo_struct(name, signature, structsize, maxalign, fields) )
|
|
160 end
|
|
161
|
|
162 end
|
|
163
|
|
164 function parsebasetypes(basetypesignature)
|
|
165 for type,size,align in basetypesignature:gmatch("(%a+)%s+(%d)%s+(%d)%s*") do
|
|
166 settypeinfo(type, {signature=type,base=type,class="storage",size=size,align=align})
|
|
167 end
|
|
168 end
|
|
169
|
|
170 -- initialize typeinfo registry
|
|
171
|
|
172 regbasetypes()
|
|
173
|