source: src/InitTweak/FixGlobalInit.cc @ 64a32c6

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 64a32c6 was 7baed7d, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

generalize notion of a GCC attribute

  • Property mode set to 100644
File size: 7.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// FixGlobalInit.cc --
8//
9// Author           : Rob Schluntz
10// Created On       : Mon May 04 15:14:56 2016
11// Last Modified By : Rob Schluntz
12// Last Modified On : Fri May 13 11:37:30 2016
13// Update Count     : 2
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( Initializer * init ) {
83                if ( init ) {
84                        ConstExprChecker checker;
85                        init->accept( checker );
86                        return checker.isConstExpr;
87                }
88                // for all intents and purposes, no initializer means const expr
89                return true;
90        }
91
92        void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
93                GlobalFixer fixer( name, inLibrary );
94                acceptAll( translationUnit, fixer );
95                // don't need to include function if it's empty
96                if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
97                        delete fixer.initFunction;
98                } else {
99                        translationUnit.push_back( fixer.initFunction );
100                }
101                if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
102                        delete fixer.destroyFunction;
103                } else {
104                        translationUnit.push_back( fixer.destroyFunction );
105                }
106        }
107
108  std::string globalFunctionName( const std::string & name ) {
109        // get basename
110        std::string ret = name.substr( 0, name.find( '.' ) );
111        // replace invalid characters with _
112                static std::string invalid = "/-";
113        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
114        return ret;
115  }
116
117        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
118                std::string fixedName = globalFunctionName( name );
119                std::list< Expression * > ctorParameters;
120                std::list< Expression * > dtorParameters;
121                if ( inLibrary ) {
122                        // Constructor/destructor attributes take a single parameter which
123                        // is the priority, with lower numbers meaning higher priority.
124                        // Functions specified with priority are guaranteed to run before
125                        // functions without a priority. To ensure that constructors and destructors
126                        // for library code are run before constructors and destructors for user code,
127                        // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
128                        ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
129                        dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
130                }
131                initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
132                initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
133                destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
134                destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
135        }
136
137        void GlobalFixer::visit( ObjectDecl *objDecl ) {
138                std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
139                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
140
141                if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
142                if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return;
143                // C allows you to initialize objects with constant expressions
144                // xxx - this is an optimization. Need to first resolve constructors before we decide
145                // to keep C-style initializer.
146                // if ( isConstExpr( objDecl->get_init() ) ) return;
147
148                if ( ArrayType * at = dynamic_cast< ArrayType * > ( objDecl->get_type() ) ) {
149                        // xxx - initialize each element of the array
150                } else {
151                        // steal initializer from object and attach it to a new temporary
152                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
153                        objDecl->set_init( NULL );
154                        initStatements.push_back( new DeclStmt( noLabels, newObj ) );
155
156                        // copy construct objDecl using temporary
157                        UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
158                        init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
159                        init->get_args().push_back( new VariableExpr( newObj ) );
160                        initStatements.push_back( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, init ) ) );
161
162                        // add destructor calls to global destroy function
163                        UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
164                        destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
165                        destroyStatements.push_front( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, destroy ) ) );
166                }
167        }
168
169        // only modify global variables
170        void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
171        void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
172        void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
173        void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
174        void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
175        void GlobalFixer::visit( TypeDecl *typeDecl ) {}
176
177} // namespace InitTweak
178
179// Local Variables: //
180// tab-width: 4 //
181// mode: c++ //
182// compile-command: "make install" //
183// End: //
184
Note: See TracBrowser for help on using the repository browser.