-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathIRABIDetailsProvider.cpp
164 lines (144 loc) · 5.5 KB
/
IRABIDetailsProvider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//===--- IRABIDetailsProvider.cpp - Get ABI details for decls ---*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/IRGen/IRABIDetailsProvider.h"
#include "FixedTypeInfo.h"
#include "GenType.h"
#include "IRGen.h"
#include "IRGenModule.h"
#include "NativeConventionSchema.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/Types.h"
#include "swift/SIL/SILModule.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/CodeGen/SwiftCallingConv.h"
#include "llvm/IR/DerivedTypes.h"
using namespace swift;
using namespace irgen;
static Optional<Type> getPrimitiveTypeFromLLVMType(ASTContext &ctx,
const llvm::Type *type) {
if (const auto *intType = dyn_cast<llvm::IntegerType>(type)) {
switch (intType->getBitWidth()) {
case 8:
return ctx.getUInt8Type();
case 16:
return ctx.getUInt16Type();
case 32:
return ctx.getUInt32Type();
case 64:
return ctx.getUInt64Type();
default:
return None;
}
} else if (type->isFloatTy()) {
return ctx.getFloatType();
} else if (type->isDoubleTy()) {
return ctx.getDoubleType();
} else if (type->isPointerTy()) {
return ctx.getOpaquePointerType();
}
// FIXME: Handle vector type.
return None;
}
namespace swift {
class IRABIDetailsProviderImpl {
public:
IRABIDetailsProviderImpl(ModuleDecl &mod, const IRGenOptions &opts)
: typeConverter(mod),
silMod(SILModule::createEmptyModule(&mod, typeConverter, silOpts)),
IRGen(opts, *silMod), IGM(IRGen, IRGen.createTargetMachine()) {}
llvm::Optional<IRABIDetailsProvider::SizeAndAlignment>
getTypeSizeAlignment(const NominalTypeDecl *TD) {
auto *TI = &IGM.getTypeInfoForUnlowered(TD->getDeclaredTypeInContext());
auto *fixedTI = dyn_cast<FixedTypeInfo>(TI);
if (!fixedTI)
return None;
return IRABIDetailsProvider::SizeAndAlignment{
fixedTI->getFixedSize().getValue(),
fixedTI->getFixedAlignment().getValue()};
}
bool shouldPassIndirectly(Type type) {
auto *TI = &IGM.getTypeInfoForUnlowered(type);
NativeConventionSchema schema(IGM, TI, /*isResult=*/false);
return schema.requiresIndirect();
}
bool shouldReturnIndirectly(Type type) {
if (type->isVoid())
return false;
auto *TI = &IGM.getTypeInfoForUnlowered(type);
NativeConventionSchema schema(IGM, TI, /*isResult=*/true);
return schema.requiresIndirect();
}
bool enumerateDirectPassingRecordMembers(
Type t, llvm::function_ref<void(clang::CharUnits, clang::CharUnits, Type)>
callback) {
auto *TI = &IGM.getTypeInfoForUnlowered(t);
NativeConventionSchema schema(IGM, TI, /*isResult=*/false);
bool hasError = false;
schema.enumerateComponents(
[&](clang::CharUnits offset, clang::CharUnits end, llvm::Type *type) {
auto primitiveType = getPrimitiveTypeFromLLVMType(
IGM.getSwiftModule()->getASTContext(), type);
if (!primitiveType) {
hasError = true;
return;
}
callback(offset, end, *primitiveType);
});
return hasError;
}
IRABIDetailsProvider::FunctionABISignature
getTypeMetadataAccessFunctionSignature() {
auto &ctx = IGM.getSwiftModule()->getASTContext();
llvm::StructType *responseTy = IGM.getTypeMetadataResponseTy();
IRABIDetailsProvider::TypeRecordABIRepresentation::MemberVectorTy members;
for (auto *elementTy : responseTy->elements())
members.push_back(*getPrimitiveTypeFromLLVMType(ctx, elementTy));
auto returnTy =
IRABIDetailsProvider::TypeRecordABIRepresentation(std::move(members));
auto paramTy = IRABIDetailsProvider::TypeRecordABIRepresentation(
{*getPrimitiveTypeFromLLVMType(ctx,
IGM.getTypeMetadataRequestParamTy())});
return {returnTy, {paramTy}};
}
private:
Lowering::TypeConverter typeConverter;
// Default silOptions are sufficient, as we don't need to generated SIL.
SILOptions silOpts;
std::unique_ptr<SILModule> silMod;
IRGenerator IRGen;
IRGenModule IGM;
};
} // namespace swift
IRABIDetailsProvider::IRABIDetailsProvider(ModuleDecl &mod,
const IRGenOptions &opts)
: impl(std::make_unique<IRABIDetailsProviderImpl>(mod, opts)) {}
IRABIDetailsProvider::~IRABIDetailsProvider() {}
llvm::Optional<IRABIDetailsProvider::SizeAndAlignment>
IRABIDetailsProvider::getTypeSizeAlignment(const NominalTypeDecl *TD) {
return impl->getTypeSizeAlignment(TD);
}
bool IRABIDetailsProvider::shouldPassIndirectly(Type t) {
return impl->shouldPassIndirectly(t);
}
bool IRABIDetailsProvider::shouldReturnIndirectly(Type t) {
return impl->shouldReturnIndirectly(t);
}
bool IRABIDetailsProvider::enumerateDirectPassingRecordMembers(
Type t, llvm::function_ref<void(clang::CharUnits, clang::CharUnits, Type)>
callback) {
return impl->enumerateDirectPassingRecordMembers(t, callback);
}
IRABIDetailsProvider::FunctionABISignature
IRABIDetailsProvider::getTypeMetadataAccessFunctionSignature() {
return impl->getTypeMetadataAccessFunctionSignature();
}