Source file src/cmd/compile/internal/reflectdata/reflect.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package reflectdata
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  	"internal/abi"
    11  	"os"
    12  	"sort"
    13  	"strings"
    14  	"sync"
    15  
    16  	"cmd/compile/internal/base"
    17  	"cmd/compile/internal/bitvec"
    18  	"cmd/compile/internal/compare"
    19  	"cmd/compile/internal/ir"
    20  	"cmd/compile/internal/objw"
    21  	"cmd/compile/internal/rttype"
    22  	"cmd/compile/internal/staticdata"
    23  	"cmd/compile/internal/typebits"
    24  	"cmd/compile/internal/typecheck"
    25  	"cmd/compile/internal/types"
    26  	"cmd/internal/gcprog"
    27  	"cmd/internal/obj"
    28  	"cmd/internal/objabi"
    29  	"cmd/internal/src"
    30  )
    31  
    32  type ptabEntry struct {
    33  	s *types.Sym
    34  	t *types.Type
    35  }
    36  
    37  // runtime interface and reflection data structures
    38  var (
    39  	// protects signatset and signatslice
    40  	signatmu sync.Mutex
    41  	// Tracking which types need runtime type descriptor
    42  	signatset = make(map[*types.Type]struct{})
    43  	// Queue of types wait to be generated runtime type descriptor
    44  	signatslice []typeAndStr
    45  
    46  	gcsymmu  sync.Mutex // protects gcsymset and gcsymslice
    47  	gcsymset = make(map[*types.Type]struct{})
    48  )
    49  
    50  type typeSig struct {
    51  	name  *types.Sym
    52  	isym  *obj.LSym
    53  	tsym  *obj.LSym
    54  	type_ *types.Type
    55  	mtype *types.Type
    56  }
    57  
    58  // Builds a type representing a Bucket structure for
    59  // the given map type. This type is not visible to users -
    60  // we include only enough information to generate a correct GC
    61  // program for it.
    62  // Make sure this stays in sync with runtime/map.go.
    63  //
    64  //	A "bucket" is a "struct" {
    65  //	      tophash [BUCKETSIZE]uint8
    66  //	      keys [BUCKETSIZE]keyType
    67  //	      elems [BUCKETSIZE]elemType
    68  //	      overflow *bucket
    69  //	    }
    70  const (
    71  	BUCKETSIZE  = abi.MapBucketCount
    72  	MAXKEYSIZE  = abi.MapMaxKeyBytes
    73  	MAXELEMSIZE = abi.MapMaxElemBytes
    74  )
    75  
    76  func commonSize() int { return int(rttype.Type.Size()) } // Sizeof(runtime._type{})
    77  
    78  func uncommonSize(t *types.Type) int { // Sizeof(runtime.uncommontype{})
    79  	if t.Sym() == nil && len(methods(t)) == 0 {
    80  		return 0
    81  	}
    82  	return int(rttype.UncommonType.Size())
    83  }
    84  
    85  func makefield(name string, t *types.Type) *types.Field {
    86  	sym := (*types.Pkg)(nil).Lookup(name)
    87  	return types.NewField(src.NoXPos, sym, t)
    88  }
    89  
    90  // MapBucketType makes the map bucket type given the type of the map.
    91  func MapBucketType(t *types.Type) *types.Type {
    92  	if t.MapType().Bucket != nil {
    93  		return t.MapType().Bucket
    94  	}
    95  
    96  	keytype := t.Key()
    97  	elemtype := t.Elem()
    98  	types.CalcSize(keytype)
    99  	types.CalcSize(elemtype)
   100  	if keytype.Size() > MAXKEYSIZE {
   101  		keytype = types.NewPtr(keytype)
   102  	}
   103  	if elemtype.Size() > MAXELEMSIZE {
   104  		elemtype = types.NewPtr(elemtype)
   105  	}
   106  
   107  	field := make([]*types.Field, 0, 5)
   108  
   109  	// The first field is: uint8 topbits[BUCKETSIZE].
   110  	arr := types.NewArray(types.Types[types.TUINT8], BUCKETSIZE)
   111  	field = append(field, makefield("topbits", arr))
   112  
   113  	arr = types.NewArray(keytype, BUCKETSIZE)
   114  	arr.SetNoalg(true)
   115  	keys := makefield("keys", arr)
   116  	field = append(field, keys)
   117  
   118  	arr = types.NewArray(elemtype, BUCKETSIZE)
   119  	arr.SetNoalg(true)
   120  	elems := makefield("elems", arr)
   121  	field = append(field, elems)
   122  
   123  	// If keys and elems have no pointers, the map implementation
   124  	// can keep a list of overflow pointers on the side so that
   125  	// buckets can be marked as having no pointers.
   126  	// Arrange for the bucket to have no pointers by changing
   127  	// the type of the overflow field to uintptr in this case.
   128  	// See comment on hmap.overflow in runtime/map.go.
   129  	otyp := types.Types[types.TUNSAFEPTR]
   130  	if !elemtype.HasPointers() && !keytype.HasPointers() {
   131  		otyp = types.Types[types.TUINTPTR]
   132  	}
   133  	overflow := makefield("overflow", otyp)
   134  	field = append(field, overflow)
   135  
   136  	// link up fields
   137  	bucket := types.NewStruct(field[:])
   138  	bucket.SetNoalg(true)
   139  	types.CalcSize(bucket)
   140  
   141  	// Check invariants that map code depends on.
   142  	if !types.IsComparable(t.Key()) {
   143  		base.Fatalf("unsupported map key type for %v", t)
   144  	}
   145  	if BUCKETSIZE < 8 {
   146  		base.Fatalf("bucket size %d too small for proper alignment %d", BUCKETSIZE, 8)
   147  	}
   148  	if uint8(keytype.Alignment()) > BUCKETSIZE {
   149  		base.Fatalf("key align too big for %v", t)
   150  	}
   151  	if uint8(elemtype.Alignment()) > BUCKETSIZE {
   152  		base.Fatalf("elem align %d too big for %v, BUCKETSIZE=%d", elemtype.Alignment(), t, BUCKETSIZE)
   153  	}
   154  	if keytype.Size() > MAXKEYSIZE {
   155  		base.Fatalf("key size too large for %v", t)
   156  	}
   157  	if elemtype.Size() > MAXELEMSIZE {
   158  		base.Fatalf("elem size too large for %v", t)
   159  	}
   160  	if t.Key().Size() > MAXKEYSIZE && !keytype.IsPtr() {
   161  		base.Fatalf("key indirect incorrect for %v", t)
   162  	}
   163  	if t.Elem().Size() > MAXELEMSIZE && !elemtype.IsPtr() {
   164  		base.Fatalf("elem indirect incorrect for %v", t)
   165  	}
   166  	if keytype.Size()%keytype.Alignment() != 0 {
   167  		base.Fatalf("key size not a multiple of key align for %v", t)
   168  	}
   169  	if elemtype.Size()%elemtype.Alignment() != 0 {
   170  		base.Fatalf("elem size not a multiple of elem align for %v", t)
   171  	}
   172  	if uint8(bucket.Alignment())%uint8(keytype.Alignment()) != 0 {
   173  		base.Fatalf("bucket align not multiple of key align %v", t)
   174  	}
   175  	if uint8(bucket.Alignment())%uint8(elemtype.Alignment()) != 0 {
   176  		base.Fatalf("bucket align not multiple of elem align %v", t)
   177  	}
   178  	if keys.Offset%keytype.Alignment() != 0 {
   179  		base.Fatalf("bad alignment of keys in bmap for %v", t)
   180  	}
   181  	if elems.Offset%elemtype.Alignment() != 0 {
   182  		base.Fatalf("bad alignment of elems in bmap for %v", t)
   183  	}
   184  
   185  	// Double-check that overflow field is final memory in struct,
   186  	// with no padding at end.
   187  	if overflow.Offset != bucket.Size()-int64(types.PtrSize) {
   188  		base.Fatalf("bad offset of overflow in bmap for %v, overflow.Offset=%d, bucket.Size()-int64(types.PtrSize)=%d",
   189  			t, overflow.Offset, bucket.Size()-int64(types.PtrSize))
   190  	}
   191  
   192  	t.MapType().Bucket = bucket
   193  
   194  	bucket.StructType().Map = t
   195  	return bucket
   196  }
   197  
   198  var hmapType *types.Type
   199  
   200  // MapType returns a type interchangeable with runtime.hmap.
   201  // Make sure this stays in sync with runtime/map.go.
   202  func MapType() *types.Type {
   203  	if hmapType != nil {
   204  		return hmapType
   205  	}
   206  
   207  	// build a struct:
   208  	// type hmap struct {
   209  	//    count      int
   210  	//    flags      uint8
   211  	//    B          uint8
   212  	//    noverflow  uint16
   213  	//    hash0      uint32
   214  	//    buckets    unsafe.Pointer
   215  	//    oldbuckets unsafe.Pointer
   216  	//    nevacuate  uintptr
   217  	//    extra      unsafe.Pointer // *mapextra
   218  	// }
   219  	// must match runtime/map.go:hmap.
   220  	fields := []*types.Field{
   221  		makefield("count", types.Types[types.TINT]),
   222  		makefield("flags", types.Types[types.TUINT8]),
   223  		makefield("B", types.Types[types.TUINT8]),
   224  		makefield("noverflow", types.Types[types.TUINT16]),
   225  		makefield("hash0", types.Types[types.TUINT32]),      // Used in walk.go for OMAKEMAP.
   226  		makefield("buckets", types.Types[types.TUNSAFEPTR]), // Used in walk.go for OMAKEMAP.
   227  		makefield("oldbuckets", types.Types[types.TUNSAFEPTR]),
   228  		makefield("nevacuate", types.Types[types.TUINTPTR]),
   229  		makefield("extra", types.Types[types.TUNSAFEPTR]),
   230  	}
   231  
   232  	n := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, ir.Pkgs.Runtime.Lookup("hmap"))
   233  	hmap := types.NewNamed(n)
   234  	n.SetType(hmap)
   235  	n.SetTypecheck(1)
   236  
   237  	hmap.SetUnderlying(types.NewStruct(fields))
   238  	types.CalcSize(hmap)
   239  
   240  	// The size of hmap should be 48 bytes on 64 bit
   241  	// and 28 bytes on 32 bit platforms.
   242  	if size := int64(8 + 5*types.PtrSize); hmap.Size() != size {
   243  		base.Fatalf("hmap size not correct: got %d, want %d", hmap.Size(), size)
   244  	}
   245  
   246  	hmapType = hmap
   247  	return hmap
   248  }
   249  
   250  var hiterType *types.Type
   251  
   252  // MapIterType returns a type interchangeable with runtime.hiter.
   253  // Make sure this stays in sync with runtime/map.go.
   254  func MapIterType() *types.Type {
   255  	if hiterType != nil {
   256  		return hiterType
   257  	}
   258  
   259  	hmap := MapType()
   260  
   261  	// build a struct:
   262  	// type hiter struct {
   263  	//    key         unsafe.Pointer // *Key
   264  	//    elem        unsafe.Pointer // *Elem
   265  	//    t           unsafe.Pointer // *MapType
   266  	//    h           *hmap
   267  	//    buckets     unsafe.Pointer
   268  	//    bptr        unsafe.Pointer // *bmap
   269  	//    overflow    unsafe.Pointer // *[]*bmap
   270  	//    oldoverflow unsafe.Pointer // *[]*bmap
   271  	//    startBucket uintptr
   272  	//    offset      uint8
   273  	//    wrapped     bool
   274  	//    B           uint8
   275  	//    i           uint8
   276  	//    bucket      uintptr
   277  	//    checkBucket uintptr
   278  	// }
   279  	// must match runtime/map.go:hiter.
   280  	fields := []*types.Field{
   281  		makefield("key", types.Types[types.TUNSAFEPTR]),  // Used in range.go for TMAP.
   282  		makefield("elem", types.Types[types.TUNSAFEPTR]), // Used in range.go for TMAP.
   283  		makefield("t", types.Types[types.TUNSAFEPTR]),
   284  		makefield("h", types.NewPtr(hmap)),
   285  		makefield("buckets", types.Types[types.TUNSAFEPTR]),
   286  		makefield("bptr", types.Types[types.TUNSAFEPTR]),
   287  		makefield("overflow", types.Types[types.TUNSAFEPTR]),
   288  		makefield("oldoverflow", types.Types[types.TUNSAFEPTR]),
   289  		makefield("startBucket", types.Types[types.TUINTPTR]),
   290  		makefield("offset", types.Types[types.TUINT8]),
   291  		makefield("wrapped", types.Types[types.TBOOL]),
   292  		makefield("B", types.Types[types.TUINT8]),
   293  		makefield("i", types.Types[types.TUINT8]),
   294  		makefield("bucket", types.Types[types.TUINTPTR]),
   295  		makefield("checkBucket", types.Types[types.TUINTPTR]),
   296  	}
   297  
   298  	// build iterator struct holding the above fields
   299  	n := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, ir.Pkgs.Runtime.Lookup("hiter"))
   300  	hiter := types.NewNamed(n)
   301  	n.SetType(hiter)
   302  	n.SetTypecheck(1)
   303  
   304  	hiter.SetUnderlying(types.NewStruct(fields))
   305  	types.CalcSize(hiter)
   306  	if hiter.Size() != int64(12*types.PtrSize) {
   307  		base.Fatalf("hash_iter size not correct %d %d", hiter.Size(), 12*types.PtrSize)
   308  	}
   309  
   310  	hiterType = hiter
   311  	return hiter
   312  }
   313  
   314  // methods returns the methods of the non-interface type t, sorted by name.
   315  // Generates stub functions as needed.
   316  func methods(t *types.Type) []*typeSig {
   317  	if t.HasShape() {
   318  		// Shape types have no methods.
   319  		return nil
   320  	}
   321  	// method type
   322  	mt := types.ReceiverBaseType(t)
   323  
   324  	if mt == nil {
   325  		return nil
   326  	}
   327  	typecheck.CalcMethods(mt)
   328  
   329  	// make list of methods for t,
   330  	// generating code if necessary.
   331  	var ms []*typeSig
   332  	for _, f := range mt.AllMethods() {
   333  		if f.Sym == nil {
   334  			base.Fatalf("method with no sym on %v", mt)
   335  		}
   336  		if !f.IsMethod() {
   337  			base.Fatalf("non-method on %v method %v %v", mt, f.Sym, f)
   338  		}
   339  		if f.Type.Recv() == nil {
   340  			base.Fatalf("receiver with no type on %v method %v %v", mt, f.Sym, f)
   341  		}
   342  		if f.Nointerface() && !t.IsFullyInstantiated() {
   343  			// Skip creating method wrappers if f is nointerface. But, if
   344  			// t is an instantiated type, we still have to call
   345  			// methodWrapper, because methodWrapper generates the actual
   346  			// generic method on the type as well.
   347  			continue
   348  		}
   349  
   350  		// get receiver type for this particular method.
   351  		// if pointer receiver but non-pointer t and
   352  		// this is not an embedded pointer inside a struct,
   353  		// method does not apply.
   354  		if !types.IsMethodApplicable(t, f) {
   355  			continue
   356  		}
   357  
   358  		sig := &typeSig{
   359  			name:  f.Sym,
   360  			isym:  methodWrapper(t, f, true),
   361  			tsym:  methodWrapper(t, f, false),
   362  			type_: typecheck.NewMethodType(f.Type, t),
   363  			mtype: typecheck.NewMethodType(f.Type, nil),
   364  		}
   365  		if f.Nointerface() {
   366  			// In the case of a nointerface method on an instantiated
   367  			// type, don't actually append the typeSig.
   368  			continue
   369  		}
   370  		ms = append(ms, sig)
   371  	}
   372  
   373  	return ms
   374  }
   375  
   376  // imethods returns the methods of the interface type t, sorted by name.
   377  func imethods(t *types.Type) []*typeSig {
   378  	var methods []*typeSig
   379  	for _, f := range t.AllMethods() {
   380  		if f.Type.Kind() != types.TFUNC || f.Sym == nil {
   381  			continue
   382  		}
   383  		if f.Sym.IsBlank() {
   384  			base.Fatalf("unexpected blank symbol in interface method set")
   385  		}
   386  		if n := len(methods); n > 0 {
   387  			last := methods[n-1]
   388  			if !last.name.Less(f.Sym) {
   389  				base.Fatalf("sigcmp vs sortinter %v %v", last.name, f.Sym)
   390  			}
   391  		}
   392  
   393  		sig := &typeSig{
   394  			name:  f.Sym,
   395  			mtype: f.Type,
   396  			type_: typecheck.NewMethodType(f.Type, nil),
   397  		}
   398  		methods = append(methods, sig)
   399  
   400  		// NOTE(rsc): Perhaps an oversight that
   401  		// IfaceType.Method is not in the reflect data.
   402  		// Generate the method body, so that compiled
   403  		// code can refer to it.
   404  		methodWrapper(t, f, false)
   405  	}
   406  
   407  	return methods
   408  }
   409  
   410  func dimportpath(p *types.Pkg) {
   411  	if p.Pathsym != nil {
   412  		return
   413  	}
   414  
   415  	if p == types.LocalPkg && base.Ctxt.Pkgpath == "" {
   416  		panic("missing pkgpath")
   417  	}
   418  
   419  	// If we are compiling the runtime package, there are two runtime packages around
   420  	// -- localpkg and Pkgs.Runtime. We don't want to produce import path symbols for
   421  	// both of them, so just produce one for localpkg.
   422  	if base.Ctxt.Pkgpath == "runtime" && p == ir.Pkgs.Runtime {
   423  		return
   424  	}
   425  
   426  	s := base.Ctxt.Lookup("type:.importpath." + p.Prefix + ".")
   427  	ot := dnameData(s, 0, p.Path, "", nil, false, false)
   428  	objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA)
   429  	s.Set(obj.AttrContentAddressable, true)
   430  	p.Pathsym = s
   431  }
   432  
   433  func dgopkgpath(c rttype.Cursor, pkg *types.Pkg) {
   434  	c = c.Field("Bytes")
   435  	if pkg == nil {
   436  		c.WritePtr(nil)
   437  		return
   438  	}
   439  
   440  	dimportpath(pkg)
   441  	c.WritePtr(pkg.Pathsym)
   442  }
   443  
   444  // dgopkgpathOff writes an offset relocation to the pkg path symbol to c.
   445  func dgopkgpathOff(c rttype.Cursor, pkg *types.Pkg) {
   446  	if pkg == nil {
   447  		c.WriteInt32(0)
   448  		return
   449  	}
   450  
   451  	dimportpath(pkg)
   452  	c.WriteSymPtrOff(pkg.Pathsym, false)
   453  }
   454  
   455  // dnameField dumps a reflect.name for a struct field.
   456  func dnameField(c rttype.Cursor, spkg *types.Pkg, ft *types.Field) {
   457  	if !types.IsExported(ft.Sym.Name) && ft.Sym.Pkg != spkg {
   458  		base.Fatalf("package mismatch for %v", ft.Sym)
   459  	}
   460  	nsym := dname(ft.Sym.Name, ft.Note, nil, types.IsExported(ft.Sym.Name), ft.Embedded != 0)
   461  	c.Field("Bytes").WritePtr(nsym)
   462  }
   463  
   464  // dnameData writes the contents of a reflect.name into s at offset ot.
   465  func dnameData(s *obj.LSym, ot int, name, tag string, pkg *types.Pkg, exported, embedded bool) int {
   466  	if len(name) >= 1<<29 {
   467  		base.Fatalf("name too long: %d %s...", len(name), name[:1024])
   468  	}
   469  	if len(tag) >= 1<<29 {
   470  		base.Fatalf("tag too long: %d %s...", len(tag), tag[:1024])
   471  	}
   472  	var nameLen [binary.MaxVarintLen64]byte
   473  	nameLenLen := binary.PutUvarint(nameLen[:], uint64(len(name)))
   474  	var tagLen [binary.MaxVarintLen64]byte
   475  	tagLenLen := binary.PutUvarint(tagLen[:], uint64(len(tag)))
   476  
   477  	// Encode name and tag. See reflect/type.go for details.
   478  	var bits byte
   479  	l := 1 + nameLenLen + len(name)
   480  	if exported {
   481  		bits |= 1 << 0
   482  	}
   483  	if len(tag) > 0 {
   484  		l += tagLenLen + len(tag)
   485  		bits |= 1 << 1
   486  	}
   487  	if pkg != nil {
   488  		bits |= 1 << 2
   489  	}
   490  	if embedded {
   491  		bits |= 1 << 3
   492  	}
   493  	b := make([]byte, l)
   494  	b[0] = bits
   495  	copy(b[1:], nameLen[:nameLenLen])
   496  	copy(b[1+nameLenLen:], name)
   497  	if len(tag) > 0 {
   498  		tb := b[1+nameLenLen+len(name):]
   499  		copy(tb, tagLen[:tagLenLen])
   500  		copy(tb[tagLenLen:], tag)
   501  	}
   502  
   503  	ot = int(s.WriteBytes(base.Ctxt, int64(ot), b))
   504  
   505  	if pkg != nil {
   506  		c := rttype.NewCursor(s, int64(ot), types.Types[types.TUINT32])
   507  		dgopkgpathOff(c, pkg)
   508  		ot += 4
   509  	}
   510  
   511  	return ot
   512  }
   513  
   514  var dnameCount int
   515  
   516  // dname creates a reflect.name for a struct field or method.
   517  func dname(name, tag string, pkg *types.Pkg, exported, embedded bool) *obj.LSym {
   518  	// Write out data as "type:." to signal two things to the
   519  	// linker, first that when dynamically linking, the symbol
   520  	// should be moved to a relro section, and second that the
   521  	// contents should not be decoded as a type.
   522  	sname := "type:.namedata."
   523  	if pkg == nil {
   524  		// In the common case, share data with other packages.
   525  		if name == "" {
   526  			if exported {
   527  				sname += "-noname-exported." + tag
   528  			} else {
   529  				sname += "-noname-unexported." + tag
   530  			}
   531  		} else {
   532  			if exported {
   533  				sname += name + "." + tag
   534  			} else {
   535  				sname += name + "-" + tag
   536  			}
   537  		}
   538  	} else {
   539  		// TODO(mdempsky): We should be able to share these too (except
   540  		// maybe when dynamic linking).
   541  		sname = fmt.Sprintf("%s%s.%d", sname, types.LocalPkg.Prefix, dnameCount)
   542  		dnameCount++
   543  	}
   544  	if embedded {
   545  		sname += ".embedded"
   546  	}
   547  	s := base.Ctxt.Lookup(sname)
   548  	if len(s.P) > 0 {
   549  		return s
   550  	}
   551  	ot := dnameData(s, 0, name, tag, pkg, exported, embedded)
   552  	objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA)
   553  	s.Set(obj.AttrContentAddressable, true)
   554  	return s
   555  }
   556  
   557  // dextratype dumps the fields of a runtime.uncommontype.
   558  // dataAdd is the offset in bytes after the header where the
   559  // backing array of the []method field should be written.
   560  func dextratype(lsym *obj.LSym, off int64, t *types.Type, dataAdd int) {
   561  	m := methods(t)
   562  	if t.Sym() == nil && len(m) == 0 {
   563  		base.Fatalf("extra requested of type with no extra info %v", t)
   564  	}
   565  	noff := types.RoundUp(off, int64(types.PtrSize))
   566  	if noff != off {
   567  		base.Fatalf("unexpected alignment in dextratype for %v", t)
   568  	}
   569  
   570  	for _, a := range m {
   571  		writeType(a.type_)
   572  	}
   573  
   574  	c := rttype.NewCursor(lsym, off, rttype.UncommonType)
   575  	dgopkgpathOff(c.Field("PkgPath"), typePkg(t))
   576  
   577  	dataAdd += uncommonSize(t)
   578  	mcount := len(m)
   579  	if mcount != int(uint16(mcount)) {
   580  		base.Fatalf("too many methods on %v: %d", t, mcount)
   581  	}
   582  	xcount := sort.Search(mcount, func(i int) bool { return !types.IsExported(m[i].name.Name) })
   583  	if dataAdd != int(uint32(dataAdd)) {
   584  		base.Fatalf("methods are too far away on %v: %d", t, dataAdd)
   585  	}
   586  
   587  	c.Field("Mcount").WriteUint16(uint16(mcount))
   588  	c.Field("Xcount").WriteUint16(uint16(xcount))
   589  	c.Field("Moff").WriteUint32(uint32(dataAdd))
   590  	// Note: there is an unused uint32 field here.
   591  
   592  	// Write the backing array for the []method field.
   593  	array := rttype.NewArrayCursor(lsym, off+int64(dataAdd), rttype.Method, mcount)
   594  	for i, a := range m {
   595  		exported := types.IsExported(a.name.Name)
   596  		var pkg *types.Pkg
   597  		if !exported && a.name.Pkg != typePkg(t) {
   598  			pkg = a.name.Pkg
   599  		}
   600  		nsym := dname(a.name.Name, "", pkg, exported, false)
   601  
   602  		e := array.Elem(i)
   603  		e.Field("Name").WriteSymPtrOff(nsym, false)
   604  		dmethodptrOff(e.Field("Mtyp"), writeType(a.mtype))
   605  		dmethodptrOff(e.Field("Ifn"), a.isym)
   606  		dmethodptrOff(e.Field("Tfn"), a.tsym)
   607  	}
   608  }
   609  
   610  func typePkg(t *types.Type) *types.Pkg {
   611  	tsym := t.Sym()
   612  	if tsym == nil {
   613  		switch t.Kind() {
   614  		case types.TARRAY, types.TSLICE, types.TPTR, types.TCHAN:
   615  			if t.Elem() != nil {
   616  				tsym = t.Elem().Sym()
   617  			}
   618  		}
   619  	}
   620  	if tsym != nil && tsym.Pkg != types.BuiltinPkg {
   621  		return tsym.Pkg
   622  	}
   623  	return nil
   624  }
   625  
   626  func dmethodptrOff(c rttype.Cursor, x *obj.LSym) {
   627  	c.WriteInt32(0)
   628  	r := c.Reloc()
   629  	r.Sym = x
   630  	r.Type = objabi.R_METHODOFF
   631  }
   632  
   633  var kinds = []int{
   634  	types.TINT:        objabi.KindInt,
   635  	types.TUINT:       objabi.KindUint,
   636  	types.TINT8:       objabi.KindInt8,
   637  	types.TUINT8:      objabi.KindUint8,
   638  	types.TINT16:      objabi.KindInt16,
   639  	types.TUINT16:     objabi.KindUint16,
   640  	types.TINT32:      objabi.KindInt32,
   641  	types.TUINT32:     objabi.KindUint32,
   642  	types.TINT64:      objabi.KindInt64,
   643  	types.TUINT64:     objabi.KindUint64,
   644  	types.TUINTPTR:    objabi.KindUintptr,
   645  	types.TFLOAT32:    objabi.KindFloat32,
   646  	types.TFLOAT64:    objabi.KindFloat64,
   647  	types.TBOOL:       objabi.KindBool,
   648  	types.TSTRING:     objabi.KindString,
   649  	types.TPTR:        objabi.KindPtr,
   650  	types.TSTRUCT:     objabi.KindStruct,
   651  	types.TINTER:      objabi.KindInterface,
   652  	types.TCHAN:       objabi.KindChan,
   653  	types.TMAP:        objabi.KindMap,
   654  	types.TARRAY:      objabi.KindArray,
   655  	types.TSLICE:      objabi.KindSlice,
   656  	types.TFUNC:       objabi.KindFunc,
   657  	types.TCOMPLEX64:  objabi.KindComplex64,
   658  	types.TCOMPLEX128: objabi.KindComplex128,
   659  	types.TUNSAFEPTR:  objabi.KindUnsafePointer,
   660  }
   661  
   662  var (
   663  	memhashvarlen  *obj.LSym
   664  	memequalvarlen *obj.LSym
   665  )
   666  
   667  // dcommontype dumps the contents of a reflect.rtype (runtime._type) to c.
   668  func dcommontype(c rttype.Cursor, t *types.Type) {
   669  	types.CalcSize(t)
   670  	eqfunc := geneq(t)
   671  
   672  	sptrWeak := true
   673  	var sptr *obj.LSym
   674  	if !t.IsPtr() || t.IsPtrElem() {
   675  		tptr := types.NewPtr(t)
   676  		if t.Sym() != nil || methods(tptr) != nil {
   677  			sptrWeak = false
   678  		}
   679  		sptr = writeType(tptr)
   680  	}
   681  
   682  	gcsym, useGCProg, ptrdata := dgcsym(t, true)
   683  	delete(gcsymset, t)
   684  
   685  	// ../../../../reflect/type.go:/^type.rtype
   686  	// actual type structure
   687  	//	type rtype struct {
   688  	//		size          uintptr
   689  	//		ptrdata       uintptr
   690  	//		hash          uint32
   691  	//		tflag         tflag
   692  	//		align         uint8
   693  	//		fieldAlign    uint8
   694  	//		kind          uint8
   695  	//		equal         func(unsafe.Pointer, unsafe.Pointer) bool
   696  	//		gcdata        *byte
   697  	//		str           nameOff
   698  	//		ptrToThis     typeOff
   699  	//	}
   700  	c.Field("Size_").WriteUintptr(uint64(t.Size()))
   701  	c.Field("PtrBytes").WriteUintptr(uint64(ptrdata))
   702  	c.Field("Hash").WriteUint32(types.TypeHash(t))
   703  
   704  	var tflag abi.TFlag
   705  	if uncommonSize(t) != 0 {
   706  		tflag |= abi.TFlagUncommon
   707  	}
   708  	if t.Sym() != nil && t.Sym().Name != "" {
   709  		tflag |= abi.TFlagNamed
   710  	}
   711  	if compare.IsRegularMemory(t) {
   712  		tflag |= abi.TFlagRegularMemory
   713  	}
   714  
   715  	exported := false
   716  	p := t.NameString()
   717  	// If we're writing out type T,
   718  	// we are very likely to write out type *T as well.
   719  	// Use the string "*T"[1:] for "T", so that the two
   720  	// share storage. This is a cheap way to reduce the
   721  	// amount of space taken up by reflect strings.
   722  	if !strings.HasPrefix(p, "*") {
   723  		p = "*" + p
   724  		tflag |= abi.TFlagExtraStar
   725  		if t.Sym() != nil {
   726  			exported = types.IsExported(t.Sym().Name)
   727  		}
   728  	} else {
   729  		if t.Elem() != nil && t.Elem().Sym() != nil {
   730  			exported = types.IsExported(t.Elem().Sym().Name)
   731  		}
   732  	}
   733  
   734  	if tflag != abi.TFlag(uint8(tflag)) {
   735  		// this should optimize away completely
   736  		panic("Unexpected change in size of abi.TFlag")
   737  	}
   738  	c.Field("TFlag").WriteUint8(uint8(tflag))
   739  
   740  	// runtime (and common sense) expects alignment to be a power of two.
   741  	i := int(uint8(t.Alignment()))
   742  
   743  	if i == 0 {
   744  		i = 1
   745  	}
   746  	if i&(i-1) != 0 {
   747  		base.Fatalf("invalid alignment %d for %v", uint8(t.Alignment()), t)
   748  	}
   749  	c.Field("Align_").WriteUint8(uint8(t.Alignment()))
   750  	c.Field("FieldAlign_").WriteUint8(uint8(t.Alignment()))
   751  
   752  	i = kinds[t.Kind()]
   753  	if types.IsDirectIface(t) {
   754  		i |= objabi.KindDirectIface
   755  	}
   756  	if useGCProg {
   757  		i |= objabi.KindGCProg
   758  	}
   759  	c.Field("Kind_").WriteUint8(uint8(i))
   760  
   761  	c.Field("Equal").WritePtr(eqfunc)
   762  	c.Field("GCData").WritePtr(gcsym)
   763  
   764  	nsym := dname(p, "", nil, exported, false)
   765  	c.Field("Str").WriteSymPtrOff(nsym, false)
   766  	c.Field("PtrToThis").WriteSymPtrOff(sptr, sptrWeak)
   767  }
   768  
   769  // TrackSym returns the symbol for tracking use of field/method f, assumed
   770  // to be a member of struct/interface type t.
   771  func TrackSym(t *types.Type, f *types.Field) *obj.LSym {
   772  	return base.PkgLinksym("go:track", t.LinkString()+"."+f.Sym.Name, obj.ABI0)
   773  }
   774  
   775  func TypeSymPrefix(prefix string, t *types.Type) *types.Sym {
   776  	p := prefix + "." + t.LinkString()
   777  	s := types.TypeSymLookup(p)
   778  
   779  	// This function is for looking up type-related generated functions
   780  	// (e.g. eq and hash). Make sure they are indeed generated.
   781  	signatmu.Lock()
   782  	NeedRuntimeType(t)
   783  	signatmu.Unlock()
   784  
   785  	//print("algsym: %s -> %+S\n", p, s);
   786  
   787  	return s
   788  }
   789  
   790  func TypeSym(t *types.Type) *types.Sym {
   791  	if t == nil || (t.IsPtr() && t.Elem() == nil) || t.IsUntyped() {
   792  		base.Fatalf("TypeSym %v", t)
   793  	}
   794  	if t.Kind() == types.TFUNC && t.Recv() != nil {
   795  		base.Fatalf("misuse of method type: %v", t)
   796  	}
   797  	s := types.TypeSym(t)
   798  	signatmu.Lock()
   799  	NeedRuntimeType(t)
   800  	signatmu.Unlock()
   801  	return s
   802  }
   803  
   804  func TypeLinksymPrefix(prefix string, t *types.Type) *obj.LSym {
   805  	return TypeSymPrefix(prefix, t).Linksym()
   806  }
   807  
   808  func TypeLinksymLookup(name string) *obj.LSym {
   809  	return types.TypeSymLookup(name).Linksym()
   810  }
   811  
   812  func TypeLinksym(t *types.Type) *obj.LSym {
   813  	lsym := TypeSym(t).Linksym()
   814  	signatmu.Lock()
   815  	if lsym.Extra == nil {
   816  		ti := lsym.NewTypeInfo()
   817  		ti.Type = t
   818  	}
   819  	signatmu.Unlock()
   820  	return lsym
   821  }
   822  
   823  // TypePtrAt returns an expression that evaluates to the
   824  // *runtime._type value for t.
   825  func TypePtrAt(pos src.XPos, t *types.Type) *ir.AddrExpr {
   826  	return typecheck.LinksymAddr(pos, TypeLinksym(t), types.Types[types.TUINT8])
   827  }
   828  
   829  // ITabLsym returns the LSym representing the itab for concrete type typ implementing
   830  // interface iface. A dummy tab will be created in the unusual case where typ doesn't
   831  // implement iface. Normally, this wouldn't happen, because the typechecker would
   832  // have reported a compile-time error. This situation can only happen when the
   833  // destination type of a type assert or a type in a type switch is parameterized, so
   834  // it may sometimes, but not always, be a type that can't implement the specified
   835  // interface.
   836  func ITabLsym(typ, iface *types.Type) *obj.LSym {
   837  	s, existed := ir.Pkgs.Itab.LookupOK(typ.LinkString() + "," + iface.LinkString())
   838  	lsym := s.Linksym()
   839  
   840  	if !existed {
   841  		writeITab(lsym, typ, iface, true)
   842  	}
   843  	return lsym
   844  }
   845  
   846  // ITabAddrAt returns an expression that evaluates to the
   847  // *runtime.itab value for concrete type typ implementing interface
   848  // iface.
   849  func ITabAddrAt(pos src.XPos, typ, iface *types.Type) *ir.AddrExpr {
   850  	s, existed := ir.Pkgs.Itab.LookupOK(typ.LinkString() + "," + iface.LinkString())
   851  	lsym := s.Linksym()
   852  
   853  	if !existed {
   854  		writeITab(lsym, typ, iface, false)
   855  	}
   856  
   857  	return typecheck.LinksymAddr(pos, lsym, types.Types[types.TUINT8])
   858  }
   859  
   860  // needkeyupdate reports whether map updates with t as a key
   861  // need the key to be updated.
   862  func needkeyupdate(t *types.Type) bool {
   863  	switch t.Kind() {
   864  	case types.TBOOL, types.TINT, types.TUINT, types.TINT8, types.TUINT8, types.TINT16, types.TUINT16, types.TINT32, types.TUINT32,
   865  		types.TINT64, types.TUINT64, types.TUINTPTR, types.TPTR, types.TUNSAFEPTR, types.TCHAN:
   866  		return false
   867  
   868  	case types.TFLOAT32, types.TFLOAT64, types.TCOMPLEX64, types.TCOMPLEX128, // floats and complex can be +0/-0
   869  		types.TINTER,
   870  		types.TSTRING: // strings might have smaller backing stores
   871  		return true
   872  
   873  	case types.TARRAY:
   874  		return needkeyupdate(t.Elem())
   875  
   876  	case types.TSTRUCT:
   877  		for _, t1 := range t.Fields() {
   878  			if needkeyupdate(t1.Type) {
   879  				return true
   880  			}
   881  		}
   882  		return false
   883  
   884  	default:
   885  		base.Fatalf("bad type for map key: %v", t)
   886  		return true
   887  	}
   888  }
   889  
   890  // hashMightPanic reports whether the hash of a map key of type t might panic.
   891  func hashMightPanic(t *types.Type) bool {
   892  	switch t.Kind() {
   893  	case types.TINTER:
   894  		return true
   895  
   896  	case types.TARRAY:
   897  		return hashMightPanic(t.Elem())
   898  
   899  	case types.TSTRUCT:
   900  		for _, t1 := range t.Fields() {
   901  			if hashMightPanic(t1.Type) {
   902  				return true
   903  			}
   904  		}
   905  		return false
   906  
   907  	default:
   908  		return false
   909  	}
   910  }
   911  
   912  // formalType replaces predeclared aliases with real types.
   913  // They've been separate internally to make error messages
   914  // better, but we have to merge them in the reflect tables.
   915  func formalType(t *types.Type) *types.Type {
   916  	switch t {
   917  	case types.AnyType, types.ByteType, types.RuneType:
   918  		return types.Types[t.Kind()]
   919  	}
   920  	return t
   921  }
   922  
   923  func writeType(t *types.Type) *obj.LSym {
   924  	t = formalType(t)
   925  	if t.IsUntyped() {
   926  		base.Fatalf("writeType %v", t)
   927  	}
   928  
   929  	s := types.TypeSym(t)
   930  	lsym := s.Linksym()
   931  
   932  	// special case (look for runtime below):
   933  	// when compiling package runtime,
   934  	// emit the type structures for int, float, etc.
   935  	tbase := t
   936  	if t.IsPtr() && t.Sym() == nil && t.Elem().Sym() != nil {
   937  		tbase = t.Elem()
   938  	}
   939  	if tbase.Kind() == types.TFORW {
   940  		base.Fatalf("unresolved defined type: %v", tbase)
   941  	}
   942  
   943  	// This is a fake type we generated for our builtin pseudo-runtime
   944  	// package. We'll emit a description for the real type while
   945  	// compiling package runtime, so we don't need or want to emit one
   946  	// from this fake type.
   947  	if sym := tbase.Sym(); sym != nil && sym.Pkg == ir.Pkgs.Runtime {
   948  		return lsym
   949  	}
   950  
   951  	if s.Siggen() {
   952  		return lsym
   953  	}
   954  	s.SetSiggen(true)
   955  
   956  	if !NeedEmit(tbase) {
   957  		if i := typecheck.BaseTypeIndex(t); i >= 0 {
   958  			lsym.Pkg = tbase.Sym().Pkg.Prefix
   959  			lsym.SymIdx = int32(i)
   960  			lsym.Set(obj.AttrIndexed, true)
   961  		}
   962  
   963  		// TODO(mdempsky): Investigate whether this still happens.
   964  		// If we know we don't need to emit code for a type,
   965  		// we should have a link-symbol index for it.
   966  		// See also TODO in NeedEmit.
   967  		return lsym
   968  	}
   969  
   970  	// Type layout                          Written by               Marker
   971  	// +--------------------------------+                            - 0
   972  	// | abi/internal.Type              |   dcommontype
   973  	// +--------------------------------+                            - A
   974  	// | additional type-dependent      |   code in the switch below
   975  	// | fields, e.g.                   |
   976  	// | abi/internal.ArrayType.Len     |
   977  	// +--------------------------------+                            - B
   978  	// | internal/abi.UncommonType      |   dextratype
   979  	// | This section is optional,      |
   980  	// | if type has a name or methods  |
   981  	// +--------------------------------+                            - C
   982  	// | variable-length data           |   code in the switch below
   983  	// | referenced by                  |
   984  	// | type-dependent fields, e.g.    |
   985  	// | abi/internal.StructType.Fields |
   986  	// | dataAdd = size of this section |
   987  	// +--------------------------------+                            - D
   988  	// | method list, if any            |   dextratype
   989  	// +--------------------------------+                            - E
   990  
   991  	// UncommonType section is included if we have a name or a method.
   992  	extra := t.Sym() != nil || len(methods(t)) != 0
   993  
   994  	// Decide the underlying type of the descriptor, and remember
   995  	// the size we need for variable-length data.
   996  	var rt *types.Type
   997  	dataAdd := 0
   998  	switch t.Kind() {
   999  	default:
  1000  		rt = rttype.Type
  1001  	case types.TARRAY:
  1002  		rt = rttype.ArrayType
  1003  	case types.TSLICE:
  1004  		rt = rttype.SliceType
  1005  	case types.TCHAN:
  1006  		rt = rttype.ChanType
  1007  	case types.TFUNC:
  1008  		rt = rttype.FuncType
  1009  		dataAdd = (t.NumRecvs() + t.NumParams() + t.NumResults()) * types.PtrSize
  1010  	case types.TINTER:
  1011  		rt = rttype.InterfaceType
  1012  		dataAdd = len(imethods(t)) * int(rttype.IMethod.Size())
  1013  	case types.TMAP:
  1014  		rt = rttype.MapType
  1015  	case types.TPTR:
  1016  		rt = rttype.PtrType
  1017  		// TODO: use rttype.Type for Elem() is ANY?
  1018  	case types.TSTRUCT:
  1019  		rt = rttype.StructType
  1020  		dataAdd = t.NumFields() * int(rttype.StructField.Size())
  1021  	}
  1022  
  1023  	// Compute offsets of each section.
  1024  	B := rt.Size()
  1025  	C := B
  1026  	if extra {
  1027  		C = B + rttype.UncommonType.Size()
  1028  	}
  1029  	D := C + int64(dataAdd)
  1030  	E := D + int64(len(methods(t)))*rttype.Method.Size()
  1031  
  1032  	// Write the runtime._type
  1033  	c := rttype.NewCursor(lsym, 0, rt)
  1034  	if rt == rttype.Type {
  1035  		dcommontype(c, t)
  1036  	} else {
  1037  		dcommontype(c.Field("Type"), t)
  1038  	}
  1039  
  1040  	// Write additional type-specific data
  1041  	// (Both the fixed size and variable-sized sections.)
  1042  	switch t.Kind() {
  1043  	case types.TARRAY:
  1044  		// internal/abi.ArrayType
  1045  		s1 := writeType(t.Elem())
  1046  		t2 := types.NewSlice(t.Elem())
  1047  		s2 := writeType(t2)
  1048  		c.Field("Elem").WritePtr(s1)
  1049  		c.Field("Slice").WritePtr(s2)
  1050  		c.Field("Len").WriteUintptr(uint64(t.NumElem()))
  1051  
  1052  	case types.TSLICE:
  1053  		// internal/abi.SliceType
  1054  		s1 := writeType(t.Elem())
  1055  		c.Field("Elem").WritePtr(s1)
  1056  
  1057  	case types.TCHAN:
  1058  		// internal/abi.ChanType
  1059  		s1 := writeType(t.Elem())
  1060  		c.Field("Elem").WritePtr(s1)
  1061  		c.Field("Dir").WriteInt(int64(t.ChanDir()))
  1062  
  1063  	case types.TFUNC:
  1064  		// internal/abi.FuncType
  1065  		for _, t1 := range t.RecvParamsResults() {
  1066  			writeType(t1.Type)
  1067  		}
  1068  		inCount := t.NumRecvs() + t.NumParams()
  1069  		outCount := t.NumResults()
  1070  		if t.IsVariadic() {
  1071  			outCount |= 1 << 15
  1072  		}
  1073  
  1074  		c.Field("InCount").WriteUint16(uint16(inCount))
  1075  		c.Field("OutCount").WriteUint16(uint16(outCount))
  1076  
  1077  		// Array of rtype pointers follows funcType.
  1078  		typs := t.RecvParamsResults()
  1079  		array := rttype.NewArrayCursor(lsym, C, types.Types[types.TUNSAFEPTR], len(typs))
  1080  		for i, t1 := range typs {
  1081  			array.Elem(i).WritePtr(writeType(t1.Type))
  1082  		}
  1083  
  1084  	case types.TINTER:
  1085  		// internal/abi.InterfaceType
  1086  		m := imethods(t)
  1087  		n := len(m)
  1088  		for _, a := range m {
  1089  			writeType(a.type_)
  1090  		}
  1091  
  1092  		var tpkg *types.Pkg
  1093  		if t.Sym() != nil && t != types.Types[t.Kind()] && t != types.ErrorType {
  1094  			tpkg = t.Sym().Pkg
  1095  		}
  1096  		dgopkgpath(c.Field("PkgPath"), tpkg)
  1097  		c.Field("Methods").WriteSlice(lsym, C, int64(n), int64(n))
  1098  
  1099  		array := rttype.NewArrayCursor(lsym, C, rttype.IMethod, n)
  1100  		for i, a := range m {
  1101  			exported := types.IsExported(a.name.Name)
  1102  			var pkg *types.Pkg
  1103  			if !exported && a.name.Pkg != tpkg {
  1104  				pkg = a.name.Pkg
  1105  			}
  1106  			nsym := dname(a.name.Name, "", pkg, exported, false)
  1107  
  1108  			e := array.Elem(i)
  1109  			e.Field("Name").WriteSymPtrOff(nsym, false)
  1110  			e.Field("Typ").WriteSymPtrOff(writeType(a.type_), false)
  1111  		}
  1112  
  1113  	case types.TMAP:
  1114  		// internal/abi.MapType
  1115  		s1 := writeType(t.Key())
  1116  		s2 := writeType(t.Elem())
  1117  		s3 := writeType(MapBucketType(t))
  1118  		hasher := genhash(t.Key())
  1119  
  1120  		c.Field("Key").WritePtr(s1)
  1121  		c.Field("Elem").WritePtr(s2)
  1122  		c.Field("Bucket").WritePtr(s3)
  1123  		c.Field("Hasher").WritePtr(hasher)
  1124  		var flags uint32
  1125  		// Note: flags must match maptype accessors in ../../../../runtime/type.go
  1126  		// and maptype builder in ../../../../reflect/type.go:MapOf.
  1127  		if t.Key().Size() > MAXKEYSIZE {
  1128  			c.Field("KeySize").WriteUint8(uint8(types.PtrSize))
  1129  			flags |= 1 // indirect key
  1130  		} else {
  1131  			c.Field("KeySize").WriteUint8(uint8(t.Key().Size()))
  1132  		}
  1133  
  1134  		if t.Elem().Size() > MAXELEMSIZE {
  1135  			c.Field("ValueSize").WriteUint8(uint8(types.PtrSize))
  1136  			flags |= 2 // indirect value
  1137  		} else {
  1138  			c.Field("ValueSize").WriteUint8(uint8(t.Elem().Size()))
  1139  		}
  1140  		c.Field("BucketSize").WriteUint16(uint16(MapBucketType(t).Size()))
  1141  		if types.IsReflexive(t.Key()) {
  1142  			flags |= 4 // reflexive key
  1143  		}
  1144  		if needkeyupdate(t.Key()) {
  1145  			flags |= 8 // need key update
  1146  		}
  1147  		if hashMightPanic(t.Key()) {
  1148  			flags |= 16 // hash might panic
  1149  		}
  1150  		c.Field("Flags").WriteUint32(flags)
  1151  
  1152  		if u := t.Underlying(); u != t {
  1153  			// If t is a named map type, also keep the underlying map
  1154  			// type live in the binary. This is important to make sure that
  1155  			// a named map and that same map cast to its underlying type via
  1156  			// reflection, use the same hash function. See issue 37716.
  1157  			r := obj.Addrel(lsym)
  1158  			r.Sym = writeType(u)
  1159  			r.Type = objabi.R_KEEP
  1160  		}
  1161  
  1162  	case types.TPTR:
  1163  		// internal/abi.PtrType
  1164  		if t.Elem().Kind() == types.TANY {
  1165  			base.Fatalf("bad pointer base type")
  1166  		}
  1167  
  1168  		s1 := writeType(t.Elem())
  1169  		c.Field("Elem").WritePtr(s1)
  1170  
  1171  	case types.TSTRUCT:
  1172  		// internal/abi.StructType
  1173  		fields := t.Fields()
  1174  		for _, t1 := range fields {
  1175  			writeType(t1.Type)
  1176  		}
  1177  
  1178  		// All non-exported struct field names within a struct
  1179  		// type must originate from a single package. By
  1180  		// identifying and recording that package within the
  1181  		// struct type descriptor, we can omit that
  1182  		// information from the field descriptors.
  1183  		var spkg *types.Pkg
  1184  		for _, f := range fields {
  1185  			if !types.IsExported(f.Sym.Name) {
  1186  				spkg = f.Sym.Pkg
  1187  				break
  1188  			}
  1189  		}
  1190  
  1191  		dgopkgpath(c.Field("PkgPath"), spkg)
  1192  		c.Field("Fields").WriteSlice(lsym, C, int64(len(fields)), int64(len(fields)))
  1193  
  1194  		array := rttype.NewArrayCursor(lsym, C, rttype.StructField, len(fields))
  1195  		for i, f := range fields {
  1196  			e := array.Elem(i)
  1197  			dnameField(e.Field("Name"), spkg, f)
  1198  			e.Field("Typ").WritePtr(writeType(f.Type))
  1199  			e.Field("Offset").WriteUintptr(uint64(f.Offset))
  1200  		}
  1201  	}
  1202  
  1203  	// Write the extra info, if any.
  1204  	if extra {
  1205  		dextratype(lsym, B, t, dataAdd)
  1206  	}
  1207  
  1208  	// Note: DUPOK is required to ensure that we don't end up with more
  1209  	// than one type descriptor for a given type, if the type descriptor
  1210  	// can be defined in multiple packages, that is, unnamed types,
  1211  	// instantiated types and shape types.
  1212  	dupok := 0
  1213  	if tbase.Sym() == nil || tbase.IsFullyInstantiated() || tbase.HasShape() {
  1214  		dupok = obj.DUPOK
  1215  	}
  1216  
  1217  	objw.Global(lsym, int32(E), int16(dupok|obj.RODATA))
  1218  
  1219  	// The linker will leave a table of all the typelinks for
  1220  	// types in the binary, so the runtime can find them.
  1221  	//
  1222  	// When buildmode=shared, all types are in typelinks so the
  1223  	// runtime can deduplicate type pointers.
  1224  	keep := base.Ctxt.Flag_dynlink
  1225  	if !keep && t.Sym() == nil {
  1226  		// For an unnamed type, we only need the link if the type can
  1227  		// be created at run time by reflect.PointerTo and similar
  1228  		// functions. If the type exists in the program, those
  1229  		// functions must return the existing type structure rather
  1230  		// than creating a new one.
  1231  		switch t.Kind() {
  1232  		case types.TPTR, types.TARRAY, types.TCHAN, types.TFUNC, types.TMAP, types.TSLICE, types.TSTRUCT:
  1233  			keep = true
  1234  		}
  1235  	}
  1236  	// Do not put Noalg types in typelinks.  See issue #22605.
  1237  	if types.TypeHasNoAlg(t) {
  1238  		keep = false
  1239  	}
  1240  	lsym.Set(obj.AttrMakeTypelink, keep)
  1241  
  1242  	return lsym
  1243  }
  1244  
  1245  // InterfaceMethodOffset returns the offset of the i-th method in the interface
  1246  // type descriptor, ityp.
  1247  func InterfaceMethodOffset(ityp *types.Type, i int64) int64 {
  1248  	// interface type descriptor layout is struct {
  1249  	//   _type        // commonSize
  1250  	//   pkgpath      // 1 word
  1251  	//   []imethod    // 3 words (pointing to [...]imethod below)
  1252  	//   uncommontype // uncommonSize
  1253  	//   [...]imethod
  1254  	// }
  1255  	// The size of imethod is 8.
  1256  	return int64(commonSize()+4*types.PtrSize+uncommonSize(ityp)) + i*8
  1257  }
  1258  
  1259  // NeedRuntimeType ensures that a runtime type descriptor is emitted for t.
  1260  func NeedRuntimeType(t *types.Type) {
  1261  	if _, ok := signatset[t]; !ok {
  1262  		signatset[t] = struct{}{}
  1263  		signatslice = append(signatslice, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
  1264  	}
  1265  }
  1266  
  1267  func WriteRuntimeTypes() {
  1268  	// Process signatslice. Use a loop, as writeType adds
  1269  	// entries to signatslice while it is being processed.
  1270  	for len(signatslice) > 0 {
  1271  		signats := signatslice
  1272  		// Sort for reproducible builds.
  1273  		sort.Sort(typesByString(signats))
  1274  		for _, ts := range signats {
  1275  			t := ts.t
  1276  			writeType(t)
  1277  			if t.Sym() != nil {
  1278  				writeType(types.NewPtr(t))
  1279  			}
  1280  		}
  1281  		signatslice = signatslice[len(signats):]
  1282  	}
  1283  }
  1284  
  1285  func WriteGCSymbols() {
  1286  	// Emit GC data symbols.
  1287  	gcsyms := make([]typeAndStr, 0, len(gcsymset))
  1288  	for t := range gcsymset {
  1289  		gcsyms = append(gcsyms, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
  1290  	}
  1291  	sort.Sort(typesByString(gcsyms))
  1292  	for _, ts := range gcsyms {
  1293  		dgcsym(ts.t, true)
  1294  	}
  1295  }
  1296  
  1297  // writeITab writes the itab for concrete type typ implementing interface iface. If
  1298  // allowNonImplement is true, allow the case where typ does not implement iface, and just
  1299  // create a dummy itab with zeroed-out method entries.
  1300  func writeITab(lsym *obj.LSym, typ, iface *types.Type, allowNonImplement bool) {
  1301  	// TODO(mdempsky): Fix methodWrapper, geneq, and genhash (and maybe
  1302  	// others) to stop clobbering these.
  1303  	oldpos, oldfn := base.Pos, ir.CurFunc
  1304  	defer func() { base.Pos, ir.CurFunc = oldpos, oldfn }()
  1305  
  1306  	if typ == nil || (typ.IsPtr() && typ.Elem() == nil) || typ.IsUntyped() || iface == nil || !iface.IsInterface() || iface.IsEmptyInterface() {
  1307  		base.Fatalf("writeITab(%v, %v)", typ, iface)
  1308  	}
  1309  
  1310  	sigs := iface.AllMethods()
  1311  	entries := make([]*obj.LSym, 0, len(sigs))
  1312  
  1313  	// both sigs and methods are sorted by name,
  1314  	// so we can find the intersection in a single pass
  1315  	for _, m := range methods(typ) {
  1316  		if m.name == sigs[0].Sym {
  1317  			entries = append(entries, m.isym)
  1318  			if m.isym == nil {
  1319  				panic("NO ISYM")
  1320  			}
  1321  			sigs = sigs[1:]
  1322  			if len(sigs) == 0 {
  1323  				break
  1324  			}
  1325  		}
  1326  	}
  1327  	completeItab := len(sigs) == 0
  1328  	if !allowNonImplement && !completeItab {
  1329  		base.Fatalf("incomplete itab")
  1330  	}
  1331  
  1332  	// dump empty itab symbol into i.sym
  1333  	// type itab struct {
  1334  	//   inter  *interfacetype
  1335  	//   _type  *_type
  1336  	//   hash   uint32 // copy of _type.hash. Used for type switches.
  1337  	//   _      [4]byte
  1338  	//   fun    [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
  1339  	// }
  1340  	o := objw.SymPtr(lsym, 0, writeType(iface), 0)
  1341  	o = objw.SymPtr(lsym, o, writeType(typ), 0)
  1342  	o = objw.Uint32(lsym, o, types.TypeHash(typ)) // copy of type hash
  1343  	o += 4                                        // skip unused field
  1344  	if !completeItab {
  1345  		// If typ doesn't implement iface, make method entries be zero.
  1346  		o = objw.Uintptr(lsym, o, 0)
  1347  		entries = entries[:0]
  1348  	}
  1349  	for _, fn := range entries {
  1350  		o = objw.SymPtrWeak(lsym, o, fn, 0) // method pointer for each method
  1351  	}
  1352  	// Nothing writes static itabs, so they are read only.
  1353  	objw.Global(lsym, int32(o), int16(obj.DUPOK|obj.RODATA))
  1354  	lsym.Set(obj.AttrContentAddressable, true)
  1355  }
  1356  
  1357  func WritePluginTable() {
  1358  	ptabs := typecheck.Target.PluginExports
  1359  	if len(ptabs) == 0 {
  1360  		return
  1361  	}
  1362  
  1363  	lsym := base.Ctxt.Lookup("go:plugin.tabs")
  1364  	ot := 0
  1365  	for _, p := range ptabs {
  1366  		// Dump ptab symbol into go.pluginsym package.
  1367  		//
  1368  		// type ptab struct {
  1369  		//	name nameOff
  1370  		//	typ  typeOff // pointer to symbol
  1371  		// }
  1372  		nsym := dname(p.Sym().Name, "", nil, true, false)
  1373  		t := p.Type()
  1374  		if p.Class != ir.PFUNC {
  1375  			t = types.NewPtr(t)
  1376  		}
  1377  		tsym := writeType(t)
  1378  		ot = objw.SymPtrOff(lsym, ot, nsym)
  1379  		ot = objw.SymPtrOff(lsym, ot, tsym)
  1380  		// Plugin exports symbols as interfaces. Mark their types
  1381  		// as UsedInIface.
  1382  		tsym.Set(obj.AttrUsedInIface, true)
  1383  	}
  1384  	objw.Global(lsym, int32(ot), int16(obj.RODATA))
  1385  
  1386  	lsym = base.Ctxt.Lookup("go:plugin.exports")
  1387  	ot = 0
  1388  	for _, p := range ptabs {
  1389  		ot = objw.SymPtr(lsym, ot, p.Linksym(), 0)
  1390  	}
  1391  	objw.Global(lsym, int32(ot), int16(obj.RODATA))
  1392  }
  1393  
  1394  // writtenByWriteBasicTypes reports whether typ is written by WriteBasicTypes.
  1395  // WriteBasicTypes always writes pointer types; any pointer has been stripped off typ already.
  1396  func writtenByWriteBasicTypes(typ *types.Type) bool {
  1397  	if typ.Sym() == nil && typ.Kind() == types.TFUNC {
  1398  		// func(error) string
  1399  		if typ.NumRecvs() == 0 &&
  1400  			typ.NumParams() == 1 && typ.NumResults() == 1 &&
  1401  			typ.Param(0).Type == types.ErrorType &&
  1402  			typ.Result(0).Type == types.Types[types.TSTRING] {
  1403  			return true
  1404  		}
  1405  	}
  1406  
  1407  	// Now we have left the basic types plus any and error, plus slices of them.
  1408  	// Strip the slice.
  1409  	if typ.Sym() == nil && typ.IsSlice() {
  1410  		typ = typ.Elem()
  1411  	}
  1412  
  1413  	// Basic types.
  1414  	sym := typ.Sym()
  1415  	if sym != nil && (sym.Pkg == types.BuiltinPkg || sym.Pkg == types.UnsafePkg) {
  1416  		return true
  1417  	}
  1418  	// any or error
  1419  	return (sym == nil && typ.IsEmptyInterface()) || typ == types.ErrorType
  1420  }
  1421  
  1422  func WriteBasicTypes() {
  1423  	// do basic types if compiling package runtime.
  1424  	// they have to be in at least one package,
  1425  	// and runtime is always loaded implicitly,
  1426  	// so this is as good as any.
  1427  	// another possible choice would be package main,
  1428  	// but using runtime means fewer copies in object files.
  1429  	// The code here needs to be in sync with writtenByWriteBasicTypes above.
  1430  	if base.Ctxt.Pkgpath != "runtime" {
  1431  		return
  1432  	}
  1433  
  1434  	// Note: always write NewPtr(t) because NeedEmit's caller strips the pointer.
  1435  	var list []*types.Type
  1436  	for i := types.Kind(1); i <= types.TBOOL; i++ {
  1437  		list = append(list, types.Types[i])
  1438  	}
  1439  	list = append(list,
  1440  		types.Types[types.TSTRING],
  1441  		types.Types[types.TUNSAFEPTR],
  1442  		types.AnyType,
  1443  		types.ErrorType)
  1444  	for _, t := range list {
  1445  		writeType(types.NewPtr(t))
  1446  		writeType(types.NewPtr(types.NewSlice(t)))
  1447  	}
  1448  
  1449  	// emit type for func(error) string,
  1450  	// which is the type of an auto-generated wrapper.
  1451  	writeType(types.NewPtr(types.NewSignature(nil, []*types.Field{
  1452  		types.NewField(base.Pos, nil, types.ErrorType),
  1453  	}, []*types.Field{
  1454  		types.NewField(base.Pos, nil, types.Types[types.TSTRING]),
  1455  	})))
  1456  }
  1457  
  1458  type typeAndStr struct {
  1459  	t       *types.Type
  1460  	short   string // "short" here means TypeSymName
  1461  	regular string
  1462  }
  1463  
  1464  type typesByString []typeAndStr
  1465  
  1466  func (a typesByString) Len() int { return len(a) }
  1467  func (a typesByString) Less(i, j int) bool {
  1468  	// put named types before unnamed types
  1469  	if a[i].t.Sym() != nil && a[j].t.Sym() == nil {
  1470  		return true
  1471  	}
  1472  	if a[i].t.Sym() == nil && a[j].t.Sym() != nil {
  1473  		return false
  1474  	}
  1475  
  1476  	if a[i].short != a[j].short {
  1477  		return a[i].short < a[j].short
  1478  	}
  1479  	// When the only difference between the types is whether
  1480  	// they refer to byte or uint8, such as **byte vs **uint8,
  1481  	// the types' NameStrings can be identical.
  1482  	// To preserve deterministic sort ordering, sort these by String().
  1483  	//
  1484  	// TODO(mdempsky): This all seems suspect. Using LinkString would
  1485  	// avoid naming collisions, and there shouldn't be a reason to care
  1486  	// about "byte" vs "uint8": they share the same runtime type
  1487  	// descriptor anyway.
  1488  	if a[i].regular != a[j].regular {
  1489  		return a[i].regular < a[j].regular
  1490  	}
  1491  	// Identical anonymous interfaces defined in different locations
  1492  	// will be equal for the above checks, but different in DWARF output.
  1493  	// Sort by source position to ensure deterministic order.
  1494  	// See issues 27013 and 30202.
  1495  	if a[i].t.Kind() == types.TINTER && len(a[i].t.AllMethods()) > 0 {
  1496  		return a[i].t.AllMethods()[0].Pos.Before(a[j].t.AllMethods()[0].Pos)
  1497  	}
  1498  	return false
  1499  }
  1500  func (a typesByString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  1501  
  1502  // maxPtrmaskBytes is the maximum length of a GC ptrmask bitmap,
  1503  // which holds 1-bit entries describing where pointers are in a given type.
  1504  // Above this length, the GC information is recorded as a GC program,
  1505  // which can express repetition compactly. In either form, the
  1506  // information is used by the runtime to initialize the heap bitmap,
  1507  // and for large types (like 128 or more words), they are roughly the
  1508  // same speed. GC programs are never much larger and often more
  1509  // compact. (If large arrays are involved, they can be arbitrarily
  1510  // more compact.)
  1511  //
  1512  // The cutoff must be large enough that any allocation large enough to
  1513  // use a GC program is large enough that it does not share heap bitmap
  1514  // bytes with any other objects, allowing the GC program execution to
  1515  // assume an aligned start and not use atomic operations. In the current
  1516  // runtime, this means all malloc size classes larger than the cutoff must
  1517  // be multiples of four words. On 32-bit systems that's 16 bytes, and
  1518  // all size classes >= 16 bytes are 16-byte aligned, so no real constraint.
  1519  // On 64-bit systems, that's 32 bytes, and 32-byte alignment is guaranteed
  1520  // for size classes >= 256 bytes. On a 64-bit system, 256 bytes allocated
  1521  // is 32 pointers, the bits for which fit in 4 bytes. So maxPtrmaskBytes
  1522  // must be >= 4.
  1523  //
  1524  // We used to use 16 because the GC programs do have some constant overhead
  1525  // to get started, and processing 128 pointers seems to be enough to
  1526  // amortize that overhead well.
  1527  //
  1528  // To make sure that the runtime's chansend can call typeBitsBulkBarrier,
  1529  // we raised the limit to 2048, so that even 32-bit systems are guaranteed to
  1530  // use bitmaps for objects up to 64 kB in size.
  1531  //
  1532  // Also known to reflect/type.go.
  1533  const maxPtrmaskBytes = 2048
  1534  
  1535  // GCSym returns a data symbol containing GC information for type t, along
  1536  // with a boolean reporting whether the UseGCProg bit should be set in the
  1537  // type kind, and the ptrdata field to record in the reflect type information.
  1538  // GCSym may be called in concurrent backend, so it does not emit the symbol
  1539  // content.
  1540  func GCSym(t *types.Type) (lsym *obj.LSym, useGCProg bool, ptrdata int64) {
  1541  	// Record that we need to emit the GC symbol.
  1542  	gcsymmu.Lock()
  1543  	if _, ok := gcsymset[t]; !ok {
  1544  		gcsymset[t] = struct{}{}
  1545  	}
  1546  	gcsymmu.Unlock()
  1547  
  1548  	return dgcsym(t, false)
  1549  }
  1550  
  1551  // dgcsym returns a data symbol containing GC information for type t, along
  1552  // with a boolean reporting whether the UseGCProg bit should be set in the
  1553  // type kind, and the ptrdata field to record in the reflect type information.
  1554  // When write is true, it writes the symbol data.
  1555  func dgcsym(t *types.Type, write bool) (lsym *obj.LSym, useGCProg bool, ptrdata int64) {
  1556  	ptrdata = types.PtrDataSize(t)
  1557  	if ptrdata/int64(types.PtrSize) <= maxPtrmaskBytes*8 {
  1558  		lsym = dgcptrmask(t, write)
  1559  		return
  1560  	}
  1561  
  1562  	useGCProg = true
  1563  	lsym, ptrdata = dgcprog(t, write)
  1564  	return
  1565  }
  1566  
  1567  // dgcptrmask emits and returns the symbol containing a pointer mask for type t.
  1568  func dgcptrmask(t *types.Type, write bool) *obj.LSym {
  1569  	// Bytes we need for the ptrmask.
  1570  	n := (types.PtrDataSize(t)/int64(types.PtrSize) + 7) / 8
  1571  	// Runtime wants ptrmasks padded to a multiple of uintptr in size.
  1572  	n = (n + int64(types.PtrSize) - 1) &^ (int64(types.PtrSize) - 1)
  1573  	ptrmask := make([]byte, n)
  1574  	fillptrmask(t, ptrmask)
  1575  	p := fmt.Sprintf("runtime.gcbits.%x", ptrmask)
  1576  
  1577  	lsym := base.Ctxt.Lookup(p)
  1578  	if write && !lsym.OnList() {
  1579  		for i, x := range ptrmask {
  1580  			objw.Uint8(lsym, i, x)
  1581  		}
  1582  		objw.Global(lsym, int32(len(ptrmask)), obj.DUPOK|obj.RODATA|obj.LOCAL)
  1583  		lsym.Set(obj.AttrContentAddressable, true)
  1584  	}
  1585  	return lsym
  1586  }
  1587  
  1588  // fillptrmask fills in ptrmask with 1s corresponding to the
  1589  // word offsets in t that hold pointers.
  1590  // ptrmask is assumed to fit at least types.PtrDataSize(t)/PtrSize bits.
  1591  func fillptrmask(t *types.Type, ptrmask []byte) {
  1592  	for i := range ptrmask {
  1593  		ptrmask[i] = 0
  1594  	}
  1595  	if !t.HasPointers() {
  1596  		return
  1597  	}
  1598  
  1599  	vec := bitvec.New(8 * int32(len(ptrmask)))
  1600  	typebits.Set(t, 0, vec)
  1601  
  1602  	nptr := types.PtrDataSize(t) / int64(types.PtrSize)
  1603  	for i := int64(0); i < nptr; i++ {
  1604  		if vec.Get(int32(i)) {
  1605  			ptrmask[i/8] |= 1 << (uint(i) % 8)
  1606  		}
  1607  	}
  1608  }
  1609  
  1610  // dgcprog emits and returns the symbol containing a GC program for type t
  1611  // along with the size of the data described by the program (in the range
  1612  // [types.PtrDataSize(t), t.Width]).
  1613  // In practice, the size is types.PtrDataSize(t) except for non-trivial arrays.
  1614  // For non-trivial arrays, the program describes the full t.Width size.
  1615  func dgcprog(t *types.Type, write bool) (*obj.LSym, int64) {
  1616  	types.CalcSize(t)
  1617  	if t.Size() == types.BADWIDTH {
  1618  		base.Fatalf("dgcprog: %v badwidth", t)
  1619  	}
  1620  	lsym := TypeLinksymPrefix(".gcprog", t)
  1621  	var p gcProg
  1622  	p.init(lsym, write)
  1623  	p.emit(t, 0)
  1624  	offset := p.w.BitIndex() * int64(types.PtrSize)
  1625  	p.end()
  1626  	if ptrdata := types.PtrDataSize(t); offset < ptrdata || offset > t.Size() {
  1627  		base.Fatalf("dgcprog: %v: offset=%d but ptrdata=%d size=%d", t, offset, ptrdata, t.Size())
  1628  	}
  1629  	return lsym, offset
  1630  }
  1631  
  1632  type gcProg struct {
  1633  	lsym   *obj.LSym
  1634  	symoff int
  1635  	w      gcprog.Writer
  1636  	write  bool
  1637  }
  1638  
  1639  func (p *gcProg) init(lsym *obj.LSym, write bool) {
  1640  	p.lsym = lsym
  1641  	p.write = write && !lsym.OnList()
  1642  	p.symoff = 4 // first 4 bytes hold program length
  1643  	if !write {
  1644  		p.w.Init(func(byte) {})
  1645  		return
  1646  	}
  1647  	p.w.Init(p.writeByte)
  1648  	if base.Debug.GCProg > 0 {
  1649  		fmt.Fprintf(os.Stderr, "compile: start GCProg for %v\n", lsym)
  1650  		p.w.Debug(os.Stderr)
  1651  	}
  1652  }
  1653  
  1654  func (p *gcProg) writeByte(x byte) {
  1655  	p.symoff = objw.Uint8(p.lsym, p.symoff, x)
  1656  }
  1657  
  1658  func (p *gcProg) end() {
  1659  	p.w.End()
  1660  	if !p.write {
  1661  		return
  1662  	}
  1663  	objw.Uint32(p.lsym, 0, uint32(p.symoff-4))
  1664  	objw.Global(p.lsym, int32(p.symoff), obj.DUPOK|obj.RODATA|obj.LOCAL)
  1665  	p.lsym.Set(obj.AttrContentAddressable, true)
  1666  	if base.Debug.GCProg > 0 {
  1667  		fmt.Fprintf(os.Stderr, "compile: end GCProg for %v\n", p.lsym)
  1668  	}
  1669  }
  1670  
  1671  func (p *gcProg) emit(t *types.Type, offset int64) {
  1672  	types.CalcSize(t)
  1673  	if !t.HasPointers() {
  1674  		return
  1675  	}
  1676  	if t.Size() == int64(types.PtrSize) {
  1677  		p.w.Ptr(offset / int64(types.PtrSize))
  1678  		return
  1679  	}
  1680  	switch t.Kind() {
  1681  	default:
  1682  		base.Fatalf("gcProg.emit: unexpected type %v", t)
  1683  
  1684  	case types.TSTRING:
  1685  		p.w.Ptr(offset / int64(types.PtrSize))
  1686  
  1687  	case types.TINTER:
  1688  		// Note: the first word isn't a pointer. See comment in typebits.Set
  1689  		p.w.Ptr(offset/int64(types.PtrSize) + 1)
  1690  
  1691  	case types.TSLICE:
  1692  		p.w.Ptr(offset / int64(types.PtrSize))
  1693  
  1694  	case types.TARRAY:
  1695  		if t.NumElem() == 0 {
  1696  			// should have been handled by haspointers check above
  1697  			base.Fatalf("gcProg.emit: empty array")
  1698  		}
  1699  
  1700  		// Flatten array-of-array-of-array to just a big array by multiplying counts.
  1701  		count := t.NumElem()
  1702  		elem := t.Elem()
  1703  		for elem.IsArray() {
  1704  			count *= elem.NumElem()
  1705  			elem = elem.Elem()
  1706  		}
  1707  
  1708  		if !p.w.ShouldRepeat(elem.Size()/int64(types.PtrSize), count) {
  1709  			// Cheaper to just emit the bits.
  1710  			for i := int64(0); i < count; i++ {
  1711  				p.emit(elem, offset+i*elem.Size())
  1712  			}
  1713  			return
  1714  		}
  1715  		p.emit(elem, offset)
  1716  		p.w.ZeroUntil((offset + elem.Size()) / int64(types.PtrSize))
  1717  		p.w.Repeat(elem.Size()/int64(types.PtrSize), count-1)
  1718  
  1719  	case types.TSTRUCT:
  1720  		for _, t1 := range t.Fields() {
  1721  			p.emit(t1.Type, offset+t1.Offset)
  1722  		}
  1723  	}
  1724  }
  1725  
  1726  // ZeroAddr returns the address of a symbol with at least
  1727  // size bytes of zeros.
  1728  func ZeroAddr(size int64) ir.Node {
  1729  	if size >= 1<<31 {
  1730  		base.Fatalf("map elem too big %d", size)
  1731  	}
  1732  	if ZeroSize < size {
  1733  		ZeroSize = size
  1734  	}
  1735  	lsym := base.PkgLinksym("go:map", "zero", obj.ABI0)
  1736  	x := ir.NewLinksymExpr(base.Pos, lsym, types.Types[types.TUINT8])
  1737  	return typecheck.Expr(typecheck.NodAddr(x))
  1738  }
  1739  
  1740  // NeedEmit reports whether typ is a type that we need to emit code
  1741  // for (e.g., runtime type descriptors, method wrappers).
  1742  func NeedEmit(typ *types.Type) bool {
  1743  	// TODO(mdempsky): Export data should keep track of which anonymous
  1744  	// and instantiated types were emitted, so at least downstream
  1745  	// packages can skip re-emitting them.
  1746  	//
  1747  	// Perhaps we can just generalize the linker-symbol indexing to
  1748  	// track the index of arbitrary types, not just defined types, and
  1749  	// use its presence to detect this. The same idea would work for
  1750  	// instantiated generic functions too.
  1751  
  1752  	switch sym := typ.Sym(); {
  1753  	case writtenByWriteBasicTypes(typ):
  1754  		return base.Ctxt.Pkgpath == "runtime"
  1755  
  1756  	case sym == nil:
  1757  		// Anonymous type; possibly never seen before or ever again.
  1758  		// Need to emit to be safe (however, see TODO above).
  1759  		return true
  1760  
  1761  	case sym.Pkg == types.LocalPkg:
  1762  		// Local defined type; our responsibility.
  1763  		return true
  1764  
  1765  	case typ.IsFullyInstantiated():
  1766  		// Instantiated type; possibly instantiated with unique type arguments.
  1767  		// Need to emit to be safe (however, see TODO above).
  1768  		return true
  1769  
  1770  	case typ.HasShape():
  1771  		// Shape type; need to emit even though it lives in the .shape package.
  1772  		// TODO: make sure the linker deduplicates them (see dupok in writeType above).
  1773  		return true
  1774  
  1775  	default:
  1776  		// Should have been emitted by an imported package.
  1777  		return false
  1778  	}
  1779  }
  1780  
  1781  // Generate a wrapper function to convert from
  1782  // a receiver of type T to a receiver of type U.
  1783  // That is,
  1784  //
  1785  //	func (t T) M() {
  1786  //		...
  1787  //	}
  1788  //
  1789  // already exists; this function generates
  1790  //
  1791  //	func (u U) M() {
  1792  //		u.M()
  1793  //	}
  1794  //
  1795  // where the types T and U are such that u.M() is valid
  1796  // and calls the T.M method.
  1797  // The resulting function is for use in method tables.
  1798  //
  1799  //	rcvr - U
  1800  //	method - M func (t T)(), a TFIELD type struct
  1801  //
  1802  // Also wraps methods on instantiated generic types for use in itab entries.
  1803  // For an instantiated generic type G[int], we generate wrappers like:
  1804  // G[int] pointer shaped:
  1805  //
  1806  //	func (x G[int]) f(arg) {
  1807  //		.inst.G[int].f(dictionary, x, arg)
  1808  //	}
  1809  //
  1810  // G[int] not pointer shaped:
  1811  //
  1812  //	func (x *G[int]) f(arg) {
  1813  //		.inst.G[int].f(dictionary, *x, arg)
  1814  //	}
  1815  //
  1816  // These wrappers are always fully stenciled.
  1817  func methodWrapper(rcvr *types.Type, method *types.Field, forItab bool) *obj.LSym {
  1818  	if forItab && !types.IsDirectIface(rcvr) {
  1819  		rcvr = rcvr.PtrTo()
  1820  	}
  1821  
  1822  	newnam := ir.MethodSym(rcvr, method.Sym)
  1823  	lsym := newnam.Linksym()
  1824  
  1825  	// Unified IR creates its own wrappers.
  1826  	return lsym
  1827  }
  1828  
  1829  var ZeroSize int64
  1830  
  1831  // MarkTypeUsedInInterface marks that type t is converted to an interface.
  1832  // This information is used in the linker in dead method elimination.
  1833  func MarkTypeUsedInInterface(t *types.Type, from *obj.LSym) {
  1834  	if t.HasShape() {
  1835  		// Shape types shouldn't be put in interfaces, so we shouldn't ever get here.
  1836  		base.Fatalf("shape types have no methods %+v", t)
  1837  	}
  1838  	MarkTypeSymUsedInInterface(TypeLinksym(t), from)
  1839  }
  1840  func MarkTypeSymUsedInInterface(tsym *obj.LSym, from *obj.LSym) {
  1841  	// Emit a marker relocation. The linker will know the type is converted
  1842  	// to an interface if "from" is reachable.
  1843  	r := obj.Addrel(from)
  1844  	r.Sym = tsym
  1845  	r.Type = objabi.R_USEIFACE
  1846  }
  1847  
  1848  // MarkUsedIfaceMethod marks that an interface method is used in the current
  1849  // function. n is OCALLINTER node.
  1850  func MarkUsedIfaceMethod(n *ir.CallExpr) {
  1851  	// skip unnamed functions (func _())
  1852  	if ir.CurFunc.LSym == nil {
  1853  		return
  1854  	}
  1855  	dot := n.Fun.(*ir.SelectorExpr)
  1856  	ityp := dot.X.Type()
  1857  	if ityp.HasShape() {
  1858  		// Here we're calling a method on a generic interface. Something like:
  1859  		//
  1860  		// type I[T any] interface { foo() T }
  1861  		// func f[T any](x I[T]) {
  1862  		//     ... = x.foo()
  1863  		// }
  1864  		// f[int](...)
  1865  		// f[string](...)
  1866  		//
  1867  		// In this case, in f we're calling foo on a generic interface.
  1868  		// Which method could that be? Normally we could match the method
  1869  		// both by name and by type. But in this case we don't really know
  1870  		// the type of the method we're calling. It could be func()int
  1871  		// or func()string. So we match on just the function name, instead
  1872  		// of both the name and the type used for the non-generic case below.
  1873  		// TODO: instantiations at least know the shape of the instantiated
  1874  		// type, and the linker could do more complicated matching using
  1875  		// some sort of fuzzy shape matching. For now, only use the name
  1876  		// of the method for matching.
  1877  		r := obj.Addrel(ir.CurFunc.LSym)
  1878  		r.Sym = staticdata.StringSymNoCommon(dot.Sel.Name)
  1879  		r.Type = objabi.R_USENAMEDMETHOD
  1880  		return
  1881  	}
  1882  
  1883  	tsym := TypeLinksym(ityp)
  1884  	r := obj.Addrel(ir.CurFunc.LSym)
  1885  	r.Sym = tsym
  1886  	// dot.Offset() is the method index * PtrSize (the offset of code pointer
  1887  	// in itab).
  1888  	midx := dot.Offset() / int64(types.PtrSize)
  1889  	r.Add = InterfaceMethodOffset(ityp, midx)
  1890  	r.Type = objabi.R_USEIFACEMETHOD
  1891  }
  1892  
  1893  func deref(t *types.Type) *types.Type {
  1894  	if t.IsPtr() {
  1895  		return t.Elem()
  1896  	}
  1897  	return t
  1898  }
  1899  

View as plain text