Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1 | //===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===// |
| 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 contains code dealing with C++ code generation of virtual tables. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
| 15 | #include "CodeGenFunction.h" |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
Zhongxing Xu | 1721ef7 | 2009-11-13 05:46:16 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Anders Carlsson | d5988502 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 24 | namespace { |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 25 | class VtableBuilder { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 26 | public: |
| 27 | /// Index_t - Vtable index type. |
| 28 | typedef uint64_t Index_t; |
| 29 | private: |
| 30 | std::vector<llvm::Constant *> &methods; |
| 31 | std::vector<llvm::Constant *> submethods; |
| 32 | llvm::Type *Ptr8Ty; |
| 33 | /// Class - The most derived class that this vtable is being built for. |
| 34 | const CXXRecordDecl *Class; |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 35 | /// LayoutClass - The most derived class used for virtual base layout |
| 36 | /// information. |
| 37 | const CXXRecordDecl *LayoutClass; |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 38 | /// LayoutOffset - The offset for Class in LayoutClass. |
| 39 | uint64_t LayoutOffset; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 40 | /// BLayout - Layout for the most derived class that this vtable is being |
| 41 | /// built for. |
| 42 | const ASTRecordLayout &BLayout; |
| 43 | llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary; |
| 44 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
| 45 | llvm::Constant *rtti; |
| 46 | llvm::LLVMContext &VMContext; |
| 47 | CodeGenModule &CGM; // Per-module state. |
| 48 | /// Index - Maps a method decl into a vtable index. Useful for virtual |
| 49 | /// dispatch codegen. |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 50 | llvm::DenseMap<GlobalDecl, Index_t> Index; |
| 51 | llvm::DenseMap<GlobalDecl, Index_t> VCall; |
| 52 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffset; |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 53 | // This is the offset to the nearest virtual base |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 54 | llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 55 | llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex; |
Mike Stump | bb9ff05 | 2009-10-27 23:46:47 +0000 | [diff] [blame] | 56 | |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 57 | /// PureVirtualFunction - Points to __cxa_pure_virtual. |
| 58 | llvm::Constant *PureVirtualFn; |
| 59 | |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame^] | 60 | /// VtableMethods - A data structure for keeping track of methods in a vtable. |
| 61 | /// Can add methods, override methods and iterate in vtable order. |
| 62 | class VtableMethods { |
| 63 | // MethodToIndexMap - Maps from a global decl to the index it has in the |
| 64 | // Methods vector. |
| 65 | llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap; |
| 66 | |
| 67 | /// Methods - The methods, in vtable order. |
| 68 | typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy; |
| 69 | MethodsVectorTy Methods; |
| 70 | |
| 71 | public: |
| 72 | /// AddMethod - Add a method to the vtable methods. |
| 73 | void AddMethod(GlobalDecl GD) { |
| 74 | assert(!MethodToIndexMap.count(GD) && |
| 75 | "Method has already been added!"); |
| 76 | |
| 77 | MethodToIndexMap[GD] = Methods.size(); |
| 78 | Methods.push_back(GD); |
| 79 | } |
| 80 | |
| 81 | /// OverrideMethod - Replace a method with another. |
| 82 | void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) { |
| 83 | llvm::DenseMap<GlobalDecl, uint64_t>::iterator i |
| 84 | = MethodToIndexMap.find(OverriddenGD); |
| 85 | assert(i != MethodToIndexMap.end() && "Did not find entry!"); |
| 86 | |
| 87 | // Get the index of the old decl. |
| 88 | uint64_t Index = i->second; |
| 89 | |
| 90 | // Replace the old decl with the new decl. |
| 91 | Methods[Index] = GD; |
| 92 | |
| 93 | // Now remove the old decl from the method to index map. |
| 94 | MethodToIndexMap.erase(i); |
| 95 | |
| 96 | // And add the new. |
| 97 | MethodToIndexMap[GD] = Index; |
| 98 | } |
| 99 | |
| 100 | MethodsVectorTy::size_type size() const { |
| 101 | return Methods.size(); |
| 102 | } |
| 103 | |
| 104 | void clear() { |
| 105 | MethodToIndexMap.clear(); |
| 106 | Methods.clear(); |
| 107 | } |
| 108 | |
| 109 | GlobalDecl operator[](unsigned Index) const { |
| 110 | return Methods[Index]; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | /// Methods - The vtable methods we're currently building. |
| 115 | VtableMethods Methods; |
| 116 | |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 117 | /// Thunk - Represents a single thunk. |
| 118 | struct Thunk { |
Anders Carlsson | 80bc5d5 | 2009-12-03 02:41:55 +0000 | [diff] [blame] | 119 | Thunk() { } |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 120 | |
Anders Carlsson | 80bc5d5 | 2009-12-03 02:41:55 +0000 | [diff] [blame] | 121 | Thunk(GlobalDecl GD, const ThunkAdjustment &Adjustment) |
| 122 | : GD(GD), Adjustment(Adjustment) { } |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 123 | |
Anders Carlsson | 0e1e763 | 2009-12-03 02:36:40 +0000 | [diff] [blame] | 124 | GlobalDecl GD; |
| 125 | |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 126 | /// Adjustment - The thunk adjustment. |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 127 | ThunkAdjustment Adjustment; |
| 128 | }; |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 129 | |
| 130 | /// Thunks - The thunks in a vtable. |
Anders Carlsson | 29a1f75 | 2009-12-03 02:39:59 +0000 | [diff] [blame] | 131 | typedef llvm::DenseMap<uint64_t, Thunk> ThunksMapTy; |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 132 | ThunksMapTy Thunks; |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 133 | |
| 134 | /// CovariantThunk - Represents a single covariant thunk. |
| 135 | struct CovariantThunk { |
Anders Carlsson | 1157e8f | 2009-12-03 02:20:26 +0000 | [diff] [blame] | 136 | CovariantThunk() { } |
| 137 | |
Anders Carlsson | 9f98f7a | 2009-12-03 02:34:59 +0000 | [diff] [blame] | 138 | CovariantThunk(GlobalDecl GD, CanQualType ReturnType) |
| 139 | : GD(GD), ReturnType(ReturnType) { } |
Anders Carlsson | 06c14b6 | 2009-12-03 02:16:14 +0000 | [diff] [blame] | 140 | |
Anders Carlsson | c38b40a | 2009-12-03 02:03:29 +0000 | [diff] [blame] | 141 | GlobalDecl GD; |
| 142 | |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 143 | /// ReturnType - The return type of the function. |
| 144 | CanQualType ReturnType; |
| 145 | }; |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 146 | |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 147 | /// CovariantThunks - The covariant thunks in a vtable. |
Anders Carlsson | 73295f9 | 2009-12-03 02:12:03 +0000 | [diff] [blame] | 148 | typedef llvm::DenseMap<uint64_t, CovariantThunk> CovariantThunksMapTy; |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 149 | CovariantThunksMapTy CovariantThunks; |
| 150 | |
| 151 | /// PureVirtualMethods - Pure virtual methods. |
| 152 | typedef llvm::DenseSet<GlobalDecl> PureVirtualMethodsSetTy; |
| 153 | PureVirtualMethodsSetTy PureVirtualMethods; |
| 154 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 155 | std::vector<Index_t> VCalls; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 156 | |
| 157 | typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t; |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 158 | // subAddressPoints - Used to hold the AddressPoints (offsets) into the built |
| 159 | // vtable for use in computing the initializers for the VTT. |
| 160 | llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 161 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 162 | typedef CXXRecordDecl::method_iterator method_iter; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 163 | const bool Extern; |
| 164 | const uint32_t LLVMPointerWidth; |
| 165 | Index_t extra; |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 166 | typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t; |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 167 | static llvm::DenseMap<CtorVtable_t, int64_t>& |
| 168 | AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l, |
| 169 | const CXXRecordDecl *c) { |
| 170 | CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l]; |
| 171 | if (oref == 0) |
| 172 | oref = new CodeGenModule::AddrMap_t; |
| 173 | |
| 174 | llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c]; |
| 175 | if (ref == 0) |
| 176 | ref = new llvm::DenseMap<CtorVtable_t, int64_t>; |
| 177 | return *ref; |
| 178 | } |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 179 | |
| 180 | /// getPureVirtualFn - Return the __cxa_pure_virtual function. |
| 181 | llvm::Constant* getPureVirtualFn() { |
| 182 | if (!PureVirtualFn) { |
| 183 | const llvm::FunctionType *Ty = |
| 184 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 185 | /*isVarArg=*/false); |
| 186 | PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual")); |
| 187 | } |
| 188 | |
| 189 | return PureVirtualFn; |
| 190 | } |
| 191 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 192 | public: |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 193 | VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c, |
| 194 | const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm) |
| 195 | : methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo), |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 196 | BLayout(cgm.getContext().getASTRecordLayout(l)), |
Mike Stump | c01c2b8 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 197 | rtti(cgm.GenerateRTTIRef(c)), VMContext(cgm.getModule().getContext()), |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 198 | CGM(cgm), PureVirtualFn(0),subAddressPoints(AllocAddressPoint(cgm, l, c)), |
Mike Stump | 1960b20 | 2009-11-19 00:49:05 +0000 | [diff] [blame] | 199 | Extern(!l->isInAnonymousNamespace()), |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 200 | LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 201 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
| 202 | } |
| 203 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 204 | llvm::DenseMap<GlobalDecl, Index_t> &getIndex() { return Index; } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 205 | llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex() |
| 206 | { return VBIndex; } |
| 207 | |
| 208 | llvm::Constant *wrap(Index_t i) { |
| 209 | llvm::Constant *m; |
| 210 | m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i); |
| 211 | return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 212 | } |
| 213 | |
| 214 | llvm::Constant *wrap(llvm::Constant *m) { |
| 215 | return llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 216 | } |
| 217 | |
Mike Stump | 8a96d3a | 2009-12-02 19:50:41 +0000 | [diff] [blame] | 218 | #define D1(x) |
| 219 | //#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0) |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 220 | |
| 221 | void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 222 | bool updateVBIndex, Index_t current_vbindex) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 223 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 224 | e = RD->bases_end(); i != e; ++i) { |
| 225 | const CXXRecordDecl *Base = |
| 226 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 227 | Index_t next_vbindex = current_vbindex; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 228 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 229 | SeenVBase.insert(Base); |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 230 | if (updateVBIndex) { |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 231 | next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8) |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 232 | - 3*LLVMPointerWidth/8); |
| 233 | VBIndex[Base] = next_vbindex; |
| 234 | } |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 235 | int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8; |
| 236 | VCalls.push_back((0?700:0) + BaseOffset); |
| 237 | D1(printf(" vbase for %s at %d delta %d most derived %s\n", |
| 238 | Base->getNameAsCString(), |
| 239 | (int)-VCalls.size()-3, (int)BaseOffset, |
| 240 | Class->getNameAsCString())); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 241 | } |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 242 | // We also record offsets for non-virtual bases to closest enclosing |
| 243 | // virtual base. We do this so that we don't have to search |
| 244 | // for the nearst virtual base class when generating thunks. |
| 245 | if (updateVBIndex && VBIndex.count(Base) == 0) |
| 246 | VBIndex[Base] = next_vbindex; |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 247 | GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | void StartNewTable() { |
| 252 | SeenVBase.clear(); |
| 253 | } |
| 254 | |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 255 | Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B, |
| 256 | Index_t Offset = 0) { |
| 257 | |
| 258 | if (B == D) |
| 259 | return Offset; |
| 260 | |
| 261 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D); |
| 262 | for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(), |
| 263 | e = D->bases_end(); i != e; ++i) { |
| 264 | const CXXRecordDecl *Base = |
| 265 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 266 | int64_t BaseOffset = 0; |
| 267 | if (!i->isVirtual()) |
| 268 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 269 | int64_t o = getNVOffset_1(Base, B, BaseOffset); |
| 270 | if (o >= 0) |
| 271 | return o; |
| 272 | } |
| 273 | |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | /// getNVOffset - Returns the non-virtual offset for the given (B) base of the |
| 278 | /// derived class D. |
| 279 | Index_t getNVOffset(QualType qB, QualType qD) { |
Mike Stump | 4627132 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 280 | qD = qD->getPointeeType(); |
| 281 | qB = qB->getPointeeType(); |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 282 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 283 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 284 | int64_t o = getNVOffset_1(D, B); |
| 285 | if (o >= 0) |
| 286 | return o; |
| 287 | |
| 288 | assert(false && "FIXME: non-virtual base not found"); |
| 289 | return 0; |
| 290 | } |
| 291 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 292 | /// getVbaseOffset - Returns the index into the vtable for the virtual base |
| 293 | /// offset for the given (B) virtual base of the derived class D. |
| 294 | Index_t getVbaseOffset(QualType qB, QualType qD) { |
Mike Stump | 4627132 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 295 | qD = qD->getPointeeType(); |
| 296 | qB = qB->getPointeeType(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 297 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 298 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 299 | if (D != Class) |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 300 | return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 301 | llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i; |
| 302 | i = VBIndex.find(B); |
| 303 | if (i != VBIndex.end()) |
| 304 | return i->second; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 305 | |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 306 | assert(false && "FIXME: Base not found"); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 310 | bool OverrideMethod(GlobalDecl GD, llvm::Constant *m, |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 311 | bool MorallyVirtual, Index_t OverrideOffset, |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 312 | Index_t Offset, int64_t CurrentVBaseOffset); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 313 | |
| 314 | void InstallThunks() { |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 315 | for (CovariantThunksMapTy::const_iterator i = CovariantThunks.begin(), |
| 316 | e = CovariantThunks.end(); i != e; ++i) { |
Anders Carlsson | 73295f9 | 2009-12-03 02:12:03 +0000 | [diff] [blame] | 317 | GlobalDecl GD = i->second.GD; |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 318 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 319 | if (MD->isPure()) |
| 320 | continue; |
| 321 | |
Anders Carlsson | 06c14b6 | 2009-12-03 02:16:14 +0000 | [diff] [blame] | 322 | uint64_t Index = i->first; |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 323 | const CovariantThunk &Thunk = i->second; |
Anders Carlsson | 06c14b6 | 2009-12-03 02:16:14 +0000 | [diff] [blame] | 324 | assert(Index == VtableBuilder::Index[GD] && "Thunk index mismatch!"); |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 325 | |
| 326 | // Check if there is an adjustment for the 'this' pointer. |
| 327 | ThunkAdjustment ThisAdjustment; |
Anders Carlsson | c6089fd | 2009-12-03 07:30:40 +0000 | [diff] [blame] | 328 | ThunksMapTy::iterator it = Thunks.find(Index); |
| 329 | if (it != Thunks.end()) { |
| 330 | ThisAdjustment = it->second.Adjustment; |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 331 | |
Anders Carlsson | c6089fd | 2009-12-03 07:30:40 +0000 | [diff] [blame] | 332 | Thunks.erase(it); |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 333 | } |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 334 | |
| 335 | // Construct the return adjustment. |
Anders Carlsson | dabfa3c | 2009-12-03 03:28:24 +0000 | [diff] [blame] | 336 | QualType DerivedType = |
| 337 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 338 | |
| 339 | int64_t NonVirtualAdjustment = |
| 340 | getNVOffset(Thunk.ReturnType, DerivedType) / 8; |
| 341 | |
| 342 | int64_t VirtualAdjustment = |
| 343 | getVbaseOffset(Thunk.ReturnType, DerivedType); |
| 344 | |
| 345 | ThunkAdjustment ReturnAdjustment(NonVirtualAdjustment, VirtualAdjustment); |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 346 | |
| 347 | CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment); |
Anders Carlsson | 06c14b6 | 2009-12-03 02:16:14 +0000 | [diff] [blame] | 348 | submethods[Index] = CGM.BuildCovariantThunk(MD, Extern, Adjustment); |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 349 | } |
| 350 | CovariantThunks.clear(); |
Anders Carlsson | c38b40a | 2009-12-03 02:03:29 +0000 | [diff] [blame] | 351 | |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 352 | for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 353 | i != e; ++i) { |
Anders Carlsson | 80bc5d5 | 2009-12-03 02:41:55 +0000 | [diff] [blame] | 354 | uint64_t Index = i->first; |
| 355 | const Thunk& Thunk = i->second; |
| 356 | |
| 357 | GlobalDecl GD = Thunk.GD; |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 358 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 359 | assert(!MD->isPure() && "Can't thunk pure virtual methods!"); |
Anders Carlsson | 29a1f75 | 2009-12-03 02:39:59 +0000 | [diff] [blame] | 360 | |
| 361 | assert(Index == VtableBuilder::Index[GD] && "Thunk index mismatch!"); |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 362 | |
Mike Stump | e2d4a2c | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 363 | submethods[Index] = CGM.BuildThunk(GD, Extern, Thunk.Adjustment); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 364 | } |
| 365 | Thunks.clear(); |
Anders Carlsson | 6d771bc | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 366 | |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 367 | for (PureVirtualMethodsSetTy::iterator i = PureVirtualMethods.begin(), |
| 368 | e = PureVirtualMethods.end(); i != e; ++i) { |
| 369 | GlobalDecl GD = *i; |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 370 | submethods[Index[GD]] = getPureVirtualFn(); |
Mike Stump | bb9ff05 | 2009-10-27 23:46:47 +0000 | [diff] [blame] | 371 | } |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 372 | PureVirtualMethods.clear(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 375 | llvm::Constant *WrapAddrOf(GlobalDecl GD) { |
| 376 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 377 | |
Anders Carlsson | 6445773 | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 378 | const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD); |
Mike Stump | 18e8b47 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 379 | |
Mike Stump | cdeb800 | 2009-12-03 16:55:20 +0000 | [diff] [blame] | 380 | return wrap(CGM.GetAddrOfFunction(GD, Ty)); |
Mike Stump | 18e8b47 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 383 | void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset, |
| 384 | int64_t CurrentVBaseOffset) { |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 385 | for (Path_t::reverse_iterator i = Path->rbegin(), |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 386 | e = Path->rend(); i != e; ++i) { |
| 387 | const CXXRecordDecl *RD = i->first; |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 388 | int64_t OverrideOffset = i->second; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 389 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 390 | ++mi) { |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 391 | const CXXMethodDecl *MD = *mi; |
| 392 | |
| 393 | if (!MD->isVirtual()) |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 394 | continue; |
| 395 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 396 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 397 | // Override both the complete and the deleting destructor. |
| 398 | GlobalDecl CompDtor(DD, Dtor_Complete); |
| 399 | OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual, |
| 400 | OverrideOffset, Offset, CurrentVBaseOffset); |
| 401 | |
| 402 | GlobalDecl DeletingDtor(DD, Dtor_Deleting); |
| 403 | OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual, |
| 404 | OverrideOffset, Offset, CurrentVBaseOffset); |
| 405 | } else { |
| 406 | OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset, |
| 407 | Offset, CurrentVBaseOffset); |
| 408 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 413 | void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 414 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 415 | llvm::Constant *m = WrapAddrOf(GD); |
Mike Stump | 18e8b47 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 416 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 417 | // If we can find a previously allocated slot for this, reuse it. |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 418 | if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 419 | CurrentVBaseOffset)) |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 420 | return; |
| 421 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 422 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 423 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 424 | // else allocate a new slot. |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 425 | Index[GD] = submethods.size(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 426 | submethods.push_back(m); |
Mike Stump | c5a332c | 2009-11-13 23:45:53 +0000 | [diff] [blame] | 427 | D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(), |
| 428 | (int)Index[GD])); |
Mike Stump | 375faa8 | 2009-10-28 00:35:46 +0000 | [diff] [blame] | 429 | if (MD->isPure()) |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 430 | PureVirtualMethods.insert(GD); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 431 | if (MorallyVirtual) { |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 432 | VCallOffset[GD] = Offset/8; |
| 433 | Index_t &idx = VCall[GD]; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 434 | // Allocate the first one, after that, we reuse the previous one. |
| 435 | if (idx == 0) { |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 436 | NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 437 | idx = VCalls.size()+1; |
| 438 | VCalls.push_back(0); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 439 | D1(printf(" vcall for %s at %d with delta %d\n", |
Mike Stump | c5a332c | 2009-11-13 23:45:53 +0000 | [diff] [blame] | 440 | MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0)); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 446 | Index_t Offset, int64_t CurrentVBaseOffset) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 447 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 448 | ++mi) { |
| 449 | const CXXMethodDecl *MD = *mi; |
| 450 | if (!MD->isVirtual()) |
| 451 | continue; |
| 452 | |
| 453 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 454 | // For destructors, add both the complete and the deleting destructor |
| 455 | // to the vtable. |
| 456 | AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 457 | CurrentVBaseOffset); |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 458 | AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset, |
| 459 | CurrentVBaseOffset); |
| 460 | } else |
| 461 | AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 462 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 466 | const CXXRecordDecl *PrimaryBase, |
| 467 | bool PrimaryBaseWasVirtual, bool MorallyVirtual, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 468 | int64_t Offset, int64_t CurrentVBaseOffset, |
| 469 | Path_t *Path) { |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 470 | Path->push_back(std::make_pair(RD, Offset)); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 471 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 472 | e = RD->bases_end(); i != e; ++i) { |
| 473 | if (i->isVirtual()) |
| 474 | continue; |
| 475 | const CXXRecordDecl *Base = |
| 476 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 477 | if (Base != PrimaryBase || PrimaryBaseWasVirtual) { |
| 478 | uint64_t o = Offset + Layout.getBaseClassOffset(Base); |
| 479 | StartNewTable(); |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 480 | GenerateVtableForBase(Base, o, MorallyVirtual, false, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 481 | CurrentVBaseOffset, Path); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 484 | Path->pop_back(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 487 | // #define D(X) do { X; } while (0) |
| 488 | #define D(X) |
| 489 | |
| 490 | void insertVCalls(int InsertionPoint) { |
| 491 | llvm::Constant *e = 0; |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 492 | D1(printf("============= combining vbase/vcall\n")); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 493 | D(VCalls.insert(VCalls.begin(), 673)); |
| 494 | D(VCalls.push_back(672)); |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 495 | methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 496 | // The vcalls come first... |
| 497 | for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(), |
| 498 | e = VCalls.rend(); |
| 499 | i != e; ++i) |
| 500 | methods[InsertionPoint++] = wrap((0?600:0) + *i); |
| 501 | VCalls.clear(); |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 502 | VCall.clear(); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 505 | void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset, |
| 506 | Index_t AddressPoint) { |
| 507 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
| 508 | RD->getNameAsCString(), Class->getNameAsCString(), |
| 509 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 510 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 511 | |
| 512 | // Now also add the address point for all our primary bases. |
| 513 | while (1) { |
| 514 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 515 | RD = Layout.getPrimaryBase(); |
| 516 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 517 | // FIXME: Double check this. |
| 518 | if (RD == 0) |
| 519 | break; |
| 520 | if (PrimaryBaseWasVirtual && |
| 521 | BLayout.getVBaseClassOffset(RD) != Offset) |
| 522 | break; |
| 523 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
| 524 | RD->getNameAsCString(), Class->getNameAsCString(), |
| 525 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 526 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 531 | Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 532 | const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual, |
| 533 | bool MorallyVirtual, int64_t Offset, bool ForVirtualBase, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 534 | int64_t CurrentVBaseOffset, |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 535 | Path_t *Path) { |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 536 | bool alloc = false; |
| 537 | if (Path == 0) { |
| 538 | alloc = true; |
| 539 | Path = new Path_t; |
| 540 | } |
| 541 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 542 | StartNewTable(); |
| 543 | extra = 0; |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 544 | bool DeferVCalls = MorallyVirtual || ForVirtualBase; |
| 545 | int VCallInsertionPoint = methods.size(); |
| 546 | if (!DeferVCalls) { |
| 547 | insertVCalls(VCallInsertionPoint); |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 548 | } else |
| 549 | // FIXME: just for extra, or for all uses of VCalls.size post this? |
| 550 | extra = -VCalls.size(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 551 | |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 552 | methods.push_back(wrap(-((Offset-LayoutOffset)/8))); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 553 | methods.push_back(rtti); |
| 554 | Index_t AddressPoint = methods.size(); |
| 555 | |
| 556 | InstallThunks(); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 557 | D1(printf("============= combining methods\n")); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 558 | methods.insert(methods.end(), submethods.begin(), submethods.end()); |
| 559 | submethods.clear(); |
| 560 | |
| 561 | // and then the non-virtual bases. |
| 562 | NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 563 | MorallyVirtual, Offset, CurrentVBaseOffset, Path); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 564 | |
| 565 | if (ForVirtualBase) { |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 566 | // FIXME: We're adding to VCalls in callers, we need to do the overrides |
| 567 | // in the inner part, so that we know the complete set of vcalls during |
| 568 | // the build and don't have to insert into methods. Saving out the |
| 569 | // AddressPoint here, would need to be fixed, if we didn't do that. Also |
| 570 | // retroactively adding vcalls for overrides later wind up in the wrong |
| 571 | // place, the vcall slot has to be alloted during the walk of the base |
| 572 | // when the function is first introduces. |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 573 | AddressPoint += VCalls.size(); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 574 | insertVCalls(VCallInsertionPoint); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 577 | AddAddressPoints(RD, Offset, AddressPoint); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 578 | |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 579 | if (alloc) { |
| 580 | delete Path; |
| 581 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 582 | return AddressPoint; |
| 583 | } |
| 584 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 585 | void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 586 | bool updateVBIndex, Index_t current_vbindex, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 587 | int64_t CurrentVBaseOffset) { |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 588 | if (!RD->isDynamicClass()) |
| 589 | return; |
| 590 | |
| 591 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 592 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 593 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 594 | |
| 595 | // vtables are composed from the chain of primaries. |
| 596 | if (PrimaryBase) { |
| 597 | D1(printf(" doing primaries for %s most derived %s\n", |
| 598 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 599 | |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 600 | int BaseCurrentVBaseOffset = CurrentVBaseOffset; |
| 601 | if (PrimaryBaseWasVirtual) |
| 602 | BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase); |
| 603 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 604 | if (!PrimaryBaseWasVirtual) |
| 605 | Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 606 | updateVBIndex, current_vbindex, BaseCurrentVBaseOffset); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | D1(printf(" doing vcall entries for %s most derived %s\n", |
| 610 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 611 | |
| 612 | // And add the virtuals for the class to the primary vtable. |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 613 | AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 617 | bool updateVBIndex, Index_t current_vbindex, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 618 | bool RDisVirtualBase, int64_t CurrentVBaseOffset, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 619 | bool bottom) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 620 | if (!RD->isDynamicClass()) |
| 621 | return; |
| 622 | |
| 623 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 624 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 625 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 626 | |
| 627 | // vtables are composed from the chain of primaries. |
| 628 | if (PrimaryBase) { |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 629 | int BaseCurrentVBaseOffset = CurrentVBaseOffset; |
| 630 | if (PrimaryBaseWasVirtual) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 631 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 632 | BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase); |
| 633 | } |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 634 | |
| 635 | D1(printf(" doing primaries for %s most derived %s\n", |
| 636 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 637 | |
| 638 | VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 639 | updateVBIndex, current_vbindex, PrimaryBaseWasVirtual, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 640 | BaseCurrentVBaseOffset, false); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 643 | D1(printf(" doing vbase entries for %s most derived %s\n", |
| 644 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 645 | GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex); |
| 646 | |
| 647 | if (RDisVirtualBase || bottom) { |
| 648 | Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 649 | CurrentVBaseOffset); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 650 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 653 | int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0, |
| 654 | bool MorallyVirtual = false, |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 655 | bool ForVirtualBase = false, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 656 | int CurrentVBaseOffset = 0, |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 657 | Path_t *Path = 0) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 658 | if (!RD->isDynamicClass()) |
| 659 | return 0; |
| 660 | |
Mike Stump | fa81808 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 661 | // Construction vtable don't need parts that have no virtual bases and |
| 662 | // aren't morally virtual. |
| 663 | if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual) |
| 664 | return 0; |
| 665 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 666 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 667 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 668 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 669 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 670 | extra = 0; |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 671 | D1(printf("building entries for base %s most derived %s\n", |
| 672 | RD->getNameAsCString(), Class->getNameAsCString())); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 673 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 674 | if (ForVirtualBase) |
| 675 | extra = VCalls.size(); |
| 676 | |
| 677 | VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 678 | CurrentVBaseOffset, true); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 679 | |
| 680 | if (Path) |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 681 | OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 682 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 683 | return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 684 | Offset, ForVirtualBase, CurrentVBaseOffset, Path); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void GenerateVtableForVBases(const CXXRecordDecl *RD, |
| 688 | int64_t Offset = 0, |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 689 | Path_t *Path = 0) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 690 | bool alloc = false; |
| 691 | if (Path == 0) { |
| 692 | alloc = true; |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 693 | Path = new Path_t; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 694 | } |
| 695 | // FIXME: We also need to override using all paths to a virtual base, |
| 696 | // right now, we just process the first path |
| 697 | Path->push_back(std::make_pair(RD, Offset)); |
| 698 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 699 | e = RD->bases_end(); i != e; ++i) { |
| 700 | const CXXRecordDecl *Base = |
| 701 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 702 | if (i->isVirtual() && !IndirectPrimary.count(Base)) { |
| 703 | // Mark it so we don't output it twice. |
| 704 | IndirectPrimary.insert(Base); |
| 705 | StartNewTable(); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 706 | VCall.clear(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 707 | int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 708 | int64_t CurrentVBaseOffset = BaseOffset; |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 709 | D1(printf("vtable %s virtual base %s\n", |
| 710 | Class->getNameAsCString(), Base->getNameAsCString())); |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 711 | GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 712 | Path); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 713 | } |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 714 | int64_t BaseOffset; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 715 | if (i->isVirtual()) |
| 716 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 717 | else { |
| 718 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 719 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 720 | } |
| 721 | |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 722 | if (Base->getNumVBases()) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 723 | GenerateVtableForVBases(Base, BaseOffset, Path); |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 724 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 725 | } |
| 726 | Path->pop_back(); |
| 727 | if (alloc) |
| 728 | delete Path; |
| 729 | } |
| 730 | }; |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 731 | } // end anonymous namespace |
| 732 | |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 733 | /// TypeConversionRequiresAdjustment - Returns whether conversion from a |
| 734 | /// derived type to a base type requires adjustment. |
| 735 | static bool |
| 736 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 737 | const CXXRecordDecl *DerivedDecl, |
| 738 | const CXXRecordDecl *BaseDecl) { |
| 739 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 740 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 741 | if (!const_cast<CXXRecordDecl *>(DerivedDecl)-> |
| 742 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) { |
| 743 | assert(false && "Class must be derived from the passed in base class!"); |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | // If we found a virtual base we always want to require adjustment. |
| 748 | if (Paths.getDetectedVirtual()) |
| 749 | return true; |
| 750 | |
| 751 | const CXXBasePath &Path = Paths.front(); |
| 752 | |
| 753 | for (size_t Start = 0, End = Path.size(); Start != End; ++Start) { |
| 754 | const CXXBasePathElement &Element = Path[Start]; |
| 755 | |
| 756 | // Check the base class offset. |
| 757 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class); |
| 758 | |
| 759 | const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); |
| 760 | const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 761 | |
| 762 | if (Layout.getBaseClassOffset(Base) != 0) { |
| 763 | // This requires an adjustment. |
| 764 | return true; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | static bool |
| 772 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 773 | QualType DerivedType, QualType BaseType) { |
| 774 | // Canonicalize the types. |
| 775 | QualType CanDerivedType = Ctx.getCanonicalType(DerivedType); |
| 776 | QualType CanBaseType = Ctx.getCanonicalType(BaseType); |
| 777 | |
| 778 | assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() && |
| 779 | "Types must have same type class!"); |
| 780 | |
| 781 | if (CanDerivedType == CanBaseType) { |
| 782 | // No adjustment needed. |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) { |
| 787 | CanDerivedType = RT->getPointeeType(); |
| 788 | CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType(); |
| 789 | } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) { |
| 790 | CanDerivedType = PT->getPointeeType(); |
| 791 | CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType(); |
| 792 | } else { |
| 793 | assert(false && "Unexpected return type!"); |
| 794 | } |
| 795 | |
| 796 | if (CanDerivedType == CanBaseType) { |
| 797 | // No adjustment needed. |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | const CXXRecordDecl *DerivedDecl = |
Anders Carlsson | dabfa3c | 2009-12-03 03:28:24 +0000 | [diff] [blame] | 802 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl()); |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 803 | |
| 804 | const CXXRecordDecl *BaseDecl = |
| 805 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl()); |
| 806 | |
| 807 | return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl); |
| 808 | } |
| 809 | |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 810 | bool VtableBuilder::OverrideMethod(GlobalDecl GD, llvm::Constant *m, |
| 811 | bool MorallyVirtual, Index_t OverrideOffset, |
| 812 | Index_t Offset, int64_t CurrentVBaseOffset) { |
| 813 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 814 | |
| 815 | const bool isPure = MD->isPure(); |
| 816 | typedef CXXMethodDecl::method_iterator meth_iter; |
| 817 | // FIXME: Should OverrideOffset's be Offset? |
| 818 | |
| 819 | // FIXME: Don't like the nested loops. For very large inheritance |
| 820 | // heirarchies we could have a table on the side with the final overridder |
| 821 | // and just replace each instance of an overridden method once. Would be |
| 822 | // nice to measure the cost/benefit on real code. |
| 823 | |
| 824 | for (meth_iter mi = MD->begin_overridden_methods(), |
| 825 | e = MD->end_overridden_methods(); |
| 826 | mi != e; ++mi) { |
| 827 | GlobalDecl OGD; |
| 828 | |
| 829 | const CXXMethodDecl *OMD = *mi; |
| 830 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD)) |
| 831 | OGD = GlobalDecl(DD, GD.getDtorType()); |
| 832 | else |
| 833 | OGD = OMD; |
| 834 | |
| 835 | llvm::Constant *om; |
| 836 | om = WrapAddrOf(OGD); |
| 837 | om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty); |
| 838 | |
| 839 | for (Index_t i = 0, e = submethods.size(); |
| 840 | i != e; ++i) { |
| 841 | // FIXME: begin_overridden_methods might be too lax, covariance */ |
| 842 | if (submethods[i] != om) |
| 843 | continue; |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 844 | |
| 845 | QualType ReturnType = |
| 846 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 847 | QualType OverriddenReturnType = |
| 848 | OMD->getType()->getAs<FunctionType>()->getResultType(); |
| 849 | |
| 850 | // Check if we need a return type adjustment. |
| 851 | if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType, |
| 852 | OverriddenReturnType)) { |
| 853 | CovariantThunk &Adjustment = CovariantThunks[i]; |
Anders Carlsson | 2ca285f | 2009-12-03 02:22:59 +0000 | [diff] [blame] | 854 | |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 855 | // Get the canonical return type. |
| 856 | CanQualType CanReturnType = |
| 857 | CGM.getContext().getCanonicalType(ReturnType); |
| 858 | |
| 859 | // Insert the base return type. |
| 860 | if (Adjustment.ReturnType.isNull()) |
| 861 | Adjustment.ReturnType = |
| 862 | CGM.getContext().getCanonicalType(OverriddenReturnType); |
| 863 | |
| 864 | Adjustment.GD = GD; |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 865 | } |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 866 | |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 867 | Index[GD] = i; |
| 868 | submethods[i] = m; |
| 869 | if (isPure) |
| 870 | PureVirtualMethods.insert(GD); |
| 871 | PureVirtualMethods.erase(OGD); |
Anders Carlsson | 29a1f75 | 2009-12-03 02:39:59 +0000 | [diff] [blame] | 872 | Thunks.erase(i); |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 873 | if (MorallyVirtual || VCall.count(OGD)) { |
| 874 | Index_t &idx = VCall[OGD]; |
| 875 | if (idx == 0) { |
| 876 | NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8; |
| 877 | VCallOffset[GD] = OverrideOffset/8; |
| 878 | idx = VCalls.size()+1; |
| 879 | VCalls.push_back(0); |
| 880 | D1(printf(" vcall for %s at %d with delta %d most derived %s\n", |
| 881 | MD->getNameAsString().c_str(), (int)-idx-3, |
| 882 | (int)VCalls[idx-1], Class->getNameAsCString())); |
| 883 | } else { |
| 884 | NonVirtualOffset[GD] = NonVirtualOffset[OGD]; |
| 885 | VCallOffset[GD] = VCallOffset[OGD]; |
| 886 | VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8; |
| 887 | D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n", |
| 888 | MD->getNameAsString().c_str(), (int)-idx-3, |
| 889 | (int)VCalls[idx-1], Class->getNameAsCString())); |
| 890 | } |
| 891 | VCall[GD] = idx; |
| 892 | int64_t NonVirtualAdjustment = NonVirtualOffset[GD]; |
| 893 | int64_t VirtualAdjustment = |
| 894 | -((idx + extra + 2) * LLVMPointerWidth / 8); |
| 895 | |
| 896 | // Optimize out virtual adjustments of 0. |
| 897 | if (VCalls[idx-1] == 0) |
| 898 | VirtualAdjustment = 0; |
| 899 | |
| 900 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, |
| 901 | VirtualAdjustment); |
| 902 | |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 903 | if (!isPure && !ThisAdjustment.isEmpty()) |
Anders Carlsson | 80bc5d5 | 2009-12-03 02:41:55 +0000 | [diff] [blame] | 904 | Thunks[i] = Thunk(GD, ThisAdjustment); |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 905 | return true; |
| 906 | } |
| 907 | |
| 908 | // FIXME: finish off |
| 909 | int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8; |
| 910 | |
Anders Carlsson | 2ca285f | 2009-12-03 02:22:59 +0000 | [diff] [blame] | 911 | if (NonVirtualAdjustment) { |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 912 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0); |
| 913 | |
Anders Carlsson | 2bd3c0f | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 914 | if (!isPure) |
Anders Carlsson | 80bc5d5 | 2009-12-03 02:41:55 +0000 | [diff] [blame] | 915 | Thunks[i] = Thunk(GD, ThisAdjustment); |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 916 | } |
| 917 | return true; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | return false; |
Anders Carlsson | d5988502 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 924 | void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) { |
| 925 | |
| 926 | // Itanium C++ ABI 2.5.2: |
| 927 | // The order of the virtual function pointers in a virtual table is the |
| 928 | // order of declaration of the corresponding member functions in the class. |
| 929 | // |
| 930 | // There is an entry for any virtual function declared in a class, |
| 931 | // whether it is a new function or overrides a base class function, |
| 932 | // unless it overrides a function from the primary base, and conversion |
| 933 | // between their return types does not require an adjustment. |
| 934 | |
| 935 | int64_t CurrentIndex = 0; |
| 936 | |
| 937 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 938 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 939 | |
| 940 | if (PrimaryBase) { |
Anders Carlsson | c920fa2 | 2009-11-30 19:43:26 +0000 | [diff] [blame] | 941 | assert(PrimaryBase->isDefinition() && |
| 942 | "Should have the definition decl of the primary base!"); |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 943 | |
| 944 | // Since the record decl shares its vtable pointer with the primary base |
| 945 | // we need to start counting at the end of the primary base's vtable. |
| 946 | CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); |
| 947 | } |
| 948 | |
| 949 | const CXXDestructorDecl *ImplicitVirtualDtor = 0; |
| 950 | |
| 951 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 952 | e = RD->method_end(); i != e; ++i) { |
| 953 | const CXXMethodDecl *MD = *i; |
| 954 | |
| 955 | // We only want virtual methods. |
| 956 | if (!MD->isVirtual()) |
| 957 | continue; |
| 958 | |
| 959 | bool ShouldAddEntryForMethod = true; |
| 960 | |
| 961 | // Check if this method overrides a method in the primary base. |
| 962 | for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(), |
| 963 | e = MD->end_overridden_methods(); i != e; ++i) { |
| 964 | const CXXMethodDecl *OverriddenMD = *i; |
| 965 | const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent(); |
| 966 | assert(OverriddenMD->isCanonicalDecl() && |
| 967 | "Should have the canonical decl of the overridden RD!"); |
| 968 | |
| 969 | if (OverriddenRD == PrimaryBase) { |
| 970 | // Check if converting from the return type of the method to the |
| 971 | // return type of the overridden method requires conversion. |
| 972 | QualType ReturnType = |
| 973 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 974 | QualType OverriddenReturnType = |
| 975 | OverriddenMD->getType()->getAs<FunctionType>()->getResultType(); |
| 976 | |
| 977 | if (!TypeConversionRequiresAdjustment(CGM.getContext(), |
| 978 | ReturnType, OverriddenReturnType)) { |
| 979 | // This index is shared between the index in the vtable of the primary |
| 980 | // base class. |
| 981 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 982 | const CXXDestructorDecl *OverriddenDD = |
| 983 | cast<CXXDestructorDecl>(OverriddenMD); |
| 984 | |
| 985 | // Add both the complete and deleting entries. |
| 986 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = |
| 987 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); |
| 988 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = |
| 989 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); |
| 990 | } else { |
| 991 | MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD); |
| 992 | } |
| 993 | |
| 994 | // We don't need to add an entry for this method. |
| 995 | ShouldAddEntryForMethod = false; |
| 996 | break; |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | if (!ShouldAddEntryForMethod) |
| 1002 | continue; |
| 1003 | |
| 1004 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1005 | if (MD->isImplicit()) { |
| 1006 | assert(!ImplicitVirtualDtor && |
| 1007 | "Did already see an implicit virtual dtor!"); |
| 1008 | ImplicitVirtualDtor = DD; |
| 1009 | continue; |
| 1010 | } |
| 1011 | |
| 1012 | // Add the complete dtor. |
| 1013 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; |
| 1014 | |
| 1015 | // Add the deleting dtor. |
| 1016 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; |
| 1017 | } else { |
| 1018 | // Add the entry. |
| 1019 | MethodVtableIndices[MD] = CurrentIndex++; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | if (ImplicitVirtualDtor) { |
| 1024 | // Itanium C++ ABI 2.5.2: |
| 1025 | // If a class has an implicitly-defined virtual destructor, |
| 1026 | // its entries come after the declared virtual function pointers. |
| 1027 | |
| 1028 | // Add the complete dtor. |
| 1029 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = |
| 1030 | CurrentIndex++; |
| 1031 | |
| 1032 | // Add the deleting dtor. |
| 1033 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = |
| 1034 | CurrentIndex++; |
| 1035 | } |
| 1036 | |
| 1037 | NumVirtualFunctionPointers[RD] = CurrentIndex; |
| 1038 | } |
| 1039 | |
| 1040 | uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { |
| 1041 | llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 1042 | NumVirtualFunctionPointers.find(RD); |
| 1043 | if (I != NumVirtualFunctionPointers.end()) |
| 1044 | return I->second; |
| 1045 | |
| 1046 | ComputeMethodVtableIndices(RD); |
| 1047 | |
| 1048 | I = NumVirtualFunctionPointers.find(RD); |
| 1049 | assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); |
| 1050 | return I->second; |
| 1051 | } |
| 1052 | |
| 1053 | uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) { |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1054 | MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1055 | if (I != MethodVtableIndices.end()) |
| 1056 | return I->second; |
| 1057 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1058 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1059 | |
| 1060 | ComputeMethodVtableIndices(RD); |
| 1061 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1062 | I = MethodVtableIndices.find(GD); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1063 | assert(I != MethodVtableIndices.end() && "Did not find index!"); |
| 1064 | return I->second; |
| 1065 | } |
| 1066 | |
| 1067 | int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD, |
| 1068 | const CXXRecordDecl *VBase) { |
| 1069 | ClassPairTy ClassPair(RD, VBase); |
| 1070 | |
| 1071 | VirtualBaseClassIndiciesTy::iterator I = |
| 1072 | VirtualBaseClassIndicies.find(ClassPair); |
| 1073 | if (I != VirtualBaseClassIndicies.end()) |
| 1074 | return I->second; |
| 1075 | |
| 1076 | std::vector<llvm::Constant *> methods; |
| 1077 | // FIXME: This seems expensive. Can we do a partial job to get |
| 1078 | // just this data. |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 1079 | VtableBuilder b(methods, RD, RD, 0, CGM); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1080 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1081 | b.GenerateVtableForBase(RD); |
| 1082 | b.GenerateVtableForVBases(RD); |
| 1083 | |
| 1084 | for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 1085 | b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) { |
| 1086 | // Insert all types. |
| 1087 | ClassPairTy ClassPair(RD, I->first); |
| 1088 | |
| 1089 | VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second)); |
| 1090 | } |
| 1091 | |
| 1092 | I = VirtualBaseClassIndicies.find(ClassPair); |
| 1093 | assert(I != VirtualBaseClassIndicies.end() && "Did not find index!"); |
| 1094 | |
| 1095 | return I->second; |
| 1096 | } |
| 1097 | |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1098 | llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass, |
| 1099 | const CXXRecordDecl *RD, |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1100 | uint64_t Offset) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1101 | llvm::SmallString<256> OutName; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1102 | if (LayoutClass != RD) |
Daniel Dunbar | e128dd1 | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1103 | getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName); |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1104 | else |
Daniel Dunbar | e128dd1 | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1105 | getMangleContext().mangleCXXVtable(RD, OutName); |
| 1106 | llvm::StringRef Name = OutName.str(); |
Benjamin Kramer | bb0a07b | 2009-10-11 22:57:54 +0000 | [diff] [blame] | 1107 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1108 | std::vector<llvm::Constant *> methods; |
| 1109 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
| 1110 | int64_t AddressPoint; |
| 1111 | |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1112 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name); |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1113 | if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) { |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1114 | AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD, |
| 1115 | Offset)]; |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1116 | // FIXME: We can never have 0 address point. Do this for now so gepping |
| 1117 | // retains the same structure. Later, we'll just assert. |
| 1118 | if (AddressPoint == 0) |
| 1119 | AddressPoint = 1; |
| 1120 | } else { |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1121 | VtableBuilder b(methods, RD, LayoutClass, Offset, *this); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1122 | |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1123 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
| 1124 | // First comes the vtables for all the non-virtual bases... |
| 1125 | AddressPoint = b.GenerateVtableForBase(RD, Offset); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1126 | |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1127 | // then the vtables for all the virtual bases. |
| 1128 | b.GenerateVtableForVBases(RD, Offset); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1129 | |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1130 | bool CreateDefinition = true; |
| 1131 | if (LayoutClass != RD) |
| 1132 | CreateDefinition = true; |
| 1133 | else { |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1134 | const ASTRecordLayout &Layout = |
| 1135 | getContext().getASTRecordLayout(LayoutClass); |
| 1136 | |
| 1137 | if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) { |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1138 | if (!KeyFunction->getBody()) { |
| 1139 | // If there is a KeyFunction, and it isn't defined, just build a |
| 1140 | // reference to the vtable. |
| 1141 | CreateDefinition = false; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | llvm::Constant *C = 0; |
| 1147 | llvm::Type *type = Ptr8Ty; |
| 1148 | llvm::GlobalVariable::LinkageTypes linktype |
| 1149 | = llvm::GlobalValue::ExternalLinkage; |
| 1150 | if (CreateDefinition) { |
| 1151 | llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size()); |
| 1152 | C = llvm::ConstantArray::get(ntype, methods); |
| 1153 | linktype = llvm::GlobalValue::LinkOnceODRLinkage; |
| 1154 | if (LayoutClass->isInAnonymousNamespace()) |
| 1155 | linktype = llvm::GlobalValue::InternalLinkage; |
| 1156 | type = ntype; |
| 1157 | } |
| 1158 | llvm::GlobalVariable *OGV = GV; |
| 1159 | GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name); |
| 1160 | if (OGV) { |
| 1161 | GV->takeName(OGV); |
| 1162 | llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV, |
| 1163 | OGV->getType()); |
| 1164 | OGV->replaceAllUsesWith(NewPtr); |
| 1165 | OGV->eraseFromParent(); |
| 1166 | } |
| 1167 | bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 1168 | if (Hidden) |
| 1169 | GV->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
| 1170 | } |
Mike Stump | c0f632d | 2009-11-18 04:00:48 +0000 | [diff] [blame] | 1171 | llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty); |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1172 | llvm::Constant *AddressPointC; |
| 1173 | uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0); |
| 1174 | AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
| 1175 | AddressPoint*LLVMPointerWidth/8); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1176 | vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC, |
| 1177 | 1); |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1178 | |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1179 | assert(vtable->getType() == Ptr8Ty); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1180 | return vtable; |
| 1181 | } |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1182 | |
Mike Stump | ae1b85d | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 1183 | namespace { |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1184 | class VTTBuilder { |
| 1185 | /// Inits - The list of values built for the VTT. |
| 1186 | std::vector<llvm::Constant *> &Inits; |
| 1187 | /// Class - The most derived class that this vtable is being built for. |
| 1188 | const CXXRecordDecl *Class; |
| 1189 | CodeGenModule &CGM; // Per-module state. |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1190 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
Mike Stump | c7b9f5e | 2009-11-11 03:08:24 +0000 | [diff] [blame] | 1191 | /// BLayout - Layout for the most derived class that this vtable is being |
| 1192 | /// built for. |
| 1193 | const ASTRecordLayout &BLayout; |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1194 | CodeGenModule::AddrMap_t &AddressPoints; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1195 | // vtbl - A pointer to the vtable for Class. |
| 1196 | llvm::Constant *ClassVtbl; |
| 1197 | llvm::LLVMContext &VMContext; |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1198 | |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1199 | /// BuildVtablePtr - Build up a referene to the given secondary vtable |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1200 | llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl, |
| 1201 | const CXXRecordDecl *VtblClass, |
| 1202 | const CXXRecordDecl *RD, |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1203 | uint64_t Offset) { |
| 1204 | int64_t AddressPoint; |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1205 | AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)]; |
Mike Stump | 2b34bc5 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1206 | // FIXME: We can never have 0 address point. Do this for now so gepping |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1207 | // retains the same structure. Later we'll just assert. |
Mike Stump | 2b34bc5 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1208 | if (AddressPoint == 0) |
| 1209 | AddressPoint = 1; |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1210 | D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n", |
| 1211 | RD->getNameAsCString(), VtblClass->getNameAsCString(), |
| 1212 | Class->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1213 | uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0); |
| 1214 | llvm::Constant *init; |
| 1215 | init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
| 1216 | AddressPoint*LLVMPointerWidth/8); |
| 1217 | init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1); |
| 1218 | return init; |
| 1219 | } |
| 1220 | |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1221 | /// Secondary - Add the secondary vtable pointers to Inits. Offset is the |
| 1222 | /// current offset in bits to the object we're working on. |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1223 | void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl, |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1224 | const CXXRecordDecl *VtblClass, uint64_t Offset=0, |
| 1225 | bool MorallyVirtual=false) { |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1226 | if (RD->getNumVBases() == 0 && ! MorallyVirtual) |
| 1227 | return; |
| 1228 | |
| 1229 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1230 | e = RD->bases_end(); i != e; ++i) { |
| 1231 | const CXXRecordDecl *Base = |
| 1232 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1233 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1234 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1235 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1236 | bool NonVirtualPrimaryBase; |
| 1237 | NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase; |
| 1238 | bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual(); |
Mike Stump | c7b9f5e | 2009-11-11 03:08:24 +0000 | [diff] [blame] | 1239 | uint64_t BaseOffset; |
| 1240 | if (!i->isVirtual()) { |
| 1241 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1242 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 1243 | } else |
| 1244 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 2b34bc5 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1245 | llvm::Constant *subvtbl = vtbl; |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1246 | const CXXRecordDecl *subVtblClass = VtblClass; |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1247 | if ((Base->getNumVBases() || BaseMorallyVirtual) |
| 1248 | && !NonVirtualPrimaryBase) { |
| 1249 | // FIXME: Slightly too many of these for __ZTT8test8_B2 |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1250 | llvm::Constant *init; |
Mike Stump | 2b34bc5 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1251 | if (BaseMorallyVirtual) |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1252 | init = BuildVtablePtr(vtbl, VtblClass, RD, Offset); |
Mike Stump | 2b34bc5 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1253 | else { |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1254 | init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset); |
Mike Stump | 2b34bc5 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1255 | subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0)); |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1256 | subVtblClass = Base; |
Mike Stump | 2b34bc5 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1257 | } |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1258 | Inits.push_back(init); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1259 | } |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1260 | Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1264 | /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the |
| 1265 | /// currnet object we're working on. |
| 1266 | void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) { |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1267 | if (RD->getNumVBases() == 0 && !MorallyVirtual) |
| 1268 | return; |
| 1269 | |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1270 | llvm::Constant *init; |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1271 | const CXXRecordDecl *VtblClass; |
| 1272 | |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1273 | // First comes the primary virtual table pointer... |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1274 | if (MorallyVirtual) { |
| 1275 | init = BuildVtablePtr(ClassVtbl, Class, RD, Offset); |
| 1276 | VtblClass = Class; |
| 1277 | } else { |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1278 | init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset); |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1279 | VtblClass = RD; |
| 1280 | } |
Mike Stump | 8677bc2 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1281 | llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0)); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1282 | Inits.push_back(init); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1283 | |
| 1284 | // then the secondary VTTs.... |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1285 | SecondaryVTTs(RD, Offset, MorallyVirtual); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1286 | |
| 1287 | // and last the secondary vtable pointers. |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1288 | Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are |
| 1292 | /// built from each direct non-virtual proper base that requires a VTT in |
| 1293 | /// declaration order. |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1294 | void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0, |
| 1295 | bool MorallyVirtual=false) { |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1296 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1297 | e = RD->bases_end(); i != e; ++i) { |
| 1298 | const CXXRecordDecl *Base = |
| 1299 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1300 | if (i->isVirtual()) |
| 1301 | continue; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1302 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1303 | uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 1304 | BuildVTT(Base, BaseOffset, MorallyVirtual); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance |
| 1309 | /// graph preorder. |
| 1310 | void VirtualVTTs(const CXXRecordDecl *RD) { |
| 1311 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1312 | e = RD->bases_end(); i != e; ++i) { |
| 1313 | const CXXRecordDecl *Base = |
| 1314 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1315 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 1316 | SeenVBase.insert(Base); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1317 | uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
| 1318 | BuildVTT(Base, BaseOffset, true); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1319 | } |
| 1320 | VirtualVTTs(Base); |
| 1321 | } |
| 1322 | } |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1323 | public: |
| 1324 | VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c, |
Mike Stump | c7b9f5e | 2009-11-11 03:08:24 +0000 | [diff] [blame] | 1325 | CodeGenModule &cgm) |
| 1326 | : Inits(inits), Class(c), CGM(cgm), |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1327 | BLayout(cgm.getContext().getASTRecordLayout(c)), |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1328 | AddressPoints(*cgm.AddressPoints[c]), |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1329 | VMContext(cgm.getModule().getContext()) { |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1330 | |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1331 | // First comes the primary virtual table pointer for the complete class... |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1332 | ClassVtbl = CGM.getVtableInfo().getVtable(Class); |
| 1333 | Inits.push_back(ClassVtbl); |
| 1334 | ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0)); |
| 1335 | |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1336 | // then the secondary VTTs... |
| 1337 | SecondaryVTTs(Class); |
| 1338 | |
| 1339 | // then the secondary vtable pointers... |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1340 | Secondary(Class, ClassVtbl, Class); |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1341 | |
| 1342 | // and last, the virtual VTTs. |
| 1343 | VirtualVTTs(Class); |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1344 | } |
| 1345 | }; |
Mike Stump | ae1b85d | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 1346 | } |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1347 | |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1348 | llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) { |
Mike Stump | b472221 | 2009-11-10 19:13:04 +0000 | [diff] [blame] | 1349 | // Only classes that have virtual bases need a VTT. |
| 1350 | if (RD->getNumVBases() == 0) |
| 1351 | return 0; |
| 1352 | |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1353 | llvm::SmallString<256> OutName; |
Daniel Dunbar | e128dd1 | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1354 | getMangleContext().mangleCXXVTT(RD, OutName); |
| 1355 | llvm::StringRef Name = OutName.str(); |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1356 | |
| 1357 | llvm::GlobalVariable::LinkageTypes linktype; |
| 1358 | linktype = llvm::GlobalValue::LinkOnceODRLinkage; |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1359 | if (RD->isInAnonymousNamespace()) |
| 1360 | linktype = llvm::GlobalValue::InternalLinkage; |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1361 | std::vector<llvm::Constant *> inits; |
| 1362 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
| 1363 | |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1364 | D1(printf("vtt %s\n", RD->getNameAsCString())); |
| 1365 | |
Mike Stump | 8b2d2d0 | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1366 | VTTBuilder b(inits, RD, *this); |
| 1367 | |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1368 | llvm::Constant *C; |
| 1369 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size()); |
| 1370 | C = llvm::ConstantArray::get(type, inits); |
Mike Stump | c0f632d | 2009-11-18 04:00:48 +0000 | [diff] [blame] | 1371 | llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true, |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1372 | linktype, C, Name); |
Mike Stump | c0f632d | 2009-11-18 04:00:48 +0000 | [diff] [blame] | 1373 | bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 1374 | if (Hidden) |
| 1375 | vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
| 1376 | return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty); |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1377 | } |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1378 | |
Mike Stump | 1a139f8 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 1379 | void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) { |
| 1380 | Vtables[RD] = CGM.GenerateVtable(RD, RD); |
Mike Stump | c01c2b8 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1381 | CGM.GenerateRTTI(RD); |
Mike Stump | 1a139f8 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 1382 | CGM.GenerateVTT(RD); |
| 1383 | } |
| 1384 | |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1385 | llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) { |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1386 | llvm::Constant *&vtbl = Vtables[RD]; |
| 1387 | if (vtbl) |
| 1388 | return vtbl; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1389 | vtbl = CGM.GenerateVtable(RD, RD); |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1390 | |
| 1391 | bool CreateDefinition = true; |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1392 | |
| 1393 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1394 | if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) { |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1395 | if (!KeyFunction->getBody()) { |
| 1396 | // If there is a KeyFunction, and it isn't defined, just build a |
| 1397 | // reference to the vtable. |
| 1398 | CreateDefinition = false; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | if (CreateDefinition) { |
Mike Stump | c01c2b8 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1403 | CGM.GenerateRTTI(RD); |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1404 | CGM.GenerateVTT(RD); |
| 1405 | } |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1406 | return vtbl; |
| 1407 | } |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1408 | |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1409 | llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass, |
| 1410 | const CXXRecordDecl *RD, |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1411 | uint64_t Offset) { |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1412 | return CGM.GenerateVtable(LayoutClass, RD, Offset); |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1413 | } |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1414 | |
| 1415 | void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) { |
| 1416 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 1417 | const CXXRecordDecl *RD = MD->getParent(); |
| 1418 | |
| 1419 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1420 | |
| 1421 | // Get the key function. |
| 1422 | const CXXMethodDecl *KeyFunction = Layout.getKeyFunction(); |
| 1423 | |
| 1424 | if (!KeyFunction) { |
| 1425 | // If there's no key function, we don't want to emit the vtable here. |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | // Check if we have the key function. |
| 1430 | if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) |
| 1431 | return; |
| 1432 | |
| 1433 | // If the key function is a destructor, we only want to emit the vtable once, |
| 1434 | // so do it for the complete destructor. |
| 1435 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Complete) |
| 1436 | return; |
| 1437 | |
| 1438 | // Emit the data. |
| 1439 | GenerateClassData(RD); |
| 1440 | } |
| 1441 | |