| 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 | /// Generates assignment operators, constructors, and destructor for aggregate types as required
|
|---|
| 27 | void autogenerateRoutines( std::list< Declaration * > &translationUnit );
|
|---|
| 28 |
|
|---|
| 29 | /// returns true if obj's name is the empty string and it has a bitfield width
|
|---|
| 30 | bool isUnnamedBitfield( ObjectDecl * obj );
|
|---|
| 31 |
|
|---|
| 32 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls.
|
|---|
| 33 | template< typename OutputIterator >
|
|---|
| 34 | void genCall( Expression * srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool forward = true );
|
|---|
| 35 |
|
|---|
| 36 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types.
|
|---|
| 37 | template< typename OutputIterator >
|
|---|
| 38 | void genScalarCall( Expression *srcParam, Expression *dstParam, const std::string & fname, OutputIterator out ) {
|
|---|
| 39 | // want to be able to generate assignment, ctor, and dtor generically,
|
|---|
| 40 | // so fname is either ?=?, ?{}, or ^?{}
|
|---|
| 41 | UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
|
|---|
| 42 |
|
|---|
| 43 | // do something special for unnamed members
|
|---|
| 44 | fExpr->get_args().push_back( new AddressExpr( dstParam ) );
|
|---|
| 45 |
|
|---|
| 46 | if ( srcParam ) {
|
|---|
| 47 | fExpr->get_args().push_back( srcParam );
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | *out++ = new ExprStmt( noLabels, fExpr );
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments.
|
|---|
| 54 | /// If forward is true, loop goes from 0 to N-1, else N-1 to 0
|
|---|
| 55 | template< typename OutputIterator >
|
|---|
| 56 | void genArrayCall( Expression *srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool forward = true ) {
|
|---|
| 57 | static UniqueName indexName( "_index" );
|
|---|
| 58 |
|
|---|
| 59 | // for a flexible array member nothing is done -- user must define own assignment
|
|---|
| 60 | if ( ! array->get_dimension() ) return ;
|
|---|
| 61 |
|
|---|
| 62 | Expression * begin, * end, * update, * cmp;
|
|---|
| 63 | if ( forward ) {
|
|---|
| 64 | // generate: for ( int i = 0; i < 0; ++i )
|
|---|
| 65 | begin = new NameExpr( "0" );
|
|---|
| 66 | end = array->get_dimension()->clone();
|
|---|
| 67 | cmp = new NameExpr( "?<?" );
|
|---|
| 68 | update = new NameExpr( "++?" );
|
|---|
| 69 | } else {
|
|---|
| 70 | // generate: for ( int i = N-1; i >= 0; --i )
|
|---|
| 71 | begin = new UntypedExpr( new NameExpr( "?-?" ) );
|
|---|
| 72 | ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() );
|
|---|
| 73 | ((UntypedExpr*)begin)->get_args().push_back( new NameExpr( "1" ) );
|
|---|
| 74 | end = new NameExpr( "0" );
|
|---|
| 75 | cmp = new NameExpr( "?>=?" );
|
|---|
| 76 | update = new NameExpr( "--?" );
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), NULL );
|
|---|
| 80 |
|
|---|
| 81 | UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
|
|---|
| 82 | init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
|
|---|
| 83 | init->get_args().push_back( begin );
|
|---|
| 84 | index->set_init( new SingleInit( init, std::list<Expression*>() ) );
|
|---|
| 85 |
|
|---|
| 86 | UntypedExpr *cond = new UntypedExpr( cmp );
|
|---|
| 87 | cond->get_args().push_back( new VariableExpr( index ) );
|
|---|
| 88 | cond->get_args().push_back( end );
|
|---|
| 89 |
|
|---|
| 90 | UntypedExpr *inc = new UntypedExpr( update );
|
|---|
| 91 | inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
|
|---|
| 92 |
|
|---|
| 93 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
|
|---|
| 94 | dstIndex->get_args().push_back( dstParam );
|
|---|
| 95 | dstIndex->get_args().push_back( new VariableExpr( index ) );
|
|---|
| 96 | dstParam = dstIndex;
|
|---|
| 97 |
|
|---|
| 98 | // srcParam is NULL for default ctor/dtor
|
|---|
| 99 | if ( srcParam ) {
|
|---|
| 100 | UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
|
|---|
| 101 | srcIndex->get_args().push_back( srcParam );
|
|---|
| 102 | srcIndex->get_args().push_back( new VariableExpr( index ) );
|
|---|
| 103 | srcParam = srcIndex;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // for stmt's body, eventually containing call
|
|---|
| 107 | CompoundStmt * body = new CompoundStmt( noLabels );
|
|---|
| 108 | genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), forward );
|
|---|
| 109 |
|
|---|
| 110 | // block containing for stmt and index variable
|
|---|
| 111 | std::list<Statement *> initList;
|
|---|
| 112 | CompoundStmt * block = new CompoundStmt( noLabels );
|
|---|
| 113 | block->get_kids().push_back( new DeclStmt( noLabels, index ) );
|
|---|
| 114 | block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) );
|
|---|
| 115 |
|
|---|
| 116 | *out++ = block;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | template< typename OutputIterator >
|
|---|
| 120 | void genCall( Expression * srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool forward ) {
|
|---|
| 121 | if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
|
|---|
| 122 | genArrayCall( srcParam, dstParam, fname, out, at, forward );
|
|---|
| 123 | } else {
|
|---|
| 124 | genScalarCall( srcParam, dstParam, fname, out );
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /// inserts into out a generated call expression to function fname with arguments dstParam
|
|---|
| 129 | /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the
|
|---|
| 130 | /// object being constructed. The function wraps constructor and destructor calls in an
|
|---|
| 131 | /// ImplicitCtorDtorStmt node.
|
|---|
| 132 | template< typename OutputIterator >
|
|---|
| 133 | void genImplicitCall( Expression * srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) {
|
|---|
| 134 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl );
|
|---|
| 135 | assert( obj );
|
|---|
| 136 | // unnamed bit fields are not copied as they cannot be accessed
|
|---|
| 137 | if ( isUnnamedBitfield( obj ) ) return;
|
|---|
| 138 |
|
|---|
| 139 | std::list< Statement * > stmts;
|
|---|
| 140 | genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), forward );
|
|---|
| 141 |
|
|---|
| 142 | // currently genCall should produce only one element, but if that changes then the next line needs to be updated to grab the statement which contains the call
|
|---|
| 143 | assert( stmts.size() == 1 );
|
|---|
| 144 | Statement * callStmt = stmts.front();
|
|---|
| 145 | if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && obj->get_bitfieldWidth() == NULL ) ) ) {
|
|---|
| 146 | // implicitly generated ctor/dtor calls should be wrapped
|
|---|
| 147 | // so that later passes are aware they were generated.
|
|---|
| 148 | // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
|
|---|
| 149 | // because this causes the address to be taken at codegen, which is illegal in C.
|
|---|
| 150 | callStmt = new ImplicitCtorDtorStmt( callStmt );
|
|---|
| 151 | }
|
|---|
| 152 | *out++ = callStmt;
|
|---|
| 153 | }
|
|---|
| 154 | } // namespace SymTab
|
|---|
| 155 | #endif // AUTOGEN_H
|
|---|
| 156 |
|
|---|