[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 | // |
---|
[0f8e4ac] | 7 | // LabelFixer.cc -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[35a2d47] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Mon Mar 11 22:26:02 2019 |
---|
| 13 | // Update Count : 159 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[d180746] | 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 |
---|
[51b7345] | 20 | |
---|
[d180746] | 21 | #include "ControlStruct/LabelGenerator.h" // for LabelGenerator |
---|
[51b7345] | 22 | #include "LabelFixer.h" |
---|
[d180746] | 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... |
---|
[46cbfe1] | 27 | |
---|
[51b7345] | 28 | namespace ControlStruct { |
---|
[a08ba92] | 29 | bool LabelFixer::Entry::insideLoop() { |
---|
| 30 | return ( dynamic_cast< ForStmt * > ( definition ) || |
---|
[46cbfe1] | 31 | dynamic_cast< WhileStmt * > ( definition ) ); |
---|
[a08ba92] | 32 | } |
---|
[d9a0e76] | 33 | |
---|
[35a2d47] | 34 | LabelFixer::LabelFixer( LabelGenerator * gen ) : generator ( gen ) { |
---|
[a08ba92] | 35 | if ( generator == 0 ) |
---|
| 36 | generator = LabelGenerator::getGenerator(); |
---|
| 37 | } |
---|
[51b7345] | 38 | |
---|
[1fbeebd] | 39 | void LabelFixer::previsit( FunctionDecl * ) { |
---|
[0a0a65b] | 40 | // need to go into a nested function in a fresh state |
---|
[1fbeebd] | 41 | GuardValue( labelTable ); |
---|
[0a0a65b] | 42 | labelTable.clear(); |
---|
[1fbeebd] | 43 | } |
---|
[0a0a65b] | 44 | |
---|
[1fbeebd] | 45 | void LabelFixer::postvisit( FunctionDecl * functionDecl ) { |
---|
[94e025a2] | 46 | PassVisitor<MLEMutator> mlemut( resolveJumps(), generator ); |
---|
[a08ba92] | 47 | functionDecl->acceptMutator( mlemut ); |
---|
| 48 | } |
---|
[51b7345] | 49 | |
---|
[46cbfe1] | 50 | // prune to at most one label definition for each statement |
---|
[35a2d47] | 51 | void LabelFixer::previsit( Statement * stmt ) { |
---|
[a08ba92] | 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 | } // if |
---|
| 58 | } |
---|
[d9a0e76] | 59 | |
---|
[35a2d47] | 60 | void LabelFixer::previsit( BranchStmt * branchStmt ) { |
---|
[1fbeebd] | 61 | previsit( ( Statement *)branchStmt ); |
---|
[d9a0e76] | 62 | |
---|
[46cbfe1] | 63 | // for labeled branches, add an entry to the label table |
---|
| 64 | Label target = branchStmt->get_target(); |
---|
| 65 | if ( target != "" ) { |
---|
[a08ba92] | 66 | setLabelsUsg( target, branchStmt ); |
---|
[46cbfe1] | 67 | } |
---|
[d9a0e76] | 68 | } |
---|
| 69 | |
---|
[1fbeebd] | 70 | void LabelFixer::previsit( LabelAddressExpr * addrExpr ) { |
---|
| 71 | Label & target = addrExpr->arg; |
---|
| 72 | assert( target != "" ); |
---|
| 73 | setLabelsUsg( target, addrExpr ); |
---|
[1869adf] | 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
[35a2d47] | 77 | // sets the definition of the labelTable entry to be the provided statement for every label in the list |
---|
| 78 | // parameter. Happens for every kind of statement |
---|
| 79 | Label LabelFixer::setLabelsDef( std::list< Label > & llabel, Statement * definition ) { |
---|
[a08ba92] | 80 | assert( definition != 0 ); |
---|
[46cbfe1] | 81 | assert( llabel.size() > 0 ); |
---|
| 82 | |
---|
[954463b8] | 83 | Entry * e = new Entry( definition ); |
---|
| 84 | |
---|
[46cbfe1] | 85 | for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) { |
---|
[23b6f4d7] | 86 | Label & l = *i; |
---|
| 87 | l.set_statement( definition ); // attach statement to the label to be used later |
---|
| 88 | if ( labelTable.find( l ) == labelTable.end() ) { |
---|
[954463b8] | 89 | // all labels on this statement need to use the same entry, so this should only be created once |
---|
[46cbfe1] | 90 | // undefined and unused until now, add an entry |
---|
[23b6f4d7] | 91 | labelTable[ l ] = e; |
---|
| 92 | } else if ( labelTable[ l ]->defined() ) { |
---|
[46cbfe1] | 93 | // defined twice, error |
---|
[a16764a6] | 94 | SemanticError( l.get_statement()->location, "Duplicate definition of label: " + l.get_name() ); |
---|
[46cbfe1] | 95 | } else { |
---|
[954463b8] | 96 | // used previously, but undefined until now -> link with this entry |
---|
[23b6f4d7] | 97 | delete labelTable[ l ]; |
---|
| 98 | labelTable[ l ] = e; |
---|
[46cbfe1] | 99 | } // if |
---|
| 100 | } // for |
---|
[a08ba92] | 101 | |
---|
[35a2d47] | 102 | // produce one of the labels attached to this statement to be temporarily used as the canonical label |
---|
[46cbfe1] | 103 | return labelTable[ llabel.front() ]->get_label(); |
---|
[a08ba92] | 104 | } |
---|
| 105 | |
---|
[23b6f4d7] | 106 | // A label was used, add it to the table if it isn't already there |
---|
[1869adf] | 107 | template< typename UsageNode > |
---|
| 108 | void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) { |
---|
[a08ba92] | 109 | assert( use != 0 ); |
---|
| 110 | |
---|
[e766208] | 111 | // add label with an unknown origin |
---|
| 112 | if ( labelTable.find( orgValue ) == labelTable.end() ) { |
---|
| 113 | labelTable[ orgValue ] = new Entry( 0 ); |
---|
[46cbfe1] | 114 | } |
---|
[a08ba92] | 115 | } |
---|
| 116 | |
---|
[e766208] | 117 | // Builds a table that maps a label to its defining statement. |
---|
[35a2d47] | 118 | std::map<Label, Statement * > * LabelFixer::resolveJumps() throw ( SemanticErrorException ) { |
---|
[a08ba92] | 119 | std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); |
---|
[e766208] | 120 | for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) { |
---|
| 121 | if ( ! i->second->defined() ) { |
---|
[a16764a6] | 122 | SemanticError( i->first.get_statement()->location, "Use of undefined label: " + i->first.get_name() ); |
---|
[e766208] | 123 | } |
---|
| 124 | (*ret)[ i->first ] = i->second->get_definition(); |
---|
[46cbfe1] | 125 | } |
---|
[a08ba92] | 126 | |
---|
| 127 | return ret; |
---|
| 128 | } |
---|
[51b7345] | 129 | } // namespace ControlStruct |
---|
[a08ba92] | 130 | |
---|
[51587aa] | 131 | // Local Variables: // |
---|
| 132 | // tab-width: 4 // |
---|
| 133 | // mode: c++ // |
---|
| 134 | // compile-command: "make install" // |
---|
| 135 | // End: // |
---|