• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----- CGCXXABI.cpp - Interface to C++ ABIs -----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides an abstract class for C++ code generation. Concrete subclasses
11 // of this implement code generation for specific C++ ABIs.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "CGCXXABI.h"
16 
17 using namespace clang;
18 using namespace CodeGen;
19 
~CGCXXABI()20 CGCXXABI::~CGCXXABI() { }
21 
ErrorUnsupportedABI(CodeGenFunction & CGF,StringRef S)22 static void ErrorUnsupportedABI(CodeGenFunction &CGF,
23                                 StringRef S) {
24   DiagnosticsEngine &Diags = CGF.CGM.getDiags();
25   unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
26                                           "cannot yet compile %0 in this ABI");
27   Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()),
28                DiagID)
29     << S;
30 }
31 
GetBogusMemberPointer(CodeGenModule & CGM,QualType T)32 static llvm::Constant *GetBogusMemberPointer(CodeGenModule &CGM,
33                                              QualType T) {
34   return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
35 }
36 
37 llvm::Type *
ConvertMemberPointerType(const MemberPointerType * MPT)38 CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
39   return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
40 }
41 
EmitLoadOfMemberFunctionPointer(CodeGenFunction & CGF,llvm::Value * & This,llvm::Value * MemPtr,const MemberPointerType * MPT)42 llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
43                                                        llvm::Value *&This,
44                                                        llvm::Value *MemPtr,
45                                                  const MemberPointerType *MPT) {
46   ErrorUnsupportedABI(CGF, "calls through member pointers");
47 
48   const FunctionProtoType *FPT =
49     MPT->getPointeeType()->getAs<FunctionProtoType>();
50   const CXXRecordDecl *RD =
51     cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
52   llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
53                               CGM.getTypes().arrangeCXXMethodType(RD, FPT));
54   return llvm::Constant::getNullValue(FTy->getPointerTo());
55 }
56 
EmitMemberDataPointerAddress(CodeGenFunction & CGF,llvm::Value * Base,llvm::Value * MemPtr,const MemberPointerType * MPT)57 llvm::Value *CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
58                                                     llvm::Value *Base,
59                                                     llvm::Value *MemPtr,
60                                               const MemberPointerType *MPT) {
61   ErrorUnsupportedABI(CGF, "loads of member pointers");
62   llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo();
63   return llvm::Constant::getNullValue(Ty);
64 }
65 
EmitMemberPointerConversion(CodeGenFunction & CGF,const CastExpr * E,llvm::Value * Src)66 llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
67                                                    const CastExpr *E,
68                                                    llvm::Value *Src) {
69   ErrorUnsupportedABI(CGF, "member function pointer conversions");
70   return GetBogusMemberPointer(CGM, E->getType());
71 }
72 
EmitMemberPointerConversion(const CastExpr * E,llvm::Constant * Src)73 llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E,
74                                                       llvm::Constant *Src) {
75   return GetBogusMemberPointer(CGM, E->getType());
76 }
77 
78 llvm::Value *
EmitMemberPointerComparison(CodeGenFunction & CGF,llvm::Value * L,llvm::Value * R,const MemberPointerType * MPT,bool Inequality)79 CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
80                                       llvm::Value *L,
81                                       llvm::Value *R,
82                                       const MemberPointerType *MPT,
83                                       bool Inequality) {
84   ErrorUnsupportedABI(CGF, "member function pointer comparison");
85   return CGF.Builder.getFalse();
86 }
87 
88 llvm::Value *
EmitMemberPointerIsNotNull(CodeGenFunction & CGF,llvm::Value * MemPtr,const MemberPointerType * MPT)89 CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
90                                      llvm::Value *MemPtr,
91                                      const MemberPointerType *MPT) {
92   ErrorUnsupportedABI(CGF, "member function pointer null testing");
93   return CGF.Builder.getFalse();
94 }
95 
96 llvm::Constant *
EmitNullMemberPointer(const MemberPointerType * MPT)97 CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
98   return GetBogusMemberPointer(CGM, QualType(MPT, 0));
99 }
100 
EmitMemberPointer(const CXXMethodDecl * MD)101 llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
102   return GetBogusMemberPointer(CGM,
103                          CGM.getContext().getMemberPointerType(MD->getType(),
104                                          MD->getParent()->getTypeForDecl()));
105 }
106 
EmitMemberDataPointer(const MemberPointerType * MPT,CharUnits offset)107 llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
108                                                 CharUnits offset) {
109   return GetBogusMemberPointer(CGM, QualType(MPT, 0));
110 }
111 
EmitMemberPointer(const APValue & MP,QualType MPT)112 llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
113   return GetBogusMemberPointer(CGM, MPT);
114 }
115 
isZeroInitializable(const MemberPointerType * MPT)116 bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
117   // Fake answer.
118   return true;
119 }
120 
BuildThisParam(CodeGenFunction & CGF,FunctionArgList & params)121 void CGCXXABI::BuildThisParam(CodeGenFunction &CGF, FunctionArgList &params) {
122   const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
123 
124   // FIXME: I'm not entirely sure I like using a fake decl just for code
125   // generation. Maybe we can come up with a better way?
126   ImplicitParamDecl *ThisDecl
127     = ImplicitParamDecl::Create(CGM.getContext(), 0, MD->getLocation(),
128                                 &CGM.getContext().Idents.get("this"),
129                                 MD->getThisType(CGM.getContext()));
130   params.push_back(ThisDecl);
131   getThisDecl(CGF) = ThisDecl;
132 }
133 
EmitThisParam(CodeGenFunction & CGF)134 void CGCXXABI::EmitThisParam(CodeGenFunction &CGF) {
135   /// Initialize the 'this' slot.
136   assert(getThisDecl(CGF) && "no 'this' variable for function");
137   getThisValue(CGF)
138     = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)),
139                              "this");
140 }
141 
EmitReturnFromThunk(CodeGenFunction & CGF,RValue RV,QualType ResultType)142 void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
143                                    RValue RV, QualType ResultType) {
144   CGF.EmitReturnOfRValue(RV, ResultType);
145 }
146 
GetArrayCookieSize(const CXXNewExpr * expr)147 CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
148   if (!requiresArrayCookie(expr))
149     return CharUnits::Zero();
150   return getArrayCookieSizeImpl(expr->getAllocatedType());
151 }
152 
getArrayCookieSizeImpl(QualType elementType)153 CharUnits CGCXXABI::getArrayCookieSizeImpl(QualType elementType) {
154   // BOGUS
155   return CharUnits::Zero();
156 }
157 
InitializeArrayCookie(CodeGenFunction & CGF,llvm::Value * NewPtr,llvm::Value * NumElements,const CXXNewExpr * expr,QualType ElementType)158 llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
159                                              llvm::Value *NewPtr,
160                                              llvm::Value *NumElements,
161                                              const CXXNewExpr *expr,
162                                              QualType ElementType) {
163   // Should never be called.
164   ErrorUnsupportedABI(CGF, "array cookie initialization");
165   return 0;
166 }
167 
requiresArrayCookie(const CXXDeleteExpr * expr,QualType elementType)168 bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
169                                    QualType elementType) {
170   // If the class's usual deallocation function takes two arguments,
171   // it needs a cookie.
172   if (expr->doesUsualArrayDeleteWantSize())
173     return true;
174 
175   return elementType.isDestructedType();
176 }
177 
requiresArrayCookie(const CXXNewExpr * expr)178 bool CGCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
179   // If the class's usual deallocation function takes two arguments,
180   // it needs a cookie.
181   if (expr->doesUsualArrayDeleteWantSize())
182     return true;
183 
184   return expr->getAllocatedType().isDestructedType();
185 }
186 
ReadArrayCookie(CodeGenFunction & CGF,llvm::Value * ptr,const CXXDeleteExpr * expr,QualType eltTy,llvm::Value * & numElements,llvm::Value * & allocPtr,CharUnits & cookieSize)187 void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *ptr,
188                                const CXXDeleteExpr *expr, QualType eltTy,
189                                llvm::Value *&numElements,
190                                llvm::Value *&allocPtr, CharUnits &cookieSize) {
191   // Derive a char* in the same address space as the pointer.
192   unsigned AS = cast<llvm::PointerType>(ptr->getType())->getAddressSpace();
193   llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
194   ptr = CGF.Builder.CreateBitCast(ptr, charPtrTy);
195 
196   // If we don't need an array cookie, bail out early.
197   if (!requiresArrayCookie(expr, eltTy)) {
198     allocPtr = ptr;
199     numElements = 0;
200     cookieSize = CharUnits::Zero();
201     return;
202   }
203 
204   cookieSize = getArrayCookieSizeImpl(eltTy);
205   allocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ptr,
206                                                     -cookieSize.getQuantity());
207   numElements = readArrayCookieImpl(CGF, allocPtr, cookieSize);
208 }
209 
readArrayCookieImpl(CodeGenFunction & CGF,llvm::Value * ptr,CharUnits cookieSize)210 llvm::Value *CGCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
211                                            llvm::Value *ptr,
212                                            CharUnits cookieSize) {
213   ErrorUnsupportedABI(CGF, "reading a new[] cookie");
214   return llvm::ConstantInt::get(CGF.SizeTy, 0);
215 }
216 
EmitGuardedInit(CodeGenFunction & CGF,const VarDecl & D,llvm::GlobalVariable * GV,bool PerformInit)217 void CGCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
218                                const VarDecl &D,
219                                llvm::GlobalVariable *GV,
220                                bool PerformInit) {
221   ErrorUnsupportedABI(CGF, "static local variable initialization");
222 }
223 
registerGlobalDtor(CodeGenFunction & CGF,llvm::Constant * dtor,llvm::Constant * addr)224 void CGCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
225                                   llvm::Constant *dtor,
226                                   llvm::Constant *addr) {
227   // The default behavior is to use atexit.
228   CGF.registerGlobalDtorWithAtExit(dtor, addr);
229 }
230 
231 /// Returns the adjustment, in bytes, required for the given
232 /// member-pointer operation.  Returns null if no adjustment is
233 /// required.
getMemberPointerAdjustment(const CastExpr * E)234 llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
235   assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
236          E->getCastKind() == CK_BaseToDerivedMemberPointer);
237 
238   QualType derivedType;
239   if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
240     derivedType = E->getSubExpr()->getType();
241   else
242     derivedType = E->getType();
243 
244   const CXXRecordDecl *derivedClass =
245     derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
246 
247   return CGM.GetNonVirtualBaseClassOffset(derivedClass,
248                                           E->path_begin(),
249                                           E->path_end());
250 }
251