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 : Wed May 04 16:53:12 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 & fileName ); |
---|
33 | |
---|
34 | virtual void visit( ObjectDecl *objDecl ); |
---|
35 | virtual void visit( FunctionDecl *functionDecl ); |
---|
36 | |
---|
37 | UniqueName tempNamer; |
---|
38 | FunctionDecl * initFunction; |
---|
39 | }; |
---|
40 | |
---|
41 | class ConstExprChecker : public Visitor { |
---|
42 | public: |
---|
43 | ConstExprChecker() : isConstExpr( true ) {} |
---|
44 | |
---|
45 | virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; } |
---|
46 | virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; } |
---|
47 | virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; } |
---|
48 | virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } |
---|
49 | virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; } |
---|
50 | virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; } |
---|
51 | virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; } |
---|
52 | virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; } |
---|
53 | virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ } |
---|
54 | // these might be okay? |
---|
55 | // virtual void visit( SizeofExpr *sizeofExpr ); |
---|
56 | // virtual void visit( AlignofExpr *alignofExpr ); |
---|
57 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
58 | // virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
59 | // virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
60 | // virtual void visit( AttrExpr *attrExpr ); |
---|
61 | // virtual void visit( CommaExpr *commaExpr ); |
---|
62 | // virtual void visit( LogicalExpr *logicalExpr ); |
---|
63 | // virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
64 | virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
65 | virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
66 | virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; } |
---|
67 | virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; } |
---|
68 | virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; } |
---|
69 | virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; } |
---|
70 | |
---|
71 | bool isConstExpr; |
---|
72 | }; |
---|
73 | |
---|
74 | bool isConstExpr( Initializer * init ) { |
---|
75 | if ( init ) { |
---|
76 | ConstExprChecker checker; |
---|
77 | init->accept( checker ); |
---|
78 | return checker.isConstExpr; |
---|
79 | } |
---|
80 | // for all intents and purposes, no initializer means const expr |
---|
81 | return true; |
---|
82 | } |
---|
83 | |
---|
84 | void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name ) { |
---|
85 | GlobalFixer fixer( name ); |
---|
86 | acceptAll( translationUnit, fixer ); |
---|
87 | translationUnit.push_back( fixer.initFunction ); |
---|
88 | } |
---|
89 | |
---|
90 | std::string initName( const std::string & name ) { |
---|
91 | // get basename |
---|
92 | std::string ret = name.substr( 0, name.find( '.' ) ); |
---|
93 | // replace invalid characters with _ |
---|
94 | static std::string invalid = "/-"; |
---|
95 | replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' ); |
---|
96 | return "_init_" + ret; |
---|
97 | } |
---|
98 | |
---|
99 | GlobalFixer::GlobalFixer( const std::string & fileName ) : tempNamer( "_global_init" ) { |
---|
100 | initFunction = new FunctionDecl( initName( fileName ), DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false ); |
---|
101 | } |
---|
102 | |
---|
103 | void GlobalFixer::visit( ObjectDecl *objDecl ) { |
---|
104 | std::list< Statement * > & statements = initFunction->get_statements()->get_kids(); |
---|
105 | |
---|
106 | // C allows you to initialize objects with constant expressions |
---|
107 | if ( isConstExpr( objDecl->get_init() ) ) return; |
---|
108 | |
---|
109 | // steal initializer from object and attach it to a new temporary |
---|
110 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() ); |
---|
111 | objDecl->set_init( NULL ); |
---|
112 | statements.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
113 | |
---|
114 | // assign (later: copy construct) objDecl using temporary |
---|
115 | UntypedExpr * init = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
116 | init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
117 | init->get_args().push_back( new VariableExpr( newObj ) ); |
---|
118 | statements.push_back( new ExprStmt( noLabels, init ) ); |
---|
119 | |
---|
120 | // xxx- need to destruct objDecl atexit |
---|
121 | } |
---|
122 | |
---|
123 | void GlobalFixer::visit( FunctionDecl *functionDecl ) { |
---|
124 | // only modify global variables |
---|
125 | } |
---|
126 | } // namespace InitTweak |
---|
127 | |
---|
128 | // Local Variables: // |
---|
129 | // tab-width: 4 // |
---|
130 | // mode: c++ // |
---|
131 | // compile-command: "make install" // |
---|
132 | // End: // |
---|
133 | |
---|