source: src/Tuples/TupleExpansion.cc@ 7756647

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 7756647 was 65660bd, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

replace multiple-returning functions with tuple-returning functions, implement tuple ctor/dtor, allow N-arg tuple assignment

  • Property mode set to 100644
File size: 7.9 KB
Line 
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// TupleAssignment.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 15:02:53 2015
13// Update Count : 2
14//
15
16#include <iterator>
17#include <iostream>
18#include <cassert>
19#include "Tuples.h"
20#include "GenPoly/DeclMutator.h"
21#include "SynTree/Mutator.h"
22#include "SynTree/Statement.h"
23#include "SynTree/Declaration.h"
24#include "SynTree/Type.h"
25#include "SynTree/Expression.h"
26#include "SynTree/Initializer.h"
27#include "SymTab/Mangler.h"
28#include "Common/ScopedMap.h"
29#include "ResolvExpr/typeops.h"
30
31namespace Tuples {
32 namespace {
33 class UniqueExprExpander : public GenPoly::DeclMutator {
34 public:
35 typedef GenPoly::DeclMutator Parent;
36 virtual Expression * mutate( UniqueExpr * unqExpr );
37 std::map< Expression *, ObjectDecl * > decls;
38 };
39
40 class TupleAssignExpander : public Mutator {
41 public:
42 typedef Mutator Parent;
43 virtual Expression * mutate( TupleAssignExpr * tupleExpr );
44 };
45
46 class TupleTypeReplacer : public GenPoly::DeclMutator {
47 public:
48 typedef GenPoly::DeclMutator Parent;
49
50 virtual Type * mutate( TupleType * tupleType );
51
52 virtual CompoundStmt * mutate( CompoundStmt * stmt ) {
53 typeMap.beginScope();
54 stmt = Parent::mutate( stmt );
55 typeMap.endScope();
56 return stmt;
57 }
58 private:
59 ScopedMap< std::string, StructDecl * > typeMap;
60 };
61
62 class TupleIndexExpander : public Mutator {
63 public:
64 typedef Mutator Parent;
65 virtual Expression * mutate( TupleIndexExpr * tupleExpr );
66 };
67
68 class TupleExprExpander : public Mutator {
69 public:
70 typedef Mutator Parent;
71 virtual Expression * mutate( TupleExpr * tupleExpr );
72 };
73 }
74
75 void expandUniqueExpr( std::list< Declaration * > & translationUnit ) {
76 UniqueExprExpander unqExpander;
77 unqExpander.mutateDeclarationList( translationUnit );
78 }
79
80 void expandTuples( std::list< Declaration * > & translationUnit ) {
81 TupleAssignExpander assnExpander;
82 mutateAll( translationUnit, assnExpander );
83
84 TupleTypeReplacer replacer;
85 replacer.mutateDeclarationList( translationUnit );
86
87 TupleIndexExpander idxExpander;
88 mutateAll( translationUnit, idxExpander );
89
90 TupleExprExpander exprExpander;
91 mutateAll( translationUnit, exprExpander );
92 }
93
94 Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
95 static UniqueName tempNamer( "_unq_expr_" );
96 unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
97 if ( ! decls.count( unqExpr->get_expr() ) ) {
98 // xxx - it's possible (likely?) that expressions can appear in the wrong order because of this. Need to ensure they're placed in the correct location.
99 ObjectDecl * decl = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, unqExpr->get_result()->clone(), new SingleInit( unqExpr->get_expr()->clone() ) );
100 decls[unqExpr->get_expr()] = decl;
101 addDeclaration( decl );
102 }
103 return new VariableExpr( decls[unqExpr->get_expr()] );
104 }
105
106 Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
107 // xxx - Parent::mutate?
108 CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
109 std::list< Statement * > & stmts = compoundStmt->get_kids();
110 for ( ObjectDecl * obj : assnExpr->get_tempDecls() ) {
111 stmts.push_back( new DeclStmt( noLabels, obj ) );
112 }
113 TupleExpr * tupleExpr = new TupleExpr( assnExpr->get_assigns() );
114 assert( tupleExpr->get_result() );
115 stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
116 assnExpr->get_tempDecls().clear();
117 assnExpr->get_assigns().clear();
118 delete assnExpr;
119 return new StmtExpr( compoundStmt );
120 }
121
122 Type * TupleTypeReplacer::mutate( TupleType * tupleType ) {
123 std::string mangleName = SymTab::Mangler::mangleType( tupleType );
124 TupleType * newType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) );
125 if ( ! typeMap.count( mangleName ) ) {
126 // generate struct type to replace tuple type
127 StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName );
128 decl->set_body( true );
129 int cnt = 0;
130 for ( Type * t : *newType ) {
131 decl->get_members().push_back( new ObjectDecl( "field_"+std::to_string(++cnt), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );
132 }
133 typeMap[mangleName] = decl;
134 addDeclaration( decl );
135 }
136 Type::Qualifiers qualifiers = newType->get_qualifiers();
137 delete newType;
138 return new StructInstType( qualifiers, typeMap[mangleName] );
139 }
140
141 Expression * TupleIndexExpander::mutate( TupleIndexExpr * tupleExpr ) {
142 Expression * tuple = maybeMutate( tupleExpr->get_tuple(), *this );
143 assert( tuple );
144 tupleExpr->set_tuple( nullptr );
145 unsigned int idx = tupleExpr->get_index();
146 delete tupleExpr;
147
148 StructInstType * type = safe_dynamic_cast< StructInstType * >( tuple->get_result() );
149 StructDecl * structDecl = type->get_baseStruct();
150 assert( structDecl->get_members().size() > idx );
151 Declaration * member = *std::next(structDecl->get_members().begin(), idx);
152 return new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
153 }
154
155 Expression * replaceTupleExpr( Type * result, const std::list< Expression * > & exprs ) {
156 if ( result->isVoid() ) {
157 // void result - don't need to produce a value for cascading - just output a chain of comma exprs
158 assert( ! exprs.empty() );
159 std::list< Expression * >::const_iterator iter = exprs.begin();
160 Expression * expr = *iter++;
161 for ( ; iter != exprs.end(); ++iter ) {
162 expr = new CommaExpr( expr, *iter );
163 }
164 return expr;
165 } else {
166 // typed tuple expression - produce a compound literal which performs each of the expressions
167 // as a distinct part of its initializer - the produced compound literal may be used as part of
168 // another expression
169 std::list< Initializer * > inits;
170 for ( Expression * expr : exprs ) {
171 inits.push_back( new SingleInit( expr ) );
172 }
173 return new CompoundLiteralExpr( result, new ListInit( inits ) );
174 }
175 }
176
177 Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) {
178 Type * result = tupleExpr->get_result();
179 std::list< Expression * > exprs = tupleExpr->get_exprs();
180 assert( result );
181
182 tupleExpr->set_result( nullptr );
183 tupleExpr->get_exprs().clear();
184 delete tupleExpr;
185
186 return replaceTupleExpr( result, exprs );
187 }
188
189 Type * makeTupleType( const std::list< Expression * > & exprs ) {
190 // produce the TupleType which aggregates the types of the exprs
191 TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true, false) );
192 Type::Qualifiers &qualifiers = tupleType->get_qualifiers();
193 for ( Expression * expr : exprs ) {
194 assert( expr->get_result() );
195 if ( expr->get_result()->isVoid() ) {
196 // if the type of any expr is void, the type of the entire tuple is void
197 delete tupleType;
198 return new VoidType( Type::Qualifiers() );
199 }
200 Type * type = expr->get_result()->clone();
201 tupleType->get_types().push_back( type );
202 // the qualifiers on the tuple type are the qualifiers that exist on all component types
203 qualifiers &= type->get_qualifiers();
204 } // for
205 return tupleType;
206 }
207
208 namespace {
209 /// determines if impurity (read: side-effects) may exist in a piece of code. Currently gives a very crude approximation, wherein any function call expression means the code may be impure
210 class ImpurityDetector : public Visitor {
211 public:
212 typedef Visitor Parent;
213 virtual void visit( ApplicationExpr * appExpr ) { maybeImpure = true; }
214 virtual void visit( UntypedExpr * untypedExpr ) { maybeImpure = true; }
215 bool maybeImpure = false;
216 };
217 } // namespace
218
219 bool maybeImpure( Expression * expr ) {
220 ImpurityDetector detector;
221 expr->accept( detector );
222 return detector.maybeImpure;
223 }
224} // namespace Tuples
225
226// Local Variables: //
227// tab-width: 4 //
228// mode: c++ //
229// compile-command: "make install" //
230// End: //
231
Note: See TracBrowser for help on using the repository browser.