blob: e7a6bb98e55ebfbab1015b1804fc2ce587a571f4 [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,
Stephen Hinese639eb52010-11-08 19:27:20 -080046 clang::Diagnostic *Diags,
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 Hines2e35b132011-07-22 02:50:19 -070053 bool AllowRSPrefix,
54 unsigned int TargetAPI)
zonr6315f762010-10-05 15:35:14 +080055 : Backend(Diags,
56 CodeGenOpts,
57 TargetOpts,
58 Pragmas,
59 OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080060 OT),
zonr6315f762010-10-05 15:35:14 +080061 mContext(Context),
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080062 mSourceMgr(SourceMgr),
63 mAllowRSPrefix(AllowRSPrefix),
Stephen Hines2e35b132011-07-22 02:50:19 -070064 mTargetAPI(TargetAPI),
zonr6315f762010-10-05 15:35:14 +080065 mExportVarMetadata(NULL),
66 mExportFuncMetadata(NULL),
Stephen Hines4ccf75e2011-08-16 18:21:01 -070067 mExportForEachMetadata(NULL),
Stephen Hinesb3a12fe2011-01-26 20:16:38 -080068 mExportTypeMetadata(NULL),
Stephen Hinesd0b5edd2011-04-18 16:38:03 -070069 mRSObjectSlotsMetadata(NULL),
70 mRefCount(mContext->getASTContext()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070071 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070072}
73
Stephen Hinescfae0f32010-11-01 18:57:31 -070074// 1) Add zero initialization of local RS object types
75void RSBackend::AnnotateFunction(clang::FunctionDecl *FD) {
76 if (FD &&
77 FD->hasBody() &&
78 !SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr)) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -070079 mRefCount.Init();
Stephen Hines4b32ffd2010-11-05 18:47:11 -070080 mRefCount.Visit(FD->getBody());
Stephen Hinescfae0f32010-11-01 18:57:31 -070081 }
82 return;
83}
84
85void RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) {
86 // Disallow user-defined functions with prefix "rs"
87 if (!mAllowRSPrefix) {
88 // Iterate all function declarations in the program.
89 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end();
90 I != E; I++) {
Logan Chienab992e52011-07-20 22:06:52 +080091 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hinescfae0f32010-11-01 18:57:31 -070092 if (FD == NULL)
93 continue;
94 if (!FD->getName().startswith("rs")) // Check prefix
95 continue;
96 if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
97 mDiags.Report(clang::FullSourceLoc(FD->getLocation(), mSourceMgr),
98 mDiags.getCustomDiagID(clang::Diagnostic::Error,
99 "invalid function name prefix, "
100 "\"rs\" is reserved: '%0'"))
101 << FD->getName();
102 }
103 }
104
105 // Process any non-static function declarations
106 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) {
Logan Chienab992e52011-07-20 22:06:52 +0800107 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hineseb2eec92011-01-09 19:13:09 -0800108 if (FD && FD->isGlobal()) {
109 AnnotateFunction(FD);
110 }
Stephen Hinescfae0f32010-11-01 18:57:31 -0700111 }
112
113 Backend::HandleTopLevelDecl(D);
114 return;
115}
116
Stephen Hinesc97a3332010-11-30 15:31:08 -0800117namespace {
118
Stephen Hines78e69cb2011-04-22 15:03:19 -0700119static bool ValidateVarDecl(clang::VarDecl *VD) {
120 if (!VD) {
121 return true;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800122 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700123
124 clang::ASTContext &C = VD->getASTContext();
125 const clang::Type *T = VD->getType().getTypePtr();
126 bool valid = true;
127
128 if (VD->getLinkage() == clang::ExternalLinkage) {
129 llvm::StringRef TypeName;
130 if (!RSExportType::NormalizeType(T, TypeName, &C.getDiagnostics(), VD)) {
131 valid = false;
132 }
133 }
134 valid &= RSExportType::ValidateVarDecl(VD);
135
136 return valid;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800137}
138
Stephen Hines78e69cb2011-04-22 15:03:19 -0700139static bool ValidateASTContext(clang::ASTContext &C) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800140 bool valid = true;
Stephen Hinesfcda2352010-10-19 16:49:32 -0700141 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
Stephen Hinesc97a3332010-11-30 15:31:08 -0800142 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
143 DE = TUDecl->decls_end();
144 DI != DE;
145 DI++) {
Logan Chienab992e52011-07-20 22:06:52 +0800146 clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*DI);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700147 if (VD && !ValidateVarDecl(VD)) {
148 valid = false;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800149 }
150 }
151
152 return valid;
153}
154
Stephen Hinese5e64432010-12-02 18:48:20 -0800155} // namespace
Stephen Hinesc97a3332010-11-30 15:31:08 -0800156
157void RSBackend::HandleTranslationUnitPre(clang::ASTContext &C) {
158 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
159
Stephen Hines78e69cb2011-04-22 15:03:19 -0700160 if (!ValidateASTContext(C)) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800161 return;
162 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700163
Stephen Hines96ab06c2011-01-05 15:29:26 -0800164 int version = mContext->getVersion();
165 if (version == 0) {
166 // Not setting a version is an error
167 mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
168 "Missing pragma for version in source file"));
169 } else if (version > 1) {
170 mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
171 "Pragma for version in source file must be set to 1"));
172 }
173
Stephen Hinescfae0f32010-11-01 18:57:31 -0700174 // Process any static function declarations
Stephen Hinesfcda2352010-10-19 16:49:32 -0700175 for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
176 E = TUDecl->decls_end(); I != E; I++) {
177 if ((I->getKind() >= clang::Decl::firstFunction) &&
178 (I->getKind() <= clang::Decl::lastFunction)) {
Logan Chienab992e52011-07-20 22:06:52 +0800179 clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(*I);
Stephen Hineseb2eec92011-01-09 19:13:09 -0800180 if (FD && !FD->isGlobal()) {
181 AnnotateFunction(FD);
182 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700183 }
184 }
185
186 return;
187}
188
189///////////////////////////////////////////////////////////////////////////////
Zonr Chang68fc02c2010-10-13 19:09:19 +0800190void RSBackend::HandleTranslationUnitPost(llvm::Module *M) {
Stephen Hinesc808a992010-11-29 17:20:42 -0800191 if (!mContext->processExport()) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800192 return;
Stephen Hinesc808a992010-11-29 17:20:42 -0800193 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700194
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700195 // Dump export variable info
196 if (mContext->hasExportVar()) {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800197 int slotCount = 0;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700198 if (mExportVarMetadata == NULL)
Zonr Chang68fc02c2010-10-13 19:09:19 +0800199 mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700200
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700201 llvm::SmallVector<llvm::Value*, 2> ExportVarInfo;
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800202
203 // We emit slot information (#rs_object_slots) for any reference counted
204 // RS type or pointer (which can also be bound).
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700205
zonr6315f762010-10-05 15:35:14 +0800206 for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(),
207 E = mContext->export_vars_end();
208 I != E;
209 I++) {
210 const RSExportVar *EV = *I;
211 const RSExportType *ET = EV->getType();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800212 bool countsAsRSObject = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700213
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700214 // Variable name
215 ExportVarInfo.push_back(
216 llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700217
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700218 // Type name
Zonr Changa65ec162010-10-17 01:53:05 +0800219 switch (ET->getClass()) {
220 case RSExportType::ExportClassPrimitive: {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800221 const RSExportPrimitiveType *PT =
222 static_cast<const RSExportPrimitiveType*>(ET);
Zonr Changa65ec162010-10-17 01:53:05 +0800223 ExportVarInfo.push_back(
224 llvm::MDString::get(
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800225 mLLVMContext, llvm::utostr_32(PT->getType())));
226 if (PT->isRSObjectType()) {
227 countsAsRSObject = true;
228 }
Zonr Changa65ec162010-10-17 01:53:05 +0800229 break;
230 }
231 case RSExportType::ExportClassPointer: {
232 ExportVarInfo.push_back(
233 llvm::MDString::get(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700234 mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
Zonr Changa65ec162010-10-17 01:53:05 +0800235 ->getPointeeType()->getName()).c_str()));
236 break;
237 }
238 case RSExportType::ExportClassMatrix: {
239 ExportVarInfo.push_back(
240 llvm::MDString::get(
241 mLLVMContext, llvm::utostr_32(
242 RSExportPrimitiveType::DataTypeRSMatrix2x2 +
243 static_cast<const RSExportMatrixType*>(ET)->getDim() - 2)));
244 break;
245 }
246 case RSExportType::ExportClassVector:
247 case RSExportType::ExportClassConstantArray:
248 case RSExportType::ExportClassRecord: {
249 ExportVarInfo.push_back(
250 llvm::MDString::get(mLLVMContext,
251 EV->getType()->getName().c_str()));
252 break;
253 }
254 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700255
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700256 mExportVarMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700257 llvm::MDNode::get(mLLVMContext, ExportVarInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700258 ExportVarInfo.clear();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800259
260 if (mRSObjectSlotsMetadata == NULL) {
261 mRSObjectSlotsMetadata =
262 M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN);
263 }
264
265 if (countsAsRSObject) {
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700266 mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
267 llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount))));
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800268 }
269
270 slotCount++;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700271 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700272 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700273
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700274 // Dump export function info
275 if (mContext->hasExportFunc()) {
276 if (mExportFuncMetadata == NULL)
277 mExportFuncMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800278 M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700279
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700280 llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700281
zonr6315f762010-10-05 15:35:14 +0800282 for (RSContext::const_export_func_iterator
283 I = mContext->export_funcs_begin(),
284 E = mContext->export_funcs_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700285 I != E;
286 I++) {
zonr6315f762010-10-05 15:35:14 +0800287 const RSExportFunc *EF = *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700288
289 // Function name
zonr6315f762010-10-05 15:35:14 +0800290 if (!EF->hasParam()) {
291 ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
292 EF->getName().c_str()));
293 } else {
Zonr Chang68fc02c2010-10-13 19:09:19 +0800294 llvm::Function *F = M->getFunction(EF->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700295 llvm::Function *HelperFunction;
296 const std::string HelperFunctionName(".helper_" + EF->getName());
297
Stephen Hines6e6578a2011-02-07 18:05:48 -0800298 slangAssert(F && "Function marked as exported disappeared in Bitcode");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700299
300 // Create helper function
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700301 {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800302 llvm::StructType *HelperFunctionParameterTy = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700303
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800304 if (!F->getArgumentList().empty()) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700305 std::vector<llvm::Type*> HelperFunctionParameterTys;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800306 for (llvm::Function::arg_iterator AI = F->arg_begin(),
307 AE = F->arg_end(); AI != AE; AI++)
308 HelperFunctionParameterTys.push_back(AI->getType());
309
310 HelperFunctionParameterTy =
Stephen Hinesa67e4452011-07-19 15:39:26 -0700311 llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys);
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800312 }
313
314 if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) {
315 fprintf(stderr, "Failed to export function %s: parameter type "
316 "mismatch during creation of helper function.\n",
317 EF->getName().c_str());
318
319 const RSExportRecordType *Expected = EF->getParamPacketType();
320 if (Expected) {
321 fprintf(stderr, "Expected:\n");
322 Expected->getLLVMType()->dump();
323 }
324 if (HelperFunctionParameterTy) {
325 fprintf(stderr, "Got:\n");
326 HelperFunctionParameterTy->dump();
327 }
328 }
329
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700330 std::vector<llvm::Type*> Params;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800331 if (HelperFunctionParameterTy) {
332 llvm::PointerType *HelperFunctionParameterTyP =
333 llvm::PointerType::getUnqual(HelperFunctionParameterTy);
334 Params.push_back(HelperFunctionParameterTyP);
335 }
336
337 llvm::FunctionType * HelperFunctionType =
338 llvm::FunctionType::get(F->getReturnType(),
Stephen Hinesa67e4452011-07-19 15:39:26 -0700339 Params,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800340 /* IsVarArgs = */false);
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700341
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700342 HelperFunction =
343 llvm::Function::Create(HelperFunctionType,
344 llvm::GlobalValue::ExternalLinkage,
345 HelperFunctionName,
Zonr Chang68fc02c2010-10-13 19:09:19 +0800346 M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700347
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700348 HelperFunction->addFnAttr(llvm::Attribute::NoInline);
349 HelperFunction->setCallingConv(F->getCallingConv());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700350
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700351 // Create helper function body
352 {
353 llvm::Argument *HelperFunctionParameter =
354 &(*HelperFunction->arg_begin());
355 llvm::BasicBlock *BB =
356 llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction);
357 llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB);
358 llvm::SmallVector<llvm::Value*, 6> Params;
359 llvm::Value *Idx[2];
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700360
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700361 Idx[0] =
362 llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700363
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700364 // getelementptr and load instruction for all elements in
365 // parameter .p
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800366 for (size_t i = 0; i < EF->getNumParameters(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700367 // getelementptr
368 Idx[1] =
369 llvm::ConstantInt::get(
370 llvm::Type::getInt32Ty(mLLVMContext), i);
371 llvm::Value *Ptr = IB->CreateInBoundsGEP(HelperFunctionParameter,
372 Idx,
373 Idx + 2);
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