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

new-envwith_gc
Last change on this file since 9f2012f 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
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//
[23b6f4d7]7// MLEMutator.cc --
[51587aa]8//
[843054c2]9// Author           : Rodolfo G. Esteves
[51587aa]10// Created On       : Mon May 18 07:44:20 2015
[adcc065]11// Last Modified By : Peter A. Buhr
[8688ce1]12// Last Modified On : Thu Aug  4 11:21:32 2016
13// Update Count     : 202
[51587aa]14//
[a08ba92]15
[adcc065]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.
[994ec2c]21
[d180746]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_...
[51b7345]26
[d180746]27#include "Common/utility.h"                // for toString, operator+
28#include "ControlStruct/LabelGenerator.h"  // for LabelGenerator
[51b7345]29#include "MLEMutator.h"
[d180746]30#include "SynTree/Attribute.h"             // for Attribute
31#include "SynTree/Expression.h"            // for Expression
32#include "SynTree/Statement.h"             // for BranchStmt, CompoundStmt
[51b7345]33
34namespace ControlStruct {
[a08ba92]35        MLEMutator::~MLEMutator() {
36                delete targetTable;
37                targetTable = 0;
38        }
[e39aa0f]39        namespace {
40                Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; }
41        }
[a08ba92]42
[adcc065]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
[27de955]46        void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
[a08ba92]47                for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
[94e025a2]48                        *k = (*k)->acceptMutator(*visitor);
[a08ba92]49
50                        if ( ! get_breakLabel().empty() ) {
[d939274]51                                std::list< Statement * >::iterator next = k+1;
[e39aa0f]52                                std::list<Label> ls; ls.push_back( get_breakLabel() );
53                                kids.insert( next, new NullStmt( ls ) );
[a08ba92]54                                set_breakLabel("");
55                        } // if
56                } // for
[27de955]57        }
58
[94e025a2]59        void MLEMutator::premutate( CompoundStmt *cmpndStmt ) {
60                visit_children = false;
61                bool labeledBlock = !(cmpndStmt->labels.empty());
[27de955]62                if ( labeledBlock ) {
[af68f0a]63                        Label brkLabel = generator->newLabel("blockBreak", cmpndStmt);
[e39aa0f]64                        enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) );
[27de955]65                } // if
66
[adcc065]67                // a child statement may set the break label - if they do, attach it to the next statement
[94e025a2]68                std::list< Statement * > &kids = cmpndStmt->kids;
[27de955]69                fixBlock( kids );
[a08ba92]70
71                if ( labeledBlock ) {
[e39aa0f]72                        assert( ! enclosingControlStructures.empty() );
73                        if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
74                                set_breakLabel( enclosingControlStructures.back().useBreakExit() );
[adcc065]75                        } // if
[e39aa0f]76                        enclosingControlStructures.pop_back();
[a08ba92]77                } // if
[adcc065]78        }
79
[a08ba92]80
[33c4b81]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
[a16764a6]100        Statement *MLEMutator::postmutate( BranchStmt *branchStmt ) throw ( SemanticErrorException ) {
[94e025a2]101                std::string originalTarget = branchStmt->originalTarget;
[be5aa1b]102
[e39aa0f]103                std::list< Entry >::reverse_iterator targetEntry;
[af68f0a]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
[a16764a6]117                                                if ( enclosingControlStructures.empty() ) SemanticError( branchStmt->location, "'break' outside a loop, switch, or labelled block" );
[af68f0a]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() ) ) ) {
[a16764a6]126                                        SemanticError( branchStmt->location, toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) );
[af68f0a]127                                } // if
128                                break;
129                        }
130                        default:
131                                assert( false );
132                } // switch
[a08ba92]133
[27de955]134                // branch error checks, get the appropriate label name and create a goto
135                Label exitLabel;
[94e025a2]136                switch ( branchStmt->type ) {
[a08ba92]137                  case BranchStmt::Break:
[e39aa0f]138                                assert( targetEntry->useBreakExit() != "");
139                                exitLabel = targetEntry->useBreakExit();
[be5aa1b]140                                break;
[a08ba92]141                  case BranchStmt::Continue:
[e39aa0f]142                                assert( targetEntry->useContExit() != "");
143                                exitLabel = targetEntry->useContExit();
[be5aa1b]144                                break;
[a08ba92]145                  default:
[27de955]146                                assert(0);                                      // shouldn't be here
[a08ba92]147                } // switch
148
[33c4b81]149                // add unused attribute to label to silence warnings
[94e025a2]150                addUnused( targetEntry->get_controlStructure(), branchStmt->originalTarget );
[33c4b81]151
[af68f0a]152                // transform break/continue statements into goto to simplify later handling of branches
[ba3706f]153                return new BranchStmt( exitLabel, BranchStmt::Goto );
[a08ba92]154        }
155
156        Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
[d939274]157                // ensure loop body is a block
[a08ba92]158                CompoundStmt *newBody;
159                if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
[ba3706f]160                        newBody = new CompoundStmt();
[a08ba92]161                        newBody->get_kids().push_back( bodyLoop );
162                } // if
163
[27de955]164                // only generate these when needed
[a08ba92]165
[27de955]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() );
[e39aa0f]169                        newBody->get_kids().push_back( new NullStmt( labels ) );
[adcc065]170                } // if
[27de955]171
172                if ( e.isBreakUsed() ) {
[adcc065]173                        // break label goes after the loop -- it'll get set by the outer mutator if we do this
[23b6f4d7]174                        set_breakLabel( e.useBreakExit() );
[adcc065]175                } // if
[a08ba92]176
177                return newBody;
178        }
179
[94e025a2]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 );
[27de955]209        }
210
[94e025a2]211        Statement * MLEMutator::postmutate( WhileStmt * whileStmt ) {
212                return posthandleLoopStmt( whileStmt );
[27de955]213        }
214
[94e025a2]215        Statement * MLEMutator::postmutate( ForStmt * forStmt ) {
216                return posthandleLoopStmt( forStmt );
[adcc065]217        }
218
[94e025a2]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;
[a08ba92]275        }
[51b7345]276} // namespace ControlStruct
[a08ba92]277
[51587aa]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.