source: src/ControlStruct/MLEMutator.cc@ 9a8930f

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

fix multi-level exit code (labeled break and continue)

  • Property mode set to 100644
File size: 8.2 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//
[a08ba92]7// MLEMutator.cc --
[51587aa]8//
[843054c2]9// Author : Rodolfo G. Esteves
[51587aa]10// Created On : Mon May 18 07:44:20 2015
[be5aa1b]11// Last Modified By : Rob Schluntz
[27de955]12// Last Modified On : Wed Jun 03 15:09:27 2015
13// Update Count : 170
[51587aa]14//
[a08ba92]15
[51b73452]16#include <cassert>
17#include <algorithm>
18
19#include "MLEMutator.h"
20#include "SynTree/Statement.h"
[27de955]21#include "SynTree/Expression.h"
[51b73452]22
23namespace ControlStruct {
[a08ba92]24 MLEMutator::~MLEMutator() {
25 delete targetTable;
26 targetTable = 0;
27 }
28
[27de955]29 // break labels have to come after the statement they break out of,
30 // so mutate a statement, then if they inform us through the breakLabel field
31 // tha they need a place to jump to on a break statement, add the break label
32 // to the body of statements
33 void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
[a08ba92]34 for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
35 *k = (*k)->acceptMutator(*this);
36
37 if ( ! get_breakLabel().empty() ) {
[d939274]38 std::list< Statement * >::iterator next = k+1;
[a08ba92]39 if ( next == kids.end() ) {
40 std::list<Label> ls; ls.push_back( get_breakLabel() );
41 kids.push_back( new NullStmt( ls ) );
[be5aa1b]42 } else {
[a08ba92]43 (*next)->get_labels().push_back( get_breakLabel() );
[be5aa1b]44 }
[a08ba92]45
46 set_breakLabel("");
47 } // if
48 } // for
[27de955]49 }
50
51 CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
52 bool labeledBlock = !(cmpndStmt->get_labels().empty());
53 if ( labeledBlock ) {
54 Label brkLabel = generator->newLabel();
55 enclosingBlocks.push_back( Entry( cmpndStmt, brkLabel ) );
56 } // if
57
58 // a child statement may set the break label
59 // - if they do, attach it to the next statement
60 std::list< Statement * > &kids = cmpndStmt->get_kids();
61 fixBlock( kids );
[a08ba92]62
63 if ( labeledBlock ) {
64 assert( ! enclosingBlocks.empty() );
[27de955]65 if ( ! enclosingBlocks.back().useBreakExit().empty() ) {
66 set_breakLabel( enclosingBlocks.back().useBreakExit() );
[be5aa1b]67 }
[a08ba92]68 enclosingBlocks.pop_back();
69 } // if
70
71 return cmpndStmt;
72 }
73
[27de955]74 template< typename LoopClass >
75 Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
76 // remember this as the most recent enclosing loop, then mutate
77 // the body of the loop -- this will do SOMETHING with branch statements
78 // and will recursively do the same to nested loops
79 Label brkLabel = generator->newLabel("loopBreak");
80 Label contLabel = generator->newLabel("loopContinue");
81 enclosingLoops.push_back( Entry( loopStmt, brkLabel, contLabel ) );
82 loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
83
84 // sanity check that the enclosing loops have been popped correctly
[a08ba92]85 Entry &e = enclosingLoops.back();
[27de955]86 assert ( e == loopStmt );
87
88 // generate labels as needed
89 loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
[a08ba92]90 enclosingLoops.pop_back();
91
[27de955]92 return loopStmt;
[d9a0e76]93 }
94
[27de955]95 Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
96 caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
97 fixBlock( caseStmt->get_statements() );
[a08ba92]98
[27de955]99 return caseStmt;
100 }
101
102 template< typename SwitchClass >
103 Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
104 // generate a label for breaking out of a labeled switch
105 Label brkLabel = generator->newLabel("switchBreak");
106 enclosingSwitches.push_back( Entry(switchStmt, brkLabel) );
107 mutateAll( switchStmt->get_branches(), *this );
[a08ba92]108
[27de955]109 Entry &e = enclosingSwitches.back();
110 assert ( e == switchStmt );
111
112 // only generate break label if labeled break is used
113 if (e.isBreakUsed()) {
114 // for the purposes of keeping switch statements uniform (i.e. all statements that are
115 // direct children of a switch should be CastStmts), append the exit label + break to the
116 // last case statement; create a default case if there are no cases
117 std::list< Statement * > &branches = switchStmt->get_branches();
118 if ( branches.empty() ) {
119 branches.push_back( CaseStmt::makeDefault() );
120 }
121
122 if ( CaseStmt * c = dynamic_cast< CaseStmt * >( branches.back() ) ) {
123 std::list<Label> temp; temp.push_back( brkLabel );
124 c->get_statements().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
125 } else assert(0); // as of this point, all branches of a switch are still CaseStmts
126 }
127
128 assert ( enclosingSwitches.back() == switchStmt );
129 enclosingSwitches.pop_back();
130 return switchStmt;
[d9a0e76]131 }
[a08ba92]132
133 Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
[be5aa1b]134 std::string originalTarget = branchStmt->get_originalTarget();
135
[a08ba92]136 if ( branchStmt->get_type() == BranchStmt::Goto )
137 return branchStmt;
138
139 // test if continue target is a loop
[be5aa1b]140 if ( branchStmt->get_type() == BranchStmt::Continue) {
141 if ( enclosingLoops.empty() ) {
142 throw SemanticError( "'continue' outside a loop" );
143 } else if ( std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) == enclosingLoops.end() ) {
144 throw SemanticError( "'continue' target label must be an enclosing loop: " + originalTarget );
145 }
146 }
[a08ba92]147
148 if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
149 throw SemanticError( "'break' outside a loop or switch" );
150
151 if ( branchStmt->get_target() == "" ) return branchStmt;
152
153 if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
[be5aa1b]154 throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget ); // shouldn't happen (since that's already checked)
[a08ba92]155
156 std::list< Entry >::iterator check;
157 if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
158 // not in loop, checking if in block
159 if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
160 // neither in loop nor in block, checking if in switch/choose
161 if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
[be5aa1b]162 throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing control structure: " + originalTarget );
[a08ba92]163
[27de955]164 // what about exiting innermost block or switch???
[a08ba92]165 if ( enclosingLoops.back() == (*check) )
166 return branchStmt; // exit the innermost loop (labels unnecessary)
167
[27de955]168 // branch error checks, get the appropriate label name and create a goto
169 Label exitLabel;
[a08ba92]170 switch ( branchStmt->get_type() ) {
171 case BranchStmt::Break:
[27de955]172 assert( check->useBreakExit() != "");
173 exitLabel = check->useBreakExit();
[be5aa1b]174 break;
[a08ba92]175 case BranchStmt::Continue:
[27de955]176 assert( check->useContExit() != "");
177 exitLabel = check->useContExit();
[be5aa1b]178 break;
[a08ba92]179 default:
[27de955]180 assert(0); // shouldn't be here
[a08ba92]181 } // switch
182
[27de955]183 return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto );
[a08ba92]184 }
185
186 Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
[d939274]187 // ensure loop body is a block
[a08ba92]188 CompoundStmt *newBody;
189 if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
190 newBody = new CompoundStmt( std::list< Label >() );
191 newBody->get_kids().push_back( bodyLoop );
192 } // if
193
[27de955]194 // only generate these when needed
[a08ba92]195
[27de955]196 if ( e.isContUsed() ) {
197 // continue label goes in the body as the last statement
198 std::list< Label > labels; labels.push_back( e.useContExit() );
199 newBody->get_kids().push_back( new NullStmt( labels ) );
200 }
201
202 if ( e.isBreakUsed() ) {
203 // break label goes after the loop -- it'll get set by the
204 // outer mutator if we do this
205 set_breakLabel( e.useBreakExit() );
206 }
[a08ba92]207
208 return newBody;
209 }
210
[27de955]211 Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
212 return handleLoopStmt( whileStmt );
213 }
214
215 Statement *MLEMutator::mutate( ForStmt *forStmt ) {
216 return handleLoopStmt( forStmt );
217 }
218
219 Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
220 return handleSwitchStmt( switchStmt );
[a08ba92]221 }
222
[27de955]223 Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) {
224 return handleSwitchStmt( switchStmt );
[d9a0e76]225 }
[27de955]226
[51b73452]227} // namespace ControlStruct
[a08ba92]228
[51587aa]229// Local Variables: //
230// tab-width: 4 //
231// mode: c++ //
232// compile-command: "make install" //
233// End: //
Note: See TracBrowser for help on using the repository browser.