source: src/ControlStruct/LabelFixer.cc @ be5aa1b

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 be5aa1b was be5aa1b, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

error if continue statement targets a location that is not an enclosing loop, better error messages for branch statements with labels, formatting, refactoring

  • Property mode set to 100644
File size: 4.4 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
12// Last Modified On : Wed May 27 16:16:14 2015
13// Update Count     : 4
[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
25namespace ControlStruct {
[a08ba92]26        LabelFixer::Entry::Entry( Statement *to, Statement *from ) : definition ( to ) {
27                if ( from != 0 )
28                        usage.push_back( from );
29        }
[d9a0e76]30
[a08ba92]31        bool LabelFixer::Entry::insideLoop() {
32                return ( dynamic_cast< ForStmt * > ( definition ) ||
33                                 dynamic_cast< WhileStmt * > ( definition )  );
34        }
[d9a0e76]35
[a08ba92]36        LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) {
37                if ( generator == 0 )
38                        generator = LabelGenerator::getGenerator();
39        }
[51b7345]40
[a08ba92]41        void LabelFixer::visit( FunctionDecl *functionDecl ) {
[be5aa1b]42                maybeAccept( functionDecl->get_statements(), *this );
[d9a0e76]43
[a08ba92]44                MLEMutator mlemut( resolveJumps(), generator );
45                functionDecl->acceptMutator( mlemut );
46        }
[51b7345]47
[a08ba92]48        void LabelFixer::visit( Statement *stmt ) {
49                std::list< Label > &labels = stmt->get_labels();
[51b7345]50
[a08ba92]51                if ( ! labels.empty() ) {
52                        Label current = setLabelsDef( labels, stmt );
53                        labels.clear();
54                        labels.push_front( current );
55                } // if
56        }
[d9a0e76]57
[a08ba92]58        void LabelFixer::visit( BranchStmt *branchStmt ) {
59                visit ( ( Statement * )branchStmt );  // the labels this statement might have
[d9a0e76]60
[a08ba92]61                Label target;
62                if ( (target = branchStmt->get_target()) != "" ) {
63                        setLabelsUsg( target, branchStmt );
64                } //else       /* computed goto or normal exit-loop statements */
[d9a0e76]65        }
66
[a08ba92]67        Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) {
68                assert( definition != 0 );
69                Entry *entry = new Entry( definition );
70                bool used = false;
71
72                for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ )
73                        if ( labelTable.find( *i ) == labelTable.end() )
74                                { used = true; labelTable[ *i ] = entry; } // undefined and unused
75                        else
76                                if ( labelTable[ *i ]->defined() )
77                                        throw SemanticError( "Duplicate definition of label: " + *i );
78                                else
79                                        labelTable[ *i ]->set_definition( definition );
80
81                if ( ! used ) delete entry;
82
83                return labelTable[ llabel.front() ]->get_label();  // this *must* exist
84        }
85
86        Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use ) {
87                assert( use != 0 );
88
89                if ( labelTable.find( orgValue ) != labelTable.end() )
90                        labelTable[ orgValue ]->add_use( use );         // the label has been defined or used before
91                else
92                        labelTable[ orgValue ] = new Entry( 0, use );
93
94                return labelTable[ orgValue ]->get_label();
95        }
96
97        std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) {
98                std::map< Statement *, Entry * > def_us;
99
100                for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) {
101                        Entry *e = i->second;
102
[be5aa1b]103                        if ( def_us.find ( e->get_definition() ) == def_us.end() ) {
104                                def_us[ e->get_definition() ] = e;                             
105                        } else {
[a08ba92]106                                if ( e->used() )
107                                        def_us[ e->get_definition() ]->add_uses( e->get_uses() );
[be5aa1b]108                        }
[a08ba92]109                }
110
111                // get rid of labelTable
112                for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) {
113                        Statement *to = (*i).first;
114                        std::list< Statement *> &from = (*i).second->get_uses();
115                        Label finalLabel = generator->newLabel();
116                        (*i).second->set_label( finalLabel );
117
118                        if ( to == 0 ) {
119                                BranchStmt *first_use = dynamic_cast<BranchStmt *>(from.back());
120                                Label undef("");
121                                if ( first_use != 0 )
122                                        undef = first_use->get_target();
123                                throw SemanticError ( "'" + undef + "' label not defined");
124                        }
125
126                        to->get_labels().clear();
127                        to->get_labels().push_back( finalLabel );
128
129                        for ( std::list< Statement *>::iterator j = from.begin(); j != from.end(); j++ ) {
130                                BranchStmt *jumpTo = dynamic_cast< BranchStmt * > ( *j );
131                                assert( jumpTo != 0 );
132                                jumpTo->set_target( finalLabel );
133                        } // for
134                } // for
135
136                // reverse table
137                std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
138                for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) 
139                        (*ret)[ (*i).second->get_label() ] = (*i).first;
140
141                return ret;
142        }
[51b7345]143}  // namespace ControlStruct
[a08ba92]144
[51587aa]145// Local Variables: //
146// tab-width: 4 //
147// mode: c++ //
148// compile-command: "make install" //
149// End: //
Note: See TracBrowser for help on using the repository browser.