Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012, 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 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 17 | #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_ // NOLINT |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 18 | #define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_ |
| 19 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 20 | #include "slang_rs_reflect_utils.h" |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 21 | |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 22 | #include <set> |
| 23 | #include <string> |
| 24 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 25 | #define RS_EXPORT_VAR_PREFIX "mExportVar_" |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 26 | |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 27 | namespace slang { |
| 28 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 29 | class RSReflectionCpp { |
| 30 | public: |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 31 | RSReflectionCpp(const RSContext *Context, const std::string &OutputDirectory, |
| 32 | const std::string &RSSourceFileName, |
| 33 | const std::string &BitCodeFileName); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 34 | virtual ~RSReflectionCpp(); |
| 35 | |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 36 | bool reflect(); |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 37 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 38 | private: |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 39 | struct Argument { |
| 40 | std::string Type; |
| 41 | std::string Name; |
| 42 | std::string DefaultValue; |
| 43 | Argument(std::string Type, std::string Name, std::string DefaultValue = "") |
| 44 | : Type(Type), Name(Name), DefaultValue(DefaultValue) {} |
| 45 | }; |
| 46 | typedef std::vector<Argument> ArgumentList; |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 47 | |
| 48 | // Information coming from the compiler about the code we're reflecting. |
| 49 | const RSContext *mRSContext; |
| 50 | |
| 51 | // Path to the *.rs file for which we're generating C++ code. |
| 52 | std::string mRSSourceFilePath; |
| 53 | // Path to the file that contains the byte code generated from the *.rs file. |
| 54 | std::string mBitCodeFilePath; |
| 55 | // The directory where we'll generate the C++ files. |
| 56 | std::string mOutputDirectory; |
| 57 | // A cleaned up version of the *.rs file name that can be used in generating |
| 58 | // C++ identifiers. |
| 59 | std::string mCleanedRSFileName; |
| 60 | // The name of the generated C++ class. |
| 61 | std::string mClassName; |
| 62 | |
| 63 | // TODO document |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 64 | unsigned int mNextExportVarSlot; |
| 65 | unsigned int mNextExportFuncSlot; |
| 66 | unsigned int mNextExportForEachSlot; |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 67 | unsigned int mNextExportReduceSlot; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 68 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 69 | // Generated RS Elements for type-checking code. |
| 70 | std::set<std::string> mTypesToCheck; |
| 71 | |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 72 | inline void clear() { |
| 73 | mNextExportVarSlot = 0; |
| 74 | mNextExportFuncSlot = 0; |
| 75 | mNextExportForEachSlot = 0; |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 76 | mNextExportReduceSlot = 0; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 77 | mTypesToCheck.clear(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 80 | // The file we are currently generating, either the header or the |
| 81 | // implementation file. |
| 82 | GeneratedFile mOut; |
| 83 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 84 | void genInitValue(const clang::APValue &Val, bool asBool = false); |
| 85 | static const char *getVectorAccessor(unsigned index); |
| 86 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 87 | inline unsigned int getNextExportVarSlot() { return mNextExportVarSlot++; } |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 88 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 89 | inline unsigned int getNextExportFuncSlot() { return mNextExportFuncSlot++; } |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 90 | |
| 91 | inline unsigned int getNextExportForEachSlot() { |
| 92 | return mNextExportForEachSlot++; |
| 93 | } |
| 94 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 95 | inline unsigned int getNextExportReduceSlot() { |
| 96 | return mNextExportReduceSlot++; |
| 97 | } |
| 98 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 99 | bool writeHeaderFile(); |
| 100 | bool writeImplementationFile(); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 101 | |
| 102 | // Write out signatures both in the header and implementation. |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 103 | void makeFunctionSignature(bool isDefinition, const RSExportFunc *ef); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 104 | void makeReduceSignatureAllocationVariant(bool isDefinition, const RSExportReduce *er); |
| 105 | void makeReduceSignatureArrayVariant(bool isDefinition, const RSExportReduce *er); |
| 106 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 107 | bool genEncodedBitCode(); |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 108 | void genFieldsToStoreExportVariableValues(); |
| 109 | void genTypeInstancesUsedInForEach(); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 110 | void genTypeInstancesUsedInReduce(); |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 111 | void genFieldsForAllocationTypeVerification(); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 112 | |
| 113 | // Write out the code for the getters and setters. |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 114 | void genExportVariablesGetterAndSetter(); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 115 | |
| 116 | // Write out the code for the declaration of the kernel entry points. |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 117 | void genForEachDeclarations(); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 118 | void genReduceDeclarations(); |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 119 | void genExportFunctionDeclarations(); |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 120 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 121 | // Write out code for the definitions of the kernel entry points. |
| 122 | void genExportForEachBodies(); |
| 123 | void genExportReduceBodies(); |
| 124 | void genExportFunctionBodies(); |
| 125 | |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 126 | bool startScriptHeader(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 127 | |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 128 | // Write out code for an export variable initialization. |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 129 | void genInitExportVariable(const RSExportType *ET, const std::string &VarName, |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 130 | const clang::APValue &Val); |
| 131 | void genZeroInitExportVariable(const std::string &VarName); |
| 132 | void genInitBoolExportVariable(const std::string &VarName, |
| 133 | const clang::APValue &Val); |
| 134 | void genInitPrimitiveExportVariable(const std::string &VarName, |
| 135 | const clang::APValue &Val); |
| 136 | |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 137 | // Produce an argument string of the form "T1 t, T2 u, T3 v". |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 138 | void genArguments(const ArgumentList &Args, int Offset); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 139 | |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 140 | void genPointerTypeExportVariable(const RSExportVar *EV); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 141 | void genMatrixTypeExportVariable(const RSExportVar *EV); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 142 | void genRecordTypeExportVariable(const RSExportVar *EV); |
| 143 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 144 | void genGetterAndSetter(const RSExportPrimitiveType *EPT, const RSExportVar* EV); |
| 145 | void genGetterAndSetter(const RSExportVectorType *EVT, const RSExportVar* EV); |
| 146 | void genGetterAndSetter(const RSExportConstantArrayType *AT, const RSExportVar* EV); |
| 147 | void genGetterAndSetter(const RSExportRecordType *ERT, const RSExportVar *EV); |
| 148 | |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 149 | // Write out a local FieldPacker (if necessary). |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 150 | bool genCreateFieldPacker(const RSExportType *T, const char *FieldPackerName); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 151 | |
| 152 | // Populate (write) the FieldPacker with add() operations. |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 153 | void genPackVarOfType(const RSExportType *ET, const char *VarName, |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 154 | const char *FieldPackerName); |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 155 | |
| 156 | // Generate a runtime type check for VarName. |
| 157 | void genTypeCheck(const RSExportType *ET, const char *VarName); |
| 158 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 159 | // Generate a runtime check that VarName is 1-dimensional. |
| 160 | void gen1DCheck(const std::string &VarName); |
| 161 | |
| 162 | // Generate a runtime check that VarName is non-null. |
| 163 | void genNullOrEmptyArrayCheck(const std::string &ArrayName, const std::string &Length, |
| 164 | const std::string &ValueToReturn); |
| 165 | |
| 166 | // Generate a runtime check that ArrayName's length is a multiple of |
| 167 | // a vector size. |
| 168 | void genVectorLengthCompatibilityCheck(const std::string &Length, unsigned VecSize, |
| 169 | const std::string &ValueToReturn, |
| 170 | unsigned IndentLevels = 1); |
| 171 | |
| 172 | // Generate a type instance for a given type. |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 173 | void genTypeInstanceFromPointer(const RSExportType *ET); |
| 174 | void genTypeInstance(const RSExportType *ET); |
| 175 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 176 | }; // class RSReflectionCpp |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 177 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 178 | } // namespace slang |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 179 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 180 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_ NOLINT |