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