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

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 9e2c1f0 was 9e2c1f0, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'global-init' into ctor and add global destroy function to call destructors on global objects

Conflicts:

src/CodeGen/CodeGenerator.cc
src/InitTweak/module.mk
src/Makefile.in
src/SynTree/Declaration.h
src/SynTree/FunctionDecl.cc
src/main.cc

  • Property mode set to 100644
File size: 6.7 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 16:14:13 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
26namespace 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                translationUnit.push_back( fixer.initFunction );
95                translationUnit.push_back( fixer.destroyFunction );
96        }
97
98  std::string globalFunctionName( const std::string & name ) {
99        // get basename
100        std::string ret = name.substr( 0, name.find( '.' ) );
101        // replace invalid characters with _
102                static std::string invalid = "/-";
103        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
104        return ret;
105  }
106
107        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
108                std::string fixedName = globalFunctionName( name );
109                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 ) );
110
111                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 ) );
112        }
113
114        void GlobalFixer::visit( ObjectDecl *objDecl ) {
115                std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
116                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
117
118                if ( objDecl->get_init() == NULL ) return;
119                if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
120                if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
121                // C allows you to initialize objects with constant expressions
122                // xxx - this is an optimization. Need to first resolve constructors before we decide
123                // to keep C-style initializer.
124                // if ( isConstExpr( objDecl->get_init() ) ) return;
125
126                // steal initializer from object and attach it to a new temporary
127                ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
128                objDecl->set_init( NULL );
129                initStatements.push_back( new DeclStmt( noLabels, newObj ) );
130
131                // copy construct objDecl using temporary
132                UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
133                init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
134                init->get_args().push_back( new VariableExpr( newObj ) );
135                initStatements.push_back( new ExprStmt( noLabels, init ) );
136
137                // add destructor calls to global destroy function
138                UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
139                destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
140                destroyStatements.push_front( new ExprStmt( noLabels, destroy ) );
141        }
142
143        // only modify global variables
144        void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
145        void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
146        void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
147        void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
148        void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
149        void GlobalFixer::visit( TypeDecl *typeDecl ) {}
150
151} // namespace InitTweak
152
153// Local Variables: //
154// tab-width: 4 //
155// mode: c++ //
156// compile-command: "make install" //
157// End: //
158
Note: See TracBrowser for help on using the repository browser.