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.
15 // runtime interface and reflection data structures
16 var signatlist *NodeList
18 // byMethodNameAndPackagePath sorts method signatures by name, then package path.
19 type byMethodNameAndPackagePath []*Sig
21 func (x byMethodNameAndPackagePath) Len() int { return len(x) }
22 func (x byMethodNameAndPackagePath) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
23 func (x byMethodNameAndPackagePath) Less(i, j int) bool {
24 return siglt(x[i], x[j])
27 // siglt reports whether a < b
28 func siglt(a, b *Sig) bool {
30 return a.name < b.name
41 return a.pkg.Path < b.pkg.Path
44 // Builds a type representing a Bucket structure for
45 // the given map type. This type is not visible to users -
46 // we include only enough information to generate a correct GC
48 // Make sure this stays in sync with ../../../../runtime/hashmap.go!
55 func makefield(name string, t *Type) *Type {
58 f.Sym = nopkg.Lookup(name)
62 func mapbucket(t *Type) *Type {
67 bucket := typ(TSTRUCT)
72 if keytype.Width > MAXKEYSIZE {
73 keytype = Ptrto(keytype)
75 if valtype.Width > MAXVALSIZE {
76 valtype = Ptrto(valtype)
79 // The first field is: uint8 topbits[BUCKETSIZE].
82 arr.Type = Types[TUINT8]
83 arr.Bound = BUCKETSIZE
84 field := make([]*Type, 0, 5)
85 field = append(field, makefield("topbits", arr))
88 arr.Bound = BUCKETSIZE
89 field = append(field, makefield("keys", arr))
92 arr.Bound = BUCKETSIZE
93 field = append(field, makefield("values", arr))
95 // Make sure the overflow pointer is the last memory in the struct,
96 // because the runtime assumes it can use size-ptrSize as the
97 // offset of the overflow pointer. We double-check that property
98 // below once the offsets and size are computed.
100 // BUCKETSIZE is 8, so the struct is aligned to 64 bits to this point.
101 // On 32-bit systems, the max alignment is 32-bit, and the
102 // overflow pointer will add another 32-bit field, and the struct
103 // will end with no padding.
104 // On 64-bit systems, the max alignment is 64-bit, and the
105 // overflow pointer will add another 64-bit field, and the struct
106 // will end with no padding.
107 // On nacl/amd64p32, however, the max alignment is 64-bit,
108 // but the overflow pointer will add only a 32-bit field,
109 // so if the struct needs 64-bit padding (because a key or value does)
110 // then it would end with an extra 32-bit padding field.
111 // Preempt that by emitting the padding here.
112 if int(t.Type.Align) > Widthptr || int(t.Down.Align) > Widthptr {
113 field = append(field, makefield("pad", Types[TUINTPTR]))
116 // If keys and values have no pointers, the map implementation
117 // can keep a list of overflow pointers on the side so that
118 // buckets can be marked as having no pointers.
119 // Arrange for the bucket to have no pointers by changing
120 // the type of the overflow field to uintptr in this case.
121 // See comment on hmap.overflow in ../../../../runtime/hashmap.go.
122 otyp := Ptrto(bucket)
123 if !haspointers(t.Type) && !haspointers(t.Down) && t.Type.Width <= MAXKEYSIZE && t.Down.Width <= MAXVALSIZE {
124 otyp = Types[TUINTPTR]
126 ovf := makefield("overflow", otyp)
127 field = append(field, ovf)
131 bucket.Local = t.Local
132 bucket.Type = field[0]
133 for n := int32(0); n < int32(len(field)-1); n++ {
134 field[n].Down = field[n+1]
136 field[len(field)-1].Down = nil
139 // Double-check that overflow field is final memory in struct,
140 // with no padding at end. See comment above.
141 if ovf.Width != bucket.Width-int64(Widthptr) {
142 Yyerror("bad math in mapbucket for %v", t)
151 // Builds a type representing a Hmap structure for the given map type.
152 // Make sure this stays in sync with ../../../../runtime/hashmap.go!
153 func hmap(t *Type) *Type {
158 bucket := mapbucket(t)
160 field[0] = makefield("count", Types[TINT])
161 field[1] = makefield("flags", Types[TUINT8])
162 field[2] = makefield("B", Types[TUINT8])
163 field[3] = makefield("hash0", Types[TUINT32])
164 field[4] = makefield("buckets", Ptrto(bucket))
165 field[5] = makefield("oldbuckets", Ptrto(bucket))
166 field[6] = makefield("nevacuate", Types[TUINTPTR])
167 field[7] = makefield("overflow", Types[TUNSAFEPTR])
173 for n := int32(0); n < int32(len(field)-1); n++ {
174 field[n].Down = field[n+1]
176 field[len(field)-1].Down = nil
183 func hiter(t *Type) *Type {
196 // overflow0 unsafe.Pointer
197 // overflow1 unsafe.Pointer
198 // startBucket uintptr
201 // checkBucket uintptr
203 // must match ../../../../runtime/hashmap.go:hiter.
205 field[0] = makefield("key", Ptrto(t.Down))
207 field[1] = makefield("val", Ptrto(t.Type))
208 field[2] = makefield("t", Ptrto(Types[TUINT8]))
209 field[3] = makefield("h", Ptrto(hmap(t)))
210 field[4] = makefield("buckets", Ptrto(mapbucket(t)))
211 field[5] = makefield("bptr", Ptrto(mapbucket(t)))
212 field[6] = makefield("overflow0", Types[TUNSAFEPTR])
213 field[7] = makefield("overflow1", Types[TUNSAFEPTR])
214 field[8] = makefield("startBucket", Types[TUINTPTR])
215 field[9] = makefield("stuff", Types[TUINTPTR]) // offset+wrapped+B+I
216 field[10] = makefield("bucket", Types[TUINTPTR])
217 field[11] = makefield("checkBucket", Types[TUINTPTR])
219 // build iterator struct holding the above fields
224 for n := int32(0); n < int32(len(field)-1); n++ {
225 field[n].Down = field[n+1]
227 field[len(field)-1].Down = nil
229 if i.Width != int64(12*Widthptr) {
230 Yyerror("hash_iter size not correct %d %d", i.Width, 12*Widthptr)
237 // f is method type, with receiver.
238 // return function type, receiver as first argument (or not).
239 func methodfunc(f *Type, receiver *Type) *Type {
242 d := Nod(ODCLFIELD, nil, nil)
248 for t := getinargx(f).Type; t != nil; t = t.Down {
249 d = Nod(ODCLFIELD, nil, nil)
256 for t := getoutargx(f).Type; t != nil; t = t.Down {
257 d = Nod(ODCLFIELD, nil, nil)
262 t := functype(nil, in, out)
264 // Link to name of original method function.
271 // methods returns the methods of the non-interface type t, sorted by name.
272 // Generates stub functions as needed.
273 func methods(t *Type) []*Sig {
282 // type stored in interface word
285 if !isdirectiface(it) {
289 // make list of methods for t,
290 // generating code if necessary.
292 for f := mt.Xmethod; f != nil; f = f.Down {
293 if f.Etype != TFIELD {
294 Fatalf("methods: not field %v", f)
296 if f.Type.Etype != TFUNC || f.Type.Thistuple == 0 {
297 Fatalf("non-method on %v method %v %v\n", mt, f.Sym, f)
299 if getthisx(f.Type).Type == nil {
300 Fatalf("receiver with no type on %v method %v %v\n", mt, f.Sym, f)
311 // get receiver type for this particular method.
312 // if pointer receiver but non-pointer t and
313 // this is not an embedded pointer inside a struct,
314 // method does not apply.
315 this := getthisx(f.Type).Type.Type
317 if Isptr[this.Etype] && this.Type == t {
320 if Isptr[this.Etype] && !Isptr[t.Etype] && f.Embedded != 2 && !isifacemethod(f.Type) {
325 ms = append(ms, &sig)
327 sig.name = method.Name
328 if !exportname(method.Name) {
329 if method.Pkg == nil {
330 Fatalf("methods: missing package")
335 sig.isym = methodsym(method, it, 1)
336 sig.tsym = methodsym(method, t, 0)
337 sig.type_ = methodfunc(f.Type, t)
338 sig.mtype = methodfunc(f.Type, nil)
340 if sig.isym.Flags&SymSiggen == 0 {
341 sig.isym.Flags |= SymSiggen
342 if !Eqtype(this, it) || this.Width < Types[Tptr].Width {
343 compiling_wrappers = 1
344 genwrapper(it, f, sig.isym, 1)
345 compiling_wrappers = 0
349 if sig.tsym.Flags&SymSiggen == 0 {
350 sig.tsym.Flags |= SymSiggen
351 if !Eqtype(this, t) {
352 compiling_wrappers = 1
353 genwrapper(t, f, sig.tsym, 0)
354 compiling_wrappers = 0
359 sort.Sort(byMethodNameAndPackagePath(ms))
363 // imethods returns the methods of the interface type t, sorted by name.
364 func imethods(t *Type) []*Sig {
366 for f := t.Type; f != nil; f = f.Down {
367 if f.Etype != TFIELD {
368 Fatalf("imethods: not field")
370 if f.Type.Etype != TFUNC || f.Sym == nil {
377 if !exportname(method.Name) {
378 if method.Pkg == nil {
379 Fatalf("imethods: missing package")
386 sig.type_ = methodfunc(f.Type, nil)
388 if n := len(methods); n > 0 {
390 if !(siglt(last, &sig)) {
391 Fatalf("sigcmp vs sortinter %s %s", last.name, sig.name)
394 methods = append(methods, &sig)
396 // Compiler can only refer to wrappers for non-blank methods.
397 if isblanksym(method) {
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 isym := methodsym(method, t, 0)
407 if isym.Flags&SymSiggen == 0 {
408 isym.Flags |= SymSiggen
409 genwrapper(t, f, isym, 0)
416 var dimportpath_gopkg *Pkg
418 func dimportpath(p *Pkg) {
419 if p.Pathsym != nil {
423 // If we are compiling the runtime package, there are two runtime packages around
424 // -- localpkg and Runtimepkg. We don't want to produce import path symbols for
425 // both of them, so just produce one for localpkg.
426 if myimportpath == "runtime" && p == Runtimepkg {
430 if dimportpath_gopkg == nil {
431 dimportpath_gopkg = mkpkg("go")
432 dimportpath_gopkg.Name = "go"
435 nam := "importpath." + p.Prefix + "."
437 n := Nod(ONAME, nil, nil)
438 n.Sym = Pkglookup(nam, dimportpath_gopkg)
445 // Note: myimportpath != "", or else dgopkgpath won't call dimportpath.
446 gdatastring(n, myimportpath)
448 gdatastring(n, p.Path)
450 ggloblsym(n.Sym, int32(Types[TSTRING].Width), obj.DUPOK|obj.RODATA)
453 func dgopkgpath(s *Sym, ot int, pkg *Pkg) int {
455 return dgostringptr(s, ot, "")
458 if pkg == localpkg && myimportpath == "" {
459 // If we don't know the full path of the package being compiled (i.e. -p
460 // was not passed on the compiler command line), emit reference to
461 // go.importpath.""., which 6l will rewrite using the correct import path.
462 // Every package that imports this one directly defines the symbol.
466 ns = Pkglookup("importpath.\"\".", mkpkg("go"))
468 return dsymptr(s, ot, ns, 0)
472 return dsymptr(s, ot, pkg.Pathsym, 0)
476 // ../../../../runtime/type.go:/uncommonType
477 func dextratype(sym *Sym, off int, t *Type, ptroff int) int {
479 if t.Sym == nil && len(m) == 0 {
483 // fill in *extraType pointer in header
484 off = int(Rnd(int64(off), int64(Widthptr)))
486 dsymptr(sym, ptroff, sym, off)
488 for _, a := range m {
494 if t.Sym != nil && t != Types[t.Etype] && t != errortype {
495 ot = dgopkgpath(s, ot, t.Sym.Pkg)
497 ot = dgostringptr(s, ot, "")
501 ot = dsymptr(s, ot, s, ot+Widthptr+2*Widthint)
504 ot = duintxx(s, ot, uint64(n), Widthint)
505 ot = duintxx(s, ot, uint64(n), Widthint)
508 for _, a := range m {
510 // ../../../../runtime/type.go:/method
511 ot = dgostringptr(s, ot, a.name)
513 ot = dgopkgpath(s, ot, a.pkg)
514 ot = dsymptr(s, ot, dtypesym(a.mtype), 0)
515 ot = dsymptr(s, ot, dtypesym(a.type_), 0)
517 ot = dsymptr(s, ot, a.isym, 0)
519 ot = duintptr(s, ot, 0)
522 ot = dsymptr(s, ot, a.tsym, 0)
524 ot = duintptr(s, ot, 0)
535 TUINT8: obj.KindUint8,
536 TINT16: obj.KindInt16,
537 TUINT16: obj.KindUint16,
538 TINT32: obj.KindInt32,
539 TUINT32: obj.KindUint32,
540 TINT64: obj.KindInt64,
541 TUINT64: obj.KindUint64,
542 TUINTPTR: obj.KindUintptr,
543 TFLOAT32: obj.KindFloat32,
544 TFLOAT64: obj.KindFloat64,
546 TSTRING: obj.KindString,
549 TSTRUCT: obj.KindStruct,
550 TINTER: obj.KindInterface,
553 TARRAY: obj.KindArray,
555 TCOMPLEX64: obj.KindComplex64,
556 TCOMPLEX128: obj.KindComplex128,
557 TUNSAFEPTR: obj.KindUnsafePointer,
560 func haspointers(t *Type) bool {
561 if t.Haspointers != 0 {
562 return t.Haspointers-1 != 0
586 if t.Bound < 0 { // slice
591 if t.Bound == 0 { // empty array
596 ret = haspointers(t.Type)
600 for t1 := t.Type; t1 != nil; t1 = t1.Down {
601 if haspointers(t1.Type) {
620 Fatalf("haspointers: unexpected type, %v", t)
623 t.Haspointers = 1 + uint8(obj.Bool2int(ret))
627 // typeptrdata returns the length in bytes of the prefix of t
628 // containing pointer data. Anything after this offset is scalar data.
629 func typeptrdata(t *Type) int64 {
641 return int64(Widthptr)
644 // struct { byte *str; intgo len; }
645 return int64(Widthptr)
648 // struct { Itab *tab; void *data; } or
649 // struct { Type *type; void *data; }
650 return 2 * int64(Widthptr)
654 // struct { byte *array; uintgo len; uintgo cap; }
655 return int64(Widthptr)
657 // haspointers already eliminated t.Bound == 0.
658 return (t.Bound-1)*t.Type.Width + typeptrdata(t.Type)
661 // Find the last field that has pointers.
662 var lastPtrField *Type
663 for t1 := t.Type; t1 != nil; t1 = t1.Down {
664 if haspointers(t1.Type) {
668 return lastPtrField.Width + typeptrdata(lastPtrField.Type)
671 Fatalf("typeptrdata: unexpected type, %v", t)
677 // ../../runtime/type.go:/commonType
679 var dcommontype_algarray *Sym
681 func dcommontype(s *Sym, ot int, t *Type) int {
683 Fatalf("dcommontype %d", ot)
686 sizeofAlg := 2 * Widthptr
687 if dcommontype_algarray == nil {
688 dcommontype_algarray = Pkglookup("algarray", Runtimepkg)
693 if alg < 0 || alg == AMEM {
698 if !Isptr[t.Etype] && (t.Sym != nil || methods(tptr) != nil) {
699 sptr := dtypesym(tptr)
700 r := obj.Addrel(Linksym(s))
704 r.Type = obj.R_USETYPE
707 gcsym, useGCProg, ptrdata := dgcsym(t)
709 // ../../../../reflect/type.go:/^type.rtype
710 // actual type structure
711 // type rtype struct {
724 ot = duintptr(s, ot, uint64(t.Width))
725 ot = duintptr(s, ot, uint64(ptrdata))
727 ot = duint32(s, ot, typehash(t))
728 ot = duint8(s, ot, 0) // unused
730 // runtime (and common sense) expects alignment to be a power of two.
737 Fatalf("invalid alignment %d for %v", t.Align, t)
739 ot = duint8(s, ot, t.Align) // align
740 ot = duint8(s, ot, t.Align) // fieldAlign
743 if t.Etype == TARRAY && t.Bound < 0 {
747 i |= obj.KindNoPointers
749 if isdirectiface(t) {
750 i |= obj.KindDirectIface
755 ot = duint8(s, ot, uint8(i)) // kind
757 ot = dsymptr(s, ot, dcommontype_algarray, alg*sizeofAlg)
759 ot = dsymptr(s, ot, algsym, 0)
761 ot = dsymptr(s, ot, gcsym, 0) // gcdata
763 p := Tconv(t, obj.FmtLeft|obj.FmtUnsigned)
765 _, symdata := stringsym(p) // string
766 ot = dsymptr(s, ot, symdata, 0)
767 ot = duintxx(s, ot, uint64(len(p)), Widthint)
768 //fmt.Printf("dcommontype: %s\n", p)
770 // skip pointer to extraType,
771 // which follows the rest of this type structure.
772 // caller will fill in if needed.
773 // otherwise linker will assume 0.
779 func typesym(t *Type) *Sym {
780 return Pkglookup(Tconv(t, obj.FmtLeft), typepkg)
783 func tracksym(t *Type) *Sym {
784 return Pkglookup(Tconv(t.Outer, obj.FmtLeft)+"."+t.Sym.Name, trackpkg)
787 func typelinksym(t *Type) *Sym {
788 // %-uT is what the generated Type's string field says.
789 // It uses (ambiguous) package names instead of import paths.
790 // %-T is the complete, unambiguous type name.
791 // We want the types to end up sorted by string field,
792 // so use that first in the name, and then add :%-T to
793 // disambiguate. We use a tab character as the separator to
794 // ensure the types appear sorted by their string field. The
795 // names are a little long but they are discarded by the linker
796 // and do not end up in the symbol table of the final binary.
797 p := Tconv(t, obj.FmtLeft|obj.FmtUnsigned) + "\t" + Tconv(t, obj.FmtLeft)
799 s := Pkglookup(p, typelinkpkg)
801 //print("typelinksym: %s -> %+S\n", p, s);
806 func typesymprefix(prefix string, t *Type) *Sym {
807 p := prefix + "." + Tconv(t, obj.FmtLeft)
808 s := Pkglookup(p, typepkg)
810 //print("algsym: %s -> %+S\n", p, s);
815 func typenamesym(t *Type) *Sym {
816 if t == nil || (Isptr[t.Etype] && t.Type == nil) || isideal(t) {
817 Fatalf("typename %v", t)
821 n := Nod(ONAME, nil, nil)
823 n.Type = Types[TUINT8]
831 signatlist = list(signatlist, typenod(t))
837 func typename(t *Type) *Node {
839 n := Nod(OADDR, s.Def, nil)
840 n.Type = Ptrto(s.Def.Type)
847 func weaktypesym(t *Type) *Sym {
848 p := Tconv(t, obj.FmtLeft)
849 s := Pkglookup(p, weaktypepkg)
851 //print("weaktypesym: %s -> %+S\n", p, s);
856 // isreflexive reports whether t has a reflexive equality operator.
857 // That is, if x==x for all x of type t.
858 func isreflexive(t *Type) bool {
888 Fatalf("slice can't be a map key: %v", t)
890 return isreflexive(t.Type)
893 for t1 := t.Type; t1 != nil; t1 = t1.Down {
894 if !isreflexive(t1.Type) {
901 Fatalf("bad type for map key: %v", t)
906 // needkeyupdate reports whether map updates with t as a key
907 // need the key to be updated.
908 func needkeyupdate(t *Type) bool {
928 case TFLOAT32, // floats can be +0/-0
933 TSTRING: // strings might have smaller backing stores
938 Fatalf("slice can't be a map key: %v", t)
940 return needkeyupdate(t.Type)
943 for t1 := t.Type; t1 != nil; t1 = t1.Down {
944 if needkeyupdate(t1.Type) {
951 Fatalf("bad type for map key: %v", t)
956 func dtypesym(t *Type) *Sym {
957 // Replace byte, rune aliases with real type.
958 // They've been separate internally to make error messages
959 // better, but we have to merge them in the reflect tables.
960 if t == bytetype || t == runetype {
965 Fatalf("dtypesym %v", t)
969 if s.Flags&SymSiggen != 0 {
974 // special case (look for runtime below):
975 // when compiling package runtime,
976 // emit the type structures for int, float, etc.
979 if Isptr[t.Etype] && t.Sym == nil && t.Type.Sym != nil {
983 if tbase.Sym == nil {
987 if myimportpath == "runtime" && (tbase == Types[tbase.Etype] || tbase == bytetype || tbase == runetype || tbase == errortype) { // int, float, etc
991 // named types from other files are defined only by those files
992 if tbase.Sym != nil && !tbase.Local {
995 if isforw[tbase.Etype] {
1004 ot = dcommontype(s, ot, t)
1005 xt = ot - 1*Widthptr
1009 // ../../../../runtime/type.go:/arrayType
1010 s1 := dtypesym(t.Type)
1014 t2.Bound = -1 // slice
1016 ot = dcommontype(s, ot, t)
1017 xt = ot - 1*Widthptr
1018 ot = dsymptr(s, ot, s1, 0)
1019 ot = dsymptr(s, ot, s2, 0)
1020 ot = duintptr(s, ot, uint64(t.Bound))
1022 // ../../../../runtime/type.go:/sliceType
1023 s1 := dtypesym(t.Type)
1025 ot = dcommontype(s, ot, t)
1026 xt = ot - 1*Widthptr
1027 ot = dsymptr(s, ot, s1, 0)
1030 // ../../../../runtime/type.go:/chanType
1032 s1 := dtypesym(t.Type)
1034 ot = dcommontype(s, ot, t)
1035 xt = ot - 1*Widthptr
1036 ot = dsymptr(s, ot, s1, 0)
1037 ot = duintptr(s, ot, uint64(t.Chan))
1040 for t1 := getthisx(t).Type; t1 != nil; t1 = t1.Down {
1044 for t1 := getinargx(t).Type; t1 != nil; t1 = t1.Down {
1049 for t1 := getoutargx(t).Type; t1 != nil; t1 = t1.Down {
1053 ot = dcommontype(s, ot, t)
1054 xt = ot - 1*Widthptr
1055 ot = duint8(s, ot, uint8(obj.Bool2int(isddd)))
1057 // two slice headers: in and out.
1058 ot = int(Rnd(int64(ot), int64(Widthptr)))
1060 ot = dsymptr(s, ot, s, ot+2*(Widthptr+2*Widthint))
1061 n := t.Thistuple + t.Intuple
1062 ot = duintxx(s, ot, uint64(n), Widthint)
1063 ot = duintxx(s, ot, uint64(n), Widthint)
1064 ot = dsymptr(s, ot, s, ot+1*(Widthptr+2*Widthint)+n*Widthptr)
1065 ot = duintxx(s, ot, uint64(t.Outtuple), Widthint)
1066 ot = duintxx(s, ot, uint64(t.Outtuple), Widthint)
1069 for t1 := getthisx(t).Type; t1 != nil; t1 = t1.Down {
1070 ot = dsymptr(s, ot, dtypesym(t1.Type), 0)
1073 for t1 := getinargx(t).Type; t1 != nil; t1 = t1.Down {
1074 ot = dsymptr(s, ot, dtypesym(t1.Type), 0)
1077 for t1 := getoutargx(t).Type; t1 != nil; t1 = t1.Down {
1078 ot = dsymptr(s, ot, dtypesym(t1.Type), 0)
1085 for _, a := range m {
1089 // ../../../../runtime/type.go:/interfaceType
1090 ot = dcommontype(s, ot, t)
1092 xt = ot - 1*Widthptr
1093 ot = dsymptr(s, ot, s, ot+Widthptr+2*Widthint)
1094 ot = duintxx(s, ot, uint64(n), Widthint)
1095 ot = duintxx(s, ot, uint64(n), Widthint)
1096 for _, a := range m {
1097 // ../../../../runtime/type.go:/imethod
1098 ot = dgostringptr(s, ot, a.name)
1100 ot = dgopkgpath(s, ot, a.pkg)
1101 ot = dsymptr(s, ot, dtypesym(a.type_), 0)
1104 // ../../../../runtime/type.go:/mapType
1106 s1 := dtypesym(t.Down)
1108 s2 := dtypesym(t.Type)
1109 s3 := dtypesym(mapbucket(t))
1110 s4 := dtypesym(hmap(t))
1111 ot = dcommontype(s, ot, t)
1112 xt = ot - 1*Widthptr
1113 ot = dsymptr(s, ot, s1, 0)
1114 ot = dsymptr(s, ot, s2, 0)
1115 ot = dsymptr(s, ot, s3, 0)
1116 ot = dsymptr(s, ot, s4, 0)
1117 if t.Down.Width > MAXKEYSIZE {
1118 ot = duint8(s, ot, uint8(Widthptr))
1119 ot = duint8(s, ot, 1) // indirect
1121 ot = duint8(s, ot, uint8(t.Down.Width))
1122 ot = duint8(s, ot, 0) // not indirect
1125 if t.Type.Width > MAXVALSIZE {
1126 ot = duint8(s, ot, uint8(Widthptr))
1127 ot = duint8(s, ot, 1) // indirect
1129 ot = duint8(s, ot, uint8(t.Type.Width))
1130 ot = duint8(s, ot, 0) // not indirect
1133 ot = duint16(s, ot, uint16(mapbucket(t).Width))
1134 ot = duint8(s, ot, uint8(obj.Bool2int(isreflexive(t.Down))))
1135 ot = duint8(s, ot, uint8(obj.Bool2int(needkeyupdate(t.Down))))
1137 case TPTR32, TPTR64:
1138 if t.Type.Etype == TANY {
1139 // ../../../../runtime/type.go:/UnsafePointerType
1140 ot = dcommontype(s, ot, t)
1145 // ../../../../runtime/type.go:/ptrType
1146 s1 := dtypesym(t.Type)
1148 ot = dcommontype(s, ot, t)
1149 xt = ot - 1*Widthptr
1150 ot = dsymptr(s, ot, s1, 0)
1152 // ../../../../runtime/type.go:/structType
1153 // for security, only the exported fields.
1157 for t1 := t.Type; t1 != nil; t1 = t1.Down {
1162 ot = dcommontype(s, ot, t)
1163 xt = ot - 1*Widthptr
1164 ot = dsymptr(s, ot, s, ot+Widthptr+2*Widthint)
1165 ot = duintxx(s, ot, uint64(n), Widthint)
1166 ot = duintxx(s, ot, uint64(n), Widthint)
1167 for t1 := t.Type; t1 != nil; t1 = t1.Down {
1168 // ../../../../runtime/type.go:/structField
1169 if t1.Sym != nil && t1.Embedded == 0 {
1170 ot = dgostringptr(s, ot, t1.Sym.Name)
1171 if exportname(t1.Sym.Name) {
1172 ot = dgostringptr(s, ot, "")
1174 ot = dgopkgpath(s, ot, t1.Sym.Pkg)
1177 ot = dgostringptr(s, ot, "")
1178 if t1.Type.Sym != nil &&
1179 (t1.Type.Sym.Pkg == builtinpkg || !exportname(t1.Type.Sym.Name)) {
1180 ot = dgopkgpath(s, ot, localpkg)
1182 ot = dgostringptr(s, ot, "")
1186 ot = dsymptr(s, ot, dtypesym(t1.Type), 0)
1187 ot = dgostrlitptr(s, ot, t1.Note)
1188 ot = duintptr(s, ot, uint64(t1.Width)) // field offset
1192 ot = dextratype(s, ot, t, xt)
1193 ggloblsym(s, int32(ot), int16(dupok|obj.RODATA))
1195 // generate typelink.foo pointing at s = type.foo.
1196 // The linker will leave a table of all the typelinks for
1197 // types in the binary, so reflect can find them.
1198 // We only need the link for unnamed composites that
1199 // we want be able to find.
1202 case TPTR32, TPTR64, TARRAY, TCHAN, TFUNC, TMAP:
1203 slink := typelinksym(t)
1204 dsymptr(slink, 0, s, 0)
1205 ggloblsym(slink, int32(Widthptr), int16(dupok|obj.RODATA))
1212 func dumptypestructs() {
1215 // copy types from externdcl list to signatlist
1216 for _, n := range externdcl {
1220 signatlist = list(signatlist, n)
1223 // process signatlist
1225 for l := signatlist; l != nil; l = l.Next {
1237 // generate import strings for imported packages
1238 for _, p := range pkgs {
1244 // do basic types if compiling package runtime.
1245 // they have to be in at least one package,
1246 // and runtime is always loaded implicitly,
1247 // so this is as good as any.
1248 // another possible choice would be package main,
1249 // but using runtime means fewer copies in .6 files.
1250 if myimportpath == "runtime" {
1251 for i := EType(1); i <= TBOOL; i++ {
1252 dtypesym(Ptrto(Types[i]))
1254 dtypesym(Ptrto(Types[TSTRING]))
1255 dtypesym(Ptrto(Types[TUNSAFEPTR]))
1257 // emit type structs for error and func(error) string.
1258 // The latter is the type of an auto-generated wrapper.
1259 dtypesym(Ptrto(errortype))
1261 dtypesym(functype(nil, list1(Nod(ODCLFIELD, nil, typenod(errortype))), list1(Nod(ODCLFIELD, nil, typenod(Types[TSTRING])))))
1263 // add paths for runtime and main, which 6l imports implicitly.
1264 dimportpath(Runtimepkg)
1267 dimportpath(racepkg)
1270 dimportpath(msanpkg)
1272 dimportpath(mkpkg("main"))
1276 func dalgsym(t *Type) *Sym {
1281 // dalgsym is only called for a type that needs an algorithm table,
1282 // which implies that the type is comparable (or else it would use ANOEQ).
1284 if algtype(t) == AMEM {
1285 // we use one algorithm table for all AMEM types of a given size
1286 p := fmt.Sprintf(".alg%d", t.Width)
1288 s = Pkglookup(p, typepkg)
1290 if s.Flags&SymAlgGen != 0 {
1293 s.Flags |= SymAlgGen
1295 // make hash closure
1296 p = fmt.Sprintf(".hashfunc%d", t.Width)
1298 hashfunc = Pkglookup(p, typepkg)
1301 ot = dsymptr(hashfunc, ot, Pkglookup("memhash_varlen", Runtimepkg), 0)
1302 ot = duintxx(hashfunc, ot, uint64(t.Width), Widthptr) // size encoded in closure
1303 ggloblsym(hashfunc, int32(ot), obj.DUPOK|obj.RODATA)
1305 // make equality closure
1306 p = fmt.Sprintf(".eqfunc%d", t.Width)
1308 eqfunc = Pkglookup(p, typepkg)
1311 ot = dsymptr(eqfunc, ot, Pkglookup("memequal_varlen", Runtimepkg), 0)
1312 ot = duintxx(eqfunc, ot, uint64(t.Width), Widthptr)
1313 ggloblsym(eqfunc, int32(ot), obj.DUPOK|obj.RODATA)
1315 // generate an alg table specific to this type
1316 s = typesymprefix(".alg", t)
1318 hash := typesymprefix(".hash", t)
1319 eq := typesymprefix(".eq", t)
1320 hashfunc = typesymprefix(".hashfunc", t)
1321 eqfunc = typesymprefix(".eqfunc", t)
1326 // make Go funcs (closures) for calling hash and equal from Go
1327 dsymptr(hashfunc, 0, hash, 0)
1329 ggloblsym(hashfunc, int32(Widthptr), obj.DUPOK|obj.RODATA)
1330 dsymptr(eqfunc, 0, eq, 0)
1331 ggloblsym(eqfunc, int32(Widthptr), obj.DUPOK|obj.RODATA)
1334 // ../../../../runtime/alg.go:/typeAlg
1337 ot = dsymptr(s, ot, hashfunc, 0)
1338 ot = dsymptr(s, ot, eqfunc, 0)
1339 ggloblsym(s, int32(ot), obj.DUPOK|obj.RODATA)
1343 // maxPtrmaskBytes is the maximum length of a GC ptrmask bitmap,
1344 // which holds 1-bit entries describing where pointers are in a given type.
1345 // 16 bytes is enough to describe 128 pointer-sized words, 512 or 1024 bytes
1346 // depending on the system. Above this length, the GC information is
1347 // recorded as a GC program, which can express repetition compactly.
1348 // In either form, the information is used by the runtime to initialize the
1349 // heap bitmap, and for large types (like 128 or more words), they are
1350 // roughly the same speed. GC programs are never much larger and often
1351 // more compact. (If large arrays are involved, they can be arbitrarily more
1354 // The cutoff must be large enough that any allocation large enough to
1355 // use a GC program is large enough that it does not share heap bitmap
1356 // bytes with any other objects, allowing the GC program execution to
1357 // assume an aligned start and not use atomic operations. In the current
1358 // runtime, this means all malloc size classes larger than the cutoff must
1359 // be multiples of four words. On 32-bit systems that's 16 bytes, and
1360 // all size classes >= 16 bytes are 16-byte aligned, so no real constraint.
1361 // On 64-bit systems, that's 32 bytes, and 32-byte alignment is guaranteed
1362 // for size classes >= 256 bytes. On a 64-bit system, 256 bytes allocated
1363 // is 32 pointers, the bits for which fit in 4 bytes. So maxPtrmaskBytes
1366 // We used to use 16 because the GC programs do have some constant overhead
1367 // to get started, and processing 128 pointers seems to be enough to
1368 // amortize that overhead well.
1370 // To make sure that the runtime's chansend can call typeBitsBulkBarrier,
1371 // we raised the limit to 2048, so that even 32-bit systems are guaranteed to
1372 // use bitmaps for objects up to 64 kB in size.
1374 // Also known to reflect/type.go.
1376 const maxPtrmaskBytes = 2048
1378 // dgcsym emits and returns a data symbol containing GC information for type t,
1379 // along with a boolean reporting whether the UseGCProg bit should be set in
1380 // the type kind, and the ptrdata field to record in the reflect type information.
1381 func dgcsym(t *Type) (sym *Sym, useGCProg bool, ptrdata int64) {
1382 ptrdata = typeptrdata(t)
1383 if ptrdata/int64(Widthptr) <= maxPtrmaskBytes*8 {
1389 sym, ptrdata = dgcprog(t)
1393 // dgcptrmask emits and returns the symbol containing a pointer mask for type t.
1394 func dgcptrmask(t *Type) *Sym {
1395 ptrmask := make([]byte, (typeptrdata(t)/int64(Widthptr)+7)/8)
1396 fillptrmask(t, ptrmask)
1397 p := fmt.Sprintf("gcbits.%x", ptrmask)
1399 sym := Pkglookup(p, Runtimepkg)
1400 if sym.Flags&SymUniq == 0 {
1401 sym.Flags |= SymUniq
1402 for i, x := range ptrmask {
1405 ggloblsym(sym, int32(len(ptrmask)), obj.DUPOK|obj.RODATA|obj.LOCAL)
1410 // fillptrmask fills in ptrmask with 1s corresponding to the
1411 // word offsets in t that hold pointers.
1412 // ptrmask is assumed to fit at least typeptrdata(t)/Widthptr bits.
1413 func fillptrmask(t *Type, ptrmask []byte) {
1414 for i := range ptrmask {
1417 if !haspointers(t) {
1421 vec := bvalloc(8 * int32(len(ptrmask)))
1423 onebitwalktype1(t, &xoffset, vec)
1425 nptr := typeptrdata(t) / int64(Widthptr)
1426 for i := int64(0); i < nptr; i++ {
1427 if bvget(vec, int32(i)) == 1 {
1428 ptrmask[i/8] |= 1 << (uint(i) % 8)
1433 // dgcprog emits and returns the symbol containing a GC program for type t
1434 // along with the size of the data described by the program (in the range [typeptrdata(t), t.Width]).
1435 // In practice, the size is typeptrdata(t) except for non-trivial arrays.
1436 // For non-trivial arrays, the program describes the full t.Width size.
1437 func dgcprog(t *Type) (*Sym, int64) {
1439 if t.Width == BADWIDTH {
1440 Fatalf("dgcprog: %v badwidth", t)
1442 sym := typesymprefix(".gcprog", t)
1446 offset := p.w.BitIndex() * int64(Widthptr)
1448 if ptrdata := typeptrdata(t); offset < ptrdata || offset > t.Width {
1449 Fatalf("dgcprog: %v: offset=%d but ptrdata=%d size=%d", t, offset, ptrdata, t.Width)
1454 type GCProg struct {
1460 var Debug_gcprog int // set by -d gcprog
1462 func (p *GCProg) init(sym *Sym) {
1464 p.symoff = 4 // first 4 bytes hold program length
1465 p.w.Init(p.writeByte)
1466 if Debug_gcprog > 0 {
1467 fmt.Fprintf(os.Stderr, "compile: start GCProg for %v\n", sym)
1468 p.w.Debug(os.Stderr)
1472 func (p *GCProg) writeByte(x byte) {
1473 p.symoff = duint8(p.sym, p.symoff, x)
1476 func (p *GCProg) end() {
1478 duint32(p.sym, 0, uint32(p.symoff-4))
1479 ggloblsym(p.sym, int32(p.symoff), obj.DUPOK|obj.RODATA|obj.LOCAL)
1480 if Debug_gcprog > 0 {
1481 fmt.Fprintf(os.Stderr, "compile: end GCProg for %v\n", p.sym)
1485 func (p *GCProg) emit(t *Type, offset int64) {
1487 if !haspointers(t) {
1490 if t.Width == int64(Widthptr) {
1491 p.w.Ptr(offset / int64(Widthptr))
1496 Fatalf("GCProg.emit: unexpected type %v", t)
1499 p.w.Ptr(offset / int64(Widthptr))
1502 p.w.Ptr(offset / int64(Widthptr))
1503 p.w.Ptr(offset/int64(Widthptr) + 1)
1507 p.w.Ptr(offset / int64(Widthptr))
1511 // should have been handled by haspointers check above
1512 Fatalf("GCProg.emit: empty array")
1515 // Flatten array-of-array-of-array to just a big array by multiplying counts.
1518 for Isfixedarray(elem) {
1523 if !p.w.ShouldRepeat(elem.Width/int64(Widthptr), count) {
1524 // Cheaper to just emit the bits.
1525 for i := int64(0); i < count; i++ {
1526 p.emit(elem, offset+i*elem.Width)
1530 p.emit(elem, offset)
1531 p.w.ZeroUntil((offset + elem.Width) / int64(Widthptr))
1532 p.w.Repeat(elem.Width/int64(Widthptr), count-1)
1535 for t1 := t.Type; t1 != nil; t1 = t1.Down {
1536 p.emit(t1.Type, offset+t1.Width)