| 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 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Thu Mar 16 07:53:11 2017 | 
|---|
| 13 | // Update Count     : 18 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "FixGlobalInit.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include <cassert>                 // for assert | 
|---|
| 19 | #include <stddef.h>                // for NULL | 
|---|
| 20 | #include <algorithm>               // for replace_if | 
|---|
| 21 |  | 
|---|
| 22 | #include "Common/PassVisitor.h" | 
|---|
| 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 | 
|---|
| 32 | #include "SynTree/Label.h"         // for Label | 
|---|
| 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 | 
|---|
| 36 |  | 
|---|
| 37 | namespace InitTweak { | 
|---|
| 38 | class GlobalFixer : public WithShortCircuiting { | 
|---|
| 39 | public: | 
|---|
| 40 | GlobalFixer( const std::string & name, bool inLibrary ); | 
|---|
| 41 |  | 
|---|
| 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 ); | 
|---|
| 49 |  | 
|---|
| 50 | UniqueName tempNamer; | 
|---|
| 51 | FunctionDecl * initFunction; | 
|---|
| 52 | FunctionDecl * destroyFunction; | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) { | 
|---|
| 56 | PassVisitor<GlobalFixer> visitor( name, inLibrary ); | 
|---|
| 57 | acceptAll( translationUnit, visitor ); | 
|---|
| 58 | GlobalFixer & fixer = visitor.pass; | 
|---|
| 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 ); | 
|---|
| 64 | } // if | 
|---|
| 65 |  | 
|---|
| 66 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) { | 
|---|
| 67 | delete fixer.destroyFunction; | 
|---|
| 68 | } else { | 
|---|
| 69 | translationUnit.push_back( fixer.destroyFunction ); | 
|---|
| 70 | } // if | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | std::string globalFunctionName( const std::string & name ) { | 
|---|
| 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; }, '_' ); | 
|---|
| 79 | return ret; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) { | 
|---|
| 83 | std::string fixedName = globalFunctionName( name ); | 
|---|
| 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. | 
|---|
| 93 | ctorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) ); | 
|---|
| 94 | dtorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) ); | 
|---|
| 95 | } | 
|---|
| 96 | initFunction = new FunctionDecl( "_init_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() ); | 
|---|
| 97 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) ); | 
|---|
| 98 | destroyFunction = new FunctionDecl( "_destroy_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() ); | 
|---|
| 99 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) ); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void GlobalFixer::previsit( ObjectDecl *objDecl ) { | 
|---|
| 103 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids(); | 
|---|
| 104 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids(); | 
|---|
| 105 |  | 
|---|
| 106 | // C allows you to initialize objects with constant expressions | 
|---|
| 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; | 
|---|
| 110 |  | 
|---|
| 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(); | 
|---|
| 116 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) { | 
|---|
| 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; | 
|---|
| 134 | } // if | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | // only modify global variables | 
|---|
| 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; } | 
|---|
| 144 |  | 
|---|
| 145 | } // namespace InitTweak | 
|---|
| 146 |  | 
|---|
| 147 | // Local Variables: // | 
|---|
| 148 | // tab-width: 4 // | 
|---|
| 149 | // mode: c++ // | 
|---|
| 150 | // compile-command: "make install" // | 
|---|
| 151 | // End: // | 
|---|