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