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  	"internal/buildcfg"
    12  	"slices"
    13  	"sort"
    14  	"strings"
    15  	"sync"
    16  
    17  	"cmd/compile/internal/base"
    18  	"cmd/compile/internal/bitvec"
    19  	"cmd/compile/internal/compare"
    20  	"cmd/compile/internal/ir"
    21  	"cmd/compile/internal/objw"
    22  	"cmd/compile/internal/rttype"
    23  	"cmd/compile/internal/staticdata"
    24  	"cmd/compile/internal/typebits"
    25  	"cmd/compile/internal/typecheck"
    26  	"cmd/compile/internal/types"
    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  func commonSize() int { return int(rttype.Type.Size()) } // Sizeof(runtime._type{})
    59  
    60  func uncommonSize(t *types.Type) int { // Sizeof(runtime.uncommontype{})
    61  	if t.Sym() == nil && len(methods(t)) == 0 {
    62  		return 0
    63  	}
    64  	return int(rttype.UncommonType.Size())
    65  }
    66  
    67  func makefield(name string, t *types.Type) *types.Field {
    68  	sym := (*types.Pkg)(nil).Lookup(name)
    69  	return types.NewField(src.NoXPos, sym, t)
    70  }
    71  
    72  // methods returns the methods of the non-interface type t, sorted by name.
    73  // Generates stub functions as needed.
    74  func methods(t *types.Type) []*typeSig {
    75  	if t.HasShape() {
    76  		// Shape types have no methods.
    77  		return nil
    78  	}
    79  	// method type
    80  	mt := types.ReceiverBaseType(t)
    81  
    82  	if mt == nil {
    83  		return nil
    84  	}
    85  	typecheck.CalcMethods(mt)
    86  
    87  	// make list of methods for t,
    88  	// generating code if necessary.
    89  	var ms []*typeSig
    90  	for _, f := range mt.AllMethods() {
    91  		if f.Sym == nil {
    92  			base.Fatalf("method with no sym on %v", mt)
    93  		}
    94  		if !f.IsMethod() {
    95  			base.Fatalf("non-method on %v method %v %v", mt, f.Sym, f)
    96  		}
    97  		if f.Type.Recv() == nil {
    98  			base.Fatalf("receiver with no type on %v method %v %v", mt, f.Sym, f)
    99  		}
   100  		if f.Nointerface() && !t.IsFullyInstantiated() {
   101  			// Skip creating method wrappers if f is nointerface. But, if
   102  			// t is an instantiated type, we still have to call
   103  			// methodWrapper, because methodWrapper generates the actual
   104  			// generic method on the type as well.
   105  			continue
   106  		}
   107  
   108  		// get receiver type for this particular method.
   109  		// if pointer receiver but non-pointer t and
   110  		// this is not an embedded pointer inside a struct,
   111  		// method does not apply.
   112  		if !types.IsMethodApplicable(t, f) {
   113  			continue
   114  		}
   115  
   116  		sig := &typeSig{
   117  			name:  f.Sym,
   118  			isym:  methodWrapper(t, f, true),
   119  			tsym:  methodWrapper(t, f, false),
   120  			type_: typecheck.NewMethodType(f.Type, t),
   121  			mtype: typecheck.NewMethodType(f.Type, nil),
   122  		}
   123  		if f.Nointerface() {
   124  			// In the case of a nointerface method on an instantiated
   125  			// type, don't actually append the typeSig.
   126  			continue
   127  		}
   128  		ms = append(ms, sig)
   129  	}
   130  
   131  	return ms
   132  }
   133  
   134  // imethods returns the methods of the interface type t, sorted by name.
   135  func imethods(t *types.Type) []*typeSig {
   136  	var methods []*typeSig
   137  	for _, f := range t.AllMethods() {
   138  		if f.Type.Kind() != types.TFUNC || f.Sym == nil {
   139  			continue
   140  		}
   141  		if f.Sym.IsBlank() {
   142  			base.Fatalf("unexpected blank symbol in interface method set")
   143  		}
   144  		if n := len(methods); n > 0 {
   145  			last := methods[n-1]
   146  			if types.CompareSyms(last.name, f.Sym) >= 0 {
   147  				base.Fatalf("sigcmp vs sortinter %v %v", last.name, f.Sym)
   148  			}
   149  		}
   150  
   151  		sig := &typeSig{
   152  			name:  f.Sym,
   153  			mtype: f.Type,
   154  			type_: typecheck.NewMethodType(f.Type, nil),
   155  		}
   156  		methods = append(methods, sig)
   157  
   158  		// NOTE(rsc): Perhaps an oversight that
   159  		// IfaceType.Method is not in the reflect data.
   160  		// Generate the method body, so that compiled
   161  		// code can refer to it.
   162  		methodWrapper(t, f, false)
   163  	}
   164  
   165  	return methods
   166  }
   167  
   168  func dimportpath(p *types.Pkg) {
   169  	if p.Pathsym != nil {
   170  		return
   171  	}
   172  
   173  	if p == types.LocalPkg && base.Ctxt.Pkgpath == "" {
   174  		panic("missing pkgpath")
   175  	}
   176  
   177  	// If we are compiling the runtime package, there are two runtime packages around
   178  	// -- localpkg and Pkgs.Runtime. We don't want to produce import path symbols for
   179  	// both of them, so just produce one for localpkg.
   180  	if base.Ctxt.Pkgpath == "runtime" && p == ir.Pkgs.Runtime {
   181  		return
   182  	}
   183  
   184  	s := base.Ctxt.Lookup("type:.importpath." + p.Prefix + ".")
   185  	ot := dnameData(s, 0, p.Path, "", nil, false, false)
   186  	objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA)
   187  	s.Set(obj.AttrContentAddressable, true)
   188  	p.Pathsym = s
   189  }
   190  
   191  func dgopkgpath(c rttype.Cursor, pkg *types.Pkg) {
   192  	c = c.Field("Bytes")
   193  	if pkg == nil {
   194  		c.WritePtr(nil)
   195  		return
   196  	}
   197  
   198  	dimportpath(pkg)
   199  	c.WritePtr(pkg.Pathsym)
   200  }
   201  
   202  // dgopkgpathOff writes an offset relocation to the pkg path symbol to c.
   203  func dgopkgpathOff(c rttype.Cursor, pkg *types.Pkg) {
   204  	if pkg == nil {
   205  		c.WriteInt32(0)
   206  		return
   207  	}
   208  
   209  	dimportpath(pkg)
   210  	c.WriteSymPtrOff(pkg.Pathsym, false)
   211  }
   212  
   213  // dnameField dumps a reflect.name for a struct field.
   214  func dnameField(c rttype.Cursor, spkg *types.Pkg, ft *types.Field) {
   215  	if !types.IsExported(ft.Sym.Name) && ft.Sym.Pkg != spkg {
   216  		base.Fatalf("package mismatch for %v", ft.Sym)
   217  	}
   218  	nsym := dname(ft.Sym.Name, ft.Note, nil, types.IsExported(ft.Sym.Name), ft.Embedded != 0)
   219  	c.Field("Bytes").WritePtr(nsym)
   220  }
   221  
   222  // dnameData writes the contents of a reflect.name into s at offset ot.
   223  func dnameData(s *obj.LSym, ot int, name, tag string, pkg *types.Pkg, exported, embedded bool) int {
   224  	if len(name) >= 1<<29 {
   225  		base.Fatalf("name too long: %d %s...", len(name), name[:1024])
   226  	}
   227  	if len(tag) >= 1<<29 {
   228  		base.Fatalf("tag too long: %d %s...", len(tag), tag[:1024])
   229  	}
   230  	var nameLen [binary.MaxVarintLen64]byte
   231  	nameLenLen := binary.PutUvarint(nameLen[:], uint64(len(name)))
   232  	var tagLen [binary.MaxVarintLen64]byte
   233  	tagLenLen := binary.PutUvarint(tagLen[:], uint64(len(tag)))
   234  
   235  	// Encode name and tag. See reflect/type.go for details.
   236  	var bits byte
   237  	l := 1 + nameLenLen + len(name)
   238  	if exported {
   239  		bits |= 1 << 0
   240  	}
   241  	if len(tag) > 0 {
   242  		l += tagLenLen + len(tag)
   243  		bits |= 1 << 1
   244  	}
   245  	if pkg != nil {
   246  		bits |= 1 << 2
   247  	}
   248  	if embedded {
   249  		bits |= 1 << 3
   250  	}
   251  	b := make([]byte, l)
   252  	b[0] = bits
   253  	copy(b[1:], nameLen[:nameLenLen])
   254  	copy(b[1+nameLenLen:], name)
   255  	if len(tag) > 0 {
   256  		tb := b[1+nameLenLen+len(name):]
   257  		copy(tb, tagLen[:tagLenLen])
   258  		copy(tb[tagLenLen:], tag)
   259  	}
   260  
   261  	ot = int(s.WriteBytes(base.Ctxt, int64(ot), b))
   262  
   263  	if pkg != nil {
   264  		c := rttype.NewCursor(s, int64(ot), types.Types[types.TUINT32])
   265  		dgopkgpathOff(c, pkg)
   266  		ot += 4
   267  	}
   268  
   269  	return ot
   270  }
   271  
   272  var dnameCount int
   273  
   274  // dname creates a reflect.name for a struct field or method.
   275  func dname(name, tag string, pkg *types.Pkg, exported, embedded bool) *obj.LSym {
   276  	// Write out data as "type:." to signal two things to the
   277  	// linker, first that when dynamically linking, the symbol
   278  	// should be moved to a relro section, and second that the
   279  	// contents should not be decoded as a type.
   280  	sname := "type:.namedata."
   281  	if pkg == nil {
   282  		// In the common case, share data with other packages.
   283  		if name == "" {
   284  			if exported {
   285  				sname += "-noname-exported." + tag
   286  			} else {
   287  				sname += "-noname-unexported." + tag
   288  			}
   289  		} else {
   290  			if exported {
   291  				sname += name + "." + tag
   292  			} else {
   293  				sname += name + "-" + tag
   294  			}
   295  		}
   296  	} else {
   297  		// TODO(mdempsky): We should be able to share these too (except
   298  		// maybe when dynamic linking).
   299  		sname = fmt.Sprintf("%s%s.%d", sname, types.LocalPkg.Prefix, dnameCount)
   300  		dnameCount++
   301  	}
   302  	if embedded {
   303  		sname += ".embedded"
   304  	}
   305  	s := base.Ctxt.Lookup(sname)
   306  	if len(s.P) > 0 {
   307  		return s
   308  	}
   309  	ot := dnameData(s, 0, name, tag, pkg, exported, embedded)
   310  	objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA)
   311  	s.Set(obj.AttrContentAddressable, true)
   312  	return s
   313  }
   314  
   315  // dextratype dumps the fields of a runtime.uncommontype.
   316  // dataAdd is the offset in bytes after the header where the
   317  // backing array of the []method field should be written.
   318  func dextratype(lsym *obj.LSym, off int64, t *types.Type, dataAdd int) {
   319  	m := methods(t)
   320  	if t.Sym() == nil && len(m) == 0 {
   321  		base.Fatalf("extra requested of type with no extra info %v", t)
   322  	}
   323  	noff := types.RoundUp(off, int64(types.PtrSize))
   324  	if noff != off {
   325  		base.Fatalf("unexpected alignment in dextratype for %v", t)
   326  	}
   327  
   328  	for _, a := range m {
   329  		writeType(a.type_)
   330  	}
   331  
   332  	c := rttype.NewCursor(lsym, off, rttype.UncommonType)
   333  	dgopkgpathOff(c.Field("PkgPath"), typePkg(t))
   334  
   335  	dataAdd += uncommonSize(t)
   336  	mcount := len(m)
   337  	if mcount != int(uint16(mcount)) {
   338  		base.Fatalf("too many methods on %v: %d", t, mcount)
   339  	}
   340  	xcount := sort.Search(mcount, func(i int) bool { return !types.IsExported(m[i].name.Name) })
   341  	if dataAdd != int(uint32(dataAdd)) {
   342  		base.Fatalf("methods are too far away on %v: %d", t, dataAdd)
   343  	}
   344  
   345  	c.Field("Mcount").WriteUint16(uint16(mcount))
   346  	c.Field("Xcount").WriteUint16(uint16(xcount))
   347  	c.Field("Moff").WriteUint32(uint32(dataAdd))
   348  	// Note: there is an unused uint32 field here.
   349  
   350  	// Write the backing array for the []method field.
   351  	array := rttype.NewArrayCursor(lsym, off+int64(dataAdd), rttype.Method, mcount)
   352  	for i, a := range m {
   353  		exported := types.IsExported(a.name.Name)
   354  		var pkg *types.Pkg
   355  		if !exported && a.name.Pkg != typePkg(t) {
   356  			pkg = a.name.Pkg
   357  		}
   358  		nsym := dname(a.name.Name, "", pkg, exported, false)
   359  
   360  		e := array.Elem(i)
   361  		e.Field("Name").WriteSymPtrOff(nsym, false)
   362  		dmethodptrOff(e.Field("Mtyp"), writeType(a.mtype))
   363  		dmethodptrOff(e.Field("Ifn"), a.isym)
   364  		dmethodptrOff(e.Field("Tfn"), a.tsym)
   365  	}
   366  }
   367  
   368  func typePkg(t *types.Type) *types.Pkg {
   369  	tsym := t.Sym()
   370  	if tsym == nil {
   371  		switch t.Kind() {
   372  		case types.TARRAY, types.TSLICE, types.TPTR, types.TCHAN:
   373  			if t.Elem() != nil {
   374  				tsym = t.Elem().Sym()
   375  			}
   376  		}
   377  	}
   378  	if tsym != nil && tsym.Pkg != types.BuiltinPkg {
   379  		return tsym.Pkg
   380  	}
   381  	return nil
   382  }
   383  
   384  func dmethodptrOff(c rttype.Cursor, x *obj.LSym) {
   385  	c.WriteInt32(0)
   386  	c.Reloc(obj.Reloc{Type: objabi.R_METHODOFF, Sym: x})
   387  }
   388  
   389  var kinds = []abi.Kind{
   390  	types.TINT:        abi.Int,
   391  	types.TUINT:       abi.Uint,
   392  	types.TINT8:       abi.Int8,
   393  	types.TUINT8:      abi.Uint8,
   394  	types.TINT16:      abi.Int16,
   395  	types.TUINT16:     abi.Uint16,
   396  	types.TINT32:      abi.Int32,
   397  	types.TUINT32:     abi.Uint32,
   398  	types.TINT64:      abi.Int64,
   399  	types.TUINT64:     abi.Uint64,
   400  	types.TUINTPTR:    abi.Uintptr,
   401  	types.TFLOAT32:    abi.Float32,
   402  	types.TFLOAT64:    abi.Float64,
   403  	types.TBOOL:       abi.Bool,
   404  	types.TSTRING:     abi.String,
   405  	types.TPTR:        abi.Pointer,
   406  	types.TSTRUCT:     abi.Struct,
   407  	types.TINTER:      abi.Interface,
   408  	types.TCHAN:       abi.Chan,
   409  	types.TMAP:        abi.Map,
   410  	types.TARRAY:      abi.Array,
   411  	types.TSLICE:      abi.Slice,
   412  	types.TFUNC:       abi.Func,
   413  	types.TCOMPLEX64:  abi.Complex64,
   414  	types.TCOMPLEX128: abi.Complex128,
   415  	types.TUNSAFEPTR:  abi.UnsafePointer,
   416  }
   417  
   418  var (
   419  	memhashvarlen  *obj.LSym
   420  	memequalvarlen *obj.LSym
   421  )
   422  
   423  // dcommontype dumps the contents of a reflect.rtype (runtime._type) to c.
   424  func dcommontype(c rttype.Cursor, t *types.Type) {
   425  	types.CalcSize(t)
   426  	eqfunc := geneq(t)
   427  
   428  	sptrWeak := true
   429  	var sptr *obj.LSym
   430  	if !t.IsPtr() || t.IsPtrElem() {
   431  		tptr := types.NewPtr(t)
   432  		if t.Sym() != nil || methods(tptr) != nil {
   433  			sptrWeak = false
   434  		}
   435  		sptr = writeType(tptr)
   436  	}
   437  
   438  	gcsym, onDemand, ptrdata := dgcsym(t, true, true)
   439  	if !onDemand {
   440  		delete(gcsymset, t)
   441  	}
   442  
   443  	// ../../../../reflect/type.go:/^type.rtype
   444  	// actual type structure
   445  	//	type rtype struct {
   446  	//		size          uintptr
   447  	//		ptrdata       uintptr
   448  	//		hash          uint32
   449  	//		tflag         tflag
   450  	//		align         uint8
   451  	//		fieldAlign    uint8
   452  	//		kind          uint8
   453  	//		equal         func(unsafe.Pointer, unsafe.Pointer) bool
   454  	//		gcdata        *byte
   455  	//		str           nameOff
   456  	//		ptrToThis     typeOff
   457  	//	}
   458  	c.Field("Size_").WriteUintptr(uint64(t.Size()))
   459  	c.Field("PtrBytes").WriteUintptr(uint64(ptrdata))
   460  	c.Field("Hash").WriteUint32(types.TypeHash(t))
   461  
   462  	var tflag abi.TFlag
   463  	if uncommonSize(t) != 0 {
   464  		tflag |= abi.TFlagUncommon
   465  	}
   466  	if t.Sym() != nil && t.Sym().Name != "" {
   467  		tflag |= abi.TFlagNamed
   468  	}
   469  	if compare.IsRegularMemory(t) {
   470  		tflag |= abi.TFlagRegularMemory
   471  	}
   472  	if onDemand {
   473  		tflag |= abi.TFlagGCMaskOnDemand
   474  	}
   475  
   476  	exported := false
   477  	p := t.NameString()
   478  	// If we're writing out type T,
   479  	// we are very likely to write out type *T as well.
   480  	// Use the string "*T"[1:] for "T", so that the two
   481  	// share storage. This is a cheap way to reduce the
   482  	// amount of space taken up by reflect strings.
   483  	if !strings.HasPrefix(p, "*") {
   484  		p = "*" + p
   485  		tflag |= abi.TFlagExtraStar
   486  		if t.Sym() != nil {
   487  			exported = types.IsExported(t.Sym().Name)
   488  		}
   489  	} else {
   490  		if t.Elem() != nil && t.Elem().Sym() != nil {
   491  			exported = types.IsExported(t.Elem().Sym().Name)
   492  		}
   493  	}
   494  
   495  	if tflag != abi.TFlag(uint8(tflag)) {
   496  		// this should optimize away completely
   497  		panic("Unexpected change in size of abi.TFlag")
   498  	}
   499  	c.Field("TFlag").WriteUint8(uint8(tflag))
   500  
   501  	// runtime (and common sense) expects alignment to be a power of two.
   502  	i := int(uint8(t.Alignment()))
   503  
   504  	if i == 0 {
   505  		i = 1
   506  	}
   507  	if i&(i-1) != 0 {
   508  		base.Fatalf("invalid alignment %d for %v", uint8(t.Alignment()), t)
   509  	}
   510  	c.Field("Align_").WriteUint8(uint8(t.Alignment()))
   511  	c.Field("FieldAlign_").WriteUint8(uint8(t.Alignment()))
   512  
   513  	kind := kinds[t.Kind()]
   514  	if types.IsDirectIface(t) {
   515  		kind |= abi.KindDirectIface
   516  	}
   517  	c.Field("Kind_").WriteUint8(uint8(kind))
   518  
   519  	c.Field("Equal").WritePtr(eqfunc)
   520  	c.Field("GCData").WritePtr(gcsym)
   521  
   522  	nsym := dname(p, "", nil, exported, false)
   523  	c.Field("Str").WriteSymPtrOff(nsym, false)
   524  	c.Field("PtrToThis").WriteSymPtrOff(sptr, sptrWeak)
   525  }
   526  
   527  // TrackSym returns the symbol for tracking use of field/method f, assumed
   528  // to be a member of struct/interface type t.
   529  func TrackSym(t *types.Type, f *types.Field) *obj.LSym {
   530  	return base.PkgLinksym("go:track", t.LinkString()+"."+f.Sym.Name, obj.ABI0)
   531  }
   532  
   533  func TypeSymPrefix(prefix string, t *types.Type) *types.Sym {
   534  	p := prefix + "." + t.LinkString()
   535  	s := types.TypeSymLookup(p)
   536  
   537  	// This function is for looking up type-related generated functions
   538  	// (e.g. eq and hash). Make sure they are indeed generated.
   539  	signatmu.Lock()
   540  	NeedRuntimeType(t)
   541  	signatmu.Unlock()
   542  
   543  	//print("algsym: %s -> %+S\n", p, s);
   544  
   545  	return s
   546  }
   547  
   548  func TypeSym(t *types.Type) *types.Sym {
   549  	if t == nil || (t.IsPtr() && t.Elem() == nil) || t.IsUntyped() {
   550  		base.Fatalf("TypeSym %v", t)
   551  	}
   552  	if t.Kind() == types.TFUNC && t.Recv() != nil {
   553  		base.Fatalf("misuse of method type: %v", t)
   554  	}
   555  	s := types.TypeSym(t)
   556  	signatmu.Lock()
   557  	NeedRuntimeType(t)
   558  	signatmu.Unlock()
   559  	return s
   560  }
   561  
   562  func TypeLinksymPrefix(prefix string, t *types.Type) *obj.LSym {
   563  	return TypeSymPrefix(prefix, t).Linksym()
   564  }
   565  
   566  func TypeLinksymLookup(name string) *obj.LSym {
   567  	return types.TypeSymLookup(name).Linksym()
   568  }
   569  
   570  func TypeLinksym(t *types.Type) *obj.LSym {
   571  	lsym := TypeSym(t).Linksym()
   572  	signatmu.Lock()
   573  	if lsym.Extra == nil {
   574  		ti := lsym.NewTypeInfo()
   575  		ti.Type = t
   576  	}
   577  	signatmu.Unlock()
   578  	return lsym
   579  }
   580  
   581  // TypePtrAt returns an expression that evaluates to the
   582  // *runtime._type value for t.
   583  func TypePtrAt(pos src.XPos, t *types.Type) *ir.AddrExpr {
   584  	return typecheck.LinksymAddr(pos, TypeLinksym(t), types.Types[types.TUINT8])
   585  }
   586  
   587  // ITabLsym returns the LSym representing the itab for concrete type typ implementing
   588  // interface iface. A dummy tab will be created in the unusual case where typ doesn't
   589  // implement iface. Normally, this wouldn't happen, because the typechecker would
   590  // have reported a compile-time error. This situation can only happen when the
   591  // destination type of a type assert or a type in a type switch is parameterized, so
   592  // it may sometimes, but not always, be a type that can't implement the specified
   593  // interface.
   594  func ITabLsym(typ, iface *types.Type) *obj.LSym {
   595  	return itabLsym(typ, iface, true)
   596  }
   597  
   598  func itabLsym(typ, iface *types.Type, allowNonImplement bool) *obj.LSym {
   599  	s, existed := ir.Pkgs.Itab.LookupOK(typ.LinkString() + "," + iface.LinkString())
   600  	lsym := s.Linksym()
   601  	signatmu.Lock()
   602  	if lsym.Extra == nil {
   603  		ii := lsym.NewItabInfo()
   604  		ii.Type = typ
   605  	}
   606  	signatmu.Unlock()
   607  
   608  	if !existed {
   609  		writeITab(lsym, typ, iface, allowNonImplement)
   610  	}
   611  	return lsym
   612  }
   613  
   614  // ITabAddrAt returns an expression that evaluates to the
   615  // *runtime.itab value for concrete type typ implementing interface
   616  // iface.
   617  func ITabAddrAt(pos src.XPos, typ, iface *types.Type) *ir.AddrExpr {
   618  	lsym := itabLsym(typ, iface, false)
   619  	return typecheck.LinksymAddr(pos, lsym, types.Types[types.TUINT8])
   620  }
   621  
   622  // needkeyupdate reports whether map updates with t as a key
   623  // need the key to be updated.
   624  func needkeyupdate(t *types.Type) bool {
   625  	switch t.Kind() {
   626  	case types.TBOOL, types.TINT, types.TUINT, types.TINT8, types.TUINT8, types.TINT16, types.TUINT16, types.TINT32, types.TUINT32,
   627  		types.TINT64, types.TUINT64, types.TUINTPTR, types.TPTR, types.TUNSAFEPTR, types.TCHAN:
   628  		return false
   629  
   630  	case types.TFLOAT32, types.TFLOAT64, types.TCOMPLEX64, types.TCOMPLEX128, // floats and complex can be +0/-0
   631  		types.TINTER,
   632  		types.TSTRING: // strings might have smaller backing stores
   633  		return true
   634  
   635  	case types.TARRAY:
   636  		return needkeyupdate(t.Elem())
   637  
   638  	case types.TSTRUCT:
   639  		for _, t1 := range t.Fields() {
   640  			if needkeyupdate(t1.Type) {
   641  				return true
   642  			}
   643  		}
   644  		return false
   645  
   646  	default:
   647  		base.Fatalf("bad type for map key: %v", t)
   648  		return true
   649  	}
   650  }
   651  
   652  // hashMightPanic reports whether the hash of a map key of type t might panic.
   653  func hashMightPanic(t *types.Type) bool {
   654  	switch t.Kind() {
   655  	case types.TINTER:
   656  		return true
   657  
   658  	case types.TARRAY:
   659  		return hashMightPanic(t.Elem())
   660  
   661  	case types.TSTRUCT:
   662  		for _, t1 := range t.Fields() {
   663  			if hashMightPanic(t1.Type) {
   664  				return true
   665  			}
   666  		}
   667  		return false
   668  
   669  	default:
   670  		return false
   671  	}
   672  }
   673  
   674  // formalType replaces predeclared aliases with real types.
   675  // They've been separate internally to make error messages
   676  // better, but we have to merge them in the reflect tables.
   677  func formalType(t *types.Type) *types.Type {
   678  	switch t {
   679  	case types.AnyType, types.ByteType, types.RuneType:
   680  		return types.Types[t.Kind()]
   681  	}
   682  	return t
   683  }
   684  
   685  func writeType(t *types.Type) *obj.LSym {
   686  	t = formalType(t)
   687  	if t.IsUntyped() {
   688  		base.Fatalf("writeType %v", t)
   689  	}
   690  
   691  	s := types.TypeSym(t)
   692  	lsym := s.Linksym()
   693  
   694  	// special case (look for runtime below):
   695  	// when compiling package runtime,
   696  	// emit the type structures for int, float, etc.
   697  	tbase := t
   698  	if t.IsPtr() && t.Sym() == nil && t.Elem().Sym() != nil {
   699  		tbase = t.Elem()
   700  	}
   701  	if tbase.Kind() == types.TFORW {
   702  		base.Fatalf("unresolved defined type: %v", tbase)
   703  	}
   704  
   705  	// This is a fake type we generated for our builtin pseudo-runtime
   706  	// package. We'll emit a description for the real type while
   707  	// compiling package runtime, so we don't need or want to emit one
   708  	// from this fake type.
   709  	if sym := tbase.Sym(); sym != nil && sym.Pkg == ir.Pkgs.Runtime {
   710  		return lsym
   711  	}
   712  
   713  	if s.Siggen() {
   714  		return lsym
   715  	}
   716  	s.SetSiggen(true)
   717  
   718  	if !NeedEmit(tbase) {
   719  		if i := typecheck.BaseTypeIndex(t); i >= 0 {
   720  			lsym.Pkg = tbase.Sym().Pkg.Prefix
   721  			lsym.SymIdx = int32(i)
   722  			lsym.Set(obj.AttrIndexed, true)
   723  		}
   724  
   725  		// TODO(mdempsky): Investigate whether this still happens.
   726  		// If we know we don't need to emit code for a type,
   727  		// we should have a link-symbol index for it.
   728  		// See also TODO in NeedEmit.
   729  		return lsym
   730  	}
   731  
   732  	// Type layout                          Written by               Marker
   733  	// +--------------------------------+                            - 0
   734  	// | abi/internal.Type              |   dcommontype
   735  	// +--------------------------------+                            - A
   736  	// | additional type-dependent      |   code in the switch below
   737  	// | fields, e.g.                   |
   738  	// | abi/internal.ArrayType.Len     |
   739  	// +--------------------------------+                            - B
   740  	// | internal/abi.UncommonType      |   dextratype
   741  	// | This section is optional,      |
   742  	// | if type has a name or methods  |
   743  	// +--------------------------------+                            - C
   744  	// | variable-length data           |   code in the switch below
   745  	// | referenced by                  |
   746  	// | type-dependent fields, e.g.    |
   747  	// | abi/internal.StructType.Fields |
   748  	// | dataAdd = size of this section |
   749  	// +--------------------------------+                            - D
   750  	// | method list, if any            |   dextratype
   751  	// +--------------------------------+                            - E
   752  
   753  	// UncommonType section is included if we have a name or a method.
   754  	extra := t.Sym() != nil || len(methods(t)) != 0
   755  
   756  	// Decide the underlying type of the descriptor, and remember
   757  	// the size we need for variable-length data.
   758  	var rt *types.Type
   759  	dataAdd := 0
   760  	switch t.Kind() {
   761  	default:
   762  		rt = rttype.Type
   763  	case types.TARRAY:
   764  		rt = rttype.ArrayType
   765  	case types.TSLICE:
   766  		rt = rttype.SliceType
   767  	case types.TCHAN:
   768  		rt = rttype.ChanType
   769  	case types.TFUNC:
   770  		rt = rttype.FuncType
   771  		dataAdd = (t.NumRecvs() + t.NumParams() + t.NumResults()) * types.PtrSize
   772  	case types.TINTER:
   773  		rt = rttype.InterfaceType
   774  		dataAdd = len(imethods(t)) * int(rttype.IMethod.Size())
   775  	case types.TMAP:
   776  		if buildcfg.Experiment.SwissMap {
   777  			rt = rttype.SwissMapType
   778  		} else {
   779  			rt = rttype.OldMapType
   780  		}
   781  	case types.TPTR:
   782  		rt = rttype.PtrType
   783  		// TODO: use rttype.Type for Elem() is ANY?
   784  	case types.TSTRUCT:
   785  		rt = rttype.StructType
   786  		dataAdd = t.NumFields() * int(rttype.StructField.Size())
   787  	}
   788  
   789  	// Compute offsets of each section.
   790  	B := rt.Size()
   791  	C := B
   792  	if extra {
   793  		C = B + rttype.UncommonType.Size()
   794  	}
   795  	D := C + int64(dataAdd)
   796  	E := D + int64(len(methods(t)))*rttype.Method.Size()
   797  
   798  	// Write the runtime._type
   799  	c := rttype.NewCursor(lsym, 0, rt)
   800  	if rt == rttype.Type {
   801  		dcommontype(c, t)
   802  	} else {
   803  		dcommontype(c.Field("Type"), t)
   804  	}
   805  
   806  	// Write additional type-specific data
   807  	// (Both the fixed size and variable-sized sections.)
   808  	switch t.Kind() {
   809  	case types.TARRAY:
   810  		// internal/abi.ArrayType
   811  		s1 := writeType(t.Elem())
   812  		t2 := types.NewSlice(t.Elem())
   813  		s2 := writeType(t2)
   814  		c.Field("Elem").WritePtr(s1)
   815  		c.Field("Slice").WritePtr(s2)
   816  		c.Field("Len").WriteUintptr(uint64(t.NumElem()))
   817  
   818  	case types.TSLICE:
   819  		// internal/abi.SliceType
   820  		s1 := writeType(t.Elem())
   821  		c.Field("Elem").WritePtr(s1)
   822  
   823  	case types.TCHAN:
   824  		// internal/abi.ChanType
   825  		s1 := writeType(t.Elem())
   826  		c.Field("Elem").WritePtr(s1)
   827  		c.Field("Dir").WriteInt(int64(t.ChanDir()))
   828  
   829  	case types.TFUNC:
   830  		// internal/abi.FuncType
   831  		for _, t1 := range t.RecvParamsResults() {
   832  			writeType(t1.Type)
   833  		}
   834  		inCount := t.NumRecvs() + t.NumParams()
   835  		outCount := t.NumResults()
   836  		if t.IsVariadic() {
   837  			outCount |= 1 << 15
   838  		}
   839  
   840  		c.Field("InCount").WriteUint16(uint16(inCount))
   841  		c.Field("OutCount").WriteUint16(uint16(outCount))
   842  
   843  		// Array of rtype pointers follows funcType.
   844  		typs := t.RecvParamsResults()
   845  		array := rttype.NewArrayCursor(lsym, C, types.Types[types.TUNSAFEPTR], len(typs))
   846  		for i, t1 := range typs {
   847  			array.Elem(i).WritePtr(writeType(t1.Type))
   848  		}
   849  
   850  	case types.TINTER:
   851  		// internal/abi.InterfaceType
   852  		m := imethods(t)
   853  		n := len(m)
   854  		for _, a := range m {
   855  			writeType(a.type_)
   856  		}
   857  
   858  		var tpkg *types.Pkg
   859  		if t.Sym() != nil && t != types.Types[t.Kind()] && t != types.ErrorType {
   860  			tpkg = t.Sym().Pkg
   861  		}
   862  		dgopkgpath(c.Field("PkgPath"), tpkg)
   863  		c.Field("Methods").WriteSlice(lsym, C, int64(n), int64(n))
   864  
   865  		array := rttype.NewArrayCursor(lsym, C, rttype.IMethod, n)
   866  		for i, a := range m {
   867  			exported := types.IsExported(a.name.Name)
   868  			var pkg *types.Pkg
   869  			if !exported && a.name.Pkg != tpkg {
   870  				pkg = a.name.Pkg
   871  			}
   872  			nsym := dname(a.name.Name, "", pkg, exported, false)
   873  
   874  			e := array.Elem(i)
   875  			e.Field("Name").WriteSymPtrOff(nsym, false)
   876  			e.Field("Typ").WriteSymPtrOff(writeType(a.type_), false)
   877  		}
   878  
   879  	case types.TMAP:
   880  		if buildcfg.Experiment.SwissMap {
   881  			writeSwissMapType(t, lsym, c)
   882  		} else {
   883  			writeOldMapType(t, lsym, c)
   884  		}
   885  
   886  	case types.TPTR:
   887  		// internal/abi.PtrType
   888  		if t.Elem().Kind() == types.TANY {
   889  			base.Fatalf("bad pointer base type")
   890  		}
   891  
   892  		s1 := writeType(t.Elem())
   893  		c.Field("Elem").WritePtr(s1)
   894  
   895  	case types.TSTRUCT:
   896  		// internal/abi.StructType
   897  		fields := t.Fields()
   898  		for _, t1 := range fields {
   899  			writeType(t1.Type)
   900  		}
   901  
   902  		// All non-exported struct field names within a struct
   903  		// type must originate from a single package. By
   904  		// identifying and recording that package within the
   905  		// struct type descriptor, we can omit that
   906  		// information from the field descriptors.
   907  		var spkg *types.Pkg
   908  		for _, f := range fields {
   909  			if !types.IsExported(f.Sym.Name) {
   910  				spkg = f.Sym.Pkg
   911  				break
   912  			}
   913  		}
   914  
   915  		dgopkgpath(c.Field("PkgPath"), spkg)
   916  		c.Field("Fields").WriteSlice(lsym, C, int64(len(fields)), int64(len(fields)))
   917  
   918  		array := rttype.NewArrayCursor(lsym, C, rttype.StructField, len(fields))
   919  		for i, f := range fields {
   920  			e := array.Elem(i)
   921  			dnameField(e.Field("Name"), spkg, f)
   922  			e.Field("Typ").WritePtr(writeType(f.Type))
   923  			e.Field("Offset").WriteUintptr(uint64(f.Offset))
   924  		}
   925  	}
   926  
   927  	// Write the extra info, if any.
   928  	if extra {
   929  		dextratype(lsym, B, t, dataAdd)
   930  	}
   931  
   932  	// Note: DUPOK is required to ensure that we don't end up with more
   933  	// than one type descriptor for a given type, if the type descriptor
   934  	// can be defined in multiple packages, that is, unnamed types,
   935  	// instantiated types and shape types.
   936  	dupok := 0
   937  	if tbase.Sym() == nil || tbase.IsFullyInstantiated() || tbase.HasShape() {
   938  		dupok = obj.DUPOK
   939  	}
   940  
   941  	objw.Global(lsym, int32(E), int16(dupok|obj.RODATA))
   942  
   943  	// The linker will leave a table of all the typelinks for
   944  	// types in the binary, so the runtime can find them.
   945  	//
   946  	// When buildmode=shared, all types are in typelinks so the
   947  	// runtime can deduplicate type pointers.
   948  	keep := base.Ctxt.Flag_dynlink
   949  	if !keep && t.Sym() == nil {
   950  		// For an unnamed type, we only need the link if the type can
   951  		// be created at run time by reflect.PointerTo and similar
   952  		// functions. If the type exists in the program, those
   953  		// functions must return the existing type structure rather
   954  		// than creating a new one.
   955  		switch t.Kind() {
   956  		case types.TPTR, types.TARRAY, types.TCHAN, types.TFUNC, types.TMAP, types.TSLICE, types.TSTRUCT:
   957  			keep = true
   958  		}
   959  	}
   960  	// Do not put Noalg types in typelinks.  See issue #22605.
   961  	if types.TypeHasNoAlg(t) {
   962  		keep = false
   963  	}
   964  	lsym.Set(obj.AttrMakeTypelink, keep)
   965  
   966  	return lsym
   967  }
   968  
   969  // InterfaceMethodOffset returns the offset of the i-th method in the interface
   970  // type descriptor, ityp.
   971  func InterfaceMethodOffset(ityp *types.Type, i int64) int64 {
   972  	// interface type descriptor layout is struct {
   973  	//   _type        // commonSize
   974  	//   pkgpath      // 1 word
   975  	//   []imethod    // 3 words (pointing to [...]imethod below)
   976  	//   uncommontype // uncommonSize
   977  	//   [...]imethod
   978  	// }
   979  	// The size of imethod is 8.
   980  	return int64(commonSize()+4*types.PtrSize+uncommonSize(ityp)) + i*8
   981  }
   982  
   983  // NeedRuntimeType ensures that a runtime type descriptor is emitted for t.
   984  func NeedRuntimeType(t *types.Type) {
   985  	if _, ok := signatset[t]; !ok {
   986  		signatset[t] = struct{}{}
   987  		signatslice = append(signatslice, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
   988  	}
   989  }
   990  
   991  func WriteRuntimeTypes() {
   992  	// Process signatslice. Use a loop, as writeType adds
   993  	// entries to signatslice while it is being processed.
   994  	for len(signatslice) > 0 {
   995  		signats := signatslice
   996  		// Sort for reproducible builds.
   997  		slices.SortFunc(signats, typesStrCmp)
   998  		for _, ts := range signats {
   999  			t := ts.t
  1000  			writeType(t)
  1001  			if t.Sym() != nil {
  1002  				writeType(types.NewPtr(t))
  1003  			}
  1004  		}
  1005  		signatslice = signatslice[len(signats):]
  1006  	}
  1007  }
  1008  
  1009  func WriteGCSymbols() {
  1010  	// Emit GC data symbols.
  1011  	gcsyms := make([]typeAndStr, 0, len(gcsymset))
  1012  	for t := range gcsymset {
  1013  		gcsyms = append(gcsyms, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
  1014  	}
  1015  	slices.SortFunc(gcsyms, typesStrCmp)
  1016  	for _, ts := range gcsyms {
  1017  		dgcsym(ts.t, true, false)
  1018  	}
  1019  }
  1020  
  1021  // writeITab writes the itab for concrete type typ implementing interface iface. If
  1022  // allowNonImplement is true, allow the case where typ does not implement iface, and just
  1023  // create a dummy itab with zeroed-out method entries.
  1024  func writeITab(lsym *obj.LSym, typ, iface *types.Type, allowNonImplement bool) {
  1025  	// TODO(mdempsky): Fix methodWrapper, geneq, and genhash (and maybe
  1026  	// others) to stop clobbering these.
  1027  	oldpos, oldfn := base.Pos, ir.CurFunc
  1028  	defer func() { base.Pos, ir.CurFunc = oldpos, oldfn }()
  1029  
  1030  	if typ == nil || (typ.IsPtr() && typ.Elem() == nil) || typ.IsUntyped() || iface == nil || !iface.IsInterface() || iface.IsEmptyInterface() {
  1031  		base.Fatalf("writeITab(%v, %v)", typ, iface)
  1032  	}
  1033  
  1034  	sigs := iface.AllMethods()
  1035  	entries := make([]*obj.LSym, 0, len(sigs))
  1036  
  1037  	// both sigs and methods are sorted by name,
  1038  	// so we can find the intersection in a single pass
  1039  	for _, m := range methods(typ) {
  1040  		if m.name == sigs[0].Sym {
  1041  			entries = append(entries, m.isym)
  1042  			if m.isym == nil {
  1043  				panic("NO ISYM")
  1044  			}
  1045  			sigs = sigs[1:]
  1046  			if len(sigs) == 0 {
  1047  				break
  1048  			}
  1049  		}
  1050  	}
  1051  	completeItab := len(sigs) == 0
  1052  	if !allowNonImplement && !completeItab {
  1053  		base.Fatalf("incomplete itab")
  1054  	}
  1055  
  1056  	// dump empty itab symbol into i.sym
  1057  	// type itab struct {
  1058  	//   inter  *interfacetype
  1059  	//   _type  *_type
  1060  	//   hash   uint32 // copy of _type.hash. Used for type switches.
  1061  	//   _      [4]byte
  1062  	//   fun    [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
  1063  	// }
  1064  	c := rttype.NewCursor(lsym, 0, rttype.ITab)
  1065  	c.Field("Inter").WritePtr(writeType(iface))
  1066  	c.Field("Type").WritePtr(writeType(typ))
  1067  	c.Field("Hash").WriteUint32(types.TypeHash(typ)) // copy of type hash
  1068  
  1069  	var delta int64
  1070  	c = c.Field("Fun")
  1071  	if !completeItab {
  1072  		// If typ doesn't implement iface, make method entries be zero.
  1073  		c.Elem(0).WriteUintptr(0)
  1074  	} else {
  1075  		var a rttype.ArrayCursor
  1076  		a, delta = c.ModifyArray(len(entries))
  1077  		for i, fn := range entries {
  1078  			a.Elem(i).WritePtrWeak(fn) // method pointer for each method
  1079  		}
  1080  	}
  1081  	// Nothing writes static itabs, so they are read only.
  1082  	objw.Global(lsym, int32(rttype.ITab.Size()+delta), int16(obj.DUPOK|obj.RODATA))
  1083  	lsym.Set(obj.AttrContentAddressable, true)
  1084  }
  1085  
  1086  func WritePluginTable() {
  1087  	ptabs := typecheck.Target.PluginExports
  1088  	if len(ptabs) == 0 {
  1089  		return
  1090  	}
  1091  
  1092  	lsym := base.Ctxt.Lookup("go:plugin.tabs")
  1093  	ot := 0
  1094  	for _, p := range ptabs {
  1095  		// Dump ptab symbol into go.pluginsym package.
  1096  		//
  1097  		// type ptab struct {
  1098  		//	name nameOff
  1099  		//	typ  typeOff // pointer to symbol
  1100  		// }
  1101  		nsym := dname(p.Sym().Name, "", nil, true, false)
  1102  		t := p.Type()
  1103  		if p.Class != ir.PFUNC {
  1104  			t = types.NewPtr(t)
  1105  		}
  1106  		tsym := writeType(t)
  1107  		ot = objw.SymPtrOff(lsym, ot, nsym)
  1108  		ot = objw.SymPtrOff(lsym, ot, tsym)
  1109  		// Plugin exports symbols as interfaces. Mark their types
  1110  		// as UsedInIface.
  1111  		tsym.Set(obj.AttrUsedInIface, true)
  1112  	}
  1113  	objw.Global(lsym, int32(ot), int16(obj.RODATA))
  1114  
  1115  	lsym = base.Ctxt.Lookup("go:plugin.exports")
  1116  	ot = 0
  1117  	for _, p := range ptabs {
  1118  		ot = objw.SymPtr(lsym, ot, p.Linksym(), 0)
  1119  	}
  1120  	objw.Global(lsym, int32(ot), int16(obj.RODATA))
  1121  }
  1122  
  1123  // writtenByWriteBasicTypes reports whether typ is written by WriteBasicTypes.
  1124  // WriteBasicTypes always writes pointer types; any pointer has been stripped off typ already.
  1125  func writtenByWriteBasicTypes(typ *types.Type) bool {
  1126  	if typ.Sym() == nil && typ.Kind() == types.TFUNC {
  1127  		// func(error) string
  1128  		if typ.NumRecvs() == 0 &&
  1129  			typ.NumParams() == 1 && typ.NumResults() == 1 &&
  1130  			typ.Param(0).Type == types.ErrorType &&
  1131  			typ.Result(0).Type == types.Types[types.TSTRING] {
  1132  			return true
  1133  		}
  1134  	}
  1135  
  1136  	// Now we have left the basic types plus any and error, plus slices of them.
  1137  	// Strip the slice.
  1138  	if typ.Sym() == nil && typ.IsSlice() {
  1139  		typ = typ.Elem()
  1140  	}
  1141  
  1142  	// Basic types.
  1143  	sym := typ.Sym()
  1144  	if sym != nil && (sym.Pkg == types.BuiltinPkg || sym.Pkg == types.UnsafePkg) {
  1145  		return true
  1146  	}
  1147  	// any or error
  1148  	return (sym == nil && typ.IsEmptyInterface()) || typ == types.ErrorType
  1149  }
  1150  
  1151  func WriteBasicTypes() {
  1152  	// do basic types if compiling package runtime.
  1153  	// they have to be in at least one package,
  1154  	// and runtime is always loaded implicitly,
  1155  	// so this is as good as any.
  1156  	// another possible choice would be package main,
  1157  	// but using runtime means fewer copies in object files.
  1158  	// The code here needs to be in sync with writtenByWriteBasicTypes above.
  1159  	if base.Ctxt.Pkgpath != "runtime" {
  1160  		return
  1161  	}
  1162  
  1163  	// Note: always write NewPtr(t) because NeedEmit's caller strips the pointer.
  1164  	var list []*types.Type
  1165  	for i := types.Kind(1); i <= types.TBOOL; i++ {
  1166  		list = append(list, types.Types[i])
  1167  	}
  1168  	list = append(list,
  1169  		types.Types[types.TSTRING],
  1170  		types.Types[types.TUNSAFEPTR],
  1171  		types.AnyType,
  1172  		types.ErrorType)
  1173  	for _, t := range list {
  1174  		writeType(types.NewPtr(t))
  1175  		writeType(types.NewPtr(types.NewSlice(t)))
  1176  	}
  1177  
  1178  	// emit type for func(error) string,
  1179  	// which is the type of an auto-generated wrapper.
  1180  	writeType(types.NewPtr(types.NewSignature(nil, []*types.Field{
  1181  		types.NewField(base.Pos, nil, types.ErrorType),
  1182  	}, []*types.Field{
  1183  		types.NewField(base.Pos, nil, types.Types[types.TSTRING]),
  1184  	})))
  1185  }
  1186  
  1187  type typeAndStr struct {
  1188  	t       *types.Type
  1189  	short   string // "short" here means TypeSymName
  1190  	regular string
  1191  }
  1192  
  1193  func typesStrCmp(a, b typeAndStr) int {
  1194  	// put named types before unnamed types
  1195  	if a.t.Sym() != nil && b.t.Sym() == nil {
  1196  		return -1
  1197  	}
  1198  	if a.t.Sym() == nil && b.t.Sym() != nil {
  1199  		return +1
  1200  	}
  1201  
  1202  	if r := strings.Compare(a.short, b.short); r != 0 {
  1203  		return r
  1204  	}
  1205  	// When the only difference between the types is whether
  1206  	// they refer to byte or uint8, such as **byte vs **uint8,
  1207  	// the types' NameStrings can be identical.
  1208  	// To preserve deterministic sort ordering, sort these by String().
  1209  	//
  1210  	// TODO(mdempsky): This all seems suspect. Using LinkString would
  1211  	// avoid naming collisions, and there shouldn't be a reason to care
  1212  	// about "byte" vs "uint8": they share the same runtime type
  1213  	// descriptor anyway.
  1214  	if r := strings.Compare(a.regular, b.regular); r != 0 {
  1215  		return r
  1216  	}
  1217  	// Identical anonymous interfaces defined in different locations
  1218  	// will be equal for the above checks, but different in DWARF output.
  1219  	// Sort by source position to ensure deterministic order.
  1220  	// See issues 27013 and 30202.
  1221  	if a.t.Kind() == types.TINTER && len(a.t.AllMethods()) > 0 {
  1222  		if a.t.AllMethods()[0].Pos.Before(b.t.AllMethods()[0].Pos) {
  1223  			return -1
  1224  		}
  1225  		return +1
  1226  	}
  1227  	return 0
  1228  }
  1229  
  1230  // GCSym returns a data symbol containing GC information for type t.
  1231  // GC information is always a bitmask, never a gc program.
  1232  // GCSym may be called in concurrent backend, so it does not emit the symbol
  1233  // content.
  1234  func GCSym(t *types.Type) (lsym *obj.LSym, ptrdata int64) {
  1235  	// Record that we need to emit the GC symbol.
  1236  	gcsymmu.Lock()
  1237  	if _, ok := gcsymset[t]; !ok {
  1238  		gcsymset[t] = struct{}{}
  1239  	}
  1240  	gcsymmu.Unlock()
  1241  
  1242  	lsym, _, ptrdata = dgcsym(t, false, false)
  1243  	return
  1244  }
  1245  
  1246  // dgcsym returns a data symbol containing GC information for type t, along
  1247  // with a boolean reporting whether the gc mask should be computed on demand
  1248  // at runtime, and the ptrdata field to record in the reflect type information.
  1249  // When write is true, it writes the symbol data.
  1250  func dgcsym(t *types.Type, write, onDemandAllowed bool) (lsym *obj.LSym, onDemand bool, ptrdata int64) {
  1251  	ptrdata = types.PtrDataSize(t)
  1252  	if !onDemandAllowed || ptrdata/int64(types.PtrSize) <= abi.MaxPtrmaskBytes*8 {
  1253  		lsym = dgcptrmask(t, write)
  1254  		return
  1255  	}
  1256  
  1257  	onDemand = true
  1258  	lsym = dgcptrmaskOnDemand(t, write)
  1259  	return
  1260  }
  1261  
  1262  // dgcptrmask emits and returns the symbol containing a pointer mask for type t.
  1263  func dgcptrmask(t *types.Type, write bool) *obj.LSym {
  1264  	// Bytes we need for the ptrmask.
  1265  	n := (types.PtrDataSize(t)/int64(types.PtrSize) + 7) / 8
  1266  	// Runtime wants ptrmasks padded to a multiple of uintptr in size.
  1267  	n = (n + int64(types.PtrSize) - 1) &^ (int64(types.PtrSize) - 1)
  1268  	ptrmask := make([]byte, n)
  1269  	fillptrmask(t, ptrmask)
  1270  	p := fmt.Sprintf("runtime.gcbits.%x", ptrmask)
  1271  
  1272  	lsym := base.Ctxt.Lookup(p)
  1273  	if write && !lsym.OnList() {
  1274  		for i, x := range ptrmask {
  1275  			objw.Uint8(lsym, i, x)
  1276  		}
  1277  		objw.Global(lsym, int32(len(ptrmask)), obj.DUPOK|obj.RODATA|obj.LOCAL)
  1278  		lsym.Set(obj.AttrContentAddressable, true)
  1279  	}
  1280  	return lsym
  1281  }
  1282  
  1283  // fillptrmask fills in ptrmask with 1s corresponding to the
  1284  // word offsets in t that hold pointers.
  1285  // ptrmask is assumed to fit at least types.PtrDataSize(t)/PtrSize bits.
  1286  func fillptrmask(t *types.Type, ptrmask []byte) {
  1287  	for i := range ptrmask {
  1288  		ptrmask[i] = 0
  1289  	}
  1290  	if !t.HasPointers() {
  1291  		return
  1292  	}
  1293  
  1294  	vec := bitvec.New(8 * int32(len(ptrmask)))
  1295  	typebits.Set(t, 0, vec)
  1296  
  1297  	nptr := types.PtrDataSize(t) / int64(types.PtrSize)
  1298  	for i := int64(0); i < nptr; i++ {
  1299  		if vec.Get(int32(i)) {
  1300  			ptrmask[i/8] |= 1 << (uint(i) % 8)
  1301  		}
  1302  	}
  1303  }
  1304  
  1305  // dgcptrmaskOnDemand emits and returns the symbol that should be referenced by
  1306  // the GCData field of a type, for large types.
  1307  func dgcptrmaskOnDemand(t *types.Type, write bool) *obj.LSym {
  1308  	lsym := TypeLinksymPrefix(".gcmask", t)
  1309  	if write && !lsym.OnList() {
  1310  		// Note: contains a pointer, but a pointer to a
  1311  		// persistentalloc allocation. Starts with nil.
  1312  		objw.Uintptr(lsym, 0, 0)
  1313  		objw.Global(lsym, int32(types.PtrSize), obj.DUPOK|obj.NOPTR|obj.LOCAL) // TODO:bss?
  1314  	}
  1315  	return lsym
  1316  }
  1317  
  1318  // ZeroAddr returns the address of a symbol with at least
  1319  // size bytes of zeros.
  1320  func ZeroAddr(size int64) ir.Node {
  1321  	if size >= 1<<31 {
  1322  		base.Fatalf("map elem too big %d", size)
  1323  	}
  1324  	if ZeroSize < size {
  1325  		ZeroSize = size
  1326  	}
  1327  	lsym := base.PkgLinksym("go:map", "zero", obj.ABI0)
  1328  	x := ir.NewLinksymExpr(base.Pos, lsym, types.Types[types.TUINT8])
  1329  	return typecheck.Expr(typecheck.NodAddr(x))
  1330  }
  1331  
  1332  // NeedEmit reports whether typ is a type that we need to emit code
  1333  // for (e.g., runtime type descriptors, method wrappers).
  1334  func NeedEmit(typ *types.Type) bool {
  1335  	// TODO(mdempsky): Export data should keep track of which anonymous
  1336  	// and instantiated types were emitted, so at least downstream
  1337  	// packages can skip re-emitting them.
  1338  	//
  1339  	// Perhaps we can just generalize the linker-symbol indexing to
  1340  	// track the index of arbitrary types, not just defined types, and
  1341  	// use its presence to detect this. The same idea would work for
  1342  	// instantiated generic functions too.
  1343  
  1344  	switch sym := typ.Sym(); {
  1345  	case writtenByWriteBasicTypes(typ):
  1346  		return base.Ctxt.Pkgpath == "runtime"
  1347  
  1348  	case sym == nil:
  1349  		// Anonymous type; possibly never seen before or ever again.
  1350  		// Need to emit to be safe (however, see TODO above).
  1351  		return true
  1352  
  1353  	case sym.Pkg == types.LocalPkg:
  1354  		// Local defined type; our responsibility.
  1355  		return true
  1356  
  1357  	case typ.IsFullyInstantiated():
  1358  		// Instantiated type; possibly instantiated with unique type arguments.
  1359  		// Need to emit to be safe (however, see TODO above).
  1360  		return true
  1361  
  1362  	case typ.HasShape():
  1363  		// Shape type; need to emit even though it lives in the .shape package.
  1364  		// TODO: make sure the linker deduplicates them (see dupok in writeType above).
  1365  		return true
  1366  
  1367  	default:
  1368  		// Should have been emitted by an imported package.
  1369  		return false
  1370  	}
  1371  }
  1372  
  1373  // Generate a wrapper function to convert from
  1374  // a receiver of type T to a receiver of type U.
  1375  // That is,
  1376  //
  1377  //	func (t T) M() {
  1378  //		...
  1379  //	}
  1380  //
  1381  // already exists; this function generates
  1382  //
  1383  //	func (u U) M() {
  1384  //		u.M()
  1385  //	}
  1386  //
  1387  // where the types T and U are such that u.M() is valid
  1388  // and calls the T.M method.
  1389  // The resulting function is for use in method tables.
  1390  //
  1391  //	rcvr - U
  1392  //	method - M func (t T)(), a TFIELD type struct
  1393  //
  1394  // Also wraps methods on instantiated generic types for use in itab entries.
  1395  // For an instantiated generic type G[int], we generate wrappers like:
  1396  // G[int] pointer shaped:
  1397  //
  1398  //	func (x G[int]) f(arg) {
  1399  //		.inst.G[int].f(dictionary, x, arg)
  1400  //	}
  1401  //
  1402  // G[int] not pointer shaped:
  1403  //
  1404  //	func (x *G[int]) f(arg) {
  1405  //		.inst.G[int].f(dictionary, *x, arg)
  1406  //	}
  1407  //
  1408  // These wrappers are always fully stenciled.
  1409  func methodWrapper(rcvr *types.Type, method *types.Field, forItab bool) *obj.LSym {
  1410  	if forItab && !types.IsDirectIface(rcvr) {
  1411  		rcvr = rcvr.PtrTo()
  1412  	}
  1413  
  1414  	newnam := ir.MethodSym(rcvr, method.Sym)
  1415  	lsym := newnam.Linksym()
  1416  
  1417  	// Unified IR creates its own wrappers.
  1418  	return lsym
  1419  }
  1420  
  1421  var ZeroSize int64
  1422  
  1423  // MarkTypeUsedInInterface marks that type t is converted to an interface.
  1424  // This information is used in the linker in dead method elimination.
  1425  func MarkTypeUsedInInterface(t *types.Type, from *obj.LSym) {
  1426  	if t.HasShape() {
  1427  		// Shape types shouldn't be put in interfaces, so we shouldn't ever get here.
  1428  		base.Fatalf("shape types have no methods %+v", t)
  1429  	}
  1430  	MarkTypeSymUsedInInterface(TypeLinksym(t), from)
  1431  }
  1432  func MarkTypeSymUsedInInterface(tsym *obj.LSym, from *obj.LSym) {
  1433  	// Emit a marker relocation. The linker will know the type is converted
  1434  	// to an interface if "from" is reachable.
  1435  	from.AddRel(base.Ctxt, obj.Reloc{Type: objabi.R_USEIFACE, Sym: tsym})
  1436  }
  1437  
  1438  // MarkUsedIfaceMethod marks that an interface method is used in the current
  1439  // function. n is OCALLINTER node.
  1440  func MarkUsedIfaceMethod(n *ir.CallExpr) {
  1441  	// skip unnamed functions (func _())
  1442  	if ir.CurFunc.LSym == nil {
  1443  		return
  1444  	}
  1445  	dot := n.Fun.(*ir.SelectorExpr)
  1446  	ityp := dot.X.Type()
  1447  	if ityp.HasShape() {
  1448  		// Here we're calling a method on a generic interface. Something like:
  1449  		//
  1450  		// type I[T any] interface { foo() T }
  1451  		// func f[T any](x I[T]) {
  1452  		//     ... = x.foo()
  1453  		// }
  1454  		// f[int](...)
  1455  		// f[string](...)
  1456  		//
  1457  		// In this case, in f we're calling foo on a generic interface.
  1458  		// Which method could that be? Normally we could match the method
  1459  		// both by name and by type. But in this case we don't really know
  1460  		// the type of the method we're calling. It could be func()int
  1461  		// or func()string. So we match on just the function name, instead
  1462  		// of both the name and the type used for the non-generic case below.
  1463  		// TODO: instantiations at least know the shape of the instantiated
  1464  		// type, and the linker could do more complicated matching using
  1465  		// some sort of fuzzy shape matching. For now, only use the name
  1466  		// of the method for matching.
  1467  		ir.CurFunc.LSym.AddRel(base.Ctxt, obj.Reloc{
  1468  			Type: objabi.R_USENAMEDMETHOD,
  1469  			Sym:  staticdata.StringSymNoCommon(dot.Sel.Name),
  1470  		})
  1471  		return
  1472  	}
  1473  
  1474  	// dot.Offset() is the method index * PtrSize (the offset of code pointer in itab).
  1475  	midx := dot.Offset() / int64(types.PtrSize)
  1476  	ir.CurFunc.LSym.AddRel(base.Ctxt, obj.Reloc{
  1477  		Type: objabi.R_USEIFACEMETHOD,
  1478  		Sym:  TypeLinksym(ityp),
  1479  		Add:  InterfaceMethodOffset(ityp, midx),
  1480  	})
  1481  }
  1482  
  1483  func deref(t *types.Type) *types.Type {
  1484  	if t.IsPtr() {
  1485  		return t.Elem()
  1486  	}
  1487  	return t
  1488  }
  1489  

View as plain text