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