[711eee5] | 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
|
---|
[ca35c51] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[07de76b] | 12 | // Last Modified On : Fri Dec 13 23:41:10 2019
|
---|
| 13 | // Update Count : 19
|
---|
[711eee5] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "FixGlobalInit.h"
|
---|
[d180746] | 17 |
|
---|
[ea6332d] | 18 | #include <cassert> // for assert
|
---|
[d180746] | 19 | #include <stddef.h> // for NULL
|
---|
| 20 | #include <algorithm> // for replace_if
|
---|
| 21 |
|
---|
[1cb934d] | 22 | #include "Common/PassVisitor.h"
|
---|
[d180746] | 23 | #include "Common/UniqueName.h" // for UniqueName
|
---|
| 24 | #include "InitTweak.h" // for isIntrinsicSingleArgCallStmt
|
---|
[07de76b] | 25 | #include "SynTree/LinkageSpec.h" // for C
|
---|
[d180746] | 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
|
---|
[ba3706f] | 31 | #include "SynTree/Label.h" // for Label
|
---|
[d180746] | 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
|
---|
[711eee5] | 35 |
|
---|
[490fb92e] | 36 | #include "AST/Expr.hpp"
|
---|
| 37 | #include "AST/Node.hpp"
|
---|
| 38 | #include "AST/Pass.hpp"
|
---|
| 39 |
|
---|
[711eee5] | 40 | namespace InitTweak {
|
---|
[1cb934d] | 41 | class GlobalFixer : public WithShortCircuiting {
|
---|
[711eee5] | 42 | public:
|
---|
[1be845b] | 43 | GlobalFixer( bool inLibrary );
|
---|
[711eee5] | 44 |
|
---|
[1cb934d] | 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 );
|
---|
[711eee5] | 52 |
|
---|
| 53 | UniqueName tempNamer;
|
---|
[37024fd] | 54 | FunctionDecl * initFunction;
|
---|
[9e2c1f0] | 55 | FunctionDecl * destroyFunction;
|
---|
[711eee5] | 56 | };
|
---|
| 57 |
|
---|
[490fb92e] | 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 |
|
---|
[1be845b] | 72 | void fixGlobalInit( std::list< Declaration * > & translationUnit, bool inLibrary ) {
|
---|
| 73 | PassVisitor<GlobalFixer> visitor( inLibrary );
|
---|
[1cb934d] | 74 | acceptAll( translationUnit, visitor );
|
---|
| 75 | GlobalFixer & fixer = visitor.pass;
|
---|
[ec79847] | 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 );
|
---|
[ca35c51] | 81 | } // if
|
---|
| 82 |
|
---|
[ec79847] | 83 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
|
---|
| 84 | delete fixer.destroyFunction;
|
---|
| 85 | } else {
|
---|
| 86 | translationUnit.push_back( fixer.destroyFunction );
|
---|
[ca35c51] | 87 | } // if
|
---|
[711eee5] | 88 | }
|
---|
| 89 |
|
---|
[1be845b] | 90 | GlobalFixer::GlobalFixer( bool inLibrary ) : tempNamer( "_global_init" ) {
|
---|
[7baed7d] | 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.
|
---|
[1be845b] | 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 ) ) );
|
---|
[7baed7d] | 104 | }
|
---|
[1be845b] | 105 | initFunction = new FunctionDecl( "__global_init__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
|
---|
[7baed7d] | 106 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
|
---|
[1be845b] | 107 | destroyFunction = new FunctionDecl( "__global_destroy__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
|
---|
[7baed7d] | 108 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
|
---|
[711eee5] | 109 | }
|
---|
| 110 |
|
---|
[293dc1c] | 111 | void fixGlobalInit(ast::TranslationUnit & translationUnit, bool inLibrary) {
|
---|
[490fb92e] | 112 | ast::Pass<GlobalFixer_new> fixer;
|
---|
| 113 | accept_all(translationUnit, fixer);
|
---|
| 114 |
|
---|
[33b7d49] | 115 | // Say these magic declarations come at the end of the file.
|
---|
| 116 | CodeLocation const & location = translationUnit.decls.back()->location;
|
---|
| 117 |
|
---|
[490fb92e] | 118 | if ( !fixer.core.initStmts.empty() ) {
|
---|
| 119 | std::vector<ast::ptr<ast::Expr>> ctorParams;
|
---|
[33b7d49] | 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))});
|
---|
[490fb92e] | 126 |
|
---|
[293dc1c] | 127 | translationUnit.decls.emplace_back( initFunction );
|
---|
[490fb92e] | 128 | } // if
|
---|
| 129 |
|
---|
| 130 | if ( !fixer.core.destroyStmts.empty() ) {
|
---|
| 131 | std::vector<ast::ptr<ast::Expr>> dtorParams;
|
---|
[33b7d49] | 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))});
|
---|
[490fb92e] | 138 |
|
---|
[293dc1c] | 139 | translationUnit.decls.emplace_back(destroyFunction);
|
---|
[490fb92e] | 140 | } // if
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[1cb934d] | 143 | void GlobalFixer::previsit( ObjectDecl *objDecl ) {
|
---|
[9e2c1f0] | 144 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
|
---|
| 145 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
|
---|
[711eee5] | 146 |
|
---|
| 147 | // C allows you to initialize objects with constant expressions
|
---|
[4e24610] | 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;
|
---|
[711eee5] | 151 |
|
---|
[6cf27a07] | 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
|
---|
[f072892] | 154 | assert( ! ctorInit->ctor || ! ctorInit->init );
|
---|
[6cf27a07] | 155 |
|
---|
[f072892] | 156 | Statement * dtor = ctorInit->dtor;
|
---|
[f9cebb5] | 157 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
|
---|
[6cf27a07] | 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 );
|
---|
[f072892] | 161 | ctorInit->dtor = nullptr;
|
---|
[6cf27a07] | 162 | } // if
|
---|
[f072892] | 163 | if ( Statement * ctor = ctorInit->ctor ) {
|
---|
[720f2fe2] | 164 | addDataSectionAttribute( objDecl );
|
---|
[6cf27a07] | 165 | initStatements.push_back( ctor );
|
---|
[f072892] | 166 | objDecl->init = nullptr;
|
---|
| 167 | ctorInit->ctor = nullptr;
|
---|
| 168 | } else if ( Initializer * init = ctorInit->init ) {
|
---|
| 169 | objDecl->init = init;
|
---|
| 170 | ctorInit->init = nullptr;
|
---|
[6cf27a07] | 171 | } else {
|
---|
| 172 | // no constructor and no initializer, which is okay
|
---|
[f072892] | 173 | objDecl->init = nullptr;
|
---|
[6cf27a07] | 174 | } // if
|
---|
| 175 | delete ctorInit;
|
---|
[ca35c51] | 176 | } // if
|
---|
[711eee5] | 177 | }
|
---|
| 178 |
|
---|
[490fb92e] | 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 ) {
|
---|
[7d651a66] | 194 | addDataSectionAttribute(mutDecl);
|
---|
[490fb92e] | 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 |
|
---|
[4e24610] | 209 | // only modify global variables
|
---|
[1cb934d] | 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; }
|
---|
[4e24610] | 216 |
|
---|
[711eee5] | 217 | } // namespace InitTweak
|
---|
| 218 |
|
---|
| 219 | // Local Variables: //
|
---|
| 220 | // tab-width: 4 //
|
---|
| 221 | // mode: c++ //
|
---|
| 222 | // compile-command: "make install" //
|
---|
| 223 | // End: //
|
---|