Source file src/go/types/sizes.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  // This file implements Sizes.
     6  
     7  package types
     8  
     9  // Sizes defines the sizing functions for package unsafe.
    10  type Sizes interface {
    11  	// Alignof returns the alignment of a variable of type T.
    12  	// Alignof must implement the alignment guarantees required by the spec.
    13  	Alignof(T Type) int64
    14  
    15  	// Offsetsof returns the offsets of the given struct fields, in bytes.
    16  	// Offsetsof must implement the offset guarantees required by the spec.
    17  	Offsetsof(fields []*Var) []int64
    18  
    19  	// Sizeof returns the size of a variable of type T.
    20  	// Sizeof must implement the size guarantees required by the spec.
    21  	Sizeof(T Type) int64
    22  }
    23  
    24  // StdSizes is a convenience type for creating commonly used Sizes.
    25  // It makes the following simplifying assumptions:
    26  //
    27  //	- The size of explicitly sized basic types (int16, etc.) is the
    28  //	  specified size.
    29  //	- The size of strings and interfaces is 2*WordSize.
    30  //	- The size of slices is 3*WordSize.
    31  //	- The size of an array of n elements corresponds to the size of
    32  //	  a struct of n consecutive fields of the array's element type.
    33  //      - The size of a struct is the offset of the last field plus that
    34  //	  field's size. As with all element types, if the struct is used
    35  //	  in an array its size must first be aligned to a multiple of the
    36  //	  struct's alignment.
    37  //	- All other types have size WordSize.
    38  //	- Arrays and structs are aligned per spec definition; all other
    39  //	  types are naturally aligned with a maximum alignment MaxAlign.
    40  //
    41  // *StdSizes implements Sizes.
    42  //
    43  type StdSizes struct {
    44  	WordSize int64 // word size in bytes - must be >= 4 (32bits)
    45  	MaxAlign int64 // maximum alignment in bytes - must be >= 1
    46  }
    47  
    48  func (s *StdSizes) Alignof(T Type) int64 {
    49  	// For arrays and structs, alignment is defined in terms
    50  	// of alignment of the elements and fields, respectively.
    51  	switch t := under(T).(type) {
    52  	case *Array:
    53  		// spec: "For a variable x of array type: unsafe.Alignof(x)
    54  		// is the same as unsafe.Alignof(x[0]), but at least 1."
    55  		return s.Alignof(t.elem)
    56  	case *Struct:
    57  		// spec: "For a variable x of struct type: unsafe.Alignof(x)
    58  		// is the largest of the values unsafe.Alignof(x.f) for each
    59  		// field f of x, but at least 1."
    60  		max := int64(1)
    61  		for _, f := range t.fields {
    62  			if a := s.Alignof(f.typ); a > max {
    63  				max = a
    64  			}
    65  		}
    66  		return max
    67  	case *Slice, *Interface:
    68  		// Multiword data structures are effectively structs
    69  		// in which each element has size WordSize.
    70  		// Type parameters lead to variable sizes/alignments;
    71  		// StdSizes.Alignof won't be called for them.
    72  		assert(!isTypeParam(T))
    73  		return s.WordSize
    74  	case *Basic:
    75  		// Strings are like slices and interfaces.
    76  		if t.Info()&IsString != 0 {
    77  			return s.WordSize
    78  		}
    79  	case *TypeParam, *Union:
    80  		unreachable()
    81  	}
    82  	a := s.Sizeof(T) // may be 0
    83  	// spec: "For a variable x of any type: unsafe.Alignof(x) is at least 1."
    84  	if a < 1 {
    85  		return 1
    86  	}
    87  	// complex{64,128} are aligned like [2]float{32,64}.
    88  	if isComplex(T) {
    89  		a /= 2
    90  	}
    91  	if a > s.MaxAlign {
    92  		return s.MaxAlign
    93  	}
    94  	return a
    95  }
    96  
    97  func (s *StdSizes) Offsetsof(fields []*Var) []int64 {
    98  	offsets := make([]int64, len(fields))
    99  	var o int64
   100  	for i, f := range fields {
   101  		a := s.Alignof(f.typ)
   102  		o = align(o, a)
   103  		offsets[i] = o
   104  		o += s.Sizeof(f.typ)
   105  	}
   106  	return offsets
   107  }
   108  
   109  var basicSizes = [...]byte{
   110  	Bool:       1,
   111  	Int8:       1,
   112  	Int16:      2,
   113  	Int32:      4,
   114  	Int64:      8,
   115  	Uint8:      1,
   116  	Uint16:     2,
   117  	Uint32:     4,
   118  	Uint64:     8,
   119  	Float32:    4,
   120  	Float64:    8,
   121  	Complex64:  8,
   122  	Complex128: 16,
   123  }
   124  
   125  func (s *StdSizes) Sizeof(T Type) int64 {
   126  	switch t := under(T).(type) {
   127  	case *Basic:
   128  		assert(isTyped(T))
   129  		k := t.kind
   130  		if int(k) < len(basicSizes) {
   131  			if s := basicSizes[k]; s > 0 {
   132  				return int64(s)
   133  			}
   134  		}
   135  		if k == String {
   136  			return s.WordSize * 2
   137  		}
   138  	case *Array:
   139  		n := t.len
   140  		if n <= 0 {
   141  			return 0
   142  		}
   143  		// n > 0
   144  		a := s.Alignof(t.elem)
   145  		z := s.Sizeof(t.elem)
   146  		return align(z, a)*(n-1) + z
   147  	case *Slice:
   148  		return s.WordSize * 3
   149  	case *Struct:
   150  		n := t.NumFields()
   151  		if n == 0 {
   152  			return 0
   153  		}
   154  		offsets := s.Offsetsof(t.fields)
   155  		return offsets[n-1] + s.Sizeof(t.fields[n-1].typ)
   156  	case *Interface:
   157  		// Type parameters lead to variable sizes/alignments;
   158  		// StdSizes.Sizeof won't be called for them.
   159  		assert(!isTypeParam(T))
   160  		return s.WordSize * 2
   161  	case *TypeParam, *Union:
   162  		unreachable()
   163  	}
   164  	return s.WordSize // catch-all
   165  }
   166  
   167  // common architecture word sizes and alignments
   168  var gcArchSizes = map[string]*StdSizes{
   169  	"386":      {4, 4},
   170  	"arm":      {4, 4},
   171  	"arm64":    {8, 8},
   172  	"amd64":    {8, 8},
   173  	"amd64p32": {4, 8},
   174  	"mips":     {4, 4},
   175  	"mipsle":   {4, 4},
   176  	"mips64":   {8, 8},
   177  	"mips64le": {8, 8},
   178  	"ppc64":    {8, 8},
   179  	"ppc64le":  {8, 8},
   180  	"riscv64":  {8, 8},
   181  	"s390x":    {8, 8},
   182  	"sparc64":  {8, 8},
   183  	"wasm":     {8, 8},
   184  	// When adding more architectures here,
   185  	// update the doc string of SizesFor below.
   186  }
   187  
   188  // SizesFor returns the Sizes used by a compiler for an architecture.
   189  // The result is nil if a compiler/architecture pair is not known.
   190  //
   191  // Supported architectures for compiler "gc":
   192  // "386", "arm", "arm64", "amd64", "amd64p32", "mips", "mipsle",
   193  // "mips64", "mips64le", "ppc64", "ppc64le", "riscv64", "s390x", "sparc64", "wasm".
   194  func SizesFor(compiler, arch string) Sizes {
   195  	var m map[string]*StdSizes
   196  	switch compiler {
   197  	case "gc":
   198  		m = gcArchSizes
   199  	case "gccgo":
   200  		m = gccgoArchSizes
   201  	default:
   202  		return nil
   203  	}
   204  	s, ok := m[arch]
   205  	if !ok {
   206  		return nil
   207  	}
   208  	return s
   209  }
   210  
   211  // stdSizes is used if Config.Sizes == nil.
   212  var stdSizes = SizesFor("gc", "amd64")
   213  
   214  func (conf *Config) alignof(T Type) int64 {
   215  	if s := conf.Sizes; s != nil {
   216  		if a := s.Alignof(T); a >= 1 {
   217  			return a
   218  		}
   219  		panic("Config.Sizes.Alignof returned an alignment < 1")
   220  	}
   221  	return stdSizes.Alignof(T)
   222  }
   223  
   224  func (conf *Config) offsetsof(T *Struct) []int64 {
   225  	var offsets []int64
   226  	if T.NumFields() > 0 {
   227  		// compute offsets on demand
   228  		if s := conf.Sizes; s != nil {
   229  			offsets = s.Offsetsof(T.fields)
   230  			// sanity checks
   231  			if len(offsets) != T.NumFields() {
   232  				panic("Config.Sizes.Offsetsof returned the wrong number of offsets")
   233  			}
   234  			for _, o := range offsets {
   235  				if o < 0 {
   236  					panic("Config.Sizes.Offsetsof returned an offset < 0")
   237  				}
   238  			}
   239  		} else {
   240  			offsets = stdSizes.Offsetsof(T.fields)
   241  		}
   242  	}
   243  	return offsets
   244  }
   245  
   246  // offsetof returns the offset of the field specified via
   247  // the index sequence relative to typ. All embedded fields
   248  // must be structs (rather than pointer to structs).
   249  func (conf *Config) offsetof(typ Type, index []int) int64 {
   250  	var o int64
   251  	for _, i := range index {
   252  		s := under(typ).(*Struct)
   253  		o += conf.offsetsof(s)[i]
   254  		typ = s.fields[i].typ
   255  	}
   256  	return o
   257  }
   258  
   259  func (conf *Config) sizeof(T Type) int64 {
   260  	if s := conf.Sizes; s != nil {
   261  		if z := s.Sizeof(T); z >= 0 {
   262  			return z
   263  		}
   264  		panic("Config.Sizes.Sizeof returned a size < 0")
   265  	}
   266  	return stdSizes.Sizeof(T)
   267  }
   268  
   269  // align returns the smallest y >= x such that y % a == 0.
   270  func align(x, a int64) int64 {
   271  	y := x + a - 1
   272  	return y - y%a
   273  }
   274  

View as plain text