source: src/InitTweak/FixGlobalInit.cc @ cbce272

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since cbce272 was 10e90cb, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

  • Property mode set to 100644
File size: 5.6 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 : Thu Mar 16 07:53:11 2017
13// Update Count     : 18
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        class GlobalFixer : public Visitor {
29          public:
30                GlobalFixer( const std::string & name, bool inLibrary );
31
32                virtual void visit( ObjectDecl *objDecl );
33                virtual void visit( FunctionDecl *functionDecl );
34                virtual void visit( StructDecl *aggregateDecl );
35                virtual void visit( UnionDecl *aggregateDecl );
36                virtual void visit( EnumDecl *aggregateDecl );
37                virtual void visit( TraitDecl *aggregateDecl );
38                virtual void visit( TypeDecl *typeDecl );
39
40                UniqueName tempNamer;
41                FunctionDecl * initFunction;
42                FunctionDecl * destroyFunction;
43        };
44
45        void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
46                GlobalFixer fixer( name, inLibrary );
47                acceptAll( translationUnit, fixer );
48                // don't need to include function if it's empty
49                if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
50                        delete fixer.initFunction;
51                } else {
52                        translationUnit.push_back( fixer.initFunction );
53                } // if
54
55                if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
56                        delete fixer.destroyFunction;
57                } else {
58                        translationUnit.push_back( fixer.destroyFunction );
59                } // if
60        }
61
62  std::string globalFunctionName( const std::string & name ) {
63        // get basename
64        std::string ret = name.substr( 0, name.find( '.' ) );
65        // replace invalid characters with _
66                static std::string invalid = "/-";
67        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
68        return ret;
69  }
70
71        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
72                std::string fixedName = globalFunctionName( name );
73                std::list< Expression * > ctorParameters;
74                std::list< Expression * > dtorParameters;
75                if ( inLibrary ) {
76                        // Constructor/destructor attributes take a single parameter which
77                        // is the priority, with lower numbers meaning higher priority.
78                        // Functions specified with priority are guaranteed to run before
79                        // functions without a priority. To ensure that constructors and destructors
80                        // for library code are run before constructors and destructors for user code,
81                        // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
82                        ctorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
83                        dtorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
84                }
85                initFunction = new FunctionDecl( "_init_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ) );
86                initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
87                destroyFunction = new FunctionDecl( "_destroy_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ) );
88                destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
89        }
90
91        void GlobalFixer::visit( ObjectDecl *objDecl ) {
92                std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
93                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
94
95                // C allows you to initialize objects with constant expressions
96                // xxx - this is an optimization. Need to first resolve constructors before we decide
97                // to keep C-style initializer.
98                // if ( isConstExpr( objDecl->get_init() ) ) return;
99
100                if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
101                        // a decision should have been made by the resolver, so ctor and init are not both non-NULL
102                        assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
103
104                        Statement * dtor = ctorInit->get_dtor();
105                        if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
106                                // don't need to call intrinsic dtor, because it does nothing, but
107                                // non-intrinsic dtors must be called
108                                destroyStatements.push_front( dtor );
109                                ctorInit->set_dtor( NULL );
110                        } // if
111                        if ( Statement * ctor = ctorInit->get_ctor() ) {
112                                initStatements.push_back( ctor );
113                                objDecl->set_init( NULL );
114                                ctorInit->set_ctor( NULL );
115                        } else if ( Initializer * init = ctorInit->get_init() ) {
116                                objDecl->set_init( init );
117                                ctorInit->set_init( NULL );
118                        } else {
119                                // no constructor and no initializer, which is okay
120                                objDecl->set_init( NULL );
121                        } // if
122                        delete ctorInit;
123                } // if
124        }
125
126        // only modify global variables
127        void GlobalFixer::visit( __attribute__((unused)) FunctionDecl *functionDecl ) {}
128        void GlobalFixer::visit( __attribute__((unused)) StructDecl *aggregateDecl ) {}
129        void GlobalFixer::visit( __attribute__((unused)) UnionDecl *aggregateDecl ) {}
130        void GlobalFixer::visit( __attribute__((unused)) EnumDecl *aggregateDecl ) {}
131        void GlobalFixer::visit( __attribute__((unused)) TraitDecl *aggregateDecl ) {}
132        void GlobalFixer::visit( __attribute__((unused)) TypeDecl *typeDecl ) {}
133
134} // namespace InitTweak
135
136// Local Variables: //
137// tab-width: 4 //
138// mode: c++ //
139// compile-command: "make install" //
140// End: //
Note: See TracBrowser for help on using the repository browser.