| 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 | // Autogen.h --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Rob Schluntz
 | 
|---|
| 10 | // Created On       : Sun May 17 21:53:34 2015
 | 
|---|
| 11 | // Last Modified By : Rob Schluntz
 | 
|---|
| 12 | // Last Modified On : Tue May 19 16:49:43 2015
 | 
|---|
| 13 | // Update Count     : 1
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #ifndef AUTOGEN_H
 | 
|---|
| 17 | #define AUTOGEN_H
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <string>
 | 
|---|
| 20 | #include "SynTree/Statement.h"
 | 
|---|
| 21 | #include "SynTree/Expression.h"
 | 
|---|
| 22 | #include "SynTree/Declaration.h"
 | 
|---|
| 23 | #include "SynTree/Initializer.h"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | namespace SymTab {
 | 
|---|
| 26 |   static const std::list< std::string > noLabels;
 | 
|---|
| 27 | 
 | 
|---|
| 28 |   /// Generates assignment operators, constructors, and destructor for aggregate types as required
 | 
|---|
| 29 |   void autogenerateRoutines( std::list< Declaration * > &translationUnit );
 | 
|---|
| 30 | 
 | 
|---|
| 31 |   // originally makeArrayAssignment - changed to Function because it is now used for ctors and dtors as well
 | 
|---|
| 32 |   // admittedly not a great name change. This used to live in Validate.cc, but has been moved so it can be reused elsewhere
 | 
|---|
| 33 | 
 | 
|---|
| 34 |   /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
 | 
|---|
| 35 |   /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
 | 
|---|
| 36 |   template< typename OutputIterator >
 | 
|---|
| 37 |   void makeArrayFunction( Expression *srcParam, Expression *dstParam, ArrayType *array, std::string fname, OutputIterator out, bool forward = true ) {
 | 
|---|
| 38 |     static UniqueName indexName( "_index" );
 | 
|---|
| 39 | 
 | 
|---|
| 40 |     // for a flexible array member nothing is done -- user must define own assignment
 | 
|---|
| 41 |     if ( ! array->get_dimension() ) return;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |     Expression * begin, * end, * update, * cmp;
 | 
|---|
| 44 |     if ( forward ) {
 | 
|---|
| 45 |       // generate: for ( int i = 0; i < 0; ++i )
 | 
|---|
| 46 |       begin = new NameExpr( "0" );
 | 
|---|
| 47 |       end = array->get_dimension()->clone();
 | 
|---|
| 48 |       cmp = new NameExpr( "?<?" );
 | 
|---|
| 49 |       update = new NameExpr( "++?" );
 | 
|---|
| 50 |     } else {
 | 
|---|
| 51 |       // generate: for ( int i = N-1; i >= 0; --i )
 | 
|---|
| 52 |       begin = new UntypedExpr( new NameExpr( "?-?" ) );
 | 
|---|
| 53 |       ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
 | 
|---|
| 54 |       ((UntypedExpr*)begin)->get_args().push_back( new NameExpr( "1" ) );
 | 
|---|
| 55 |       end = new NameExpr( "0" );
 | 
|---|
| 56 |       cmp = new NameExpr( "?>=?" );
 | 
|---|
| 57 |       update = new NameExpr( "--?" );
 | 
|---|
| 58 |     }
 | 
|---|
| 59 | 
 | 
|---|
| 60 |     ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), NULL );
 | 
|---|
| 61 | 
 | 
|---|
| 62 |     UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
 | 
|---|
| 63 |     init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
 | 
|---|
| 64 |     init->get_args().push_back( begin );
 | 
|---|
| 65 |     index->set_init( new SingleInit( init, std::list<Expression*>() ) );
 | 
|---|
| 66 | 
 | 
|---|
| 67 |     UntypedExpr *cond = new UntypedExpr( cmp );
 | 
|---|
| 68 |     cond->get_args().push_back( new VariableExpr( index ) );
 | 
|---|
| 69 |     cond->get_args().push_back( end );
 | 
|---|
| 70 | 
 | 
|---|
| 71 |     UntypedExpr *inc = new UntypedExpr( update );
 | 
|---|
| 72 |     inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
 | 
|---|
| 73 | 
 | 
|---|
| 74 |     // want to be able to generate assignment, ctor, and dtor generically,
 | 
|---|
| 75 |     // so fname is either ?=?, ?{}, or ^?{}
 | 
|---|
| 76 |     UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
 | 
|---|
| 77 | 
 | 
|---|
| 78 |     UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );
 | 
|---|
| 79 |     dstIndex->get_args().push_back( dstParam );
 | 
|---|
| 80 |     dstIndex->get_args().push_back( new VariableExpr( index ) );
 | 
|---|
| 81 |     fExpr->get_args().push_back( dstIndex );
 | 
|---|
| 82 | 
 | 
|---|
| 83 |     // srcParam is NULL for default ctor/dtor
 | 
|---|
| 84 |     if ( srcParam ) {
 | 
|---|
| 85 |       UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
 | 
|---|
| 86 |       srcIndex->get_args().push_back( srcParam );
 | 
|---|
| 87 |       srcIndex->get_args().push_back( new VariableExpr( index ) );
 | 
|---|
| 88 |       fExpr->get_args().push_back( srcIndex );
 | 
|---|
| 89 |     }
 | 
|---|
| 90 | 
 | 
|---|
| 91 |     std::list<Statement *> initList;
 | 
|---|
| 92 |     CompoundStmt * block = new CompoundStmt( noLabels );
 | 
|---|
| 93 |     block->get_kids().push_back( new DeclStmt( noLabels, index ) );
 | 
|---|
| 94 |     block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, new ExprStmt( noLabels, fExpr ) ) );
 | 
|---|
| 95 |     *out++ = block;
 | 
|---|
| 96 |   }
 | 
|---|
| 97 | } // namespace SymTab
 | 
|---|
| 98 | #endif // AUTOGEN_H
 | 
|---|
| 99 | 
 | 
|---|