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