]> Cypherpunks.ru repositories - gostls13.git/blob - src/internal/buildcfg/exp.go
runtime: enable allocheaders by default
[gostls13.git] / src / internal / buildcfg / exp.go
1 // Copyright 2021 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.
4
5 package buildcfg
6
7 import (
8         "fmt"
9         "reflect"
10         "strings"
11
12         "internal/goexperiment"
13 )
14
15 // ExperimentFlags represents a set of GOEXPERIMENT flags relative to a baseline
16 // (platform-default) experiment configuration.
17 type ExperimentFlags struct {
18         goexperiment.Flags
19         baseline goexperiment.Flags
20 }
21
22 // Experiment contains the toolchain experiments enabled for the
23 // current build.
24 //
25 // (This is not necessarily the set of experiments the compiler itself
26 // was built with.)
27 //
28 // experimentBaseline specifies the experiment flags that are enabled by
29 // default in the current toolchain. This is, in effect, the "control"
30 // configuration and any variation from this is an experiment.
31 var Experiment ExperimentFlags = func() ExperimentFlags {
32         flags, err := ParseGOEXPERIMENT(GOOS, GOARCH, envOr("GOEXPERIMENT", defaultGOEXPERIMENT))
33         if err != nil {
34                 Error = err
35                 return ExperimentFlags{}
36         }
37         return *flags
38 }()
39
40 // DefaultGOEXPERIMENT is the embedded default GOEXPERIMENT string.
41 // It is not guaranteed to be canonical.
42 const DefaultGOEXPERIMENT = defaultGOEXPERIMENT
43
44 // FramePointerEnabled enables the use of platform conventions for
45 // saving frame pointers.
46 //
47 // This used to be an experiment, but now it's always enabled on
48 // platforms that support it.
49 //
50 // Note: must agree with runtime.framepointer_enabled.
51 var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64"
52
53 // ParseGOEXPERIMENT parses a (GOOS, GOARCH, GOEXPERIMENT)
54 // configuration tuple and returns the enabled and baseline experiment
55 // flag sets.
56 //
57 // TODO(mdempsky): Move to internal/goexperiment.
58 func ParseGOEXPERIMENT(goos, goarch, goexp string) (*ExperimentFlags, error) {
59         // regabiSupported is set to true on platforms where register ABI is
60         // supported and enabled by default.
61         // regabiAlwaysOn is set to true on platforms where register ABI is
62         // always on.
63         var regabiSupported, regabiAlwaysOn bool
64         switch goarch {
65         case "amd64", "arm64", "ppc64le", "ppc64", "riscv64":
66                 regabiAlwaysOn = true
67                 regabiSupported = true
68         }
69
70         baseline := goexperiment.Flags{
71                 RegabiWrappers:   regabiSupported,
72                 RegabiArgs:       regabiSupported,
73                 CoverageRedesign: true,
74                 AllocHeaders:     true,
75         }
76
77         // Start with the statically enabled set of experiments.
78         flags := &ExperimentFlags{
79                 Flags:    baseline,
80                 baseline: baseline,
81         }
82
83         // Pick up any changes to the baseline configuration from the
84         // GOEXPERIMENT environment. This can be set at make.bash time
85         // and overridden at build time.
86         if goexp != "" {
87                 // Create a map of known experiment names.
88                 names := make(map[string]func(bool))
89                 rv := reflect.ValueOf(&flags.Flags).Elem()
90                 rt := rv.Type()
91                 for i := 0; i < rt.NumField(); i++ {
92                         field := rv.Field(i)
93                         names[strings.ToLower(rt.Field(i).Name)] = field.SetBool
94                 }
95
96                 // "regabi" is an alias for all working regabi
97                 // subexperiments, and not an experiment itself. Doing
98                 // this as an alias make both "regabi" and "noregabi"
99                 // do the right thing.
100                 names["regabi"] = func(v bool) {
101                         flags.RegabiWrappers = v
102                         flags.RegabiArgs = v
103                 }
104
105                 // Parse names.
106                 for _, f := range strings.Split(goexp, ",") {
107                         if f == "" {
108                                 continue
109                         }
110                         if f == "none" {
111                                 // GOEXPERIMENT=none disables all experiment flags.
112                                 // This is used by cmd/dist, which doesn't know how
113                                 // to build with any experiment flags.
114                                 flags.Flags = goexperiment.Flags{}
115                                 continue
116                         }
117                         val := true
118                         if strings.HasPrefix(f, "no") {
119                                 f, val = f[2:], false
120                         }
121                         set, ok := names[f]
122                         if !ok {
123                                 return nil, fmt.Errorf("unknown GOEXPERIMENT %s", f)
124                         }
125                         set(val)
126                 }
127         }
128
129         if regabiAlwaysOn {
130                 flags.RegabiWrappers = true
131                 flags.RegabiArgs = true
132         }
133         // regabi is only supported on amd64, arm64, riscv64, ppc64 and ppc64le.
134         if !regabiSupported {
135                 flags.RegabiWrappers = false
136                 flags.RegabiArgs = false
137         }
138         // Check regabi dependencies.
139         if flags.RegabiArgs && !flags.RegabiWrappers {
140                 return nil, fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers")
141         }
142         return flags, nil
143 }
144
145 // String returns the canonical GOEXPERIMENT string to enable this experiment
146 // configuration. (Experiments in the same state as in the baseline are elided.)
147 func (exp *ExperimentFlags) String() string {
148         return strings.Join(expList(&exp.Flags, &exp.baseline, false), ",")
149 }
150
151 // expList returns the list of lower-cased experiment names for
152 // experiments that differ from base. base may be nil to indicate no
153 // experiments. If all is true, then include all experiment flags,
154 // regardless of base.
155 func expList(exp, base *goexperiment.Flags, all bool) []string {
156         var list []string
157         rv := reflect.ValueOf(exp).Elem()
158         var rBase reflect.Value
159         if base != nil {
160                 rBase = reflect.ValueOf(base).Elem()
161         }
162         rt := rv.Type()
163         for i := 0; i < rt.NumField(); i++ {
164                 name := strings.ToLower(rt.Field(i).Name)
165                 val := rv.Field(i).Bool()
166                 baseVal := false
167                 if base != nil {
168                         baseVal = rBase.Field(i).Bool()
169                 }
170                 if all || val != baseVal {
171                         if val {
172                                 list = append(list, name)
173                         } else {
174                                 list = append(list, "no"+name)
175                         }
176                 }
177         }
178         return list
179 }
180
181 // Enabled returns a list of enabled experiments, as
182 // lower-cased experiment names.
183 func (exp *ExperimentFlags) Enabled() []string {
184         return expList(&exp.Flags, nil, false)
185 }
186
187 // All returns a list of all experiment settings.
188 // Disabled experiments appear in the list prefixed by "no".
189 func (exp *ExperimentFlags) All() []string {
190         return expList(&exp.Flags, nil, true)
191 }