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 : Fri May 06 15:40:48 2016
|
---|
13 | // Update Count : 2
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "FixGlobalInit.h"
|
---|
17 | #include "SynTree/Declaration.h"
|
---|
18 | #include "SynTree/Type.h"
|
---|
19 | #include "SynTree/Expression.h"
|
---|
20 | #include "SynTree/Statement.h"
|
---|
21 | #include "SynTree/Initializer.h"
|
---|
22 | #include "SynTree/Visitor.h"
|
---|
23 | #include <algorithm>
|
---|
24 |
|
---|
25 | namespace InitTweak {
|
---|
26 | namespace {
|
---|
27 | const std::list<Label> noLabels;
|
---|
28 | }
|
---|
29 |
|
---|
30 | class GlobalFixer : public Visitor {
|
---|
31 | public:
|
---|
32 | GlobalFixer( const std::string & name, bool inLibrary );
|
---|
33 |
|
---|
34 | virtual void visit( ObjectDecl *objDecl );
|
---|
35 | virtual void visit( FunctionDecl *functionDecl );
|
---|
36 | virtual void visit( StructDecl *aggregateDecl );
|
---|
37 | virtual void visit( UnionDecl *aggregateDecl );
|
---|
38 | virtual void visit( EnumDecl *aggregateDecl );
|
---|
39 | virtual void visit( TraitDecl *aggregateDecl );
|
---|
40 | virtual void visit( TypeDecl *typeDecl );
|
---|
41 |
|
---|
42 | UniqueName tempNamer;
|
---|
43 | FunctionDecl * initFunction;
|
---|
44 | };
|
---|
45 |
|
---|
46 | class ConstExprChecker : public Visitor {
|
---|
47 | public:
|
---|
48 | ConstExprChecker() : isConstExpr( true ) {}
|
---|
49 |
|
---|
50 | virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; }
|
---|
51 | virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; }
|
---|
52 | virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; }
|
---|
53 | virtual void visit( CastExpr *castExpr ) { isConstExpr = false; }
|
---|
54 | virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; }
|
---|
55 | virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; }
|
---|
56 | virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; }
|
---|
57 | virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; }
|
---|
58 | virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ }
|
---|
59 | // these might be okay?
|
---|
60 | // virtual void visit( SizeofExpr *sizeofExpr );
|
---|
61 | // virtual void visit( AlignofExpr *alignofExpr );
|
---|
62 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr );
|
---|
63 | // virtual void visit( OffsetofExpr *offsetofExpr );
|
---|
64 | // virtual void visit( OffsetPackExpr *offsetPackExpr );
|
---|
65 | // virtual void visit( AttrExpr *attrExpr );
|
---|
66 | // virtual void visit( CommaExpr *commaExpr );
|
---|
67 | // virtual void visit( LogicalExpr *logicalExpr );
|
---|
68 | // virtual void visit( ConditionalExpr *conditionalExpr );
|
---|
69 | virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; }
|
---|
70 | virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; }
|
---|
71 | virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; }
|
---|
72 | virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; }
|
---|
73 | virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; }
|
---|
74 | virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; }
|
---|
75 |
|
---|
76 | bool isConstExpr;
|
---|
77 | };
|
---|
78 |
|
---|
79 | bool isConstExpr( Initializer * init ) {
|
---|
80 | if ( init ) {
|
---|
81 | ConstExprChecker checker;
|
---|
82 | init->accept( checker );
|
---|
83 | return checker.isConstExpr;
|
---|
84 | }
|
---|
85 | // for all intents and purposes, no initializer means const expr
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
|
---|
90 | GlobalFixer fixer( name, inLibrary );
|
---|
91 | acceptAll( translationUnit, fixer );
|
---|
92 | translationUnit.push_back( fixer.initFunction );
|
---|
93 | }
|
---|
94 |
|
---|
95 | std::string initName( const std::string & name ) {
|
---|
96 | // get basename
|
---|
97 | std::string ret = name.substr( 0, name.find( '.' ) );
|
---|
98 | // replace invalid characters with _
|
---|
99 | static std::string invalid = "/-";
|
---|
100 | replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
|
---|
101 | return "_init_" + ret;
|
---|
102 | }
|
---|
103 |
|
---|
104 | GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
|
---|
105 | initFunction = new FunctionDecl( initName( name ), 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 ) );
|
---|
106 | }
|
---|
107 |
|
---|
108 | void GlobalFixer::visit( ObjectDecl *objDecl ) {
|
---|
109 | std::list< Statement * > & statements = initFunction->get_statements()->get_kids();
|
---|
110 |
|
---|
111 | if ( objDecl->get_init() == NULL ) return;
|
---|
112 | if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
|
---|
113 | // C allows you to initialize objects with constant expressions
|
---|
114 | // xxx - this is an optimization. Need to first resolve constructors before we decide
|
---|
115 | // to keep C-style initializer.
|
---|
116 | // if ( isConstExpr( objDecl->get_init() ) ) return;
|
---|
117 |
|
---|
118 | // steal initializer from object and attach it to a new temporary
|
---|
119 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
|
---|
120 | objDecl->set_init( NULL );
|
---|
121 | statements.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
122 |
|
---|
123 | // assign (later: copy construct) objDecl using temporary
|
---|
124 | UntypedExpr * init = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
125 | init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
|
---|
126 | init->get_args().push_back( new VariableExpr( newObj ) );
|
---|
127 | statements.push_back( new ExprStmt( noLabels, init ) );
|
---|
128 |
|
---|
129 | // xxx- need to destruct objDecl atexit
|
---|
130 | }
|
---|
131 |
|
---|
132 | // only modify global variables
|
---|
133 | void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
|
---|
134 | void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
|
---|
135 | void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
|
---|
136 | void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
|
---|
137 | void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
|
---|
138 | void GlobalFixer::visit( TypeDecl *typeDecl ) {}
|
---|
139 |
|
---|
140 | } // namespace InitTweak
|
---|
141 |
|
---|
142 | // Local Variables: //
|
---|
143 | // tab-width: 4 //
|
---|
144 | // mode: c++ //
|
---|
145 | // compile-command: "make install" //
|
---|
146 | // End: //
|
---|
147 |
|
---|