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