Source file src/cmd/compile/internal/typebits/typebits.go

     1  // Copyright 2013 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 typebits
     6  
     7  import (
     8  	"cmd/compile/internal/base"
     9  	"cmd/compile/internal/bitvec"
    10  	"cmd/compile/internal/types"
    11  )
    12  
    13  // NOTE: The bitmap for a specific type t could be cached in t after
    14  // the first run and then simply copied into bv at the correct offset
    15  // on future calls with the same type t.
    16  func Set(t *types.Type, off int64, bv bitvec.BitVec) {
    17  	if uint8(t.Alignment()) > 0 && off&int64(uint8(t.Alignment())-1) != 0 {
    18  		base.Fatalf("typebits.Set: invalid initial alignment: type %v has alignment %d, but offset is %v", t, uint8(t.Alignment()), off)
    19  	}
    20  	if !t.HasPointers() {
    21  		// Note: this case ensures that pointers to go:notinheap types
    22  		// are not considered pointers by garbage collection and stack copying.
    23  		return
    24  	}
    25  
    26  	switch t.Kind() {
    27  	case types.TPTR, types.TUNSAFEPTR, types.TFUNC, types.TCHAN, types.TMAP:
    28  		if off&int64(types.PtrSize-1) != 0 {
    29  			base.Fatalf("typebits.Set: invalid alignment, %v", t)
    30  		}
    31  		bv.Set(int32(off / int64(types.PtrSize))) // pointer
    32  
    33  	case types.TSTRING:
    34  		// struct { byte *str; intgo len; }
    35  		if off&int64(types.PtrSize-1) != 0 {
    36  			base.Fatalf("typebits.Set: invalid alignment, %v", t)
    37  		}
    38  		bv.Set(int32(off / int64(types.PtrSize))) //pointer in first slot
    39  
    40  	case types.TINTER:
    41  		// struct { Itab *tab;	void *data; }
    42  		// or, when isnilinter(t)==true:
    43  		// struct { Type *type; void *data; }
    44  		if off&int64(types.PtrSize-1) != 0 {
    45  			base.Fatalf("typebits.Set: invalid alignment, %v", t)
    46  		}
    47  		// The first word of an interface is a pointer, but we don't
    48  		// treat it as such.
    49  		// 1. If it is a non-empty interface, the pointer points to an itab
    50  		//    which is always in persistentalloc space.
    51  		// 2. If it is an empty interface, the pointer points to a _type.
    52  		//   a. If it is a compile-time-allocated type, it points into
    53  		//      the read-only data section.
    54  		//   b. If it is a reflect-allocated type, it points into the Go heap.
    55  		//      Reflect is responsible for keeping a reference to
    56  		//      the underlying type so it won't be GCd.
    57  		// If we ever have a moving GC, we need to change this for 2b (as
    58  		// well as scan itabs to update their itab._type fields).
    59  		bv.Set(int32(off/int64(types.PtrSize) + 1)) // pointer in second slot
    60  
    61  	case types.TSLICE:
    62  		// struct { byte *array; uintgo len; uintgo cap; }
    63  		if off&int64(types.PtrSize-1) != 0 {
    64  			base.Fatalf("typebits.Set: invalid TARRAY alignment, %v", t)
    65  		}
    66  		bv.Set(int32(off / int64(types.PtrSize))) // pointer in first slot (BitsPointer)
    67  
    68  	case types.TARRAY:
    69  		elt := t.Elem()
    70  		if elt.Size() == 0 {
    71  			// Short-circuit for #20739.
    72  			break
    73  		}
    74  		for i := int64(0); i < t.NumElem(); i++ {
    75  			Set(elt, off, bv)
    76  			off += elt.Size()
    77  		}
    78  
    79  	case types.TSTRUCT:
    80  		for _, f := range t.Fields().Slice() {
    81  			Set(f.Type, off+f.Offset, bv)
    82  		}
    83  
    84  	default:
    85  		base.Fatalf("typebits.Set: unexpected type, %v", t)
    86  	}
    87  }
    88  

View as plain text