[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 |
---|
| 11 | // Last Modified By : Rob Schluntz |
---|
[7b3f66b] | 12 | // Last Modified On : Fri May 13 11:37:30 2016 |
---|
[711eee5] | 13 | // Update Count : 2 |
---|
| 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 | |
---|
| 49 | class ConstExprChecker : public Visitor { |
---|
| 50 | public: |
---|
| 51 | ConstExprChecker() : isConstExpr( true ) {} |
---|
| 52 | |
---|
| 53 | virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; } |
---|
| 54 | virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; } |
---|
| 55 | virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; } |
---|
| 56 | virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } |
---|
| 57 | virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; } |
---|
| 58 | virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; } |
---|
| 59 | virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; } |
---|
| 60 | virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; } |
---|
| 61 | virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ } |
---|
| 62 | // these might be okay? |
---|
| 63 | // virtual void visit( SizeofExpr *sizeofExpr ); |
---|
| 64 | // virtual void visit( AlignofExpr *alignofExpr ); |
---|
| 65 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
| 66 | // virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
| 67 | // virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
| 68 | // virtual void visit( AttrExpr *attrExpr ); |
---|
| 69 | // virtual void visit( CommaExpr *commaExpr ); |
---|
| 70 | // virtual void visit( LogicalExpr *logicalExpr ); |
---|
| 71 | // virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
| 72 | virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
| 73 | virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
| 74 | virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; } |
---|
| 75 | virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; } |
---|
| 76 | virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; } |
---|
| 77 | virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; } |
---|
| 78 | |
---|
| 79 | bool isConstExpr; |
---|
| 80 | }; |
---|
| 81 | |
---|
| 82 | bool isConstExpr( Initializer * init ) { |
---|
| 83 | if ( init ) { |
---|
| 84 | ConstExprChecker checker; |
---|
| 85 | init->accept( checker ); |
---|
| 86 | return checker.isConstExpr; |
---|
| 87 | } |
---|
| 88 | // for all intents and purposes, no initializer means const expr |
---|
| 89 | return true; |
---|
| 90 | } |
---|
| 91 | |
---|
[03e5d14] | 92 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) { |
---|
| 93 | GlobalFixer fixer( name, inLibrary ); |
---|
[711eee5] | 94 | acceptAll( translationUnit, fixer ); |
---|
[ec79847] | 95 | // don't need to include function if it's empty |
---|
| 96 | if ( fixer.initFunction->get_statements()->get_kids().empty() ) { |
---|
| 97 | delete fixer.initFunction; |
---|
| 98 | } else { |
---|
| 99 | translationUnit.push_back( fixer.initFunction ); |
---|
| 100 | } |
---|
| 101 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) { |
---|
| 102 | delete fixer.destroyFunction; |
---|
| 103 | } else { |
---|
| 104 | translationUnit.push_back( fixer.destroyFunction ); |
---|
| 105 | } |
---|
[711eee5] | 106 | } |
---|
| 107 | |
---|
[9e2c1f0] | 108 | std::string globalFunctionName( const std::string & name ) { |
---|
[711eee5] | 109 | // get basename |
---|
| 110 | std::string ret = name.substr( 0, name.find( '.' ) ); |
---|
| 111 | // replace invalid characters with _ |
---|
| 112 | static std::string invalid = "/-"; |
---|
| 113 | replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' ); |
---|
[9e2c1f0] | 114 | return ret; |
---|
[711eee5] | 115 | } |
---|
| 116 | |
---|
[03e5d14] | 117 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) { |
---|
[9e2c1f0] | 118 | std::string fixedName = globalFunctionName( name ); |
---|
[7baed7d] | 119 | std::list< Expression * > ctorParameters; |
---|
| 120 | std::list< Expression * > dtorParameters; |
---|
| 121 | if ( inLibrary ) { |
---|
| 122 | // Constructor/destructor attributes take a single parameter which |
---|
| 123 | // is the priority, with lower numbers meaning higher priority. |
---|
| 124 | // Functions specified with priority are guaranteed to run before |
---|
| 125 | // functions without a priority. To ensure that constructors and destructors |
---|
| 126 | // for library code are run before constructors and destructors for user code, |
---|
| 127 | // specify a priority when building the library. Priorities 0-100 are reserved by gcc. |
---|
| 128 | ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) ); |
---|
| 129 | dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) ); |
---|
| 130 | } |
---|
| 131 | initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false ); |
---|
| 132 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) ); |
---|
| 133 | destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false ); |
---|
| 134 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) ); |
---|
[711eee5] | 135 | } |
---|
| 136 | |
---|
| 137 | void GlobalFixer::visit( ObjectDecl *objDecl ) { |
---|
[9e2c1f0] | 138 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids(); |
---|
| 139 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids(); |
---|
[711eee5] | 140 | |
---|
[9e2c1f0] | 141 | if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects |
---|
[7b3f66b] | 142 | if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return; |
---|
[711eee5] | 143 | // C allows you to initialize objects with constant expressions |
---|
[4e24610] | 144 | // xxx - this is an optimization. Need to first resolve constructors before we decide |
---|
| 145 | // to keep C-style initializer. |
---|
| 146 | // if ( isConstExpr( objDecl->get_init() ) ) return; |
---|
[711eee5] | 147 | |
---|
[7b3f66b] | 148 | if ( ArrayType * at = dynamic_cast< ArrayType * > ( objDecl->get_type() ) ) { |
---|
| 149 | // xxx - initialize each element of the array |
---|
| 150 | } else { |
---|
| 151 | // steal initializer from object and attach it to a new temporary |
---|
| 152 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() ); |
---|
| 153 | objDecl->set_init( NULL ); |
---|
| 154 | initStatements.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
| 155 | |
---|
| 156 | // copy construct objDecl using temporary |
---|
| 157 | UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) ); |
---|
| 158 | init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
| 159 | init->get_args().push_back( new VariableExpr( newObj ) ); |
---|
[f1b1e4c] | 160 | initStatements.push_back( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, init ) ) ); |
---|
[7b3f66b] | 161 | |
---|
| 162 | // add destructor calls to global destroy function |
---|
| 163 | UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) ); |
---|
| 164 | destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
[f1b1e4c] | 165 | destroyStatements.push_front( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, destroy ) ) ); |
---|
[7b3f66b] | 166 | } |
---|
[711eee5] | 167 | } |
---|
| 168 | |
---|
[4e24610] | 169 | // only modify global variables |
---|
| 170 | void GlobalFixer::visit( FunctionDecl *functionDecl ) {} |
---|
| 171 | void GlobalFixer::visit( StructDecl *aggregateDecl ) {} |
---|
| 172 | void GlobalFixer::visit( UnionDecl *aggregateDecl ) {} |
---|
| 173 | void GlobalFixer::visit( EnumDecl *aggregateDecl ) {} |
---|
| 174 | void GlobalFixer::visit( TraitDecl *aggregateDecl ) {} |
---|
| 175 | void GlobalFixer::visit( TypeDecl *typeDecl ) {} |
---|
| 176 | |
---|
[711eee5] | 177 | } // namespace InitTweak |
---|
| 178 | |
---|
| 179 | // Local Variables: // |
---|
| 180 | // tab-width: 4 // |
---|
| 181 | // mode: c++ // |
---|
| 182 | // compile-command: "make install" // |
---|
| 183 | // End: // |
---|
| 184 | |
---|