[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
|
---|
[68fe077a] | 12 | // Last Modified On : Thu Mar 16 07:53:11 2017
|
---|
| 13 | // Update Count : 18
|
---|
[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/SemanticError.h" // for SemanticError
|
---|
| 24 | #include "Common/UniqueName.h" // for UniqueName
|
---|
| 25 | #include "InitTweak.h" // for isIntrinsicSingleArgCallStmt
|
---|
| 26 | #include "Parser/LinkageSpec.h" // for C
|
---|
| 27 | #include "SynTree/Attribute.h" // for Attribute
|
---|
| 28 | #include "SynTree/Constant.h" // for Constant
|
---|
| 29 | #include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declaration
|
---|
| 30 | #include "SynTree/Expression.h" // for ConstantExpr, Expression (ptr only)
|
---|
| 31 | #include "SynTree/Initializer.h" // for ConstructorInit, Initializer
|
---|
[ba3706f] | 32 | #include "SynTree/Label.h" // for Label
|
---|
[d180746] | 33 | #include "SynTree/Statement.h" // for CompoundStmt, Statement (ptr only)
|
---|
| 34 | #include "SynTree/Type.h" // for Type, Type::StorageClasses, Functi...
|
---|
| 35 | #include "SynTree/Visitor.h" // for acceptAll, Visitor
|
---|
[711eee5] | 36 |
|
---|
| 37 | namespace InitTweak {
|
---|
[1cb934d] | 38 | class GlobalFixer : public WithShortCircuiting {
|
---|
[711eee5] | 39 | public:
|
---|
[03e5d14] | 40 | GlobalFixer( const std::string & name, bool inLibrary );
|
---|
[711eee5] | 41 |
|
---|
[1cb934d] | 42 | void previsit( ObjectDecl *objDecl );
|
---|
| 43 | void previsit( FunctionDecl *functionDecl );
|
---|
| 44 | void previsit( StructDecl *aggregateDecl );
|
---|
| 45 | void previsit( UnionDecl *aggregateDecl );
|
---|
| 46 | void previsit( EnumDecl *aggregateDecl );
|
---|
| 47 | void previsit( TraitDecl *aggregateDecl );
|
---|
| 48 | void previsit( TypeDecl *typeDecl );
|
---|
[711eee5] | 49 |
|
---|
| 50 | UniqueName tempNamer;
|
---|
[37024fd] | 51 | FunctionDecl * initFunction;
|
---|
[9e2c1f0] | 52 | FunctionDecl * destroyFunction;
|
---|
[711eee5] | 53 | };
|
---|
| 54 |
|
---|
[03e5d14] | 55 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
|
---|
[1cb934d] | 56 | PassVisitor<GlobalFixer> visitor( name, inLibrary );
|
---|
| 57 | acceptAll( translationUnit, visitor );
|
---|
| 58 | GlobalFixer & fixer = visitor.pass;
|
---|
[ec79847] | 59 | // don't need to include function if it's empty
|
---|
| 60 | if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
|
---|
| 61 | delete fixer.initFunction;
|
---|
| 62 | } else {
|
---|
| 63 | translationUnit.push_back( fixer.initFunction );
|
---|
[ca35c51] | 64 | } // if
|
---|
| 65 |
|
---|
[ec79847] | 66 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
|
---|
| 67 | delete fixer.destroyFunction;
|
---|
| 68 | } else {
|
---|
| 69 | translationUnit.push_back( fixer.destroyFunction );
|
---|
[ca35c51] | 70 | } // if
|
---|
[711eee5] | 71 | }
|
---|
| 72 |
|
---|
[9e2c1f0] | 73 | std::string globalFunctionName( const std::string & name ) {
|
---|
[711eee5] | 74 | // get basename
|
---|
| 75 | std::string ret = name.substr( 0, name.find( '.' ) );
|
---|
| 76 | // replace invalid characters with _
|
---|
| 77 | static std::string invalid = "/-";
|
---|
| 78 | replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
|
---|
[9e2c1f0] | 79 | return ret;
|
---|
[711eee5] | 80 | }
|
---|
| 81 |
|
---|
[03e5d14] | 82 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
|
---|
[9e2c1f0] | 83 | std::string fixedName = globalFunctionName( name );
|
---|
[7baed7d] | 84 | std::list< Expression * > ctorParameters;
|
---|
| 85 | std::list< Expression * > dtorParameters;
|
---|
| 86 | if ( inLibrary ) {
|
---|
| 87 | // Constructor/destructor attributes take a single parameter which
|
---|
| 88 | // is the priority, with lower numbers meaning higher priority.
|
---|
| 89 | // Functions specified with priority are guaranteed to run before
|
---|
| 90 | // functions without a priority. To ensure that constructors and destructors
|
---|
| 91 | // for library code are run before constructors and destructors for user code,
|
---|
| 92 | // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
|
---|
[eb2e723] | 93 | ctorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
|
---|
| 94 | dtorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
|
---|
[7baed7d] | 95 | }
|
---|
[ba3706f] | 96 | initFunction = new FunctionDecl( "_init_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
|
---|
[7baed7d] | 97 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
|
---|
[ba3706f] | 98 | destroyFunction = new FunctionDecl( "_destroy_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() );
|
---|
[7baed7d] | 99 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
|
---|
[711eee5] | 100 | }
|
---|
| 101 |
|
---|
[1cb934d] | 102 | void GlobalFixer::previsit( ObjectDecl *objDecl ) {
|
---|
[9e2c1f0] | 103 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
|
---|
| 104 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
|
---|
[711eee5] | 105 |
|
---|
| 106 | // C allows you to initialize objects with constant expressions
|
---|
[4e24610] | 107 | // xxx - this is an optimization. Need to first resolve constructors before we decide
|
---|
| 108 | // to keep C-style initializer.
|
---|
| 109 | // if ( isConstExpr( objDecl->get_init() ) ) return;
|
---|
[711eee5] | 110 |
|
---|
[6cf27a07] | 111 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
|
---|
| 112 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL
|
---|
| 113 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
|
---|
| 114 |
|
---|
| 115 | Statement * dtor = ctorInit->get_dtor();
|
---|
[f9cebb5] | 116 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
|
---|
[6cf27a07] | 117 | // don't need to call intrinsic dtor, because it does nothing, but
|
---|
| 118 | // non-intrinsic dtors must be called
|
---|
| 119 | destroyStatements.push_front( dtor );
|
---|
| 120 | ctorInit->set_dtor( NULL );
|
---|
| 121 | } // if
|
---|
| 122 | if ( Statement * ctor = ctorInit->get_ctor() ) {
|
---|
| 123 | initStatements.push_back( ctor );
|
---|
| 124 | objDecl->set_init( NULL );
|
---|
| 125 | ctorInit->set_ctor( NULL );
|
---|
| 126 | } else if ( Initializer * init = ctorInit->get_init() ) {
|
---|
| 127 | objDecl->set_init( init );
|
---|
| 128 | ctorInit->set_init( NULL );
|
---|
| 129 | } else {
|
---|
| 130 | // no constructor and no initializer, which is okay
|
---|
| 131 | objDecl->set_init( NULL );
|
---|
| 132 | } // if
|
---|
| 133 | delete ctorInit;
|
---|
[ca35c51] | 134 | } // if
|
---|
[711eee5] | 135 | }
|
---|
| 136 |
|
---|
[4e24610] | 137 | // only modify global variables
|
---|
[1cb934d] | 138 | void GlobalFixer::previsit( FunctionDecl * ) { visit_children = false; }
|
---|
| 139 | void GlobalFixer::previsit( StructDecl * ) { visit_children = false; }
|
---|
| 140 | void GlobalFixer::previsit( UnionDecl * ) { visit_children = false; }
|
---|
| 141 | void GlobalFixer::previsit( EnumDecl * ) { visit_children = false; }
|
---|
| 142 | void GlobalFixer::previsit( TraitDecl * ) { visit_children = false; }
|
---|
| 143 | void GlobalFixer::previsit( TypeDecl * ) { visit_children = false; }
|
---|
[4e24610] | 144 |
|
---|
[711eee5] | 145 | } // namespace InitTweak
|
---|
| 146 |
|
---|
| 147 | // Local Variables: //
|
---|
| 148 | // tab-width: 4 //
|
---|
| 149 | // mode: c++ //
|
---|
| 150 | // compile-command: "make install" //
|
---|
| 151 | // End: //
|
---|