forked from arl/bitfield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitfield.go
388 lines (345 loc) · 8.81 KB
/
bitfield.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io"
"os"
"slices"
"strconv"
"strings"
"unicode"
)
type config struct {
in, out string
tname string
pkgname string
}
func parseFlags(args []string) (*config, string, error) {
var cfg config
flags := flag.NewFlagSet("bitfield", flag.ContinueOnError)
var buf bytes.Buffer
flags.SetOutput(&buf)
flags.StringVar(&cfg.in, "in", "", "INPUT file name (necessary unless within a go:generate comment)")
flags.StringVar(&cfg.out, "out", "", "output file name (defaults to standard output)")
flags.StringVar(&cfg.tname, "type", "all", "name of the type to convert (defaults to all structs)")
flags.StringVar(&cfg.pkgname, "pkg", "", "package name (defaults to INPUT file package)")
if err := flags.Parse(args); err != nil {
return nil, buf.String(), err
}
return &cfg, buf.String(), nil
}
func main() {
cfg, output, err := parseFlags(os.Args[1:])
if err == flag.ErrHelp {
fmt.Println(output)
os.Exit(2)
} else if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
if goFile := os.Getenv("GOFILE"); cfg.in == "" && goFile != "" {
cfg.in = goFile
}
if err := run(cfg); err != nil {
fmt.Fprintf(os.Stderr, "bitfield, fatal error:\n")
fmt.Fprintf(os.Stderr, "\t%s\n", err)
os.Exit(1)
}
}
type structInfo struct {
name string
width uint8 // type width in bits
unions map[string]*union
unionOrder []string // unions in file order
}
func newStructInfo(name string) *structInfo {
return &structInfo{
name: name,
unions: make(map[string]*union),
}
}
func (si structInfo) receiver() string {
name := []rune(si.name)
if unicode.IsUpper(name[0]) {
return string(unicode.ToLower(name[0]))
}
if len(name) == 1 {
if si.name[0] != 's' {
return "s"
}
return "x"
}
return si.name[:1]
}
func (si *structInfo) union(name string) *union {
if u, ok := si.unions[name]; ok {
return u
}
si.unionOrder = append(si.unionOrder, name)
var u union
si.unions[name] = &u
return &u
}
type union struct {
fields []fieldInfo
bits int // bits actually used
}
type fieldInfo struct {
name string
mask uint64
offset int
typ string // org field type
}
func (fi fieldInfo) getter() string {
return fi.name
}
func (fi fieldInfo) setter() string {
name := []rune(fi.name)
if unicode.IsUpper(name[0]) {
return "Set" + fi.name
}
return "set" + string(unicode.ToUpper(name[0])) + string(name[1:])
}
// returns the type bit-width and a boolean indicating if we support it.
func typeWidth(tname string) (int, bool) {
switch tname {
case "bool":
return 1, true
case "uint8":
return 8, true
case "uint16":
return 16, true
case "uint32":
return 32, true
case "uint64":
return 64, true
}
return 0, false
}
func nextpow2(n uint8) uint8 {
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n++
return n
}
func run(cfg *config) error {
if cfg.in == "" {
return fmt.Errorf("input file must be provided")
}
var out io.Writer = os.Stdout
if cfg.out != "" {
f, err := os.Create(cfg.out)
if err != nil {
return fmt.Errorf("output file: %s", err)
}
defer f.Close()
out = f
}
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, cfg.in, nil, parser.ParseComments)
if err != nil {
return fmt.Errorf("failed to parse input file: %s", err)
}
var (
structs []*structInfo
tErr error
)
ast.Inspect(node, func(n ast.Node) bool {
t, ok := n.(*ast.TypeSpec)
if !ok {
return true
}
tname := t.Name.Name
s, ok := t.Type.(*ast.StructType)
if !ok {
if cfg.tname != "all" && tname == cfg.tname {
tErr = fmt.Errorf("type %s is not a struct", cfg.tname)
}
return false
}
if tname != cfg.tname && cfg.tname != "all" {
return true
}
offsets := make(map[string]int)
structInfo := newStructInfo(tname)
for _, field := range s.Fields.List {
if field.Tag == nil {
continue
}
tags := strings.Fields(strings.Trim(field.Tag.Value, "`"))
union := "default"
for _, tag := range tags {
if !strings.HasPrefix(tag, "bitfield:") {
continue
}
fieldName := field.Names[0].Name
if !strings.HasPrefix(tag[9:], `"`) || !strings.HasSuffix(tag, `"`) {
tErr = fmt.Errorf("field '%s' has a malformed struct tag", fieldName)
return false
}
kvs := strings.Split(strings.Trim(tag[9:], `"`), ",")
bits := 0
for _, tag := range kvs {
k, v, ok := strings.Cut(tag, "=")
if !ok {
if bits != 0 {
tErr = fmt.Errorf("field '%s' has a malformed struct tag", fieldName)
return false
}
k = "bits"
v = tag
}
switch k {
case "bits":
ibits, err := strconv.Atoi(v)
if err != nil {
tErr = fmt.Errorf("failed to parse bit count for field '%s'", fieldName)
return false
}
if ibits <= 0 || ibits > 64 {
tErr = fmt.Errorf("field '%s' has an invalid bit count (%d), must be (0, 64]", fieldName, ibits)
return false
}
bits = ibits
case "union":
union = v
}
}
tname := field.Type.(*ast.Ident).Name
twidth, ok := typeWidth(tname)
if !ok {
tErr = fmt.Errorf("field '%s' has an unsupported type %s", fieldName, tname)
return false
}
switch {
case bits == 0:
tErr = fmt.Errorf("missing bit count for field '%s': %s", fieldName, kvs)
return false
case twidth < bits:
tErr = fmt.Errorf("field '%s' can't represent %d bits with type %s", fieldName, bits, tname)
return false
}
if fieldName != "_" {
u := structInfo.union(union)
off := offsets[union]
mask := uint64(1<<uint64(bits) - 1)
if tname == "bool" {
mask = 1 << uint64(off)
}
u.fields = append(u.fields, fieldInfo{
name: fieldName,
offset: off,
mask: mask,
typ: tname,
})
}
offsets[union] += bits
}
}
for n, u := range structInfo.unions {
u.bits = offsets[n]
structInfo.width = max(structInfo.width, uint8(offsets[n]))
}
structInfo.width = nextpow2(structInfo.width)
structs = append(structs, structInfo)
return true
})
if tErr != nil {
// Return the error set during the AST traversal.
return tErr
}
if cfg.tname != "all" && len(structs) == 0 {
return fmt.Errorf("struct %s not found", cfg.tname)
}
if !slices.ContainsFunc(structs, func(si *structInfo) bool {
return len(si.unions) > 0
}) {
return fmt.Errorf("nothing to generate")
}
// Generate the file.
if cfg.pkgname == "" {
cfg.pkgname = node.Name.Name
}
var g generator
g.printf("package %s\n\n", cfg.pkgname)
g.printf("// Code generated by github.com/arl/bitfield. DO NOT EDIT.\n")
for _, si := range structs {
if len(si.unions) == 0 {
// skip structs without any fields
continue
}
g.printf(`type %s uint%d`, si.name, si.width)
for _, uname := range si.unionOrder {
union := si.unions[uname]
// Define the final type
if union.bits > 64 {
if uname == "default" {
return fmt.Errorf("struct '%s' has too many bits (%d)", si.name, union.bits)
}
return fmt.Errorf("struct '%s' has too many bits in union '%s' (%d)", si.name, uname, union.bits)
}
for _, fi := range union.fields {
// Getter
g.printf(`func (%s %s) %s() %s {`, si.receiver(), si.name, fi.getter(), fi.typ)
switch {
case fi.typ == "bool":
g.printf(` return %s&0x%x != 0`, si.receiver(), fi.mask)
case fi.offset > 0:
g.printf(` return %s((%s >> %d) & 0x%x)`, fi.typ, si.receiver(), fi.offset, fi.mask)
default:
g.printf(` return %s(%s & 0x%x)`, fi.typ, si.receiver(), fi.mask)
}
g.printf(`}`)
g.printf(``)
// Setter
g.printf(`func (%s *%s) %s(val %s) {`, si.receiver(), si.name, fi.setter(), fi.typ)
switch {
case fi.typ == "bool":
// The generated assembly doesn't branch.
g.printf(` var ival %s`, si.name)
g.printf(` if val {`)
g.printf(` ival = 1`)
g.printf(` }`)
g.printf(` *%s &^= 0x%x`, si.receiver(), fi.mask)
if fi.offset == 0 {
g.printf(` *%s |= ival`, si.receiver())
} else {
g.printf(` *%s |= ival<<%d`, si.receiver(), fi.offset)
}
case fi.offset == 0:
g.printf(` *%s &^= 0x%x`, si.receiver(), fi.mask)
g.printf(` *%s |= %s(val&0x%x)`, si.receiver(), si.name, fi.mask)
default:
g.printf(` *%s &^= 0x%x<<%d`, si.receiver(), fi.mask, fi.offset)
g.printf(` *%s |= %s(val&0x%x)<<%d`, si.receiver(), si.name, fi.mask, fi.offset)
}
g.printf(`}`)
g.printf(``)
}
}
}
return g.format(out)
}
type generator struct {
buf bytes.Buffer
}
func (g *generator) printf(format string, args ...any) {
fmt.Fprintf(&g.buf, format+"\n", args...)
}
func (g *generator) format(w io.Writer) error {
buf, err := format.Source(g.buf.Bytes())
if err != nil {
return fmt.Errorf("go format failed: %s", err)
}
if _, err := w.Write(buf); err != nil {
return fmt.Errorf("write failed: %s", err)
}
return nil
}