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