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