github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/gc/util.go (about)

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