Debian Codegraph @

go/ir/mode.go

raw
1 // Copyright 2015 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 ir
6
7 // This file defines the BuilderMode type and its command-line flag.
8
9 import (
10 "bytes"
11 "fmt"
12 )
13
14 // BuilderMode is a bitmask of options for diagnostics and checking.
15 //
16 // *BuilderMode satisfies the flag.Value interface. Example:
17 //
18 // var mode = ir.BuilderMode(0)
19 // func init() { flag.Var(&mode, "build", ir.BuilderModeDoc) }
20 type BuilderMode uint
21
22 const (
23 PrintPackages BuilderMode = 1 << iota // Print package inventory to stdout
24 PrintFunctions // Print function IR code to stdout
25 PrintSource // Print source code when printing function IR
26 LogSource // Log source locations as IR builder progresses
27 SanityCheckFunctions // Perform sanity checking of function bodies
28 NaiveForm // Build naïve IR form: don't replace local loads/stores with registers
29 BuildSerially // Build packages serially, not in parallel.
30 GlobalDebug // Enable debug info for all packages
31 BareInits // Build init functions without guards or calls to dependent inits
32 InstantiateGenerics // Instantiate generics functions (monomorphize) while building
33 )
34
35 const BuilderModeDoc = `Options controlling the IR builder.
36 The value is a sequence of zero or more of these letters:
37 C perform sanity [C]hecking of the IR form.
38 D include [D]ebug info for every function.
39 P print [P]ackage inventory.
40 F print [F]unction IR code.
41 A print [A]ST nodes responsible for IR instructions
42 S log [S]ource locations as IR builder progresses.
43 L build distinct packages seria[L]ly instead of in parallel.
44 N build [N]aive IR form: don't replace local loads/stores with registers.
45 I build bare [I]nit functions: no init guards or calls to dependent inits.
46 G instantiate [G]eneric function bodies via monomorphization
47 `
48
49 func (m BuilderMode) String() string {
50 var buf bytes.Buffer
51 if m&GlobalDebug != 0 {
52 buf.WriteByte('D')
53 }
54 if m&PrintPackages != 0 {
55 buf.WriteByte('P')
56 }
57 if m&PrintFunctions != 0 {
58 buf.WriteByte('F')
59 }
60 if m&PrintSource != 0 {
61 buf.WriteByte('A')
62 }
63 if m&LogSource != 0 {
64 buf.WriteByte('S')
65 }
66 if m&SanityCheckFunctions != 0 {
67 buf.WriteByte('C')
68 }
69 if m&NaiveForm != 0 {
70 buf.WriteByte('N')
71 }
72 if m&BuildSerially != 0 {
73 buf.WriteByte('L')
74 }
75 if m&BareInits != 0 {
76 buf.WriteByte('I')
77 }
78 if m&InstantiateGenerics != 0 {
79 buf.WriteByte('G')
80 }
81 return buf.String()
82 }
83
84 // Set parses the flag characters in s and updates *m.
85 func (m *BuilderMode) Set(s string) error {
86 var mode BuilderMode
87 for _, c := range s {
88 switch c {
89 case 'D':
90 mode |= GlobalDebug
91 case 'P':
92 mode |= PrintPackages
93 case 'F':
94 mode |= PrintFunctions
95 case 'A':
96 mode |= PrintSource
97 case 'S':
98 mode |= LogSource | BuildSerially
99 case 'C':
100 mode |= SanityCheckFunctions
101 case 'N':
102 mode |= NaiveForm
103 case 'L':
104 mode |= BuildSerially
105 case 'I':
106 mode |= BareInits
107 case 'G':
108 mode |= InstantiateGenerics
109 default:
110 return fmt.Errorf("unknown BuilderMode option: %q", c)
111 }
112 }
113 *m = mode
114 return nil
115 }
116
117 // Get returns m.
118 func (m BuilderMode) Get() any { return m }