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 : Fri Dec 13 23:41:10 2019
|
---|
13 | // Update Count : 19
|
---|
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 "SynTree/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 | #include "AST/Expr.hpp"
|
---|
37 | #include "AST/Node.hpp"
|
---|
38 | #include "AST/Pass.hpp"
|
---|
39 |
|
---|
40 | namespace InitTweak {
|
---|
41 | class GlobalFixer : public WithShortCircuiting {
|
---|
42 | public:
|
---|
43 | GlobalFixer( bool inLibrary );
|
---|
44 |
|
---|
45 | void previsit( ObjectDecl *objDecl );
|
---|
46 | void previsit( FunctionDecl *functionDecl );
|
---|
47 | void previsit( StructDecl *aggregateDecl );
|
---|
48 | void previsit( UnionDecl *aggregateDecl );
|
---|
49 | void previsit( EnumDecl *aggregateDecl );
|
---|
50 | void previsit( TraitDecl *aggregateDecl );
|
---|
51 | void previsit( TypeDecl *typeDecl );
|
---|
52 |
|
---|
53 | UniqueName tempNamer;
|
---|
54 | FunctionDecl * initFunction;
|
---|
55 | FunctionDecl * destroyFunction;
|
---|
56 | };
|
---|
57 |
|
---|
58 | class GlobalFixer_new : public ast::WithShortCircuiting {
|
---|
59 | public:
|
---|
60 | void previsit (const ast::ObjectDecl *);
|
---|
61 | void previsit (const ast::FunctionDecl *) { visit_children = false; }
|
---|
62 | void previsit (const ast::StructDecl *) { visit_children = false; }
|
---|
63 | void previsit (const ast::UnionDecl *) { visit_children = false; }
|
---|
64 | void previsit (const ast::EnumDecl *) { visit_children = false; }
|
---|
65 | void previsit (const ast::TraitDecl *) { visit_children = false; }
|
---|
66 | void previsit (const ast::TypeDecl *) { visit_children = false; }
|
---|
67 |
|
---|
68 | std::list< ast::ptr<ast::Stmt> > initStmts;
|
---|
69 | std::list< ast::ptr<ast::Stmt> > destroyStmts;
|
---|
70 | };
|
---|
71 |
|
---|
72 | void fixGlobalInit( std::list< Declaration * > & translationUnit, bool inLibrary ) {
|
---|
73 | PassVisitor<GlobalFixer> visitor( inLibrary );
|
---|
74 | acceptAll( translationUnit, visitor );
|
---|
75 | GlobalFixer & fixer = visitor.pass;
|
---|
76 | // don't need to include function if it's empty
|
---|
77 | if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
|
---|
78 | delete fixer.initFunction;
|
---|
79 | } else {
|
---|
80 | translationUnit.push_back( fixer.initFunction );
|
---|
81 | } // if
|
---|
82 |
|
---|
83 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
|
---|
84 | delete fixer.destroyFunction;
|
---|
85 | } else {
|
---|
86 | translationUnit.push_back( fixer.destroyFunction );
|
---|
87 | } // if
|
---|
88 | }
|
---|
89 |
|
---|
90 | GlobalFixer::GlobalFixer( bool inLibrary ) : tempNamer( "_global_init" ) {
|
---|
91 | std::list< Expression * > ctorParameters;
|
---|
92 | std::list< Expression * > dtorParameters;
|
---|
93 | if ( inLibrary ) {
|
---|
94 | // Constructor/destructor attributes take a single parameter which
|
---|
95 | // is the priority, with lower numbers meaning higher priority.
|
---|
96 | // Functions specified with priority are guaranteed to run before
|
---|
97 | // functions without a priority. To ensure that constructors and destructors
|
---|
98 | // for library code are run before constructors and destructors for user code,
|
---|
99 | // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
|
---|
100 | // Priorities 101-200 are reserved by cfa, so use priority 200 for CFA library globals,
|
---|
101 | // allowing room for overriding with a higher priority.
|
---|
102 | ctorParameters.push_back( new ConstantExpr( Constant::from_int( 200 ) ) );
|
---|
103 | dtorParameters.push_back( new ConstantExpr( Constant::from_int( 200 ) ) );
|
---|
104 | }
|
---|
105 | initFunction = new FunctionDecl( "__global_init__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
|
---|
106 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
|
---|
107 | destroyFunction = new FunctionDecl( "__global_destroy__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
|
---|
108 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
|
---|
109 | }
|
---|
110 |
|
---|
111 | void fixGlobalInit(ast::TranslationUnit & translationUnit, bool inLibrary) {
|
---|
112 | ast::Pass<GlobalFixer_new> fixer;
|
---|
113 | accept_all(translationUnit, fixer);
|
---|
114 |
|
---|
115 | if ( !fixer.core.initStmts.empty() ) {
|
---|
116 | std::vector<ast::ptr<ast::Expr>> ctorParams;
|
---|
117 | if (inLibrary) ctorParams.emplace_back(ast::ConstantExpr::from_int({}, 200));
|
---|
118 | auto initFunction = new ast::FunctionDecl({}, "__global_init__", {}, {}, {}, new ast::CompoundStmt({}, std::move(fixer.core.initStmts)),
|
---|
119 | ast::Storage::Static, ast::Linkage::C, {new ast::Attribute("constructor", std::move(ctorParams))});
|
---|
120 |
|
---|
121 | translationUnit.decls.emplace_back( initFunction );
|
---|
122 | } // if
|
---|
123 |
|
---|
124 | if ( !fixer.core.destroyStmts.empty() ) {
|
---|
125 | std::vector<ast::ptr<ast::Expr>> dtorParams;
|
---|
126 | if (inLibrary) dtorParams.emplace_back(ast::ConstantExpr::from_int({}, 200));
|
---|
127 | auto destroyFunction = new ast::FunctionDecl({}, "__global_destroy__", {}, {}, {}, new ast::CompoundStmt({}, std::move(fixer.core.destroyStmts)),
|
---|
128 | ast::Storage::Static, ast::Linkage::C, {new ast::Attribute("destructor", std::move(dtorParams))});
|
---|
129 |
|
---|
130 | translationUnit.decls.emplace_back(destroyFunction);
|
---|
131 | } // if
|
---|
132 | }
|
---|
133 |
|
---|
134 | void GlobalFixer::previsit( ObjectDecl *objDecl ) {
|
---|
135 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
|
---|
136 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
|
---|
137 |
|
---|
138 | // C allows you to initialize objects with constant expressions
|
---|
139 | // xxx - this is an optimization. Need to first resolve constructors before we decide
|
---|
140 | // to keep C-style initializer.
|
---|
141 | // if ( isConstExpr( objDecl->get_init() ) ) return;
|
---|
142 |
|
---|
143 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
|
---|
144 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL
|
---|
145 | assert( ! ctorInit->ctor || ! ctorInit->init );
|
---|
146 |
|
---|
147 | Statement * dtor = ctorInit->dtor;
|
---|
148 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
|
---|
149 | // don't need to call intrinsic dtor, because it does nothing, but
|
---|
150 | // non-intrinsic dtors must be called
|
---|
151 | destroyStatements.push_front( dtor );
|
---|
152 | ctorInit->dtor = nullptr;
|
---|
153 | } // if
|
---|
154 | if ( Statement * ctor = ctorInit->ctor ) {
|
---|
155 | addDataSectonAttribute( objDecl );
|
---|
156 | initStatements.push_back( ctor );
|
---|
157 | objDecl->init = nullptr;
|
---|
158 | ctorInit->ctor = nullptr;
|
---|
159 | } else if ( Initializer * init = ctorInit->init ) {
|
---|
160 | objDecl->init = init;
|
---|
161 | ctorInit->init = nullptr;
|
---|
162 | } else {
|
---|
163 | // no constructor and no initializer, which is okay
|
---|
164 | objDecl->init = nullptr;
|
---|
165 | } // if
|
---|
166 | delete ctorInit;
|
---|
167 | } // if
|
---|
168 | }
|
---|
169 |
|
---|
170 | void GlobalFixer_new::previsit(const ast::ObjectDecl * objDecl) {
|
---|
171 | auto mutDecl = mutate(objDecl);
|
---|
172 | assertf(mutDecl == objDecl, "Global object decl must be unique");
|
---|
173 | if ( auto ctorInit = objDecl->init.as<ast::ConstructorInit>() ) {
|
---|
174 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL
|
---|
175 | assert( ! ctorInit->ctor || ! ctorInit->init );
|
---|
176 |
|
---|
177 | const ast::Stmt * dtor = ctorInit->dtor;
|
---|
178 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
|
---|
179 | // don't need to call intrinsic dtor, because it does nothing, but
|
---|
180 | // non-intrinsic dtors must be called
|
---|
181 | destroyStmts.push_front( dtor );
|
---|
182 | // ctorInit->dtor = nullptr;
|
---|
183 | } // if
|
---|
184 | if ( const ast::Stmt * ctor = ctorInit->ctor ) {
|
---|
185 | addDataSectionAttribute(mutDecl);
|
---|
186 | initStmts.push_back( ctor );
|
---|
187 | mutDecl->init = nullptr;
|
---|
188 | // ctorInit->ctor = nullptr;
|
---|
189 | } else if ( const ast::Init * init = ctorInit->init ) {
|
---|
190 | mutDecl->init = init;
|
---|
191 | // ctorInit->init = nullptr;
|
---|
192 | } else {
|
---|
193 | // no constructor and no initializer, which is okay
|
---|
194 | mutDecl->init = nullptr;
|
---|
195 | } // if
|
---|
196 | // delete ctorInit;
|
---|
197 | } // if
|
---|
198 | }
|
---|
199 |
|
---|
200 | // only modify global variables
|
---|
201 | void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; }
|
---|
202 | void GlobalFixer::previsit( StructDecl * ) { visit_children = false; }
|
---|
203 | void GlobalFixer::previsit( UnionDecl * ) { visit_children = false; }
|
---|
204 | void GlobalFixer::previsit( EnumDecl * ) { visit_children = false; }
|
---|
205 | void GlobalFixer::previsit( TraitDecl * ) { visit_children = false; }
|
---|
206 | void GlobalFixer::previsit( TypeDecl * ) { visit_children = false; }
|
---|
207 |
|
---|
208 | } // namespace InitTweak
|
---|
209 |
|
---|
210 | // Local Variables: //
|
---|
211 | // tab-width: 4 //
|
---|
212 | // mode: c++ //
|
---|
213 | // compile-command: "make install" //
|
---|
214 | // End: //
|
---|