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 | // FunctionChecker.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 : Sat Jun 13 08:17:19 2015 |
---|
13 | // Update Count : 4 |
---|
14 | // |
---|
15 | |
---|
16 | #include "FunctionChecker.h" |
---|
17 | #include "FunctionFixer.h" |
---|
18 | #include "Common/SemanticError.h" |
---|
19 | |
---|
20 | #include <algorithm> |
---|
21 | #include <iostream> |
---|
22 | #include <cassert> |
---|
23 | |
---|
24 | namespace Tuples { |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | void checkFunctions( std::list< Declaration * > translationUnit ) { |
---|
28 | FunctionChecker fchk( true ); |
---|
29 | TupleDistrib td; |
---|
30 | FunctionFixer ff; |
---|
31 | |
---|
32 | mutateAll( translationUnit , fchk ); |
---|
33 | mutateAll( translationUnit , ff ); |
---|
34 | mutateAll( translationUnit , td ); |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | FunctionChecker::FunctionChecker( bool _topLevel, UniqueName *_nameGen ) : topLevel( _topLevel ), nameGen( _nameGen ) { |
---|
39 | if ( topLevel ) { |
---|
40 | assert( ! nameGen ); |
---|
41 | nameGen = new UniqueName("_MVR_"); |
---|
42 | } else |
---|
43 | assert( nameGen ); |
---|
44 | } |
---|
45 | |
---|
46 | FunctionChecker::~FunctionChecker() { |
---|
47 | if ( topLevel ) { |
---|
48 | delete nameGen; |
---|
49 | nameGen = 0; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | Statement* FunctionChecker::mutate( ExprStmt *exprStmt ) { |
---|
54 | exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), *this ) ); |
---|
55 | if ( ! tempExpr.empty() ) { |
---|
56 | assert ( ! temporaries.empty() ); |
---|
57 | CompoundStmt *newBlock = new CompoundStmt( std::list< Label >() ); |
---|
58 | // declarations |
---|
59 | for ( std::list< ObjectDecl *>::iterator d = temporaries.begin(); d != temporaries.end(); ++d ) |
---|
60 | newBlock->get_kids().push_back( new DeclStmt( std::list<Label>(), *d ) ); |
---|
61 | // new expression statements |
---|
62 | for ( std::list< Expression *>::iterator e = tempExpr.begin(); e != tempExpr.end(); ++e ) |
---|
63 | newBlock->get_kids().push_back( new ExprStmt( std::list<Label>(), *e ) ); |
---|
64 | |
---|
65 | newBlock->get_kids().push_back( exprStmt ); |
---|
66 | return newBlock; |
---|
67 | } else |
---|
68 | return exprStmt; |
---|
69 | } |
---|
70 | |
---|
71 | Expression* FunctionChecker::mutate( ApplicationExpr *applicationExpr ) { |
---|
72 | if ( topLevel ) |
---|
73 | ; // In top level of Functionchecker |
---|
74 | |
---|
75 | if ( applicationExpr->get_results().size() > 1 ) { |
---|
76 | for ( std::list< Type *>::iterator res = applicationExpr->get_results().begin(); res != applicationExpr->get_results().end(); res++ ) |
---|
77 | temporaries.push_back( new ObjectDecl( nameGen->newName(), DeclarationNode::Auto, LinkageSpec::AutoGen, 0, (*res )->clone(), 0 ) ); |
---|
78 | |
---|
79 | assert( ! temporaries.empty() ); |
---|
80 | } |
---|
81 | |
---|
82 | applicationExpr->set_function( maybeMutate( applicationExpr->get_function(), *this ) ); |
---|
83 | |
---|
84 | std::list< Expression * > newArgs; |
---|
85 | for ( std::list< Expression *>::iterator e = applicationExpr->get_args().begin(); e != applicationExpr->get_args().end(); ++e ) { |
---|
86 | FunctionChecker rec( false, nameGen ); |
---|
87 | (*e )->acceptMutator( rec ); |
---|
88 | |
---|
89 | if ( ! rec.temporaries.empty() ) { |
---|
90 | TupleExpr *lhs = new TupleExpr; |
---|
91 | std::list< Expression * > &tmem = lhs->get_exprs(); |
---|
92 | for ( std::list<ObjectDecl *>::iterator d = rec.temporaries.begin(); d != rec.temporaries.end(); ++d ) { |
---|
93 | tmem.push_back( new VariableExpr( *d ) ); |
---|
94 | newArgs.push_back( new VariableExpr( *d ) ); |
---|
95 | } |
---|
96 | |
---|
97 | // construct tuple assignment |
---|
98 | std::list<Expression *> args; |
---|
99 | args.push_back( new AddressExpr( lhs ) ); |
---|
100 | args.push_back( *e ); |
---|
101 | tempExpr.push_back( new UntypedExpr( new NameExpr("?=?"), args ) ); |
---|
102 | |
---|
103 | temporaries.splice( temporaries.end(), rec.temporaries ); |
---|
104 | } else |
---|
105 | newArgs.push_back( *e ); |
---|
106 | // percolate to recursive calls |
---|
107 | } |
---|
108 | |
---|
109 | applicationExpr->get_args().clear(); |
---|
110 | std::copy( newArgs.begin(), newArgs.end(), back_inserter( applicationExpr->get_args()) ); |
---|
111 | |
---|
112 | return applicationExpr; |
---|
113 | } |
---|
114 | |
---|
115 | Expression* TupleDistrib::mutate( UntypedExpr *expr ) { |
---|
116 | if ( NameExpr *assgnop = dynamic_cast< NameExpr * >( expr->get_function()) ) { |
---|
117 | if ( assgnop->get_name() == std::string("?=?") ) { |
---|
118 | std::list<Expression *> &args = expr->get_args(); |
---|
119 | assert( args.size() == 2 ); |
---|
120 | //if args.front() points to a tuple and if args.back() is already resolved |
---|
121 | if ( AddressExpr *addr = dynamic_cast<AddressExpr *>( args.front()) ) |
---|
122 | if ( TupleExpr *lhs = dynamic_cast<TupleExpr *>( addr->get_arg()) ) |
---|
123 | if ( ApplicationExpr *rhs = dynamic_cast<ApplicationExpr *>( args.back() ) ) { |
---|
124 | for ( std::list<Expression *>::iterator tc = lhs->get_exprs().begin(); tc != lhs->get_exprs().end(); ++tc ) |
---|
125 | rhs->get_args().push_back( new AddressExpr( *tc ) ); |
---|
126 | return rhs; // XXX |
---|
127 | } // if |
---|
128 | } else |
---|
129 | assert( false ); // It's not an assignment, shouldn't be here |
---|
130 | } // if |
---|
131 | return expr; |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | // namespace Tuples |
---|
137 | // Local Variables: // |
---|
138 | // tab-width: 4 // |
---|
139 | // mode: c++ // |
---|
140 | // compile-command: "make install" // |
---|
141 | // End: // |
---|