forked from mefistotelis/psx_mnd_sym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_fixups.go
351 lines (324 loc) · 10.4 KB
/
parse_fixups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package csym
import (
"fmt"
"reflect"
"github.com/mefistotelis/psx_mnd_sym/csym/c"
)
// RemoveDuplicateTypes goes through parsed types and marks exact duplicates.
func (p *Parser) RemoveDuplicateTypes() {
if p.opts.Verbose { fmt.Printf("Remove duplicate types...\n") }
p.removeStructsDuplicates()
p.removeUnionsDuplicates()
p.removeStructsDuplicates()
}
// removeStructsDuplicates goes through parsed symbols and marks exact duplicates.
func (p *Parser) removeStructsDuplicates() {
// Create a type replacing map
typeRemap := make(map[c.Type]c.Type)
for _, structs := range p.StructTags {
for i := 0; i < len(structs); i++ {
t1 := structs[i]
if _, ok := typeRemap[t1]; ok { continue }
for k := i+1; k < len(structs); k++ {
t2 := structs[k]
if _, ok := typeRemap[t2]; ok { continue }
if !reflect.DeepEqual(t2, t1) { continue }
typeRemap[t2] = t1
}
}
}
// Replace the pointers in uses of types within other types and declarations
p.ReplaceUsedTypes(typeRemap)
// Replace the pointers on main lists with nil, then remove nil items
for t2, _ := range typeRemap {
typeRemap[t2] = nil
}
p.ReplaceStructs(typeRemap)
p.RmNilStructs()
if p.opts.Verbose { fmt.Printf("Removed structs: %d\n", len(typeRemap)) }
}
// removeUnionsDuplicates goes through parsed symbols and marks exact duplicates.
func (p *Parser) removeUnionsDuplicates() {
// Create a type replacing map
typeRemap := make(map[c.Type]c.Type)
for _, unions := range p.UnionTags {
for i := 0; i < len(unions); i++ {
t1 := unions[i]
if _, ok := typeRemap[t1]; ok { continue }
for k := i+1; k < len(unions); k++ {
t2 := unions[k]
if _, ok := typeRemap[t2]; ok { continue }
if !reflect.DeepEqual(t2, t1) { continue }
typeRemap[t2] = t1
}
}
}
// Replace the pointers in uses of types within other types and declarations
p.ReplaceUsedTypes(typeRemap)
// Replace the pointers on main lists with nil, then remove nil items
for t2, _ := range typeRemap {
typeRemap[t2] = nil
}
p.ReplaceUnions(typeRemap)
p.RmNilUnions()
if p.opts.Verbose { fmt.Printf("Removed unions: %d\n", len(typeRemap)) }
}
// removeEnumsDuplicates goes through parsed symbols and marks exact duplicates.
func (p *Parser) removeEnumsDuplicates() {
// Create a type replacing map
typeRemap := make(map[c.Type]c.Type)
for _, enums := range p.EnumTags {
for i := 0; i < len(enums); i++ {
t1 := enums[i]
if _, ok := typeRemap[t1]; ok { continue }
for k := i+1; k < len(enums); k++ {
t2 := enums[k]
if _, ok := typeRemap[t2]; ok { continue }
if !reflect.DeepEqual(t2, t1) { continue }
typeRemap[t2] = t1
}
}
}
// Replace the pointers in uses of types within other types and declarations
p.ReplaceUsedTypes(typeRemap)
// Replace the pointers on main lists with nil, then remove nil items
for t2, _ := range typeRemap {
typeRemap[t2] = nil
}
p.ReplaceEnums(typeRemap)
p.RmNilEnums()
if p.opts.Verbose { fmt.Printf("Removed enums: %d\n", len(typeRemap)) }
}
// replaceUsedSubtypesInType remaps sub-types within the Type interface.
func replaceUsedSubtypesInType(t c.Type, typeRemap map[c.Type]c.Type) {
switch tp := t.(type) {
case *c.PointerType:
t1, ok := typeRemap[tp.Elem]
if ok {
tp.Elem = t1
}
replaceUsedSubtypesInType(tp.Elem, typeRemap)
case *c.ArrayType:
t1, ok := typeRemap[tp.Elem]
if ok {
tp.Elem = t1
}
replaceUsedSubtypesInType(tp.Elem, typeRemap)
case *c.FuncType:
t1, ok := typeRemap[tp.RetType]
if ok {
tp.RetType = t1
}
replaceUsedSubtypesInType(tp.RetType, typeRemap)
for i := 0; i < len(tp.Params); i++ {
replaceUsedTypesInVar(&tp.Params[i].Var, typeRemap)
}
case *c.UnionType:
for i := 0; i < len(tp.Fields); i++ {
replaceUsedTypesInVar(&tp.Fields[i].Var, typeRemap)
}
}
}
func replaceUsedTypesInVar(v *c.Var, typeRemap map[c.Type]c.Type) {
t1, ok := typeRemap[v.Type]
if ok {
v.Type = t1
}
replaceUsedSubtypesInType(v.Type, typeRemap)
}
func (p *Parser) replaceUsedTypesInStructs(typeRemap map[c.Type]c.Type) {
for i := 0; i < len(p.Structs); i++ {
t := p.Structs[i]
for k := 0; k < len(t.Fields); k++ {
replaceUsedTypesInVar(&t.Fields[k].Var, typeRemap)
}
for k := 0; k < len(t.Methods); k++ {
replaceUsedTypesInVar(&t.Methods[k].Var, typeRemap)
}
}
}
func (p *Parser) replaceUsedTypesInUnions(typeRemap map[c.Type]c.Type) {
for i := 0; i < len(p.Unions); i++ {
t := p.Unions[i]
for k := 0; k < len(t.Fields); k++ {
replaceUsedTypesInVar(&t.Fields[k].Var, typeRemap)
}
}
}
func (p *Parser) replaceUsedTypesInTypedefs(typeRemap map[c.Type]c.Type) {
for i := 0; i < len(p.Typedefs); i++ {
t := p.Typedefs[i]
// Do not replace the typedef itself, only uses of types within
replaceUsedSubtypesInType(t, typeRemap)
}
}
func (p *Parser) replaceUsedVarTypesInInOverlay(overlay *Overlay, typeRemap map[c.Type]c.Type) {
for i := 0; i < len(overlay.Vars); i++ {
t := overlay.Vars[i]
replaceUsedTypesInVar(&t.Var, typeRemap)
}
}
func (p *Parser) replaceUsedFuncTypesInInOverlay(overlay *Overlay, typeRemap map[c.Type]c.Type) {
for i := 0; i < len(overlay.Funcs); i++ {
t := overlay.Funcs[i]
replaceUsedTypesInVar(&t.Var, typeRemap)
for k := 0; k < len(t.Blocks); k++ {
b := t.Blocks[k]
for n := 0; n < len(b.Locals); n++ {
replaceUsedTypesInVar(&b.Locals[n].Var, typeRemap)
}
}
}
}
func (p *Parser) ReplaceUsedTypes(typeRemap map[c.Type]c.Type) {
p.replaceUsedTypesInStructs(typeRemap)
p.replaceUsedTypesInUnions(typeRemap)
p.replaceUsedTypesInTypedefs(typeRemap)
// Default overlay
p.replaceUsedVarTypesInInOverlay(p.Overlay, typeRemap)
p.replaceUsedFuncTypesInInOverlay(p.Overlay, typeRemap)
// Other overlays
for _, overlay := range p.Overlays {
p.replaceUsedVarTypesInInOverlay(overlay, typeRemap)
p.replaceUsedFuncTypesInInOverlay(overlay, typeRemap)
}
}
// MakeNamesUnique goes through parsed symbols and renames duplicate names.
func (p *Parser) MakeNamesUnique() {
if p.opts.Verbose { fmt.Printf("Making names unique...\n") }
p.makeStructsUnique()
p.makeUnionsUnique()
p.makeEnumsUnique()
// Default overlay
p.makeVarNamesUniqueInOverlay(p.Overlay)
p.makeFuncNamesUniqueInOverlay(p.Overlay)
// Other overlays
for _, overlay := range p.Overlays {
p.makeVarNamesUniqueInOverlay(overlay)
p.makeFuncNamesUniqueInOverlay(overlay)
}
}
// makeVarNamesUniqueInOverlay goes through parsed symbols and renames duplicate ones.
func (p *Parser) makeVarNamesUniqueInOverlay(overlay *Overlay) {
for _, variables := range overlay.varNames {
// Do not rename extern declarations, only real variables
real_len := 0
for i := 0; i < len(variables); i++ {
v := variables[i]
if v.Class == c.Extern { continue }
real_len++
}
if real_len < 2 { continue }
for i := 0; i < len(variables); i++ {
v := variables[i]
if v.Class == c.Extern { continue }
v.Var.Name = UniqueVarName(overlay.varNames, v)
}
}
}
// makeFuncNamesUniqueInOverlay goes through parsed symbols and renames duplicate ones.
func (p *Parser) makeFuncNamesUniqueInOverlay(overlay *Overlay) {
for _, funcs := range overlay.funcNames {
// Do not rename extern declarations
real_len := len(funcs)
if real_len < 2 { continue }
for i := 0; i < len(funcs); i++ {
f := funcs[i]
f.Var.Name = UniqueFuncName(overlay.funcNames, f)
}
}
}
// makeStructsUnique goes through parsed symbols and renames duplicate ones.
func (p *Parser) makeStructsUnique() {
for _, structs := range p.StructTags {
real_len := len(structs)
if real_len < 2 { continue }
for i := 0; i < len(structs); i++ {
t := structs[i]
t.Tag = UniqueStructTag(p.StructTags, t)
}
}
}
// makeUnionsUnique goes through parsed symbols and renames duplicate ones.
func (p *Parser) makeUnionsUnique() {
for _, unions := range p.UnionTags {
real_len := len(unions)
if real_len < 2 { continue }
for i := 0; i < len(unions); i++ {
t := unions[i]
t.Tag = UniqueUnionTag(p.UnionTags, t)
}
}
}
// makeEnumsUnique goes through parsed symbols and renames duplicate ones.
func (p *Parser) makeEnumsUnique() {
for _, enums := range p.EnumTags {
real_len := len(enums)
if real_len < 2 { continue }
for i := 0; i < len(enums); i++ {
t := enums[i]
t.Tag = UniqueEnumTag(p.EnumTags, t)
}
}
}
// UniqueName returns a unique name based on the given name and address.
func UniqueName(name string, addr uint32) string {
return fmt.Sprintf("%s_addr_%08X", name, addr)
}
// UniqueTag returns a unique tag based on the given tag and duplicate index.
func UniqueTag(tag string, typ string, idx int) string {
return fmt.Sprintf("%s_duplicate_%s%d", tag, typ, idx)
}
// UniqueVarName returns a unique variable name based on the given variable
// and set of present variables mapped by names.
func UniqueVarName(varNames map[string][]*c.VarDecl, v *c.VarDecl) string {
newName := v.Var.Name
newName = UniqueName(newName, v.Addr)
return newName
}
// UniqueFuncName returns a unique function name based on the given function
// and set of present functions mapped by names.
func UniqueFuncName(funcNames map[string][]*c.FuncDecl, f *c.FuncDecl) string {
newName := f.Var.Name
newName = UniqueName(newName, f.Addr)
return newName
}
// UniqueStructTag returns a unique struct tag based on the given struct
// and set of present structs mapped by tags.
func UniqueStructTag(structTags map[string][]*c.StructType, t *c.StructType) string {
newTag := t.Tag
for {
structs, ok := structTags[newTag]
if !ok { break } // the tag is unique - done
k := SliceIndex(len(structs), func(i int) bool { return structs[i] == t })
if k < 0 { k = len(structs) }
newTag = UniqueTag(newTag, "s", k)
}
return newTag
}
// UniqueUnionTag returns a unique union tag based on the given union
// and set of present unions mapped by tags.
func UniqueUnionTag(unionTags map[string][]*c.UnionType, t *c.UnionType) string {
newTag := t.Tag
for {
unions, ok := unionTags[newTag]
if !ok { break } // the tag is unique - done
k := SliceIndex(len(unions), func(i int) bool { return unions[i] == t })
if k < 0 { k = len(unions) }
newTag = UniqueTag(newTag, "u", k)
}
return newTag
}
// UniqueEnumTag returns a unique enum tag based on the given enum
// and set of present enums mapped by tags.
func UniqueEnumTag(EnumTags map[string][]*c.EnumType, t *c.EnumType) string {
newTag := t.Tag
for {
enums, ok := EnumTags[newTag]
if !ok { break } // the tag is unique - done
k := SliceIndex(len(enums), func(i int) bool { return enums[i] == t })
if k < 0 { k = len(enums) }
newTag = UniqueTag(newTag, "e", k)
}
return newTag
}