| [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 | 
|---|
| [51ec1ab] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [3b0bc16] | 12 | // Last Modified On : Tue Feb  1 09:12:09 2022 | 
|---|
|  | 13 | // Update Count     : 162 | 
|---|
| [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 | 
|---|
| [51b73452] | 20 |  | 
|---|
| [d180746] | 21 | #include "ControlStruct/LabelGenerator.h"  // for LabelGenerator | 
|---|
| [51b73452] | 22 | #include "LabelFixer.h" | 
|---|
| [5cdeecd] | 23 | #include "MLEMutator.h"                    // for MultiLevelExitMutator | 
|---|
| [d180746] | 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 |  | 
|---|
| [51b73452] | 28 | namespace ControlStruct { | 
|---|
| [51ec1ab] | 29 | bool LabelFixer::Entry::insideLoop() { | 
|---|
|  | 30 | return ( dynamic_cast< ForStmt * > ( definition ) || | 
|---|
| [3b0bc16] | 31 | dynamic_cast< WhileDoStmt * > ( definition )  ); | 
|---|
| [51ec1ab] | 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<MultiLevelExitMutator> mlem( resolveJumps(), generator ); | 
|---|
|  | 47 | // We start in the body so we can stop when we hit another FunctionDecl. | 
|---|
|  | 48 | maybeMutate( functionDecl->statements, mlem ); | 
|---|
|  | 49 | } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | // prune to at most one label definition for each statement | 
|---|
|  | 52 | void LabelFixer::previsit( Statement * stmt ) { | 
|---|
|  | 53 | std::list< Label > &labels = stmt->get_labels(); | 
|---|
|  | 54 |  | 
|---|
|  | 55 | if ( ! labels.empty() ) { | 
|---|
|  | 56 | // only remember one label for each statement | 
|---|
|  | 57 | Label current = setLabelsDef( labels, stmt ); | 
|---|
|  | 58 | } // if | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 | void LabelFixer::previsit( BranchStmt * branchStmt ) { | 
|---|
|  | 62 | previsit( ( Statement *)branchStmt ); | 
|---|
|  | 63 |  | 
|---|
|  | 64 | // for labeled branches, add an entry to the label table | 
|---|
|  | 65 | Label target = branchStmt->get_target(); | 
|---|
|  | 66 | if ( target != "" ) { | 
|---|
|  | 67 | setLabelsUsg( target, branchStmt ); | 
|---|
| [a08ba92] | 68 | } | 
|---|
| [51ec1ab] | 69 | } | 
|---|
|  | 70 |  | 
|---|
|  | 71 | void LabelFixer::previsit( LabelAddressExpr * addrExpr ) { | 
|---|
|  | 72 | Label & target = addrExpr->arg; | 
|---|
|  | 73 | assert( target != "" ); | 
|---|
|  | 74 | setLabelsUsg( target, addrExpr ); | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
|  | 77 |  | 
|---|
|  | 78 | // Sets the definition of the labelTable entry to be the provided statement for every label in | 
|---|
|  | 79 | // the list parameter. Happens for every kind of statement. | 
|---|
|  | 80 | Label LabelFixer::setLabelsDef( std::list< Label > & llabel, Statement * definition ) { | 
|---|
|  | 81 | assert( definition != 0 ); | 
|---|
|  | 82 | assert( llabel.size() > 0 ); | 
|---|
|  | 83 |  | 
|---|
|  | 84 | for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) { | 
|---|
|  | 85 | Label & l = *i; | 
|---|
|  | 86 | l.set_statement( definition ); // attach statement to the label to be used later | 
|---|
|  | 87 | if ( labelTable.find( l ) == labelTable.end() ) { | 
|---|
|  | 88 | // All labels on this statement need to use the same entry, | 
|---|
|  | 89 | // so this should only be created once. | 
|---|
|  | 90 | // undefined and unused until now, add an entry | 
|---|
|  | 91 | labelTable[ l ] = new Entry( definition ); | 
|---|
|  | 92 | } else if ( labelTable[ l ]->defined() ) { | 
|---|
|  | 93 | // defined twice, error | 
|---|
|  | 94 | SemanticError( l.get_statement()->location, | 
|---|
|  | 95 | "Duplicate definition of label: " + l.get_name() ); | 
|---|
|  | 96 | } else { | 
|---|
|  | 97 | // used previously, but undefined until now -> link with this entry | 
|---|
|  | 98 | // Question: Is changing objects important? | 
|---|
|  | 99 | delete labelTable[ l ]; | 
|---|
|  | 100 | labelTable[ l ] = new Entry( definition ); | 
|---|
| [a08ba92] | 101 | } // if | 
|---|
| [51ec1ab] | 102 | } // for | 
|---|
| [d9a0e76] | 103 |  | 
|---|
| [51ec1ab] | 104 | // Produce one of the labels attached to this statement to be temporarily used as the | 
|---|
|  | 105 | // canonical label. | 
|---|
|  | 106 | return labelTable[ llabel.front() ]->get_label(); | 
|---|
|  | 107 | } | 
|---|
| [d9a0e76] | 108 |  | 
|---|
| [51ec1ab] | 109 | // A label was used, add it to the table if it isn't already there | 
|---|
|  | 110 | template< typename UsageNode > | 
|---|
|  | 111 | void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) { | 
|---|
|  | 112 | assert( use != 0 ); | 
|---|
| [d9a0e76] | 113 |  | 
|---|
| [51ec1ab] | 114 | // add label with an unknown origin | 
|---|
|  | 115 | if ( labelTable.find( orgValue ) == labelTable.end() ) { | 
|---|
|  | 116 | labelTable[ orgValue ] = new Entry( 0 ); | 
|---|
| [1869adf] | 117 | } | 
|---|
| [51ec1ab] | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | // Builds a table that maps a label to its defining statement. | 
|---|
| [f8965f4] | 121 | std::map<Label, Statement * > * LabelFixer::resolveJumps() { | 
|---|
| [51ec1ab] | 122 | std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); | 
|---|
|  | 123 | for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) { | 
|---|
|  | 124 | if ( ! i->second->defined() ) { | 
|---|
|  | 125 | SemanticError( i->first.get_statement()->location, "Use of undefined label: " + i->first.get_name() ); | 
|---|
| [46cbfe1] | 126 | } | 
|---|
| [51ec1ab] | 127 | (*ret)[ i->first ] = i->second->get_definition(); | 
|---|
| [a08ba92] | 128 | } | 
|---|
|  | 129 |  | 
|---|
| [51ec1ab] | 130 | return ret; | 
|---|
|  | 131 | } | 
|---|
| [51b73452] | 132 | }  // namespace ControlStruct | 
|---|
| [a08ba92] | 133 |  | 
|---|
| [51587aa] | 134 | // Local Variables: // | 
|---|
|  | 135 | // tab-width: 4 // | 
|---|
|  | 136 | // mode: c++ // | 
|---|
|  | 137 | // compile-command: "make install" // | 
|---|
|  | 138 | // End: // | 
|---|