source: src/InitTweak/FixInit.cc @ e0323a2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since e0323a2 was e0323a2, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

routine scoped static variables are constructed once the first time the routine is called, and destructed once at the end of the program using atexit

  • Property mode set to 100644
File size: 6.3 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// FixInit.h --
8//
9// Author           : Rob Schluntz
10// Created On       : Wed Jan 13 16:29:30 2016
11// Last Modified By : Rob Schluntz
12// Last Modified On : Thu Mar 31 10:08:49 2016
13// Update Count     : 30
14//
15
16#include <stack>
17#include <list>
18#include "RemoveInit.h"
19#include "SynTree/Declaration.h"
20#include "SynTree/Type.h"
21#include "SynTree/Expression.h"
22#include "SynTree/Statement.h"
23#include "SynTree/Initializer.h"
24#include "SynTree/Mutator.h"
25#include "GenPoly/PolyMutator.h"
26
27namespace InitTweak {
28        namespace {
29                const std::list<Label> noLabels;
30                const std::list<Expression*> noDesignators;
31        }
32
33        class FixInit : public GenPoly::PolyMutator {
34          public:
35                static void fixInitializers( std::list< Declaration * > &translationUnit );
36
37                virtual ObjectDecl * mutate( ObjectDecl *objDecl );
38
39                virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
40
41          private:
42                std::list< Statement * > dtorStmts;
43        };
44
45        void fix( std::list< Declaration * > & translationUnit ) {
46                FixInit::fixInitializers( translationUnit );
47        }
48
49        void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) {
50                FixInit fixer;
51                mutateAll( translationUnit, fixer );
52        }
53
54        ObjectDecl *FixInit::mutate( ObjectDecl *objDecl ) {
55                if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
56                        // a decision should have been made by the resolver, so ctor and init are not both non-NULL
57                        assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
58                        if ( Statement * ctor = ctorInit->get_ctor() ) {
59                                if ( objDecl->get_storageClass() == DeclarationNode::Static ) {
60                                        // generate:
61                                        // static bool __objName_uninitialized = true;
62                                        // if (__objName_uninitialized) {
63                                        //   __ctor(__objName);
64                                        //   void dtor_atexit() {
65                                        //     __dtor(__objName);
66                                        //   }
67                                        //   on_exit(dtorOnExit, &__objName);
68                                        //   __objName_uninitialized = false;
69                                        // }
70
71                                        // generate first line
72                                        BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool );
73                                        SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant( boolType->clone(), "1" ) ), noDesignators );
74                                        ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", DeclarationNode::Static, LinkageSpec::Cforall, 0, boolType, boolInitExpr );
75                                        isUninitializedVar->fixUniqueId();
76
77                                        // void dtor_atexit(...) {...}
78                                        FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + "_dtor_atexit", DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
79                                        dtorCaller->fixUniqueId();
80                                        dtorCaller->get_statements()->get_kids().push_back( ctorInit->get_dtor() );
81
82                                        // on_exit(dtor_atexit);
83                                        UntypedExpr * callAtexit = new UntypedExpr( new NameExpr( "atexit" ) );
84                                        callAtexit->get_args().push_back( new VariableExpr( dtorCaller ) );
85
86                                        // __objName_uninitialized = false;
87                                        UntypedExpr * setTrue = new UntypedExpr( new NameExpr( "?=?" ) );
88                                        setTrue->get_args().push_back( new VariableExpr( isUninitializedVar ) );
89                                        setTrue->get_args().push_back( new ConstantExpr( Constant( boolType->clone(), "0" ) ) );
90
91                                        // generate body of if
92                                        CompoundStmt * initStmts = new CompoundStmt( noLabels );
93                                        std::list< Statement * > & body = initStmts->get_kids();
94                                        body.push_back( ctor );
95                                        body.push_back( new DeclStmt( noLabels, dtorCaller ) );
96                                        body.push_back( new ExprStmt( noLabels, callAtexit ) );
97                                        body.push_back( new ExprStmt( noLabels, setTrue ) );
98
99                                        // put it all together
100                                        IfStmt * ifStmt = new IfStmt( noLabels, new VariableExpr( isUninitializedVar ), initStmts, 0 );
101                                        stmtsToAddAfter.push_back( new DeclStmt( noLabels, isUninitializedVar ) );
102                                        stmtsToAddAfter.push_back( ifStmt );
103                                } else {
104                                        stmtsToAddAfter.push_back( ctor );
105                                        dtorStmts.push_front( ctorInit->get_dtor() );
106                                }
107                                objDecl->set_init( NULL );
108                                ctorInit->set_ctor( NULL );
109                                ctorInit->set_dtor( NULL );  // xxx - only destruct when constructing? Probably not?
110                        } else if ( Initializer * init = ctorInit->get_init() ) {
111                                objDecl->set_init( init );
112                                ctorInit->set_init( NULL );
113                        } else {
114                                // no constructor and no initializer, which is okay
115                                objDecl->set_init( NULL );
116                        }
117                        delete ctorInit;
118                }
119                return objDecl;
120        }
121
122        CompoundStmt * FixInit::mutate( CompoundStmt * compoundStmt ) {
123                // mutate statements - this will also populate dtorStmts list
124                // don't want to dump all destructors when block is left,
125                // just the destructors associated with variables defined in this block
126                std::list< Statement * > oldDestructorStmts = dtorStmts;
127                dtorStmts = std::list<Statement *>();
128
129                compoundStmt = PolyMutator::mutate( compoundStmt );
130                std::list< Statement * > & statements = compoundStmt->get_kids();
131                for ( std::list< Statement * >::iterator it = dtorStmts.begin(); it != dtorStmts.end(); ++it ) {
132                        // remove if instrinsic destructor statement
133                        // xxx - test user manually calling intrinsic functions - what happens?
134                        if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( *it ) ) {
135                                ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( exprStmt->get_expr() );
136                                assert( appExpr );
137                                VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() );
138                                assert( function );
139                                // check for Intrinsic only - don't want to remove all overridable dtors because autogenerated dtor
140                                // will call all member dtors, and some members may have a user defined dtor.
141                                if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
142                                        // don't ned to call intrinsic dtor, because it does nothing
143                                        delete *it;
144                                } else {
145                                        // non-intrinsic dtors must be called
146                                        statements.push_back( *it );
147                                }
148                        } else {
149                                // could also be a compound statement with a loop, in the case of an array
150                                statements.push_back( *it );
151                        }
152                }
153                // TODO: adding to the end of a block isn't sufficient, since
154                // return/break/goto should trigger destructor when block is left.
155                dtorStmts = oldDestructorStmts;
156                return compoundStmt;
157        }
158
159} // namespace InitTweak
160
161// Local Variables: //
162// tab-width: 4 //
163// mode: c++ //
164// compile-command: "make install" //
165// End: //
Note: See TracBrowser for help on using the repository browser.