source: src/InitTweak/FixGlobalInit.cc @ 9a92216

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 9a92216 was 4e24610, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add constructor attribute to global initializer function, don't try to fix const globals, add Constructor/Destructor? attributes to FunctionDecl?

  • Property mode set to 100644
File size: 5.9 KB
Line 
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 13:51:00 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
25namespace InitTweak {
26        namespace {
27                const std::list<Label> noLabels;
28        }
29
30        class GlobalFixer : public Visitor {
31          public:
32                GlobalFixer();
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                CompoundStmt * block;
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 ) {
90                GlobalFixer fixer;
91                acceptAll( translationUnit, fixer );
92                // attribute only appears on the forward declaration, so need to make two function decls:
93                // one with the body, one with the attribute.
94                FunctionDecl * initFunction = new FunctionDecl( initName( name ), DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), 0, false, false );
95                FunctionDecl * forward = initFunction->clone();
96                forward->set_attribute( FunctionDecl::Constructor );
97                initFunction->set_statements( fixer.block );
98                translationUnit.push_back( forward );
99                translationUnit.push_back( initFunction );
100        }
101
102  std::string initName( const std::string & name ) {
103        // get basename
104        std::string ret = name.substr( 0, name.find( '.' ) );
105        // replace invalid characters with _
106                static std::string invalid = "/-";
107        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
108        return "_init_" + ret;
109  }
110
111        GlobalFixer::GlobalFixer() : tempNamer( "_global_init" ), block( new CompoundStmt( noLabels ) ) {
112        }
113
114        void GlobalFixer::visit( ObjectDecl *objDecl ) {
115                std::list< Statement * > & statements = block->get_kids();
116
117                if ( objDecl->get_init() == NULL ) return;
118                if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
119                // C allows you to initialize objects with constant expressions
120                // xxx - this is an optimization. Need to first resolve constructors before we decide
121                // to keep C-style initializer.
122                // if ( isConstExpr( objDecl->get_init() ) ) return;
123
124                // steal initializer from object and attach it to a new temporary
125                ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
126                objDecl->set_init( NULL );
127                statements.push_back( new DeclStmt( noLabels, newObj ) );
128
129                // assign (later: copy construct) objDecl using temporary
130                UntypedExpr * init = new UntypedExpr( new NameExpr( "?=?" ) );
131                init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
132                init->get_args().push_back( new VariableExpr( newObj ) );
133                statements.push_back( new ExprStmt( noLabels, init ) );
134
135                // xxx- need to destruct objDecl atexit
136        }
137
138        // only modify global variables
139        void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
140        void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
141        void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
142        void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
143        void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
144        void GlobalFixer::visit( TypeDecl *typeDecl ) {}
145
146} // namespace InitTweak
147
148// Local Variables: //
149// tab-width: 4 //
150// mode: c++ //
151// compile-command: "make install" //
152// End: //
153
Note: See TracBrowser for help on using the repository browser.