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 | // AssignExpand.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon May 18 11:24:47 2015 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #include <ctime> |
---|
17 | #include <cstdlib> |
---|
18 | |
---|
19 | #include <list> |
---|
20 | #include <cassert> |
---|
21 | #include <algorithm> |
---|
22 | |
---|
23 | #include "AssignExpand.h" |
---|
24 | |
---|
25 | #include "SynTree/Type.h" |
---|
26 | #include "SynTree/Statement.h" |
---|
27 | #include "SynTree/Expression.h" |
---|
28 | #include "SynTree/Declaration.h" |
---|
29 | |
---|
30 | namespace Tuples { |
---|
31 | AssignExpander::AssignExpander() : temporaryNamer("__tpl") {} |
---|
32 | |
---|
33 | Statement *AssignExpander::mutate( ExprStmt *exprStmt ) { |
---|
34 | replace.clear(); |
---|
35 | extra.clear(); |
---|
36 | extra2.clear(); |
---|
37 | exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), *this ) ); |
---|
38 | |
---|
39 | CompoundStmt *newSt = 0; |
---|
40 | if ( ! extra.empty() ) { |
---|
41 | if ( ! newSt ) |
---|
42 | newSt= new CompoundStmt(std::list<Label>()); |
---|
43 | |
---|
44 | newSt->get_kids().splice(newSt->get_kids().end(), extra); |
---|
45 | } // if |
---|
46 | |
---|
47 | if ( ! extra2.empty() ) { |
---|
48 | if ( ! newSt ) |
---|
49 | newSt= new CompoundStmt(std::list<Label>()); |
---|
50 | |
---|
51 | newSt->get_kids().splice(newSt->get_kids().end(), extra2); |
---|
52 | } |
---|
53 | |
---|
54 | if ( ! replace.empty() ) { |
---|
55 | if ( ! newSt ) |
---|
56 | newSt= new CompoundStmt(std::list<Label>()); |
---|
57 | |
---|
58 | for ( std::list<Expression *>::iterator r = replace.begin(); r != replace.end(); r++ ) |
---|
59 | newSt->get_kids().push_back( new ExprStmt( std::list<Label>(), *r )); |
---|
60 | } |
---|
61 | |
---|
62 | if ( newSt ) return newSt; else return exprStmt; |
---|
63 | } |
---|
64 | |
---|
65 | Expression *AssignExpander::mutate( SolvedTupleExpr *tupleExpr ) { |
---|
66 | /* |
---|
67 | std::list<Expression *> &exprs = tupleExpr->get_exprs(); |
---|
68 | |
---|
69 | if ( tupleExpr->get_type() == SolvedTupleExpr::MASS ) { |
---|
70 | // extract lhs of assignments, assert that rhs is the same, create temporaries |
---|
71 | assert ( ! exprs.empty()); |
---|
72 | ApplicationExpr *ap1 = dynamic_cast< ApplicationExpr * >( exprs.front() ); |
---|
73 | std::list<Expression *> &args = ap1->get_args(); |
---|
74 | assert(args.size() == 2); |
---|
75 | std::list<Type *> &temp_types = args.back()->get_results(); |
---|
76 | assert(temp_types.size() == 1); |
---|
77 | extra.push_back(new DeclStmt( std::list<Label>(), new ObjectDecl(temporaryNamer.newName(), Declaration::Auto, LinkageSpec::C, 0, temp_types.front(), 0 ) )); |
---|
78 | |
---|
79 | for ( std::list<Expression *>::iterator e = exprs.begin(); e != exprs.end(); e++ ) { |
---|
80 | ApplicationExpr *ap = dynamic_cast< ApplicationExpr * >( *e ); |
---|
81 | assert( ap != 0 ); |
---|
82 | replace.push_back(ap); |
---|
83 | } |
---|
84 | |
---|
85 | return tupleExpr; |
---|
86 | } else if ( tupleExpr->get_type() == SolvedTupleExpr::MULTIPLE || |
---|
87 | tupleExpr->get_type() == SolvedTupleExpr::MASS ) */ { |
---|
88 | std::list<Expression *> &comps = tupleExpr->get_exprs(); |
---|
89 | for ( std::list<Expression *>::iterator i = comps.begin(); i != comps.end(); ++i ) { |
---|
90 | std::list<Statement *> decls; |
---|
91 | std::list<Statement *> temps; |
---|
92 | std::list<Statement *> assigns; |
---|
93 | if ( ApplicationExpr *app = dynamic_cast< ApplicationExpr * >(*i) ) { |
---|
94 | assert( app->get_args().size() == 2 ); |
---|
95 | |
---|
96 | Expression *lhsT = app->get_args().front(); |
---|
97 | Expression *rhsT = app->get_args().back(); |
---|
98 | // after the round of type analysis this should be true |
---|
99 | assert( lhsT->get_results().size() == 1 ); |
---|
100 | assert( rhsT->get_results().size() == 1 ); |
---|
101 | // declare temporaries |
---|
102 | ObjectDecl *lhs = new ObjectDecl( temporaryNamer.newName("_lhs_"), Declaration::NoStorageClass, LinkageSpec::Intrinsic, 0, |
---|
103 | lhsT->get_results().front(), 0 ); |
---|
104 | decls.push_back( new DeclStmt( std::list< Label >(), lhs ) ); |
---|
105 | ObjectDecl *rhs = new ObjectDecl( temporaryNamer.newName("_rhs_"), Declaration::NoStorageClass, LinkageSpec::Intrinsic, 0, |
---|
106 | rhsT->get_results().front(), 0); |
---|
107 | decls.push_back( new DeclStmt( std::list< Label >(), rhs )); |
---|
108 | |
---|
109 | |
---|
110 | // create temporary for lhs, assign address |
---|
111 | UntypedExpr *assgnL = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
112 | assgnL->get_args().push_back( new VariableExpr( lhs ) ); |
---|
113 | assgnL->get_args().push_back( lhsT ); |
---|
114 | temps.push_back( new ExprStmt(std::list<Label>(), assgnL) ); |
---|
115 | |
---|
116 | // create temporary for rhs, assign value |
---|
117 | UntypedExpr *assgnR = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
118 | assgnR->get_args().push_back( new VariableExpr( rhs ) ); |
---|
119 | assgnR->get_args().push_back( rhsT ); |
---|
120 | temps.push_back( new ExprStmt(std::list<Label>(), assgnR) ); |
---|
121 | |
---|
122 | // assign rhs to lhs |
---|
123 | UntypedExpr *assgn = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
124 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
125 | deref->get_args().push_back( new VariableExpr( lhs ) ); |
---|
126 | assgn->get_args().push_back( deref ); |
---|
127 | assgn->get_args().push_back( new VariableExpr( rhs ) ); |
---|
128 | assigns.push_back( new ExprStmt(std::list<Label>(), assgn) ); |
---|
129 | } else |
---|
130 | throw CompilerError("Solved Tuple should contain only assignment statements"); |
---|
131 | |
---|
132 | extra.splice( extra.begin(), decls ); |
---|
133 | extra.splice( extra.end(), temps ); |
---|
134 | extra2.splice( extra2.end(), assigns ); |
---|
135 | } // for |
---|
136 | return tupleExpr; |
---|
137 | } |
---|
138 | throw 0; // shouldn't be here |
---|
139 | } |
---|
140 | } // namespace Tuples |
---|
141 | |
---|
142 | // Local Variables: // |
---|
143 | // tab-width: 4 // |
---|
144 | // mode: c++ // |
---|
145 | // compile-command: "make install" // |
---|
146 | // End: // |
---|