| [972e6f7] | 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
 | 
|---|
| [c0aa336] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [6b0b624] | 12 | // Last Modified On : Sat Jul 22 09:50:25 2017
 | 
|---|
 | 13 | // Update Count     : 15
 | 
|---|
| [972e6f7] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [6b0b624] | 16 | #pragma once
 | 
|---|
| [972e6f7] | 17 | 
 | 
|---|
| [30f9072] | 18 | #include <cassert>                // for assert
 | 
|---|
| [d180746] | 19 | #include <string>                 // for string
 | 
|---|
| [30f9072] | 20 | 
 | 
|---|
 | 21 | #include "Common/UniqueName.h"    // for UniqueName
 | 
|---|
 | 22 | #include "InitTweak/InitTweak.h"  // for InitExpander
 | 
|---|
 | 23 | #include "SynTree/Constant.h"     // for Constant
 | 
|---|
| [d180746] | 24 | #include "SynTree/Declaration.h"  // for DeclarationWithType, ObjectDecl
 | 
|---|
 | 25 | #include "SynTree/Expression.h"   // for NameExpr, ConstantExpr, UntypedExpr...
 | 
|---|
| [30f9072] | 26 | #include "SynTree/Type.h"         // for Type, ArrayType, Type::Qualifiers
 | 
|---|
| [972e6f7] | 27 | 
 | 
|---|
| [d180746] | 28 | class CompoundStmt;
 | 
|---|
 | 29 | class Statement;
 | 
|---|
 | 30 | 
 | 
|---|
| [972e6f7] | 31 | namespace SymTab {
 | 
|---|
| [2be1023] | 32 |         /// Generates assignment operators, constructors, and destructor for aggregate types as required
 | 
|---|
 | 33 |         void autogenerateRoutines( std::list< Declaration * > &translationUnit );
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |         /// returns true if obj's name is the empty string and it has a bitfield width
 | 
|---|
 | 36 |         bool isUnnamedBitfield( ObjectDecl * obj );
 | 
|---|
 | 37 | 
 | 
|---|
| [5f98ce5] | 38 |         /// size_t type - set when size_t typedef is seen. Useful in a few places,
 | 
|---|
 | 39 |         /// such as in determining array dimension type
 | 
|---|
 | 40 |         extern Type * SizeType;
 | 
|---|
 | 41 | 
 | 
|---|
| [2be1023] | 42 |         /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
 | 
|---|
 | 43 |         template< typename OutputIterator >
 | 
|---|
| [f9cebb5] | 44 |         Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false, bool forward = true );
 | 
|---|
| [2be1023] | 45 | 
 | 
|---|
 | 46 |         /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
 | 
|---|
| [f9cebb5] | 47 |         /// optionally returns a statement which must be inserted prior to the containing loop, if there is one
 | 
|---|
| [2be1023] | 48 |         template< typename OutputIterator >
 | 
|---|
| [f9cebb5] | 49 |         Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast = false ) {
 | 
|---|
| [d56e5bc] | 50 |         // want to be able to generate assignment, ctor, and dtor generically,
 | 
|---|
 | 51 |         // so fname is either ?=?, ?{}, or ^?{}
 | 
|---|
 | 52 |         UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 |         // do something special for unnamed members
 | 
|---|
 | 55 |         dstParam = new AddressExpr( dstParam );
 | 
|---|
 | 56 |         if ( addCast ) {
 | 
|---|
| [579263a] | 57 |                 // cast to T* with qualifiers removed, so that qualified objects can be constructed
 | 
|---|
 | 58 |                 // and destructed with the same functions as non-qualified objects.
 | 
|---|
 | 59 |                 // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
 | 
|---|
 | 60 |                 // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
 | 
|---|
 | 61 |                 // remove lvalue as a qualifier, this can change to
 | 
|---|
 | 62 |                 //   type->get_qualifiers() = Type::Qualifiers();
 | 
|---|
 | 63 |                 assert( type );
 | 
|---|
 | 64 |                 Type * castType = type->clone();
 | 
|---|
 | 65 |                 castType->get_qualifiers() -= Type::Qualifiers( Type::Const | Type::Volatile | Type::Restrict | Type::Atomic );
 | 
|---|
 | 66 |                 castType->set_lvalue( true ); // xxx - might not need this
 | 
|---|
 | 67 |                 dstParam = new CastExpr( dstParam, new PointerType( Type::Qualifiers(), castType ) );
 | 
|---|
| [d56e5bc] | 68 |         }
 | 
|---|
 | 69 |         fExpr->get_args().push_back( dstParam );
 | 
|---|
| [2be1023] | 70 | 
 | 
|---|
| [d56e5bc] | 71 |         Statement * listInit = srcParam.buildListInit( fExpr );
 | 
|---|
| [39f84a4] | 72 | 
 | 
|---|
| [d56e5bc] | 73 |         std::list< Expression * > args = *++srcParam;
 | 
|---|
 | 74 |         fExpr->get_args().splice( fExpr->get_args().end(), args );
 | 
|---|
| [4d2434a] | 75 | 
 | 
|---|
| [d56e5bc] | 76 |         *out++ = new ExprStmt( noLabels, fExpr );
 | 
|---|
| [4d2434a] | 77 | 
 | 
|---|
| [d56e5bc] | 78 |         srcParam.clearArrayIndices();
 | 
|---|
| [f9cebb5] | 79 | 
 | 
|---|
| [d56e5bc] | 80 |         return listInit;
 | 
|---|
| [2be1023] | 81 |         }
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 |         /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
 | 
