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