[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 | |
---|
| 36 | namespace InitTweak { |
---|
[1cb934d] | 37 | class GlobalFixer : public WithShortCircuiting { |
---|
[711eee5] | 38 | public: |
---|
[1be845b] | 39 | GlobalFixer( bool inLibrary ); |
---|
[711eee5] | 40 | |
---|
[1cb934d] | 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 ); |
---|
[711eee5] | 48 | |
---|
| 49 | UniqueName tempNamer; |
---|
[37024fd] | 50 | FunctionDecl * initFunction; |
---|
[9e2c1f0] | 51 | FunctionDecl * destroyFunction; |
---|
[711eee5] | 52 | }; |
---|
| 53 | |
---|
[1be845b] | 54 | void fixGlobalInit( std::list< Declaration * > & translationUnit, bool inLibrary ) { |
---|
| 55 | PassVisitor<GlobalFixer> visitor( inLibrary ); |
---|
[1cb934d] | 56 | acceptAll( translationUnit, visitor ); |
---|
| 57 | GlobalFixer & fixer = visitor.pass; |
---|
[ec79847] | 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 ); |
---|
[ca35c51] | 63 | } // if |
---|
| 64 | |
---|
[ec79847] | 65 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) { |
---|
| 66 | delete fixer.destroyFunction; |
---|
| 67 | } else { |
---|
| 68 | translationUnit.push_back( fixer.destroyFunction ); |
---|
[ca35c51] | 69 | } // if |
---|
[711eee5] | 70 | } |
---|
| 71 | |
---|
[1be845b] | 72 | GlobalFixer::GlobalFixer( bool inLibrary ) : tempNamer( "_global_init" ) { |
---|
[7baed7d] | 73 | std::list< Expression * > ctorParameters; |
---|
| 74 | std::list< Expression * > dtorParameters; |
---|
| 75 | if ( inLibrary ) { |
---|
| 76 | // Constructor/destructor attributes take a single parameter which |
---|
| 77 | // is the priority, with lower numbers meaning higher priority. |
---|
| 78 | // Functions specified with priority are guaranteed to run before |
---|
| 79 | // functions without a priority. To ensure that constructors and destructors |
---|
| 80 | // for library code are run before constructors and destructors for user code, |
---|
| 81 | // specify a priority when building the library. Priorities 0-100 are reserved by gcc. |
---|
[1be845b] | 82 | // Priorities 101-200 are reserved by cfa, so use priority 200 for CFA library globals, |
---|
| 83 | // allowing room for overriding with a higher priority. |
---|
| 84 | ctorParameters.push_back( new ConstantExpr( Constant::from_int( 200 ) ) ); |
---|
| 85 | dtorParameters.push_back( new ConstantExpr( Constant::from_int( 200 ) ) ); |
---|
[7baed7d] | 86 | } |
---|
[1be845b] | 87 | initFunction = new FunctionDecl( "__global_init__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() ); |
---|
[7baed7d] | 88 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) ); |
---|
[1be845b] | 89 | destroyFunction = new FunctionDecl( "__global_destroy__", Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() ); |
---|
[7baed7d] | 90 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) ); |
---|
[711eee5] | 91 | } |
---|
| 92 | |
---|
[1cb934d] | 93 | void GlobalFixer::previsit( ObjectDecl *objDecl ) { |
---|
[9e2c1f0] | 94 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids(); |
---|
| 95 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids(); |
---|
[711eee5] | 96 | |
---|
| 97 | // C allows you to initialize objects with constant expressions |
---|
[4e24610] | 98 | // xxx - this is an optimization. Need to first resolve constructors before we decide |
---|
| 99 | // to keep C-style initializer. |
---|
| 100 | // if ( isConstExpr( objDecl->get_init() ) ) return; |
---|
[711eee5] | 101 | |
---|
[6cf27a07] | 102 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { |
---|
| 103 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL |
---|
[f072892] | 104 | assert( ! ctorInit->ctor || ! ctorInit->init ); |
---|
[6cf27a07] | 105 | |
---|
[f072892] | 106 | Statement * dtor = ctorInit->dtor; |
---|
[f9cebb5] | 107 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) { |
---|
[6cf27a07] | 108 | // don't need to call intrinsic dtor, because it does nothing, but |
---|
| 109 | // non-intrinsic dtors must be called |
---|
| 110 | destroyStatements.push_front( dtor ); |
---|
[f072892] | 111 | ctorInit->dtor = nullptr; |
---|
[6cf27a07] | 112 | } // if |
---|
[f072892] | 113 | if ( Statement * ctor = ctorInit->ctor ) { |
---|
[6cf27a07] | 114 | initStatements.push_back( ctor ); |
---|
[f072892] | 115 | objDecl->init = nullptr; |
---|
| 116 | ctorInit->ctor = nullptr; |
---|
| 117 | } else if ( Initializer * init = ctorInit->init ) { |
---|
| 118 | objDecl->init = init; |
---|
| 119 | ctorInit->init = nullptr; |
---|
[6cf27a07] | 120 | } else { |
---|
| 121 | // no constructor and no initializer, which is okay |
---|
[f072892] | 122 | objDecl->init = nullptr; |
---|
[6cf27a07] | 123 | } // if |
---|
| 124 | delete ctorInit; |
---|
[ca35c51] | 125 | } // if |
---|
[711eee5] | 126 | } |
---|
| 127 | |
---|
[4e24610] | 128 | // only modify global variables |
---|
[1cb934d] | 129 | void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; } |
---|
| 130 | void GlobalFixer::previsit( StructDecl * ) { visit_children = false; } |
---|
| 131 | void GlobalFixer::previsit( UnionDecl * ) { visit_children = false; } |
---|
| 132 | void GlobalFixer::previsit( EnumDecl * ) { visit_children = false; } |
---|
| 133 | void GlobalFixer::previsit( TraitDecl * ) { visit_children = false; } |
---|
| 134 | void GlobalFixer::previsit( TypeDecl * ) { visit_children = false; } |
---|
[4e24610] | 135 | |
---|
[711eee5] | 136 | } // namespace InitTweak |
---|
| 137 | |
---|
| 138 | // Local Variables: // |
---|
| 139 | // tab-width: 4 // |
---|
| 140 | // mode: c++ // |
---|
| 141 | // compile-command: "make install" // |
---|
| 142 | // End: // |
---|