|---|
 | 84 |         /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
 | 
|---|
 | 85 |         template< typename OutputIterator >
 | 
|---|
| [4d2434a] | 86 |         void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool addCast = false, bool forward = true ) {
 | 
|---|
| [2be1023] | 87 |                 static UniqueName indexName( "_index" );
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |                 // for a flexible array member nothing is done -- user must define own assignment
 | 
|---|
 | 90 |                 if ( ! array->get_dimension() ) return ;
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |                 Expression * begin, * end, * update, * cmp;
 | 
|---|
 | 93 |                 if ( forward ) {
 | 
|---|
| [579263a] | 94 |                         // generate: for ( int i = 0; i < N; ++i )
 | 
|---|
 | 95 |                         begin = new ConstantExpr( Constant::from_int( 0 ) );
 | 
|---|
| [2be1023] | 96 |                         end = array->get_dimension()->clone();
 | 
|---|
 | 97 |                         cmp = new NameExpr( "?<?" );
 | 
|---|
 | 98 |                         update = new NameExpr( "++?" );
 | 
|---|
 | 99 |                 } else {
 | 
|---|
 | 100 |                         // generate: for ( int i = N-1; i >= 0; --i )
 | 
|---|
 | 101 |                         begin = new UntypedExpr( new NameExpr( "?-?" ) );
 | 
|---|
 | 102 |                         ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
 | 
|---|
| [579263a] | 103 |                         ((UntypedExpr*)begin)->get_args().push_back( new ConstantExpr( Constant::from_int( 1 ) ) );
 | 
|---|
 | 104 |                         end = new ConstantExpr( Constant::from_int( 0 ) );
 | 
|---|
| [2be1023] | 105 |                         cmp = new NameExpr( "?>=?" );
 | 
|---|
 | 106 |                         update = new NameExpr( "--?" );
 | 
|---|
 | 107 |                 }
 | 
|---|
 | 108 | 
 | 
|---|
| [e4d829b] | 109 |                 ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) );
 | 
|---|
| [2be1023] | 110 | 
 | 
|---|
 | 111 |                 UntypedExpr *cond = new UntypedExpr( cmp );
 | 
|---|
 | 112 |                 cond->get_args().push_back( new VariableExpr( index ) );
 | 
|---|
 | 113 |                 cond->get_args().push_back( end );
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |                 UntypedExpr *inc = new UntypedExpr( update );
 | 
|---|
 | 116 |                 inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |                 UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
 | 
|---|
 | 119 |                 dstIndex->get_args().push_back( dstParam );
 | 
|---|
 | 120 |                 dstIndex->get_args().push_back( new VariableExpr( index ) );
 | 
|---|
 | 121 |                 dstParam = dstIndex;
 | 
|---|
 | 122 | 
 | 
|---|
| [39f84a4] | 123 |                 // srcParam must keep track of the array indices to build the
 | 
|---|
 | 124 |                 // source parameter and/or array list initializer
 | 
|---|
 | 125 |                 srcParam.addArrayIndex( new VariableExpr( index ), array->get_dimension()->clone() );
 | 
|---|
 | 126 | 
 | 
|---|
| [2be1023] | 127 |                 // for stmt's body, eventually containing call
 | 
