Source file src/internal/abi/switch.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 abi
     6  
     7  import "internal/goarch"
     8  
     9  type InterfaceSwitch struct {
    10  	Cache  *InterfaceSwitchCache
    11  	NCases int
    12  
    13  	// Array of NCases elements.
    14  	// Each case must be a non-empty interface type.
    15  	Cases [1]*InterfaceType
    16  }
    17  
    18  type InterfaceSwitchCache struct {
    19  	Mask    uintptr                      // mask for index. Must be a power of 2 minus 1
    20  	Entries [1]InterfaceSwitchCacheEntry // Mask+1 entries total
    21  }
    22  
    23  type InterfaceSwitchCacheEntry struct {
    24  	// type of source value (a *Type)
    25  	Typ uintptr
    26  	// case # to dispatch to
    27  	Case int
    28  	// itab to use for resulting case variable (a *runtime.itab)
    29  	Itab uintptr
    30  }
    31  
    32  func UseInterfaceSwitchCache(arch goarch.ArchFamilyType) bool {
    33  	// We need an atomic load instruction to make the cache multithreaded-safe.
    34  	// (AtomicLoadPtr needs to be implemented in cmd/compile/internal/ssa/_gen/ARCH.rules.)
    35  	switch arch {
    36  	case goarch.AMD64, goarch.ARM64, goarch.LOONG64, goarch.MIPS, goarch.MIPS64, goarch.PPC64, goarch.RISCV64, goarch.S390X:
    37  		return true
    38  	default:
    39  		return false
    40  	}
    41  }
    42  
    43  type TypeAssert struct {
    44  	Cache   *TypeAssertCache
    45  	Inter   *InterfaceType
    46  	CanFail bool
    47  }
    48  type TypeAssertCache struct {
    49  	Mask    uintptr
    50  	Entries [1]TypeAssertCacheEntry
    51  }
    52  type TypeAssertCacheEntry struct {
    53  	// type of source value (a *runtime._type)
    54  	Typ uintptr
    55  	// itab to use for result (a *runtime.itab)
    56  	// nil if CanFail is set and conversion would fail.
    57  	Itab uintptr
    58  }
    59  

View as plain text