-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
183 lines (153 loc) · 4.26 KB
/
generate.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
// To run this program: go generate
//
// (the go:generate line is in gen.go)
//
//go:build endiangen && !go1.17
// +build endiangen,!go1.17
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"runtime"
"sort"
"strconv"
"strings"
"github.com/dolmen-go/codegen"
)
// parseArch extracts the "BigEndian" const from an arch definition source
// $GOROOT/src/runtime/internal/sys
func parseArch(filename string) (bool, error) {
fs := token.NewFileSet()
fileAST, err := parser.ParseFile(fs, filename, nil, parser.Mode(0))
//fileAST, err := parser.ParseFile(fs, filename, nil, parser.Trace)
if err != nil {
return false, err
}
if len(fileAST.Decls) == 0 {
return false, fmt.Errorf("%s: no Decls in AST", filename)
}
decl, ok := fileAST.Decls[0].(*ast.GenDecl)
if !ok || decl.Tok != token.CONST {
return false, fmt.Errorf("%s: CONST expected at Decls[0]", filename)
}
for _, rawSpec := range decl.Specs {
spec := rawSpec.(*ast.ValueSpec)
if len(spec.Names) != 1 || spec.Names[0].Name != "BigEndian" {
continue
}
// We found the const "BigEndian"
// Let's extract its value!
if len(spec.Values) != 1 {
return false, fmt.Errorf("%s: single value expected for const BigEndian", filename)
}
switch valueExpr := spec.Values[0].(type) {
// !go1.10
case *ast.BasicLit:
if valueExpr.Kind != token.INT {
return false, fmt.Errorf("%s: INT value expected for const BigEndian; got %s", filename, valueExpr.Kind)
}
intValue, _ := strconv.ParseInt(valueExpr.Value, 0, 64)
if intValue < 0 || intValue > 1 {
return false, fmt.Errorf("%s: value 0/1 expected for const BigEndian", filename)
}
return intValue == 1, nil
// go1.10 https://go-review.googlesource.com/c/go/+/73270
case *ast.Ident:
switch valueExpr.Name {
case "true":
return true, nil
case "false":
return false, nil
}
return false, fmt.Errorf("%s: BOOL value expected for const BigEndian; got %q", filename, valueExpr.Name)
default:
return false, fmt.Errorf("%s: unexpected value type for const BigEndian; got %T", filename, spec.Values[0])
}
}
return true, fmt.Errorf("%s: const BigEndian not found", filename)
}
func main() {
log.SetPrefix("")
log.SetFlags(0)
var srcDir = runtime.GOROOT() + "/src/runtime/internal/sys"
dir, err := os.Open(srcDir)
if err != nil {
log.Fatal(err)
}
defer dir.Close()
files, err := dir.Readdirnames(0)
if err != nil {
log.Fatal(err)
}
knownArchs := []string{"amd64p32"}
archsByEndian := map[bool][]string{
false: { // little
knownArchs[0],
},
}
for _, f := range files {
if !strings.HasPrefix(f, "arch_") || !strings.HasSuffix(f, ".go") {
continue
}
arch := f[5 : len(f)-3]
//fmt.Println(arch)
big, err := parseArch(srcDir + "/" + f)
if err != nil {
log.Println(err)
continue
}
archsByEndian[big] = append(archsByEndian[big], arch)
knownArchs = append(knownArchs, arch)
}
//fmt.Printf("%#v\n", archByEndian)
const template = `// Code generated by generate.go; DO NOT EDIT.
{{/**/}}// +build {{- range .Tags}} {{.}}{{end}}
{{/**/}}// +build !generate
package endian
import "encoding/binary"
// Native is the byte order of GOARCH.
var Native = binary.{{if .Big}}Big{{else}}Little{{end}}Endian
`
tmplArch := codegen.MustParse(template)
bigStr := map[bool]string{true: "Big", false: "Little"}
for big, tags := range archsByEndian {
sort.Strings(tags)
err = tmplArch.CreateFile(
strings.ToLower(bigStr[big])+".go",
map[string]interface{}{
"Tags": tags,
"Big": big,
},
)
}
const templateOthers = `// Code generated by generate.go; DO NOT EDIT.
{{/**/}}// +build !generate,{{range .}}!{{.}},{{end}}!generate
package endian
import (
"encoding/binary"
"unsafe"
)
// Native is the byte order of GOARCH.
// It will be determined at runtime because it was unknown at code
// generation time.
var Native binary.ByteOrder
func init() {
// http://grokbase.com/t/gg/golang-nuts/129jhmdb3d/go-nuts-how-to-tell-endian-ness-of-machine#20120918nttlyywfpl7ughnsys6pm4pgpe
var x int32 = 0x01020304
switch *(*byte)(unsafe.Pointer(&x)) {
case 1:
Native = binary.BigEndian
case 4:
Native = binary.LittleEndian
}
}
`
sort.Strings(knownArchs)
if err = codegen.MustParse(templateOthers).CreateFile("others.go", knownArchs); err != nil {
log.Fatal(err)
}
}