|---|
 | 128 |                 CompoundStmt * body = new CompoundStmt( noLabels );
 | 
|---|
| [f9cebb5] | 129 |                 Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), addCast, forward );
 | 
|---|
| [2be1023] | 130 | 
 | 
|---|
 | 131 |                 // block containing for stmt and index variable
 | 
|---|
 | 132 |                 std::list<Statement *> initList;
 | 
|---|
 | 133 |                 CompoundStmt * block = new CompoundStmt( noLabels );
 | 
|---|
 | 134 |                 block->get_kids().push_back( new DeclStmt( noLabels, index ) );
 | 
|---|
| [f9cebb5] | 135 |                 if ( listInit ) block->get_kids().push_back( listInit );
 | 
|---|
| [2be1023] | 136 |                 block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 |                 *out++ = block;
 | 
|---|
 | 139 |         }
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 |         template< typename OutputIterator >
 | 
|---|
| [f9cebb5] | 142 |         Statement * genCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool addCast, bool forward ) {
 | 
|---|
| [2be1023] | 143 |                 if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
 | 
|---|
| [4d2434a] | 144 |                         genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward );
 | 
|---|
| [f9cebb5] | 145 |                         return 0;
 | 
|---|
| [2be1023] | 146 |                 } else {
 | 
|---|
| [f9cebb5] | 147 |                         return genScalarCall( srcParam, dstParam, fname, out, type, addCast );
 | 
|---|
| [2be1023] | 148 |                 }
 | 
|---|
 | 149 |         }
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 |         /// inserts into out a generated call expression to function fname with arguments dstParam
 | 
|---|
 | 152 |         /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
 | 
|---|
 | 153 |         /// object being constructed. The function wraps constructor and destructor calls in an
 | 
|---|
 | 154 |         /// ImplicitCtorDtorStmt node.
 | 
|---|
 | 155 |         template< typename OutputIterator >
 | 
|---|
| [39f84a4] | 156 |         void genImplicitCall( InitTweak::InitExpander &  srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
 | 
|---|
| [2be1023] | 157 |                 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
 | 
|---|
 | 158 |                 assert( obj );
 | 
|---|
 | 159 |                 // unnamed bit fields are not copied as they cannot be accessed
 | 
|---|
 | 160 |                 if ( isUnnamedBitfield( obj ) ) return;
 | 
|---|
 | 161 | 
 | 
|---|
| [30f9072] | 162 |                 bool addCast = (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) );
 | 
|---|
| [2be1023] | 163 |                 std::list< Statement * > stmts;
 | 
|---|
| [4d2434a] | 164 |                 genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), addCast, forward );
 | 
|---|
| [2be1023] | 165 | 
 | 
|---|
| [6cf27a07] | 166 |                 // currently genCall should produce at most one element, but if that changes then the next line needs to be updated to grab the statement which contains the call
 | 
|---|
 | 167 |                 assert( stmts.size() <= 1 );
 | 
|---|
| [f9cebb5] | 168 |                 if ( stmts.size() == 1 ) {
 | 
|---|
 | 169 |                         Statement * callStmt = stmts.front();
 | 
|---|
 | 170 |                         if ( addCast ) {
 | 
|---|
 | 171 |                                 // implicitly generated ctor/dtor calls should be wrapped
 | 
|---|
 | 172 |                                 // so that later passes are aware they were generated.
 | 
|---|
 | 173 |                                 // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
 | 
|---|
 | 174 |                                 // because this causes the address to be taken at codegen, which is illegal in C.
 | 
|---|
 | 175 |                                 callStmt = new ImplicitCtorDtorStmt( callStmt );
 | 
|---|
 | 176 |                         }
 | 
|---|
 | 177 |                         *out++ = callStmt;
 | 
|---|
 | 178 |                 }
 | 
|---|
| [2be1023] | 179 |         }
 | 
|---|
| [972e6f7] | 180 | } // namespace SymTab
 | 
|---|
| [6b0b624] | 181 | 
 | 
|---|
 | 182 | // Local Variables: //
 | 
|---|
 | 183 | // tab-width: 4 //
 | 
|---|
 | 184 | // mode: c++ //
 | 
|---|
 | 185 | // compile-command: "make install" //
 | 
|---|
 | 186 | // End: //
 | 
|---|
 | 187 | 
 | 
|---|