github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/engine/wazevo/ssa/type.go (about)

     1  package ssa
     2  
     3  type Type byte
     4  
     5  const (
     6  	typeInvalid Type = iota
     7  
     8  	// TODO: add 8, 16 bit types when it's needed for optimizations.
     9  
    10  	// TypeI32 represents an integer type with 32 bits.
    11  	TypeI32
    12  
    13  	// TypeI64 represents an integer type with 64 bits.
    14  	TypeI64
    15  
    16  	// TypeF32 represents 32-bit floats in the IEEE 754.
    17  	TypeF32
    18  
    19  	// TypeF64 represents 64-bit floats in the IEEE 754.
    20  	TypeF64
    21  
    22  	// TypeV128 represents 128-bit SIMD vectors.
    23  	TypeV128
    24  )
    25  
    26  // String implements fmt.Stringer.
    27  func (t Type) String() (ret string) {
    28  	switch t {
    29  	case typeInvalid:
    30  		return "invalid"
    31  	case TypeI32:
    32  		return "i32"
    33  	case TypeI64:
    34  		return "i64"
    35  	case TypeF32:
    36  		return "f32"
    37  	case TypeF64:
    38  		return "f64"
    39  	case TypeV128:
    40  		return "v128"
    41  	default:
    42  		panic(int(t))
    43  	}
    44  }
    45  
    46  // IsInt returns true if the type is an integer type.
    47  func (t Type) IsInt() bool {
    48  	return t == TypeI32 || t == TypeI64
    49  }
    50  
    51  // Bits returns the number of bits required to represent the type.
    52  func (t Type) Bits() byte {
    53  	switch t {
    54  	case TypeI32, TypeF32:
    55  		return 32
    56  	case TypeI64, TypeF64:
    57  		return 64
    58  	case TypeV128:
    59  		return 128
    60  	default:
    61  		panic(int(t))
    62  	}
    63  }
    64  
    65  // Size returns the number of bytes required to represent the type.
    66  func (t Type) Size() byte {
    67  	return t.Bits() / 8
    68  }
    69  
    70  func (t Type) invalid() bool {
    71  	return t == typeInvalid
    72  }
    73  
    74  // VecLane represents a lane in a SIMD vector.
    75  type VecLane byte
    76  
    77  const (
    78  	VecLaneInvalid VecLane = 1 + iota
    79  	VecLaneI8x16
    80  	VecLaneI16x8
    81  	VecLaneI32x4
    82  	VecLaneI64x2
    83  	VecLaneF32x4
    84  	VecLaneF64x2
    85  )
    86  
    87  // String implements fmt.Stringer.
    88  func (vl VecLane) String() (ret string) {
    89  	switch vl {
    90  	case VecLaneInvalid:
    91  		return "invalid"
    92  	case VecLaneI8x16:
    93  		return "i8x16"
    94  	case VecLaneI16x8:
    95  		return "i16x8"
    96  	case VecLaneI32x4:
    97  		return "i32x4"
    98  	case VecLaneI64x2:
    99  		return "i64x2"
   100  	case VecLaneF32x4:
   101  		return "f32x4"
   102  	case VecLaneF64x2:
   103  		return "f64x2"
   104  	default:
   105  		panic(int(vl))
   106  	}
   107  }