slang_rs_spec_table.cpp revision b1771ef128b10c4d4575634828006bfba20b1d9c
1/* 2 * Copyright 2010, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <string> 18#include <cstring> 19#include <cstdio> 20 21#include "slang_rs_type_spec.h" 22 23enum { 24#define ENUM_PRIMITIVE_DATA_TYPE(x, name, bits) x, 25#define PRIMITIVE_DATA_TYPE_RANGE(x, y) \ 26 FirstPrimitiveType = x, \ 27 LastPrimitiveType = y, 28 PRIMITIVE_DATA_TYPE_ENUMS 29#undef ENUM_PRIMITIVE_DATA_TYPE 30#undef PRIMITIVE_DATA_TYPE_RANGE 31 32#define ENUM_RS_MATRIX_DATA_TYPE(x, name, dim) x, 33#define RS_MATRIX_DATA_TYPE_RANGE(x, y) \ 34 FirstRSMatrixType = x, \ 35 LastRSMatrixType = y, 36 RS_MATRIX_DATA_TYPE_ENUMS 37#undef ENUM_RS_MATRIX_DATA_TYPE 38#undef RS_MATRIX_DATA_TYPE_RANGE 39 40#define ENUM_RS_OBJECT_DATA_TYPE(x, name) x, 41#define RS_OBJECT_DATA_TYPE_RANGE(x, y) \ 42 FirstRSObjectType = x, \ 43 LastRSObjectType = y, 44 RS_OBJECT_DATA_TYPE_ENUMS 45#undef ENUM_RS_OBJECT_DATA_TYPE 46#undef RS_OBJECT_DATA_TYPE_RANGE 47}; 48 49enum { 50#define ENUM_RS_DATA_KIND(x) x, 51 RS_DATA_KIND_ENUMS 52#undef ENUM_RS_DATA_KIND 53}; 54 55class RSDataTypeSpec { 56 private: 57 const char *mTypeName; // e.g. Float32 58 // FIXME: better name 59 const char *mTypePragmaName; // e.g. float 60 size_t mBits; 61 62 protected: 63 enum { 64 DT_PrimitiveClass, 65 DT_RSMatrixClass, 66 DT_RSObjectClass 67 } mClass; 68 69 public: 70 RSDataTypeSpec(const char *TypeName, 71 const char *TypePragmaName, 72 size_t Bits) 73 : mTypeName(TypeName), 74 mTypePragmaName(TypePragmaName), 75 mBits(Bits), 76 mClass(DT_PrimitiveClass) { 77 return; 78 } 79 80 inline const char *getTypeName() const { return mTypeName; } 81 inline const char *getTypePragmaName() const { return mTypePragmaName; } 82 inline size_t getSizeInBit() const { return mBits; } 83 inline bool isRSMatrix() const { return (mClass == DT_RSMatrixClass); } 84 inline bool isRSObject() const { return (mClass == DT_RSObjectClass); } 85}; 86 87class RSMatrixDataTypeSpec : public RSDataTypeSpec { 88 private: 89 unsigned mDim; 90 91 public: 92 RSMatrixDataTypeSpec(const char *TypeName, 93 const char *TypePragmaName, 94 unsigned Dim) 95 : RSDataTypeSpec(TypeName, TypePragmaName, Dim * Dim * sizeof(float)), 96 mDim(Dim) { 97 mClass = DT_RSMatrixClass; 98 return; 99 } 100 101 inline unsigned getDim() const { return mDim; } 102}; 103 104class RSObjectDataTypeSpec : public RSDataTypeSpec { 105 public: 106 RSObjectDataTypeSpec(const char *TypeName, 107 const char *TypePragmaName) 108 : RSDataTypeSpec(TypeName, TypePragmaName, 32 /* opaque pointer */) { 109 mClass = DT_RSObjectClass; 110 return; 111 } 112}; 113 114///////////////////////////////////////////////////////////////////////////// 115 116// clang::BuiltinType::Kind -> RSDataTypeSpec 117class ClangBuiltinTypeMap { 118 const char *mBuiltinTypeKind; 119 const RSDataTypeSpec *mDataType; 120 121 public: 122 ClangBuiltinTypeMap(const char *BuiltinTypeKind, 123 const RSDataTypeSpec *DataType) 124 : mBuiltinTypeKind(BuiltinTypeKind), 125 mDataType(DataType) { 126 return; 127 } 128 129 inline const char *getBuiltinTypeKind() const { return mBuiltinTypeKind; } 130 inline const RSDataTypeSpec *getDataType() const { return mDataType; } 131}; 132 133///////////////////////////////////////////////////////////////////////////// 134 135class RSDataKindSpec { 136 private: 137 const char *mKindName; 138 139 public: 140 explicit RSDataKindSpec(const char *KindName) : mKindName(KindName) { 141 return; 142 } 143 144 inline const char *getKindName() const { return mKindName; } 145}; 146 147///////////////////////////////////////////////////////////////////////////// 148 149class RSDataElementSpec { 150 private: 151 const char *mElementName; 152 const RSDataKindSpec *mDataKind; 153 const RSDataTypeSpec *mDataType; 154 bool mIsNormal; 155 unsigned mVectorSize; 156 157 public: 158 RSDataElementSpec(const char *ElementName, 159 const RSDataKindSpec *DataKind, 160 const RSDataTypeSpec *DataType, 161 bool IsNormal, 162 unsigned VectorSize) 163 : mElementName(ElementName), 164 mDataKind(DataKind), 165 mDataType(DataType), 166 mIsNormal(IsNormal), 167 mVectorSize(VectorSize) { 168 return; 169 } 170 171 inline const char *getElementName() const { return mElementName; } 172 inline const RSDataKindSpec *getDataKind() const { return mDataKind; } 173 inline const RSDataTypeSpec *getDataType() const { return mDataType; } 174 inline bool isNormal() const { return mIsNormal; } 175 inline unsigned getVectorSize() const { return mVectorSize; } 176}; 177 178///////////////////////////////////////////////////////////////////////////// 179 180// -gen-rs-data-type-enums 181// 182// ENUM_PRIMITIVE_DATA_TYPE(type, cname, bits) 183// ENUM_PRIMITIVE_DATA_TYPE_RANGE(begin_type, end_type) 184// ENUM_RS_MATRIX_DATA_TYPE(type, cname, bits) 185// ENUM_RS_MATRIX_DATA_TYPE_RANGE(begin_type, end_type) 186// ENUM_RS_OBJECT_DATA_TYPE(type, cname, bits) 187// ENUM_RS_OBJECT_DATA_TYPE_RANGE(begin_type, end_type) 188// 189// ENUM_RS_DATA_TYPE(type, cname, bits) 190// e.g., ENUM_RS_DATA_TYPE(Float32, "float", 256) 191static int GenRSDataTypeEnums(const RSDataTypeSpec *const DataTypes[], 192 unsigned NumDataTypes) { 193 // Alias missing #define 194#define ALIAS_DEF(x, y) \ 195 printf("#ifndef " #x "\n"); \ 196 printf("#define " #x "(type, cname, bits) " #y "(type, cname, bits)\n"); \ 197 printf("#endif\n\n") 198 ALIAS_DEF(ENUM_PRIMITIVE_DATA_TYPE, ENUM_RS_DATA_TYPE); 199 ALIAS_DEF(ENUM_RS_MATRIX_DATA_TYPE, ENUM_RS_DATA_TYPE); 200 ALIAS_DEF(ENUM_RS_OBJECT_DATA_TYPE, ENUM_RS_DATA_TYPE); 201#undef ALIAS_DEF 202 203#define ALIAS_DEF(x) \ 204 printf("#ifndef " #x "\n"); \ 205 printf("#define " #x "(begin_type, end_type)\n"); \ 206 printf("#endif\n\n") 207 ALIAS_DEF(ENUM_PRIMITIVE_DATA_TYPE_RANGE); 208 ALIAS_DEF(ENUM_RS_MATRIX_DATA_TYPE_RANGE); 209 ALIAS_DEF(ENUM_RS_OBJECT_DATA_TYPE_RANGE); 210#undef ALIAS_DEF 211 212#define DEF(x) \ 213 printf(#x "(%s, \"%s\", %lu)\n", \ 214 DataTypes[i]->getTypeName(), \ 215 DataTypes[i]->getTypePragmaName(), \ 216 DataTypes[i]->getSizeInBit()); 217#define DEF_RANGE(x, begin, end) \ 218 printf(#x "(%s, %s)\n\n", \ 219 DataTypes[begin]->getTypeName(), \ 220 DataTypes[end]->getTypeName()) 221 for (unsigned i = FirstPrimitiveType; i <= LastPrimitiveType; i++) 222 DEF(ENUM_PRIMITIVE_DATA_TYPE); 223 DEF_RANGE(ENUM_PRIMITIVE_DATA_TYPE_RANGE, 224 FirstPrimitiveType, LastPrimitiveType); 225 for (unsigned i = FirstRSMatrixType; i <= LastRSMatrixType; i++) 226 DEF(ENUM_RS_MATRIX_DATA_TYPE) 227 DEF_RANGE(ENUM_RS_MATRIX_DATA_TYPE_RANGE, 228 FirstRSMatrixType, LastRSMatrixType); 229 for (unsigned i = FirstRSObjectType; i <= LastRSObjectType; i++) 230 DEF(ENUM_RS_OBJECT_DATA_TYPE) 231 DEF_RANGE(ENUM_RS_OBJECT_DATA_TYPE_RANGE, 232 FirstRSObjectType, LastRSObjectType); 233#undef DEF 234#undef DEF_RANGE 235 236#define UNDEF(x) \ 237 printf("#undef " #x "\n") 238 UNDEF(ENUM_PRIMITIVE_DATA_TYPE); 239 UNDEF(ENUM_RS_MATRIX_DATA_TYPE); 240 UNDEF(ENUM_RS_OBJECT_DATA_TYPE); 241 UNDEF(ENUM_PRIMITIVE_DATA_TYPE_RANGE); 242 UNDEF(ENUM_RS_MATRIX_DATA_TYPE_RANGE); 243 UNDEF(ENUM_RS_OBJECT_DATA_TYPE_RANGE); 244 UNDEF(ENUM_RS_DATA_TYPE); 245 return 0; 246} 247 248// -gen-clang-builtin-cnames 249// 250// ENUM_SUPPORT_BUILTIN_TYPE(builtin_type, type, cname) 251// e.g., ENUM_SUPPORT_BUILTIN_TYPE(clang::BuiltinType::Float, Float32, "float") 252static int GenClangBuiltinEnum( 253 const ClangBuiltinTypeMap *const ClangBuilitinsMap[], 254 unsigned NumClangBuilitins) { 255 for (unsigned i = 0; i < NumClangBuilitins; i++) 256 printf("ENUM_SUPPORT_BUILTIN_TYPE(%s, %s, \"%s\")\n", 257 ClangBuilitinsMap[i]->getBuiltinTypeKind(), 258 ClangBuilitinsMap[i]->getDataType()->getTypeName(), 259 ClangBuilitinsMap[i]->getDataType()->getTypePragmaName()); 260 printf("#undef ENUM_SUPPORT_BUILTIN_TYPE"); 261 return 0; 262} 263 264// -gen-rs-matrix-type-enums 265// 266// ENUM_RS_MATRIX_TYPE(type, cname, dim) 267// e.g., ENUM_RS_MATRIX_TYPE(RSMatrix2x2, "rs_matrix2x2", 2) 268static int GenRSMatrixTypeEnums(const RSDataTypeSpec *const DataTypes[], 269 unsigned NumDataTypes) { 270 for (unsigned i = 0; i < NumDataTypes; i++) 271 if (DataTypes[i]->isRSMatrix()) { 272 const RSMatrixDataTypeSpec *const MatrixDataType = 273 static_cast<const RSMatrixDataTypeSpec *const>(DataTypes[i]); 274 printf("ENUM_RS_MATRIX_TYPE(%s, \"%s\", %u)\n", 275 MatrixDataType->getTypeName(), 276 MatrixDataType->getTypePragmaName(), 277 MatrixDataType->getDim()); 278 } 279 printf("#undef ENUM_RS_MATRIX_TYPE"); 280 return 0; 281} 282 283// -gen-rs-object-type-enums 284// 285// ENUM_RS_OBJECT_TYPE(type, cname) 286// e.g., ENUM_RS_OBJECT_TYPE(RSElement, "rs_element") 287static int GenRSObjectTypeEnums(const RSDataTypeSpec *const DataTypes[], 288 unsigned NumDataTypes) { 289 for (unsigned i = 0; i < NumDataTypes; i++) 290 if (DataTypes[i]->isRSObject()) 291 printf("ENUM_RS_OBJECT_TYPE(%s, \"%s\")\n", 292 DataTypes[i]->getTypeName(), 293 DataTypes[i]->getTypePragmaName()); 294 printf("#undef ENUM_RS_OBJECT_TYPE"); 295 return 0; 296} 297 298// -gen-rs-data-kind-enums 299// 300// ENUM_RS_DATA_KIND(kind) 301// e.g., ENUM_RS_DATA_KIND(PixelRGB) 302int GenRSDataKindEnums(const RSDataKindSpec *const DataKinds[], 303 unsigned NumDataKinds) { 304 for (unsigned i = 0; i < NumDataKinds; i++) 305 printf("ENUM_RS_DATA_KIND(%s)\n", DataKinds[i]->getKindName()); 306 printf("#undef ENUM_RS_DATA_KIND"); 307 return 0; 308} 309 310// -gen-rs-data-element-enums 311// 312// ENUM_RS_DATA_ELEMENT(name, dt, dk, normailized, vsize) 313// e.g., ENUM_RS_DATA_ELEMENT("rs_pixel_rgba", PixelRGB, Unsigned8, true, 4) 314int GenRSDataElementEnums(const RSDataElementSpec *const DataElements[], 315 unsigned NumDataElements) { 316 for (unsigned i = 0; i < NumDataElements; i++) 317 printf("ENUM_RS_DATA_ELEMENT(\"%s\", %s, %s, %s, %d)\n", 318 DataElements[i]->getElementName(), 319 DataElements[i]->getDataKind()->getKindName(), 320 DataElements[i]->getDataType()->getTypeName(), 321 ((DataElements[i]->isNormal()) ? "true" : "false"), 322 DataElements[i]->getVectorSize()); 323 printf("#undef ENUM_RS_DATA_ELEMENT"); 324 return 0; 325} 326 327int main(int argc, char **argv) { 328 if (argc < 2) { 329 fprintf(stderr, "usage: %s [gen type]\n", argv[0]); 330 return 1; 331 } 332 333 RSDataTypeSpec *DataTypes[] = { 334#define ENUM_PRIMITIVE_DATA_TYPE(x, name, bits) \ 335 new RSDataTypeSpec(#x , name, bits), 336#define PRIMITIVE_DATA_TYPE_RANGE(x, y) 337 PRIMITIVE_DATA_TYPE_ENUMS 338#undef ENUM_PRIMITIVE_DATA_TYPE 339#undef PRIMITIVE_DATA_TYPE_RANGE 340 341#define ENUM_RS_MATRIX_DATA_TYPE(x, name, dim) \ 342 new RSMatrixDataTypeSpec(#x , name, dim), 343#define RS_MATRIX_DATA_TYPE_RANGE(x, y) 344 RS_MATRIX_DATA_TYPE_ENUMS 345#undef ENUM_RS_MATRIX_DATA_TYPE 346#undef RS_MATRIX_DATA_TYPE_RANGE 347 348#define ENUM_RS_OBJECT_DATA_TYPE(x, name) \ 349 new RSObjectDataTypeSpec(#x, name), 350#define RS_OBJECT_DATA_TYPE_RANGE(x, y) 351 RS_OBJECT_DATA_TYPE_ENUMS 352#undef ENUM_RS_OBJECT_DATA_TYPE 353#undef RS_OBJECT_DATA_TYPE_RANGE 354 }; 355 356 unsigned NumDataTypes = sizeof(DataTypes) / sizeof(DataTypes[0]); 357 ///////////////////////////////////////////////////////////////////////////// 358 359 ClangBuiltinTypeMap *ClangBuilitinsMap[] = { 360 new ClangBuiltinTypeMap("clang::BuiltinType::Bool", DataTypes[Boolean]), 361 new ClangBuiltinTypeMap("clang::BuiltinType::Char_U", DataTypes[Unsigned8]), 362 new ClangBuiltinTypeMap("clang::BuiltinType::UChar", DataTypes[Unsigned8]), 363 new ClangBuiltinTypeMap("clang::BuiltinType::Char16", DataTypes[Signed16]), 364 new ClangBuiltinTypeMap("clang::BuiltinType::Char32", DataTypes[Signed32]), 365 new ClangBuiltinTypeMap("clang::BuiltinType::UShort", DataTypes[Unsigned16]), 366 new ClangBuiltinTypeMap("clang::BuiltinType::UInt", DataTypes[Unsigned32]), 367 new ClangBuiltinTypeMap("clang::BuiltinType::ULong", DataTypes[Unsigned32]), 368 new ClangBuiltinTypeMap("clang::BuiltinType::ULongLong", DataTypes[Unsigned64]), 369 370 new ClangBuiltinTypeMap("clang::BuiltinType::Char_S", DataTypes[Signed8]), 371 new ClangBuiltinTypeMap("clang::BuiltinType::SChar", DataTypes[Signed8]), 372 new ClangBuiltinTypeMap("clang::BuiltinType::Short", DataTypes[Signed16]), 373 new ClangBuiltinTypeMap("clang::BuiltinType::Int", DataTypes[Signed32]), 374 new ClangBuiltinTypeMap("clang::BuiltinType::Long", DataTypes[Signed64]), 375 new ClangBuiltinTypeMap("clang::BuiltinType::LongLong", DataTypes[Signed64]), 376 377 new ClangBuiltinTypeMap("clang::BuiltinType::Float", DataTypes[Float32]), 378 new ClangBuiltinTypeMap("clang::BuiltinType::Double", DataTypes[Float64]), 379 }; 380 381 unsigned NumClangBuilitins = 382 sizeof(ClangBuilitinsMap) / sizeof(ClangBuilitinsMap[0]); 383 384 ///////////////////////////////////////////////////////////////////////////// 385 386 RSDataKindSpec *DataKinds[] = { 387#define ENUM_RS_DATA_KIND(x) \ 388 new RSDataKindSpec(#x), 389 RS_DATA_KIND_ENUMS 390#undef ENUM_RS_DATA_KIND 391 }; 392 393 unsigned NumDataKinds = sizeof(DataKinds) / sizeof(DataKinds[0]); 394 ///////////////////////////////////////////////////////////////////////////// 395 396 RSDataElementSpec *DataElements[] = { 397 new RSDataElementSpec("rs_pixel_l", 398 DataKinds[PixelL], 399 DataTypes[Unsigned8], 400 /* IsNormal = */true, /* VectorSize = */1), 401 new RSDataElementSpec("rs_pixel_a", 402 DataKinds[PixelA], 403 DataTypes[Unsigned8], 404 true, 1), 405 new RSDataElementSpec("rs_pixel_la", 406 DataKinds[PixelLA], 407 DataTypes[Unsigned8], 408 true, 2), 409 new RSDataElementSpec("rs_pixel_rgb", 410 DataKinds[PixelRGB], 411 DataTypes[Unsigned8], 412 true, 3), 413 new RSDataElementSpec("rs_pixel_rgba", 414 DataKinds[PixelRGB], 415 DataTypes[Unsigned8], 416 true, 4), 417 new RSDataElementSpec("rs_pixel_rgb565", 418 DataKinds[PixelRGB], 419 DataTypes[Unsigned8], 420 true, 3), 421 new RSDataElementSpec("rs_pixel_rgb5551", 422 DataKinds[PixelRGBA], 423 DataTypes[Unsigned8], 424 true, 4), 425 new RSDataElementSpec("rs_pixel_rgb4444", 426 DataKinds[PixelRGBA], 427 DataTypes[Unsigned8], 428 true, 4), 429 }; 430 431 unsigned NumDataElements = sizeof(DataElements) / sizeof(DataElements[0]); 432 ///////////////////////////////////////////////////////////////////////////// 433 int Result = 1; 434 435 if (::strcmp(argv[1], "-gen-rs-data-type-enums") == 0) 436 Result = GenRSDataTypeEnums(DataTypes, NumDataTypes); 437 else if (::strcmp(argv[1], "-gen-clang-builtin-enums") == 0) 438 Result = GenClangBuiltinEnum(ClangBuilitinsMap, NumClangBuilitins); 439 else if (::strcmp(argv[1], "-gen-rs-matrix-type-enums") == 0) 440 Result = GenRSMatrixTypeEnums(DataTypes, NumDataTypes); 441 else if (::strcmp(argv[1], "-gen-rs-object-type-enums") == 0) 442 Result = GenRSObjectTypeEnums(DataTypes, NumDataTypes); 443 else if (::strcmp(argv[1], "-gen-rs-data-kind-enums") == 0) 444 Result = GenRSDataKindEnums(DataKinds, NumDataKinds); 445 else if (::strcmp(argv[1], "-gen-rs-data-element-enums") == 0) 446 Result = GenRSDataElementEnums(DataElements, NumDataElements); 447 else 448 fprintf(stderr, "%s: Unknown table generation type '%s'\n", 449 argv[0], argv[1]); 450 451 452 ///////////////////////////////////////////////////////////////////////////// 453 for (unsigned i = 0; i < NumDataTypes; i++) 454 delete DataTypes[i]; 455 for (unsigned i = 0; i < NumClangBuilitins; i++) 456 delete ClangBuilitinsMap[i]; 457 for (unsigned i = 0; i < NumDataKinds; i++) 458 delete DataKinds[i]; 459 for (unsigned i = 0; i < NumDataElements; i++) 460 delete DataElements[i]; 461 462 return Result; 463} 464