Changeset 141b786 for src/Tuples
- Timestamp:
- Nov 9, 2016, 2:21:05 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- b726084
- Parents:
- 23bb1b9
- Location:
- src/Tuples
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/Tuples/TupleAssignment.cc ¶
r23bb1b9 r141b786 20 20 #include "SynTree/Initializer.h" 21 21 #include "Tuples.h" 22 #include "Explode.h" 22 23 #include "Common/SemanticError.h" 23 24 #include "InitTweak/InitTweak.h" -
TabularUnified src/Tuples/TupleExpansion.cc ¶
r23bb1b9 r141b786 41 41 public: 42 42 typedef GenPoly::DeclMutator Parent; 43 43 44 virtual Expression * mutate( UniqueExpr * unqExpr ); 44 std::map< int, ObjectDecl * > decls; // not vector, because order added may not be increasing order 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 } 45 53 }; 46 54 … … 107 115 /// given a expression representing the member and an expression representing the aggregate, 108 116 /// reconstructs a flattened UntypedMemberExpr with the right precedence 109 Expression * reconstructMemberExpr( Expression * member, UniqueExpr* aggr ) {117 Expression * reconstructMemberExpr( Expression * member, Expression * aggr ) { 110 118 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( member ) ) { 111 119 // construct a new UntypedMemberExpr with the correct structure , and recursively … … 127 135 Expression * MemberTupleExpander::mutate( UntypedMemberExpr * memberExpr ) { 128 136 if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * > ( memberExpr->get_member() ) ) { 129 UniqueExpr * unqExpr = new UniqueExpr( memberExpr->get_aggregate()->clone() ); 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 ); 130 142 for ( Expression *& expr : tupleExpr->get_exprs() ) { 131 expr = reconstructMemberExpr( expr, unqExpr );132 } 133 delete unqExpr;143 expr = reconstructMemberExpr( expr, aggr ); 144 } 145 delete aggr; 134 146 return tupleExpr; 135 147 } else { … … 142 154 Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) { 143 155 unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) ); 144 if ( ! decls.count( unqExpr->get_id() ) ) { 145 // 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. 146 147 // xxx - this doesn't work, because it would need to be placed after fixInit, but fixInit doesn't know (and shouldn't have to know) about the existance of UniqueExprs - i.e. it will visit them twice 148 // need to construct/destruct unique exprs in general - maybe it's not worth it and fixInit should handle UniqueExpr explicitly? 149 // currently, tmp is being destructed before unqExpr is used, which suggests there should be a separate lifetime for unqExpr from the tmp_ret 150 151 // if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( unqExpr->get_expr() ) ) { 152 // if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( commaExpr->get_arg2() ) ) { 153 // // steal existing decl from expr 154 // if ( ObjectDecl * decl = dynamic_cast< ObjectDecl * >( varExpr->get_var() ) ) { 155 // decls[unqExpr->get_id()] = decl; 156 // return unqExpr->get_expr()->clone(); 157 // } 158 // } 159 // } 160 161 ObjectDecl * objDecl = unqExpr->get_object(); 162 unqExpr->set_object( nullptr ); 163 decls[unqExpr->get_id()] = objDecl; 164 addDeclaration( objDecl ); 165 } 166 return new VariableExpr( decls[unqExpr->get_id()] ); 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; 187 } 188 delete unqExpr; 189 return decls[id]->clone(); 167 190 } 168 191 169 192 Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) { 170 // xxx - Parent::mutate?193 assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) ); 171 194 CompoundStmt * compoundStmt = new CompoundStmt( noLabels ); 172 195 std::list< Statement * > & stmts = compoundStmt->get_kids(); … … 192 215 int cnt = 0; 193 216 for ( Type * t : *newType ) { 194 decl->get_members().push_back( new ObjectDecl( "field_"+std::to_string(++cnt), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );217 decl->get_members().push_back( new ObjectDecl( toString("field_", cnt++), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) ); 195 218 } 196 219 typeMap[mangleName] = decl; -
TabularUnified src/Tuples/Tuples.h ¶
r23bb1b9 r141b786 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Mon May 18 15:04:02 201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Nov 9 13:17:58 2016 13 // Update Count : 15 14 14 // 15 15 … … 18 18 19 19 #include <string> 20 #include <vector>21 #include "ResolvExpr/AlternativeFinder.h"22 #include "ResolvExpr/Resolver.h"23 20 24 21 #include "SynTree/Expression.h" 25 22 #include "SynTree/Declaration.h" 26 23 #include "SynTree/Type.h" 24 25 #include "ResolvExpr/AlternativeFinder.h" 27 26 28 27 namespace Tuples { … … 45 44 /// returns true if the expression may contain side-effects. 46 45 bool maybeImpure( Expression * expr ); 47 48 49 /// helper function used by explode50 template< typename OutputIterator >51 void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out ) {52 Type * res = expr->get_result();53 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {54 ResolvExpr::AltList alts;55 explodeUnique( addrExpr->get_arg(), alt, indexer, back_inserter( alts ) );56 for ( ResolvExpr::Alternative & alt : alts ) {57 // distribute '&' over all components58 alt.expr = new AddressExpr( alt.expr );59 *out++ = alt;60 }61 } else if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {62 if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) {63 // can open tuple expr and dump its exploded components64 for ( Expression * expr : tupleExpr->get_exprs() ) {65 explodeUnique( expr, alt, indexer, out );66 }67 } else {68 // tuple type, but not tuple expr - recursively index into its components69 Expression * arg = expr->clone();70 if ( Tuples::maybeImpure( arg ) ) {71 // expressions which may contain side effects require a single unique instance of the expression.72 // resolve the UniqueExpr (which should be relatively cheap, since the argument is already resolved)73 // so that its accompanying object is properly constructed and destructed.74 arg = new UniqueExpr( arg );75 arg = ResolvExpr::resolveInVoidContext( arg, indexer );76 }77 for ( unsigned int i = 0; i < tupleType->size(); i++ ) {78 TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );79 explodeUnique( idx, alt, indexer, out );80 delete idx;81 }82 delete arg;83 }84 } else {85 // atomic (non-tuple) type - output a clone of the expression in a new alternative86 *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost );87 }88 }89 90 /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type91 template< typename OutputIterator >92 void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out ) {93 explodeUnique( alt.expr, alt, indexer, out );94 }95 96 // explode list of alternatives97 template< typename AltIterator, typename OutputIterator >98 void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out ) {99 for ( ; altBegin != altEnd; ++altBegin ) {100 explode( *altBegin, indexer, out );101 }102 }103 104 template< typename OutputIterator >105 void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out ) {106 explode( alts.begin(), alts.end(), indexer, out );107 }108 46 } // namespace Tuples 109 47 -
TabularUnified src/Tuples/module.mk ¶
r23bb1b9 r141b786 16 16 17 17 SRC += Tuples/TupleAssignment.cc \ 18 Tuples/TupleExpansion.cc 18 Tuples/TupleExpansion.cc \ 19 Tuples/Explode.cc
Note: See TracChangeset
for help on using the changeset viewer.