Changeset a08ba92
- Timestamp:
- May 19, 2015, 4:58:14 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 843054c2
- Parents:
- 01aeade
- Location:
- translator
- Files:
-
- 75 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/CodeGen/GenType.cc
r01aeade ra08ba92 122 122 } 123 123 124 void GenType::visit( ArrayType *arrayType ) {124 void GenType::visit( ArrayType *arrayType ) { 125 125 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); 126 126 } … … 153 153 cg.genCommaList( pars.begin(), pars.end() ); 154 154 155 if ( funcType->get_isVarArgs() ) {155 if ( funcType->get_isVarArgs() ) { 156 156 os << ", ..."; 157 157 } // if -
translator/Common/utility.h
r01aeade ra08ba92 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 07:27:38201513 // Update Count : 212 // Last Modified On : Tue May 19 15:34:57 2015 13 // Update Count : 3 14 14 // 15 15 … … 116 116 Ts ret; 117 117 118 switch ( l.size() ) {118 switch ( l.size() ) { 119 119 case 0: 120 120 return ret; … … 156 156 157 157 //if ( next != org.end() ) { 158 159 160 158 org.erase( pos ); 159 org.splice( next, with ); 160 //} 161 161 162 162 return; … … 175 175 template< typename T > 176 176 struct is_null_pointer { 177 bool operator()( const T *ptr ) { return ( ptr == 0 ); }177 bool operator()( const T *ptr ) { return ( ptr == 0 ); } 178 178 }; 179 179 -
translator/ControlStruct/CaseRangeMutator.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // CaseRangeMutator.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 13:00:28 2015 13 // Update Count : 3 14 // 15 15 16 #include <list> 16 17 #include <cassert> … … 27 28 28 29 namespace ControlStruct { 29 Statement *CaseRangeMutator::mutate(ChooseStmt *chooseStmt) { 30 /* There shouldn't be any `choose' statements by now, throw an exception or something. */ 31 throw( 0 ) ; /* FIXME */ 32 } 33 34 Statement *CaseRangeMutator::mutate(SwitchStmt *switchStmt) { 35 std::list< Statement * > &cases = switchStmt->get_branches(); 36 37 // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator 38 std::list< Statement * >::iterator i = cases.begin(); 39 while ( i != cases.end() ) { 40 (*i)->acceptMutator( *this ); 41 42 if ( ! newCaseLabels.empty() ) { 43 std::list< Statement * > newCases; 44 45 // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) ); 46 47 for ( std::list< Expression * >::iterator j = newCaseLabels.begin(); 48 j != newCaseLabels.end(); j++ ) { 49 std::list<Label> emptyLabels; 50 std::list< Statement *> emptyStmts; 51 newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) ); 52 } 53 54 if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) ) 55 if ( ! currentCase->get_statements().empty() ) { 56 CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() ); 57 if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases 58 // transfer the statement block (if any) to the new list: 59 lastCase->set_statements( currentCase->get_statements() ); 60 } 61 std::list< Statement * >::iterator j = i; advance( j, 1 ); 62 replace ( cases, i, newCases ); 63 i = j; 64 newCaseLabels.clear(); 65 } else 66 i++; 67 } // while 68 69 return switchStmt; 70 } 71 72 Statement *CaseRangeMutator::mutate(FallthruStmt *fallthruStmt) { 73 //delete fallthruStmt; 74 return new NullStmt(); 75 } 76 77 Statement *CaseRangeMutator::mutate(CaseStmt *caseStmt) { 78 UntypedExpr *cond; 79 if ( (cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) { 80 NameExpr *nmfunc; 81 if ( (nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) { 82 if ( nmfunc->get_name() == std::string("Range") ) { 83 assert( cond->get_args().size() == 2 ); 84 std::list<Expression *>::iterator i = cond->get_args().begin(); 85 Expression *lo = *i, *hi = *(++i); // "unnecessary" temporaries 86 fillRange( lo, hi); 87 } 88 } 89 } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) { 90 // case list 91 assert( ! tcond->get_exprs().empty() ); 92 for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ ) 93 newCaseLabels.push_back( *i ); // do I need to clone them? 94 } // if 95 96 std::list< Statement * > &stmts = caseStmt->get_statements(); 97 mutateAll ( stmts, *this ); 98 99 return caseStmt; 100 } 101 102 void CaseRangeMutator::fillRange(Expression *lo, Expression *hi) { 103 // generate the actual range (and check for consistency) 104 Constant *c_lo, *c_hi; 105 ConstantExpr *ce_lo, *ce_hi; 106 ce_lo = dynamic_cast< ConstantExpr * >( lo ); 107 ce_hi = dynamic_cast< ConstantExpr * >( hi ); 108 109 if ( ce_lo && ce_hi ) { 110 c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant(); 111 } /* else { 112 if ( ! ce_lo ) ; 113 if ( ! ce_hi ) ; 114 } */ 115 BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ), 116 *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() ); 117 118 if ( ! ty_lo || ! ty_hi ) 119 return; // one of them is not a constant 120 121 switch ( ty_lo->get_kind() ) { 122 case BasicType::Char: 123 case BasicType::UnsignedChar: 124 switch ( ty_hi->get_kind() ){ 30 Statement *CaseRangeMutator::mutate( ChooseStmt *chooseStmt ) { 31 // There shouldn't be any `choose' statements by now, throw an exception or something. 32 throw( 0 ) ; /* FIXME */ 33 } 34 35 Statement *CaseRangeMutator::mutate( SwitchStmt *switchStmt ) { 36 std::list< Statement * > &cases = switchStmt->get_branches(); 37 38 // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator 39 std::list< Statement * >::iterator i = cases.begin(); 40 while ( i != cases.end() ) { 41 (*i )->acceptMutator( *this ); 42 43 if ( ! newCaseLabels.empty() ) { 44 std::list< Statement * > newCases; 45 46 // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) ); 47 48 for ( std::list< Expression * >::iterator j = newCaseLabels.begin(); 49 j != newCaseLabels.end(); j++ ) { 50 std::list<Label> emptyLabels; 51 std::list< Statement *> emptyStmts; 52 newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) ); 53 } // for 54 55 if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) ) 56 if ( ! currentCase->get_statements().empty() ) { 57 CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() ); 58 if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases 59 // transfer the statement block ( if any ) to the new list: 60 lastCase->set_statements( currentCase->get_statements() ); 61 } // if 62 std::list< Statement * >::iterator j = i; advance( j, 1 ); 63 replace ( cases, i, newCases ); 64 i = j; 65 newCaseLabels.clear(); 66 } else 67 i++; 68 } // while 69 70 return switchStmt; 71 } 72 73 Statement *CaseRangeMutator::mutate( FallthruStmt *fallthruStmt ) { 74 //delete fallthruStmt; 75 return new NullStmt(); 76 } 77 78 Statement *CaseRangeMutator::mutate( CaseStmt *caseStmt ) { 79 UntypedExpr *cond; 80 if ( ( cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) { 81 NameExpr *nmfunc; 82 if ( ( nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) { 83 if ( nmfunc->get_name() == std::string("Range") ) { 84 assert( cond->get_args().size() == 2 ); 85 std::list<Expression *>::iterator i = cond->get_args().begin(); 86 Expression *lo = *i, *hi = *(++i ); // "unnecessary" temporaries 87 fillRange( lo, hi ); 88 } // if 89 } // if 90 } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) { 91 // case list 92 assert( ! tcond->get_exprs().empty() ); 93 for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ ) 94 newCaseLabels.push_back( *i ); // do I need to clone them? 95 } // if 96 97 std::list< Statement * > &stmts = caseStmt->get_statements(); 98 mutateAll ( stmts, *this ); 99 100 return caseStmt; 101 } 102 103 void CaseRangeMutator::fillRange( Expression *lo, Expression *hi ) { 104 // generate the actual range ( and check for consistency ) 105 Constant *c_lo, *c_hi; 106 ConstantExpr *ce_lo, *ce_hi; 107 ce_lo = dynamic_cast< ConstantExpr * >( lo ); 108 ce_hi = dynamic_cast< ConstantExpr * >( hi ); 109 110 if ( ce_lo && ce_hi ) { 111 c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant(); 112 } /* else { 113 if ( ! ce_lo ) ; 114 if ( ! ce_hi ) ; 115 } */ 116 BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ), 117 *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() ); 118 119 if ( ! ty_lo || ! ty_hi ) 120 return; // one of them is not a constant 121 122 switch ( ty_lo->get_kind() ) { 125 123 case BasicType::Char: 126 124 case BasicType::UnsignedChar: 127 // first case, they are both printable ASCII characters represented as 'x' 128 if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) { 129 char ch_lo = (c_lo->get_value())[1], ch_hi = (c_hi->get_value())[1]; 130 131 if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; } 132 133 for ( char c = ch_lo; c <= ch_hi; c++ ){ 134 Type::Qualifiers q; 135 Constant cnst( new BasicType(q, BasicType::Char), 136 std::string("'") + c + std::string("'") ); 137 newCaseLabels.push_back( new ConstantExpr( cnst ) ); 125 switch ( ty_hi->get_kind() ) { 126 case BasicType::Char: 127 case BasicType::UnsignedChar: 128 // first case, they are both printable ASCII characters represented as 'x' 129 if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) { 130 char ch_lo = ( c_lo->get_value())[1], ch_hi = ( c_hi->get_value())[1]; 131 132 if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; } 133 134 for ( char c = ch_lo; c <= ch_hi; c++ ) { 135 Type::Qualifiers q; 136 Constant cnst( new BasicType( q, BasicType::Char ), 137 std::string("'") + c + std::string("'") ); 138 newCaseLabels.push_back( new ConstantExpr( cnst ) ); 139 } // for 140 141 return; 142 } // if 143 break; 144 default: 145 // error: incompatible constants 146 break; 147 } // switch 148 break; 149 case BasicType::ShortSignedInt: 150 case BasicType::ShortUnsignedInt: 151 case BasicType::SignedInt: 152 case BasicType::UnsignedInt: 153 case BasicType::LongSignedInt: 154 case BasicType::LongUnsignedInt: 155 case BasicType::LongLongSignedInt: 156 case BasicType::LongLongUnsignedInt: 157 switch ( ty_hi->get_kind() ) { 158 case BasicType::ShortSignedInt: 159 case BasicType::ShortUnsignedInt: 160 case BasicType::SignedInt: 161 case BasicType::UnsignedInt: 162 case BasicType::LongSignedInt: 163 case BasicType::LongUnsignedInt: 164 case BasicType::LongLongSignedInt: 165 case BasicType::LongLongUnsignedInt: { 166 int i_lo = atoi( c_lo->get_value().c_str()), 167 i_hi = atoi( c_hi->get_value().c_str()); 168 169 if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; } 170 171 for ( int c = i_lo; c <= i_hi; c++ ) { 172 Type::Qualifiers q; 173 Constant cnst( new BasicType( q, ty_hi->get_kind()), // figure can't hurt (used to think in positives) 174 toString< int >( c ) ); 175 newCaseLabels.push_back( new ConstantExpr( cnst ) ); 176 } 177 178 return; 179 } 180 default: 181 // error: incompatible constants 182 break; 138 183 } 139 184 break; 185 default: 186 break; 187 } // switch 188 189 /* End: */{ 190 // invalid range, signal a warning (it still generates the two case labels) 191 newCaseLabels.push_back( lo ); 192 newCaseLabels.push_back( hi ); 140 193 return; 141 }142 break;143 default:144 // error: incompatible constants145 break;146 194 } 147 break; 148 case BasicType::ShortSignedInt: 149 case BasicType::ShortUnsignedInt: 150 case BasicType::SignedInt: 151 case BasicType::UnsignedInt: 152 case BasicType::LongSignedInt: 153 case BasicType::LongUnsignedInt: 154 case BasicType::LongLongSignedInt: 155 case BasicType::LongLongUnsignedInt: 156 switch ( ty_hi->get_kind() ) { 157 case BasicType::ShortSignedInt: 158 case BasicType::ShortUnsignedInt: 159 case BasicType::SignedInt: 160 case BasicType::UnsignedInt: 161 case BasicType::LongSignedInt: 162 case BasicType::LongUnsignedInt: 163 case BasicType::LongLongSignedInt: 164 case BasicType::LongLongUnsignedInt: { 165 int i_lo = atoi(c_lo->get_value().c_str()), 166 i_hi = atoi(c_hi->get_value().c_str()); 167 168 if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; } 169 170 for ( int c = i_lo; c <= i_hi; c++ ){ 171 Type::Qualifiers q; 172 Constant cnst( new BasicType(q, ty_hi->get_kind()), // figure can't hurt (used to think in positives) 173 toString< int >( c ) ); 174 newCaseLabels.push_back( new ConstantExpr( cnst ) ); 175 } 176 177 return; 178 } 179 default: 180 // error: incompatible constants 181 break; 182 } 183 break; 184 default: 185 break; 186 } // switch 187 188 /* End: */{ 189 // invalid range, signal a warning (it still generates the two case labels) 190 newCaseLabels.push_back( lo ); 191 newCaseLabels.push_back( hi ); 192 return; 193 } 194 } 195 } 195 196 } // namespace ControlStruct 197 196 198 // Local Variables: // 197 199 // tab-width: 4 // -
translator/ControlStruct/CaseRangeMutator.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // CaseRangeMutator.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:22:51 2015 13 // Update Count : 3 14 14 // 15 15 16 #ifndef CASERNG_MUTATOR_H 16 17 #define CASERNG_MUTATOR_H … … 21 22 22 23 namespace ControlStruct { 23 24 25 CaseRangeMutator() {}24 class CaseRangeMutator : public Mutator { 25 public: 26 CaseRangeMutator() {} 26 27 27 virtual Statement *mutate( ChooseStmt * );28 virtual Statement *mutate( SwitchStmt * );29 virtual Statement *mutate( FallthruStmt * );30 virtual Statement *mutate( CaseStmt * );31 32 void fillRange( Expression *lo, Expression *hi );28 virtual Statement *mutate( ChooseStmt * ); 29 virtual Statement *mutate( SwitchStmt * ); 30 virtual Statement *mutate( FallthruStmt * ); 31 virtual Statement *mutate( CaseStmt * ); 32 private: 33 void fillRange( Expression *lo, Expression *hi ); 33 34 34 Expression *currentCondition; 35 std::list< Expression * > newCaseLabels; 36 }; 37 35 Expression *currentCondition; 36 std::list< Expression * > newCaseLabels; 37 }; 38 38 } // namespace ControlStruct 39 39 40 40 #endif // CASERNG_MUTATOR_H 41 41 42 /*43 Local Variables:44 mode: c++45 End:46 */47 42 // Local Variables: // 48 43 // tab-width: 4 // -
translator/ControlStruct/ChooseMutator.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // ChooseMutator.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:31:39 2015 13 // Update Count : 2 14 14 // 15 15 16 #include <list> 16 17 … … 19 20 20 21 namespace ControlStruct { 21 Statement *ChooseMutator::mutate( ChooseStmt *chooseStmt) {22 bool enclosingChoose = insideChoose;23 insideChoose = true;24 mutateAll( chooseStmt->get_branches(), *this );25 insideChoose = enclosingChoose;26 return new SwitchStmt( chooseStmt->get_labels(), chooseStmt->get_condition(), chooseStmt->get_branches() );27 22 Statement *ChooseMutator::mutate( ChooseStmt *chooseStmt ) { 23 bool enclosingChoose = insideChoose; 24 insideChoose = true; 25 mutateAll( chooseStmt->get_branches(), *this ); 26 insideChoose = enclosingChoose; 27 return new SwitchStmt( chooseStmt->get_labels(), chooseStmt->get_condition(), chooseStmt->get_branches() ); 28 } 28 29 29 30 bool enclosingChoose = insideChoose;31 insideChoose = false;32 mutateAll( switchStmt->get_branches(), *this );33 insideChoose = enclosingChoose;34 return switchStmt;35 30 Statement *ChooseMutator::mutate( SwitchStmt *switchStmt ) { 31 bool enclosingChoose = insideChoose; 32 insideChoose = false; 33 mutateAll( switchStmt->get_branches(), *this ); 34 insideChoose = enclosingChoose; 35 return switchStmt; 36 } 36 37 37 38 delete fallthruStmt;39 return new NullStmt();40 38 Statement *ChooseMutator::mutate( FallthruStmt *fallthruStmt ) { 39 delete fallthruStmt; 40 return new NullStmt(); 41 } 41 42 42 Statement* ChooseMutator::mutate(CaseStmt *caseStmt) {43 std::list< Statement * > &stmts = caseStmt->get_statements();43 Statement* ChooseMutator::mutate( CaseStmt *caseStmt ) { 44 std::list< Statement * > &stmts = caseStmt->get_statements(); 44 45 45 if ( insideChoose ) {46 47 48 ( posBrk->get_type() == BranchStmt::Break )) // last statement in the list is a (superfluous) 'break'49 || dynamic_cast< FallthruStmt * > ( stmts.back() ) )50 ;51 52 stmts.push_back( new BranchStmt( std::list< Label >(), "", BranchStmt::Break ) );53 54 } // if46 if ( insideChoose ) { 47 BranchStmt *posBrk; 48 if ( (( posBrk = dynamic_cast< BranchStmt * > ( stmts.back() ) ) && 49 ( posBrk->get_type() == BranchStmt::Break )) // last statement in the list is a (superfluous) 'break' 50 || dynamic_cast< FallthruStmt * > ( stmts.back() ) ) 51 ; 52 else { 53 stmts.push_back( new BranchStmt( std::list< Label >(), "", BranchStmt::Break ) ); 54 } // if 55 } // if 55 56 56 mutateAll ( stmts, *this );57 return caseStmt;58 57 mutateAll ( stmts, *this ); 58 return caseStmt; 59 } 59 60 } // namespace ControlStruct 61 60 62 // Local Variables: // 61 63 // tab-width: 4 // -
translator/ControlStruct/ChooseMutator.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // ChooseMutator.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:33:11 2015 13 // Update Count : 3 14 14 // 15 15 16 #ifndef CHOOSE_MUTATOR_H 16 17 #define CHOOSE_MUTATOR_H … … 21 22 22 23 namespace ControlStruct { 24 class ChooseMutator : public Mutator { 25 public: 26 ChooseMutator() : insideChoose( false ) {} 23 27 24 class ChooseMutator : public Mutator { 25 public: 26 ChooseMutator() : insideChoose( false ) {} 27 28 virtual Statement *mutate( ChooseStmt * ); 29 virtual Statement *mutate( SwitchStmt * ); 30 virtual Statement *mutate( FallthruStmt * ); 31 virtual Statement *mutate( CaseStmt * ); 32 private: 33 bool insideChoose; 34 }; 28 virtual Statement *mutate( ChooseStmt * ); 29 virtual Statement *mutate( SwitchStmt * ); 30 virtual Statement *mutate( FallthruStmt * ); 31 virtual Statement *mutate( CaseStmt * ); 32 private: 33 bool insideChoose; 34 }; 35 35 } // namespace ControlStruct 36 36 37 37 #endif // CHOOSE_MUTATOR_H 38 38 39 /*40 Local Variables:41 mode: c++42 End:43 */44 39 // Local Variables: // 45 40 // tab-width: 4 // -
translator/ControlStruct/ForExprMutator.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // ForExprMutator.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:31:47 2015 13 // Update Count : 2 14 14 // 15 15 16 #include "SynTree/Mutator.h" 16 17 #include "SynTree/Statement.h" … … 18 19 19 20 namespace ControlStruct { 20 21 // recurse down all nest for loops to hoist any initializer declarations to make them C89 (rather than C99)22 23 if ( DeclStmt *decl = dynamic_cast< DeclStmt * > ( forStmt->get_initialization() ) ) {24 25 26 21 Statement *ForExprMutator::mutate( ForStmt *forStmt ) { 22 // recurse down all nest for loops to hoist any initializer declarations to make them C89 (rather than C99) 23 forStmt->set_body( forStmt->get_body()->acceptMutator( *this ) ); 24 if ( DeclStmt *decl = dynamic_cast< DeclStmt * > ( forStmt->get_initialization() ) ) { 25 // create compound statement, move initializer declaration outside, leave _for_ as-is 26 CompoundStmt *block = new CompoundStmt( std::list< Label >() ); 27 std::list<Statement *> &stmts = block->get_kids(); 27 28 28 29 30 29 stmts.push_back( decl ); 30 forStmt->set_initialization( 0 ); 31 stmts.push_back( forStmt ); 31 32 32 33 } // if33 return block; 34 } // if 34 35 35 return forStmt;36 36 return forStmt; 37 } 37 38 } // namespace ControlStruct 39 38 40 // Local Variables: // 39 41 // tab-width: 4 // -
translator/ControlStruct/ForExprMutator.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // ForExprMutator.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:25:19 2015 13 // Update Count : 2 14 14 // 15 15 16 #ifndef FOR_MUTATOR_H 16 17 #define FOR_MUTATOR_H 17 18 18 19 #include "SynTree/Mutator.h" 19 20 20 #include "utility.h" 21 21 22 22 namespace ControlStruct { 23 24 25 virtual Statement *mutate( ForStmt * );26 23 class ForExprMutator : public Mutator { 24 public: 25 virtual Statement *mutate( ForStmt * ); 26 }; 27 27 } // namespace ControlStruct 28 28 29 29 #endif // CHOOSE_MUTATOR_H 30 30 31 /*32 Local Variables:33 mode: c++34 End:35 */36 31 // Local Variables: // 37 32 // tab-width: 4 // -
translator/ControlStruct/LabelFixer.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // LabelFixer.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:25:59 2015 13 // Update Count : 1 14 14 // 15 15 16 #include <list> 16 17 #include <cassert> … … 23 24 24 25 namespace ControlStruct { 25 LabelFixer::Entry::Entry( Statement *to, Statement *from ) : definition ( to ) { 26 if ( from != 0 ) 27 usage.push_back( from ); 28 } 29 30 bool LabelFixer::Entry::insideLoop() { 31 return ( dynamic_cast< ForStmt * > ( definition ) || 32 dynamic_cast< WhileStmt * > ( definition ) ); 33 } 34 35 LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) { 36 if ( generator == 0 ) 37 generator = LabelGenerator::getGenerator(); 38 } 39 40 void LabelFixer::visit( FunctionDecl *functionDecl ) { 41 if ( functionDecl->get_statements() != 0 ) 42 functionDecl->get_statements()->accept( *this ); 43 44 MLEMutator mlemut( resolveJumps(), generator ); 45 functionDecl->acceptMutator( mlemut ); 46 } 47 48 void LabelFixer::visit( Statement *stmt ) { 49 std::list< Label > &labels = stmt->get_labels(); 50 51 if ( ! labels.empty() ) { 52 Label current = setLabelsDef( labels, stmt ); 53 labels.clear(); 54 labels.push_front( current ); 55 } // if 56 } 57 58 void LabelFixer::visit( BranchStmt *branchStmt ) { 59 visit ( ( Statement * )branchStmt ); // the labels this statement might have 60 61 Label target; 62 if ( (target = branchStmt->get_target()) != "" ) { 63 setLabelsUsg( target, branchStmt ); 64 } //else /* computed goto or normal exit-loop statements */ 65 } 66 67 Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { 68 assert( definition != 0 ); 69 Entry *entry = new Entry( definition ); 70 bool used = false; 71 72 for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) 73 if ( labelTable.find( *i ) == labelTable.end() ) 74 { used = true; labelTable[ *i ] = entry; } // undefined and unused 75 else 76 if ( labelTable[ *i ]->defined() ) 77 throw SemanticError( "Duplicate definition of label: " + *i ); 78 else 79 labelTable[ *i ]->set_definition( definition ); 80 81 if (! used ) delete entry; 82 83 return labelTable[ llabel.front() ]->get_label(); // this *must* exist 84 } 85 86 Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use ) { 87 assert( use != 0 ); 88 89 if ( labelTable.find( orgValue ) != labelTable.end() ) 90 labelTable[ orgValue ]->add_use( use ); // the label has been defined or used before 91 else 92 labelTable[ orgValue ] = new Entry( 0, use ); 93 94 return labelTable[ orgValue ]->get_label(); 95 } 96 97 std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) { 98 std::map< Statement *, Entry * > def_us; 99 100 for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) { 101 Entry *e = i->second; 102 103 if ( def_us.find ( e->get_definition() ) == def_us.end() ) 104 def_us[ e->get_definition() ] = e; 105 else 106 if ( e->used() ) 107 def_us[ e->get_definition() ]->add_uses( e->get_uses() ); 26 LabelFixer::Entry::Entry( Statement *to, Statement *from ) : definition ( to ) { 27 if ( from != 0 ) 28 usage.push_back( from ); 108 29 } 109 30 110 // get rid of labelTable 111 for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) { 112 Statement *to = (*i).first; 113 std::list< Statement *> &from = (*i).second->get_uses(); 114 Label finalLabel = generator->newLabel(); 115 (*i).second->set_label( finalLabel ); 31 bool LabelFixer::Entry::insideLoop() { 32 return ( dynamic_cast< ForStmt * > ( definition ) || 33 dynamic_cast< WhileStmt * > ( definition ) ); 34 } 116 35 117 if ( to == 0 ) { 118 BranchStmt *first_use = dynamic_cast<BranchStmt *>(from.back()); 119 Label undef(""); 120 if ( first_use != 0 ) 121 undef = first_use->get_target(); 122 throw SemanticError ( "'" + undef + "' label not defined"); 123 } 36 LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) { 37 if ( generator == 0 ) 38 generator = LabelGenerator::getGenerator(); 39 } 124 40 125 to->get_labels().clear(); 126 to->get_labels().push_back( finalLabel ); 41 void LabelFixer::visit( FunctionDecl *functionDecl ) { 42 if ( functionDecl->get_statements() != 0 ) 43 functionDecl->get_statements()->accept( *this ); 127 44 128 for ( std::list< Statement *>::iterator j = from.begin(); j != from.end(); j++ ) { 129 BranchStmt *jumpTo = dynamic_cast< BranchStmt * > ( *j ); 130 assert( jumpTo != 0 ); 131 jumpTo->set_target( finalLabel ); 132 } // for 133 } // for 45 MLEMutator mlemut( resolveJumps(), generator ); 46 functionDecl->acceptMutator( mlemut ); 47 } 134 48 135 // reverse table 136 std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); 137 for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) 138 (*ret)[ (*i).second->get_label() ] = (*i).first; 49 void LabelFixer::visit( Statement *stmt ) { 50 std::list< Label > &labels = stmt->get_labels(); 139 51 140 return ret; 141 } 52 if ( ! labels.empty() ) { 53 Label current = setLabelsDef( labels, stmt ); 54 labels.clear(); 55 labels.push_front( current ); 56 } // if 57 } 58 59 void LabelFixer::visit( BranchStmt *branchStmt ) { 60 visit ( ( Statement * )branchStmt ); // the labels this statement might have 61 62 Label target; 63 if ( (target = branchStmt->get_target()) != "" ) { 64 setLabelsUsg( target, branchStmt ); 65 } //else /* computed goto or normal exit-loop statements */ 66 } 67 68 Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { 69 assert( definition != 0 ); 70 Entry *entry = new Entry( definition ); 71 bool used = false; 72 73 for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) 74 if ( labelTable.find( *i ) == labelTable.end() ) 75 { used = true; labelTable[ *i ] = entry; } // undefined and unused 76 else 77 if ( labelTable[ *i ]->defined() ) 78 throw SemanticError( "Duplicate definition of label: " + *i ); 79 else 80 labelTable[ *i ]->set_definition( definition ); 81 82 if ( ! used ) delete entry; 83 84 return labelTable[ llabel.front() ]->get_label(); // this *must* exist 85 } 86 87 Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use ) { 88 assert( use != 0 ); 89 90 if ( labelTable.find( orgValue ) != labelTable.end() ) 91 labelTable[ orgValue ]->add_use( use ); // the label has been defined or used before 92 else 93 labelTable[ orgValue ] = new Entry( 0, use ); 94 95 return labelTable[ orgValue ]->get_label(); 96 } 97 98 std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) { 99 std::map< Statement *, Entry * > def_us; 100 101 for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) { 102 Entry *e = i->second; 103 104 if ( def_us.find ( e->get_definition() ) == def_us.end() ) 105 def_us[ e->get_definition() ] = e; 106 else 107 if ( e->used() ) 108 def_us[ e->get_definition() ]->add_uses( e->get_uses() ); 109 } 110 111 // get rid of labelTable 112 for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) { 113 Statement *to = (*i).first; 114 std::list< Statement *> &from = (*i).second->get_uses(); 115 Label finalLabel = generator->newLabel(); 116 (*i).second->set_label( finalLabel ); 117 118 if ( to == 0 ) { 119 BranchStmt *first_use = dynamic_cast<BranchStmt *>(from.back()); 120 Label undef(""); 121 if ( first_use != 0 ) 122 undef = first_use->get_target(); 123 throw SemanticError ( "'" + undef + "' label not defined"); 124 } 125 126 to->get_labels().clear(); 127 to->get_labels().push_back( finalLabel ); 128 129 for ( std::list< Statement *>::iterator j = from.begin(); j != from.end(); j++ ) { 130 BranchStmt *jumpTo = dynamic_cast< BranchStmt * > ( *j ); 131 assert( jumpTo != 0 ); 132 jumpTo->set_target( finalLabel ); 133 } // for 134 } // for 135 136 // reverse table 137 std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); 138 for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) 139 (*ret)[ (*i).second->get_label() ] = (*i).first; 140 141 return ret; 142 } 142 143 } // namespace ControlStruct 144 143 145 // Local Variables: // 144 146 // tab-width: 4 // -
translator/ControlStruct/LabelFixer.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // LabelFixer.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:31:55 2015 13 // Update Count : 3 14 14 // 15 15 16 #ifndef LABEL_FIXER_H 16 17 #define LABEL_FIXER_H 17 18 18 19 #include "utility.h" 19 20 20 #include "SynTree/SynTree.h" 21 21 #include "SynTree/Visitor.h" 22 23 22 #include "LabelGenerator.h" 24 23 … … 26 25 27 26 namespace ControlStruct { 28 29 typedef Visitor Parent;30 31 LabelFixer( LabelGenerator *gen = 0 );27 class LabelFixer : public Visitor { 28 typedef Visitor Parent; 29 public: 30 LabelFixer( LabelGenerator *gen = 0 ); 32 31 33 std::map < Label, Statement * > *resolveJumps() throw ( SemanticError );32 std::map < Label, Statement * > *resolveJumps() throw ( SemanticError ); 34 33 35 // Declarations36 virtual void visit( FunctionDecl *functionDecl );34 // Declarations 35 virtual void visit( FunctionDecl *functionDecl ); 37 36 38 // Statements39 void visit( Statement *stmt );37 // Statements 38 void visit( Statement *stmt ); 40 39 41 virtual void visit( CompoundStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }42 virtual void visit( NullStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }43 virtual void visit( ExprStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }44 virtual void visit( IfStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }45 virtual void visit( WhileStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }46 virtual void visit( ForStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }47 virtual void visit( SwitchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }48 virtual void visit( ChooseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }49 virtual void visit( FallthruStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }50 virtual void visit( CaseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }51 virtual void visit( ReturnStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }52 virtual void visit( TryStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }53 virtual void visit( CatchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }54 virtual void visit( DeclStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }55 virtual void visit( BranchStmt *branchStmt );40 virtual void visit( CompoundStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 41 virtual void visit( NullStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 42 virtual void visit( ExprStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 43 virtual void visit( IfStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 44 virtual void visit( WhileStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 45 virtual void visit( ForStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 46 virtual void visit( SwitchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 47 virtual void visit( ChooseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 48 virtual void visit( FallthruStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 49 virtual void visit( CaseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 50 virtual void visit( ReturnStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 51 virtual void visit( TryStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 52 virtual void visit( CatchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 53 virtual void visit( DeclStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } 54 virtual void visit( BranchStmt *branchStmt ); 56 55 57 Label setLabelsDef( std::list< Label > &, Statement *definition );58 Label setLabelsUsg( Label, Statement *usage = 0 );56 Label setLabelsDef( std::list< Label > &, Statement *definition ); 57 Label setLabelsUsg( Label, Statement *usage = 0 ); 59 58 60 61 class Entry {62 public:63 64 65 66 59 private: 60 class Entry { 61 public: 62 Entry( Statement *to = 0, Statement *from = 0 ); 63 bool used() { return ( usage.empty() ); } 64 bool defined() { return ( definition != 0 ); } 65 bool insideLoop(); 67 66 68 69 70 67 Label get_label() const { return label; } 68 Statement *get_definition() const { return definition; } 69 std::list< Statement *> &get_uses() { return usage; } 71 70 72 73 74 71 void add_use ( Statement *use ) { usage.push_back( use ); } 72 void add_uses ( std::list<Statement *> uses ) { usage.insert( usage.end(), uses.begin(), uses.end() ); } 73 void set_definition( Statement *def ) { definition = def; } 75 74 76 void set_label( Label lab ) { label = lab; } 77 Label gset_label() const { return label; } 78 private: 79 Label label; 80 Statement *definition; 81 std::list<Statement *> usage; 75 void set_label( Label lab ) { label = lab; } 76 Label gset_label() const { return label; } 77 private: 78 Label label; 79 Statement *definition; 80 std::list<Statement *> usage; 81 }; 82 83 std::map < Label, Entry *> labelTable; 84 LabelGenerator *generator; 82 85 }; 83 84 std::map < Label, Entry *> labelTable;85 LabelGenerator *generator;86 };87 86 } // namespace ControlStruct 88 87 89 88 #endif // LABEL_FIXER_H 90 89 91 /*92 Local Variables:93 mode: c++94 End:95 */96 90 // Local Variables: // 97 91 // tab-width: 4 // -
translator/ControlStruct/LabelGenerator.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // LabelGenerator.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:32:04 2015 13 // Update Count : 2 14 14 // 15 15 16 #include <iostream> 16 17 #include <strstream> … … 19 20 20 21 namespace ControlStruct { 21 22 LabelGenerator *LabelGenerator::labelGenerator = 0; 22 23 23 24 if ( LabelGenerator::labelGenerator == 0 )25 24 LabelGenerator *LabelGenerator::getGenerator() { 25 if ( LabelGenerator::labelGenerator == 0 ) 26 LabelGenerator::labelGenerator = new LabelGenerator(); 26 27 27 return labelGenerator;28 28 return labelGenerator; 29 } 29 30 30 31 std::ostrstream os;32 os << "__L" << current++ << "__";// << std::ends;33 os.freeze( false );34 std::string ret = std::string (os.str(), os.pcount());35 return Label( ret );36 31 Label LabelGenerator::newLabel() { 32 std::ostrstream os; 33 os << "__L" << current++ << "__";// << std::ends; 34 os.freeze( false ); 35 std::string ret = std::string (os.str(), os.pcount()); 36 return Label( ret ); 37 } 37 38 } // namespace ControlStruct 39 38 40 // Local Variables: // 39 41 // tab-width: 4 // -
translator/ControlStruct/LabelGenerator.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // LabelGenerator.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:33:20 2015 13 // Update Count : 3 14 14 // 15 15 16 #ifndef LABEL_GENERATOR_H 16 17 #define LABEL_GENERATOR_H … … 19 20 20 21 namespace ControlStruct { 21 22 23 static LabelGenerator *getGenerator();24 Label newLabel();25 void reset() { current = 0; }26 void rewind() { current--; }27 28 LabelGenerator(): current(0) {}29 30 int current;31 static LabelGenerator *labelGenerator;32 22 class LabelGenerator { 23 public: 24 static LabelGenerator *getGenerator(); 25 Label newLabel(); 26 void reset() { current = 0; } 27 void rewind() { current--; } 28 protected: 29 LabelGenerator(): current(0) {} 30 private: 31 int current; 32 static LabelGenerator *labelGenerator; 33 }; 33 34 } // namespace ControlStruct 34 35 35 36 #endif // LABEL_GENERATOR_H 36 37 37 /*38 Local Variables:39 mode: c++40 End:41 */42 38 // Local Variables: // 43 39 // tab-width: 4 // -
translator/ControlStruct/LabelTypeChecker.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // LabelTypeChecker.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:32:15 2015 13 // Update Count : 2 14 14 // 15 15 16 #include <list> 16 17 #include <cassert> … … 23 24 #include "LabelTypeChecker.h" 24 25 26 namespace ControlStruct { 27 void LabelTypeChecker::visit(UntypedExpr *untypedExpr) { 28 assert( untypedExpr != 0 ); 29 NameExpr *fname; 30 if ( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0) 31 && fname->get_name() == std::string("LabAddress") ) 32 std::cerr << "Taking the label of an address." << std::endl; 33 else { 34 acceptAll( untypedExpr->get_results(), *this ); 35 acceptAll( untypedExpr->get_args(), *this ); 36 } // if 37 return; 38 } 25 39 26 namespace ControlStruct { 27 void LabelTypeChecker::visit(UntypedExpr *untypedExpr){ 28 assert( untypedExpr != 0 ); 29 NameExpr *fname; 30 if ( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0) 31 && fname->get_name() == std::string("LabAddress") ) 32 std::cerr << "Taking the label of an address." << std::endl; 33 else { 34 acceptAll( untypedExpr->get_results(), *this ); 35 acceptAll( untypedExpr->get_args(), *this ); 36 } // if 37 return; 38 } 40 void LabelTypeChecker::visit(CompoundStmt *compoundStmt) { 41 index.enterScope(); 42 acceptAll( compoundStmt->get_kids(), *this ); 43 index.leaveScope(); 44 } 39 45 40 void LabelTypeChecker::visit(CompoundStmt *compoundStmt) { 41 index.enterScope(); 42 acceptAll( compoundStmt->get_kids(), *this ); 43 index.leaveScope(); 44 } 46 void LabelTypeChecker::visit(DeclStmt *declStmt) { 47 declStmt->accept( index ); 45 48 46 void LabelTypeChecker::visit(DeclStmt *declStmt){ 47 declStmt->accept( index ); 49 //ObjectDecl *odecl = 0; 50 // if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ) { 51 return; 52 } 48 53 49 //ObjectDecl *odecl = 0;50 // if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){51 return;52 } 54 void LabelTypeChecker::visit(BranchStmt *branchStmt) { 55 if ( branchStmt->get_type() != BranchStmt::Goto ) return; 56 Expression *target; 57 if ( (target = branchStmt->get_computedTarget()) == 0 ) return; 53 58 54 void LabelTypeChecker::visit(BranchStmt *branchStmt) { 55 if ( branchStmt->get_type() != BranchStmt::Goto ) return; 56 Expression *target; 57 if ( (target = branchStmt->get_computedTarget()) == 0 ) return; 59 NameExpr *name; 60 if ( ((name = dynamic_cast<NameExpr *>(target)) == 0) ) 61 return; // Not a name expression 62 63 std::list< DeclarationWithType * > interps; 64 index.lookupId(name->get_name(), interps); 65 if ( interps.size() != 1) 66 // in case of multiple declarations 67 throw SemanticError("Illegal label expression: " + name->get_name() ); 58 68 59 NameExpr *name; 60 if ( ((name = dynamic_cast<NameExpr *>(target)) == 0) ) 61 return; // Not a name expression 62 63 std::list< DeclarationWithType * > interps; 64 index.lookupId(name->get_name(), interps); 65 if ( interps.size() != 1) 66 // in case of multiple declarations 67 throw SemanticError("Illegal label expression: " + name->get_name() ); 69 PointerType *ptr; 70 if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 ) 71 if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 ) 72 return; 73 else 74 throw SemanticError("Wrong type of parameter for computed goto"); 75 else 76 throw SemanticError("Wrong type of parameter for computed goto"); 68 77 69 PointerType *ptr;70 if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 )71 if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 )72 78 return; 73 else 74 throw SemanticError("Wrong type of parameter for computed goto"); 75 else 76 throw SemanticError("Wrong type of parameter for computed goto"); 79 } 80 } // namespace ControlStruct 77 81 78 return;79 }80 } // namespace ControlStruct81 82 // Local Variables: // 82 83 // tab-width: 4 // -
translator/ControlStruct/LabelTypeChecker.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // LabelTypeChecker.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:33:47 2015 13 // Update Count : 3 14 14 // 15 15 16 #ifndef LABEL_TYPE_H 16 17 #define LABEL_TYPE_H … … 23 24 24 25 namespace ControlStruct { 25 26 27 //LabelTypeChecker() {26 class LabelTypeChecker : public Visitor { 27 public: 28 //LabelTypeChecker() { 28 29 29 virtual void visit( CompoundStmt *compoundStmt );30 virtual void visit( DeclStmt *declStmt );31 virtual void visit( BranchStmt *branchStmt );32 virtual void visit( UntypedExpr *untypedExpr );33 34 SymTab::Indexer index;35 30 virtual void visit( CompoundStmt *compoundStmt ); 31 virtual void visit( DeclStmt *declStmt ); 32 virtual void visit( BranchStmt *branchStmt ); 33 virtual void visit( UntypedExpr *untypedExpr ); 34 private: 35 SymTab::Indexer index; 36 }; 36 37 } // namespace ControlStruct 37 38 38 39 #endif // LABEL_TYPE_H 39 40 /*41 Local Variables:42 mode: c++43 End:44 */45 46 47 40 48 41 // Local Variables: // -
translator/ControlStruct/MLEMutator.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // MLEMutator.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:32:26 2015 13 // Update Count : 2 14 // 15 15 16 #include <cassert> 16 17 #include <algorithm> … … 19 20 #include "SynTree/Statement.h" 20 21 21 22 22 namespace ControlStruct { 23 24 delete targetTable;25 targetTable = 0;26 27 28 29 bool labeledBlock = false;30 if ( !((cmpndStmt->get_labels()).empty()) ) {31 32 33 }34 35 std::list< Statement * > &kids = cmpndStmt->get_kids();36 for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {37 38 39 40 std::list< Statement * >::iterator next = k; next++;41 if ( next == kids.end() ) {42 43 44 } else45 46 47 set_breakLabel("");48 49 } // for50 51 if ( labeledBlock ) {52 53 54 set_breakLabel( enclosingBlocks.back().get_breakExit() );55 56 } // if57 58 //mutateAll( cmpndStmt->get_kids(), *this );59 return cmpndStmt;60 61 62 63 enclosingLoops.push_back( Entry( whileStmt ) );64 whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) );65 66 Entry &e = enclosingLoops.back();67 assert ( e == whileStmt );68 whileStmt->set_body( mutateLoop( whileStmt->get_body(), e ) );69 enclosingLoops.pop_back();70 71 return whileStmt;72 73 74 75 enclosingLoops.push_back( Entry( forStmt ) );76 maybeMutate( forStmt->get_body(), *this );77 78 Entry &e = enclosingLoops.back();79 assert ( e == forStmt );80 forStmt->set_body( mutateLoop( forStmt->get_body(), e ) );81 enclosingLoops.pop_back();82 83 return forStmt;84 85 86 87 if ( branchStmt->get_type() == BranchStmt::Goto )88 89 90 // test if continue target is a loop91 if ( branchStmt->get_type() == BranchStmt::Continue && enclosingLoops.empty() )92 93 94 if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )95 96 97 if ( branchStmt->get_target() == "" ) return branchStmt;98 99 if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )100 101 102 std::list< Entry >::iterator check;103 if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )104 105 106 // neither in loop nor in block, checking if in switch/choose107 if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )108 109 110 if ( enclosingLoops.back() == (*check) )111 112 113 Label newLabel;114 switch ( branchStmt->get_type() ) {115 case BranchStmt::Break:116 117 newLabel = check->get_breakExit();118 119 newLabel = generator->newLabel();120 check->set_breakExit( newLabel );121 122 123 case BranchStmt::Continue:124 125 newLabel = check->get_contExit();126 127 newLabel = generator->newLabel();128 check->set_contExit( newLabel );129 130 131 default:132 133 } // switch134 135 return new BranchStmt( std::list<Label>(), newLabel, BranchStmt::Goto );136 137 138 139 140 Label brkLabel = generator->newLabel();141 enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) );142 mutateAll( switchStmt->get_branches(), *this ); {143 144 145 146 }147 assert ( enclosingSwitches.back() == switchStmt );148 enclosingSwitches.pop_back();149 return switchStmt;150 151 152 153 Label brkLabel = generator->newLabel();154 enclosingSwitches.push_back( Entry(switchStmt,"", brkLabel) );155 mutateAll( switchStmt->get_branches(), *this ); {156 157 158 159 }160 assert ( enclosingSwitches.back() == switchStmt );161 enclosingSwitches.pop_back();162 return switchStmt;163 164 165 166 CompoundStmt *newBody;167 if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {168 169 170 } // if171 172 Label endLabel = e.get_contExit();173 174 if ( e.get_breakExit() != "" ) {175 176 177 178 newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );179 180 181 182 183 } // if184 185 if ( e.get_breakExit() != "" || e.get_contExit() != "" ){186 187 newBody->get_kids().back()->get_labels().push_back( endLabel );188 189 std::list < Label > ls; ls.push_back( endLabel );190 newBody->get_kids().push_back( new NullStmt( ls ) );191 192 } // if193 194 return newBody;195 196 197 198 199 assert ( contExit == "" || contExit == l );200 contExit = l;201 202 203 204 assert ( breakExit == "" || breakExit == l );205 breakExit = l;206 23 MLEMutator::~MLEMutator() { 24 delete targetTable; 25 targetTable = 0; 26 } 27 28 CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) { 29 bool labeledBlock = false; 30 if ( !((cmpndStmt->get_labels()).empty()) ) { 31 labeledBlock = true; 32 enclosingBlocks.push_back( Entry( cmpndStmt ) ); 33 } // if 34 35 std::list< Statement * > &kids = cmpndStmt->get_kids(); 36 for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) { 37 *k = (*k)->acceptMutator(*this); 38 39 if ( ! get_breakLabel().empty() ) { 40 std::list< Statement * >::iterator next = k; next++; 41 if ( next == kids.end() ) { 42 std::list<Label> ls; ls.push_back( get_breakLabel() ); 43 kids.push_back( new NullStmt( ls ) ); 44 } else 45 (*next)->get_labels().push_back( get_breakLabel() ); 46 47 set_breakLabel(""); 48 } // if 49 } // for 50 51 if ( labeledBlock ) { 52 assert( ! enclosingBlocks.empty() ); 53 if ( ! enclosingBlocks.back().get_breakExit().empty() ) 54 set_breakLabel( enclosingBlocks.back().get_breakExit() ); 55 enclosingBlocks.pop_back(); 56 } // if 57 58 //mutateAll( cmpndStmt->get_kids(), *this ); 59 return cmpndStmt; 60 } 61 62 Statement *MLEMutator::mutate( WhileStmt *whileStmt ) { 63 enclosingLoops.push_back( Entry( whileStmt ) ); 64 whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) ); 65 66 Entry &e = enclosingLoops.back(); 67 assert ( e == whileStmt ); 68 whileStmt->set_body( mutateLoop( whileStmt->get_body(), e ) ); 69 enclosingLoops.pop_back(); 70 71 return whileStmt; 72 } 73 74 Statement *MLEMutator::mutate( ForStmt *forStmt ) { 75 enclosingLoops.push_back( Entry( forStmt ) ); 76 maybeMutate( forStmt->get_body(), *this ); 77 78 Entry &e = enclosingLoops.back(); 79 assert ( e == forStmt ); 80 forStmt->set_body( mutateLoop( forStmt->get_body(), e ) ); 81 enclosingLoops.pop_back(); 82 83 return forStmt; 84 } 85 86 Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) { 87 if ( branchStmt->get_type() == BranchStmt::Goto ) 88 return branchStmt; 89 90 // test if continue target is a loop 91 if ( branchStmt->get_type() == BranchStmt::Continue && enclosingLoops.empty() ) 92 throw SemanticError( "'continue' outside a loop" ); 93 94 if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) ) 95 throw SemanticError( "'break' outside a loop or switch" ); 96 97 if ( branchStmt->get_target() == "" ) return branchStmt; 98 99 if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() ) 100 throw SemanticError("The label defined in the exit loop statement does not exist." ); // shouldn't happen (since that's already checked) 101 102 std::list< Entry >::iterator check; 103 if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() ) 104 // not in loop, checking if in block 105 if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() ) 106 // neither in loop nor in block, checking if in switch/choose 107 if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() ) 108 throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing loop."); 109 110 if ( enclosingLoops.back() == (*check) ) 111 return branchStmt; // exit the innermost loop (labels unnecessary) 112 113 Label newLabel; 114 switch ( branchStmt->get_type() ) { 115 case BranchStmt::Break: 116 if ( check->get_breakExit() != "" ) 117 newLabel = check->get_breakExit(); 118 else { 119 newLabel = generator->newLabel(); 120 check->set_breakExit( newLabel ); 121 } // if 122 break; 123 case BranchStmt::Continue: 124 if ( check->get_contExit() != "" ) 125 newLabel = check->get_contExit(); 126 else { 127 newLabel = generator->newLabel(); 128 check->set_contExit( newLabel ); 129 } // if 130 break; 131 default: 132 return 0; // shouldn't be here 133 } // switch 134 135 return new BranchStmt( std::list<Label>(), newLabel, BranchStmt::Goto ); 136 } 137 138 139 Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) { 140 Label brkLabel = generator->newLabel(); 141 enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) ); 142 mutateAll( switchStmt->get_branches(), *this ); { 143 // check if this is necessary (if there is a break to this point, otherwise do not generate 144 std::list<Label> temp; temp.push_back( brkLabel ); 145 switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) ); 146 } 147 assert ( enclosingSwitches.back() == switchStmt ); 148 enclosingSwitches.pop_back(); 149 return switchStmt; 150 } 151 152 Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) { 153 Label brkLabel = generator->newLabel(); 154 enclosingSwitches.push_back( Entry(switchStmt,"", brkLabel) ); 155 mutateAll( switchStmt->get_branches(), *this ); { 156 // check if this is necessary (if there is a break to this point, otherwise do not generate 157 std::list<Label> temp; temp.push_back( brkLabel ); 158 switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) ); 159 } 160 assert ( enclosingSwitches.back() == switchStmt ); 161 enclosingSwitches.pop_back(); 162 return switchStmt; 163 } 164 165 Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) { 166 CompoundStmt *newBody; 167 if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) { 168 newBody = new CompoundStmt( std::list< Label >() ); 169 newBody->get_kids().push_back( bodyLoop ); 170 } // if 171 172 Label endLabel = e.get_contExit(); 173 174 if ( e.get_breakExit() != "" ) { 175 if ( endLabel == "" ) endLabel = generator->newLabel(); 176 // check for whether this label is used or not, so as to not generate extraneous gotos 177 if (e.breakExitUsed) 178 newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) ); 179 // xxx 180 //std::list< Label > ls; ls.push_back( e.get_breakExit() ); 181 set_breakLabel( e.get_breakExit() ); 182 //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) ); 183 } // if 184 185 if ( e.get_breakExit() != "" || e.get_contExit() != "" ) { 186 if (dynamic_cast< NullStmt *>( newBody->get_kids().back() )) 187 newBody->get_kids().back()->get_labels().push_back( endLabel ); 188 else { 189 std::list < Label > ls; ls.push_back( endLabel ); 190 newBody->get_kids().push_back( new NullStmt( ls ) ); 191 } // if 192 } // if 193 194 return newBody; 195 } 196 197 //*** Entry's methods 198 void MLEMutator::Entry::set_contExit( Label l ) { 199 assert ( contExit == "" || contExit == l ); 200 contExit = l; 201 } 202 203 void MLEMutator::Entry::set_breakExit( Label l ) { 204 assert ( breakExit == "" || breakExit == l ); 205 breakExit = l; 206 } 207 207 } // namespace ControlStruct 208 208 209 // Local Variables: // 209 210 // tab-width: 4 // -
translator/ControlStruct/MLEMutator.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // MLEMutator.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:32:39 2015 13 // Update Count : 3 14 14 // 15 15 16 #ifndef MLE_MUTATOR_H 16 17 #define MLE_MUTATOR_H … … 26 27 27 28 namespace ControlStruct { 28 29 class Entry;30 31 MLEMutator( std::map<Label, Statement *> *t, LabelGenerator *gen = 0 ) : targetTable( t ), breakLabel(std::string("")), generator( gen ) {}32 ~MLEMutator();29 class MLEMutator : public Mutator { 30 class Entry; 31 public: 32 MLEMutator( std::map<Label, Statement *> *t, LabelGenerator *gen = 0 ) : targetTable( t ), breakLabel(std::string("")), generator( gen ) {} 33 ~MLEMutator(); 33 34 34 CompoundStmt *mutate( CompoundStmt *cmpndStmt );35 Statement *mutate( WhileStmt *whileStmt );36 Statement *mutate( ForStmt *forStmt );37 Statement *mutate( BranchStmt *branchStmt ) throw ( SemanticError );35 CompoundStmt *mutate( CompoundStmt *cmpndStmt ); 36 Statement *mutate( WhileStmt *whileStmt ); 37 Statement *mutate( ForStmt *forStmt ); 38 Statement *mutate( BranchStmt *branchStmt ) throw ( SemanticError ); 38 39 39 Statement *mutate( SwitchStmt *switchStmt );40 Statement *mutate( ChooseStmt *switchStmt );40 Statement *mutate( SwitchStmt *switchStmt ); 41 Statement *mutate( ChooseStmt *switchStmt ); 41 42 42 Statement *mutateLoop( Statement *bodyLoop, Entry &e );43 Statement *mutateLoop( Statement *bodyLoop, Entry &e ); 43 44 44 Label &get_breakLabel() { return breakLabel; }45 void set_breakLabel( Label newValue ) { breakLabel = newValue; }46 47 class Entry {48 public:49 50 loop( _loop ), contExit( _contExit ), breakExit( _breakExit ), contExitUsed( false ), breakExitUsed( false ) {}45 Label &get_breakLabel() { return breakLabel; } 46 void set_breakLabel( Label newValue ) { breakLabel = newValue; } 47 private: 48 class Entry { 49 public: 50 explicit Entry( Statement *_loop = 0, Label _contExit = Label(""), Label _breakExit = Label("") ) : 51 loop( _loop ), contExit( _contExit ), breakExit( _breakExit ), contExitUsed( false ), breakExitUsed( false ) {} 51 52 52 53 53 bool operator==( const Statement *stmt ) { return ( loop == stmt ); } 54 bool operator!=( const Statement *stmt ) { return ( loop != stmt ); } 54 55 55 56 bool operator==( const Entry &other ) { return ( loop == other.get_loop() ); } 56 57 57 58 Statement *get_loop() const { return loop; } 58 59 59 60 60 Label get_contExit() const { return contExit; } 61 void set_contExit( Label ); 61 62 62 63 63 Label get_breakExit() const { return breakExit; } 64 void set_breakExit( Label ); 64 65 65 private: 66 Statement *loop; 67 Label contExit, breakExit; 68 public: // hack, provide proper [sg]etters 69 bool contExitUsed, breakExitUsed; 66 private: 67 Statement *loop; 68 Label contExit, breakExit; 69 public: // hack, provide proper [sg]etters 70 bool contExitUsed, breakExitUsed; 71 }; 72 73 std::map< Label, Statement * > *targetTable; 74 std::list< Entry > enclosingBlocks, enclosingLoops, enclosingSwitches; 75 Label breakLabel; 76 LabelGenerator *generator; 70 77 }; 71 72 std::map< Label, Statement * > *targetTable;73 std::list< Entry > enclosingBlocks, enclosingLoops, enclosingSwitches;74 Label breakLabel;75 LabelGenerator *generator;76 };77 78 78 } // namespace ControlStruct 79 79 80 #endif 80 #endif // MLE_MUTATOR_H 81 81 82 /*83 Local Variables:84 mode: c++85 End:86 */87 82 // Local Variables: // 88 83 // tab-width: 4 // -
translator/ControlStruct/Mutate.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // Mutate.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:32:52 2015 13 // Update Count : 2 14 14 // 15 15 16 #include <algorithm> 16 17 #include <iostream> … … 34 35 35 36 namespace ControlStruct { 36 37 // ForExprMutator formut;38 LabelFixer lfix;39 ChooseMutator chmut;40 CaseRangeMutator ranges; // has to run after ChooseMutator41 //ExceptMutator exc;42 // LabelTypeChecker lbl;37 void mutate( std::list< Declaration * > translationUnit ) { 38 // ForExprMutator formut; 39 LabelFixer lfix; 40 ChooseMutator chmut; 41 CaseRangeMutator ranges; // has to run after ChooseMutator 42 //ExceptMutator exc; 43 // LabelTypeChecker lbl; 43 44 44 // mutateAll( translationUnit, formut );45 acceptAll( translationUnit, lfix );46 mutateAll( translationUnit, chmut );47 mutateAll( translationUnit, ranges );48 //mutateAll( translationUnit, exc );49 //acceptAll( translationUnit, lbl );50 45 // mutateAll( translationUnit, formut ); 46 acceptAll( translationUnit, lfix ); 47 mutateAll( translationUnit, chmut ); 48 mutateAll( translationUnit, ranges ); 49 //mutateAll( translationUnit, exc ); 50 //acceptAll( translationUnit, lbl ); 51 } 51 52 } // namespace CodeGen 53 52 54 // Local Variables: // 53 55 // tab-width: 4 // -
translator/ControlStruct/Mutate.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // Mutate.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 15:31:20 2015 13 // Update Count : 2 14 14 // 15 15 16 #ifndef CTRLS_MUTATE_H 16 17 #define CTRLS_MUTATE_H … … 22 23 23 24 namespace ControlStruct { 24 25 void mutate( std::list< Declaration* > translationUnit ); 25 26 } // namespace ControlStruct 26 27 27 28 #endif // CTRLS_MUTATE_H 28 29 29 /*30 Local Variables:31 mode: c++32 End:33 */34 30 // Local Variables: // 35 31 // tab-width: 4 // -
translator/Designators/Processor.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // Processor.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:23:10 2015 13 // Update Count : 2 14 14 // 15 15 16 #include <vector> 16 17 #include <algorithm> … … 20 21 21 22 namespace Designators { 22 Matcher::Matcher( const std::list< DeclarationWithType * > &decls ) { 23 int cnt = 0; 24 for ( std::list< DeclarationWithType * >::const_iterator i = decls.begin(); 25 i != decls.end(); i++, cnt++ ) { 26 std::string newName = (*i)->get_name(); 27 if ( table.find( newName ) == table.end() ) { 28 table.insert( std::pair<std::string, int>(newName, cnt) ); 29 order.push_back( newName ); 30 declarations.push_back( *i ); 31 alternatives.push_back( 0 ); 32 } 23 Matcher::Matcher( const std::list< DeclarationWithType * > &decls ) { 24 int cnt = 0; 25 for ( std::list< DeclarationWithType * >::const_iterator i = decls.begin(); 26 i != decls.end(); i++, cnt++ ) { 27 std::string newName = (*i)->get_name(); 28 if ( table.find( newName ) == table.end() ) { 29 table.insert( std::pair<std::string, int>(newName, cnt) ); 30 order.push_back( newName ); 31 declarations.push_back( *i ); 32 alternatives.push_back( 0 ); 33 } // if 34 } // for 33 35 } 34 }35 36 36 template< class InputIterator > 37 bool Matcher::add(InputIterator begin, InputIterator end, ResolvExpr::Alternative &alt ) { 38 while ( begin != end ) { 39 if ( table.find( *begin ) != table.end() ) 40 alternatives[ table[ *begin ] ] = new ResolvExpr::Alternative(alt); 41 else 42 return false; 43 begin++; 37 template< class InputIterator > 38 bool Matcher::add(InputIterator begin, InputIterator end, ResolvExpr::Alternative &alt ) { 39 while ( begin != end ) { 40 if ( table.find( *begin ) != table.end() ) 41 alternatives[ table[ *begin ] ] = new ResolvExpr::Alternative(alt); 42 else 43 return false; 44 begin++; 45 } // while 46 return true; 44 47 } 45 return true;46 }47 48 48 49 50 while ( begin != end )51 52 *out++ = declarations [ table[ *begin++ ] ];53 54 return false; // throw 0;55 return true;56 49 template< class InputIterator, class OutputIterator > 50 bool Matcher::slice(InputIterator begin, InputIterator end, OutputIterator out ) { 51 while ( begin != end ) 52 if ( table.find( *begin ) != table.end() ) 53 *out++ = declarations [ table[ *begin++ ] ]; 54 else 55 return false; // throw 0; 56 return true; 57 } 57 58 58 59 60 // fill call with defaults, if need be61 for (Matcher::iterator o = begin(); o != end(); o++ )62 63 return false;64 65 out++ = *alternatives[table[ *o ]];66 return true;67 59 template< class OutputIterator > 60 bool Matcher::get_reorderedCall( OutputIterator out ) { 61 // fill call with defaults, if need be 62 for (Matcher::iterator o = begin(); o != end(); o++ ) 63 if ( alternatives[ table[ *o ] ] == 0 ) 64 return false; 65 else 66 out++ = *alternatives[table[ *o ]]; 67 return true; 68 } 68 69 69 70 // Distribute `designation' over alternatives contained in `finder'71 if ( ! designation) return false;72 else73 74 alt->expr->set_argName( designation );75 return true;76 70 bool fixDesignations( ResolvExpr::AlternativeFinder &finder, Expression *designation ) { 71 // Distribute `designation' over alternatives contained in `finder' 72 if ( ! designation) return false; 73 else 74 for ( ResolvExpr::AlternativeFinder::iterator alt = finder.begin(); alt != finder.end(); alt++ ) 75 alt->expr->set_argName( designation ); 76 return true; 77 } 77 78 78 79 80 Expression *designator = expr->get_argName();81 if ( designator == 0 ) return false;79 template < class OutputIterator > 80 bool extractNames( Expression *expr, OutputIterator out, Matcher matcher ) { 81 Expression *designator = expr->get_argName(); 82 if ( designator == 0 ) return false; 82 83 83 if ( NameExpr *ndes = dynamic_cast<NameExpr *>(designator) )84 85 else if ( TupleExpr *tdes = dynamic_cast<TupleExpr *>(designator) ) {86 84 if ( NameExpr *ndes = dynamic_cast<NameExpr *>(designator) ) 85 out++ = ndes->get_name(); 86 else if ( TupleExpr *tdes = dynamic_cast<TupleExpr *>(designator) ) { 87 std::cerr << "Tuple designation" << std::endl; 87 88 // ObjectDecl *decl = extractTupleV(matcher, tdes); // xxx 88 89 90 n != tdes->get_exprs().end(); n++ ) {89 // transform? 90 for ( std::list< Expression * >::iterator n = tdes->get_exprs().begin(); 91 n != tdes->get_exprs().end(); n++ ) { 91 92 92 if ( NameExpr *name = dynamic_cast<NameExpr *>(*n) ) 93 out++ = name->get_name(); 94 else 95 // flatten nested Tuples 96 throw SemanticError( "Invalid tuple designation." ); 97 } 93 if ( NameExpr *name = dynamic_cast<NameExpr *>(*n) ) 94 out++ = name->get_name(); 95 else 96 // flatten nested Tuples 97 throw SemanticError( "Invalid tuple designation." ); 98 } // for 99 } // if 100 return true; 98 101 } 99 return true;100 }101 102 102 103 if ( NameExpr *name = dynamic_cast< NameExpr *>(expr) )104 105 else /* if () */106 107 103 std::string extractName( Expression *expr ) /* throw NoNameExtraction */ { 104 if ( NameExpr *name = dynamic_cast< NameExpr *>(expr) ) 105 return name->get_name(); 106 else /* if () */ 107 throw 0; 108 } 108 109 109 110 return 0;111 110 DeclarationWithType *gensym( DeclarationWithType *, std::string prefix ) { 111 return 0; 112 } 112 113 113 114 // extract a subtuple of the function `fun' argument list, corresponding to the tuple of names requested by115 // `nmTuple'.116 std::list< Expression * > &exprs = nmTuple->get_exprs();117 std::cerr << "In extractTupleV, the tuple has " << exprs.size() << " components." << std::endl;118 std::list< std::string > names;119 std::transform( exprs.begin(), exprs.end(), back_inserter(names), extractName );120 std::list< DeclarationWithType * > decls;121 matcher.slice( names.begin(), names.end(), back_inserter(decls) );122 //std::for_each( decls.begin(), decls.end(), gensym );123 std::cerr << "Returning declaration with " << decls.size() << " components." << std::endl;114 ObjectDecl *extractTupleV( Matcher matcher, TupleExpr *nmTuple ) { 115 // extract a subtuple of the function `fun' argument list, corresponding to the tuple of names requested by 116 // `nmTuple'. 117 std::list< Expression * > &exprs = nmTuple->get_exprs(); 118 std::cerr << "In extractTupleV, the tuple has " << exprs.size() << " components." << std::endl; 119 std::list< std::string > names; 120 std::transform( exprs.begin(), exprs.end(), back_inserter(names), extractName ); 121 std::list< DeclarationWithType * > decls; 122 matcher.slice( names.begin(), names.end(), back_inserter(decls) ); 123 //std::for_each( decls.begin(), decls.end(), gensym ); 124 std::cerr << "Returning declaration with " << decls.size() << " components." << std::endl; 124 125 125 return 0;//new ObjectDecl()126 126 return 0;//new ObjectDecl() 127 } 127 128 128 129 using namespace ResolvExpr;129 void check_alternative( FunctionType *fun, ResolvExpr::AltList &args ) { 130 using namespace ResolvExpr; 130 131 131 Matcher matcher( fun->get_parameters() ); 132 for ( AltList::iterator a = args.begin(); a != args.end(); a++ ) { 133 std::list< std::string > actNames; 134 if ( ! extractNames( a->expr, back_inserter(actNames), matcher ) ) { 135 return; // not a designated call, leave alternative alone 136 } else { 137 // see if there's a match 138 matcher.add( actNames.begin(), actNames.end(), *a ); 139 } 132 Matcher matcher( fun->get_parameters() ); 133 for ( AltList::iterator a = args.begin(); a != args.end(); a++ ) { 134 std::list< std::string > actNames; 135 if ( ! extractNames( a->expr, back_inserter(actNames), matcher ) ) { 136 return; // not a designated call, leave alternative alone 137 } else { 138 // see if there's a match 139 matcher.add( actNames.begin(), actNames.end(), *a ); 140 } // if 141 } // for 142 //AltList newArgs; 143 args.clear(); 144 matcher.get_reorderedCall( back_inserter(args) ); 145 146 return; 140 147 } 141 //AltList newArgs;142 args.clear();143 matcher.get_reorderedCall( back_inserter(args) );144 145 return;146 }147 148 #if 0 148 void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder &finder ) { 149 if ( expr->get_argName() != 0 ) { 150 // Looking at designated expression 151 using namespace ResolvExpr; 152 AltList &alternatives = finder.get_alternatives(); 153 std::cerr << "Now printing alternatives: " << std::endl; 154 for ( AltList::iterator a = alternatives.begin(); a != alternatives.end(); a++ ) 155 a->expr->print( std::cerr ); 156 //std::cerr << "Looking for constructions of length no more than: " << tdes->get_exprs().size() << "." << std::endl; 149 void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder &finder ) { 150 if ( expr->get_argName() != 0 ) { 151 // Looking at designated expression 152 using namespace ResolvExpr; 153 AltList &alternatives = finder.get_alternatives(); 154 std::cerr << "Now printing alternatives: " << std::endl; 155 for ( AltList::iterator a = alternatives.begin(); a != alternatives.end(); a++ ) 156 a->expr->print( std::cerr ); 157 //std::cerr << "Looking for constructions of length no more than: " << tdes->get_exprs().size() << "." << std::endl; 158 } 159 return; 157 160 } 158 return;159 }160 161 #endif // 0 161 162 } // namespaces Designators 163 162 164 // Local Variables: // 163 165 // tab-width: 4 // -
translator/Designators/Processor.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // Processor.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:24:34 2015 13 // Update Count : 3 14 14 // 15 15 16 #include "SynTree/Declaration.h" 16 17 #include "SynTree/Expression.h" … … 24 25 25 26 namespace Designators { 26 27 27 class Matcher; 28 class GenSym; 28 29 29 30 31 32 33 34 30 template < class OutputIterator > bool extractNames( std::list< DeclarationWithType * > &, OutputIterator ); 31 template < class OutputIterator > bool extractNames( Expression *, OutputIterator, Matcher ); 32 void check_alternative( FunctionType *, ResolvExpr::AltList & ); 33 ObjectDecl *extractTupleV( Matcher, TupleExpr *names ); 34 bool fixDesignations( ResolvExpr::AlternativeFinder &finder, Expression *designation ); 35 DeclarationWithType *gensym( GenSym &, DeclarationWithType * ); 35 36 36 37 public:38 39 37 class GenSym { 38 public: 39 GenSym( std::string prefix = "" ) : gensym_count(0) {} 40 GenSym( GenSym &other ) { gensym_count = other.gensym_count; } 40 41 41 42 // std::string get_symbol() { } 42 private:43 44 45 43 private: 44 std::string prefix; 45 int gensym_count; 46 }; 46 47 47 48 49 public:50 48 // template< typename Key > 49 class Matcher { 50 public: 51 typedef std::vector< std::string >::iterator iterator; 51 52 52 53 53 Matcher( const std::list< DeclarationWithType * > & ); 54 ~Matcher() {} 54 55 55 56 57 58 56 template< class OutputIterator > bool get_reorderedCall( OutputIterator ); 57 template< class InputIterator > bool add(InputIterator, InputIterator, ResolvExpr::Alternative &); 58 template< class InputIterator, class OutputIterator > bool slice(InputIterator begin, InputIterator end, OutputIterator ); 59 //std::vector<std::string> &get_order() const { return order; } 59 60 60 61 61 iterator begin() { return order.begin(); } 62 iterator end() { return order.end(); } 62 63 63 //Expression *operator[]( int idx ) { return table( order[ idx ] ); } 64 private: 65 std::map< std::string, int > table; 66 std::vector<std::string> order; 67 std::vector<DeclarationWithType *> declarations; 68 std::vector<ResolvExpr::Alternative *> alternatives; 69 }; 70 // void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder & ); 71 64 //Expression *operator[]( int idx ) { return table( order[ idx ] ); } 65 private: 66 std::map< std::string, int > table; 67 std::vector<std::string> order; 68 std::vector<DeclarationWithType *> declarations; 69 std::vector<ResolvExpr::Alternative *> alternatives; 70 }; 71 // void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder & ); 72 72 } // namespace Designators 73 73 74 /*75 Local Variables:76 mode: c++77 End:78 */79 74 // Local Variables: // 80 75 // tab-width: 4 // -
translator/GenPoly/ScrubTyVars.cc
r01aeade ra08ba92 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 07:47:31201513 // Update Count : 112 // Last Modified On : Tue May 19 16:42:42 2015 13 // Update Count : 2 14 14 // 15 15 … … 22 22 23 23 namespace GenPoly { 24 24 Type * ScrubTyVars::mutate( TypeInstType *typeInst ) { 25 25 TyVarMap::const_iterator tyVar = tyVars.find( typeInst->get_name() ); 26 26 if ( doAll || tyVar != tyVars.end() ) { … … 39 39 } // if 40 40 return typeInst; 41 41 } 42 42 43 43 Expression * ScrubTyVars::mutate( SizeofExpr *szeof ) { 44 44 // sizeof( T ) => T parameter, which is the size of T 45 45 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( szeof->get_type() ) ) { … … 49 49 return Mutator::mutate( szeof ); 50 50 } // if 51 51 } 52 52 53 53 Type * ScrubTyVars::mutate( PointerType *pointer ) { 54 54 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( pointer->get_base() ) ) { 55 55 if ( doAll || tyVars.find( typeInst->get_name() ) != tyVars.end() ) { … … 62 62 } // if 63 63 return Mutator::mutate( pointer ); 64 64 } 65 65 } // namespace GenPoly 66 66 -
translator/InitTweak/Association.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // Association.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:25:09 2015 13 // Update Count : 1 14 14 // 15 15 16 #include "Association.h" 16 17 … … 22 23 const int RangeAssociation::RangeAssociation::UNDEF = -1; 23 24 RangeAssociation::~RangeAssociation() {} 25 24 26 // Local Variables: // 25 27 // tab-width: 4 // -
translator/InitTweak/Association.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // Association.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 15 #ifndef _ASSOCIATE_H_ 16 #define _ASSOCIATE_H_ 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:27:09 2015 13 // Update Count : 2 14 // 15 16 #ifndef _ASSOCIATION_H_ 17 #define _ASSOCIATION_H_ 17 18 18 19 #include <map> … … 34 35 // ** exceptions 35 36 class AssocException : public std::exception { 36 public:37 38 39 40 41 42 43 private:44 37 public: 38 AssocException() {} 39 AssocException( std::string _what ) : what( _what ) {} 40 ~AssocException() throw () {} 41 42 std::string get_what() const { return what; } 43 void set_what( std::string newValue ) { what = newValue; } 44 private: 45 std::string what; 45 46 }; 46 47 47 48 // ** visitors 48 49 class AssociationVisitor { 49 public:50 51 52 53 54 55 50 public: 51 AssociationVisitor() {} 52 virtual ~AssociationVisitor() {} 53 54 virtual void visit( SingleName * ) = 0; 55 virtual void visit( PointAssociation * ) = 0; 56 virtual void visit( RangeAssociation * ) = 0; 56 57 }; 57 58 58 59 // ** containers 59 60 class Association { 60 public:61 62 63 64 65 66 67 68 69 70 71 72 73 61 public: 62 virtual ~Association(); 63 64 virtual Association *clone() = 0; 65 virtual long int add_single( std::string, Expression *) = 0; 66 virtual long int add_single( long int, Expression *expr) = 0; 67 68 virtual Association *operator[]( int idx ) = 0; 69 virtual Association *operator[]( std::string ) = 0; 70 71 // virtual AssociationIterator *get_iterator() = 0; 72 73 virtual void accept( AssociationVisitor & ) = 0; 74 virtual void display( std::ostream & ) = 0; 74 75 }; 75 76 76 77 class SingleName : public Association { 77 public:78 79 80 81 82 83 84 85 86 87 88 89 90 91 virtual long int add_single( std::string str, Expression *newExpr) {92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 private:108 109 78 public: 79 SingleName( Expression *initExpr = 0 ) : expr( initExpr ) {} 80 virtual ~SingleName(); 81 82 virtual SingleName *clone() { 83 return 0; // XXX! 84 } 85 86 virtual long int add_single( long int idx, Expression *newExpr) { 87 if ( expr == 0 ) //|| *expr == *newExpr ) 88 expr = newExpr; 89 return 0; 90 } 91 92 virtual long int add_single( std::string str, Expression *newExpr ) { 93 if ( expr == 0 ) //|| *expr == *newExpr ) 94 expr = newExpr; 95 return 0; 96 } 97 98 virtual Association *operator[]( int idx ) { assert(false); } 99 virtual Association *operator[]( std::string idx ) { assert(false); } 100 101 virtual void accept( AssociationVisitor &v ) { v.visit( this ); } 102 virtual void display( std::ostream &os ) { 103 os << "Single association" << std::endl; 104 } 105 106 Expression *get_expr() const { return expr; } 107 108 private: 109 Expression *expr; 110 Expression *deflt; 110 111 }; 111 112 112 113 class PointAssociation : public Association { 113 public:114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 } 139 140 141 142 143 144 145 146 147 148 throw AssocException("No such member");149 150 return add_single( j->second.first, expr );151 } 152 153 154 155 156 157 158 159 160 161 162 } 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 virtual AssociationIterator *get_iterator() {194 195 196 }197 198 199 200 201 202 203 204 205 206 i->second.second->display( os );207 208 std::cerr << "No recursive association" << std::endl;209 210 211 } 212 213 214 215 216 private:217 218 219 220 114 public: 115 typedef std::map< std::string, std::pair< long int, Association *> > map_type; 116 117 PointAssociation() {} 118 PointAssociation( const PointAssociation &other ) { 119 copy( other.anonym.begin(), other.anonym.end(), back_inserter( anonym )); 120 } 121 122 virtual ~PointAssociation(); 123 124 virtual PointAssociation *clone() { 125 return ( new PointAssociation( *this ) ); 126 } 127 128 virtual long int add_single( long int idx, Expression *expr) { 129 long int ret; 130 131 if ( idx >= (long int)ordering.size() ) throw AssocException("extra (spurious) members"); 132 133 if ( ordering[ idx ] == "") 134 std::cerr << "Checkpoint 2" << std::endl; 135 else { 136 assert( table[ordering[idx]].second != 0 ); 137 ret = idx; 138 table[ ordering[idx] ].second->add_single("", expr ); 139 } // if 140 return ret; 141 } 142 143 virtual long int add_single( std::string idx, Expression *expr) { 144 if ( idx == "" ) 145 std::cerr << "Checkpoint 1" << std::endl; 146 else { 147 map_type::iterator j; 148 if ( (j = table.find( idx )) == table.end() ) // this doesn't amount for reachable members deeper down the structure, fix 149 throw AssocException("No such member"); 150 else 151 return add_single( j->second.first, expr ); 152 } // if 153 154 return -1; 155 } 156 157 void add_member( std::string str ) { 158 if ( table.find( str ) != table.end() ) return; 159 ordering.push_back( str ); 160 if ( str != "" ) { 161 std::pair<long int, Association *> p( ordering.size() - 1, 0 ); 162 table.insert( std::pair< std::string, std::pair<long int, Association *> >(str, p) ); 163 } // if 164 return; 165 } 166 167 virtual void set_member( std::string str, Association *assoc ) { 168 if ( str == "" ) 169 anonym.push_back( assoc ); 170 else if ( table.find( str ) == table.end() ) 171 throw AssocException( "no such member" ); 172 else 173 table[ str ] = std::pair<long int, Association *>(ordering.size() - 1, assoc); 174 175 return; 176 } 177 178 virtual Association *operator[]( int idx ) { 179 if ( ordering[idx] == "" ) { 180 std::cerr << "Error, anonymous members not implemented yet" << std::endl; 181 throw 0; 182 } else 183 return table[ ordering[idx] ].second; 184 } 185 186 virtual Association *operator[]( std::string idx ) { 187 if ( table.find( idx ) == table.end() ) 188 throw AssocException("Member not found"); 189 else 190 return table[ idx ].second; 191 } 192 193 /* 194 virtual AssociationIterator *get_iterator() { 195 PointAssocIterator *it; 196 return it; 197 } 198 */ 199 200 void accept( AssociationVisitor &v ) { v.visit( this ); } 201 202 virtual void display( std::ostream &os ) { 203 os << "Point association: " << std::endl; 204 for ( map_type::iterator i = table.begin(); i != table.end(); i++ ) { 205 os << "Member [" << i->first << ", index = " << i->second.first << "]"; 206 if ( i->second.second != 0 ) 207 i->second.second->display( os ); 208 else 209 std::cerr << "No recursive association" << std::endl; 210 211 os << std::endl; 212 } // for 213 } 214 215 const int size() const { return ordering.size(); } 216 217 private: 218 PointAssociation &operator=(const PointAssociation &); 219 std::vector<std::string> ordering; 220 std::list< Association * > anonym; 221 std::map< std::string, std::pair<long int, Association *> > table; 221 222 }; 222 223 223 224 class RangeAssociation : public Association { 224 public:225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 virtual AssociationIterator *get_iterator() {244 245 246 }247 248 249 250 251 252 253 254 255 256 private:257 258 259 260 for ( diet_tree<int>::iterator i = tree.begin(); i != tree.end(); i++ )261 262 diet_tree<int> tree;263 tree.insert(100,200);264 225 public: 226 static const int UNDEF; 227 RangeAssociation( int _hi= UNDEF ) : hi( _hi ) { 228 std::cerr << "Constructed RangeAssociation with: [" << hi << "]" << std::endl; 229 } 230 231 virtual ~RangeAssociation(); 232 233 virtual RangeAssociation *clone() { 234 return 0; // XXX !!!! 235 } 236 237 virtual Association *operator[]( int idx ) { 238 return 0; // XXX !!! 239 } 240 241 virtual Association *operator[]( std::string idx ) { assert(false); return 0; } 242 243 /* 244 virtual AssociationIterator *get_iterator() { 245 RangeAssocIterator *it; 246 return it; 247 } 248 */ 249 250 virtual long int add_single( long int idx, Expression *newExpr) { return 0; } 251 virtual long int add_single( std::string, Expression *) { return 0; } 252 void accept( AssociationVisitor &v ) { v.visit( this ); } 253 virtual void display( std::ostream &os ) { 254 os << "Range association, with limit: " << std::endl; 255 } 256 257 private: 258 int hi; 259 diet::diet_tree<int> tree; 260 /* 261 for ( diet_tree<int>::iterator i = tree.begin(); i != tree.end(); i++ ) 262 std::cout << "--(" << (*i).first << ", " << (*i).second << ")--" << std::endl; 263 diet_tree<int> tree; 264 tree.insert(100,200); 265 */ 265 266 }; 266 267 267 268 // ** builders 268 269 class AssociationBuilder { 269 public:270 271 272 273 274 270 public: 271 /* AssociationBuilder( Declaration * ) */ 272 virtual ~AssociationBuilder() {} 273 virtual Association *get_assoc() = 0; 274 virtual Association *grab_assoc() = 0; 275 virtual void set_assoc( Association * ) = 0; 275 276 }; 276 277 277 278 class AssociationFiller { 278 public: 279 // AssociationFiller( Declaration * ) {} 280 virtual ~AssociationFiller() {} 281 virtual Association *get_assoc() = 0; 282 virtual void set_assoc( Association * ) = 0; 283 }; 284 285 #endif //#define _ASSOCIATE_H_ 286 287 /* 288 Local Variables: 289 mode: c++ 290 End: 291 */ 279 public: 280 // AssociationFiller( Declaration * ) {} 281 virtual ~AssociationFiller() {} 282 virtual Association *get_assoc() = 0; 283 virtual void set_assoc( Association * ) = 0; 284 }; 285 286 #endif // _ASSOCIATION_H_ 287 292 288 // Local Variables: // 293 289 // tab-width: 4 // -
translator/InitTweak/BasicInit.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // BasicInit.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:30:43 2015 13 // Update Count : 1 14 // 15 15 16 #include <list> 16 17 #include <cassert> … … 32 33 33 34 namespace InitTweak { 34 35 CompoundStmt* BasicInit::mutate(CompoundStmt *compoundStmt) 36 { 37 index.visit( compoundStmt ); 38 39 std::list< Statement * > &kids = compoundStmt->get_kids(); 40 std::list< Statement * > newKids; 41 42 for ( std::list< Statement * >::iterator i = kids.begin(); i!= kids.end(); i++ ) { 43 //BasicInit newMut( ); 44 (*i)->acceptMutator( *this ); 45 newKids.push_back( *i ); 46 47 if ( has_bindings() ) { // if ( get_bindings() != 0 ) { 48 std::list< Statement *> newSt = get_statements(); 49 //newSt.push_back( *i ); 50 newKids.splice( newKids.end(), newSt ); 51 bindings = 0; 52 stmts.clear(); 53 } 54 } 55 56 compoundStmt->get_kids() = newKids; 57 return compoundStmt; 58 } 59 60 Statement * BasicInit::mutate(DeclStmt *declStmt) { 61 62 declStmt->accept( index ); 63 64 ObjectDecl *odecl = 0; 65 66 if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){ 67 68 Initializer *init = odecl->get_init(); 69 if ( init == 0 ) return declStmt; 70 71 if ( Classify::type( odecl->get_type() ) == Classify::COMPOUND_T ) 72 if ( Classify::initializer(init) == Classify::SINGLE_I ) 73 throw( 0 ); // mismatch of type and initializer 74 else { 75 NameInCollection *col = Classify::declaration( odecl, &index ); 76 bindings = NameAssociation< Expression *, BreakInitializer >::createNameAssoc(col); 77 bindings->add_value( std::string(""), BreakInitializer(init) ); 78 BasicInit::build_statements( bindings, odecl->get_name(), stmts ); 79 } 80 else 81 if ( Classify::initializer(init) == Classify::COMPOUND_I ) 82 throw( 0 ); // mismatch of type and initializer 83 else { 84 // Single inits 85 SingleInit *sinit = dynamic_cast< SingleInit * > ( init ); 86 assert( sinit != 0); 87 88 std::list<Expression *> args; 89 args.push_back( new AddressExpr( new NameExpr( odecl->get_name() )) ); // have to get address of object 90 args.push_back( sinit->get_value() ); 91 // replace declaration with initialization 92 stmts.push_back(new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args))); 93 } 94 95 delete init; 96 odecl->set_init( 0 ); 97 } else { 98 // no declaration statement 99 } 100 101 return declStmt; 102 } 103 104 ExprStmt *assignFromDecl( DeclStmt *declStmt ) 105 { 106 ObjectDecl *decl; 107 if ( (decl = dynamic_cast<ObjectDecl *>( declStmt->get_decl() )) != 0 ) 108 { 109 SingleInit *init; 110 if ( (init = dynamic_cast<SingleInit *>(decl->get_init())) != 0 ) 111 { 112 } 113 } 114 115 return 0; 116 } 117 118 bool isDeclStmtP(Statement *stmt) 119 { 120 return ( dynamic_cast< DeclStmt *>(stmt) != 0 ); 121 } 122 123 BasicInit::Classify::TypeKind BasicInit::Classify::type( Type *toClassify ) { 124 if ( toClassify == 0 ) return NULL_T; 125 126 if ( dynamic_cast< StructInstType * >(toClassify) || 127 dynamic_cast< UnionInstType * >(toClassify) || 128 dynamic_cast< ArrayType * >(toClassify) ) 129 return COMPOUND_T; 130 else 131 return SINGLE_T; 132 } 133 134 BasicInit::Classify::InitKind BasicInit::Classify::initializer( Initializer *init) { 135 if ( init == 0 ) return NULL_I; 136 if ( dynamic_cast< ListInit * >(init) ) 137 return COMPOUND_I; 138 if ( dynamic_cast< SingleInit * >(init) ) 139 return SINGLE_I; 140 141 return NULL_I; // shouldn't be here anyways 142 } 143 144 NameInCollection * 145 BasicInit::Classify::declaration( ObjectDecl *objdecl, SymTab::Indexer *index ) { 146 assert ( index != 0 ); 147 148 ReferenceToType *reftype; 149 if ( (reftype = dynamic_cast< StructInstType * >( objdecl->get_type() )) != 0 ){ 150 StructDecl *strDecl = index->lookupStruct( reftype->get_name() ); 151 if ( strDecl != 0 ) { 152 NameCollectionBuilder bld; 153 NameCollector nc( bld ); 154 strDecl->accept( nc ); 155 NameInCollection *col = nc.get_builder().get_collection(); 156 nc.get_builder().set_collection( 0 ); 157 158 return col; 159 } else 160 throw( SemanticError( std::string("No structure of name: ") + reftype->get_name() ) ); 161 } else { 162 throw(SemanticError( reftype->get_name() + std::string("is not a reference to type") )); 163 return 0; 164 } 165 } 166 167 std::list< Statement * > 168 BasicInit::Classify::matchInit( NameInCollection *col, 169 ObjectDecl *toInitialize, 170 Initializer *init ) { 171 assert ( col != 0 ); 172 173 std::list< Statement * > arranged(0); //( col->size() ); 174 std::fill( arranged.begin(), arranged.end(), (Statement *)0 ); 175 int current = 0; 176 177 if ( init == 0 ) 178 // no initializer... shouldn't even bother... fix this. 179 return arranged; 180 181 { 182 ListInit *li = dynamic_cast< ListInit * >( init ); 183 184 if ( li != 0 ) { 185 for ( std::list<Initializer *>::iterator i = li->begin_initializers(); 186 i != li->end_initializers(); 187 i++) { 188 std::list<Expression *> d_orig = (*i)->get_designators(); 189 190 NameInCollection *corrsp; // corresponding element to this initializer 191 if ( ! d_orig.empty() ) { 192 // 1) has designators 193 std::list<NameExpr *> des; 194 std::transform( d_orig.begin(), d_orig.end(), 195 std::back_inserter( des ), cast_ptr<Expression, NameExpr > ); 196 197 for ( std::list<NameExpr *>::iterator j = des.begin(); j != des.end(); j++ ) { 198 // check for existence of the element 199 200 if ( (corrsp = (*col)[ (*j)->get_name() ]) != 0 ) { 201 // current++; 202 SingleInit *sinit; 203 if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 ) 204 arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) ); 35 CompoundStmt* BasicInit::mutate(CompoundStmt *compoundStmt) { 36 index.visit( compoundStmt ); 37 38 std::list< Statement * > &kids = compoundStmt->get_kids(); 39 std::list< Statement * > newKids; 40 41 for ( std::list< Statement * >::iterator i = kids.begin(); i!= kids.end(); i++ ) { 42 //BasicInit newMut( ); 43 (*i)->acceptMutator( *this ); 44 newKids.push_back( *i ); 45 46 if ( has_bindings() ) { // if ( get_bindings() != 0 ) { 47 std::list< Statement *> newSt = get_statements(); 48 //newSt.push_back( *i ); 49 newKids.splice( newKids.end(), newSt ); 50 bindings = 0; 51 stmts.clear(); 52 } // if 53 } // for 54 55 compoundStmt->get_kids() = newKids; 56 return compoundStmt; 57 } 58 59 Statement * BasicInit::mutate(DeclStmt *declStmt) { 60 declStmt->accept( index ); 61 62 ObjectDecl *odecl = 0; 63 64 if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ) { 65 Initializer *init = odecl->get_init(); 66 if ( init == 0 ) return declStmt; 67 68 if ( Classify::type( odecl->get_type() ) == Classify::COMPOUND_T ) 69 if ( Classify::initializer(init) == Classify::SINGLE_I ) 70 throw( 0 ); // mismatch of type and initializer 71 else { 72 NameInCollection *col = Classify::declaration( odecl, &index ); 73 bindings = NameAssociation< Expression *, BreakInitializer >::createNameAssoc(col); 74 bindings->add_value( std::string(""), BreakInitializer(init) ); 75 BasicInit::build_statements( bindings, odecl->get_name(), stmts ); 76 } // if 77 else 78 if ( Classify::initializer(init) == Classify::COMPOUND_I ) 79 throw( 0 ); // mismatch of type and initializer 80 else { 81 // Single inits 82 SingleInit *sinit = dynamic_cast< SingleInit * > ( init ); 83 assert( sinit != 0); 84 85 std::list<Expression *> args; 86 args.push_back( new AddressExpr( new NameExpr( odecl->get_name() )) ); // have to get address of object 87 args.push_back( sinit->get_value() ); 88 // replace declaration with initialization 89 stmts.push_back(new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args))); 90 } // if 91 92 delete init; 93 odecl->set_init( 0 ); 94 } else { 95 // no declaration statement 96 } // if 97 98 return declStmt; 99 } 100 101 ExprStmt *assignFromDecl( DeclStmt *declStmt ) { 102 ObjectDecl *decl; 103 if ( (decl = dynamic_cast<ObjectDecl *>( declStmt->get_decl() )) != 0 ) { 104 SingleInit *init; 105 if ( (init = dynamic_cast<SingleInit *>(decl->get_init())) != 0 ) { 106 } // if 107 } // if 108 109 return 0; 110 } 111 112 bool isDeclStmtP(Statement *stmt) { 113 return ( dynamic_cast< DeclStmt *>(stmt) != 0 ); 114 } 115 116 BasicInit::Classify::TypeKind BasicInit::Classify::type( Type *toClassify ) { 117 if ( toClassify == 0 ) return NULL_T; 118 119 if ( dynamic_cast< StructInstType * >(toClassify) || 120 dynamic_cast< UnionInstType * >(toClassify) || 121 dynamic_cast< ArrayType * >(toClassify) ) 122 return COMPOUND_T; 205 123 else 206 ; // recursive call to matchInit 207 } else 208 // error, member doesn't exist 209 return arranged; // throw( 0 ); // XXX 210 } 211 } else { 212 // 2) doesn't have designators 213 if ( (corrsp = (*col)[ current++ ]) != 0 ) { 214 SingleInit *sinit; 215 if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 ) 216 arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) ); 217 else 218 ; // recursive call to matchInit 219 } else { 220 // Shouldn't be here... probably too many elements in initializer? 221 } 222 } 223 } 224 } 225 } 226 227 return arranged; 228 } 229 230 Statement *BasicInit::Classify::constructAssgn( std::string membName, ObjectDecl *toInit, SingleInit *sinit ) { 231 std::list< Expression * > args; 232 args.push_back(new AddressExpr( new UntypedMemberExpr( membName, new NameExpr(toInit->get_name()) ))); 233 args.push_back( sinit->get_value() ); 234 Statement *ret = new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)); 235 return ret; 236 } 237 238 void BasicInit::build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, 239 std::string aggName, std::list< Statement *> &stmts ) { 240 assert( assoc != 0 ); 241 static std::list< std::string > prefix; 242 243 NameInCollection *col = assoc->get_collection(); 244 if ( col->isComposite() ) { 245 VariousNames *vc = dynamic_cast< VariousNames * >( col ); 246 for ( VariousNames::key_iterator it = vc->keys_begin(); it != vc->keys_end(); it++ ) { 247 prefix.push_back( *it ); 248 if ( (*assoc)[ *it ] != 0 ) 249 build_statements( (*assoc)[ *it ], aggName, stmts ); 250 251 prefix.pop_back(); 252 } 253 } else { 254 SingleNameAssoc< Expression *, BreakInitializer > *sa = \ 255 dynamic_cast< SingleNameAssoc< Expression *, BreakInitializer > * >( assoc ); 256 assert( sa != 0 ); 257 258 Expression * rh = sa->get_data(); 259 260 if (rh != 0) { 261 // construct assignment statement list 262 Expression *lh = new NameExpr ( aggName ); 263 for ( std::list< std::string >::iterator i = prefix.begin(); i != prefix.end(); i++ ) 264 lh = new UntypedMemberExpr( *i, lh ); 265 266 std::list< Expression * > args; 267 args.push_back( new AddressExpr(lh) ); args.push_back( rh ); 268 269 stmts.push_back( new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)) ); 270 } 271 } 272 273 return; 274 } 275 124 return SINGLE_T; 125 } 126 127 BasicInit::Classify::InitKind BasicInit::Classify::initializer( Initializer *init) { 128 if ( init == 0 ) return NULL_I; 129 if ( dynamic_cast< ListInit * >(init) ) 130 return COMPOUND_I; 131 if ( dynamic_cast< SingleInit * >(init) ) 132 return SINGLE_I; 133 134 return NULL_I; // shouldn't be here anyways 135 } 136 137 NameInCollection * BasicInit::Classify::declaration( ObjectDecl *objdecl, SymTab::Indexer *index ) { 138 assert ( index != 0 ); 139 140 ReferenceToType *reftype; 141 if ( (reftype = dynamic_cast< StructInstType * >( objdecl->get_type() )) != 0 ) { 142 StructDecl *strDecl = index->lookupStruct( reftype->get_name() ); 143 if ( strDecl != 0 ) { 144 NameCollectionBuilder bld; 145 NameCollector nc( bld ); 146 strDecl->accept( nc ); 147 NameInCollection *col = nc.get_builder().get_collection(); 148 nc.get_builder().set_collection( 0 ); 149 150 return col; 151 } else 152 throw( SemanticError( std::string("No structure of name: ") + reftype->get_name() ) ); 153 } else { 154 throw(SemanticError( reftype->get_name() + std::string("is not a reference to type") )); 155 return 0; 156 } // if 157 } 158 159 std::list< Statement * > 160 BasicInit::Classify::matchInit( NameInCollection *col, ObjectDecl *toInitialize, Initializer *init ) { 161 assert ( col != 0 ); 162 163 std::list< Statement * > arranged(0); //( col->size() ); 164 std::fill( arranged.begin(), arranged.end(), (Statement *)0 ); 165 int current = 0; 166 167 if ( init == 0 ) 168 // no initializer... shouldn't even bother... fix this. 169 return arranged; 170 171 ListInit *li = dynamic_cast< ListInit * >( init ); 172 173 if ( li != 0 ) { 174 for ( std::list<Initializer *>::iterator i = li->begin_initializers(); 175 i != li->end_initializers(); 176 i++) { 177 std::list<Expression *> d_orig = (*i)->get_designators(); 178 179 NameInCollection *corrsp; // corresponding element to this initializer 180 if ( ! d_orig.empty() ) { 181 // 1) has designators 182 std::list<NameExpr *> des; 183 std::transform( d_orig.begin(), d_orig.end(), 184 std::back_inserter( des ), cast_ptr<Expression, NameExpr > ); 185 186 for ( std::list<NameExpr *>::iterator j = des.begin(); j != des.end(); j++ ) { 187 // check for existence of the element 188 189 if ( (corrsp = (*col)[ (*j)->get_name() ]) != 0 ) { 190 // current++; 191 SingleInit *sinit; 192 if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 ) 193 arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) ); 194 else 195 ; // recursive call to matchInit 196 } else 197 // error, member doesn't exist 198 return arranged; // throw( 0 ); // XXX 199 } 200 } else { 201 // 2) doesn't have designators 202 if ( (corrsp = (*col)[ current++ ]) != 0 ) { 203 SingleInit *sinit; 204 if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 ) 205 arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) ); 206 else 207 ; // recursive call to matchInit 208 } else { 209 // Shouldn't be here... probably too many elements in initializer? 210 } // if 211 } // if 212 } // for 213 } // if 214 215 return arranged; 216 } 217 218 Statement *BasicInit::Classify::constructAssgn( std::string membName, ObjectDecl *toInit, SingleInit *sinit ) { 219 std::list< Expression * > args; 220 args.push_back(new AddressExpr( new UntypedMemberExpr( membName, new NameExpr(toInit->get_name()) ))); 221 args.push_back( sinit->get_value() ); 222 Statement *ret = new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)); 223 return ret; 224 } 225 226 void BasicInit::build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, std::string aggName, std::list< Statement *> &stmts ) { 227 assert( assoc != 0 ); 228 static std::list< std::string > prefix; 229 230 NameInCollection *col = assoc->get_collection(); 231 if ( col->isComposite() ) { 232 VariousNames *vc = dynamic_cast< VariousNames * >( col ); 233 for ( VariousNames::key_iterator it = vc->keys_begin(); it != vc->keys_end(); it++ ) { 234 prefix.push_back( *it ); 235 if ( (*assoc)[ *it ] != 0 ) 236 build_statements( (*assoc)[ *it ], aggName, stmts ); 237 238 prefix.pop_back(); 239 } 240 } else { 241 SingleNameAssoc< Expression *, BreakInitializer > *sa = \ 242 dynamic_cast< SingleNameAssoc< Expression *, BreakInitializer > * >( assoc ); 243 assert( sa != 0 ); 244 245 Expression * rh = sa->get_data(); 246 247 if (rh != 0) { 248 // construct assignment statement list 249 Expression *lh = new NameExpr ( aggName ); 250 for ( std::list< std::string >::iterator i = prefix.begin(); i != prefix.end(); i++ ) 251 lh = new UntypedMemberExpr( *i, lh ); 252 253 std::list< Expression * > args; 254 args.push_back( new AddressExpr(lh) ); args.push_back( rh ); 255 256 stmts.push_back( new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)) ); 257 } // if 258 } // if 259 260 return; 261 } 276 262 } // namespace InitTweak 277 263 -
translator/InitTweak/BasicInit.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // BasicInit.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:32:21 2015 13 // Update Count : 3 14 // 15 15 16 #ifndef _BASINIT_H_ 16 17 #define _BASINIT_H_ … … 28 29 29 30 namespace InitTweak { 30 31 bool isDeclStmtP(Statement *stmt); 32 33 class BreakInitializer; 34 class BreakDesignator; 35 36 class BasicInit: public Mutator 37 { 38 public: 39 BasicInit() : bindings( 0 ) {} 40 BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {} 41 BasicInit( const BasicInit &other ) { 42 bindings = other.get_bindings(); 43 index = other.index; 44 } 45 46 ~BasicInit() { /* delete bindings; bindings = 0; */ } 47 48 NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; } 49 void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) { 50 bindings = newValue; 51 } 52 53 bool has_bindings() { 54 return ( get_bindings() != 0 || ! stmts.empty() ); 55 } 56 57 virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) 58 { index.visit( objectDecl ); return objectDecl; } 59 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 60 { index.visit( typeDecl ); return typeDecl; } 61 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 62 { index.visit( typeDecl ); return typeDecl; } 63 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 64 { index.visit( aggregateDecl ); return aggregateDecl; } 65 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 66 { index.visit( aggregateDecl ); return aggregateDecl; } 67 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 68 { index.visit( aggregateDecl ); return aggregateDecl; } 69 70 virtual Type *mutate( StructInstType *aggrInst ) 71 { index.visit( aggrInst ); return aggrInst; } 72 virtual Type *mutate( UnionInstType *aggrInst ) 73 { index.visit( aggrInst ); return aggrInst; } 74 75 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 76 virtual Statement *mutate(DeclStmt *declStmt); 77 78 std::list< Statement *> get_statements() const { return stmts; } 79 80 static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, 81 std::string aggName, std::list< Statement *> &stmts ); 82 private: 83 NameAssociation< Expression *, BreakInitializer > *bindings; 84 Statement *assignFromDecl( DeclStmt *declStmt ); 85 SymTab::Indexer index; 86 std::list< Statement *> stmts; 87 88 class Classify { 89 public: 90 enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T }; 91 enum InitKind { NULL_I, SINGLE_I, COMPOUND_I }; 92 93 static TypeKind type( Type * ); 94 static InitKind initializer( Initializer *); 95 96 static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index ); 97 static std::list< Statement * > 98 matchInit( NameInCollection *, ObjectDecl *, Initializer * ); 99 static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit ); 100 101 // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc ); 102 }; 103 }; 104 105 class BreakInitializer { 106 enum InitKind { EMPTY, SINGLE, COMPOUND }; 107 108 class BreakDesignator; 109 typedef BreakDesignator NameSplitter; 110 111 public: 112 typedef std::list<Initializer *>::iterator element_iterator; 113 typedef std::list< NameSplitter >::iterator name_iterator; 114 115 BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) { 116 std::list<Expression *> temp; 117 118 if ( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) { 119 kind = SINGLE; 120 temp = sinit->get_designators(); 121 } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) { 122 kind = COMPOUND; 123 temp = cinit->get_designators(); 124 } 125 126 std::transform( temp.begin(), temp.end(), std::back_inserter( designators ), 127 ctor_noptr<NameSplitter, Expression *> ); 128 129 } 130 131 //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; } 132 ~BreakInitializer () {} 133 134 BreakInitializer set_name( NameSplitter &name ) { 135 designators.clear(); 136 designators.push_back( name ); 137 138 return *this; 139 } 140 141 element_iterator element_begin() { 142 assert( cinit != 0 ); 143 return cinit->begin_initializers(); 144 } 145 element_iterator element_end() { 146 assert( cinit != 0 ); 147 return cinit->end_initializers(); 148 } 149 150 name_iterator names_begin() { return designators.begin(); } 151 name_iterator names_end() { return designators.end(); } 152 153 int names_size() const { return designators.size(); } 154 155 bool has_index() const { return ! designators.empty(); } 156 bool is_single() const { return kind == SINGLE; } 157 bool is_composite() const { return kind == COMPOUND; } 158 159 Expression *get_value() { 160 switch ( kind ) { 161 case EMPTY: 162 return 0; 163 break; 164 case SINGLE: 165 return sinit->get_value(); 166 break; 167 case COMPOUND: 168 assert(false); 169 break; 170 default: 171 assert(false); 172 } 173 return 0; 174 } 175 176 // attributes 177 private: 178 InitKind kind; 179 SingleInit *sinit; 180 ListInit *cinit; 181 std::list< BreakDesignator > designators; 182 183 // helper classes 184 public: 185 class BreakDesignator { 186 public: 187 BreakDesignator( Expression *exp ) { 188 Expression *prfx = exp; 189 UntypedMemberExpr *me = 0; 190 191 do { 192 if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break; 193 blown_struct.push_front( me->get_member() ); 194 prfx = me->get_aggregate(); 195 } while ( prfx != 0 ); 196 197 NameExpr *ne; 198 if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 ) 199 blown_struct.push_front( ne->get_name() ); 200 } 201 202 BreakDesignator( std::string name ) { 203 blown_struct.push_front( name ); 204 } 205 206 bool is_flat() const { return blown_struct.size() == 1; } 207 bool is_nested() const { return blown_struct.size() > 1; } 208 209 std::string get_name() { return blown_struct.front(); } 210 211 BreakDesignator &name_remainder() { 212 blown_struct.pop_front(); 213 return *this; 214 } 215 216 private: 217 std::list< std::string > blown_struct; 218 }; 219 220 }; 221 31 bool isDeclStmtP(Statement *stmt); 32 33 class BreakInitializer; 34 class BreakDesignator; 35 36 class BasicInit: public Mutator { 37 public: 38 BasicInit() : bindings( 0 ) {} 39 BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {} 40 BasicInit( const BasicInit &other ) { 41 bindings = other.get_bindings(); 42 index = other.index; 43 } 44 45 ~BasicInit() { /* delete bindings; bindings = 0; */ } 46 47 NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; } 48 void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) { 49 bindings = newValue; 50 } 51 52 bool has_bindings() { 53 return ( get_bindings() != 0 || ! stmts.empty() ); 54 } 55 56 virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) 57 { index.visit( objectDecl ); return objectDecl; } 58 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 59 { index.visit( typeDecl ); return typeDecl; } 60 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 61 { index.visit( typeDecl ); return typeDecl; } 62 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 63 { index.visit( aggregateDecl ); return aggregateDecl; } 64 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 65 { index.visit( aggregateDecl ); return aggregateDecl; } 66 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 67 { index.visit( aggregateDecl ); return aggregateDecl; } 68 69 virtual Type *mutate( StructInstType *aggrInst ) 70 { index.visit( aggrInst ); return aggrInst; } 71 virtual Type *mutate( UnionInstType *aggrInst ) 72 { index.visit( aggrInst ); return aggrInst; } 73 74 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 75 virtual Statement *mutate(DeclStmt *declStmt); 76 77 std::list< Statement *> get_statements() const { return stmts; } 78 79 static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, std::string aggName, std::list< Statement *> &stmts ); 80 private: 81 NameAssociation< Expression *, BreakInitializer > *bindings; 82 Statement *assignFromDecl( DeclStmt *declStmt ); 83 SymTab::Indexer index; 84 std::list< Statement *> stmts; 85 86 class Classify { 87 public: 88 enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T }; 89 enum InitKind { NULL_I, SINGLE_I, COMPOUND_I }; 90 91 static TypeKind type( Type * ); 92 static InitKind initializer( Initializer *); 93 94 static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index ); 95 static std::list< Statement * > 96 matchInit( NameInCollection *, ObjectDecl *, Initializer * ); 97 static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit ); 98 99 // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc ); 100 }; 101 }; 102 103 class BreakInitializer { 104 enum InitKind { EMPTY, SINGLE, COMPOUND }; 105 106 class BreakDesignator; 107 typedef BreakDesignator NameSplitter; 108 109 public: 110 typedef std::list<Initializer *>::iterator element_iterator; 111 typedef std::list< NameSplitter >::iterator name_iterator; 112 113 BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) { 114 std::list<Expression *> temp; 115 116 if ( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) { 117 kind = SINGLE; 118 temp = sinit->get_designators(); 119 } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) { 120 kind = COMPOUND; 121 temp = cinit->get_designators(); 122 } // if 123 124 std::transform( temp.begin(), temp.end(), std::back_inserter( designators ), ctor_noptr<NameSplitter, Expression *> ); 125 } 126 127 //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; } 128 ~BreakInitializer () {} 129 130 BreakInitializer set_name( NameSplitter &name ) { 131 designators.clear(); 132 designators.push_back( name ); 133 134 return *this; 135 } 136 137 element_iterator element_begin() { 138 assert( cinit != 0 ); 139 return cinit->begin_initializers(); 140 } 141 element_iterator element_end() { 142 assert( cinit != 0 ); 143 return cinit->end_initializers(); 144 } 145 146 name_iterator names_begin() { return designators.begin(); } 147 name_iterator names_end() { return designators.end(); } 148 149 int names_size() const { return designators.size(); } 150 151 bool has_index() const { return ! designators.empty(); } 152 bool is_single() const { return kind == SINGLE; } 153 bool is_composite() const { return kind == COMPOUND; } 154 155 Expression *get_value() { 156 switch ( kind ) { 157 case EMPTY: 158 return 0; 159 break; 160 case SINGLE: 161 return sinit->get_value(); 162 break; 163 case COMPOUND: 164 assert(false); 165 break; 166 default: 167 assert(false); 168 } // switch 169 return 0; 170 } 171 // attributes 172 private: 173 InitKind kind; 174 SingleInit *sinit; 175 ListInit *cinit; 176 std::list< BreakDesignator > designators; 177 // helper classes 178 public: 179 class BreakDesignator { 180 public: 181 BreakDesignator( Expression *exp ) { 182 Expression *prfx = exp; 183 UntypedMemberExpr *me = 0; 184 185 do { 186 if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break; 187 blown_struct.push_front( me->get_member() ); 188 prfx = me->get_aggregate(); 189 } while ( prfx != 0 ); 190 191 NameExpr *ne; 192 if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 ) 193 blown_struct.push_front( ne->get_name() ); 194 } 195 196 BreakDesignator( std::string name ) { 197 blown_struct.push_front( name ); 198 } 199 200 bool is_flat() const { return blown_struct.size() == 1; } 201 bool is_nested() const { return blown_struct.size() > 1; } 202 203 std::string get_name() { return blown_struct.front(); } 204 205 BreakDesignator &name_remainder() { 206 blown_struct.pop_front(); 207 return *this; 208 } 209 210 private: 211 std::list< std::string > blown_struct; 212 }; 213 }; 222 214 } // namespace InitTweak 223 215 224 #endif // #ifndef _BASINIT_H_ 225 226 /* 227 Local Variables: 228 mode: c++ 229 End: 230 */ 216 #endif // _BASINIT_H_ 217 231 218 // Local Variables: // 232 219 // tab-width: 4 // -
translator/InitTweak/DeclarationHoister.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // DeclarationHoister.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:33:20 2015 13 // Update Count : 2 14 14 // 15 15 16 #include <list> 16 17 #include <cassert> … … 27 28 #include "DeclarationHoister.h" 28 29 30 namespace InitTweak { 31 CompoundStmt* DeclarationHoister::mutate( CompoundStmt *compoundStmt ) { 32 typedef std::list<Statement *>::iterator stmt_it; 33 // 1. collect Declaration Statements in this scope 34 std::list<Statement *> &kids = compoundStmt->get_kids(); 35 std::list<Statement *>::iterator result = kids.begin(); 36 std::list< stmt_it > decls; 29 37 30 namespace InitTweak { 38 while ( result != kids.end() ) { 39 result = std::find_if (result, kids.end(), cast_ptr< Statement, DeclStmt > ); 31 40 32 CompoundStmt* DeclarationHoister::mutate(CompoundStmt *compoundStmt) { 33 typedef std::list<Statement *>::iterator stmt_it; 34 // 1. collect Declaration Statements in this scope 35 std::list<Statement *> &kids = compoundStmt->get_kids(); 36 std::list<Statement *>::iterator result = kids.begin(); 37 std::list< stmt_it > decls; 41 if ( result != kids.end() ) { 42 decls.push_back( result ); 43 std::advance( result, 1 ); 44 } // if 45 } // while 38 46 39 while ( result != kids.end() ) { 40 result = std::find_if (result, kids.end(), cast_ptr< Statement, DeclStmt > ); 47 for ( std::list< stmt_it >::reverse_iterator i = decls.rbegin(); i!= decls.rend(); i++ ) { 48 kids.push_front( **i ); 49 kids.erase( *i ); 50 } // for 41 51 42 if ( result != kids.end() ) { 43 decls.push_back( result ); 44 std::advance( result, 1 ); 45 } 46 } 47 48 for ( std::list< stmt_it >::reverse_iterator i = decls.rbegin(); i!= decls.rend(); i++ ){ 49 kids.push_front( **i ); 50 kids.erase( *i ); 51 } 52 53 return compoundStmt; 54 } 52 return compoundStmt; 53 } 55 54 } // namespace InitTweak 56 57 58 59 60 61 62 63 55 64 56 // Local Variables: // -
translator/InitTweak/DeclarationHoister.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // DeclarationHoister.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:33:48 2015 13 // Update Count : 2 14 14 // 15 15 16 #include "SynTree/Visitor.h" 16 17 #include "SymTab/Indexer.h" 17 18 18 19 namespace InitTweak { 20 bool isDeclStmtP(Statement *stmt); 19 21 20 bool isDeclStmtP(Statement *stmt); 21 22 class DeclarationHoister: public Mutator { 23 public: 24 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 25 }; 26 22 class DeclarationHoister: public Mutator { 23 public: 24 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 25 }; 27 26 } // namespace InitTweak 28 27 29 /*30 Local Variables:31 mode: c++32 End:33 */34 28 // Local Variables: // 35 29 // tab-width: 4 // -
translator/InitTweak/InitExpander.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // InitExpander.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:34:12 2015 13 // Update Count : 1 14 14 // 15 15 16 #include <list> 16 17 #include <stack> 17 18 #include <cassert> 18 19 #include <algorithm> 19 20 20 21 21 #include "utility.h" … … 24 24 25 25 namespace InitTweak { 26 InitExpander::InitExpander() {} 26 27 27 InitExpander::InitExpander() {}28 InitExpander::~InitExpander() {} 28 29 29 InitExpander::~InitExpander() {} 30 ObjectDecl *InitExpander::mutate( ObjectDecl *objectDecl ) { 31 index.visit( objectDecl ); 30 32 31 ObjectDecl *InitExpander::mutate( ObjectDecl *objectDecl ) { 32 index.visit( objectDecl ); 33 if ( objectDecl->get_init() == 0 ) return objectDecl; 33 34 34 if ( objectDecl->get_init() == 0 ) return objectDecl; 35 InitModelBuilder builder( objectDecl ); 36 builder.get_assoc()->display( std::cerr ); // xxx 37 InitModelFiller filler( builder.get_assoc(), objectDecl->get_init(), true ); 38 // filler.get_assoc()->display( std::cerr ); // xxx 39 InitUnspooler exp; 40 filler.get_assoc()->accept( exp ); 41 objectDecl->set_init( exp.grab_initializer() ); 42 objectDecl->get_init()->print( std::cerr ); 35 43 36 InitModelBuilder builder( objectDecl ); 37 builder.get_assoc()->display( std::cerr ); // xxx 38 InitModelFiller filler( builder.get_assoc(), objectDecl->get_init(), true ); 39 // filler.get_assoc()->display( std::cerr ); // xxx 40 InitUnspooler exp; 41 filler.get_assoc()->accept( exp ); 42 objectDecl->set_init( exp.grab_initializer() ); 43 objectDecl->get_init()->print( std::cerr ); 44 45 return objectDecl; 46 } 47 44 return objectDecl; 45 } 48 46 } // namespace InitTweak 49 47 -
translator/InitTweak/InitExpander.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // InitExpander.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:35:33 2015 13 // Update Count : 2 14 14 // 15 15 16 #ifndef _INIT_EXPANDER_H_ 16 17 #define _INIT_EXPANDER_H_ … … 27 28 28 29 namespace InitTweak { 30 class InitExpander : public Mutator { 31 typedef Mutator Parent; 32 public: 33 InitExpander(); 34 ~InitExpander(); 29 35 30 class InitExpander : public Mutator 31 { 32 typedef Mutator Parent; 36 virtual ObjectDecl *mutate( ObjectDecl * ); 33 37 34 public: 35 InitExpander(); 36 ~InitExpander(); 38 // indexer runs 39 virtual FunctionDecl *mutate( FunctionDecl *functionDecl ) { 40 functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) ); 41 mutateAll( functionDecl->get_oldDecls(), *this ); 42 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 37 43 38 virtual ObjectDecl *mutate( ObjectDecl * ); 44 index.visit( functionDecl ); 45 return functionDecl; 46 } 39 47 40 // indexer runs 41 virtual FunctionDecl *mutate( FunctionDecl *functionDecl ) 42 { 43 functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) ); 44 mutateAll( functionDecl->get_oldDecls(), *this ); 45 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 48 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 49 { index.visit( typeDecl ); return typeDecl; } 50 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 51 { index.visit( typeDecl ); return typeDecl; } 52 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 53 { index.visit( aggregateDecl ); return aggregateDecl; } 54 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 55 { index.visit( aggregateDecl ); return aggregateDecl; } 56 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 57 { index.visit( aggregateDecl ); return aggregateDecl; } 46 58 47 index.visit( functionDecl ); 48 return functionDecl; 49 } 50 51 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 52 { index.visit( typeDecl ); return typeDecl; } 53 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 54 { index.visit( typeDecl ); return typeDecl; } 55 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 56 { index.visit( aggregateDecl ); return aggregateDecl; } 57 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 58 { index.visit( aggregateDecl ); return aggregateDecl; } 59 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 60 { index.visit( aggregateDecl ); return aggregateDecl; } 61 62 virtual Type *mutate( StructInstType *aggrInst ) 63 { index.visit( aggrInst ); return aggrInst; } 64 virtual Type *mutate( UnionInstType *aggrInst ) 65 { index.visit( aggrInst ); return aggrInst; } 66 67 private: 68 SymTab::Indexer index; 69 }; // class InitExpander 70 59 virtual Type *mutate( StructInstType *aggrInst ) 60 { index.visit( aggrInst ); return aggrInst; } 61 virtual Type *mutate( UnionInstType *aggrInst ) 62 { index.visit( aggrInst ); return aggrInst; } 63 private: 64 SymTab::Indexer index; 65 }; // class InitExpander 71 66 } // namespace InitTweak 72 67 68 #endif // _INIT_EXPANDER_H_ 73 69 74 #endif // #ifndef _INIT_EXPANDER_H_75 76 /*77 Local Variables:78 mode: c++79 End:80 */81 70 // Local Variables: // 82 71 // tab-width: 4 // -
translator/InitTweak/InitModel.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // InitModel.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:37:08 2015 13 // Update Count : 1 14 // 15 15 16 #include "SynTree/Constant.h" 16 17 #include "InitModel.h" … … 21 22 22 23 namespace InitTweak { 23 24 InitModelBuilder::InitModelBuilder( Declaration *_decl ) 25 : taken( false ), decl( 0 ), building(0) { 26 27 ObjectDecl *_odecl = dynamic_cast< ObjectDecl * >( _decl ); 28 assert( _odecl != 0 ); 29 Type *objectType = _odecl->get_type(); 30 31 /* this to be replaced by dynamic dispatch */ 32 if ( dynamic_cast< BasicType * >(objectType) != 0 ) { 33 if ( building == 0 ) building = new SingleName; 34 } else if ( ReferenceToType *rt = dynamic_cast< ReferenceToType * >(objectType) ) { 35 rt->accept( *this ); 36 } else if ( ArrayType *at = dynamic_cast< ArrayType * >(objectType) ) { 37 at->accept( *this ); 38 } else // if (tuples) 39 std::cerr << "Got something else" << std::endl; 40 41 if ( decl != 0 ) init(); 42 } 43 44 InitModelBuilder::~InitModelBuilder() { if ( ! taken ) { delete building; building = 0; } } 45 46 void InitModelBuilder::init() { 47 assert( decl != 0 ); 48 decl->accept( *this ); 49 } 50 51 // Visitor interface 52 void InitModelBuilder::visit( ArrayType *at ) { 53 if ( building == 0 ) building = new RangeAssociation(interpretDimension( at->get_dimension() )); 54 decl = 0; 55 return; 56 } 57 58 void InitModelBuilder::visit( StructInstType *st ) { 59 if ( building == 0 ) building = new PointAssociation; 60 decl = st->get_baseStruct(); 61 return; 62 } 63 64 void InitModelBuilder::visit( UnionInstType *ut ) { 65 decl = ut->get_baseUnion(); 66 return; 67 } 68 void InitModelBuilder::visit( EnumInstType * ) {} 69 70 void InitModelBuilder::visit( StructDecl *aggregateDecl) { 71 PointAssociation *pa = dynamic_cast< PointAssociation * >( building ); 72 assert( pa != 0 ); 73 std::list< Declaration * > mem = aggregateDecl->get_members(); 74 75 for ( std::list<Declaration *>::iterator i = mem.begin(); i != mem.end(); i++ ) { 76 pa->add_member( (*i)->get_name() ); 77 InitModelBuilder rec(*i); 78 pa->set_member( (*i)->get_name(), rec.grab_assoc() ); 79 } 80 81 return; 82 } 83 84 void InitModelBuilder::visit( UnionDecl *) {} 85 void InitModelBuilder::visit( EnumDecl *) {} 86 87 // InitModelBuilder::ConstantFolder 88 void InitModelBuilder::ConstantFolder::visit( ConstantExpr *cnst ) { 89 Constant *c = cnst->get_constant(); 90 assert (c != 0); 91 if ( BasicType *bt = dynamic_cast<BasicType *>( c->get_type() ) ) { 92 if ( bt->isInteger() ) { 93 // need more intelligence here, not necessarily base 10 94 value = std::strtol( c->get_value().c_str(), NULL, 10 ); 95 return; 96 } else 97 std::cerr << "Basic type but not integer" << std::endl; 98 } 99 throw 0; 100 } 101 102 // InitModelFiller 103 InitModelFiller::InitModelFiller( Association *_model, Initializer *_init, bool _topLevel ) 104 : model( _model ), orgInit( _init ), topLevel( _topLevel ), next( 0 ) { 105 //next = model.begin(); 106 if ( orgInit != 0 ) init(); 107 } 108 109 void InitModelFiller::init() { 110 assert( model != 0 ); // change it into a reference 111 assert( orgInit != 0 ); 112 orgInit->accept( *this ); 113 } 114 115 void InitModelFiller::visit( SingleInit *singleInit ) { 116 std::list< Expression *> &des = singleInit->get_designators(); 117 118 if ( topLevel ) { 119 assert ( des.empty() ); 120 assert ( dynamic_cast< SingleName * >(model) != 0 ); 121 try { 122 model->add_single( next++, singleInit->get_value() ); 123 } catch (...) { 124 std::cerr << "Illegal initialization" << std::endl; 125 } 126 return; 127 } 128 129 if ( des.empty() ) { 130 assert( model != 0 ); 131 try { 132 model->add_single( next++, singleInit->get_value() ); 133 } catch ( AssocException &e ) { 134 throw SemanticError( "Illegal initialization: " + e.get_what() ); 135 } catch ( ... ) { 136 std::cerr << "Shouldn't be here" << std::endl; 137 } 138 return; 139 } 140 141 // not general enough (does not contend well with designated arrays) 142 std::list<std::string> desnames; 143 std::transform( des.begin(), des.end(), back_inserter(desnames), Initializer::designator_name ); 144 145 for ( std::list<std::string>::iterator i = desnames.begin(); i != desnames.end(); i++ ) { 146 try { 147 next = model->add_single( *i, singleInit->get_value() ); 148 next++; 149 } catch ( AssocException &e ) { 150 throw SemanticError( "Illegal initialization: " + e.get_what() ); 151 } catch ( ... ) { 152 std::cerr << "Shouldn't happen, check association" << std::endl; 153 } 154 } 155 156 return; 157 } 158 159 void InitModelFiller::visit( ListInit *listInit ) { 160 assert( listInit != 0 ); 161 162 // designators 163 std::list< Expression *> &des = listInit->get_designators(); 164 std::list< Initializer *> &ini = listInit->get_initializers(); 165 166 if (! des.empty() ) { 167 if (topLevel) 168 throw SemanticError( "Invalid initializer: designated at top level." ); 169 170 std::list<Expression *> des2; 171 std::copy (des.begin(), des.end(), back_inserter( des2 )); 172 std::list< Expression * > empty; 173 listInit->set_designators( empty ); 174 for ( std::list<Expression *>::iterator i = des2.begin(); i != des2.end(); i++ ) { 175 Association * newModel = 0; 176 if ( NameExpr *n = dynamic_cast< NameExpr * >( *i ) ) 177 try { 178 newModel = (*model)[ n->get_name() ]; 179 } catch( AssocException &e ) { 180 std::cerr << "Didn't find member: " << e.get_what() << std::endl; 181 } 182 else // if ( RangeExpr *r = dynamic_cast< RangeExpr * >( *i ) ) 183 std::cerr << "Invalid designator specification" << std::endl; 184 185 InitModelFiller rec( newModel, listInit, true ); 186 } 187 188 } else 189 if (topLevel) { 190 topLevel = false; 191 for ( std::list<Initializer*>::iterator i = ini.begin(); i != ini.end(); i++ ) 192 (*i)->accept(*this); 193 } else 194 // next available uninitialized member 195 InitModelFiller rec( (*model)[next++], listInit, true ); 196 } 197 198 void InitUnspooler::visit( SingleName *single ) { 199 assert(init == 0 && single != 0); 200 std::list< Expression * > empty; 201 init = new SingleInit( single->get_expr(), empty ); 202 return; 203 } 204 205 void InitUnspooler::visit( PointAssociation *pa ) { 206 assert( pa != 0 ); 207 208 std::list< Initializer * > contents; 209 for ( int i = 0; i < pa->size(); i++ ) 210 if ( (*pa)[i] != 0 ) { 211 InitUnspooler rec; 212 (*pa)[i]->accept( rec ); 213 assert( rec.get_initializer() != 0 ); 214 contents.push_back( rec.grab_initializer() ); 215 } 216 217 init = new ListInit( contents ); 218 return; 219 } 220 221 222 24 InitModelBuilder::InitModelBuilder( Declaration *_decl ) 25 : taken( false ), decl( 0 ), building(0) { 26 27 ObjectDecl *_odecl = dynamic_cast< ObjectDecl * >( _decl ); 28 assert( _odecl != 0 ); 29 Type *objectType = _odecl->get_type(); 30 31 /* this to be replaced by dynamic dispatch */ 32 if ( dynamic_cast< BasicType * >(objectType) != 0 ) { 33 if ( building == 0 ) building = new SingleName; 34 } else if ( ReferenceToType *rt = dynamic_cast< ReferenceToType * >(objectType) ) { 35 rt->accept( *this ); 36 } else if ( ArrayType *at = dynamic_cast< ArrayType * >(objectType) ) { 37 at->accept( *this ); 38 } else // if (tuples) 39 std::cerr << "Got something else" << std::endl; 40 41 if ( decl != 0 ) init(); 42 } 43 44 InitModelBuilder::~InitModelBuilder() { if ( ! taken ) { delete building; building = 0; } } 45 46 void InitModelBuilder::init() { 47 assert( decl != 0 ); 48 decl->accept( *this ); 49 } 50 51 // Visitor interface 52 void InitModelBuilder::visit( ArrayType *at ) { 53 if ( building == 0 ) building = new RangeAssociation(interpretDimension( at->get_dimension() )); 54 decl = 0; 55 return; 56 } 57 58 void InitModelBuilder::visit( StructInstType *st ) { 59 if ( building == 0 ) building = new PointAssociation; 60 decl = st->get_baseStruct(); 61 return; 62 } 63 64 void InitModelBuilder::visit( UnionInstType *ut ) { 65 decl = ut->get_baseUnion(); 66 return; 67 } 68 void InitModelBuilder::visit( EnumInstType * ) {} 69 70 void InitModelBuilder::visit( StructDecl *aggregateDecl) { 71 PointAssociation *pa = dynamic_cast< PointAssociation * >( building ); 72 assert( pa != 0 ); 73 std::list< Declaration * > mem = aggregateDecl->get_members(); 74 75 for ( std::list<Declaration *>::iterator i = mem.begin(); i != mem.end(); i++ ) { 76 pa->add_member( (*i)->get_name() ); 77 InitModelBuilder rec(*i); 78 pa->set_member( (*i)->get_name(), rec.grab_assoc() ); 79 } // for 80 81 return; 82 } 83 84 void InitModelBuilder::visit( UnionDecl *) {} 85 void InitModelBuilder::visit( EnumDecl *) {} 86 87 // InitModelBuilder::ConstantFolder 88 void InitModelBuilder::ConstantFolder::visit( ConstantExpr *cnst ) { 89 Constant *c = cnst->get_constant(); 90 assert (c != 0); 91 if ( BasicType *bt = dynamic_cast<BasicType *>( c->get_type() ) ) { 92 if ( bt->isInteger() ) { 93 // need more intelligence here, not necessarily base 10 94 value = std::strtol( c->get_value().c_str(), NULL, 10 ); 95 return; 96 } else 97 std::cerr << "Basic type but not integer" << std::endl; 98 } // if 99 throw 0; 100 } 101 102 // InitModelFiller 103 InitModelFiller::InitModelFiller( Association *_model, Initializer *_init, bool _topLevel ) 104 : model( _model ), orgInit( _init ), topLevel( _topLevel ), next( 0 ) { 105 //next = model.begin(); 106 if ( orgInit != 0 ) init(); 107 } 108 109 void InitModelFiller::init() { 110 assert( model != 0 ); // change it into a reference 111 assert( orgInit != 0 ); 112 orgInit->accept( *this ); 113 } 114 115 void InitModelFiller::visit( SingleInit *singleInit ) { 116 std::list< Expression *> &des = singleInit->get_designators(); 117 118 if ( topLevel ) { 119 assert ( des.empty() ); 120 assert ( dynamic_cast< SingleName * >(model) != 0 ); 121 try { 122 model->add_single( next++, singleInit->get_value() ); 123 } catch (...) { 124 std::cerr << "Illegal initialization" << std::endl; 125 } 126 return; 127 } // if 128 129 if ( des.empty() ) { 130 assert( model != 0 ); 131 try { 132 model->add_single( next++, singleInit->get_value() ); 133 } catch ( AssocException &e ) { 134 throw SemanticError( "Illegal initialization: " + e.get_what() ); 135 } catch ( ... ) { 136 std::cerr << "Shouldn't be here" << std::endl; 137 } // try 138 return; 139 } // if 140 141 // not general enough (does not contend well with designated arrays) 142 std::list<std::string> desnames; 143 std::transform( des.begin(), des.end(), back_inserter(desnames), Initializer::designator_name ); 144 145 for ( std::list<std::string>::iterator i = desnames.begin(); i != desnames.end(); i++ ) { 146 try { 147 next = model->add_single( *i, singleInit->get_value() ); 148 next++; 149 } catch ( AssocException &e ) { 150 throw SemanticError( "Illegal initialization: " + e.get_what() ); 151 } catch ( ... ) { 152 std::cerr << "Shouldn't happen, check association" << std::endl; 153 } // try 154 } // for 155 156 return; 157 } 158 159 void InitModelFiller::visit( ListInit *listInit ) { 160 assert( listInit != 0 ); 161 162 // designators 163 std::list< Expression *> &des = listInit->get_designators(); 164 std::list< Initializer *> &ini = listInit->get_initializers(); 165 166 if ( ! des.empty() ) { 167 if (topLevel) 168 throw SemanticError( "Invalid initializer: designated at top level." ); 169 170 std::list<Expression *> des2; 171 std::copy (des.begin(), des.end(), back_inserter( des2 )); 172 std::list< Expression * > empty; 173 listInit->set_designators( empty ); 174 for ( std::list<Expression *>::iterator i = des2.begin(); i != des2.end(); i++ ) { 175 Association * newModel = 0; 176 if ( NameExpr *n = dynamic_cast< NameExpr * >( *i ) ) 177 try { 178 newModel = (*model)[ n->get_name() ]; 179 } catch( AssocException &e ) { 180 std::cerr << "Didn't find member: " << e.get_what() << std::endl; 181 } 182 else // if ( RangeExpr *r = dynamic_cast< RangeExpr * >( *i ) ) 183 std::cerr << "Invalid designator specification" << std::endl; 184 185 InitModelFiller rec( newModel, listInit, true ); 186 } // for 187 } else 188 if (topLevel) { 189 topLevel = false; 190 for ( std::list<Initializer*>::iterator i = ini.begin(); i != ini.end(); i++ ) 191 (*i)->accept(*this); 192 } else 193 // next available uninitialized member 194 InitModelFiller rec( (*model)[next++], listInit, true ); 195 } 196 197 void InitUnspooler::visit( SingleName *single ) { 198 assert(init == 0 && single != 0); 199 std::list< Expression * > empty; 200 init = new SingleInit( single->get_expr(), empty ); 201 return; 202 } 203 204 void InitUnspooler::visit( PointAssociation *pa ) { 205 assert( pa != 0 ); 206 207 std::list< Initializer * > contents; 208 for ( int i = 0; i < pa->size(); i++ ) 209 if ( (*pa)[i] != 0 ) { 210 InitUnspooler rec; 211 (*pa)[i]->accept( rec ); 212 assert( rec.get_initializer() != 0 ); 213 contents.push_back( rec.grab_initializer() ); 214 } // if 215 216 init = new ListInit( contents ); 217 return; 218 } 223 219 } // namespace InitTweak 220 224 221 // Local Variables: // 225 222 // tab-width: 4 // -
translator/InitTweak/InitModel.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // InitModel.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:37:52 2015 13 // Update Count : 2 14 14 // 15 15 16 #ifndef _INITTWEAK_MODEL_H_ 16 17 #define _INITTWEAK_MODEL_H_ … … 26 27 27 28 namespace InitTweak { 28 29 30 InitModelBuilder( Declaration * );31 ~InitModelBuilder();29 class InitModelBuilder : public AssociationBuilder, public Visitor { 30 public: 31 InitModelBuilder( Declaration * ); 32 ~InitModelBuilder(); 32 33 33 virtual Association *grab_assoc() { taken = true; return building; }34 virtual Association *get_assoc() { return building; }35 void set_assoc( Association *newAssoc ) { building = newAssoc; }34 virtual Association *grab_assoc() { taken = true; return building; } 35 virtual Association *get_assoc() { return building; } 36 void set_assoc( Association *newAssoc ) { building = newAssoc; } 36 37 37 void init();38 static int interpretDimension( Expression *exp ) {39 40 41 return folder.get_constant();42 43 throw SemanticError("Invalid array dimension");44 45 }38 void init(); 39 static int interpretDimension( Expression *exp ) { 40 ConstantFolder folder( exp ); 41 try { 42 return folder.get_constant(); 43 } catch (...) { 44 throw SemanticError("Invalid array dimension"); 45 } 46 } 46 47 47 // types 48 virtual void visit( ArrayType * ); 49 virtual void visit( StructInstType * ); 50 virtual void visit( UnionInstType * ); 51 virtual void visit( EnumInstType * ); 52 virtual void visit( ContextInstType * ) { throw 0; } 53 virtual void visit( TypeInstType * ) { throw 0; } 54 // virtual void visit( TupleType *tupleType ); 55 // declarations 56 virtual void visit( StructDecl *); 57 virtual void visit( UnionDecl *); 58 virtual void visit( EnumDecl *); 59 private: 60 class ConstantFolder : public Visitor { 61 public: 62 ConstantFolder( Expression *_expr = 0 ): expr(_expr) {} 63 int get_constant() throw() { expr->accept( *this ); return value; } 64 void set_constant( Expression *newExp ) { expr = newExp; } 65 // Visitor interface 66 void visit( Expression * ) { throw 0; } 67 void visit( NameExpr * ) { throw 0; } 68 void visit( CastExpr * ) { throw 0; } 69 void visit( UntypedMemberExpr * ) { throw 0; } 70 void visit( VariableExpr * ) { throw 0; } 71 void visit( ConstantExpr * ); 72 void visit( SizeofExpr * ) { throw 0; } 73 void visit( AttrExpr * ) { throw 0; } 74 void visit( LogicalExpr * ) { throw 0; } 75 void visit( ConditionalExpr * ) { throw 0; } 76 void visit( CommaExpr * ) { throw 0; } 48 // types 49 virtual void visit( ArrayType * ); 50 virtual void visit( StructInstType * ); 51 virtual void visit( UnionInstType * ); 52 virtual void visit( EnumInstType * ); 53 virtual void visit( ContextInstType * ) { throw 0; } 54 virtual void visit( TypeInstType * ) { throw 0; } 55 // virtual void visit( TupleType *tupleType ); 56 // declarations 57 virtual void visit( StructDecl *); 58 virtual void visit( UnionDecl *); 59 virtual void visit( EnumDecl *); 77 60 private: 78 Expression *expr; 79 int value; 61 class ConstantFolder : public Visitor { 62 public: 63 ConstantFolder( Expression *_expr = 0 ): expr(_expr) {} 64 int get_constant() throw() { expr->accept( *this ); return value; } 65 void set_constant( Expression *newExp ) { expr = newExp; } 66 // Visitor interface 67 void visit( Expression * ) { throw 0; } 68 void visit( NameExpr * ) { throw 0; } 69 void visit( CastExpr * ) { throw 0; } 70 void visit( UntypedMemberExpr * ) { throw 0; } 71 void visit( VariableExpr * ) { throw 0; } 72 void visit( ConstantExpr * ); 73 void visit( SizeofExpr * ) { throw 0; } 74 void visit( AttrExpr * ) { throw 0; } 75 void visit( LogicalExpr * ) { throw 0; } 76 void visit( ConditionalExpr * ) { throw 0; } 77 void visit( CommaExpr * ) { throw 0; } 78 private: 79 Expression *expr; 80 int value; 81 }; 82 83 bool taken; 84 Declaration *decl; // ? 85 Association *building; 80 86 }; 81 87 82 bool taken; 83 Declaration *decl; // ? 84 Association *building; 85 }; 88 class InitModelFiller : public AssociationFiller, public Visitor { 89 public: 90 InitModelFiller( Association *, Initializer *, bool _topLevel = false ); 91 ~InitModelFiller() { /* pointers in here are not owned by object (never created by object either) */ } 92 virtual Association *get_assoc() { return model; } 93 virtual void set_assoc( Association *newAssoc ) { model = newAssoc; } 86 94 87 class InitModelFiller : public AssociationFiller, public Visitor { 88 public: 89 InitModelFiller( Association *, Initializer *, bool _topLevel = false ); 90 ~InitModelFiller() { /* pointers in here are not owned by object (never created by object either) */ } 91 virtual Association *get_assoc() { return model; } 92 virtual void set_assoc( Association *newAssoc ) { model = newAssoc; } 95 void init(); 96 // Visitor interface 97 virtual void visit( SingleInit *singleInit ); 98 virtual void visit( ListInit *listInit ); 99 private: 100 Association *model; 101 Initializer *orgInit; 102 bool topLevel; 103 long int next; 104 }; 93 105 94 void init(); 95 // Visitor interface 96 virtual void visit( SingleInit *singleInit ); 97 virtual void visit( ListInit *listInit ); 98 private: 99 Association *model; 100 Initializer *orgInit; 101 bool topLevel; 102 long int next; 103 }; 106 class InitUnspooler : public AssociationVisitor { 107 public: 108 InitUnspooler() : init(0), taken( false ) {} 109 virtual ~InitUnspooler() { if ( ! taken && (init != 0)) { delete init; init = 0; } } 110 Initializer *get_initializer() { return init; } 111 Initializer *grab_initializer() { taken = true; return init; } 104 112 105 class InitUnspooler : public AssociationVisitor { 106 public: 107 InitUnspooler() : init(0), taken( false ) {} 108 virtual ~InitUnspooler() { if (! taken && (init != 0)) { delete init; init = 0; } } 109 Initializer *get_initializer() { return init; } 110 Initializer *grab_initializer() { taken = true; return init; } 111 112 virtual void visit( SingleName * ); 113 virtual void visit( PointAssociation * ); 114 virtual void visit( RangeAssociation * ) { std::cerr << "InitUnspooler - In a range assoc" << std::endl; return; } 115 private: 116 Initializer *init; 117 bool taken; 118 }; 119 113 virtual void visit( SingleName * ); 114 virtual void visit( PointAssociation * ); 115 virtual void visit( RangeAssociation * ) { std::cerr << "InitUnspooler - In a range assoc" << std::endl; return; } 116 private: 117 Initializer *init; 118 bool taken; 119 }; 120 120 } // namespace InitTweak 121 121 122 122 #endif // _INITTWEAK_MODEL_H_ 123 123 124 /*125 Local Variables:126 mode: c++127 End:128 */129 124 // Local Variables: // 130 125 // tab-width: 4 // -
translator/InitTweak/Mutate.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // Mutate.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:38:19 2015 13 // Update Count : 1 14 14 // 15 15 16 #include "SynTree/Mutator.h" 16 17 … … 22 23 23 24 namespace InitTweak { 25 void mutate( std::list< Declaration * > translationUnit ) { 26 //BasicInit bi; 27 InitExpander ini; 28 //DeclarationHoister dh; 24 29 25 void mutate( std::list< Declaration * > translationUnit ) 26 { 27 //BasicInit bi; 28 InitExpander ini; 29 //DeclarationHoister dh; 30 31 //mutateAll( translationUnit, bi ); 32 mutateAll( translationUnit, ini ); 33 //mutateAll( translationUnit, dh ); 34 } 35 30 //mutateAll( translationUnit, bi ); 31 mutateAll( translationUnit, ini ); 32 //mutateAll( translationUnit, dh ); 33 } 36 34 } // namespace InitTweak 37 38 35 39 36 // Local Variables: // -
translator/InitTweak/Mutate.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // Mutate.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:38:52 2015 13 // Update Count : 2 14 14 // 15 15 16 #ifndef INIT_MUTATE_H 16 17 #define INIT_MUTATE_H … … 21 22 22 23 namespace InitTweak { 23 24 void mutate( std::list< Declaration* > translationUnit ); 25 24 void mutate( std::list< Declaration* > translationUnit ); 26 25 } // namespace InitTweak 27 26 28 #endif // #ifndefINIT_MUTATE_H27 #endif // INIT_MUTATE_H 29 28 30 /*31 Local Variables:32 mode: c++33 End:34 */35 29 // Local Variables: // 36 30 // tab-width: 4 // -
translator/InitTweak/RemoveInit.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // RemoveInit.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:39:32 2015 13 // Update Count : 1 14 14 // 15 15 16 #include "RemoveInit.h" 16 17 #include "SynTree/Declaration.h" … … 22 23 23 24 namespace InitTweak { 25 namespace { 26 const std::list<Label> noLabels; 27 } 24 28 25 namespace { 26 const std::list<Label> noLabels; 27 } 29 void tweak( std::list< Declaration * > translationUnit ) { 30 RemoveInit remover; 31 mutateAll( translationUnit, remover ); 32 } 28 33 29 void tweak( std::list< Declaration * > translationUnit ) { 30 RemoveInit remover; 31 mutateAll( translationUnit, remover ); 32 } 34 void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) { 35 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { 36 if ( ! stmtsToAddAfter.empty() ) { 37 statements.splice( i, stmtsToAddAfter ); 38 } // if 39 *i = (*i)->acceptMutator( *this ); 40 } // for 41 if ( ! stmtsToAddAfter.empty() ) { 42 statements.splice( statements.end(), stmtsToAddAfter ); 43 } // if 44 } 33 45 34 void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) { 35 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { 36 if ( ! stmtsToAddAfter.empty() ) { 37 statements.splice( i, stmtsToAddAfter ); 38 } 39 *i = (*i)->acceptMutator( *this ); 40 } 41 if ( ! stmtsToAddAfter.empty() ) { 42 statements.splice( statements.end(), stmtsToAddAfter ); 43 } 44 } 45 46 CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) { 47 mutateStatementList( compoundStmt->get_kids() ); 48 return compoundStmt; 49 } 46 CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) { 47 mutateStatementList( compoundStmt->get_kids() ); 48 return compoundStmt; 49 } 50 50 51 51 // in the case where an object has an initializer and a polymorphic type, insert an assignment 52 52 // immediately after the declaration. This will (seemingly) cause the later phases to do the right 53 53 // thing with the assignment 54 ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {55 56 57 58 59 60 61 } 62 } 63 64 }54 ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) { 55 if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) { 56 if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) { 57 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); 58 assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) ); 59 assign->get_args().push_back( single->get_value()->clone() ); 60 stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign)); 61 } // if 62 } // if 63 return objDecl; 64 } 65 65 } // namespace InitTweak 66 66 -
translator/InitTweak/RemoveInit.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // RemoveInit.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:40:11 2015 13 // Update Count : 1 14 14 // 15 /*16 * This file is part of the Cforall project17 *18 * $Id: PolyMutator.h,v 1.8 2005/08/29 20:14:13 rcbilson Exp $19 *20 */21 15 22 16 #ifndef REMOVE_INIT_H … … 31 25 32 26 namespace InitTweak { 27 void tweak( std::list< Declaration * > translationUnit ); 33 28 34 void tweak( std::list< Declaration * > translationUnit ); 35 36 class RemoveInit : public Mutator { 37 public: 38 // RemoveInit(); 39 virtual ObjectDecl *mutate(ObjectDecl *objDecl); 40 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 41 protected: 42 std::list< Statement* > stmtsToAddAfter; 43 void mutateStatementList( std::list< Statement* > &statements ); 44 }; 45 29 class RemoveInit : public Mutator { 30 public: 31 // RemoveInit(); 32 virtual ObjectDecl *mutate(ObjectDecl *objDecl); 33 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 34 protected: 35 std::list< Statement* > stmtsToAddAfter; 36 void mutateStatementList( std::list< Statement* > &statements ); 37 }; 46 38 } // namespace 47 39 48 #endif /* #ifndef GENPOLY_POLYMUTATOR_H */ 40 #endif // GENPOLY_POLYMUTATOR_H 41 49 42 // Local Variables: // 50 43 // tab-width: 4 // -
translator/InitTweak/diet_map.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // diet_map.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:41:17 2015 13 // Update Count : 2 14 // 15 15 16 #include <cassert> 16 17 #include <string> … … 18 19 19 20 namespace diet { 20 /* A DIET ( Discrete Interval Encoding Tree ) range-map 21 */ 22 23 class diet_tree_exception : public std::exception { 24 public: 25 diet_tree_exception() {} 26 diet_tree_exception( std::string _what ) : what( _what ) {} 27 ~diet_tree_exception() throw () {} 28 29 std::string get_what() const { return what; } 30 void set_what( std::string newValue ) { what = newValue; } 31 private: 32 std::string what; 33 }; 34 35 template < typename T > class diet_tree_node; 36 template < typename T > class diet_tree_iterator; 37 38 template< typename key_type > 39 class diet_tree { 40 typedef key_type OrderedValue; 41 typedef OrderedValue T; 42 friend class diet_tree_iterator<T>; 43 public: 44 typedef OrderedValue value_type; 45 typedef diet_tree_iterator<OrderedValue> iterator; 46 typedef std::pair<value_type, value_type> pair_type; 47 48 diet_tree() : root(0), left(0), right(0) {} 49 ~diet_tree() { 50 if ( root != 0 ) { delete root; root = 0; } 51 if ( left != 0 ) { delete left; left = 0; } 52 if ( right != 0 ) { delete right; right = 0; } 53 } 54 55 void insert( value_type _lo, value_type _hi ) { 56 if ( _lo > _hi ) return; // throw exception? 57 if ( root == 0 ) 58 root = new diet_tree_node<value_type>(_lo, _hi); 59 else { 60 value_type lo = root->get_lo(), hi = root->get_hi(); 61 if ( _lo < lo ) { 62 if ( _hi > hi ) { 63 /* can either minimize the work or minimize the number of nodes. 64 Let's minimize the work. */ 65 if ( left == 0 ) left = new diet_tree<T>(); 66 left->insert( _lo, lo ); 67 if ( right == 0 ) right = new diet_tree<T>(); 68 right->insert( _hi, hi ); 69 return; 70 } else if ( _hi < lo ) { 71 if ( left == 0 ) left = new diet_tree<T>(); 72 left->insert( _lo, _hi ); 73 } else if ( _hi <= hi ) { 74 if ( left == 0 ) left = new diet_tree<T>(); 75 left->insert( _lo, _hi ); 76 root->set_range(_hi,hi); 77 } 78 } else if (_lo >= lo && _hi <= hi ) { 79 root->set_range(_lo,_hi); 80 } else if ( _hi > hi) { 81 if ( _lo > hi ) { 82 if ( right == 0 ) right = new diet_tree<T>(); 83 right->insert( _lo, _hi ); 84 } else if ( _lo < hi ) {