source: src/InitTweak/FixGlobalInit.cc @ b81adcc4

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 b81adcc4 was 40e636a, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

only generate array dimension constant inside of functions, recursively mutate array types into pointer types in function parameter lists, fix bug where global compound literal object is lost

  • 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 : 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        void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
50                GlobalFixer fixer( name, inLibrary );
51                acceptAll( translationUnit, fixer );
52                // don't need to include function if it's empty
53                if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
54                        delete fixer.initFunction;
55                } else {
56                        translationUnit.push_back( fixer.initFunction );
57                } // if
58
59                if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
60                        delete fixer.destroyFunction;
61                } else {
62                        translationUnit.push_back( fixer.destroyFunction );
63                } // if
64        }
65
66  std::string globalFunctionName( const std::string & name ) {
67        // get basename
68        std::string ret = name.substr( 0, name.find( '.' ) );
69        // replace invalid characters with _
70                static std::string invalid = "/-";
71        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
72        return ret;
73  }
74
75        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
76                std::string fixedName = globalFunctionName( name );
77                std::list< Expression * > ctorParameters;
78                std::list< Expression * > dtorParameters;
79                if ( inLibrary ) {
80                        // Constructor/destructor attributes take a single parameter which
81                        // is the priority, with lower numbers meaning higher priority.
82                        // Functions specified with priority are guaranteed to run before
83                        // functions without a priority. To ensure that constructors and destructors
84                        // for library code are run before constructors and destructors for user code,
85                        // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
86                        ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
87                        dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
88                }
89                initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
90                initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
91                destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
92                destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
93        }
94
95        void GlobalFixer::visit( ObjectDecl *objDecl ) {
96                std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
97                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
98
99                if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
100                if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return;
101                // C allows you to initialize objects with constant expressions
102                // xxx - this is an optimization. Need to first resolve constructors before we decide
103                // to keep C-style initializer.
104                // if ( isConstExpr( objDecl->get_init() ) ) return;
105
106                if ( dynamic_cast< ArrayType * > ( objDecl->get_type() ) ) {
107                        // xxx - initialize each element of the array
108                } else {
109                        // insert constructor for objDecl into global init function
110                        UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
111                        init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
112                        init->get_args().splice( init->get_args().end(), makeInitList( objDecl->get_init() ) );
113                        objDecl->set_init( NULL );
114                        initStatements.push_back( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, init ) ) );
115
116                        // add destructor calls to global destroy function
117                        UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
118                        destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
119                        destroyStatements.push_front( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, destroy ) ) );
120                } // if
121        }
122
123        // only modify global variables
124        void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
125        void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
126        void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
127        void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
128        void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
129        void GlobalFixer::visit( TypeDecl *typeDecl ) {}
130
131} // namespace InitTweak
132
133// Local Variables: //
134// tab-width: 4 //
135// mode: c++ //
136// compile-command: "make install" //
137// End: //
138
Note: See TracBrowser for help on using the repository browser.