clang API Documentation

CGVTT.cpp
Go to the documentation of this file.
00001 //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This contains code dealing with C++ code generation of VTTs (vtable tables).
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "CodeGenModule.h"
00015 #include "CGCXXABI.h"
00016 #include "clang/AST/RecordLayout.h"
00017 #include "clang/AST/VTTBuilder.h"
00018 using namespace clang;
00019 using namespace CodeGen;
00020 
00021 static llvm::Constant *
00022 GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM,
00023                    const CXXRecordDecl *MostDerivedClass,
00024                    const VTTVTable &VTable,
00025                    llvm::GlobalVariable::LinkageTypes Linkage,
00026                    llvm::DenseMap<BaseSubobject, uint64_t> &AddressPoints) {
00027   if (VTable.getBase() == MostDerivedClass) {
00028     assert(VTable.getBaseOffset().isZero() &&
00029            "Most derived class vtable must have a zero offset!");
00030     // This is a regular vtable.
00031     return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
00032   }
00033   
00034   return CGVT.GenerateConstructionVTable(MostDerivedClass, 
00035                                          VTable.getBaseSubobject(),
00036                                          VTable.isVirtual(),
00037                                          Linkage,
00038                                          AddressPoints);
00039 }
00040 
00041 void
00042 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
00043                                   llvm::GlobalVariable::LinkageTypes Linkage,
00044                                   const CXXRecordDecl *RD) {
00045   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
00046 
00047   llvm::Type *Int8PtrTy = CGM.Int8PtrTy, *Int64Ty = CGM.Int64Ty;
00048   llvm::ArrayType *ArrayType = 
00049     llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
00050   
00051   SmallVector<llvm::Constant *, 8> VTables;
00052   SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
00053   for (const VTTVTable *i = Builder.getVTTVTables().begin(),
00054                        *e = Builder.getVTTVTables().end(); i != e; ++i) {
00055     VTableAddressPoints.push_back(VTableAddressPointsMapTy());
00056     VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
00057                                          VTableAddressPoints.back()));
00058   }
00059 
00060   SmallVector<llvm::Constant *, 8> VTTComponents;
00061   for (const VTTComponent *i = Builder.getVTTComponents().begin(),
00062                           *e = Builder.getVTTComponents().end(); i != e; ++i) {
00063     const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
00064     llvm::Constant *VTable = VTables[i->VTableIndex];
00065     uint64_t AddressPoint;
00066     if (VTTVT.getBase() == RD) {
00067       // Just get the address point for the regular vtable.
00068       AddressPoint =
00069           getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
00070               i->VTableBase);
00071       assert(AddressPoint != 0 && "Did not find vtable address point!");
00072     } else {
00073       AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
00074       assert(AddressPoint != 0 && "Did not find ctor vtable address point!");
00075     }
00076 
00077      llvm::Value *Idxs[] = {
00078        llvm::ConstantInt::get(Int64Ty, 0),
00079        llvm::ConstantInt::get(Int64Ty, AddressPoint)
00080      };
00081 
00082      llvm::Constant *Init = 
00083        llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Idxs);
00084 
00085      Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
00086 
00087      VTTComponents.push_back(Init);
00088   }
00089 
00090   llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
00091 
00092   VTT->setInitializer(Init);
00093 
00094   // Set the correct linkage.
00095   VTT->setLinkage(Linkage);
00096 
00097   // Set the right visibility.
00098   CGM.setGlobalVisibility(VTT, RD);
00099 }
00100 
00101 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
00102   assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
00103 
00104   SmallString<256> OutName;
00105   llvm::raw_svector_ostream Out(OutName);
00106   cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
00107       .mangleCXXVTT(RD, Out);
00108   Out.flush();
00109   StringRef Name = OutName.str();
00110 
00111   // This will also defer the definition of the VTT.
00112   (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
00113 
00114   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
00115 
00116   llvm::ArrayType *ArrayType = 
00117     llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
00118 
00119   llvm::GlobalVariable *GV =
00120     CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, 
00121                                           llvm::GlobalValue::ExternalLinkage);
00122   GV->setUnnamedAddr(true);
00123   return GV;
00124 }
00125 
00126 uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD, 
00127                                         BaseSubobject Base) {
00128   BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
00129 
00130   SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
00131   if (I != SubVTTIndicies.end())
00132     return I->second;
00133   
00134   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
00135 
00136   for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
00137        Builder.getSubVTTIndicies().begin(), 
00138        E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
00139     // Insert all indices.
00140     BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
00141     
00142     SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
00143   }
00144     
00145   I = SubVTTIndicies.find(ClassSubobjectPair);
00146   assert(I != SubVTTIndicies.end() && "Did not find index!");
00147   
00148   return I->second;
00149 }
00150 
00151 uint64_t 
00152 CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
00153                                                 BaseSubobject Base) {
00154   SecondaryVirtualPointerIndicesMapTy::iterator I =
00155     SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
00156 
00157   if (I != SecondaryVirtualPointerIndices.end())
00158     return I->second;
00159 
00160   VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
00161 
00162   // Insert all secondary vpointer indices.
00163   for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I = 
00164        Builder.getSecondaryVirtualPointerIndices().begin(),
00165        E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
00166     std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
00167       std::make_pair(RD, I->first);
00168     
00169     SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
00170   }
00171 
00172   I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
00173   assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
00174   
00175   return I->second;
00176 }
00177