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