2015-02-13 14:40:36 -05:00
|
|
|
package gc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"cmd/internal/obj"
|
2015-02-23 10:22:26 -05:00
|
|
|
"os"
|
2015-04-22 17:53:32 -07:00
|
|
|
"runtime"
|
2015-02-23 10:22:26 -05:00
|
|
|
"runtime/pprof"
|
2015-02-13 14:40:36 -05:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (n *Node) Line() string {
|
|
|
|
|
return obj.Linklinefmt(Ctxt, int(n.Lineno), false, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func atoi(s string) int {
|
|
|
|
|
// NOTE: Not strconv.Atoi, accepts hex and octal prefixes.
|
|
|
|
|
n, _ := strconv.ParseInt(s, 0, 0)
|
|
|
|
|
return int(n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isalnum(c int) bool {
|
|
|
|
|
return isalpha(c) || isdigit(c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isalpha(c int) bool {
|
|
|
|
|
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isdigit(c int) bool {
|
|
|
|
|
return '0' <= c && c <= '9'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func plan9quote(s string) string {
|
|
|
|
|
if s == "" {
|
2015-03-02 12:35:15 -05:00
|
|
|
return "'" + strings.Replace(s, "'", "''", -1) + "'"
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
|
if s[i] <= ' ' || s[i] == '\'' {
|
2015-03-02 12:35:15 -05:00
|
|
|
return "'" + strings.Replace(s, "'", "''", -1) + "'"
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// strings.Compare, introduced in Go 1.5.
|
|
|
|
|
func stringsCompare(a, b string) int {
|
|
|
|
|
if a == b {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
if a < b {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
return +1
|
|
|
|
|
}
|
2015-02-23 10:22:26 -05:00
|
|
|
|
|
|
|
|
var atExitFuncs []func()
|
|
|
|
|
|
|
|
|
|
func AtExit(f func()) {
|
|
|
|
|
atExitFuncs = append(atExitFuncs, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Exit(code int) {
|
|
|
|
|
for i := len(atExitFuncs) - 1; i >= 0; i-- {
|
|
|
|
|
f := atExitFuncs[i]
|
|
|
|
|
atExitFuncs = atExitFuncs[:i]
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
os.Exit(code)
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-22 17:53:32 -07:00
|
|
|
var (
|
|
|
|
|
cpuprofile string
|
|
|
|
|
memprofile string
|
|
|
|
|
memprofilerate int64
|
|
|
|
|
)
|
2015-02-23 10:22:26 -05:00
|
|
|
|
|
|
|
|
func startProfile() {
|
|
|
|
|
if cpuprofile != "" {
|
|
|
|
|
f, err := os.Create(cpuprofile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
Fatal("%v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
|
|
|
|
Fatal("%v", err)
|
|
|
|
|
}
|
|
|
|
|
AtExit(pprof.StopCPUProfile)
|
|
|
|
|
}
|
|
|
|
|
if memprofile != "" {
|
2015-04-22 17:53:32 -07:00
|
|
|
if memprofilerate != 0 {
|
|
|
|
|
runtime.MemProfileRate = int(memprofilerate)
|
|
|
|
|
}
|
2015-02-23 10:22:26 -05:00
|
|
|
f, err := os.Create(memprofile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
Fatal("%v", err)
|
|
|
|
|
}
|
|
|
|
|
AtExit(func() {
|
2015-04-22 17:53:32 -07:00
|
|
|
runtime.GC() // profile all outstanding allocations
|
2015-02-23 10:22:26 -05:00
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
|
|
|
Fatal("%v", err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|