go/src/cmd/internal/obj/util.go

652 lines
12 KiB
Go
Raw Normal View History

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package obj
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
const REG_NONE = 0
var start time.Time
func Cputime() float64 {
if start.IsZero() {
start = time.Now()
}
return time.Since(start).Seconds()
}
type Biobuf struct {
f *os.File
r *bufio.Reader
w *bufio.Writer
linelen int
}
func Bopenw(name string) (*Biobuf, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &Biobuf{f: f, w: bufio.NewWriter(f)}, nil
}
func Bopenr(name string) (*Biobuf, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
return &Biobuf{f: f, r: bufio.NewReader(f)}, nil
}
func Binitw(w io.Writer) *Biobuf {
return &Biobuf{w: bufio.NewWriter(w)}
}
cmd/compile/internal/gc: compact binary export format The binary import/export format is significantly more compact than the existing textual format. It should also be faster to read and write (to be measured). Use -newexport to enable, for instance: export GO_GCFLAGS=-newexport; make.bash The compiler can import packages using both the old and the new format ("mixed mode"). Missing: export info for inlined functions bodies (performance issue, does not affect correctness). Disabled by default until we have inlined function bodies and confirmation of no regression and equality of binaries. For #6110. For #1909. This change depends on: https://go-review.googlesource.com/16220 https://go-review.googlesource.com/16222 (already submitted) for all.bash to work. Some initial export data sizes for std lib packages. This data is without exported functions with inlineable function bodies. Package old new new/old archive/tar.................................13875.....3883 28% archive/zip.................................19464.....5046 26% bufio....................................... 7733.....2222 29% bytes.......................................10342.....3347 32% cmd/addr2line.................................242.......26 11% cmd/api.....................................39305....10368 26% cmd/asm/internal/arch.......................27732.....7939 29% cmd/asm/internal/asm........................35264....10295 29% cmd/asm/internal/flags........................629......178 28% cmd/asm/internal/lex........................39248....11128 28% cmd/asm.......................................306.......26 8% cmd/cgo.....................................40197....10570 26% cmd/compile/internal/amd64...................1106......214 19% cmd/compile/internal/arm....................27891.....7710 28% cmd/compile/internal/arm64....................891......153 17% cmd/compile/internal/big....................21637.....8336 39% cmd/compile/internal/gc....................109845....29727 27% cmd/compile/internal/mips64...................972......168 17% cmd/compile/internal/ppc64....................972......168 17% cmd/compile/internal/x86.....................1104......195 18% cmd/compile...................................329.......26 8% cmd/cover...................................12986.....3749 29% cmd/dist......................................477.......67 14% cmd/doc.....................................23043.....6793 29% cmd/expdump...................................167.......26 16% cmd/fix......................................1190......208 17% cmd/go......................................26399.....5629 21% cmd/gofmt.....................................499.......26 5% cmd/internal/gcprog..........................1342......490 37% cmd/internal/goobj...........................2690......980 36% cmd/internal/obj/arm........................32740....10057 31% cmd/internal/obj/arm64......................46542....15364 33% cmd/internal/obj/mips.......................42140....13731 33% cmd/internal/obj/ppc64......................42140....13731 33% cmd/internal/obj/x86........................52732....19015 36% cmd/internal/obj............................36729....11690 32% cmd/internal/objfile........................36365....10287 28% cmd/link/internal/amd64.....................45893....12220 27% cmd/link/internal/arm.........................307.......96 31% cmd/link/internal/arm64.......................345.......98 28% cmd/link/internal/ld.......................109300....46326 42% cmd/link/internal/ppc64.......................344.......99 29% cmd/link/internal/x86.........................334......107 32% cmd/link......................................314.......26 8% cmd/newlink..................................8110.....2544 31% cmd/nm........................................210.......26 12% cmd/objdump...................................244.......26 11% cmd/pack....................................14248.....4066 29% cmd/pprof/internal/commands..................5239.....1285 25% cmd/pprof/internal/driver...................37967.....8860 23% cmd/pprof/internal/fetch....................30962.....7337 24% cmd/pprof/internal/plugin...................47734.....7719 16% cmd/pprof/internal/profile..................22286.....6922 31% cmd/pprof/internal/report...................31187.....7838 25% cmd/pprof/internal/svg.......................4315......965 22% cmd/pprof/internal/symbolizer...............30051.....7397 25% cmd/pprof/internal/symbolz..................28545.....6949 24% cmd/pprof/internal/tempfile.................12550.....3356 27% cmd/pprof.....................................563.......26 5% cmd/trace....................................1455......636 44% cmd/vendor/golang.org/x/arch/arm/armasm....168035....64737 39% cmd/vendor/golang.org/x/arch/x86/x86asm.....26871.....8578 32% cmd/vet.....................................38980.....9913 25% cmd/vet/whitelist.............................102.......49 48% cmd/yacc.....................................2518......926 37% compress/bzip2...............................6326......129 2% compress/flate...............................7069.....2541 36% compress/gzip...............................20143.....5069 25% compress/lzw..................................828......295 36% compress/zlib...............................10676.....2692 25% container/heap................................523......181 35% container/list...............................3517......740 21% container/ring................................881......229 26% crypto/aes....................................550......187 34% crypto/cipher................................1966......825 42% crypto.......................................1836......646 35% crypto/des....................................632......235 37% crypto/dsa..................................18718.....5035 27% crypto/ecdsa................................23131.....6097 26% crypto/elliptic.............................20790.....5740 28% crypto/hmac...................................455......186 41% crypto/md5...................................1375......171 12% crypto/rand.................................18132.....4748 26% crypto/rc4....................................561......240 43% crypto/rsa..................................22094.....6380 29% crypto/sha1..................................1416......172 12% crypto/sha256.................................551......238 43% crypto/sha512.................................839......378 45% crypto/subtle................................1153......250 22% crypto/tls..................................58203....17984 31% crypto/x509/pkix............................29447.....8161 28% database/sql/driver..........................3318.....1096 33% database/sql................................11258.....3942 35% debug/dwarf.................................18416.....7006 38% debug/elf...................................57530....21014 37% debug/gosym..................................4992.....2058 41% debug/macho.................................23037.....6538 28% debug/pe....................................21063.....6619 31% debug/plan9obj...............................2467......802 33% encoding/ascii85.............................1523......360 24% encoding/asn1................................1718......527 31% encoding/base32..............................2642......686 26% encoding/base64..............................3077......800 26% encoding/binary..............................4727.....1040 22% encoding/csv................................12223.....2850 23% encoding......................................383......217 57% encoding/gob................................37563....10113 27% encoding/hex.................................1327......390 29% encoding/json...............................30897.....7804 25% encoding/pem..................................595......200 34% encoding/xml................................37798.....9336 25% errors........................................274.......36 13% expvar.......................................3155.....1021 32% flag........................................19860.....2849 14% fmt..........................................3137.....1263 40% go/ast......................................44729....13422 30% go/build....................................16336.....4657 29% go/constant..................................3703......846 23% go/doc.......................................9877.....2807 28% go/format....................................5472.....1575 29% go/importer..................................4980.....1301 26% go/internal/gccgoimporter....................5587.....1525 27% go/internal/gcimporter.......................8979.....2186 24% go/parser...................................20692.....5304 26% go/printer...................................7015.....2029 29% go/scanner...................................9719.....2824 29% go/token.....................................7933.....2465 31% go/types....................................64569....19978 31% hash/adler32.................................1176......176 15% hash/crc32...................................1663......360 22% hash/crc64...................................1587......306 19% hash/fnv.....................................3964......260 7% hash..........................................591......278 47% html..........................................217.......74 34% html/template...............................69623....12588 18% image/color/palette...........................315.......98 31% image/color..................................5565.....1036 19% image/draw...................................6917.....1028 15% image/gif....................................8894.....1654 19% image/internal/imageutil.....................9112.....1476 16% image/jpeg...................................6647.....1026 15% image/png....................................6906.....1069 15% image.......................................28992.....6139 21% index/suffixarray...........................17106.....4773 28% internal/singleflight........................1614......506 31% internal/testenv............................12212.....3152 26% internal/trace...............................2762.....1323 48% io/ioutil...................................13502.....3682 27% io...........................................6765.....2482 37% log.........................................11620.....3317 29% log/syslog..................................13516.....3821 28% math/big....................................21819.....8320 38% math/cmplx...................................2816......438 16% math/rand....................................2317......929 40% math.........................................7511.....2444 33% mime/multipart..............................12679.....3360 27% mime/quotedprintable.........................5458.....1235 23% mime.........................................6076.....1628 27% net/http/cgi................................59796....17173 29% net/http/cookiejar..........................14781.....3739 25% net/http/fcgi...............................57861....16426 28% net/http/httptest...........................84100....24365 29% net/http/httputil...........................67763....18869 28% net/http/internal............................6907......637 9% net/http/pprof..............................57945....16316 28% net/http....................................95391....30210 32% net/internal/socktest........................4555.....1453 32% net/mail....................................14481.....3608 25% net/rpc/jsonrpc.............................33335......988 3% net/rpc.....................................79950....23106 29% net/smtp....................................57790....16468 28% net/textproto...............................11356.....3248 29% net/url......................................3123.....1009 32% os/exec.....................................20738.....5769 28% os/signal.....................................437......167 38% os..........................................24875.....6668 27% path/filepath...............................11340.....2826 25% path..........................................778......285 37% reflect.....................................15469.....5198 34% regexp......................................13627.....4661 34% regexp/syntax................................5539.....2249 41% runtime/debug................................9275.....2322 25% runtime/pprof................................1355......477 35% runtime/race...................................39.......17 44% runtime/trace.................................228.......92 40% runtime.....................................13498.....1821 13% sort.........................................2848......842 30% strconv......................................2947.....1252 42% strings......................................7983.....2456 31% sync/atomic..................................2666.....1149 43% sync.........................................2568......845 33% syscall.....................................81252....38398 47% testing/iotest...............................2444......302 12% testing/quick...............................18890.....5076 27% testing.....................................16502.....4800 29% text/scanner.................................6849.....2052 30% text/tabwriter...............................6607.....1863 28% text/template/parse.........................22978.....6183 27% text/template...............................64153....11518 18% time........................................12103.....3546 29% unicode......................................9706.....3320 34% unicode/utf16................................1055......148 14% unicode/utf8.................................1118......513 46% vendor/golang.org/x/net/http2/hpack..........8905.....2636 30% All packages 3518505 1017774 29% Change-Id: Id657334f276383ff1e6fa91472d3d1db5a03349c Reviewed-on: https://go-review.googlesource.com/13937 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Chris Manghane <cmang@golang.org>
2015-08-13 19:05:37 -07:00
func Binitr(r io.Reader) *Biobuf {
return &Biobuf{r: bufio.NewReader(r)}
}
func (b *Biobuf) Write(p []byte) (int, error) {
return b.w.Write(p)
}
func Bwritestring(b *Biobuf, p string) (int, error) {
return b.w.WriteString(p)
}
func Bseek(b *Biobuf, offset int64, whence int) int64 {
if b.w != nil {
if err := b.w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
} else if b.r != nil {
if whence == 1 {
offset -= int64(b.r.Buffered())
}
}
off, err := b.f.Seek(offset, whence)
if err != nil {
log.Fatalf("seeking in output: %v", err)
}
if b.r != nil {
b.r.Reset(b.f)
}
return off
}
func Boffset(b *Biobuf) int64 {
if b.w != nil {
if err := b.w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
}
off, err := b.f.Seek(0, 1)
if err != nil {
log.Fatalf("seeking in output [0, 1]: %v", err)
}
if b.r != nil {
off -= int64(b.r.Buffered())
}
return off
}
func (b *Biobuf) Flush() error {
return b.w.Flush()
}
func Bputc(b *Biobuf, c byte) {
b.w.WriteByte(c)
}
const Beof = -1
func Bread(b *Biobuf, p []byte) int {
n, err := io.ReadFull(b.r, p)
if n == 0 {
if err != nil && err != io.EOF {
n = -1
}
}
return n
}
func Bgetc(b *Biobuf) int {
c, err := b.r.ReadByte()
if err != nil {
return -1
}
return int(c)
}
func Bgetrune(b *Biobuf) int {
r, _, err := b.r.ReadRune()
if err != nil {
return -1
}
return int(r)
}
func Bungetrune(b *Biobuf) {
b.r.UnreadRune()
}
func (b *Biobuf) Read(p []byte) (int, error) {
return b.r.Read(p)
}
func (b *Biobuf) Peek(n int) ([]byte, error) {
return b.r.Peek(n)
}
func Brdline(b *Biobuf, delim int) string {
s, err := b.r.ReadBytes(byte(delim))
if err != nil {
log.Fatalf("reading input: %v", err)
}
b.linelen = len(s)
return string(s)
}
func Brdstr(b *Biobuf, delim int, cut int) string {
s, err := b.r.ReadString(byte(delim))
if err != nil {
log.Fatalf("reading input: %v", err)
}
if len(s) > 0 && cut > 0 {
s = s[:len(s)-1]
}
return s
}
func Blinelen(b *Biobuf) int {
return b.linelen
}
func Bterm(b *Biobuf) error {
var err error
if b.w != nil {
err = b.w.Flush()
}
err1 := b.f.Close()
if err == nil {
err = err1
}
return err
}
func envOr(key, value string) string {
if x := os.Getenv(key); x != "" {
return x
}
return value
}
func Getgoroot() string {
return envOr("GOROOT", defaultGOROOT)
}
func Getgoarch() string {
return envOr("GOARCH", defaultGOARCH)
}
func Getgoos() string {
return envOr("GOOS", defaultGOOS)
}
func Getgoarm() int32 {
switch v := envOr("GOARM", defaultGOARM); v {
case "5":
return 5
case "6":
return 6
case "7":
return 7
}
// Fail here, rather than validate at multiple call sites.
log.Fatalf("Invalid GOARM value. Must be 5, 6, or 7.")
panic("unreachable")
}
func Getgo386() string {
// Validated by cmd/compile.
return envOr("GO386", defaultGO386)
}
func Getgoextlinkenabled() string {
return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED)
}
func Getgoversion() string {
return version
}
func (p *Prog) Line() string {
return p.Ctxt.LineHist.LineString(int(p.Lineno))
}
var armCondCode = []string{
".EQ",
".NE",
".CS",
".CC",
".MI",
".PL",
".VS",
".VC",
".HI",
".LS",
".GE",
".LT",
".GT",
".LE",
"",
".NV",
}
/* ARM scond byte */
const (
C_SCOND = (1 << 4) - 1
C_SBIT = 1 << 4
C_PBIT = 1 << 5
C_WBIT = 1 << 6
C_FBIT = 1 << 7
C_UBIT = 1 << 7
C_SCOND_XOR = 14
)
// CConv formats ARM condition codes.
func CConv(s uint8) string {
if s == 0 {
return ""
}
sc := armCondCode[(s&C_SCOND)^C_SCOND_XOR]
if s&C_SBIT != 0 {
sc += ".S"
}
if s&C_PBIT != 0 {
sc += ".P"
}
if s&C_WBIT != 0 {
sc += ".W"
}
if s&C_UBIT != 0 { /* ambiguous with FBIT */
sc += ".U"
}
return sc
}
func (p *Prog) String() string {
if p.Ctxt == nil {
return "<Prog without ctxt>"
}
sc := CConv(p.Scond)
var buf bytes.Buffer
fmt.Fprintf(&buf, "%.5d (%v)\t%v%s", p.Pc, p.Line(), Aconv(int(p.As)), sc)
sep := "\t"
if p.From.Type != TYPE_NONE {
fmt.Fprintf(&buf, "%s%v", sep, Dconv(p, &p.From))
sep = ", "
}
if p.Reg != REG_NONE {
// Should not happen but might as well show it if it does.
fmt.Fprintf(&buf, "%s%v", sep, Rconv(int(p.Reg)))
sep = ", "
}
if p.From3Type() != TYPE_NONE {
if p.From3.Type == TYPE_CONST && (p.As == ADATA || p.As == ATEXT || p.As == AGLOBL) {
// Special case - omit $.
fmt.Fprintf(&buf, "%s%d", sep, p.From3.Offset)
} else {
fmt.Fprintf(&buf, "%s%v", sep, Dconv(p, p.From3))
}
sep = ", "
}
if p.To.Type != TYPE_NONE {
fmt.Fprintf(&buf, "%s%v", sep, Dconv(p, &p.To))
}
if p.RegTo2 != REG_NONE {
fmt.Fprintf(&buf, "%s%v", sep, Rconv(int(p.RegTo2)))
}
return buf.String()
}
func (ctxt *Link) NewProg() *Prog {
p := new(Prog) // should be the only call to this; all others should use ctxt.NewProg
p.Ctxt = ctxt
return p
}
func (ctxt *Link) Line(n int) string {
return ctxt.LineHist.LineString(n)
}
func Getcallerpc(interface{}) uintptr {
return 1
}
func (ctxt *Link) Dconv(a *Addr) string {
return Dconv(nil, a)
}
func Dconv(p *Prog, a *Addr) string {
var str string
switch a.Type {
default:
str = fmt.Sprintf("type=%d", a.Type)
case TYPE_NONE:
str = ""
if a.Name != NAME_NONE || a.Reg != 0 || a.Sym != nil {
str = fmt.Sprintf("%v(%v)(NONE)", Mconv(a), Rconv(int(a.Reg)))
}
case TYPE_REG:
// TODO(rsc): This special case is for x86 instructions like
// PINSRQ CX,$1,X6
// where the $1 is included in the p->to Addr.
// Move into a new field.
if a.Offset != 0 {
str = fmt.Sprintf("$%d,%v", a.Offset, Rconv(int(a.Reg)))
break
}
str = Rconv(int(a.Reg))
if a.Name != TYPE_NONE || a.Sym != nil {
str = fmt.Sprintf("%v(%v)(REG)", Mconv(a), Rconv(int(a.Reg)))
}
case TYPE_BRANCH:
if a.Sym != nil {
str = fmt.Sprintf("%s(SB)", a.Sym.Name)
} else if p != nil && p.Pcond != nil {
str = fmt.Sprint(p.Pcond.Pc)
} else if a.Val != nil {
str = fmt.Sprint(a.Val.(*Prog).Pc)
} else {
str = fmt.Sprintf("%d(PC)", a.Offset)
}
case TYPE_INDIR:
str = fmt.Sprintf("*%s", Mconv(a))
case TYPE_MEM:
str = Mconv(a)
if a.Index != REG_NONE {
str += fmt.Sprintf("(%v*%d)", Rconv(int(a.Index)), int(a.Scale))
}
case TYPE_CONST:
if a.Reg != 0 {
str = fmt.Sprintf("$%v(%v)", Mconv(a), Rconv(int(a.Reg)))
} else {
str = fmt.Sprintf("$%v", Mconv(a))
}
case TYPE_TEXTSIZE:
if a.Val.(int32) == ArgsSizeUnknown {
str = fmt.Sprintf("$%d", a.Offset)
} else {
str = fmt.Sprintf("$%d-%d", a.Offset, a.Val.(int32))
}
case TYPE_FCONST:
str = fmt.Sprintf("%.17g", a.Val.(float64))
// Make sure 1 prints as 1.0
if !strings.ContainsAny(str, ".e") {
str += ".0"
}
str = fmt.Sprintf("$(%s)", str)
case TYPE_SCONST:
str = fmt.Sprintf("$%q", a.Val.(string))
case TYPE_ADDR:
str = fmt.Sprintf("$%s", Mconv(a))
case TYPE_SHIFT:
v := int(a.Offset)
op := string("<<>>->@>"[((v>>5)&3)<<1:])
if v&(1<<4) != 0 {
str = fmt.Sprintf("R%d%c%cR%d", v&15, op[0], op[1], (v>>8)&15)
} else {
str = fmt.Sprintf("R%d%c%c%d", v&15, op[0], op[1], (v>>7)&31)
}
if a.Reg != 0 {
str += fmt.Sprintf("(%v)", Rconv(int(a.Reg)))
}
case TYPE_REGREG:
str = fmt.Sprintf("(%v, %v)", Rconv(int(a.Reg)), Rconv(int(a.Offset)))
case TYPE_REGREG2:
str = fmt.Sprintf("%v, %v", Rconv(int(a.Reg)), Rconv(int(a.Offset)))
case TYPE_REGLIST:
str = regListConv(int(a.Offset))
}
return str
}
func Mconv(a *Addr) string {
var str string
switch a.Name {
default:
str = fmt.Sprintf("name=%d", a.Name)
case NAME_NONE:
switch {
case a.Reg == REG_NONE:
str = fmt.Sprint(a.Offset)
case a.Offset == 0:
str = fmt.Sprintf("(%v)", Rconv(int(a.Reg)))
case a.Offset != 0:
str = fmt.Sprintf("%d(%v)", a.Offset, Rconv(int(a.Reg)))
}
case NAME_EXTERN:
if a.Sym != nil {
str = fmt.Sprintf("%s%s(SB)", a.Sym.Name, offConv(a.Offset))
} else {
str = fmt.Sprintf("%s(SB)", offConv(a.Offset))
}
case NAME_GOTREF:
if a.Sym != nil {
str = fmt.Sprintf("%s%s@GOT(SB)", a.Sym.Name, offConv(a.Offset))
} else {
str = fmt.Sprintf("%s@GOT(SB)", offConv(a.Offset))
}
case NAME_STATIC:
if a.Sym != nil {
str = fmt.Sprintf("%s<>%s(SB)", a.Sym.Name, offConv(a.Offset))
} else {
str = fmt.Sprintf("<>%s(SB)", offConv(a.Offset))
}
case NAME_AUTO:
if a.Sym != nil {
str = fmt.Sprintf("%s%s(SP)", a.Sym.Name, offConv(a.Offset))
} else {
str = fmt.Sprintf("%s(SP)", offConv(a.Offset))
}
case NAME_PARAM:
if a.Sym != nil {
str = fmt.Sprintf("%s%s(FP)", a.Sym.Name, offConv(a.Offset))
} else {
str = fmt.Sprintf("%s(FP)", offConv(a.Offset))
}
}
return str
}
func offConv(off int64) string {
if off == 0 {
return ""
}
return fmt.Sprintf("%+d", off)
}
type regSet struct {
lo int
hi int
Rconv func(int) string
}
// Few enough architectures that a linear scan is fastest.
// Not even worth sorting.
var regSpace []regSet
/*
Each architecture defines a register space as a unique
integer range.
Here is the list of architectures and the base of their register spaces.
*/
const (
// Because of masking operations in the encodings, each register
// space should start at 0 modulo some power of 2.
RBase386 = 1 * 1024
RBaseAMD64 = 2 * 1024
RBaseARM = 3 * 1024
RBasePPC64 = 4 * 1024 // range [4k, 8k)
RBaseARM64 = 8 * 1024 // range [8k, 13k)
RBaseMIPS64 = 13 * 1024 // range [13k, 16k)
)
// RegisterRegister binds a pretty-printer (Rconv) for register
// numbers to a given register number range. Lo is inclusive,
// hi exclusive (valid registers are lo through hi-1).
func RegisterRegister(lo, hi int, Rconv func(int) string) {
regSpace = append(regSpace, regSet{lo, hi, Rconv})
}
func Rconv(reg int) string {
if reg == REG_NONE {
return "NONE"
}
for i := range regSpace {
rs := &regSpace[i]
if rs.lo <= reg && reg < rs.hi {
return rs.Rconv(reg)
}
}
return fmt.Sprintf("R???%d", reg)
}
func regListConv(list int) string {
str := ""
for i := 0; i < 16; i++ { // TODO: 16 is ARM-specific.
if list&(1<<uint(i)) != 0 {
if str == "" {
str += "["
} else {
str += ","
}
// This is ARM-specific; R10 is g.
if i == 10 {
str += "g"
} else {
str += fmt.Sprintf("R%d", i)
}
}
}
str += "]"
return str
}
/*
Each architecture defines an instruction (A*) space as a unique
integer range.
Global opcodes like CALL start at 0; the architecture-specific ones
start at a distinct, big-maskable offsets.
Here is the list of architectures and the base of their opcode spaces.
*/
const (
ABase386 = (1 + iota) << 12
ABaseARM
ABaseAMD64
ABasePPC64
ABaseARM64
ABaseMIPS64
AMask = 1<<12 - 1 // AND with this to use the opcode as an array index.
)
type opSet struct {
lo int
names []string
}
// Not even worth sorting
var aSpace []opSet
// RegisterOpcode binds a list of instruction names
// to a given instruction number range.
func RegisterOpcode(lo int, Anames []string) {
aSpace = append(aSpace, opSet{lo, Anames})
}
func Aconv(a int) string {
cmd/compile: recognize Syscall-like functions for liveness analysis Consider this code: func f(*int) func g() { p := new(int) f(p) } where f is an assembly function. In general liveness analysis assumes that during the call to f, p is dead in this frame. If f has retained p, p will be found alive in f's frame and keep the new(int) from being garbage collected. This is all correct and works. We use the Go func declaration for f to give the assembly function liveness information (the arguments are assumed live for the entire call). Now consider this code: func h1() { p := new(int) syscall.Syscall(1, 2, 3, uintptr(unsafe.Pointer(p))) } Here syscall.Syscall is taking the place of f, but because its arguments are uintptr, the liveness analysis and the garbage collector ignore them. Since p is no longer live in h once the call starts, if the garbage collector scans the stack while the system call is blocked, it will find no reference to the new(int) and reclaim it. If the kernel is going to write to *p once the call finishes, reclaiming the memory is a mistake. We can't change the arguments or the liveness information for syscall.Syscall itself, both for compatibility and because sometimes the arguments really are integers, and the garbage collector will get quite upset if it finds an integer where it expects a pointer. The problem is that these arguments are fundamentally untyped. The solution we have taken in the syscall package's wrappers in past releases is to insert a call to a dummy function named "use", to make it look like the argument is live during the call to syscall.Syscall: func h2() { p := new(int) syscall.Syscall(1, 2, 3, uintptr(unsafe.Pointer(p))) use(unsafe.Pointer(p)) } Keeping p alive during the call means that if the garbage collector scans the stack during the system call now, it will find the reference to p. Unfortunately, this approach is not available to users outside syscall, because 'use' is unexported, and people also have to realize they need to use it and do so. There is much existing code using syscall.Syscall without a 'use'-like function. That code will fail very occasionally in mysterious ways (see #13372). This CL fixes all that existing code by making the compiler do the right thing automatically, without any code modifications. That is, it takes h1 above, which is incorrect code today, and makes it correct code. Specifically, if the compiler sees a foreign func definition (one without a body) that has uintptr arguments, it marks those arguments as "unsafe uintptrs". If it later sees the function being called with uintptr(unsafe.Pointer(x)) as an argument, it arranges to mark x as having escaped, and it makes sure to hold x in a live temporary variable until the call returns, so that the garbage collector cannot reclaim whatever heap memory x points to. For now I am leaving the explicit calls to use in package syscall, but they can be removed early in a future cycle (likely Go 1.7). The rule has no effect on escape analysis, only on liveness analysis. Fixes #13372. Change-Id: I2addb83f70d08db08c64d394f9d06ff0a063c500 Reviewed-on: https://go-review.googlesource.com/18584 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-01-13 00:46:28 -05:00
if 0 <= a && a < len(Anames) {
return Anames[a]
}
for i := range aSpace {
as := &aSpace[i]
if as.lo <= a && a < as.lo+len(as.names) {
return as.names[a-as.lo]
}
}
return fmt.Sprintf("A???%d", a)
}
var Anames = []string{
"XXX",
"CALL",
"CHECKNIL",
"DATA",
"DUFFCOPY",
"DUFFZERO",
"END",
"FUNCDATA",
"GLOBL",
"JMP",
"NOP",
"PCDATA",
"RET",
"TEXT",
"TYPE",
"UNDEF",
"USEFIELD",
"VARDEF",
"VARKILL",
"VARLIVE",
}
func Bool2int(b bool) int {
if b {
return 1
}
return 0
}