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

     1  // Copyright 2023 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 rttype allows the compiler to share type information with
     6  // the runtime. The shared type information is stored in
     7  // internal/abi. This package translates those types from the host
     8  // machine on which the compiler runs to the target machine on which
     9  // the compiled program will run. In particular, this package handles
    10  // layout differences between e.g. a 64 bit compiler and 32 bit
    11  // target.
    12  package rttype
    13  
    14  import (
    15  	"cmd/compile/internal/base"
    16  	"cmd/compile/internal/objw"
    17  	"cmd/compile/internal/types"
    18  	"cmd/internal/obj"
    19  	"internal/abi"
    20  	"reflect"
    21  )
    22  
    23  // The type structures shared with the runtime.
    24  var Type *types.Type
    25  
    26  var ArrayType *types.Type
    27  var ChanType *types.Type
    28  var FuncType *types.Type
    29  var InterfaceType *types.Type
    30  var MapType *types.Type
    31  var PtrType *types.Type
    32  var SliceType *types.Type
    33  var StructType *types.Type
    34  
    35  // Types that are parts of the types above.
    36  var IMethod *types.Type
    37  var Method *types.Type
    38  var StructField *types.Type
    39  var UncommonType *types.Type
    40  
    41  // Type switches and asserts
    42  var InterfaceSwitch *types.Type
    43  var TypeAssert *types.Type
    44  
    45  // Interface tables (itabs)
    46  var ITab *types.Type
    47  
    48  func Init() {
    49  	// Note: this has to be called explicitly instead of being
    50  	// an init function so it runs after the types package has
    51  	// been properly initialized.
    52  	Type = FromReflect(reflect.TypeFor[abi.Type]())
    53  	ArrayType = FromReflect(reflect.TypeFor[abi.ArrayType]())
    54  	ChanType = FromReflect(reflect.TypeFor[abi.ChanType]())
    55  	FuncType = FromReflect(reflect.TypeFor[abi.FuncType]())
    56  	InterfaceType = FromReflect(reflect.TypeFor[abi.InterfaceType]())
    57  	MapType = FromReflect(reflect.TypeFor[abi.MapType]())
    58  	PtrType = FromReflect(reflect.TypeFor[abi.PtrType]())
    59  	SliceType = FromReflect(reflect.TypeFor[abi.SliceType]())
    60  	StructType = FromReflect(reflect.TypeFor[abi.StructType]())
    61  
    62  	IMethod = FromReflect(reflect.TypeFor[abi.Imethod]())
    63  	Method = FromReflect(reflect.TypeFor[abi.Method]())
    64  	StructField = FromReflect(reflect.TypeFor[abi.StructField]())
    65  	UncommonType = FromReflect(reflect.TypeFor[abi.UncommonType]())
    66  
    67  	InterfaceSwitch = FromReflect(reflect.TypeFor[abi.InterfaceSwitch]())
    68  	TypeAssert = FromReflect(reflect.TypeFor[abi.TypeAssert]())
    69  
    70  	ITab = FromReflect(reflect.TypeFor[abi.ITab]())
    71  
    72  	// Make sure abi functions are correct. These functions are used
    73  	// by the linker which doesn't have the ability to do type layout,
    74  	// so we check the functions it uses here.
    75  	ptrSize := types.PtrSize
    76  	if got, want := int64(abi.CommonSize(ptrSize)), Type.Size(); got != want {
    77  		base.Fatalf("abi.CommonSize() == %d, want %d", got, want)
    78  	}
    79  	if got, want := int64(abi.StructFieldSize(ptrSize)), StructField.Size(); got != want {
    80  		base.Fatalf("abi.StructFieldSize() == %d, want %d", got, want)
    81  	}
    82  	if got, want := int64(abi.UncommonSize()), UncommonType.Size(); got != want {
    83  		base.Fatalf("abi.UncommonSize() == %d, want %d", got, want)
    84  	}
    85  	if got, want := int64(abi.TFlagOff(ptrSize)), Type.OffsetOf("TFlag"); got != want {
    86  		base.Fatalf("abi.TFlagOff() == %d, want %d", got, want)
    87  	}
    88  	if got, want := int64(abi.ITabTypeOff(ptrSize)), ITab.OffsetOf("Type"); got != want {
    89  		base.Fatalf("abi.ITabTypeOff() == %d, want %d", got, want)
    90  	}
    91  	for _, test := range []struct {
    92  		kind abi.Kind
    93  		typ  *types.Type
    94  		name string
    95  	}{
    96  		{abi.Struct, StructType, "Struct"},
    97  		{abi.Pointer, PtrType, "Pointer"},
    98  		{abi.Func, FuncType, "Func"},
    99  		{abi.Slice, SliceType, "Slice"},
   100  		{abi.Array, ArrayType, "Array"},
   101  		{abi.Chan, ChanType, "Chan"},
   102  		{abi.Map, MapType, "Map"},
   103  		{abi.Interface, InterfaceType, "Interface"},
   104  	} {
   105  		if got, want := int64(abi.RTypeSize(test.kind, ptrSize)), test.typ.Size(); got != want {
   106  			base.Fatalf("abi.RTypeSize(%s) == %d, want %d", test.name, got, want)
   107  		}
   108  	}
   109  }
   110  
   111  // FromReflect translates from a host type to the equivalent target type.
   112  func FromReflect(rt reflect.Type) *types.Type {
   113  	t := reflectToType(rt)
   114  	types.CalcSize(t)
   115  	return t
   116  }
   117  
   118  // reflectToType converts from a reflect.Type (which is a compiler
   119  // host type) to a *types.Type, which is a target type.  The result
   120  // must be CalcSize'd before using.
   121  func reflectToType(rt reflect.Type) *types.Type {
   122  	switch rt.Kind() {
   123  	case reflect.Bool:
   124  		return types.Types[types.TBOOL]
   125  	case reflect.Int:
   126  		return types.Types[types.TINT]
   127  	case reflect.Int8:
   128  		return types.Types[types.TINT8]
   129  	case reflect.Int16:
   130  		return types.Types[types.TINT16]
   131  	case reflect.Int32:
   132  		return types.Types[types.TINT32]
   133  	case reflect.Uint8:
   134  		return types.Types[types.TUINT8]
   135  	case reflect.Uint16:
   136  		return types.Types[types.TUINT16]
   137  	case reflect.Uint32:
   138  		return types.Types[types.TUINT32]
   139  	case reflect.Float32:
   140  		return types.Types[types.TFLOAT32]
   141  	case reflect.Float64:
   142  		return types.Types[types.TFLOAT64]
   143  	case reflect.Uintptr:
   144  		return types.Types[types.TUINTPTR]
   145  	case reflect.Ptr:
   146  		return types.NewPtr(reflectToType(rt.Elem()))
   147  	case reflect.Func, reflect.UnsafePointer:
   148  		// TODO: there's no mechanism to distinguish different pointer types,
   149  		// so we treat them all as unsafe.Pointer.
   150  		return types.Types[types.TUNSAFEPTR]
   151  	case reflect.Slice:
   152  		return types.NewSlice(reflectToType(rt.Elem()))
   153  	case reflect.Array:
   154  		return types.NewArray(reflectToType(rt.Elem()), int64(rt.Len()))
   155  	case reflect.Struct:
   156  		fields := make([]*types.Field, rt.NumField())
   157  		for i := 0; i < rt.NumField(); i++ {
   158  			f := rt.Field(i)
   159  			ft := reflectToType(f.Type)
   160  			fields[i] = &types.Field{Sym: &types.Sym{Name: f.Name}, Type: ft}
   161  		}
   162  		return types.NewStruct(fields)
   163  	case reflect.Chan:
   164  		return types.NewChan(reflectToType(rt.Elem()), types.ChanDir(rt.ChanDir()))
   165  	case reflect.String:
   166  		return types.Types[types.TSTRING]
   167  	case reflect.Complex128:
   168  		return types.Types[types.TCOMPLEX128]
   169  	default:
   170  		base.Fatalf("unhandled kind %s", rt.Kind())
   171  		return nil
   172  	}
   173  }
   174  
   175  // A Cursor represents a typed location inside a static variable where we
   176  // are going to write.
   177  type Cursor struct {
   178  	lsym   *obj.LSym
   179  	offset int64
   180  	typ    *types.Type
   181  }
   182  
   183  // NewCursor returns a cursor starting at lsym+off and having type t.
   184  func NewCursor(lsym *obj.LSym, off int64, t *types.Type) Cursor {
   185  	return Cursor{lsym: lsym, offset: off, typ: t}
   186  }
   187  
   188  // WritePtr writes a pointer "target" to the component at the location specified by c.
   189  func (c Cursor) WritePtr(target *obj.LSym) {
   190  	if c.typ.Kind() != types.TUNSAFEPTR && c.typ.Kind() != types.TPTR {
   191  		base.Fatalf("can't write ptr, it has kind %s", c.typ.Kind())
   192  	}
   193  	if target == nil {
   194  		objw.Uintptr(c.lsym, int(c.offset), 0)
   195  	} else {
   196  		objw.SymPtr(c.lsym, int(c.offset), target, 0)
   197  	}
   198  }
   199  func (c Cursor) WritePtrWeak(target *obj.LSym) {
   200  	if c.typ.Kind() != types.TUINTPTR {
   201  		base.Fatalf("can't write ptr, it has kind %s", c.typ.Kind())
   202  	}
   203  	objw.SymPtrWeak(c.lsym, int(c.offset), target, 0)
   204  }
   205  
   206  // WriteFuncPtrWeak writes a weak function entry into an itab method slot.
   207  func (c Cursor) WriteFuncPtrWeak(target *obj.LSym) {
   208  	if c.typ.Kind() != types.TUINTPTR {
   209  		base.Fatalf("can't write weak func ptr, it has kind %s", c.typ.Kind())
   210  	}
   211  	objw.SymFuncPtrWeak(c.lsym, int(c.offset), target)
   212  }
   213  func (c Cursor) WriteUintptr(val uint64) {
   214  	if c.typ.Kind() != types.TUINTPTR {
   215  		base.Fatalf("can't write uintptr, it has kind %s", c.typ.Kind())
   216  	}
   217  	objw.Uintptr(c.lsym, int(c.offset), val)
   218  }
   219  func (c Cursor) WriteUint32(val uint32) {
   220  	if c.typ.Kind() != types.TUINT32 {
   221  		base.Fatalf("can't write uint32, it has kind %s", c.typ.Kind())
   222  	}
   223  	objw.Uint32(c.lsym, int(c.offset), val)
   224  }
   225  func (c Cursor) WriteUint16(val uint16) {
   226  	if c.typ.Kind() != types.TUINT16 {
   227  		base.Fatalf("can't write uint16, it has kind %s", c.typ.Kind())
   228  	}
   229  	objw.Uint16(c.lsym, int(c.offset), val)
   230  }
   231  func (c Cursor) WriteUint8(val uint8) {
   232  	if c.typ.Kind() != types.TUINT8 {
   233  		base.Fatalf("can't write uint8, it has kind %s", c.typ.Kind())
   234  	}
   235  	objw.Uint8(c.lsym, int(c.offset), val)
   236  }
   237  func (c Cursor) WriteInt(val int64) {
   238  	if c.typ.Kind() != types.TINT {
   239  		base.Fatalf("can't write int, it has kind %s", c.typ.Kind())
   240  	}
   241  	objw.Uintptr(c.lsym, int(c.offset), uint64(val))
   242  }
   243  func (c Cursor) WriteInt32(val int32) {
   244  	if c.typ.Kind() != types.TINT32 {
   245  		base.Fatalf("can't write int32, it has kind %s", c.typ.Kind())
   246  	}
   247  	objw.Uint32(c.lsym, int(c.offset), uint32(val))
   248  }
   249  func (c Cursor) WriteBool(val bool) {
   250  	if c.typ.Kind() != types.TBOOL {
   251  		base.Fatalf("can't write bool, it has kind %s", c.typ.Kind())
   252  	}
   253  	objw.Bool(c.lsym, int(c.offset), val)
   254  }
   255  
   256  // WriteSymPtrOff writes a "pointer" to the given symbol. The symbol
   257  // is encoded as a uint32 offset from the start of the section.
   258  func (c Cursor) WriteSymPtrOff(target *obj.LSym, weak bool) {
   259  	if c.typ.Kind() != types.TINT32 && c.typ.Kind() != types.TUINT32 {
   260  		base.Fatalf("can't write SymPtr, it has kind %s", c.typ.Kind())
   261  	}
   262  	if target == nil {
   263  		objw.Uint32(c.lsym, int(c.offset), 0)
   264  	} else if weak {
   265  		objw.SymPtrWeakOff(c.lsym, int(c.offset), target)
   266  	} else {
   267  		objw.SymPtrOff(c.lsym, int(c.offset), target)
   268  	}
   269  }
   270  
   271  // WriteSlice writes a slice header to c. The pointer is target+off, the len and cap fields are given.
   272  func (c Cursor) WriteSlice(target *obj.LSym, off, len, cap int64) {
   273  	if c.typ.Kind() != types.TSLICE {
   274  		base.Fatalf("can't write slice, it has kind %s", c.typ.Kind())
   275  	}
   276  	objw.SymPtr(c.lsym, int(c.offset), target, int(off))
   277  	objw.Uintptr(c.lsym, int(c.offset)+types.PtrSize, uint64(len))
   278  	objw.Uintptr(c.lsym, int(c.offset)+2*types.PtrSize, uint64(cap))
   279  	// TODO: ability to switch len&cap. Maybe not needed here, as every caller
   280  	// passes the same thing for both?
   281  	if len != cap {
   282  		base.Fatalf("len != cap (%d != %d)", len, cap)
   283  	}
   284  }
   285  
   286  // Reloc adds a relocation from the current cursor position.
   287  // Reloc fills in Off and Siz fields. Caller should fill in the rest (Type, others).
   288  func (c Cursor) Reloc(rel obj.Reloc) {
   289  	rel.Off = int32(c.offset)
   290  	rel.Siz = uint8(c.typ.Size())
   291  	c.lsym.AddRel(base.Ctxt, rel)
   292  }
   293  
   294  // Field selects the field with the given name from the struct pointed to by c.
   295  func (c Cursor) Field(name string) Cursor {
   296  	if c.typ.Kind() != types.TSTRUCT {
   297  		base.Fatalf("can't call Field on non-struct %v", c.typ)
   298  	}
   299  	for _, f := range c.typ.Fields() {
   300  		if f.Sym.Name == name {
   301  			return Cursor{lsym: c.lsym, offset: c.offset + f.Offset, typ: f.Type}
   302  		}
   303  	}
   304  	base.Fatalf("couldn't find field %s in %v", name, c.typ)
   305  	return Cursor{}
   306  }
   307  
   308  func (c Cursor) Elem(i int64) Cursor {
   309  	if c.typ.Kind() != types.TARRAY {
   310  		base.Fatalf("can't call Elem on non-array %v", c.typ)
   311  	}
   312  	if i < 0 || i >= c.typ.NumElem() {
   313  		base.Fatalf("element access out of bounds [%d] in [0:%d]", i, c.typ.NumElem())
   314  	}
   315  	elem := c.typ.Elem()
   316  	return Cursor{lsym: c.lsym, offset: c.offset + i*elem.Size(), typ: elem}
   317  }
   318  
   319  type ArrayCursor struct {
   320  	c Cursor // cursor pointing at first element
   321  	n int    // number of elements
   322  }
   323  
   324  // NewArrayCursor returns a cursor starting at lsym+off and having n copies of type t.
   325  func NewArrayCursor(lsym *obj.LSym, off int64, t *types.Type, n int) ArrayCursor {
   326  	return ArrayCursor{
   327  		c: NewCursor(lsym, off, t),
   328  		n: n,
   329  	}
   330  }
   331  
   332  // Elem selects element i of the array pointed to by c.
   333  func (a ArrayCursor) Elem(i int) Cursor {
   334  	if i < 0 || i >= a.n {
   335  		base.Fatalf("element index %d out of range [0:%d]", i, a.n)
   336  	}
   337  	return Cursor{lsym: a.c.lsym, offset: a.c.offset + int64(i)*a.c.typ.Size(), typ: a.c.typ}
   338  }
   339  
   340  // ModifyArray converts a cursor pointing at a type [k]T to a cursor pointing
   341  // at a type [n]T.
   342  // Also returns the size delta, aka (n-k)*sizeof(T).
   343  func (c Cursor) ModifyArray(n int) (ArrayCursor, int64) {
   344  	if c.typ.Kind() != types.TARRAY {
   345  		base.Fatalf("can't call ModifyArray on non-array %v", c.typ)
   346  	}
   347  	k := c.typ.NumElem()
   348  	return ArrayCursor{c: Cursor{lsym: c.lsym, offset: c.offset, typ: c.typ.Elem()}, n: n}, (int64(n) - k) * c.typ.Elem().Size()
   349  }
   350  

View as plain text