Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1 | #include "slang_rs_context.hpp" |
| 2 | #include "slang_rs_export_type.hpp" |
| 3 | #include "slang_rs_export_element.hpp" |
| 4 | |
| 5 | #include "llvm/Type.h" |
| 6 | #include "llvm/DerivedTypes.h" |
| 7 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 8 | #include "llvm/ADT/StringExtras.h" |
| 9 | #include "llvm/Target/TargetData.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 10 | |
Shih-wei Liao | 0a3f20e | 2010-08-10 13:09:49 -0700 | [diff] [blame] | 11 | #include "clang/AST/RecordLayout.h" |
| 12 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 13 | #include <vector> |
| 14 | |
| 15 | using namespace slang; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 16 | |
| 17 | /****************************** RSExportType ******************************/ |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 18 | bool RSExportType::NormalizeType(const clang::Type *&T, |
| 19 | llvm::StringRef &TypeName) { |
| 20 | llvm::SmallPtrSet<const clang::Type*, 8> SPS = |
| 21 | llvm::SmallPtrSet<const clang::Type*, 8>(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 22 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 23 | if ((T = RSExportType::TypeExportable(T, SPS)) == NULL) |
| 24 | // TODO: warning the user: type not exportable |
| 25 | return false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 26 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 27 | // Get type name |
| 28 | TypeName = RSExportType::GetTypeName(T); |
| 29 | if (TypeName.empty()) |
| 30 | // TODO: warning the user: the type is unnamed |
| 31 | return false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 32 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 33 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 36 | const clang::Type |
| 37 | *RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) { |
| 38 | if (DD && DD->getTypeSourceInfo()) { |
| 39 | clang::QualType T = DD->getTypeSourceInfo()->getType(); |
| 40 | if (T.isNull()) |
| 41 | return NULL; |
| 42 | else |
| 43 | return T.getTypePtr(); |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) { |
| 49 | T = GET_CANONICAL_TYPE(T); |
| 50 | if (T == NULL) |
| 51 | return llvm::StringRef(); |
| 52 | |
| 53 | switch (T->getTypeClass()) { |
| 54 | case clang::Type::Builtin: { |
| 55 | const clang::BuiltinType *BT = UNSAFE_CAST_TYPE(clang::BuiltinType, T); |
| 56 | |
| 57 | switch (BT->getKind()) { |
| 58 | // Compiler is smart enough to optimize following *big if branches* |
| 59 | // since they all become "constant comparison" after macro expansion |
| 60 | #define SLANG_RS_SUPPORT_BUILTIN_TYPE(builtin_type, type) \ |
| 61 | case builtin_type: { \ |
| 62 | if (type == RSExportPrimitiveType::DataTypeFloat32) \ |
| 63 | return "float"; \ |
| 64 | else if (type == RSExportPrimitiveType::DataTypeFloat64) \ |
| 65 | return "double"; \ |
| 66 | else if (type == RSExportPrimitiveType::DataTypeUnsigned8) \ |
| 67 | return "uchar"; \ |
| 68 | else if (type == RSExportPrimitiveType::DataTypeUnsigned16) \ |
| 69 | return "ushort"; \ |
| 70 | else if (type == RSExportPrimitiveType::DataTypeUnsigned32) \ |
| 71 | return "uint"; \ |
| 72 | else if (type == RSExportPrimitiveType::DataTypeSigned8) \ |
| 73 | return "char"; \ |
| 74 | else if (type == RSExportPrimitiveType::DataTypeSigned16) \ |
| 75 | return "short"; \ |
| 76 | else if (type == RSExportPrimitiveType::DataTypeSigned32) \ |
| 77 | return "int"; \ |
| 78 | else if(type == RSExportPrimitiveType::DataTypeSigned64) \ |
| 79 | return "long"; \ |
| 80 | else if (type == RSExportPrimitiveType::DataTypeBool) \ |
| 81 | return "bool"; \ |
| 82 | else \ |
| 83 | assert(false && "Unknow data type of supported builtin"); \ |
| 84 | break; \ |
| 85 | } |
| 86 | #include "slang_rs_export_type_support.inc" |
| 87 | |
| 88 | default: { |
| 89 | assert(false && "Unknown data type of the builtin"); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 94 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 95 | |
| 96 | case clang::Type::Record: { |
| 97 | const clang::RecordDecl *RD = T->getAsStructureType()->getDecl(); |
| 98 | llvm::StringRef Name = RD->getName(); |
| 99 | if (Name.empty()) { |
| 100 | if (RD->getTypedefForAnonDecl() != NULL) |
| 101 | Name = RD->getTypedefForAnonDecl()->getName(); |
| 102 | |
| 103 | if(Name.empty()) |
| 104 | // Try to find a name from redeclaration (i.e. typedef) |
| 105 | for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(), |
| 106 | RE = RD->redecls_end(); |
| 107 | RI != RE; |
| 108 | RI++) { |
| 109 | assert(*RI != NULL && "cannot be NULL object"); |
| 110 | |
| 111 | Name = (*RI)->getName(); |
| 112 | if (!Name.empty()) |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | return Name; |
| 117 | } |
| 118 | |
| 119 | case clang::Type::Pointer: { |
| 120 | // "*" plus pointee name |
| 121 | const clang::Type* PT = GET_POINTEE_TYPE(T); |
| 122 | llvm::StringRef PointeeName; |
| 123 | if (NormalizeType(PT, PointeeName)) { |
| 124 | char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ]; |
| 125 | Name[0] = '*'; |
| 126 | memcpy(Name + 1, PointeeName.data(), PointeeName.size()); |
| 127 | Name[PointeeName.size() + 1] = '\0'; |
| 128 | return Name; |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | case clang::Type::ConstantArray: { |
| 134 | const clang::ConstantArrayType *ECT = |
| 135 | UNSAFE_CAST_TYPE(clang::ConstantArrayType, T); |
| 136 | return RSExportConstantArrayType::GetTypeName(ECT); |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | case clang::Type::ExtVector: { |
| 141 | const clang::ExtVectorType *EVT = |
| 142 | UNSAFE_CAST_TYPE(clang::ExtVectorType, T); |
| 143 | return RSExportVectorType::GetTypeName(EVT); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | default: { |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return llvm::StringRef(); |
| 153 | } |
| 154 | |
| 155 | const clang::Type *RSExportType::TypeExportable( |
| 156 | const clang::Type *T, |
| 157 | llvm::SmallPtrSet<const clang::Type*, 8>& SPS) { |
| 158 | // Normalize first |
| 159 | if ((T = GET_CANONICAL_TYPE(T)) == NULL) |
| 160 | return NULL; |
| 161 | |
| 162 | if (SPS.count(T)) |
| 163 | return T; |
| 164 | |
| 165 | switch (T->getTypeClass()) { |
| 166 | case clang::Type::Builtin: { |
| 167 | const clang::BuiltinType *BT = UNSAFE_CAST_TYPE(clang::BuiltinType, T); |
| 168 | |
| 169 | switch (BT->getKind()) { |
| 170 | #define SLANG_RS_SUPPORT_BUILTIN_TYPE(builtin_type, type) \ |
| 171 | case builtin_type: |
| 172 | #include "slang_rs_export_type_support.inc" |
| 173 | { |
| 174 | return T; |
| 175 | } |
| 176 | |
| 177 | default: { |
| 178 | return NULL; |
| 179 | } |
| 180 | } |
| 181 | // Never be here |
| 182 | } |
| 183 | |
| 184 | |
| 185 | case clang::Type::Record: { |
| 186 | if (RSExportPrimitiveType::GetRSObjectType(T) != |
| 187 | RSExportPrimitiveType::DataTypeUnknown) |
| 188 | return T; // RS object type, no further checks are needed |
| 189 | |
| 190 | // Check internal struct |
| 191 | const clang::RecordDecl *RD = T->getAsStructureType()->getDecl(); |
| 192 | if (RD != NULL) |
| 193 | RD = RD->getDefinition(); |
| 194 | |
| 195 | // Fast check |
| 196 | if (RD->hasFlexibleArrayMember() || RD->hasObjectMember()) |
| 197 | return NULL; |
| 198 | |
| 199 | // Insert myself into checking set |
| 200 | SPS.insert(T); |
| 201 | |
| 202 | // Check all element |
| 203 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 204 | FE = RD->field_end(); |
| 205 | FI != FE; |
| 206 | FI++) { |
| 207 | const clang::Type* FT = GetTypeOfDecl(*FI); |
| 208 | FT = GET_CANONICAL_TYPE(FT); |
| 209 | |
| 210 | if (!TypeExportable(FT, SPS)) |
| 211 | // TODO: warning: unsupported field type |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | return T; |
| 216 | } |
| 217 | |
| 218 | case clang::Type::Pointer: { |
| 219 | const clang::PointerType *PT = UNSAFE_CAST_TYPE(clang::PointerType, T); |
| 220 | const clang::Type *PointeeType = GET_POINTEE_TYPE(PT); |
| 221 | |
| 222 | if ((PointeeType->getTypeClass() != clang::Type::Pointer) && |
| 223 | (TypeExportable(PointeeType, SPS) == NULL) ) |
| 224 | return NULL; |
| 225 | else |
| 226 | return T; |
| 227 | } |
| 228 | |
| 229 | case clang::Type::ConstantArray: { |
| 230 | const clang::ConstantArrayType *ECT = |
| 231 | UNSAFE_CAST_TYPE(clang::ConstantArrayType, T); |
| 232 | // No longer only support 2x2, 3x3 and 4x4 arrays |
| 233 | // if (ECT->getNumElements() != 4 && |
| 234 | // ECT->getNumElements() != 9 && |
| 235 | // ECT->getNumElements() != 16) |
| 236 | // return NULL; |
| 237 | |
| 238 | // Check base element type |
| 239 | const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(ECT); |
| 240 | |
| 241 | if ((ElementType->getTypeClass() != clang::Type::Builtin) || |
| 242 | (TypeExportable(ElementType, SPS) == NULL)) |
| 243 | return NULL; |
| 244 | else |
| 245 | return T; |
| 246 | } |
| 247 | |
| 248 | case clang::Type::ExtVector: { |
| 249 | const clang::ExtVectorType *EVT = |
| 250 | UNSAFE_CAST_TYPE(clang::ExtVectorType, T); |
| 251 | // Only vector with size 2, 3 and 4 are supported. |
| 252 | if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4) |
| 253 | return NULL; |
| 254 | |
| 255 | // Check base element type |
| 256 | const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT); |
| 257 | |
| 258 | if ((ElementType->getTypeClass() != clang::Type::Builtin) || |
| 259 | (TypeExportable(ElementType, SPS) == NULL)) |
| 260 | return NULL; |
| 261 | else |
| 262 | return T; |
| 263 | } |
| 264 | |
| 265 | default: { |
| 266 | return NULL; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | RSExportType *RSExportType::Create(RSContext *Context, |
| 272 | const clang::Type *T, |
| 273 | const llvm::StringRef &TypeName) { |
| 274 | // Lookup the context to see whether the type was processed before. |
| 275 | // Newly created RSExportType will insert into context |
| 276 | // in RSExportType::RSExportType() |
| 277 | RSContext::export_type_iterator ETI = Context->findExportType(TypeName); |
| 278 | |
| 279 | if (ETI != Context->export_types_end()) |
| 280 | return ETI->second; |
| 281 | |
| 282 | RSExportType *ET = NULL; |
| 283 | switch (T->getTypeClass()) { |
| 284 | case clang::Type::Record: { |
| 285 | RSExportPrimitiveType::DataType dt = |
| 286 | RSExportPrimitiveType::GetRSObjectType(TypeName); |
| 287 | switch (dt) { |
| 288 | case RSExportPrimitiveType::DataTypeUnknown: { // User-defined types |
| 289 | ET = RSExportRecordType::Create(Context, |
| 290 | T->getAsStructureType(), |
| 291 | TypeName); |
| 292 | break; |
| 293 | } |
| 294 | case RSExportPrimitiveType::DataTypeRSMatrix2x2: |
| 295 | case RSExportPrimitiveType::DataTypeRSMatrix3x3: |
| 296 | case RSExportPrimitiveType::DataTypeRSMatrix4x4: { |
| 297 | const clang::RecordType *RT = |
| 298 | static_cast<const clang::RecordType*> (T); |
| 299 | const clang::RecordDecl *RD = RT->getDecl(); |
| 300 | RD = RD->getDefinition(); |
| 301 | clang::RecordDecl::field_iterator FI = RD->field_begin(); |
| 302 | clang::FieldDecl *FD = *FI; |
| 303 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
| 304 | ET = RSExportConstantArrayType::Create( |
| 305 | Context, |
| 306 | static_cast<const clang::ConstantArrayType*> (FT), |
| 307 | TypeName); |
| 308 | break; |
| 309 | } |
| 310 | default: { |
| 311 | ET = RSExportPrimitiveType::Create(Context, T, TypeName); |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | break; |
| 316 | } |
| 317 | case clang::Type::Builtin: { |
| 318 | ET = RSExportPrimitiveType::Create(Context, T, TypeName); |
| 319 | break; |
| 320 | } |
| 321 | case clang::Type::Pointer: { |
| 322 | ET = RSExportPointerType::Create(Context, |
| 323 | UNSAFE_CAST_TYPE(clang::PointerType, T), |
| 324 | TypeName); |
| 325 | // Free the name (allocated in RSExportType::GetTypeName) |
| 326 | delete [] TypeName.data(); |
| 327 | break; |
| 328 | } |
| 329 | case clang::Type::ConstantArray: { |
| 330 | ET = RSExportConstantArrayType::Create( |
| 331 | Context, |
| 332 | UNSAFE_CAST_TYPE(clang::ConstantArrayType, T), |
| 333 | TypeName); |
| 334 | break; |
| 335 | } |
| 336 | case clang::Type::ExtVector: { |
| 337 | ET = RSExportVectorType::Create(Context, |
| 338 | UNSAFE_CAST_TYPE(clang::ExtVectorType, T), |
| 339 | TypeName); |
| 340 | break; |
| 341 | } |
| 342 | default: { |
| 343 | // TODO: warning: type is not exportable |
| 344 | fprintf(stderr, |
| 345 | "RSExportType::Create : type '%s' is not exportable\n", |
| 346 | T->getTypeClassName()); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return ET; |
| 352 | } |
| 353 | |
| 354 | RSExportType* RSExportType::Create(RSContext *Context, const clang::Type *T) { |
| 355 | llvm::StringRef TypeName; |
| 356 | if (NormalizeType(T, TypeName)) |
| 357 | return Create(Context, T, TypeName); |
| 358 | else |
Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 359 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 362 | RSExportType *RSExportType::CreateFromDecl(RSContext *Context, |
| 363 | const clang::VarDecl *VD) { |
| 364 | return RSExportType::Create(Context, GetTypeOfDecl(VD)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 367 | size_t RSExportType::GetTypeStoreSize(const RSExportType *ET) { |
| 368 | return ET->getRSContext()->getTargetData()->getTypeStoreSize( |
| 369 | ET->getLLVMType() |
| 370 | ); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 373 | size_t RSExportType::GetTypeAllocSize(const RSExportType *ET) { |
| 374 | if (ET->getClass() == RSExportType::ExportClassRecord) |
| 375 | return static_cast<const RSExportRecordType*>(ET)->getAllocSize(); |
| 376 | else |
| 377 | return ET->getRSContext()->getTargetData()->getTypeAllocSize( |
| 378 | ET->getLLVMType() |
| 379 | ); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 380 | } |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 381 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 382 | RSExportType::RSExportType(RSContext *Context, const llvm::StringRef &Name) : |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 383 | mContext(Context), |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 384 | // Make a copy on Name since data of @Name which is stored in ASTContext |
| 385 | // will be destroyed later |
| 386 | mName(Name.data(), Name.size()), |
| 387 | mLLVMType(NULL) { |
| 388 | // TODO: need to check whether the insertion is successful or not |
| 389 | Context->insertExportType(Name, this); |
| 390 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 393 | /************************** RSExportPrimitiveType **************************/ |
| 394 | RSExportPrimitiveType::RSObjectTypeMapTy |
| 395 | *RSExportPrimitiveType::RSObjectTypeMap = NULL; |
| 396 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 397 | llvm::Type* RSExportPrimitiveType::RSObjectLLVMType = NULL; |
| 398 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 399 | bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) { |
| 400 | if ((T != NULL) && (T->getTypeClass() == clang::Type::Builtin)) |
| 401 | return true; |
| 402 | else |
| 403 | return false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 406 | RSExportPrimitiveType::DataType |
| 407 | RSExportPrimitiveType::GetRSObjectType(const llvm::StringRef &TypeName) { |
| 408 | if (TypeName.empty()) |
| 409 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 410 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 411 | if (RSObjectTypeMap == NULL) { |
| 412 | RSObjectTypeMap = new RSObjectTypeMapTy(16); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 413 | |
| 414 | #define USE_ELEMENT_DATA_TYPE |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 415 | #define DEF_RS_OBJECT_TYPE(type, name) \ |
| 416 | RSObjectTypeMap->GetOrCreateValue(name, GET_ELEMENT_DATA_TYPE(type)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 417 | #include "slang_rs_export_element_support.inc" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 418 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 419 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 420 | RSObjectTypeMapTy::const_iterator I = RSObjectTypeMap->find(TypeName); |
| 421 | if (I == RSObjectTypeMap->end()) |
| 422 | return DataTypeUnknown; |
| 423 | else |
| 424 | return I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 427 | RSExportPrimitiveType::DataType |
| 428 | RSExportPrimitiveType::GetRSObjectType(const clang::Type *T) { |
| 429 | T = GET_CANONICAL_TYPE(T); |
| 430 | if ((T == NULL) || (T->getTypeClass() != clang::Type::Record)) |
| 431 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 432 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 433 | return GetRSObjectType( RSExportType::GetTypeName(T) ); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 436 | const size_t |
| 437 | RSExportPrimitiveType::SizeOfDataTypeInBits[ |
| 438 | RSExportPrimitiveType::DataTypeMax + 1] = { |
| 439 | 0, |
| 440 | 16, // DataTypeFloat16 |
| 441 | 32, // DataTypeFloat32 |
| 442 | 64, // DataTypeFloat64 |
| 443 | 8, // DataTypeSigned8 |
| 444 | 16, // DataTypeSigned16 |
| 445 | 32, // DataTypeSigned32 |
| 446 | 64, // DataTypeSigned64 |
| 447 | 8, // DataTypeUnsigned8 |
| 448 | 16, // DataTypeUnsigned16 |
| 449 | 32, // DataTypeUnsigned32 |
| 450 | 64, // DataTypeUnSigned64 |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 451 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 452 | 16, // DataTypeUnsigned565 |
| 453 | 16, // DataTypeUnsigned5551 |
| 454 | 16, // DataTypeUnsigned4444 |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 455 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 456 | 1, // DataTypeBool |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 457 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 458 | 32, // DataTypeRSElement |
| 459 | 32, // DataTypeRSType |
| 460 | 32, // DataTypeRSAllocation |
| 461 | 32, // DataTypeRSSampler |
| 462 | 32, // DataTypeRSScript |
| 463 | 32, // DataTypeRSMesh |
| 464 | 32, // DataTypeRSProgramFragment |
| 465 | 32, // DataTypeRSProgramVertex |
| 466 | 32, // DataTypeRSProgramRaster |
| 467 | 32, // DataTypeRSProgramStore |
| 468 | 32, // DataTypeRSFont |
| 469 | 128, // DataTypeRSMatrix2x2 |
| 470 | 288, // DataTypeRSMatrix3x3 |
| 471 | 512, // DataTypeRSMatrix4x4 |
| 472 | 0 |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 473 | }; |
| 474 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 475 | size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) { |
| 476 | assert(((EPT->getType() >= DataTypeFloat32) && |
| 477 | (EPT->getType() < DataTypeMax)) && |
| 478 | "RSExportPrimitiveType::GetSizeInBits : unknown data type"); |
| 479 | return SizeOfDataTypeInBits[ static_cast<int>(EPT->getType()) ]; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 482 | RSExportPrimitiveType::DataType |
| 483 | RSExportPrimitiveType::GetDataType(const clang::Type* T) { |
| 484 | if (T == NULL) |
| 485 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 486 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 487 | switch (T->getTypeClass()) { |
| 488 | case clang::Type::Builtin: { |
| 489 | const clang::BuiltinType *BT = UNSAFE_CAST_TYPE(clang::BuiltinType, T); |
| 490 | switch (BT->getKind()) { |
| 491 | #define SLANG_RS_SUPPORT_BUILTIN_TYPE(builtin_type, type) \ |
| 492 | case builtin_type: { \ |
| 493 | return type; \ |
| 494 | break; \ |
| 495 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 496 | #include "slang_rs_export_type_support.inc" |
| 497 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 498 | // The size of types Long, ULong and WChar depend on platform so we |
| 499 | // abandon the support to them. Type of its size exceeds 32 bits (e.g. |
| 500 | // int64_t, double, etc.): no support |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 501 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 502 | default: { |
| 503 | // TODO: warning the user: unsupported type |
| 504 | fprintf(stderr, "RSExportPrimitiveType::GetDataType : built-in type " |
| 505 | "has no corresponding data type for built-in type"); |
| 506 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 507 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 508 | } |
| 509 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 512 | case clang::Type::Record: { |
| 513 | // must be RS object type |
| 514 | return RSExportPrimitiveType::GetRSObjectType(T); |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | default: { |
| 519 | fprintf(stderr, "RSExportPrimitiveType::GetDataType : type '%s' is not " |
| 520 | "supported primitive type", T->getTypeClassName()); |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 528 | RSExportPrimitiveType |
| 529 | *RSExportPrimitiveType::Create(RSContext *Context, |
| 530 | const clang::Type *T, |
| 531 | const llvm::StringRef &TypeName, |
| 532 | DataKind DK, |
| 533 | bool Normalized) { |
| 534 | DataType DT = GetDataType(T); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 535 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 536 | if ((DT == DataTypeUnknown) || TypeName.empty()) |
| 537 | return NULL; |
| 538 | else |
| 539 | return new RSExportPrimitiveType(Context, TypeName, DT, DK, Normalized); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 542 | RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context, |
| 543 | const clang::Type *T, |
| 544 | DataKind DK) { |
| 545 | llvm::StringRef TypeName; |
| 546 | if (RSExportType::NormalizeType(T, TypeName) && IsPrimitiveType(T)) |
| 547 | return Create(Context, T, TypeName, DK); |
| 548 | else |
| 549 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 552 | RSExportType::ExportClass RSExportPrimitiveType::getClass() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 553 | return RSExportType::ExportClassPrimitive; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 556 | const llvm::Type *RSExportPrimitiveType::convertToLLVMType() const { |
| 557 | llvm::LLVMContext &C = getRSContext()->getLLVMContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 558 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 559 | if (isRSObjectType()) { |
| 560 | // struct { |
| 561 | // int *p; |
| 562 | // } __attribute__((packed, aligned(pointer_size))) |
| 563 | // |
| 564 | // which is |
| 565 | // |
| 566 | // <{ [1 x i32] }> in LLVM |
| 567 | // |
| 568 | if (RSObjectLLVMType == NULL) { |
| 569 | std::vector<const llvm::Type *> Elements; |
| 570 | Elements.push_back( llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1) ); |
| 571 | RSObjectLLVMType = llvm::StructType::get(C, Elements, true); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 572 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 573 | return RSObjectLLVMType; |
| 574 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 575 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 576 | switch (mType) { |
| 577 | case DataTypeFloat32: { |
| 578 | return llvm::Type::getFloatTy(C); |
| 579 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 580 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 581 | case DataTypeFloat64: { |
| 582 | return llvm::Type::getDoubleTy(C); |
| 583 | break; |
| 584 | } |
| 585 | case DataTypeSigned8: |
| 586 | case DataTypeUnsigned8: { |
| 587 | return llvm::Type::getInt8Ty(C); |
| 588 | break; |
| 589 | } |
| 590 | case DataTypeSigned16: |
| 591 | case DataTypeUnsigned16: |
| 592 | case DataTypeUnsigned565: |
| 593 | case DataTypeUnsigned5551: |
| 594 | case DataTypeUnsigned4444: { |
| 595 | return llvm::Type::getInt16Ty(C); |
| 596 | break; |
| 597 | } |
| 598 | case DataTypeSigned32: |
| 599 | case DataTypeUnsigned32: { |
| 600 | return llvm::Type::getInt32Ty(C); |
| 601 | break; |
| 602 | } |
| 603 | case DataTypeSigned64: { |
| 604 | // case DataTypeUnsigned64: |
| 605 | return llvm::Type::getInt64Ty(C); |
| 606 | break; |
| 607 | } |
| 608 | case DataTypeBool: { |
| 609 | return llvm::Type::getInt1Ty(C); |
| 610 | break; |
| 611 | } |
| 612 | default: { |
| 613 | assert(false && "Unknown data type"); |
| 614 | } |
| 615 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 616 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 617 | return NULL; |
| 618 | } |
| 619 | |
| 620 | /**************************** RSExportPointerType ****************************/ |
| 621 | |
| 622 | const clang::Type *RSExportPointerType::IntegerType = NULL; |
| 623 | |
| 624 | RSExportPointerType |
| 625 | *RSExportPointerType::Create(RSContext *Context, |
| 626 | const clang::PointerType *PT, |
| 627 | const llvm::StringRef &TypeName) { |
| 628 | const clang::Type *PointeeType = GET_POINTEE_TYPE(PT); |
| 629 | const RSExportType *PointeeET; |
| 630 | |
| 631 | if (PointeeType->getTypeClass() != clang::Type::Pointer) { |
| 632 | PointeeET = RSExportType::Create(Context, PointeeType); |
| 633 | } else { |
| 634 | // Double or higher dimension of pointer, export as int* |
| 635 | assert(IntegerType != NULL && "Built-in integer type is not set"); |
| 636 | PointeeET = RSExportPrimitiveType::Create(Context, IntegerType); |
| 637 | } |
| 638 | |
| 639 | if (PointeeET == NULL) { |
| 640 | fprintf(stderr, "Failed to create type for pointee"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 641 | return NULL; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 642 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 643 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 644 | return new RSExportPointerType(Context, TypeName, PointeeET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 647 | RSExportType::ExportClass RSExportPointerType::getClass() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 648 | return RSExportType::ExportClassPointer; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 651 | const llvm::Type *RSExportPointerType::convertToLLVMType() const { |
| 652 | const llvm::Type *PointeeType = mPointeeType->getLLVMType(); |
| 653 | return llvm::PointerType::getUnqual(PointeeType); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 656 | /************************* RSExportConstantArrayType *************************/ |
| 657 | llvm::StringRef |
| 658 | RSExportConstantArrayType::GetTypeName(const clang::ConstantArrayType *CT) { |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 659 | llvm::APInt i = CT->getSize(); |
| 660 | if (i == 4) { |
| 661 | return llvm::StringRef("rs_matrix2x2"); |
| 662 | } else if (i == 9) { |
| 663 | return llvm::StringRef("rs_matrix3x3"); |
| 664 | } else if (i == 16) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 665 | return llvm::StringRef("rs_matrix4x4"); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 666 | } |
| 667 | return llvm::StringRef(); |
| 668 | } |
| 669 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 670 | RSExportConstantArrayType |
| 671 | *RSExportConstantArrayType::Create(RSContext *Context, |
| 672 | const clang::ConstantArrayType *CT, |
| 673 | const llvm::StringRef &TypeName, |
| 674 | DataKind DK, |
| 675 | bool Normalized) { |
| 676 | assert(CT != NULL && CT->getTypeClass() == clang::Type::ConstantArray); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 677 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 678 | int64_t Size = CT->getSize().getSExtValue(); |
| 679 | RSExportPrimitiveType::DataType DT; |
| 680 | if (Size == 4) { |
| 681 | DT = RSExportPrimitiveType::DataTypeRSMatrix2x2; |
| 682 | } else if (Size == 9) { |
| 683 | DT = RSExportPrimitiveType::DataTypeRSMatrix3x3; |
| 684 | } else if (Size == 16) { |
| 685 | DT = RSExportPrimitiveType::DataTypeRSMatrix4x4; |
| 686 | } else { |
| 687 | fprintf(stderr, "RSExportConstantArrayType::Create : unsupported base " |
| 688 | "element type\n"); |
| 689 | return NULL; |
| 690 | } |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 691 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 692 | return new RSExportConstantArrayType(Context, |
| 693 | TypeName, |
| 694 | DT, |
| 695 | DK, |
| 696 | Normalized, |
| 697 | Size); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | RSExportType::ExportClass RSExportConstantArrayType::getClass() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 701 | return RSExportType::ExportClassConstantArray; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 704 | const llvm::Type *RSExportConstantArrayType::convertToLLVMType() const { |
| 705 | llvm::LLVMContext &C = getRSContext()->getLLVMContext(); |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 706 | const llvm::Type *typ; |
| 707 | switch (getType()) { |
| 708 | case DataTypeFloat32: |
| 709 | case DataTypeRSMatrix2x2: |
| 710 | case DataTypeRSMatrix3x3: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 711 | case DataTypeRSMatrix4x4: { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 712 | typ = llvm::Type::getFloatTy(C); |
| 713 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 714 | } |
| 715 | case DataTypeFloat64: { |
Stephen Hines | 70842c7 | 2010-09-20 17:18:10 -0700 | [diff] [blame] | 716 | typ = llvm::Type::getDoubleTy(C); |
| 717 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 718 | } |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 719 | case DataTypeSigned8: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 720 | case DataTypeUnsigned8: { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 721 | typ = llvm::Type::getInt8Ty(C); |
| 722 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 723 | } |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 724 | case DataTypeSigned16: |
| 725 | case DataTypeUnsigned16: |
| 726 | case DataTypeUnsigned565: |
| 727 | case DataTypeUnsigned5551: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 728 | case DataTypeUnsigned4444: { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 729 | typ = llvm::Type::getInt16Ty(C); |
| 730 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 731 | } |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 732 | case DataTypeSigned32: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 733 | case DataTypeUnsigned32: { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 734 | typ = llvm::Type::getInt32Ty(C); |
| 735 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 736 | } |
| 737 | case DataTypeSigned64: { |
Stephen Hines | 8d75dc4 | 2010-10-01 15:36:03 -0700 | [diff] [blame] | 738 | //case DataTypeUnsigned64: |
| 739 | typ = llvm::Type::getInt64Ty(C); |
| 740 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 741 | } |
| 742 | case DataTypeBool: { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 743 | typ = llvm::Type::getInt1Ty(C); |
| 744 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 745 | } |
| 746 | default: { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 747 | assert(false && "Unknown data type"); |
| 748 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 749 | } |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 750 | } |
| 751 | return llvm::ArrayType::get(typ, mNumElement); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 754 | /****************************** RSExportVectorType ******************************/ |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 755 | const char* RSExportVectorType::VectorTypeNameStore[][3] = { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 756 | /* 0 */ { "char2", "char3", "char4" }, |
| 757 | /* 1 */ { "uchar2", "uchar3", "uchar4" }, |
| 758 | /* 2 */ { "short2", "short3", "short4" }, |
| 759 | /* 3 */ { "ushort2", "ushort3", "ushort4" }, |
| 760 | /* 4 */ { "int2", "int3", "int4" }, |
| 761 | /* 5 */ { "uint2", "uint3", "uint4" }, |
| 762 | /* 6 */ { "float2", "float3", "float4" }, |
| 763 | /* 7 */ { "double2", "double3", "double4" }, |
| 764 | /* 8 */ { "long2", "long3", "long4" }, |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 765 | }; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 766 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 767 | llvm::StringRef RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) { |
| 768 | const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 769 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 770 | if ((ElementType->getTypeClass() != clang::Type::Builtin)) |
| 771 | return llvm::StringRef(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 772 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 773 | const clang::BuiltinType *BT = UNSAFE_CAST_TYPE(clang::BuiltinType, |
| 774 | ElementType); |
| 775 | const char **BaseElement = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 776 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 777 | switch (BT->getKind()) { |
| 778 | // Compiler is smart enough to optimize following *big if branches* since |
| 779 | // they all become "constant comparison" after macro expansion |
| 780 | #define SLANG_RS_SUPPORT_BUILTIN_TYPE(builtin_type, type) \ |
| 781 | case builtin_type: { \ |
| 782 | if (type == RSExportPrimitiveType::DataTypeSigned8) \ |
| 783 | BaseElement = VectorTypeNameStore[0]; \ |
| 784 | else if (type == RSExportPrimitiveType::DataTypeUnsigned8) \ |
| 785 | BaseElement = VectorTypeNameStore[1]; \ |
| 786 | else if (type == RSExportPrimitiveType::DataTypeSigned16) \ |
| 787 | BaseElement = VectorTypeNameStore[2]; \ |
| 788 | else if (type == RSExportPrimitiveType::DataTypeUnsigned16) \ |
| 789 | BaseElement = VectorTypeNameStore[3]; \ |
| 790 | else if (type == RSExportPrimitiveType::DataTypeSigned32) \ |
| 791 | BaseElement = VectorTypeNameStore[4]; \ |
| 792 | else if (type == RSExportPrimitiveType::DataTypeUnsigned32) \ |
| 793 | BaseElement = VectorTypeNameStore[5]; \ |
| 794 | else if (type == RSExportPrimitiveType::DataTypeFloat32) \ |
| 795 | BaseElement = VectorTypeNameStore[6]; \ |
| 796 | else if (type == RSExportPrimitiveType::DataTypeFloat64) \ |
| 797 | BaseElement = VectorTypeNameStore[7]; \ |
| 798 | else if (type == RSExportPrimitiveType::DataTypeSigned64) \ |
| 799 | BaseElement = VectorTypeNameStore[8]; \ |
| 800 | else if (type == RSExportPrimitiveType::DataTypeBool) \ |
| 801 | BaseElement = VectorTypeNameStore[0]; \ |
| 802 | break; \ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 803 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 804 | #include "slang_rs_export_type_support.inc" |
| 805 | default: { |
| 806 | return llvm::StringRef(); |
| 807 | } |
| 808 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 809 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 810 | if ((BaseElement != NULL) && |
| 811 | (EVT->getNumElements() > 1) && |
| 812 | (EVT->getNumElements() <= 4)) |
| 813 | return BaseElement[EVT->getNumElements() - 2]; |
| 814 | else |
| 815 | return llvm::StringRef(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 818 | RSExportVectorType *RSExportVectorType::Create(RSContext *Context, |
| 819 | const clang::ExtVectorType *EVT, |
| 820 | const llvm::StringRef &TypeName, |
| 821 | DataKind DK, |
| 822 | bool Normalized) { |
| 823 | assert(EVT != NULL && EVT->getTypeClass() == Type::ExtVector); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 824 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 825 | const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT); |
| 826 | RSExportPrimitiveType::DataType DT = |
| 827 | RSExportPrimitiveType::GetDataType(ElementType); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 828 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 829 | if (DT != RSExportPrimitiveType::DataTypeUnknown) |
| 830 | return new RSExportVectorType(Context, |
| 831 | TypeName, |
| 832 | DT, |
| 833 | DK, |
| 834 | Normalized, |
| 835 | EVT->getNumElements()); |
| 836 | else |
| 837 | fprintf(stderr, "RSExportVectorType::Create : unsupported base element " |
| 838 | "type\n"); |
| 839 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 842 | RSExportType::ExportClass RSExportVectorType::getClass() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 843 | return RSExportType::ExportClassVector; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | const llvm::Type* RSExportVectorType::convertToLLVMType() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 847 | const llvm::Type* ElementType = RSExportPrimitiveType::convertToLLVMType(); |
| 848 | return llvm::VectorType::get(ElementType, getNumElement()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 851 | /**************************** RSExportRecordType ****************************/ |
| 852 | RSExportRecordType *RSExportRecordType::Create(RSContext *Context, |
| 853 | const clang::RecordType *RT, |
| 854 | const llvm::StringRef &TypeName, |
| 855 | bool mIsArtificial) { |
| 856 | assert(RT != NULL && RT->getTypeClass() == Type::Record); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 857 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 858 | const clang::RecordDecl *RD = RT->getDecl(); |
| 859 | assert(RD->isStruct()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 860 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 861 | RD = RD->getDefinition(); |
| 862 | if (RD == NULL) { |
| 863 | // TODO: warning: actual struct definition isn't declared in this moudle |
| 864 | fprintf(stderr, "RSExportRecordType::Create : this struct is not defined " |
| 865 | "in this module."); |
| 866 | return NULL; |
| 867 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 868 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 869 | RSExportRecordType *ERT = new RSExportRecordType(Context, |
| 870 | TypeName, |
| 871 | RD-> |
| 872 | hasAttr<clang::PackedAttr>(), |
| 873 | mIsArtificial); |
| 874 | unsigned int Index = 0; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 875 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 876 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 877 | FE = RD->field_end(); |
| 878 | FI != FE; |
| 879 | FI++) { |
| 880 | #define FAILED_CREATE_FIELD(err) do { \ |
| 881 | if (*err) \ |
| 882 | fprintf(stderr, \ |
| 883 | "RSExportRecordType::Create : failed to create field (%s)\n", \ |
| 884 | err); \ |
| 885 | delete ERT; \ |
| 886 | return NULL; \ |
| 887 | } while (false) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 888 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 889 | // FIXME: All fields should be primitive type |
| 890 | assert((*FI)->getKind() == clang::Decl::Field); |
| 891 | clang::FieldDecl *FD = *FI; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 892 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 893 | // We don't support bit field |
| 894 | // |
| 895 | // TODO: allow bitfield with size 8, 16, 32 |
| 896 | if (FD->isBitField()) |
| 897 | FAILED_CREATE_FIELD("bit field is not supported"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 898 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 899 | // Type |
| 900 | RSExportType* ET = RSExportElement::CreateFromDecl(Context, FD); |
| 901 | |
| 902 | if(ET != NULL) |
| 903 | ERT->mFields.push_back( new Field(ET, FD->getName(), ERT, Index) ); |
| 904 | else |
| 905 | FAILED_CREATE_FIELD(FD->getName().str().c_str()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 906 | #undef FAILED_CREATE_FIELD |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 907 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 908 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 909 | const clang::ASTRecordLayout &ASTRL = |
| 910 | Context->getASTContext()->getASTRecordLayout(RD); |
| 911 | ERT->AllocSize = |
| 912 | (ASTRL.getSize() > ASTRL.getDataSize()) ? |
| 913 | (ASTRL.getSize() >> 3) : |
| 914 | (ASTRL.getDataSize() >> 3); |
Shih-wei Liao | 0a3f20e | 2010-08-10 13:09:49 -0700 | [diff] [blame] | 915 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 916 | return ERT; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | RSExportType::ExportClass RSExportRecordType::getClass() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 920 | return RSExportType::ExportClassRecord; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | const llvm::Type* RSExportRecordType::convertToLLVMType() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 924 | std::vector<const llvm::Type*> FieldTypes; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 925 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 926 | for (const_field_iterator FI = fields_begin(), |
| 927 | FE = fields_end(); |
| 928 | FI != FE; |
| 929 | FI++) { |
| 930 | const Field *F = *FI; |
| 931 | const RSExportType *FET = F->getType(); |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 932 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 933 | FieldTypes.push_back(FET->getLLVMType()); |
| 934 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 935 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 936 | return llvm::StructType::get(getRSContext()->getLLVMContext(), |
| 937 | FieldTypes, |
| 938 | mIsPacked); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 941 | /************************* RSExportRecordType::Field *************************/ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 942 | size_t RSExportRecordType::Field::getOffsetInParent() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 943 | // Struct layout obtains below will be cached by LLVM |
| 944 | const llvm::StructLayout *SL = |
| 945 | mParent->getRSContext()->getTargetData()->getStructLayout( |
| 946 | static_cast<const llvm::StructType*>(mParent->getLLVMType()) |
| 947 | ); |
| 948 | return SL->getElementOffset(mIndex); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 949 | } |