source: src/ControlStruct/LabelFixer.cc @ 6b6a3b8

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6b6a3b8 was 6b6a3b8, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Fixed a memory leak and some line length issues in setLabelsDef.

  • Property mode set to 100644
File size: 4.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// LabelFixer.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 : Mon Mar 11 22:26:02 2019
13// Update Count     : 159
14//
15
16#include <cassert>                         // for assert
17#include <list>                            // for list, _List_iterator, list...
18#include <string>                          // for operator+, string, operator==
19#include <utility>                         // for pair
20
21#include "ControlStruct/LabelGenerator.h"  // for LabelGenerator
22#include "LabelFixer.h"
23#include "MLEMutator.h"                    // for MLEMutator
24#include "SynTree/Declaration.h"           // for FunctionDecl
25#include "SynTree/Expression.h"            // for NameExpr, Expression, Unty...
26#include "SynTree/Statement.h"             // for Statement, BranchStmt, Com...
27
28namespace ControlStruct {
29        bool LabelFixer::Entry::insideLoop() {
30                return ( dynamic_cast< ForStmt * > ( definition ) ||
31                        dynamic_cast< WhileStmt * > ( definition )  );
32        }
33
34        LabelFixer::LabelFixer( LabelGenerator * gen ) : generator ( gen ) {
35                if ( generator == 0 )
36                        generator = LabelGenerator::getGenerator();
37        }
38
39        void LabelFixer::previsit( FunctionDecl * ) {
40                // need to go into a nested function in a fresh state
41                GuardValue( labelTable );
42                labelTable.clear();
43        }
44
45        void LabelFixer::postvisit( FunctionDecl * functionDecl ) {
46                PassVisitor<MLEMutator> mlemut( resolveJumps(), generator );
47                functionDecl->acceptMutator( mlemut );
48        }
49
50        // prune to at most one label definition for each statement
51        void LabelFixer::previsit( Statement * stmt ) {
52                std::list< Label > &labels = stmt->get_labels();
53
54                if ( ! labels.empty() ) {
55                        // only remember one label for each statement
56                        Label current = setLabelsDef( labels, stmt );
57                } // if
58        }
59
60        void LabelFixer::previsit( BranchStmt * branchStmt ) {
61                previsit( ( Statement *)branchStmt );
62
63                // for labeled branches, add an entry to the label table
64                Label target = branchStmt->get_target();
65                if ( target != "" ) {
66                        setLabelsUsg( target, branchStmt );
67                }
68        }
69
70        void LabelFixer::previsit( LabelAddressExpr * addrExpr ) {
71                Label & target = addrExpr->arg;
72                assert( target != "" );
73                setLabelsUsg( target, addrExpr );
74        }
75
76
77        // Sets the definition of the labelTable entry to be the provided statement for every label in
78        // the list parameter. Happens for every kind of statement.
79        Label LabelFixer::setLabelsDef( std::list< Label > & llabel, Statement * definition ) {
80                assert( definition != 0 );
81                assert( llabel.size() > 0 );
82
83                for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) {
84                        Label & l = *i;
85                        l.set_statement( definition ); // attach statement to the label to be used later
86                        if ( labelTable.find( l ) == labelTable.end() ) {
87                                // All labels on this statement need to use the same entry,
88                                // so this should only be created once.
89                                // undefined and unused until now, add an entry
90                                labelTable[ l ] = new Entry( definition );
91                        } else if ( labelTable[ l ]->defined() ) {
92                                // defined twice, error
93                                SemanticError( l.get_statement()->location,
94                                        "Duplicate definition of label: " + l.get_name() );
95                        } else {
96                                // used previously, but undefined until now -> link with this entry
97                                // Question: Is changing objects important?
98                                delete labelTable[ l ];
99                                labelTable[ l ] = new Entry( definition );
100                        } // if
101                } // for
102
103                // Produce one of the labels attached to this statement to be temporarily used as the
104                // canonical label.
105                return labelTable[ llabel.front() ]->get_label();
106        }
107
108        // A label was used, add it to the table if it isn't already there
109        template< typename UsageNode >
110        void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) {
111                assert( use != 0 );
112
113                // add label with an unknown origin
114                if ( labelTable.find( orgValue ) == labelTable.end() ) {
115                        labelTable[ orgValue ] = new Entry( 0 );
116                }
117        }
118
119        // Builds a table that maps a label to its defining statement.
120        std::map<Label, Statement * > * LabelFixer::resolveJumps() throw ( SemanticErrorException ) {
121                std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
122                for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
123                        if ( ! i->second->defined() ) {
124                                SemanticError( i->first.get_statement()->location, "Use of undefined label: " + i->first.get_name() );
125                        }
126                        (*ret)[ i->first ] = i->second->get_definition();
127                }
128
129                return ret;
130        }
131}  // namespace ControlStruct
132
133// Local Variables: //
134// tab-width: 4 //
135// mode: c++ //
136// compile-command: "make install" //
137// End: //
Note: See TracBrowser for help on using the repository browser.