blob: 2ccb2ad9307f6639074ee418c99dbee01cf7e90a [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hines7b51b552012-02-16 00:12:38 -08002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
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
zonr6315f762010-10-05 15:35:14 +080017#include "slang_rs_backend.h"
18
zonr6315f762010-10-05 15:35:14 +080019#include <string>
Stephen Hinese639eb52010-11-08 19:27:20 -080020#include <vector>
zonr6315f762010-10-05 15:35:14 +080021
Shih-wei Liao43730fe2012-08-02 23:06:18 -070022#include "clang/AST/ASTContext.h"
mkopec1c460b372012-01-09 11:21:50 -050023#include "clang/Frontend/CodeGenOptions.h"
24
zonr6315f762010-10-05 15:35:14 +080025#include "llvm/ADT/Twine.h"
26#include "llvm/ADT/StringExtras.h"
27
Stephen Hinese639eb52010-11-08 19:27:20 -080028#include "llvm/Constant.h"
29#include "llvm/Constants.h"
30#include "llvm/DerivedTypes.h"
31#include "llvm/Function.h"
Shih-wei Liao43730fe2012-08-02 23:06:18 -070032#include "llvm/IRBuilder.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080033#include "llvm/Metadata.h"
34#include "llvm/Module.h"
35
mkopec1c460b372012-01-09 11:21:50 -050036#include "llvm/Support/DebugLoc.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080037
Stephen Hines6e6578a2011-02-07 18:05:48 -080038#include "slang_assert.h"
Zonr Chang592a9542010-10-07 20:03:58 +080039#include "slang_rs.h"
zonr6315f762010-10-05 15:35:14 +080040#include "slang_rs_context.h"
Stephen Hines4ccf75e2011-08-16 18:21:01 -070041#include "slang_rs_export_foreach.h"
zonr6315f762010-10-05 15:35:14 +080042#include "slang_rs_export_func.h"
43#include "slang_rs_export_type.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080044#include "slang_rs_export_var.h"
45#include "slang_rs_metadata.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
Stephen Hinese639eb52010-11-08 19:27:20 -080047namespace slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070048
49RSBackend::RSBackend(RSContext *Context,
Logan Chien9207a2e2011-10-21 15:39:28 +080050 clang::DiagnosticsEngine *DiagEngine,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 const clang::CodeGenOptions &CodeGenOpts,
52 const clang::TargetOptions &TargetOpts,
Stephen Hines3fd0a942011-01-18 12:27:39 -080053 PragmaList *Pragmas,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080055 Slang::OutputType OT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 clang::SourceManager &SourceMgr,
Stephen Hines4a4bf922011-08-18 17:20:33 -070057 bool AllowRSPrefix)
Logan Chien9207a2e2011-10-21 15:39:28 +080058 : Backend(DiagEngine, CodeGenOpts, TargetOpts, Pragmas, OS, OT),
59 mContext(Context),
60 mSourceMgr(SourceMgr),
61 mAllowRSPrefix(AllowRSPrefix),
62 mExportVarMetadata(NULL),
63 mExportFuncMetadata(NULL),
Stephen Hines7b51b552012-02-16 00:12:38 -080064 mExportForEachNameMetadata(NULL),
65 mExportForEachSignatureMetadata(NULL),
Logan Chien9207a2e2011-10-21 15:39:28 +080066 mExportTypeMetadata(NULL),
67 mRSObjectSlotsMetadata(NULL),
68 mRefCount(mContext->getASTContext()) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069}
70
Stephen Hinescfae0f32010-11-01 18:57:31 -070071// 1) Add zero initialization of local RS object types
72void RSBackend::AnnotateFunction(clang::FunctionDecl *FD) {
73 if (FD &&
74 FD->hasBody() &&
75 !SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr)) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -070076 mRefCount.Init();
Stephen Hines4b32ffd2010-11-05 18:47:11 -070077 mRefCount.Visit(FD->getBody());
Stephen Hinescfae0f32010-11-01 18:57:31 -070078 }
79 return;
80}
81
Logan Chienfa6ef562011-11-25 13:50:02 +080082bool RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) {
Stephen Hinescfae0f32010-11-01 18:57:31 -070083 // 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 Chienab992e52011-07-20 22:06:52 +080088 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hinescfae0f32010-11-01 18:57:31 -070089 if (FD == NULL)
90 continue;
91 if (!FD->getName().startswith("rs")) // Check prefix
92 continue;
93 if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
Logan Chien9207a2e2011-10-21 15:39:28 +080094 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 Hinescfae0f32010-11-01 18:57:31 -0700100 }
101 }
102
103 // Process any non-static function declarations
104 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) {
Logan Chienab992e52011-07-20 22:06:52 +0800105 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hineseb2eec92011-01-09 19:13:09 -0800106 if (FD && FD->isGlobal()) {
107 AnnotateFunction(FD);
108 }
Stephen Hinescfae0f32010-11-01 18:57:31 -0700109 }
110
Logan Chienfa6ef562011-11-25 13:50:02 +0800111 return Backend::HandleTopLevelDecl(D);
Stephen Hinescfae0f32010-11-01 18:57:31 -0700112}
113
Stephen Hinesc97a3332010-11-30 15:31:08 -0800114namespace {
115
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700116static bool ValidateVarDecl(clang::VarDecl *VD, unsigned int TargetAPI) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700117 if (!VD) {
118 return true;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800119 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700120
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 Hines5bfec8d2012-04-04 08:18:57 -0700127 if (!RSExportType::NormalizeType(T, TypeName, &C.getDiagnostics(), VD)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700128 valid = false;
129 }
130 }
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700131 valid &= RSExportType::ValidateVarDecl(VD, TargetAPI);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700132
133 return valid;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800134}
135
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700136static bool ValidateASTContext(clang::ASTContext &C, unsigned int TargetAPI) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800137 bool valid = true;
Stephen Hinesfcda2352010-10-19 16:49:32 -0700138 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
Stephen Hinesc97a3332010-11-30 15:31:08 -0800139 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
140 DE = TUDecl->decls_end();
141 DI != DE;
142 DI++) {
Logan Chienab992e52011-07-20 22:06:52 +0800143 clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*DI);
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700144 if (VD && !ValidateVarDecl(VD, TargetAPI)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700145 valid = false;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800146 }
147 }
148
149 return valid;
150}
151
Stephen Hinese5e64432010-12-02 18:48:20 -0800152} // namespace
Stephen Hinesc97a3332010-11-30 15:31:08 -0800153
154void RSBackend::HandleTranslationUnitPre(clang::ASTContext &C) {
155 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
156
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700157 if (!ValidateASTContext(C, getTargetAPI())) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800158 return;
159 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700160
Stephen Hines96ab06c2011-01-05 15:29:26 -0800161 int version = mContext->getVersion();
162 if (version == 0) {
163 // Not setting a version is an error
Stephen Hines7aff4a02011-12-08 18:34:27 -0800164 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 Hines96ab06c2011-01-05 15:29:26 -0800171 }
172
Stephen Hines688e64b2011-08-23 16:01:25 -0700173 // 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 Hinescfae0f32010-11-01 18:57:31 -0700180 // Process any static function declarations
Stephen Hinesfcda2352010-10-19 16:49:32 -0700181 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 Chienab992e52011-07-20 22:06:52 +0800185 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hineseb2eec92011-01-09 19:13:09 -0800186 if (FD && !FD->isGlobal()) {
187 AnnotateFunction(FD);
188 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700189 }
190 }
191
192 return;
193}
194
195///////////////////////////////////////////////////////////////////////////////
Zonr Chang68fc02c2010-10-13 19:09:19 +0800196void RSBackend::HandleTranslationUnitPost(llvm::Module *M) {
Stephen Hinesc808a992010-11-29 17:20:42 -0800197 if (!mContext->processExport()) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800198 return;
Stephen Hinesc808a992010-11-29 17:20:42 -0800199 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700200
mkopec1c460b372012-01-09 11:21:50 -0500201 // 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 Liao9ef2f782010-10-01 12:31:37 -0700206 // Dump export variable info
207 if (mContext->hasExportVar()) {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800208 int slotCount = 0;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700209 if (mExportVarMetadata == NULL)
Zonr Chang68fc02c2010-10-13 19:09:19 +0800210 mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700211
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700212 llvm::SmallVector<llvm::Value*, 2> ExportVarInfo;
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800213
214 // We emit slot information (#rs_object_slots) for any reference counted
215 // RS type or pointer (which can also be bound).
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700216
zonr6315f762010-10-05 15:35:14 +0800217 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 Hinesb3a12fe2011-01-26 20:16:38 -0800223 bool countsAsRSObject = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700224
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700225 // Variable name
226 ExportVarInfo.push_back(
227 llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700228
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700229 // Type name
Zonr Changa65ec162010-10-17 01:53:05 +0800230 switch (ET->getClass()) {
231 case RSExportType::ExportClassPrimitive: {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800232 const RSExportPrimitiveType *PT =
233 static_cast<const RSExportPrimitiveType*>(ET);
Zonr Changa65ec162010-10-17 01:53:05 +0800234 ExportVarInfo.push_back(
235 llvm::MDString::get(
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800236 mLLVMContext, llvm::utostr_32(PT->getType())));
237 if (PT->isRSObjectType()) {
238 countsAsRSObject = true;
239 }
Zonr Changa65ec162010-10-17 01:53:05 +0800240 break;
241 }
242 case RSExportType::ExportClassPointer: {
243 ExportVarInfo.push_back(
244 llvm::MDString::get(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700245 mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
Zonr Changa65ec162010-10-17 01:53:05 +0800246 ->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 Liao462aefd2010-06-04 15:32:04 -0700266
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700267 mExportVarMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700268 llvm::MDNode::get(mLLVMContext, ExportVarInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700269 ExportVarInfo.clear();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800270
271 if (mRSObjectSlotsMetadata == NULL) {
272 mRSObjectSlotsMetadata =
273 M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN);
274 }
275
276 if (countsAsRSObject) {
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700277 mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
278 llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount))));
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800279 }
280
281 slotCount++;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700282 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700283 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700284
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700285 // Dump export function info
286 if (mContext->hasExportFunc()) {
287 if (mExportFuncMetadata == NULL)
288 mExportFuncMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800289 M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700290
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700291 llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700292
zonr6315f762010-10-05 15:35:14 +0800293 for (RSContext::const_export_func_iterator
294 I = mContext->export_funcs_begin(),
295 E = mContext->export_funcs_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700296 I != E;
297 I++) {
zonr6315f762010-10-05 15:35:14 +0800298 const RSExportFunc *EF = *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700299
300 // Function name
zonr6315f762010-10-05 15:35:14 +0800301 if (!EF->hasParam()) {
302 ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
303 EF->getName().c_str()));
304 } else {
Zonr Chang68fc02c2010-10-13 19:09:19 +0800305 llvm::Function *F = M->getFunction(EF->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700306 llvm::Function *HelperFunction;
307 const std::string HelperFunctionName(".helper_" + EF->getName());
308
Stephen Hines6e6578a2011-02-07 18:05:48 -0800309 slangAssert(F && "Function marked as exported disappeared in Bitcode");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700310
311 // Create helper function
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700312 {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800313 llvm::StructType *HelperFunctionParameterTy = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700314
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800315 if (!F->getArgumentList().empty()) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700316 std::vector<llvm::Type*> HelperFunctionParameterTys;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800317 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 Hinesa67e4452011-07-19 15:39:26 -0700322 llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800323 }
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 Liao7c67e572011-07-19 05:54:53 -0700341 std::vector<llvm::Type*> Params;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800342 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 Hinesa67e4452011-07-19 15:39:26 -0700350 Params,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800351 /* IsVarArgs = */false);
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700352
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700353 HelperFunction =
354 llvm::Function::Create(HelperFunctionType,
355 llvm::GlobalValue::ExternalLinkage,
356 HelperFunctionName,
Zonr Chang68fc02c2010-10-13 19:09:19 +0800357 M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700358
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700359 HelperFunction->addFnAttr(llvm::Attribute::NoInline);
360 HelperFunction->setCallingConv(F->getCallingConv());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700361
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700362 // 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 Liao462aefd2010-06-04 15:32:04 -0700371
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700372 Idx[0] =
373 llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700374
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700375 // getelementptr and load instruction for all elements in
376 // parameter .p
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800377 for (size_t i = 0; i < EF->getNumParameters(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700378 // getelementptr
Logan Chien9207a2e2011-10-21 15:39:28 +0800379 Idx[1] = llvm::ConstantInt::get(
380 llvm::Type::getInt32Ty(mLLVMContext), i);
381
382 llvm::Value *Ptr =
383 IB->CreateInBoundsGEP(HelperFunctionParameter, Idx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700384
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700385 // load
386 llvm::Value *V = IB->CreateLoad(Ptr);
387 Params.push_back(V);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700388 }
389
Stephen Hinesa67e4452011-07-19 15:39:26 -0700390 // Call and pass the all elements as parameter to F
391 llvm::CallInst *CI = IB->CreateCall(F, Params);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700392
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700393 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 Liao462aefd2010-06-04 15:32:04 -0700402 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700403
404 ExportFuncInfo.push_back(
405 llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str()));
406 }
407
408 mExportFuncMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700409 llvm::MDNode::get(mLLVMContext, ExportFuncInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700410 ExportFuncInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700411 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700412 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700413
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700414 // Dump export function info
415 if (mContext->hasExportForEach()) {
Stephen Hines7b51b552012-02-16 00:12:38 -0800416 if (mExportForEachNameMetadata == NULL) {
417 mExportForEachNameMetadata =
418 M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_NAME_MN);
419 }
420 if (mExportForEachSignatureMetadata == NULL) {
421 mExportForEachSignatureMetadata =
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700422 M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_MN);
Stephen Hines7b51b552012-02-16 00:12:38 -0800423 }
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700424
Stephen Hines7b51b552012-02-16 00:12:38 -0800425 llvm::SmallVector<llvm::Value*, 1> ExportForEachName;
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700426 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 Hines7b51b552012-02-16 00:12:38 -0800435 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 Hines4ccf75e2011-08-16 18:21:01 -0700442 ExportForEachInfo.push_back(
443 llvm::MDString::get(mLLVMContext,
Stephen Hines7b51b552012-02-16 00:12:38 -0800444 llvm::utostr_32(EFE->getSignatureMetadata())));
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700445
Stephen Hines7b51b552012-02-16 00:12:38 -0800446 mExportForEachSignatureMetadata->addOperand(
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700447 llvm::MDNode::get(mLLVMContext, ExportForEachInfo));
448 ExportForEachInfo.clear();
449 }
450 }
451
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700452 // Dump export type info
453 if (mContext->hasExportType()) {
454 llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700455
zonr6315f762010-10-05 15:35:14 +0800456 for (RSContext::const_export_type_iterator
457 I = mContext->export_types_begin(),
458 E = mContext->export_types_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700459 I != E;
460 I++) {
461 // First, dump type name list to export
462 const RSExportType *ET = I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700463
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700464 ExportTypeInfo.clear();
465 // Type name
466 ExportTypeInfo.push_back(
467 llvm::MDString::get(mLLVMContext, ET->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700468
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700469 if (ET->getClass() == RSExportType::ExportClassRecord) {
470 const RSExportRecordType *ERT =
471 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700472
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700473 if (mExportTypeMetadata == NULL)
474 mExportTypeMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800475 M->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700476
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700477 mExportTypeMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700478 llvm::MDNode::get(mLLVMContext, ExportTypeInfo));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700479
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700480 // Now, export struct field information to %[struct name]
481 std::string StructInfoMetadataName("%");
482 StructInfoMetadataName.append(ET->getName());
483 llvm::NamedMDNode *StructInfoMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800484 M->getOrInsertNamedMetadata(StructInfoMetadataName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700485 llvm::SmallVector<llvm::Value*, 3> FieldInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700486
Stephen Hines6e6578a2011-02-07 18:05:48 -0800487 slangAssert(StructInfoMetadata->getNumOperands() == 0 &&
488 "Metadata with same name was created before");
zonr6315f762010-10-05 15:35:14 +0800489 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
490 FE = ERT->fields_end();
491 FI != FE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700492 FI++) {
493 const RSExportRecordType::Field *F = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700494
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700495 // 1. field name
496 FieldInfo.push_back(llvm::MDString::get(mLLVMContext,
497 F->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700498
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700499 // 2. field type name
500 FieldInfo.push_back(
501 llvm::MDString::get(mLLVMContext,
502 F->getType()->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700503
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700504 StructInfoMetadata->addOperand(
505 llvm::MDNode::get(mLLVMContext, FieldInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700506 FieldInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700507 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700508 } // ET->getClass() == RSExportType::ExportClassRecord
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700509 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700510 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700511
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700512 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700513}
514
515RSBackend::~RSBackend() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700516 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700517}
Stephen Hinese639eb52010-11-08 19:27:20 -0800518
519} // namespace slang