Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Expose virtual method lookup and this ptr adjustment. #349

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/clang/CodeGen/CodeGenABITypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const CGFunctionInfo &arrangeCXXMethodType(CodeGenModule &CGM,
const FunctionProtoType *FTP,
const CXXMethodDecl *MD);

const CGFunctionInfo &arrangeCXXMethodDeclaration(CodeGenModule &CGM,
const CXXMethodDecl *MD);

const CGFunctionInfo &arrangeFreeFunctionCall(CodeGenModule &CGM,
CanQualType returnType,
ArrayRef<CanQualType> argTypes,
Expand All @@ -76,6 +79,9 @@ const CGFunctionInfo &arrangeFreeFunctionCall(CodeGenModule &CGM,
llvm::FunctionType *convertFreeFunctionType(CodeGenModule &CGM,
const FunctionDecl *FD);

llvm::FunctionType *getFunctionType(CodeGenModule &CGM,
const CGFunctionInfo &FI);

llvm::Type *convertTypeForMemory(CodeGenModule &CGM, QualType T);

/// Given a non-bitfield struct field, return its index within the elements of
Expand Down
12 changes: 12 additions & 0 deletions include/clang/CodeGen/SwiftCallingConv.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
namespace llvm {
class IntegerType;
class Type;
class Value;
class StructType;
class VectorType;
class FunctionType;
class IRBuilderBase;
}

namespace clang {
class Decl;
class FieldDecl;
class ASTRecordLayout;
class CXXMethodDecl;

namespace CodeGen {
class ABIArgInfo;
Expand Down Expand Up @@ -178,6 +182,14 @@ void computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI);
/// Is swifterror lowered to a register by the target ABI?
bool isSwiftErrorLoweredInRegister(CodeGenModule &CGM);

/// Lookup a virtual method `MD` from the vtable and adjusts the this ptr.
llvm::Value *lowerCXXVirtualMethodDeclReference(CodeGenModule &CGM,
const CXXMethodDecl *MD,
llvm::Value *&thisPtr,
CharUnits alignment,
llvm::FunctionType *type,
llvm::IRBuilderBase *builder);

} // end namespace swiftcall
} // end namespace CodeGen
} // end namespace clang
Expand Down
11 changes: 11 additions & 0 deletions lib/CodeGen/CodeGenABITypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ CodeGen::arrangeCXXMethodType(CodeGenModule &CGM,
return CGM.getTypes().arrangeCXXMethodType(RD, FTP, MD);
}

const CGFunctionInfo &
CodeGen::arrangeCXXMethodDeclaration(CodeGenModule &CGM,
const CXXMethodDecl *MD) {
return CGM.getTypes().arrangeCXXMethodDeclaration(MD);
}

llvm::FunctionType *CodeGen::getFunctionType(CodeGenModule &CGM,
const CGFunctionInfo &FI) {
return CGM.getTypes().GetFunctionType(FI);
}

const CGFunctionInfo &
CodeGen::arrangeFreeFunctionCall(CodeGenModule &CGM,
CanQualType returnType,
Expand Down
26 changes: 25 additions & 1 deletion lib/CodeGen/SwiftCallingConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
//===----------------------------------------------------------------------===//

#include "clang/CodeGen/SwiftCallingConv.h"
#include "clang/Basic/TargetInfo.h"
#include "CGCXXABI.h"
#include "CodeGenFunction.h"
#include "CodeGenModule.h"
#include "TargetInfo.h"
#include "clang/Basic/TargetInfo.h"

using namespace clang;
using namespace CodeGen;
Expand Down Expand Up @@ -863,3 +865,25 @@ void swiftcall::computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
bool swiftcall::isSwiftErrorLoweredInRegister(CodeGenModule &CGM) {
return getSwiftABIInfo(CGM).isSwiftErrorInRegister();
}

llvm::Value *swiftcall::lowerCXXVirtualMethodDeclReference(
CodeGenModule &CGM, const CXXMethodDecl *MD, llvm::Value *&thisPtr,
CharUnits alignment, llvm::FunctionType *type,
llvm::IRBuilderBase *builder) {
assert(MD->isVirtual());

CodeGenFunction CGF(CGM, true);
CGF.Builder.SetInsertPoint(builder->GetInsertBlock(),
builder->GetInsertPoint());
Address thisAddr(thisPtr, alignment);
auto callee = CGCallee::forVirtual(nullptr, MD, thisAddr, type);
const CGCallee &concreteCallee = callee.prepareConcreteCallee(CGF);
Address newThisAddr =
CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall(CGF, MD,
thisAddr, true);
thisPtr = newThisAddr.getPointer();
auto *result = concreteCallee.getFunctionPointer();
builder->SetInsertPoint(CGF.Builder.GetInsertBlock(),
CGF.Builder.GetInsertPoint());
return result;
}