[6eb8948] | 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"
|
---|
[f006f01] | 20 | #include "GenPoly/DeclMutator.h"
|
---|
| 21 | #include "SynTree/Mutator.h"
|
---|
[6eb8948] | 22 | #include "SynTree/Statement.h"
|
---|
[f006f01] | 23 | #include "SynTree/Declaration.h"
|
---|
| 24 | #include "SynTree/Type.h"
|
---|
[3c13c03] | 25 | #include "SynTree/Expression.h"
|
---|
| 26 | #include "SynTree/Initializer.h"
|
---|
[f006f01] | 27 | #include "SymTab/Mangler.h"
|
---|
| 28 | #include "Common/ScopedMap.h"
|
---|
[1132b62] | 29 | #include "ResolvExpr/typeops.h"
|
---|
[f0121d7] | 30 | #include "InitTweak/GenInit.h"
|
---|
[6eb8948] | 31 |
|
---|
| 32 | namespace Tuples {
|
---|
[3c13c03] | 33 | namespace {
|
---|
[bf32bb8] | 34 | class MemberTupleExpander : public Mutator {
|
---|
| 35 | public:
|
---|
| 36 | typedef Mutator Parent;
|
---|
| 37 | virtual Expression * mutate( UntypedMemberExpr * memberExpr );
|
---|
| 38 | };
|
---|
| 39 |
|
---|
[3c13c03] | 40 | class UniqueExprExpander : public GenPoly::DeclMutator {
|
---|
| 41 | public:
|
---|
| 42 | typedef GenPoly::DeclMutator Parent;
|
---|
[141b786] | 43 |
|
---|
[3c13c03] | 44 | virtual Expression * mutate( UniqueExpr * unqExpr );
|
---|
[141b786] | 45 |
|
---|
| 46 | std::map< int, Expression * > decls; // not vector, because order added may not be increasing order
|
---|
| 47 |
|
---|
| 48 | ~UniqueExprExpander() {
|
---|
| 49 | for ( std::pair<const int, Expression *> & p : decls ) {
|
---|
| 50 | delete p.second;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[3c13c03] | 53 | };
|
---|
| 54 |
|
---|
| 55 | class TupleAssignExpander : public Mutator {
|
---|
| 56 | public:
|
---|
| 57 | typedef Mutator Parent;
|
---|
| 58 | virtual Expression * mutate( TupleAssignExpr * tupleExpr );
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | class TupleTypeReplacer : public GenPoly::DeclMutator {
|
---|
| 62 | public:
|
---|
| 63 | typedef GenPoly::DeclMutator Parent;
|
---|
| 64 |
|
---|
| 65 | virtual Type * mutate( TupleType * tupleType );
|
---|
| 66 |
|
---|
| 67 | virtual CompoundStmt * mutate( CompoundStmt * stmt ) {
|
---|
| 68 | typeMap.beginScope();
|
---|
| 69 | stmt = Parent::mutate( stmt );
|
---|
| 70 | typeMap.endScope();
|
---|
| 71 | return stmt;
|
---|
| 72 | }
|
---|
| 73 | private:
|
---|
| 74 | ScopedMap< std::string, StructDecl * > typeMap;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | class TupleIndexExpander : public Mutator {
|
---|
| 78 | public:
|
---|
| 79 | typedef Mutator Parent;
|
---|
| 80 | virtual Expression * mutate( TupleIndexExpr * tupleExpr );
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 | class TupleExprExpander : public Mutator {
|
---|
| 84 | public:
|
---|
| 85 | typedef Mutator Parent;
|
---|
| 86 | virtual Expression * mutate( TupleExpr * tupleExpr );
|
---|
| 87 | };
|
---|
| 88 | }
|
---|
[f006f01] | 89 |
|
---|
[bf32bb8] | 90 | void expandMemberTuples( std::list< Declaration * > & translationUnit ) {
|
---|
| 91 | MemberTupleExpander expander;
|
---|
| 92 | mutateAll( translationUnit, expander );
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[aefcc3b] | 95 | void expandUniqueExpr( std::list< Declaration * > & translationUnit ) {
|
---|
[3c13c03] | 96 | UniqueExprExpander unqExpander;
|
---|
| 97 | unqExpander.mutateDeclarationList( translationUnit );
|
---|
[aefcc3b] | 98 | }
|
---|
[3c13c03] | 99 |
|
---|
[aefcc3b] | 100 | void expandTuples( std::list< Declaration * > & translationUnit ) {
|
---|
[3c13c03] | 101 | TupleAssignExpander assnExpander;
|
---|
| 102 | mutateAll( translationUnit, assnExpander );
|
---|
[f006f01] | 103 |
|
---|
| 104 | TupleTypeReplacer replacer;
|
---|
| 105 | replacer.mutateDeclarationList( translationUnit );
|
---|
[3c13c03] | 106 |
|
---|
| 107 | TupleIndexExpander idxExpander;
|
---|
| 108 | mutateAll( translationUnit, idxExpander );
|
---|
| 109 |
|
---|
| 110 | TupleExprExpander exprExpander;
|
---|
| 111 | mutateAll( translationUnit, exprExpander );
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[bf32bb8] | 114 | namespace {
|
---|
| 115 | /// given a expression representing the member and an expression representing the aggregate,
|
---|
| 116 | /// reconstructs a flattened UntypedMemberExpr with the right precedence
|
---|
[141b786] | 117 | Expression * reconstructMemberExpr( Expression * member, Expression * aggr ) {
|
---|
[bf32bb8] | 118 | if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( member ) ) {
|
---|
| 119 | // construct a new UntypedMemberExpr with the correct structure , and recursively
|
---|
| 120 | // expand that member expression.
|
---|
| 121 | MemberTupleExpander expander;
|
---|
| 122 | UntypedMemberExpr * newMemberExpr = new UntypedMemberExpr( memberExpr->get_member(), new UntypedMemberExpr( memberExpr->get_aggregate(), aggr->clone() ) );
|
---|
| 123 |
|
---|
| 124 | memberExpr->set_member(nullptr);
|
---|
| 125 | memberExpr->set_aggregate(nullptr);
|
---|
| 126 | delete memberExpr;
|
---|
| 127 | return newMemberExpr->acceptMutator( expander );
|
---|
| 128 | } else {
|
---|
| 129 | // not a member expression, so there is nothing to do but attach and return
|
---|
| 130 | return new UntypedMemberExpr( member, aggr->clone() );
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | Expression * MemberTupleExpander::mutate( UntypedMemberExpr * memberExpr ) {
|
---|
| 136 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * > ( memberExpr->get_member() ) ) {
|
---|
[141b786] | 137 | Expression * aggr = memberExpr->get_aggregate()->clone()->acceptMutator( *this );
|
---|
| 138 | // aggregate expressions which might be impure must be wrapped in unique expressions
|
---|
| 139 | // xxx - if there's a member-tuple expression nested in the aggregate, this currently generates the wrong code if a UniqueExpr is not used, and it's purely an optimization to remove the UniqueExpr
|
---|
| 140 | // if ( Tuples::maybeImpure( memberExpr->get_aggregate() ) ) aggr = new UniqueExpr( aggr );
|
---|
| 141 | aggr = new UniqueExpr( aggr );
|
---|
[bf32bb8] | 142 | for ( Expression *& expr : tupleExpr->get_exprs() ) {
|
---|
[141b786] | 143 | expr = reconstructMemberExpr( expr, aggr );
|
---|
[bf32bb8] | 144 | }
|
---|
[141b786] | 145 | delete aggr;
|
---|
[bf32bb8] | 146 | return tupleExpr;
|
---|
| 147 | } else {
|
---|
[f0121d7] | 148 | // there may be a tuple expr buried in the aggregate
|
---|
| 149 | // xxx - this is a memory leak
|
---|
| 150 | return new UntypedMemberExpr( memberExpr->get_member()->clone(), memberExpr->get_aggregate()->acceptMutator( *this ) );
|
---|
[bf32bb8] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[3c13c03] | 154 | Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
|
---|
| 155 | unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
|
---|
[141b786] | 156 | const int id = unqExpr->get_id();
|
---|
| 157 |
|
---|
| 158 | // on first time visiting a unique expr with a particular ID, generate the expression that replaces all UniqueExprs with that ID,
|
---|
| 159 | // and lookup on subsequent hits. This ensures that all unique exprs with the same ID reference the same variable.
|
---|
| 160 | if ( ! decls.count( id ) ) {
|
---|
| 161 | Expression * assignUnq;
|
---|
| 162 | Expression * var = unqExpr->get_var();
|
---|
| 163 | if ( unqExpr->get_object() ) {
|
---|
| 164 | // an object was generated to represent this unique expression -- it should be added to the list of declarations now
|
---|
| 165 | addDeclaration( unqExpr->get_object() );
|
---|
| 166 | unqExpr->set_object( nullptr );
|
---|
| 167 | // steal the expr from the unqExpr
|
---|
| 168 | assignUnq = UntypedExpr::createAssign( unqExpr->get_var()->clone(), unqExpr->get_expr() );
|
---|
| 169 | unqExpr->set_expr( nullptr );
|
---|
| 170 | } else {
|
---|
| 171 | // steal the already generated assignment to var from the unqExpr - this has been generated by FixInit
|
---|
| 172 | Expression * expr = unqExpr->get_expr();
|
---|
| 173 | CommaExpr * commaExpr = safe_dynamic_cast< CommaExpr * >( expr );
|
---|
| 174 | assignUnq = commaExpr->get_arg1();
|
---|
| 175 | commaExpr->set_arg1( nullptr );
|
---|
| 176 | }
|
---|
| 177 | BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool );
|
---|
| 178 | ObjectDecl * finished = new ObjectDecl( toString( "_unq_expr_finished_", id ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::Bool ), new SingleInit( new ConstantExpr( Constant( boolType->clone(), "0" ) ), noDesignators ) );
|
---|
| 179 | addDeclaration( finished );
|
---|
| 180 | // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N))
|
---|
| 181 | // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code.
|
---|
| 182 | Expression * assignFinished = UntypedExpr::createAssign( new VariableExpr(finished), new ConstantExpr( Constant( boolType->clone(), "1" ) ) );
|
---|
| 183 | ConditionalExpr * condExpr = new ConditionalExpr( new VariableExpr( finished ), var->clone(),
|
---|
| 184 | new CommaExpr( new CommaExpr( assignUnq, assignFinished ), var->clone() ) );
|
---|
| 185 | condExpr->set_result( var->get_result()->clone() );
|
---|
| 186 | decls[id] = condExpr;
|
---|
[3c13c03] | 187 | }
|
---|
[141b786] | 188 | delete unqExpr;
|
---|
| 189 | return decls[id]->clone();
|
---|
[6eb8948] | 190 | }
|
---|
| 191 |
|
---|
[3c13c03] | 192 | Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
|
---|
[141b786] | 193 | assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) );
|
---|
[6eb8948] | 194 | CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
|
---|
| 195 | std::list< Statement * > & stmts = compoundStmt->get_kids();
|
---|
[3c13c03] | 196 | for ( ObjectDecl * obj : assnExpr->get_tempDecls() ) {
|
---|
[6eb8948] | 197 | stmts.push_back( new DeclStmt( noLabels, obj ) );
|
---|
| 198 | }
|
---|
[3c13c03] | 199 | TupleExpr * tupleExpr = new TupleExpr( assnExpr->get_assigns() );
|
---|
| 200 | assert( tupleExpr->get_result() );
|
---|
| 201 | stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
|
---|
| 202 | assnExpr->get_tempDecls().clear();
|
---|
| 203 | assnExpr->get_assigns().clear();
|
---|
| 204 | delete assnExpr;
|
---|
[6eb8948] | 205 | return new StmtExpr( compoundStmt );
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[f006f01] | 208 | Type * TupleTypeReplacer::mutate( TupleType * tupleType ) {
|
---|
| 209 | std::string mangleName = SymTab::Mangler::mangleType( tupleType );
|
---|
| 210 | TupleType * newType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) );
|
---|
| 211 | if ( ! typeMap.count( mangleName ) ) {
|
---|
| 212 | // generate struct type to replace tuple type
|
---|
| 213 | StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName );
|
---|
| 214 | decl->set_body( true );
|
---|
| 215 | int cnt = 0;
|
---|
| 216 | for ( Type * t : *newType ) {
|
---|
[141b786] | 217 | decl->get_members().push_back( new ObjectDecl( toString("field_", cnt++), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );
|
---|
[f006f01] | 218 | }
|
---|
| 219 | typeMap[mangleName] = decl;
|
---|
| 220 | addDeclaration( decl );
|
---|
| 221 | }
|
---|
[aa8f9df] | 222 | Type::Qualifiers qualifiers = newType->get_qualifiers();
|
---|
[f006f01] | 223 | delete newType;
|
---|
[aa8f9df] | 224 | return new StructInstType( qualifiers, typeMap[mangleName] );
|
---|
[f006f01] | 225 | }
|
---|
| 226 |
|
---|
[3c13c03] | 227 | Expression * TupleIndexExpander::mutate( TupleIndexExpr * tupleExpr ) {
|
---|
| 228 | Expression * tuple = maybeMutate( tupleExpr->get_tuple(), *this );
|
---|
| 229 | assert( tuple );
|
---|
| 230 | tupleExpr->set_tuple( nullptr );
|
---|
| 231 | unsigned int idx = tupleExpr->get_index();
|
---|
| 232 | delete tupleExpr;
|
---|
| 233 |
|
---|
| 234 | StructInstType * type = safe_dynamic_cast< StructInstType * >( tuple->get_result() );
|
---|
| 235 | StructDecl * structDecl = type->get_baseStruct();
|
---|
| 236 | assert( structDecl->get_members().size() > idx );
|
---|
| 237 | Declaration * member = *std::next(structDecl->get_members().begin(), idx);
|
---|
| 238 | return new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[65660bd] | 241 | Expression * replaceTupleExpr( Type * result, const std::list< Expression * > & exprs ) {
|
---|
| 242 | if ( result->isVoid() ) {
|
---|
| 243 | // void result - don't need to produce a value for cascading - just output a chain of comma exprs
|
---|
| 244 | assert( ! exprs.empty() );
|
---|
| 245 | std::list< Expression * >::const_iterator iter = exprs.begin();
|
---|
| 246 | Expression * expr = *iter++;
|
---|
| 247 | for ( ; iter != exprs.end(); ++iter ) {
|
---|
| 248 | expr = new CommaExpr( expr, *iter );
|
---|
| 249 | }
|
---|
| 250 | return expr;
|
---|
| 251 | } else {
|
---|
| 252 | // typed tuple expression - produce a compound literal which performs each of the expressions
|
---|
| 253 | // as a distinct part of its initializer - the produced compound literal may be used as part of
|
---|
| 254 | // another expression
|
---|
| 255 | std::list< Initializer * > inits;
|
---|
| 256 | for ( Expression * expr : exprs ) {
|
---|
| 257 | inits.push_back( new SingleInit( expr ) );
|
---|
| 258 | }
|
---|
| 259 | return new CompoundLiteralExpr( result, new ListInit( inits ) );
|
---|
[3c13c03] | 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[65660bd] | 263 | Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) {
|
---|
[bf32bb8] | 264 | // recursively expand sub-tuple-expressions
|
---|
| 265 | tupleExpr = safe_dynamic_cast<TupleExpr *>(Parent::mutate(tupleExpr));
|
---|
[65660bd] | 266 | Type * result = tupleExpr->get_result();
|
---|
| 267 | std::list< Expression * > exprs = tupleExpr->get_exprs();
|
---|
| 268 | assert( result );
|
---|
| 269 |
|
---|
[bf32bb8] | 270 | // remove data from shell and delete it
|
---|
[65660bd] | 271 | tupleExpr->set_result( nullptr );
|
---|
| 272 | tupleExpr->get_exprs().clear();
|
---|
| 273 | delete tupleExpr;
|
---|
| 274 |
|
---|
| 275 | return replaceTupleExpr( result, exprs );
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | Type * makeTupleType( const std::list< Expression * > & exprs ) {
|
---|
| 279 | // produce the TupleType which aggregates the types of the exprs
|
---|
[3c13c03] | 280 | TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true, false) );
|
---|
| 281 | Type::Qualifiers &qualifiers = tupleType->get_qualifiers();
|
---|
| 282 | for ( Expression * expr : exprs ) {
|
---|
| 283 | assert( expr->get_result() );
|
---|
[65660bd] | 284 | if ( expr->get_result()->isVoid() ) {
|
---|
| 285 | // if the type of any expr is void, the type of the entire tuple is void
|
---|
| 286 | delete tupleType;
|
---|
| 287 | return new VoidType( Type::Qualifiers() );
|
---|
| 288 | }
|
---|
[3c13c03] | 289 | Type * type = expr->get_result()->clone();
|
---|
| 290 | tupleType->get_types().push_back( type );
|
---|
[65660bd] | 291 | // the qualifiers on the tuple type are the qualifiers that exist on all component types
|
---|
[3c13c03] | 292 | qualifiers &= type->get_qualifiers();
|
---|
| 293 | } // for
|
---|
| 294 | return tupleType;
|
---|
| 295 | }
|
---|
[65660bd] | 296 |
|
---|
| 297 | namespace {
|
---|
| 298 | /// 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
|
---|
| 299 | class ImpurityDetector : public Visitor {
|
---|
| 300 | public:
|
---|
| 301 | typedef Visitor Parent;
|
---|
| 302 | virtual void visit( ApplicationExpr * appExpr ) { maybeImpure = true; }
|
---|
| 303 | virtual void visit( UntypedExpr * untypedExpr ) { maybeImpure = true; }
|
---|
| 304 | bool maybeImpure = false;
|
---|
| 305 | };
|
---|
| 306 | } // namespace
|
---|
| 307 |
|
---|
| 308 | bool maybeImpure( Expression * expr ) {
|
---|
| 309 | ImpurityDetector detector;
|
---|
| 310 | expr->accept( detector );
|
---|
| 311 | return detector.maybeImpure;
|
---|
| 312 | }
|
---|
[6eb8948] | 313 | } // namespace Tuples
|
---|
| 314 |
|
---|
| 315 | // Local Variables: //
|
---|
| 316 | // tab-width: 4 //
|
---|
| 317 | // mode: c++ //
|
---|
| 318 | // compile-command: "make install" //
|
---|
| 319 | // End: //
|
---|
| 320 |
|
---|