source: src/InitTweak/FixGlobalInit.cc@ 34ed17b

ADT ast-experimental
Last change on this file since 34ed17b was 720f2fe2, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Changed 'addDataSectionAttribute' to correctly handle tls.
Fixed Typo.

  • Property mode set to 100644
File size: 9.0 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 : 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
40namespace 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 // Say these magic declarations come at the end of the file.
116 CodeLocation const & location = translationUnit.decls.back()->location;
117
118 if ( !fixer.core.initStmts.empty() ) {
119 std::vector<ast::ptr<ast::Expr>> ctorParams;
120 if (inLibrary) ctorParams.emplace_back(ast::ConstantExpr::from_int(location, 200));
121 auto initFunction = new ast::FunctionDecl(location,
122 "__global_init__", {}, {}, {},
123 new ast::CompoundStmt(location, std::move(fixer.core.initStmts)),
124 ast::Storage::Static, ast::Linkage::C,
125 {new ast::Attribute("constructor", std::move(ctorParams))});
126
127 translationUnit.decls.emplace_back( initFunction );
128 } // if
129
130 if ( !fixer.core.destroyStmts.empty() ) {
131 std::vector<ast::ptr<ast::Expr>> dtorParams;
132 if (inLibrary) dtorParams.emplace_back(ast::ConstantExpr::from_int(location, 200));
133 auto destroyFunction = new ast::FunctionDecl( location,
134 "__global_destroy__", {}, {}, {},
135 new ast::CompoundStmt(location, std::move(fixer.core.destroyStmts)),
136 ast::Storage::Static, ast::Linkage::C,
137 {new ast::Attribute("destructor", std::move(dtorParams))});
138
139 translationUnit.decls.emplace_back(destroyFunction);
140 } // if
141 }
142
143 void GlobalFixer::previsit( ObjectDecl *objDecl ) {
144 std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
145 std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
146
147 // C allows you to initialize objects with constant expressions
148 // xxx - this is an optimization. Need to first resolve constructors before we decide
149 // to keep C-style initializer.
150 // if ( isConstExpr( objDecl->get_init() ) ) return;
151
152 if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
153 // a decision should have been made by the resolver, so ctor and init are not both non-NULL
154 assert( ! ctorInit->ctor || ! ctorInit->init );
155
156 Statement * dtor = ctorInit->dtor;
157 if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
158 // don't need to call intrinsic dtor, because it does nothing, but
159 // non-intrinsic dtors must be called
160 destroyStatements.push_front( dtor );
161 ctorInit->dtor = nullptr;
162 } // if
163 if ( Statement * ctor = ctorInit->ctor ) {
164 addDataSectionAttribute( objDecl );
165 initStatements.push_back( ctor );
166 objDecl->init = nullptr;
167 ctorInit->ctor = nullptr;
168 } else if ( Initializer * init = ctorInit->init ) {
169 objDecl->init = init;
170 ctorInit->init = nullptr;
171 } else {
172 // no constructor and no initializer, which is okay
173 objDecl->init = nullptr;
174 } // if
175 delete ctorInit;
176 } // if
177 }
178
179 void GlobalFixer_new::previsit(const ast::ObjectDecl * objDecl) {
180 auto mutDecl = mutate(objDecl);
181 assertf(mutDecl == objDecl, "Global object decl must be unique");
182 if ( auto ctorInit = objDecl->init.as<ast::ConstructorInit>() ) {
183 // a decision should have been made by the resolver, so ctor and init are not both non-NULL
184 assert( ! ctorInit->ctor || ! ctorInit->init );
185
186 const ast::Stmt * dtor = ctorInit->dtor;
187 if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
188 // don't need to call intrinsic dtor, because it does nothing, but
189 // non-intrinsic dtors must be called
190 destroyStmts.push_front( dtor );
191 // ctorInit->dtor = nullptr;
192 } // if
193 if ( const ast::Stmt * ctor = ctorInit->ctor ) {
194 addDataSectionAttribute(mutDecl);
195 initStmts.push_back( ctor );
196 mutDecl->init = nullptr;
197 // ctorInit->ctor = nullptr;
198 } else if ( const ast::Init * init = ctorInit->init ) {
199 mutDecl->init = init;
200 // ctorInit->init = nullptr;
201 } else {
202 // no constructor and no initializer, which is okay
203 mutDecl->init = nullptr;
204 } // if
205 // delete ctorInit;
206 } // if
207 }
208
209 // only modify global variables
210 void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; }
211 void GlobalFixer::previsit( StructDecl * ) { visit_children = false; }
212 void GlobalFixer::previsit( UnionDecl * ) { visit_children = false; }
213 void GlobalFixer::previsit( EnumDecl * ) { visit_children = false; }
214 void GlobalFixer::previsit( TraitDecl * ) { visit_children = false; }
215 void GlobalFixer::previsit( TypeDecl * ) { visit_children = false; }
216
217} // namespace InitTweak
218
219// Local Variables: //
220// tab-width: 4 //
221// mode: c++ //
222// compile-command: "make install" //
223// End: //
Note: See TracBrowser for help on using the repository browser.