source: src/ControlStruct/MLEMutator.cc @ d180746

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 d180746 was d180746, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Big header cleaning pass - commit 2

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