source: src/ControlStruct/MLEMutator.cc@ 533804b

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 533804b 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
Line 
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//
7// MLEMutator.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Aug 4 11:21:32 2016
13// Update Count : 202
14//
15
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.
21
22#include <cassert>
23#include <algorithm>
24
25#include "MLEMutator.h"
26#include "SynTree/Statement.h"
27#include "SynTree/Expression.h"
28#include "SynTree/Attribute.h"
29
30namespace ControlStruct {
31 MLEMutator::~MLEMutator() {
32 delete targetTable;
33 targetTable = 0;
34 }
35 namespace {
36 Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; }
37 }
38
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
42 void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
43 for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
44 *k = (*k)->acceptMutator(*this);
45
46 if ( ! get_breakLabel().empty() ) {
47 std::list< Statement * >::iterator next = k+1;
48 std::list<Label> ls; ls.push_back( get_breakLabel() );
49 kids.insert( next, new NullStmt( ls ) );
50 set_breakLabel("");
51 } // if
52 } // for
53 }
54
55 CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
56 bool labeledBlock = !(cmpndStmt->get_labels().empty());
57 if ( labeledBlock ) {
58 Label brkLabel = generator->newLabel("blockBreak", cmpndStmt);
59 enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) );
60 } // if
61
62 // a child statement may set the break label - if they do, attach it to the next statement
63 std::list< Statement * > &kids = cmpndStmt->get_kids();
64 fixBlock( kids );
65
66 if ( labeledBlock ) {
67 assert( ! enclosingControlStructures.empty() );
68 if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
69 set_breakLabel( enclosingControlStructures.back().useBreakExit() );
70 } // if
71 enclosingControlStructures.pop_back();
72 } // if
73
74 return cmpndStmt;
75 }
76
77 template< typename LoopClass >
78 Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
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
82 Label brkLabel = generator->newLabel("loopBreak", loopStmt);
83 Label contLabel = generator->newLabel("loopContinue", loopStmt);
84 enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) );
85 loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
86
87 assert( ! enclosingControlStructures.empty() );
88 Entry &e = enclosingControlStructures.back();
89 // sanity check that the enclosing loops have been popped correctly
90 assert ( e == loopStmt );
91
92 // this will take the necessary steps to add definitions of the previous two labels, if they are used.
93 loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
94 enclosingControlStructures.pop_back();
95
96 return loopStmt;
97 }
98
99 Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
100 caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
101 fixBlock( caseStmt->get_statements() );
102
103 return caseStmt;
104 }
105
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 ) {
111 Label brkLabel = generator->newLabel("blockBreak", ifStmt);
112 enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) );
113 } // if
114
115 Parent::mutate( ifStmt );
116
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
126 template< typename SwitchClass >
127 Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
128 // generate a label for breaking out of a labeled switch
129 Label brkLabel = generator->newLabel("switchBreak", switchStmt);
130 enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) );
131 mutateAll( switchStmt->get_statements(), *this );
132
133 Entry &e = enclosingControlStructures.back();
134 assert ( e == switchStmt );
135
136 // only generate break label if labeled break is used
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
141 std::list< Statement * > &statements = switchStmt->get_statements();
142 if ( statements.empty() ) {
143 statements.push_back( CaseStmt::makeDefault() );
144 } // if
145
146 if ( CaseStmt * c = dynamic_cast< CaseStmt * >( statements.back() ) ) {
147 std::list<Label> temp; temp.push_back( brkLabel );
148 c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) );
149 } else assert(0); // as of this point, all statements of a switch are still CaseStmts
150 } // if
151
152 assert ( enclosingControlStructures.back() == switchStmt );
153 enclosingControlStructures.pop_back();
154 return switchStmt;
155 }
156
157 Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
158 std::string originalTarget = branchStmt->get_originalTarget();
159
160 std::list< Entry >::reverse_iterator targetEntry;
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
190
191 // branch error checks, get the appropriate label name and create a goto
192 Label exitLabel;
193 switch ( branchStmt->get_type() ) {
194 case BranchStmt::Break:
195 assert( targetEntry->useBreakExit() != "");
196 exitLabel = targetEntry->useBreakExit();
197 break;
198 case BranchStmt::Continue:
199 assert( targetEntry->useContExit() != "");
200 exitLabel = targetEntry->useContExit();
201 break;
202 default:
203 assert(0); // shouldn't be here
204 } // switch
205
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 );
209 }
210
211 Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
212 // ensure loop body is a block
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
219 // only generate these when needed
220
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() );
224 newBody->get_kids().push_back( new NullStmt( labels ) );
225 } // if
226
227 if ( e.isBreakUsed() ) {
228 // break label goes after the loop -- it'll get set by the outer mutator if we do this
229 set_breakLabel( e.useBreakExit() );
230 } // if
231
232 return newBody;
233 }
234
235 Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
236 return handleLoopStmt( whileStmt );
237 }
238
239 Statement *MLEMutator::mutate( ForStmt *forStmt ) {
240 return handleLoopStmt( forStmt );
241 }
242
243 Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
244 return handleIfStmt( ifStmt );
245 }
246
247 Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
248 return handleSwitchStmt( switchStmt );
249 }
250} // namespace ControlStruct
251
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.