| |
| |
| |
|
|
| package reflectdata |
|
|
| import ( |
| "encoding/binary" |
| "fmt" |
| "internal/abi" |
| "slices" |
| "sort" |
| "strings" |
| "sync" |
|
|
| "cmd/compile/internal/base" |
| "cmd/compile/internal/bitvec" |
| "cmd/compile/internal/compare" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/objw" |
| "cmd/compile/internal/rttype" |
| "cmd/compile/internal/staticdata" |
| "cmd/compile/internal/typebits" |
| "cmd/compile/internal/typecheck" |
| "cmd/compile/internal/types" |
| "cmd/internal/obj" |
| "cmd/internal/objabi" |
| "cmd/internal/src" |
| ) |
|
|
| type ptabEntry struct { |
| s *types.Sym |
| t *types.Type |
| } |
|
|
| |
| var ( |
| |
| signatmu sync.Mutex |
| |
| signatset = make(map[*types.Type]struct{}) |
| |
| signatslice []typeAndStr |
|
|
| gcsymmu sync.Mutex |
| gcsymset = make(map[*types.Type]struct{}) |
| ) |
|
|
| type typeSig struct { |
| name *types.Sym |
| isym *obj.LSym |
| tsym *obj.LSym |
| type_ *types.Type |
| mtype *types.Type |
| } |
|
|
| func commonSize() int { return int(rttype.Type.Size()) } |
|
|
| func uncommonSize(t *types.Type) int { |
| if t.Sym() == nil && len(methods(t)) == 0 { |
| return 0 |
| } |
| return int(rttype.UncommonType.Size()) |
| } |
|
|
| func makefield(name string, t *types.Type) *types.Field { |
| sym := (*types.Pkg)(nil).Lookup(name) |
| return types.NewField(src.NoXPos, sym, t) |
| } |
|
|
| |
| |
| func methods(t *types.Type) []*typeSig { |
| if t.HasShape() { |
| |
| return nil |
| } |
| |
| mt := types.ReceiverBaseType(t) |
|
|
| if mt == nil { |
| return nil |
| } |
| typecheck.CalcMethods(mt) |
|
|
| |
| |
| var ms []*typeSig |
| for _, f := range mt.AllMethods() { |
| if f.Sym == nil { |
| base.Fatalf("method with no sym on %v", mt) |
| } |
| if !f.IsMethod() { |
| base.Fatalf("non-method on %v method %v %v", mt, f.Sym, f) |
| } |
| if f.Type.Recv() == nil { |
| base.Fatalf("receiver with no type on %v method %v %v", mt, f.Sym, f) |
| } |
| if f.Nointerface() && !t.IsFullyInstantiated() { |
| |
| |
| |
| |
| continue |
| } |
|
|
| |
| |
| |
| |
| if !types.IsMethodApplicable(t, f) { |
| continue |
| } |
|
|
| sig := &typeSig{ |
| name: f.Sym, |
| isym: methodWrapper(t, f, true), |
| tsym: methodWrapper(t, f, false), |
| type_: typecheck.NewMethodType(f.Type, t), |
| mtype: typecheck.NewMethodType(f.Type, nil), |
| } |
| if f.Nointerface() { |
| |
| |
| continue |
| } |
| ms = append(ms, sig) |
| } |
|
|
| return ms |
| } |
|
|
| |
| func imethods(t *types.Type) []*typeSig { |
| var methods []*typeSig |
| for _, f := range t.AllMethods() { |
| if f.Type.Kind() != types.TFUNC || f.Sym == nil { |
| continue |
| } |
| if f.Sym.IsBlank() { |
| base.Fatalf("unexpected blank symbol in interface method set") |
| } |
| if n := len(methods); n > 0 { |
| last := methods[n-1] |
| if types.CompareSyms(last.name, f.Sym) >= 0 { |
| base.Fatalf("sigcmp vs sortinter %v %v", last.name, f.Sym) |
| } |
| } |
|
|
| sig := &typeSig{ |
| name: f.Sym, |
| mtype: f.Type, |
| type_: typecheck.NewMethodType(f.Type, nil), |
| } |
| methods = append(methods, sig) |
|
|
| |
| |
| |
| |
| methodWrapper(t, f, false) |
| } |
|
|
| return methods |
| } |
|
|
| func dimportpath(p *types.Pkg) { |
| if p.Pathsym != nil { |
| return |
| } |
|
|
| if p == types.LocalPkg && base.Ctxt.Pkgpath == "" { |
| panic("missing pkgpath") |
| } |
|
|
| |
| |
| |
| if base.Ctxt.Pkgpath == "runtime" && p == ir.Pkgs.Runtime { |
| return |
| } |
|
|
| s := base.Ctxt.Lookup("type:.importpath." + p.Prefix + ".") |
| ot := dnameData(s, 0, p.Path, "", nil, false, false) |
| objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA) |
| s.Set(obj.AttrContentAddressable, true) |
| p.Pathsym = s |
| } |
|
|
| func dgopkgpath(c rttype.Cursor, pkg *types.Pkg) { |
| c = c.Field("Bytes") |
| if pkg == nil { |
| c.WritePtr(nil) |
| return |
| } |
|
|
| dimportpath(pkg) |
| c.WritePtr(pkg.Pathsym) |
| } |
|
|
| |
| func dgopkgpathOff(c rttype.Cursor, pkg *types.Pkg) { |
| if pkg == nil { |
| c.WriteInt32(0) |
| return |
| } |
|
|
| dimportpath(pkg) |
| c.WriteSymPtrOff(pkg.Pathsym, false) |
| } |
|
|
| |
| func dnameField(c rttype.Cursor, spkg *types.Pkg, ft *types.Field) { |
| if !types.IsExported(ft.Sym.Name) && ft.Sym.Pkg != spkg { |
| base.Fatalf("package mismatch for %v", ft.Sym) |
| } |
| nsym := dname(ft.Sym.Name, ft.Note, nil, types.IsExported(ft.Sym.Name), ft.Embedded != 0) |
| c.Field("Bytes").WritePtr(nsym) |
| } |
|
|
| |
| func dnameData(s *obj.LSym, ot int, name, tag string, pkg *types.Pkg, exported, embedded bool) int { |
| if len(name) >= 1<<29 { |
| base.Fatalf("name too long: %d %s...", len(name), name[:1024]) |
| } |
| if len(tag) >= 1<<29 { |
| base.Fatalf("tag too long: %d %s...", len(tag), tag[:1024]) |
| } |
| var nameLen [binary.MaxVarintLen64]byte |
| nameLenLen := binary.PutUvarint(nameLen[:], uint64(len(name))) |
| var tagLen [binary.MaxVarintLen64]byte |
| tagLenLen := binary.PutUvarint(tagLen[:], uint64(len(tag))) |
|
|
| |
| var bits byte |
| l := 1 + nameLenLen + len(name) |
| if exported { |
| bits |= 1 << 0 |
| } |
| if len(tag) > 0 { |
| l += tagLenLen + len(tag) |
| bits |= 1 << 1 |
| } |
| if pkg != nil { |
| bits |= 1 << 2 |
| } |
| if embedded { |
| bits |= 1 << 3 |
| } |
| b := make([]byte, l) |
| b[0] = bits |
| copy(b[1:], nameLen[:nameLenLen]) |
| copy(b[1+nameLenLen:], name) |
| if len(tag) > 0 { |
| tb := b[1+nameLenLen+len(name):] |
| copy(tb, tagLen[:tagLenLen]) |
| copy(tb[tagLenLen:], tag) |
| } |
|
|
| ot = int(s.WriteBytes(base.Ctxt, int64(ot), b)) |
|
|
| if pkg != nil { |
| c := rttype.NewCursor(s, int64(ot), types.Types[types.TUINT32]) |
| dgopkgpathOff(c, pkg) |
| ot += 4 |
| } |
|
|
| return ot |
| } |
|
|
| var dnameCount int |
|
|
| |
| func dname(name, tag string, pkg *types.Pkg, exported, embedded bool) *obj.LSym { |
| |
| |
| |
| |
| sname := "type:.namedata." |
| if pkg == nil { |
| |
| if name == "" { |
| if exported { |
| sname += "-noname-exported." + tag |
| } else { |
| sname += "-noname-unexported." + tag |
| } |
| } else { |
| if exported { |
| sname += name + "." + tag |
| } else { |
| sname += name + "-" + tag |
| } |
| } |
| } else { |
| |
| |
| sname = fmt.Sprintf("%s%s.%d", sname, types.LocalPkg.Prefix, dnameCount) |
| dnameCount++ |
| } |
| if embedded { |
| sname += ".embedded" |
| } |
| s := base.Ctxt.Lookup(sname) |
| if len(s.P) > 0 { |
| return s |
| } |
| ot := dnameData(s, 0, name, tag, pkg, exported, embedded) |
| objw.Global(s, int32(ot), obj.DUPOK|obj.RODATA) |
| s.Set(obj.AttrContentAddressable, true) |
| return s |
| } |
|
|
| |
| |
| |
| func dextratype(lsym *obj.LSym, off int64, t *types.Type, dataAdd int) { |
| m := methods(t) |
| if t.Sym() == nil && len(m) == 0 { |
| base.Fatalf("extra requested of type with no extra info %v", t) |
| } |
| noff := types.RoundUp(off, int64(types.PtrSize)) |
| if noff != off { |
| base.Fatalf("unexpected alignment in dextratype for %v", t) |
| } |
|
|
| for _, a := range m { |
| writeType(a.type_) |
| } |
|
|
| c := rttype.NewCursor(lsym, off, rttype.UncommonType) |
| dgopkgpathOff(c.Field("PkgPath"), typePkg(t)) |
|
|
| dataAdd += uncommonSize(t) |
| mcount := len(m) |
| if mcount != int(uint16(mcount)) { |
| base.Fatalf("too many methods on %v: %d", t, mcount) |
| } |
| xcount := sort.Search(mcount, func(i int) bool { return !types.IsExported(m[i].name.Name) }) |
| if dataAdd != int(uint32(dataAdd)) { |
| base.Fatalf("methods are too far away on %v: %d", t, dataAdd) |
| } |
|
|
| c.Field("Mcount").WriteUint16(uint16(mcount)) |
| c.Field("Xcount").WriteUint16(uint16(xcount)) |
| c.Field("Moff").WriteUint32(uint32(dataAdd)) |
| |
|
|
| |
| array := rttype.NewArrayCursor(lsym, off+int64(dataAdd), rttype.Method, mcount) |
| for i, a := range m { |
| exported := types.IsExported(a.name.Name) |
| var pkg *types.Pkg |
| if !exported && a.name.Pkg != typePkg(t) { |
| pkg = a.name.Pkg |
| } |
| nsym := dname(a.name.Name, "", pkg, exported, false) |
|
|
| e := array.Elem(i) |
| e.Field("Name").WriteSymPtrOff(nsym, false) |
| dmethodptrOff(e.Field("Mtyp"), writeType(a.mtype)) |
| dmethodptrOff(e.Field("Ifn"), a.isym) |
| dmethodptrOff(e.Field("Tfn"), a.tsym) |
| } |
| } |
|
|
| func typePkg(t *types.Type) *types.Pkg { |
| tsym := t.Sym() |
| if tsym == nil { |
| switch t.Kind() { |
| case types.TARRAY, types.TSLICE, types.TPTR, types.TCHAN: |
| if t.Elem() != nil { |
| tsym = t.Elem().Sym() |
| } |
| } |
| } |
| if tsym != nil && tsym.Pkg != types.BuiltinPkg { |
| return tsym.Pkg |
| } |
| return nil |
| } |
|
|
| func dmethodptrOff(c rttype.Cursor, x *obj.LSym) { |
| c.WriteInt32(0) |
| c.Reloc(obj.Reloc{Type: objabi.R_METHODOFF, Sym: x}) |
| } |
|
|
| var kinds = []abi.Kind{ |
| types.TINT: abi.Int, |
| types.TUINT: abi.Uint, |
| types.TINT8: abi.Int8, |
| types.TUINT8: abi.Uint8, |
| types.TINT16: abi.Int16, |
| types.TUINT16: abi.Uint16, |
| types.TINT32: abi.Int32, |
| types.TUINT32: abi.Uint32, |
| types.TINT64: abi.Int64, |
| types.TUINT64: abi.Uint64, |
| types.TUINTPTR: abi.Uintptr, |
| types.TFLOAT32: abi.Float32, |
| types.TFLOAT64: abi.Float64, |
| types.TBOOL: abi.Bool, |
| types.TSTRING: abi.String, |
| types.TPTR: abi.Pointer, |
| types.TSTRUCT: abi.Struct, |
| types.TINTER: abi.Interface, |
| types.TCHAN: abi.Chan, |
| types.TMAP: abi.Map, |
| types.TARRAY: abi.Array, |
| types.TSLICE: abi.Slice, |
| types.TFUNC: abi.Func, |
| types.TCOMPLEX64: abi.Complex64, |
| types.TCOMPLEX128: abi.Complex128, |
| types.TUNSAFEPTR: abi.UnsafePointer, |
| } |
|
|
| func ABIKindOfType(t *types.Type) abi.Kind { |
| return kinds[t.Kind()] |
| } |
|
|
| var ( |
| memhashvarlen *obj.LSym |
| memequalvarlen *obj.LSym |
| ) |
|
|
| |
| func dcommontype(c rttype.Cursor, t *types.Type) { |
| types.CalcSize(t) |
| eqfunc := geneq(t) |
|
|
| sptrWeak := true |
| var sptr *obj.LSym |
| if !t.IsPtr() || t.IsPtrElem() { |
| tptr := types.NewPtr(t) |
| if t.Sym() != nil || methods(tptr) != nil { |
| sptrWeak = false |
| } |
| sptr = writeType(tptr) |
| } |
|
|
| gcsym, onDemand, ptrdata := dgcsym(t, true, true) |
| if !onDemand { |
| delete(gcsymset, t) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| c.Field("Size_").WriteUintptr(uint64(t.Size())) |
| c.Field("PtrBytes").WriteUintptr(uint64(ptrdata)) |
| c.Field("Hash").WriteUint32(types.TypeHash(t)) |
|
|
| var tflag abi.TFlag |
| if uncommonSize(t) != 0 { |
| tflag |= abi.TFlagUncommon |
| } |
| if t.Sym() != nil && t.Sym().Name != "" { |
| tflag |= abi.TFlagNamed |
| } |
| if compare.IsRegularMemory(t) { |
| tflag |= abi.TFlagRegularMemory |
| } |
| if onDemand { |
| tflag |= abi.TFlagGCMaskOnDemand |
| } |
|
|
| exported := false |
| p := t.NameString() |
| |
| |
| |
| |
| |
| if !strings.HasPrefix(p, "*") { |
| p = "*" + p |
| tflag |= abi.TFlagExtraStar |
| if t.Sym() != nil { |
| exported = types.IsExported(t.Sym().Name) |
| } |
| } else { |
| if t.Elem() != nil && t.Elem().Sym() != nil { |
| exported = types.IsExported(t.Elem().Sym().Name) |
| } |
| } |
| if types.IsDirectIface(t) { |
| tflag |= abi.TFlagDirectIface |
| } |
|
|
| if tflag != abi.TFlag(uint8(tflag)) { |
| |
| panic("Unexpected change in size of abi.TFlag") |
| } |
| c.Field("TFlag").WriteUint8(uint8(tflag)) |
|
|
| |
| i := int(uint8(t.Alignment())) |
|
|
| if i == 0 { |
| i = 1 |
| } |
| if i&(i-1) != 0 { |
| base.Fatalf("invalid alignment %d for %v", uint8(t.Alignment()), t) |
| } |
| c.Field("Align_").WriteUint8(uint8(t.Alignment())) |
| c.Field("FieldAlign_").WriteUint8(uint8(t.Alignment())) |
|
|
| c.Field("Kind_").WriteUint8(uint8(ABIKindOfType(t))) |
|
|
| c.Field("Equal").WritePtr(eqfunc) |
| c.Field("GCData").WritePtr(gcsym) |
|
|
| nsym := dname(p, "", nil, exported, false) |
| c.Field("Str").WriteSymPtrOff(nsym, false) |
| c.Field("PtrToThis").WriteSymPtrOff(sptr, sptrWeak) |
| } |
|
|
| |
| |
| func TrackSym(t *types.Type, f *types.Field) *obj.LSym { |
| return base.PkgLinksym("go:track", t.LinkString()+"."+f.Sym.Name, obj.ABI0) |
| } |
|
|
| func TypeSymPrefix(prefix string, t *types.Type) *types.Sym { |
| p := prefix + "." + t.LinkString() |
| s := types.TypeSymLookup(p) |
|
|
| |
| |
| signatmu.Lock() |
| NeedRuntimeType(t) |
| signatmu.Unlock() |
|
|
| |
|
|
| return s |
| } |
|
|
| func TypeSym(t *types.Type) *types.Sym { |
| if t == nil || (t.IsPtr() && t.Elem() == nil) || t.IsUntyped() { |
| base.Fatalf("TypeSym %v", t) |
| } |
| if t.Kind() == types.TFUNC && t.Recv() != nil { |
| base.Fatalf("misuse of method type: %v", t) |
| } |
| s := types.TypeSym(t) |
| signatmu.Lock() |
| NeedRuntimeType(t) |
| signatmu.Unlock() |
| return s |
| } |
|
|
| func TypeLinksymPrefix(prefix string, t *types.Type) *obj.LSym { |
| return TypeSymPrefix(prefix, t).Linksym() |
| } |
|
|
| func TypeLinksymLookup(name string) *obj.LSym { |
| return types.TypeSymLookup(name).Linksym() |
| } |
|
|
| func TypeLinksym(t *types.Type) *obj.LSym { |
| lsym := TypeSym(t).Linksym() |
| signatmu.Lock() |
| if lsym.Extra == nil { |
| ti := lsym.NewTypeInfo() |
| ti.Type = t |
| } |
| signatmu.Unlock() |
| return lsym |
| } |
|
|
| |
| |
| func TypePtrAt(pos src.XPos, t *types.Type) *ir.AddrExpr { |
| return typecheck.LinksymAddr(pos, TypeLinksym(t), types.Types[types.TUINT8]) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func ITabLsym(typ, iface *types.Type) *obj.LSym { |
| return itabLsym(typ, iface, true) |
| } |
|
|
| func itabLsym(typ, iface *types.Type, allowNonImplement bool) *obj.LSym { |
| s, existed := ir.Pkgs.Itab.LookupOK(typ.LinkString() + "," + iface.LinkString()) |
| lsym := s.Linksym() |
| signatmu.Lock() |
| if lsym.Extra == nil { |
| ii := lsym.NewItabInfo() |
| ii.Type = typ |
| } |
| signatmu.Unlock() |
|
|
| if !existed { |
| writeITab(lsym, typ, iface, allowNonImplement) |
| } |
| return lsym |
| } |
|
|
| |
| |
| |
| func ITabAddrAt(pos src.XPos, typ, iface *types.Type) *ir.AddrExpr { |
| lsym := itabLsym(typ, iface, false) |
| return typecheck.LinksymAddr(pos, lsym, types.Types[types.TUINT8]) |
| } |
|
|
| |
| |
| func needkeyupdate(t *types.Type) bool { |
| switch t.Kind() { |
| case types.TBOOL, types.TINT, types.TUINT, types.TINT8, types.TUINT8, types.TINT16, types.TUINT16, types.TINT32, types.TUINT32, |
| types.TINT64, types.TUINT64, types.TUINTPTR, types.TPTR, types.TUNSAFEPTR, types.TCHAN: |
| return false |
|
|
| case types.TFLOAT32, types.TFLOAT64, types.TCOMPLEX64, types.TCOMPLEX128, |
| types.TINTER, |
| types.TSTRING: |
| return true |
|
|
| case types.TARRAY: |
| return needkeyupdate(t.Elem()) |
|
|
| case types.TSTRUCT: |
| for _, t1 := range t.Fields() { |
| if needkeyupdate(t1.Type) { |
| return true |
| } |
| } |
| return false |
|
|
| default: |
| base.Fatalf("bad type for map key: %v", t) |
| return true |
| } |
| } |
|
|
| |
| func hashMightPanic(t *types.Type) bool { |
| switch t.Kind() { |
| case types.TINTER: |
| return true |
|
|
| case types.TARRAY: |
| return hashMightPanic(t.Elem()) |
|
|
| case types.TSTRUCT: |
| for _, t1 := range t.Fields() { |
| if hashMightPanic(t1.Type) { |
| return true |
| } |
| } |
| return false |
|
|
| default: |
| return false |
| } |
| } |
|
|
| |
| |
| |
| func formalType(t *types.Type) *types.Type { |
| switch t { |
| case types.AnyType, types.ByteType, types.RuneType: |
| return types.Types[t.Kind()] |
| } |
| return t |
| } |
|
|
| func writeType(t *types.Type) *obj.LSym { |
| t = formalType(t) |
| if t.IsUntyped() { |
| base.Fatalf("writeType %v", t) |
| } |
|
|
| s := types.TypeSym(t) |
| lsym := s.Linksym() |
|
|
| |
| |
| |
| tbase := t |
| if t.IsPtr() && t.Sym() == nil && t.Elem().Sym() != nil { |
| tbase = t.Elem() |
| } |
| if tbase.Kind() == types.TFORW { |
| base.Fatalf("unresolved defined type: %v", tbase) |
| } |
|
|
| |
| |
| |
| |
| if sym := tbase.Sym(); sym != nil && sym.Pkg == ir.Pkgs.Runtime { |
| return lsym |
| } |
|
|
| if s.Siggen() { |
| return lsym |
| } |
| s.SetSiggen(true) |
|
|
| if !tbase.HasShape() { |
| TypeLinksym(t) |
| } |
|
|
| if !NeedEmit(tbase) { |
| if i := typecheck.BaseTypeIndex(t); i >= 0 { |
| lsym.Pkg = tbase.Sym().Pkg.Prefix |
| lsym.SymIdx = int32(i) |
| lsym.Set(obj.AttrIndexed, true) |
| } |
|
|
| |
| |
| |
| |
| return lsym |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| extra := t.Sym() != nil || len(methods(t)) != 0 |
|
|
| |
| |
| var rt *types.Type |
| dataAdd := 0 |
| switch t.Kind() { |
| default: |
| rt = rttype.Type |
| case types.TARRAY: |
| rt = rttype.ArrayType |
| case types.TSLICE: |
| rt = rttype.SliceType |
| case types.TCHAN: |
| rt = rttype.ChanType |
| case types.TFUNC: |
| rt = rttype.FuncType |
| dataAdd = (t.NumRecvs() + t.NumParams() + t.NumResults()) * types.PtrSize |
| case types.TINTER: |
| rt = rttype.InterfaceType |
| dataAdd = len(imethods(t)) * int(rttype.IMethod.Size()) |
| case types.TMAP: |
| rt = rttype.MapType |
| case types.TPTR: |
| rt = rttype.PtrType |
| |
| case types.TSTRUCT: |
| rt = rttype.StructType |
| dataAdd = t.NumFields() * int(rttype.StructField.Size()) |
| } |
|
|
| |
| B := rt.Size() |
| C := B |
| if extra { |
| C = B + rttype.UncommonType.Size() |
| } |
| D := C + int64(dataAdd) |
| E := D + int64(len(methods(t)))*rttype.Method.Size() |
|
|
| |
| c := rttype.NewCursor(lsym, 0, rt) |
| if rt == rttype.Type { |
| dcommontype(c, t) |
| } else { |
| dcommontype(c.Field("Type"), t) |
| } |
|
|
| |
| |
| switch t.Kind() { |
| case types.TARRAY: |
| |
| s1 := writeType(t.Elem()) |
| t2 := types.NewSlice(t.Elem()) |
| s2 := writeType(t2) |
| c.Field("Elem").WritePtr(s1) |
| c.Field("Slice").WritePtr(s2) |
| c.Field("Len").WriteUintptr(uint64(t.NumElem())) |
|
|
| case types.TSLICE: |
| |
| s1 := writeType(t.Elem()) |
| c.Field("Elem").WritePtr(s1) |
|
|
| case types.TCHAN: |
| |
| s1 := writeType(t.Elem()) |
| c.Field("Elem").WritePtr(s1) |
| c.Field("Dir").WriteInt(int64(t.ChanDir())) |
|
|
| case types.TFUNC: |
| |
| for _, t1 := range t.RecvParamsResults() { |
| writeType(t1.Type) |
| } |
| inCount := t.NumRecvs() + t.NumParams() |
| outCount := t.NumResults() |
| if t.IsVariadic() { |
| outCount |= 1 << 15 |
| } |
|
|
| c.Field("InCount").WriteUint16(uint16(inCount)) |
| c.Field("OutCount").WriteUint16(uint16(outCount)) |
|
|
| |
| typs := t.RecvParamsResults() |
| array := rttype.NewArrayCursor(lsym, C, types.Types[types.TUNSAFEPTR], len(typs)) |
| for i, t1 := range typs { |
| array.Elem(i).WritePtr(writeType(t1.Type)) |
| } |
|
|
| case types.TINTER: |
| |
| m := imethods(t) |
| n := len(m) |
| for _, a := range m { |
| writeType(a.type_) |
| } |
|
|
| var tpkg *types.Pkg |
| if t.Sym() != nil && t != types.Types[t.Kind()] && t != types.ErrorType { |
| tpkg = t.Sym().Pkg |
| } |
| dgopkgpath(c.Field("PkgPath"), tpkg) |
| c.Field("Methods").WriteSlice(lsym, C, int64(n), int64(n)) |
|
|
| array := rttype.NewArrayCursor(lsym, C, rttype.IMethod, n) |
| for i, a := range m { |
| exported := types.IsExported(a.name.Name) |
| var pkg *types.Pkg |
| if !exported && a.name.Pkg != tpkg { |
| pkg = a.name.Pkg |
| } |
| nsym := dname(a.name.Name, "", pkg, exported, false) |
|
|
| e := array.Elem(i) |
| e.Field("Name").WriteSymPtrOff(nsym, false) |
| e.Field("Typ").WriteSymPtrOff(writeType(a.type_), false) |
| } |
|
|
| case types.TMAP: |
| writeMapType(t, lsym, c) |
|
|
| case types.TPTR: |
| |
| if t.Elem().Kind() == types.TANY { |
| base.Fatalf("bad pointer base type") |
| } |
|
|
| s1 := writeType(t.Elem()) |
| c.Field("Elem").WritePtr(s1) |
|
|
| case types.TSTRUCT: |
| |
| fields := t.Fields() |
| for _, t1 := range fields { |
| writeType(t1.Type) |
| } |
|
|
| |
| |
| |
| |
| |
| var spkg *types.Pkg |
| for _, f := range fields { |
| if !types.IsExported(f.Sym.Name) { |
| spkg = f.Sym.Pkg |
| break |
| } |
| } |
|
|
| dgopkgpath(c.Field("PkgPath"), spkg) |
| c.Field("Fields").WriteSlice(lsym, C, int64(len(fields)), int64(len(fields))) |
|
|
| array := rttype.NewArrayCursor(lsym, C, rttype.StructField, len(fields)) |
| for i, f := range fields { |
| e := array.Elem(i) |
| dnameField(e.Field("Name"), spkg, f) |
| e.Field("Typ").WritePtr(writeType(f.Type)) |
| e.Field("Offset").WriteUintptr(uint64(f.Offset)) |
| } |
| } |
|
|
| |
| if extra { |
| dextratype(lsym, B, t, dataAdd) |
| } |
|
|
| |
| |
| |
| |
| dupok := 0 |
| if tbase.Sym() == nil || tbase.IsFullyInstantiated() || tbase.HasShape() { |
| dupok = obj.DUPOK |
| } |
|
|
| objw.Global(lsym, int32(E), int16(dupok|obj.RODATA)) |
|
|
| |
| |
| |
| |
| |
| keep := base.Ctxt.Flag_dynlink |
| if !keep && t.Sym() == nil { |
| |
| |
| |
| |
| |
| switch t.Kind() { |
| case types.TPTR, types.TARRAY, types.TCHAN, types.TFUNC, types.TMAP, types.TSLICE, types.TSTRUCT: |
| keep = true |
| } |
| } |
| |
| if types.TypeHasNoAlg(t) { |
| keep = false |
| } |
| lsym.Set(obj.AttrMakeTypelink, keep) |
|
|
| return lsym |
| } |
|
|
| |
| |
| func InterfaceMethodOffset(ityp *types.Type, i int64) int64 { |
| |
| |
| |
| |
| |
| |
| |
| |
| return int64(commonSize()+4*types.PtrSize+uncommonSize(ityp)) + i*8 |
| } |
|
|
| |
| func NeedRuntimeType(t *types.Type) { |
| if _, ok := signatset[t]; !ok { |
| signatset[t] = struct{}{} |
| signatslice = append(signatslice, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()}) |
| } |
| } |
|
|
| func WriteRuntimeTypes() { |
| |
| |
| for len(signatslice) > 0 { |
| signats := signatslice |
| |
| slices.SortFunc(signats, typesStrCmp) |
| for _, ts := range signats { |
| t := ts.t |
| writeType(t) |
| if t.Sym() != nil { |
| writeType(types.NewPtr(t)) |
| } |
| } |
| signatslice = signatslice[len(signats):] |
| } |
| } |
|
|
| func WriteGCSymbols() { |
| |
| gcsyms := make([]typeAndStr, 0, len(gcsymset)) |
| for t := range gcsymset { |
| gcsyms = append(gcsyms, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()}) |
| } |
| slices.SortFunc(gcsyms, typesStrCmp) |
| for _, ts := range gcsyms { |
| dgcsym(ts.t, true, false) |
| } |
| } |
|
|
| |
| |
| |
| func writeITab(lsym *obj.LSym, typ, iface *types.Type, allowNonImplement bool) { |
| |
| |
| oldpos, oldfn := base.Pos, ir.CurFunc |
| defer func() { base.Pos, ir.CurFunc = oldpos, oldfn }() |
|
|
| if typ == nil || (typ.IsPtr() && typ.Elem() == nil) || typ.IsUntyped() || iface == nil || !iface.IsInterface() || iface.IsEmptyInterface() { |
| base.Fatalf("writeITab(%v, %v)", typ, iface) |
| } |
|
|
| sigs := iface.AllMethods() |
| entries := make([]*obj.LSym, 0, len(sigs)) |
|
|
| |
| |
| for _, m := range methods(typ) { |
| if m.name == sigs[0].Sym { |
| entries = append(entries, m.isym) |
| if m.isym == nil { |
| panic("NO ISYM") |
| } |
| sigs = sigs[1:] |
| if len(sigs) == 0 { |
| break |
| } |
| } |
| } |
| completeItab := len(sigs) == 0 |
| if !allowNonImplement && !completeItab { |
| base.Fatalf("incomplete itab") |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| c := rttype.NewCursor(lsym, 0, rttype.ITab) |
| c.Field("Inter").WritePtr(writeType(iface)) |
| c.Field("Type").WritePtr(writeType(typ)) |
| c.Field("Hash").WriteUint32(types.TypeHash(typ)) |
|
|
| var delta int64 |
| c = c.Field("Fun") |
| if !completeItab { |
| |
| c.Elem(0).WriteUintptr(0) |
| } else { |
| var a rttype.ArrayCursor |
| a, delta = c.ModifyArray(len(entries)) |
| for i, fn := range entries { |
| a.Elem(i).WritePtrWeak(fn) |
| } |
| } |
| |
| objw.Global(lsym, int32(rttype.ITab.Size()+delta), int16(obj.DUPOK|obj.RODATA)) |
| lsym.Set(obj.AttrContentAddressable, true) |
| } |
|
|
| func WritePluginTable() { |
| ptabs := typecheck.Target.PluginExports |
| if len(ptabs) == 0 { |
| return |
| } |
|
|
| lsym := base.Ctxt.Lookup("go:plugin.tabs") |
| ot := 0 |
| for _, p := range ptabs { |
| |
| |
| |
| |
| |
| |
| nsym := dname(p.Sym().Name, "", nil, true, false) |
| t := p.Type() |
| if p.Class != ir.PFUNC { |
| t = types.NewPtr(t) |
| } |
| tsym := writeType(t) |
| ot = objw.SymPtrOff(lsym, ot, nsym) |
| ot = objw.SymPtrOff(lsym, ot, tsym) |
| |
| |
| tsym.Set(obj.AttrUsedInIface, true) |
| } |
| objw.Global(lsym, int32(ot), int16(obj.RODATA)) |
|
|
| lsym = base.Ctxt.Lookup("go:plugin.exports") |
| ot = 0 |
| for _, p := range ptabs { |
| ot = objw.SymPtr(lsym, ot, p.Linksym(), 0) |
| } |
| objw.Global(lsym, int32(ot), int16(obj.RODATA)) |
| } |
|
|
| |
| |
| func writtenByWriteBasicTypes(typ *types.Type) bool { |
| if typ.Sym() == nil && typ.Kind() == types.TFUNC { |
| |
| if typ.NumRecvs() == 0 && |
| typ.NumParams() == 1 && typ.NumResults() == 1 && |
| typ.Param(0).Type == types.ErrorType && |
| typ.Result(0).Type == types.Types[types.TSTRING] { |
| return true |
| } |
| } |
|
|
| |
| |
| if typ.Sym() == nil && typ.IsSlice() { |
| typ = typ.Elem() |
| } |
|
|
| |
| sym := typ.Sym() |
| if sym != nil && (sym.Pkg == types.BuiltinPkg || sym.Pkg == types.UnsafePkg) { |
| return true |
| } |
| |
| return (sym == nil && typ.IsEmptyInterface()) || typ == types.ErrorType |
| } |
|
|
| func WriteBasicTypes() { |
| |
| |
| |
| |
| |
| |
| |
| if base.Ctxt.Pkgpath != "runtime" { |
| return |
| } |
|
|
| |
| var list []*types.Type |
| for i := types.Kind(1); i <= types.TBOOL; i++ { |
| list = append(list, types.Types[i]) |
| } |
| list = append(list, |
| types.Types[types.TSTRING], |
| types.Types[types.TUNSAFEPTR], |
| types.AnyType, |
| types.ErrorType) |
| for _, t := range list { |
| writeType(types.NewPtr(t)) |
| writeType(types.NewPtr(types.NewSlice(t))) |
| } |
|
|
| |
| |
| writeType(types.NewPtr(types.NewSignature(nil, []*types.Field{ |
| types.NewField(base.Pos, nil, types.ErrorType), |
| }, []*types.Field{ |
| types.NewField(base.Pos, nil, types.Types[types.TSTRING]), |
| }))) |
| } |
|
|
| type typeAndStr struct { |
| t *types.Type |
| short string |
| regular string |
| } |
|
|
| func typesStrCmp(a, b typeAndStr) int { |
| |
| if a.t.Sym() != nil && b.t.Sym() == nil { |
| return -1 |
| } |
| if a.t.Sym() == nil && b.t.Sym() != nil { |
| return +1 |
| } |
|
|
| if r := strings.Compare(a.short, b.short); r != 0 { |
| return r |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if r := strings.Compare(a.regular, b.regular); r != 0 { |
| return r |
| } |
| |
| |
| |
| |
| if a.t.Kind() == types.TINTER && len(a.t.AllMethods()) > 0 { |
| if a.t.AllMethods()[0].Pos.Before(b.t.AllMethods()[0].Pos) { |
| return -1 |
| } |
| return +1 |
| } |
| return 0 |
| } |
|
|
| |
| |
| |
| |
| func GCSym(t *types.Type, onDemandAllowed bool) (lsym *obj.LSym, ptrdata int64) { |
| |
| gcsymmu.Lock() |
| if _, ok := gcsymset[t]; !ok { |
| gcsymset[t] = struct{}{} |
| } |
| gcsymmu.Unlock() |
|
|
| lsym, _, ptrdata = dgcsym(t, false, onDemandAllowed) |
| return |
| } |
|
|
| |
| |
| |
| |
| func dgcsym(t *types.Type, write, onDemandAllowed bool) (lsym *obj.LSym, onDemand bool, ptrdata int64) { |
| ptrdata = types.PtrDataSize(t) |
| if !onDemandAllowed || ptrdata/int64(types.PtrSize) <= abi.MaxPtrmaskBytes*8 { |
| lsym = dgcptrmask(t, write) |
| return |
| } |
|
|
| onDemand = true |
| lsym = dgcptrmaskOnDemand(t, write) |
| return |
| } |
|
|
| |
| func dgcptrmask(t *types.Type, write bool) *obj.LSym { |
| |
| n := (types.PtrDataSize(t)/int64(types.PtrSize) + 7) / 8 |
| |
| n = (n + int64(types.PtrSize) - 1) &^ (int64(types.PtrSize) - 1) |
| ptrmask := make([]byte, n) |
| fillptrmask(t, ptrmask) |
| p := fmt.Sprintf("runtime.gcbits.%x", ptrmask) |
|
|
| lsym := base.Ctxt.Lookup(p) |
| if write && !lsym.OnList() { |
| for i, x := range ptrmask { |
| objw.Uint8(lsym, i, x) |
| } |
| objw.Global(lsym, int32(len(ptrmask)), obj.DUPOK|obj.RODATA|obj.LOCAL) |
| lsym.Set(obj.AttrContentAddressable, true) |
| } |
| return lsym |
| } |
|
|
| |
| |
| |
| func fillptrmask(t *types.Type, ptrmask []byte) { |
| if !t.HasPointers() { |
| return |
| } |
|
|
| vec := bitvec.New(8 * int32(len(ptrmask))) |
| typebits.Set(t, 0, vec) |
|
|
| nptr := types.PtrDataSize(t) / int64(types.PtrSize) |
| for i := int64(0); i < nptr; i++ { |
| if vec.Get(int32(i)) { |
| ptrmask[i/8] |= 1 << (uint(i) % 8) |
| } |
| } |
| } |
|
|
| |
| |
| func dgcptrmaskOnDemand(t *types.Type, write bool) *obj.LSym { |
| lsym := TypeLinksymPrefix(".gcmask", t) |
| if write && !lsym.OnList() { |
| |
| |
| objw.Uintptr(lsym, 0, 0) |
| objw.Global(lsym, int32(types.PtrSize), obj.DUPOK|obj.NOPTR|obj.LOCAL) |
| } |
| return lsym |
| } |
|
|
| |
| |
| func ZeroAddr(size int64) ir.Node { |
| if size >= 1<<31 { |
| base.Fatalf("map elem too big %d", size) |
| } |
| if ZeroSize < size { |
| ZeroSize = size |
| } |
| lsym := base.PkgLinksym("go:map", "zero", obj.ABI0) |
| x := ir.NewLinksymExpr(base.Pos, lsym, types.Types[types.TUINT8]) |
| return typecheck.Expr(typecheck.NodAddr(x)) |
| } |
|
|
| |
| |
| func NeedEmit(typ *types.Type) bool { |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| switch sym := typ.Sym(); { |
| case writtenByWriteBasicTypes(typ): |
| return base.Ctxt.Pkgpath == "runtime" |
|
|
| case sym == nil: |
| |
| |
| return true |
|
|
| case sym.Pkg == types.LocalPkg: |
| |
| return true |
|
|
| case typ.IsFullyInstantiated(): |
| |
| |
| return true |
|
|
| case typ.HasShape(): |
| |
| |
| return true |
|
|
| default: |
| |
| return false |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func methodWrapper(rcvr *types.Type, method *types.Field, forItab bool) *obj.LSym { |
| if forItab && !types.IsDirectIface(rcvr) { |
| rcvr = rcvr.PtrTo() |
| } |
|
|
| newnam := ir.MethodSym(rcvr, method.Sym) |
| lsym := newnam.Linksym() |
|
|
| |
| return lsym |
| } |
|
|
| var ZeroSize int64 |
|
|
| |
| |
| func MarkTypeUsedInInterface(t *types.Type, from *obj.LSym) { |
| if t.HasShape() { |
| |
| base.Fatalf("shape types have no methods %+v", t) |
| } |
| MarkTypeSymUsedInInterface(TypeLinksym(t), from) |
| } |
| func MarkTypeSymUsedInInterface(tsym *obj.LSym, from *obj.LSym) { |
| |
| |
| from.AddRel(base.Ctxt, obj.Reloc{Type: objabi.R_USEIFACE, Sym: tsym}) |
| } |
|
|
| |
| |
| func MarkUsedIfaceMethod(n *ir.CallExpr) { |
| |
| if ir.CurFunc.LSym == nil { |
| return |
| } |
| dot := n.Fun.(*ir.SelectorExpr) |
| ityp := dot.X.Type() |
| if ityp.HasShape() { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ir.CurFunc.LSym.AddRel(base.Ctxt, obj.Reloc{ |
| Type: objabi.R_USENAMEDMETHOD, |
| Sym: staticdata.StringSymNoCommon(dot.Sel.Name), |
| }) |
| return |
| } |
|
|
| |
| midx := dot.Offset() / int64(types.PtrSize) |
| ir.CurFunc.LSym.AddRel(base.Ctxt, obj.Reloc{ |
| Type: objabi.R_USEIFACEMETHOD, |
| Sym: TypeLinksym(ityp), |
| Add: InterfaceMethodOffset(ityp, midx), |
| }) |
| } |
|
|