[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" |
---|
| 24 | #include <algorithm> |
---|
| 25 | |
---|
| 26 | namespace InitTweak { |
---|
| 27 | namespace { |
---|
| 28 | const std::list<Label> noLabels; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | class GlobalFixer : public Visitor { |
---|
| 32 | public: |
---|
[03e5d14] | 33 | GlobalFixer( const std::string & name, bool inLibrary ); |
---|
[711eee5] | 34 | |
---|
| 35 | virtual void visit( ObjectDecl *objDecl ); |
---|
| 36 | virtual void visit( FunctionDecl *functionDecl ); |
---|
[4e24610] | 37 | virtual void visit( StructDecl *aggregateDecl ); |
---|
| 38 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
| 39 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
| 40 | virtual void visit( TraitDecl *aggregateDecl ); |
---|
| 41 | virtual void visit( TypeDecl *typeDecl ); |
---|
[711eee5] | 42 | |
---|
| 43 | UniqueName tempNamer; |
---|
[37024fd] | 44 | FunctionDecl * initFunction; |
---|
[9e2c1f0] | 45 | FunctionDecl * destroyFunction; |
---|
[711eee5] | 46 | }; |
---|
| 47 | |
---|
| 48 | class ConstExprChecker : public Visitor { |
---|
| 49 | public: |
---|
| 50 | ConstExprChecker() : isConstExpr( true ) {} |
---|
| 51 | |
---|
| 52 | virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; } |
---|
| 53 | virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; } |
---|
| 54 | virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; } |
---|
| 55 | virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } |
---|
| 56 | virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; } |
---|
| 57 | virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; } |
---|
| 58 | virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; } |
---|
| 59 | virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; } |
---|
| 60 | virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ } |
---|
| 61 | // these might be okay? |
---|
| 62 | // virtual void visit( SizeofExpr *sizeofExpr ); |
---|
| 63 | // virtual void visit( AlignofExpr *alignofExpr ); |
---|
| 64 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
| 65 | // virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
| 66 | // virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
| 67 | // virtual void visit( AttrExpr *attrExpr ); |
---|
| 68 | // virtual void visit( CommaExpr *commaExpr ); |
---|
| 69 | // virtual void visit( LogicalExpr *logicalExpr ); |
---|
| 70 | // virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
| 71 | virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
| 72 | virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
| 73 | virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; } |
---|
| 74 | virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; } |
---|
| 75 | virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; } |
---|
| 76 | virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; } |
---|
| 77 | |
---|
| 78 | bool isConstExpr; |
---|
| 79 | }; |
---|
| 80 | |
---|
| 81 | bool isConstExpr( Initializer * init ) { |
---|
| 82 | if ( init ) { |
---|
| 83 | ConstExprChecker checker; |
---|
| 84 | init->accept( checker ); |
---|
| 85 | return checker.isConstExpr; |
---|
| 86 | } |
---|
| 87 | // for all intents and purposes, no initializer means const expr |
---|
| 88 | return true; |
---|
| 89 | } |
---|
| 90 | |
---|
[03e5d14] | 91 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) { |
---|
| 92 | GlobalFixer fixer( name, inLibrary ); |
---|
[711eee5] | 93 | acceptAll( translationUnit, fixer ); |
---|
[ec79847] | 94 | // don't need to include function if it's empty |
---|
| 95 | if ( fixer.initFunction->get_statements()->get_kids().empty() ) { |
---|
| 96 | delete fixer.initFunction; |
---|
| 97 | } else { |
---|
| 98 | translationUnit.push_back( fixer.initFunction ); |
---|
| 99 | } |
---|
| 100 | if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) { |
---|
| 101 | delete fixer.destroyFunction; |
---|
| 102 | } else { |
---|
| 103 | translationUnit.push_back( fixer.destroyFunction ); |
---|
| 104 | } |
---|
[711eee5] | 105 | } |
---|
| 106 | |
---|
[9e2c1f0] | 107 | std::string globalFunctionName( const std::string & name ) { |
---|
[711eee5] | 108 | // get basename |
---|
| 109 | std::string ret = name.substr( 0, name.find( '.' ) ); |
---|
| 110 | // replace invalid characters with _ |
---|
| 111 | static std::string invalid = "/-"; |
---|
| 112 | replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' ); |
---|
[9e2c1f0] | 113 | return ret; |
---|
[711eee5] | 114 | } |
---|
| 115 | |
---|
[03e5d14] | 116 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) { |
---|
[9e2c1f0] | 117 | std::string fixedName = globalFunctionName( name ); |
---|
| 118 | initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Constructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) ); |
---|
| 119 | |
---|
| 120 | destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Destructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) ); |
---|
[711eee5] | 121 | } |
---|
| 122 | |
---|
| 123 | void GlobalFixer::visit( ObjectDecl *objDecl ) { |
---|
[9e2c1f0] | 124 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids(); |
---|
| 125 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids(); |
---|
[711eee5] | 126 | |
---|
[7b3f66b] | 127 | // if ( objDecl->get_init() == NULL ) return; |
---|
[9e2c1f0] | 128 | if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects |
---|
[4e24610] | 129 | if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable |
---|
[7b3f66b] | 130 | if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return; |
---|
[711eee5] | 131 | // C allows you to initialize objects with constant expressions |
---|
[4e24610] | 132 | // xxx - this is an optimization. Need to first resolve constructors before we decide |
---|
| 133 | // to keep C-style initializer. |
---|
| 134 | // if ( isConstExpr( objDecl->get_init() ) ) return; |
---|
[711eee5] | 135 | |
---|
[7b3f66b] | 136 | if ( ArrayType * at = dynamic_cast< ArrayType * > ( objDecl->get_type() ) ) { |
---|
| 137 | // xxx - initialize each element of the array |
---|
| 138 | } else { |
---|
| 139 | // steal initializer from object and attach it to a new temporary |
---|
| 140 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() ); |
---|
| 141 | objDecl->set_init( NULL ); |
---|
| 142 | initStatements.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
| 143 | |
---|
| 144 | // copy construct objDecl using temporary |
---|
| 145 | UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) ); |
---|
| 146 | init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
| 147 | init->get_args().push_back( new VariableExpr( newObj ) ); |
---|
| 148 | initStatements.push_back( new ExprStmt( noLabels, init ) ); |
---|
| 149 | |
---|
| 150 | // add destructor calls to global destroy function |
---|
| 151 | UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) ); |
---|
| 152 | destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
| 153 | destroyStatements.push_front( new ExprStmt( noLabels, destroy ) ); |
---|
| 154 | } |
---|
[711eee5] | 155 | } |
---|
| 156 | |
---|
[4e24610] | 157 | // only modify global variables |
---|
| 158 | void GlobalFixer::visit( FunctionDecl *functionDecl ) {} |
---|
| 159 | void GlobalFixer::visit( StructDecl *aggregateDecl ) {} |
---|
| 160 | void GlobalFixer::visit( UnionDecl *aggregateDecl ) {} |
---|
| 161 | void GlobalFixer::visit( EnumDecl *aggregateDecl ) {} |
---|
| 162 | void GlobalFixer::visit( TraitDecl *aggregateDecl ) {} |
---|
| 163 | void GlobalFixer::visit( TypeDecl *typeDecl ) {} |
---|
| 164 | |
---|
[711eee5] | 165 | } // namespace InitTweak |
---|
| 166 | |
---|
| 167 | // Local Variables: // |
---|
| 168 | // tab-width: 4 // |
---|
| 169 | // mode: c++ // |
---|
| 170 | // compile-command: "make install" // |
---|
| 171 | // End: // |
---|
| 172 | |
---|