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