blob: 6602130bbcc94be06f3ef6de6f57b02fc005e08d [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
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
mkopec1c460b372012-01-09 11:21:50 -050022#include "clang/Frontend/CodeGenOptions.h"
23
zonr6315f762010-10-05 15:35:14 +080024#include "llvm/ADT/Twine.h"
25#include "llvm/ADT/StringExtras.h"
26
Stephen Hinese639eb52010-11-08 19:27:20 -080027#include "llvm/Constant.h"
28#include "llvm/Constants.h"
29#include "llvm/DerivedTypes.h"
30#include "llvm/Function.h"
31#include "llvm/Metadata.h"
32#include "llvm/Module.h"
33
mkopec1c460b372012-01-09 11:21:50 -050034#include "llvm/Support/DebugLoc.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080035#include "llvm/Support/IRBuilder.h"
36
Stephen Hines6e6578a2011-02-07 18:05:48 -080037#include "slang_assert.h"
Zonr Chang592a9542010-10-07 20:03:58 +080038#include "slang_rs.h"
zonr6315f762010-10-05 15:35:14 +080039#include "slang_rs_context.h"
Stephen Hines4ccf75e2011-08-16 18:21:01 -070040#include "slang_rs_export_foreach.h"
zonr6315f762010-10-05 15:35:14 +080041#include "slang_rs_export_func.h"
42#include "slang_rs_export_type.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080043#include "slang_rs_export_var.h"
44#include "slang_rs_metadata.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070045
Stephen Hinese639eb52010-11-08 19:27:20 -080046namespace slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047
48RSBackend::RSBackend(RSContext *Context,
Logan Chien9207a2e2011-10-21 15:39:28 +080049 clang::DiagnosticsEngine *DiagEngine,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 const clang::CodeGenOptions &CodeGenOpts,
51 const clang::TargetOptions &TargetOpts,
Stephen Hines3fd0a942011-01-18 12:27:39 -080052 PragmaList *Pragmas,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070053 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080054 Slang::OutputType OT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070055 clang::SourceManager &SourceMgr,
Stephen Hines4a4bf922011-08-18 17:20:33 -070056 bool AllowRSPrefix)
Logan Chien9207a2e2011-10-21 15:39:28 +080057 : Backend(DiagEngine, CodeGenOpts, TargetOpts, Pragmas, OS, OT),
58 mContext(Context),
59 mSourceMgr(SourceMgr),
60 mAllowRSPrefix(AllowRSPrefix),
61 mExportVarMetadata(NULL),
62 mExportFuncMetadata(NULL),
63 mExportForEachMetadata(NULL),
64 mExportTypeMetadata(NULL),
65 mRSObjectSlotsMetadata(NULL),
mkopec1c460b372012-01-09 11:21:50 -050066 mRSOptimizationMetadata(NULL),
Logan Chien9207a2e2011-10-21 15:39:28 +080067 mRefCount(mContext->getASTContext()) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070068}
69
Stephen Hinescfae0f32010-11-01 18:57:31 -070070// 1) Add zero initialization of local RS object types
71void RSBackend::AnnotateFunction(clang::FunctionDecl *FD) {
72 if (FD &&
73 FD->hasBody() &&
74 !SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr)) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -070075 mRefCount.Init();
Stephen Hines4b32ffd2010-11-05 18:47:11 -070076 mRefCount.Visit(FD->getBody());
Stephen Hinescfae0f32010-11-01 18:57:31 -070077 }
78 return;
79}
80
81void 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++) {
Logan Chienab992e52011-07-20 22:06:52 +080087 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hinescfae0f32010-11-01 18:57:31 -070088 if (FD == NULL)
89 continue;
90 if (!FD->getName().startswith("rs")) // Check prefix
91 continue;
92 if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
Logan Chien9207a2e2011-10-21 15:39:28 +080093 mDiagEngine.Report(
94 clang::FullSourceLoc(FD->getLocation(), mSourceMgr),
95 mDiagEngine.getCustomDiagID(clang::DiagnosticsEngine::Error,
96 "invalid function name prefix, "
97 "\"rs\" is reserved: '%0'"))
98 << FD->getName();
Stephen Hinescfae0f32010-11-01 18:57:31 -070099 }
100 }
101
102 // Process any non-static function declarations
103 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) {
Logan Chienab992e52011-07-20 22:06:52 +0800104 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hineseb2eec92011-01-09 19:13:09 -0800105 if (FD && FD->isGlobal()) {
106 AnnotateFunction(FD);
107 }
Stephen Hinescfae0f32010-11-01 18:57:31 -0700108 }
109
110 Backend::HandleTopLevelDecl(D);
111 return;
112}
113
Stephen Hinesc97a3332010-11-30 15:31:08 -0800114namespace {
115
Stephen Hines78e69cb2011-04-22 15:03:19 -0700116static bool ValidateVarDecl(clang::VarDecl *VD) {
117 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;
127 if (!RSExportType::NormalizeType(T, TypeName, &C.getDiagnostics(), VD)) {
128 valid = false;
129 }
130 }
131 valid &= RSExportType::ValidateVarDecl(VD);
132
133 return valid;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800134}
135
Stephen Hines78e69cb2011-04-22 15:03:19 -0700136static bool ValidateASTContext(clang::ASTContext &C) {
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 Hines78e69cb2011-04-22 15:03:19 -0700144 if (VD && !ValidateVarDecl(VD)) {
145 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 Hines78e69cb2011-04-22 15:03:19 -0700157 if (!ValidateASTContext(C)) {
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
Logan Chien9207a2e2011-10-21 15:39:28 +0800164 mDiagEngine.Report(mDiagEngine.getCustomDiagID(
165 clang::DiagnosticsEngine::Error,
166 "Missing pragma for version in source file"));
Stephen Hines96ab06c2011-01-05 15:29:26 -0800167 } else if (version > 1) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800168 mDiagEngine.Report(mDiagEngine.getCustomDiagID(
169 clang::DiagnosticsEngine::Error,
170 "Pragma for version in source file must be set to 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
206 if (mRSOptimizationMetadata == NULL)
207 mRSOptimizationMetadata = M->getOrInsertNamedMetadata(OPTIMIZATION_LEVEL_MN);
208 mRSOptimizationMetadata->addOperand(
209 llvm::MDNode::get(mLLVMContext, OptimizationOption));
210
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700211 // Dump export variable info
212 if (mContext->hasExportVar()) {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800213 int slotCount = 0;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700214 if (mExportVarMetadata == NULL)
Zonr Chang68fc02c2010-10-13 19:09:19 +0800215 mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700216
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700217 llvm::SmallVector<llvm::Value*, 2> ExportVarInfo;
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800218
219 // We emit slot information (#rs_object_slots) for any reference counted
220 // RS type or pointer (which can also be bound).
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700221
zonr6315f762010-10-05 15:35:14 +0800222 for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(),
223 E = mContext->export_vars_end();
224 I != E;
225 I++) {
226 const RSExportVar *EV = *I;
227 const RSExportType *ET = EV->getType();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800228 bool countsAsRSObject = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700229
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700230 // Variable name
231 ExportVarInfo.push_back(
232 llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700233
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700234 // Type name
Zonr Changa65ec162010-10-17 01:53:05 +0800235 switch (ET->getClass()) {
236 case RSExportType::ExportClassPrimitive: {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800237 const RSExportPrimitiveType *PT =
238 static_cast<const RSExportPrimitiveType*>(ET);
Zonr Changa65ec162010-10-17 01:53:05 +0800239 ExportVarInfo.push_back(
240 llvm::MDString::get(
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800241 mLLVMContext, llvm::utostr_32(PT->getType())));
242 if (PT->isRSObjectType()) {
243 countsAsRSObject = true;
244 }
Zonr Changa65ec162010-10-17 01:53:05 +0800245 break;
246 }
247 case RSExportType::ExportClassPointer: {
248 ExportVarInfo.push_back(
249 llvm::MDString::get(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700250 mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
Zonr Changa65ec162010-10-17 01:53:05 +0800251 ->getPointeeType()->getName()).c_str()));
252 break;
253 }
254 case RSExportType::ExportClassMatrix: {
255 ExportVarInfo.push_back(
256 llvm::MDString::get(
257 mLLVMContext, llvm::utostr_32(
258 RSExportPrimitiveType::DataTypeRSMatrix2x2 +
259 static_cast<const RSExportMatrixType*>(ET)->getDim() - 2)));
260 break;
261 }
262 case RSExportType::ExportClassVector:
263 case RSExportType::ExportClassConstantArray:
264 case RSExportType::ExportClassRecord: {
265 ExportVarInfo.push_back(
266 llvm::MDString::get(mLLVMContext,
267 EV->getType()->getName().c_str()));
268 break;
269 }
270 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700271
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700272 mExportVarMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700273 llvm::MDNode::get(mLLVMContext, ExportVarInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700274 ExportVarInfo.clear();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800275
276 if (mRSObjectSlotsMetadata == NULL) {
277 mRSObjectSlotsMetadata =
278 M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN);
279 }
280
281 if (countsAsRSObject) {
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700282 mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
283 llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount))));
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800284 }
285
286 slotCount++;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700287 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700288 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700289
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700290 // Dump export function info
291 if (mContext->hasExportFunc()) {
292 if (mExportFuncMetadata == NULL)
293 mExportFuncMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800294 M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700295
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700296 llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700297
zonr6315f762010-10-05 15:35:14 +0800298 for (RSContext::const_export_func_iterator
299 I = mContext->export_funcs_begin(),
300 E = mContext->export_funcs_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700301 I != E;
302 I++) {
zonr6315f762010-10-05 15:35:14 +0800303 const RSExportFunc *EF = *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700304
305 // Function name
zonr6315f762010-10-05 15:35:14 +0800306 if (!EF->hasParam()) {
307 ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
308 EF->getName().c_str()));
309 } else {
Zonr Chang68fc02c2010-10-13 19:09:19 +0800310 llvm::Function *F = M->getFunction(EF->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700311 llvm::Function *HelperFunction;
312 const std::string HelperFunctionName(".helper_" + EF->getName());
313
Stephen Hines6e6578a2011-02-07 18:05:48 -0800314 slangAssert(F && "Function marked as exported disappeared in Bitcode");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700315
316 // Create helper function
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700317 {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800318 llvm::StructType *HelperFunctionParameterTy = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700319
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800320 if (!F->getArgumentList().empty()) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700321 std::vector<llvm::Type*> HelperFunctionParameterTys;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800322 for (llvm::Function::arg_iterator AI = F->arg_begin(),
323 AE = F->arg_end(); AI != AE; AI++)
324 HelperFunctionParameterTys.push_back(AI->getType());
325
326 HelperFunctionParameterTy =
Stephen Hinesa67e4452011-07-19 15:39:26 -0700327 llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800328 }
329
330 if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) {
331 fprintf(stderr, "Failed to export function %s: parameter type "
332 "mismatch during creation of helper function.\n",
333 EF->getName().c_str());
334
335 const RSExportRecordType *Expected = EF->getParamPacketType();
336 if (Expected) {
337 fprintf(stderr, "Expected:\n");
338 Expected->getLLVMType()->dump();
339 }
340 if (HelperFunctionParameterTy) {
341 fprintf(stderr, "Got:\n");
342 HelperFunctionParameterTy->dump();
343 }
344 }
345
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700346 std::vector<llvm::Type*> Params;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800347 if (HelperFunctionParameterTy) {
348 llvm::PointerType *HelperFunctionParameterTyP =
349 llvm::PointerType::getUnqual(HelperFunctionParameterTy);
350 Params.push_back(HelperFunctionParameterTyP);
351 }
352
353 llvm::FunctionType * HelperFunctionType =
354 llvm::FunctionType::get(F->getReturnType(),
Stephen Hinesa67e4452011-07-19 15:39:26 -0700355 Params,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800356 /* IsVarArgs = */false);
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700357
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700358 HelperFunction =
359 llvm::Function::Create(HelperFunctionType,
360 llvm::GlobalValue::ExternalLinkage,
361 HelperFunctionName,
Zonr Chang68fc02c2010-10-13 19:09:19 +0800362 M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700363
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700364 HelperFunction->addFnAttr(llvm::Attribute::NoInline);
365 HelperFunction->setCallingConv(F->getCallingConv());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700366
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700367 // Create helper function body
368 {
369 llvm::Argument *HelperFunctionParameter =
370 &(*HelperFunction->arg_begin());
371 llvm::BasicBlock *BB =
372 llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction);
373 llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB);
374 llvm::SmallVector<llvm::Value*, 6> Params;
375 llvm::Value *Idx[2];
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700376
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700377 Idx[0] =
378 llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700379
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700380 // getelementptr and load instruction for all elements in
381 // parameter .p
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800382 for (size_t i = 0; i < EF->getNumParameters(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700383 // getelementptr
Logan Chien9207a2e2011-10-21 15:39:28 +0800384 Idx[1] = llvm::ConstantInt::get(
385 llvm::Type::getInt32Ty(mLLVMContext), i);
386
387 llvm::Value *Ptr =
388 IB->CreateInBoundsGEP(HelperFunctionParameter, Idx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700389
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700390 // load
391 llvm::Value *V = IB->CreateLoad(Ptr);
392 Params.push_back(V);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700393 }
394
Stephen Hinesa67e4452011-07-19 15:39:26 -0700395 // Call and pass the all elements as parameter to F
396 llvm::CallInst *CI = IB->CreateCall(F, Params);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700397
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700398 CI->setCallingConv(F->getCallingConv());
399
400 if (F->getReturnType() == llvm::Type::getVoidTy(mLLVMContext))
401 IB->CreateRetVoid();
402 else
403 IB->CreateRet(CI);
404
405 delete IB;
406 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700407 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700408
409 ExportFuncInfo.push_back(
410 llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str()));
411 }
412
413 mExportFuncMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700414 llvm::MDNode::get(mLLVMContext, ExportFuncInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700415 ExportFuncInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700416 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700417 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700418
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700419 // Dump export function info
420 if (mContext->hasExportForEach()) {
421 if (mExportForEachMetadata == NULL)
422 mExportForEachMetadata =
423 M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_MN);
424
425 llvm::SmallVector<llvm::Value*, 1> ExportForEachInfo;
426
427 for (RSContext::const_export_foreach_iterator
428 I = mContext->export_foreach_begin(),
429 E = mContext->export_foreach_end();
430 I != E;
431 I++) {
432 const RSExportForEach *EFE = *I;
433
434 ExportForEachInfo.push_back(
435 llvm::MDString::get(mLLVMContext,
436 llvm::utostr_32(EFE->getMetadataEncoding())));
437
438 mExportForEachMetadata->addOperand(
439 llvm::MDNode::get(mLLVMContext, ExportForEachInfo));
440 ExportForEachInfo.clear();
441 }
442 }
443
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700444 // Dump export type info
445 if (mContext->hasExportType()) {
446 llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700447
zonr6315f762010-10-05 15:35:14 +0800448 for (RSContext::const_export_type_iterator
449 I = mContext->export_types_begin(),
450 E = mContext->export_types_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700451 I != E;
452 I++) {
453 // First, dump type name list to export
454 const RSExportType *ET = I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700455
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700456 ExportTypeInfo.clear();
457 // Type name
458 ExportTypeInfo.push_back(
459 llvm::MDString::get(mLLVMContext, ET->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700460
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700461 if (ET->getClass() == RSExportType::ExportClassRecord) {
462 const RSExportRecordType *ERT =
463 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700464
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700465 if (mExportTypeMetadata == NULL)
466 mExportTypeMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800467 M->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700468
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700469 mExportTypeMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700470 llvm::MDNode::get(mLLVMContext, ExportTypeInfo));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700471
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700472 // Now, export struct field information to %[struct name]
473 std::string StructInfoMetadataName("%");
474 StructInfoMetadataName.append(ET->getName());
475 llvm::NamedMDNode *StructInfoMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800476 M->getOrInsertNamedMetadata(StructInfoMetadataName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700477 llvm::SmallVector<llvm::Value*, 3> FieldInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700478
Stephen Hines6e6578a2011-02-07 18:05:48 -0800479 slangAssert(StructInfoMetadata->getNumOperands() == 0 &&
480 "Metadata with same name was created before");
zonr6315f762010-10-05 15:35:14 +0800481 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
482 FE = ERT->fields_end();
483 FI != FE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700484 FI++) {
485 const RSExportRecordType::Field *F = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700486
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700487 // 1. field name
488 FieldInfo.push_back(llvm::MDString::get(mLLVMContext,
489 F->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700490
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700491 // 2. field type name
492 FieldInfo.push_back(
493 llvm::MDString::get(mLLVMContext,
494 F->getType()->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700495
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700496 // 3. field kind
497 switch (F->getType()->getClass()) {
498 case RSExportType::ExportClassPrimitive:
499 case RSExportType::ExportClassVector: {
500 const RSExportPrimitiveType *EPT =
501 static_cast<const RSExportPrimitiveType*>(F->getType());
502 FieldInfo.push_back(
503 llvm::MDString::get(mLLVMContext,
504 llvm::itostr(EPT->getKind())));
505 break;
506 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700507
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700508 default: {
509 FieldInfo.push_back(
510 llvm::MDString::get(mLLVMContext,
511 llvm::itostr(
zonr6315f762010-10-05 15:35:14 +0800512 RSExportPrimitiveType::DataKindUser)));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700513 break;
514 }
515 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700516
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700517 StructInfoMetadata->addOperand(
518 llvm::MDNode::get(mLLVMContext, FieldInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700519 FieldInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700520 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700521 } // ET->getClass() == RSExportType::ExportClassRecord
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700522 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700523 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700524
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700525 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700526}
527
528RSBackend::~RSBackend() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700529 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700530}
Stephen Hinese639eb52010-11-08 19:27:20 -0800531
532} // namespace slang