Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 1 | /* |
Tim Murray | ee9d7b0 | 2013-07-30 11:13:27 -0700 | [diff] [blame] | 2 | * Copyright 2013, The Android Open Source Project |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 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 <sys/stat.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 20 | #include <iostream> |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 21 | |
| 22 | #include <cstdarg> |
| 23 | #include <cctype> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <sstream> |
| 27 | #include <string> |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 28 | |
| 29 | #include "os_sep.h" |
| 30 | #include "slang_rs_context.h" |
| 31 | #include "slang_rs_export_var.h" |
| 32 | #include "slang_rs_export_foreach.h" |
| 33 | #include "slang_rs_export_func.h" |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 34 | #include "slang_rs_export_reduce.h" |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 35 | #include "slang_rs_reflect_utils.h" |
| 36 | #include "slang_version.h" |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 37 | |
| 38 | #include "slang_rs_reflection_cpp.h" |
| 39 | |
| 40 | using namespace std; |
| 41 | |
| 42 | namespace slang { |
| 43 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 44 | const char kRsTypeItemClassName[] = "Item"; |
| 45 | const char kRsElemPrefix[] = "__rs_elem_"; |
| 46 | // The name of the Allocation type that is reflected in C++ |
| 47 | const char kAllocationSp[] = "android::RSC::sp<android::RSC::Allocation>"; |
| 48 | const char kConstRsScriptCall[] = "const RsScriptCall"; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 49 | |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 50 | static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) { |
| 51 | static const char *MatrixTypeCNameMap[] = { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 52 | "rs_matrix2x2", "rs_matrix3x3", "rs_matrix4x4", |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 53 | }; |
| 54 | unsigned Dim = EMT->getDim(); |
| 55 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 56 | if ((Dim - 2) < (sizeof(MatrixTypeCNameMap) / sizeof(const char *))) |
| 57 | return MatrixTypeCNameMap[EMT->getDim() - 2]; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 58 | |
| 59 | slangAssert(false && "GetMatrixTypeName : Unsupported matrix dimension"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 60 | return nullptr; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 63 | static std::string GetTypeName(const RSExportType *ET, bool Brackets = true) { |
| 64 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 65 | case RSExportType::ExportClassPrimitive: { |
| 66 | const RSExportPrimitiveType *EPT = |
| 67 | static_cast<const RSExportPrimitiveType *>(ET); |
| 68 | if (EPT->isRSObjectType()) { |
| 69 | return std::string("android::RSC::sp<const android::RSC::") + |
| 70 | RSExportPrimitiveType::getRSReflectionType(EPT)->c_name + ">"; |
| 71 | } else { |
| 72 | return RSExportPrimitiveType::getRSReflectionType(EPT)->c_name; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 73 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 74 | } |
| 75 | case RSExportType::ExportClassPointer: { |
| 76 | const RSExportType *PointeeType = |
| 77 | static_cast<const RSExportPointerType *>(ET)->getPointeeType(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 78 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 79 | if (PointeeType->getClass() != RSExportType::ExportClassRecord) |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 80 | return kAllocationSp; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 81 | else |
| 82 | return PointeeType->getElementName(); |
| 83 | } |
| 84 | case RSExportType::ExportClassVector: { |
| 85 | const RSExportVectorType *EVT = static_cast<const RSExportVectorType *>(ET); |
| 86 | std::stringstream VecName; |
| 87 | VecName << EVT->getRSReflectionType(EVT)->rs_c_vector_prefix |
| 88 | << EVT->getNumElement(); |
| 89 | return VecName.str(); |
| 90 | } |
| 91 | case RSExportType::ExportClassMatrix: { |
| 92 | return GetMatrixTypeName(static_cast<const RSExportMatrixType *>(ET)); |
| 93 | } |
| 94 | case RSExportType::ExportClassConstantArray: { |
| 95 | // TODO: Fix this for C arrays! |
| 96 | const RSExportConstantArrayType *CAT = |
| 97 | static_cast<const RSExportConstantArrayType *>(ET); |
| 98 | std::string ElementTypeName = GetTypeName(CAT->getElementType()); |
| 99 | if (Brackets) { |
| 100 | ElementTypeName.append("[]"); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 101 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 102 | return ElementTypeName; |
| 103 | } |
| 104 | case RSExportType::ExportClassRecord: { |
| 105 | // TODO: Fix for C structs! |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 106 | return ET->getElementName() + "." + kRsTypeItemClassName; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 107 | } |
| 108 | default: { slangAssert(false && "Unknown class of type"); } |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return ""; |
| 112 | } |
| 113 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 114 | static bool canExportReduceArrayVariant(const RSExportType *Type) { |
| 115 | // FIXME: No half types available for C++ reflection yet |
| 116 | if (Type->getElementName().find("F16") == 0) { |
| 117 | return false; |
| 118 | } |
| 119 | return Type->getClass() == RSExportType::ExportClassPrimitive || |
| 120 | Type->getClass() == RSExportType::ExportClassVector; |
| 121 | } |
| 122 | |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 123 | RSReflectionCpp::RSReflectionCpp(const RSContext *Context, |
| 124 | const string &OutputDirectory, |
| 125 | const string &RSSourceFileName, |
| 126 | const string &BitCodeFileName) |
| 127 | : mRSContext(Context), mRSSourceFilePath(RSSourceFileName), |
| 128 | mBitCodeFilePath(BitCodeFileName), mOutputDirectory(OutputDirectory), |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 129 | mNextExportVarSlot(0), mNextExportFuncSlot(0), mNextExportForEachSlot(0), |
| 130 | mNextExportReduceSlot(0) { |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 131 | mCleanedRSFileName = RootNameFromRSFileName(mRSSourceFilePath); |
| 132 | mClassName = "ScriptC_" + mCleanedRSFileName; |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 135 | RSReflectionCpp::~RSReflectionCpp() {} |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 136 | |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 137 | bool RSReflectionCpp::reflect() { |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 138 | writeHeaderFile(); |
| 139 | writeImplementationFile(); |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 140 | |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 141 | return true; |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 144 | #define RS_TYPE_CLASS_NAME_PREFIX "ScriptField_" |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 145 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 146 | bool RSReflectionCpp::writeHeaderFile() { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 147 | // Create the file and write the license note. |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 148 | if (!mOut.startFile(mOutputDirectory, mClassName + ".h", mRSSourceFilePath, |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 149 | mRSContext->getLicenseNote(), false, |
| 150 | mRSContext->getVerbose())) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 151 | return false; |
| 152 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 153 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 154 | mOut.indent() << "#include \"RenderScript.h\"\n\n"; |
| 155 | mOut.indent() << "using namespace android::RSC;\n\n"; |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 156 | |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 157 | mOut.comment("This class encapsulates access to the exported elements of the script. " |
| 158 | "Typically, you would instantiate this class once, call the set_* methods " |
| 159 | "for each of the exported global variables you want to change, then call " |
| 160 | "one of the forEach_ methods to invoke a kernel."); |
| 161 | mOut.indent() << "class " << mClassName << " : public android::RSC::ScriptC"; |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 162 | mOut.startBlock(); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 163 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 164 | mOut.decreaseIndent(); |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 165 | mOut.indent() << "private:\n"; |
| 166 | mOut.increaseIndent(); |
| 167 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 168 | genFieldsToStoreExportVariableValues(); |
| 169 | genTypeInstancesUsedInForEach(); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 170 | genTypeInstancesUsedInReduce(); |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 171 | genFieldsForAllocationTypeVerification(); |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 172 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 173 | mOut.decreaseIndent(); |
| 174 | mOut.indent() << "public:\n"; |
| 175 | mOut.increaseIndent(); |
| 176 | |
| 177 | // Generate the constructor and destructor declarations. |
| 178 | mOut.indent() << mClassName << "(android::RSC::sp<android::RSC::RS> rs);\n"; |
| 179 | mOut.indent() << "virtual ~" << mClassName << "();\n\n"; |
| 180 | |
| 181 | genExportVariablesGetterAndSetter(); |
| 182 | genForEachDeclarations(); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 183 | genReduceDeclarations(); |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 184 | genExportFunctionDeclarations(); |
| 185 | |
| 186 | mOut.endBlock(true); |
| 187 | mOut.closeFile(); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | void RSReflectionCpp::genTypeInstancesUsedInForEach() { |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 192 | for (auto I = mRSContext->export_foreach_begin(), |
| 193 | E = mRSContext->export_foreach_end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 194 | I != E; I++) { |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 195 | const RSExportForEach *EF = *I; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 196 | const RSExportType *OET = EF->getOutType(); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 197 | |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 198 | if (OET) { |
| 199 | genTypeInstanceFromPointer(OET); |
| 200 | } |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 201 | |
| 202 | const RSExportForEach::InTypeVec &InTypes = EF->getInTypes(); |
| 203 | |
| 204 | for (RSExportForEach::InTypeIter BI = InTypes.begin(), |
| 205 | EI = InTypes.end(); BI != EI; BI++) { |
| 206 | |
| 207 | genTypeInstanceFromPointer(*BI); |
| 208 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 209 | } |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 210 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 211 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 212 | // Ensure that the type of the reduce kernel is reflected. |
| 213 | void RSReflectionCpp::genTypeInstancesUsedInReduce() { |
| 214 | for (auto I = mRSContext->export_reduce_begin(), |
| 215 | E = mRSContext->export_reduce_end(); |
| 216 | I != E; ++I) { |
| 217 | genTypeInstance((*I)->getType()); |
| 218 | } |
| 219 | } |
| 220 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 221 | void RSReflectionCpp::genFieldsForAllocationTypeVerification() { |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 222 | bool CommentAdded = false; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 223 | for (std::set<std::string>::iterator I = mTypesToCheck.begin(), |
| 224 | E = mTypesToCheck.end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 225 | I != E; I++) { |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 226 | if (!CommentAdded) { |
| 227 | mOut.comment("The following elements are used to verify the types of " |
| 228 | "allocations passed to kernels."); |
| 229 | CommentAdded = true; |
| 230 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 231 | mOut.indent() << "android::RSC::sp<const android::RSC::Element> " |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 232 | << kRsElemPrefix << *I << ";\n"; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 233 | } |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 234 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 235 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 236 | void RSReflectionCpp::genFieldsToStoreExportVariableValues() { |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 237 | bool CommentAdded = false; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 238 | for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 239 | E = mRSContext->export_vars_end(); |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 240 | I != E; I++) { |
| 241 | const RSExportVar *ev = *I; |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 242 | if (ev->isConst()) { |
| 243 | continue; |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 244 | } |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 245 | if (!CommentAdded) { |
| 246 | mOut.comment("For each non-const variable exported by the script, we " |
| 247 | "have an equivalent field. This field contains the last " |
| 248 | "value this variable was set to using the set_ method. " |
| 249 | "This may not be current value of the variable in the " |
| 250 | "script, as the script is free to modify its internal " |
| 251 | "variable without changing this field. If the script " |
| 252 | "initializes the exported variable, the constructor will " |
| 253 | "initialize this field to the same value."); |
| 254 | CommentAdded = true; |
| 255 | } |
| 256 | mOut.indent() << GetTypeName(ev->getType()) << " " RS_EXPORT_VAR_PREFIX |
| 257 | << ev->getName() << ";\n"; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 258 | } |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 259 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 260 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 261 | void RSReflectionCpp::genForEachDeclarations() { |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 262 | bool CommentAdded = false; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 263 | for (RSContext::const_export_foreach_iterator |
| 264 | I = mRSContext->export_foreach_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 265 | E = mRSContext->export_foreach_end(); |
| 266 | I != E; I++) { |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 267 | const RSExportForEach *ForEach = *I; |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 268 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 269 | if (ForEach->isDummyRoot()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 270 | mOut.indent() << "// No forEach_root(...)\n"; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 271 | continue; |
| 272 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 273 | |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 274 | if (!CommentAdded) { |
| 275 | mOut.comment("For each kernel of the script corresponds one method. " |
| 276 | "That method queues the kernel for execution. The kernel " |
| 277 | "may not have completed nor even started by the time this " |
| 278 | "function returns. Calls that extract the data out of the " |
| 279 | "output allocation will wait for the kernels to complete."); |
| 280 | CommentAdded = true; |
| 281 | } |
| 282 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 283 | std::string FunctionStart = "void forEach_" + ForEach->getName() + "("; |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 284 | mOut.indent() << FunctionStart; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 285 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 286 | ArgumentList Arguments; |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 287 | const RSExportForEach::InVec &Ins = ForEach->getIns(); |
| 288 | for (RSExportForEach::InIter BI = Ins.begin(), EI = Ins.end(); |
| 289 | BI != EI; BI++) { |
| 290 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 291 | Arguments.push_back(Argument(kAllocationSp, (*BI)->getName())); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 292 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 293 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 294 | if (ForEach->hasOut() || ForEach->hasReturn()) { |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 295 | Arguments.push_back(Argument(kAllocationSp, "aout")); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 298 | const RSExportRecordType *ERT = ForEach->getParamPacketType(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 299 | if (ERT) { |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 300 | for (RSExportForEach::const_param_iterator i = ForEach->params_begin(), |
| 301 | e = ForEach->params_end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 302 | i != e; i++) { |
Jason Sams | 192392f | 2012-03-13 16:22:12 -0700 | [diff] [blame] | 303 | RSReflectionTypeData rtd; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 304 | (*i)->getType()->convertToRTD(&rtd); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 305 | Arguments.push_back(Argument(rtd.type->c_name, (*i)->getName())); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 306 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 307 | } |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 308 | genArguments(Arguments, FunctionStart.length()); |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 309 | mOut << ");\n"; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 310 | } |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 311 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 312 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 313 | void RSReflectionCpp::genReduceDeclarations() { |
| 314 | bool CommentAdded = false; |
| 315 | for (auto I = mRSContext->export_reduce_begin(), |
| 316 | E = mRSContext->export_reduce_end(); I != E; I++) { |
| 317 | if (!CommentAdded) { |
| 318 | mOut.comment("For each reduce kernel of the script, there is an entry " |
| 319 | "point to call the reduce kernel."); |
| 320 | CommentAdded = true; |
| 321 | } |
| 322 | |
| 323 | makeReduceSignatureAllocationVariant(false, *I); |
| 324 | makeReduceSignatureArrayVariant(false, *I); |
| 325 | } |
| 326 | } |
| 327 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 328 | void RSReflectionCpp::genExportFunctionDeclarations() { |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 329 | for (RSContext::const_export_func_iterator |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 330 | I = mRSContext->export_funcs_begin(), |
| 331 | E = mRSContext->export_funcs_end(); |
| 332 | I != E; I++) { |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 333 | const RSExportFunc *ef = *I; |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 334 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 335 | makeFunctionSignature(false, ef); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 336 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 339 | // forEach_* implementation |
| 340 | void RSReflectionCpp::genExportForEachBodies() { |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 341 | uint32_t slot = 0; |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 342 | for (auto I = mRSContext->export_foreach_begin(), |
| 343 | E = mRSContext->export_foreach_end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 344 | I != E; I++, slot++) { |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 345 | const RSExportForEach *ef = *I; |
| 346 | if (ef->isDummyRoot()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 347 | mOut.indent() << "// No forEach_root(...)\n"; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 351 | ArgumentList Arguments; |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 352 | std::string FunctionStart = |
| 353 | "void " + mClassName + "::forEach_" + ef->getName() + "("; |
| 354 | mOut.indent() << FunctionStart; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 355 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 356 | if (ef->hasIns()) { |
| 357 | // FIXME: Add support for kernels with multiple inputs. |
David Gross | 85dc696 | 2015-05-29 14:55:30 -0700 | [diff] [blame] | 358 | slangAssert(ef->getIns().size() == 1); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 359 | Arguments.push_back(Argument(kAllocationSp, "ain")); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 360 | } |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 361 | |
| 362 | if (ef->hasOut() || ef->hasReturn()) { |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 363 | Arguments.push_back(Argument(kAllocationSp, "aout")); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | const RSExportRecordType *ERT = ef->getParamPacketType(); |
| 367 | if (ERT) { |
| 368 | for (RSExportForEach::const_param_iterator i = ef->params_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 369 | e = ef->params_end(); |
| 370 | i != e; i++) { |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 371 | RSReflectionTypeData rtd; |
| 372 | (*i)->getType()->convertToRTD(&rtd); |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 373 | Arguments.push_back(Argument(rtd.type->c_name, (*i)->getName())); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 376 | genArguments(Arguments, FunctionStart.length()); |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 377 | mOut << ")"; |
| 378 | mOut.startBlock(); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 379 | |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 380 | const RSExportType *OET = ef->getOutType(); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 381 | const RSExportForEach::InTypeVec &InTypes = ef->getInTypes(); |
| 382 | if (ef->hasIns()) { |
| 383 | // FIXME: Add support for kernels with multiple inputs. |
David Gross | 85dc696 | 2015-05-29 14:55:30 -0700 | [diff] [blame] | 384 | slangAssert(ef->getIns().size() == 1); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 385 | genTypeCheck(InTypes[0], "ain"); |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 386 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 387 | if (OET) { |
| 388 | genTypeCheck(OET, "aout"); |
| 389 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 390 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 391 | // TODO Add the appropriate dimension checking code, as seen in |
| 392 | // slang_rs_reflection.cpp. |
| 393 | |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 394 | std::string FieldPackerName = ef->getName() + "_fp"; |
| 395 | if (ERT) { |
| 396 | if (genCreateFieldPacker(ERT, FieldPackerName.c_str())) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 397 | genPackVarOfType(ERT, nullptr, FieldPackerName.c_str()); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 398 | } |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 399 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 400 | mOut.indent() << "forEach(" << slot << ", "; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 401 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 402 | if (ef->hasIns()) { |
| 403 | // FIXME: Add support for kernels with multiple inputs. |
David Gross | 85dc696 | 2015-05-29 14:55:30 -0700 | [diff] [blame] | 404 | slangAssert(ef->getIns().size() == 1); |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 405 | mOut << "ain, "; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 406 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 407 | mOut << "NULL, "; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (ef->hasOut() || ef->hasReturn()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 411 | mOut << "aout, "; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 412 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 413 | mOut << "NULL, "; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 416 | // FIXME (no support for usrData with C++ kernels) |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 417 | mOut << "NULL, 0);\n"; |
| 418 | mOut.endBlock(); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 419 | } |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 420 | } |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 421 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 422 | // reduce_* implementation |
| 423 | void RSReflectionCpp::genExportReduceBodies() { |
| 424 | for (auto I = mRSContext->export_reduce_begin(), |
| 425 | E = mRSContext->export_reduce_end(); |
| 426 | I != E; ++I) { |
| 427 | const RSExportReduce &Reduce = **I; |
| 428 | const RSExportType *Type = Reduce.getType(); |
| 429 | |
| 430 | // Allocation variant |
| 431 | // |
| 432 | // void reduce_foo(sp<Allocation> ain, sp<Allocation> aout, |
| 433 | // const RsScriptCall *sc); |
| 434 | makeReduceSignatureAllocationVariant(true, &Reduce); |
| 435 | mOut.startBlock(); |
| 436 | |
| 437 | // Type check |
| 438 | genTypeCheck(Type, "ain"); |
| 439 | genTypeCheck(Type, "aout"); |
| 440 | |
| 441 | // Dimension check |
| 442 | gen1DCheck("ain"); |
| 443 | |
| 444 | const uint32_t Slot = getNextExportReduceSlot(); |
| 445 | |
| 446 | // Call into RenderScript. |
| 447 | mOut.indent() << "reduce(" << Slot << ", " |
| 448 | << "ain, aout, sc);\n"; |
| 449 | mOut.endBlock(); |
| 450 | |
| 451 | if (!canExportReduceArrayVariant(Type)) { |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | // Array variant |
| 456 | // |
| 457 | // Ty reduce_foo(const ElemTy[] in, uint32_t x1, uint32_t x2, uint32_t inLen); |
| 458 | // "Ty" could be different from "ElemTy" in the case of vectors. |
| 459 | makeReduceSignatureArrayVariant(true, &Reduce); |
| 460 | mOut.startBlock(); |
| 461 | |
| 462 | const std::string ReturnType = GetTypeName(Type); |
| 463 | const std::string DefaultReturnValue = ReturnType + "()"; |
| 464 | |
| 465 | genNullOrEmptyArrayCheck("in", "inLen", DefaultReturnValue); |
| 466 | |
| 467 | RSReflectionTypeData TypeData; |
| 468 | Type->convertToRTD(&TypeData); |
| 469 | const uint32_t VecSize = TypeData.vecSize; |
| 470 | std::string InLength = "inLen"; |
| 471 | // Adjust the length so that it corresponds to the number of elements in the allocation. |
| 472 | if (VecSize > 1) { |
| 473 | InLength += " / " + std::to_string(VecSize); |
| 474 | } |
| 475 | genVectorLengthCompatibilityCheck("inLen", VecSize, DefaultReturnValue); |
| 476 | |
| 477 | mOut.indent() << "if (x1 >= x2 || x2 > " << InLength << ")"; |
| 478 | mOut.startBlock(); |
| 479 | mOut.indent() << "mRS->throwError(RS_ERROR_RUNTIME_ERROR, " |
| 480 | << "\"Input bounds are invalid\");\n"; |
| 481 | mOut.indent() << "return " << DefaultReturnValue << ";\n"; |
| 482 | mOut.endBlock(); |
| 483 | |
| 484 | mOut.indent() << kAllocationSp |
| 485 | << " ain = android::RSC::Allocation::createSized(mRS, " |
| 486 | << kRsElemPrefix << Type->getElementName() << ", " |
| 487 | << "x2 - x1);\n"; |
| 488 | |
| 489 | mOut.indent() << "ain->setAutoPadding(true);\n"; |
| 490 | |
| 491 | mOut.indent() << kAllocationSp |
| 492 | << " aout = android::RSC::Allocation::createSized(mRS, " |
| 493 | << kRsElemPrefix << Type->getElementName() << ", 1);\n"; |
| 494 | |
| 495 | mOut.indent() << "aout->setAutoPadding(true);\n"; |
| 496 | |
| 497 | const std::string ArrayElementType = TypeData.type->c_name; |
| 498 | |
| 499 | std::string StartOffset = "x1"; |
| 500 | if (VecSize > 1) { |
| 501 | StartOffset += " * " + std::to_string(VecSize); |
| 502 | } |
| 503 | mOut.indent() << "ain->copy1DRangeFrom(0, x2 - x1, &in[" << StartOffset << "]);\n"; |
| 504 | mOut.indent() << "reduce_" << Reduce.getName() << "(ain, aout);\n"; |
| 505 | mOut.indent() << ArrayElementType << " outArray[" << VecSize << "];\n"; |
| 506 | |
| 507 | mOut.indent() << "aout->copy1DRangeTo(0, 1, &outArray[0]);\n"; |
| 508 | |
| 509 | mOut.indent() << "return " << ReturnType << "("; |
| 510 | for (uint32_t VecElem = 0; VecElem < VecSize; ++VecElem) { |
| 511 | if (VecElem > 0) mOut << ", "; |
| 512 | mOut << "outArray[" << VecElem << "]"; |
| 513 | } |
| 514 | mOut << ");\n"; |
| 515 | mOut.endBlock(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // invoke_* implementation |
| 520 | void RSReflectionCpp::genExportFunctionBodies() { |
| 521 | uint32_t slot = 0; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 522 | // Reflect export function |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 523 | for (auto I = mRSContext->export_funcs_begin(), |
| 524 | E = mRSContext->export_funcs_end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 525 | I != E; I++) { |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 526 | const RSExportFunc *ef = *I; |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 527 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 528 | makeFunctionSignature(true, ef); |
| 529 | mOut.startBlock(); |
Stephen Hines | 713377e | 2012-11-14 19:32:43 -0800 | [diff] [blame] | 530 | const RSExportRecordType *params = ef->getParamPacketType(); |
| 531 | size_t param_len = 0; |
| 532 | if (params) { |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 533 | param_len = params->getAllocSize(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 534 | if (genCreateFieldPacker(params, "__fp")) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 535 | genPackVarOfType(params, nullptr, "__fp"); |
Stephen Hines | 713377e | 2012-11-14 19:32:43 -0800 | [diff] [blame] | 536 | } |
Stephen Hines | 713377e | 2012-11-14 19:32:43 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 539 | mOut.indent() << "invoke(" << slot; |
Stephen Hines | 713377e | 2012-11-14 19:32:43 -0800 | [diff] [blame] | 540 | if (params) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 541 | mOut << ", __fp.getData(), " << param_len << ");\n"; |
Stephen Hines | 713377e | 2012-11-14 19:32:43 -0800 | [diff] [blame] | 542 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 543 | mOut << ", NULL, 0);\n"; |
Stephen Hines | 713377e | 2012-11-14 19:32:43 -0800 | [diff] [blame] | 544 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 545 | mOut.endBlock(); |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 546 | |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 547 | slot++; |
| 548 | } |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | bool RSReflectionCpp::genEncodedBitCode() { |
| 552 | FILE *pfin = fopen(mBitCodeFilePath.c_str(), "rb"); |
| 553 | if (pfin == nullptr) { |
| 554 | fprintf(stderr, "Error: could not read file %s\n", |
| 555 | mBitCodeFilePath.c_str()); |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | unsigned char buf[16]; |
| 560 | int read_length; |
| 561 | mOut.indent() << "static const unsigned char __txt[] ="; |
| 562 | mOut.startBlock(); |
| 563 | while ((read_length = fread(buf, 1, sizeof(buf), pfin)) > 0) { |
| 564 | mOut.indent(); |
| 565 | for (int i = 0; i < read_length; i++) { |
| 566 | char buf2[16]; |
| 567 | snprintf(buf2, sizeof(buf2), "0x%02x,", buf[i]); |
| 568 | mOut << buf2; |
| 569 | } |
| 570 | mOut << "\n"; |
| 571 | } |
| 572 | mOut.endBlock(true); |
| 573 | mOut << "\n"; |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | bool RSReflectionCpp::writeImplementationFile() { |
| 578 | if (!mOut.startFile(mOutputDirectory, mClassName + ".cpp", mRSSourceFilePath, |
| 579 | mRSContext->getLicenseNote(), false, |
| 580 | mRSContext->getVerbose())) { |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | // Front matter |
| 585 | mOut.indent() << "#include \"" << mClassName << ".h\"\n\n"; |
| 586 | |
| 587 | genEncodedBitCode(); |
| 588 | mOut.indent() << "\n\n"; |
| 589 | |
| 590 | // Constructor |
| 591 | const std::string &packageName = mRSContext->getReflectJavaPackageName(); |
| 592 | mOut.indent() << mClassName << "::" << mClassName |
| 593 | << "(android::RSC::sp<android::RSC::RS> rs):\n" |
| 594 | " ScriptC(rs, __txt, sizeof(__txt), \"" |
| 595 | << mCleanedRSFileName << "\", " << mCleanedRSFileName.length() |
| 596 | << ", \"/data/data/" << packageName << "/app\", sizeof(\"" |
| 597 | << packageName << "\"))"; |
| 598 | mOut.startBlock(); |
| 599 | for (std::set<std::string>::iterator I = mTypesToCheck.begin(), |
| 600 | E = mTypesToCheck.end(); |
| 601 | I != E; I++) { |
| 602 | mOut.indent() << kRsElemPrefix << *I << " = android::RSC::Element::" << *I |
| 603 | << "(mRS);\n"; |
| 604 | } |
| 605 | |
| 606 | for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), |
| 607 | E = mRSContext->export_vars_end(); |
| 608 | I != E; I++) { |
| 609 | const RSExportVar *EV = *I; |
| 610 | if (!EV->getInit().isUninit()) { |
| 611 | genInitExportVariable(EV->getType(), EV->getName(), EV->getInit()); |
| 612 | } else { |
| 613 | genZeroInitExportVariable(EV->getName()); |
| 614 | } |
| 615 | } |
| 616 | mOut.endBlock(); |
| 617 | |
| 618 | // Destructor |
| 619 | mOut.indent() << mClassName << "::~" << mClassName << "()"; |
| 620 | mOut.startBlock(); |
| 621 | mOut.endBlock(); |
| 622 | |
| 623 | // Function bodies |
| 624 | genExportForEachBodies(); |
| 625 | genExportReduceBodies(); |
| 626 | genExportFunctionBodies(); |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 627 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 628 | mOut.closeFile(); |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 629 | return true; |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 632 | void RSReflectionCpp::genExportVariablesGetterAndSetter() { |
Jean-Luc Brouillet | c83b790 | 2014-07-25 15:50:32 -0700 | [diff] [blame] | 633 | mOut.comment("Methods to set and get the variables exported by the script. " |
| 634 | "Const variables will not have a setter.\n\n" |
| 635 | "Note that the value returned by the getter may not be the " |
| 636 | "current value of the variable in the script. The getter will " |
| 637 | "return the initial value of the variable (as defined in the " |
| 638 | "script) or the the last value set by using the setter method. " |
| 639 | "The script is free to modify its value independently."); |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 640 | for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), |
| 641 | E = mRSContext->export_vars_end(); |
| 642 | I != E; I++) { |
| 643 | const RSExportVar *EV = *I; |
| 644 | const RSExportType *ET = EV->getType(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 645 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 646 | switch (ET->getClass()) { |
| 647 | case RSExportType::ExportClassPrimitive: { |
| 648 | genGetterAndSetter(static_cast<const RSExportPrimitiveType *>(ET), EV); |
| 649 | break; |
| 650 | } |
| 651 | case RSExportType::ExportClassPointer: { |
| 652 | // TODO Deprecate this. |
| 653 | genPointerTypeExportVariable(EV); |
| 654 | break; |
| 655 | } |
| 656 | case RSExportType::ExportClassVector: { |
| 657 | genGetterAndSetter(static_cast<const RSExportVectorType *>(ET), EV); |
| 658 | break; |
| 659 | } |
| 660 | case RSExportType::ExportClassMatrix: { |
| 661 | genMatrixTypeExportVariable(EV); |
| 662 | break; |
| 663 | } |
| 664 | case RSExportType::ExportClassConstantArray: { |
| 665 | genGetterAndSetter(static_cast<const RSExportConstantArrayType *>(ET), |
| 666 | EV); |
| 667 | break; |
| 668 | } |
| 669 | case RSExportType::ExportClassRecord: { |
| 670 | genGetterAndSetter(static_cast<const RSExportRecordType *>(ET), EV); |
| 671 | break; |
| 672 | } |
| 673 | default: { slangAssert(false && "Unknown class of type"); } |
| 674 | } |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 678 | void RSReflectionCpp::genGetterAndSetter(const RSExportPrimitiveType *EPT, |
| 679 | const RSExportVar *EV) { |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 680 | RSReflectionTypeData rtd; |
Stephen Hines | d6f36b1 | 2013-11-20 22:03:35 -0800 | [diff] [blame] | 681 | EPT->convertToRTD(&rtd); |
| 682 | std::string TypeName = GetTypeName(EPT, false); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 683 | |
| 684 | if (!EV->isConst()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 685 | mOut.indent() << "void set_" << EV->getName() << "(" << TypeName << " v)"; |
| 686 | mOut.startBlock(); |
| 687 | mOut.indent() << "setVar(" << getNextExportVarSlot() << ", "; |
Stephen Hines | d6f36b1 | 2013-11-20 22:03:35 -0800 | [diff] [blame] | 688 | if (EPT->isRSObjectType()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 689 | mOut << "v"; |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 690 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 691 | mOut << "&v, sizeof(v)"; |
Stephen Hines | d6f36b1 | 2013-11-20 22:03:35 -0800 | [diff] [blame] | 692 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 693 | mOut << ");\n"; |
| 694 | mOut.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;\n"; |
| 695 | mOut.endBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 696 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 697 | mOut.indent() << TypeName << " get_" << EV->getName() << "() const"; |
| 698 | mOut.startBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 699 | if (EV->isConst()) { |
| 700 | const clang::APValue &val = EV->getInit(); |
Stephen Hines | d6f36b1 | 2013-11-20 22:03:35 -0800 | [diff] [blame] | 701 | bool isBool = !strcmp(TypeName.c_str(), "bool"); |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 702 | mOut.indent() << "return "; |
| 703 | genInitValue(val, isBool); |
| 704 | mOut << ";\n"; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 705 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 706 | mOut.indent() << "return " << RS_EXPORT_VAR_PREFIX << EV->getName() |
| 707 | << ";\n"; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 708 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 709 | mOut.endBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | void RSReflectionCpp::genPointerTypeExportVariable(const RSExportVar *EV) { |
| 713 | const RSExportType *ET = EV->getType(); |
| 714 | |
| 715 | slangAssert((ET->getClass() == RSExportType::ExportClassPointer) && |
| 716 | "Variable should be type of pointer here"); |
| 717 | |
| 718 | std::string TypeName = GetTypeName(ET); |
| 719 | std::string VarName = EV->getName(); |
| 720 | |
| 721 | RSReflectionTypeData rtd; |
| 722 | EV->getType()->convertToRTD(&rtd); |
| 723 | uint32_t slot = getNextExportVarSlot(); |
| 724 | |
| 725 | if (!EV->isConst()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 726 | mOut.indent() << "void bind_" << VarName << "(" << TypeName << " v)"; |
| 727 | mOut.startBlock(); |
| 728 | mOut.indent() << "bindAllocation(v, " << slot << ");\n"; |
| 729 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;\n"; |
| 730 | mOut.endBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 731 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 732 | mOut.indent() << TypeName << " get_" << VarName << "() const"; |
| 733 | mOut.startBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 734 | if (EV->isConst()) { |
| 735 | const clang::APValue &val = EV->getInit(); |
| 736 | bool isBool = !strcmp(TypeName.c_str(), "bool"); |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 737 | mOut.indent() << "return "; |
| 738 | genInitValue(val, isBool); |
| 739 | mOut << ";\n"; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 740 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 741 | mOut.indent() << "return " << RS_EXPORT_VAR_PREFIX << VarName << ";\n"; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 742 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 743 | mOut.endBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 746 | void RSReflectionCpp::genGetterAndSetter(const RSExportVectorType *EVT, |
| 747 | const RSExportVar *EV) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 748 | slangAssert(EVT != nullptr); |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 749 | |
| 750 | RSReflectionTypeData rtd; |
| 751 | EVT->convertToRTD(&rtd); |
| 752 | |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 753 | if (!EV->isConst()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 754 | mOut.indent() << "void set_" << EV->getName() << "(" |
| 755 | << rtd.type->rs_c_vector_prefix << EVT->getNumElement() |
| 756 | << " v)"; |
| 757 | mOut.startBlock(); |
| 758 | mOut.indent() << "setVar(" << getNextExportVarSlot() |
| 759 | << ", &v, sizeof(v));\n"; |
| 760 | mOut.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;\n"; |
| 761 | mOut.endBlock(); |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 762 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 763 | mOut.indent() << rtd.type->rs_c_vector_prefix << EVT->getNumElement() |
| 764 | << " get_" << EV->getName() << "() const"; |
| 765 | mOut.startBlock(); |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 766 | if (EV->isConst()) { |
| 767 | const clang::APValue &val = EV->getInit(); |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 768 | mOut.indent() << "return "; |
| 769 | genInitValue(val, false); |
| 770 | mOut << ";\n"; |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 771 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 772 | mOut.indent() << "return " << RS_EXPORT_VAR_PREFIX << EV->getName() |
| 773 | << ";\n"; |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 774 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 775 | mOut.endBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | void RSReflectionCpp::genMatrixTypeExportVariable(const RSExportVar *EV) { |
| 779 | slangAssert(false); |
| 780 | } |
| 781 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 782 | void RSReflectionCpp::genGetterAndSetter(const RSExportConstantArrayType *AT, |
| 783 | const RSExportVar *EV) { |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 784 | slangAssert(false); |
| 785 | } |
| 786 | |
Jean-Luc Brouillet | 1cea271 | 2014-06-05 13:54:11 -0700 | [diff] [blame] | 787 | void RSReflectionCpp::genGetterAndSetter(const RSExportRecordType *ERT, |
| 788 | const RSExportVar *EV) { |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 789 | slangAssert(false); |
| 790 | } |
| 791 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 792 | void RSReflectionCpp::makeFunctionSignature(bool isDefinition, |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 793 | const RSExportFunc *ef) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 794 | mOut.indent() << "void "; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 795 | if (isDefinition) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 796 | mOut << mClassName << "::"; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 797 | } |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 798 | mOut << "invoke_" << ef->getName() << "("; |
Stephen Hines | 38dceea | 2012-11-13 17:33:55 -0800 | [diff] [blame] | 799 | |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 800 | if (ef->getParamPacketType()) { |
| 801 | bool FirstArg = true; |
| 802 | for (RSExportFunc::const_param_iterator i = ef->params_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 803 | e = ef->params_end(); |
| 804 | i != e; i++) { |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 805 | if (!FirstArg) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 806 | mOut << ", "; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 807 | } else { |
| 808 | FirstArg = false; |
| 809 | } |
Stephen Hines | 19879e8 | 2015-06-30 16:11:34 -0700 | [diff] [blame] | 810 | mOut << GetTypeName((*i)->getType(), false) << " " << (*i)->getName(); |
Stephen Hines | 38dceea | 2012-11-13 17:33:55 -0800 | [diff] [blame] | 811 | } |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 812 | } |
Stephen Hines | 38dceea | 2012-11-13 17:33:55 -0800 | [diff] [blame] | 813 | |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 814 | if (isDefinition) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 815 | mOut << ")"; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 816 | } else { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 817 | mOut << ");\n"; |
Stephen Hines | 02a9826 | 2012-11-14 12:40:26 -0800 | [diff] [blame] | 818 | } |
Stephen Hines | 38dceea | 2012-11-13 17:33:55 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 821 | void RSReflectionCpp::makeReduceSignatureAllocationVariant(bool IsDefinition, |
| 822 | const RSExportReduce *ER) { |
| 823 | // void reduce_foo(sp<Allocation> ain, sp<Allocation> aout, |
| 824 | // const RsScriptCall *sc = nullptr); |
| 825 | std::string FunctionStart = "void "; |
| 826 | if (IsDefinition) { |
| 827 | FunctionStart += mClassName + "::"; |
| 828 | } |
| 829 | FunctionStart += "reduce_" + ER->getName() + "("; |
| 830 | |
| 831 | ArgumentList Arguments{ |
| 832 | Argument(kAllocationSp, "ain"), |
| 833 | Argument(kAllocationSp, "aout"), |
| 834 | Argument(kConstRsScriptCall, "*sc", IsDefinition ? "" : "nullptr") |
| 835 | }; |
| 836 | |
| 837 | mOut.indent() << FunctionStart; |
| 838 | |
| 839 | genArguments(Arguments, FunctionStart.length()); |
| 840 | |
| 841 | if (IsDefinition) { |
| 842 | mOut << ")"; |
| 843 | } else { |
| 844 | mOut << ");\n\n"; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | void RSReflectionCpp::makeReduceSignatureArrayVariant(bool IsDefinition, |
| 849 | const RSExportReduce *ER) { |
| 850 | // Ty reduce_foo(const ElemTy[] in, uint32_t x1, uint32_t x2, size_t inLen); |
| 851 | // "Ty" could be different from "ElemTy" in the case of vectors. |
| 852 | |
| 853 | const RSExportType *Type = ER->getType(); |
| 854 | if (!canExportReduceArrayVariant(Type)) { |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | RSReflectionTypeData TypeData; |
| 859 | Type->convertToRTD(&TypeData); |
| 860 | |
| 861 | const std::string ReturnType = GetTypeName(Type); |
| 862 | std::string FunctionStart = ReturnType + " "; |
| 863 | if (IsDefinition) { |
| 864 | FunctionStart += mClassName + "::"; |
| 865 | } |
| 866 | FunctionStart += "reduce_" + ER->getName() + "("; |
| 867 | |
| 868 | const std::string ArrayElementType = TypeData.type->c_name; |
| 869 | |
| 870 | ArgumentList Arguments{ |
| 871 | Argument("const " + ArrayElementType, "in[]"), |
| 872 | Argument("uint32_t", "x1"), |
| 873 | Argument("uint32_t", "x2"), |
| 874 | Argument("size_t", "inLen") |
| 875 | }; |
| 876 | |
| 877 | mOut.indent() << FunctionStart; |
| 878 | genArguments(Arguments, FunctionStart.size()); |
| 879 | |
| 880 | if (IsDefinition) { |
| 881 | mOut << ")"; |
| 882 | } else { |
| 883 | mOut << ");\n\n"; |
| 884 | } |
| 885 | |
| 886 | if (!IsDefinition) { |
| 887 | // We reflect three more variants in the header. First, there is |
| 888 | // |
| 889 | // Ty reduce_foo(const ElemTy[] in, size_t inLen); |
| 890 | // |
| 891 | // Note the inLen is the number of primitive elements in the array, as opposed to the |
| 892 | // bounds whose units are allocation elements. The other variants use templates to infer |
| 893 | // the array length statically: |
| 894 | // |
| 895 | // template<size_t inLen> Ty reduce_foo(const ElemTy (&in)[inLen]); |
| 896 | // template<size_t inLen> Ty reduce_foo(const ElemTy (&in)[inLen], uint32_t x1, uint32_t x2); |
| 897 | |
| 898 | // Generate inLen variant |
| 899 | const uint32_t VecSize = TypeData.vecSize; |
| 900 | std::string X2 = "inLen"; |
| 901 | |
| 902 | const std::string FunctionName = ER->getName(); |
| 903 | |
| 904 | auto ForwardReduce = [this, &FunctionName](const std::string &x1, |
| 905 | const std::string &x2, |
| 906 | const std::string &inLen) { |
| 907 | this->mOut.indent() << " return reduce_" << FunctionName << "(in, " |
| 908 | << x1 << ", " << x2 << ", " << inLen << ");\n"; |
| 909 | this->mOut.indent() << "}\n\n"; |
| 910 | }; |
| 911 | |
| 912 | const std::string DefaultValue = ReturnType + "()"; |
| 913 | |
| 914 | ArgumentList InLenVariantArguments{ |
| 915 | Argument("const " + ArrayElementType, "in[]"), Argument("size_t", "inLen") |
| 916 | }; |
| 917 | mOut.indent() << FunctionStart; |
| 918 | genArguments(InLenVariantArguments, FunctionStart.size()); |
| 919 | mOut << ") {\n"; |
| 920 | if (VecSize > 1) { |
| 921 | genVectorLengthCompatibilityCheck("inLen", VecSize, DefaultValue, 2); |
| 922 | X2 += " / " + std::to_string(VecSize); |
| 923 | } |
| 924 | ForwardReduce("0", X2, "inLen"); |
| 925 | |
| 926 | // Generate template variants |
| 927 | ArgumentList TemplateVariantArguments{ |
| 928 | Argument("const " + ArrayElementType, "(&in)[inLen]") |
| 929 | }; |
| 930 | |
| 931 | mOut.indent() << "template<size_t inLen>\n"; |
| 932 | mOut.indent() << FunctionStart; |
| 933 | genArguments(TemplateVariantArguments, FunctionStart.size()); |
| 934 | mOut << ") {\n return reduce_" << FunctionName << "(in, inLen);\n }\n\n"; |
| 935 | |
| 936 | TemplateVariantArguments.push_back(Argument("uint32_t", "x1")); |
| 937 | TemplateVariantArguments.push_back(Argument("uint32_t", "x2")); |
| 938 | mOut.indent() << "template<size_t inLen>\n"; |
| 939 | mOut.indent() << FunctionStart; |
| 940 | genArguments(TemplateVariantArguments, FunctionStart.size()); |
| 941 | mOut << ") {\n"; |
| 942 | ForwardReduce("x1", "x2", "inLen"); |
| 943 | } |
| 944 | } |
| 945 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 946 | void RSReflectionCpp::genArguments(const ArgumentList &Arguments, int Offset) { |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 947 | bool FirstArg = true; |
| 948 | |
Jean-Luc Brouillet | eb8b99e | 2014-06-03 20:59:01 -0700 | [diff] [blame] | 949 | for (ArgumentList::const_iterator I = Arguments.begin(), E = Arguments.end(); |
| 950 | I != E; I++) { |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 951 | if (!FirstArg) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 952 | mOut << ",\n"; |
| 953 | mOut.indent() << string(Offset, ' '); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 954 | } else { |
| 955 | FirstArg = false; |
| 956 | } |
| 957 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 958 | mOut << I->Type << " " << I->Name; |
| 959 | if (!I->DefaultValue.empty()) { |
| 960 | mOut << " = " << I->DefaultValue; |
| 961 | } |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
| 965 | bool RSReflectionCpp::genCreateFieldPacker(const RSExportType *ET, |
| 966 | const char *FieldPackerName) { |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 967 | size_t AllocSize = ET->getAllocSize(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 968 | |
| 969 | if (AllocSize > 0) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 970 | mOut.indent() << "android::RSC::FieldPacker " << FieldPackerName << "(" |
| 971 | << AllocSize << ");\n"; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 972 | return true; |
| 973 | } |
| 974 | |
| 975 | return false; |
| 976 | } |
| 977 | |
| 978 | void RSReflectionCpp::genPackVarOfType(const RSExportType *ET, |
| 979 | const char *VarName, |
| 980 | const char *FieldPackerName) { |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 981 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 982 | case RSExportType::ExportClassPrimitive: |
| 983 | case RSExportType::ExportClassVector: |
| 984 | case RSExportType::ExportClassPointer: |
| 985 | case RSExportType::ExportClassMatrix: { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 986 | mOut.indent() << FieldPackerName << ".add(" << VarName << ");\n"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 987 | break; |
| 988 | } |
| 989 | case RSExportType::ExportClassConstantArray: { |
| 990 | /*const RSExportConstantArrayType *ECAT = |
| 991 | static_cast<const RSExportConstantArrayType *>(ET); |
| 992 | |
| 993 | // TODO(zonr): more elegant way. Currently, we obtain the unique index |
| 994 | // variable (this method involves recursive call which means |
| 995 | // we may have more than one level loop, therefore we can't |
| 996 | // always use the same index variable name here) name given |
| 997 | // in the for-loop from counting the '.' in @VarName. |
| 998 | unsigned Level = 0; |
| 999 | size_t LastDotPos = 0; |
| 1000 | std::string ElementVarName(VarName); |
| 1001 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1002 | while (LastDotPos != std::string::npos) { |
| 1003 | LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1); |
| 1004 | Level++; |
| 1005 | } |
| 1006 | std::string IndexVarName("ct"); |
| 1007 | IndexVarName.append(llvm::utostr_32(Level)); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1008 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1009 | C.indent() << "for (int " << IndexVarName << " = 0; " << |
| 1010 | IndexVarName << " < " << ECAT->getSize() << "; " << |
| 1011 | IndexVarName << "++)"; |
| 1012 | C.startBlock(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1013 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1014 | ElementVarName.append("[" + IndexVarName + "]"); |
| 1015 | genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(), |
| 1016 | FieldPackerName); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1017 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1018 | C.endBlock();*/ |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | case RSExportType::ExportClassRecord: { |
| 1022 | const RSExportRecordType *ERT = static_cast<const RSExportRecordType *>(ET); |
| 1023 | // Relative pos from now on in field packer |
| 1024 | unsigned Pos = 0; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1025 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1026 | for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), |
| 1027 | E = ERT->fields_end(); |
| 1028 | I != E; I++) { |
| 1029 | const RSExportRecordType::Field *F = *I; |
| 1030 | std::string FieldName; |
| 1031 | size_t FieldOffset = F->getOffsetInParent(); |
| 1032 | const RSExportType *T = F->getType(); |
| 1033 | size_t FieldStoreSize = T->getStoreSize(); |
| 1034 | size_t FieldAllocSize = T->getAllocSize(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1035 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1036 | if (VarName != nullptr) |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1037 | FieldName = VarName + ("." + F->getName()); |
| 1038 | else |
| 1039 | FieldName = F->getName(); |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1040 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1041 | if (FieldOffset > Pos) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1042 | mOut.indent() << FieldPackerName << ".skip(" << (FieldOffset - Pos) |
| 1043 | << ");\n"; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1044 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1045 | |
| 1046 | genPackVarOfType(F->getType(), FieldName.c_str(), FieldPackerName); |
| 1047 | |
| 1048 | // There is padding in the field type |
| 1049 | if (FieldAllocSize > FieldStoreSize) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1050 | mOut.indent() << FieldPackerName << ".skip(" |
| 1051 | << (FieldAllocSize - FieldStoreSize) << ");\n"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | Pos = FieldOffset + FieldAllocSize; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1055 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1056 | |
| 1057 | // There maybe some padding after the struct |
| 1058 | if (ERT->getAllocSize() > Pos) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1059 | mOut.indent() << FieldPackerName << ".skip(" << ERT->getAllocSize() - Pos |
| 1060 | << ");\n"; |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1061 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1062 | break; |
| 1063 | } |
| 1064 | default: { slangAssert(false && "Unknown class of type"); } |
Stephen Hines | 7dd6da2 | 2012-11-15 19:56:03 -0800 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1068 | void RSReflectionCpp::genTypeCheck(const RSExportType *ET, |
| 1069 | const char *VarName) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1070 | mOut.indent() << "// Type check for " << VarName << "\n"; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1071 | |
| 1072 | if (ET->getClass() == RSExportType::ExportClassPointer) { |
| 1073 | const RSExportPointerType *EPT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1074 | static_cast<const RSExportPointerType *>(ET); |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1075 | ET = EPT->getPointeeType(); |
| 1076 | } |
| 1077 | |
| 1078 | std::string TypeName; |
| 1079 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1080 | case RSExportType::ExportClassPrimitive: |
| 1081 | case RSExportType::ExportClassVector: |
| 1082 | case RSExportType::ExportClassRecord: { |
| 1083 | TypeName = ET->getElementName(); |
| 1084 | break; |
| 1085 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1086 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1087 | default: |
| 1088 | break; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | if (!TypeName.empty()) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1092 | mOut.indent() << "if (!" << VarName |
| 1093 | << "->getType()->getElement()->isCompatible(" |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 1094 | << kRsElemPrefix << TypeName << "))"; |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1095 | mOut.startBlock(); |
| 1096 | mOut.indent() << "mRS->throwError(RS_ERROR_RUNTIME_ERROR, " |
| 1097 | "\"Incompatible type\");\n"; |
| 1098 | mOut.indent() << "return;\n"; |
| 1099 | mOut.endBlock(); |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Matt Wala | 1c6b927 | 2015-08-03 14:06:07 -0700 | [diff] [blame] | 1103 | // Ensure that the input is 1 dimensional. |
| 1104 | void RSReflectionCpp::gen1DCheck(const std::string &VarName) { |
| 1105 | mOut.indent() << "// check that " << VarName << " is 1d\n"; |
| 1106 | mOut.indent() << "sp<const Type> t0 = " << VarName << "->getType();\n"; |
| 1107 | mOut.indent() << "if (t0->getY() != 0 ||\n"; |
| 1108 | mOut.indent() << " t0->hasFaces() ||\n"; |
| 1109 | mOut.indent() << " t0->hasMipmaps())"; |
| 1110 | mOut.startBlock(); |
| 1111 | mOut.indent() << "mRS->throwError(RS_ERROR_INVALID_PARAMETER, " |
| 1112 | << "\"" << VarName << " is not 1D!\");\n"; |
| 1113 | mOut.indent() << "return;\n"; |
| 1114 | mOut.endBlock(); |
| 1115 | } |
| 1116 | |
| 1117 | // Generates code to ensure that the supplied array length is a multiple of the vector size. |
| 1118 | void RSReflectionCpp::genVectorLengthCompatibilityCheck(const std::string &Length, |
| 1119 | unsigned VecSize, |
| 1120 | const std::string &ValueToReturn, |
| 1121 | unsigned IndentLevels) { |
| 1122 | auto Indenter = [this, IndentLevels]() -> std::ofstream& { |
| 1123 | GeneratedFile &Out = this->mOut; |
| 1124 | for (unsigned Level = 0; Level < IndentLevels; ++Level) { |
| 1125 | Out.indent(); |
| 1126 | } |
| 1127 | return Out; |
| 1128 | }; |
| 1129 | |
| 1130 | Indenter() << "// Verify that the array length is a multiple of the vector size.\n"; |
| 1131 | Indenter() << "if (" << Length << " % " << std::to_string(VecSize) << " != 0) {\n"; |
| 1132 | Indenter() << " mRS->throwError(RS_ERROR_INVALID_PARAMETER, " |
| 1133 | << "\"Input array length is not a multiple of " |
| 1134 | << std::to_string(VecSize) << "\");\n"; |
| 1135 | Indenter() << " return " << ValueToReturn << ";\n"; |
| 1136 | Indenter() << "}\n\n"; |
| 1137 | } |
| 1138 | |
| 1139 | // Generates code to ensure that the supplied array is non-null and nonzero in length. |
| 1140 | void RSReflectionCpp::genNullOrEmptyArrayCheck(const std::string &ArrayName, |
| 1141 | const std::string &Length, |
| 1142 | const std::string &ValueToReturn) { |
| 1143 | mOut.indent() << "// Verify that the array is non-null and non-empty.\n"; |
| 1144 | mOut.indent() << "if (" << ArrayName << " == nullptr) {\n"; |
| 1145 | mOut.indent() << " mRS->throwError(RS_ERROR_INVALID_PARAMETER, " |
| 1146 | << "\"Input array is null\");\n"; |
| 1147 | mOut.indent() << " return " << ValueToReturn << ";\n"; |
| 1148 | mOut.indent() << "}\n\n"; |
| 1149 | |
| 1150 | mOut.indent() << "if (" << Length << " == 0) {\n"; |
| 1151 | mOut.indent() << " mRS->throwError(RS_ERROR_INVALID_PARAMETER, " |
| 1152 | << "\"Input array is zero-length\");\n"; |
| 1153 | mOut.indent() << " return " << ValueToReturn << ";\n"; |
| 1154 | mOut.indent() << "}\n\n"; |
| 1155 | } |
| 1156 | |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1157 | void RSReflectionCpp::genTypeInstanceFromPointer(const RSExportType *ET) { |
| 1158 | if (ET->getClass() == RSExportType::ExportClassPointer) { |
| 1159 | // For pointer parameters to original forEach kernels. |
| 1160 | const RSExportPointerType *EPT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1161 | static_cast<const RSExportPointerType *>(ET); |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1162 | genTypeInstance(EPT->getPointeeType()); |
| 1163 | } else { |
| 1164 | // For handling pass-by-value kernel parameters. |
| 1165 | genTypeInstance(ET); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | void RSReflectionCpp::genTypeInstance(const RSExportType *ET) { |
| 1170 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1171 | case RSExportType::ExportClassPrimitive: |
| 1172 | case RSExportType::ExportClassVector: |
| 1173 | case RSExportType::ExportClassConstantArray: |
| 1174 | case RSExportType::ExportClassRecord: { |
| 1175 | std::string TypeName = ET->getElementName(); |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1176 | mTypesToCheck.insert(TypeName); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1177 | break; |
| 1178 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1179 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1180 | default: |
| 1181 | break; |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1185 | void RSReflectionCpp::genInitExportVariable(const RSExportType *ET, |
| 1186 | const std::string &VarName, |
| 1187 | const clang::APValue &Val) { |
| 1188 | slangAssert(!Val.isUninit() && "Not a valid initializer"); |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1189 | |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1190 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1191 | case RSExportType::ExportClassPrimitive: { |
| 1192 | const RSExportPrimitiveType *EPT = |
| 1193 | static_cast<const RSExportPrimitiveType *>(ET); |
| 1194 | if (EPT->getType() == DataTypeBoolean) { |
| 1195 | genInitBoolExportVariable(VarName, Val); |
| 1196 | } else { |
| 1197 | genInitPrimitiveExportVariable(VarName, Val); |
| 1198 | } |
| 1199 | break; |
| 1200 | } |
| 1201 | case RSExportType::ExportClassPointer: { |
| 1202 | if (!Val.isInt() || Val.getInt().getSExtValue() != 0) |
| 1203 | std::cerr << "Initializer which is non-NULL to pointer type variable " |
| 1204 | "will be ignored" << std::endl; |
| 1205 | break; |
| 1206 | } |
| 1207 | case RSExportType::ExportClassVector: { |
| 1208 | const RSExportVectorType *EVT = static_cast<const RSExportVectorType *>(ET); |
| 1209 | switch (Val.getKind()) { |
| 1210 | case clang::APValue::Int: |
| 1211 | case clang::APValue::Float: { |
| 1212 | for (unsigned i = 0; i < EVT->getNumElement(); i++) { |
| 1213 | std::string Name = VarName + "." + getVectorAccessor(i); |
| 1214 | genInitPrimitiveExportVariable(Name, Val); |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1215 | } |
| 1216 | break; |
| 1217 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1218 | case clang::APValue::Vector: { |
| 1219 | unsigned NumElements = std::min( |
| 1220 | static_cast<unsigned>(EVT->getNumElement()), Val.getVectorLength()); |
| 1221 | for (unsigned i = 0; i < NumElements; i++) { |
| 1222 | const clang::APValue &ElementVal = Val.getVectorElt(i); |
| 1223 | std::string Name = VarName + "." + getVectorAccessor(i); |
| 1224 | genInitPrimitiveExportVariable(Name, ElementVal); |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1225 | } |
| 1226 | break; |
| 1227 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1228 | case clang::APValue::MemberPointer: |
| 1229 | case clang::APValue::Uninitialized: |
| 1230 | case clang::APValue::ComplexInt: |
| 1231 | case clang::APValue::ComplexFloat: |
| 1232 | case clang::APValue::LValue: |
| 1233 | case clang::APValue::Array: |
| 1234 | case clang::APValue::Struct: |
| 1235 | case clang::APValue::Union: |
| 1236 | case clang::APValue::AddrLabelDiff: { |
| 1237 | slangAssert(false && "Unexpected type of value of initializer."); |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1238 | } |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1239 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | } |
| 1242 | case RSExportType::ExportClassMatrix: |
| 1243 | case RSExportType::ExportClassConstantArray: |
| 1244 | case RSExportType::ExportClassRecord: { |
| 1245 | slangAssert(false && "Unsupported initializer for record/matrix/constant " |
| 1246 | "array type variable currently"); |
| 1247 | break; |
| 1248 | } |
| 1249 | default: { slangAssert(false && "Unknown class of type"); } |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1250 | } |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1253 | const char *RSReflectionCpp::getVectorAccessor(unsigned Index) { |
| 1254 | static const char *VectorAccessorMap[] = {/* 0 */ "x", |
| 1255 | /* 1 */ "y", |
| 1256 | /* 2 */ "z", |
| 1257 | /* 3 */ "w", |
| 1258 | }; |
| 1259 | |
| 1260 | slangAssert((Index < (sizeof(VectorAccessorMap) / sizeof(const char *))) && |
| 1261 | "Out-of-bound index to access vector member"); |
| 1262 | |
| 1263 | return VectorAccessorMap[Index]; |
| 1264 | } |
| 1265 | |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1266 | void RSReflectionCpp::genZeroInitExportVariable(const std::string &VarName) { |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1267 | mOut.indent() << "memset(&" << RS_EXPORT_VAR_PREFIX << VarName |
| 1268 | << ", 0, sizeof(" << RS_EXPORT_VAR_PREFIX << VarName << "));\n"; |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1271 | void |
| 1272 | RSReflectionCpp::genInitPrimitiveExportVariable(const std::string &VarName, |
| 1273 | const clang::APValue &Val) { |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1274 | slangAssert(!Val.isUninit() && "Not a valid initializer"); |
| 1275 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1276 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; |
| 1277 | genInitValue(Val); |
| 1278 | mOut << ";\n"; |
| 1279 | } |
| 1280 | |
| 1281 | void RSReflectionCpp::genInitValue(const clang::APValue &Val, bool asBool) { |
| 1282 | switch (Val.getKind()) { |
| 1283 | case clang::APValue::Int: { |
| 1284 | llvm::APInt api = Val.getInt(); |
| 1285 | if (asBool) { |
| 1286 | mOut << ((api.getSExtValue() == 0) ? "false" : "true"); |
| 1287 | } else { |
| 1288 | // TODO: Handle unsigned correctly for C++ |
| 1289 | mOut << api.getSExtValue(); |
| 1290 | if (api.getBitWidth() > 32) { |
| 1291 | mOut << "L"; |
| 1292 | } |
| 1293 | } |
| 1294 | break; |
| 1295 | } |
| 1296 | |
| 1297 | case clang::APValue::Float: { |
| 1298 | llvm::APFloat apf = Val.getFloat(); |
| 1299 | llvm::SmallString<30> s; |
| 1300 | apf.toString(s); |
| 1301 | mOut << s.c_str(); |
| 1302 | if (&apf.getSemantics() == &llvm::APFloat::IEEEsingle) { |
| 1303 | if (s.count('.') == 0) { |
| 1304 | mOut << ".f"; |
| 1305 | } else { |
| 1306 | mOut << "f"; |
| 1307 | } |
| 1308 | } |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | case clang::APValue::ComplexInt: |
| 1313 | case clang::APValue::ComplexFloat: |
| 1314 | case clang::APValue::LValue: |
| 1315 | case clang::APValue::Vector: { |
| 1316 | slangAssert(false && "Primitive type cannot have such kind of initializer"); |
| 1317 | break; |
| 1318 | } |
| 1319 | |
| 1320 | default: { slangAssert(false && "Unknown kind of initializer"); } |
| 1321 | } |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | void RSReflectionCpp::genInitBoolExportVariable(const std::string &VarName, |
| 1325 | const clang::APValue &Val) { |
| 1326 | slangAssert(!Val.isUninit() && "Not a valid initializer"); |
| 1327 | slangAssert((Val.getKind() == clang::APValue::Int) && |
| 1328 | "Bool type has wrong initial APValue"); |
| 1329 | |
Jean-Luc Brouillet | 1f9d121 | 2014-06-02 20:27:29 -0700 | [diff] [blame] | 1330 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = " |
| 1331 | << ((Val.getInt().getSExtValue() == 0) ? "false" : "true") |
| 1332 | << ";"; |
Stephen Hines | 8070683 | 2013-08-28 18:08:57 -0700 | [diff] [blame] | 1333 | } |
Stephen Hines | 003ac66 | 2013-08-21 00:37:51 -0700 | [diff] [blame] | 1334 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1335 | } // namespace slang |