forked from Happy-Ferret/sym
-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.go
356 lines (317 loc) · 8.91 KB
/
type.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
351
352
353
354
355
356
package c
import (
"fmt"
"sort"
"strconv"
"strings"
"text/tabwriter"
)
// Type is a C type.
type Type interface {
fmt.Stringer
// Def returns the C syntax representation of the definition of the type.
Def() string
}
// --- [ Base type ] -----------------------------------------------------------
//go:generate stringer -linecomment -type BaseType
// BaseType is a base type.
type BaseType uint8
// Base types.
const (
Void BaseType = iota + 1 // void
Char // char
Short // short
Int // int
Long // long
UChar // unsigned char
UShort // unsigned short
UInt // unsigned int
ULong // unsigned long
)
// Def returns the C syntax representation of the definition of the type.
func (t BaseType) Def() string {
return t.String()
}
// --- [ Struct type ] ---------------------------------------------------------
// StructType is a structure type.
type StructType struct {
// Size in bytes (optional).
Size uint32
// Structure tag.
Tag string
// Structure fields.
Fields []Field
// Struct methods.
Methods []Field
}
// String returns the string representation of the structure type.
func (t *StructType) String() string {
return fmt.Sprintf("struct %s", t.Tag)
}
// Def returns the C syntax representation of the definition of the type.
func (t *StructType) Def() string {
buf := &strings.Builder{}
if t.Size > 0 {
fmt.Fprintf(buf, "// size: 0x%X\n", t.Size)
}
if len(t.Tag) > 0 {
fmt.Fprintf(buf, "struct %s {\n", t.Tag)
} else {
buf.WriteString("struct {\n")
}
for _, field := range t.Fields {
if field.Size > 0 {
fmt.Fprintf(buf, "\t// offset: %04X (%d bytes)\n", field.Offset, field.Size)
} else if len(t.Fields) > 1 && t.Fields[1].Offset > 0 {
fmt.Fprintf(buf, "\t// offset: %04X\n", field.Offset)
}
fmt.Fprintf(buf, "\t%s;\n", field)
}
// TODO: Figure out how to print methods in a good way; for now, commented
// out.
for _, method := range t.Methods {
if method.Size > 0 {
fmt.Fprintf(buf, "\t// offset: %04X (%d bytes)\n", method.Offset, method.Size)
} else if len(t.Fields) > 1 && t.Fields[1].Offset > 0 {
fmt.Fprintf(buf, "\t// offset: %04X\n", method.Offset)
}
fmt.Fprintf(buf, "\t// %s;\n", method)
}
buf.WriteString("}")
return buf.String()
}
// --- [ Union type ] ---------------------------------------------------------
// UnionType is a union type.
type UnionType struct {
// Size in bytes (optional).
Size uint32
// Union tag.
Tag string
// Union fields.
Fields []Field
}
// String returns the string representation of the union type.
func (t *UnionType) String() string {
return fmt.Sprintf("union %s", t.Tag)
}
// Def returns the C syntax representation of the definition of the type.
func (t *UnionType) Def() string {
buf := &strings.Builder{}
if t.Size > 0 {
fmt.Fprintf(buf, "// size: 0x%X\n", t.Size)
}
if len(t.Tag) > 0 {
fmt.Fprintf(buf, "union %s {\n", t.Tag)
} else {
buf.WriteString("union {\n")
}
for _, field := range t.Fields {
if field.Size > 0 {
fmt.Fprintf(buf, "\t// offset: %04X (%d bytes)\n", field.Offset, field.Size)
} else if len(t.Fields) > 1 && t.Fields[1].Offset > 0 {
fmt.Fprintf(buf, "\t// offset: %04X\n", field.Offset)
}
fmt.Fprintf(buf, "\t%s;\n", field)
}
buf.WriteString("}")
return buf.String()
}
// --- [ Enum type ] -----------------------------------------------------------
// EnumType is a enum type.
type EnumType struct {
// Enum tag.
Tag string
// Enum members.
Members []*EnumMember
}
// String returns the string representation of the enum type.
func (t *EnumType) String() string {
return fmt.Sprintf("enum %s", t.Tag)
}
// Def returns the C syntax representation of the definition of the type.
func (t *EnumType) Def() string {
buf := &strings.Builder{}
if len(t.Tag) > 0 {
fmt.Fprintf(buf, "enum %s {\n", t.Tag)
} else {
buf.WriteString("enum {\n")
}
less := func(i, j int) bool {
if t.Members[i].Value == t.Members[j].Value {
return t.Members[i].Name < t.Members[j].Name
}
return t.Members[i].Value < t.Members[j].Value
}
sort.Slice(t.Members, less)
w := tabwriter.NewWriter(buf, 1, 3, 1, ' ', tabwriter.TabIndent)
for _, member := range t.Members {
fmt.Fprintf(w, "\t%s\t= %d,\n", member.Name, member.Value)
}
if err := w.Flush(); err != nil {
panic(fmt.Errorf("unable to flush tabwriter; %v", err))
}
buf.WriteString("}")
return buf.String()
}
// ~~~ [ Enum member ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// EnumMember is an enum member.
type EnumMember struct {
// Enum value.
Value uint32
// Enum name.
Name string
}
// --- [ Pointer type ] --------------------------------------------------------
// PointerType is a pointer type.
type PointerType struct {
// Element type.
Elem Type
}
// String returns the string representation of the pointer type.
func (t *PointerType) String() string {
return fmt.Sprintf("%s*", t.Elem)
}
// Def returns the C syntax representation of the definition of the type.
func (t *PointerType) Def() string {
return t.String()
}
// --- [ Array type ] ----------------------------------------------------------
// ArrayType is an array type.
type ArrayType struct {
// Element type.
Elem Type
// Array length.
Len int
}
// String returns the string representation of the array type.
func (t *ArrayType) String() string {
if t.Len > 0 {
return fmt.Sprintf("%s[%d]", t.Elem, t.Len)
}
return fmt.Sprintf("%s[]", t.Elem)
}
// Def returns the C syntax representation of the definition of the type.
func (t *ArrayType) Def() string {
return t.String()
}
// --- [ Function type ] -------------------------------------------------------
// FuncType is a function type.
type FuncType struct {
// Return type.
RetType Type
// Function parameters.
Params []*VarDecl
// Variadic function.
Variadic bool
}
// String returns the string representation of the function type.
func (t *FuncType) String() string {
// HACK, but works. The syntax of the C type system is pre-historic.
v := Var{Type: t}
return v.String()
}
// Def returns the C syntax representation of the definition of the type.
func (t *FuncType) Def() string {
return t.String()
}
// ### [ Helper types ] ########################################################
// A Field represents a field in a structure type or union type.
type Field struct {
// Offset (optional).
Offset uint32
// Size in bytes (optional).
Size uint32
// Underlying variable.
Var
}
// A Var represents a variable declaration or function parameter.
type Var struct {
// Variable type.
Type Type
// Variable name.
Name string
}
// String returns the string representation of the variable.
func (v Var) String() string {
switch t := v.Type.(type) {
case *PointerType:
// HACK, but works. The syntax of the C type system is pre-historic.
switch t.Elem.(type) {
case *FuncType, *ArrayType:
// Add grouping parenthesis.
v.Name = fmt.Sprintf("(*%s)", v.Name)
default:
v.Name = fmt.Sprintf("*%s", v.Name)
}
v.Type = t.Elem
return v.String()
case *ArrayType:
// HACK, but works. The syntax of the C type system is pre-historic.
if t.Len > 0 {
v.Name = fmt.Sprintf("%s[%d]", v.Name, t.Len)
} else {
v.Name = fmt.Sprintf("%s[]", v.Name)
}
v.Type = t.Elem
return v.String()
case *FuncType:
// HACK, but works. The syntax of the C type system is pre-historic.
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s(", v.Name)
for i, param := range t.Params {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(param.Var.String())
}
if t.Variadic {
if len(t.Params) > 0 {
buf.WriteString(", ")
}
buf.WriteString("...")
}
buf.WriteString(")")
v.Name = buf.String()
v.Type = t.RetType
return v.String()
case *UnionType:
if IsFakeTag(t.Tag) {
return fmt.Sprintf("%s %s", fakeUnionString(t), v.Name)
}
return fmt.Sprintf("%s %s", t, v.Name)
default:
return fmt.Sprintf("%s %s", t, v.Name)
}
}
// fakeUnionString returns the string representation of the given union with a
// fake name.
func fakeUnionString(t *UnionType) string {
buf := &strings.Builder{}
if t.Size > 0 {
fmt.Fprintf(buf, "// size: 0x%X\n", t.Size)
}
buf.WriteString("\tunion {\n")
for _, field := range t.Fields {
if field.Size > 0 {
fmt.Fprintf(buf, "\t\t// offset: %04X (%d bytes)\n", field.Offset, field.Size)
} else if len(t.Fields) > 1 && t.Fields[1].Offset > 0 {
fmt.Fprintf(buf, "\t\t// offset: %04X\n", field.Offset)
}
fmt.Fprintf(buf, "\t\t%s;\n", field)
}
buf.WriteString("\t}")
return buf.String()
}
// IsFakeTag reports whether the tag name is fake (generated by the compiler for
// symbols lacking a tag name).
func IsFakeTag(tag string) bool {
if i := strings.LastIndex(tag, "_duplicate_"); i > 0 {
tag = tag[:i]
}
if strings.HasPrefix(tag, "_") && strings.HasSuffix(tag, "fake") {
s := tag[len("_") : len(tag)-len("fake")]
_, err := strconv.Atoi(s)
return err == nil
}
return false
}