source: src/ControlStruct/MLEMutator.cc@ e2f1eeb

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

change all break/continue statements into goto for consistency and fix bug with labeled break

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[51587aa]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[23b6f4d7]7// MLEMutator.cc --
[51587aa]8//
[843054c2]9// Author : Rodolfo G. Esteves
[51587aa]10// Created On : Mon May 18 07:44:20 2015
[adcc065]11// Last Modified By : Peter A. Buhr
[8688ce1]12// Last Modified On : Thu Aug 4 11:21:32 2016
13// Update Count : 202
[51587aa]14//
[a08ba92]15
[adcc065]16// NOTE: There are two known subtle differences from the code that uC++ generates for the same input
17// -CFA puts the break label inside at the end of a switch, uC++ puts it after
18// -CFA puts the break label after a block, uC++ puts it inside at the end
19// It is unclear if these differences are important, but if they are, then the fix would go in this file, since this is
20// where these labels are generated.
[994ec2c]21
[51b73452]22#include <cassert>
23#include <algorithm>
24
25#include "MLEMutator.h"
26#include "SynTree/Statement.h"
[27de955]27#include "SynTree/Expression.h"
[e39aa0f]28#include "SynTree/Attribute.h"
[51b73452]29
30namespace ControlStruct {
[a08ba92]31 MLEMutator::~MLEMutator() {
32 delete targetTable;
33 targetTable = 0;
34 }
[e39aa0f]35 namespace {
36 Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; }
37 }
[a08ba92]38
[adcc065]39 // break labels have to come after the statement they break out of, so mutate a statement, then if they inform us
40 // through the breakLabel field tha they need a place to jump to on a break statement, add the break label to the
41 // body of statements
[27de955]42 void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
[a08ba92]43 for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
44 *k = (*k)->acceptMutator(*this);
45
46 if ( ! get_breakLabel().empty() ) {
[d939274]47 std::list< Statement * >::iterator next = k+1;
[e39aa0f]48 std::list<Label> ls; ls.push_back( get_breakLabel() );
49 kids.insert( next, new NullStmt( ls ) );
[a08ba92]50 set_breakLabel("");
51 } // if
52 } // for
[27de955]53 }
54
55 CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
56 bool labeledBlock = !(cmpndStmt->get_labels().empty());
57 if ( labeledBlock ) {
[af68f0a]58 Label brkLabel = generator->newLabel("blockBreak", cmpndStmt);
[e39aa0f]59 enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) );
[27de955]60 } // if
61
[adcc065]62 // a child statement may set the break label - if they do, attach it to the next statement
[27de955]63 std::list< Statement * > &kids = cmpndStmt->get_kids();
64 fixBlock( kids );
[a08ba92]65
66 if ( labeledBlock ) {
[e39aa0f]67 assert( ! enclosingControlStructures.empty() );
68 if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
69 set_breakLabel( enclosingControlStructures.back().useBreakExit() );
[adcc065]70 } // if
[e39aa0f]71 enclosingControlStructures.pop_back();
[a08ba92]72 } // if
73
74 return cmpndStmt;
75 }
76
[27de955]77 template< typename LoopClass >
78 Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
[adcc065]79 // remember this as the most recent enclosing loop, then mutate the body of the loop -- this will determine
80 // whether brkLabel and contLabel are used with branch statements and will recursively do the same to nested
81 // loops
[af68f0a]82 Label brkLabel = generator->newLabel("loopBreak", loopStmt);
83 Label contLabel = generator->newLabel("loopContinue", loopStmt);
[e39aa0f]84 enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) );
[27de955]85 loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
86
[af68f0a]87 assert( ! enclosingControlStructures.empty() );
[e39aa0f]88 Entry &e = enclosingControlStructures.back();
[adcc065]89 // sanity check that the enclosing loops have been popped correctly
[27de955]90 assert ( e == loopStmt );
91
[adcc065]92 // this will take the necessary steps to add definitions of the previous two labels, if they are used.
[27de955]93 loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
[e39aa0f]94 enclosingControlStructures.pop_back();
[a08ba92]95
[27de955]96 return loopStmt;
[d9a0e76]97 }
98
[27de955]99 Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
100 caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
101 fixBlock( caseStmt->get_statements() );
[a08ba92]102
[27de955]103 return caseStmt;
104 }
105
[adcc065]106 template< typename IfClass >
107 Statement *MLEMutator::handleIfStmt( IfClass *ifStmt ) {
108 // generate a label for breaking out of a labeled if
109 bool labeledBlock = !(ifStmt->get_labels().empty());
110 if ( labeledBlock ) {
[af68f0a]111 Label brkLabel = generator->newLabel("blockBreak", ifStmt);
[adcc065]112 enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) );
113 } // if
114
115 Parent::mutate( ifStmt );
[af68f0a]116
[adcc065]117 if ( labeledBlock ) {
118 if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
119 set_breakLabel( enclosingControlStructures.back().useBreakExit() );
120 } // if
121 enclosingControlStructures.pop_back();
122 } // if
123 return ifStmt;
124 }
125
[27de955]126 template< typename SwitchClass >
127 Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
[23b6f4d7]128 // generate a label for breaking out of a labeled switch
[af68f0a]129 Label brkLabel = generator->newLabel("switchBreak", switchStmt);
[e39aa0f]130 enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) );
[8688ce1]131 mutateAll( switchStmt->get_statements(), *this );
[a08ba92]132
[e39aa0f]133 Entry &e = enclosingControlStructures.back();
[27de955]134 assert ( e == switchStmt );
135
136 // only generate break label if labeled break is used
[adcc065]137 if ( e.isBreakUsed() ) {
138 // for the purposes of keeping switch statements uniform (i.e. all statements that are direct children of a
139 // switch should be CastStmts), append the exit label + break to the last case statement; create a default
140 // case if there are no cases
[8688ce1]141 std::list< Statement * > &statements = switchStmt->get_statements();
142 if ( statements.empty() ) {
143 statements.push_back( CaseStmt::makeDefault() );
[adcc065]144 } // if
[27de955]145
[8688ce1]146 if ( CaseStmt * c = dynamic_cast< CaseStmt * >( statements.back() ) ) {
[27de955]147 std::list<Label> temp; temp.push_back( brkLabel );
[e39aa0f]148 c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) );
[8688ce1]149 } else assert(0); // as of this point, all statements of a switch are still CaseStmts
[adcc065]150 } // if
[27de955]151
[e39aa0f]152 assert ( enclosingControlStructures.back() == switchStmt );
153 enclosingControlStructures.pop_back();
[27de955]154 return switchStmt;
[d9a0e76]155 }
[a08ba92]156
157 Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
[be5aa1b]158 std::string originalTarget = branchStmt->get_originalTarget();
159
[e39aa0f]160 std::list< Entry >::reverse_iterator targetEntry;
[af68f0a]161 switch ( branchStmt->get_type() ) {
162 case BranchStmt::Goto:
163 return branchStmt;
164 case BranchStmt::Continue:
165 case BranchStmt::Break: {
166 bool isContinue = branchStmt->get_type() == BranchStmt::Continue;
167 // unlabeled break/continue
168 if ( branchStmt->get_target() == "" ) {
169 if ( isContinue ) {
170 // continue target is outermost loop
171 targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } );
172 } else {
173 // break target is outmost control structure
174 if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" );
175 targetEntry = enclosingControlStructures.rbegin();
176 } // if
177 } else {
178 // labeled break/continue - lookup label in table to find attached control structure
179 targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] );
180 } // if
181 // ensure that selected target is valid
182 if ( targetEntry == enclosingControlStructures.rend() || (isContinue && ! isLoop( targetEntry->get_controlStructure() ) ) ) {
183 throw SemanticError( toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) );
184 } // if
185 break;
186 }
187 default:
188 assert( false );
189 } // switch
[a08ba92]190
[27de955]191 // branch error checks, get the appropriate label name and create a goto
192 Label exitLabel;
[a08ba92]193 switch ( branchStmt->get_type() ) {
194 case BranchStmt::Break:
[e39aa0f]195 assert( targetEntry->useBreakExit() != "");
196 exitLabel = targetEntry->useBreakExit();
[be5aa1b]197 break;
[a08ba92]198 case BranchStmt::Continue:
[e39aa0f]199 assert( targetEntry->useContExit() != "");
200 exitLabel = targetEntry->useContExit();
[be5aa1b]201 break;
[a08ba92]202 default:
[27de955]203 assert(0); // shouldn't be here
[a08ba92]204 } // switch
205
[af68f0a]206 // transform break/continue statements into goto to simplify later handling of branches
207 delete branchStmt;
208 return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto );
[a08ba92]209 }
210
211 Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
[d939274]212 // ensure loop body is a block
[a08ba92]213 CompoundStmt *newBody;
214 if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
215 newBody = new CompoundStmt( std::list< Label >() );
216 newBody->get_kids().push_back( bodyLoop );
217 } // if
218
[27de955]219 // only generate these when needed
[a08ba92]220
[27de955]221 if ( e.isContUsed() ) {
222 // continue label goes in the body as the last statement
223 std::list< Label > labels; labels.push_back( e.useContExit() );
[e39aa0f]224 newBody->get_kids().push_back( new NullStmt( labels ) );
[adcc065]225 } // if
[27de955]226
227 if ( e.isBreakUsed() ) {
[adcc065]228 // break label goes after the loop -- it'll get set by the outer mutator if we do this
[23b6f4d7]229 set_breakLabel( e.useBreakExit() );
[adcc065]230 } // if
[a08ba92]231
232 return newBody;
233 }
234
[27de955]235 Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
236 return handleLoopStmt( whileStmt );
237 }
238
239 Statement *MLEMutator::mutate( ForStmt *forStmt ) {
240 return handleLoopStmt( forStmt );
241 }
242
[adcc065]243 Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
244 return handleIfStmt( ifStmt );
245 }
246
[27de955]247 Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
248 return handleSwitchStmt( switchStmt );
[a08ba92]249 }
[51b73452]250} // namespace ControlStruct
[a08ba92]251
[51587aa]252// Local Variables: //
253// tab-width: 4 //
254// mode: c++ //
255// compile-command: "make install" //
256// End: //
Note: See TracBrowser for help on using the repository browser.