From a94822e8937eeb340a653b88024f805a07a2d2ee Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Thu, 26 Nov 2009 02:32:05 +0000 Subject: [PATCH] Add a ThunkAdjustment struct which holds a non-virtual and a virtual adjustment offset. Start using it. General cleanup in Mangle.cpp. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89925 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCXX.cpp | 24 ++++++++------ lib/CodeGen/CGVtable.cpp | 3 +- lib/CodeGen/CGVtable.h | 21 ++++++++++++ lib/CodeGen/CodeGenFunction.h | 3 +- lib/CodeGen/CodeGenModule.h | 7 ++-- lib/CodeGen/Mangle.cpp | 61 +++++++++++++++++++---------------- lib/CodeGen/Mangle.h | 3 +- 7 files changed, 79 insertions(+), 43 deletions(-) diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index 71cdf95f48..9674146825 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -819,11 +819,13 @@ const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, return UniqueMangledName(Name.begin(), Name.end()); } -llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn, - const CXXMethodDecl *MD, - bool Extern, int64_t nv, - int64_t v) { - return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0); +llvm::Constant * +CodeGenFunction::GenerateThunk(llvm::Function *Fn, const CXXMethodDecl *MD, + bool Extern, + const ThunkAdjustment &ThisAdjustment) { + return GenerateCovariantThunk(Fn, MD, Extern, + ThisAdjustment.NonVirtual, + ThisAdjustment.Virtual, 0, 0); } llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv, @@ -961,10 +963,14 @@ llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, return Fn; } -llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, - int64_t nv, int64_t v) { +llvm::Constant * +CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, + const ThunkAdjustment &ThisAdjustment) { + llvm::SmallString<256> OutName; - getMangleContext().mangleThunk(MD, nv, v, OutName); + getMangleContext().mangleThunk(MD, ThisAdjustment.NonVirtual, + ThisAdjustment.Virtual, OutName); + llvm::GlobalVariable::LinkageTypes linktype; linktype = llvm::GlobalValue::WeakAnyLinkage; if (!Extern) @@ -977,7 +983,7 @@ llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), &getModule()); - CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v); + CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, ThisAdjustment); llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); return m; } diff --git a/lib/CodeGen/CGVtable.cpp b/lib/CodeGen/CGVtable.cpp index 0c36ef492b..08690f23a8 100644 --- a/lib/CodeGen/CGVtable.cpp +++ b/lib/CodeGen/CGVtable.cpp @@ -333,7 +333,8 @@ public: Index_t idx = Index[GD]; Index_t nv_O = i->second.first; Index_t v_O = i->second.second; - submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O); + submethods[idx] = CGM.BuildThunk(MD, Extern, + ThunkAdjustment(nv_O, v_O)); } Thunks.clear(); for (CovariantThunks_t::iterator i = CovariantThunks.begin(), diff --git a/lib/CodeGen/CGVtable.h b/lib/CodeGen/CGVtable.h index 78ae670cf3..af718b3632 100644 --- a/lib/CodeGen/CGVtable.h +++ b/lib/CodeGen/CGVtable.h @@ -23,7 +23,28 @@ namespace clang { namespace CodeGen { class CodeGenModule; + +/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks. +struct ThunkAdjustment { + ThunkAdjustment(int64_t NonVirtual, int64_t Virtual) + : NonVirtual(NonVirtual), + Virtual(Virtual) { } + + ThunkAdjustment() + : NonVirtual(0), Virtual(0) { } + + // isEmpty - Return whether this thunk adjustment is empty. + bool isEmpty() const { + return NonVirtual == 0 && Virtual == 0; + } + /// NonVirtual - The non-virtual adjustment. + int64_t NonVirtual; + + /// Virtual - The virtual adjustment. + int64_t Virtual; +}; + class CGVtableInfo { CodeGenModule &CGM; diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 28e000a492..b811c25b8a 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -441,7 +441,8 @@ public: /// GenerateThunk - Generate a thunk for the given method llvm::Constant *GenerateThunk(llvm::Function *Fn, const CXXMethodDecl *MD, - bool Extern, int64_t nv, int64_t v); + bool Extern, + const ThunkAdjustment &ThisAdjustment); llvm::Constant *GenerateCovariantThunk(llvm::Function *Fn, const CXXMethodDecl *MD, bool Extern, int64_t nv_t, int64_t v_t, diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index 64314d3fbe..bc2d73edc1 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -233,9 +233,10 @@ public: /// non-class type. llvm::Constant *GenerateRtti(QualType Ty); - /// BuildThunk - Build a thunk for the given method - llvm::Constant *BuildThunk(const CXXMethodDecl *MD, bool Extern, int64_t nv, - int64_t v); + /// BuildThunk - Build a thunk for the given method. + llvm::Constant *BuildThunk(const CXXMethodDecl *MD, bool Extern, + const ThunkAdjustment &ThisAdjustment); + /// BuildCoVariantThunk - Build a thunk for the given method llvm::Constant *BuildCovariantThunk(const CXXMethodDecl *MD, bool Extern, int64_t nv_t, int64_t v_t, diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp index b09b27f489..e2ae86bd45 100644 --- a/lib/CodeGen/Mangle.cpp +++ b/lib/CodeGen/Mangle.cpp @@ -67,7 +67,9 @@ public: llvm::raw_svector_ostream &getStream() { return Out; } void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z"); - void mangleCalloffset(int64_t nv, int64_t v); + void mangleCallOffset(int64_t NonVirtualOffset, + int64_t VirtualOffset); + void mangleNumber(int64_t Number); void mangleFunctionEncoding(const FunctionDecl *FD); void mangleName(const NamedDecl *ND); void mangleType(QualType T); @@ -336,34 +338,35 @@ void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) { addSubstitution(ND); } -void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) { +void CXXNameMangler::mangleNumber(int64_t Number) { + // ::= [n] + if (Number < 0) { + Out << 'n'; + Number = -Number; + } + + Out << Number; +} + +void CXXNameMangler::mangleCallOffset(int64_t NonVirtualOffset, + int64_t VirtualOffset) { // ::= h _ // ::= v _ // ::= # non-virtual base override - // ::= _ + // ::= _ // # virtual base override, with vcall offset - if (v == 0) { - Out << "h"; - if (nv < 0) { - Out << "n"; - nv = -nv; - } - Out << nv; - } else { - Out << "v"; - if (nv < 0) { - Out << "n"; - nv = -nv; - } - Out << nv; - Out << "_"; - if (v < 0) { - Out << "n"; - v = -v; - } - Out << v; + if (!VirtualOffset) { + Out << 'h'; + mangleNumber(NonVirtualOffset); + Out << '_'; + return; } - Out << "_"; + + Out << 'v'; + mangleNumber(NonVirtualOffset); + Out << '_'; + mangleNumber(VirtualOffset); + Out << '_'; } void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) { @@ -1351,7 +1354,9 @@ void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, /// \brief Mangles the a thunk with the offset n for the declaration D and /// emits that name to the given output stream. -void MangleContext::mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v, +void MangleContext::mangleThunk(const FunctionDecl *FD, + int64_t NonVirtualOffset, + int64_t VirtualOffset, llvm::SmallVectorImpl &Res) { // FIXME: Hum, we might have to thunk these, fix. assert(!isa(FD) && @@ -1361,7 +1366,7 @@ void MangleContext::mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v, // # base is the nominal target function of thunk CXXNameMangler Mangler(*this, Res); Mangler.getStream() << "_ZT"; - Mangler.mangleCalloffset(nv, v); + Mangler.mangleCallOffset(NonVirtualOffset, VirtualOffset); Mangler.mangleFunctionEncoding(FD); } @@ -1380,8 +1385,8 @@ void MangleContext::mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, // # second call-offset is result adjustment CXXNameMangler Mangler(*this, Res); Mangler.getStream() << "_ZTc"; - Mangler.mangleCalloffset(nv_t, v_t); - Mangler.mangleCalloffset(nv_r, v_r); + Mangler.mangleCallOffset(nv_t, v_t); + Mangler.mangleCallOffset(nv_r, v_r); Mangler.mangleFunctionEncoding(FD); } diff --git a/lib/CodeGen/Mangle.h b/lib/CodeGen/Mangle.h index edbbff5161..15951fe876 100644 --- a/lib/CodeGen/Mangle.h +++ b/lib/CodeGen/Mangle.h @@ -61,7 +61,8 @@ public: bool shouldMangleDeclName(const NamedDecl *D); void mangleName(const NamedDecl *D, llvm::SmallVectorImpl &); - void mangleThunk(const FunctionDecl *FD, int64_t n, int64_t vn, + void mangleThunk(const FunctionDecl *FD, + int64_t NonVirtualOffset, int64_t VirtualOffset, llvm::SmallVectorImpl &); void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t, int64_t nv_r, int64_t v_r, -- 2.40.0