Talvos  0.1
SPIR-V interpreter and dynamic analysis framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Type.h
Go to the documentation of this file.
1 // Copyright (c) 2018 the Talvos developers. All rights reserved.
2 //
3 // This file is distributed under a three-clause BSD license. For full license
4 // terms please see the LICENSE file distributed with this source code.
5 
8 
9 #ifndef TALVOS_TYPE_H
10 #define TALVOS_TYPE_H
11 
12 #include <cstdint>
13 #include <cstring>
14 #include <iosfwd>
15 #include <map>
16 #include <memory>
17 #include <vector>
18 
19 namespace talvos
20 {
21 
22 class Type;
23 
26 typedef std::vector<std::pair<const Type *, std::map<uint32_t, uint32_t>>>
28 
33 class Type
34 {
35 public:
37  enum TypeId
38  {
41  INT,
53  };
54 
57  uint32_t getBitWidth() const;
58 
60  uint32_t getDimensionality() const { return Dimensionality; }
61 
64  uint32_t getElementCount() const { return ElementCount; }
65 
68  size_t getElementOffset(uint64_t Index) const;
69 
73  const Type *getElementType(uint64_t Index = 0) const;
74 
77  const Type *getScalarType() const;
78 
81  size_t getSize() const { return ByteSize; };
82 
85  uint32_t getStorageClass() const;
86 
88  const std::map<uint32_t, uint32_t> &
89  getStructMemberDecorations(uint32_t Index) const;
90 
92  TypeId getTypeId() const { return Id; }
93 
95  bool isArray() const { return Id == ARRAY; }
96 
98  bool isArrayedImage() const { return Arrayed; }
99 
101  bool isBool() const { return Id == BOOL; }
102 
104  bool isComposite() const;
105 
107  bool isFloat() const { return Id == FLOAT; }
108 
110  bool isInt() const { return Id == INT; }
111 
113  bool isMatrix() const { return Id == MATRIX; }
114 
116  bool isPointer() const { return Id == POINTER; }
117 
119  bool isScalar() const;
120 
122  bool isStruct() const { return Id == STRUCT; }
123 
125  bool isVector() const { return Id == VECTOR; }
126 
129  friend std::ostream &operator<<(std::ostream &Stream, const Type *Ty);
130 
132  static std::unique_ptr<Type>
133  getArray(const Type *ElemType, uint32_t ElementCount, uint32_t ArrayStride);
134 
136  static std::unique_ptr<Type> getBool();
137 
139  static std::unique_ptr<Type> getFloat(uint32_t Width);
140 
142  static std::unique_ptr<Type> getInt(uint32_t Width);
143 
145  static std::unique_ptr<Type>
146  getFunction(const Type *ReturnType,
147  const std::vector<const Type *> &ArgTypes);
148 
150  static std::unique_ptr<Type> getImage(const Type *SampledType, uint32_t Dim,
151  uint32_t Depth, bool Arrayed, bool MS,
152  uint32_t Sampled, uint32_t Format);
153 
155  static std::unique_ptr<Type> getMatrix(const Type *ColumnType,
156  uint32_t NumColumns);
157 
159  static std::unique_ptr<Type>
160  getPointer(uint32_t StorageClass, const Type *ElemType, uint32_t ArrayStride);
161 
163  static std::unique_ptr<Type> getRuntimeArray(const Type *ElemType,
164  uint32_t ArrayStride);
165 
167  static std::unique_ptr<Type> getSampledImage(const Type *ImageType);
168 
170  static std::unique_ptr<Type> getSampler();
171 
173  static std::unique_ptr<Type>
174  getStruct(const StructElementTypeList &ElemTypes);
175 
177  static std::unique_ptr<Type> getVector(const Type *ElemType,
178  uint32_t ElemCount);
179 
181  static std::unique_ptr<Type> getVoid();
182 
183 private:
187  {
188  this->Id = Id;
189  this->ByteSize = ByteSize;
190  ElementCount = 1;
191  ElementType = nullptr;
192  };
193 
194  TypeId Id;
195 
196  size_t ByteSize;
197 
198  uint32_t BitWidth;
199 
200  uint32_t StorageClass;
201 
202  const Type *ElementType;
203  uint32_t ElementCount;
204  uint32_t ArrayStride;
205 
207  std::vector<size_t> ElementOffsets;
208 
209  const Type *ReturnType;
210  std::vector<const Type *> ArgumentTypes;
211 
212  uint32_t Dimensionality;
213  uint32_t Depth;
214  bool Arrayed;
216  uint32_t Sampled;
217  uint32_t Format;
218 };
219 
220 } // namespace talvos
221 
222 #endif
bool Multisampled
Valid for image types.
Definition: Type.h:215
static std::unique_ptr< Type > getVector(const Type *ElemType, uint32_t ElemCount)
Create a vector type.
Definition: Type.cpp:368
bool isBool() const
Returns true if this is a bool type.
Definition: Type.h:101
static std::unique_ptr< Type > getVoid()
Create a void type.
Definition: Type.cpp:377
bool isScalar() const
Returns true if this is a scalar type.
Definition: Type.cpp:77
uint32_t Depth
Valid for image types.
Definition: Type.h:213
bool isFloat() const
Returns true if this is a floating point type.
Definition: Type.h:107
uint32_t Sampled
Valid for image types.
Definition: Type.h:216
bool Arrayed
Valid for image types.
Definition: Type.h:214
static std::unique_ptr< Type > getPointer(uint32_t StorageClass, const Type *ElemType, uint32_t ArrayStride)
Create a pointer type.
Definition: Type.cpp:295
size_t getSize() const
Returns the size of this type in bytes.
Definition: Type.h:81
uint32_t StorageClass
Valid for pointer types.
Definition: Type.h:200
std::vector< size_t > ElementOffsets
Valid for struct types.
Definition: Type.h:207
static std::unique_ptr< Type > getSampledImage(const Type *ImageType)
Create a sampled image type.
Definition: Type.cpp:316
std::vector< std::pair< const Type *, std::map< uint32_t, uint32_t > > > StructElementTypeList
A list of types used for structure members.
Definition: Type.h:22
uint32_t BitWidth
Valid for integer and floating point types.
Definition: Type.h:198
static std::unique_ptr< Type > getBool()
Create a boolean type.
Definition: Type.cpp:242
bool isPointer() const
Returns true if this is a pointer type.
Definition: Type.h:116
size_t getElementOffset(uint64_t Index) const
Returns the byte offset of the element at Index.
Definition: Type.cpp:26
const Type * getElementType(uint64_t Index=0) const
Returns the type of the element at Index.
Definition: Type.cpp:38
const Type * getScalarType() const
Returns the element type for vector types, or this for scalar types.
Definition: Type.cpp:49
bool isInt() const
Returns true if this is an integer type.
Definition: Type.h:110
static std::unique_ptr< Type > getSampler()
Create a sampler type.
Definition: Type.cpp:323
static std::unique_ptr< Type > getImage(const Type *SampledType, uint32_t Dim, uint32_t Depth, bool Arrayed, bool MS, uint32_t Sampled, uint32_t Format)
Create an image type.
Definition: Type.cpp:271
Type(TypeId Id, size_t ByteSize)
Create a new type.
Definition: Type.h:186
bool isStruct() const
Returns true if this is a struct type.
Definition: Type.h:122
bool isArray() const
Returns true if this is an array type.
Definition: Type.h:95
bool isMatrix() const
Returns true if this is a matrix type.
Definition: Type.h:113
uint32_t getDimensionality() const
Returns the dimensionality of an image type.
Definition: Type.h:60
const Type * ReturnType
Valid for function types.
Definition: Type.h:209
const std::map< uint32_t, uint32_t > & getStructMemberDecorations(uint32_t Index) const
Returns the decoration map for the structure member at Index.
Definition: Type.cpp:67
uint32_t Dimensionality
Valid for image types.
Definition: Type.h:212
uint32_t getElementCount() const
Returns the number of elements in this array, struct, or vector type.
Definition: Type.h:64
std::vector< const Type * > ArgumentTypes
Valid for function types.
Definition: Type.h:210
uint32_t ArrayStride
Valid for array and pointer types.
Definition: Type.h:204
bool isComposite() const
Returns true if this is an array, struct, or vector type.
Definition: Type.cpp:72
static std::unique_ptr< Type > getInt(uint32_t Width)
Create an integer type.
Definition: Type.cpp:254
uint32_t getBitWidth() const
Returns the bit-width of this type.
Definition: Type.cpp:20
uint32_t getStorageClass() const
Returns the storage class of this type.
Definition: Type.cpp:60
TypeId
Identifiers that distinguish between different base types.
Definition: Type.h:37
static std::unique_ptr< Type > getRuntimeArray(const Type *ElemType, uint32_t ArrayStride)
Create a runtime array type.
Definition: Type.cpp:306
uint32_t Format
Valid for image types.
Definition: Type.h:217
static std::unique_ptr< Type > getStruct(const StructElementTypeList &ElemTypes)
Create a structure type.
Definition: Type.cpp:328
TypeId getTypeId() const
Returns the type ID of this type.
Definition: Type.h:92
uint32_t ElementCount
Valid for composite types.
Definition: Type.h:203
This class represents a SPIR-V type.
Definition: Type.h:33
size_t ByteSize
The size of this type in bytes.
Definition: Type.h:196
static std::unique_ptr< Type > getFunction(const Type *ReturnType, const std::vector< const Type * > &ArgTypes)
Create a function type.
Definition: Type.cpp:262
bool isArrayedImage() const
Returns the Arrayed flag of an image type.
Definition: Type.h:98
TypeId Id
The ID of this type.
Definition: Type.h:192
const Type * ElementType
Valid for pointer and composite types.
Definition: Type.h:202
static std::unique_ptr< Type > getFloat(uint32_t Width)
Create a floating point type.
Definition: Type.cpp:247
static std::unique_ptr< Type > getArray(const Type *ElemType, uint32_t ElementCount, uint32_t ArrayStride)
Create an array type.
Definition: Type.cpp:230
static std::unique_ptr< Type > getMatrix(const Type *ColumnType, uint32_t NumColumns)
Create a matrix type.
Definition: Type.cpp:286
friend std::ostream & operator<<(std::ostream &Stream, const Type *Ty)
Allow a Type to be inserted into an output stream.
Definition: Type.cpp:82
StructElementTypeList ElementTypes
Valid for struct types.
Definition: Type.h:206
bool isVector() const
Returns true if this is a vector type.
Definition: Type.h:125