source: src/Virtual/VirtualDtor.cpp@ 994030ce

Last change on this file since 994030ce was 7e4bd9b6, checked in by caparsons <caparson@…>, 2 years ago

updated actor-related passes to fix some bugs

  • Property mode set to 100644
File size: 15.0 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 start of any actor/message dtor:
149 // __CFA_dtor_shutdown( this );
150 dtorBody->push_front(
151 new IfStmt( 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 new ReturnStmt( decl->location, nullptr )
160 )
161 );
162 return;
163 }
164
165 // not dtor by this point so must be ctor
166 CompoundStmt * ctorBody = mutate( decl->stmts.get() );
167 // Adds the following to the end of any actor/message ctor:
168 // __CFA_set_dtor( this );
169 ctorBody->push_back( new ExprStmt(
170 decl->location,
171 new UntypedExpr (
172 decl->location,
173 new NameExpr( decl->location, "__CFA_set_dtor" ),
174 {
175 new NameExpr( decl->location, decl->params.at(0)->name )
176 }
177 )
178 ));
179
180 if ( torDecls.inTable( structIter->first ) ) return;
181
182 // Generates the following:
183 // void __CFA_set_dtor( Derived_type & this ){
184 // void (*__my_dtor)( Derived_type & ) = ^?{};
185 // this.__virtual_dtor = (void (*)( Base_type & ))__my_dtor;
186 // this.__virtual_obj_start = (void *)(&this);
187 // }
188 CompoundStmt * setDtorBody = new CompoundStmt( decl->location );
189
190 // Function type is: (void (*)(Derived_type &))
191 FunctionType * derivedDtor = new FunctionType();
192 derivedDtor->params.push_back( ast::deepCopy( ref ) );
193
194 // Generates:
195 // void (*__my_dtor)( Derived_type & ) = ^?{};
196 setDtorBody->push_back( new DeclStmt(
197 decl->location,
198 new ObjectDecl(
199 decl->location,
200 "__my_dtor",
201 new PointerType( derivedDtor ),
202 new SingleInit( decl->location, new NameExpr( decl->location, "^?{}" ) )
203 )
204 ));
205
206 // Function type is: (void (*)( Base_type & ))
207 FunctionType * baseDtor = new FunctionType();
208 baseDtor->params.push_back( new ReferenceType( new StructInstType( *virtualDtor ) ) );
209
210 // Generates:
211 // __CFA_set_virt_dtor( this, (void (*)( Base_type & ))__my_dtor )
212 setDtorBody->push_back( new ExprStmt(
213 decl->location,
214 new UntypedExpr (
215 decl->location,
216 new NameExpr( decl->location, "__CFA_set_virt_dtor" ),
217 {
218 new NameExpr( decl->location, "this" ),
219 new CastExpr( decl->location, new NameExpr( decl->location, "__my_dtor" ), new PointerType( baseDtor ), ExplicitCast )
220 }
221 )
222 ));
223
224 // Generates:
225 // __CFA_set_virt_start( (void *)(&this) );
226 setDtorBody->push_back( new ExprStmt(
227 decl->location,
228 new UntypedExpr (
229 decl->location,
230 new NameExpr( decl->location, "__CFA_set_virt_start" ),
231 {
232 new NameExpr( decl->location, "this" ),
233 new CastExpr(
234 decl->location,
235 new AddressExpr( decl->location, new NameExpr( decl->location, "this" )),
236 new PointerType( new ast::VoidType() ), ExplicitCast
237 )
238 }
239 )
240 ));
241
242 // put it all together into the complete function decl from above
243 FunctionDecl * setDtorFunction = new FunctionDecl(
244 decl->location,
245 "__CFA_set_dtor",
246 {}, // forall
247 {
248 new ObjectDecl(
249 decl->location,
250 "this",
251 ast::deepCopy( ref )
252 ),
253 }, // params
254 {},
255 nullptr, // body
256 { Storage::Static }, // storage
257 Linkage::Cforall, // linkage
258 {}, // attributes
259 { Function::Inline }
260 );
261
262 declsToAddBefore.push_back( ast::deepCopy( setDtorFunction ) );
263
264 setDtorFunction->stmts = setDtorBody;
265
266 // The following generates the following specialized delete routine:
267 // static inline void delete( derived_type * ptr ) {
268 // if ( ptr )
269 // ^(*ptr){};
270 // __CFA_virt_free( *ptr );
271 // }
272 CompoundStmt * deleteFnBody = new CompoundStmt( decl->location );
273
274 // Generates:
275 // if ( ptr )
276 // ^(*ptr){};
277 deleteFnBody->push_back(
278 new IfStmt(
279 decl->location,
280 UntypedExpr::createCall(
281 decl->location,
282 "?!=?",
283 {
284 new NameExpr( decl->location, "ptr" ),
285 ConstantExpr::null( decl->location, new PointerType( ast::deepCopy( instType ) ) )
286 }
287 ),
288 new ExprStmt(
289 decl->location,
290 UntypedExpr::createCall(
291 decl->location,
292 "^?{}",
293 {
294 UntypedExpr::createDeref( decl->location, new NameExpr( decl->location, "ptr" ))
295 }
296 )
297 )
298 )
299 );
300
301 // Generates:
302 // __CFA_virt_free( *ptr );
303 deleteFnBody->push_back( new ExprStmt(
304 decl->location,
305 UntypedExpr::createCall(
306 decl->location,
307 "__CFA_virt_free",
308 {
309 UntypedExpr::createDeref( decl->location, new NameExpr( decl->location, "ptr" ))
310 }
311 )
312 )
313 );
314
315 FunctionDecl * deleteFn = new FunctionDecl(
316 decl->location,
317 "delete",
318 {}, // forall
319 {
320 new ObjectDecl(
321 decl->location,
322 "ptr",
323 new PointerType( ast::deepCopy( instType ) )
324 ),
325 }, // params
326 {},
327 nullptr, // body
328 { Storage::Static }, // storage
329 Linkage::Cforall, // linkage
330 {}, // attributes
331 { Function::Inline }
332 );
333
334 declsToAddBefore.push_back( ast::deepCopy( deleteFn ) );
335
336 deleteFn->stmts = deleteFnBody;
337
338 torDecls.addLater( structIter->first, setDtorFunction, deleteFn );
339 }
340
341 public:
342 GenFuncsCreateTables( unordered_map<const StructDecl *, CtorDtor> & structDecls, CtorDtorTable & torDecls, const StructDecl ** virtualDtor ):
343 structDecls(structDecls), torDecls(torDecls), virtualDtor(virtualDtor) {}
344};
345
346
347// generates the trailing definitions of dtor setting routines for virtual dtors on messages and actors
348// generates the function defns of __CFA_set_dtor
349// separate pass is needed since __CFA_set_dtor needs to be defined after
350// the last dtor defn which is found in prior pass
351struct GenSetDtor : public ast::WithDeclsToAdd<> {
352 unordered_map<const StructDecl *, CtorDtor> & structDecls; // set of decls that inherit from virt dtor
353 CtorDtorTable & torDecls;
354
355 // handles adding the declaration of the dtor init routine after the last dtor detected
356 void postvisit( const FunctionDecl * decl ) {
357 if ( decl->name != "^?{}" || !decl->stmts || decl->params.size() != 1 ) return;
358
359 // the one param should be a reference
360 const ReferenceType * ref = dynamic_cast<const ReferenceType *>(decl->params.at(0)->get_type());
361 if ( !ref ) return;
362
363 // the reference should be to a struct instance
364 const StructInstType * instType = dynamic_cast<const StructInstType *>(ref->base.get());
365 if ( !instType ) return;
366
367 FunctionDecl * deleteRtn;
368
369 // returns nullptr if not in table
370 FunctionDecl * maybeAdd = torDecls.getToAddLater( instType->aggr(), mutate( decl ), &deleteRtn );
371 if ( maybeAdd ) {
372 declsToAddAfter.push_back( maybeAdd );
373 declsToAddAfter.push_back( deleteRtn );
374 }
375 }
376
377 public:
378 GenSetDtor( unordered_map<const StructDecl *, CtorDtor> & structDecls, CtorDtorTable & torDecls ):
379 structDecls(structDecls), torDecls(torDecls) {}
380};
381
382void implementVirtDtors( TranslationUnit & translationUnit ) {
383 // unordered_map to collect all derived types and associated data
384 unordered_map<const StructDecl *, CtorDtor> structDecls;
385 CtorDtorTable torDecls( structDecls );
386
387 const StructDecl * virtualDtorPtr = nullptr;
388 const StructDecl ** virtualDtor = &virtualDtorPtr;
389
390 // first pass collects all structs that inherit from virtual_dtor
391 Pass<CollectStructDecls>::run( translationUnit, structDecls, virtualDtor );
392
393 // second pass locates all dtor/ctor routines that need modifying or need fns inserted before/after
394 Pass<GenFuncsCreateTables>::run( translationUnit, structDecls, torDecls, virtualDtor );
395
396 // The third pass adds the forward decls needed to resolve circular defn problems
397 Pass<GenSetDtor>::run( translationUnit, structDecls, torDecls );
398}
399
400
401} // namespace Virtual
402
403// Local Variables: //
404// tab-width: 4 //
405// mode: c++ //
406// compile-command: "make install" //
407// End: //
408
Note: See TracBrowser for help on using the repository browser.