source: src/Virtual/VirtualDtor.cpp@ 699a97d

ADT ast-experimental
Last change on this file since 699a97d was 3830c84, checked in by caparsons <caparson@…>, 3 years ago

cleaned up actor pass and added virtual destructor pass

  • Property mode set to 100644
File size: 14.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// VirtualDtor.cpp -- generate code needed by the actor system
8//
9// Author : Colby Parsons
10// Created On : Tues Mar 14 15:16:42 2023
11// Last Modified By : Colby Parsons
12// Last Modified On : Tues Mar 14 15:16:42 2023
13// Update Count : 0
14//
15
16#include "AST/Print.hpp"
17#include "AST/Decl.hpp"
18#include "AST/Pass.hpp"
19#include "AST/Type.hpp"
20#include "AST/Stmt.hpp"
21#include "AST/TranslationUnit.hpp"
22#include "AST/Expr.hpp"
23#include <algorithm>
24using namespace ast;
25using namespace std;
26
27namespace Virtual {
28
29struct CtorDtor {
30 FunctionDecl * dtorSetup; // dtor init routine to add after last dtor for a struct
31 FunctionDecl * deleteFn;
32 FunctionDecl * lastDtor; // pointer to last occurence of dtor to know where to insert after
33
34 CtorDtor() : dtorSetup(nullptr), deleteFn(nullptr), lastDtor(nullptr) {}
35};
36
37class CtorDtorTable {
38 unordered_map<const StructDecl *, CtorDtor> & structMap;
39
40 public:
41 // if dtor is last dtor for this decl return the routine to add afterwards
42 // otherwise return nullptr
43 FunctionDecl * getToAddLater( const StructDecl * decl, FunctionDecl * dtor, FunctionDecl ** retDeleteFn ) {
44 auto iter = structMap.find( decl );
45 if ( iter == structMap.end() || iter->second.lastDtor != dtor ) return nullptr; // check if this is needed
46 *retDeleteFn = iter->second.deleteFn;
47 return iter->second.dtorSetup;
48 }
49
50 // return if the dtorSetup field has been defined for this decl
51 bool inTable( const StructDecl * decl ) {
52 auto iter = structMap.find( decl );
53 return iter->second.dtorSetup != nullptr;
54 }
55
56 void addLater( const StructDecl * decl, FunctionDecl * dtorSetup, FunctionDecl * deleteFn ) {
57 auto iter = structMap.find( decl );
58 iter->second.dtorSetup = dtorSetup;
59 iter->second.deleteFn = deleteFn;
60 }
61
62 void addDtor( const StructDecl * decl, FunctionDecl * dtor ) {
63 auto iter = structMap.find( decl );
64 iter->second.lastDtor = dtor;
65 }
66
67 CtorDtorTable( unordered_map<const StructDecl *, CtorDtor> & structMap ) : structMap(structMap) {}
68};
69
70struct CollectStructDecls : public ast::WithGuards {
71 unordered_map<const StructDecl *, CtorDtor> & structDecls;
72 StructDecl * parentDecl;
73 bool insideStruct = false;
74 bool namedDecl = false;
75
76 const StructDecl ** virtualDtor;
77
78 // finds and sets a ptr to the actor, message, and request structs, which are needed in the next pass
79 void previsit( const StructDecl * decl ) {
80 if ( !decl->body ) return;
81 if( decl->name == "virtual_dtor" ) {
82 structDecls.emplace( make_pair( decl, CtorDtor() ) );
83 *virtualDtor = decl;
84 } else {
85 GuardValue(insideStruct);
86 insideStruct = true;
87 parentDecl = mutate( decl );
88 }
89 }
90
91 // this catches structs of the form:
92 // struct derived_type { virtual_dtor a; };
93 // since they should be:
94 // struct derived_type { inline virtual_dtor; };
95 void previsit ( const ObjectDecl * decl ) {
96 if ( insideStruct && ! decl->name.empty() ) {
97 GuardValue(namedDecl);
98 namedDecl = true;
99 }
100 }
101
102 // this collects the derived actor and message struct decl ptrs
103 void postvisit( const StructInstType * node ) {
104 if ( ! *virtualDtor ) return;
105 if ( insideStruct && !namedDecl ) {
106 auto structIter = structDecls.find( node->aggr() );
107 if ( structIter != structDecls.end() )
108 structDecls.emplace( make_pair( parentDecl, CtorDtor() ) );
109 }
110 }
111
112 public:
113 CollectStructDecls( unordered_map<const StructDecl *, CtorDtor> & structDecls, const StructDecl ** virtualDtor ):
114 structDecls( structDecls ), virtualDtor(virtualDtor) {}
115};
116
117// generates the forward decl of virtual dtor setting routine and delete routine
118// generates the call to the virtual dtor routine in each appropriate ctor
119// collects data needed for next pass that does the circular defn resolution
120// for dtor setters and delete fns (via table above)
121struct GenFuncsCreateTables : public ast::WithDeclsToAdd<> {
122 unordered_map<const StructDecl *, CtorDtor> & structDecls;
123 CtorDtorTable & torDecls;
124 const StructDecl ** virtualDtor;
125
126 // collects the dtor info for actors/messages
127 // gens the dtor fwd decl and dtor call in ctor
128 void previsit( const FunctionDecl * decl ) {
129 if ( (decl->name != "?{}" && decl->name != "^?{}") || decl->params.size() == 0
130 || !decl->stmts || (decl->name == "^?{}" && decl->params.size() != 1)) return;
131
132 // the first param should be a reference
133 const ReferenceType * ref = dynamic_cast<const ReferenceType *>(decl->params.at(0)->get_type());
134 if ( !ref ) return;
135
136 // the reference should be to a struct instance
137 const StructInstType * instType = dynamic_cast<const StructInstType *>(ref->base.get());
138 if ( !instType ) return;
139
140 // return if not ctor/dtor for an actor or message
141 auto structIter = structDecls.find( instType->aggr() );
142 if ( structIter == structDecls.end() ) return;
143
144 if ( decl->name == "^?{}") {
145 torDecls.addDtor( structIter->first, mutate( decl ) );
146
147 CompoundStmt * dtorBody = mutate( decl->stmts.get() );
148 // Adds the following to the end of any actor/message dtor:
149 // __CFA_dtor_shutdown( this );
150 dtorBody->push_front( new ExprStmt(
151 decl->location,
152 new UntypedExpr (
153 decl->location,
154 new NameExpr( decl->location, "__CFA_dtor_shutdown" ),
155 {
156 new NameExpr( decl->location, decl->params.at(0)->name )
157 }
158 )
159 ));
160 return;
161 }
162
163 // not dtor by this point so must be ctor
164 CompoundStmt * ctorBody = mutate( decl->stmts.get() );
165 // Adds the following to the end of any actor/message ctor:
166 // __CFA_set_dtor( this );
167 ctorBody->push_back( new ExprStmt(
168 decl->location,
169 new UntypedExpr (
170 decl->location,
171 new NameExpr( decl->location, "__CFA_set_dtor" ),
172 {
173 new NameExpr( decl->location, decl->params.at(0)->name )
174 }
175 )
176 ));
177
178 if ( torDecls.inTable( structIter->first ) ) return;
179
180 // Generates the following:
181 // void __CFA_set_dtor( Derived_type & this ){
182 // void (*__my_dtor)( Derived_type & ) = ^?{};
183 // this.__virtual_dtor = (void (*)( Base_type & ))__my_dtor;
184 // this.__virtual_obj_start = (void *)(&this);
185 // }
186 CompoundStmt * setDtorBody = new CompoundStmt( decl->location );
187
188 // Function type is: (void (*)(Derived_type &))
189 FunctionType * derivedDtor = new FunctionType();
190 derivedDtor->params.push_back( ast::deepCopy( ref ) );
191
192 // Generates:
193 // void (*__my_dtor)( Derived_type & ) = ^?{};
194 setDtorBody->push_back( new DeclStmt(
195 decl->location,
196 new ObjectDecl(
197 decl->location,
198 "__my_dtor",
199 new PointerType( derivedDtor ),
200 new SingleInit( decl->location, new NameExpr( decl->location, "^?{}" ) )
201 )
202 ));
203
204 // Function type is: (void (*)( Base_type & ))
205 FunctionType * baseDtor = new FunctionType();
206 baseDtor->params.push_back( new ReferenceType( new StructInstType( *virtualDtor ) ) );
207
208 // Generates:
209 // __CFA_set_virt_dtor( this, (void (*)( Base_type & ))__my_dtor )
210 setDtorBody->push_back( new ExprStmt(
211 decl->location,
212 new UntypedExpr (
213 decl->location,
214 new NameExpr( decl->location, "__CFA_set_virt_dtor" ),
215 {
216 new NameExpr( decl->location, "this" ),
217 new CastExpr( decl->location, new NameExpr( decl->location, "__my_dtor" ), new PointerType( baseDtor ), ExplicitCast )
218 }
219 )
220 ));
221
222 // Generates:
223 // __CFA_set_virt_start( (void *)(&this) );
224 setDtorBody->push_back( new ExprStmt(
225 decl->location,
226 new UntypedExpr (
227 decl->location,
228 new NameExpr( decl->location, "__CFA_set_virt_start" ),
229 {
230 new NameExpr( decl->location, "this" ),
231 new CastExpr(
232 decl->location,
233 new AddressExpr( decl->location, new NameExpr( decl->location, "this" )),
234 new PointerType( new ast::VoidType() ), ExplicitCast
235 )
236 }
237 )
238 ));
239
240 // put it all together into the complete function decl from above
241 FunctionDecl * setDtorFunction = new FunctionDecl(
242 decl->location,
243 "__CFA_set_dtor",
244 {}, // forall
245 {
246 new ObjectDecl(
247 decl->location,
248 "this",
249 ast::deepCopy( ref )
250 ),
251 }, // params
252 {},
253 nullptr, // body
254 { Storage::Static }, // storage
255 Linkage::Cforall, // linkage
256 {}, // attributes
257 { Function::Inline }
258 );
259
260 declsToAddBefore.push_back( ast::deepCopy( setDtorFunction ) );
261
262 setDtorFunction->stmts = setDtorBody;
263
264 // The following generates the following specialized delete routine:
265 // static inline void delete( derived_type * ptr ) {
266 // if ( ptr )
267 // ^(*ptr){};
268 // __CFA_virt_free( *ptr );
269 // }
270 CompoundStmt * deleteFnBody = new CompoundStmt( decl->location );
271
272 // Generates:
273 // if ( ptr )
274 // ^(*ptr){};
275 deleteFnBody->push_back(
276 new IfStmt(
277 decl->location,
278 UntypedExpr::createCall(
279 decl->location,
280 "?!=?",
281 {
282 new NameExpr( decl->location, "ptr" ),
283 ConstantExpr::null( decl->location, new PointerType( ast::deepCopy( instType ) ) )
284 }
285 ),
286 new ExprStmt(
287 decl->location,
288 UntypedExpr::createCall(
289 decl->location,
290 "^?{}",
291 {
292 UntypedExpr::createDeref( decl->location, new NameExpr( decl->location, "ptr" ))
293 }
294 )
295 )
296 )
297 );
298
299 // Generates:
300 // __CFA_virt_free( *ptr );
301 deleteFnBody->push_back( new ExprStmt(
302 decl->location,
303 UntypedExpr::createCall(
304 decl->location,
305 "__CFA_virt_free",
306 {
307 UntypedExpr::createDeref( decl->location, new NameExpr( decl->location, "ptr" ))
308 }
309 )
310 )
311 );
312
313 FunctionDecl * deleteFn = new FunctionDecl(
314 decl->location,
315 "delete",
316 {}, // forall
317 {
318 new ObjectDecl(
319 decl->location,
320 "ptr",
321 new PointerType( ast::deepCopy( instType ) )
322 ),
323 }, // params
324 {},
325 nullptr, // body
326 { Storage::Static }, // storage
327 Linkage::Cforall, // linkage
328 {}, // attributes
329 { Function::Inline }
330 );
331
332 declsToAddBefore.push_back( ast::deepCopy( deleteFn ) );
333
334 deleteFn->stmts = deleteFnBody;
335
336 torDecls.addLater( structIter->first, setDtorFunction, deleteFn );
337 }
338
339 public:
340 GenFuncsCreateTables( unordered_map<const StructDecl *, CtorDtor> & structDecls, CtorDtorTable & torDecls, const StructDecl ** virtualDtor ):
341 structDecls(structDecls), torDecls(torDecls), virtualDtor(virtualDtor) {}
342};
343
344
345// generates the trailing definitions of dtor setting routines for virtual dtors on messages and actors
346// generates the function defns of __CFA_set_dtor
347// separate pass is needed since __CFA_set_dtor needs to be defined after
348// the last dtor defn which is found in prior pass
349struct GenSetDtor : public ast::WithDeclsToAdd<> {
350 unordered_map<const StructDecl *, CtorDtor> & structDecls; // set of decls that inherit from virt dtor
351 CtorDtorTable & torDecls;
352
353 // handles adding the declaration of the dtor init routine after the last dtor detected
354 void postvisit( const FunctionDecl * decl ) {
355 if ( decl->name != "^?{}" || !decl->stmts || decl->params.size() != 1 ) return;
356
357 // the one param should be a reference
358 const ReferenceType * ref = dynamic_cast<const ReferenceType *>(decl->params.at(0)->get_type());
359 if ( !ref ) return;
360
361 // the reference should be to a struct instance
362 const StructInstType * instType = dynamic_cast<const StructInstType *>(ref->base.get());
363 if ( !instType ) return;
364
365 FunctionDecl * deleteRtn;
366
367 // returns nullptr if not in table
368 FunctionDecl * maybeAdd = torDecls.getToAddLater( instType->aggr(), mutate( decl ), &deleteRtn );
369 if ( maybeAdd ) {
370 declsToAddAfter.push_back( maybeAdd );
371 declsToAddAfter.push_back( deleteRtn );
372 }
373 }
374
375 public:
376 GenSetDtor( unordered_map<const StructDecl *, CtorDtor> & structDecls, CtorDtorTable & torDecls ):
377 structDecls(structDecls), torDecls(torDecls) {}
378};
379
380void implementVirtDtors( TranslationUnit & translationUnit ) {
381 // unordered_map to collect all derived types and associated data
382 unordered_map<const StructDecl *, CtorDtor> structDecls;
383 CtorDtorTable torDecls( structDecls );
384
385 const StructDecl * virtualDtorPtr = nullptr;
386 const StructDecl ** virtualDtor = &virtualDtorPtr;
387
388 // first pass collects all structs that inherit from virtual_dtor
389 Pass<CollectStructDecls>::run( translationUnit, structDecls, virtualDtor );
390
391 // second pass locates all dtor/ctor routines that need modifying or need fns inserted before/after
392 Pass<GenFuncsCreateTables>::run( translationUnit, structDecls, torDecls, virtualDtor );
393
394 // The third pass adds the forward decls needed to resolve circular defn problems
395 Pass<GenSetDtor>::run( translationUnit, structDecls, torDecls );
396}
397
398
399} // namespace Virtual
400
401// Local Variables: //
402// tab-width: 4 //
403// mode: c++ //
404// compile-command: "make install" //
405// End: //
406
Note: See TracBrowser for help on using the repository browser.