[51587aa] | 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 | // |
---|
[a0fdbd5] | 7 | // GenInit.cc -- |
---|
[51587aa] | 8 | // |
---|
[cf16f94] | 9 | // Author : Rob Schluntz |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[1e9d87b] | 11 | // Last Modified By : Rob Schluntz |
---|
[7b3f66b] | 12 | // Last Modified On : Fri May 13 11:37:48 2016 |
---|
[d63eeb0] | 13 | // Update Count : 166 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[974906e2] | 16 | #include <stack> |
---|
| 17 | #include <list> |
---|
[a0fdbd5] | 18 | #include "GenInit.h" |
---|
[7b3f66b] | 19 | #include "InitTweak.h" |
---|
[42e2ad7] | 20 | #include "SynTree/Declaration.h" |
---|
| 21 | #include "SynTree/Type.h" |
---|
| 22 | #include "SynTree/Expression.h" |
---|
| 23 | #include "SynTree/Statement.h" |
---|
| 24 | #include "SynTree/Initializer.h" |
---|
| 25 | #include "SynTree/Mutator.h" |
---|
[620cb95] | 26 | #include "SymTab/Autogen.h" |
---|
[1e9d87b] | 27 | #include "GenPoly/PolyMutator.h" |
---|
[5f98ce5] | 28 | #include "GenPoly/DeclMutator.h" |
---|
[42e2ad7] | 29 | |
---|
| 30 | namespace InitTweak { |
---|
[a08ba92] | 31 | namespace { |
---|
| 32 | const std::list<Label> noLabels; |
---|
[5b40f30] | 33 | const std::list<Expression *> noDesignators; |
---|
[a08ba92] | 34 | } |
---|
[1e9d87b] | 35 | |
---|
[a0fdbd5] | 36 | class ReturnFixer : public GenPoly::PolyMutator { |
---|
[cf16f94] | 37 | public: |
---|
[a0fdbd5] | 38 | /// consistently allocates a temporary variable for the return value |
---|
| 39 | /// of a function so that anything which the resolver decides can be constructed |
---|
[02c7d04] | 40 | /// into the return type of a function can be returned. |
---|
[a0fdbd5] | 41 | static void makeReturnTemp( std::list< Declaration * > &translationUnit ); |
---|
[02c7d04] | 42 | |
---|
[a0fdbd5] | 43 | ReturnFixer(); |
---|
[db4ecc5] | 44 | |
---|
[cf16f94] | 45 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); |
---|
| 46 | |
---|
| 47 | virtual Statement * mutate( ReturnStmt * returnStmt ); |
---|
[1e9d87b] | 48 | |
---|
| 49 | protected: |
---|
[cf16f94] | 50 | std::list<DeclarationWithType*> returnVals; |
---|
| 51 | UniqueName tempNamer; |
---|
| 52 | std::string funcName; |
---|
| 53 | }; |
---|
[42e2ad7] | 54 | |
---|
[974906e2] | 55 | class CtorDtor : public GenPoly::PolyMutator { |
---|
| 56 | public: |
---|
[02c7d04] | 57 | /// create constructor and destructor statements for object declarations. |
---|
[5f98ce5] | 58 | /// the actual call statements will be added in after the resolver has run |
---|
| 59 | /// so that the initializer expression is only removed if a constructor is found |
---|
| 60 | /// and the same destructor call is inserted in all of the appropriate locations. |
---|
[02c7d04] | 61 | static void generateCtorDtor( std::list< Declaration * > &translationUnit ); |
---|
[974906e2] | 62 | |
---|
[f1e012b] | 63 | CtorDtor() : inFunction( false ) {} |
---|
| 64 | |
---|
[db4ecc5] | 65 | virtual DeclarationWithType * mutate( ObjectDecl * ); |
---|
[02c7d04] | 66 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); |
---|
[5f98ce5] | 67 | // should not traverse into any of these declarations to find objects |
---|
| 68 | // that need to be constructed or destructed |
---|
| 69 | virtual Declaration* mutate( StructDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 70 | virtual Declaration* mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 71 | virtual Declaration* mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 72 | virtual Declaration* mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 73 | virtual TypeDecl* mutate( TypeDecl *typeDecl ) { return typeDecl; } |
---|
| 74 | virtual Declaration* mutate( TypedefDecl *typeDecl ) { return typeDecl; } |
---|
[974906e2] | 75 | |
---|
[5f98ce5] | 76 | virtual Type * mutate( FunctionType *funcType ) { return funcType; } |
---|
[db4ecc5] | 77 | |
---|
[974906e2] | 78 | protected: |
---|
[f1e012b] | 79 | bool inFunction; |
---|
[974906e2] | 80 | }; |
---|
| 81 | |
---|
[5f98ce5] | 82 | class HoistArrayDimension : public GenPoly::DeclMutator { |
---|
| 83 | public: |
---|
| 84 | typedef GenPoly::DeclMutator Parent; |
---|
| 85 | |
---|
| 86 | /// hoist dimension from array types in object declaration so that it uses a single |
---|
| 87 | /// const variable of type size_t, so that side effecting array dimensions are only |
---|
| 88 | /// computed once. |
---|
| 89 | static void hoistArrayDimension( std::list< Declaration * > & translationUnit ); |
---|
| 90 | |
---|
| 91 | private: |
---|
[40e636a] | 92 | virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ); |
---|
| 93 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); |
---|
[5f98ce5] | 94 | // should not traverse into any of these declarations to find objects |
---|
| 95 | // that need to be constructed or destructed |
---|
| 96 | virtual Declaration* mutate( StructDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 97 | virtual Declaration* mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 98 | virtual Declaration* mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 99 | virtual Declaration* mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 100 | virtual TypeDecl* mutate( TypeDecl *typeDecl ) { return typeDecl; } |
---|
| 101 | virtual Declaration* mutate( TypedefDecl *typeDecl ) { return typeDecl; } |
---|
| 102 | |
---|
| 103 | virtual Type* mutate( FunctionType *funcType ) { return funcType; } |
---|
| 104 | |
---|
| 105 | void hoist( Type * type ); |
---|
| 106 | |
---|
| 107 | DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass; |
---|
[40e636a] | 108 | bool inFunction = false; |
---|
[5f98ce5] | 109 | }; |
---|
| 110 | |
---|
[a0fdbd5] | 111 | void genInit( std::list< Declaration * > & translationUnit ) { |
---|
| 112 | ReturnFixer::makeReturnTemp( translationUnit ); |
---|
[5f98ce5] | 113 | HoistArrayDimension::hoistArrayDimension( translationUnit ); |
---|
[02c7d04] | 114 | CtorDtor::generateCtorDtor( translationUnit ); |
---|
| 115 | } |
---|
| 116 | |
---|
[a0fdbd5] | 117 | void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) { |
---|
| 118 | ReturnFixer fixer; |
---|
| 119 | mutateAll( translationUnit, fixer ); |
---|
[a08ba92] | 120 | } |
---|
[42e2ad7] | 121 | |
---|
[a0fdbd5] | 122 | ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {} |
---|
[cf16f94] | 123 | |
---|
[a0fdbd5] | 124 | Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) { |
---|
[cf16f94] | 125 | // update for multiple return values |
---|
| 126 | assert( returnVals.size() == 0 || returnVals.size() == 1 ); |
---|
| 127 | // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address |
---|
| 128 | // is being returned |
---|
[7eabc25] | 129 | if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) { |
---|
[5b40f30] | 130 | // ensure return value is not destructed by explicitly creating |
---|
| 131 | // an empty SingleInit node wherein maybeConstruct is false |
---|
| 132 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) ); |
---|
[1e9d87b] | 133 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
| 134 | |
---|
[5b40f30] | 135 | // and explicitly create the constructor expression separately |
---|
| 136 | UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) ); |
---|
[7eabc25] | 137 | construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) ); |
---|
[5b40f30] | 138 | construct->get_args().push_back( returnStmt->get_expr() ); |
---|
| 139 | stmtsToAdd.push_back(new ExprStmt(noLabels, construct)); |
---|
[cf16f94] | 140 | |
---|
| 141 | returnStmt->set_expr( new VariableExpr( newObj ) ); |
---|
| 142 | } // if |
---|
| 143 | return returnStmt; |
---|
| 144 | } |
---|
| 145 | |
---|
[a0fdbd5] | 146 | DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) { |
---|
[cf16f94] | 147 | std::list<DeclarationWithType*> oldReturnVals = returnVals; |
---|
| 148 | std::string oldFuncName = funcName; |
---|
[1e9d87b] | 149 | |
---|
[cf16f94] | 150 | FunctionType * type = functionDecl->get_functionType(); |
---|
| 151 | returnVals = type->get_returnVals(); |
---|
| 152 | funcName = functionDecl->get_name(); |
---|
| 153 | DeclarationWithType * decl = Mutator::mutate( functionDecl ); |
---|
| 154 | returnVals = oldReturnVals; |
---|
| 155 | funcName = oldFuncName; |
---|
| 156 | return decl; |
---|
| 157 | } |
---|
[974906e2] | 158 | |
---|
[5f98ce5] | 159 | void HoistArrayDimension::hoistArrayDimension( std::list< Declaration * > & translationUnit ) { |
---|
| 160 | HoistArrayDimension hoister; |
---|
[40e636a] | 161 | hoister.mutateDeclarationList( translationUnit ); |
---|
[5f98ce5] | 162 | } |
---|
| 163 | |
---|
| 164 | DeclarationWithType * HoistArrayDimension::mutate( ObjectDecl * objectDecl ) { |
---|
| 165 | storageclass = objectDecl->get_storageClass(); |
---|
| 166 | DeclarationWithType * temp = Parent::mutate( objectDecl ); |
---|
| 167 | hoist( objectDecl->get_type() ); |
---|
| 168 | storageclass = DeclarationNode::NoStorageClass; |
---|
| 169 | return temp; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void HoistArrayDimension::hoist( Type * type ) { |
---|
[40e636a] | 173 | // if in function, generate const size_t var |
---|
[5f98ce5] | 174 | static UniqueName dimensionName( "_array_dim" ); |
---|
| 175 | if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
[40e636a] | 176 | if ( ! inFunction ) return; |
---|
| 177 | |
---|
[5f98ce5] | 178 | if ( ! arrayType->get_dimension() ) return; // xxx - recursive call to hoist? |
---|
| 179 | |
---|
| 180 | // don't need to hoist dimension if it's a constexpr - only need to if there's potential |
---|
| 181 | // for side effects. |
---|
| 182 | if ( isConstExpr( arrayType->get_dimension() ) ) return; |
---|
| 183 | |
---|
| 184 | ObjectDecl * arrayDimension = new ObjectDecl( dimensionName.newName(), storageclass, LinkageSpec::C, 0, SymTab::SizeType->clone(), new SingleInit( arrayType->get_dimension() ) ); |
---|
| 185 | arrayDimension->get_type()->set_isConst( true ); |
---|
| 186 | |
---|
| 187 | arrayType->set_dimension( new VariableExpr( arrayDimension ) ); |
---|
| 188 | addDeclaration( arrayDimension ); |
---|
| 189 | |
---|
| 190 | hoist( arrayType->get_base() ); |
---|
| 191 | return; |
---|
| 192 | } |
---|
| 193 | } |
---|
[974906e2] | 194 | |
---|
[40e636a] | 195 | DeclarationWithType * HoistArrayDimension::mutate( FunctionDecl *functionDecl ) { |
---|
| 196 | bool oldInFunc = inFunction; |
---|
| 197 | inFunction = true; |
---|
| 198 | DeclarationWithType * decl = Parent::mutate( functionDecl ); |
---|
| 199 | inFunction = oldInFunc; |
---|
| 200 | return decl; |
---|
| 201 | } |
---|
| 202 | |
---|
[02c7d04] | 203 | void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { |
---|
| 204 | CtorDtor ctordtor; |
---|
| 205 | mutateAll( translationUnit, ctordtor ); |
---|
[974906e2] | 206 | } |
---|
| 207 | |
---|
[02c7d04] | 208 | namespace { |
---|
[71f4e4f] | 209 | Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) { |
---|
[02c7d04] | 210 | UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) ); |
---|
| 211 | expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
| 212 | expr->get_args().splice( expr->get_args().end(), args ); |
---|
[71f4e4f] | 213 | return expr; |
---|
[02c7d04] | 214 | } |
---|
[974906e2] | 215 | } |
---|
| 216 | |
---|
[db4ecc5] | 217 | DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) { |
---|
[974906e2] | 218 | // hands off if designated or if @= |
---|
| 219 | if ( tryConstruct( objDecl ) ) { |
---|
[f1e012b] | 220 | if ( inFunction ) { |
---|
[071a31a] | 221 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) { |
---|
| 222 | // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array |
---|
[2be1023] | 223 | // TODO: walk initializers and generate appropriate ctor if element has initializer. |
---|
| 224 | // Initializer could be nested (depends on the depth of the array type on the object) |
---|
| 225 | |
---|
[7b3f66b] | 226 | std::list< Expression * > args = makeInitList( objDecl->get_init() ); |
---|
| 227 | if ( args.empty() ) { |
---|
| 228 | std::list< Statement * > ctor; |
---|
| 229 | std::list< Statement * > dtor; |
---|
| 230 | |
---|
[2be1023] | 231 | SymTab::genImplicitCall( NULL, new VariableExpr( objDecl ), "?{}", back_inserter( ctor ), objDecl ); |
---|
| 232 | SymTab::genImplicitCall( NULL, new VariableExpr( objDecl ), "^?{}", front_inserter( dtor ), objDecl, false ); |
---|
[7b3f66b] | 233 | |
---|
[2be1023] | 234 | // Currently genArrayCall produces a single Statement - a CompoundStmt |
---|
[7b3f66b] | 235 | // which wraps everything that needs to happen. As such, it's technically |
---|
| 236 | // possible to use a Statement ** in the above calls, but this is inherently |
---|
| 237 | // unsafe, so instead we take the slightly less efficient route, but will be |
---|
| 238 | // immediately informed if somehow the above assumption is broken. In this case, |
---|
| 239 | // we could always wrap the list of statements at this point with a CompoundStmt, |
---|
[2be1023] | 240 | // but it seems reasonable at the moment for this to be done by genArrayCall |
---|
[7b3f66b] | 241 | // itself |
---|
[cad355a] | 242 | assert( ctor.size() == 1 && dynamic_cast< ImplicitCtorDtorStmt * >( ctor.front() ) ); |
---|
| 243 | assert( dtor.size() == 1 && dynamic_cast< ImplicitCtorDtorStmt * >( dtor.front() ) ); |
---|
| 244 | objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) ); |
---|
[7b3f66b] | 245 | } else { |
---|
| 246 | // array came with an initializer list: initialize each element |
---|
| 247 | // may have more initializers than elements in the array - need to check at each index that |
---|
| 248 | // we haven't exceeded size. This requires precomputing the size because it might be a side-effecting |
---|
| 249 | // computation. |
---|
[2be1023] | 250 | // may have fewer initializers than elements in the array - need to default construct |
---|
[7b3f66b] | 251 | // remaining elements. |
---|
| 252 | // might be able to merge this with the case above. |
---|
[2be1023] | 253 | |
---|
[7b3f66b] | 254 | } |
---|
[071a31a] | 255 | } else { |
---|
| 256 | // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer |
---|
| 257 | Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) ); |
---|
| 258 | Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() ); |
---|
| 259 | |
---|
| 260 | // need to remember init expression, in case no ctors exist |
---|
| 261 | // if ctor does exist, want to use ctor expression instead of init |
---|
| 262 | // push this decision to the resolver |
---|
[5b2f5bb] | 263 | ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor ); |
---|
| 264 | ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor ); |
---|
[f1b1e4c] | 265 | objDecl->set_init( new ConstructorInit( new ImplicitCtorDtorStmt( ctorStmt ), new ImplicitCtorDtorStmt( dtorStmt ), objDecl->get_init() ) ); |
---|
[071a31a] | 266 | } |
---|
[f1e012b] | 267 | } |
---|
[974906e2] | 268 | } |
---|
[db4ecc5] | 269 | return Mutator::mutate( objDecl ); |
---|
[974906e2] | 270 | } |
---|
| 271 | |
---|
[02c7d04] | 272 | DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) { |
---|
| 273 | // parameters should not be constructed and destructed, so don't mutate FunctionType |
---|
[f1e012b] | 274 | bool oldInFunc = inFunction; |
---|
[02c7d04] | 275 | mutateAll( functionDecl->get_oldDecls(), *this ); |
---|
[f1e012b] | 276 | inFunction = true; |
---|
[02c7d04] | 277 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); |
---|
[f1e012b] | 278 | inFunction = oldInFunc; |
---|
[02c7d04] | 279 | return functionDecl; |
---|
| 280 | } |
---|
[42e2ad7] | 281 | } // namespace InitTweak |
---|
| 282 | |
---|
[51587aa] | 283 | // Local Variables: // |
---|
| 284 | // tab-width: 4 // |
---|
| 285 | // mode: c++ // |
---|
| 286 | // compile-command: "make install" // |
---|
| 287 | // End: // |
---|