Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame^] | 1 | /* |
| 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_rs_backend.h" |
| 18 | |
| 19 | #include <vector> |
| 20 | #include <string> |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 21 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 22 | #include "llvm/Metadata.h" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 23 | #include "llvm/Constant.h" |
| 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/Module.h" |
| 26 | #include "llvm/Function.h" |
| 27 | #include "llvm/DerivedTypes.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 28 | |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 29 | #include "llvm/System/Path.h" |
| 30 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 31 | #include "llvm/Support/IRBuilder.h" |
| 32 | |
| 33 | #include "llvm/ADT/Twine.h" |
| 34 | #include "llvm/ADT/StringExtras.h" |
| 35 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 36 | #include "clang/AST/DeclGroup.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 37 | |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 38 | #include "slang_rs.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 39 | #include "slang_rs_context.h" |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 40 | #include "slang_rs_metadata.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 41 | #include "slang_rs_export_var.h" |
| 42 | #include "slang_rs_export_func.h" |
| 43 | #include "slang_rs_export_type.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 44 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 45 | using namespace slang; |
| 46 | |
| 47 | RSBackend::RSBackend(RSContext *Context, |
| 48 | clang::Diagnostic &Diags, |
| 49 | const clang::CodeGenOptions &CodeGenOpts, |
| 50 | const clang::TargetOptions &TargetOpts, |
| 51 | const PragmaList &Pragmas, |
| 52 | llvm::raw_ostream *OS, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 53 | Slang::OutputType OT, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 54 | clang::SourceManager &SourceMgr, |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 55 | bool AllowRSPrefix) |
| 56 | : Backend(Diags, |
| 57 | CodeGenOpts, |
| 58 | TargetOpts, |
| 59 | Pragmas, |
| 60 | OS, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 61 | OT), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 62 | mContext(Context), |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 63 | mSourceMgr(SourceMgr), |
| 64 | mAllowRSPrefix(AllowRSPrefix), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 65 | mExportVarMetadata(NULL), |
| 66 | mExportFuncMetadata(NULL), |
| 67 | mExportTypeMetadata(NULL) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 68 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 71 | void RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 72 | // Disallow user-defined functions with prefix "rs" |
| 73 | if (!mAllowRSPrefix) { |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 74 | // Iterate all function declarations in the program. |
| 75 | for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end(); |
| 76 | I != E; I++) { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 77 | clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I); |
Zonr Chang | 592a954 | 2010-10-07 20:03:58 +0800 | [diff] [blame] | 78 | if (FD == NULL) |
| 79 | continue; |
| 80 | if (FD->getName().startswith("rs")) { // Check prefix |
| 81 | clang::FullSourceLoc FSL(FD->getLocStart(), mSourceMgr); |
| 82 | clang::PresumedLoc PLoc = mSourceMgr.getPresumedLoc(FSL); |
| 83 | llvm::sys::Path HeaderFilename(PLoc.getFilename()); |
| 84 | |
| 85 | // Skip if that function declared in the RS default header. |
| 86 | if (SlangRS::IsRSHeaderFile(HeaderFilename.getLast().data())) |
| 87 | continue; |
| 88 | mDiags.Report(FSL, mDiags.getCustomDiagID(clang::Diagnostic::Error, |
| 89 | "invalid function name prefix, \"rs\" is reserved: '%0'")) |
| 90 | << FD->getName(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 95 | Backend::HandleTopLevelDecl(D); |
| 96 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 99 | void RSBackend::HandleTranslationUnitEx(clang::ASTContext &Ctx) { |
| 100 | assert((&Ctx == mContext->getASTContext()) && "Unexpected AST context change" |
| 101 | " during LLVM IR generation"); |
| 102 | mContext->processExport(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 103 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 104 | // Dump export variable info |
| 105 | if (mContext->hasExportVar()) { |
| 106 | if (mExportVarMetadata == NULL) |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 107 | mExportVarMetadata = mpModule->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 108 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 109 | llvm::SmallVector<llvm::Value*, 2> ExportVarInfo; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 110 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 111 | for (RSContext::const_export_var_iterator I = mContext->export_vars_begin(), |
| 112 | E = mContext->export_vars_end(); |
| 113 | I != E; |
| 114 | I++) { |
| 115 | const RSExportVar *EV = *I; |
| 116 | const RSExportType *ET = EV->getType(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 117 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 118 | // Variable name |
| 119 | ExportVarInfo.push_back( |
| 120 | llvm::MDString::get(mLLVMContext, EV->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 121 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 122 | // Type name |
| 123 | if (ET->getClass() == RSExportType::ExportClassPrimitive) |
| 124 | ExportVarInfo.push_back( |
| 125 | llvm::MDString::get( |
| 126 | mLLVMContext, llvm::utostr_32( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 127 | static_cast<const RSExportPrimitiveType*>(ET)->getType()))); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 128 | else if (ET->getClass() == RSExportType::ExportClassPointer) |
| 129 | ExportVarInfo.push_back( |
| 130 | llvm::MDString::get( |
| 131 | mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET) |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 132 | ->getPointeeType()->getName()).c_str())); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 133 | else |
| 134 | ExportVarInfo.push_back( |
| 135 | llvm::MDString::get(mLLVMContext, |
| 136 | EV->getType()->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 137 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 138 | mExportVarMetadata->addOperand( |
| 139 | llvm::MDNode::get(mLLVMContext, |
| 140 | ExportVarInfo.data(), |
| 141 | ExportVarInfo.size()) ); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 142 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 143 | ExportVarInfo.clear(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 144 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 145 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 146 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 147 | // Dump export function info |
| 148 | if (mContext->hasExportFunc()) { |
| 149 | if (mExportFuncMetadata == NULL) |
| 150 | mExportFuncMetadata = |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 151 | mpModule->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 152 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 153 | llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 154 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 155 | for (RSContext::const_export_func_iterator |
| 156 | I = mContext->export_funcs_begin(), |
| 157 | E = mContext->export_funcs_end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 158 | I != E; |
| 159 | I++) { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 160 | const RSExportFunc *EF = *I; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 161 | |
| 162 | // Function name |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 163 | if (!EF->hasParam()) { |
| 164 | ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext, |
| 165 | EF->getName().c_str())); |
| 166 | } else { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 167 | llvm::Function *F = mpModule->getFunction(EF->getName()); |
| 168 | llvm::Function *HelperFunction; |
| 169 | const std::string HelperFunctionName(".helper_" + EF->getName()); |
| 170 | |
| 171 | assert(F && "Function marked as exported disappeared in Bitcode"); |
| 172 | |
| 173 | // Create helper function |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 174 | { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 175 | llvm::StructType *HelperFunctionParameterTy = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 176 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 177 | if (!F->getArgumentList().empty()) { |
| 178 | std::vector<const llvm::Type*> HelperFunctionParameterTys; |
| 179 | for (llvm::Function::arg_iterator AI = F->arg_begin(), |
| 180 | AE = F->arg_end(); AI != AE; AI++) |
| 181 | HelperFunctionParameterTys.push_back(AI->getType()); |
| 182 | |
| 183 | HelperFunctionParameterTy = |
| 184 | llvm::StructType::get(mLLVMContext, HelperFunctionParameterTys); |
| 185 | } |
| 186 | |
| 187 | if (!EF->checkParameterPacketType(HelperFunctionParameterTy)) { |
| 188 | fprintf(stderr, "Failed to export function %s: parameter type " |
| 189 | "mismatch during creation of helper function.\n", |
| 190 | EF->getName().c_str()); |
| 191 | |
| 192 | const RSExportRecordType *Expected = EF->getParamPacketType(); |
| 193 | if (Expected) { |
| 194 | fprintf(stderr, "Expected:\n"); |
| 195 | Expected->getLLVMType()->dump(); |
| 196 | } |
| 197 | if (HelperFunctionParameterTy) { |
| 198 | fprintf(stderr, "Got:\n"); |
| 199 | HelperFunctionParameterTy->dump(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | std::vector<const llvm::Type*> Params; |
| 204 | if (HelperFunctionParameterTy) { |
| 205 | llvm::PointerType *HelperFunctionParameterTyP = |
| 206 | llvm::PointerType::getUnqual(HelperFunctionParameterTy); |
| 207 | Params.push_back(HelperFunctionParameterTyP); |
| 208 | } |
| 209 | |
| 210 | llvm::FunctionType * HelperFunctionType = |
| 211 | llvm::FunctionType::get(F->getReturnType(), |
| 212 | Params, |
| 213 | /* IsVarArgs = */false); |
Shih-wei Liao | cecd11d | 2010-09-21 08:07:58 -0700 | [diff] [blame] | 214 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 215 | HelperFunction = |
| 216 | llvm::Function::Create(HelperFunctionType, |
| 217 | llvm::GlobalValue::ExternalLinkage, |
| 218 | HelperFunctionName, |
| 219 | mpModule); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 220 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 221 | HelperFunction->addFnAttr(llvm::Attribute::NoInline); |
| 222 | HelperFunction->setCallingConv(F->getCallingConv()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 223 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 224 | // Create helper function body |
| 225 | { |
| 226 | llvm::Argument *HelperFunctionParameter = |
| 227 | &(*HelperFunction->arg_begin()); |
| 228 | llvm::BasicBlock *BB = |
| 229 | llvm::BasicBlock::Create(mLLVMContext, "entry", HelperFunction); |
| 230 | llvm::IRBuilder<> *IB = new llvm::IRBuilder<>(BB); |
| 231 | llvm::SmallVector<llvm::Value*, 6> Params; |
| 232 | llvm::Value *Idx[2]; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 233 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 234 | Idx[0] = |
| 235 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 236 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 237 | // getelementptr and load instruction for all elements in |
| 238 | // parameter .p |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 239 | for (size_t i = 0; i < EF->getNumParameters(); i++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 240 | // getelementptr |
| 241 | Idx[1] = |
| 242 | llvm::ConstantInt::get( |
| 243 | llvm::Type::getInt32Ty(mLLVMContext), i); |
| 244 | llvm::Value *Ptr = IB->CreateInBoundsGEP(HelperFunctionParameter, |
| 245 | Idx, |
| 246 | Idx + 2); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 247 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 248 | // load |
| 249 | llvm::Value *V = IB->CreateLoad(Ptr); |
| 250 | Params.push_back(V); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 253 | // Call and pass the all elements as paramter to F |
| 254 | llvm::CallInst *CI = IB->CreateCall(F, |
| 255 | Params.data(), |
| 256 | Params.data() + Params.size()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 257 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 258 | CI->setCallingConv(F->getCallingConv()); |
| 259 | |
| 260 | if (F->getReturnType() == llvm::Type::getVoidTy(mLLVMContext)) |
| 261 | IB->CreateRetVoid(); |
| 262 | else |
| 263 | IB->CreateRet(CI); |
| 264 | |
| 265 | delete IB; |
| 266 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 267 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 268 | |
| 269 | ExportFuncInfo.push_back( |
| 270 | llvm::MDString::get(mLLVMContext, HelperFunctionName.c_str())); |
| 271 | } |
| 272 | |
| 273 | mExportFuncMetadata->addOperand( |
| 274 | llvm::MDNode::get(mLLVMContext, |
| 275 | ExportFuncInfo.data(), |
| 276 | ExportFuncInfo.size())); |
| 277 | |
| 278 | ExportFuncInfo.clear(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 279 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 280 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 281 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 282 | // Dump export type info |
| 283 | if (mContext->hasExportType()) { |
| 284 | llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 285 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 286 | for (RSContext::const_export_type_iterator |
| 287 | I = mContext->export_types_begin(), |
| 288 | E = mContext->export_types_end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 289 | I != E; |
| 290 | I++) { |
| 291 | // First, dump type name list to export |
| 292 | const RSExportType *ET = I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 293 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 294 | ExportTypeInfo.clear(); |
| 295 | // Type name |
| 296 | ExportTypeInfo.push_back( |
| 297 | llvm::MDString::get(mLLVMContext, ET->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 298 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 299 | if (ET->getClass() == RSExportType::ExportClassRecord) { |
| 300 | const RSExportRecordType *ERT = |
| 301 | static_cast<const RSExportRecordType*>(ET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 302 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 303 | if (mExportTypeMetadata == NULL) |
| 304 | mExportTypeMetadata = |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 305 | mpModule->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 306 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 307 | mExportTypeMetadata->addOperand( |
| 308 | llvm::MDNode::get(mLLVMContext, |
| 309 | ExportTypeInfo.data(), |
| 310 | ExportTypeInfo.size())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 311 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 312 | // Now, export struct field information to %[struct name] |
| 313 | std::string StructInfoMetadataName("%"); |
| 314 | StructInfoMetadataName.append(ET->getName()); |
| 315 | llvm::NamedMDNode *StructInfoMetadata = |
| 316 | mpModule->getOrInsertNamedMetadata(StructInfoMetadataName); |
| 317 | llvm::SmallVector<llvm::Value*, 3> FieldInfo; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 318 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 319 | assert(StructInfoMetadata->getNumOperands() == 0 && |
| 320 | "Metadata with same name was created before"); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 321 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
| 322 | FE = ERT->fields_end(); |
| 323 | FI != FE; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 324 | FI++) { |
| 325 | const RSExportRecordType::Field *F = *FI; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 326 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 327 | // 1. field name |
| 328 | FieldInfo.push_back(llvm::MDString::get(mLLVMContext, |
| 329 | F->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 330 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 331 | // 2. field type name |
| 332 | FieldInfo.push_back( |
| 333 | llvm::MDString::get(mLLVMContext, |
| 334 | F->getType()->getName().c_str())); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 335 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 336 | // 3. field kind |
| 337 | switch (F->getType()->getClass()) { |
| 338 | case RSExportType::ExportClassPrimitive: |
| 339 | case RSExportType::ExportClassVector: { |
| 340 | const RSExportPrimitiveType *EPT = |
| 341 | static_cast<const RSExportPrimitiveType*>(F->getType()); |
| 342 | FieldInfo.push_back( |
| 343 | llvm::MDString::get(mLLVMContext, |
| 344 | llvm::itostr(EPT->getKind()))); |
| 345 | break; |
| 346 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 347 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 348 | default: { |
| 349 | FieldInfo.push_back( |
| 350 | llvm::MDString::get(mLLVMContext, |
| 351 | llvm::itostr( |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 352 | RSExportPrimitiveType::DataKindUser))); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 356 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 357 | StructInfoMetadata->addOperand(llvm::MDNode::get(mLLVMContext, |
| 358 | FieldInfo.data(), |
| 359 | FieldInfo.size())); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 360 | |
| 361 | FieldInfo.clear(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 362 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 363 | } // ET->getClass() == RSExportType::ExportClassRecord |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 364 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 365 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 366 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 367 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | RSBackend::~RSBackend() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 371 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 372 | } |