| [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 |
|
|---|
| 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 |
|
|---|
| [5f98ce5] | 82 | bool isConstExpr( Expression * expr ) {
|
|---|
| 83 | if ( expr ) {
|
|---|
| 84 | ConstExprChecker checker;
|
|---|
| 85 | expr->accept( checker );
|
|---|
| 86 | return checker.isConstExpr;
|
|---|
| 87 | }
|
|---|
| 88 | return true;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| [711eee5] | 91 | bool isConstExpr( Initializer * init ) {
|
|---|
| 92 | if ( init ) {
|
|---|
| 93 | ConstExprChecker checker;
|
|---|
| 94 | init->accept( checker );
|
|---|
| 95 | return checker.isConstExpr;
|
|---|
| [ca35c51] | 96 | } // if
|
|---|
| [711eee5] | 97 | // for all intents and purposes, no initializer means const expr
|
|---|
| 98 | return true;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| [03e5d14] | 101 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
|
|---|
| 102 | GlobalFixer fixer( name, inLibrary );
|
|---|
| [711eee5] | 103 | acceptAll( translationUnit, fixer );
|
|---|
| [ec79847] | 104 | // don't need to include function if it's empty
|
|---|
| 105 | if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
|
|---|
| 106 | delete fixer.initFunction;
|
|---|
| 107 | } else {
|
|---|
| 108 | translationUnit.push_back( fixer.initFunction );
|
|---|
| [ca35c51] | 109 | } // if
|
|---|
| 110 |
|
|---|
| [ec79847] | 111 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
|
|---|
| 112 | delete fixer.destroyFunction;
|
|---|
| 113 | } else {
|
|---|
| 114 | translationUnit.push_back( fixer.destroyFunction );
|
|---|
| [ca35c51] | 115 | } // if
|
|---|
| [711eee5] | 116 | }
|
|---|
| 117 |
|
|---|
| [9e2c1f0] | 118 | std::string globalFunctionName( const std::string & name ) {
|
|---|
| [711eee5] | 119 | // get basename
|
|---|
| 120 | std::string ret = name.substr( 0, name.find( '.' ) );
|
|---|
| 121 | // replace invalid characters with _
|
|---|
| 122 | static std::string invalid = "/-";
|
|---|
| 123 | replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
|
|---|
| [9e2c1f0] | 124 | return ret;
|
|---|
| [711eee5] | 125 | }
|
|---|
| 126 |
|
|---|
| [03e5d14] | 127 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
|
|---|
| [9e2c1f0] | 128 | std::string fixedName = globalFunctionName( name );
|
|---|
| [7baed7d] | 129 | std::list< Expression * > ctorParameters;
|
|---|
| 130 | std::list< Expression * > dtorParameters;
|
|---|
| 131 | if ( inLibrary ) {
|
|---|
| 132 | // Constructor/destructor attributes take a single parameter which
|
|---|
| 133 | // is the priority, with lower numbers meaning higher priority.
|
|---|
| 134 | // Functions specified with priority are guaranteed to run before
|
|---|
| 135 | // functions without a priority. To ensure that constructors and destructors
|
|---|
| 136 | // for library code are run before constructors and destructors for user code,
|
|---|
| 137 | // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
|
|---|
| 138 | ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
|
|---|
| 139 | dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
|
|---|
| 140 | }
|
|---|
| 141 | initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
|
|---|
| 142 | initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
|
|---|
| 143 | destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
|
|---|
| 144 | destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
|
|---|
| [711eee5] | 145 | }
|
|---|
| 146 |
|
|---|
| 147 | void GlobalFixer::visit( ObjectDecl *objDecl ) {
|
|---|
| [9e2c1f0] | 148 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
|
|---|
| 149 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
|
|---|
| [711eee5] | 150 |
|
|---|
| [9e2c1f0] | 151 | if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
|
|---|
| [7b3f66b] | 152 | if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return;
|
|---|
| [711eee5] | 153 | // C allows you to initialize objects with constant expressions
|
|---|
| [4e24610] | 154 | // xxx - this is an optimization. Need to first resolve constructors before we decide
|
|---|
| 155 | // to keep C-style initializer.
|
|---|
| 156 | // if ( isConstExpr( objDecl->get_init() ) ) return;
|
|---|
| [711eee5] | 157 |
|
|---|
| [ca35c51] | 158 | if ( dynamic_cast< ArrayType * > ( objDecl->get_type() ) ) {
|
|---|
| [7b3f66b] | 159 | // xxx - initialize each element of the array
|
|---|
| 160 | } else {
|
|---|
| 161 | // steal initializer from object and attach it to a new temporary
|
|---|
| 162 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
|
|---|
| 163 | objDecl->set_init( NULL );
|
|---|
| 164 | initStatements.push_back( new DeclStmt( noLabels, newObj ) );
|
|---|
| 165 |
|
|---|
| 166 | // copy construct objDecl using temporary
|
|---|
| 167 | UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
|
|---|
| 168 | init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
|
|---|
| 169 | init->get_args().push_back( new VariableExpr( newObj ) );
|
|---|
| [f1b1e4c] | 170 | initStatements.push_back( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, init ) ) );
|
|---|
| [7b3f66b] | 171 |
|
|---|
| 172 | // add destructor calls to global destroy function
|
|---|
| 173 | UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
|
|---|
| 174 | destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
|
|---|
| [f1b1e4c] | 175 | destroyStatements.push_front( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, destroy ) ) );
|
|---|
| [ca35c51] | 176 | } // if
|
|---|
| [711eee5] | 177 | }
|
|---|
| 178 |
|
|---|
| [4e24610] | 179 | // only modify global variables
|
|---|
| 180 | void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
|
|---|
| 181 | void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
|
|---|
| 182 | void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
|
|---|
| 183 | void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
|
|---|
| 184 | void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
|
|---|
| 185 | void GlobalFixer::visit( TypeDecl *typeDecl ) {}
|
|---|
| 186 |
|
|---|
| [711eee5] | 187 | } // namespace InitTweak
|
|---|
| 188 |
|
|---|
| 189 | // Local Variables: //
|
|---|
| 190 | // tab-width: 4 //
|
|---|
| 191 | // mode: c++ //
|
|---|
| 192 | // compile-command: "make install" //
|
|---|
| 193 | // End: //
|
|---|
| 194 |
|
|---|