12
12
13
13
#include " Cleanup.h"
14
14
#include " SILGenFunction.h"
15
+
15
16
using namespace swift ;
16
17
using namespace Lowering ;
17
18
@@ -28,34 +29,34 @@ namespace {
28
29
// / A CleanupBuffer is a location to which to temporarily copy a
29
30
// / cleanup.
30
31
class CleanupBuffer {
31
- SmallVector<char , sizeof (Cleanup) + 10 * sizeof (void *)> Data ;
32
-
32
+ SmallVector<char , sizeof (Cleanup) + 10 * sizeof (void *)> data ;
33
+
33
34
public:
34
35
CleanupBuffer (const Cleanup &cleanup) {
35
36
size_t size = cleanup.allocated_size ();
36
- Data .set_size (size);
37
- memcpy (Data .data (), reinterpret_cast <const void *>(&cleanup), size);
37
+ data .set_size (size);
38
+ memcpy (data .data (), reinterpret_cast <const void *>(&cleanup), size);
38
39
}
39
-
40
- Cleanup &getCopy () { return *reinterpret_cast <Cleanup*>(Data .data ()); }
40
+
41
+ Cleanup &getCopy () { return *reinterpret_cast <Cleanup *>(data .data ()); }
41
42
};
42
43
} // end anonymous namespace
43
44
44
45
void CleanupManager::popTopDeadCleanups (CleanupsDepth end) {
45
- Stack .checkIterator (end);
46
+ stack .checkIterator (end);
46
47
47
- while (Stack .stable_begin () != end && Stack .begin ()->isDead ()) {
48
- assert (!Stack .empty ());
49
- Stack .pop ();
50
- Stack .checkIterator (end);
48
+ while (stack .stable_begin () != end && stack .begin ()->isDead ()) {
49
+ assert (!stack .empty ());
50
+ stack .pop ();
51
+ stack .checkIterator (end);
51
52
}
52
53
}
53
54
54
- void CleanupManager::emitCleanups (CleanupsDepth depth, CleanupLocation l ,
55
+ void CleanupManager::emitCleanups (CleanupsDepth depth, CleanupLocation loc ,
55
56
bool popCleanups) {
56
- auto begin = Stack .stable_begin ();
57
+ auto begin = stack .stable_begin ();
57
58
while (begin != depth) {
58
- auto iter = Stack .find (begin);
59
+ auto iter = stack .find (begin);
59
60
60
61
Cleanup &stackCleanup = *iter;
61
62
@@ -65,78 +66,78 @@ void CleanupManager::emitCleanups(CleanupsDepth depth, CleanupLocation l,
65
66
Cleanup &cleanup = buffer.getCopy ();
66
67
67
68
// Advance stable iterator.
68
- begin = Stack .stabilize (++iter);
69
+ begin = stack .stabilize (++iter);
69
70
70
71
// Pop now.
71
72
if (popCleanups)
72
- Stack .pop ();
73
+ stack .pop ();
73
74
74
- if (cleanup.isActive () && Gen .B .hasValidInsertionPoint ())
75
- cleanup.emit (Gen, l );
75
+ if (cleanup.isActive () && SGF .B .hasValidInsertionPoint ())
76
+ cleanup.emit (SGF, loc );
76
77
77
- Stack .checkIterator (begin);
78
+ stack .checkIterator (begin);
78
79
}
79
80
}
80
81
81
82
// / Leave a scope, with all its cleanups.
82
- void CleanupManager::endScope (CleanupsDepth depth, CleanupLocation l ) {
83
- Stack .checkIterator (depth);
84
-
83
+ void CleanupManager::endScope (CleanupsDepth depth, CleanupLocation loc ) {
84
+ stack .checkIterator (depth);
85
+
85
86
// FIXME: Thread a branch through the cleanups if there are any active
86
87
// cleanups and we have a valid insertion point.
87
-
88
- if (!::hasAnyActiveCleanups (Stack .begin (), Stack .find (depth))) {
88
+
89
+ if (!::hasAnyActiveCleanups (stack .begin (), stack .find (depth))) {
89
90
return ;
90
91
}
91
92
92
93
// Iteratively mark cleanups dead and pop them.
93
94
// Maybe we'd get better results if we marked them all dead in one shot?
94
- emitCleanups (depth, l );
95
+ emitCleanups (depth, loc );
95
96
}
96
97
97
98
bool CleanupManager::hasAnyActiveCleanups (CleanupsDepth from,
98
99
CleanupsDepth to) {
99
- return ::hasAnyActiveCleanups (Stack .find (from), Stack .find (to));
100
+ return ::hasAnyActiveCleanups (stack .find (from), stack .find (to));
100
101
}
101
102
102
103
bool CleanupManager::hasAnyActiveCleanups (CleanupsDepth from) {
103
- return ::hasAnyActiveCleanups (Stack .begin (), Stack .find (from));
104
+ return ::hasAnyActiveCleanups (stack .begin (), stack .find (from));
104
105
}
105
106
106
107
// / emitBranchAndCleanups - Emit a branch to the given jump destination,
107
108
// / threading out through any cleanups we might need to run. This does not
108
109
// / pop the cleanup stack.
109
- void CleanupManager::emitBranchAndCleanups (JumpDest Dest ,
110
- SILLocation BranchLoc,
111
- ArrayRef<SILValue> Args) {
112
- SILGenBuilder &B = Gen. getBuilder ( );
113
- assert (B. hasValidInsertionPoint () && " Emitting branch in invalid spot " );
114
- emitCleanups (Dest. getDepth (), Dest. getCleanupLocation (), /* popCleanups=*/ false );
115
- B .createBranch (BranchLoc, Dest .getBlock (), Args );
110
+ void CleanupManager::emitBranchAndCleanups (JumpDest dest, SILLocation branchLoc ,
111
+ ArrayRef<SILValue> args) {
112
+ SILGenBuilder &builder = SGF. getBuilder ();
113
+ assert (builder. hasValidInsertionPoint () && " Emitting branch in invalid spot " );
114
+ emitCleanups (dest. getDepth (), dest. getCleanupLocation (),
115
+ /* popCleanups=*/ false );
116
+ builder .createBranch (branchLoc, dest .getBlock (), args );
116
117
}
117
118
118
- void CleanupManager::emitCleanupsForReturn (CleanupLocation Loc ) {
119
- SILGenBuilder &B = Gen .getBuilder ();
120
- assert (B .hasValidInsertionPoint () && " Emitting return in invalid spot" );
121
- (void ) B ;
122
- emitCleanups (Stack .stable_end (), Loc , /* popCleanups=*/ false );
119
+ void CleanupManager::emitCleanupsForReturn (CleanupLocation loc ) {
120
+ SILGenBuilder &builder = SGF .getBuilder ();
121
+ assert (builder .hasValidInsertionPoint () && " Emitting return in invalid spot" );
122
+ (void )builder ;
123
+ emitCleanups (stack .stable_end (), loc , /* popCleanups=*/ false );
123
124
}
124
125
125
126
// / Emit a new block that jumps to the specified location and runs necessary
126
127
// / cleanups based on its level. If there are no cleanups to run, this just
127
128
// / returns the dest block.
128
- SILBasicBlock *CleanupManager::emitBlockForCleanups (JumpDest Dest ,
129
- SILLocation BranchLoc ,
130
- ArrayRef<SILValue> Args ) {
129
+ SILBasicBlock *CleanupManager::emitBlockForCleanups (JumpDest dest ,
130
+ SILLocation branchLoc ,
131
+ ArrayRef<SILValue> args ) {
131
132
// If there are no cleanups to run, just return the Dest block directly.
132
- if (!hasAnyActiveCleanups (Dest .getDepth ()))
133
- return Dest .getBlock ();
133
+ if (!hasAnyActiveCleanups (dest .getDepth ()))
134
+ return dest .getBlock ();
134
135
135
136
// Otherwise, create and emit a new block.
136
- auto *NewBB = Gen .createBasicBlock ();
137
- SavedInsertionPoint IPRAII (Gen, NewBB );
138
- emitBranchAndCleanups (Dest, BranchLoc, Args );
139
- return NewBB ;
137
+ auto *newBlock = SGF .createBasicBlock ();
138
+ SavedInsertionPoint IPRAII (SGF, newBlock );
139
+ emitBranchAndCleanups (dest, branchLoc, args );
140
+ return newBlock ;
140
141
}
141
142
142
143
@@ -149,17 +150,17 @@ Cleanup &CleanupManager::initCleanup(Cleanup &cleanup,
149
150
}
150
151
151
152
void CleanupManager::setCleanupState (CleanupsDepth depth, CleanupState state) {
152
- auto iter = Stack .find (depth);
153
- assert (iter != Stack .end () && " can't change end of cleanups stack" );
153
+ auto iter = stack .find (depth);
154
+ assert (iter != stack .end () && " can't change end of cleanups stack" );
154
155
setCleanupState (*iter, state);
155
-
156
- if (state == CleanupState::Dead && iter == Stack .begin ())
157
- popTopDeadCleanups (InnermostScope );
156
+
157
+ if (state == CleanupState::Dead && iter == stack .begin ())
158
+ popTopDeadCleanups (innermostScope );
158
159
}
159
160
160
161
void CleanupManager::forwardCleanup (CleanupsDepth handle) {
161
- auto iter = Stack .find (handle);
162
- assert (iter != Stack .end () && " can't change end of cleanups stack" );
162
+ auto iter = stack .find (handle);
163
+ assert (iter != stack .end () && " can't change end of cleanups stack" );
163
164
Cleanup &cleanup = *iter;
164
165
assert (cleanup.isActive () && " forwarding inactive or dead cleanup?" );
165
166
@@ -168,18 +169,18 @@ void CleanupManager::forwardCleanup(CleanupsDepth handle) {
168
169
: CleanupState::Dormant);
169
170
setCleanupState (cleanup, newState);
170
171
171
- if (newState == CleanupState::Dead && iter == Stack .begin ())
172
- popTopDeadCleanups (InnermostScope );
172
+ if (newState == CleanupState::Dead && iter == stack .begin ())
173
+ popTopDeadCleanups (innermostScope );
173
174
}
174
175
175
176
void CleanupManager::setCleanupState (Cleanup &cleanup, CleanupState state) {
176
- assert (Gen .B .hasValidInsertionPoint () &&
177
+ assert (SGF .B .hasValidInsertionPoint () &&
177
178
" changing cleanup state at invalid IP" );
178
179
179
180
// Do the transition now to avoid doing it in N places below.
180
181
CleanupState oldState = cleanup.getState ();
181
182
(void )oldState;
182
- cleanup.setState (Gen , state);
183
+ cleanup.setState (SGF , state);
183
184
184
185
assert (state != oldState && " trivial cleanup state change" );
185
186
assert (oldState != CleanupState::Dead && " changing state of dead cleanup" );
@@ -194,45 +195,46 @@ void CleanupStateRestorationScope::pushCleanupState(CleanupHandle handle,
194
195
// Don't put the cleanup in a state we can't restore it from.
195
196
assert (newState != CleanupState::Dead && " cannot restore cleanup from death" );
196
197
197
- auto iter = Cleanups. Stack .find (handle);
198
- assert (iter != Cleanups. Stack .end () && " can't change end of cleanups stack" );
198
+ auto iter = cleanups. stack .find (handle);
199
+ assert (iter != cleanups. stack .end () && " can't change end of cleanups stack" );
199
200
Cleanup &cleanup = *iter;
200
201
assert (cleanup.getState () != CleanupState::Dead &&
201
202
" changing state of dead cleanup" );
202
203
203
204
CleanupState oldState = cleanup.getState ();
204
- cleanup.setState (Cleanups. Gen , newState);
205
+ cleanup.setState (cleanups. SGF , newState);
205
206
206
- SavedStates .push_back ({handle, oldState});
207
+ savedStates .push_back ({handle, oldState});
207
208
}
208
209
209
210
void
210
211
CleanupStateRestorationScope::pushCurrentCleanupState (CleanupHandle handle) {
211
- auto iter = Cleanups. Stack .find (handle);
212
- assert (iter != Cleanups. Stack .end () && " can't change end of cleanups stack" );
212
+ auto iter = cleanups. stack .find (handle);
213
+ assert (iter != cleanups. stack .end () && " can't change end of cleanups stack" );
213
214
Cleanup &cleanup = *iter;
214
215
assert (cleanup.getState () != CleanupState::Dead &&
215
216
" changing state of dead cleanup" );
216
217
217
218
CleanupState oldState = cleanup.getState ();
218
- SavedStates .push_back ({handle, oldState});
219
+ savedStates .push_back ({handle, oldState});
219
220
}
220
221
221
222
void CleanupStateRestorationScope::popImpl () {
222
223
// Restore cleanup states in the opposite order in which we saved them.
223
- for (auto i = SavedStates .rbegin (), e = SavedStates .rend (); i != e; ++i) {
224
+ for (auto i = savedStates .rbegin (), e = savedStates .rend (); i != e; ++i) {
224
225
CleanupHandle handle = i->first ;
225
226
CleanupState stateToRestore = i->second ;
226
227
227
- auto iter = Cleanups.Stack .find (handle);
228
- assert (iter != Cleanups.Stack .end () && " can't change end of cleanups stack" );
228
+ auto iter = cleanups.stack .find (handle);
229
+ assert (iter != cleanups.stack .end () &&
230
+ " can't change end of cleanups stack" );
229
231
Cleanup &cleanup = *iter;
230
232
assert (cleanup.getState () != CleanupState::Dead &&
231
233
" changing state of dead cleanup" );
232
- cleanup.setState (Cleanups. Gen , stateToRestore);
234
+ cleanup.setState (cleanups. SGF , stateToRestore);
233
235
}
234
236
235
- SavedStates .clear ();
237
+ savedStates .clear ();
236
238
}
237
239
238
240
void CleanupStateRestorationScope::pop () && { popImpl (); }
@@ -255,22 +257,22 @@ llvm::raw_ostream &Lowering::operator<<(llvm::raw_ostream &os,
255
257
256
258
void CleanupManager::dump () const {
257
259
#ifndef NDEBUG
258
- auto begin = Stack .stable_begin ();
259
- auto end = Stack .stable_end ();
260
+ auto begin = stack .stable_begin ();
261
+ auto end = stack .stable_end ();
260
262
while (begin != end) {
261
- auto iter = Stack .find (begin);
263
+ auto iter = stack .find (begin);
262
264
const Cleanup &stackCleanup = *iter;
263
265
llvm::errs () << " CLEANUP DEPTH: " << begin.getDepth () << " \n " ;
264
- stackCleanup.dump (Gen );
265
- begin = Stack .stabilize (++iter);
266
- Stack .checkIterator (begin);
266
+ stackCleanup.dump (SGF );
267
+ begin = stack .stabilize (++iter);
268
+ stack .checkIterator (begin);
267
269
}
268
270
#endif
269
271
}
270
272
271
273
void CleanupManager::dump (CleanupHandle handle) const {
272
- auto iter = Stack .find (handle);
274
+ auto iter = stack .find (handle);
273
275
const Cleanup &stackCleanup = *iter;
274
276
llvm::errs () << " CLEANUP DEPTH: " << handle.getDepth () << " \n " ;
275
- stackCleanup.dump (Gen );
277
+ stackCleanup.dump (SGF );
276
278
}
0 commit comments