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 : Rob Schluntz |
---|
12 | // Last Modified On : Tue Jun 02 15:30:32 2015 |
---|
13 | // Update Count : 93 |
---|
14 | // |
---|
15 | |
---|
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 | |
---|
25 | #include <iostream> |
---|
26 | |
---|
27 | namespace ControlStruct { |
---|
28 | LabelFixer::Entry::Entry( Statement *to, BranchStmt *from ) : definition ( to ) { |
---|
29 | if ( from != 0 ) |
---|
30 | usage.push_back( from ); |
---|
31 | } |
---|
32 | |
---|
33 | bool LabelFixer::Entry::insideLoop() { |
---|
34 | return ( dynamic_cast< ForStmt * > ( definition ) || |
---|
35 | dynamic_cast< WhileStmt * > ( definition ) ); |
---|
36 | } |
---|
37 | |
---|
38 | LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) { |
---|
39 | if ( generator == 0 ) |
---|
40 | generator = LabelGenerator::getGenerator(); |
---|
41 | } |
---|
42 | |
---|
43 | void LabelFixer::visit( FunctionDecl *functionDecl ) { |
---|
44 | maybeAccept( functionDecl->get_statements(), *this ); |
---|
45 | |
---|
46 | MLEMutator mlemut( resolveJumps(), generator ); |
---|
47 | functionDecl->acceptMutator( mlemut ); |
---|
48 | } |
---|
49 | |
---|
50 | // prune to at most one label definition for each statement |
---|
51 | void LabelFixer::visit( 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 | labels.clear(); |
---|
58 | labels.push_front( current ); |
---|
59 | } // if |
---|
60 | } |
---|
61 | |
---|
62 | void LabelFixer::visit( BranchStmt *branchStmt ) { |
---|
63 | visit ( ( Statement * )branchStmt ); |
---|
64 | |
---|
65 | // for labeled branches, add an entry to the label table |
---|
66 | Label target = branchStmt->get_target(); |
---|
67 | if ( target != "" ) { |
---|
68 | setLabelsUsg( target, branchStmt ); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
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 |
---|
74 | Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { |
---|
75 | assert( definition != 0 ); |
---|
76 | assert( llabel.size() > 0 ); |
---|
77 | |
---|
78 | Entry * e = new Entry( definition ); |
---|
79 | |
---|
80 | for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) { |
---|
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 |
---|
83 | // undefined and unused until now, add an entry |
---|
84 | labelTable[ *i ] = e; |
---|
85 | } else if ( labelTable[ *i ]->defined() ) { |
---|
86 | // defined twice, error |
---|
87 | throw SemanticError( "Duplicate definition of label: " + *i ); |
---|
88 | } else { |
---|
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; |
---|
93 | } // if |
---|
94 | } // for |
---|
95 | |
---|
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(); |
---|
99 | } |
---|
100 | |
---|
101 | // Remember all uses of a label. |
---|
102 | void LabelFixer::setLabelsUsg( Label orgValue, BranchStmt *use ) { |
---|
103 | assert( use != 0 ); |
---|
104 | |
---|
105 | if ( labelTable.find( orgValue ) != labelTable.end() ) { |
---|
106 | // the label has been defined or used before |
---|
107 | labelTable[ orgValue ]->add_use( use ); |
---|
108 | } else { |
---|
109 | labelTable[ orgValue ] = new Entry( 0, use ); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | // Ultimately builds a table that maps a label to its defining statement. |
---|
114 | // In the process, |
---|
115 | std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) { |
---|
116 | std::map< Statement *, Entry * > def_us; |
---|
117 | |
---|
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 ) { |
---|
120 | Entry *e = i->second; |
---|
121 | |
---|
122 | if ( def_us.find ( e->get_definition() ) == def_us.end() ) { |
---|
123 | def_us[ e->get_definition() ] = e; |
---|
124 | } else if ( e->used() ) { |
---|
125 | def_us[ e->get_definition() ]->add_uses( e->get_uses() ); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
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 ) { |
---|
131 | Statement *to = (*i).first; |
---|
132 | Entry * entry = (*i).second; |
---|
133 | std::list< BranchStmt *> &from = entry->get_uses(); |
---|
134 | |
---|
135 | // no label definition found |
---|
136 | if ( to == 0 ) { |
---|
137 | Label undef = from.back()->get_target(); |
---|
138 | throw SemanticError ( "'" + undef + "' label not defined"); |
---|
139 | } |
---|
140 | |
---|
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 | |
---|
146 | to->get_labels().clear(); |
---|
147 | to->get_labels().push_back( finalLabel ); |
---|
148 | |
---|
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 ); |
---|
154 | } // for |
---|
155 | } // for |
---|
156 | |
---|
157 | // create a table where each label maps to its defining statement |
---|
158 | std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); |
---|
159 | for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); ++i ) { |
---|
160 | (*ret)[ (*i).second->get_label() ] = (*i).first; |
---|
161 | } |
---|
162 | |
---|
163 | return ret; |
---|
164 | } |
---|
165 | } // namespace ControlStruct |
---|
166 | |
---|
167 | // Local Variables: // |
---|
168 | // tab-width: 4 // |
---|
169 | // mode: c++ // |
---|
170 | // compile-command: "make install" // |
---|
171 | // End: // |
---|