Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_rs_backend.h" |
| 18 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 19 | #include <string> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 20 | #include <vector> |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 21 | |
| 22 | #include "llvm/ADT/Twine.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 25 | #include "llvm/Constant.h" |
| 26 | #include "llvm/Constants.h" |
| 27 | #include "llvm/DerivedTypes.h" |
| 28 | #include "llvm/Function.h" |
| 29 | #include "llvm/Metadata.h" |
| 30 | #include "llvm/Module.h" |
| 31 | |
| 32 | #include "llvm/Support/IRBuilder.h" |
| 33 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 34 | #include "slang_assert.h" |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 35 | #include "slang_rs.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 36 | #include "slang_rs_context.h" |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 37 | #include "slang_rs_export_foreach.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 38 | #include "slang_rs_export_func.h" |
| 39 | #include "slang_rs_export_type.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 40 | #include "slang_rs_export_var.h" |
| 41 | #include "slang_rs_metadata.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 42 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 43 | namespace slang { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 44 | |
| 45 | RSBackend::RSBackend(RSContext *Context, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 46 | clang::DiagnosticsEngine *DiagEngine, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 47 | const clang::CodeGenOptions &CodeGenOpts, |
| 48 | const clang::TargetOptions &TargetOpts, |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 49 | PragmaList *Pragmas, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | llvm::raw_ostream *OS, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 51 | Slang::OutputType OT, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 52 | clang::SourceManager &SourceMgr, |
Stephen Hines | 4a4bf92 | 2011-08-18 17:20:33 -0700 | [diff] [blame] | 53 | bool AllowRSPrefix) |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 54 | : Backend(DiagEngine, CodeGenOpts, TargetOpts, Pragmas, OS, OT), |
| 55 | mContext(Context), |
| 56 | mSourceMgr(SourceMgr), |
| 57 | mAllowRSPrefix(AllowRSPrefix), |
| 58 | mExportVarMetadata(NULL), |
| 59 | mExportFuncMetadata(NULL), |
| 60 | mExportForEachMetadata(NULL), |
| 61 | mExportTypeMetadata(NULL), |
| 62 | mRSObjectSlotsMetadata(NULL), |
| 63 | mRefCount(mContext->getASTContext()) { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Stephen Hines | cfae0f3 | 2010-11-01 18:57:31 -0700 | [diff] [blame] | 66 | // 1) Add zero initialization of local RS object types |
| 67 | void RSBackend::AnnotateFunction(clang::FunctionDecl *FD) { |
| 68 | if (FD && |
| 69 | FD->hasBody() && |
| 70 | !SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr)) { |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 71 | mRefCount.Init(); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 72 | mRefCount.Visit(FD->getBody()); |
Stephen Hines | cfae0f3 | 2010-11-01 18:57:31 -0700 | [diff] [blame] | 73 | } |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | void RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) { |
| 78 | // Disallow user-defined functions with prefix "rs" |
| 79 | if (!mAllowRSPrefix) { |
| 80 | // Iterate all function declarations in the program. |
| 81 | for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); |
| 82 | I != E; I++) { |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 83 | clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I); |
Stephen Hines | cfae0f3 | 2010-11-01 18:57:31 -0700 | [diff] [blame] | 84 | if (FD == NULL) |
| 85 | continue; |
| 86 | if (!FD->getName().startswith("rs")) // Check prefix |
| 87 | continue; |
| 88 | if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr)) |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 89 | mDiagEngine.Report( |
| 90 | clang::FullSourceLoc(FD->getLocation(), mSourceMgr), |
| 91 | mDiagEngine.getCustomDiagID(clang::DiagnosticsEngine::Error, |
| 92 | "invalid function name prefix, " |
| 93 | "\"rs\" is reserved: '%0'")) |
| 94 | << FD->getName(); |
Stephen Hines | cfae0f3 | 2010-11-01 18:57:31 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | // Process any non-static function declarations |
| 99 | for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) { |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 100 | clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I); |
Stephen Hines | eb2eec9 | 2011-01-09 19:13:09 -0800 | [diff] [blame] | 101 | if (FD && FD->isGlobal()) { |
| 102 | AnnotateFunction(FD); |
| 103 | } |
Stephen Hines | cfae0f3 | 2010-11-01 18:57:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | Backend::HandleTopLevelDecl(D); |
| 107 | return; |
| 108 | } |
| 109 | |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 110 | namespace { |
| 111 | |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 112 | static bool ValidateVarDecl(clang::VarDecl *VD) { |
| 113 | if (!VD) { |
| 114 | return true; |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 115 | } |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 116 | |
| 117 | clang::ASTContext &C = VD->getASTContext(); |
| 118 | const clang::Type *T = VD->getType().getTypePtr(); |
| 119 | bool valid = true; |
| 120 | |
| 121 | if (VD->getLinkage() == clang::ExternalLinkage) { |
| 122 | llvm::StringRef TypeName; |
| 123 | if (!RSExportType::NormalizeType(T, TypeName, &C.getDiagnostics(), VD)) { |
| 124 | valid = false; |
| 125 | } |
| 126 | } |
| 127 | valid &= RSExportType::ValidateVarDecl(VD); |
| 128 | |
| 129 | return valid; |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 132 | static bool ValidateASTContext(clang::ASTContext &C) { |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 133 | bool valid = true; |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 134 | clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 135 | for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(), |
| 136 | DE = TUDecl->decls_end(); |
| 137 | DI != DE; |
| 138 | DI++) { |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 139 | clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*DI); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 140 | if (VD && !ValidateVarDecl(VD)) { |
| 141 | valid = false; |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | return valid; |
| 146 | } |
| 147 | |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 148 | } // namespace |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 149 | |
| 150 | void RSBackend::HandleTranslationUnitPre(clang::ASTContext &C) { |
| 151 | clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
| 152 | |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 153 | if (!ValidateASTContext(C)) { |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 154 | return; |
| 155 | } |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 156 | |
Stephen Hines | 96ab06c | 2011-01-05 15:29:26 -0800 | [diff] [blame] | 157 | int version = mContext->getVersion(); |
| 158 | if (version == 0) { |
| 159 | // Not setting a version is an error |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 160 | mDiagEngine.Report(mDiagEngine.getCustomDiagID( |
| 161 | clang::DiagnosticsEngine::Error, |
| 162 | "Missing pragma for version in source file")); |
Stephen Hines | 96ab06c | 2011-01-05 15:29:26 -0800 | [diff] [blame] | 163 | } else if (version > 1) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 164 | mDiagEngine.Report(mDiagEngine.getCustomDiagID( |
| 165 | clang::DiagnosticsEngine::Error, |
| 166 | "Pragma for version in source file must be set to 1")); |
Stephen Hines | 96ab06c | 2011-01-05 15:29:26 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 169 | // Create a static global destructor if necessary (to handle RS object |
| 170 | // runtime cleanup). |
| 171 | clang::FunctionDecl *FD = mRefCount.CreateStaticGlobalDtor(); |
| 172 | if (FD) { |
| 173 | HandleTopLevelDecl(clang::DeclGroupRef(FD)); |
| 174 | } |
| 175 | |
Stephen Hines | cfae0f3 | 2010-11-01 18:57:31 -0700 | [diff] [blame] | 176 | // Process any static function declarations |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 177 | for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(), |
| 178 | E = TUDecl->decls_end(); I != E; I++) { |
| 179 | if ((I->getKind() >= clang::Decl::firstFunction) && |
| 180 | (I->getKind() <= clang::Decl::lastFunction)) { |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 181 | clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I); |
Stephen Hines | eb2eec9 | 2011-01-09 19:13:09 -0800 | [diff] [blame] | 182 | if (FD && !FD->isGlobal()) { |
| 183 | AnnotateFunction(FD); |
| 184 | } |
Stephen Hines | fcda235 | 2010-10-19 16:49:32 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | /////////////////////////////////////////////////////////////////////////////// |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 192 | void RSBackend::HandleTranslationUnitPost(llvm::Module *M) { |
Stephen Hines | c808a99 | 2010-11-29 17:20:42 -0800 | [diff] [blame] | 193 | if (!mContext->processExport()) { |
Stephen Hines | c97a333 | 2010-11-30 15:31:08 -0800 | [diff] [blame] | 194 | return; |
Stephen Hines | c808a99 | 2010-11-29 17:20:42 -0800 | [diff] [blame] | 195 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 196 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 197 | // Dump export variable info |
| 198 | if (mContext->hasExportVar()) { |
Stephen Hines | b3a12fe | 2011-01-26 20:16:38 -0800 | [diff] [blame] | 199 | int slotCount = 0; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 200 | if (mExportVarMetadata == NULL) |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 201 | mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 202 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 203 | llvm::SmallVector<llvm::Value*, 2> ExportVarInfo; |
Stephen Hines | b3a12fe | 2011-01-26 20:16:38 -0800 | [diff] [blame] | 204 | |
| 205 | // We emit slot information (#rs_object_slots) for any reference counted |
| 206 | // RS type or pointer (which can also be bound). |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 207 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 208 | for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(), |
| 209 | E = mContext->export_vars_end(); |
| 210 | I != E; |
| 211 | I++) { |
| 212 | const RSExportVar *EV = *I; |
| 213 | const RSExportType *ET = EV->getType(); |
Stephen Hines | b3a12fe | 2011-01-26 20:16:38 -0800 | [diff] [blame] | 214 | bool countsAsRSObject = false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 215 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 216 | // Variable name |
| 217 | ExportVarInfo.push_back( |
| 218 | llvm::MDString::get(mLLVMContext, EV->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 219 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 220 | // Type name |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 221 | switch (ET->getClass()) { |
| 222 | case RSExportType::ExportClassPrimitive: { |
Stephen Hines | b3a12fe | 2011-01-26 20:16:38 -0800 | [diff] [blame] | 223 | const RSExportPrimitiveType *PT = |
| 224 | static_cast<const RSExportPrimitiveType*>(ET); |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 225 | ExportVarInfo.push_back( |
| 226 | llvm::MDString::get( |
Stephen Hines | b3a12fe | 2011-01-26 20:16:38 -0800 | [diff] [blame] | 227 | mLLVMContext, llvm::utostr_32(PT->getType()))); |
| 228 | if (PT->isRSObjectType()) { |
| 229 | countsAsRSObject = true; |
| 230 | } |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | case RSExportType::ExportClassPointer: { |
| 234 | ExportVarInfo.push_back( |
| 235 | llvm::MDString::get( |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 236 | mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET) |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 237 | ->getPointeeType()->getName()).c_str())); |
| 238 | break; |
| 239 | } |
| 240 | case RSExportType::ExportClassMatrix: { |
| 241 | ExportVarInfo.push_back( |
| 242 | llvm::MDString::get( |
| 243 | mLLVMContext, llvm::utostr_32( |
| 244 | RSExportPrimitiveType::DataTypeRSMatrix2x2 + |
| 245 | static_cast<const RSExportMatrixType*>(ET)->getDim() - 2))); |
| 246 | break; |
| 247 | } |
| 248 | case RSExportType::ExportClassVector: |
| 249 | case RSExportType::ExportClassConstantArray: |
| 250 | case RSExportType::ExportClassRecord: { |
| 251 | ExportVarInfo.push_back( |
| 252 | llvm::MDString::get(mLLVMContext, |
| 253 | EV->getType()->getName().c_str())); |
| 254 | break; |
| 255 | } |
| 256 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 257 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 258 | mExportVarMetadata->addOperand( |
Stephen Hines | d27a74e | 2011-07-13 20:59:46 -0700 | [diff] [blame] | 259 | llvm::MDNode::get(mLLVMContext, ExportVarInfo)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 260 | ExportVarInfo.clear(); |
Stephen Hines | b3a12fe | 2011-01-26 20:16:38 -0800 | [diff] [blame] | 261 | |
| 262 | if (mRSObjectSlotsMetadata == NULL) { |
| 263 | mRSObjectSlotsMetadata = |
| 264 | M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN); |
| 265 | } |
| 266 | |
| 267 | if (countsAsRSObject) { |
Stephen Hines | d27a74e | 2011-07-13 20:59:46 -0700 | [diff] [blame] | 268 | mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext, |
| 269 | llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount)))); |
Stephen Hines | b3a12fe | 2011-01-26 20:16:38 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | slotCount++; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 273 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 274 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 275 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 276 | // Dump export function info |
| 277 | if (mContext->hasExportFunc()) { |
| 278 | if (mExportFuncMetadata == NULL) |
| 279 | mExportFuncMetadata = |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 280 | M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 281 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 282 | llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 283 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 284 | for (RSContext::const_export_func_iterator |
| 285 | I = mContext->export_funcs_begin(), |
| 286 | E = mContext->export_funcs_end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 287 | I != E; |
| 288 | I++) { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 289 | const RSExportFunc *EF = *I; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 290 | |
| 291 | // Function name |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 292 | if (!EF->hasParam()) { |
| 293 | ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext, |
| 294 | EF->getName().c_str())); |
| 295 | } else { |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 296 | llvm::Function *F = M->getFunction(EF->getName()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 297 | llvm::Function *HelperFunction; |
| 298 | const std::string HelperFunctionName(".helper_" + EF->getName()); |
| 299 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 300 | slangAssert(F && "Function marked as exported disappeared in Bitcode"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 301 | |
| 302 | // Create helper function |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 303 | { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 304 | llvm::StructType *HelperFunctionParameterTy = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 305 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 306 | if (!F->getArgumentList().empty()) { |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 307 | std::vector<llvm::Type*> HelperFunctionParameterTys; |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 308 | for (llvm::Function::arg_iterator AI = F->arg_begin(), |
| 309 | AE = F->arg_end(); AI != AE; AI++) |
| 310 | HelperFunctionParameterTys.push_back(AI->getType()); |
| 311 | |
| 312 | HelperFunctionParameterTy = |
Stephen Hines | a67e445 | 2011-07-19 15:39:26 -0700 | [diff] [blame] | 313 | llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) { |
| 317 | fprintf(stderr, "Failed to export function %s: parameter type " |
| 318 | "mismatch during creation of helper function.\n", |
| 319 | EF->getName().c_str()); |
| 320 | |
| 321 | const RSExportRecordType *Expected = EF->getParamPacketType(); |
| 322 | if (Expected) { |
| 323 | fprintf(stderr, "Expected:\n"); |
| 324 | Expected->getLLVMType()->dump(); |
| 325 | } |
| 326 | if (HelperFunctionParameterTy) { |
| 327 | fprintf(stderr, "Got:\n"); |
| 328 | HelperFunctionParameterTy->dump(); |
| 329 | } |
| 330 | } |
| 331 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 332 | std::vector<llvm::Type*> Params; |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 333 | if (HelperFunctionParameterTy) { |
| 334 | llvm::PointerType *HelperFunctionParameterTyP = |
| 335 | llvm::PointerType::getUnqual(HelperFunctionParameterTy); |
| 336 | Params.push_back(HelperFunctionParameterTyP); |
| 337 | } |
| 338 | |
| 339 | llvm::FunctionType * HelperFunctionType = |
| 340 | llvm::FunctionType::get(F->getReturnType(), |
Stephen Hines | a67e445 | 2011-07-19 15:39:26 -0700 | [diff] [blame] | 341 | Params, |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 342 | /* IsVarArgs = */false); |
Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 343 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 344 | HelperFunction = |
| 345 | llvm::Function::Create(HelperFunctionType, |
| 346 | llvm::GlobalValue::ExternalLinkage, |
| 347 | HelperFunctionName, |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 348 | M); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 349 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 350 | HelperFunction->addFnAttr(llvm::Attribute::NoInline); |
| 351 | HelperFunction->setCallingConv(F->getCallingConv()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 352 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 353 | // Create helper function body |
| 354 | { |
| 355 | llvm::Argument *HelperFunctionParameter = |
| 356 | &(*HelperFunction->arg_begin()); |
| 357 | llvm::BasicBlock *BB = |
| 358 | llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction); |
| 359 | llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB); |
| 360 | llvm::SmallVector<llvm::Value*, 6> Params; |
| 361 | llvm::Value *Idx[2]; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 362 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 363 | Idx[0] = |
| 364 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 365 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 366 | // getelementptr and load instruction for all elements in |
| 367 | // parameter .p |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 368 | for (size_t i = 0; i < EF->getNumParameters(); i++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 369 | // getelementptr |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 370 | Idx[1] = llvm::ConstantInt::get( |
| 371 | llvm::Type::getInt32Ty(mLLVMContext), i); |
| 372 | |
| 373 | llvm::Value *Ptr = |
| 374 | IB->CreateInBoundsGEP(HelperFunctionParameter, Idx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 375 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 376 | // load |
| 377 | llvm::Value *V = IB->CreateLoad(Ptr); |
| 378 | Params.push_back(V); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Stephen Hines | a67e445 | 2011-07-19 15:39:26 -0700 | [diff] [blame] | 381 | // Call and pass the all elements as parameter to F |
| 382 | llvm::CallInst *CI = IB->CreateCall(F, Params); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 383 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 384 | CI->setCallingConv(F->getCallingConv()); |
| 385 | |
| 386 | if (F->getReturnType() == llvm::Type::getVoidTy(mLLVMContext)) |
| 387 | IB->CreateRetVoid(); |
| 388 | else |
| 389 | IB->CreateRet(CI); |
| 390 | |
| 391 | delete IB; |
| 392 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 393 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 394 | |
| 395 | ExportFuncInfo.push_back( |
| 396 | llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str())); |
| 397 | } |
| 398 | |
| 399 | mExportFuncMetadata->addOperand( |
Stephen Hines | d27a74e | 2011-07-13 20:59:46 -0700 | [diff] [blame] | 400 | llvm::MDNode::get(mLLVMContext, ExportFuncInfo)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 401 | ExportFuncInfo.clear(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 402 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 403 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 404 | |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 405 | // Dump export function info |
| 406 | if (mContext->hasExportForEach()) { |
| 407 | if (mExportForEachMetadata == NULL) |
| 408 | mExportForEachMetadata = |
| 409 | M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_MN); |
| 410 | |
| 411 | llvm::SmallVector<llvm::Value*, 1> ExportForEachInfo; |
| 412 | |
| 413 | for (RSContext::const_export_foreach_iterator |
| 414 | I = mContext->export_foreach_begin(), |
| 415 | E = mContext->export_foreach_end(); |
| 416 | I != E; |
| 417 | I++) { |
| 418 | const RSExportForEach *EFE = *I; |
| 419 | |
| 420 | ExportForEachInfo.push_back( |
| 421 | llvm::MDString::get(mLLVMContext, |
| 422 | llvm::utostr_32(EFE->getMetadataEncoding()))); |
| 423 | |
| 424 | mExportForEachMetadata->addOperand( |
| 425 | llvm::MDNode::get(mLLVMContext, ExportForEachInfo)); |
| 426 | ExportForEachInfo.clear(); |
| 427 | } |
| 428 | } |
| 429 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 430 | // Dump export type info |
| 431 | if (mContext->hasExportType()) { |
| 432 | llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 433 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 434 | for (RSContext::const_export_type_iterator |
| 435 | I = mContext->export_types_begin(), |
| 436 | E = mContext->export_types_end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 437 | I != E; |
| 438 | I++) { |
| 439 | // First, dump type name list to export |
| 440 | const RSExportType *ET = I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 441 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 442 | ExportTypeInfo.clear(); |
| 443 | // Type name |
| 444 | ExportTypeInfo.push_back( |
| 445 | llvm::MDString::get(mLLVMContext, ET->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 446 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 447 | if (ET->getClass() == RSExportType::ExportClassRecord) { |
| 448 | const RSExportRecordType *ERT = |
| 449 | static_cast<const RSExportRecordType*>(ET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 450 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 451 | if (mExportTypeMetadata == NULL) |
| 452 | mExportTypeMetadata = |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 453 | M->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 454 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 455 | mExportTypeMetadata->addOperand( |
Stephen Hines | d27a74e | 2011-07-13 20:59:46 -0700 | [diff] [blame] | 456 | llvm::MDNode::get(mLLVMContext, ExportTypeInfo)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 457 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 458 | // Now, export struct field information to %[struct name] |
| 459 | std::string StructInfoMetadataName("%"); |
| 460 | StructInfoMetadataName.append(ET->getName()); |
| 461 | llvm::NamedMDNode *StructInfoMetadata = |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 462 | M->getOrInsertNamedMetadata(StructInfoMetadataName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 463 | llvm::SmallVector<llvm::Value*, 3> FieldInfo; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 464 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 465 | slangAssert(StructInfoMetadata->getNumOperands() == 0 && |
| 466 | "Metadata with same name was created before"); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 467 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
| 468 | FE = ERT->fields_end(); |
| 469 | FI != FE; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 470 | FI++) { |
| 471 | const RSExportRecordType::Field *F = *FI; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 472 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 473 | // 1. field name |
| 474 | FieldInfo.push_back(llvm::MDString::get(mLLVMContext, |
| 475 | F->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 476 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 477 | // 2. field type name |
| 478 | FieldInfo.push_back( |
| 479 | llvm::MDString::get(mLLVMContext, |
| 480 | F->getType()->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 481 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 482 | // 3. field kind |
| 483 | switch (F->getType()->getClass()) { |
| 484 | case RSExportType::ExportClassPrimitive: |
| 485 | case RSExportType::ExportClassVector: { |
| 486 | const RSExportPrimitiveType *EPT = |
| 487 | static_cast<const RSExportPrimitiveType*>(F->getType()); |
| 488 | FieldInfo.push_back( |
| 489 | llvm::MDString::get(mLLVMContext, |
| 490 | llvm::itostr(EPT->getKind()))); |
| 491 | break; |
| 492 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 493 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 494 | default: { |
| 495 | FieldInfo.push_back( |
| 496 | llvm::MDString::get(mLLVMContext, |
| 497 | llvm::itostr( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 498 | RSExportPrimitiveType::DataKindUser))); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 502 | |
Stephen Hines | d27a74e | 2011-07-13 20:59:46 -0700 | [diff] [blame] | 503 | StructInfoMetadata->addOperand( |
| 504 | llvm::MDNode::get(mLLVMContext, FieldInfo)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 505 | FieldInfo.clear(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 506 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 507 | } // ET->getClass() == RSExportType::ExportClassRecord |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 508 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 509 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 510 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 511 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | RSBackend::~RSBackend() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 515 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 516 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 517 | |
| 518 | } // namespace slang |