Addressed the comments.
Hi rnk,
http://llvm-reviews.chandlerc.com/D1787
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D1787?vs=4626&id=4667#toc
Files:
include/clang/Basic/ABI.h
lib/AST/MicrosoftMangle.cpp
lib/AST/VTableBuilder.cpp
lib/CodeGen/CGCXXABI.h
lib/CodeGen/CGVTables.cpp
lib/CodeGen/CGVTables.h
lib/CodeGen/ItaniumCXXABI.cpp
lib/CodeGen/MicrosoftCXXABI.cpp
test/CodeGenCXX/microsoft-abi-thunks.cpp
test/CodeGenCXX/microsoft-abi-virtual-inheritance.cpp
test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance.cpp
Index: include/clang/Basic/ABI.h
===================================================================
--- include/clang/Basic/ABI.h
+++ include/clang/Basic/ABI.h
@@ -103,10 +103,14 @@
/// \brief The return adjustment.
ReturnAdjustment Return;
- ThunkInfo() { }
+ /// \brief Optional ABI-specific data.
+ const void *OpaqueData;
- ThunkInfo(const ThisAdjustment &This, const ReturnAdjustment &Return)
- : This(This), Return(Return) { }
+ ThunkInfo() : OpaqueData(0) { }
+
+ ThunkInfo(const ThisAdjustment &This, const ReturnAdjustment &Return,
+ const void *OpaqueData = 0)
+ : This(This), Return(Return), OpaqueData(OpaqueData) {}
friend bool operator==(const ThunkInfo &LHS, const ThunkInfo &RHS) {
return LHS.This == RHS.This && LHS.Return == RHS.Return;
Index: lib/AST/MicrosoftMangle.cpp
===================================================================
--- lib/AST/MicrosoftMangle.cpp
+++ lib/AST/MicrosoftMangle.cpp
@@ -15,6 +15,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/CharUnits.h"
+#include "clang/AST/CXXInheritance.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
@@ -1407,7 +1408,8 @@
// ::= Z # global far
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
switch (MD->getAccess()) {
- default:
+ case AS_none:
+ llvm_unreachable("Unsupported access specifier");
case AS_private:
if (MD->isStatic())
Out << 'C';
@@ -1860,36 +1862,74 @@
return Mangler.mangle(D);
}
+static void mangleThunkThisAdjustment(const CXXMethodDecl *MD,
+ const ThisAdjustment &Adjustment,
+ MicrosoftCXXNameMangler &Mangler,
+ raw_ostream &Out) {
+ // FIXME: add support for vtordisp thunks.
+ if (Adjustment.NonVirtual != 0) {
+ switch (MD->getAccess()) {
+ case AS_none:
+ llvm_unreachable("Unsupported access specifier");
+ case AS_private:
+ Out << 'G';
+ break;
+ case AS_protected:
+ Out << 'O';
+ break;
+ case AS_public:
+ Out << 'W';
+ }
+ llvm::APSInt APSNumber(/*BitWidth=*/32, /*isUnsigned=*/true);
+ APSNumber = -Adjustment.NonVirtual;
+ Mangler.mangleNumber(APSNumber);
+ } else {
+ switch (MD->getAccess()) {
+ case AS_none:
+ llvm_unreachable("Unsupported access specifier");
+ case AS_private:
+ Out << 'A';
+ break;
+ case AS_protected:
+ Out << 'I';
+ break;
+ case AS_public:
+ Out << 'Q';
+ }
+ }
+}
+
void MicrosoftMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
const ThunkInfo &Thunk,
raw_ostream &Out) {
- // FIXME: this is not yet a complete implementation, but merely a
- // reasonably-working stub to avoid crashing when required to emit a thunk.
MicrosoftCXXNameMangler Mangler(*this, Out);
Out << "\01?";
Mangler.mangleName(MD);
- if (Thunk.This.NonVirtual != 0) {
- // FIXME: add support for protected/private or use mangleFunctionClass.
- Out << "W";
- llvm::APSInt APSNumber(/*BitWidth=*/32 /*FIXME: check on x64*/,
- /*isUnsigned=*/true);
- APSNumber = -Thunk.This.NonVirtual;
- Mangler.mangleNumber(APSNumber);
+ mangleThunkThisAdjustment(MD, Thunk.This, Mangler, Out);
+ const CXXMethodDecl *DeclForFPT;
+ if (Thunk.Return.isEmpty()) {
+ DeclForFPT = MD;
} else {
- // FIXME: add support for protected/private or use mangleFunctionClass.
- Out << "Q";
+ assert(Thunk.OpaqueData != 0 &&
+ "Thunk info should hold the overridee decl");
+ DeclForFPT = static_cast<const CXXMethodDecl*>(Thunk.OpaqueData);
}
- // FIXME: mangle return adjustment? Most likely includes using an overridee FPT?
- Mangler.mangleFunctionType(MD->getType()->castAs<FunctionProtoType>(), MD);
-}
-
-void MicrosoftMangleContextImpl::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
- CXXDtorType Type,
- const ThisAdjustment &,
- raw_ostream &) {
- unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
- "cannot mangle thunk for this destructor yet");
- getDiags().Report(DD->getLocation(), DiagID);
+ Mangler.mangleFunctionType(DeclForFPT->getType()->castAs<FunctionProtoType>(),
+ MD);
+}
+
+void MicrosoftMangleContextImpl::mangleCXXDtorThunk(
+ const CXXDestructorDecl *DD, CXXDtorType Type,
+ const ThisAdjustment &Adjustment, raw_ostream &Out) {
+ // FIXME: Actually, the dtor thunk should be emitted for vector deleting
+ // dtors rather than scalar deleting dtors. Just use the vector deleting dtor
+ // mangling manually until we support both deleting dtor types.
+ assert(Type == Dtor_Deleting);
+ MicrosoftCXXNameMangler Mangler(*this, Out, DD, Type);
+ Out << "\01??_E";
+ Mangler.mangleName(DD->getParent());
+ mangleThunkThisAdjustment(DD, Adjustment, Mangler, Out);
+ Mangler.mangleFunctionType(DD->getType()->castAs<FunctionProtoType>(), DD);
}
void MicrosoftMangleContextImpl::mangleCXXVFTable(
Index: lib/AST/VTableBuilder.cpp
===================================================================
--- lib/AST/VTableBuilder.cpp
+++ lib/AST/VTableBuilder.cpp
@@ -992,6 +992,7 @@
MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
+ assert(!Context.getTargetInfo().getCXXABI().isMicrosoft());
LayoutVTable();
@@ -2520,18 +2521,14 @@
/// AddMethod - Add a single virtual member function to the vftable
/// components vector.
- void AddMethod(const CXXMethodDecl *MD, ThisAdjustment ThisAdjustment,
- ReturnAdjustment ReturnAdjustment) {
+ void AddMethod(const CXXMethodDecl *MD, ThunkInfo TI) {
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
- assert(ReturnAdjustment.isEmpty() &&
+ assert(TI.Return.isEmpty() &&
"Destructor can't have return adjustment!");
Components.push_back(VTableComponent::MakeDeletingDtor(DD));
} else {
- // Add the return adjustment if necessary.
- if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) {
- VTableThunks[Components.size()].Return = ReturnAdjustment;
- VTableThunks[Components.size()].This = ThisAdjustment;
- }
+ if (!TI.isEmpty())
+ VTableThunks[Components.size()] = TI;
Components.push_back(VTableComponent::MakeFunction(MD));
}
}
@@ -2886,7 +2883,8 @@
}
}
- AddMethod(Overrider.Method, ThisAdjustmentOffset, ReturnAdjustment);
+ AddMethod(Overrider.Method,
+ ThunkInfo(ThisAdjustmentOffset, ReturnAdjustment, MD));
}
}
Index: lib/CodeGen/CGCXXABI.h
===================================================================
--- lib/CodeGen/CGCXXABI.h
+++ lib/CodeGen/CGCXXABI.h
@@ -339,11 +339,17 @@
SourceLocation CallLoc,
llvm::Value *This) = 0;
+ virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
+ GlobalDecl GD,
+ CallArgList &CallArgs) {}
+
/// Emit any tables needed to implement virtual inheritance. For Itanium,
/// this emits virtual table tables. For the MSVC++ ABI, this emits virtual
/// base tables.
virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
+ virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) = 0;
+
virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
RValue RV, QualType ResultType);
Index: lib/CodeGen/CGVTables.cpp
===================================================================
--- lib/CodeGen/CGVTables.cpp
+++ lib/CodeGen/CGVTables.cpp
@@ -333,6 +333,9 @@
// Add our adjusted 'this' pointer.
CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
+ if (isa<CXXDestructorDecl>(MD))
+ CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, GD, CallArgs);
+
// Add the rest of the parameters.
for (FunctionDecl::param_const_iterator I = MD->param_begin(),
E = MD->param_end(); I != E; ++I) {
@@ -390,14 +393,8 @@
setThunkVisibility(CGM, MD, Thunk, Fn);
}
-void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
- bool UseAvailableExternallyLinkage)
-{
- if (CGM.getTarget().getCXXABI().isMicrosoft()) {
- // Emission of thunks is not supported yet in Microsoft ABI.
- return;
- }
-
+void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
+ bool ForVTable) {
const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
// FIXME: re-use FnInfo in this computation.
@@ -435,9 +432,11 @@
}
llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
+ bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
+ bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
if (!ThunkFn->isDeclaration()) {
- if (UseAvailableExternallyLinkage) {
+ if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
// There is already a thunk emitted for this function, do nothing.
return;
}
@@ -466,23 +465,24 @@
CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
}
- if (UseAvailableExternallyLinkage)
- ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
+ CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable);
}
-void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
- const ThunkInfo &Thunk) {
- // We only want to do this when building with optimizations.
- if (!CGM.getCodeGenOpts().OptimizationLevel)
+void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
+ const ThunkInfo &Thunk) {
+ if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
+ !CGM.getCodeGenOpts().OptimizationLevel) {
+ // If the ABI has key functions, we only want to do this when building with optimizations.
return;
+ }
// We can't emit thunks for member functions with incomplete types.
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
if (!CGM.getTypes().isFuncTypeConvertible(
cast<FunctionType>(MD->getType().getTypePtr())))
return;
- EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
+ emitThunk(GD, Thunk, /*ForVTable=*/true);
}
void CodeGenVTables::EmitThunks(GlobalDecl GD)
@@ -494,21 +494,22 @@
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
return;
+ const VTableContext::ThunkInfoVectorTy *ThunkInfoVector;
if (VFTContext.isValid()) {
- // FIXME: This is a temporary solution to force generation of vftables in
- // Microsoft ABI. Remove when we thread VFTableContext through CodeGen.
- VFTContext->getVFPtrOffsets(MD->getParent());
- return;
+ // Don't need to generate thunks for complete destructor in this ABI.
+ if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Complete)
+ return;
+ ThunkInfoVector = VFTContext->getThunkInfo(MD);
+ } else {
+ ThunkInfoVector = VTContext.getThunkInfo(MD);
}
- const VTableContext::ThunkInfoVectorTy *ThunkInfoVector =
- VTContext.getThunkInfo(MD);
if (!ThunkInfoVector)
return;
- for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
- EmitThunk(GD, (*ThunkInfoVector)[I],
- /*UseAvailableExternallyLinkage=*/false);
+ for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I) {
+ emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false);
+ }
}
llvm::Constant *
@@ -603,7 +604,7 @@
VTableThunks[NextVTableThunkIndex].first == I) {
const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
- MaybeEmitThunkAvailableExternally(GD, Thunk);
+ maybeEmitThunkForVTable(GD, Thunk);
Init = CGM.GetAddrOfThunk(GD, Thunk);
NextVTableThunkIndex++;
Index: lib/CodeGen/CGVTables.h
===================================================================
--- lib/CodeGen/CGVTables.h
+++ lib/CodeGen/CGVTables.h
@@ -52,15 +52,12 @@
/// indices.
SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
- /// EmitThunk - Emit a single thunk.
- void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
- bool UseAvailableExternallyLinkage);
-
- /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with
- /// available_externally linkage to allow for inlining of thunks.
- /// This will be done iff optimizations are enabled and the member function
- /// doesn't contain any incomplete types.
- void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
+ /// emitThunk - Emit a single thunk.
+ void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable);
+
+ /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by
+ /// the ABI.
+ void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk);
public:
/// CreateVTableInitializer - Create a vtable initializer for the given record
Index: lib/CodeGen/ItaniumCXXABI.cpp
===================================================================
--- lib/CodeGen/ItaniumCXXABI.cpp
+++ lib/CodeGen/ItaniumCXXABI.cpp
@@ -173,6 +173,13 @@
void emitVirtualInheritanceTables(const CXXRecordDecl *RD);
+ bool shouldTryToEmitThunksForExternalVirtualFunctions() { return false; }
+
+ void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) {
+ if (ForVTable)
+ Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
+ }
+
StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; }
Index: lib/CodeGen/MicrosoftCXXABI.cpp
===================================================================
--- lib/CodeGen/MicrosoftCXXABI.cpp
+++ lib/CodeGen/MicrosoftCXXABI.cpp
@@ -173,8 +173,22 @@
CXXDtorType DtorType, SourceLocation CallLoc,
llvm::Value *This);
+ void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD,
+ CallArgList &CallArgs) {
+ assert(GD.getDtorType() == Dtor_Deleting &&
+ "Only deleting destructor thunks are available in this ABI");
+ CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)),
+ CGM.getContext().IntTy);
+ }
+
void emitVirtualInheritanceTables(const CXXRecordDecl *RD);
+ bool shouldTryToEmitThunksForExternalVirtualFunctions() { return true; }
+
+ void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) {
+ Thunk->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
+ }
+
void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
llvm::GlobalVariable *DeclPtr,
bool PerformInit);
Index: test/CodeGenCXX/microsoft-abi-thunks.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/microsoft-abi-thunks.cpp
@@ -0,0 +1,100 @@
+// RUN: %clang_cc1 -fno-rtti -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 >%t 2>&1
+// RUN: FileCheck --check-prefix=MANGLING %s < %t
+// RUN: FileCheck --check-prefix=CODEGEN %s < %t
+// RUN: %clang_cc1 -fno-rtti -emit-llvm %s -o - -cxx-abi microsoft -triple=x86_64-pc-win32 2>&1 | FileCheck --check-prefix=MANGLING-X64 %s
+
+void foo(void *);
+
+struct A {
+ virtual ~A();
+ virtual void public_f();
+ protected:
+ virtual void protected_f();
+ private:
+ virtual void private_f();
+};
+
+struct B {
+ virtual ~B();
+ virtual void public_f();
+ protected:
+ virtual void protected_f();
+ private:
+ virtual void private_f();
+};
+
+
+struct C : A, B {
+ C();
+
+ virtual ~C();
+ // MANGLING-DAG: @"\01??1C@@UAE@XZ"
+ // MANGLING-DAG: @"\01??_GC@@UAEPAXI@Z"
+ // MANGLING-DAG: @"\01??_EC@@W3AEPAXI@Z"
+ // MANGLING-X64-DAG: @"\01??1C@@UEAA@XZ"
+ // MANGLING-X64-DAG: @"\01??_EC@@W7EAAPEAXI@Z"
+
+ // Overrides public_f() of two subobjects with distinct vfptrs, thus needs a thunk.
+ virtual void public_f();
+ // MANGLING-DAG: @"\01?public_f@C@@UAEXXZ"
+ // MANGLING-DAG: @"\01?public_f@C@@W3AEXXZ"
+ // MANGLING-X64-DAG: @"\01?public_f@C@@UEAAXXZ"
+ // MANGLING-X64-DAG: @"\01?public_f@C@@W7EAAXXZ"
+ protected:
+ virtual void protected_f();
+ // MANGLING-DAG: @"\01?protected_f@C@@MAEXXZ"
+ // MANGLING-DAG: @"\01?protected_f@C@@O3AEXXZ"
+ // MANGLING-X64-DAG: @"\01?protected_f@C@@MEAAXXZ"
+ // MANGLING-X64-DAG: @"\01?protected_f@C@@O7EAAXXZ"
+
+ private:
+ virtual void private_f();
+ // MANGLING-DAG: @"\01?private_f@C@@EAEXXZ"
+ // MANGLING-DAG: @"\01?private_f@C@@G3AEXXZ"
+ // MANGLING-X64-DAG: @"\01?private_f@C@@EEAAXXZ"
+ // MANGLING-X64-DAG: @"\01?private_f@C@@G7EAAXXZ"
+};
+
+C::C() {} // Emits vftable and forces thunk generation.
+
+// CODEGEN: define weak x86_thiscallcc void @"\01??_EC@@W3AEPAXI@Z"(%struct.C* %this, i32 %should_call_delete)
+// CODEGEN: getelementptr inbounds i8* {{.*}}, i64 -4
+// FIXME: should actually call _EC, not _GC.
+// CODEGEN: call x86_thiscallcc void @"\01??_GC@@UAEPAXI@Z"
+// CODEGEN: ret
+
+// CODEGEN: define weak x86_thiscallcc void @"\01?public_f@C@@W3AEXXZ"(%struct.C*
+// CODEGEN: getelementptr inbounds i8* {{.*}}, i64 -4
+// CODEGEN: call x86_thiscallcc void @"\01?public_f@C@@UAEXXZ"(%struct.C*
+// CODEGEN: ret
+
+void zoo(C* obj) {
+ delete obj;
+}
+
+struct D {
+ virtual B* goo();
+};
+
+struct E : D {
+ virtual C* goo();
+ // MANGLING-DAG: @"\01?goo@E@@UAEPAUC@@XZ"
+ // MANGLING-DAG: @"\01?goo@E@@QAEPAUB@@XZ"
+ // MANGLING-X64-DAG: @"\01?goo@E@@UEAAPEAUC@@XZ"
+ // MANGLING-X64-DAG: @"\01?goo@E@@QEAAPEAUB@@XZ"
+};
+
+E e; // Emits vftable and forces thunk generation.
+
+// CODEGEN: define weak x86_thiscallcc %struct.C* @"\01?goo@E@@QAEPAUB@@XZ"
+// CODEGEN: call x86_thiscallcc %struct.C* @"\01?goo@E@@UAEPAUC@@XZ"
+// CODEGEN: getelementptr inbounds i8* {{.*}}, i64 4
+// CODEGEN: ret
+
+struct F : virtual A, virtual B {
+ virtual ~F();
+};
+
+F f; // Just make sure we don't crash, e.g. mangling the complete dtor.
+
+// FIXME: Write vtordisp adjusting thunk tests
Index: test/CodeGenCXX/microsoft-abi-virtual-inheritance.cpp
===================================================================
--- test/CodeGenCXX/microsoft-abi-virtual-inheritance.cpp
+++ test/CodeGenCXX/microsoft-abi-virtual-inheritance.cpp
@@ -93,3 +93,11 @@
//
// CHECK: ret void
}
+
+struct C : B {
+ C();
+ // has an implicit vdtor.
+};
+
+// Used to crash on an assertion.
+C::C() {}
Index: test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance.cpp
===================================================================
--- test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance.cpp
+++ test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance.cpp
@@ -154,6 +154,9 @@
// TEST4-NOT: VFTable indices for 'Test4::X'
// MANGLING-DAG: @"\01??_7X@Test4@@6B@"
+
+ // Also check the mangling of the thunk.
+ // MANGLING-DAG: define weak x86_thiscallcc void @"\01?f@C@@WPPPPPPPE@AEXXZ"
};
X x;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits