source: src/ControlStruct/MLEMutator.cc @ db67b11

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since db67b11 was 33c4b81, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add unused attribute to original target of labelled break/continue to silence warnings

  • Property mode set to 100644
File size: 10.2 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        void addUnused( Statement * stmt, const Label & originalTarget ) {
158                // break/continue without a label doesn't need unused attribute
159                if ( originalTarget == "" ) return;
160                // add unused attribute to the originalTarget of a labelled break/continue
161                for ( Label & l : stmt->get_labels() ) {
162                        // find the label to add unused attribute to
163                        if ( l == originalTarget ) {
164                                for ( Attribute * attr : l.get_attributes() ) {
165                                        // ensure attribute isn't added twice
166                                        if ( attr->get_name() == "unused" ) return;
167                                }
168                                l.get_attributes().push_back( new Attribute( "unused" ) );
169                                return;
170                        }
171                }
172                assertf( false, "Could not find label '%s' on statement %s", originalTarget.get_name().c_str(), toString( stmt ).c_str() );
173        }
174
175
176        Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
177                std::string originalTarget = branchStmt->get_originalTarget();
178
179                std::list< Entry >::reverse_iterator targetEntry;
180                switch ( branchStmt->get_type() ) {
181                        case BranchStmt::Goto:
182                                return branchStmt;
183                        case BranchStmt::Continue:
184                        case BranchStmt::Break: {
185                                bool isContinue = branchStmt->get_type() == BranchStmt::Continue;
186                                // unlabeled break/continue
187                                if ( branchStmt->get_target() == "" ) {
188                                        if ( isContinue ) {
189                                                // continue target is outermost loop
190                                                targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } );
191                                        } else {
192                                                // break target is outmost control structure
193                                                if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" );
194                                                targetEntry = enclosingControlStructures.rbegin();
195                                        } // if
196                                } else {
197                                        // labeled break/continue - lookup label in table to find attached control structure
198                                        targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] );
199                                } // if
200                                // ensure that selected target is valid
201                                if ( targetEntry == enclosingControlStructures.rend() || (isContinue && ! isLoop( targetEntry->get_controlStructure() ) ) ) {
202                                        throw SemanticError( toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) );
203                                } // if
204                                break;
205                        }
206                        default:
207                                assert( false );
208                } // switch
209
210                // branch error checks, get the appropriate label name and create a goto
211                Label exitLabel;
212                switch ( branchStmt->get_type() ) {
213                  case BranchStmt::Break:
214                                assert( targetEntry->useBreakExit() != "");
215                                exitLabel = targetEntry->useBreakExit();
216                                break;
217                  case BranchStmt::Continue:
218                                assert( targetEntry->useContExit() != "");
219                                exitLabel = targetEntry->useContExit();
220                                break;
221                  default:
222                                assert(0);                                      // shouldn't be here
223                } // switch
224
225                // add unused attribute to label to silence warnings
226                addUnused( targetEntry->get_controlStructure(), branchStmt->get_originalTarget() );
227
228                // transform break/continue statements into goto to simplify later handling of branches
229                delete branchStmt;
230                return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto );
231        }
232
233        Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
234                // ensure loop body is a block
235                CompoundStmt *newBody;
236                if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
237                        newBody = new CompoundStmt( std::list< Label >() );
238                        newBody->get_kids().push_back( bodyLoop );
239                } // if
240
241                // only generate these when needed
242
243                if ( e.isContUsed() ) {
244                        // continue label goes in the body as the last statement
245                        std::list< Label > labels; labels.push_back( e.useContExit() );
246                        newBody->get_kids().push_back( new NullStmt( labels ) );
247                } // if
248
249                if ( e.isBreakUsed() ) {
250                        // break label goes after the loop -- it'll get set by the outer mutator if we do this
251                        set_breakLabel( e.useBreakExit() );
252                } // if
253
254                return newBody;
255        }
256
257        Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
258                return handleLoopStmt( whileStmt );
259        }
260
261        Statement *MLEMutator::mutate( ForStmt *forStmt ) {
262                return handleLoopStmt( forStmt );
263        }
264
265        Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
266                return handleIfStmt( ifStmt );
267        }
268
269        Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
270                return handleSwitchStmt( switchStmt );
271        }
272} // namespace ControlStruct
273
274// Local Variables: //
275// tab-width: 4 //
276// mode: c++ //
277// compile-command: "make install" //
278// End: //
Note: See TracBrowser for help on using the repository browser.