source: src/InitTweak/FixGlobalInit.cc @ 5f98ce5

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

hoist non-constexpr array dimension into const variable in case of side effects

  • Property mode set to 100644
File size: 8.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// FixGlobalInit.cc --
8//
9// Author           : Rob Schluntz
10// Created On       : Mon May 04 15:14:56 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Jun 29 22:33:15 2016
13// Update Count     : 4
14//
15
16#include "FixGlobalInit.h"
17#include "InitTweak.h"
18#include "SynTree/Declaration.h"
19#include "SynTree/Type.h"
20#include "SynTree/Expression.h"
21#include "SynTree/Statement.h"
22#include "SynTree/Initializer.h"
23#include "SynTree/Visitor.h"
24#include "SynTree/Attribute.h"
25#include <algorithm>
26
27namespace InitTweak {
28        namespace {
29                const std::list<Label> noLabels;
30        }
31
32        class GlobalFixer : public Visitor {
33          public:
34                GlobalFixer( const std::string & name, bool inLibrary );
35
36                virtual void visit( ObjectDecl *objDecl );
37                virtual void visit( FunctionDecl *functionDecl );
38                virtual void visit( StructDecl *aggregateDecl );
39                virtual void visit( UnionDecl *aggregateDecl );
40                virtual void visit( EnumDecl *aggregateDecl );
41                virtual void visit( TraitDecl *aggregateDecl );
42                virtual void visit( TypeDecl *typeDecl );
43
44                UniqueName tempNamer;
45                FunctionDecl * initFunction;
46                FunctionDecl * destroyFunction;
47        };
48
49        class ConstExprChecker : public Visitor {
50        public:
51                ConstExprChecker() : isConstExpr( true ) {}
52
53                virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; }
54                virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; }
55                virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; }
56                virtual void visit( CastExpr *castExpr ) { isConstExpr = false; }
57                virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; }
58                virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; }
59                virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; }
60                virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; }
61                virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ }
62                // these might be okay?
63                // virtual void visit( SizeofExpr *sizeofExpr );
64                // virtual void visit( AlignofExpr *alignofExpr );
65                // virtual void visit( UntypedOffsetofExpr *offsetofExpr );
66                // virtual void visit( OffsetofExpr *offsetofExpr );
67                // virtual void visit( OffsetPackExpr *offsetPackExpr );
68                // virtual void visit( AttrExpr *attrExpr );
69                // virtual void visit( CommaExpr *commaExpr );
70                // virtual void visit( LogicalExpr *logicalExpr );
71                // virtual void visit( ConditionalExpr *conditionalExpr );
72                virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; }
73                virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; }
74                virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; }
75                virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; }
76                virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; }
77                virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; }
78
79                bool isConstExpr;
80        };
81
82        bool isConstExpr( Expression * expr ) {
83                if ( expr ) {
84                        ConstExprChecker checker;
85                        expr->accept( checker );
86                        return checker.isConstExpr;
87                }
88                return true;
89        }
90
91        bool isConstExpr( Initializer * init ) {
92                if ( init ) {
93                        ConstExprChecker checker;
94                        init->accept( checker );
95                        return checker.isConstExpr;
96                } // if
97                // for all intents and purposes, no initializer means const expr
98                return true;
99        }
100
101        void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
102                GlobalFixer fixer( name, inLibrary );
103                acceptAll( translationUnit, fixer );
104                // don't need to include function if it's empty
105                if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
106                        delete fixer.initFunction;
107                } else {
108                        translationUnit.push_back( fixer.initFunction );
109                } // if
110
111                if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
112                        delete fixer.destroyFunction;
113                } else {
114                        translationUnit.push_back( fixer.destroyFunction );
115                } // if
116        }
117
118  std::string globalFunctionName( const std::string & name ) {
119        // get basename
120        std::string ret = name.substr( 0, name.find( '.' ) );
121        // replace invalid characters with _
122                static std::string invalid = "/-";
123        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
124        return ret;
125  }
126
127        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
128                std::string fixedName = globalFunctionName( name );
129                std::list< Expression * > ctorParameters;
130                std::list< Expression * > dtorParameters;
131                if ( inLibrary ) {
132                        // Constructor/destructor attributes take a single parameter which
133                        // is the priority, with lower numbers meaning higher priority.
134                        // Functions specified with priority are guaranteed to run before
135                        // functions without a priority. To ensure that constructors and destructors
136                        // for library code are run before constructors and destructors for user code,
137                        // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
138                        ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
139                        dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
140                }
141                initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
142                initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
143                destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
144                destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
145        }
146
147        void GlobalFixer::visit( ObjectDecl *objDecl ) {
148                std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
149                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
150
151                if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
152                if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return;
153                // C allows you to initialize objects with constant expressions
154                // xxx - this is an optimization. Need to first resolve constructors before we decide
155                // to keep C-style initializer.
156                // if ( isConstExpr( objDecl->get_init() ) ) return;
157
158                if ( dynamic_cast< ArrayType * > ( objDecl->get_type() ) ) {
159                        // xxx - initialize each element of the array
160                } else {
161                        // steal initializer from object and attach it to a new temporary
162                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
163                        objDecl->set_init( NULL );
164                        initStatements.push_back( new DeclStmt( noLabels, newObj ) );
165
166                        // copy construct objDecl using temporary
167                        UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
168                        init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
169                        init->get_args().push_back( new VariableExpr( newObj ) );
170                        initStatements.push_back( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, init ) ) );
171
172                        // add destructor calls to global destroy function
173                        UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
174                        destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
175                        destroyStatements.push_front( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, destroy ) ) );
176                } // if
177        }
178
179        // only modify global variables
180        void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
181        void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
182        void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
183        void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
184        void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
185        void GlobalFixer::visit( TypeDecl *typeDecl ) {}
186
187} // namespace InitTweak
188
189// Local Variables: //
190// tab-width: 4 //
191// mode: c++ //
192// compile-command: "make install" //
193// End: //
194
Note: See TracBrowser for help on using the repository browser.