source: src/ControlStruct/LabelFixer.cc @ 44b5ca0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 44b5ca0 was 954463b8, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fix label forward referencesC

  • Property mode set to 100644
File size: 5.3 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//
[a08ba92]7// LabelFixer.cc --
[51587aa]8//
[843054c2]9// Author           : Rodolfo G. Esteves
[51587aa]10// Created On       : Mon May 18 07:44:20 2015
[be5aa1b]11// Last Modified By : Rob Schluntz
[954463b8]12// Last Modified On : Tue Jun 02 15:30:32 2015
13// Update Count     : 93
[51587aa]14//
[a08ba92]15
[51b7345]16#include <list>
17#include <cassert>
18
19#include "LabelFixer.h"
20#include "MLEMutator.h"
21#include "SynTree/Statement.h"
22#include "SynTree/Declaration.h"
23#include "utility.h"
24
[46cbfe1]25#include <iostream>
26
[51b7345]27namespace ControlStruct {
[46cbfe1]28        LabelFixer::Entry::Entry( Statement *to, BranchStmt *from ) : definition ( to ) {
[a08ba92]29                if ( from != 0 )
30                        usage.push_back( from );
31        }
[d9a0e76]32
[a08ba92]33        bool LabelFixer::Entry::insideLoop() {
34                return ( dynamic_cast< ForStmt * > ( definition ) ||
[46cbfe1]35                        dynamic_cast< WhileStmt * > ( definition )  );
[a08ba92]36        }
[d9a0e76]37
[a08ba92]38        LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) {
39                if ( generator == 0 )
40                        generator = LabelGenerator::getGenerator();
41        }
[51b7345]42
[a08ba92]43        void LabelFixer::visit( FunctionDecl *functionDecl ) {
[be5aa1b]44                maybeAccept( functionDecl->get_statements(), *this );
[d9a0e76]45
[a08ba92]46                MLEMutator mlemut( resolveJumps(), generator );
47                functionDecl->acceptMutator( mlemut );
48        }
[51b7345]49
[46cbfe1]50        // prune to at most one label definition for each statement
[a08ba92]51        void LabelFixer::visit( Statement *stmt ) {
52                std::list< Label > &labels = stmt->get_labels();
[51b7345]53
[a08ba92]54                if ( ! labels.empty() ) {
[46cbfe1]55                        // only remember one label for each statement
[a08ba92]56                        Label current = setLabelsDef( labels, stmt );
57                        labels.clear();
58                        labels.push_front( current );
59                } // if
60        }
[d9a0e76]61
[a08ba92]62        void LabelFixer::visit( BranchStmt *branchStmt ) {
[46cbfe1]63                visit ( ( Statement * )branchStmt );
[d9a0e76]64
[46cbfe1]65                // for labeled branches, add an entry to the label table
66                Label target = branchStmt->get_target();
67                if ( target != "" ) {
[a08ba92]68                        setLabelsUsg( target, branchStmt );
[46cbfe1]69                }
[d9a0e76]70        }
71
[46cbfe1]72        // sets the definition of the labelTable entry to be the provided
73        // statement for every label in the list parameter. Happens for every kind of statement
[a08ba92]74        Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) {
75                assert( definition != 0 );
[46cbfe1]76                assert( llabel.size() > 0 );
77
[954463b8]78                Entry * e = new Entry( definition );
79
[46cbfe1]80                for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) {
[954463b8]81                        if ( labelTable.find( *i ) == labelTable.end() ) {
82                                // all labels on this statement need to use the same entry, so this should only be created once
[46cbfe1]83                                // undefined and unused until now, add an entry
[954463b8]84                                labelTable[ *i ] =  e;
85                        } else if ( labelTable[ *i ]->defined() ) {
[46cbfe1]86                                // defined twice, error
87                                throw SemanticError( "Duplicate definition of label: " + *i );
88                        }       else {
[954463b8]89                                // used previously, but undefined until now -> link with this entry
90                                Entry * oldEntry = labelTable[ *i ];
91                                e->add_uses( oldEntry->get_uses() );
92                                labelTable[ *i ] = e;
[46cbfe1]93                        } // if
94                } // for
[a08ba92]95
[46cbfe1]96                // produce one of the labels attached to this statement to be
97                // temporarily used as the canonical label
98                return labelTable[ llabel.front() ]->get_label();
[a08ba92]99        }
100
[46cbfe1]101        // Remember all uses of a label.
102        void LabelFixer::setLabelsUsg( Label orgValue, BranchStmt *use ) {
[a08ba92]103                assert( use != 0 );
104
[46cbfe1]105                if ( labelTable.find( orgValue ) != labelTable.end() ) {
106                        // the label has been defined or used before
107                        labelTable[ orgValue ]->add_use( use );
108                } else {
[a08ba92]109                        labelTable[ orgValue ] = new Entry( 0, use );
[46cbfe1]110                }
[a08ba92]111        }
112
[46cbfe1]113        // Ultimately builds a table that maps a label to its defining statement.
114        // In the process,
[a08ba92]115        std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) {
116                std::map< Statement *, Entry * > def_us;
117
[46cbfe1]118                // combine the entries for all labels that target the same location
119                for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
[a08ba92]120                        Entry *e = i->second;
121
[be5aa1b]122                        if ( def_us.find ( e->get_definition() ) == def_us.end() ) {
123                                def_us[ e->get_definition() ] = e;                             
[46cbfe1]124                        } else if ( e->used() ) {
125                                def_us[ e->get_definition() ]->add_uses( e->get_uses() );
[be5aa1b]126                        }
[a08ba92]127                }
128
[46cbfe1]129                // create a unique label for each target location.
130                for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); ++i ) {
[a08ba92]131                        Statement *to = (*i).first;
[46cbfe1]132                        Entry * entry = (*i).second;
133                        std::list< BranchStmt *> &from = entry->get_uses();
[a08ba92]134
[46cbfe1]135                        // no label definition found
[a08ba92]136                        if ( to == 0 ) {
[46cbfe1]137                                Label undef = from.back()->get_target();
[a08ba92]138                                throw SemanticError ( "'" + undef + "' label not defined");
139                        }
140
[46cbfe1]141                        // generate a new label, and attach it to its defining statement
142                        // as the only label on that statement
143                        Label finalLabel = generator->newLabel();
144                        entry->set_label( finalLabel );
145
[a08ba92]146                        to->get_labels().clear();
147                        to->get_labels().push_back( finalLabel );
148
[46cbfe1]149                        // redirect each of the source branch statements to the new target label
150                        for ( std::list< BranchStmt *>::iterator j = from.begin(); j != from.end(); ++j ) {
151                                BranchStmt *jump = *j;
152                                assert( jump != 0 );
153                                jump->set_target( finalLabel );
[a08ba92]154                        } // for
155                } // for
156
[46cbfe1]157                // create a table where each label maps to its defining statement
[a08ba92]158                std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
[46cbfe1]159                for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); ++i ) {
[a08ba92]160                        (*ret)[ (*i).second->get_label() ] = (*i).first;
[46cbfe1]161                }
[a08ba92]162
163                return ret;
164        }
[51b7345]165}  // namespace ControlStruct
[a08ba92]166
[51587aa]167// Local Variables: //
168// tab-width: 4 //
169// mode: c++ //
170// compile-command: "make install" //
171// End: //
Note: See TracBrowser for help on using the repository browser.