| 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
|
|---|
| 12 | // Last Modified On : Mon May 09 11:44:29 2016
|
|---|
| 13 | // Update Count : 2
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "FixGlobalInit.h"
|
|---|
| 17 | #include "GenInit.h"
|
|---|
| 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:
|
|---|
| 33 | GlobalFixer( const std::string & name, bool inLibrary );
|
|---|
| 34 |
|
|---|
| 35 | virtual void visit( ObjectDecl *objDecl );
|
|---|
| 36 | virtual void visit( FunctionDecl *functionDecl );
|
|---|
| 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 );
|
|---|
| 42 |
|
|---|
| 43 | UniqueName tempNamer;
|
|---|
| 44 | FunctionDecl * initFunction;
|
|---|
| 45 | FunctionDecl * destroyFunction;
|
|---|
| 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 |
|
|---|
| 91 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
|
|---|
| 92 | GlobalFixer fixer( name, inLibrary );
|
|---|
| 93 | acceptAll( translationUnit, fixer );
|
|---|
| 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 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | std::string globalFunctionName( const std::string & name ) {
|
|---|
| 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; }, '_' );
|
|---|
| 113 | return ret;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
|
|---|
| 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 ) );
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void GlobalFixer::visit( ObjectDecl *objDecl ) {
|
|---|
| 124 | std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
|
|---|
| 125 | std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
|
|---|
| 126 |
|
|---|
| 127 | if ( objDecl->get_init() == NULL ) return;
|
|---|
| 128 | if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
|
|---|
| 129 | if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
|
|---|
| 130 | // C allows you to initialize objects with constant expressions
|
|---|
| 131 | // xxx - this is an optimization. Need to first resolve constructors before we decide
|
|---|
| 132 | // to keep C-style initializer.
|
|---|
| 133 | // if ( isConstExpr( objDecl->get_init() ) ) return;
|
|---|
| 134 |
|
|---|
| 135 | // steal initializer from object and attach it to a new temporary
|
|---|
| 136 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
|
|---|
| 137 | objDecl->set_init( NULL );
|
|---|
| 138 | initStatements.push_back( new DeclStmt( noLabels, newObj ) );
|
|---|
| 139 |
|
|---|
| 140 | // copy construct objDecl using temporary
|
|---|
| 141 | UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
|
|---|
| 142 | init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
|
|---|
| 143 | init->get_args().push_back( new VariableExpr( newObj ) );
|
|---|
| 144 | initStatements.push_back( new ExprStmt( noLabels, init ) );
|
|---|
| 145 |
|
|---|
| 146 | // add destructor calls to global destroy function
|
|---|
| 147 | UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
|
|---|
| 148 | destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
|
|---|
| 149 | destroyStatements.push_front( new ExprStmt( noLabels, destroy ) );
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | // only modify global variables
|
|---|
| 153 | void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
|
|---|
| 154 | void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
|
|---|
| 155 | void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
|
|---|
| 156 | void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
|
|---|
| 157 | void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
|
|---|
| 158 | void GlobalFixer::visit( TypeDecl *typeDecl ) {}
|
|---|
| 159 |
|
|---|
| 160 | } // namespace InitTweak
|
|---|
| 161 |
|
|---|
| 162 | // Local Variables: //
|
|---|
| 163 | // tab-width: 4 //
|
|---|
| 164 | // mode: c++ //
|
|---|
| 165 | // compile-command: "make install" //
|
|---|
| 166 | // End: //
|
|---|
| 167 |
|
|---|