...
Run Format

Source file src/cmd/internal/objabi/util.go

Documentation: cmd/internal/objabi

     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 objabi
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  func envOr(key, value string) string {
    15  	if x := os.Getenv(key); x != "" {
    16  		return x
    17  	}
    18  	return value
    19  }
    20  
    21  var (
    22  	defaultGOROOT string // set by linker
    23  
    24  	GOROOT   = envOr("GOROOT", defaultGOROOT)
    25  	GOARCH   = envOr("GOARCH", defaultGOARCH)
    26  	GOOS     = envOr("GOOS", defaultGOOS)
    27  	GO386    = envOr("GO386", defaultGO386)
    28  	GOARM    = goarm()
    29  	GOMIPS   = gomips()
    30  	GOMIPS64 = gomips64()
    31  	Version  = version
    32  )
    33  
    34  func goarm() int {
    35  	switch v := envOr("GOARM", defaultGOARM); v {
    36  	case "5":
    37  		return 5
    38  	case "6":
    39  		return 6
    40  	case "7":
    41  		return 7
    42  	}
    43  	// Fail here, rather than validate at multiple call sites.
    44  	log.Fatalf("Invalid GOARM value. Must be 5, 6, or 7.")
    45  	panic("unreachable")
    46  }
    47  
    48  func gomips() string {
    49  	switch v := envOr("GOMIPS", defaultGOMIPS); v {
    50  	case "hardfloat", "softfloat":
    51  		return v
    52  	}
    53  	log.Fatalf("Invalid GOMIPS value. Must be hardfloat or softfloat.")
    54  	panic("unreachable")
    55  }
    56  
    57  func gomips64() string {
    58  	switch v := envOr("GOMIPS64", defaultGOMIPS64); v {
    59  	case "hardfloat", "softfloat":
    60  		return v
    61  	}
    62  	log.Fatalf("Invalid GOMIPS64 value. Must be hardfloat or softfloat.")
    63  	panic("unreachable")
    64  }
    65  
    66  func Getgoextlinkenabled() string {
    67  	return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED)
    68  }
    69  
    70  func init() {
    71  	for _, f := range strings.Split(goexperiment, ",") {
    72  		if f != "" {
    73  			addexp(f)
    74  		}
    75  	}
    76  }
    77  
    78  func Framepointer_enabled(goos, goarch string) bool {
    79  	return framepointer_enabled != 0 && goarch == "amd64" && goos != "nacl"
    80  }
    81  
    82  func addexp(s string) {
    83  	// Could do general integer parsing here, but the runtime copy doesn't yet.
    84  	v := 1
    85  	name := s
    86  	if len(name) > 2 && name[:2] == "no" {
    87  		v = 0
    88  		name = name[2:]
    89  	}
    90  	for i := 0; i < len(exper); i++ {
    91  		if exper[i].name == name {
    92  			if exper[i].val != nil {
    93  				*exper[i].val = v
    94  			}
    95  			return
    96  		}
    97  	}
    98  
    99  	fmt.Printf("unknown experiment %s\n", s)
   100  	os.Exit(2)
   101  }
   102  
   103  var (
   104  	framepointer_enabled     int = 1
   105  	Fieldtrack_enabled       int
   106  	Preemptibleloops_enabled int
   107  	Clobberdead_enabled      int
   108  	DebugCPU_enabled         int
   109  )
   110  
   111  // Toolchain experiments.
   112  // These are controlled by the GOEXPERIMENT environment
   113  // variable recorded when the toolchain is built.
   114  // This list is also known to cmd/gc.
   115  var exper = []struct {
   116  	name string
   117  	val  *int
   118  }{
   119  	{"fieldtrack", &Fieldtrack_enabled},
   120  	{"framepointer", &framepointer_enabled},
   121  	{"preemptibleloops", &Preemptibleloops_enabled},
   122  	{"clobberdead", &Clobberdead_enabled},
   123  	{"debugcpu", &DebugCPU_enabled},
   124  }
   125  
   126  var defaultExpstring = Expstring()
   127  
   128  func DefaultExpstring() string {
   129  	return defaultExpstring
   130  }
   131  
   132  func Expstring() string {
   133  	buf := "X"
   134  	for i := range exper {
   135  		if *exper[i].val != 0 {
   136  			buf += "," + exper[i].name
   137  		}
   138  	}
   139  	if buf == "X" {
   140  		buf += ",none"
   141  	}
   142  	return "X:" + buf[2:]
   143  }
   144  

View as plain text