blob: 9acf338bd4b4bb3941410bdc56b343cb8325a142 [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
19#include <vector>
20#include <string>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070021
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#include "llvm/Metadata.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070023#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 Liao462aefd2010-06-04 15:32:04 -070028
Zonr Chang592a9542010-10-07 20:03:58 +080029#include "llvm/System/Path.h"
30
zonr6315f762010-10-05 15:35:14 +080031#include "llvm/Support/IRBuilder.h"
32
33#include "llvm/ADT/Twine.h"
34#include "llvm/ADT/StringExtras.h"
35
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070036#include "clang/AST/DeclGroup.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070037
Zonr Chang592a9542010-10-07 20:03:58 +080038#include "slang_rs.h"
zonr6315f762010-10-05 15:35:14 +080039#include "slang_rs_context.h"
Zonr Chang7f2f3852010-10-07 19:12:23 +080040#include "slang_rs_metadata.h"
zonr6315f762010-10-05 15:35:14 +080041#include "slang_rs_export_var.h"
42#include "slang_rs_export_func.h"
43#include "slang_rs_export_type.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070044
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045using namespace slang;
46
47RSBackend::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 Chang3a9ca1f2010-10-06 17:52:56 +080053 Slang::OutputType OT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 clang::SourceManager &SourceMgr,
zonr6315f762010-10-05 15:35:14 +080055 bool AllowRSPrefix)
56 : Backend(Diags,
57 CodeGenOpts,
58 TargetOpts,
59 Pragmas,
60 OS,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080061 OT),
zonr6315f762010-10-05 15:35:14 +080062 mContext(Context),
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080063 mSourceMgr(SourceMgr),
64 mAllowRSPrefix(AllowRSPrefix),
zonr6315f762010-10-05 15:35:14 +080065 mExportVarMetadata(NULL),
66 mExportFuncMetadata(NULL),
67 mExportTypeMetadata(NULL) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069}
70
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070071void RSBackend::HandleTopLevelDecl(clang::DeclGroupRef D) {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080072 // Disallow user-defined functions with prefix "rs"
73 if (!mAllowRSPrefix) {
Zonr Chang592a9542010-10-07 20:03:58 +080074 // Iterate all function declarations in the program.
75 for (clang::DeclGroupRef::iterator I = D.begin(), E = D.end();
76 I != E; I++) {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080077 clang::FunctionDecl *FD = dyn_cast<clang::FunctionDecl>(*I);
Zonr Chang592a9542010-10-07 20:03:58 +080078 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 Chang3a9ca1f2010-10-06 17:52:56 +080091 }
92 }
93 }
94
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070095 Backend::HandleTopLevelDecl(D);
96 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070097}
98
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099void RSBackend::HandleTranslationUnitEx(clang::ASTContext &Ctx) {
100 assert((&Ctx == mContext->getASTContext()) && "Unexpected AST context change"
101 " during LLVM IR generation");
102 mContext->processExport();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700103
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104 // Dump export variable info
105 if (mContext->hasExportVar()) {
106 if (mExportVarMetadata == NULL)
Zonr Chang7f2f3852010-10-07 19:12:23 +0800107 mExportVarMetadata = mpModule->getOrInsertNamedMetadata(RS_EXPORT_VAR_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700108
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 llvm::SmallVector<llvm::Value*, 2> ExportVarInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700110
zonr6315f762010-10-05 15:35:14 +0800111 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 Liao462aefd2010-06-04 15:32:04 -0700117
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118 // Variable name
119 ExportVarInfo.push_back(
120 llvm::MDString::get(mLLVMContext, EV->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700121
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700122 // Type name
123 if (ET->getClass() == RSExportType::ExportClassPrimitive)
124 ExportVarInfo.push_back(
125 llvm::MDString::get(
126 mLLVMContext, llvm::utostr_32(
zonr6315f762010-10-05 15:35:14 +0800127 static_cast<const RSExportPrimitiveType*>(ET)->getType())));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700128 else if (ET->getClass() == RSExportType::ExportClassPointer)
129 ExportVarInfo.push_back(
130 llvm::MDString::get(
131 mLLVMContext, ("*" + static_cast<const RSExportPointerType*>(ET)
zonr6315f762010-10-05 15:35:14 +0800132 ->getPointeeType()->getName()).c_str()));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700133 else
134 ExportVarInfo.push_back(
135 llvm::MDString::get(mLLVMContext,
136 EV->getType()->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700137
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700138 mExportVarMetadata->addOperand(
139 llvm::MDNode::get(mLLVMContext,
140 ExportVarInfo.data(),
141 ExportVarInfo.size()) );
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700142
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143 ExportVarInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700144 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700146
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700147 // Dump export function info
148 if (mContext->hasExportFunc()) {
149 if (mExportFuncMetadata == NULL)
150 mExportFuncMetadata =
Zonr Chang7f2f3852010-10-07 19:12:23 +0800151 mpModule->getOrInsertNamedMetadata(RS_EXPORT_FUNC_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700152
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700153 llvm::SmallVector<llvm::Value*, 1> ExportFuncInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154
zonr6315f762010-10-05 15:35:14 +0800155 for (RSContext::const_export_func_iterator
156 I = mContext->export_funcs_begin(),
157 E = mContext->export_funcs_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158 I != E;
159 I++) {
zonr6315f762010-10-05 15:35:14 +0800160 const RSExportFunc *EF = *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700161
162 // Function name
zonr6315f762010-10-05 15:35:14 +0800163 if (!EF->hasParam()) {
164 ExportFuncInfo.push_back(llvm::MDString::get(mLLVMContext,
165 EF->getName().c_str()));
166 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700167 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 Liao462aefd2010-06-04 15:32:04 -0700174 {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800175 llvm::StructType *HelperFunctionParameterTy = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700176
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800177 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 Liaocecd11d2010-09-21 08:07:58 -0700214
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700215 HelperFunction =
216 llvm::Function::Create(HelperFunctionType,
217 llvm::GlobalValue::ExternalLinkage,
218 HelperFunctionName,
219 mpModule);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700220
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700221 HelperFunction->addFnAttr(llvm::Attribute::NoInline);
222 HelperFunction->setCallingConv(F->getCallingConv());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700223
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700224 // 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 Liao462aefd2010-06-04 15:32:04 -0700233
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700234 Idx[0] =
235 llvm::ConstantInt::get(llvm::Type::getInt32Ty(mLLVMContext), 0);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700236
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700237 // getelementptr and load instruction for all elements in
238 // parameter .p
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800239 for (size_t i = 0; i < EF->getNumParameters(); i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700240 // 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 Liao462aefd2010-06-04 15:32:04 -0700247
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700248 // load
249 llvm::Value *V = IB->CreateLoad(Ptr);
250 Params.push_back(V);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700251 }
252
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700253 // 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 Liao462aefd2010-06-04 15:32:04 -0700257
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700258 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 Liao462aefd2010-06-04 15:32:04 -0700267 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700268
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 Liao462aefd2010-06-04 15:32:04 -0700279 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700280 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700281
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700282 // Dump export type info
283 if (mContext->hasExportType()) {
284 llvm::SmallVector<llvm::Value*, 1> ExportTypeInfo;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700285
zonr6315f762010-10-05 15:35:14 +0800286 for (RSContext::const_export_type_iterator
287 I = mContext->export_types_begin(),
288 E = mContext->export_types_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700289 I != E;
290 I++) {
291 // First, dump type name list to export
292 const RSExportType *ET = I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700293
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700294 ExportTypeInfo.clear();
295 // Type name
296 ExportTypeInfo.push_back(
297 llvm::MDString::get(mLLVMContext, ET->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700298
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700299 if (ET->getClass() == RSExportType::ExportClassRecord) {
300 const RSExportRecordType *ERT =
301 static_cast<const RSExportRecordType*>(ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700302
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700303 if (mExportTypeMetadata == NULL)
304 mExportTypeMetadata =
Zonr Chang7f2f3852010-10-07 19:12:23 +0800305 mpModule->getOrInsertNamedMetadata(RS_EXPORT_TYPE_MN);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700306
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700307 mExportTypeMetadata->addOperand(
308 llvm::MDNode::get(mLLVMContext,
309 ExportTypeInfo.data(),
310 ExportTypeInfo.size()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700311
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700312 // 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 Liao462aefd2010-06-04 15:32:04 -0700318
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700319 assert(StructInfoMetadata->getNumOperands() == 0 &&
320 "Metadata with same name was created before");
zonr6315f762010-10-05 15:35:14 +0800321 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(),
322 FE = ERT->fields_end();
323 FI != FE;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700324 FI++) {
325 const RSExportRecordType::Field *F = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700326
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700327 // 1. field name
328 FieldInfo.push_back(llvm::MDString::get(mLLVMContext,
329 F->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700330
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700331 // 2. field type name
332 FieldInfo.push_back(
333 llvm::MDString::get(mLLVMContext,
334 F->getType()->getName().c_str()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700335
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700336 // 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 Liao462aefd2010-06-04 15:32:04 -0700347
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700348 default: {
349 FieldInfo.push_back(
350 llvm::MDString::get(mLLVMContext,
351 llvm::itostr(
zonr6315f762010-10-05 15:35:14 +0800352 RSExportPrimitiveType::DataKindUser)));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700353 break;
354 }
355 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700356
zonr6315f762010-10-05 15:35:14 +0800357 StructInfoMetadata->addOperand(llvm::MDNode::get(mLLVMContext,
358 FieldInfo.data(),
359 FieldInfo.size()));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700360
361 FieldInfo.clear();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700362 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700363 } // ET->getClass() == RSExportType::ExportClassRecord
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700364 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700365 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700366
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700367 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700368}
369
370RSBackend::~RSBackend() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700371 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700372}