source: src/ControlStruct/MLEMutator.cc @ 68f9c43

new-envwith_gc
Last change on this file since 68f9c43 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 6 years ago

First pass at delete removal

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