slang_rs_spec_table.cpp revision 0307eaa71dc1d05582347cacd9209ac1e6100102
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  PRIMITIVE_DATA_TYPE_ENUMS
26#undef ENUM_PRIMITIVE_DATA_TYPE
27#define ENUM_RS_OBJECT_DATA_TYPE(x, name) x,
28  RS_OBJECT_DATA_TYPE_ENUMS
29#undef ENUM_RS_OBJECT_DATA_TYPE
30};
31
32enum {
33#define ENUM_RS_DATA_KIND(x) x,
34  RS_DATA_KIND_ENUMS
35#undef ENUM_RS_DATA_KIND
36};
37
38class RSDataTypeSpec {
39 private:
40  const char *mTypeName;        // e.g. Float32
41  // FIXME: better name
42  const char *mTypePragmaName;  // e.g. float
43  size_t mBits;
44
45 protected:
46  bool mIsRSObject;
47
48 public:
49  RSDataTypeSpec(const char *TypeName,
50                 const char *TypePragmaName,
51                 size_t Bits)
52      : mTypeName(TypeName),
53        mTypePragmaName(TypePragmaName),
54        mBits(Bits),
55        mIsRSObject(false) {
56    return;
57  }
58
59  inline const char *getTypeName() const { return mTypeName; }
60  inline const char *getTypePragmaName() const { return mTypePragmaName; }
61  inline size_t getSizeInBit() const { return mBits; }
62  inline bool isRSObject() const { return mIsRSObject; }
63};
64
65class RSObjectDataTypeSpec : public RSDataTypeSpec {
66 public:
67  RSObjectDataTypeSpec(const char *TypeName,
68                       const char *TypePragmaName)
69      : RSDataTypeSpec(TypeName, TypePragmaName, 32 /* opaque pointer */) {
70    mIsRSObject = true;
71    return;
72  }
73};
74
75/////////////////////////////////////////////////////////////////////////////
76
77// clang::BuiltinType::Kind -> RSDataTypeSpec
78class ClangBuiltinTypeMap {
79  const char *mBuiltinTypeKind;
80  const RSDataTypeSpec *mDataType;
81
82 public:
83  ClangBuiltinTypeMap(const char *BuiltinTypeKind,
84                      const RSDataTypeSpec *DataType)
85      : mBuiltinTypeKind(BuiltinTypeKind),
86        mDataType(DataType) {
87    return;
88  }
89
90  inline const char *getBuiltinTypeKind() const { return mBuiltinTypeKind; }
91  inline const RSDataTypeSpec *getDataType() const { return mDataType; }
92};
93
94/////////////////////////////////////////////////////////////////////////////
95
96class RSDataKindSpec {
97 private:
98  const char *mKindName;
99
100 public:
101  explicit RSDataKindSpec(const char *KindName) : mKindName(KindName) {
102    return;
103  }
104
105  inline const char *getKindName() const { return mKindName; }
106};
107
108/////////////////////////////////////////////////////////////////////////////
109
110class RSDataElementSpec {
111 private:
112  const char *mElementName;
113  const RSDataKindSpec *mDataKind;
114  const RSDataTypeSpec *mDataType;
115  bool mIsNormal;
116  unsigned mVectorSize;
117
118 public:
119  RSDataElementSpec(const char *ElementName,
120                    const RSDataKindSpec *DataKind,
121                    const RSDataTypeSpec *DataType,
122                    bool IsNormal,
123                    unsigned VectorSize)
124      : mElementName(ElementName),
125        mDataKind(DataKind),
126        mDataType(DataType),
127        mIsNormal(IsNormal),
128        mVectorSize(VectorSize) {
129    return;
130  }
131
132  inline const char *getElementName() const { return mElementName; }
133  inline const RSDataKindSpec *getDataKind() const { return mDataKind; }
134  inline const RSDataTypeSpec *getDataType() const { return mDataType; }
135  inline bool isNormal() const { return mIsNormal; }
136  inline unsigned getVectorSize() const { return mVectorSize; }
137};
138
139/////////////////////////////////////////////////////////////////////////////
140
141// -gen-rs-data-type-enums
142//
143// ENUM_RS_DATA_TYPE(type, cname, bits)
144// e.g., ENUM_RS_DATA_TYPE(Float32, "float", 256)
145static int GenRSDataTypeEnums(const RSDataTypeSpec *const DataTypes[],
146                              unsigned NumDataTypes) {
147  for (unsigned i = 0; i < NumDataTypes; i++)
148    printf("ENUM_RS_DATA_TYPE(%s, \"%s\", %lu)\n",
149           DataTypes[i]->getTypeName(),
150           DataTypes[i]->getTypePragmaName(),
151           DataTypes[i]->getSizeInBit());
152  printf("#undef ENUM_RS_DATA_TYPE");
153  return 0;
154}
155
156// -gen-clang-builtin-cnames
157//
158// ENUM_SUPPORT_BUILTIN_TYPE(builtin_type, type, cname)
159// e.g., ENUM_SUPPORT_BUILTIN_TYPE(clang::BuiltinType::Float, Float32, "float")
160static int GenClangBuiltinEnum(
161    const ClangBuiltinTypeMap *const ClangBuilitinsMap[],
162    unsigned NumClangBuilitins) {
163  for (unsigned i = 0; i < NumClangBuilitins; i++)
164    printf("ENUM_SUPPORT_BUILTIN_TYPE(%s, %s, \"%s\")\n",
165           ClangBuilitinsMap[i]->getBuiltinTypeKind(),
166           ClangBuilitinsMap[i]->getDataType()->getTypeName(),
167           ClangBuilitinsMap[i]->getDataType()->getTypePragmaName());
168  printf("#undef ENUM_SUPPORT_BUILTIN_TYPE");
169  return 0;
170}
171
172// -gen-rs-object-type-enums
173//
174// ENUM_RS_OBJECT_TYPE(type, cname)
175// e.g., ENUM_RS_OBJECT_TYPE(RSMatrix2x2, "rs_matrix2x2")
176static int GenRSObjectTypeEnums(const RSDataTypeSpec *const DataTypes[],
177                                unsigned NumDataTypes) {
178  for (unsigned i = 0; i < NumDataTypes; i++)
179    if (DataTypes[i]->isRSObject())
180      printf("ENUM_RS_OBJECT_TYPE(%s, \"%s\")\n",
181             DataTypes[i]->getTypeName(),
182             DataTypes[i]->getTypePragmaName());
183  printf("#undef ENUM_RS_OBJECT_TYPE");
184  return 0;
185}
186
187// -gen-rs-data-kind-enums
188//
189// ENUM_RS_DATA_KIND(kind)
190// e.g., ENUM_RS_DATA_KIND(PixelRGB)
191int GenRSDataKindEnums(const RSDataKindSpec *const DataKinds[],
192                       unsigned NumDataKinds) {
193  for (unsigned i = 0; i < NumDataKinds; i++)
194    printf("ENUM_RS_DATA_KIND(%s)\n", DataKinds[i]->getKindName());
195  printf("#undef ENUM_RS_DATA_KIND");
196  return 0;
197}
198
199// -gen-rs-data-element-enums
200//
201// ENUM_RS_DATA_ELEMENT(name, dt, dk, normailized, vsize)
202// e.g., ENUM_RS_DATA_ELEMENT("rs_pixel_rgba", PixelRGB, Unsigned8, true, 4)
203int GenRSDataElementEnums(const RSDataElementSpec *const DataElements[],
204                          unsigned NumDataElements) {
205  for (unsigned i = 0; i < NumDataElements; i++)
206    printf("ENUM_RS_DATA_ELEMENT(\"%s\", %s, %s, %s, %d)\n",
207           DataElements[i]->getElementName(),
208           DataElements[i]->getDataKind()->getKindName(),
209           DataElements[i]->getDataType()->getTypeName(),
210           ((DataElements[i]->isNormal()) ? "true" : "false"),
211           DataElements[i]->getVectorSize());
212  printf("#undef ENUM_RS_DATA_ELEMENT");
213  return 0;
214}
215
216int main(int argc, char **argv) {
217  if (argc < 2) {
218    fprintf(stderr, "usage: %s [gen type]\n", argv[0]);
219    return 1;
220  }
221
222  RSDataTypeSpec *DataTypes[] = {
223#define ENUM_PRIMITIVE_DATA_TYPE(x, name, bits) \
224  new RSDataTypeSpec(#x , name, bits),
225  PRIMITIVE_DATA_TYPE_ENUMS
226#undef ENUM_PRIMITIVE_DATA_TYPE
227#define ENUM_RS_OBJECT_DATA_TYPE(x, name)  \
228  new RSObjectDataTypeSpec(#x, name),
229  RS_OBJECT_DATA_TYPE_ENUMS
230#undef ENUM_RS_OBJECT_DATA_TYPE
231  };
232
233  unsigned NumDataTypes = sizeof(DataTypes) / sizeof(DataTypes[0]);
234  /////////////////////////////////////////////////////////////////////////////
235
236  ClangBuiltinTypeMap *ClangBuilitinsMap[] = {
237    new ClangBuiltinTypeMap("clang::BuiltinType::Bool",   DataTypes[Boolean]),
238    new ClangBuiltinTypeMap("clang::BuiltinType::Char_U", DataTypes[Unsigned8]),
239    new ClangBuiltinTypeMap("clang::BuiltinType::UChar",  DataTypes[Unsigned8]),
240    new ClangBuiltinTypeMap("clang::BuiltinType::Char16", DataTypes[Signed16]),
241    new ClangBuiltinTypeMap("clang::BuiltinType::Char32", DataTypes[Signed32]),
242    new ClangBuiltinTypeMap("clang::BuiltinType::UShort", DataTypes[Unsigned16]),
243    new ClangBuiltinTypeMap("clang::BuiltinType::UInt",   DataTypes[Unsigned32]),
244    new ClangBuiltinTypeMap("clang::BuiltinType::ULong",  DataTypes[Unsigned32]),
245    new ClangBuiltinTypeMap("clang::BuiltinType::ULongLong", DataTypes[Unsigned64]),
246
247    new ClangBuiltinTypeMap("clang::BuiltinType::Char_S", DataTypes[Signed8]),
248    new ClangBuiltinTypeMap("clang::BuiltinType::SChar",  DataTypes[Signed8]),
249    new ClangBuiltinTypeMap("clang::BuiltinType::Short",  DataTypes[Signed16]),
250    new ClangBuiltinTypeMap("clang::BuiltinType::Int",    DataTypes[Signed32]),
251    new ClangBuiltinTypeMap("clang::BuiltinType::Long",   DataTypes[Signed64]),
252    new ClangBuiltinTypeMap("clang::BuiltinType::LongLong", DataTypes[Signed64]),
253
254    new ClangBuiltinTypeMap("clang::BuiltinType::Float",  DataTypes[Float32]),
255    new ClangBuiltinTypeMap("clang::BuiltinType::Double", DataTypes[Float64]),
256  };
257
258  unsigned NumClangBuilitins =
259      sizeof(ClangBuilitinsMap) / sizeof(ClangBuilitinsMap[0]);
260
261  /////////////////////////////////////////////////////////////////////////////
262
263  RSDataKindSpec *DataKinds[] = {
264#define ENUM_RS_DATA_KIND(x)  \
265    new RSDataKindSpec(#x),
266    RS_DATA_KIND_ENUMS
267#undef ENUM_RS_DATA_KIND
268  };
269
270  unsigned NumDataKinds = sizeof(DataKinds) / sizeof(DataKinds[0]);
271  /////////////////////////////////////////////////////////////////////////////
272
273  RSDataElementSpec *DataElements[] = {
274    new RSDataElementSpec("rs_pixel_l",
275                          DataKinds[PixelL],
276                          DataTypes[Unsigned8],
277                          /* IsNormal = */true, /* VectorSize = */1),
278    new RSDataElementSpec("rs_pixel_a",
279                          DataKinds[PixelA],
280                          DataTypes[Unsigned8],
281                          true, 1),
282    new RSDataElementSpec("rs_pixel_la",
283                          DataKinds[PixelLA],
284                          DataTypes[Unsigned8],
285                          true, 2),
286    new RSDataElementSpec("rs_pixel_rgb",
287                          DataKinds[PixelRGB],
288                          DataTypes[Unsigned8],
289                          true, 3),
290    new RSDataElementSpec("rs_pixel_rgba",
291                          DataKinds[PixelRGB],
292                          DataTypes[Unsigned8],
293                          true, 4),
294    new RSDataElementSpec("rs_pixel_rgb565",
295                          DataKinds[PixelRGB],
296                          DataTypes[Unsigned8],
297                          true, 3),
298    new RSDataElementSpec("rs_pixel_rgb5551",
299                          DataKinds[PixelRGBA],
300                          DataTypes[Unsigned8],
301                          true, 4),
302    new RSDataElementSpec("rs_pixel_rgb4444",
303                          DataKinds[PixelRGBA],
304                          DataTypes[Unsigned8],
305                          true, 4),
306  };
307
308  unsigned NumDataElements = sizeof(DataElements) / sizeof(DataElements[0]);
309  /////////////////////////////////////////////////////////////////////////////
310  int Result = 1;
311
312  if (::strcmp(argv[1], "-gen-rs-data-type-enums") == 0)
313    Result = GenRSDataTypeEnums(DataTypes, NumDataTypes);
314  else if (::strcmp(argv[1], "-gen-clang-builtin-enums") == 0)
315    Result = GenClangBuiltinEnum(ClangBuilitinsMap, NumClangBuilitins);
316  else if (::strcmp(argv[1], "-gen-rs-object-type-enums") == 0)
317    Result = GenRSObjectTypeEnums(DataTypes, NumDataTypes);
318  else if (::strcmp(argv[1], "-gen-rs-data-kind-enums") == 0)
319    Result = GenRSDataKindEnums(DataKinds, NumDataKinds);
320  else if (::strcmp(argv[1], "-gen-rs-data-element-enums") == 0)
321    Result = GenRSDataElementEnums(DataElements, NumDataElements);
322  else
323    fprintf(stderr, "%s: Unknown table generation type '%s'\n",
324                    argv[0], argv[1]);
325
326
327  /////////////////////////////////////////////////////////////////////////////
328  for (unsigned i = 0; i < NumDataTypes; i++)
329    delete DataTypes[i];
330  for (unsigned i = 0; i < NumClangBuilitins; i++)
331    delete ClangBuilitinsMap[i];
332  for (unsigned i = 0; i < NumDataKinds; i++)
333    delete DataKinds[i];
334  for (unsigned i = 0; i < NumDataElements; i++)
335    delete DataElements[i];
336
337  return Result;
338}
339