github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/cmd/internal/gc/util.go (about) 1 package gc 2 3 import ( 4 "cmd/internal/obj" 5 "os" 6 "runtime/pprof" 7 "strconv" 8 "strings" 9 ) 10 11 func bool2int(b bool) int { 12 if b { 13 return 1 14 } 15 return 0 16 } 17 18 func (n *Node) Line() string { 19 return obj.Linklinefmt(Ctxt, int(n.Lineno), false, false) 20 } 21 22 func atoi(s string) int { 23 // NOTE: Not strconv.Atoi, accepts hex and octal prefixes. 24 n, _ := strconv.ParseInt(s, 0, 0) 25 return int(n) 26 } 27 28 func isalnum(c int) bool { 29 return isalpha(c) || isdigit(c) 30 } 31 32 func isalpha(c int) bool { 33 return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' 34 } 35 36 func isdigit(c int) bool { 37 return '0' <= c && c <= '9' 38 } 39 40 func plan9quote(s string) string { 41 if s == "" { 42 return "'" + strings.Replace(s, "'", "''", -1) + "'" 43 } 44 for i := 0; i < len(s); i++ { 45 if s[i] <= ' ' || s[i] == '\'' { 46 return "'" + strings.Replace(s, "'", "''", -1) + "'" 47 } 48 } 49 return s 50 } 51 52 // simulation of int(*s++) in C 53 func intstarstringplusplus(s string) (int, string) { 54 if s == "" { 55 return 0, "" 56 } 57 return int(s[0]), s[1:] 58 } 59 60 // strings.Compare, introduced in Go 1.5. 61 func stringsCompare(a, b string) int { 62 if a == b { 63 return 0 64 } 65 if a < b { 66 return -1 67 } 68 return +1 69 } 70 71 var atExitFuncs []func() 72 73 func AtExit(f func()) { 74 atExitFuncs = append(atExitFuncs, f) 75 } 76 77 func Exit(code int) { 78 for i := len(atExitFuncs) - 1; i >= 0; i-- { 79 f := atExitFuncs[i] 80 atExitFuncs = atExitFuncs[:i] 81 f() 82 } 83 os.Exit(code) 84 } 85 86 var cpuprofile string 87 var memprofile string 88 89 func startProfile() { 90 if cpuprofile != "" { 91 f, err := os.Create(cpuprofile) 92 if err != nil { 93 Fatal("%v", err) 94 } 95 if err := pprof.StartCPUProfile(f); err != nil { 96 Fatal("%v", err) 97 } 98 AtExit(pprof.StopCPUProfile) 99 } 100 if memprofile != "" { 101 f, err := os.Create(memprofile) 102 if err != nil { 103 Fatal("%v", err) 104 } 105 AtExit(func() { 106 if err := pprof.WriteHeapProfile(f); err != nil { 107 Fatal("%v", err) 108 } 109 }) 110 } 111 }