[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 | |
---|
[490fb92e] | 111 | void fixGlobalInit(std::list<ast::ptr<ast::Decl>> & 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.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.emplace_back(destroyFunction); |
---|
| 131 | } // if |
---|
| 132 | } |
---|
| 133 | |
---|
[1cb934d] | 134 | void GlobalFixer::previsit( ObjectDecl *objDecl ) { |
---|
[9e2c1f0] | 135 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids(); |
---|
| 136 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids(); |
---|
[711eee5] | 137 | |
---|
| 138 | // C allows you to initialize objects with constant expressions |
---|
[4e24610] | 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; |
---|
[711eee5] | 142 | |
---|
[6cf27a07] | 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 |
---|
[f072892] | 145 | assert( ! ctorInit->ctor || ! ctorInit->init ); |
---|
[6cf27a07] | 146 | |
---|
[f072892] | 147 | Statement * dtor = ctorInit->dtor; |
---|
[f9cebb5] | 148 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) { |
---|
[6cf27a07] | 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 ); |
---|
[f072892] | 152 | ctorInit->dtor = nullptr; |
---|
[6cf27a07] | 153 | } // if |
---|
[f072892] | 154 | if ( Statement * ctor = ctorInit->ctor ) { |
---|
[f1791a4] | 155 | addDataSectonAttribute( objDecl ); |
---|
[6cf27a07] | 156 | initStatements.push_back( ctor ); |
---|
[f072892] | 157 | objDecl->init = nullptr; |
---|
| 158 | ctorInit->ctor = nullptr; |
---|
| 159 | } else if ( Initializer * init = ctorInit->init ) { |
---|
| 160 | objDecl->init = init; |
---|
| 161 | ctorInit->init = nullptr; |
---|
[6cf27a07] | 162 | } else { |
---|
| 163 | // no constructor and no initializer, which is okay |
---|
[f072892] | 164 | objDecl->init = nullptr; |
---|
[6cf27a07] | 165 | } // if |
---|
| 166 | delete ctorInit; |
---|
[ca35c51] | 167 | } // if |
---|
[711eee5] | 168 | } |
---|
| 169 | |
---|
[490fb92e] | 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 | initStmts.push_back( ctor ); |
---|
| 186 | mutDecl->init = nullptr; |
---|
| 187 | // ctorInit->ctor = nullptr; |
---|
| 188 | } else if ( const ast::Init * init = ctorInit->init ) { |
---|
| 189 | mutDecl->init = init; |
---|
| 190 | // ctorInit->init = nullptr; |
---|
| 191 | } else { |
---|
| 192 | // no constructor and no initializer, which is okay |
---|
| 193 | mutDecl->init = nullptr; |
---|
| 194 | } // if |
---|
| 195 | // delete ctorInit; |
---|
| 196 | } // if |
---|
| 197 | } |
---|
| 198 | |
---|
[4e24610] | 199 | // only modify global variables |
---|
[1cb934d] | 200 | void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; } |
---|
| 201 | void GlobalFixer::previsit( StructDecl * ) { visit_children = false; } |
---|
| 202 | void GlobalFixer::previsit( UnionDecl * ) { visit_children = false; } |
---|
| 203 | void GlobalFixer::previsit( EnumDecl * ) { visit_children = false; } |
---|
| 204 | void GlobalFixer::previsit( TraitDecl * ) { visit_children = false; } |
---|
| 205 | void GlobalFixer::previsit( TypeDecl * ) { visit_children = false; } |
---|
[4e24610] | 206 | |
---|
[711eee5] | 207 | } // namespace InitTweak |
---|
| 208 | |
---|
| 209 | // Local Variables: // |
---|
| 210 | // tab-width: 4 // |
---|
| 211 | // mode: c++ // |
---|
| 212 | // compile-command: "make install" // |
---|
| 213 | // End: // |
---|