Changeset 87b5bf0 for src/ControlStruct
- Timestamp:
- Jun 29, 2016, 2:30:12 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, 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, with_gc
- Children:
- e64365c
- Parents:
- 7305915 (diff), 2fb2ad5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- src/ControlStruct
- Files:
-
- 6 edited
-
LabelFixer.cc (modified) (4 diffs)
-
LabelFixer.h (modified) (3 diffs)
-
LabelGenerator.cc (modified) (3 diffs)
-
MLEMutator.cc (modified) (17 diffs)
-
MLEMutator.h (modified) (6 diffs)
-
Mutate.cc (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/LabelFixer.cc
r7305915 r87b5bf0 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // LabelFixer.cc -- 7 // LabelFixer.cc -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 86 86 87 87 88 // sets the definition of the labelTable entry to be the provided 88 // sets the definition of the labelTable entry to be the provided 89 89 // statement for every label in the list parameter. Happens for every kind of statement 90 90 Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { … … 95 95 96 96 for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) { 97 if ( labelTable.find( *i ) == labelTable.end() ) { 97 Label & l = *i; 98 l.set_statement( definition ); // attach statement to the label to be used later 99 if ( labelTable.find( l ) == labelTable.end() ) { 98 100 // all labels on this statement need to use the same entry, so this should only be created once 99 101 // undefined and unused until now, add an entry 100 labelTable[ *i] = e;101 } else if ( labelTable[ *i]->defined() ) {102 labelTable[ l ] = e; 103 } else if ( labelTable[ l ]->defined() ) { 102 104 // defined twice, error 103 throw SemanticError( "Duplicate definition of label: " + *i);105 throw SemanticError( "Duplicate definition of label: " + l.get_name() ); 104 106 } else { 105 107 // used previously, but undefined until now -> link with this entry 106 delete labelTable[ *i];107 labelTable[ *i] = e;108 delete labelTable[ l ]; 109 labelTable[ l ] = e; 108 110 } // if 109 111 } // for 110 112 111 // produce one of the labels attached to this statement to be 113 // produce one of the labels attached to this statement to be 112 114 // temporarily used as the canonical label 113 115 return labelTable[ llabel.front() ]->get_label(); 114 116 } 115 117 116 // A label was used, add it otthe table if it isn't already there118 // A label was used, add it to the table if it isn't already there 117 119 template< typename UsageNode > 118 120 void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) { … … 130 132 for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) { 131 133 if ( ! i->second->defined() ) { 132 throw SemanticError( "Use of undefined label: " + i->first );134 throw SemanticError( "Use of undefined label: " + i->first.get_name() ); 133 135 } 134 136 (*ret)[ i->first ] = i->second->get_definition(); -
src/ControlStruct/LabelFixer.h
r7305915 r87b5bf0 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // LabelFixer.h -- 7 // LabelFixer.h -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 20 20 #include "SynTree/SynTree.h" 21 21 #include "SynTree/Visitor.h" 22 #include "SynTree/Label.h" 22 23 #include "LabelGenerator.h" 23 24 24 #include <map> 25 25 … … 74 74 75 75 private: 76 Label label; 76 Label label; 77 77 Statement *definition; 78 78 }; 79 79 80 80 std::map < Label, Entry *> labelTable; 81 81 LabelGenerator *generator; -
src/ControlStruct/LabelGenerator.cc
r7305915 r87b5bf0 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // LabelGenerator.cc -- 7 // LabelGenerator.cc -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 18 18 19 19 #include "LabelGenerator.h" 20 #include "SynTree/Label.h" 21 #include "SynTree/Attribute.h" 20 22 21 23 namespace ControlStruct { … … 33 35 os << "__L" << current++ << "__" << suffix; 34 36 std::string ret = os.str(); 35 return Label( ret ); 37 Label l( ret ); 38 l.get_attributes().push_back( new Attribute("unused") ); 39 return l; 36 40 } 37 41 } // namespace ControlStruct -
src/ControlStruct/MLEMutator.cc
r7305915 r87b5bf0 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // MLEMutator.cc -- 7 // MLEMutator.cc -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 14 14 // 15 15 16 // NOTE: There are two known subtle differences from the code that uC++ 16 // NOTE: There are two known subtle differences from the code that uC++ 17 17 // generates for the same input 18 18 // -CFA puts the break label inside at the end of a switch, uC++ puts it after … … 27 27 #include "SynTree/Statement.h" 28 28 #include "SynTree/Expression.h" 29 #include "SynTree/Attribute.h" 29 30 30 31 namespace ControlStruct { … … 33 34 targetTable = 0; 34 35 } 35 36 // break labels have to come after the statement they break out of, 36 namespace { 37 Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; } 38 } 39 40 // break labels have to come after the statement they break out of, 37 41 // so mutate a statement, then if they inform us through the breakLabel field 38 // tha they need a place to jump to on a break statement, add the break label 42 // tha they need a place to jump to on a break statement, add the break label 39 43 // to the body of statements 40 44 void MLEMutator::fixBlock( std::list< Statement * > &kids ) { … … 44 48 if ( ! get_breakLabel().empty() ) { 45 49 std::list< Statement * >::iterator next = k+1; 46 if ( next == kids.end() ) { 47 std::list<Label> ls; ls.push_back( get_breakLabel() ); 48 kids.push_back( new NullStmt( ls ) ); 49 } else { 50 (*next)->get_labels().push_back( get_breakLabel() ); 51 } 52 50 std::list<Label> ls; ls.push_back( get_breakLabel() ); 51 kids.insert( next, new NullStmt( ls ) ); 53 52 set_breakLabel(""); 54 53 } // if … … 60 59 if ( labeledBlock ) { 61 60 Label brkLabel = generator->newLabel("blockBreak"); 62 enclosing Blocks.push_back( Entry( cmpndStmt, brkLabel ) );61 enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) ); 63 62 } // if 64 63 … … 69 68 70 69 if ( labeledBlock ) { 71 assert( ! enclosing Blocks.empty() );72 if ( ! enclosing Blocks.back().useBreakExit().empty() ) {73 set_breakLabel( enclosing Blocks.back().useBreakExit() );74 } 75 enclosing Blocks.pop_back();70 assert( ! enclosingControlStructures.empty() ); 71 if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { 72 set_breakLabel( enclosingControlStructures.back().useBreakExit() ); 73 } 74 enclosingControlStructures.pop_back(); 76 75 } // if 77 76 … … 81 80 template< typename LoopClass > 82 81 Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) { 83 // remember this as the most recent enclosing loop, then mutate 82 // remember this as the most recent enclosing loop, then mutate 84 83 // the body of the loop -- this will determine whether brkLabel 85 84 // and contLabel are used with branch statements … … 87 86 Label brkLabel = generator->newLabel("loopBreak"); 88 87 Label contLabel = generator->newLabel("loopContinue"); 89 enclosing Loops.push_back( Entry( loopStmt, brkLabel, contLabel ) );88 enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) ); 90 89 loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) ); 91 90 92 91 // sanity check that the enclosing loops have been popped correctly 93 Entry &e = enclosing Loops.back();92 Entry &e = enclosingControlStructures.back(); 94 93 assert ( e == loopStmt ); 95 94 … … 97 96 // two labels, if they are used. 98 97 loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) ); 99 enclosing Loops.pop_back();98 enclosingControlStructures.pop_back(); 100 99 101 100 return loopStmt; … … 111 110 template< typename SwitchClass > 112 111 Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) { 113 // generate a label for breaking out of a labeled switch 112 // generate a label for breaking out of a labeled switch 114 113 Label brkLabel = generator->newLabel("switchBreak"); 115 enclosing Switches.push_back( Entry(switchStmt, brkLabel) );116 mutateAll( switchStmt->get_branches(), *this ); 117 118 Entry &e = enclosing Switches.back();114 enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) ); 115 mutateAll( switchStmt->get_branches(), *this ); 116 117 Entry &e = enclosingControlStructures.back(); 119 118 assert ( e == switchStmt ); 120 119 121 120 // only generate break label if labeled break is used 122 121 if (e.isBreakUsed()) { 123 // for the purposes of keeping switch statements uniform (i.e. all statements that are 124 // direct children of a switch should be CastStmts), append the exit label + break to the 122 // for the purposes of keeping switch statements uniform (i.e. all statements that are 123 // direct children of a switch should be CastStmts), append the exit label + break to the 125 124 // last case statement; create a default case if there are no cases 126 125 std::list< Statement * > &branches = switchStmt->get_branches(); … … 131 130 if ( CaseStmt * c = dynamic_cast< CaseStmt * >( branches.back() ) ) { 132 131 std::list<Label> temp; temp.push_back( brkLabel ); 133 c->get_statements().push_back( new BranchStmt( temp, Label(" "), BranchStmt::Break ) );132 c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) ); 134 133 } else assert(0); // as of this point, all branches of a switch are still CaseStmts 135 134 } 136 135 137 assert ( enclosing Switches.back() == switchStmt );138 enclosing Switches.pop_back();136 assert ( enclosingControlStructures.back() == switchStmt ); 137 enclosingControlStructures.pop_back(); 139 138 return switchStmt; 140 139 } … … 143 142 std::string originalTarget = branchStmt->get_originalTarget(); 144 143 145 if ( branchStmt->get_type() == BranchStmt::Goto ) 144 std::list< Entry >::reverse_iterator targetEntry; 145 if ( branchStmt->get_type() == BranchStmt::Goto ) { 146 146 return branchStmt; 147 148 // test if continue target is a loop 149 if ( branchStmt->get_type() == BranchStmt::Continue) { 150 if ( enclosingLoops.empty() ) { 151 throw SemanticError( "'continue' outside a loop" ); 152 } else if ( branchStmt->get_target() != "" && std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) == enclosingLoops.end() ) { 153 throw SemanticError( "'continue' target label must be an enclosing loop: " + originalTarget ); 154 } 155 } 156 157 if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) ) 158 throw SemanticError( "'break' outside a loop or switch" ); 159 160 if ( branchStmt->get_target() == "" ) return branchStmt; 161 162 if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() ) 147 } else if ( branchStmt->get_type() == BranchStmt::Continue) { 148 // continue target must be a loop 149 if ( branchStmt->get_target() == "" ) { 150 targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } ); 151 } else { 152 // labelled continue - lookup label in table ot find attached control structure 153 targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] ); 154 } 155 if ( targetEntry == enclosingControlStructures.rend() || ! isLoop( targetEntry->get_controlStructure() ) ) { 156 throw SemanticError( "'continue' target must be an enclosing loop: " + originalTarget ); 157 } 158 } else if ( branchStmt->get_type() == BranchStmt::Break ) { 159 if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" ); 160 targetEntry = enclosingControlStructures.rbegin(); 161 } else { 162 assert( false ); 163 } 164 165 if ( branchStmt->get_target() != "" && targetTable->find( branchStmt->get_target() ) == targetTable->end() ) { 163 166 throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget ); // shouldn't happen (since that's already checked) 164 165 std::list< Entry >::iterator check; 166 if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() ) 167 // not in loop, checking if in block 168 if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() ) 169 // neither in loop nor in block, checking if in switch/choose 170 if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() ) 171 throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing control structure: " + originalTarget ); 172 173 // what about exiting innermost block or switch??? 174 if ( enclosingLoops.back() == (*check) ) 175 return branchStmt; // exit the innermost loop (labels unnecessary) 167 } 176 168 177 169 // branch error checks, get the appropriate label name and create a goto … … 179 171 switch ( branchStmt->get_type() ) { 180 172 case BranchStmt::Break: 181 assert( check->useBreakExit() != "");182 exitLabel = check->useBreakExit();173 assert( targetEntry->useBreakExit() != ""); 174 exitLabel = targetEntry->useBreakExit(); 183 175 break; 184 176 case BranchStmt::Continue: 185 assert( check->useContExit() != "");186 exitLabel = check->useContExit();177 assert( targetEntry->useContExit() != ""); 178 exitLabel = targetEntry->useContExit(); 187 179 break; 188 180 default: … … 190 182 } // switch 191 183 192 return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto ); 184 if ( branchStmt->get_target() == "" && branchStmt->get_type() != BranchStmt::Continue ) { 185 // unlabelled break/continue - can keep branch as break/continue for extra semantic information, but add 186 // exitLabel as its destination so that label passes can easily determine where the break/continue goes to 187 branchStmt->set_target( exitLabel ); 188 return branchStmt; 189 } else { 190 // labelled break/continue - can't easily emulate this with break and continue, so transform into a goto 191 delete branchStmt; 192 return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto ); 193 } 193 194 } 194 195 … … 206 207 // continue label goes in the body as the last statement 207 208 std::list< Label > labels; labels.push_back( e.useContExit() ); 208 newBody->get_kids().push_back( new NullStmt( labels ) ); 209 newBody->get_kids().push_back( new NullStmt( labels ) ); 209 210 } 210 211 211 212 if ( e.isBreakUsed() ) { 212 // break label goes after the loop -- it'll get set by the 213 // break label goes after the loop -- it'll get set by the 213 214 // outer mutator if we do this 214 set_breakLabel( e.useBreakExit() ); 215 set_breakLabel( e.useBreakExit() ); 215 216 } 216 217 … … 231 232 232 233 Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) { 233 return handleSwitchStmt( switchStmt ); 234 return handleSwitchStmt( switchStmt ); 234 235 } 235 236 -
src/ControlStruct/MLEMutator.h
r7305915 r87b5bf0 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // MLEMutator.h -- 7 // MLEMutator.h -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 23 23 #include "SynTree/SynTree.h" 24 24 #include "SynTree/Mutator.h" 25 #include "SynTree/Label.h" 25 26 26 27 #include "LabelGenerator.h" … … 38 39 Statement *mutate( BranchStmt *branchStmt ) throw ( SemanticError ); 39 40 40 Statement *mutate( CaseStmt *caseStmt ); 41 Statement *mutate( CaseStmt *caseStmt ); 41 42 Statement *mutate( SwitchStmt *switchStmt ); 42 43 Statement *mutate( ChooseStmt *switchStmt ); … … 55 56 bool operator!=( const Statement *stmt ) { return ( loop != stmt ); } 56 57 57 bool operator==( const Entry &other ) { return ( loop == other.get_ loop() ); }58 bool operator==( const Entry &other ) { return ( loop == other.get_controlStructure() ); } 58 59 59 Statement *get_ loop() const { return loop; }60 Statement *get_controlStructure() const { return loop; } 60 61 61 62 Label useContExit() { contUsed = true; return contExit; } … … 72 73 73 74 std::map< Label, Statement * > *targetTable; 74 std::list< Entry > enclosing Blocks, enclosingLoops, enclosingSwitches;75 std::list< Entry > enclosingControlStructures; 75 76 Label breakLabel; 76 77 LabelGenerator *generator; … … 79 80 Statement *handleLoopStmt( LoopClass *loopStmt ); 80 81 81 template< typename SwitchClass > 82 template< typename SwitchClass > 82 83 Statement *handleSwitchStmt( SwitchClass *switchStmt ); 83 84 -
src/ControlStruct/Mutate.cc
r7305915 r87b5bf0 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // Mutate.cc -- 7 // Mutate.cc -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 39 39 ForExprMutator formut; 40 40 41 // transform choose statements into switch statements 42 ChooseMutator chmut; 43 41 44 // normalizes label definitions and generates multi-level 42 45 // exit labels 43 46 LabelFixer lfix; 44 45 // transform choose statements into switch statements46 ChooseMutator chmut;47 47 48 48 // expand case ranges and turn fallthru into a null statement … … 53 53 54 54 mutateAll( translationUnit, formut ); 55 mutateAll( translationUnit, chmut ); 55 56 acceptAll( translationUnit, lfix ); 56 mutateAll( translationUnit, chmut );57 57 mutateAll( translationUnit, ranges ); 58 58 //mutateAll( translationUnit, exc );
Note:
See TracChangeset
for help on using the changeset viewer.