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