blob: 6459d14306b10a707859111b4e8ad951b4332c7e [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"
zonr6315f762010-10-05 15:35:14 +080037#include "slang_rs_export_func.h"
38#include "slang_rs_export_type.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080039#include "slang_rs_export_var.h"
40#include "slang_rs_metadata.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070041
Stephen Hinese639eb52010-11-08 19:27:20 -080042namespace slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070043
44RSBackend::RSBackend(RSContext *Context,
Stephen Hinese639eb52010-11-08 19:27:20 -080045 clang::Diagnostic *Diags,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070046 const clang::CodeGenOptions &CodeGenOpts,
47 const clang::TargetOptions &TargetOpts,
Stephen Hines3fd0a942011-01-18 12:27:39 -080048 PragmaList *Pragmas,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049 llvm::raw_ostream *OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080050 Slang::OutputType OT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 clang::SourceManager &SourceMgr,
zonr6315f762010-10-05 15:35:14 +080052 bool AllowRSPrefix)
53 : Backend(Diags,
54 CodeGenOpts,
55 TargetOpts,
56 Pragmas,
57 OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080058 OT),
zonr6315f762010-10-05 15:35:14 +080059 mContext(Context),
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080060 mSourceMgr(SourceMgr),
61 mAllowRSPrefix(AllowRSPrefix),
zonr6315f762010-10-05 15:35:14 +080062 mExportVarMetadata(NULL),
63 mExportFuncMetadata(NULL),
Stephen Hinesb3a12fe2011-01-26 20:16:38 -080064 mExportTypeMetadata(NULL),
Stephen Hinesd0b5edd2011-04-18 16:38:03 -070065 mRSObjectSlotsMetadata(NULL),
66 mRefCount(mContext->getASTContext()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070067 return;
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++) {
87 clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I);
88 if (FD == NULL)
89 continue;
90 if (!FD->getName().startswith("rs")) // Check prefix
91 continue;
92 if (!SlangRS::IsFunctionInRSHeaderFile(FD, mSourceMgr))
93 mDiags.Report(clang::FullSourceLoc(FD->getLocation(), mSourceMgr),
94 mDiags.getCustomDiagID(clang::Diagnostic::Error,
95 "invalid function name prefix, "
96 "\"rs\" is reserved: '%0'"))
97 << FD->getName();
98 }
99 }
100
101 // Process any non-static function declarations
102 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; I++) {
Stephen Hineseb2eec92011-01-09 19:13:09 -0800103 clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I);
104 if (FD && FD->isGlobal()) {
105 AnnotateFunction(FD);
106 }
Stephen Hinescfae0f32010-11-01 18:57:31 -0700107 }
108
109 Backend::HandleTopLevelDecl(D);
110 return;
111}
112
Stephen Hinesc97a3332010-11-30 15:31:08 -0800113namespace {
114
Stephen Hines78e69cb2011-04-22 15:03:19 -0700115static bool ValidateVarDecl(clang::VarDecl *VD) {
116 if (!VD) {
117 return true;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800118 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700119
120 clang::ASTContext &C = VD->getASTContext();
121 const clang::Type *T = VD->getType().getTypePtr();
122 bool valid = true;
123
124 if (VD->getLinkage() == clang::ExternalLinkage) {
125 llvm::StringRef TypeName;
126 if (!RSExportType::NormalizeType(T, TypeName, &C.getDiagnostics(), VD)) {
127 valid = false;
128 }
129 }
130 valid &= RSExportType::ValidateVarDecl(VD);
131
132 return valid;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800133}
134
Stephen Hines78e69cb2011-04-22 15:03:19 -0700135static bool ValidateASTContext(clang::ASTContext &C) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800136 bool valid = true;
Stephen Hinesfcda2352010-10-19 16:49:32 -0700137 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
Stephen Hinesc97a3332010-11-30 15:31:08 -0800138 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
139 DE = TUDecl->decls_end();
140 DI != DE;
141 DI++) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700142 clang::VarDecl *VD = dyn_cast<clang::VarDecl>(*DI);
143 if (VD && !ValidateVarDecl(VD)) {
144 valid = false;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800145 }
146 }
147
148 return valid;
149}
150
Stephen Hinese5e64432010-12-02 18:48:20 -0800151} // namespace
Stephen Hinesc97a3332010-11-30 15:31:08 -0800152
153void RSBackend::HandleTranslationUnitPre(clang::ASTContext &C) {
154 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
155
Stephen Hines78e69cb2011-04-22 15:03:19 -0700156 if (!ValidateASTContext(C)) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800157 return;
158 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700159
Stephen Hines96ab06c2011-01-05 15:29:26 -0800160 int version = mContext->getVersion();
161 if (version == 0) {
162 // Not setting a version is an error
163 mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
164 "Missing pragma for version in source file"));
165 } else if (version > 1) {
166 mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
167 "Pragma for version in source file must be set to 1"));
168 }
169
Stephen Hinescfae0f32010-11-01 18:57:31 -0700170 // Process any static function declarations
Stephen Hinesfcda2352010-10-19 16:49:32 -0700171 for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
172 E = TUDecl->decls_end(); I != E; I++) {
173 if ((I->getKind() >= clang::Decl::firstFunction) &&
174 (I->getKind() <= clang::Decl::lastFunction)) {
Stephen Hineseb2eec92011-01-09 19:13:09 -0800175 clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I);
176 if (FD && !FD->isGlobal()) {
177 AnnotateFunction(FD);
178 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700179 }
180 }
181
182 return;
183}
184
185///////////////////////////////////////////////////////////////////////////////
Zonr Chang68fc02c2010-10-13 19:09:19 +0800186void RSBackend::HandleTranslationUnitPost(llvm::Module *M) {
Stephen Hinesc808a992010-11-29 17:20:42 -0800187 if (!mContext->processExport()) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800188 return;
Stephen Hinesc808a992010-11-29 17:20:42 -0800189 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700190
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700191 // Dump export variable info
192 if (mContext->hasExportVar()) {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800193 int slotCount = 0;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 if (mExportVarMetadata == NULL)
Zonr Chang68fc02c2010-10-13 19:09:19 +0800195 mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700196
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700197 llvm::SmallVector<llvm::Value*, 2> ExportVarInfo;
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800198
199 // We emit slot information (#rs_object_slots) for any reference counted
200 // RS type or pointer (which can also be bound).
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700201
zonr6315f762010-10-05 15:35:14 +0800202 for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(),
203 E = mContext->export_vars_end();
204 I != E;
205 I++) {
206 const RSExportVar *EV = *I;
207 const RSExportType *ET = EV->getType();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800208 bool countsAsRSObject = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700209
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700210 // Variable name
211 ExportVarInfo.push_back(
212 llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700213
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700214 // Type name
Zonr Changa65ec162010-10-17 01:53:05 +0800215 switch (ET->getClass()) {
216 case RSExportType::ExportClassPrimitive: {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800217 const RSExportPrimitiveType *PT =
218 static_cast<const RSExportPrimitiveType*>(ET);
Zonr Changa65ec162010-10-17 01:53:05 +0800219 ExportVarInfo.push_back(
220 llvm::MDString::get(
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800221 mLLVMContext, llvm::utostr_32(PT->getType())));
222 if (PT->isRSObjectType()) {
223 countsAsRSObject = true;
224 }
Zonr Changa65ec162010-10-17 01:53:05 +0800225 break;
226 }
227 case RSExportType::ExportClassPointer: {
228 ExportVarInfo.push_back(
229 llvm::MDString::get(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700230 mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
Zonr Changa65ec162010-10-17 01:53:05 +0800231 ->getPointeeType()->getName()).c_str()));
232 break;
233 }
234 case RSExportType::ExportClassMatrix: {
235 ExportVarInfo.push_back(
236 llvm::MDString::get(
237 mLLVMContext, llvm::utostr_32(
238 RSExportPrimitiveType::DataTypeRSMatrix2x2 +
239 static_cast<const RSExportMatrixType*>(ET)->getDim() - 2)));
240 break;
241 }
242 case RSExportType::ExportClassVector:
243 case RSExportType::ExportClassConstantArray:
244 case RSExportType::ExportClassRecord: {
245 ExportVarInfo.push_back(
246 llvm::MDString::get(mLLVMContext,
247 EV->getType()->getName().c_str()));
248 break;
249 }
250 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700251
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700252 mExportVarMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700253 llvm::MDNode::get(mLLVMContext, ExportVarInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700254 ExportVarInfo.clear();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800255
256 if (mRSObjectSlotsMetadata == NULL) {
257 mRSObjectSlotsMetadata =
258 M->getOrInsertNamedMetadata(RS_OBJECT_SLOTS_MN);
259 }
260
261 if (countsAsRSObject) {
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700262 mRSObjectSlotsMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
263 llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount))));
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800264 }
265
266 slotCount++;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700267 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700268 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700269
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700270 // Dump export function info
271 if (mContext->hasExportFunc()) {
272 if (mExportFuncMetadata == NULL)
273 mExportFuncMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800274 M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700275
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700276 llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700277
zonr6315f762010-10-05 15:35:14 +0800278 for (RSContext::const_export_func_iterator
279 I = mContext->export_funcs_begin(),
280 E = mContext->export_funcs_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700281 I != E;
282 I++) {
zonr6315f762010-10-05 15:35:14 +0800283 const RSExportFunc *EF = *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700284
285 // Function name
zonr6315f762010-10-05 15:35:14 +0800286 if (!EF->hasParam()) {
287 ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
288 EF->getName().c_str()));
289 } else {
Zonr Chang68fc02c2010-10-13 19:09:19 +0800290 llvm::Function *F = M->getFunction(EF->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700291 llvm::Function *HelperFunction;
292 const std::string HelperFunctionName(".helper_" + EF->getName());
293
Stephen Hines6e6578a2011-02-07 18:05:48 -0800294 slangAssert(F && "Function marked as exported disappeared in Bitcode");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700295
296 // Create helper function
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700297 {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800298 llvm::StructType *HelperFunctionParameterTy = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700299
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800300 if (!F->getArgumentList().empty()) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700301 std::vector<llvm::Type*> HelperFunctionParameterTys;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800302 for (llvm::Function::arg_iterator AI = F->arg_begin(),
303 AE = F->arg_end(); AI != AE; AI++)
304 HelperFunctionParameterTys.push_back(AI->getType());
305
306 HelperFunctionParameterTy =
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700307 llvm::StructType::get(mLLVMContext,
308 llvm::ArrayRef<llvm::Type*>(
309 HelperFunctionParameterTys));
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800310 }
311
312 if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) {
313 fprintf(stderr, "Failed to export function %s: parameter type "
314 "mismatch during creation of helper function.\n",
315 EF->getName().c_str());
316
317 const RSExportRecordType *Expected = EF->getParamPacketType();
318 if (Expected) {
319 fprintf(stderr, "Expected:\n");
320 Expected->getLLVMType()->dump();
321 }
322 if (HelperFunctionParameterTy) {
323 fprintf(stderr, "Got:\n");
324 HelperFunctionParameterTy->dump();
325 }
326 }
327
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700328 std::vector<llvm::Type*> Params;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800329 if (HelperFunctionParameterTy) {
330 llvm::PointerType *HelperFunctionParameterTyP =
331 llvm::PointerType::getUnqual(HelperFunctionParameterTy);
332 Params.push_back(HelperFunctionParameterTyP);
333 }
334
335 llvm::FunctionType * HelperFunctionType =
336 llvm::FunctionType::get(F->getReturnType(),
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700337 llvm::ArrayRef<llvm::Type*>(Params),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800338 /* IsVarArgs = */false);
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700339
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700340 HelperFunction =
341 llvm::Function::Create(HelperFunctionType,
342 llvm::GlobalValue::ExternalLinkage,
343 HelperFunctionName,
Zonr Chang68fc02c2010-10-13 19:09:19 +0800344 M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700345
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700346 HelperFunction->addFnAttr(llvm::Attribute::NoInline);
347 HelperFunction->setCallingConv(F->getCallingConv());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700348
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700349 // Create helper function body
350 {
351 llvm::Argument *HelperFunctionParameter =
352 &(*HelperFunction->arg_begin());
353 llvm::BasicBlock *BB =
354 llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction);
355 llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB);
356 llvm::SmallVector<llvm::Value*, 6> Params;
357 llvm::Value *Idx[2];
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700358
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700359 Idx[0] =
360 llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700361
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700362 // getelementptr and load instruction for all elements in
363 // parameter .p
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800364 for (size_t i = 0; i < EF->getNumParameters(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700365 // getelementptr
366 Idx[1] =
367 llvm::ConstantInt::get(
368 llvm::Type::getInt32Ty(mLLVMContext), i);
369 llvm::Value *Ptr = IB->CreateInBoundsGEP(HelperFunctionParameter,
370 Idx,
371 Idx + 2);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700372
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700373 // load
374 llvm::Value *V = IB->CreateLoad(Ptr);
375 Params.push_back(V);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700376 }
377
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700378 // Call and pass the all elements as paramter to F
379 llvm::CallInst *CI = IB->CreateCall(F,
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700380 llvm::ArrayRef<llvm::Value*>(
381 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
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700404 // Dump export type info
405 if (mContext->hasExportType()) {
406 llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700407
zonr6315f762010-10-05 15:35:14 +0800408 for (RSContext::const_export_type_iterator
409 I = mContext->export_types_begin(),
410 E = mContext->export_types_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700411 I != E;
412 I++) {
413 // First, dump type name list to export
414 const RSExportType *ET = I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700415
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700416 ExportTypeInfo.clear();
417 // Type name
418 ExportTypeInfo.push_back(
419 llvm::MDString::get(mLLVMContext, ET->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700420
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700421 if (ET->getClass() == RSExportType::ExportClassRecord) {
422 const RSExportRecordType *ERT =
423 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700424
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700425 if (mExportTypeMetadata == NULL)
426 mExportTypeMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800427 M->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700428
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700429 mExportTypeMetadata->addOperand(
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700430 llvm::MDNode::get(mLLVMContext, ExportTypeInfo));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700431
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700432 // Now, export struct field information to %[struct name]
433 std::string StructInfoMetadataName("%");
434 StructInfoMetadataName.append(ET->getName());
435 llvm::NamedMDNode *StructInfoMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800436 M->getOrInsertNamedMetadata(StructInfoMetadataName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700437 llvm::SmallVector<llvm::Value*, 3> FieldInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700438
Stephen Hines6e6578a2011-02-07 18:05:48 -0800439 slangAssert(StructInfoMetadata->getNumOperands() == 0 &&
440 "Metadata with same name was created before");
zonr6315f762010-10-05 15:35:14 +0800441 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
442 FE = ERT->fields_end();
443 FI != FE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700444 FI++) {
445 const RSExportRecordType::Field *F = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700446
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700447 // 1. field name
448 FieldInfo.push_back(llvm::MDString::get(mLLVMContext,
449 F->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700450
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700451 // 2. field type name
452 FieldInfo.push_back(
453 llvm::MDString::get(mLLVMContext,
454 F->getType()->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700455
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700456 // 3. field kind
457 switch (F->getType()->getClass()) {
458 case RSExportType::ExportClassPrimitive:
459 case RSExportType::ExportClassVector: {
460 const RSExportPrimitiveType *EPT =
461 static_cast<const RSExportPrimitiveType*>(F->getType());
462 FieldInfo.push_back(
463 llvm::MDString::get(mLLVMContext,
464 llvm::itostr(EPT->getKind())));
465 break;
466 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700467
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700468 default: {
469 FieldInfo.push_back(
470 llvm::MDString::get(mLLVMContext,
471 llvm::itostr(
zonr6315f762010-10-05 15:35:14 +0800472 RSExportPrimitiveType::DataKindUser)));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700473 break;
474 }
475 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700476
Stephen Hinesd27a74e2011-07-13 20:59:46 -0700477 StructInfoMetadata->addOperand(
478 llvm::MDNode::get(mLLVMContext, FieldInfo));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700479 FieldInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700480 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700481 } // ET->getClass() == RSExportType::ExportClassRecord
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700482 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700483 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700484
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700485 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700486}
487
488RSBackend::~RSBackend() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700489 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700490}
Stephen Hinese639eb52010-11-08 19:27:20 -0800491
492} // namespace slang