| 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 | // Explode.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Rob Schluntz
 | 
|---|
| 10 | // Created On       : Wed Nov 9 13:12:24 2016
 | 
|---|
| 11 | // Last Modified By : Rob Schluntz
 | 
|---|
| 12 | // Last Modified On : Wed Nov 9 13:20:24 2016
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "Explode.h"
 | 
|---|
| 17 | #include "SynTree/Mutator.h"
 | 
|---|
| 18 | 
 | 
|---|
| 19 | namespace Tuples {
 | 
|---|
| 20 |         namespace {
 | 
|---|
| 21 |                 struct AddrExploder : public Mutator {
 | 
|---|
| 22 |                         bool foundUniqueExpr = false;
 | 
|---|
| 23 |                         Expression * applyAddr( Expression * expr, bool first = true ) {
 | 
|---|
| 24 |                                 if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ){
 | 
|---|
| 25 |                                         foundUniqueExpr = true;
 | 
|---|
| 26 |                                         std::list< Expression * > exprs;
 | 
|---|
| 27 |                                         for ( Expression *& expr : tupleExpr->get_exprs() ) {
 | 
|---|
| 28 |                                                 // move & into tuple exprs
 | 
|---|
| 29 |                                                 exprs.push_back( applyAddr( expr, false ) );
 | 
|---|
| 30 |                                         }
 | 
|---|
| 31 |                                         // want the top-level expression to be address-taken, but not nested
 | 
|---|
| 32 |                                         // tuple expressions
 | 
|---|
| 33 |                                         if ( first ) {
 | 
|---|
| 34 |                                                 return new AddressExpr( new TupleExpr( exprs ) );
 | 
|---|
| 35 |                                         } else {
 | 
|---|
| 36 |                                                 return new TupleExpr( exprs );
 | 
|---|
| 37 |                                         }
 | 
|---|
| 38 |                                 }
 | 
|---|
| 39 |                                 // anything else should be address-taken as normal
 | 
|---|
| 40 |                                 return new AddressExpr( expr->clone() );
 | 
|---|
| 41 |                         }
 | 
|---|
| 42 | 
 | 
|---|
| 43 |                         virtual Expression * mutate( UniqueExpr * uniqueExpr ) {
 | 
|---|
| 44 |                                 // move & into unique expr so that the unique expr has type T* rather than
 | 
|---|
| 45 |                                 // type T. In particular, this transformation helps with generating the
 | 
|---|
| 46 |                                 // correct code for address-taken member tuple expressions, since the result
 | 
|---|
| 47 |                                 // should now be a tuple of addresses rather than the address of a tuple.
 | 
|---|
| 48 |                                 // Still, this code is a bit awkward, and could use some improvement.
 | 
|---|
| 49 |                                 if ( dynamic_cast< AddressExpr * > ( uniqueExpr->get_expr() ) ) {
 | 
|---|
| 50 |                                         // this unique expression has already been mutated or otherwise shouldn't be (can't take the address-of an address-of expression)
 | 
|---|
| 51 |                                         return uniqueExpr;
 | 
|---|
| 52 |                                 }
 | 
|---|
| 53 |                                 UniqueExpr * newUniqueExpr = new UniqueExpr( applyAddr( uniqueExpr->get_expr() ), uniqueExpr->get_id() );
 | 
|---|
| 54 |                                 delete uniqueExpr;
 | 
|---|
| 55 |                                 UntypedExpr * deref = UntypedExpr::createDeref( Mutator::mutate( newUniqueExpr ) );
 | 
|---|
| 56 |                                 return deref;
 | 
|---|
| 57 |                         }
 | 
|---|
| 58 | 
 | 
|---|
| 59 |                         virtual Expression * mutate( TupleIndexExpr * tupleExpr ) {
 | 
|---|
| 60 |                                 // tuple index expr needs to be rebuilt to ensure that the type of the
 | 
|---|
| 61 |                                 // field is consistent with the type of the tuple expr, since the field
 | 
|---|
| 62 |                                 // may have changed from type T to T*.
 | 
|---|
| 63 |                                 Expression * expr = tupleExpr->get_tuple()->acceptMutator( *this );
 | 
|---|
| 64 |                                 tupleExpr->set_tuple( nullptr );
 | 
|---|
| 65 |                                 TupleIndexExpr * ret = new TupleIndexExpr( expr, tupleExpr->get_index() );
 | 
|---|
| 66 |                                 delete tupleExpr;
 | 
|---|
| 67 |                                 return ret;
 | 
|---|
| 68 |                         }
 | 
|---|
| 69 |                 };
 | 
|---|
| 70 |         } // namespace
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         Expression * distributeAddr( Expression * expr ) {
 | 
|---|
| 73 |                 AddrExploder addrExploder;
 | 
|---|
| 74 |                 expr = expr->acceptMutator( addrExploder );
 | 
|---|
| 75 |                 if ( ! addrExploder.foundUniqueExpr ) {
 | 
|---|
| 76 |                         expr = new AddressExpr( expr );
 | 
|---|
| 77 |                 }
 | 
|---|
| 78 |                 return expr;
 | 
|---|
| 79 |         }
 | 
|---|
| 80 | } // namespace Tuples
 | 
|---|
| 81 | 
 | 
|---|
| 82 | // Local Variables: //
 | 
|---|
| 83 | // tab-width: 4 //
 | 
|---|
| 84 | // mode: c++ //
 | 
|---|
| 85 | // compile-command: "make install" //
 | 
|---|
| 86 | // End: //
 | 
|---|