[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"
|
---|
[7b3f66b] | 17 | #include "InitTweak.h"
|
---|
[711eee5] | 18 | #include "SynTree/Declaration.h"
|
---|
| 19 | #include "SynTree/Type.h"
|
---|
| 20 | #include "SynTree/Expression.h"
|
---|
| 21 | #include "SynTree/Statement.h"
|
---|
| 22 | #include "SynTree/Initializer.h"
|
---|
| 23 | #include "SynTree/Visitor.h"
|
---|
[7baed7d] | 24 | #include "SynTree/Attribute.h"
|
---|
[711eee5] | 25 | #include <algorithm>
|
---|
| 26 |
|
---|
| 27 | namespace InitTweak {
|
---|
| 28 | class GlobalFixer : public Visitor {
|
---|
| 29 | public:
|
---|
[03e5d14] | 30 | GlobalFixer( const std::string & name, bool inLibrary );
|
---|
[711eee5] | 31 |
|
---|
| 32 | virtual void visit( ObjectDecl *objDecl );
|
---|
| 33 | virtual void visit( FunctionDecl *functionDecl );
|
---|
[4e24610] | 34 | virtual void visit( StructDecl *aggregateDecl );
|
---|
| 35 | virtual void visit( UnionDecl *aggregateDecl );
|
---|
| 36 | virtual void visit( EnumDecl *aggregateDecl );
|
---|
| 37 | virtual void visit( TraitDecl *aggregateDecl );
|
---|
| 38 | virtual void visit( TypeDecl *typeDecl );
|
---|
[711eee5] | 39 |
|
---|
| 40 | UniqueName tempNamer;
|
---|
[37024fd] | 41 | FunctionDecl * initFunction;
|
---|
[9e2c1f0] | 42 | FunctionDecl * destroyFunction;
|
---|
[711eee5] | 43 | };
|
---|
| 44 |
|
---|
[03e5d14] | 45 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
|
---|
| 46 | GlobalFixer fixer( name, inLibrary );
|
---|
[711eee5] | 47 | acceptAll( translationUnit, fixer );
|
---|
[ec79847] | 48 | // don't need to include function if it's empty
|
---|
| 49 | if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
|
---|
| 50 | delete fixer.initFunction;
|
---|
| 51 | } else {
|
---|
| 52 | translationUnit.push_back( fixer.initFunction );
|
---|
[ca35c51] | 53 | } // if
|
---|
| 54 |
|
---|
[ec79847] | 55 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
|
---|
| 56 | delete fixer.destroyFunction;
|
---|
| 57 | } else {
|
---|
| 58 | translationUnit.push_back( fixer.destroyFunction );
|
---|
[ca35c51] | 59 | } // if
|
---|
[711eee5] | 60 | }
|
---|
| 61 |
|
---|
[9e2c1f0] | 62 | std::string globalFunctionName( const std::string & name ) {
|
---|
[711eee5] | 63 | // get basename
|
---|
| 64 | std::string ret = name.substr( 0, name.find( '.' ) );
|
---|
| 65 | // replace invalid characters with _
|
---|
| 66 | static std::string invalid = "/-";
|
---|
| 67 | replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
|
---|
[9e2c1f0] | 68 | return ret;
|
---|
[711eee5] | 69 | }
|
---|
| 70 |
|
---|
[03e5d14] | 71 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
|
---|
[9e2c1f0] | 72 | std::string fixedName = globalFunctionName( name );
|
---|
[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.
|
---|
[eb2e723] | 82 | ctorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
|
---|
| 83 | dtorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
|
---|
[7baed7d] | 84 | }
|
---|
[68fe077a] | 85 | initFunction = new FunctionDecl( "_init_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ) );
|
---|
[7baed7d] | 86 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
|
---|
[68fe077a] | 87 | destroyFunction = new FunctionDecl( "_destroy_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ) );
|
---|
[7baed7d] | 88 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
|
---|
[711eee5] | 89 | }
|
---|
| 90 |
|
---|
| 91 | void GlobalFixer::visit( ObjectDecl *objDecl ) {
|
---|
[9e2c1f0] | 92 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
|
---|
| 93 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
|
---|
[711eee5] | 94 |
|
---|
| 95 | // C allows you to initialize objects with constant expressions
|
---|
[4e24610] | 96 | // xxx - this is an optimization. Need to first resolve constructors before we decide
|
---|
| 97 | // to keep C-style initializer.
|
---|
| 98 | // if ( isConstExpr( objDecl->get_init() ) ) return;
|
---|
[711eee5] | 99 |
|
---|
[6cf27a07] | 100 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
|
---|
| 101 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL
|
---|
| 102 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
|
---|
| 103 |
|
---|
| 104 | Statement * dtor = ctorInit->get_dtor();
|
---|
[f9cebb5] | 105 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
|
---|
[6cf27a07] | 106 | // don't need to call intrinsic dtor, because it does nothing, but
|
---|
| 107 | // non-intrinsic dtors must be called
|
---|
| 108 | destroyStatements.push_front( dtor );
|
---|
| 109 | ctorInit->set_dtor( NULL );
|
---|
| 110 | } // if
|
---|
| 111 | if ( Statement * ctor = ctorInit->get_ctor() ) {
|
---|
| 112 | initStatements.push_back( ctor );
|
---|
| 113 | objDecl->set_init( NULL );
|
---|
| 114 | ctorInit->set_ctor( NULL );
|
---|
| 115 | } else if ( Initializer * init = ctorInit->get_init() ) {
|
---|
| 116 | objDecl->set_init( init );
|
---|
| 117 | ctorInit->set_init( NULL );
|
---|
| 118 | } else {
|
---|
| 119 | // no constructor and no initializer, which is okay
|
---|
| 120 | objDecl->set_init( NULL );
|
---|
| 121 | } // if
|
---|
| 122 | delete ctorInit;
|
---|
[ca35c51] | 123 | } // if
|
---|
[711eee5] | 124 | }
|
---|
| 125 |
|
---|
[4e24610] | 126 | // only modify global variables
|
---|
[7e003011] | 127 | void GlobalFixer::visit( __attribute__((unused)) FunctionDecl *functionDecl ) {}
|
---|
| 128 | void GlobalFixer::visit( __attribute__((unused)) StructDecl *aggregateDecl ) {}
|
---|
| 129 | void GlobalFixer::visit( __attribute__((unused)) UnionDecl *aggregateDecl ) {}
|
---|
| 130 | void GlobalFixer::visit( __attribute__((unused)) EnumDecl *aggregateDecl ) {}
|
---|
| 131 | void GlobalFixer::visit( __attribute__((unused)) TraitDecl *aggregateDecl ) {}
|
---|
| 132 | void GlobalFixer::visit( __attribute__((unused)) TypeDecl *typeDecl ) {}
|
---|
[4e24610] | 133 |
|
---|
[711eee5] | 134 | } // namespace InitTweak
|
---|
| 135 |
|
---|
| 136 | // Local Variables: //
|
---|
| 137 | // tab-width: 4 //
|
---|
| 138 | // mode: c++ //
|
---|
| 139 | // compile-command: "make install" //
|
---|
| 140 | // End: //
|
---|