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