source: src/InitTweak/FixGlobalInit.cc@ 2edd5502

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 2edd5502 was f1b1e4c, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

can construct global const objects, except with intrinsic constructors

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