blob: 7c749b9801e0236c2f7095466208e5cedeb8921b [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
22#include "llvm/ADT/Twine.h"
23#include "llvm/ADT/StringExtras.h"
24
Stephen Hinese639eb52010-11-08 19:27:20 -080025#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 Hines6e6578a2011-02-07 18:05:48 -080034#include "slang_assert.h"
Zonr Chang592a9542010-10-07 20:03:58 +080035#include "slang_rs.h"
zonr6315f762010-10-05 15:35:14 +080036#include "slang_rs_context.h"
Stephen Hines4ccf75e2011-08-16 18:21:01 -070037#include "slang_rs_export_foreach.h"
zonr6315f762010-10-05 15:35:14 +080038#include "slang_rs_export_func.h"
39#include "slang_rs_export_type.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080040#include "slang_rs_export_var.h"
41#include "slang_rs_metadata.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070042
Stephen Hinese639eb52010-11-08 19:27:20 -080043namespace slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044
45RSBackend::RSBackend(RSContext *Context,
Logan Chien9207a2e2011-10-21 15:39:28 +080046 clang::DiagnosticsEngine *DiagEngine,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047 const clang::CodeGenOptions &CodeGenOpts,
48 const clang::TargetOptions &TargetOpts,
Stephen Hines3fd0a942011-01-18 12:27:39 -080049 PragmaList *Pragmas,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080051 Slang::OutputType OT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070052 clang::SourceManager &SourceMgr,
Stephen Hines4a4bf922011-08-18 17:20:33 -070053 bool AllowRSPrefix)
Logan Chien9207a2e2011-10-21 15:39:28 +080054 : Backend(DiagEngine, CodeGenOpts, TargetOpts, Pragmas, OS, OT),
55 mContext(Context),
56 mSourceMgr(SourceMgr),
57 mAllowRSPrefix(AllowRSPrefix),
58 mExportVarMetadata(NULL),
59 mExportFuncMetadata(NULL),
60 mExportForEachMetadata(NULL),
61 mExportTypeMetadata(NULL),
62 mRSObjectSlotsMetadata(NULL),
63 mRefCount(mContext->getASTContext()) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064}
65
Stephen Hinescfae0f32010-11-01 18:57:31 -070066// 1) Add zero initialization of local RS object types
67void RSBackend::AnnotateFunction(clang::FunctionDecl *FD) {
68 if (FD &&
69 FD->hasBody() &&
70 !SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr)) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -070071 mRefCount.Init();
Stephen Hines4b32ffd2010-11-05 18:47:11 -070072 mRefCount.Visit(FD->getBody());
Stephen Hinescfae0f32010-11-01 18:57:31 -070073 }
74 return;
75}
76
Logan Chienfa6ef562011-11-25 13:50:02 +080077bool RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) {
Stephen Hinescfae0f32010-11-01 18:57:31 -070078 // Disallow user-defined functions with prefix "rs"
79 if (!mAllowRSPrefix) {
80 // Iterate all function declarations in the program.
81 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end();
82 I != E; I++) {
Logan Chienab992e52011-07-20 22:06:52 +080083 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hinescfae0f32010-11-01 18:57:31 -070084 if (FD == NULL)
85 continue;
86 if (!FD->getName().startswith("rs")) // Check prefix
87 continue;
88 if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
Logan Chien9207a2e2011-10-21 15:39:28 +080089 mDiagEngine.Report(
90 clang::FullSourceLoc(FD->getLocation(), mSourceMgr),
91 mDiagEngine.getCustomDiagID(clang::DiagnosticsEngine::Error,
92 "invalid function name prefix, "
93 "\"rs\" is reserved: '%0'"))
94 << FD->getName();
Stephen Hinescfae0f32010-11-01 18:57:31 -070095 }
96 }
97
98 // Process any non-static function declarations
99 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) {
Logan Chienab992e52011-07-20 22:06:52 +0800100 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hineseb2eec92011-01-09 19:13:09 -0800101 if (FD && FD->isGlobal()) {
102 AnnotateFunction(FD);
103 }
Stephen Hinescfae0f32010-11-01 18:57:31 -0700104 }
105
Logan Chienfa6ef562011-11-25 13:50:02 +0800106 return Backend::HandleTopLevelDecl(D);
Stephen Hinescfae0f32010-11-01 18:57:31 -0700107}
108
Stephen Hinesc97a3332010-11-30 15:31:08 -0800109namespace {
110
Stephen Hines78e69cb2011-04-22 15:03:19 -0700111static bool ValidateVarDecl(clang::VarDecl *VD) {
112 if (!VD) {
113 return true;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800114 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700115
116 clang::ASTContext &C = VD->getASTContext();
117 const clang::Type *T = VD->getType().getTypePtr();
118 bool valid = true;
119
120 if (VD->getLinkage() == clang::ExternalLinkage) {
121 llvm::StringRef TypeName;
122 if (!RSExportType::NormalizeType(T, TypeName, &C.getDiagnostics(), VD)) {
123 valid = false;
124 }
125 }
126 valid &= RSExportType::ValidateVarDecl(VD);
127
128 return valid;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800129}
130
Stephen Hines78e69cb2011-04-22 15:03:19 -0700131static bool ValidateASTContext(clang::ASTContext &C) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800132 bool valid = true;
Stephen Hinesfcda2352010-10-19 16:49:32 -0700133 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
Stephen Hinesc97a3332010-11-30 15:31:08 -0800134 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
135 DE = TUDecl->decls_end();
136 DI != DE;
137 DI++) {
Logan Chienab992e52011-07-20 22:06:52 +0800138 clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*DI);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700139 if (VD && !ValidateVarDecl(VD)) {
140 valid = false;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800141 }
142 }
143
144 return valid;
145}
146
Stephen Hinese5e64432010-12-02 18:48:20 -0800147} // namespace
Stephen Hinesc97a3332010-11-30 15:31:08 -0800148
149void RSBackend::HandleTranslationUnitPre(clang::ASTContext &C) {
150 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
151
Stephen Hines78e69cb2011-04-22 15:03:19 -0700152 if (!ValidateASTContext(C)) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800153 return;
154 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700155
Stephen Hines96ab06c2011-01-05 15:29:26 -0800156 int version = mContext->getVersion();
157 if (version == 0) {
158 // Not setting a version is an error
Logan Chien9207a2e2011-10-21 15:39:28 +0800159 mDiagEngine.Report(mDiagEngine.getCustomDiagID(
160 clang::DiagnosticsEngine::Error,
161 "Missing pragma for version in source file"));
Stephen Hines96ab06c2011-01-05 15:29:26 -0800162 } else if (version > 1) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800163 mDiagEngine.Report(mDiagEngine.getCustomDiagID(
164 clang::DiagnosticsEngine::Error,
165 "Pragma for version in source file must be set to 1"));
Stephen Hines96ab06c2011-01-05 15:29:26 -0800166 }
167
Stephen Hines688e64b2011-08-23 16:01:25 -0700168 // Create a static global destructor if necessary (to handle RS object
169 // runtime cleanup).
170 clang::FunctionDecl *FD = mRefCount.CreateStaticGlobalDtor();
171 if (FD) {
172 HandleTopLevelDecl(clang::DeclGroupRef(FD));
173 }
174
Stephen Hinescfae0f32010-11-01 18:57:31 -0700175 // Process any static function declarations
Stephen Hinesfcda2352010-10-19 16:49:32 -0700176 for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
177 E = TUDecl->decls_end(); I != E; I++) {
178 if ((I->getKind() >= clang::Decl::firstFunction) &&
179 (I->getKind() <= clang::Decl::lastFunction)) {
Logan Chienab992e52011-07-20 22:06:52 +0800180 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hineseb2eec92011-01-09 19:13:09 -0800181 if (FD && !FD->isGlobal()) {
182 AnnotateFunction(FD);
183 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700184 }
185 }
186
187 return;
188}
189
190///////////////////////////////////////////////////////////////////////////////
Zonr Chang68fc02c2010-10-13 19:09:19 +0800191void RSBackend::HandleTranslationUnitPost(llvm::Module *M) {
Stephen Hinesc808a992010-11-29 17:20:42 -0800192 if (!mContext->processExport()) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800193 return;
Stephen Hinesc808a992010-11-29 17:20:42 -0800194 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700195
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700196 // Dump export variable info
197 if (mContext->hasExportVar()) {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800198 int slotCount = 0;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700199 if (mExportVarMetadata == NULL)
Zonr Chang68fc02c2010-10-13 19:09:19 +0800200 mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700201
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700202 llvm::SmallVector<llvm::Value*, 2> ExportVarInfo;
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800203
204 // We emit slot information (#rs_object_slots) for any reference counted
205 // RS type or pointer (which can also be bound).
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700206
zonr6315f762010-10-05 15:35:14 +0800207 for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(),
208 E = mContext->export_vars_end();
209 I != E;
210 I++) {
211 const RSExportVar *EV = *I;
212 const RSExportType *ET = EV->getType();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800213 bool countsAsRSObject = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700214
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700215 // Variable name
216 ExportVarInfo.push_back(
217 llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700218
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700219 // Type name
Zonr Changa65ec162010-10-17 01:53:05 +0800220 switch (ET->getClass()) {
221 case RSExportType::ExportClassPrimitive: {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800222 const RSExportPrimitiveType *PT =
223 static_cast<const RSExportPrimitiveType*>(ET);
Zonr Changa65ec162010-10-17 01:53:05 +0800224 ExportVarInfo.push_back(
225 llvm::MDString::get(
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800226 mLLVMContext, llvm::utostr_32(PT->getType())));
227 if (PT->isRSObjectType()) {
228 countsAsRSObject = true;
229 }
Zonr Changa65ec162010-10-17 01:53:05 +0800230 break;
231 }
232 case RSExportType::ExportClassPointer: {
233 ExportVarInfo.push_back(
234 llvm::MDString::get(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700235 mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
Zonr Changa65ec162010-10-17 01:53:05 +0800236 ->getPointeeType()->getName()).c_str()));
237 break;
238 }
239 case RSExportType::ExportClassMatrix: {
240 ExportVarInfo.push_back(
241 llvm::MDString::get(
242 mLLVMContext, llvm::utostr_32(
243 RSExportPrimitiveType::DataTypeRSMatrix2x2 +
244 static_cast<const RSExportMatrixType*>(ET)->getDim() - 2)));
245 break;
246 }
247 case RSExportType::ExportClassVector:
248 case RSExportType::ExportClassConstantArray:
249 case RSExportType::ExportClassRecord: {
250 ExportVarInfo.push_back(
251 llvm::MDString::get(mLLVMContext,
252 EV->getType()->getName().c_str()));
253 break;
254 }
255 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700256
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700257 mExportVarMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700258 llvm::MDNode::get(mLLVMContext, ExportVarInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700259 ExportVarInfo.clear();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800260
261 if (mRSObjectSlotsMetadata == NULL) {
262 mRSObjectSlotsMetadata =
263 M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN);
264 }
265
266 if (countsAsRSObject) {
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700267 mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
268 llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount))));
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800269 }
270
271 slotCount++;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700272 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700273 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700274
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700275 // Dump export function info
276 if (mContext->hasExportFunc()) {
277 if (mExportFuncMetadata == NULL)
278 mExportFuncMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800279 M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700280
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700281 llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700282
zonr6315f762010-10-05 15:35:14 +0800283 for (RSContext::const_export_func_iterator
284 I = mContext->export_funcs_begin(),
285 E = mContext->export_funcs_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700286 I != E;
287 I++) {
zonr6315f762010-10-05 15:35:14 +0800288 const RSExportFunc *EF = *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700289
290 // Function name
zonr6315f762010-10-05 15:35:14 +0800291 if (!EF->hasParam()) {
292 ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
293 EF->getName().c_str()));
294 } else {
Zonr Chang68fc02c2010-10-13 19:09:19 +0800295 llvm::Function *F = M->getFunction(EF->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700296 llvm::Function *HelperFunction;
297 const std::string HelperFunctionName(".helper_" + EF->getName());
298
Stephen Hines6e6578a2011-02-07 18:05:48 -0800299 slangAssert(F && "Function marked as exported disappeared in Bitcode");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700300
301 // Create helper function
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700302 {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800303 llvm::StructType *HelperFunctionParameterTy = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700304
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800305 if (!F->getArgumentList().empty()) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700306 std::vector<llvm::Type*> HelperFunctionParameterTys;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800307 for (llvm::Function::arg_iterator AI = F->arg_begin(),
308 AE = F->arg_end(); AI != AE; AI++)
309 HelperFunctionParameterTys.push_back(AI->getType());
310
311 HelperFunctionParameterTy =
Stephen Hinesa67e4452011-07-19 15:39:26 -0700312 llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800313 }
314
315 if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) {
316 fprintf(stderr, "Failed to export function %s: parameter type "
317 "mismatch during creation of helper function.\n",
318 EF->getName().c_str());
319
320 const RSExportRecordType *Expected = EF->getParamPacketType();
321 if (Expected) {
322 fprintf(stderr, "Expected:\n");
323 Expected->getLLVMType()->dump();
324 }
325 if (HelperFunctionParameterTy) {
326 fprintf(stderr, "Got:\n");
327 HelperFunctionParameterTy->dump();
328 }
329 }
330
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700331 std::vector<llvm::Type*> Params;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800332 if (HelperFunctionParameterTy) {
333 llvm::PointerType *HelperFunctionParameterTyP =
334 llvm::PointerType::getUnqual(HelperFunctionParameterTy);
335 Params.push_back(HelperFunctionParameterTyP);
336 }
337
338 llvm::FunctionType * HelperFunctionType =
339 llvm::FunctionType::get(F->getReturnType(),
Stephen Hinesa67e4452011-07-19 15:39:26 -0700340 Params,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800341 /* IsVarArgs = */false);
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700342
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700343 HelperFunction =
344 llvm::Function::Create(HelperFunctionType,
345 llvm::GlobalValue::ExternalLinkage,
346 HelperFunctionName,
Zonr Chang68fc02c2010-10-13 19:09:19 +0800347 M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700348
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700349 HelperFunction->addFnAttr(llvm::Attribute::NoInline);
350 HelperFunction->setCallingConv(F->getCallingConv());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700351
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700352 // Create helper function body
353 {
354 llvm::Argument *HelperFunctionParameter =
355 &(*HelperFunction->arg_begin());
356 llvm::BasicBlock *BB =
357 llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction);
358 llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB);
359 llvm::SmallVector<llvm::Value*, 6> Params;
360 llvm::Value *Idx[2];
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700361
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700362 Idx[0] =
363 llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700364
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700365 // getelementptr and load instruction for all elements in
366 // parameter .p
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800367 for (size_t i = 0; i < EF->getNumParameters(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700368 // getelementptr
Logan Chien9207a2e2011-10-21 15:39:28 +0800369 Idx[1] = llvm::ConstantInt::get(
370 llvm::Type::getInt32Ty(mLLVMContext), i);
371
372 llvm::Value *Ptr =
373 IB->CreateInBoundsGEP(HelperFunctionParameter, Idx);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700374
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700375 // load
376 llvm::Value *V = IB->CreateLoad(Ptr);
377 Params.push_back(V);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700378 }
379
Stephen Hinesa67e4452011-07-19 15:39:26 -0700380 // Call and pass the all elements as parameter to F
381 llvm::CallInst *CI = IB->CreateCall(F, Params);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700382
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700383 CI->setCallingConv(F->getCallingConv());
384
385 if (F->getReturnType() == llvm::Type::getVoidTy(mLLVMContext))
386 IB->CreateRetVoid();
387 else
388 IB->CreateRet(CI);
389
390 delete IB;
391 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700392 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700393
394 ExportFuncInfo.push_back(
395 llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str()));
396 }
397
398 mExportFuncMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700399 llvm::MDNode::get(mLLVMContext, ExportFuncInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700400 ExportFuncInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700401 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700402 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700403
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700404 // Dump export function info
405 if (mContext->hasExportForEach()) {
406 if (mExportForEachMetadata == NULL)
407 mExportForEachMetadata =
408 M->getOrInsertNamedMetadata(RS_EXPORT_FOREACH_MN);
409
410 llvm::SmallVector<llvm::Value*, 1> ExportForEachInfo;
411
412 for (RSContext::const_export_foreach_iterator
413 I = mContext->export_foreach_begin(),
414 E = mContext->export_foreach_end();
415 I != E;
416 I++) {
417 const RSExportForEach *EFE = *I;
418
419 ExportForEachInfo.push_back(
420 llvm::MDString::get(mLLVMContext,
421 llvm::utostr_32(EFE->getMetadataEncoding())));
422
423 mExportForEachMetadata->addOperand(
424 llvm::MDNode::get(mLLVMContext, ExportForEachInfo));
425 ExportForEachInfo.clear();
426 }
427 }
428
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700429 // Dump export type info
430 if (mContext->hasExportType()) {
431 llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700432
zonr6315f762010-10-05 15:35:14 +0800433 for (RSContext::const_export_type_iterator
434 I = mContext->export_types_begin(),
435 E = mContext->export_types_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700436 I != E;
437 I++) {
438 // First, dump type name list to export
439 const RSExportType *ET = I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700440
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700441 ExportTypeInfo.clear();
442 // Type name
443 ExportTypeInfo.push_back(
444 llvm::MDString::get(mLLVMContext, ET->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700445
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700446 if (ET->getClass() == RSExportType::ExportClassRecord) {
447 const RSExportRecordType *ERT =
448 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700449
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700450 if (mExportTypeMetadata == NULL)
451 mExportTypeMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800452 M->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700453
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700454 mExportTypeMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700455 llvm::MDNode::get(mLLVMContext, ExportTypeInfo));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700456
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700457 // Now, export struct field information to %[struct name]
458 std::string StructInfoMetadataName("%");
459 StructInfoMetadataName.append(ET->getName());
460 llvm::NamedMDNode *StructInfoMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800461 M->getOrInsertNamedMetadata(StructInfoMetadataName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700462 llvm::SmallVector<llvm::Value*, 3> FieldInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700463
Stephen Hines6e6578a2011-02-07 18:05:48 -0800464 slangAssert(StructInfoMetadata->getNumOperands() == 0 &&
465 "Metadata with same name was created before");
zonr6315f762010-10-05 15:35:14 +0800466 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
467 FE = ERT->fields_end();
468 FI != FE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700469 FI++) {
470 const RSExportRecordType::Field *F = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700471
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700472 // 1. field name
473 FieldInfo.push_back(llvm::MDString::get(mLLVMContext,
474 F->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700475
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700476 // 2. field type name
477 FieldInfo.push_back(
478 llvm::MDString::get(mLLVMContext,
479 F->getType()->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700480
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700481 // 3. field kind
482 switch (F->getType()->getClass()) {
483 case RSExportType::ExportClassPrimitive:
484 case RSExportType::ExportClassVector: {
485 const RSExportPrimitiveType *EPT =
486 static_cast<const RSExportPrimitiveType*>(F->getType());
487 FieldInfo.push_back(
488 llvm::MDString::get(mLLVMContext,
489 llvm::itostr(EPT->getKind())));
490 break;
491 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700492
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700493 default: {
494 FieldInfo.push_back(
495 llvm::MDString::get(mLLVMContext,
496 llvm::itostr(
zonr6315f762010-10-05 15:35:14 +0800497 RSExportPrimitiveType::DataKindUser)));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700498 break;
499 }
500 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700501
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700502 StructInfoMetadata->addOperand(
503 llvm::MDNode::get(mLLVMContext, FieldInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700504 FieldInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700505 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700506 } // ET->getClass() == RSExportType::ExportClassRecord
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700507 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700508 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700509
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700510 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700511}
512
513RSBackend::~RSBackend() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700514 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700515}
Stephen Hinese639eb52010-11-08 19:27:20 -0800516
517} // namespace slang