source: src/ControlStruct/MLEMutator.cc@ 05a0ff2

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 05a0ff2 was ba3706f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Remove label lists from various Statement constructors

  • Property mode set to 100644
File size: 10.7 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 <ext/alloc_traits.h> // for __alloc_traits<>::value_type
23#include <algorithm> // for find, find_if
24#include <cassert> // for assert, assertf
25#include <memory> // for allocator_traits<>::value_...
26
27#include "Common/utility.h" // for toString, operator+
28#include "ControlStruct/LabelGenerator.h" // for LabelGenerator
29#include "MLEMutator.h"
30#include "SynTree/Attribute.h" // for Attribute
31#include "SynTree/Expression.h" // for Expression
32#include "SynTree/Statement.h" // for BranchStmt, CompoundStmt
33
34namespace ControlStruct {
35 MLEMutator::~MLEMutator() {
36 delete targetTable;
37 targetTable = 0;
38 }
39 namespace {
40 Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; }
41 }
42
43 // break labels have to come after the statement they break out of, so mutate a statement, then if they inform us
44 // through the breakLabel field tha they need a place to jump to on a break statement, add the break label to the
45 // body of statements
46 void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
47 for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
48 *k = (*k)->acceptMutator(*this);
49
50 if ( ! get_breakLabel().empty() ) {
51 std::list< Statement * >::iterator next = k+1;
52 std::list<Label> ls; ls.push_back( get_breakLabel() );
53 kids.insert( next, new NullStmt( ls ) );
54 set_breakLabel("");
55 } // if
56 } // for
57 }
58
59 CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
60 bool labeledBlock = !(cmpndStmt->get_labels().empty());
61 if ( labeledBlock ) {
62 Label brkLabel = generator->newLabel("blockBreak", cmpndStmt);
63 enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) );
64 } // if
65
66 // a child statement may set the break label - if they do, attach it to the next statement
67 std::list< Statement * > &kids = cmpndStmt->get_kids();
68 fixBlock( kids );
69
70 if ( labeledBlock ) {
71 assert( ! enclosingControlStructures.empty() );
72 if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
73 set_breakLabel( enclosingControlStructures.back().useBreakExit() );
74 } // if
75 enclosingControlStructures.pop_back();
76 } // if
77
78 return cmpndStmt;
79 }
80
81 template< typename LoopClass >
82 Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
83 // remember this as the most recent enclosing loop, then mutate the body of the loop -- this will determine
84 // whether brkLabel and contLabel are used with branch statements and will recursively do the same to nested
85 // loops
86 Label brkLabel = generator->newLabel("loopBreak", loopStmt);
87 Label contLabel = generator->newLabel("loopContinue", loopStmt);
88 enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) );
89 loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
90
91 assert( ! enclosingControlStructures.empty() );
92 Entry &e = enclosingControlStructures.back();
93 // sanity check that the enclosing loops have been popped correctly
94 assert ( e == loopStmt );
95
96 // this will take the necessary steps to add definitions of the previous two labels, if they are used.
97 loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
98 enclosingControlStructures.pop_back();
99
100 return loopStmt;
101 }
102
103 Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
104 caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
105 fixBlock( caseStmt->get_statements() );
106
107 return caseStmt;
108 }
109
110 template< typename IfClass >
111 Statement *MLEMutator::handleIfStmt( IfClass *ifStmt ) {
112 // generate a label for breaking out of a labeled if
113 bool labeledBlock = !(ifStmt->get_labels().empty());
114 if ( labeledBlock ) {
115 Label brkLabel = generator->newLabel("blockBreak", ifStmt);
116 enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) );
117 } // if
118
119 Parent::mutate( ifStmt );
120
121 if ( labeledBlock ) {
122 if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
123 set_breakLabel( enclosingControlStructures.back().useBreakExit() );
124 } // if
125 enclosingControlStructures.pop_back();
126 } // if
127 return ifStmt;
128 }
129
130 template< typename SwitchClass >
131 Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
132 // generate a label for breaking out of a labeled switch
133 Label brkLabel = generator->newLabel("switchBreak", switchStmt);
134 enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) );
135 mutateAll( switchStmt->get_statements(), *this );
136
137 Entry &e = enclosingControlStructures.back();
138 assert ( e == switchStmt );
139
140 // only generate break label if labeled break is used
141 if ( e.isBreakUsed() ) {
142 // for the purposes of keeping switch statements uniform (i.e. all statements that are direct children of a
143 // switch should be CastStmts), append the exit label + break to the last case statement; create a default
144 // case if there are no cases
145 std::list< Statement * > &statements = switchStmt->get_statements();
146 if ( statements.empty() ) {
147 statements.push_back( CaseStmt::makeDefault() );
148 } // if
149
150 if ( CaseStmt * c = dynamic_cast< CaseStmt * >( statements.back() ) ) {
151 Statement * stmt = new BranchStmt( Label("brkLabel"), BranchStmt::Break );
152 stmt->labels.push_back( brkLabel );
153 c->get_statements().push_back( stmt );
154 } else assert(0); // as of this point, all statements of a switch are still CaseStmts
155 } // if
156
157 assert ( enclosingControlStructures.back() == switchStmt );
158 enclosingControlStructures.pop_back();
159 return switchStmt;
160 }
161
162 void addUnused( Statement * stmt, const Label & originalTarget ) {
163 // break/continue without a label doesn't need unused attribute
164 if ( originalTarget == "" ) return;
165 // add unused attribute to the originalTarget of a labelled break/continue
166 for ( Label & l : stmt->get_labels() ) {
167 // find the label to add unused attribute to
168 if ( l == originalTarget ) {
169 for ( Attribute * attr : l.get_attributes() ) {
170 // ensure attribute isn't added twice
171 if ( attr->get_name() == "unused" ) return;
172 }
173 l.get_attributes().push_back( new Attribute( "unused" ) );
174 return;
175 }
176 }
177 assertf( false, "Could not find label '%s' on statement %s", originalTarget.get_name().c_str(), toString( stmt ).c_str() );
178 }
179
180
181 Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
182 std::string originalTarget = branchStmt->get_originalTarget();
183
184 std::list< Entry >::reverse_iterator targetEntry;
185 switch ( branchStmt->get_type() ) {
186 case BranchStmt::Goto:
187 return branchStmt;
188 case BranchStmt::Continue:
189 case BranchStmt::Break: {
190 bool isContinue = branchStmt->get_type() == BranchStmt::Continue;
191 // unlabeled break/continue
192 if ( branchStmt->get_target() == "" ) {
193 if ( isContinue ) {
194 // continue target is outermost loop
195 targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } );
196 } else {
197 // break target is outmost control structure
198 if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" );
199 targetEntry = enclosingControlStructures.rbegin();
200 } // if
201 } else {
202 // labeled break/continue - lookup label in table to find attached control structure
203 targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] );
204 } // if
205 // ensure that selected target is valid
206 if ( targetEntry == enclosingControlStructures.rend() || (isContinue && ! isLoop( targetEntry->get_controlStructure() ) ) ) {
207 throw SemanticError( toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) );
208 } // if
209 break;
210 }
211 default:
212 assert( false );
213 } // switch
214
215 // branch error checks, get the appropriate label name and create a goto
216 Label exitLabel;
217 switch ( branchStmt->get_type() ) {
218 case BranchStmt::Break:
219 assert( targetEntry->useBreakExit() != "");
220 exitLabel = targetEntry->useBreakExit();
221 break;
222 case BranchStmt::Continue:
223 assert( targetEntry->useContExit() != "");
224 exitLabel = targetEntry->useContExit();
225 break;
226 default:
227 assert(0); // shouldn't be here
228 } // switch
229
230 // add unused attribute to label to silence warnings
231 addUnused( targetEntry->get_controlStructure(), branchStmt->get_originalTarget() );
232
233 // transform break/continue statements into goto to simplify later handling of branches
234 delete branchStmt;
235 return new BranchStmt( exitLabel, BranchStmt::Goto );
236 }
237
238 Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
239 // ensure loop body is a block
240 CompoundStmt *newBody;
241 if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
242 newBody = new CompoundStmt();
243 newBody->get_kids().push_back( bodyLoop );
244 } // if
245
246 // only generate these when needed
247
248 if ( e.isContUsed() ) {
249 // continue label goes in the body as the last statement
250 std::list< Label > labels; labels.push_back( e.useContExit() );
251 newBody->get_kids().push_back( new NullStmt( labels ) );
252 } // if
253
254 if ( e.isBreakUsed() ) {
255 // break label goes after the loop -- it'll get set by the outer mutator if we do this
256 set_breakLabel( e.useBreakExit() );
257 } // if
258
259 return newBody;
260 }
261
262 Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
263 return handleLoopStmt( whileStmt );
264 }
265
266 Statement *MLEMutator::mutate( ForStmt *forStmt ) {
267 return handleLoopStmt( forStmt );
268 }
269
270 Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
271 return handleIfStmt( ifStmt );
272 }
273
274 Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
275 return handleSwitchStmt( switchStmt );
276 }
277} // namespace ControlStruct
278
279// Local Variables: //
280// tab-width: 4 //
281// mode: c++ //
282// compile-command: "make install" //
283// End: //
Note: See TracBrowser for help on using the repository browser.