-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathCallEmission.h
204 lines (154 loc) · 6.34 KB
/
CallEmission.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//===--- CallEmission.h - Utility for emitting calls ------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the CallEmitter class.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_CALLEMISSION_H
#define SWIFT_IRGEN_CALLEMISSION_H
#include "Address.h"
#include "Callee.h"
#include "Explosion.h"
#include "Temporary.h"
namespace llvm {
class CallSite;
}
namespace swift {
namespace irgen {
class Explosion;
class LoadableTypeInfo;
struct WitnessMetadata;
class FunctionPointer;
/// A plan for emitting a series of calls.
class CallEmission {
enum class State { Emitting, Finished };
State state = State::Emitting;
public:
IRGenFunction &IGF;
protected:
llvm::Value *selfValue;
/// The builtin/special arguments to pass to the call.
SmallVector<llvm::Value*, 8> Args;
/// Temporaries required by the call.
TemporarySet Temporaries;
/// Temporaries required by the call that are not mapped to any
/// SIL value.
SmallVector<StackAddress, 8> RawTempraries;
/// The function we're going to call.
Callee CurCallee;
/// Only valid if the SIL function has indirect return values.
/// If the function has multiple indirect return values, this is the address
/// of the first indirect return value.
Address indirectReturnAddress;
/// For C-functions: true if the return is indirect in SIL, but direct for a C-function.
/// That can happen for "sensitive" structs.
bool convertDirectToIndirectReturn = false;
unsigned LastArgWritten;
/// Whether this is a coroutine invocation.
bool IsCoroutine;
/// Whether this is a invocation for a coroutine that dynamically allocates
/// in the callee.
bool IsCalleeAllocatedCoroutine;
/// Whether we've emitted the call for the current callee yet. This
/// is just for debugging purposes --- e.g. the destructor asserts
/// that it's true --- but is otherwise derivable from
/// RemainingArgsForCallee, at least between calls.
bool EmittedCall;
bool UseProfilingThunk = false;
/// The basic block to which the call to a potentially throwing foreign
/// function should jump to continue normal execution of the program.
llvm::BasicBlock *invokeNormalDest = nullptr;
/// The basic block to which the call to a potentially throwing foreign
/// function should jump to in case an exception has been thrown during the
/// invocation of the call.
llvm::BasicBlock *invokeUnwindDest = nullptr;
unsigned IndirectTypedErrorArgIdx = 0;
std::optional<Explosion> typedErrorExplosion;
virtual void setFromCallee();
void emitToUnmappedMemory(Address addr);
void emitToUnmappedExplosion(Explosion &out);
virtual void emitCallToUnmappedExplosion(llvm::CallBase *call,
Explosion &out) = 0;
void emitYieldsToExplosion(Explosion &out);
void setKeyPathAccessorArguments(Explosion &in, bool isOutlined,
Explosion &out);
virtual FunctionPointer getCalleeFunctionPointer() = 0;
llvm::CallBase *emitCallSite();
virtual llvm::CallBase *createCall(const FunctionPointer &fn,
ArrayRef<llvm::Value *> args) = 0;
void externalizeArguments(IRGenFunction &IGF, const Callee &callee,
Explosion &in, Explosion &out,
TemporarySet &temporaries,
bool isOutlined);
bool mayReturnTypedErrorDirectly() const;
void emitToUnmappedExplosionWithDirectTypedError(SILType resultType,
llvm::Value *result,
Explosion &out);
CallEmission(IRGenFunction &IGF, llvm::Value *selfValue, Callee &&callee)
: IGF(IGF), selfValue(selfValue), CurCallee(std::move(callee)) {}
public:
CallEmission(const CallEmission &other) = delete;
CallEmission(CallEmission &&other);
CallEmission &operator=(const CallEmission &other) = delete;
virtual ~CallEmission();
const Callee &getCallee() const { return CurCallee; }
SubstitutionMap getSubstitutions() const {
return CurCallee.getSubstitutions();
}
void useProfilingThunk() {
UseProfilingThunk = true;
}
std::optional<Explosion> &getTypedErrorExplosion() {
return typedErrorExplosion;
}
virtual void begin();
virtual void end();
virtual SILType getParameterType(unsigned index) = 0;
/// Set the arguments to the function from an explosion.
virtual void setArgs(Explosion &arg, bool isOutlined,
WitnessMetadata *witnessMetadata);
virtual Address getCalleeErrorSlot(SILType errorType, bool isCalleeAsync) = 0;
void addFnAttribute(llvm::Attribute::AttrKind Attr);
void setIndirectReturnAddress(Address addr) { indirectReturnAddress = addr; }
void addParamAttribute(unsigned ParamIndex, llvm::Attribute::AttrKind Attr);
void emitToMemory(Address addr, const LoadableTypeInfo &substResultTI,
bool isOutlined);
void emitToExplosion(Explosion &out, bool isOutlined);
llvm::CallBase *emitCoroutineAsOrdinaryFunction() {
assert(IsCoroutine);
IsCoroutine = false;
return emitCallSite();
}
TemporarySet claimTemporaries() {
// Move the actual temporary set out.
auto result = std::move(Temporaries);
// Flag that we've cleared the set.
Temporaries.clear();
return result;
}
virtual llvm::Value *getResumeFunctionPointer() = 0;
virtual llvm::Value *getAsyncContext() = 0;
virtual StackAddress getCoroStaticFrame() = 0;
virtual llvm::Value *getCoroAllocator() = 0;
void setIndirectTypedErrorResultSlot(llvm::Value *addr) {
Args[IndirectTypedErrorArgIdx] = addr;
}
void setIndirectTypedErrorResultSlotArgsIndex(unsigned idx) {
assert(idx != 0);
IndirectTypedErrorArgIdx = idx;
}
};
std::unique_ptr<CallEmission>
getCallEmission(IRGenFunction &IGF, llvm::Value *selfValue, Callee &&callee);
} // end namespace irgen
} // end namespace swift
#endif