1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 // Generate builtinlist.go from cmd/compile/internal/typecheck/builtin/runtime.go.
28 var stdout = flag.Bool("stdout", false, "write to stdout instead of builtinlist.go")
34 fmt.Fprintln(&b, "// Code generated by mkbuiltin.go. DO NOT EDIT.")
36 fmt.Fprintln(&b, "package goobj")
40 out, err := format.Source(b.Bytes())
45 _, err = os.Stdout.Write(out)
47 err = ioutil.WriteFile("builtinlist.go", out, 0666)
54 func mkbuiltin(w io.Writer) {
56 fset := token.NewFileSet()
57 path := filepath.Join("..", "..", "compile", "internal", "typecheck", "builtin", "runtime.go")
58 f, err := parser.ParseFile(fset, path, nil, 0)
63 decls := make(map[string]bool)
65 fmt.Fprintf(w, "var builtins = [...]struct{ name string; abi int }{\n")
66 for _, decl := range f.Decls {
67 switch decl := decl.(type) {
70 log.Fatal("methods unsupported")
73 log.Fatal("unexpected function body")
75 declName := pkg + "." + decl.Name.Name
76 decls[declName] = true
77 fmt.Fprintf(w, "{%q, 1},\n", declName) // functions are ABIInternal (1)
79 if decl.Tok == token.IMPORT {
82 if decl.Tok != token.VAR {
83 log.Fatal("unhandled declaration kind", decl.Tok)
85 for _, spec := range decl.Specs {
86 spec := spec.(*ast.ValueSpec)
87 if len(spec.Values) != 0 {
88 log.Fatal("unexpected values")
90 for _, name := range spec.Names {
91 declName := pkg + "." + name.Name
92 decls[declName] = true
93 fmt.Fprintf(w, "{%q, 0},\n", declName) // variables are ABI0
97 log.Fatal("unhandled decl type", decl)
101 // The list above only contains ones that are used by the frontend.
102 // The backend may create more references of builtin functions.
103 // We also want to include predefined types.
105 extras := append(fextras[:], enumerateBasicTypes()...)
106 for _, b := range extras {
108 if !strings.HasPrefix(b.name, "type.") {
111 name := prefix + b.name
113 log.Fatalf("%q already added -- mkbuiltin.go out of sync?", name)
115 fmt.Fprintf(w, "{%q, %d},\n", name, b.abi)
120 // addBasicTypes returns the symbol names for basic types that are
121 // defined in the runtime and referenced in other packages.
122 // Needs to be kept in sync with reflect.go:WriteBasicTypes() and
123 // reflect.go:writeType() in the compiler.
124 func enumerateBasicTypes() []extra {
125 names := [...]string{
126 "int8", "uint8", "int16", "uint16",
127 "int32", "uint32", "int64", "uint64",
128 "float32", "float64", "complex64", "complex128",
129 "unsafe.Pointer", "uintptr", "bool", "string", "error",
130 "func(error) string"}
132 for _, n := range names {
133 result = append(result, extra{"type." + n, 0})
134 result = append(result, extra{"type.*" + n, 0})
144 var fextras = [...]extra{
145 // compiler frontend inserted calls (sysfunc)
147 {"deferprocStack", 1},
150 {"panicoverflow", 1},
153 // compiler backend inserted calls
154 {"gcWriteBarrier", 1},
158 // assembler backend inserted calls
159 {"morestack", 0}, // asm function, ABI0
160 {"morestackc", 0}, // asm function, ABI0
161 {"morestack_noctxt", 0}, // asm function, ABI0