Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1 | package gc |
| 2 | |
| 3 | import ( |
| 4 | "cmd/internal/obj" |
Russ Cox | 53d4123 | 2015-02-23 10:22:26 -0500 | [diff] [blame] | 5 | "os" |
| 6 | "runtime/pprof" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 7 | "strconv" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 11 | func (n *Node) Line() string { |
| 12 | return obj.Linklinefmt(Ctxt, int(n.Lineno), false, false) |
| 13 | } |
| 14 | |
| 15 | func 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 | |
| 21 | func isalnum(c int) bool { |
| 22 | return isalpha(c) || isdigit(c) |
| 23 | } |
| 24 | |
| 25 | func isalpha(c int) bool { |
| 26 | return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
| 27 | } |
| 28 | |
| 29 | func isdigit(c int) bool { |
| 30 | return '0' <= c && c <= '9' |
| 31 | } |
| 32 | |
| 33 | func plan9quote(s string) string { |
| 34 | if s == "" { |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 35 | return "'" + strings.Replace(s, "'", "''", -1) + "'" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 36 | } |
| 37 | for i := 0; i < len(s); i++ { |
| 38 | if s[i] <= ' ' || s[i] == '\'' { |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 39 | return "'" + strings.Replace(s, "'", "''", -1) + "'" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | return s |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // simulation of int(*s++) in C |
| 46 | func 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. |
| 54 | func 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 Cox | 53d4123 | 2015-02-23 10:22:26 -0500 | [diff] [blame] | 63 | |
| 64 | var atExitFuncs []func() |
| 65 | |
| 66 | func AtExit(f func()) { |
| 67 | atExitFuncs = append(atExitFuncs, f) |
| 68 | } |
| 69 | |
| 70 | func 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 | |
| 79 | var cpuprofile string |
| 80 | var memprofile string |
| 81 | |
| 82 | func 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 | } |