github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/go/ir/mode.go (about)

     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  	GlobalDebug                                      // Enable debug info for all packages
    30  	SplitAfterNewInformation                         // Split live range after we learn something new about a value
    31  )
    32  
    33  const BuilderModeDoc = `Options controlling the IR builder.
    34  The value is a sequence of zero or more of these symbols:
    35  C	perform sanity [C]hecking of the IR form.
    36  D	include [D]ebug info for every function.
    37  P	print [P]ackage inventory.
    38  F	print [F]unction IR code.
    39  A	print [A]ST nodes responsible for IR instructions
    40  S	log [S]ource locations as IR builder progresses.
    41  N	build [N]aive IR form: don't replace local loads/stores with registers.
    42  I	Split live range after a value is used as slice or array index
    43  `
    44  
    45  func (m BuilderMode) String() string {
    46  	var buf bytes.Buffer
    47  	if m&GlobalDebug != 0 {
    48  		buf.WriteByte('D')
    49  	}
    50  	if m&PrintPackages != 0 {
    51  		buf.WriteByte('P')
    52  	}
    53  	if m&PrintFunctions != 0 {
    54  		buf.WriteByte('F')
    55  	}
    56  	if m&PrintSource != 0 {
    57  		buf.WriteByte('A')
    58  	}
    59  	if m&LogSource != 0 {
    60  		buf.WriteByte('S')
    61  	}
    62  	if m&SanityCheckFunctions != 0 {
    63  		buf.WriteByte('C')
    64  	}
    65  	if m&NaiveForm != 0 {
    66  		buf.WriteByte('N')
    67  	}
    68  	if m&SplitAfterNewInformation != 0 {
    69  		buf.WriteByte('I')
    70  	}
    71  	return buf.String()
    72  }
    73  
    74  // Set parses the flag characters in s and updates *m.
    75  func (m *BuilderMode) Set(s string) error {
    76  	var mode BuilderMode
    77  	for _, c := range s {
    78  		switch c {
    79  		case 'D':
    80  			mode |= GlobalDebug
    81  		case 'P':
    82  			mode |= PrintPackages
    83  		case 'F':
    84  			mode |= PrintFunctions
    85  		case 'A':
    86  			mode |= PrintSource
    87  		case 'S':
    88  			mode |= LogSource
    89  		case 'C':
    90  			mode |= SanityCheckFunctions
    91  		case 'N':
    92  			mode |= NaiveForm
    93  		case 'I':
    94  			mode |= SplitAfterNewInformation
    95  		default:
    96  			return fmt.Errorf("unknown BuilderMode option: %q", c)
    97  		}
    98  	}
    99  	*m = mode
   100  	return nil
   101  }
   102  
   103  // Get returns m.
   104  func (m BuilderMode) Get() interface{} { return m }