blob: 083e588c5126de465b9c1b3e98bf6b2ddd96c83a [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 Hinese5e64432010-12-02 18:48:20 -0800115bool ValidateVar(clang::VarDecl *VD, clang::Diagnostic *Diags,
116 clang::SourceManager *SM) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800117 llvm::StringRef TypeName;
118 const clang::Type *T = VD->getType().getTypePtr();
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800119 if (!RSExportType::NormalizeType(T, TypeName, Diags, SM, VD)) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800120 return false;
121 }
122 return true;
123}
124
125bool ValidateASTContext(clang::ASTContext &C, clang::Diagnostic &Diags) {
126 bool valid = true;
Stephen Hinesfcda2352010-10-19 16:49:32 -0700127 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
Stephen Hinesc97a3332010-11-30 15:31:08 -0800128 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
129 DE = TUDecl->decls_end();
130 DI != DE;
131 DI++) {
132 if (DI->getKind() == clang::Decl::Var) {
133 clang::VarDecl *VD = (clang::VarDecl*) (*DI);
134 if (VD->getLinkage() == clang::ExternalLinkage) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800135 if (!ValidateVar(VD, &Diags, &C.getSourceManager())) {
Stephen Hinesc97a3332010-11-30 15:31:08 -0800136 valid = false;
Stephen Hinesc97a3332010-11-30 15:31:08 -0800137 }
138 }
139 }
140 }
141
142 return valid;
143}
144
Stephen Hinese5e64432010-12-02 18:48:20 -0800145} // namespace
Stephen Hinesc97a3332010-11-30 15:31:08 -0800146
147void RSBackend::HandleTranslationUnitPre(clang::ASTContext &C) {
148 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
149
150 if (!ValidateASTContext(C, mDiags)) {
151 return;
152 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700153
Stephen Hines96ab06c2011-01-05 15:29:26 -0800154 int version = mContext->getVersion();
155 if (version == 0) {
156 // Not setting a version is an error
157 mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
158 "Missing pragma for version in source file"));
159 } else if (version > 1) {
160 mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
161 "Pragma for version in source file must be set to 1"));
162 }
163
Stephen Hinescfae0f32010-11-01 18:57:31 -0700164 // Process any static function declarations
Stephen Hinesfcda2352010-10-19 16:49:32 -0700165 for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
166 E = TUDecl->decls_end(); I != E; I++) {
167 if ((I->getKind() >= clang::Decl::firstFunction) &&
168 (I->getKind() <= clang::Decl::lastFunction)) {
Stephen Hineseb2eec92011-01-09 19:13:09 -0800169 clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I);
170 if (FD && !FD->isGlobal()) {
171 AnnotateFunction(FD);
172 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700173 }
174 }
175
176 return;
177}
178
179///////////////////////////////////////////////////////////////////////////////
Zonr Chang68fc02c2010-10-13 19:09:19 +0800180void RSBackend::HandleTranslationUnitPost(llvm::Module *M) {
Stephen Hinesc808a992010-11-29 17:20:42 -0800181 if (!mContext->processExport()) {
182 mDiags.Report(mDiags.getCustomDiagID(clang::Diagnostic::Error,
183 "elements cannot be exported"));
Stephen Hinesc97a3332010-11-30 15:31:08 -0800184 return;
Stephen Hinesc808a992010-11-29 17:20:42 -0800185 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700186
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700187 // Dump export variable info
188 if (mContext->hasExportVar()) {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800189 int slotCount = 0;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700190 if (mExportVarMetadata == NULL)
Zonr Chang68fc02c2010-10-13 19:09:19 +0800191 mExportVarMetadata = M->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700192
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700193 llvm::SmallVector<llvm::Value*, 2> ExportVarInfo;
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800194 llvm::SmallVector<llvm::Value*, 1> SlotVarInfo;
195
196 // We emit slot information (#rs_object_slots) for any reference counted
197 // RS type or pointer (which can also be bound).
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700198
zonr6315f762010-10-05 15:35:14 +0800199 for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(),
200 E = mContext->export_vars_end();
201 I != E;
202 I++) {
203 const RSExportVar *EV = *I;
204 const RSExportType *ET = EV->getType();
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800205 bool countsAsRSObject = false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700206
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700207 // Variable name
208 ExportVarInfo.push_back(
209 llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700210
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700211 // Type name
Zonr Changa65ec162010-10-17 01:53:05 +0800212 switch (ET->getClass()) {
213 case RSExportType::ExportClassPrimitive: {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800214 const RSExportPrimitiveType *PT =
215 static_cast<const RSExportPrimitiveType*>(ET);
Zonr Changa65ec162010-10-17 01:53:05 +0800216 ExportVarInfo.push_back(
217 llvm::MDString::get(
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800218 mLLVMContext, llvm::utostr_32(PT->getType())));
219 if (PT->isRSObjectType()) {
220 countsAsRSObject = true;
221 }
Zonr Changa65ec162010-10-17 01:53:05 +0800222 break;
223 }
224 case RSExportType::ExportClassPointer: {
225 ExportVarInfo.push_back(
226 llvm::MDString::get(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700227 mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
Zonr Changa65ec162010-10-17 01:53:05 +0800228 ->getPointeeType()->getName()).c_str()));
229 break;
230 }
231 case RSExportType::ExportClassMatrix: {
232 ExportVarInfo.push_back(
233 llvm::MDString::get(
234 mLLVMContext, llvm::utostr_32(
235 RSExportPrimitiveType::DataTypeRSMatrix2x2 +
236 static_cast<const RSExportMatrixType*>(ET)->getDim() - 2)));
237 break;
238 }
239 case RSExportType::ExportClassVector:
240 case RSExportType::ExportClassConstantArray:
241 case RSExportType::ExportClassRecord: {
242 ExportVarInfo.push_back(
243 llvm::MDString::get(mLLVMContext,
244 EV->getType()->getName().c_str()));
245 break;
246 }
247 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700248
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700249 mExportVarMetadata->addOperand(
250 llvm::MDNode::get(mLLVMContext,
251 ExportVarInfo.data(),
252 ExportVarInfo.size()) );
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700253
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) {
262 SlotVarInfo.push_back(
263 llvm::MDString::get(mLLVMContext, llvm::utostr_32(slotCount)));
264
265 mRSObjectSlotsMetadata->addOperand(
266 llvm::MDNode::get(mLLVMContext,
267 SlotVarInfo.data(),
268 SlotVarInfo.size()));
269 }
270
271 slotCount++;
272 SlotVarInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700273 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700274 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700275
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700276 // Dump export function info
277 if (mContext->hasExportFunc()) {
278 if (mExportFuncMetadata == NULL)
279 mExportFuncMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800280 M->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700281
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700282 llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700283
zonr6315f762010-10-05 15:35:14 +0800284 for (RSContext::const_export_func_iterator
285 I = mContext->export_funcs_begin(),
286 E = mContext->export_funcs_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700287 I != E;
288 I++) {
zonr6315f762010-10-05 15:35:14 +0800289 const RSExportFunc *EF = *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700290
291 // Function name
zonr6315f762010-10-05 15:35:14 +0800292 if (!EF->hasParam()) {
293 ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
294 EF->getName().c_str()));
295 } else {
Zonr Chang68fc02c2010-10-13 19:09:19 +0800296 llvm::Function *F = M->getFunction(EF->getName());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700297 llvm::Function *HelperFunction;
298 const std::string HelperFunctionName(".helper_" + EF->getName());
299
Stephen Hines6e6578a2011-02-07 18:05:48 -0800300 slangAssert(F && "Function marked as exported disappeared in Bitcode");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700301
302 // Create helper function
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700303 {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800304 llvm::StructType *HelperFunctionParameterTy = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700305
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800306 if (!F->getArgumentList().empty()) {
307 std::vector<const llvm::Type*> HelperFunctionParameterTys;
308 for (llvm::Function::arg_iterator AI = F->arg_begin(),
309 AE = F->arg_end(); AI != AE; AI++)
310 HelperFunctionParameterTys.push_back(AI->getType());
311
312 HelperFunctionParameterTy =
313 llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys);
314 }
315
316 if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) {
317 fprintf(stderr, "Failed to export function %s: parameter type "
318 "mismatch during creation of helper function.\n",
319 EF->getName().c_str());
320
321 const RSExportRecordType *Expected = EF->getParamPacketType();
322 if (Expected) {
323 fprintf(stderr, "Expected:\n");
324 Expected->getLLVMType()->dump();
325 }
326 if (HelperFunctionParameterTy) {
327 fprintf(stderr, "Got:\n");
328 HelperFunctionParameterTy->dump();
329 }
330 }
331
332 std::vector<const llvm::Type*> Params;
333 if (HelperFunctionParameterTy) {
334 llvm::PointerType *HelperFunctionParameterTyP =
335 llvm::PointerType::getUnqual(HelperFunctionParameterTy);
336 Params.push_back(HelperFunctionParameterTyP);
337 }
338
339 llvm::FunctionType * HelperFunctionType =
340 llvm::FunctionType::get(F->getReturnType(),
341 Params,
342 /* IsVarArgs = */false);
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700343
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700344 HelperFunction =
345 llvm::Function::Create(HelperFunctionType,
346 llvm::GlobalValue::ExternalLinkage,
347 HelperFunctionName,
Zonr Chang68fc02c2010-10-13 19:09:19 +0800348 M);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700349
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700350 HelperFunction->addFnAttr(llvm::Attribute::NoInline);
351 HelperFunction->setCallingConv(F->getCallingConv());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700352
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700353 // Create helper function body
354 {
355 llvm::Argument *HelperFunctionParameter =
356 &(*HelperFunction->arg_begin());
357 llvm::BasicBlock *BB =
358 llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction);
359 llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB);
360 llvm::SmallVector<llvm::Value*, 6> Params;
361 llvm::Value *Idx[2];
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700362
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700363 Idx[0] =
364 llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700365
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700366 // getelementptr and load instruction for all elements in
367 // parameter .p
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800368 for (size_t i = 0; i < EF->getNumParameters(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700369 // getelementptr
370 Idx[1] =
371 llvm::ConstantInt::get(
372 llvm::Type::getInt32Ty(mLLVMContext), i);
373 llvm::Value *Ptr = IB->CreateInBoundsGEP(HelperFunctionParameter,
374 Idx,
375 Idx + 2);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700376
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700377 // load
378 llvm::Value *V = IB->CreateLoad(Ptr);
379 Params.push_back(V);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700380 }
381
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700382 // Call and pass the all elements as paramter to F
383 llvm::CallInst *CI = IB->CreateCall(F,
384 Params.data(),
385 Params.data() + Params.size());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700386
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700387 CI->setCallingConv(F->getCallingConv());
388
389 if (F->getReturnType() == llvm::Type::getVoidTy(mLLVMContext))
390 IB->CreateRetVoid();
391 else
392 IB->CreateRet(CI);
393
394 delete IB;
395 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700396 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700397
398 ExportFuncInfo.push_back(
399 llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str()));
400 }
401
402 mExportFuncMetadata->addOperand(
403 llvm::MDNode::get(mLLVMContext,
404 ExportFuncInfo.data(),
405 ExportFuncInfo.size()));
406
407 ExportFuncInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700408 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700409 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700410
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700411 // Dump export type info
412 if (mContext->hasExportType()) {
413 llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700414
zonr6315f762010-10-05 15:35:14 +0800415 for (RSContext::const_export_type_iterator
416 I = mContext->export_types_begin(),
417 E = mContext->export_types_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700418 I != E;
419 I++) {
420 // First, dump type name list to export
421 const RSExportType *ET = I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700422
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700423 ExportTypeInfo.clear();
424 // Type name
425 ExportTypeInfo.push_back(
426 llvm::MDString::get(mLLVMContext, ET->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700427
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700428 if (ET->getClass() == RSExportType::ExportClassRecord) {
429 const RSExportRecordType *ERT =
430 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700431
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700432 if (mExportTypeMetadata == NULL)
433 mExportTypeMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800434 M->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700435
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700436 mExportTypeMetadata->addOperand(
437 llvm::MDNode::get(mLLVMContext,
438 ExportTypeInfo.data(),
439 ExportTypeInfo.size()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700440
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700441 // Now, export struct field information to %[struct name]
442 std::string StructInfoMetadataName("%");
443 StructInfoMetadataName.append(ET->getName());
444 llvm::NamedMDNode *StructInfoMetadata =
Zonr Chang68fc02c2010-10-13 19:09:19 +0800445 M->getOrInsertNamedMetadata(StructInfoMetadataName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700446 llvm::SmallVector<llvm::Value*, 3> FieldInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700447
Stephen Hines6e6578a2011-02-07 18:05:48 -0800448 slangAssert(StructInfoMetadata->getNumOperands() == 0 &&
449 "Metadata with same name was created before");
zonr6315f762010-10-05 15:35:14 +0800450 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
451 FE = ERT->fields_end();
452 FI != FE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700453 FI++) {
454 const RSExportRecordType::Field *F = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700455
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700456 // 1. field name
457 FieldInfo.push_back(llvm::MDString::get(mLLVMContext,
458 F->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700459
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700460 // 2. field type name
461 FieldInfo.push_back(
462 llvm::MDString::get(mLLVMContext,
463 F->getType()->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700464
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700465 // 3. field kind
466 switch (F->getType()->getClass()) {
467 case RSExportType::ExportClassPrimitive:
468 case RSExportType::ExportClassVector: {
469 const RSExportPrimitiveType *EPT =
470 static_cast<const RSExportPrimitiveType*>(F->getType());
471 FieldInfo.push_back(
472 llvm::MDString::get(mLLVMContext,
473 llvm::itostr(EPT->getKind())));
474 break;
475 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700476
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700477 default: {
478 FieldInfo.push_back(
479 llvm::MDString::get(mLLVMContext,
480 llvm::itostr(
zonr6315f762010-10-05 15:35:14 +0800481 RSExportPrimitiveType::DataKindUser)));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700482 break;
483 }
484 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700485
zonr6315f762010-10-05 15:35:14 +0800486 StructInfoMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
487 FieldInfo.data(),
488 FieldInfo.size()));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700489
490 FieldInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700491 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700492 } // ET->getClass() == RSExportType::ExportClassRecord
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700493 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700494 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700495
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700496 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700497}
498
499RSBackend::~RSBackend() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700500 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700501}
Stephen Hinese639eb52010-11-08 19:27:20 -0800502
503} // namespace slang