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