| [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 |  | 
|---|
| [1a5ad8c] | 21 | #include "CodeGen/OperatorTable.h" | 
|---|
| [30f9072] | 22 | #include "Common/UniqueName.h"    // for UniqueName | 
|---|
|  | 23 | #include "InitTweak/InitTweak.h"  // for InitExpander | 
|---|
|  | 24 | #include "SynTree/Constant.h"     // for Constant | 
|---|
| [d180746] | 25 | #include "SynTree/Declaration.h"  // for DeclarationWithType, ObjectDecl | 
|---|
|  | 26 | #include "SynTree/Expression.h"   // for NameExpr, ConstantExpr, UntypedExpr... | 
|---|
| [30f9072] | 27 | #include "SynTree/Type.h"         // for Type, ArrayType, Type::Qualifiers | 
|---|
| [972e6f7] | 28 |  | 
|---|
| [d180746] | 29 | class CompoundStmt; | 
|---|
|  | 30 | class Statement; | 
|---|
|  | 31 |  | 
|---|
| [972e6f7] | 32 | namespace SymTab { | 
|---|
| [2be1023] | 33 | /// Generates assignment operators, constructors, and destructor for aggregate types as required | 
|---|
|  | 34 | void autogenerateRoutines( std::list< Declaration * > &translationUnit ); | 
|---|
|  | 35 |  | 
|---|
|  | 36 | /// returns true if obj's name is the empty string and it has a bitfield width | 
|---|
|  | 37 | bool isUnnamedBitfield( ObjectDecl * obj ); | 
|---|
|  | 38 |  | 
|---|
| [5f98ce5] | 39 | /// size_t type - set when size_t typedef is seen. Useful in a few places, | 
|---|
|  | 40 | /// such as in determining array dimension type | 
|---|
|  | 41 | extern Type * SizeType; | 
|---|
|  | 42 |  | 
|---|
| [4fbdfae0] | 43 | /// intrinsic dereference operator for unqualified types - set when *? function is seen in FindSpecialDeclarations. | 
|---|
|  | 44 | /// Useful for creating dereference ApplicationExprs without a full resolver pass. | 
|---|
|  | 45 | extern FunctionDecl * dereferenceOperator; | 
|---|
|  | 46 |  | 
|---|
| [8404321] | 47 | // generate the type of an assignment function for paramType | 
|---|
| [8a6cf7e] | 48 | FunctionType * genAssignType( Type * paramType ); | 
|---|
|  | 49 |  | 
|---|
| [8404321] | 50 | // generate the type of a default constructor or destructor for paramType | 
|---|
|  | 51 | FunctionType * genDefaultType( Type * paramType ); | 
|---|
|  | 52 |  | 
|---|
|  | 53 | // generate the type of a copy constructor for paramType | 
|---|
|  | 54 | FunctionType * genCopyType( Type * paramType ); | 
|---|
|  | 55 |  | 
|---|
| [2be1023] | 56 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. | 
|---|
|  | 57 | template< typename OutputIterator > | 
|---|
| [b95fe40] | 58 | Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast = nullptr, bool forward = true ); | 
|---|
| [2be1023] | 59 |  | 
|---|
|  | 60 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types. | 
|---|
| [f9cebb5] | 61 | /// optionally returns a statement which must be inserted prior to the containing loop, if there is one | 
|---|
| [2be1023] | 62 | template< typename OutputIterator > | 
|---|
| [b95fe40] | 63 | Statement * genScalarCall( InitTweak::InitExpander & srcParam, Expression * dstParam, std::string fname, OutputIterator out, Type * type, Type * addCast = nullptr ) { | 
|---|
| [1a5ad8c] | 64 | bool isReferenceCtorDtor = false; | 
|---|
|  | 65 | if ( dynamic_cast< ReferenceType * >( type ) && CodeGen::isCtorDtor( fname ) ) { | 
|---|
|  | 66 | // reference constructors are essentially application of the rebind operator. | 
|---|
|  | 67 | // apply & to both arguments, do not need a cast | 
|---|
|  | 68 | fname = "?=?"; | 
|---|
|  | 69 | dstParam = new AddressExpr( dstParam ); | 
|---|
| [b95fe40] | 70 | addCast = nullptr; | 
|---|
| [1a5ad8c] | 71 | isReferenceCtorDtor = true; | 
|---|
|  | 72 | } | 
|---|
|  | 73 |  | 
|---|
| [49148d5] | 74 | // want to be able to generate assignment, ctor, and dtor generically, | 
|---|
|  | 75 | // so fname is either ?=?, ?{}, or ^?{} | 
|---|
| [1a5ad8c] | 76 | UntypedExpr * fExpr = new UntypedExpr( new NameExpr( fname ) ); | 
|---|
| [49148d5] | 77 |  | 
|---|
|  | 78 | if ( addCast ) { | 
|---|
|  | 79 | // cast to T& with qualifiers removed, so that qualified objects can be constructed | 
|---|
|  | 80 | // and destructed with the same functions as non-qualified objects. | 
|---|
|  | 81 | // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument | 
|---|
|  | 82 | // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever | 
|---|
|  | 83 | // remove lvalue as a qualifier, this can change to | 
|---|
|  | 84 | //   type->get_qualifiers() = Type::Qualifiers(); | 
|---|
| [b95fe40] | 85 | Type * castType = addCast->clone(); | 
|---|
| [49148d5] | 86 | castType->get_qualifiers() -= Type::Qualifiers( Type::Lvalue | Type::Const | Type::Volatile | Type::Restrict | Type::Atomic ); | 
|---|
|  | 87 | // castType->set_lvalue( true ); // xxx - might not need this | 
|---|
|  | 88 | dstParam = new CastExpr( dstParam, new ReferenceType( Type::Qualifiers(), castType ) ); | 
|---|
|  | 89 | } | 
|---|
| [1a5ad8c] | 90 | fExpr->args.push_back( dstParam ); | 
|---|
| [2be1023] | 91 |  | 
|---|
| [49148d5] | 92 | Statement * listInit = srcParam.buildListInit( fExpr ); | 
|---|
| [39f84a4] | 93 |  | 
|---|
| [1a5ad8c] | 94 | // fetch next set of arguments | 
|---|
|  | 95 | ++srcParam; | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // return if adding reference fails - will happen on default constructor and destructor | 
|---|
|  | 98 | if ( isReferenceCtorDtor && ! srcParam.addReference() ) { | 
|---|
|  | 99 | delete fExpr; | 
|---|
|  | 100 | return listInit; | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
|  | 103 | std::list< Expression * > args = *srcParam; | 
|---|
|  | 104 | fExpr->args.splice( fExpr->args.end(), args ); | 
|---|
| [4d2434a] | 105 |  | 
|---|
| [ba3706f] | 106 | *out++ = new ExprStmt( fExpr ); | 
|---|
| [4d2434a] | 107 |  | 
|---|
| [49148d5] | 108 | srcParam.clearArrayIndices(); | 
|---|
| [f9cebb5] | 109 |  | 
|---|
| [49148d5] | 110 | return listInit; | 
|---|
| [2be1023] | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments. | 
|---|
|  | 114 | /// If forward is true, loop goes from 0 to N-1, else N-1 to 0 | 
|---|
|  | 115 | template< typename OutputIterator > | 
|---|
| [b95fe40] | 116 | void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, Type * addCast = nullptr, bool forward = true ) { | 
|---|
| [2be1023] | 117 | static UniqueName indexName( "_index" ); | 
|---|
|  | 118 |  | 
|---|
|  | 119 | // for a flexible array member nothing is done -- user must define own assignment | 
|---|
| [b95fe40] | 120 | if ( ! array->get_dimension() ) return; | 
|---|
|  | 121 |  | 
|---|
|  | 122 | if ( addCast ) { | 
|---|
|  | 123 | // peel off array layer from cast | 
|---|
|  | 124 | ArrayType * at = strict_dynamic_cast< ArrayType * >( addCast ); | 
|---|
|  | 125 | addCast = at->base; | 
|---|
|  | 126 | } | 
|---|
| [2be1023] | 127 |  | 
|---|
|  | 128 | Expression * begin, * end, * update, * cmp; | 
|---|
|  | 129 | if ( forward ) { | 
|---|
| [579263a] | 130 | // generate: for ( int i = 0; i < N; ++i ) | 
|---|
|  | 131 | begin = new ConstantExpr( Constant::from_int( 0 ) ); | 
|---|
| [1a5ad8c] | 132 | end = array->dimension->clone(); | 
|---|
| [2be1023] | 133 | cmp = new NameExpr( "?<?" ); | 
|---|
|  | 134 | update = new NameExpr( "++?" ); | 
|---|
|  | 135 | } else { | 
|---|
|  | 136 | // generate: for ( int i = N-1; i >= 0; --i ) | 
|---|
|  | 137 | begin = new UntypedExpr( new NameExpr( "?-?" ) ); | 
|---|
| [1a5ad8c] | 138 | ((UntypedExpr*)begin)->args.push_back( array->dimension->clone() ); | 
|---|
|  | 139 | ((UntypedExpr*)begin)->args.push_back( new ConstantExpr( Constant::from_int( 1 ) ) ); | 
|---|
| [579263a] | 140 | end = new ConstantExpr( Constant::from_int( 0 ) ); | 
|---|
| [2be1023] | 141 | cmp = new NameExpr( "?>=?" ); | 
|---|
|  | 142 | update = new NameExpr( "--?" ); | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
| [e4d829b] | 145 | ObjectDecl *index = new ObjectDecl( indexName.newName(), Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), new SingleInit( begin ) ); | 
|---|
| [2be1023] | 146 |  | 
|---|
|  | 147 | UntypedExpr *cond = new UntypedExpr( cmp ); | 
|---|
| [1a5ad8c] | 148 | cond->args.push_back( new VariableExpr( index ) ); | 
|---|
|  | 149 | cond->args.push_back( end ); | 
|---|
| [2be1023] | 150 |  | 
|---|
|  | 151 | UntypedExpr *inc = new UntypedExpr( update ); | 
|---|
| [1a5ad8c] | 152 | inc->args.push_back( new VariableExpr( index ) ); | 
|---|
| [2be1023] | 153 |  | 
|---|
|  | 154 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) ); | 
|---|
| [1a5ad8c] | 155 | dstIndex->args.push_back( dstParam ); | 
|---|
|  | 156 | dstIndex->args.push_back( new VariableExpr( index ) ); | 
|---|
| [2be1023] | 157 | dstParam = dstIndex; | 
|---|
|  | 158 |  | 
|---|
| [39f84a4] | 159 | // srcParam must keep track of the array indices to build the | 
|---|
|  | 160 | // source parameter and/or array list initializer | 
|---|
| [1a5ad8c] | 161 | srcParam.addArrayIndex( new VariableExpr( index ), array->dimension->clone() ); | 
|---|
| [39f84a4] | 162 |  | 
|---|
| [2be1023] | 163 | // for stmt's body, eventually containing call | 
|---|
| [ba3706f] | 164 | CompoundStmt * body = new CompoundStmt(); | 
|---|
| [1a5ad8c] | 165 | Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->kids ), array->base, addCast, forward ); | 
|---|
| [2be1023] | 166 |  | 
|---|
|  | 167 | // block containing for stmt and index variable | 
|---|
|  | 168 | std::list<Statement *> initList; | 
|---|
| [ba3706f] | 169 | CompoundStmt * block = new CompoundStmt(); | 
|---|
|  | 170 | block->push_back( new DeclStmt( index ) ); | 
|---|
| [f9cebb5] | 171 | if ( listInit ) block->get_kids().push_back( listInit ); | 
|---|
| [ba3706f] | 172 | block->push_back( new ForStmt( initList, cond, inc, body ) ); | 
|---|
| [2be1023] | 173 |  | 
|---|
|  | 174 | *out++ = block; | 
|---|
|  | 175 | } | 
|---|
|  | 176 |  | 
|---|
|  | 177 | template< typename OutputIterator > | 
|---|
| [b95fe40] | 178 | Statement * genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, Type * addCast, bool forward ) { | 
|---|
| [2be1023] | 179 | if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { | 
|---|
| [4d2434a] | 180 | genArrayCall( srcParam, dstParam, fname, out, at, addCast, forward ); | 
|---|
| [f9cebb5] | 181 | return 0; | 
|---|
| [2be1023] | 182 | } else { | 
|---|
| [f9cebb5] | 183 | return genScalarCall( srcParam, dstParam, fname, out, type, addCast ); | 
|---|
| [2be1023] | 184 | } | 
|---|
|  | 185 | } | 
|---|
|  | 186 |  | 
|---|
|  | 187 | /// inserts into out a generated call expression to function fname with arguments dstParam | 
|---|
|  | 188 | /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the | 
|---|
|  | 189 | /// object being constructed. The function wraps constructor and destructor calls in an | 
|---|
|  | 190 | /// ImplicitCtorDtorStmt node. | 
|---|
|  | 191 | template< typename OutputIterator > | 
|---|
| [1a5ad8c] | 192 | void genImplicitCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) { | 
|---|
| [2be1023] | 193 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl ); | 
|---|
|  | 194 | assert( obj ); | 
|---|
|  | 195 | // unnamed bit fields are not copied as they cannot be accessed | 
|---|
|  | 196 | if ( isUnnamedBitfield( obj ) ) return; | 
|---|
|  | 197 |  | 
|---|
| [b95fe40] | 198 | Type * addCast = nullptr; | 
|---|
|  | 199 | if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && ! obj->get_bitfieldWidth() ) ) ) { | 
|---|
|  | 200 | assert( dstParam->result ); | 
|---|
|  | 201 | addCast = dstParam->result; | 
|---|
|  | 202 | } | 
|---|
| [2be1023] | 203 | std::list< Statement * > stmts; | 
|---|
| [1a5ad8c] | 204 | genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->type, addCast, forward ); | 
|---|
| [2be1023] | 205 |  | 
|---|
| [6cf27a07] | 206 | // 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 | 
|---|
|  | 207 | assert( stmts.size() <= 1 ); | 
|---|
| [f9cebb5] | 208 | if ( stmts.size() == 1 ) { | 
|---|
|  | 209 | Statement * callStmt = stmts.front(); | 
|---|
|  | 210 | if ( addCast ) { | 
|---|
|  | 211 | // implicitly generated ctor/dtor calls should be wrapped | 
|---|
|  | 212 | // so that later passes are aware they were generated. | 
|---|
|  | 213 | // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield, | 
|---|
|  | 214 | // because this causes the address to be taken at codegen, which is illegal in C. | 
|---|
|  | 215 | callStmt = new ImplicitCtorDtorStmt( callStmt ); | 
|---|
|  | 216 | } | 
|---|
|  | 217 | *out++ = callStmt; | 
|---|
|  | 218 | } | 
|---|
| [2be1023] | 219 | } | 
|---|
| [972e6f7] | 220 | } // namespace SymTab | 
|---|
| [6b0b624] | 221 |  | 
|---|
|  | 222 | // Local Variables: // | 
|---|
|  | 223 | // tab-width: 4 // | 
|---|
|  | 224 | // mode: c++ // | 
|---|
|  | 225 | // compile-command: "make install" // | 
|---|
|  | 226 | // End: // | 
|---|
|  | 227 |  | 
|---|