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