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