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
5package ssa
6
7// This file defines the BuilderMode type and its command-line flag.
8
9import (
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 = ssa.BuilderMode(0)
19// 	func init() { flag.Var(&mode, "build", ssa.BuilderModeDoc) }
20//
21type BuilderMode uint
22
23const (
24	PrintPackages        BuilderMode = 1 << iota // Print package inventory to stdout
25	PrintFunctions                               // Print function SSA code to stdout
26	LogSource                                    // Log source locations as SSA builder progresses
27	SanityCheckFunctions                         // Perform sanity checking of function bodies
28	NaiveForm                                    // Build naïve SSA 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)
33
34const BuilderModeDoc = `Options controlling the SSA builder.
35The value is a sequence of zero or more of these letters:
36C	perform sanity [C]hecking of the SSA form.
37D	include [D]ebug info for every function.
38P	print [P]ackage inventory.
39F	print [F]unction SSA code.
40S	log [S]ource locations as SSA builder progresses.
41L	build distinct packages seria[L]ly instead of in parallel.
42N	build [N]aive SSA form: don't replace local loads/stores with registers.
43I	build bare [I]nit functions: no init guards or calls to dependent inits.
44`
45
46func (m BuilderMode) String() string {
47	var buf bytes.Buffer
48	if m&GlobalDebug != 0 {
49		buf.WriteByte('D')
50	}
51	if m&PrintPackages != 0 {
52		buf.WriteByte('P')
53	}
54	if m&PrintFunctions != 0 {
55		buf.WriteByte('F')
56	}
57	if m&LogSource != 0 {
58		buf.WriteByte('S')
59	}
60	if m&SanityCheckFunctions != 0 {
61		buf.WriteByte('C')
62	}
63	if m&NaiveForm != 0 {
64		buf.WriteByte('N')
65	}
66	if m&BuildSerially != 0 {
67		buf.WriteByte('L')
68	}
69	if m&BareInits != 0 {
70		buf.WriteByte('I')
71	}
72	return buf.String()
73}
74
75// Set parses the flag characters in s and updates *m.
76func (m *BuilderMode) Set(s string) error {
77	var mode BuilderMode
78	for _, c := range s {
79		switch c {
80		case 'D':
81			mode |= GlobalDebug
82		case 'P':
83			mode |= PrintPackages
84		case 'F':
85			mode |= PrintFunctions
86		case 'S':
87			mode |= LogSource | BuildSerially
88		case 'C':
89			mode |= SanityCheckFunctions
90		case 'N':
91			mode |= NaiveForm
92		case 'L':
93			mode |= BuildSerially
94		case 'I':
95			mode |= BareInits
96		default:
97			return fmt.Errorf("unknown BuilderMode option: %q", c)
98		}
99	}
100	*m = mode
101	return nil
102}
103
104// Get returns m.
105func (m BuilderMode) Get() interface{} { return m }
106